blob: c2a8b569cded02e691040e7984f6cda745513120 [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
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000014#include "Hexagon.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000015#include "HexagonHazardRecognizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonInstrInfo.h"
Craig Topperb25fda92012-03-17 18:46:09 +000017#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "HexagonSubtarget.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000019#include "llvm/ADT/SmallPtrSet.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000021#include "llvm/ADT/StringRef.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000022#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000023#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
31#include "llvm/CodeGen/MachineLoopInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000033#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000035#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000036#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000037#include "llvm/MC/MCInstrDesc.h"
38#include "llvm/MC/MCInstrItineraries.h"
39#include "llvm/MC/MCRegisterInfo.h"
40#include "llvm/Support/BranchProbability.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000041#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000042#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000043#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000044#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000045#include "llvm/Support/raw_ostream.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetSubtargetInfo.h"
48#include <cassert>
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000049#include <cctype>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000050#include <cstdint>
51#include <cstring>
52#include <iterator>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054using namespace llvm;
55
Chandler Carruthe96dd892014-04-21 22:55:11 +000056#define DEBUG_TYPE "hexagon-instrinfo"
57
Chandler Carruthd174b722014-04-22 02:03:14 +000058#define GET_INSTRINFO_CTOR_DTOR
59#define GET_INSTRMAP_INFO
60#include "HexagonGenInstrInfo.inc"
61#include "HexagonGenDFAPacketizer.inc"
62
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000063cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000064 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
65 "packetization boundary."));
66
67static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
68 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
69
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000070static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
71 cl::Hidden, cl::ZeroOrMore, cl::init(false),
72 cl::desc("Disable schedule adjustment for new value stores."));
73
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000074static cl::opt<bool> EnableTimingClassLatency(
75 "enable-timing-class-latency", cl::Hidden, cl::init(false),
76 cl::desc("Enable timing class latency"));
77
78static cl::opt<bool> EnableALUForwarding(
79 "enable-alu-forwarding", cl::Hidden, cl::init(true),
80 cl::desc("Enable vec alu forwarding"));
81
82static cl::opt<bool> EnableACCForwarding(
83 "enable-acc-forwarding", cl::Hidden, cl::init(true),
84 cl::desc("Enable vec acc forwarding"));
85
86static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
87 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
88
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000089static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
90 cl::init(true), cl::Hidden, cl::ZeroOrMore,
91 cl::desc("Use the DFA based hazard recognizer."));
92
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093///
94/// Constants for Hexagon instructions.
95///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000096const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
97const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
98const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
99const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000100const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000101const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000103const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000105const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000107const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000108const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000109const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000111const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000112const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000113const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000115const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000116const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000117const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +0000118const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
119const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
120const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
121const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000122
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000123// Pin the vtable to this file.
124void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000125
126HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000127 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000128 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000130static bool isIntRegForSubInst(unsigned Reg) {
131 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
132 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000133}
134
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000135static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000136 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
137 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000138}
139
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000140/// Calculate number of instructions excluding the debug instructions.
141static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
142 MachineBasicBlock::const_instr_iterator MIE) {
143 unsigned Count = 0;
144 for (; MIB != MIE; ++MIB) {
145 if (!MIB->isDebugValue())
146 ++Count;
147 }
148 return Count;
149}
150
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000151/// Find the hardware loop instruction used to set-up the specified loop.
152/// On Hexagon, we have two instructions used to set-up the hardware loop
153/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
154/// to indicate the end of a loop.
155static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
156 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000157 int LOOPi;
158 int LOOPr;
159 if (EndLoopOp == Hexagon::ENDLOOP0) {
160 LOOPi = Hexagon::J2_loop0i;
161 LOOPr = Hexagon::J2_loop0r;
162 } else { // EndLoopOp == Hexagon::EndLOOP1
163 LOOPi = Hexagon::J2_loop1i;
164 LOOPr = Hexagon::J2_loop1r;
165 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000166
Brendon Cahoondf43e682015-05-08 16:16:29 +0000167 // The loop set-up instruction will be in a predecessor block
168 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
169 PE = BB->pred_end(); PB != PE; ++PB) {
170 // If this has been visited, already skip it.
171 if (!Visited.insert(*PB).second)
172 continue;
173 if (*PB == BB)
174 continue;
175 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
176 E = (*PB)->instr_rend(); I != E; ++I) {
177 int Opc = I->getOpcode();
178 if (Opc == LOOPi || Opc == LOOPr)
179 return &*I;
180 // We've reached a different loop, which means the loop0 has been removed.
181 if (Opc == EndLoopOp)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000182 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000183 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000184 // Check the predecessors for the LOOP instruction.
185 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
186 if (loop)
187 return loop;
188 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000189 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000190}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000191
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000192/// Gather register def/uses from MI.
193/// This treats possible (predicated) defs as actually happening ones
194/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000195static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000196 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
197 Defs.clear();
198 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000199
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000200 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
201 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000202
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000203 if (!MO.isReg())
204 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000205
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000206 unsigned Reg = MO.getReg();
207 if (!Reg)
208 continue;
209
210 if (MO.isUse())
211 Uses.push_back(MO.getReg());
212
213 if (MO.isDef())
214 Defs.push_back(MO.getReg());
215 }
216}
217
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000218// Position dependent, so check twice for swap.
219static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
220 switch (Ga) {
221 case HexagonII::HSIG_None:
222 default:
223 return false;
224 case HexagonII::HSIG_L1:
225 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
226 case HexagonII::HSIG_L2:
227 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
228 Gb == HexagonII::HSIG_A);
229 case HexagonII::HSIG_S1:
230 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
231 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
232 case HexagonII::HSIG_S2:
233 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
234 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
235 Gb == HexagonII::HSIG_A);
236 case HexagonII::HSIG_A:
237 return (Gb == HexagonII::HSIG_A);
238 case HexagonII::HSIG_Compound:
239 return (Gb == HexagonII::HSIG_Compound);
240 }
241 return false;
242}
243
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000244/// isLoadFromStackSlot - If the specified machine instruction is a direct
245/// load from a stack slot, return the virtual or physical register number of
246/// the destination along with the FrameIndex of the loaded stack slot. If
247/// not, return 0. This predicate must return 0 if the instruction has
248/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000249unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000250 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000251 switch (MI.getOpcode()) {
252 default:
253 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000254 case Hexagon::L2_loadri_io:
255 case Hexagon::L2_loadrd_io:
256 case Hexagon::V6_vL32b_ai:
257 case Hexagon::V6_vL32b_ai_128B:
258 case Hexagon::V6_vL32Ub_ai:
259 case Hexagon::V6_vL32Ub_ai_128B:
260 case Hexagon::LDriw_pred:
261 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000262 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000263 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000264 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000265 case Hexagon::PS_vloadrw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000266 const MachineOperand OpFI = MI.getOperand(1);
267 if (!OpFI.isFI())
268 return 0;
269 const MachineOperand OpOff = MI.getOperand(2);
270 if (!OpOff.isImm() || OpOff.getImm() != 0)
271 return 0;
272 FrameIndex = OpFI.getIndex();
273 return MI.getOperand(0).getReg();
274 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000275
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000276 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/// isStoreToStackSlot - If the specified machine instruction is a direct
295/// store to a stack slot, return the virtual or physical register number of
296/// the source reg along with the FrameIndex of the loaded stack slot. If
297/// not, return 0. This predicate must return 0 if the instruction has
298/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000299unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000300 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000301 switch (MI.getOpcode()) {
302 default:
303 break;
304 case Hexagon::S2_storerb_io:
305 case Hexagon::S2_storerh_io:
306 case Hexagon::S2_storeri_io:
307 case Hexagon::S2_storerd_io:
308 case Hexagon::V6_vS32b_ai:
309 case Hexagon::V6_vS32b_ai_128B:
310 case Hexagon::V6_vS32Ub_ai:
311 case Hexagon::V6_vS32Ub_ai_128B:
312 case Hexagon::STriw_pred:
313 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000314 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000315 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000316 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000317 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000318 const MachineOperand &OpFI = MI.getOperand(0);
319 if (!OpFI.isFI())
320 return 0;
321 const MachineOperand &OpOff = MI.getOperand(1);
322 if (!OpOff.isImm() || OpOff.getImm() != 0)
323 return 0;
324 FrameIndex = OpFI.getIndex();
325 return MI.getOperand(2).getReg();
326 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000327
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000328 case Hexagon::S2_pstorerbt_io:
329 case Hexagon::S2_pstorerbf_io:
330 case Hexagon::S2_pstorerht_io:
331 case Hexagon::S2_pstorerhf_io:
332 case Hexagon::S2_pstorerit_io:
333 case Hexagon::S2_pstorerif_io:
334 case Hexagon::S2_pstorerdt_io:
335 case Hexagon::S2_pstorerdf_io: {
336 const MachineOperand &OpFI = MI.getOperand(1);
337 if (!OpFI.isFI())
338 return 0;
339 const MachineOperand &OpOff = MI.getOperand(2);
340 if (!OpOff.isImm() || OpOff.getImm() != 0)
341 return 0;
342 FrameIndex = OpFI.getIndex();
343 return MI.getOperand(3).getReg();
344 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000345 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000346
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000347 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000348}
349
Brendon Cahoondf43e682015-05-08 16:16:29 +0000350/// This function can analyze one/two way branching only and should (mostly) be
351/// called by target independent side.
352/// First entry is always the opcode of the branching instruction, except when
353/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
354/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
355/// e.g. Jump_c p will have
356/// Cond[0] = Jump_c
357/// Cond[1] = p
358/// HW-loop ENDLOOP:
359/// Cond[0] = ENDLOOP
360/// Cond[1] = MBB
361/// New value jump:
362/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
363/// Cond[1] = R
364/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000365///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000366bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000367 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000368 MachineBasicBlock *&FBB,
369 SmallVectorImpl<MachineOperand> &Cond,
370 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000371 TBB = nullptr;
372 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000373 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000374
375 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000376 MachineBasicBlock::instr_iterator I = MBB.instr_end();
377 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000378 return false;
379
380 // A basic block may looks like this:
381 //
382 // [ insn
383 // EH_LABEL
384 // insn
385 // insn
386 // insn
387 // EH_LABEL
388 // insn ]
389 //
390 // It has two succs but does not have a terminator
391 // Don't know how to handle it.
392 do {
393 --I;
394 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000395 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000397 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000399 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000400 --I;
401
402 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000403 if (I == MBB.instr_begin())
404 return false;
405 --I;
406 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000407
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000408 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
409 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000410 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000411 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000412 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000413 DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000414 I->eraseFromParent();
415 I = MBB.instr_end();
416 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417 return false;
418 --I;
419 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000420 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000421 return false;
422
423 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000424 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000425 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000426 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000427 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000428 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000429 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000430 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000431 else
432 // This is a third branch.
433 return true;
434 }
435 if (I == MBB.instr_begin())
436 break;
437 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000438 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000439
440 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000441 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
442 // If the branch target is not a basic block, it could be a tail call.
443 // (It is, if the target is a function.)
444 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
445 return true;
446 if (SecLastOpcode == Hexagon::J2_jump &&
447 !SecondLastInst->getOperand(0).isMBB())
448 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000449
450 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000451 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000452
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000453 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
454 return true;
455
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000456 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000457 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000458 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 TBB = LastInst->getOperand(0).getMBB();
460 return false;
461 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000462 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000463 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000464 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465 Cond.push_back(LastInst->getOperand(0));
466 return false;
467 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000468 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000469 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000470 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000471 Cond.push_back(LastInst->getOperand(0));
472 return false;
473 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000474 // Only supporting rr/ri versions of new-value jumps.
475 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
476 TBB = LastInst->getOperand(2).getMBB();
477 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
478 Cond.push_back(LastInst->getOperand(0));
479 Cond.push_back(LastInst->getOperand(1));
480 return false;
481 }
482 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
483 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000484 // Otherwise, don't know what this is.
485 return true;
486 }
487
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000488 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000489 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000490 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000491 if (!SecondLastInst->getOperand(1).isMBB())
492 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000493 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000494 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 Cond.push_back(SecondLastInst->getOperand(0));
496 FBB = LastInst->getOperand(0).getMBB();
497 return false;
498 }
499
Brendon Cahoondf43e682015-05-08 16:16:29 +0000500 // Only supporting rr/ri versions of new-value jumps.
501 if (SecLastOpcodeHasNVJump &&
502 (SecondLastInst->getNumExplicitOperands() == 3) &&
503 (LastOpcode == Hexagon::J2_jump)) {
504 TBB = SecondLastInst->getOperand(2).getMBB();
505 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
506 Cond.push_back(SecondLastInst->getOperand(0));
507 Cond.push_back(SecondLastInst->getOperand(1));
508 FBB = LastInst->getOperand(0).getMBB();
509 return false;
510 }
511
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000512 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
513 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000514 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000515 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000516 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000517 if (AllowModify)
518 I->eraseFromParent();
519 return false;
520 }
521
Brendon Cahoondf43e682015-05-08 16:16:29 +0000522 // If the block ends with an ENDLOOP, and J2_jump, handle it.
523 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000524 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000525 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000526 Cond.push_back(SecondLastInst->getOperand(0));
527 FBB = LastInst->getOperand(0).getMBB();
528 return false;
529 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000530 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
531 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000532 // Otherwise, can't handle this.
533 return true;
534}
535
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000536unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000537 int *BytesRemoved) const {
538 assert(!BytesRemoved && "code size not handled");
539
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
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000559unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000560 MachineBasicBlock *TBB,
561 MachineBasicBlock *FBB,
562 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000563 const DebugLoc &DL,
564 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000565 unsigned BOpc = Hexagon::J2_jump;
566 unsigned BccOpc = Hexagon::J2_jumpt;
567 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000568 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000569 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000570
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000571 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000572 // If we want to reverse the branch an odd number of times, we want
573 // J2_jumpf.
574 if (!Cond.empty() && Cond[0].isImm())
575 BccOpc = Cond[0].getImm();
576
577 if (!FBB) {
578 if (Cond.empty()) {
579 // Due to a bug in TailMerging/CFG Optimization, we need to add a
580 // special case handling of a predicated jump followed by an
581 // unconditional jump. If not, Tail Merging and CFG Optimization go
582 // into an infinite loop.
583 MachineBasicBlock *NewTBB, *NewFBB;
584 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000585 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000586 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000587 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
588 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000589 reverseBranchCondition(Cond);
590 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000591 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000592 }
593 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
594 } else if (isEndLoopN(Cond[0].getImm())) {
595 int EndLoopOp = Cond[0].getImm();
596 assert(Cond[1].isMBB());
597 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
598 // Check for it, and change the BB target if needed.
599 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
600 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
601 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
602 Loop->getOperand(0).setMBB(TBB);
603 // Add the ENDLOOP after the finding the LOOP0.
604 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
605 } else if (isNewValueJump(Cond[0].getImm())) {
606 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
607 // New value jump
608 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
609 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
610 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
611 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
612 if (Cond[2].isReg()) {
613 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
614 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
615 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
616 } else if(Cond[2].isImm()) {
617 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
618 addImm(Cond[2].getImm()).addMBB(TBB);
619 } else
620 llvm_unreachable("Invalid condition for branching");
621 } else {
622 assert((Cond.size() == 2) && "Malformed cond vector");
623 const MachineOperand &RO = Cond[1];
624 unsigned Flags = getUndefRegState(RO.isUndef());
625 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
626 }
627 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000628 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000629 assert((!Cond.empty()) &&
630 "Cond. cannot be empty when multiple branchings are required");
631 assert((!isNewValueJump(Cond[0].getImm())) &&
632 "NV-jump cannot be inserted with another branch");
633 // Special case for hardware loops. The condition is a basic block.
634 if (isEndLoopN(Cond[0].getImm())) {
635 int EndLoopOp = Cond[0].getImm();
636 assert(Cond[1].isMBB());
637 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
638 // Check for it, and change the BB target if needed.
639 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
640 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
641 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
642 Loop->getOperand(0).setMBB(TBB);
643 // Add the ENDLOOP after the finding the LOOP0.
644 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
645 } else {
646 const MachineOperand &RO = Cond[1];
647 unsigned Flags = getUndefRegState(RO.isUndef());
648 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000649 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000650 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000651
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000652 return 2;
653}
654
Brendon Cahoon254f8892016-07-29 16:44:44 +0000655/// Analyze the loop code to find the loop induction variable and compare used
656/// to compute the number of iterations. Currently, we analyze loop that are
657/// controlled using hardware loops. In this case, the induction variable
658/// instruction is null. For all other cases, this function returns true, which
659/// means we're unable to analyze it.
660bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
661 MachineInstr *&IndVarInst,
662 MachineInstr *&CmpInst) const {
663
664 MachineBasicBlock *LoopEnd = L.getBottomBlock();
665 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
666 // We really "analyze" only hardware loops right now.
667 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
668 IndVarInst = nullptr;
669 CmpInst = &*I;
670 return false;
671 }
672 return true;
673}
674
675/// Generate code to reduce the loop iteration by one and check if the loop is
676/// finished. Return the value/register of the new loop count. this function
677/// assumes the nth iteration is peeled first.
678unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000679 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000680 SmallVectorImpl<MachineOperand> &Cond,
681 SmallVectorImpl<MachineInstr *> &PrevInsts,
682 unsigned Iter, unsigned MaxIter) const {
683 // We expect a hardware loop currently. This means that IndVar is set
684 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000685 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000686 && "Expecting a hardware loop");
687 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000688 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000689 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000690 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000691 if (!Loop)
692 return 0;
693 // If the loop trip count is a compile-time value, then just change the
694 // value.
695 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
696 Loop->getOpcode() == Hexagon::J2_loop1i) {
697 int64_t Offset = Loop->getOperand(1).getImm();
698 if (Offset <= 1)
699 Loop->eraseFromParent();
700 else
701 Loop->getOperand(1).setImm(Offset - 1);
702 return Offset - 1;
703 }
704 // The loop trip count is a run-time value. We generate code to subtract
705 // one from the trip count, and update the loop instruction.
706 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
707 unsigned LoopCount = Loop->getOperand(1).getReg();
708 // Check if we're done with the loop.
709 unsigned LoopEnd = createVR(MF, MVT::i1);
710 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
711 addReg(LoopCount).addImm(1);
712 unsigned NewLoopCount = createVR(MF, MVT::i32);
713 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
714 addReg(LoopCount).addImm(-1);
715 // Update the previously generated instructions with the new loop counter.
716 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
717 E = PrevInsts.end(); I != E; ++I)
718 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
719 PrevInsts.clear();
720 PrevInsts.push_back(NewCmp);
721 PrevInsts.push_back(NewAdd);
722 // Insert the new loop instruction if this is the last time the loop is
723 // decremented.
724 if (Iter == MaxIter)
725 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
726 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
727 // Delete the old loop instruction.
728 if (Iter == 0)
729 Loop->eraseFromParent();
730 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
731 Cond.push_back(NewCmp->getOperand(0));
732 return NewLoopCount;
733}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000734
735bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
736 unsigned NumCycles, unsigned ExtraPredCycles,
737 BranchProbability Probability) const {
738 return nonDbgBBSize(&MBB) <= 3;
739}
740
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000741bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
742 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
743 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
744 const {
745 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
746}
747
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000748bool 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 Parzyszeka5409972016-11-09 16:19:08 +0000818 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
819 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000820 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000821 .addReg(HiSrc, KillFlag)
822 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000823 return;
824 }
825 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000826 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
827 .addReg(SrcReg)
828 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000829 return;
830 }
831 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000832 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000833 llvm_unreachable("Unimplemented pred to vec");
834 return;
835 }
836 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
837 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
838 llvm_unreachable("Unimplemented vec to pred");
839 return;
840 }
841 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000842 unsigned HiDst = HRI.getSubReg(DestReg, Hexagon::vsub_hi);
843 unsigned LoDst = HRI.getSubReg(DestReg, Hexagon::vsub_lo);
844 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
845 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
846 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), HiDst)
847 .addReg(HiSrc, KillFlag);
848 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), LoDst)
849 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000850 return;
851 }
Sirish Pande30804c22012-02-15 18:52:27 +0000852
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000853#ifndef NDEBUG
854 // Show the invalid registers to ease debugging.
855 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
856 << ": " << PrintReg(DestReg, &HRI)
857 << " = " << PrintReg(SrcReg, &HRI) << '\n';
858#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000859 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000860}
861
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000862void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
863 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
864 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000865 DebugLoc DL = MBB.findDebugLoc(I);
866 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000867 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000868 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000869 unsigned KillFlag = getKillRegState(isKill);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000870
Alex Lorenze40c8a22015-08-11 23:09:45 +0000871 MachineMemOperand *MMO = MF.getMachineMemOperand(
872 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
873 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000874
Craig Topperc7242e02012-04-20 07:30:17 +0000875 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000876 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000877 .addFrameIndex(FI).addImm(0)
878 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000879 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000880 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000881 .addFrameIndex(FI).addImm(0)
882 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000883 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000884 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000885 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000886 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000887 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
888 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
889 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000890 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
891 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000892 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai_128B))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000893 .addFrameIndex(FI).addImm(0)
894 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
895 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000896 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000897 .addFrameIndex(FI).addImm(0)
898 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
899 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000900 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
901 : Hexagon::V6_vS32b_ai_128B;
902 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000903 .addFrameIndex(FI).addImm(0)
904 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
905 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000906 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
907 : Hexagon::V6_vS32b_ai;
908 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000909 .addFrameIndex(FI).addImm(0)
910 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
911 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000912 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
913 : Hexagon::PS_vstorerw_ai;
914 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000915 .addFrameIndex(FI).addImm(0)
916 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
917 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000918 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
919 : Hexagon::PS_vstorerw_ai_128B;
920 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000921 .addFrameIndex(FI).addImm(0)
922 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000923 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000924 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000925 }
926}
927
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000928void HexagonInstrInfo::loadRegFromStackSlot(
929 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
930 int FI, const TargetRegisterClass *RC,
931 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000932 DebugLoc DL = MBB.findDebugLoc(I);
933 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000934 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000935 unsigned Align = MFI.getObjectAlignment(FI);
936
Alex Lorenze40c8a22015-08-11 23:09:45 +0000937 MachineMemOperand *MMO = MF.getMachineMemOperand(
938 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
939 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000940
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000941 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000942 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_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::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000945 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000946 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000947 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000948 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000949 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
950 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
951 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
952 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000953 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000954 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000955 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
956 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000957 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000958 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
959 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000960 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
961 : Hexagon::PS_vloadrw_ai_128B;
962 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000963 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
964 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000965 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
966 : Hexagon::V6_vL32b_ai_128B;
967 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000968 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
969 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000970 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
971 : Hexagon::V6_vL32b_ai;
972 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000973 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
974 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000975 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
976 : Hexagon::PS_vloadrw_ai;
977 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000978 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000980 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000981 }
982}
983
Ron Lieberman88159e52016-09-02 22:56:24 +0000984static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
985 const MachineBasicBlock &B = *MI.getParent();
986 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000987 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +0000988 for (auto I = B.rbegin(); I != E; ++I)
989 Regs.stepBackward(*I);
990}
991
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000992/// expandPostRAPseudo - This function is called for all pseudo instructions
993/// that remain after register allocation. Many pseudo instructions are
994/// created to help register allocation. This is the place to convert them
995/// into real instructions. The target can edit MI in place, or it can insert
996/// new instructions and erase MI. The function should return true if
997/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000998bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000999 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001000 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1001 MachineBasicBlock &MBB = *MI.getParent();
1002 DebugLoc DL = MI.getDebugLoc();
1003 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001004 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001005
1006 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001007 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001008 MachineOperand &MD = MI.getOperand(0);
1009 MachineOperand &MS = MI.getOperand(1);
1010 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001011 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1012 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001013 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001014 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001015 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001016 return true;
1017 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001018 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001019 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001020 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001021 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001022 MBB.erase(MI);
1023 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001024 case Hexagon::V6_vassignp_128B:
1025 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001026 unsigned SrcReg = MI.getOperand(1).getReg();
1027 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001028 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1029 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001030 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1031 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001032 MBB.erase(MI);
1033 return true;
1034 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001035 case Hexagon::V6_lo_128B:
1036 case Hexagon::V6_lo: {
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 Parzyszeka5409972016-11-09 16:19:08 +00001039 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001040 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001041 MBB.erase(MI);
1042 MRI.clearKillFlags(SrcSubLo);
1043 return true;
1044 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001045 case Hexagon::V6_hi_128B:
1046 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001047 unsigned SrcReg = MI.getOperand(1).getReg();
1048 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001049 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001050 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001051 MBB.erase(MI);
1052 MRI.clearKillFlags(SrcSubHi);
1053 return true;
1054 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001055 case Hexagon::PS_vstorerw_ai:
1056 case Hexagon::PS_vstorerwu_ai:
1057 case Hexagon::PS_vstorerw_ai_128B:
1058 case Hexagon::PS_vstorerwu_ai_128B: {
1059 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1060 Opc == Hexagon::PS_vstorerwu_ai_128B);
1061 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1062 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001063 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001064 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1065 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001066 unsigned NewOpc;
1067 if (Aligned)
1068 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1069 : Hexagon::V6_vS32b_ai;
1070 else
1071 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1072 : Hexagon::V6_vS32Ub_ai;
1073
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001074 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001075 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001076 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001077 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001078 .addImm(MI.getOperand(1).getImm())
1079 .addReg(SrcSubLo)
1080 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001081 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001082 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001083 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001084 // The Vectors are indexed in multiples of vector size.
1085 .addImm(MI.getOperand(1).getImm() + Offset)
1086 .addReg(SrcSubHi)
1087 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001088 MBB.erase(MI);
1089 return true;
1090 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001091 case Hexagon::PS_vloadrw_ai:
1092 case Hexagon::PS_vloadrwu_ai:
1093 case Hexagon::PS_vloadrw_ai_128B:
1094 case Hexagon::PS_vloadrwu_ai_128B: {
1095 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1096 Opc == Hexagon::PS_vloadrwu_ai_128B);
1097 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1098 Opc == Hexagon::PS_vloadrw_ai_128B);
1099 unsigned NewOpc;
1100 if (Aligned)
1101 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1102 : Hexagon::V6_vL32b_ai;
1103 else
1104 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1105 : Hexagon::V6_vL32Ub_ai;
1106
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001107 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001108 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Diana Picus116bbab2017-01-13 09:58:52 +00001109 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1110 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
1111 .add(MI.getOperand(1))
1112 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001113 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001114 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1115 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001116 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001117 .addImm(MI.getOperand(2).getImm() + Offset)
1118 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001119 MBB.erase(MI);
1120 return true;
1121 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001122 case Hexagon::PS_true: {
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_orn), 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_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001131 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001132 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1133 .addReg(Reg, RegState::Undef)
1134 .addReg(Reg, RegState::Undef);
1135 MBB.erase(MI);
1136 return true;
1137 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001138 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001139 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001140 unsigned DstReg = MI.getOperand(0).getReg();
1141 unsigned Src1Reg = MI.getOperand(1).getReg();
1142 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001143 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1144 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1145 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1146 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001147 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001148 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001149 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001150 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001151 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001152 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001153 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001154 .addReg(Src2SubLo);
1155 MBB.erase(MI);
1156 MRI.clearKillFlags(Src1SubHi);
1157 MRI.clearKillFlags(Src1SubLo);
1158 MRI.clearKillFlags(Src2SubHi);
1159 MRI.clearKillFlags(Src2SubLo);
1160 return true;
1161 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001162 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001163 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001164 unsigned DstReg = MI.getOperand(0).getReg();
1165 unsigned Src1Reg = MI.getOperand(1).getReg();
1166 unsigned Src2Reg = MI.getOperand(2).getReg();
1167 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001168 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1169 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1170 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1171 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1172 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1173 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001174 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001175 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001176 .addReg(Src1SubHi)
1177 .addReg(Src2SubHi)
1178 .addReg(Src3SubHi);
1179 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001180 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001181 .addReg(Src1SubLo)
1182 .addReg(Src2SubLo)
1183 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001184 MBB.erase(MI);
1185 MRI.clearKillFlags(Src1SubHi);
1186 MRI.clearKillFlags(Src1SubLo);
1187 MRI.clearKillFlags(Src2SubHi);
1188 MRI.clearKillFlags(Src2SubLo);
1189 MRI.clearKillFlags(Src3SubHi);
1190 MRI.clearKillFlags(Src3SubLo);
1191 return true;
1192 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001193 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001194 const MachineOperand &Op0 = MI.getOperand(0);
1195 const MachineOperand &Op1 = MI.getOperand(1);
1196 const MachineOperand &Op2 = MI.getOperand(2);
1197 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001198 unsigned Rd = Op0.getReg();
1199 unsigned Pu = Op1.getReg();
1200 unsigned Rs = Op2.getReg();
1201 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001202 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001203 unsigned K1 = getKillRegState(Op1.isKill());
1204 unsigned K2 = getKillRegState(Op2.isKill());
1205 unsigned K3 = getKillRegState(Op3.isKill());
1206 if (Rd != Rs)
1207 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1208 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1209 .addReg(Rs, K2);
1210 if (Rd != Rt)
1211 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1212 .addReg(Pu, K1)
1213 .addReg(Rt, K3);
1214 MBB.erase(MI);
1215 return true;
1216 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001217 case Hexagon::PS_vselect:
1218 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001219 const MachineOperand &Op0 = MI.getOperand(0);
1220 const MachineOperand &Op1 = MI.getOperand(1);
1221 const MachineOperand &Op2 = MI.getOperand(2);
1222 const MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001223 LivePhysRegs LiveAtMI(&HRI);
1224 getLiveRegsAt(LiveAtMI, MI);
1225 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1226 if (Op0.getReg() != Op2.getReg()) {
1227 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001228 .add(Op0)
1229 .add(Op1)
1230 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001231 if (IsDestLive)
1232 T.addReg(Op0.getReg(), RegState::Implicit);
1233 IsDestLive = true;
1234 }
1235 if (Op0.getReg() != Op3.getReg()) {
1236 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001237 .add(Op0)
1238 .add(Op1)
1239 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001240 if (IsDestLive)
1241 T.addReg(Op0.getReg(), RegState::Implicit);
1242 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001243 MBB.erase(MI);
1244 return true;
1245 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001246 case Hexagon::PS_wselect:
1247 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001248 MachineOperand &Op0 = MI.getOperand(0);
1249 MachineOperand &Op1 = MI.getOperand(1);
1250 MachineOperand &Op2 = MI.getOperand(2);
1251 MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001252 LivePhysRegs LiveAtMI(&HRI);
1253 getLiveRegsAt(LiveAtMI, MI);
1254 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1255
1256 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001257 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1258 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001259 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001260 .add(Op0)
1261 .add(Op1)
1262 .addReg(SrcHi)
1263 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001264 if (IsDestLive)
1265 T.addReg(Op0.getReg(), RegState::Implicit);
1266 IsDestLive = true;
1267 }
1268 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001269 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1270 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001271 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001272 .add(Op0)
1273 .add(Op1)
1274 .addReg(SrcHi)
1275 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001276 if (IsDestLive)
1277 T.addReg(Op0.getReg(), RegState::Implicit);
1278 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001279 MBB.erase(MI);
1280 return true;
1281 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001282 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001283 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001284 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001285 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001286 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001287 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001288 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001289 case Hexagon::PS_jmprett:
1290 MI.setDesc(get(Hexagon::J2_jumprt));
1291 return true;
1292 case Hexagon::PS_jmpretf:
1293 MI.setDesc(get(Hexagon::J2_jumprf));
1294 return true;
1295 case Hexagon::PS_jmprettnewpt:
1296 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1297 return true;
1298 case Hexagon::PS_jmpretfnewpt:
1299 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1300 return true;
1301 case Hexagon::PS_jmprettnew:
1302 MI.setDesc(get(Hexagon::J2_jumprtnew));
1303 return true;
1304 case Hexagon::PS_jmpretfnew:
1305 MI.setDesc(get(Hexagon::J2_jumprfnew));
1306 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001307 }
1308
1309 return false;
1310}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001311
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001312// We indicate that we want to reverse the branch by
1313// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001314bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001315 SmallVectorImpl<MachineOperand> &Cond) const {
1316 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001317 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001318 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1319 unsigned opcode = Cond[0].getImm();
1320 //unsigned temp;
1321 assert(get(opcode).isBranch() && "Should be a branching condition.");
1322 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001323 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001324 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1325 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001326 return false;
1327}
1328
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001329void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1330 MachineBasicBlock::iterator MI) const {
1331 DebugLoc DL;
1332 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1333}
1334
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001335bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1336 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001337}
1338
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001339// Returns true if an instruction is predicated irrespective of the predicate
1340// sense. For example, all of the following will return true.
1341// if (p0) R1 = add(R2, R3)
1342// if (!p0) R1 = add(R2, R3)
1343// if (p0.new) R1 = add(R2, R3)
1344// if (!p0.new) R1 = add(R2, R3)
1345// Note: New-value stores are not included here as in the current
1346// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001347bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1348 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001349 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001350}
1351
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001352bool HexagonInstrInfo::PredicateInstruction(
1353 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001354 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1355 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001356 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001357 return false;
1358 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001359 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001360 assert (isPredicable(MI) && "Expected predicable instruction");
1361 bool invertJump = predOpcodeHasNot(Cond);
1362
1363 // We have to predicate MI "in place", i.e. after this function returns,
1364 // MI will need to be transformed into a predicated form. To avoid com-
1365 // plicated manipulations with the operands (handling tied operands,
1366 // etc.), build a new temporary instruction, then overwrite MI with it.
1367
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001368 MachineBasicBlock &B = *MI.getParent();
1369 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001370 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1371 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001372 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001373 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001374 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001375 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1376 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001377 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001378 NOp++;
1379 }
1380
1381 unsigned PredReg, PredRegPos, PredRegFlags;
1382 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1383 (void)GotPredReg;
1384 assert(GotPredReg);
1385 T.addReg(PredReg, PredRegFlags);
1386 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001387 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001388
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001389 MI.setDesc(get(PredOpc));
1390 while (unsigned n = MI.getNumOperands())
1391 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001392 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001393 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001394
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001395 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001396 B.erase(TI);
1397
1398 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1399 MRI.clearKillFlags(PredReg);
1400 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001401}
1402
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001403bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1404 ArrayRef<MachineOperand> Pred2) const {
1405 // TODO: Fix this
1406 return false;
1407}
1408
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001409bool HexagonInstrInfo::DefinesPredicate(
1410 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001411 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001412 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1413 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001414 if (MO.isReg() && MO.isDef()) {
1415 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1416 if (RC == &Hexagon::PredRegsRegClass) {
1417 Pred.push_back(MO);
1418 return true;
1419 }
1420 }
1421 }
1422 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001423}
Andrew Trickd06df962012-02-01 22:13:57 +00001424
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001425bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001426 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001427}
1428
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001429bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1430 const MachineBasicBlock *MBB,
1431 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001432 // Debug info is never a scheduling boundary. It's necessary to be explicit
1433 // due to the special treatment of IT instructions below, otherwise a
1434 // dbg_value followed by an IT will result in the IT instruction being
1435 // considered a scheduling hazard, which is wrong. It should be the actual
1436 // instruction preceding the dbg_value instruction(s), just like it is
1437 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001438 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001439 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001440
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001441 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001442 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001443 // Don't mess around with no return calls.
1444 if (doesNotReturn(MI))
1445 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001446 // If any of the block's successors is a landing pad, this could be a
1447 // throwing call.
1448 for (auto I : MBB->successors())
1449 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001450 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001451 }
1452
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001453 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001454 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001455 return true;
1456
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001457 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1458 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001459
1460 return false;
1461}
1462
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001463/// Measure the specified inline asm to determine an approximation of its
1464/// length.
1465/// Comments (which run till the next SeparatorString or newline) do not
1466/// count as an instruction.
1467/// Any other non-whitespace text is considered an instruction, with
1468/// multiple instructions separated by SeparatorString or newlines.
1469/// Variable-length instructions are not handled here; this function
1470/// may be overloaded in the target code to do that.
1471/// Hexagon counts the number of ##'s and adjust for that many
1472/// constant exenders.
1473unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1474 const MCAsmInfo &MAI) const {
1475 StringRef AStr(Str);
1476 // Count the number of instructions in the asm.
1477 bool atInsnStart = true;
1478 unsigned Length = 0;
1479 for (; *Str; ++Str) {
1480 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1481 strlen(MAI.getSeparatorString())) == 0)
1482 atInsnStart = true;
1483 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1484 Length += MAI.getMaxInstLength();
1485 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001486 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001487 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1488 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001489 atInsnStart = false;
1490 }
1491
1492 // Add to size number of constant extenders seen * 4.
1493 StringRef Occ("##");
1494 Length += AStr.count(Occ)*4;
1495 return Length;
1496}
1497
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001498ScheduleHazardRecognizer*
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
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001508/// \brief For a comparison instruction, return the source registers in
1509/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1510/// compares against in CmpValue. Return true if the comparison instruction
1511/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001512bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1513 unsigned &SrcReg2, int &Mask,
1514 int &Value) const {
1515 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001516
1517 // Set mask and the first source register.
1518 switch (Opc) {
1519 case Hexagon::C2_cmpeq:
1520 case Hexagon::C2_cmpeqp:
1521 case Hexagon::C2_cmpgt:
1522 case Hexagon::C2_cmpgtp:
1523 case Hexagon::C2_cmpgtu:
1524 case Hexagon::C2_cmpgtup:
1525 case Hexagon::C4_cmpneq:
1526 case Hexagon::C4_cmplte:
1527 case Hexagon::C4_cmplteu:
1528 case Hexagon::C2_cmpeqi:
1529 case Hexagon::C2_cmpgti:
1530 case Hexagon::C2_cmpgtui:
1531 case Hexagon::C4_cmpneqi:
1532 case Hexagon::C4_cmplteui:
1533 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001534 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001535 Mask = ~0;
1536 break;
1537 case Hexagon::A4_cmpbeq:
1538 case Hexagon::A4_cmpbgt:
1539 case Hexagon::A4_cmpbgtu:
1540 case Hexagon::A4_cmpbeqi:
1541 case Hexagon::A4_cmpbgti:
1542 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001543 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001544 Mask = 0xFF;
1545 break;
1546 case Hexagon::A4_cmpheq:
1547 case Hexagon::A4_cmphgt:
1548 case Hexagon::A4_cmphgtu:
1549 case Hexagon::A4_cmpheqi:
1550 case Hexagon::A4_cmphgti:
1551 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001552 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001553 Mask = 0xFFFF;
1554 break;
1555 }
1556
1557 // Set the value/second source register.
1558 switch (Opc) {
1559 case Hexagon::C2_cmpeq:
1560 case Hexagon::C2_cmpeqp:
1561 case Hexagon::C2_cmpgt:
1562 case Hexagon::C2_cmpgtp:
1563 case Hexagon::C2_cmpgtu:
1564 case Hexagon::C2_cmpgtup:
1565 case Hexagon::A4_cmpbeq:
1566 case Hexagon::A4_cmpbgt:
1567 case Hexagon::A4_cmpbgtu:
1568 case Hexagon::A4_cmpheq:
1569 case Hexagon::A4_cmphgt:
1570 case Hexagon::A4_cmphgtu:
1571 case Hexagon::C4_cmpneq:
1572 case Hexagon::C4_cmplte:
1573 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001574 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001575 return true;
1576
1577 case Hexagon::C2_cmpeqi:
1578 case Hexagon::C2_cmpgtui:
1579 case Hexagon::C2_cmpgti:
1580 case Hexagon::C4_cmpneqi:
1581 case Hexagon::C4_cmplteui:
1582 case Hexagon::C4_cmpltei:
1583 case Hexagon::A4_cmpbeqi:
1584 case Hexagon::A4_cmpbgti:
1585 case Hexagon::A4_cmpbgtui:
1586 case Hexagon::A4_cmpheqi:
1587 case Hexagon::A4_cmphgti:
1588 case Hexagon::A4_cmphgtui:
1589 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001590 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001591 return true;
1592 }
1593
1594 return false;
1595}
1596
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001597unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001598 const MachineInstr &MI,
1599 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001600 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001601}
1602
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001603DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1604 const TargetSubtargetInfo &STI) const {
1605 const InstrItineraryData *II = STI.getInstrItineraryData();
1606 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1607}
1608
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001609// Inspired by this pair:
1610// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1611// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1612// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001613bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1614 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001615 int OffsetA = 0, OffsetB = 0;
1616 unsigned SizeA = 0, SizeB = 0;
1617
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001618 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1619 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001620 return false;
1621
1622 // Instructions that are pure loads, not loads and stores like memops are not
1623 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001624 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001625 return true;
1626
1627 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001628 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001629 if (!BaseRegA || !SizeA)
1630 return false;
1631
1632 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001633 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001634 if (!BaseRegB || !SizeB)
1635 return false;
1636
1637 if (BaseRegA != BaseRegB)
1638 return false;
1639
1640 // This is a mem access with the same base register and known offsets from it.
1641 // Reason about it.
1642 if (OffsetA > OffsetB) {
1643 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1644 return (SizeB <= offDiff);
1645 } else if (OffsetA < OffsetB) {
1646 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1647 return (SizeA <= offDiff);
1648 }
1649
1650 return false;
1651}
1652
Brendon Cahoon254f8892016-07-29 16:44:44 +00001653/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001654bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001655 int &Value) const {
1656 if (isPostIncrement(MI)) {
1657 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001658 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001659 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001660 if (MI.getOpcode() == Hexagon::A2_addi) {
1661 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001662 return true;
1663 }
1664
1665 return false;
1666}
1667
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001668unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001669 MachineRegisterInfo &MRI = MF->getRegInfo();
1670 const TargetRegisterClass *TRC;
1671 if (VT == MVT::i1) {
1672 TRC = &Hexagon::PredRegsRegClass;
1673 } else if (VT == MVT::i32 || VT == MVT::f32) {
1674 TRC = &Hexagon::IntRegsRegClass;
1675 } else if (VT == MVT::i64 || VT == MVT::f64) {
1676 TRC = &Hexagon::DoubleRegsRegClass;
1677 } else {
1678 llvm_unreachable("Cannot handle this register class");
1679 }
1680
1681 unsigned NewReg = MRI.createVirtualRegister(TRC);
1682 return NewReg;
1683}
1684
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001685bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001686 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1687}
1688
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001689bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1690 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001691 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1692}
1693
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001694bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1695 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001696 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1697 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1698
1699 if (!(isTC1(MI))
1700 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001701 && !(MI.getDesc().mayLoad())
1702 && !(MI.getDesc().mayStore())
1703 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1704 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001705 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001706 && !(MI.isBranch())
1707 && !(MI.isReturn())
1708 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001709 return true;
1710
1711 return false;
1712}
1713
Sanjay Patele4b9f502015-12-07 19:21:39 +00001714// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001715bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
1716 return (getType(MI) == HexagonII::TypeCOMPOUND && MI.isBranch());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001717}
1718
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001719bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1720 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001721 isConditionalTransfer(MI) ||
1722 isConditionalALU32(MI) ||
1723 isConditionalLoad(MI) ||
1724 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001725 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1726 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001727}
1728
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001729bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1730 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001731 case Hexagon::A2_paddf:
1732 case Hexagon::A2_paddfnew:
1733 case Hexagon::A2_paddif:
1734 case Hexagon::A2_paddifnew:
1735 case Hexagon::A2_paddit:
1736 case Hexagon::A2_padditnew:
1737 case Hexagon::A2_paddt:
1738 case Hexagon::A2_paddtnew:
1739 case Hexagon::A2_pandf:
1740 case Hexagon::A2_pandfnew:
1741 case Hexagon::A2_pandt:
1742 case Hexagon::A2_pandtnew:
1743 case Hexagon::A2_porf:
1744 case Hexagon::A2_porfnew:
1745 case Hexagon::A2_port:
1746 case Hexagon::A2_portnew:
1747 case Hexagon::A2_psubf:
1748 case Hexagon::A2_psubfnew:
1749 case Hexagon::A2_psubt:
1750 case Hexagon::A2_psubtnew:
1751 case Hexagon::A2_pxorf:
1752 case Hexagon::A2_pxorfnew:
1753 case Hexagon::A2_pxort:
1754 case Hexagon::A2_pxortnew:
1755 case Hexagon::A4_paslhf:
1756 case Hexagon::A4_paslhfnew:
1757 case Hexagon::A4_paslht:
1758 case Hexagon::A4_paslhtnew:
1759 case Hexagon::A4_pasrhf:
1760 case Hexagon::A4_pasrhfnew:
1761 case Hexagon::A4_pasrht:
1762 case Hexagon::A4_pasrhtnew:
1763 case Hexagon::A4_psxtbf:
1764 case Hexagon::A4_psxtbfnew:
1765 case Hexagon::A4_psxtbt:
1766 case Hexagon::A4_psxtbtnew:
1767 case Hexagon::A4_psxthf:
1768 case Hexagon::A4_psxthfnew:
1769 case Hexagon::A4_psxtht:
1770 case Hexagon::A4_psxthtnew:
1771 case Hexagon::A4_pzxtbf:
1772 case Hexagon::A4_pzxtbfnew:
1773 case Hexagon::A4_pzxtbt:
1774 case Hexagon::A4_pzxtbtnew:
1775 case Hexagon::A4_pzxthf:
1776 case Hexagon::A4_pzxthfnew:
1777 case Hexagon::A4_pzxtht:
1778 case Hexagon::A4_pzxthtnew:
1779 case Hexagon::C2_ccombinewf:
1780 case Hexagon::C2_ccombinewt:
1781 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001782 }
1783 return false;
1784}
1785
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001786// FIXME - Function name and it's functionality don't match.
1787// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001788bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1789 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001790 return false;
1791
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001792 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001793 // Instruction with valid predicated-new opcode can be promoted to .new.
1794 return PNewOpcode >= 0;
1795}
1796
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001797// Returns true if an instruction is a conditional store.
1798//
1799// Note: It doesn't include conditional new-value stores as they can't be
1800// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001801bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1802 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001803 default: return false;
1804 case Hexagon::S4_storeirbt_io:
1805 case Hexagon::S4_storeirbf_io:
1806 case Hexagon::S4_pstorerbt_rr:
1807 case Hexagon::S4_pstorerbf_rr:
1808 case Hexagon::S2_pstorerbt_io:
1809 case Hexagon::S2_pstorerbf_io:
1810 case Hexagon::S2_pstorerbt_pi:
1811 case Hexagon::S2_pstorerbf_pi:
1812 case Hexagon::S2_pstorerdt_io:
1813 case Hexagon::S2_pstorerdf_io:
1814 case Hexagon::S4_pstorerdt_rr:
1815 case Hexagon::S4_pstorerdf_rr:
1816 case Hexagon::S2_pstorerdt_pi:
1817 case Hexagon::S2_pstorerdf_pi:
1818 case Hexagon::S2_pstorerht_io:
1819 case Hexagon::S2_pstorerhf_io:
1820 case Hexagon::S4_storeirht_io:
1821 case Hexagon::S4_storeirhf_io:
1822 case Hexagon::S4_pstorerht_rr:
1823 case Hexagon::S4_pstorerhf_rr:
1824 case Hexagon::S2_pstorerht_pi:
1825 case Hexagon::S2_pstorerhf_pi:
1826 case Hexagon::S2_pstorerit_io:
1827 case Hexagon::S2_pstorerif_io:
1828 case Hexagon::S4_storeirit_io:
1829 case Hexagon::S4_storeirif_io:
1830 case Hexagon::S4_pstorerit_rr:
1831 case Hexagon::S4_pstorerif_rr:
1832 case Hexagon::S2_pstorerit_pi:
1833 case Hexagon::S2_pstorerif_pi:
1834
1835 // V4 global address store before promoting to dot new.
1836 case Hexagon::S4_pstorerdt_abs:
1837 case Hexagon::S4_pstorerdf_abs:
1838 case Hexagon::S4_pstorerbt_abs:
1839 case Hexagon::S4_pstorerbf_abs:
1840 case Hexagon::S4_pstorerht_abs:
1841 case Hexagon::S4_pstorerhf_abs:
1842 case Hexagon::S4_pstorerit_abs:
1843 case Hexagon::S4_pstorerif_abs:
1844 return true;
1845
1846 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1847 // from the "Conditional Store" list. Because a predicated new value store
1848 // would NOT be promoted to a double dot new store.
1849 // This function returns yes for those stores that are predicated but not
1850 // yet promoted to predicate dot new instructions.
1851 }
1852}
1853
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001854bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1855 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001856 case Hexagon::A2_tfrt:
1857 case Hexagon::A2_tfrf:
1858 case Hexagon::C2_cmoveit:
1859 case Hexagon::C2_cmoveif:
1860 case Hexagon::A2_tfrtnew:
1861 case Hexagon::A2_tfrfnew:
1862 case Hexagon::C2_cmovenewit:
1863 case Hexagon::C2_cmovenewif:
1864 case Hexagon::A2_tfrpt:
1865 case Hexagon::A2_tfrpf:
1866 return true;
1867
1868 default:
1869 return false;
1870 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001871 return false;
1872}
1873
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001874// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1875// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001876bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1877 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001878 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1879 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001880 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001881
1882 unsigned isExtendable =
1883 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1884 if (!isExtendable)
1885 return false;
1886
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001887 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001888 return false;
1889
1890 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001891 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001892 // Use MO operand flags to determine if MO
1893 // has the HMOTF_ConstExtended flag set.
1894 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001895 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001896 // If this is a Machine BB address we are talking about, and it is
1897 // not marked as extended, say so.
1898 if (MO.isMBB())
1899 return false;
1900
1901 // We could be using an instruction with an extendable immediate and shoehorn
1902 // a global address into it. If it is a global address it will be constant
1903 // extended. We do this for COMBINE.
1904 // We currently only handle isGlobal() because it is the only kind of
1905 // object we are going to end up with here for now.
1906 // In the future we probably should add isSymbol(), etc.
1907 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001908 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001909 return true;
1910
1911 // If the extendable operand is not 'Immediate' type, the instruction should
1912 // have 'isExtended' flag set.
1913 assert(MO.isImm() && "Extendable operand must be Immediate type");
1914
1915 int MinValue = getMinValue(MI);
1916 int MaxValue = getMaxValue(MI);
1917 int ImmValue = MO.getImm();
1918
1919 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001920}
1921
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001922bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1923 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001924 case Hexagon::L4_return :
1925 case Hexagon::L4_return_t :
1926 case Hexagon::L4_return_f :
1927 case Hexagon::L4_return_tnew_pnt :
1928 case Hexagon::L4_return_fnew_pnt :
1929 case Hexagon::L4_return_tnew_pt :
1930 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001931 return true;
1932 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001933 return false;
1934}
1935
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001936// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001937bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1938 const MachineInstr &ConsMI) const {
1939 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001940 return false;
1941
1942 auto &HRI = getRegisterInfo();
1943
1944 SmallVector<unsigned, 4> DefsA;
1945 SmallVector<unsigned, 4> DefsB;
1946 SmallVector<unsigned, 8> UsesA;
1947 SmallVector<unsigned, 8> UsesB;
1948
1949 parseOperands(ProdMI, DefsA, UsesA);
1950 parseOperands(ConsMI, DefsB, UsesB);
1951
1952 for (auto &RegA : DefsA)
1953 for (auto &RegB : UsesB) {
1954 // True data dependency.
1955 if (RegA == RegB)
1956 return true;
1957
1958 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1959 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1960 if (RegB == *SubRegs)
1961 return true;
1962
1963 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1964 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1965 if (RegA == *SubRegs)
1966 return true;
1967 }
1968
1969 return false;
1970}
1971
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001972// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001973bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1974 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001975 case Hexagon::V6_vL32b_cur_pi:
1976 case Hexagon::V6_vL32b_cur_ai:
1977 case Hexagon::V6_vL32b_cur_pi_128B:
1978 case Hexagon::V6_vL32b_cur_ai_128B:
1979 return true;
1980 }
1981 return false;
1982}
1983
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001984// Returns true, if any one of the operands is a dot new
1985// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001986bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
1987 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001988 return true;
1989
1990 return false;
1991}
1992
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001993/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001994bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
1995 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001996 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1997 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1998 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1999}
2000
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002001bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2002 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002003 return true;
2004
2005 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002006 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002007 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2008 return true;
2009 return false;
2010}
2011
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002012bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2013 return (Opcode == Hexagon::ENDLOOP0 ||
2014 Opcode == Hexagon::ENDLOOP1);
2015}
2016
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002017bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2018 switch(OpType) {
2019 case MachineOperand::MO_MachineBasicBlock:
2020 case MachineOperand::MO_GlobalAddress:
2021 case MachineOperand::MO_ExternalSymbol:
2022 case MachineOperand::MO_JumpTableIndex:
2023 case MachineOperand::MO_ConstantPoolIndex:
2024 case MachineOperand::MO_BlockAddress:
2025 return true;
2026 default:
2027 return false;
2028 }
2029}
2030
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002031bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2032 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002033 const uint64_t F = MID.TSFlags;
2034 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2035 return true;
2036
2037 // TODO: This is largely obsolete now. Will need to be removed
2038 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002039 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002040 // PS_fi and PS_fia remain special cases.
2041 case Hexagon::PS_fi:
2042 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002043 return true;
2044 default:
2045 return false;
2046 }
2047 return false;
2048}
2049
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002050// This returns true in two cases:
2051// - The OP code itself indicates that this is an extended instruction.
2052// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002053bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002054 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002055 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002056 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2057 return true;
2058 // Use MO operand flags to determine if one of MI's operands
2059 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002060 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
2061 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002062 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2063 return true;
2064 }
2065 return false;
2066}
2067
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002068bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2069 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002070 const uint64_t F = get(Opcode).TSFlags;
2071 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2072}
2073
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002074// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002075bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2076 const MachineInstr &J) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002077 if (!isV60VectorInstruction(I))
2078 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002079 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002080 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002081 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002082}
2083
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002084bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2085 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002086 case Hexagon::J2_callr :
2087 case Hexagon::J2_callrf :
2088 case Hexagon::J2_callrt :
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00002089 case Hexagon::PS_call_nr :
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002090 return true;
2091 }
2092 return false;
2093}
2094
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002095bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2096 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002097 case Hexagon::L4_return :
2098 case Hexagon::L4_return_t :
2099 case Hexagon::L4_return_f :
2100 case Hexagon::L4_return_fnew_pnt :
2101 case Hexagon::L4_return_fnew_pt :
2102 case Hexagon::L4_return_tnew_pnt :
2103 case Hexagon::L4_return_tnew_pt :
2104 return true;
2105 }
2106 return false;
2107}
2108
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002109bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2110 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002111 case Hexagon::J2_jumpr :
2112 case Hexagon::J2_jumprt :
2113 case Hexagon::J2_jumprf :
2114 case Hexagon::J2_jumprtnewpt :
2115 case Hexagon::J2_jumprfnewpt :
2116 case Hexagon::J2_jumprtnew :
2117 case Hexagon::J2_jumprfnew :
2118 return true;
2119 }
2120 return false;
2121}
2122
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002123// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002124// Use abs estimate as oppose to the exact number.
2125// TODO: This will need to be changed to use MC level
2126// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002127bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002128 unsigned offset) const {
2129 // This selection of jump instructions matches to that what
2130 // AnalyzeBranch can parse, plus NVJ.
2131 if (isNewValueJump(MI)) // r9:2
2132 return isInt<11>(offset);
2133
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002134 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002135 // Still missing Jump to address condition on register value.
2136 default:
2137 return false;
2138 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2139 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002140 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002141 return isInt<24>(offset);
2142 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2143 case Hexagon::J2_jumpf:
2144 case Hexagon::J2_jumptnew:
2145 case Hexagon::J2_jumptnewpt:
2146 case Hexagon::J2_jumpfnew:
2147 case Hexagon::J2_jumpfnewpt:
2148 case Hexagon::J2_callt:
2149 case Hexagon::J2_callf:
2150 return isInt<17>(offset);
2151 case Hexagon::J2_loop0i:
2152 case Hexagon::J2_loop0iext:
2153 case Hexagon::J2_loop0r:
2154 case Hexagon::J2_loop0rext:
2155 case Hexagon::J2_loop1i:
2156 case Hexagon::J2_loop1iext:
2157 case Hexagon::J2_loop1r:
2158 case Hexagon::J2_loop1rext:
2159 return isInt<9>(offset);
2160 // TODO: Add all the compound branches here. Can we do this in Relation model?
2161 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2162 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2163 return isInt<11>(offset);
2164 }
2165}
2166
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002167bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2168 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002169 bool isLate = isLateResultInstr(LRMI);
2170 bool isEarly = isEarlySourceInstr(ESMI);
2171
2172 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002173 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002174 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002175 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002176
2177 if (isLate && isEarly) {
2178 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2179 return true;
2180 }
2181
2182 return false;
2183}
2184
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002185bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2186 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002187 case TargetOpcode::EXTRACT_SUBREG:
2188 case TargetOpcode::INSERT_SUBREG:
2189 case TargetOpcode::SUBREG_TO_REG:
2190 case TargetOpcode::REG_SEQUENCE:
2191 case TargetOpcode::IMPLICIT_DEF:
2192 case TargetOpcode::COPY:
2193 case TargetOpcode::INLINEASM:
2194 case TargetOpcode::PHI:
2195 return false;
2196 default:
2197 break;
2198 }
2199
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002200 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002201
2202 switch (SchedClass) {
2203 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2204 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2205 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2206 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2207 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2208 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2209 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2210 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2211 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2212 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2213 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2214 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2215 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2216 return false;
2217 }
2218 return true;
2219}
2220
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002221bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002222 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2223 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002224 return MI.getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002225}
2226
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002227bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2228 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002229 return Opcode == Hexagon::J2_loop0i ||
2230 Opcode == Hexagon::J2_loop0r ||
2231 Opcode == Hexagon::J2_loop0iext ||
2232 Opcode == Hexagon::J2_loop0rext ||
2233 Opcode == Hexagon::J2_loop1i ||
2234 Opcode == Hexagon::J2_loop1r ||
2235 Opcode == Hexagon::J2_loop1iext ||
2236 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002237}
2238
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002239bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2240 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002241 default: return false;
2242 case Hexagon::L4_iadd_memopw_io :
2243 case Hexagon::L4_isub_memopw_io :
2244 case Hexagon::L4_add_memopw_io :
2245 case Hexagon::L4_sub_memopw_io :
2246 case Hexagon::L4_and_memopw_io :
2247 case Hexagon::L4_or_memopw_io :
2248 case Hexagon::L4_iadd_memoph_io :
2249 case Hexagon::L4_isub_memoph_io :
2250 case Hexagon::L4_add_memoph_io :
2251 case Hexagon::L4_sub_memoph_io :
2252 case Hexagon::L4_and_memoph_io :
2253 case Hexagon::L4_or_memoph_io :
2254 case Hexagon::L4_iadd_memopb_io :
2255 case Hexagon::L4_isub_memopb_io :
2256 case Hexagon::L4_add_memopb_io :
2257 case Hexagon::L4_sub_memopb_io :
2258 case Hexagon::L4_and_memopb_io :
2259 case Hexagon::L4_or_memopb_io :
2260 case Hexagon::L4_ior_memopb_io:
2261 case Hexagon::L4_ior_memoph_io:
2262 case Hexagon::L4_ior_memopw_io:
2263 case Hexagon::L4_iand_memopb_io:
2264 case Hexagon::L4_iand_memoph_io:
2265 case Hexagon::L4_iand_memopw_io:
2266 return true;
2267 }
2268 return false;
2269}
2270
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002271bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2272 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002273 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2274}
2275
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002276bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2277 const uint64_t F = get(Opcode).TSFlags;
2278 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2279}
2280
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002281bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002282 return isNewValueJump(MI) || isNewValueStore(MI);
2283}
2284
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002285bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2286 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002287}
2288
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002289bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2290 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2291}
2292
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002293bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2294 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002295 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2296}
2297
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002298bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2299 const uint64_t F = get(Opcode).TSFlags;
2300 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2301}
2302
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002303// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002304bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002305 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002306 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002307 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2308 == OperandNum;
2309}
2310
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002311bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2312 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002313 assert(isPredicated(MI));
2314 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2315}
2316
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002317bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2318 const uint64_t F = get(Opcode).TSFlags;
2319 assert(isPredicated(Opcode));
2320 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2321}
2322
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002323bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2324 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002325 return !((F >> HexagonII::PredicatedFalsePos) &
2326 HexagonII::PredicatedFalseMask);
2327}
2328
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002329bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2330 const uint64_t F = get(Opcode).TSFlags;
2331 // Make sure that the instruction is predicated.
2332 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2333 return !((F >> HexagonII::PredicatedFalsePos) &
2334 HexagonII::PredicatedFalseMask);
2335}
2336
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002337bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2338 const uint64_t F = get(Opcode).TSFlags;
2339 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2340}
2341
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002342bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2343 const uint64_t F = get(Opcode).TSFlags;
2344 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2345}
2346
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002347bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2348 const uint64_t F = get(Opcode).TSFlags;
2349 assert(get(Opcode).isBranch() &&
2350 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2351 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2352}
2353
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002354bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2355 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2356 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2357 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2358 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002359}
2360
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002361bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2362 switch (MI.getOpcode()) {
2363 // Byte
2364 case Hexagon::L2_loadrb_io:
2365 case Hexagon::L4_loadrb_ur:
2366 case Hexagon::L4_loadrb_ap:
2367 case Hexagon::L2_loadrb_pr:
2368 case Hexagon::L2_loadrb_pbr:
2369 case Hexagon::L2_loadrb_pi:
2370 case Hexagon::L2_loadrb_pci:
2371 case Hexagon::L2_loadrb_pcr:
2372 case Hexagon::L2_loadbsw2_io:
2373 case Hexagon::L4_loadbsw2_ur:
2374 case Hexagon::L4_loadbsw2_ap:
2375 case Hexagon::L2_loadbsw2_pr:
2376 case Hexagon::L2_loadbsw2_pbr:
2377 case Hexagon::L2_loadbsw2_pi:
2378 case Hexagon::L2_loadbsw2_pci:
2379 case Hexagon::L2_loadbsw2_pcr:
2380 case Hexagon::L2_loadbsw4_io:
2381 case Hexagon::L4_loadbsw4_ur:
2382 case Hexagon::L4_loadbsw4_ap:
2383 case Hexagon::L2_loadbsw4_pr:
2384 case Hexagon::L2_loadbsw4_pbr:
2385 case Hexagon::L2_loadbsw4_pi:
2386 case Hexagon::L2_loadbsw4_pci:
2387 case Hexagon::L2_loadbsw4_pcr:
2388 case Hexagon::L4_loadrb_rr:
2389 case Hexagon::L2_ploadrbt_io:
2390 case Hexagon::L2_ploadrbt_pi:
2391 case Hexagon::L2_ploadrbf_io:
2392 case Hexagon::L2_ploadrbf_pi:
2393 case Hexagon::L2_ploadrbtnew_io:
2394 case Hexagon::L2_ploadrbfnew_io:
2395 case Hexagon::L4_ploadrbt_rr:
2396 case Hexagon::L4_ploadrbf_rr:
2397 case Hexagon::L4_ploadrbtnew_rr:
2398 case Hexagon::L4_ploadrbfnew_rr:
2399 case Hexagon::L2_ploadrbtnew_pi:
2400 case Hexagon::L2_ploadrbfnew_pi:
2401 case Hexagon::L4_ploadrbt_abs:
2402 case Hexagon::L4_ploadrbf_abs:
2403 case Hexagon::L4_ploadrbtnew_abs:
2404 case Hexagon::L4_ploadrbfnew_abs:
2405 case Hexagon::L2_loadrbgp:
2406 // Half
2407 case Hexagon::L2_loadrh_io:
2408 case Hexagon::L4_loadrh_ur:
2409 case Hexagon::L4_loadrh_ap:
2410 case Hexagon::L2_loadrh_pr:
2411 case Hexagon::L2_loadrh_pbr:
2412 case Hexagon::L2_loadrh_pi:
2413 case Hexagon::L2_loadrh_pci:
2414 case Hexagon::L2_loadrh_pcr:
2415 case Hexagon::L4_loadrh_rr:
2416 case Hexagon::L2_ploadrht_io:
2417 case Hexagon::L2_ploadrht_pi:
2418 case Hexagon::L2_ploadrhf_io:
2419 case Hexagon::L2_ploadrhf_pi:
2420 case Hexagon::L2_ploadrhtnew_io:
2421 case Hexagon::L2_ploadrhfnew_io:
2422 case Hexagon::L4_ploadrht_rr:
2423 case Hexagon::L4_ploadrhf_rr:
2424 case Hexagon::L4_ploadrhtnew_rr:
2425 case Hexagon::L4_ploadrhfnew_rr:
2426 case Hexagon::L2_ploadrhtnew_pi:
2427 case Hexagon::L2_ploadrhfnew_pi:
2428 case Hexagon::L4_ploadrht_abs:
2429 case Hexagon::L4_ploadrhf_abs:
2430 case Hexagon::L4_ploadrhtnew_abs:
2431 case Hexagon::L4_ploadrhfnew_abs:
2432 case Hexagon::L2_loadrhgp:
2433 return true;
2434 default:
2435 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002436 }
2437}
2438
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002439bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2440 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002441 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2442}
2443
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002444bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2445 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002446 case Hexagon::STriw_pred :
2447 case Hexagon::LDriw_pred :
2448 return true;
2449 default:
2450 return false;
2451 }
2452}
2453
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002454bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2455 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002456 return false;
2457
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002458 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002459 if (Op.isGlobal() || Op.isSymbol())
2460 return true;
2461 return false;
2462}
2463
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002464// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002465bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2466 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002467 switch (SchedClass) {
2468 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2469 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2470 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2471 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2472 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2473 //case Hexagon::Sched::M_tc_1_SLOT23:
2474 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2475 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2476 return true;
2477
2478 default:
2479 return false;
2480 }
2481}
2482
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002483bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2484 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002485 switch (SchedClass) {
2486 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2487 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2488 case Hexagon::Sched::CR_tc_2_SLOT3:
2489 case Hexagon::Sched::M_tc_2_SLOT23:
2490 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2491 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2492 return true;
2493
2494 default:
2495 return false;
2496 }
2497}
2498
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002499bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2500 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002501 switch (SchedClass) {
2502 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2503 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2504 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2505 case Hexagon::Sched::CR_tc_2early_SLOT23:
2506 case Hexagon::Sched::CR_tc_2early_SLOT3:
2507 case Hexagon::Sched::J_tc_2early_SLOT0123:
2508 case Hexagon::Sched::J_tc_2early_SLOT2:
2509 case Hexagon::Sched::J_tc_2early_SLOT23:
2510 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2511 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2512 return true;
2513
2514 default:
2515 return false;
2516 }
2517}
2518
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002519bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2520 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002521 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2522}
2523
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002524// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002525bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2526 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002527 if (mayBeCurLoad(MI1)) {
2528 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002529 unsigned DstReg = MI1.getOperand(0).getReg();
2530 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002531 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002532 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002533 return true;
2534 }
2535 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002536 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2537 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2538 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002539 return true;
2540 return false;
2541}
2542
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002543bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002544 const uint64_t V = getType(MI);
2545 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2546}
2547
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002548// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2549//
2550bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2551 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2552 VT == MVT::v32i16 || VT == MVT::v64i8) {
2553 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2554 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2555 (Offset & 0x3f) == 0);
2556 }
2557 // 128B
2558 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2559 VT == MVT::v64i16 || VT == MVT::v128i8) {
2560 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2561 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2562 (Offset & 0x7f) == 0);
2563 }
2564 if (VT == MVT::i64) {
2565 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2566 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2567 (Offset & 0x7) == 0);
2568 }
2569 if (VT == MVT::i32) {
2570 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2571 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2572 (Offset & 0x3) == 0);
2573 }
2574 if (VT == MVT::i16) {
2575 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2576 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2577 (Offset & 0x1) == 0);
2578 }
2579 if (VT == MVT::i8) {
2580 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2581 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2582 }
2583 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002584}
2585
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002586bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2587 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002588 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002589 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002590 // inserted to calculate the final address. Due to this reason, the function
2591 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002592 // We used to assert if the offset was not properly aligned, however,
2593 // there are cases where a misaligned pointer recast can cause this
2594 // problem, and we need to allow for it. The front end warns of such
2595 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002596
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002597 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002598 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002599 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002600 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002601 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002602 case Hexagon::V6_vL32b_ai:
2603 case Hexagon::V6_vS32b_ai:
2604 case Hexagon::V6_vL32Ub_ai:
2605 case Hexagon::V6_vS32Ub_ai:
2606 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2607 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2608
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002609 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002610 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002611 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002612 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002613 case Hexagon::V6_vL32b_ai_128B:
2614 case Hexagon::V6_vS32b_ai_128B:
2615 case Hexagon::V6_vL32Ub_ai_128B:
2616 case Hexagon::V6_vS32Ub_ai_128B:
2617 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2618 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2619
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002620 case Hexagon::J2_loop0i:
2621 case Hexagon::J2_loop1i:
2622 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002623
2624 case Hexagon::S4_storeirb_io:
2625 case Hexagon::S4_storeirbt_io:
2626 case Hexagon::S4_storeirbf_io:
2627 return isUInt<6>(Offset);
2628
2629 case Hexagon::S4_storeirh_io:
2630 case Hexagon::S4_storeirht_io:
2631 case Hexagon::S4_storeirhf_io:
2632 return isShiftedUInt<6,1>(Offset);
2633
2634 case Hexagon::S4_storeiri_io:
2635 case Hexagon::S4_storeirit_io:
2636 case Hexagon::S4_storeirif_io:
2637 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002638 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002639
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002640 if (Extend)
2641 return true;
2642
2643 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002644 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002645 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002646 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2647 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2648
Colin LeMahieu947cd702014-12-23 20:44:59 +00002649 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002650 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002651 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2652 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2653
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002654 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002655 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002656 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002657 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2658 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2659
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002660 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002661 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002662 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002663 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2664 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2665
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002666 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002667 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2668 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2669
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002670 case Hexagon::L4_iadd_memopw_io :
2671 case Hexagon::L4_isub_memopw_io :
2672 case Hexagon::L4_add_memopw_io :
2673 case Hexagon::L4_sub_memopw_io :
2674 case Hexagon::L4_and_memopw_io :
2675 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002676 return (0 <= Offset && Offset <= 255);
2677
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002678 case Hexagon::L4_iadd_memoph_io :
2679 case Hexagon::L4_isub_memoph_io :
2680 case Hexagon::L4_add_memoph_io :
2681 case Hexagon::L4_sub_memoph_io :
2682 case Hexagon::L4_and_memoph_io :
2683 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002684 return (0 <= Offset && Offset <= 127);
2685
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002686 case Hexagon::L4_iadd_memopb_io :
2687 case Hexagon::L4_isub_memopb_io :
2688 case Hexagon::L4_add_memopb_io :
2689 case Hexagon::L4_sub_memopb_io :
2690 case Hexagon::L4_and_memopb_io :
2691 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002692 return (0 <= Offset && Offset <= 63);
2693
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002694 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002695 // any size. Later pass knows how to handle it.
2696 case Hexagon::STriw_pred:
2697 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002698 case Hexagon::STriw_mod:
2699 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002700 return true;
2701
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002702 case Hexagon::PS_fi:
2703 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002704 case Hexagon::INLINEASM:
2705 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002706
2707 case Hexagon::L2_ploadrbt_io:
2708 case Hexagon::L2_ploadrbf_io:
2709 case Hexagon::L2_ploadrubt_io:
2710 case Hexagon::L2_ploadrubf_io:
2711 case Hexagon::S2_pstorerbt_io:
2712 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002713 return isUInt<6>(Offset);
2714
2715 case Hexagon::L2_ploadrht_io:
2716 case Hexagon::L2_ploadrhf_io:
2717 case Hexagon::L2_ploadruht_io:
2718 case Hexagon::L2_ploadruhf_io:
2719 case Hexagon::S2_pstorerht_io:
2720 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002721 return isShiftedUInt<6,1>(Offset);
2722
2723 case Hexagon::L2_ploadrit_io:
2724 case Hexagon::L2_ploadrif_io:
2725 case Hexagon::S2_pstorerit_io:
2726 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002727 return isShiftedUInt<6,2>(Offset);
2728
2729 case Hexagon::L2_ploadrdt_io:
2730 case Hexagon::L2_ploadrdf_io:
2731 case Hexagon::S2_pstorerdt_io:
2732 case Hexagon::S2_pstorerdf_io:
2733 return isShiftedUInt<6,3>(Offset);
2734 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002735
Benjamin Kramerb6684012011-12-27 11:41:05 +00002736 llvm_unreachable("No offset range is defined for this opcode. "
2737 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002738}
2739
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002740bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2741 return isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002742}
2743
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002744bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2745 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002746 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2747 return
2748 V == HexagonII::TypeCVI_VA ||
2749 V == HexagonII::TypeCVI_VA_DV;
2750}
Andrew Trickd06df962012-02-01 22:13:57 +00002751
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002752bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2753 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002754 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2755 return true;
2756
2757 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2758 return true;
2759
2760 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002761 return true;
2762
2763 return false;
2764}
Jyotsna Verma84256432013-03-01 17:37:13 +00002765
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002766bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2767 switch (MI.getOpcode()) {
2768 // Byte
2769 case Hexagon::L2_loadrub_io:
2770 case Hexagon::L4_loadrub_ur:
2771 case Hexagon::L4_loadrub_ap:
2772 case Hexagon::L2_loadrub_pr:
2773 case Hexagon::L2_loadrub_pbr:
2774 case Hexagon::L2_loadrub_pi:
2775 case Hexagon::L2_loadrub_pci:
2776 case Hexagon::L2_loadrub_pcr:
2777 case Hexagon::L2_loadbzw2_io:
2778 case Hexagon::L4_loadbzw2_ur:
2779 case Hexagon::L4_loadbzw2_ap:
2780 case Hexagon::L2_loadbzw2_pr:
2781 case Hexagon::L2_loadbzw2_pbr:
2782 case Hexagon::L2_loadbzw2_pi:
2783 case Hexagon::L2_loadbzw2_pci:
2784 case Hexagon::L2_loadbzw2_pcr:
2785 case Hexagon::L2_loadbzw4_io:
2786 case Hexagon::L4_loadbzw4_ur:
2787 case Hexagon::L4_loadbzw4_ap:
2788 case Hexagon::L2_loadbzw4_pr:
2789 case Hexagon::L2_loadbzw4_pbr:
2790 case Hexagon::L2_loadbzw4_pi:
2791 case Hexagon::L2_loadbzw4_pci:
2792 case Hexagon::L2_loadbzw4_pcr:
2793 case Hexagon::L4_loadrub_rr:
2794 case Hexagon::L2_ploadrubt_io:
2795 case Hexagon::L2_ploadrubt_pi:
2796 case Hexagon::L2_ploadrubf_io:
2797 case Hexagon::L2_ploadrubf_pi:
2798 case Hexagon::L2_ploadrubtnew_io:
2799 case Hexagon::L2_ploadrubfnew_io:
2800 case Hexagon::L4_ploadrubt_rr:
2801 case Hexagon::L4_ploadrubf_rr:
2802 case Hexagon::L4_ploadrubtnew_rr:
2803 case Hexagon::L4_ploadrubfnew_rr:
2804 case Hexagon::L2_ploadrubtnew_pi:
2805 case Hexagon::L2_ploadrubfnew_pi:
2806 case Hexagon::L4_ploadrubt_abs:
2807 case Hexagon::L4_ploadrubf_abs:
2808 case Hexagon::L4_ploadrubtnew_abs:
2809 case Hexagon::L4_ploadrubfnew_abs:
2810 case Hexagon::L2_loadrubgp:
2811 // Half
2812 case Hexagon::L2_loadruh_io:
2813 case Hexagon::L4_loadruh_ur:
2814 case Hexagon::L4_loadruh_ap:
2815 case Hexagon::L2_loadruh_pr:
2816 case Hexagon::L2_loadruh_pbr:
2817 case Hexagon::L2_loadruh_pi:
2818 case Hexagon::L2_loadruh_pci:
2819 case Hexagon::L2_loadruh_pcr:
2820 case Hexagon::L4_loadruh_rr:
2821 case Hexagon::L2_ploadruht_io:
2822 case Hexagon::L2_ploadruht_pi:
2823 case Hexagon::L2_ploadruhf_io:
2824 case Hexagon::L2_ploadruhf_pi:
2825 case Hexagon::L2_ploadruhtnew_io:
2826 case Hexagon::L2_ploadruhfnew_io:
2827 case Hexagon::L4_ploadruht_rr:
2828 case Hexagon::L4_ploadruhf_rr:
2829 case Hexagon::L4_ploadruhtnew_rr:
2830 case Hexagon::L4_ploadruhfnew_rr:
2831 case Hexagon::L2_ploadruhtnew_pi:
2832 case Hexagon::L2_ploadruhfnew_pi:
2833 case Hexagon::L4_ploadruht_abs:
2834 case Hexagon::L4_ploadruhf_abs:
2835 case Hexagon::L4_ploadruhtnew_abs:
2836 case Hexagon::L4_ploadruhfnew_abs:
2837 case Hexagon::L2_loadruhgp:
2838 return true;
2839 default:
2840 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002841 }
2842}
2843
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002844// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002845bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2846 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002847 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2848 if (!isVecUsableNextPacket(MI1, MI2))
2849 return true;
2850 return false;
2851}
2852
Brendon Cahoon254f8892016-07-29 16:44:44 +00002853/// \brief Get the base register and byte offset of a load/store instr.
2854bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2855 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2856 const {
2857 unsigned AccessSize = 0;
2858 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002859 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002860 Offset = OffsetVal;
2861 return BaseReg != 0;
2862}
2863
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002864/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002865bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2866 const MachineInstr &Second) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002867 if (DisableNVSchedule)
2868 return false;
2869 if (mayBeNewStore(Second)) {
2870 // Make sure the definition of the first instruction is the value being
2871 // stored.
2872 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002873 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002874 if (!Stored.isReg())
2875 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002876 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2877 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002878 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2879 return true;
2880 }
2881 }
2882 return false;
2883}
2884
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002885bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2886 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002887 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002888}
2889
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002890bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2891 for (auto &I : *B)
2892 if (I.isEHLabel())
2893 return true;
2894 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002895}
2896
Jyotsna Verma84256432013-03-01 17:37:13 +00002897// Returns true if an instruction can be converted into a non-extended
2898// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002899bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002900 short NonExtOpcode;
2901 // Check if the instruction has a register form that uses register in place
2902 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002903 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002904 return true;
2905
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002906 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002907 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002908
2909 switch (getAddrMode(MI)) {
2910 case HexagonII::Absolute :
2911 // Load/store with absolute addressing mode can be converted into
2912 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002913 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002914 break;
2915 case HexagonII::BaseImmOffset :
2916 // Load/store with base+offset addressing mode can be converted into
2917 // base+register offset addressing mode. However left shift operand should
2918 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002919 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002920 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002921 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002922 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002923 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002924 default:
2925 return false;
2926 }
2927 if (NonExtOpcode < 0)
2928 return false;
2929 return true;
2930 }
2931 return false;
2932}
2933
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002934bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2935 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002936 Hexagon::InstrType_Pseudo) >= 0;
2937}
2938
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002939bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2940 const {
2941 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2942 while (I != E) {
2943 if (I->isBarrier())
2944 return true;
2945 ++I;
2946 }
2947 return false;
2948}
2949
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002950// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002951bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
2952 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2953 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002954 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2955 HST.hasV60TOps();
2956}
2957
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002958// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002959bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2960 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002961 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2962}
2963
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002964bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2965 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002966 // There is no stall when ProdMI is not a V60 vector.
2967 if (!isV60VectorInstruction(ProdMI))
2968 return false;
2969
2970 // There is no stall when ProdMI and ConsMI are not dependent.
2971 if (!isDependent(ProdMI, ConsMI))
2972 return false;
2973
2974 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2975 // are scheduled in consecutive packets.
2976 if (isVecUsableNextPacket(ProdMI, ConsMI))
2977 return false;
2978
2979 return true;
2980}
2981
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002982bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002983 MachineBasicBlock::const_instr_iterator BII) const {
2984 // There is no stall when I is not a V60 vector.
2985 if (!isV60VectorInstruction(MI))
2986 return false;
2987
2988 MachineBasicBlock::const_instr_iterator MII = BII;
2989 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2990
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002991 if (!MII->isBundle()) {
2992 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002993 if (!isV60VectorInstruction(J))
2994 return false;
2995 else if (isVecUsableNextPacket(J, MI))
2996 return false;
2997 return true;
2998 }
2999
3000 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003001 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003002 if (producesStall(J, MI))
3003 return true;
3004 }
3005 return false;
3006}
3007
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003008bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003009 unsigned PredReg) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003010 for (unsigned opNum = 0; opNum < MI.getNumOperands(); opNum++) {
3011 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003012 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3013 return false; // Predicate register must be explicitly defined.
3014 }
3015
3016 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3017 // memd_locked cannot be used as .new as well,
3018 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003019 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003020}
3021
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003022bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3023 return (Opcode == Hexagon::J2_jumpt) ||
3024 (Opcode == Hexagon::J2_jumpf) ||
3025 (Opcode == Hexagon::J2_jumptnew) ||
3026 (Opcode == Hexagon::J2_jumpfnew) ||
3027 (Opcode == Hexagon::J2_jumptnewpt) ||
3028 (Opcode == Hexagon::J2_jumpfnewpt);
3029}
3030
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003031bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3032 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3033 return false;
3034 return !isPredicatedTrue(Cond[0].getImm());
3035}
3036
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003037short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3038 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003039}
3040
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003041unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3042 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003043 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3044}
3045
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003046// Returns the base register in a memory access (load/store). The offset is
3047// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003048unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003049 int &Offset, unsigned &AccessSize) const {
3050 // Return if it is not a base+offset type instruction or a MemOp.
3051 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3052 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003053 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003054 return 0;
3055
3056 // Since it is a memory access instruction, getMemAccessSize() should never
3057 // return 0.
3058 assert (getMemAccessSize(MI) &&
3059 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3060
3061 // Return Values of getMemAccessSize() are
3062 // 0 - Checked in the assert above.
3063 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3064 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3065 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3066
3067 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003068 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003069 return 0;
3070
3071 // Post increment updates its EA after the mem access,
3072 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003073 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003074 Offset = 0;
3075 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003076 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003077 }
3078
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003079 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003080}
3081
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003082/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003083bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003084 unsigned &BasePos, unsigned &OffsetPos) const {
3085 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003086 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003087 BasePos = 0;
3088 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003089 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003090 BasePos = 0;
3091 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003092 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003093 BasePos = 1;
3094 OffsetPos = 2;
3095 } else
3096 return false;
3097
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003098 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003099 BasePos++;
3100 OffsetPos++;
3101 }
3102 if (isPostIncrement(MI)) {
3103 BasePos++;
3104 OffsetPos++;
3105 }
3106
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003107 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003108 return false;
3109
3110 return true;
3111}
3112
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003113// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003114// e.g. jump_t t1 (i1)
3115// jump t2 (i2)
3116// Jumpers = {i2, i1}
3117SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3118 MachineBasicBlock& MBB) const {
3119 SmallVector<MachineInstr*, 2> Jumpers;
3120 // If the block has no terminators, it just falls into the block after it.
3121 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3122 if (I == MBB.instr_begin())
3123 return Jumpers;
3124
3125 // A basic block may looks like this:
3126 //
3127 // [ insn
3128 // EH_LABEL
3129 // insn
3130 // insn
3131 // insn
3132 // EH_LABEL
3133 // insn ]
3134 //
3135 // It has two succs but does not have a terminator
3136 // Don't know how to handle it.
3137 do {
3138 --I;
3139 if (I->isEHLabel())
3140 return Jumpers;
3141 } while (I != MBB.instr_begin());
3142
3143 I = MBB.instr_end();
3144 --I;
3145
3146 while (I->isDebugValue()) {
3147 if (I == MBB.instr_begin())
3148 return Jumpers;
3149 --I;
3150 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003151 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003152 return Jumpers;
3153
3154 // Get the last instruction in the block.
3155 MachineInstr *LastInst = &*I;
3156 Jumpers.push_back(LastInst);
3157 MachineInstr *SecondLastInst = nullptr;
3158 // Find one more terminator if present.
3159 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003160 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003161 if (!SecondLastInst) {
3162 SecondLastInst = &*I;
3163 Jumpers.push_back(SecondLastInst);
3164 } else // This is a third branch.
3165 return Jumpers;
3166 }
3167 if (I == MBB.instr_begin())
3168 break;
3169 --I;
3170 } while (true);
3171 return Jumpers;
3172}
3173
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003174short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3175 if (Opcode < 0)
3176 return -1;
3177 return Hexagon::getBaseWithLongOffset(Opcode);
3178}
3179
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003180short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3181 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003182}
3183
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003184short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3185 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003186}
3187
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003188// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003189unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3190 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003191 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3192}
3193
3194// See if instruction could potentially be a duplex candidate.
3195// If so, return its group. Zero otherwise.
3196HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003197 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003198 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3199
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003200 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003201 default:
3202 return HexagonII::HCG_None;
3203 //
3204 // Compound pairs.
3205 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3206 // "Rd16=#U6 ; jump #r9:2"
3207 // "Rd16=Rs16 ; jump #r9:2"
3208 //
3209 case Hexagon::C2_cmpeq:
3210 case Hexagon::C2_cmpgt:
3211 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003212 DstReg = MI.getOperand(0).getReg();
3213 Src1Reg = MI.getOperand(1).getReg();
3214 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003215 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3216 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3217 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3218 return HexagonII::HCG_A;
3219 break;
3220 case Hexagon::C2_cmpeqi:
3221 case Hexagon::C2_cmpgti:
3222 case Hexagon::C2_cmpgtui:
3223 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003224 DstReg = MI.getOperand(0).getReg();
3225 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003226 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3227 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003228 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3229 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3230 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003231 return HexagonII::HCG_A;
3232 break;
3233 case Hexagon::A2_tfr:
3234 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003235 DstReg = MI.getOperand(0).getReg();
3236 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003237 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3238 return HexagonII::HCG_A;
3239 break;
3240 case Hexagon::A2_tfrsi:
3241 // Rd = #u6
3242 // Do not test for #u6 size since the const is getting extended
3243 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003244 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003245 if (isIntRegForSubInst(DstReg))
3246 return HexagonII::HCG_A;
3247 break;
3248 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003249 DstReg = MI.getOperand(0).getReg();
3250 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003251 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3252 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003253 MI.getOperand(2).isImm() &&
3254 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003255 return HexagonII::HCG_A;
3256 break;
3257 // The fact that .new form is used pretty much guarantees
3258 // that predicate register will match. Nevertheless,
3259 // there could be some false positives without additional
3260 // checking.
3261 case Hexagon::J2_jumptnew:
3262 case Hexagon::J2_jumpfnew:
3263 case Hexagon::J2_jumptnewpt:
3264 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003265 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003266 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3267 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3268 return HexagonII::HCG_B;
3269 break;
3270 // Transfer and jump:
3271 // Rd=#U6 ; jump #r9:2
3272 // Rd=Rs ; jump #r9:2
3273 // Do not test for jump range here.
3274 case Hexagon::J2_jump:
3275 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003276 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003277 return HexagonII::HCG_C;
3278 break;
3279 }
3280
3281 return HexagonII::HCG_None;
3282}
3283
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003284// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003285unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3286 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003287 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3288 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003289 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3290 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003291 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003292 unsigned DestReg = GA.getOperand(0).getReg();
3293 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003294 return -1;
3295 if (DestReg == Hexagon::P0)
3296 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3297 if (DestReg == Hexagon::P1)
3298 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3299 return -1;
3300}
3301
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003302int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3303 enum Hexagon::PredSense inPredSense;
3304 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3305 Hexagon::PredSense_true;
3306 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3307 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3308 return CondOpcode;
3309
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003310 llvm_unreachable("Unexpected predicable instruction");
3311}
3312
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003313// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003314int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3315 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003316 default: llvm_unreachable("Unknown .cur type");
3317 case Hexagon::V6_vL32b_pi:
3318 return Hexagon::V6_vL32b_cur_pi;
3319 case Hexagon::V6_vL32b_ai:
3320 return Hexagon::V6_vL32b_cur_ai;
3321 //128B
3322 case Hexagon::V6_vL32b_pi_128B:
3323 return Hexagon::V6_vL32b_cur_pi_128B;
3324 case Hexagon::V6_vL32b_ai_128B:
3325 return Hexagon::V6_vL32b_cur_ai_128B;
3326 }
3327 return 0;
3328}
3329
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003330// The diagram below shows the steps involved in the conversion of a predicated
3331// store instruction to its .new predicated new-value form.
3332//
3333// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3334// ^ ^
3335// / \ (not OK. it will cause new-value store to be
3336// / X conditional on p0.new while R2 producer is
3337// / \ on p0)
3338// / \.
3339// p.new store p.old NV store
3340// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3341// ^ ^
3342// \ /
3343// \ /
3344// \ /
3345// p.old store
3346// [if (p0)memw(R0+#0)=R2]
3347//
3348//
3349// The following set of instructions further explains the scenario where
3350// conditional new-value store becomes invalid when promoted to .new predicate
3351// form.
3352//
3353// { 1) if (p0) r0 = add(r1, r2)
3354// 2) p0 = cmp.eq(r3, #0) }
3355//
3356// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3357// the first two instructions because in instr 1, r0 is conditional on old value
3358// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3359// is not valid for new-value stores.
3360// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3361// from the "Conditional Store" list. Because a predicated new value store
3362// would NOT be promoted to a double dot new store. See diagram below:
3363// This function returns yes for those stores that are predicated but not
3364// yet promoted to predicate dot new instructions.
3365//
3366// +---------------------+
3367// /-----| if (p0) memw(..)=r0 |---------\~
3368// || +---------------------+ ||
3369// promote || /\ /\ || promote
3370// || /||\ /||\ ||
3371// \||/ demote || \||/
3372// \/ || || \/
3373// +-------------------------+ || +-------------------------+
3374// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3375// +-------------------------+ || +-------------------------+
3376// || || ||
3377// || demote \||/
3378// promote || \/ NOT possible
3379// || || /\~
3380// \||/ || /||\~
3381// \/ || ||
3382// +-----------------------------+
3383// | if (p0.new) memw(..)=r0.new |
3384// +-----------------------------+
3385// Double Dot New Store
3386//
3387// Returns the most basic instruction for the .new predicated instructions and
3388// new-value stores.
3389// For example, all of the following instructions will be converted back to the
3390// same instruction:
3391// 1) if (p0.new) memw(R0+#0) = R1.new --->
3392// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3393// 3) if (p0.new) memw(R0+#0) = R1 --->
3394//
3395// To understand the translation of instruction 1 to its original form, consider
3396// a packet with 3 instructions.
3397// { p0 = cmp.eq(R0,R1)
3398// if (p0.new) R2 = add(R3, R4)
3399// R5 = add (R3, R1)
3400// }
3401// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3402//
3403// This instruction can be part of the previous packet only if both p0 and R2
3404// are promoted to .new values. This promotion happens in steps, first
3405// predicate register is promoted to .new and in the next iteration R2 is
3406// promoted. Therefore, in case of dependence check failure (due to R5) during
3407// next iteration, it should be converted back to its most basic form.
3408
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003409// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003410int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3411 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003412 if (NVOpcode >= 0) // Valid new-value store instruction.
3413 return NVOpcode;
3414
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003415 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003416 default: llvm_unreachable("Unknown .new type");
3417 case Hexagon::S4_storerb_ur:
3418 return Hexagon::S4_storerbnew_ur;
3419
3420 case Hexagon::S2_storerb_pci:
3421 return Hexagon::S2_storerb_pci;
3422
3423 case Hexagon::S2_storeri_pci:
3424 return Hexagon::S2_storeri_pci;
3425
3426 case Hexagon::S2_storerh_pci:
3427 return Hexagon::S2_storerh_pci;
3428
3429 case Hexagon::S2_storerd_pci:
3430 return Hexagon::S2_storerd_pci;
3431
3432 case Hexagon::S2_storerf_pci:
3433 return Hexagon::S2_storerf_pci;
3434
3435 case Hexagon::V6_vS32b_ai:
3436 return Hexagon::V6_vS32b_new_ai;
3437
3438 case Hexagon::V6_vS32b_pi:
3439 return Hexagon::V6_vS32b_new_pi;
3440
3441 // 128B
3442 case Hexagon::V6_vS32b_ai_128B:
3443 return Hexagon::V6_vS32b_new_ai_128B;
3444
3445 case Hexagon::V6_vS32b_pi_128B:
3446 return Hexagon::V6_vS32b_new_pi_128B;
3447 }
3448 return 0;
3449}
3450
3451// Returns the opcode to use when converting MI, which is a conditional jump,
3452// into a conditional instruction which uses the .new value of the predicate.
3453// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003454int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003455 const MachineBranchProbabilityInfo *MBPI) const {
3456 // We assume that block can have at most two successors.
3457 bool taken = false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003458 const MachineBasicBlock *Src = MI.getParent();
3459 const MachineOperand &BrTarget = MI.getOperand(1);
3460 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003461
3462 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3463 if (Prediction >= BranchProbability(1,2))
3464 taken = true;
3465
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003466 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003467 case Hexagon::J2_jumpt:
3468 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3469 case Hexagon::J2_jumpf:
3470 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3471
3472 default:
3473 llvm_unreachable("Unexpected jump instruction.");
3474 }
3475}
3476
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003477// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003478int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003479 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003480 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003481 if (NewOpcode >= 0) // Valid predicate new instruction
3482 return NewOpcode;
3483
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003484 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003485 // Condtional Jumps
3486 case Hexagon::J2_jumpt:
3487 case Hexagon::J2_jumpf:
3488 return getDotNewPredJumpOp(MI, MBPI);
3489
3490 default:
3491 assert(0 && "Unknown .new type");
3492 }
3493 return 0;
3494}
3495
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003496int HexagonInstrInfo::getDotOldOp(const int opc) const {
3497 int NewOp = opc;
3498 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3499 NewOp = Hexagon::getPredOldOpcode(NewOp);
3500 assert(NewOp >= 0 &&
3501 "Couldn't change predicate new instruction to its old form.");
3502 }
3503
3504 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3505 NewOp = Hexagon::getNonNVStore(NewOp);
3506 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3507 }
3508 return NewOp;
3509}
3510
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003511// See if instruction could potentially be a duplex candidate.
3512// If so, return its group. Zero otherwise.
3513HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003514 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003515 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3516 auto &HRI = getRegisterInfo();
3517
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003518 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003519 default:
3520 return HexagonII::HSIG_None;
3521 //
3522 // Group L1:
3523 //
3524 // Rd = memw(Rs+#u4:2)
3525 // Rd = memub(Rs+#u4:0)
3526 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003527 DstReg = MI.getOperand(0).getReg();
3528 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003529 // Special case this one from Group L2.
3530 // Rd = memw(r29+#u5:2)
3531 if (isIntRegForSubInst(DstReg)) {
3532 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3533 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003534 MI.getOperand(2).isImm() &&
3535 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003536 return HexagonII::HSIG_L2;
3537 // Rd = memw(Rs+#u4:2)
3538 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003539 (MI.getOperand(2).isImm() &&
3540 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003541 return HexagonII::HSIG_L1;
3542 }
3543 break;
3544 case Hexagon::L2_loadrub_io:
3545 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003546 DstReg = MI.getOperand(0).getReg();
3547 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003548 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003549 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003550 return HexagonII::HSIG_L1;
3551 break;
3552 //
3553 // Group L2:
3554 //
3555 // Rd = memh/memuh(Rs+#u3:1)
3556 // Rd = memb(Rs+#u3:0)
3557 // Rd = memw(r29+#u5:2) - Handled above.
3558 // Rdd = memd(r29+#u5:3)
3559 // deallocframe
3560 // [if ([!]p0[.new])] dealloc_return
3561 // [if ([!]p0[.new])] jumpr r31
3562 case Hexagon::L2_loadrh_io:
3563 case Hexagon::L2_loadruh_io:
3564 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003565 DstReg = MI.getOperand(0).getReg();
3566 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003567 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003568 MI.getOperand(2).isImm() &&
3569 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003570 return HexagonII::HSIG_L2;
3571 break;
3572 case Hexagon::L2_loadrb_io:
3573 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003574 DstReg = MI.getOperand(0).getReg();
3575 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003576 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003577 MI.getOperand(2).isImm() &&
3578 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003579 return HexagonII::HSIG_L2;
3580 break;
3581 case Hexagon::L2_loadrd_io:
3582 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003583 DstReg = MI.getOperand(0).getReg();
3584 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003585 if (isDblRegForSubInst(DstReg, HRI) &&
3586 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3587 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003588 MI.getOperand(2).isImm() &&
3589 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003590 return HexagonII::HSIG_L2;
3591 break;
3592 // dealloc_return is not documented in Hexagon Manual, but marked
3593 // with A_SUBINSN attribute in iset_v4classic.py.
3594 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003595 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003596 case Hexagon::L4_return:
3597 case Hexagon::L2_deallocframe:
3598 return HexagonII::HSIG_L2;
3599 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003600 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003601 // jumpr r31
3602 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003603 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003604 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3605 return HexagonII::HSIG_L2;
3606 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003607 case Hexagon::PS_jmprett:
3608 case Hexagon::PS_jmpretf:
3609 case Hexagon::PS_jmprettnewpt:
3610 case Hexagon::PS_jmpretfnewpt:
3611 case Hexagon::PS_jmprettnew:
3612 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003613 DstReg = MI.getOperand(1).getReg();
3614 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003615 // [if ([!]p0[.new])] jumpr r31
3616 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3617 (Hexagon::P0 == SrcReg)) &&
3618 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3619 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003620 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003621 case Hexagon::L4_return_t :
3622 case Hexagon::L4_return_f :
3623 case Hexagon::L4_return_tnew_pnt :
3624 case Hexagon::L4_return_fnew_pnt :
3625 case Hexagon::L4_return_tnew_pt :
3626 case Hexagon::L4_return_fnew_pt :
3627 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003628 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003629 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3630 return HexagonII::HSIG_L2;
3631 break;
3632 //
3633 // Group S1:
3634 //
3635 // memw(Rs+#u4:2) = Rt
3636 // memb(Rs+#u4:0) = Rt
3637 case Hexagon::S2_storeri_io:
3638 // Special case this one from Group S2.
3639 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003640 Src1Reg = MI.getOperand(0).getReg();
3641 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003642 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3643 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003644 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3645 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003646 return HexagonII::HSIG_S2;
3647 // memw(Rs+#u4:2) = Rt
3648 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003649 MI.getOperand(1).isImm() &&
3650 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003651 return HexagonII::HSIG_S1;
3652 break;
3653 case Hexagon::S2_storerb_io:
3654 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003655 Src1Reg = MI.getOperand(0).getReg();
3656 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003657 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003658 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003659 return HexagonII::HSIG_S1;
3660 break;
3661 //
3662 // Group S2:
3663 //
3664 // memh(Rs+#u3:1) = Rt
3665 // memw(r29+#u5:2) = Rt
3666 // memd(r29+#s6:3) = Rtt
3667 // memw(Rs+#u4:2) = #U1
3668 // memb(Rs+#u4) = #U1
3669 // allocframe(#u5:3)
3670 case Hexagon::S2_storerh_io:
3671 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003672 Src1Reg = MI.getOperand(0).getReg();
3673 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003674 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003675 MI.getOperand(1).isImm() &&
3676 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003677 return HexagonII::HSIG_S1;
3678 break;
3679 case Hexagon::S2_storerd_io:
3680 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003681 Src1Reg = MI.getOperand(0).getReg();
3682 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003683 if (isDblRegForSubInst(Src2Reg, HRI) &&
3684 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003685 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3686 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003687 return HexagonII::HSIG_S2;
3688 break;
3689 case Hexagon::S4_storeiri_io:
3690 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003691 Src1Reg = MI.getOperand(0).getReg();
3692 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3693 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3694 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003695 return HexagonII::HSIG_S2;
3696 break;
3697 case Hexagon::S4_storeirb_io:
3698 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003699 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003700 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003701 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3702 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003703 return HexagonII::HSIG_S2;
3704 break;
3705 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003706 if (MI.getOperand(0).isImm() &&
3707 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003708 return HexagonII::HSIG_S1;
3709 break;
3710 //
3711 // Group A:
3712 //
3713 // Rx = add(Rx,#s7)
3714 // Rd = Rs
3715 // Rd = #u6
3716 // Rd = #-1
3717 // if ([!]P0[.new]) Rd = #0
3718 // Rd = add(r29,#u6:2)
3719 // Rx = add(Rx,Rs)
3720 // P0 = cmp.eq(Rs,#u2)
3721 // Rdd = combine(#0,Rs)
3722 // Rdd = combine(Rs,#0)
3723 // Rdd = combine(#u2,#U2)
3724 // Rd = add(Rs,#1)
3725 // Rd = add(Rs,#-1)
3726 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3727 // Rd = and(Rs,#1)
3728 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003729 DstReg = MI.getOperand(0).getReg();
3730 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003731 if (isIntRegForSubInst(DstReg)) {
3732 // Rd = add(r29,#u6:2)
3733 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003734 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3735 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003736 return HexagonII::HSIG_A;
3737 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003738 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3739 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003740 return HexagonII::HSIG_A;
3741 // Rd = add(Rs,#1)
3742 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003743 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3744 ((MI.getOperand(2).getImm() == 1) ||
3745 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003746 return HexagonII::HSIG_A;
3747 }
3748 break;
3749 case Hexagon::A2_add:
3750 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003751 DstReg = MI.getOperand(0).getReg();
3752 Src1Reg = MI.getOperand(1).getReg();
3753 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003754 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3755 isIntRegForSubInst(Src2Reg))
3756 return HexagonII::HSIG_A;
3757 break;
3758 case Hexagon::A2_andir:
3759 // Same as zxtb.
3760 // Rd16=and(Rs16,#255)
3761 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003762 DstReg = MI.getOperand(0).getReg();
3763 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003764 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003765 MI.getOperand(2).isImm() &&
3766 ((MI.getOperand(2).getImm() == 1) ||
3767 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003768 return HexagonII::HSIG_A;
3769 break;
3770 case Hexagon::A2_tfr:
3771 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003772 DstReg = MI.getOperand(0).getReg();
3773 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003774 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3775 return HexagonII::HSIG_A;
3776 break;
3777 case Hexagon::A2_tfrsi:
3778 // Rd = #u6
3779 // Do not test for #u6 size since the const is getting extended
3780 // regardless and compound could be formed.
3781 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003782 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003783 if (isIntRegForSubInst(DstReg))
3784 return HexagonII::HSIG_A;
3785 break;
3786 case Hexagon::C2_cmoveit:
3787 case Hexagon::C2_cmovenewit:
3788 case Hexagon::C2_cmoveif:
3789 case Hexagon::C2_cmovenewif:
3790 // if ([!]P0[.new]) Rd = #0
3791 // Actual form:
3792 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003793 DstReg = MI.getOperand(0).getReg();
3794 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003795 if (isIntRegForSubInst(DstReg) &&
3796 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003797 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003798 return HexagonII::HSIG_A;
3799 break;
3800 case Hexagon::C2_cmpeqi:
3801 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003802 DstReg = MI.getOperand(0).getReg();
3803 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003804 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3805 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003806 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003807 return HexagonII::HSIG_A;
3808 break;
3809 case Hexagon::A2_combineii:
3810 case Hexagon::A4_combineii:
3811 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003812 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003813 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003814 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3815 (MI.getOperand(1).isGlobal() &&
3816 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3817 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3818 (MI.getOperand(2).isGlobal() &&
3819 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003820 return HexagonII::HSIG_A;
3821 break;
3822 case Hexagon::A4_combineri:
3823 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003824 DstReg = MI.getOperand(0).getReg();
3825 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003826 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003827 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3828 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003829 return HexagonII::HSIG_A;
3830 break;
3831 case Hexagon::A4_combineir:
3832 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003833 DstReg = MI.getOperand(0).getReg();
3834 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003835 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003836 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3837 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003838 return HexagonII::HSIG_A;
3839 break;
3840 case Hexagon::A2_sxtb:
3841 case Hexagon::A2_sxth:
3842 case Hexagon::A2_zxtb:
3843 case Hexagon::A2_zxth:
3844 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003845 DstReg = MI.getOperand(0).getReg();
3846 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003847 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3848 return HexagonII::HSIG_A;
3849 break;
3850 }
3851
3852 return HexagonII::HSIG_None;
3853}
3854
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003855short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3856 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003857}
3858
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003859// Return first non-debug instruction in the basic block.
3860MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3861 const {
3862 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003863 MachineInstr &MI = *MII;
3864 if (MI.isDebugValue())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003865 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003866 return &MI;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003867 }
3868 return nullptr;
3869}
3870
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003871unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003872 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003873 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3874 // still have a MinLatency property, which getStageLatency checks.
3875 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003876 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003877
3878 // Get the latency embedded in the itinerary. If we're not using timing class
3879 // latencies or if we using BSB scheduling, then restrict the maximum latency
3880 // to 1 (that is, either 0 or 1).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003881 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003882 return 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003883 unsigned Latency = ItinData->getStageLatency(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003884 if (!EnableTimingClassLatency ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003885 MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>().
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003886 useBSBScheduling())
3887 if (Latency > 1)
3888 Latency = 1;
3889 return Latency;
3890}
3891
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003892// inverts the predication logic.
3893// p -> NotP
3894// NotP -> P
3895bool HexagonInstrInfo::getInvertedPredSense(
3896 SmallVectorImpl<MachineOperand> &Cond) const {
3897 if (Cond.empty())
3898 return false;
3899 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3900 Cond[0].setImm(Opc);
3901 return true;
3902}
3903
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003904unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3905 int InvPredOpcode;
3906 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3907 : Hexagon::getTruePredOpcode(Opc);
3908 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3909 return InvPredOpcode;
3910
3911 llvm_unreachable("Unexpected predicated instruction");
3912}
3913
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003914// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003915int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
3916 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003917 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3918 & HexagonII::ExtentSignedMask;
3919 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3920 & HexagonII::ExtentBitsMask;
3921
3922 if (isSigned) // if value is signed
3923 return ~(-1U << (bits - 1));
3924 else
3925 return ~(-1U << bits);
3926}
3927
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003928unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
3929 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003930 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3931}
3932
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003933// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003934int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
3935 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003936 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3937 & HexagonII::ExtentSignedMask;
3938 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3939 & HexagonII::ExtentBitsMask;
3940
3941 if (isSigned) // if value is signed
3942 return -1U << (bits - 1);
3943 else
3944 return 0;
3945}
3946
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003947// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003948short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003949 // Check if the instruction has a register form that uses register in place
3950 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003951 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003952 if (NonExtOpcode >= 0)
3953 return NonExtOpcode;
3954
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003955 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003956 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003957 switch (getAddrMode(MI)) {
3958 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003959 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003960 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003961 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003962 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003963 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003964
Jyotsna Verma84256432013-03-01 17:37:13 +00003965 default:
3966 return -1;
3967 }
3968 }
3969 return -1;
3970}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00003971
Ahmed Bougachac88bf542015-06-11 19:30:37 +00003972bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003973 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00003974 if (Cond.empty())
3975 return false;
3976 assert(Cond.size() == 2);
3977 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003978 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3979 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00003980 }
3981 PredReg = Cond[1].getReg();
3982 PredRegPos = 1;
3983 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3984 PredRegFlags = 0;
3985 if (Cond[1].isImplicit())
3986 PredRegFlags = RegState::Implicit;
3987 if (Cond[1].isUndef())
3988 PredRegFlags |= RegState::Undef;
3989 return true;
3990}
3991
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003992short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
3993 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003994}
3995
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003996short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
3997 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003998}
3999
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004000// Return the number of bytes required to encode the instruction.
4001// Hexagon instructions are fixed length, 4 bytes, unless they
4002// use a constant extender, which requires another 4 bytes.
4003// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004004unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4005 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004006 return 0;
4007
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004008 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004009 if (!Size)
4010 // Assume the default insn size in case it cannot be determined
4011 // for whatever reason.
4012 Size = HEXAGON_INSTR_SIZE;
4013
4014 if (isConstExtended(MI) || isExtended(MI))
4015 Size += HEXAGON_INSTR_SIZE;
4016
4017 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004018 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4019 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004020 const MachineFunction *MF = MBB.getParent();
4021 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4022
4023 // Count the number of register definitions to find the asm string.
4024 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004025 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004026 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004027 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004028
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004029 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004030 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004031 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004032 Size = getInlineAsmLength(AsmStr, *MAI);
4033 }
4034
4035 return Size;
4036}
4037
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004038uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4039 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004040 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4041}
4042
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004043unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4044 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004045 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004046 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004047
4048 return IS.getUnits();
4049}
4050
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004051unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4052 const uint64_t F = get(Opcode).TSFlags;
4053 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4054}
4055
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004056// Calculate size of the basic block without debug instructions.
4057unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4058 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4059}
4060
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004061unsigned HexagonInstrInfo::nonDbgBundleSize(
4062 MachineBasicBlock::const_iterator BundleHead) const {
4063 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004064 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004065 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004066 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004067}
4068
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004069/// immediateExtend - Changes the instruction in place to one using an immediate
4070/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004071void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004072 assert((isExtendable(MI)||isConstExtended(MI)) &&
4073 "Instruction must be extendable");
4074 // Find which operand is extendable.
4075 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004076 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004077 // This needs to be something we understand.
4078 assert((MO.isMBB() || MO.isImm()) &&
4079 "Branch with unknown extendable field type");
4080 // Mark given operand as extended.
4081 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4082}
4083
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004084bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004085 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004086 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004087 << NewTarget->getNumber(); MI.dump(););
4088 assert(MI.isBranch());
4089 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4090 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004091 // In general branch target is the last operand,
4092 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004093 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004094 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004095 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4096 MI.getOperand(TargetPos).setMBB(NewTarget);
4097 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004098 NewOpcode = reversePrediction(NewOpcode);
4099 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004100 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004101 return true;
4102}
4103
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004104void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4105 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4106 MachineFunction::iterator A = MF.begin();
4107 MachineBasicBlock &B = *A;
4108 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004109 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004110 MachineInstr *NewMI;
4111
4112 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4113 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004114 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004115 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4116 " Class: " << NewMI->getDesc().getSchedClass());
4117 NewMI->eraseFromParent();
4118 }
4119 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4120}
4121
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004122// inverts the predication logic.
4123// p -> NotP
4124// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004125bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4126 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4127 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004128 return true;
4129}
4130
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004131// Reverse the branch prediction.
4132unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4133 int PredRevOpcode = -1;
4134 if (isPredictedTaken(Opcode))
4135 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4136 else
4137 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4138 assert(PredRevOpcode > 0);
4139 return PredRevOpcode;
4140}
4141
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004142// TODO: Add more rigorous validation.
4143bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4144 const {
4145 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4146}
4147
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004148short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4149 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004150}