blob: 0a7dc6b49d000cd8e5d89046091049d4e244b11d [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))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001077 .addOperand(MI.getOperand(0))
1078 .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))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001083 .addOperand(MI.getOperand(0))
1084 // 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;
1109 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001110 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001111 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001112 .addOperand(MI.getOperand(1))
1113 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001114 MI1New->getOperand(1).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001115 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001116 HRI.getSubReg(DstReg, Hexagon::vsub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001117 .addOperand(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001118 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001119 .addImm(MI.getOperand(2).getImm() + Offset)
1120 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001121 MBB.erase(MI);
1122 return true;
1123 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001124 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001125 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001126 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1127 .addReg(Reg, RegState::Undef)
1128 .addReg(Reg, RegState::Undef);
1129 MBB.erase(MI);
1130 return true;
1131 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001132 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001133 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001134 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1135 .addReg(Reg, RegState::Undef)
1136 .addReg(Reg, RegState::Undef);
1137 MBB.erase(MI);
1138 return true;
1139 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001140 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001141 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001142 unsigned DstReg = MI.getOperand(0).getReg();
1143 unsigned Src1Reg = MI.getOperand(1).getReg();
1144 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001145 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1146 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1147 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1148 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001149 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001150 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001151 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001152 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001153 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001154 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001155 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001156 .addReg(Src2SubLo);
1157 MBB.erase(MI);
1158 MRI.clearKillFlags(Src1SubHi);
1159 MRI.clearKillFlags(Src1SubLo);
1160 MRI.clearKillFlags(Src2SubHi);
1161 MRI.clearKillFlags(Src2SubLo);
1162 return true;
1163 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001164 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001165 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001166 unsigned DstReg = MI.getOperand(0).getReg();
1167 unsigned Src1Reg = MI.getOperand(1).getReg();
1168 unsigned Src2Reg = MI.getOperand(2).getReg();
1169 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001170 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1171 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1172 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1173 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1174 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1175 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001176 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001177 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001178 .addReg(Src1SubHi)
1179 .addReg(Src2SubHi)
1180 .addReg(Src3SubHi);
1181 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001182 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001183 .addReg(Src1SubLo)
1184 .addReg(Src2SubLo)
1185 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001186 MBB.erase(MI);
1187 MRI.clearKillFlags(Src1SubHi);
1188 MRI.clearKillFlags(Src1SubLo);
1189 MRI.clearKillFlags(Src2SubHi);
1190 MRI.clearKillFlags(Src2SubLo);
1191 MRI.clearKillFlags(Src3SubHi);
1192 MRI.clearKillFlags(Src3SubLo);
1193 return true;
1194 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001195 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001196 const MachineOperand &Op0 = MI.getOperand(0);
1197 const MachineOperand &Op1 = MI.getOperand(1);
1198 const MachineOperand &Op2 = MI.getOperand(2);
1199 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001200 unsigned Rd = Op0.getReg();
1201 unsigned Pu = Op1.getReg();
1202 unsigned Rs = Op2.getReg();
1203 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001204 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001205 unsigned K1 = getKillRegState(Op1.isKill());
1206 unsigned K2 = getKillRegState(Op2.isKill());
1207 unsigned K3 = getKillRegState(Op3.isKill());
1208 if (Rd != Rs)
1209 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1210 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1211 .addReg(Rs, K2);
1212 if (Rd != Rt)
1213 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1214 .addReg(Pu, K1)
1215 .addReg(Rt, K3);
1216 MBB.erase(MI);
1217 return true;
1218 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001219 case Hexagon::PS_vselect:
1220 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001221 const MachineOperand &Op0 = MI.getOperand(0);
1222 const MachineOperand &Op1 = MI.getOperand(1);
1223 const MachineOperand &Op2 = MI.getOperand(2);
1224 const MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001225 LivePhysRegs LiveAtMI(&HRI);
1226 getLiveRegsAt(LiveAtMI, MI);
1227 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1228 if (Op0.getReg() != Op2.getReg()) {
1229 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1230 .addOperand(Op0)
1231 .addOperand(Op1)
1232 .addOperand(Op2);
1233 if (IsDestLive)
1234 T.addReg(Op0.getReg(), RegState::Implicit);
1235 IsDestLive = true;
1236 }
1237 if (Op0.getReg() != Op3.getReg()) {
1238 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1239 .addOperand(Op0)
1240 .addOperand(Op1)
1241 .addOperand(Op3);
1242 if (IsDestLive)
1243 T.addReg(Op0.getReg(), RegState::Implicit);
1244 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001245 MBB.erase(MI);
1246 return true;
1247 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001248 case Hexagon::PS_wselect:
1249 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001250 MachineOperand &Op0 = MI.getOperand(0);
1251 MachineOperand &Op1 = MI.getOperand(1);
1252 MachineOperand &Op2 = MI.getOperand(2);
1253 MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001254 LivePhysRegs LiveAtMI(&HRI);
1255 getLiveRegsAt(LiveAtMI, MI);
1256 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1257
1258 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001259 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1260 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001261 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1262 .addOperand(Op0)
1263 .addOperand(Op1)
1264 .addReg(SrcHi)
1265 .addReg(SrcLo);
1266 if (IsDestLive)
1267 T.addReg(Op0.getReg(), RegState::Implicit);
1268 IsDestLive = true;
1269 }
1270 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001271 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1272 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001273 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1274 .addOperand(Op0)
1275 .addOperand(Op1)
1276 .addReg(SrcHi)
1277 .addReg(SrcLo);
1278 if (IsDestLive)
1279 T.addReg(Op0.getReg(), RegState::Implicit);
1280 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001281 MBB.erase(MI);
1282 return true;
1283 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001284 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001285 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001286 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001287 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001288 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001289 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001290 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001291 case Hexagon::PS_jmprett:
1292 MI.setDesc(get(Hexagon::J2_jumprt));
1293 return true;
1294 case Hexagon::PS_jmpretf:
1295 MI.setDesc(get(Hexagon::J2_jumprf));
1296 return true;
1297 case Hexagon::PS_jmprettnewpt:
1298 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1299 return true;
1300 case Hexagon::PS_jmpretfnewpt:
1301 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1302 return true;
1303 case Hexagon::PS_jmprettnew:
1304 MI.setDesc(get(Hexagon::J2_jumprtnew));
1305 return true;
1306 case Hexagon::PS_jmpretfnew:
1307 MI.setDesc(get(Hexagon::J2_jumprfnew));
1308 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001309 }
1310
1311 return false;
1312}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001313
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001314// We indicate that we want to reverse the branch by
1315// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001316bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001317 SmallVectorImpl<MachineOperand> &Cond) const {
1318 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001319 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001320 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1321 unsigned opcode = Cond[0].getImm();
1322 //unsigned temp;
1323 assert(get(opcode).isBranch() && "Should be a branching condition.");
1324 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001325 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001326 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1327 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001328 return false;
1329}
1330
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001331void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1332 MachineBasicBlock::iterator MI) const {
1333 DebugLoc DL;
1334 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1335}
1336
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001337bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1338 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001339}
1340
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001341// Returns true if an instruction is predicated irrespective of the predicate
1342// sense. For example, all of the following will return true.
1343// if (p0) R1 = add(R2, R3)
1344// if (!p0) R1 = add(R2, R3)
1345// if (p0.new) R1 = add(R2, R3)
1346// if (!p0.new) R1 = add(R2, R3)
1347// Note: New-value stores are not included here as in the current
1348// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001349bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1350 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001351 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001352}
1353
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001354bool HexagonInstrInfo::PredicateInstruction(
1355 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001356 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1357 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001358 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001359 return false;
1360 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001361 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001362 assert (isPredicable(MI) && "Expected predicable instruction");
1363 bool invertJump = predOpcodeHasNot(Cond);
1364
1365 // We have to predicate MI "in place", i.e. after this function returns,
1366 // MI will need to be transformed into a predicated form. To avoid com-
1367 // plicated manipulations with the operands (handling tied operands,
1368 // etc.), build a new temporary instruction, then overwrite MI with it.
1369
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001370 MachineBasicBlock &B = *MI.getParent();
1371 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001372 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1373 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001374 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001375 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001376 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001377 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1378 break;
1379 T.addOperand(Op);
1380 NOp++;
1381 }
1382
1383 unsigned PredReg, PredRegPos, PredRegFlags;
1384 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1385 (void)GotPredReg;
1386 assert(GotPredReg);
1387 T.addReg(PredReg, PredRegFlags);
1388 while (NOp < NumOps)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001389 T.addOperand(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001390
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001391 MI.setDesc(get(PredOpc));
1392 while (unsigned n = MI.getNumOperands())
1393 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001394 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001395 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001396
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001397 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001398 B.erase(TI);
1399
1400 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1401 MRI.clearKillFlags(PredReg);
1402 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001403}
1404
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001405bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1406 ArrayRef<MachineOperand> Pred2) const {
1407 // TODO: Fix this
1408 return false;
1409}
1410
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001411bool HexagonInstrInfo::DefinesPredicate(
1412 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001413 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001414 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1415 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001416 if (MO.isReg() && MO.isDef()) {
1417 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1418 if (RC == &Hexagon::PredRegsRegClass) {
1419 Pred.push_back(MO);
1420 return true;
1421 }
1422 }
1423 }
1424 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001425}
Andrew Trickd06df962012-02-01 22:13:57 +00001426
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001427bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001428 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001429}
1430
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001431bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1432 const MachineBasicBlock *MBB,
1433 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001434 // Debug info is never a scheduling boundary. It's necessary to be explicit
1435 // due to the special treatment of IT instructions below, otherwise a
1436 // dbg_value followed by an IT will result in the IT instruction being
1437 // considered a scheduling hazard, which is wrong. It should be the actual
1438 // instruction preceding the dbg_value instruction(s), just like it is
1439 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001440 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001441 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001442
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001443 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001444 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001445 // Don't mess around with no return calls.
1446 if (doesNotReturn(MI))
1447 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001448 // If any of the block's successors is a landing pad, this could be a
1449 // throwing call.
1450 for (auto I : MBB->successors())
1451 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001452 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001453 }
1454
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001455 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001456 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001457 return true;
1458
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001459 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1460 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001461
1462 return false;
1463}
1464
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001465/// Measure the specified inline asm to determine an approximation of its
1466/// length.
1467/// Comments (which run till the next SeparatorString or newline) do not
1468/// count as an instruction.
1469/// Any other non-whitespace text is considered an instruction, with
1470/// multiple instructions separated by SeparatorString or newlines.
1471/// Variable-length instructions are not handled here; this function
1472/// may be overloaded in the target code to do that.
1473/// Hexagon counts the number of ##'s and adjust for that many
1474/// constant exenders.
1475unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1476 const MCAsmInfo &MAI) const {
1477 StringRef AStr(Str);
1478 // Count the number of instructions in the asm.
1479 bool atInsnStart = true;
1480 unsigned Length = 0;
1481 for (; *Str; ++Str) {
1482 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1483 strlen(MAI.getSeparatorString())) == 0)
1484 atInsnStart = true;
1485 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1486 Length += MAI.getMaxInstLength();
1487 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001488 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001489 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1490 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001491 atInsnStart = false;
1492 }
1493
1494 // Add to size number of constant extenders seen * 4.
1495 StringRef Occ("##");
1496 Length += AStr.count(Occ)*4;
1497 return Length;
1498}
1499
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001500ScheduleHazardRecognizer*
1501HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1502 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001503 if (UseDFAHazardRec) {
1504 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1505 return new HexagonHazardRecognizer(II, this, HST);
1506 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001507 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1508}
1509
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001510/// \brief For a comparison instruction, return the source registers in
1511/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1512/// compares against in CmpValue. Return true if the comparison instruction
1513/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001514bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1515 unsigned &SrcReg2, int &Mask,
1516 int &Value) const {
1517 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001518
1519 // Set mask and the first source register.
1520 switch (Opc) {
1521 case Hexagon::C2_cmpeq:
1522 case Hexagon::C2_cmpeqp:
1523 case Hexagon::C2_cmpgt:
1524 case Hexagon::C2_cmpgtp:
1525 case Hexagon::C2_cmpgtu:
1526 case Hexagon::C2_cmpgtup:
1527 case Hexagon::C4_cmpneq:
1528 case Hexagon::C4_cmplte:
1529 case Hexagon::C4_cmplteu:
1530 case Hexagon::C2_cmpeqi:
1531 case Hexagon::C2_cmpgti:
1532 case Hexagon::C2_cmpgtui:
1533 case Hexagon::C4_cmpneqi:
1534 case Hexagon::C4_cmplteui:
1535 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001536 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001537 Mask = ~0;
1538 break;
1539 case Hexagon::A4_cmpbeq:
1540 case Hexagon::A4_cmpbgt:
1541 case Hexagon::A4_cmpbgtu:
1542 case Hexagon::A4_cmpbeqi:
1543 case Hexagon::A4_cmpbgti:
1544 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001545 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001546 Mask = 0xFF;
1547 break;
1548 case Hexagon::A4_cmpheq:
1549 case Hexagon::A4_cmphgt:
1550 case Hexagon::A4_cmphgtu:
1551 case Hexagon::A4_cmpheqi:
1552 case Hexagon::A4_cmphgti:
1553 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001554 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001555 Mask = 0xFFFF;
1556 break;
1557 }
1558
1559 // Set the value/second source register.
1560 switch (Opc) {
1561 case Hexagon::C2_cmpeq:
1562 case Hexagon::C2_cmpeqp:
1563 case Hexagon::C2_cmpgt:
1564 case Hexagon::C2_cmpgtp:
1565 case Hexagon::C2_cmpgtu:
1566 case Hexagon::C2_cmpgtup:
1567 case Hexagon::A4_cmpbeq:
1568 case Hexagon::A4_cmpbgt:
1569 case Hexagon::A4_cmpbgtu:
1570 case Hexagon::A4_cmpheq:
1571 case Hexagon::A4_cmphgt:
1572 case Hexagon::A4_cmphgtu:
1573 case Hexagon::C4_cmpneq:
1574 case Hexagon::C4_cmplte:
1575 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001576 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001577 return true;
1578
1579 case Hexagon::C2_cmpeqi:
1580 case Hexagon::C2_cmpgtui:
1581 case Hexagon::C2_cmpgti:
1582 case Hexagon::C4_cmpneqi:
1583 case Hexagon::C4_cmplteui:
1584 case Hexagon::C4_cmpltei:
1585 case Hexagon::A4_cmpbeqi:
1586 case Hexagon::A4_cmpbgti:
1587 case Hexagon::A4_cmpbgtui:
1588 case Hexagon::A4_cmpheqi:
1589 case Hexagon::A4_cmphgti:
1590 case Hexagon::A4_cmphgtui:
1591 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001592 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001593 return true;
1594 }
1595
1596 return false;
1597}
1598
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001599unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001600 const MachineInstr &MI,
1601 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001602 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001603}
1604
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001605DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1606 const TargetSubtargetInfo &STI) const {
1607 const InstrItineraryData *II = STI.getInstrItineraryData();
1608 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1609}
1610
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001611// Inspired by this pair:
1612// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1613// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1614// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001615bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1616 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001617 int OffsetA = 0, OffsetB = 0;
1618 unsigned SizeA = 0, SizeB = 0;
1619
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001620 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1621 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001622 return false;
1623
1624 // Instructions that are pure loads, not loads and stores like memops are not
1625 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001626 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001627 return true;
1628
1629 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001630 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001631 if (!BaseRegA || !SizeA)
1632 return false;
1633
1634 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001635 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001636 if (!BaseRegB || !SizeB)
1637 return false;
1638
1639 if (BaseRegA != BaseRegB)
1640 return false;
1641
1642 // This is a mem access with the same base register and known offsets from it.
1643 // Reason about it.
1644 if (OffsetA > OffsetB) {
1645 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1646 return (SizeB <= offDiff);
1647 } else if (OffsetA < OffsetB) {
1648 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1649 return (SizeA <= offDiff);
1650 }
1651
1652 return false;
1653}
1654
Brendon Cahoon254f8892016-07-29 16:44:44 +00001655/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001656bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001657 int &Value) const {
1658 if (isPostIncrement(MI)) {
1659 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001660 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001661 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001662 if (MI.getOpcode() == Hexagon::A2_addi) {
1663 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001664 return true;
1665 }
1666
1667 return false;
1668}
1669
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001670unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001671 MachineRegisterInfo &MRI = MF->getRegInfo();
1672 const TargetRegisterClass *TRC;
1673 if (VT == MVT::i1) {
1674 TRC = &Hexagon::PredRegsRegClass;
1675 } else if (VT == MVT::i32 || VT == MVT::f32) {
1676 TRC = &Hexagon::IntRegsRegClass;
1677 } else if (VT == MVT::i64 || VT == MVT::f64) {
1678 TRC = &Hexagon::DoubleRegsRegClass;
1679 } else {
1680 llvm_unreachable("Cannot handle this register class");
1681 }
1682
1683 unsigned NewReg = MRI.createVirtualRegister(TRC);
1684 return NewReg;
1685}
1686
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001687bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001688 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1689}
1690
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001691bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1692 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001693 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1694}
1695
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001696bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1697 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001698 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1699 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1700
1701 if (!(isTC1(MI))
1702 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001703 && !(MI.getDesc().mayLoad())
1704 && !(MI.getDesc().mayStore())
1705 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1706 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001707 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001708 && !(MI.isBranch())
1709 && !(MI.isReturn())
1710 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001711 return true;
1712
1713 return false;
1714}
1715
Sanjay Patele4b9f502015-12-07 19:21:39 +00001716// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001717bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
1718 return (getType(MI) == HexagonII::TypeCOMPOUND && MI.isBranch());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001719}
1720
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001721bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1722 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001723 isConditionalTransfer(MI) ||
1724 isConditionalALU32(MI) ||
1725 isConditionalLoad(MI) ||
1726 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001727 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1728 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001729}
1730
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001731bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1732 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001733 case Hexagon::A2_paddf:
1734 case Hexagon::A2_paddfnew:
1735 case Hexagon::A2_paddif:
1736 case Hexagon::A2_paddifnew:
1737 case Hexagon::A2_paddit:
1738 case Hexagon::A2_padditnew:
1739 case Hexagon::A2_paddt:
1740 case Hexagon::A2_paddtnew:
1741 case Hexagon::A2_pandf:
1742 case Hexagon::A2_pandfnew:
1743 case Hexagon::A2_pandt:
1744 case Hexagon::A2_pandtnew:
1745 case Hexagon::A2_porf:
1746 case Hexagon::A2_porfnew:
1747 case Hexagon::A2_port:
1748 case Hexagon::A2_portnew:
1749 case Hexagon::A2_psubf:
1750 case Hexagon::A2_psubfnew:
1751 case Hexagon::A2_psubt:
1752 case Hexagon::A2_psubtnew:
1753 case Hexagon::A2_pxorf:
1754 case Hexagon::A2_pxorfnew:
1755 case Hexagon::A2_pxort:
1756 case Hexagon::A2_pxortnew:
1757 case Hexagon::A4_paslhf:
1758 case Hexagon::A4_paslhfnew:
1759 case Hexagon::A4_paslht:
1760 case Hexagon::A4_paslhtnew:
1761 case Hexagon::A4_pasrhf:
1762 case Hexagon::A4_pasrhfnew:
1763 case Hexagon::A4_pasrht:
1764 case Hexagon::A4_pasrhtnew:
1765 case Hexagon::A4_psxtbf:
1766 case Hexagon::A4_psxtbfnew:
1767 case Hexagon::A4_psxtbt:
1768 case Hexagon::A4_psxtbtnew:
1769 case Hexagon::A4_psxthf:
1770 case Hexagon::A4_psxthfnew:
1771 case Hexagon::A4_psxtht:
1772 case Hexagon::A4_psxthtnew:
1773 case Hexagon::A4_pzxtbf:
1774 case Hexagon::A4_pzxtbfnew:
1775 case Hexagon::A4_pzxtbt:
1776 case Hexagon::A4_pzxtbtnew:
1777 case Hexagon::A4_pzxthf:
1778 case Hexagon::A4_pzxthfnew:
1779 case Hexagon::A4_pzxtht:
1780 case Hexagon::A4_pzxthtnew:
1781 case Hexagon::C2_ccombinewf:
1782 case Hexagon::C2_ccombinewt:
1783 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001784 }
1785 return false;
1786}
1787
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001788// FIXME - Function name and it's functionality don't match.
1789// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001790bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1791 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001792 return false;
1793
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001794 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001795 // Instruction with valid predicated-new opcode can be promoted to .new.
1796 return PNewOpcode >= 0;
1797}
1798
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001799// Returns true if an instruction is a conditional store.
1800//
1801// Note: It doesn't include conditional new-value stores as they can't be
1802// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001803bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1804 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001805 default: return false;
1806 case Hexagon::S4_storeirbt_io:
1807 case Hexagon::S4_storeirbf_io:
1808 case Hexagon::S4_pstorerbt_rr:
1809 case Hexagon::S4_pstorerbf_rr:
1810 case Hexagon::S2_pstorerbt_io:
1811 case Hexagon::S2_pstorerbf_io:
1812 case Hexagon::S2_pstorerbt_pi:
1813 case Hexagon::S2_pstorerbf_pi:
1814 case Hexagon::S2_pstorerdt_io:
1815 case Hexagon::S2_pstorerdf_io:
1816 case Hexagon::S4_pstorerdt_rr:
1817 case Hexagon::S4_pstorerdf_rr:
1818 case Hexagon::S2_pstorerdt_pi:
1819 case Hexagon::S2_pstorerdf_pi:
1820 case Hexagon::S2_pstorerht_io:
1821 case Hexagon::S2_pstorerhf_io:
1822 case Hexagon::S4_storeirht_io:
1823 case Hexagon::S4_storeirhf_io:
1824 case Hexagon::S4_pstorerht_rr:
1825 case Hexagon::S4_pstorerhf_rr:
1826 case Hexagon::S2_pstorerht_pi:
1827 case Hexagon::S2_pstorerhf_pi:
1828 case Hexagon::S2_pstorerit_io:
1829 case Hexagon::S2_pstorerif_io:
1830 case Hexagon::S4_storeirit_io:
1831 case Hexagon::S4_storeirif_io:
1832 case Hexagon::S4_pstorerit_rr:
1833 case Hexagon::S4_pstorerif_rr:
1834 case Hexagon::S2_pstorerit_pi:
1835 case Hexagon::S2_pstorerif_pi:
1836
1837 // V4 global address store before promoting to dot new.
1838 case Hexagon::S4_pstorerdt_abs:
1839 case Hexagon::S4_pstorerdf_abs:
1840 case Hexagon::S4_pstorerbt_abs:
1841 case Hexagon::S4_pstorerbf_abs:
1842 case Hexagon::S4_pstorerht_abs:
1843 case Hexagon::S4_pstorerhf_abs:
1844 case Hexagon::S4_pstorerit_abs:
1845 case Hexagon::S4_pstorerif_abs:
1846 return true;
1847
1848 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1849 // from the "Conditional Store" list. Because a predicated new value store
1850 // would NOT be promoted to a double dot new store.
1851 // This function returns yes for those stores that are predicated but not
1852 // yet promoted to predicate dot new instructions.
1853 }
1854}
1855
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001856bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1857 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001858 case Hexagon::A2_tfrt:
1859 case Hexagon::A2_tfrf:
1860 case Hexagon::C2_cmoveit:
1861 case Hexagon::C2_cmoveif:
1862 case Hexagon::A2_tfrtnew:
1863 case Hexagon::A2_tfrfnew:
1864 case Hexagon::C2_cmovenewit:
1865 case Hexagon::C2_cmovenewif:
1866 case Hexagon::A2_tfrpt:
1867 case Hexagon::A2_tfrpf:
1868 return true;
1869
1870 default:
1871 return false;
1872 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001873 return false;
1874}
1875
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001876// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1877// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001878bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1879 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001880 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1881 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001882 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001883
1884 unsigned isExtendable =
1885 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1886 if (!isExtendable)
1887 return false;
1888
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001889 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001890 return false;
1891
1892 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001893 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001894 // Use MO operand flags to determine if MO
1895 // has the HMOTF_ConstExtended flag set.
1896 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001897 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001898 // If this is a Machine BB address we are talking about, and it is
1899 // not marked as extended, say so.
1900 if (MO.isMBB())
1901 return false;
1902
1903 // We could be using an instruction with an extendable immediate and shoehorn
1904 // a global address into it. If it is a global address it will be constant
1905 // extended. We do this for COMBINE.
1906 // We currently only handle isGlobal() because it is the only kind of
1907 // object we are going to end up with here for now.
1908 // In the future we probably should add isSymbol(), etc.
1909 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001910 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001911 return true;
1912
1913 // If the extendable operand is not 'Immediate' type, the instruction should
1914 // have 'isExtended' flag set.
1915 assert(MO.isImm() && "Extendable operand must be Immediate type");
1916
1917 int MinValue = getMinValue(MI);
1918 int MaxValue = getMaxValue(MI);
1919 int ImmValue = MO.getImm();
1920
1921 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001922}
1923
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001924bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1925 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001926 case Hexagon::L4_return :
1927 case Hexagon::L4_return_t :
1928 case Hexagon::L4_return_f :
1929 case Hexagon::L4_return_tnew_pnt :
1930 case Hexagon::L4_return_fnew_pnt :
1931 case Hexagon::L4_return_tnew_pt :
1932 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001933 return true;
1934 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001935 return false;
1936}
1937
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001938// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001939bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1940 const MachineInstr &ConsMI) const {
1941 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001942 return false;
1943
1944 auto &HRI = getRegisterInfo();
1945
1946 SmallVector<unsigned, 4> DefsA;
1947 SmallVector<unsigned, 4> DefsB;
1948 SmallVector<unsigned, 8> UsesA;
1949 SmallVector<unsigned, 8> UsesB;
1950
1951 parseOperands(ProdMI, DefsA, UsesA);
1952 parseOperands(ConsMI, DefsB, UsesB);
1953
1954 for (auto &RegA : DefsA)
1955 for (auto &RegB : UsesB) {
1956 // True data dependency.
1957 if (RegA == RegB)
1958 return true;
1959
1960 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1961 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1962 if (RegB == *SubRegs)
1963 return true;
1964
1965 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1966 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1967 if (RegA == *SubRegs)
1968 return true;
1969 }
1970
1971 return false;
1972}
1973
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001974// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001975bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1976 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001977 case Hexagon::V6_vL32b_cur_pi:
1978 case Hexagon::V6_vL32b_cur_ai:
1979 case Hexagon::V6_vL32b_cur_pi_128B:
1980 case Hexagon::V6_vL32b_cur_ai_128B:
1981 return true;
1982 }
1983 return false;
1984}
1985
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001986// Returns true, if any one of the operands is a dot new
1987// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001988bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
1989 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001990 return true;
1991
1992 return false;
1993}
1994
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001995/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001996bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
1997 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001998 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1999 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2000 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2001}
2002
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002003bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2004 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002005 return true;
2006
2007 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002008 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002009 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2010 return true;
2011 return false;
2012}
2013
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002014bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2015 return (Opcode == Hexagon::ENDLOOP0 ||
2016 Opcode == Hexagon::ENDLOOP1);
2017}
2018
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002019bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2020 switch(OpType) {
2021 case MachineOperand::MO_MachineBasicBlock:
2022 case MachineOperand::MO_GlobalAddress:
2023 case MachineOperand::MO_ExternalSymbol:
2024 case MachineOperand::MO_JumpTableIndex:
2025 case MachineOperand::MO_ConstantPoolIndex:
2026 case MachineOperand::MO_BlockAddress:
2027 return true;
2028 default:
2029 return false;
2030 }
2031}
2032
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002033bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2034 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002035 const uint64_t F = MID.TSFlags;
2036 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2037 return true;
2038
2039 // TODO: This is largely obsolete now. Will need to be removed
2040 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002041 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002042 // PS_fi and PS_fia remain special cases.
2043 case Hexagon::PS_fi:
2044 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002045 return true;
2046 default:
2047 return false;
2048 }
2049 return false;
2050}
2051
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002052// This returns true in two cases:
2053// - The OP code itself indicates that this is an extended instruction.
2054// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002055bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002056 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002057 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002058 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2059 return true;
2060 // Use MO operand flags to determine if one of MI's operands
2061 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002062 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
2063 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002064 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2065 return true;
2066 }
2067 return false;
2068}
2069
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002070bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2071 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002072 const uint64_t F = get(Opcode).TSFlags;
2073 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2074}
2075
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002076// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002077bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2078 const MachineInstr &J) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002079 if (!isV60VectorInstruction(I))
2080 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002081 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002082 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002083 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002084}
2085
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002086bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2087 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002088 case Hexagon::J2_callr :
2089 case Hexagon::J2_callrf :
2090 case Hexagon::J2_callrt :
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00002091 case Hexagon::PS_call_nr :
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002092 return true;
2093 }
2094 return false;
2095}
2096
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002097bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2098 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002099 case Hexagon::L4_return :
2100 case Hexagon::L4_return_t :
2101 case Hexagon::L4_return_f :
2102 case Hexagon::L4_return_fnew_pnt :
2103 case Hexagon::L4_return_fnew_pt :
2104 case Hexagon::L4_return_tnew_pnt :
2105 case Hexagon::L4_return_tnew_pt :
2106 return true;
2107 }
2108 return false;
2109}
2110
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002111bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2112 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002113 case Hexagon::J2_jumpr :
2114 case Hexagon::J2_jumprt :
2115 case Hexagon::J2_jumprf :
2116 case Hexagon::J2_jumprtnewpt :
2117 case Hexagon::J2_jumprfnewpt :
2118 case Hexagon::J2_jumprtnew :
2119 case Hexagon::J2_jumprfnew :
2120 return true;
2121 }
2122 return false;
2123}
2124
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002125// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002126// Use abs estimate as oppose to the exact number.
2127// TODO: This will need to be changed to use MC level
2128// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002129bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002130 unsigned offset) const {
2131 // This selection of jump instructions matches to that what
2132 // AnalyzeBranch can parse, plus NVJ.
2133 if (isNewValueJump(MI)) // r9:2
2134 return isInt<11>(offset);
2135
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002136 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002137 // Still missing Jump to address condition on register value.
2138 default:
2139 return false;
2140 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2141 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002142 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002143 return isInt<24>(offset);
2144 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2145 case Hexagon::J2_jumpf:
2146 case Hexagon::J2_jumptnew:
2147 case Hexagon::J2_jumptnewpt:
2148 case Hexagon::J2_jumpfnew:
2149 case Hexagon::J2_jumpfnewpt:
2150 case Hexagon::J2_callt:
2151 case Hexagon::J2_callf:
2152 return isInt<17>(offset);
2153 case Hexagon::J2_loop0i:
2154 case Hexagon::J2_loop0iext:
2155 case Hexagon::J2_loop0r:
2156 case Hexagon::J2_loop0rext:
2157 case Hexagon::J2_loop1i:
2158 case Hexagon::J2_loop1iext:
2159 case Hexagon::J2_loop1r:
2160 case Hexagon::J2_loop1rext:
2161 return isInt<9>(offset);
2162 // TODO: Add all the compound branches here. Can we do this in Relation model?
2163 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2164 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2165 return isInt<11>(offset);
2166 }
2167}
2168
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002169bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2170 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002171 bool isLate = isLateResultInstr(LRMI);
2172 bool isEarly = isEarlySourceInstr(ESMI);
2173
2174 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002175 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002176 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002177 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002178
2179 if (isLate && isEarly) {
2180 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2181 return true;
2182 }
2183
2184 return false;
2185}
2186
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002187bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2188 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002189 case TargetOpcode::EXTRACT_SUBREG:
2190 case TargetOpcode::INSERT_SUBREG:
2191 case TargetOpcode::SUBREG_TO_REG:
2192 case TargetOpcode::REG_SEQUENCE:
2193 case TargetOpcode::IMPLICIT_DEF:
2194 case TargetOpcode::COPY:
2195 case TargetOpcode::INLINEASM:
2196 case TargetOpcode::PHI:
2197 return false;
2198 default:
2199 break;
2200 }
2201
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002202 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002203
2204 switch (SchedClass) {
2205 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2206 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2207 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2208 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2209 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2210 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2211 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2212 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2213 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2214 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2215 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2216 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2217 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2218 return false;
2219 }
2220 return true;
2221}
2222
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002223bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002224 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2225 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002226 return MI.getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002227}
2228
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002229bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2230 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002231 return Opcode == Hexagon::J2_loop0i ||
2232 Opcode == Hexagon::J2_loop0r ||
2233 Opcode == Hexagon::J2_loop0iext ||
2234 Opcode == Hexagon::J2_loop0rext ||
2235 Opcode == Hexagon::J2_loop1i ||
2236 Opcode == Hexagon::J2_loop1r ||
2237 Opcode == Hexagon::J2_loop1iext ||
2238 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002239}
2240
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002241bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2242 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002243 default: return false;
2244 case Hexagon::L4_iadd_memopw_io :
2245 case Hexagon::L4_isub_memopw_io :
2246 case Hexagon::L4_add_memopw_io :
2247 case Hexagon::L4_sub_memopw_io :
2248 case Hexagon::L4_and_memopw_io :
2249 case Hexagon::L4_or_memopw_io :
2250 case Hexagon::L4_iadd_memoph_io :
2251 case Hexagon::L4_isub_memoph_io :
2252 case Hexagon::L4_add_memoph_io :
2253 case Hexagon::L4_sub_memoph_io :
2254 case Hexagon::L4_and_memoph_io :
2255 case Hexagon::L4_or_memoph_io :
2256 case Hexagon::L4_iadd_memopb_io :
2257 case Hexagon::L4_isub_memopb_io :
2258 case Hexagon::L4_add_memopb_io :
2259 case Hexagon::L4_sub_memopb_io :
2260 case Hexagon::L4_and_memopb_io :
2261 case Hexagon::L4_or_memopb_io :
2262 case Hexagon::L4_ior_memopb_io:
2263 case Hexagon::L4_ior_memoph_io:
2264 case Hexagon::L4_ior_memopw_io:
2265 case Hexagon::L4_iand_memopb_io:
2266 case Hexagon::L4_iand_memoph_io:
2267 case Hexagon::L4_iand_memopw_io:
2268 return true;
2269 }
2270 return false;
2271}
2272
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002273bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2274 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002275 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2276}
2277
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002278bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2279 const uint64_t F = get(Opcode).TSFlags;
2280 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2281}
2282
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002283bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002284 return isNewValueJump(MI) || isNewValueStore(MI);
2285}
2286
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002287bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2288 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002289}
2290
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002291bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2292 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2293}
2294
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002295bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2296 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002297 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2298}
2299
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002300bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2301 const uint64_t F = get(Opcode).TSFlags;
2302 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2303}
2304
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002305// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002306bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002307 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002308 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002309 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2310 == OperandNum;
2311}
2312
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002313bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2314 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002315 assert(isPredicated(MI));
2316 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2317}
2318
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002319bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2320 const uint64_t F = get(Opcode).TSFlags;
2321 assert(isPredicated(Opcode));
2322 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2323}
2324
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002325bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2326 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002327 return !((F >> HexagonII::PredicatedFalsePos) &
2328 HexagonII::PredicatedFalseMask);
2329}
2330
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002331bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2332 const uint64_t F = get(Opcode).TSFlags;
2333 // Make sure that the instruction is predicated.
2334 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2335 return !((F >> HexagonII::PredicatedFalsePos) &
2336 HexagonII::PredicatedFalseMask);
2337}
2338
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002339bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2340 const uint64_t F = get(Opcode).TSFlags;
2341 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2342}
2343
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002344bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2345 const uint64_t F = get(Opcode).TSFlags;
2346 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2347}
2348
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002349bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2350 const uint64_t F = get(Opcode).TSFlags;
2351 assert(get(Opcode).isBranch() &&
2352 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2353 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2354}
2355
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002356bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2357 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2358 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2359 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2360 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002361}
2362
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002363bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2364 switch (MI.getOpcode()) {
2365 // Byte
2366 case Hexagon::L2_loadrb_io:
2367 case Hexagon::L4_loadrb_ur:
2368 case Hexagon::L4_loadrb_ap:
2369 case Hexagon::L2_loadrb_pr:
2370 case Hexagon::L2_loadrb_pbr:
2371 case Hexagon::L2_loadrb_pi:
2372 case Hexagon::L2_loadrb_pci:
2373 case Hexagon::L2_loadrb_pcr:
2374 case Hexagon::L2_loadbsw2_io:
2375 case Hexagon::L4_loadbsw2_ur:
2376 case Hexagon::L4_loadbsw2_ap:
2377 case Hexagon::L2_loadbsw2_pr:
2378 case Hexagon::L2_loadbsw2_pbr:
2379 case Hexagon::L2_loadbsw2_pi:
2380 case Hexagon::L2_loadbsw2_pci:
2381 case Hexagon::L2_loadbsw2_pcr:
2382 case Hexagon::L2_loadbsw4_io:
2383 case Hexagon::L4_loadbsw4_ur:
2384 case Hexagon::L4_loadbsw4_ap:
2385 case Hexagon::L2_loadbsw4_pr:
2386 case Hexagon::L2_loadbsw4_pbr:
2387 case Hexagon::L2_loadbsw4_pi:
2388 case Hexagon::L2_loadbsw4_pci:
2389 case Hexagon::L2_loadbsw4_pcr:
2390 case Hexagon::L4_loadrb_rr:
2391 case Hexagon::L2_ploadrbt_io:
2392 case Hexagon::L2_ploadrbt_pi:
2393 case Hexagon::L2_ploadrbf_io:
2394 case Hexagon::L2_ploadrbf_pi:
2395 case Hexagon::L2_ploadrbtnew_io:
2396 case Hexagon::L2_ploadrbfnew_io:
2397 case Hexagon::L4_ploadrbt_rr:
2398 case Hexagon::L4_ploadrbf_rr:
2399 case Hexagon::L4_ploadrbtnew_rr:
2400 case Hexagon::L4_ploadrbfnew_rr:
2401 case Hexagon::L2_ploadrbtnew_pi:
2402 case Hexagon::L2_ploadrbfnew_pi:
2403 case Hexagon::L4_ploadrbt_abs:
2404 case Hexagon::L4_ploadrbf_abs:
2405 case Hexagon::L4_ploadrbtnew_abs:
2406 case Hexagon::L4_ploadrbfnew_abs:
2407 case Hexagon::L2_loadrbgp:
2408 // Half
2409 case Hexagon::L2_loadrh_io:
2410 case Hexagon::L4_loadrh_ur:
2411 case Hexagon::L4_loadrh_ap:
2412 case Hexagon::L2_loadrh_pr:
2413 case Hexagon::L2_loadrh_pbr:
2414 case Hexagon::L2_loadrh_pi:
2415 case Hexagon::L2_loadrh_pci:
2416 case Hexagon::L2_loadrh_pcr:
2417 case Hexagon::L4_loadrh_rr:
2418 case Hexagon::L2_ploadrht_io:
2419 case Hexagon::L2_ploadrht_pi:
2420 case Hexagon::L2_ploadrhf_io:
2421 case Hexagon::L2_ploadrhf_pi:
2422 case Hexagon::L2_ploadrhtnew_io:
2423 case Hexagon::L2_ploadrhfnew_io:
2424 case Hexagon::L4_ploadrht_rr:
2425 case Hexagon::L4_ploadrhf_rr:
2426 case Hexagon::L4_ploadrhtnew_rr:
2427 case Hexagon::L4_ploadrhfnew_rr:
2428 case Hexagon::L2_ploadrhtnew_pi:
2429 case Hexagon::L2_ploadrhfnew_pi:
2430 case Hexagon::L4_ploadrht_abs:
2431 case Hexagon::L4_ploadrhf_abs:
2432 case Hexagon::L4_ploadrhtnew_abs:
2433 case Hexagon::L4_ploadrhfnew_abs:
2434 case Hexagon::L2_loadrhgp:
2435 return true;
2436 default:
2437 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002438 }
2439}
2440
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002441bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2442 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002443 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2444}
2445
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002446bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2447 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002448 case Hexagon::STriw_pred :
2449 case Hexagon::LDriw_pred :
2450 return true;
2451 default:
2452 return false;
2453 }
2454}
2455
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002456bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2457 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002458 return false;
2459
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002460 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002461 if (Op.isGlobal() || Op.isSymbol())
2462 return true;
2463 return false;
2464}
2465
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002466// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002467bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2468 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002469 switch (SchedClass) {
2470 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2471 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2472 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2473 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2474 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2475 //case Hexagon::Sched::M_tc_1_SLOT23:
2476 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2477 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2478 return true;
2479
2480 default:
2481 return false;
2482 }
2483}
2484
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002485bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2486 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002487 switch (SchedClass) {
2488 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2489 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2490 case Hexagon::Sched::CR_tc_2_SLOT3:
2491 case Hexagon::Sched::M_tc_2_SLOT23:
2492 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2493 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2494 return true;
2495
2496 default:
2497 return false;
2498 }
2499}
2500
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002501bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2502 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002503 switch (SchedClass) {
2504 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2505 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2506 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2507 case Hexagon::Sched::CR_tc_2early_SLOT23:
2508 case Hexagon::Sched::CR_tc_2early_SLOT3:
2509 case Hexagon::Sched::J_tc_2early_SLOT0123:
2510 case Hexagon::Sched::J_tc_2early_SLOT2:
2511 case Hexagon::Sched::J_tc_2early_SLOT23:
2512 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2513 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2514 return true;
2515
2516 default:
2517 return false;
2518 }
2519}
2520
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002521bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2522 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002523 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2524}
2525
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002526// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002527bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2528 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002529 if (mayBeCurLoad(MI1)) {
2530 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002531 unsigned DstReg = MI1.getOperand(0).getReg();
2532 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002533 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002534 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002535 return true;
2536 }
2537 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002538 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2539 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2540 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002541 return true;
2542 return false;
2543}
2544
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002545bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002546 const uint64_t V = getType(MI);
2547 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2548}
2549
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002550// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2551//
2552bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2553 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2554 VT == MVT::v32i16 || VT == MVT::v64i8) {
2555 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2556 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2557 (Offset & 0x3f) == 0);
2558 }
2559 // 128B
2560 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2561 VT == MVT::v64i16 || VT == MVT::v128i8) {
2562 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2563 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2564 (Offset & 0x7f) == 0);
2565 }
2566 if (VT == MVT::i64) {
2567 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2568 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2569 (Offset & 0x7) == 0);
2570 }
2571 if (VT == MVT::i32) {
2572 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2573 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2574 (Offset & 0x3) == 0);
2575 }
2576 if (VT == MVT::i16) {
2577 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2578 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2579 (Offset & 0x1) == 0);
2580 }
2581 if (VT == MVT::i8) {
2582 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2583 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2584 }
2585 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002586}
2587
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002588bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2589 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002590 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002591 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002592 // inserted to calculate the final address. Due to this reason, the function
2593 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002594 // We used to assert if the offset was not properly aligned, however,
2595 // there are cases where a misaligned pointer recast can cause this
2596 // problem, and we need to allow for it. The front end warns of such
2597 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002598
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002599 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002600 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002601 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002602 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002603 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002604 case Hexagon::V6_vL32b_ai:
2605 case Hexagon::V6_vS32b_ai:
2606 case Hexagon::V6_vL32Ub_ai:
2607 case Hexagon::V6_vS32Ub_ai:
2608 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2609 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2610
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002611 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002612 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002613 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002614 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002615 case Hexagon::V6_vL32b_ai_128B:
2616 case Hexagon::V6_vS32b_ai_128B:
2617 case Hexagon::V6_vL32Ub_ai_128B:
2618 case Hexagon::V6_vS32Ub_ai_128B:
2619 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2620 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2621
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002622 case Hexagon::J2_loop0i:
2623 case Hexagon::J2_loop1i:
2624 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002625
2626 case Hexagon::S4_storeirb_io:
2627 case Hexagon::S4_storeirbt_io:
2628 case Hexagon::S4_storeirbf_io:
2629 return isUInt<6>(Offset);
2630
2631 case Hexagon::S4_storeirh_io:
2632 case Hexagon::S4_storeirht_io:
2633 case Hexagon::S4_storeirhf_io:
2634 return isShiftedUInt<6,1>(Offset);
2635
2636 case Hexagon::S4_storeiri_io:
2637 case Hexagon::S4_storeirit_io:
2638 case Hexagon::S4_storeirif_io:
2639 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002640 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002641
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002642 if (Extend)
2643 return true;
2644
2645 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002646 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002647 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002648 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2649 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2650
Colin LeMahieu947cd702014-12-23 20:44:59 +00002651 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002652 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002653 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2654 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2655
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002656 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002657 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002658 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002659 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2660 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2661
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002662 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002663 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002664 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002665 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2666 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2667
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002668 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002669 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2670 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2671
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002672 case Hexagon::L4_iadd_memopw_io :
2673 case Hexagon::L4_isub_memopw_io :
2674 case Hexagon::L4_add_memopw_io :
2675 case Hexagon::L4_sub_memopw_io :
2676 case Hexagon::L4_and_memopw_io :
2677 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002678 return (0 <= Offset && Offset <= 255);
2679
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002680 case Hexagon::L4_iadd_memoph_io :
2681 case Hexagon::L4_isub_memoph_io :
2682 case Hexagon::L4_add_memoph_io :
2683 case Hexagon::L4_sub_memoph_io :
2684 case Hexagon::L4_and_memoph_io :
2685 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002686 return (0 <= Offset && Offset <= 127);
2687
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002688 case Hexagon::L4_iadd_memopb_io :
2689 case Hexagon::L4_isub_memopb_io :
2690 case Hexagon::L4_add_memopb_io :
2691 case Hexagon::L4_sub_memopb_io :
2692 case Hexagon::L4_and_memopb_io :
2693 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002694 return (0 <= Offset && Offset <= 63);
2695
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002696 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002697 // any size. Later pass knows how to handle it.
2698 case Hexagon::STriw_pred:
2699 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002700 case Hexagon::STriw_mod:
2701 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002702 return true;
2703
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002704 case Hexagon::PS_fi:
2705 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002706 case Hexagon::INLINEASM:
2707 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002708
2709 case Hexagon::L2_ploadrbt_io:
2710 case Hexagon::L2_ploadrbf_io:
2711 case Hexagon::L2_ploadrubt_io:
2712 case Hexagon::L2_ploadrubf_io:
2713 case Hexagon::S2_pstorerbt_io:
2714 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002715 return isUInt<6>(Offset);
2716
2717 case Hexagon::L2_ploadrht_io:
2718 case Hexagon::L2_ploadrhf_io:
2719 case Hexagon::L2_ploadruht_io:
2720 case Hexagon::L2_ploadruhf_io:
2721 case Hexagon::S2_pstorerht_io:
2722 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002723 return isShiftedUInt<6,1>(Offset);
2724
2725 case Hexagon::L2_ploadrit_io:
2726 case Hexagon::L2_ploadrif_io:
2727 case Hexagon::S2_pstorerit_io:
2728 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002729 return isShiftedUInt<6,2>(Offset);
2730
2731 case Hexagon::L2_ploadrdt_io:
2732 case Hexagon::L2_ploadrdf_io:
2733 case Hexagon::S2_pstorerdt_io:
2734 case Hexagon::S2_pstorerdf_io:
2735 return isShiftedUInt<6,3>(Offset);
2736 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002737
Benjamin Kramerb6684012011-12-27 11:41:05 +00002738 llvm_unreachable("No offset range is defined for this opcode. "
2739 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002740}
2741
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002742bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2743 return isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002744}
2745
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002746bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2747 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002748 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2749 return
2750 V == HexagonII::TypeCVI_VA ||
2751 V == HexagonII::TypeCVI_VA_DV;
2752}
Andrew Trickd06df962012-02-01 22:13:57 +00002753
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002754bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2755 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002756 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2757 return true;
2758
2759 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2760 return true;
2761
2762 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002763 return true;
2764
2765 return false;
2766}
Jyotsna Verma84256432013-03-01 17:37:13 +00002767
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002768bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2769 switch (MI.getOpcode()) {
2770 // Byte
2771 case Hexagon::L2_loadrub_io:
2772 case Hexagon::L4_loadrub_ur:
2773 case Hexagon::L4_loadrub_ap:
2774 case Hexagon::L2_loadrub_pr:
2775 case Hexagon::L2_loadrub_pbr:
2776 case Hexagon::L2_loadrub_pi:
2777 case Hexagon::L2_loadrub_pci:
2778 case Hexagon::L2_loadrub_pcr:
2779 case Hexagon::L2_loadbzw2_io:
2780 case Hexagon::L4_loadbzw2_ur:
2781 case Hexagon::L4_loadbzw2_ap:
2782 case Hexagon::L2_loadbzw2_pr:
2783 case Hexagon::L2_loadbzw2_pbr:
2784 case Hexagon::L2_loadbzw2_pi:
2785 case Hexagon::L2_loadbzw2_pci:
2786 case Hexagon::L2_loadbzw2_pcr:
2787 case Hexagon::L2_loadbzw4_io:
2788 case Hexagon::L4_loadbzw4_ur:
2789 case Hexagon::L4_loadbzw4_ap:
2790 case Hexagon::L2_loadbzw4_pr:
2791 case Hexagon::L2_loadbzw4_pbr:
2792 case Hexagon::L2_loadbzw4_pi:
2793 case Hexagon::L2_loadbzw4_pci:
2794 case Hexagon::L2_loadbzw4_pcr:
2795 case Hexagon::L4_loadrub_rr:
2796 case Hexagon::L2_ploadrubt_io:
2797 case Hexagon::L2_ploadrubt_pi:
2798 case Hexagon::L2_ploadrubf_io:
2799 case Hexagon::L2_ploadrubf_pi:
2800 case Hexagon::L2_ploadrubtnew_io:
2801 case Hexagon::L2_ploadrubfnew_io:
2802 case Hexagon::L4_ploadrubt_rr:
2803 case Hexagon::L4_ploadrubf_rr:
2804 case Hexagon::L4_ploadrubtnew_rr:
2805 case Hexagon::L4_ploadrubfnew_rr:
2806 case Hexagon::L2_ploadrubtnew_pi:
2807 case Hexagon::L2_ploadrubfnew_pi:
2808 case Hexagon::L4_ploadrubt_abs:
2809 case Hexagon::L4_ploadrubf_abs:
2810 case Hexagon::L4_ploadrubtnew_abs:
2811 case Hexagon::L4_ploadrubfnew_abs:
2812 case Hexagon::L2_loadrubgp:
2813 // Half
2814 case Hexagon::L2_loadruh_io:
2815 case Hexagon::L4_loadruh_ur:
2816 case Hexagon::L4_loadruh_ap:
2817 case Hexagon::L2_loadruh_pr:
2818 case Hexagon::L2_loadruh_pbr:
2819 case Hexagon::L2_loadruh_pi:
2820 case Hexagon::L2_loadruh_pci:
2821 case Hexagon::L2_loadruh_pcr:
2822 case Hexagon::L4_loadruh_rr:
2823 case Hexagon::L2_ploadruht_io:
2824 case Hexagon::L2_ploadruht_pi:
2825 case Hexagon::L2_ploadruhf_io:
2826 case Hexagon::L2_ploadruhf_pi:
2827 case Hexagon::L2_ploadruhtnew_io:
2828 case Hexagon::L2_ploadruhfnew_io:
2829 case Hexagon::L4_ploadruht_rr:
2830 case Hexagon::L4_ploadruhf_rr:
2831 case Hexagon::L4_ploadruhtnew_rr:
2832 case Hexagon::L4_ploadruhfnew_rr:
2833 case Hexagon::L2_ploadruhtnew_pi:
2834 case Hexagon::L2_ploadruhfnew_pi:
2835 case Hexagon::L4_ploadruht_abs:
2836 case Hexagon::L4_ploadruhf_abs:
2837 case Hexagon::L4_ploadruhtnew_abs:
2838 case Hexagon::L4_ploadruhfnew_abs:
2839 case Hexagon::L2_loadruhgp:
2840 return true;
2841 default:
2842 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002843 }
2844}
2845
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002846// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002847bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2848 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002849 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2850 if (!isVecUsableNextPacket(MI1, MI2))
2851 return true;
2852 return false;
2853}
2854
Brendon Cahoon254f8892016-07-29 16:44:44 +00002855/// \brief Get the base register and byte offset of a load/store instr.
2856bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2857 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2858 const {
2859 unsigned AccessSize = 0;
2860 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002861 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002862 Offset = OffsetVal;
2863 return BaseReg != 0;
2864}
2865
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002866/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002867bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2868 const MachineInstr &Second) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002869 if (DisableNVSchedule)
2870 return false;
2871 if (mayBeNewStore(Second)) {
2872 // Make sure the definition of the first instruction is the value being
2873 // stored.
2874 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002875 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002876 if (!Stored.isReg())
2877 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002878 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2879 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002880 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2881 return true;
2882 }
2883 }
2884 return false;
2885}
2886
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002887bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2888 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002889 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002890}
2891
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002892bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2893 for (auto &I : *B)
2894 if (I.isEHLabel())
2895 return true;
2896 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002897}
2898
Jyotsna Verma84256432013-03-01 17:37:13 +00002899// Returns true if an instruction can be converted into a non-extended
2900// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002901bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002902 short NonExtOpcode;
2903 // Check if the instruction has a register form that uses register in place
2904 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002905 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002906 return true;
2907
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002908 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002909 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002910
2911 switch (getAddrMode(MI)) {
2912 case HexagonII::Absolute :
2913 // Load/store with absolute addressing mode can be converted into
2914 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002915 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002916 break;
2917 case HexagonII::BaseImmOffset :
2918 // Load/store with base+offset addressing mode can be converted into
2919 // base+register offset addressing mode. However left shift operand should
2920 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002921 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002922 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002923 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002924 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002925 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002926 default:
2927 return false;
2928 }
2929 if (NonExtOpcode < 0)
2930 return false;
2931 return true;
2932 }
2933 return false;
2934}
2935
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002936bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2937 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002938 Hexagon::InstrType_Pseudo) >= 0;
2939}
2940
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002941bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2942 const {
2943 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2944 while (I != E) {
2945 if (I->isBarrier())
2946 return true;
2947 ++I;
2948 }
2949 return false;
2950}
2951
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002952// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002953bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
2954 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2955 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002956 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2957 HST.hasV60TOps();
2958}
2959
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002960// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002961bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2962 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002963 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2964}
2965
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002966bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2967 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002968 // There is no stall when ProdMI is not a V60 vector.
2969 if (!isV60VectorInstruction(ProdMI))
2970 return false;
2971
2972 // There is no stall when ProdMI and ConsMI are not dependent.
2973 if (!isDependent(ProdMI, ConsMI))
2974 return false;
2975
2976 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2977 // are scheduled in consecutive packets.
2978 if (isVecUsableNextPacket(ProdMI, ConsMI))
2979 return false;
2980
2981 return true;
2982}
2983
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002984bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002985 MachineBasicBlock::const_instr_iterator BII) const {
2986 // There is no stall when I is not a V60 vector.
2987 if (!isV60VectorInstruction(MI))
2988 return false;
2989
2990 MachineBasicBlock::const_instr_iterator MII = BII;
2991 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2992
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002993 if (!MII->isBundle()) {
2994 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002995 if (!isV60VectorInstruction(J))
2996 return false;
2997 else if (isVecUsableNextPacket(J, MI))
2998 return false;
2999 return true;
3000 }
3001
3002 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003003 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003004 if (producesStall(J, MI))
3005 return true;
3006 }
3007 return false;
3008}
3009
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003010bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003011 unsigned PredReg) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003012 for (unsigned opNum = 0; opNum < MI.getNumOperands(); opNum++) {
3013 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003014 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3015 return false; // Predicate register must be explicitly defined.
3016 }
3017
3018 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3019 // memd_locked cannot be used as .new as well,
3020 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003021 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003022}
3023
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003024bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3025 return (Opcode == Hexagon::J2_jumpt) ||
3026 (Opcode == Hexagon::J2_jumpf) ||
3027 (Opcode == Hexagon::J2_jumptnew) ||
3028 (Opcode == Hexagon::J2_jumpfnew) ||
3029 (Opcode == Hexagon::J2_jumptnewpt) ||
3030 (Opcode == Hexagon::J2_jumpfnewpt);
3031}
3032
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003033bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3034 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3035 return false;
3036 return !isPredicatedTrue(Cond[0].getImm());
3037}
3038
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003039short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3040 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003041}
3042
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003043unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3044 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003045 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3046}
3047
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003048// Returns the base register in a memory access (load/store). The offset is
3049// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003050unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003051 int &Offset, unsigned &AccessSize) const {
3052 // Return if it is not a base+offset type instruction or a MemOp.
3053 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3054 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003055 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003056 return 0;
3057
3058 // Since it is a memory access instruction, getMemAccessSize() should never
3059 // return 0.
3060 assert (getMemAccessSize(MI) &&
3061 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3062
3063 // Return Values of getMemAccessSize() are
3064 // 0 - Checked in the assert above.
3065 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3066 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3067 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3068
3069 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003070 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003071 return 0;
3072
3073 // Post increment updates its EA after the mem access,
3074 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003075 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003076 Offset = 0;
3077 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003078 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003079 }
3080
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003081 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003082}
3083
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003084/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003085bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003086 unsigned &BasePos, unsigned &OffsetPos) const {
3087 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003088 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003089 BasePos = 0;
3090 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003091 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003092 BasePos = 0;
3093 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003094 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003095 BasePos = 1;
3096 OffsetPos = 2;
3097 } else
3098 return false;
3099
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003100 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003101 BasePos++;
3102 OffsetPos++;
3103 }
3104 if (isPostIncrement(MI)) {
3105 BasePos++;
3106 OffsetPos++;
3107 }
3108
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003109 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003110 return false;
3111
3112 return true;
3113}
3114
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003115// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003116// e.g. jump_t t1 (i1)
3117// jump t2 (i2)
3118// Jumpers = {i2, i1}
3119SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3120 MachineBasicBlock& MBB) const {
3121 SmallVector<MachineInstr*, 2> Jumpers;
3122 // If the block has no terminators, it just falls into the block after it.
3123 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3124 if (I == MBB.instr_begin())
3125 return Jumpers;
3126
3127 // A basic block may looks like this:
3128 //
3129 // [ insn
3130 // EH_LABEL
3131 // insn
3132 // insn
3133 // insn
3134 // EH_LABEL
3135 // insn ]
3136 //
3137 // It has two succs but does not have a terminator
3138 // Don't know how to handle it.
3139 do {
3140 --I;
3141 if (I->isEHLabel())
3142 return Jumpers;
3143 } while (I != MBB.instr_begin());
3144
3145 I = MBB.instr_end();
3146 --I;
3147
3148 while (I->isDebugValue()) {
3149 if (I == MBB.instr_begin())
3150 return Jumpers;
3151 --I;
3152 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003153 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003154 return Jumpers;
3155
3156 // Get the last instruction in the block.
3157 MachineInstr *LastInst = &*I;
3158 Jumpers.push_back(LastInst);
3159 MachineInstr *SecondLastInst = nullptr;
3160 // Find one more terminator if present.
3161 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003162 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003163 if (!SecondLastInst) {
3164 SecondLastInst = &*I;
3165 Jumpers.push_back(SecondLastInst);
3166 } else // This is a third branch.
3167 return Jumpers;
3168 }
3169 if (I == MBB.instr_begin())
3170 break;
3171 --I;
3172 } while (true);
3173 return Jumpers;
3174}
3175
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003176short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3177 if (Opcode < 0)
3178 return -1;
3179 return Hexagon::getBaseWithLongOffset(Opcode);
3180}
3181
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003182short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3183 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003184}
3185
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003186short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3187 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003188}
3189
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003190// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003191unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3192 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003193 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3194}
3195
3196// See if instruction could potentially be a duplex candidate.
3197// If so, return its group. Zero otherwise.
3198HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003199 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003200 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3201
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003202 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003203 default:
3204 return HexagonII::HCG_None;
3205 //
3206 // Compound pairs.
3207 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3208 // "Rd16=#U6 ; jump #r9:2"
3209 // "Rd16=Rs16 ; jump #r9:2"
3210 //
3211 case Hexagon::C2_cmpeq:
3212 case Hexagon::C2_cmpgt:
3213 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003214 DstReg = MI.getOperand(0).getReg();
3215 Src1Reg = MI.getOperand(1).getReg();
3216 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003217 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3218 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3219 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3220 return HexagonII::HCG_A;
3221 break;
3222 case Hexagon::C2_cmpeqi:
3223 case Hexagon::C2_cmpgti:
3224 case Hexagon::C2_cmpgtui:
3225 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003226 DstReg = MI.getOperand(0).getReg();
3227 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003228 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3229 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003230 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3231 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3232 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003233 return HexagonII::HCG_A;
3234 break;
3235 case Hexagon::A2_tfr:
3236 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003237 DstReg = MI.getOperand(0).getReg();
3238 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003239 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3240 return HexagonII::HCG_A;
3241 break;
3242 case Hexagon::A2_tfrsi:
3243 // Rd = #u6
3244 // Do not test for #u6 size since the const is getting extended
3245 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003246 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003247 if (isIntRegForSubInst(DstReg))
3248 return HexagonII::HCG_A;
3249 break;
3250 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003251 DstReg = MI.getOperand(0).getReg();
3252 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003253 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3254 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003255 MI.getOperand(2).isImm() &&
3256 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003257 return HexagonII::HCG_A;
3258 break;
3259 // The fact that .new form is used pretty much guarantees
3260 // that predicate register will match. Nevertheless,
3261 // there could be some false positives without additional
3262 // checking.
3263 case Hexagon::J2_jumptnew:
3264 case Hexagon::J2_jumpfnew:
3265 case Hexagon::J2_jumptnewpt:
3266 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003267 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003268 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3269 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3270 return HexagonII::HCG_B;
3271 break;
3272 // Transfer and jump:
3273 // Rd=#U6 ; jump #r9:2
3274 // Rd=Rs ; jump #r9:2
3275 // Do not test for jump range here.
3276 case Hexagon::J2_jump:
3277 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003278 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003279 return HexagonII::HCG_C;
3280 break;
3281 }
3282
3283 return HexagonII::HCG_None;
3284}
3285
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003286// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003287unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3288 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003289 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3290 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003291 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3292 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003293 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003294 unsigned DestReg = GA.getOperand(0).getReg();
3295 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003296 return -1;
3297 if (DestReg == Hexagon::P0)
3298 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3299 if (DestReg == Hexagon::P1)
3300 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3301 return -1;
3302}
3303
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003304int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3305 enum Hexagon::PredSense inPredSense;
3306 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3307 Hexagon::PredSense_true;
3308 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3309 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3310 return CondOpcode;
3311
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003312 llvm_unreachable("Unexpected predicable instruction");
3313}
3314
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003315// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003316int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3317 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003318 default: llvm_unreachable("Unknown .cur type");
3319 case Hexagon::V6_vL32b_pi:
3320 return Hexagon::V6_vL32b_cur_pi;
3321 case Hexagon::V6_vL32b_ai:
3322 return Hexagon::V6_vL32b_cur_ai;
3323 //128B
3324 case Hexagon::V6_vL32b_pi_128B:
3325 return Hexagon::V6_vL32b_cur_pi_128B;
3326 case Hexagon::V6_vL32b_ai_128B:
3327 return Hexagon::V6_vL32b_cur_ai_128B;
3328 }
3329 return 0;
3330}
3331
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003332// The diagram below shows the steps involved in the conversion of a predicated
3333// store instruction to its .new predicated new-value form.
3334//
3335// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3336// ^ ^
3337// / \ (not OK. it will cause new-value store to be
3338// / X conditional on p0.new while R2 producer is
3339// / \ on p0)
3340// / \.
3341// p.new store p.old NV store
3342// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3343// ^ ^
3344// \ /
3345// \ /
3346// \ /
3347// p.old store
3348// [if (p0)memw(R0+#0)=R2]
3349//
3350//
3351// The following set of instructions further explains the scenario where
3352// conditional new-value store becomes invalid when promoted to .new predicate
3353// form.
3354//
3355// { 1) if (p0) r0 = add(r1, r2)
3356// 2) p0 = cmp.eq(r3, #0) }
3357//
3358// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3359// the first two instructions because in instr 1, r0 is conditional on old value
3360// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3361// is not valid for new-value stores.
3362// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3363// from the "Conditional Store" list. Because a predicated new value store
3364// would NOT be promoted to a double dot new store. See diagram below:
3365// This function returns yes for those stores that are predicated but not
3366// yet promoted to predicate dot new instructions.
3367//
3368// +---------------------+
3369// /-----| if (p0) memw(..)=r0 |---------\~
3370// || +---------------------+ ||
3371// promote || /\ /\ || promote
3372// || /||\ /||\ ||
3373// \||/ demote || \||/
3374// \/ || || \/
3375// +-------------------------+ || +-------------------------+
3376// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3377// +-------------------------+ || +-------------------------+
3378// || || ||
3379// || demote \||/
3380// promote || \/ NOT possible
3381// || || /\~
3382// \||/ || /||\~
3383// \/ || ||
3384// +-----------------------------+
3385// | if (p0.new) memw(..)=r0.new |
3386// +-----------------------------+
3387// Double Dot New Store
3388//
3389// Returns the most basic instruction for the .new predicated instructions and
3390// new-value stores.
3391// For example, all of the following instructions will be converted back to the
3392// same instruction:
3393// 1) if (p0.new) memw(R0+#0) = R1.new --->
3394// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3395// 3) if (p0.new) memw(R0+#0) = R1 --->
3396//
3397// To understand the translation of instruction 1 to its original form, consider
3398// a packet with 3 instructions.
3399// { p0 = cmp.eq(R0,R1)
3400// if (p0.new) R2 = add(R3, R4)
3401// R5 = add (R3, R1)
3402// }
3403// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3404//
3405// This instruction can be part of the previous packet only if both p0 and R2
3406// are promoted to .new values. This promotion happens in steps, first
3407// predicate register is promoted to .new and in the next iteration R2 is
3408// promoted. Therefore, in case of dependence check failure (due to R5) during
3409// next iteration, it should be converted back to its most basic form.
3410
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003411// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003412int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3413 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003414 if (NVOpcode >= 0) // Valid new-value store instruction.
3415 return NVOpcode;
3416
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003417 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003418 default: llvm_unreachable("Unknown .new type");
3419 case Hexagon::S4_storerb_ur:
3420 return Hexagon::S4_storerbnew_ur;
3421
3422 case Hexagon::S2_storerb_pci:
3423 return Hexagon::S2_storerb_pci;
3424
3425 case Hexagon::S2_storeri_pci:
3426 return Hexagon::S2_storeri_pci;
3427
3428 case Hexagon::S2_storerh_pci:
3429 return Hexagon::S2_storerh_pci;
3430
3431 case Hexagon::S2_storerd_pci:
3432 return Hexagon::S2_storerd_pci;
3433
3434 case Hexagon::S2_storerf_pci:
3435 return Hexagon::S2_storerf_pci;
3436
3437 case Hexagon::V6_vS32b_ai:
3438 return Hexagon::V6_vS32b_new_ai;
3439
3440 case Hexagon::V6_vS32b_pi:
3441 return Hexagon::V6_vS32b_new_pi;
3442
3443 // 128B
3444 case Hexagon::V6_vS32b_ai_128B:
3445 return Hexagon::V6_vS32b_new_ai_128B;
3446
3447 case Hexagon::V6_vS32b_pi_128B:
3448 return Hexagon::V6_vS32b_new_pi_128B;
3449 }
3450 return 0;
3451}
3452
3453// Returns the opcode to use when converting MI, which is a conditional jump,
3454// into a conditional instruction which uses the .new value of the predicate.
3455// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003456int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003457 const MachineBranchProbabilityInfo *MBPI) const {
3458 // We assume that block can have at most two successors.
3459 bool taken = false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003460 const MachineBasicBlock *Src = MI.getParent();
3461 const MachineOperand &BrTarget = MI.getOperand(1);
3462 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003463
3464 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3465 if (Prediction >= BranchProbability(1,2))
3466 taken = true;
3467
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003468 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003469 case Hexagon::J2_jumpt:
3470 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3471 case Hexagon::J2_jumpf:
3472 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3473
3474 default:
3475 llvm_unreachable("Unexpected jump instruction.");
3476 }
3477}
3478
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003479// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003480int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003481 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003482 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003483 if (NewOpcode >= 0) // Valid predicate new instruction
3484 return NewOpcode;
3485
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003486 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003487 // Condtional Jumps
3488 case Hexagon::J2_jumpt:
3489 case Hexagon::J2_jumpf:
3490 return getDotNewPredJumpOp(MI, MBPI);
3491
3492 default:
3493 assert(0 && "Unknown .new type");
3494 }
3495 return 0;
3496}
3497
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003498int HexagonInstrInfo::getDotOldOp(const int opc) const {
3499 int NewOp = opc;
3500 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3501 NewOp = Hexagon::getPredOldOpcode(NewOp);
3502 assert(NewOp >= 0 &&
3503 "Couldn't change predicate new instruction to its old form.");
3504 }
3505
3506 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3507 NewOp = Hexagon::getNonNVStore(NewOp);
3508 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3509 }
3510 return NewOp;
3511}
3512
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003513// See if instruction could potentially be a duplex candidate.
3514// If so, return its group. Zero otherwise.
3515HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003516 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003517 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3518 auto &HRI = getRegisterInfo();
3519
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003520 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003521 default:
3522 return HexagonII::HSIG_None;
3523 //
3524 // Group L1:
3525 //
3526 // Rd = memw(Rs+#u4:2)
3527 // Rd = memub(Rs+#u4:0)
3528 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003529 DstReg = MI.getOperand(0).getReg();
3530 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003531 // Special case this one from Group L2.
3532 // Rd = memw(r29+#u5:2)
3533 if (isIntRegForSubInst(DstReg)) {
3534 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3535 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003536 MI.getOperand(2).isImm() &&
3537 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003538 return HexagonII::HSIG_L2;
3539 // Rd = memw(Rs+#u4:2)
3540 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003541 (MI.getOperand(2).isImm() &&
3542 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003543 return HexagonII::HSIG_L1;
3544 }
3545 break;
3546 case Hexagon::L2_loadrub_io:
3547 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003548 DstReg = MI.getOperand(0).getReg();
3549 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003550 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003551 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003552 return HexagonII::HSIG_L1;
3553 break;
3554 //
3555 // Group L2:
3556 //
3557 // Rd = memh/memuh(Rs+#u3:1)
3558 // Rd = memb(Rs+#u3:0)
3559 // Rd = memw(r29+#u5:2) - Handled above.
3560 // Rdd = memd(r29+#u5:3)
3561 // deallocframe
3562 // [if ([!]p0[.new])] dealloc_return
3563 // [if ([!]p0[.new])] jumpr r31
3564 case Hexagon::L2_loadrh_io:
3565 case Hexagon::L2_loadruh_io:
3566 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003567 DstReg = MI.getOperand(0).getReg();
3568 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003569 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003570 MI.getOperand(2).isImm() &&
3571 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003572 return HexagonII::HSIG_L2;
3573 break;
3574 case Hexagon::L2_loadrb_io:
3575 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003576 DstReg = MI.getOperand(0).getReg();
3577 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003578 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003579 MI.getOperand(2).isImm() &&
3580 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003581 return HexagonII::HSIG_L2;
3582 break;
3583 case Hexagon::L2_loadrd_io:
3584 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003585 DstReg = MI.getOperand(0).getReg();
3586 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003587 if (isDblRegForSubInst(DstReg, HRI) &&
3588 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3589 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003590 MI.getOperand(2).isImm() &&
3591 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003592 return HexagonII::HSIG_L2;
3593 break;
3594 // dealloc_return is not documented in Hexagon Manual, but marked
3595 // with A_SUBINSN attribute in iset_v4classic.py.
3596 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003597 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003598 case Hexagon::L4_return:
3599 case Hexagon::L2_deallocframe:
3600 return HexagonII::HSIG_L2;
3601 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003602 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003603 // jumpr r31
3604 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003605 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003606 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3607 return HexagonII::HSIG_L2;
3608 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003609 case Hexagon::PS_jmprett:
3610 case Hexagon::PS_jmpretf:
3611 case Hexagon::PS_jmprettnewpt:
3612 case Hexagon::PS_jmpretfnewpt:
3613 case Hexagon::PS_jmprettnew:
3614 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003615 DstReg = MI.getOperand(1).getReg();
3616 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003617 // [if ([!]p0[.new])] jumpr r31
3618 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3619 (Hexagon::P0 == SrcReg)) &&
3620 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3621 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003622 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003623 case Hexagon::L4_return_t :
3624 case Hexagon::L4_return_f :
3625 case Hexagon::L4_return_tnew_pnt :
3626 case Hexagon::L4_return_fnew_pnt :
3627 case Hexagon::L4_return_tnew_pt :
3628 case Hexagon::L4_return_fnew_pt :
3629 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003630 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003631 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3632 return HexagonII::HSIG_L2;
3633 break;
3634 //
3635 // Group S1:
3636 //
3637 // memw(Rs+#u4:2) = Rt
3638 // memb(Rs+#u4:0) = Rt
3639 case Hexagon::S2_storeri_io:
3640 // Special case this one from Group S2.
3641 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003642 Src1Reg = MI.getOperand(0).getReg();
3643 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003644 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3645 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003646 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3647 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003648 return HexagonII::HSIG_S2;
3649 // memw(Rs+#u4:2) = Rt
3650 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003651 MI.getOperand(1).isImm() &&
3652 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003653 return HexagonII::HSIG_S1;
3654 break;
3655 case Hexagon::S2_storerb_io:
3656 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003657 Src1Reg = MI.getOperand(0).getReg();
3658 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003659 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003660 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003661 return HexagonII::HSIG_S1;
3662 break;
3663 //
3664 // Group S2:
3665 //
3666 // memh(Rs+#u3:1) = Rt
3667 // memw(r29+#u5:2) = Rt
3668 // memd(r29+#s6:3) = Rtt
3669 // memw(Rs+#u4:2) = #U1
3670 // memb(Rs+#u4) = #U1
3671 // allocframe(#u5:3)
3672 case Hexagon::S2_storerh_io:
3673 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003674 Src1Reg = MI.getOperand(0).getReg();
3675 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003676 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003677 MI.getOperand(1).isImm() &&
3678 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003679 return HexagonII::HSIG_S1;
3680 break;
3681 case Hexagon::S2_storerd_io:
3682 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003683 Src1Reg = MI.getOperand(0).getReg();
3684 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003685 if (isDblRegForSubInst(Src2Reg, HRI) &&
3686 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003687 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3688 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003689 return HexagonII::HSIG_S2;
3690 break;
3691 case Hexagon::S4_storeiri_io:
3692 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003693 Src1Reg = MI.getOperand(0).getReg();
3694 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3695 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3696 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003697 return HexagonII::HSIG_S2;
3698 break;
3699 case Hexagon::S4_storeirb_io:
3700 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003701 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003702 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003703 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3704 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003705 return HexagonII::HSIG_S2;
3706 break;
3707 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003708 if (MI.getOperand(0).isImm() &&
3709 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003710 return HexagonII::HSIG_S1;
3711 break;
3712 //
3713 // Group A:
3714 //
3715 // Rx = add(Rx,#s7)
3716 // Rd = Rs
3717 // Rd = #u6
3718 // Rd = #-1
3719 // if ([!]P0[.new]) Rd = #0
3720 // Rd = add(r29,#u6:2)
3721 // Rx = add(Rx,Rs)
3722 // P0 = cmp.eq(Rs,#u2)
3723 // Rdd = combine(#0,Rs)
3724 // Rdd = combine(Rs,#0)
3725 // Rdd = combine(#u2,#U2)
3726 // Rd = add(Rs,#1)
3727 // Rd = add(Rs,#-1)
3728 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3729 // Rd = and(Rs,#1)
3730 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003731 DstReg = MI.getOperand(0).getReg();
3732 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003733 if (isIntRegForSubInst(DstReg)) {
3734 // Rd = add(r29,#u6:2)
3735 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003736 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3737 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003738 return HexagonII::HSIG_A;
3739 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003740 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3741 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003742 return HexagonII::HSIG_A;
3743 // Rd = add(Rs,#1)
3744 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003745 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3746 ((MI.getOperand(2).getImm() == 1) ||
3747 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003748 return HexagonII::HSIG_A;
3749 }
3750 break;
3751 case Hexagon::A2_add:
3752 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003753 DstReg = MI.getOperand(0).getReg();
3754 Src1Reg = MI.getOperand(1).getReg();
3755 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003756 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3757 isIntRegForSubInst(Src2Reg))
3758 return HexagonII::HSIG_A;
3759 break;
3760 case Hexagon::A2_andir:
3761 // Same as zxtb.
3762 // Rd16=and(Rs16,#255)
3763 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003764 DstReg = MI.getOperand(0).getReg();
3765 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003766 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003767 MI.getOperand(2).isImm() &&
3768 ((MI.getOperand(2).getImm() == 1) ||
3769 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003770 return HexagonII::HSIG_A;
3771 break;
3772 case Hexagon::A2_tfr:
3773 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003774 DstReg = MI.getOperand(0).getReg();
3775 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003776 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3777 return HexagonII::HSIG_A;
3778 break;
3779 case Hexagon::A2_tfrsi:
3780 // Rd = #u6
3781 // Do not test for #u6 size since the const is getting extended
3782 // regardless and compound could be formed.
3783 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003784 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003785 if (isIntRegForSubInst(DstReg))
3786 return HexagonII::HSIG_A;
3787 break;
3788 case Hexagon::C2_cmoveit:
3789 case Hexagon::C2_cmovenewit:
3790 case Hexagon::C2_cmoveif:
3791 case Hexagon::C2_cmovenewif:
3792 // if ([!]P0[.new]) Rd = #0
3793 // Actual form:
3794 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003795 DstReg = MI.getOperand(0).getReg();
3796 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003797 if (isIntRegForSubInst(DstReg) &&
3798 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003799 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003800 return HexagonII::HSIG_A;
3801 break;
3802 case Hexagon::C2_cmpeqi:
3803 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003804 DstReg = MI.getOperand(0).getReg();
3805 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003806 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3807 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003808 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003809 return HexagonII::HSIG_A;
3810 break;
3811 case Hexagon::A2_combineii:
3812 case Hexagon::A4_combineii:
3813 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003814 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003815 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003816 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3817 (MI.getOperand(1).isGlobal() &&
3818 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3819 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3820 (MI.getOperand(2).isGlobal() &&
3821 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003822 return HexagonII::HSIG_A;
3823 break;
3824 case Hexagon::A4_combineri:
3825 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003826 DstReg = MI.getOperand(0).getReg();
3827 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003828 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003829 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3830 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003831 return HexagonII::HSIG_A;
3832 break;
3833 case Hexagon::A4_combineir:
3834 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003835 DstReg = MI.getOperand(0).getReg();
3836 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003837 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003838 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3839 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003840 return HexagonII::HSIG_A;
3841 break;
3842 case Hexagon::A2_sxtb:
3843 case Hexagon::A2_sxth:
3844 case Hexagon::A2_zxtb:
3845 case Hexagon::A2_zxth:
3846 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003847 DstReg = MI.getOperand(0).getReg();
3848 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003849 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3850 return HexagonII::HSIG_A;
3851 break;
3852 }
3853
3854 return HexagonII::HSIG_None;
3855}
3856
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003857short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3858 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003859}
3860
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003861// Return first non-debug instruction in the basic block.
3862MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3863 const {
3864 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003865 MachineInstr &MI = *MII;
3866 if (MI.isDebugValue())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003867 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003868 return &MI;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003869 }
3870 return nullptr;
3871}
3872
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003873unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003874 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003875 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3876 // still have a MinLatency property, which getStageLatency checks.
3877 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003878 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003879
3880 // Get the latency embedded in the itinerary. If we're not using timing class
3881 // latencies or if we using BSB scheduling, then restrict the maximum latency
3882 // to 1 (that is, either 0 or 1).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003883 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003884 return 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003885 unsigned Latency = ItinData->getStageLatency(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003886 if (!EnableTimingClassLatency ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003887 MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>().
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003888 useBSBScheduling())
3889 if (Latency > 1)
3890 Latency = 1;
3891 return Latency;
3892}
3893
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003894// inverts the predication logic.
3895// p -> NotP
3896// NotP -> P
3897bool HexagonInstrInfo::getInvertedPredSense(
3898 SmallVectorImpl<MachineOperand> &Cond) const {
3899 if (Cond.empty())
3900 return false;
3901 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3902 Cond[0].setImm(Opc);
3903 return true;
3904}
3905
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003906unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3907 int InvPredOpcode;
3908 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3909 : Hexagon::getTruePredOpcode(Opc);
3910 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3911 return InvPredOpcode;
3912
3913 llvm_unreachable("Unexpected predicated instruction");
3914}
3915
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003916// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003917int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
3918 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003919 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3920 & HexagonII::ExtentSignedMask;
3921 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3922 & HexagonII::ExtentBitsMask;
3923
3924 if (isSigned) // if value is signed
3925 return ~(-1U << (bits - 1));
3926 else
3927 return ~(-1U << bits);
3928}
3929
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003930unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
3931 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003932 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3933}
3934
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003935// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003936int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
3937 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003938 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3939 & HexagonII::ExtentSignedMask;
3940 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3941 & HexagonII::ExtentBitsMask;
3942
3943 if (isSigned) // if value is signed
3944 return -1U << (bits - 1);
3945 else
3946 return 0;
3947}
3948
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003949// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003950short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003951 // Check if the instruction has a register form that uses register in place
3952 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003953 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003954 if (NonExtOpcode >= 0)
3955 return NonExtOpcode;
3956
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003957 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003958 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003959 switch (getAddrMode(MI)) {
3960 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003961 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003962 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003963 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003964 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003965 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003966
Jyotsna Verma84256432013-03-01 17:37:13 +00003967 default:
3968 return -1;
3969 }
3970 }
3971 return -1;
3972}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00003973
Ahmed Bougachac88bf542015-06-11 19:30:37 +00003974bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003975 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00003976 if (Cond.empty())
3977 return false;
3978 assert(Cond.size() == 2);
3979 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003980 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3981 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00003982 }
3983 PredReg = Cond[1].getReg();
3984 PredRegPos = 1;
3985 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3986 PredRegFlags = 0;
3987 if (Cond[1].isImplicit())
3988 PredRegFlags = RegState::Implicit;
3989 if (Cond[1].isUndef())
3990 PredRegFlags |= RegState::Undef;
3991 return true;
3992}
3993
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003994short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
3995 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003996}
3997
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003998short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
3999 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004000}
4001
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004002// Return the number of bytes required to encode the instruction.
4003// Hexagon instructions are fixed length, 4 bytes, unless they
4004// use a constant extender, which requires another 4 bytes.
4005// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004006unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4007 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004008 return 0;
4009
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004010 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004011 if (!Size)
4012 // Assume the default insn size in case it cannot be determined
4013 // for whatever reason.
4014 Size = HEXAGON_INSTR_SIZE;
4015
4016 if (isConstExtended(MI) || isExtended(MI))
4017 Size += HEXAGON_INSTR_SIZE;
4018
4019 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004020 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4021 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004022 const MachineFunction *MF = MBB.getParent();
4023 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4024
4025 // Count the number of register definitions to find the asm string.
4026 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004027 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004028 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004029 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004030
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004031 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004032 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004033 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004034 Size = getInlineAsmLength(AsmStr, *MAI);
4035 }
4036
4037 return Size;
4038}
4039
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004040uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4041 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004042 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4043}
4044
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004045unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4046 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004047 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004048 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004049
4050 return IS.getUnits();
4051}
4052
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004053unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4054 const uint64_t F = get(Opcode).TSFlags;
4055 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4056}
4057
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004058// Calculate size of the basic block without debug instructions.
4059unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4060 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4061}
4062
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004063unsigned HexagonInstrInfo::nonDbgBundleSize(
4064 MachineBasicBlock::const_iterator BundleHead) const {
4065 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004066 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004067 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004068 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004069}
4070
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004071/// immediateExtend - Changes the instruction in place to one using an immediate
4072/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004073void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004074 assert((isExtendable(MI)||isConstExtended(MI)) &&
4075 "Instruction must be extendable");
4076 // Find which operand is extendable.
4077 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004078 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004079 // This needs to be something we understand.
4080 assert((MO.isMBB() || MO.isImm()) &&
4081 "Branch with unknown extendable field type");
4082 // Mark given operand as extended.
4083 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4084}
4085
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004086bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004087 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004088 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004089 << NewTarget->getNumber(); MI.dump(););
4090 assert(MI.isBranch());
4091 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4092 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004093 // In general branch target is the last operand,
4094 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004095 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004096 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004097 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4098 MI.getOperand(TargetPos).setMBB(NewTarget);
4099 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004100 NewOpcode = reversePrediction(NewOpcode);
4101 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004102 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004103 return true;
4104}
4105
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004106void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4107 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4108 MachineFunction::iterator A = MF.begin();
4109 MachineBasicBlock &B = *A;
4110 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004111 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004112 MachineInstr *NewMI;
4113
4114 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4115 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004116 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004117 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4118 " Class: " << NewMI->getDesc().getSchedClass());
4119 NewMI->eraseFromParent();
4120 }
4121 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4122}
4123
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004124// inverts the predication logic.
4125// p -> NotP
4126// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004127bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4128 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4129 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004130 return true;
4131}
4132
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004133// Reverse the branch prediction.
4134unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4135 int PredRevOpcode = -1;
4136 if (isPredictedTaken(Opcode))
4137 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4138 else
4139 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4140 assert(PredRevOpcode > 0);
4141 return PredRevOpcode;
4142}
4143
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004144// TODO: Add more rigorous validation.
4145bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4146 const {
4147 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4148}
4149
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004150short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4151 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004152}