blob: 03ec645a82c87002402189955a6a429ee23f9f48 [file] [log] [blame]
Eugene Zelenko3b873362017-09-28 22:27:31 +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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "HexagonInstrInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000015#include "Hexagon.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000016#include "HexagonFrameLowering.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000017#include "HexagonHazardRecognizer.h"
Craig Topperb25fda92012-03-17 18:46:09 +000018#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "HexagonSubtarget.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000020#include "llvm/ADT/ArrayRef.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000021#include "llvm/ADT/SmallPtrSet.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000022#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000023#include "llvm/ADT/StringRef.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000024#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000025#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000026#include "llvm/CodeGen/MachineBasicBlock.h"
27#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000029#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000032#include "llvm/CodeGen/MachineInstrBundle.h"
33#include "llvm/CodeGen/MachineLoopInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000035#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000037#include "llvm/CodeGen/ScheduleDAG.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000038#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000039#include "llvm/CodeGen/TargetOpcodes.h"
40#include "llvm/CodeGen/TargetRegisterInfo.h"
41#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000042#include "llvm/IR/DebugLoc.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000043#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000044#include "llvm/MC/MCInstrDesc.h"
45#include "llvm/MC/MCInstrItineraries.h"
46#include "llvm/MC/MCRegisterInfo.h"
47#include "llvm/Support/BranchProbability.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000048#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000049#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000050#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000051#include "llvm/Support/MachineValueType.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000052#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000053#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000054#include "llvm/Target/TargetMachine.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000055#include <cassert>
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000056#include <cctype>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000057#include <cstdint>
58#include <cstring>
59#include <iterator>
Eugene Zelenko3b873362017-09-28 22:27:31 +000060#include <string>
61#include <utility>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000062
Tony Linthicum1213a7a2011-12-12 21:14:40 +000063using namespace llvm;
64
Chandler Carruthe96dd892014-04-21 22:55:11 +000065#define DEBUG_TYPE "hexagon-instrinfo"
66
Chandler Carruthd174b722014-04-22 02:03:14 +000067#define GET_INSTRINFO_CTOR_DTOR
68#define GET_INSTRMAP_INFO
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +000069#include "HexagonDepTimingClasses.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000070#include "HexagonGenDFAPacketizer.inc"
71#include "HexagonGenInstrInfo.inc"
Chandler Carruthd174b722014-04-22 02:03:14 +000072
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000073cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000074 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
75 "packetization boundary."));
76
77static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
78 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
79
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000080static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
81 cl::Hidden, cl::ZeroOrMore, cl::init(false),
82 cl::desc("Disable schedule adjustment for new value stores."));
83
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000084static cl::opt<bool> EnableTimingClassLatency(
85 "enable-timing-class-latency", cl::Hidden, cl::init(false),
86 cl::desc("Enable timing class latency"));
87
88static cl::opt<bool> EnableALUForwarding(
89 "enable-alu-forwarding", cl::Hidden, cl::init(true),
90 cl::desc("Enable vec alu forwarding"));
91
92static cl::opt<bool> EnableACCForwarding(
93 "enable-acc-forwarding", cl::Hidden, cl::init(true),
94 cl::desc("Enable vec acc forwarding"));
95
96static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
97 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
98
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000099static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
100 cl::init(true), cl::Hidden, cl::ZeroOrMore,
101 cl::desc("Use the DFA based hazard recognizer."));
102
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103/// Constants for Hexagon instructions.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000105const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000107const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000108const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000109const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000111const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000112const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000113const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000115// Pin the vtable to this file.
116void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000117
118HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000119 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
120 Subtarget(ST) {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000121
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000122static bool isIntRegForSubInst(unsigned Reg) {
123 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
124 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000125}
126
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000127static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000128 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
129 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000130}
131
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000132/// Calculate number of instructions excluding the debug instructions.
133static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
134 MachineBasicBlock::const_instr_iterator MIE) {
135 unsigned Count = 0;
136 for (; MIB != MIE; ++MIB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000137 if (!MIB->isDebugInstr())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000138 ++Count;
139 }
140 return Count;
141}
142
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000143/// Find the hardware loop instruction used to set-up the specified loop.
144/// On Hexagon, we have two instructions used to set-up the hardware loop
145/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
146/// to indicate the end of a loop.
Krzysztof Parzyszek998df2c2018-03-23 20:43:02 +0000147MachineInstr *HexagonInstrInfo::findLoopInstr(MachineBasicBlock *BB,
148 unsigned EndLoopOp, MachineBasicBlock *TargetBB,
149 SmallPtrSet<MachineBasicBlock *, 8> &Visited) const {
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000150 unsigned LOOPi;
151 unsigned LOOPr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000152 if (EndLoopOp == Hexagon::ENDLOOP0) {
153 LOOPi = Hexagon::J2_loop0i;
154 LOOPr = Hexagon::J2_loop0r;
155 } else { // EndLoopOp == Hexagon::EndLOOP1
156 LOOPi = Hexagon::J2_loop1i;
157 LOOPr = Hexagon::J2_loop1r;
158 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000159
Brendon Cahoondf43e682015-05-08 16:16:29 +0000160 // The loop set-up instruction will be in a predecessor block
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000161 for (MachineBasicBlock *PB : BB->predecessors()) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000162 // If this has been visited, already skip it.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000163 if (!Visited.insert(PB).second)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000164 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000165 if (PB == BB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000166 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000167 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
168 unsigned Opc = I->getOpcode();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000169 if (Opc == LOOPi || Opc == LOOPr)
170 return &*I;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000171 // We've reached a different loop, which means the loop01 has been
172 // removed.
173 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000174 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000175 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000176 // Check the predecessors for the LOOP instruction.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000177 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
178 return Loop;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000179 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000180 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000181}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000182
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000183/// Gather register def/uses from MI.
184/// This treats possible (predicated) defs as actually happening ones
185/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000186static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000187 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
188 Defs.clear();
189 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000190
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000191 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
192 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000193
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000194 if (!MO.isReg())
195 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000196
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000197 unsigned Reg = MO.getReg();
198 if (!Reg)
199 continue;
200
201 if (MO.isUse())
202 Uses.push_back(MO.getReg());
203
204 if (MO.isDef())
205 Defs.push_back(MO.getReg());
206 }
207}
208
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000209// Position dependent, so check twice for swap.
210static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
211 switch (Ga) {
212 case HexagonII::HSIG_None:
213 default:
214 return false;
215 case HexagonII::HSIG_L1:
216 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
217 case HexagonII::HSIG_L2:
218 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
219 Gb == HexagonII::HSIG_A);
220 case HexagonII::HSIG_S1:
221 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
222 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
223 case HexagonII::HSIG_S2:
224 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
225 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
226 Gb == HexagonII::HSIG_A);
227 case HexagonII::HSIG_A:
228 return (Gb == HexagonII::HSIG_A);
229 case HexagonII::HSIG_Compound:
230 return (Gb == HexagonII::HSIG_Compound);
231 }
232 return false;
233}
234
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000235/// isLoadFromStackSlot - If the specified machine instruction is a direct
236/// load from a stack slot, return the virtual or physical register number of
237/// the destination along with the FrameIndex of the loaded stack slot. If
238/// not, return 0. This predicate must return 0 if the instruction has
239/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000240unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000241 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000242 switch (MI.getOpcode()) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000243 default:
244 break;
245 case Hexagon::L2_loadri_io:
246 case Hexagon::L2_loadrd_io:
247 case Hexagon::V6_vL32b_ai:
248 case Hexagon::V6_vL32b_nt_ai:
249 case Hexagon::V6_vL32Ub_ai:
250 case Hexagon::LDriw_pred:
251 case Hexagon::LDriw_ctr:
252 case Hexagon::PS_vloadrq_ai:
253 case Hexagon::PS_vloadrw_ai:
254 case Hexagon::PS_vloadrw_nt_ai: {
255 const MachineOperand OpFI = MI.getOperand(1);
256 if (!OpFI.isFI())
257 return 0;
258 const MachineOperand OpOff = MI.getOperand(2);
259 if (!OpOff.isImm() || OpOff.getImm() != 0)
260 return 0;
261 FrameIndex = OpFI.getIndex();
262 return MI.getOperand(0).getReg();
263 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000264
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000265 case Hexagon::L2_ploadrit_io:
266 case Hexagon::L2_ploadrif_io:
267 case Hexagon::L2_ploadrdt_io:
268 case Hexagon::L2_ploadrdf_io: {
269 const MachineOperand OpFI = MI.getOperand(2);
270 if (!OpFI.isFI())
271 return 0;
272 const MachineOperand OpOff = MI.getOperand(3);
273 if (!OpOff.isImm() || OpOff.getImm() != 0)
274 return 0;
275 FrameIndex = OpFI.getIndex();
276 return MI.getOperand(0).getReg();
277 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000278 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000279
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000280 return 0;
281}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000282
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000283/// isStoreToStackSlot - If the specified machine instruction is a direct
284/// store to a stack slot, return the virtual or physical register number of
285/// the source reg along with the FrameIndex of the loaded stack slot. If
286/// not, return 0. This predicate must return 0 if the instruction has
287/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000288unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000289 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000290 switch (MI.getOpcode()) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000291 default:
292 break;
293 case Hexagon::S2_storerb_io:
294 case Hexagon::S2_storerh_io:
295 case Hexagon::S2_storeri_io:
296 case Hexagon::S2_storerd_io:
297 case Hexagon::V6_vS32b_ai:
298 case Hexagon::V6_vS32Ub_ai:
299 case Hexagon::STriw_pred:
300 case Hexagon::STriw_ctr:
301 case Hexagon::PS_vstorerq_ai:
302 case Hexagon::PS_vstorerw_ai: {
303 const MachineOperand &OpFI = MI.getOperand(0);
304 if (!OpFI.isFI())
305 return 0;
306 const MachineOperand &OpOff = MI.getOperand(1);
307 if (!OpOff.isImm() || OpOff.getImm() != 0)
308 return 0;
309 FrameIndex = OpFI.getIndex();
310 return MI.getOperand(2).getReg();
311 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000312
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000313 case Hexagon::S2_pstorerbt_io:
314 case Hexagon::S2_pstorerbf_io:
315 case Hexagon::S2_pstorerht_io:
316 case Hexagon::S2_pstorerhf_io:
317 case Hexagon::S2_pstorerit_io:
318 case Hexagon::S2_pstorerif_io:
319 case Hexagon::S2_pstorerdt_io:
320 case Hexagon::S2_pstorerdf_io: {
321 const MachineOperand &OpFI = MI.getOperand(1);
322 if (!OpFI.isFI())
323 return 0;
324 const MachineOperand &OpOff = MI.getOperand(2);
325 if (!OpOff.isImm() || OpOff.getImm() != 0)
326 return 0;
327 FrameIndex = OpFI.getIndex();
328 return MI.getOperand(3).getReg();
329 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000330 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000331
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000332 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000333}
334
Krzysztof Parzyszek275ffa42018-01-23 19:08:40 +0000335/// This function checks if the instruction or bundle of instructions
336/// has load from stack slot and returns frameindex and machine memory
337/// operand of that instruction if true.
338bool HexagonInstrInfo::hasLoadFromStackSlot(const MachineInstr &MI,
339 const MachineMemOperand *&MMO,
340 int &FrameIndex) const {
341 if (MI.isBundle()) {
342 const MachineBasicBlock *MBB = MI.getParent();
343 MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
344 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
345 if (TargetInstrInfo::hasLoadFromStackSlot(*MII, MMO, FrameIndex))
346 return true;
347 return false;
348 }
349
350 return TargetInstrInfo::hasLoadFromStackSlot(MI, MMO, FrameIndex);
351}
352
353/// This function checks if the instruction or bundle of instructions
354/// has store to stack slot and returns frameindex and machine memory
355/// operand of that instruction if true.
356bool HexagonInstrInfo::hasStoreToStackSlot(const MachineInstr &MI,
357 const MachineMemOperand *&MMO,
358 int &FrameIndex) const {
359 if (MI.isBundle()) {
360 const MachineBasicBlock *MBB = MI.getParent();
361 MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
362 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
363 if (TargetInstrInfo::hasStoreToStackSlot(*MII, MMO, FrameIndex))
364 return true;
365 return false;
366 }
367
368 return TargetInstrInfo::hasStoreToStackSlot(MI, MMO, FrameIndex);
369}
370
Brendon Cahoondf43e682015-05-08 16:16:29 +0000371/// This function can analyze one/two way branching only and should (mostly) be
372/// called by target independent side.
373/// First entry is always the opcode of the branching instruction, except when
374/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
375/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
376/// e.g. Jump_c p will have
377/// Cond[0] = Jump_c
378/// Cond[1] = p
379/// HW-loop ENDLOOP:
380/// Cond[0] = ENDLOOP
381/// Cond[1] = MBB
382/// New value jump:
383/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
384/// Cond[1] = R
385/// Cond[2] = Imm
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000386bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000387 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000388 MachineBasicBlock *&FBB,
389 SmallVectorImpl<MachineOperand> &Cond,
390 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000391 TBB = nullptr;
392 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000393 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000394
395 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000396 MachineBasicBlock::instr_iterator I = MBB.instr_end();
397 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 return false;
399
400 // A basic block may looks like this:
401 //
402 // [ insn
403 // EH_LABEL
404 // insn
405 // insn
406 // insn
407 // EH_LABEL
408 // insn ]
409 //
410 // It has two succs but does not have a terminator
411 // Don't know how to handle it.
412 do {
413 --I;
414 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000415 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000416 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000417 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000418
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000419 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000420 --I;
421
Shiva Chen801bf7e2018-05-09 02:42:00 +0000422 while (I->isDebugInstr()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000423 if (I == MBB.instr_begin())
424 return false;
425 --I;
426 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000427
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000428 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
429 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000430 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000431 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000432 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000433 LLVM_DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000434 I->eraseFromParent();
435 I = MBB.instr_end();
436 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000437 return false;
438 --I;
439 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000440 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000441 return false;
442
443 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000444 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000445 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000446 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000447 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000448 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000449 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000450 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000451 else
452 // This is a third branch.
453 return true;
454 }
455 if (I == MBB.instr_begin())
456 break;
457 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000458 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000459
460 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000461 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
462 // If the branch target is not a basic block, it could be a tail call.
463 // (It is, if the target is a function.)
464 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
465 return true;
466 if (SecLastOpcode == Hexagon::J2_jump &&
467 !SecondLastInst->getOperand(0).isMBB())
468 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000469
470 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000471 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000472
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000473 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
474 return true;
475
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000476 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000477 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000478 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000479 TBB = LastInst->getOperand(0).getMBB();
480 return false;
481 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000482 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000483 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000484 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000485 Cond.push_back(LastInst->getOperand(0));
486 return false;
487 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000488 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000489 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000490 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000491 Cond.push_back(LastInst->getOperand(0));
492 return false;
493 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000494 // Only supporting rr/ri versions of new-value jumps.
495 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
496 TBB = LastInst->getOperand(2).getMBB();
497 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
498 Cond.push_back(LastInst->getOperand(0));
499 Cond.push_back(LastInst->getOperand(1));
500 return false;
501 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000502 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
503 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000504 // Otherwise, don't know what this is.
505 return true;
506 }
507
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000508 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000509 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000510 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000511 if (!SecondLastInst->getOperand(1).isMBB())
512 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000513 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000514 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000515 Cond.push_back(SecondLastInst->getOperand(0));
516 FBB = LastInst->getOperand(0).getMBB();
517 return false;
518 }
519
Brendon Cahoondf43e682015-05-08 16:16:29 +0000520 // Only supporting rr/ri versions of new-value jumps.
521 if (SecLastOpcodeHasNVJump &&
522 (SecondLastInst->getNumExplicitOperands() == 3) &&
523 (LastOpcode == Hexagon::J2_jump)) {
524 TBB = SecondLastInst->getOperand(2).getMBB();
525 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
526 Cond.push_back(SecondLastInst->getOperand(0));
527 Cond.push_back(SecondLastInst->getOperand(1));
528 FBB = LastInst->getOperand(0).getMBB();
529 return false;
530 }
531
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000532 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
533 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000534 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000535 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000536 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 if (AllowModify)
538 I->eraseFromParent();
539 return false;
540 }
541
Brendon Cahoondf43e682015-05-08 16:16:29 +0000542 // If the block ends with an ENDLOOP, and J2_jump, handle it.
543 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000544 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000545 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000546 Cond.push_back(SecondLastInst->getOperand(0));
547 FBB = LastInst->getOperand(0).getMBB();
548 return false;
549 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000550 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
551 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000552 // Otherwise, can't handle this.
553 return true;
554}
555
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000556unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000557 int *BytesRemoved) const {
558 assert(!BytesRemoved && "code size not handled");
559
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000560 LLVM_DEBUG(dbgs() << "\nRemoving branches out of " << printMBBReference(MBB));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000562 unsigned Count = 0;
563 while (I != MBB.begin()) {
564 --I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000565 if (I->isDebugInstr())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000566 continue;
567 // Only removing branches from end of MBB.
568 if (!I->isBranch())
569 return Count;
570 if (Count && (I->getOpcode() == Hexagon::J2_jump))
571 llvm_unreachable("Malformed basic block: unconditional branch not last");
572 MBB.erase(&MBB.back());
573 I = MBB.end();
574 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000575 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000576 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000577}
578
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000579unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000580 MachineBasicBlock *TBB,
581 MachineBasicBlock *FBB,
582 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000583 const DebugLoc &DL,
584 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000585 unsigned BOpc = Hexagon::J2_jump;
586 unsigned BccOpc = Hexagon::J2_jumpt;
587 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000588 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000589 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000590
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000591 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000592 // If we want to reverse the branch an odd number of times, we want
593 // J2_jumpf.
594 if (!Cond.empty() && Cond[0].isImm())
595 BccOpc = Cond[0].getImm();
596
597 if (!FBB) {
598 if (Cond.empty()) {
599 // Due to a bug in TailMerging/CFG Optimization, we need to add a
600 // special case handling of a predicated jump followed by an
601 // unconditional jump. If not, Tail Merging and CFG Optimization go
602 // into an infinite loop.
603 MachineBasicBlock *NewTBB, *NewFBB;
604 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000605 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000606 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000607 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
608 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000609 reverseBranchCondition(Cond);
610 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000611 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000612 }
613 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
614 } else if (isEndLoopN(Cond[0].getImm())) {
615 int EndLoopOp = Cond[0].getImm();
616 assert(Cond[1].isMBB());
617 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
618 // Check for it, and change the BB target if needed.
619 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000620 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
621 VisitedBBs);
Eugene Zelenko3b873362017-09-28 22:27:31 +0000622 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000623 Loop->getOperand(0).setMBB(TBB);
624 // Add the ENDLOOP after the finding the LOOP0.
625 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
626 } else if (isNewValueJump(Cond[0].getImm())) {
627 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
628 // New value jump
629 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
630 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
631 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000632 LLVM_DEBUG(dbgs() << "\nInserting NVJump for "
633 << printMBBReference(MBB););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000634 if (Cond[2].isReg()) {
635 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
636 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
637 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
638 } else if(Cond[2].isImm()) {
639 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
640 addImm(Cond[2].getImm()).addMBB(TBB);
641 } else
642 llvm_unreachable("Invalid condition for branching");
643 } else {
644 assert((Cond.size() == 2) && "Malformed cond vector");
645 const MachineOperand &RO = Cond[1];
646 unsigned Flags = getUndefRegState(RO.isUndef());
647 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
648 }
649 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000650 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000651 assert((!Cond.empty()) &&
652 "Cond. cannot be empty when multiple branchings are required");
653 assert((!isNewValueJump(Cond[0].getImm())) &&
654 "NV-jump cannot be inserted with another branch");
655 // Special case for hardware loops. The condition is a basic block.
656 if (isEndLoopN(Cond[0].getImm())) {
657 int EndLoopOp = Cond[0].getImm();
658 assert(Cond[1].isMBB());
659 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
660 // Check for it, and change the BB target if needed.
661 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000662 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
663 VisitedBBs);
Eugene Zelenko3b873362017-09-28 22:27:31 +0000664 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000665 Loop->getOperand(0).setMBB(TBB);
666 // Add the ENDLOOP after the finding the LOOP0.
667 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
668 } else {
669 const MachineOperand &RO = Cond[1];
670 unsigned Flags = getUndefRegState(RO.isUndef());
671 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000672 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000673 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000674
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000675 return 2;
676}
677
Brendon Cahoon254f8892016-07-29 16:44:44 +0000678/// Analyze the loop code to find the loop induction variable and compare used
679/// to compute the number of iterations. Currently, we analyze loop that are
680/// controlled using hardware loops. In this case, the induction variable
681/// instruction is null. For all other cases, this function returns true, which
682/// means we're unable to analyze it.
683bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
684 MachineInstr *&IndVarInst,
685 MachineInstr *&CmpInst) const {
686
687 MachineBasicBlock *LoopEnd = L.getBottomBlock();
688 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
689 // We really "analyze" only hardware loops right now.
690 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
691 IndVarInst = nullptr;
692 CmpInst = &*I;
693 return false;
694 }
695 return true;
696}
697
698/// Generate code to reduce the loop iteration by one and check if the loop is
699/// finished. Return the value/register of the new loop count. this function
700/// assumes the nth iteration is peeled first.
701unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000702 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000703 SmallVectorImpl<MachineOperand> &Cond,
704 SmallVectorImpl<MachineInstr *> &PrevInsts,
705 unsigned Iter, unsigned MaxIter) const {
706 // We expect a hardware loop currently. This means that IndVar is set
707 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000708 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000709 && "Expecting a hardware loop");
710 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000711 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000712 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000713 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(),
714 Cmp.getOperand(0).getMBB(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000715 if (!Loop)
716 return 0;
717 // If the loop trip count is a compile-time value, then just change the
718 // value.
719 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
720 Loop->getOpcode() == Hexagon::J2_loop1i) {
721 int64_t Offset = Loop->getOperand(1).getImm();
722 if (Offset <= 1)
723 Loop->eraseFromParent();
724 else
725 Loop->getOperand(1).setImm(Offset - 1);
726 return Offset - 1;
727 }
728 // The loop trip count is a run-time value. We generate code to subtract
729 // one from the trip count, and update the loop instruction.
730 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
731 unsigned LoopCount = Loop->getOperand(1).getReg();
732 // Check if we're done with the loop.
733 unsigned LoopEnd = createVR(MF, MVT::i1);
734 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
735 addReg(LoopCount).addImm(1);
736 unsigned NewLoopCount = createVR(MF, MVT::i32);
737 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
738 addReg(LoopCount).addImm(-1);
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000739 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000740 // Update the previously generated instructions with the new loop counter.
741 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
742 E = PrevInsts.end(); I != E; ++I)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000743 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, HRI);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000744 PrevInsts.clear();
745 PrevInsts.push_back(NewCmp);
746 PrevInsts.push_back(NewAdd);
747 // Insert the new loop instruction if this is the last time the loop is
748 // decremented.
749 if (Iter == MaxIter)
750 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
751 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
752 // Delete the old loop instruction.
753 if (Iter == 0)
754 Loop->eraseFromParent();
755 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
756 Cond.push_back(NewCmp->getOperand(0));
757 return NewLoopCount;
758}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000759
760bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
761 unsigned NumCycles, unsigned ExtraPredCycles,
762 BranchProbability Probability) const {
763 return nonDbgBBSize(&MBB) <= 3;
764}
765
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000766bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
767 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
768 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
769 const {
770 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
771}
772
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000773bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
774 unsigned NumInstrs, BranchProbability Probability) const {
775 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000776}
777
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000778void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000779 MachineBasicBlock::iterator I,
780 const DebugLoc &DL, unsigned DestReg,
781 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000782 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000783 unsigned KillFlag = getKillRegState(KillSrc);
784
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000785 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000786 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000787 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788 return;
789 }
790 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000791 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
792 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000793 return;
794 }
795 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
796 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000797 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
798 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000799 return;
800 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000801 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000802 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000803 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
804 .addReg(SrcReg, KillFlag);
805 return;
806 }
807 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
808 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
809 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
810 .addReg(SrcReg, KillFlag);
811 return;
812 }
813 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
814 Hexagon::IntRegsRegClass.contains(SrcReg)) {
815 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
816 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000817 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000818 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000819 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
820 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000821 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
822 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000823 return;
824 }
825 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
826 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000827 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
828 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000829 return;
830 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000831 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
832 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000833 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
834 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000835 return;
836 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000837 if (Hexagon::HvxVRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000838 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000839 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000840 return;
841 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000842 if (Hexagon::HvxWRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000843 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
844 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000845 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000846 .addReg(HiSrc, KillFlag)
847 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000848 return;
849 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000850 if (Hexagon::HvxQRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000851 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
852 .addReg(SrcReg)
853 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000854 return;
855 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000856 if (Hexagon::HvxQRRegClass.contains(SrcReg) &&
857 Hexagon::HvxVRRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000858 llvm_unreachable("Unimplemented pred to vec");
859 return;
860 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000861 if (Hexagon::HvxQRRegClass.contains(DestReg) &&
862 Hexagon::HvxVRRegClass.contains(SrcReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000863 llvm_unreachable("Unimplemented vec to pred");
864 return;
865 }
Sirish Pande30804c22012-02-15 18:52:27 +0000866
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000867#ifndef NDEBUG
868 // Show the invalid registers to ease debugging.
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000869 dbgs() << "Invalid registers for copy in " << printMBBReference(MBB) << ": "
870 << printReg(DestReg, &HRI) << " = " << printReg(SrcReg, &HRI) << '\n';
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000871#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000872 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000873}
874
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000875void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
876 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
877 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000878 DebugLoc DL = MBB.findDebugLoc(I);
879 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000880 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000881 unsigned SlotAlign = MFI.getObjectAlignment(FI);
882 unsigned RegAlign = TRI->getSpillAlignment(*RC);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000883 unsigned KillFlag = getKillRegState(isKill);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000884 bool HasAlloca = MFI.hasVarSizedObjects();
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000885 const HexagonFrameLowering &HFI = *Subtarget.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000886
Alex Lorenze40c8a22015-08-11 23:09:45 +0000887 MachineMemOperand *MMO = MF.getMachineMemOperand(
888 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000889 MFI.getObjectSize(FI), SlotAlign);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000890
Craig Topperc7242e02012-04-20 07:30:17 +0000891 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000892 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000893 .addFrameIndex(FI).addImm(0)
894 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000895 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000896 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000897 .addFrameIndex(FI).addImm(0)
898 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000899 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000900 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000901 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000902 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000903 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000904 BuildMI(MBB, I, DL, get(Hexagon::STriw_ctr))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000905 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000906 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000907 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000908 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000909 .addFrameIndex(FI).addImm(0)
910 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000911 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000912 // If there are variable-sized objects, spills will not be aligned.
913 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000914 SlotAlign = HFI.getStackAlignment();
915 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vS32Ub_ai
916 : Hexagon::V6_vS32b_ai;
917 MachineMemOperand *MMOA = MF.getMachineMemOperand(
918 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
919 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000920 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000921 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000922 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
923 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000924 // If there are variable-sized objects, spills will not be aligned.
925 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000926 SlotAlign = HFI.getStackAlignment();
927 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vstorerwu_ai
928 : Hexagon::PS_vstorerw_ai;
929 MachineMemOperand *MMOA = MF.getMachineMemOperand(
930 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
931 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000932 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000933 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000934 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000935 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000936 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000937 }
938}
939
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000940void HexagonInstrInfo::loadRegFromStackSlot(
941 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
942 int FI, const TargetRegisterClass *RC,
943 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000944 DebugLoc DL = MBB.findDebugLoc(I);
945 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000946 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000947 unsigned SlotAlign = MFI.getObjectAlignment(FI);
948 unsigned RegAlign = TRI->getSpillAlignment(*RC);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000949 bool HasAlloca = MFI.hasVarSizedObjects();
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000950 const HexagonFrameLowering &HFI = *Subtarget.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000951
Alex Lorenze40c8a22015-08-11 23:09:45 +0000952 MachineMemOperand *MMO = MF.getMachineMemOperand(
953 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000954 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000955
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000956 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000957 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000958 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000959 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000960 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000961 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000962 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000963 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000964 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
965 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000966 BuildMI(MBB, I, DL, get(Hexagon::LDriw_ctr), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000967 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000968 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000969 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000970 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000971 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000972 // If there are variable-sized objects, spills will not be aligned.
973 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000974 SlotAlign = HFI.getStackAlignment();
975 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vL32Ub_ai
976 : Hexagon::V6_vL32b_ai;
977 MachineMemOperand *MMOA = MF.getMachineMemOperand(
978 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
979 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000980 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000981 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
982 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000983 // If there are variable-sized objects, spills will not be aligned.
984 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000985 SlotAlign = HFI.getStackAlignment();
986 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vloadrwu_ai
987 : Hexagon::PS_vloadrw_ai;
988 MachineMemOperand *MMOA = MF.getMachineMemOperand(
989 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
990 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000991 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000992 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000993 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000994 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000995 }
996}
997
Ron Lieberman88159e52016-09-02 22:56:24 +0000998static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
999 const MachineBasicBlock &B = *MI.getParent();
1000 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +00001001 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +00001002 for (auto I = B.rbegin(); I != E; ++I)
1003 Regs.stepBackward(*I);
1004}
1005
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001006/// expandPostRAPseudo - This function is called for all pseudo instructions
1007/// that remain after register allocation. Many pseudo instructions are
1008/// created to help register allocation. This is the place to convert them
1009/// into real instructions. The target can edit MI in place, or it can insert
1010/// new instructions and erase MI. The function should return true if
1011/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001012bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001013 MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001014 MachineFunction &MF = *MBB.getParent();
1015 MachineRegisterInfo &MRI = MF.getRegInfo();
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001016 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001017 DebugLoc DL = MI.getDebugLoc();
1018 unsigned Opc = MI.getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001019
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001020 auto RealCirc = [&](unsigned Opc, bool HasImm, unsigned MxOp) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001021 unsigned Mx = MI.getOperand(MxOp).getReg();
1022 unsigned CSx = (Mx == Hexagon::M0 ? Hexagon::CS0 : Hexagon::CS1);
1023 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrrcr), CSx)
1024 .add(MI.getOperand((HasImm ? 5 : 4)));
1025 auto MIB = BuildMI(MBB, MI, DL, get(Opc)).add(MI.getOperand(0))
1026 .add(MI.getOperand(1)).add(MI.getOperand(2)).add(MI.getOperand(3));
1027 if (HasImm)
1028 MIB.add(MI.getOperand(4));
1029 MIB.addReg(CSx, RegState::Implicit);
1030 MBB.erase(MI);
1031 return true;
1032 };
1033
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001034 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001035 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001036 MachineOperand &MD = MI.getOperand(0);
1037 MachineOperand &MS = MI.getOperand(1);
1038 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001039 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1040 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001041 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001042 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001043 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001044 return true;
1045 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001046 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001047 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001048 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001049 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001050 MBB.erase(MI);
1051 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001052 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001053 unsigned SrcReg = MI.getOperand(1).getReg();
1054 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001055 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1056 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001057 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1058 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001059 MBB.erase(MI);
1060 return true;
1061 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001062 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001063 unsigned SrcReg = MI.getOperand(1).getReg();
1064 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001065 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001066 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001067 MBB.erase(MI);
1068 MRI.clearKillFlags(SrcSubLo);
1069 return true;
1070 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001071 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001072 unsigned SrcReg = MI.getOperand(1).getReg();
1073 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001074 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001075 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001076 MBB.erase(MI);
1077 MRI.clearKillFlags(SrcSubHi);
1078 return true;
1079 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001080 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001081 case Hexagon::PS_vstorerwu_ai: {
1082 bool Aligned = Opc == Hexagon::PS_vstorerw_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001083 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001084 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1085 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001086 unsigned NewOpc = Aligned ? Hexagon::V6_vS32b_ai : Hexagon::V6_vS32Ub_ai;
1087 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001088
Chandler Carruthc73c0302018-08-16 21:30:05 +00001089 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc))
1090 .add(MI.getOperand(0))
1091 .addImm(MI.getOperand(1).getImm())
1092 .addReg(SrcSubLo)
1093 .cloneMemRefs(MI);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001094 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001095 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001096 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001097 // The Vectors are indexed in multiples of vector size.
1098 .addImm(MI.getOperand(1).getImm() + Offset)
1099 .addReg(SrcSubHi)
Chandler Carruthc73c0302018-08-16 21:30:05 +00001100 .cloneMemRefs(MI);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001101 MBB.erase(MI);
1102 return true;
1103 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001104 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001105 case Hexagon::PS_vloadrwu_ai: {
1106 bool Aligned = Opc == Hexagon::PS_vloadrw_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001107 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001108 unsigned NewOpc = Aligned ? Hexagon::V6_vL32b_ai : Hexagon::V6_vL32Ub_ai;
1109 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1110
Diana Picus116bbab2017-01-13 09:58:52 +00001111 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1112 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Chandler Carruthc73c0302018-08-16 21:30:05 +00001113 .add(MI.getOperand(1))
1114 .addImm(MI.getOperand(2).getImm())
1115 .cloneMemRefs(MI);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001116 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001117 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1118 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001119 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001120 .addImm(MI.getOperand(2).getImm() + Offset)
Chandler Carruthc73c0302018-08-16 21:30:05 +00001121 .cloneMemRefs(MI);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001122 MBB.erase(MI);
1123 return true;
1124 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001125 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001126 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001127 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1128 .addReg(Reg, RegState::Undef)
1129 .addReg(Reg, RegState::Undef);
1130 MBB.erase(MI);
1131 return true;
1132 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001133 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001134 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001135 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1136 .addReg(Reg, RegState::Undef)
1137 .addReg(Reg, RegState::Undef);
1138 MBB.erase(MI);
1139 return true;
1140 }
Krzysztof Parzyszek9b48e8d2018-02-09 19:10:46 +00001141 case Hexagon::PS_qtrue: {
1142 BuildMI(MBB, MI, DL, get(Hexagon::V6_veqw), MI.getOperand(0).getReg())
1143 .addReg(Hexagon::V0, RegState::Undef)
1144 .addReg(Hexagon::V0, RegState::Undef);
1145 MBB.erase(MI);
1146 return true;
1147 }
1148 case Hexagon::PS_qfalse: {
1149 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgtw), MI.getOperand(0).getReg())
1150 .addReg(Hexagon::V0, RegState::Undef)
1151 .addReg(Hexagon::V0, RegState::Undef);
1152 MBB.erase(MI);
1153 return true;
1154 }
Krzysztof Parzyszekc1e712b2018-06-06 19:34:40 +00001155 case Hexagon::PS_vdd0: {
1156 unsigned Vd = MI.getOperand(0).getReg();
1157 BuildMI(MBB, MI, DL, get(Hexagon::V6_vsubw_dv), Vd)
1158 .addReg(Vd, RegState::Undef)
1159 .addReg(Vd, RegState::Undef);
1160 MBB.erase(MI);
1161 return true;
1162 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001163 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001164 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001165 unsigned DstReg = MI.getOperand(0).getReg();
1166 unsigned Src1Reg = MI.getOperand(1).getReg();
1167 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001168 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1169 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1170 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1171 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001172 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001173 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001174 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001175 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001176 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001177 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001178 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001179 .addReg(Src2SubLo);
1180 MBB.erase(MI);
1181 MRI.clearKillFlags(Src1SubHi);
1182 MRI.clearKillFlags(Src1SubLo);
1183 MRI.clearKillFlags(Src2SubHi);
1184 MRI.clearKillFlags(Src2SubLo);
1185 return true;
1186 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001187 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001188 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001189 unsigned DstReg = MI.getOperand(0).getReg();
1190 unsigned Src1Reg = MI.getOperand(1).getReg();
1191 unsigned Src2Reg = MI.getOperand(2).getReg();
1192 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001193 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1194 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1195 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1196 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1197 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1198 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001199 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001200 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001201 .addReg(Src1SubHi)
1202 .addReg(Src2SubHi)
1203 .addReg(Src3SubHi);
1204 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001205 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001206 .addReg(Src1SubLo)
1207 .addReg(Src2SubLo)
1208 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001209 MBB.erase(MI);
1210 MRI.clearKillFlags(Src1SubHi);
1211 MRI.clearKillFlags(Src1SubLo);
1212 MRI.clearKillFlags(Src2SubHi);
1213 MRI.clearKillFlags(Src2SubLo);
1214 MRI.clearKillFlags(Src3SubHi);
1215 MRI.clearKillFlags(Src3SubLo);
1216 return true;
1217 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001218 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001219 const MachineOperand &Op0 = MI.getOperand(0);
1220 const MachineOperand &Op1 = MI.getOperand(1);
1221 const MachineOperand &Op2 = MI.getOperand(2);
1222 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001223 unsigned Rd = Op0.getReg();
1224 unsigned Pu = Op1.getReg();
1225 unsigned Rs = Op2.getReg();
1226 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001227 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001228 unsigned K1 = getKillRegState(Op1.isKill());
1229 unsigned K2 = getKillRegState(Op2.isKill());
1230 unsigned K3 = getKillRegState(Op3.isKill());
1231 if (Rd != Rs)
1232 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1233 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1234 .addReg(Rs, K2);
1235 if (Rd != Rt)
1236 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1237 .addReg(Pu, K1)
1238 .addReg(Rt, K3);
1239 MBB.erase(MI);
1240 return true;
1241 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001242 case Hexagon::PS_vselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001243 const MachineOperand &Op0 = MI.getOperand(0);
1244 const MachineOperand &Op1 = MI.getOperand(1);
1245 const MachineOperand &Op2 = MI.getOperand(2);
1246 const MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001247 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001248 getLiveRegsAt(LiveAtMI, MI);
1249 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001250 unsigned PReg = Op1.getReg();
1251 assert(Op1.getSubReg() == 0);
1252 unsigned PState = getRegState(Op1);
1253
Ron Lieberman88159e52016-09-02 22:56:24 +00001254 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001255 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1256 : PState;
Ron Lieberman88159e52016-09-02 22:56:24 +00001257 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001258 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001259 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001260 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001261 if (IsDestLive)
1262 T.addReg(Op0.getReg(), RegState::Implicit);
1263 IsDestLive = true;
1264 }
1265 if (Op0.getReg() != Op3.getReg()) {
1266 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001267 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001268 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001269 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001270 if (IsDestLive)
1271 T.addReg(Op0.getReg(), RegState::Implicit);
1272 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001273 MBB.erase(MI);
1274 return true;
1275 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001276 case Hexagon::PS_wselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001277 MachineOperand &Op0 = MI.getOperand(0);
1278 MachineOperand &Op1 = MI.getOperand(1);
1279 MachineOperand &Op2 = MI.getOperand(2);
1280 MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001281 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001282 getLiveRegsAt(LiveAtMI, MI);
1283 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001284 unsigned PReg = Op1.getReg();
1285 assert(Op1.getSubReg() == 0);
1286 unsigned PState = getRegState(Op1);
Ron Lieberman88159e52016-09-02 22:56:24 +00001287
1288 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001289 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1290 : PState;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001291 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1292 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001293 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001294 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001295 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001296 .add(Op1)
1297 .addReg(SrcHi)
1298 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001299 if (IsDestLive)
1300 T.addReg(Op0.getReg(), RegState::Implicit);
1301 IsDestLive = true;
1302 }
1303 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001304 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1305 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001306 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001307 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001308 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001309 .addReg(SrcHi)
1310 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001311 if (IsDestLive)
1312 T.addReg(Op0.getReg(), RegState::Implicit);
1313 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001314 MBB.erase(MI);
1315 return true;
1316 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001317
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001318 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001319 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001320 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001321 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001322 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001323 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001324 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001325 case Hexagon::PS_jmprett:
1326 MI.setDesc(get(Hexagon::J2_jumprt));
1327 return true;
1328 case Hexagon::PS_jmpretf:
1329 MI.setDesc(get(Hexagon::J2_jumprf));
1330 return true;
1331 case Hexagon::PS_jmprettnewpt:
1332 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1333 return true;
1334 case Hexagon::PS_jmpretfnewpt:
1335 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1336 return true;
1337 case Hexagon::PS_jmprettnew:
1338 MI.setDesc(get(Hexagon::J2_jumprtnew));
1339 return true;
1340 case Hexagon::PS_jmpretfnew:
1341 MI.setDesc(get(Hexagon::J2_jumprfnew));
1342 return true;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001343
1344 case Hexagon::V6_vgathermh_pseudo:
1345 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermh))
1346 .add(MI.getOperand(1))
1347 .add(MI.getOperand(2))
1348 .add(MI.getOperand(3));
1349 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1350 .add(MI.getOperand(0))
1351 .addImm(0)
1352 .addReg(Hexagon::VTMP);
1353 MBB.erase(MI);
1354 return true;
1355
1356 case Hexagon::V6_vgathermw_pseudo:
1357 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermw))
1358 .add(MI.getOperand(1))
1359 .add(MI.getOperand(2))
1360 .add(MI.getOperand(3));
1361 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1362 .add(MI.getOperand(0))
1363 .addImm(0)
1364 .addReg(Hexagon::VTMP);
1365 MBB.erase(MI);
1366 return true;
1367
1368 case Hexagon::V6_vgathermhw_pseudo:
1369 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhw))
1370 .add(MI.getOperand(1))
1371 .add(MI.getOperand(2))
1372 .add(MI.getOperand(3));
1373 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1374 .add(MI.getOperand(0))
1375 .addImm(0)
1376 .addReg(Hexagon::VTMP);
1377 MBB.erase(MI);
1378 return true;
1379
1380 case Hexagon::V6_vgathermhq_pseudo:
1381 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhq))
1382 .add(MI.getOperand(1))
1383 .add(MI.getOperand(2))
1384 .add(MI.getOperand(3))
1385 .add(MI.getOperand(4));
1386 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1387 .add(MI.getOperand(0))
1388 .addImm(0)
1389 .addReg(Hexagon::VTMP);
1390 MBB.erase(MI);
1391 return true;
1392
1393 case Hexagon::V6_vgathermwq_pseudo:
1394 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermwq))
1395 .add(MI.getOperand(1))
1396 .add(MI.getOperand(2))
1397 .add(MI.getOperand(3))
1398 .add(MI.getOperand(4));
1399 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1400 .add(MI.getOperand(0))
1401 .addImm(0)
1402 .addReg(Hexagon::VTMP);
1403 MBB.erase(MI);
1404 return true;
1405
1406 case Hexagon::V6_vgathermhwq_pseudo:
1407 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhwq))
1408 .add(MI.getOperand(1))
1409 .add(MI.getOperand(2))
1410 .add(MI.getOperand(3))
1411 .add(MI.getOperand(4));
1412 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1413 .add(MI.getOperand(0))
1414 .addImm(0)
1415 .addReg(Hexagon::VTMP);
1416 MBB.erase(MI);
1417 return true;
1418
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001419 case Hexagon::PS_loadrub_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001420 return RealCirc(Hexagon::L2_loadrub_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001421 case Hexagon::PS_loadrb_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001422 return RealCirc(Hexagon::L2_loadrb_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001423 case Hexagon::PS_loadruh_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001424 return RealCirc(Hexagon::L2_loadruh_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001425 case Hexagon::PS_loadrh_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001426 return RealCirc(Hexagon::L2_loadrh_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001427 case Hexagon::PS_loadri_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001428 return RealCirc(Hexagon::L2_loadri_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001429 case Hexagon::PS_loadrd_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001430 return RealCirc(Hexagon::L2_loadrd_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001431 case Hexagon::PS_loadrub_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001432 return RealCirc(Hexagon::L2_loadrub_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001433 case Hexagon::PS_loadrb_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001434 return RealCirc(Hexagon::L2_loadrb_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001435 case Hexagon::PS_loadruh_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001436 return RealCirc(Hexagon::L2_loadruh_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001437 case Hexagon::PS_loadrh_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001438 return RealCirc(Hexagon::L2_loadrh_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001439 case Hexagon::PS_loadri_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001440 return RealCirc(Hexagon::L2_loadri_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001441 case Hexagon::PS_loadrd_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001442 return RealCirc(Hexagon::L2_loadrd_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001443 case Hexagon::PS_storerb_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001444 return RealCirc(Hexagon::S2_storerb_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001445 case Hexagon::PS_storerh_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001446 return RealCirc(Hexagon::S2_storerh_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001447 case Hexagon::PS_storerf_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001448 return RealCirc(Hexagon::S2_storerf_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001449 case Hexagon::PS_storeri_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001450 return RealCirc(Hexagon::S2_storeri_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001451 case Hexagon::PS_storerd_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001452 return RealCirc(Hexagon::S2_storerd_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001453 case Hexagon::PS_storerb_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001454 return RealCirc(Hexagon::S2_storerb_pcr, /*HasImm*/false, /*MxOp*/2);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001455 case Hexagon::PS_storerh_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001456 return RealCirc(Hexagon::S2_storerh_pcr, /*HasImm*/false, /*MxOp*/2);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001457 case Hexagon::PS_storerf_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001458 return RealCirc(Hexagon::S2_storerf_pcr, /*HasImm*/false, /*MxOp*/2);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001459 case Hexagon::PS_storeri_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001460 return RealCirc(Hexagon::S2_storeri_pcr, /*HasImm*/false, /*MxOp*/2);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001461 case Hexagon::PS_storerd_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001462 return RealCirc(Hexagon::S2_storerd_pcr, /*HasImm*/false, /*MxOp*/2);
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001463 }
1464
1465 return false;
1466}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001467
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001468// We indicate that we want to reverse the branch by
1469// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001470bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001471 SmallVectorImpl<MachineOperand> &Cond) const {
1472 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001473 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001474 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1475 unsigned opcode = Cond[0].getImm();
1476 //unsigned temp;
1477 assert(get(opcode).isBranch() && "Should be a branching condition.");
1478 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001479 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001480 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1481 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001482 return false;
1483}
1484
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001485void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1486 MachineBasicBlock::iterator MI) const {
1487 DebugLoc DL;
1488 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1489}
1490
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001491bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1492 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001493}
1494
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001495// Returns true if an instruction is predicated irrespective of the predicate
1496// sense. For example, all of the following will return true.
1497// if (p0) R1 = add(R2, R3)
1498// if (!p0) R1 = add(R2, R3)
1499// if (p0.new) R1 = add(R2, R3)
1500// if (!p0.new) R1 = add(R2, R3)
1501// Note: New-value stores are not included here as in the current
1502// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001503bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1504 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001505 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001506}
1507
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001508bool HexagonInstrInfo::PredicateInstruction(
1509 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001510 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1511 isEndLoopN(Cond[0].getImm())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001512 LLVM_DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001513 return false;
1514 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001515 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001516 assert (isPredicable(MI) && "Expected predicable instruction");
1517 bool invertJump = predOpcodeHasNot(Cond);
1518
1519 // We have to predicate MI "in place", i.e. after this function returns,
1520 // MI will need to be transformed into a predicated form. To avoid com-
1521 // plicated manipulations with the operands (handling tied operands,
1522 // etc.), build a new temporary instruction, then overwrite MI with it.
1523
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001524 MachineBasicBlock &B = *MI.getParent();
1525 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001526 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1527 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001528 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001529 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001530 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001531 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1532 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001533 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001534 NOp++;
1535 }
1536
1537 unsigned PredReg, PredRegPos, PredRegFlags;
1538 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1539 (void)GotPredReg;
1540 assert(GotPredReg);
1541 T.addReg(PredReg, PredRegFlags);
1542 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001543 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001544
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001545 MI.setDesc(get(PredOpc));
1546 while (unsigned n = MI.getNumOperands())
1547 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001548 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001549 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001550
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001551 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001552 B.erase(TI);
1553
1554 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1555 MRI.clearKillFlags(PredReg);
1556 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001557}
1558
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001559bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1560 ArrayRef<MachineOperand> Pred2) const {
1561 // TODO: Fix this
1562 return false;
1563}
1564
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001565bool HexagonInstrInfo::DefinesPredicate(MachineInstr &MI,
1566 std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001567 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001568
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001569 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1570 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001571 if (MO.isReg()) {
1572 if (!MO.isDef())
1573 continue;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001574 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1575 if (RC == &Hexagon::PredRegsRegClass) {
1576 Pred.push_back(MO);
1577 return true;
1578 }
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001579 continue;
1580 } else if (MO.isRegMask()) {
1581 for (unsigned PR : Hexagon::PredRegsRegClass) {
1582 if (!MI.modifiesRegister(PR, &HRI))
1583 continue;
1584 Pred.push_back(MO);
1585 return true;
1586 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001587 }
1588 }
1589 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001590}
Andrew Trickd06df962012-02-01 22:13:57 +00001591
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001592bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001593 if (!MI.getDesc().isPredicable())
1594 return false;
1595
1596 if (MI.isCall() || isTailCall(MI)) {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001597 if (!Subtarget.usePredicatedCalls())
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001598 return false;
1599 }
Krzysztof Parzyszek8c53c952017-10-18 17:36:46 +00001600
1601 // HVX loads are not predicable on v60, but are on v62.
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00001602 if (!Subtarget.hasV62Ops()) {
Krzysztof Parzyszek8c53c952017-10-18 17:36:46 +00001603 switch (MI.getOpcode()) {
1604 case Hexagon::V6_vL32b_ai:
1605 case Hexagon::V6_vL32b_pi:
1606 case Hexagon::V6_vL32b_ppu:
1607 case Hexagon::V6_vL32b_cur_ai:
1608 case Hexagon::V6_vL32b_cur_pi:
1609 case Hexagon::V6_vL32b_cur_ppu:
1610 case Hexagon::V6_vL32b_nt_ai:
1611 case Hexagon::V6_vL32b_nt_pi:
1612 case Hexagon::V6_vL32b_nt_ppu:
1613 case Hexagon::V6_vL32b_tmp_ai:
1614 case Hexagon::V6_vL32b_tmp_pi:
1615 case Hexagon::V6_vL32b_tmp_ppu:
1616 case Hexagon::V6_vL32b_nt_cur_ai:
1617 case Hexagon::V6_vL32b_nt_cur_pi:
1618 case Hexagon::V6_vL32b_nt_cur_ppu:
1619 case Hexagon::V6_vL32b_nt_tmp_ai:
1620 case Hexagon::V6_vL32b_nt_tmp_pi:
1621 case Hexagon::V6_vL32b_nt_tmp_ppu:
1622 return false;
1623 }
1624 }
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001625 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001626}
1627
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001628bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1629 const MachineBasicBlock *MBB,
1630 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001631 // Debug info is never a scheduling boundary. It's necessary to be explicit
1632 // due to the special treatment of IT instructions below, otherwise a
1633 // dbg_value followed by an IT will result in the IT instruction being
1634 // considered a scheduling hazard, which is wrong. It should be the actual
1635 // instruction preceding the dbg_value instruction(s), just like it is
1636 // when debug info is not present.
Shiva Chen801bf7e2018-05-09 02:42:00 +00001637 if (MI.isDebugInstr())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001638 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001639
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001640 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001641 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001642 // Don't mess around with no return calls.
1643 if (doesNotReturn(MI))
1644 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001645 // If any of the block's successors is a landing pad, this could be a
1646 // throwing call.
1647 for (auto I : MBB->successors())
1648 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001649 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001650 }
1651
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001652 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001653 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001654 return true;
1655
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001656 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1657 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001658
1659 return false;
1660}
1661
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001662/// Measure the specified inline asm to determine an approximation of its
1663/// length.
1664/// Comments (which run till the next SeparatorString or newline) do not
1665/// count as an instruction.
1666/// Any other non-whitespace text is considered an instruction, with
1667/// multiple instructions separated by SeparatorString or newlines.
1668/// Variable-length instructions are not handled here; this function
1669/// may be overloaded in the target code to do that.
1670/// Hexagon counts the number of ##'s and adjust for that many
1671/// constant exenders.
1672unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1673 const MCAsmInfo &MAI) const {
1674 StringRef AStr(Str);
1675 // Count the number of instructions in the asm.
1676 bool atInsnStart = true;
1677 unsigned Length = 0;
1678 for (; *Str; ++Str) {
1679 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1680 strlen(MAI.getSeparatorString())) == 0)
1681 atInsnStart = true;
1682 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1683 Length += MAI.getMaxInstLength();
1684 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001685 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001686 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1687 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001688 atInsnStart = false;
1689 }
1690
1691 // Add to size number of constant extenders seen * 4.
1692 StringRef Occ("##");
1693 Length += AStr.count(Occ)*4;
1694 return Length;
1695}
1696
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001697ScheduleHazardRecognizer*
1698HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1699 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001700 if (UseDFAHazardRec)
1701 return new HexagonHazardRecognizer(II, this, Subtarget);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001702 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1703}
1704
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001705/// For a comparison instruction, return the source registers in
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001706/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1707/// compares against in CmpValue. Return true if the comparison instruction
1708/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001709bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1710 unsigned &SrcReg2, int &Mask,
1711 int &Value) const {
1712 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001713
1714 // Set mask and the first source register.
1715 switch (Opc) {
1716 case Hexagon::C2_cmpeq:
1717 case Hexagon::C2_cmpeqp:
1718 case Hexagon::C2_cmpgt:
1719 case Hexagon::C2_cmpgtp:
1720 case Hexagon::C2_cmpgtu:
1721 case Hexagon::C2_cmpgtup:
1722 case Hexagon::C4_cmpneq:
1723 case Hexagon::C4_cmplte:
1724 case Hexagon::C4_cmplteu:
1725 case Hexagon::C2_cmpeqi:
1726 case Hexagon::C2_cmpgti:
1727 case Hexagon::C2_cmpgtui:
1728 case Hexagon::C4_cmpneqi:
1729 case Hexagon::C4_cmplteui:
1730 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001731 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001732 Mask = ~0;
1733 break;
1734 case Hexagon::A4_cmpbeq:
1735 case Hexagon::A4_cmpbgt:
1736 case Hexagon::A4_cmpbgtu:
1737 case Hexagon::A4_cmpbeqi:
1738 case Hexagon::A4_cmpbgti:
1739 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001740 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001741 Mask = 0xFF;
1742 break;
1743 case Hexagon::A4_cmpheq:
1744 case Hexagon::A4_cmphgt:
1745 case Hexagon::A4_cmphgtu:
1746 case Hexagon::A4_cmpheqi:
1747 case Hexagon::A4_cmphgti:
1748 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001749 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001750 Mask = 0xFFFF;
1751 break;
1752 }
1753
1754 // Set the value/second source register.
1755 switch (Opc) {
1756 case Hexagon::C2_cmpeq:
1757 case Hexagon::C2_cmpeqp:
1758 case Hexagon::C2_cmpgt:
1759 case Hexagon::C2_cmpgtp:
1760 case Hexagon::C2_cmpgtu:
1761 case Hexagon::C2_cmpgtup:
1762 case Hexagon::A4_cmpbeq:
1763 case Hexagon::A4_cmpbgt:
1764 case Hexagon::A4_cmpbgtu:
1765 case Hexagon::A4_cmpheq:
1766 case Hexagon::A4_cmphgt:
1767 case Hexagon::A4_cmphgtu:
1768 case Hexagon::C4_cmpneq:
1769 case Hexagon::C4_cmplte:
1770 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001771 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001772 return true;
1773
1774 case Hexagon::C2_cmpeqi:
1775 case Hexagon::C2_cmpgtui:
1776 case Hexagon::C2_cmpgti:
1777 case Hexagon::C4_cmpneqi:
1778 case Hexagon::C4_cmplteui:
1779 case Hexagon::C4_cmpltei:
1780 case Hexagon::A4_cmpbeqi:
1781 case Hexagon::A4_cmpbgti:
1782 case Hexagon::A4_cmpbgtui:
1783 case Hexagon::A4_cmpheqi:
1784 case Hexagon::A4_cmphgti:
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001785 case Hexagon::A4_cmphgtui: {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001786 SrcReg2 = 0;
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001787 const MachineOperand &Op2 = MI.getOperand(2);
1788 if (!Op2.isImm())
1789 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001790 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001791 return true;
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001792 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001793 }
1794
1795 return false;
1796}
1797
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001798unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001799 const MachineInstr &MI,
1800 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001801 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001802}
1803
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001804DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1805 const TargetSubtargetInfo &STI) const {
1806 const InstrItineraryData *II = STI.getInstrItineraryData();
1807 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1808}
1809
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001810// Inspired by this pair:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001811// %r13 = L2_loadri_io %r29, 136; mem:LD4[FixedStack0]
1812// S2_storeri_io %r29, 132, killed %r1; flags: mem:ST4[FixedStack1]
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001813// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001814bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1815 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001816 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1817 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001818 return false;
1819
1820 // Instructions that are pure loads, not loads and stores like memops are not
1821 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001822 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001823 return true;
1824
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001825 // Get the base register in MIa.
1826 unsigned BasePosA, OffsetPosA;
1827 if (!getBaseAndOffsetPosition(MIa, BasePosA, OffsetPosA))
1828 return false;
1829 const MachineOperand &BaseA = MIa.getOperand(BasePosA);
1830 unsigned BaseRegA = BaseA.getReg();
1831 unsigned BaseSubA = BaseA.getSubReg();
1832
1833 // Get the base register in MIb.
1834 unsigned BasePosB, OffsetPosB;
1835 if (!getBaseAndOffsetPosition(MIb, BasePosB, OffsetPosB))
1836 return false;
1837 const MachineOperand &BaseB = MIb.getOperand(BasePosB);
1838 unsigned BaseRegB = BaseB.getReg();
1839 unsigned BaseSubB = BaseB.getSubReg();
1840
1841 if (BaseRegA != BaseRegB || BaseSubA != BaseSubB)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001842 return false;
1843
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001844 // Get the access sizes.
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00001845 unsigned SizeA = getMemAccessSize(MIa);
1846 unsigned SizeB = getMemAccessSize(MIb);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001847
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001848 // Get the offsets. Handle immediates only for now.
1849 const MachineOperand &OffA = MIa.getOperand(OffsetPosA);
1850 const MachineOperand &OffB = MIb.getOperand(OffsetPosB);
1851 if (!MIa.getOperand(OffsetPosA).isImm() ||
1852 !MIb.getOperand(OffsetPosB).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001853 return false;
Krzysztof Parzyszekac019942017-07-19 19:17:32 +00001854 int OffsetA = isPostIncrement(MIa) ? 0 : OffA.getImm();
1855 int OffsetB = isPostIncrement(MIb) ? 0 : OffB.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001856
1857 // This is a mem access with the same base register and known offsets from it.
1858 // Reason about it.
1859 if (OffsetA > OffsetB) {
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001860 uint64_t OffDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1861 return SizeB <= OffDiff;
1862 }
1863 if (OffsetA < OffsetB) {
1864 uint64_t OffDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1865 return SizeA <= OffDiff;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001866 }
1867
1868 return false;
1869}
1870
Brendon Cahoon254f8892016-07-29 16:44:44 +00001871/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001872bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001873 int &Value) const {
1874 if (isPostIncrement(MI)) {
Krzysztof Parzyszek12bdcab2017-10-11 15:59:51 +00001875 unsigned BasePos = 0, OffsetPos = 0;
1876 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
1877 return false;
1878 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
1879 if (OffsetOp.isImm()) {
1880 Value = OffsetOp.getImm();
1881 return true;
1882 }
Krzysztof Parzyszekbf626192017-10-11 16:15:31 +00001883 } else if (MI.getOpcode() == Hexagon::A2_addi) {
1884 const MachineOperand &AddOp = MI.getOperand(2);
1885 if (AddOp.isImm()) {
1886 Value = AddOp.getImm();
1887 return true;
1888 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00001889 }
1890
1891 return false;
1892}
1893
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001894std::pair<unsigned, unsigned>
1895HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1896 return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
1897 TF & HexagonII::MO_Bitmasks);
1898}
1899
1900ArrayRef<std::pair<unsigned, const char*>>
1901HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1902 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00001903
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001904 static const std::pair<unsigned, const char*> Flags[] = {
1905 {MO_PCREL, "hexagon-pcrel"},
1906 {MO_GOT, "hexagon-got"},
1907 {MO_LO16, "hexagon-lo16"},
1908 {MO_HI16, "hexagon-hi16"},
1909 {MO_GPREL, "hexagon-gprel"},
1910 {MO_GDGOT, "hexagon-gdgot"},
1911 {MO_GDPLT, "hexagon-gdplt"},
1912 {MO_IE, "hexagon-ie"},
1913 {MO_IEGOT, "hexagon-iegot"},
1914 {MO_TPREL, "hexagon-tprel"}
1915 };
1916 return makeArrayRef(Flags);
1917}
1918
1919ArrayRef<std::pair<unsigned, const char*>>
1920HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1921 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00001922
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001923 static const std::pair<unsigned, const char*> Flags[] = {
1924 {HMOTF_ConstExtended, "hexagon-ext"}
1925 };
1926 return makeArrayRef(Flags);
1927}
1928
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001929unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001930 MachineRegisterInfo &MRI = MF->getRegInfo();
1931 const TargetRegisterClass *TRC;
1932 if (VT == MVT::i1) {
1933 TRC = &Hexagon::PredRegsRegClass;
1934 } else if (VT == MVT::i32 || VT == MVT::f32) {
1935 TRC = &Hexagon::IntRegsRegClass;
1936 } else if (VT == MVT::i64 || VT == MVT::f64) {
1937 TRC = &Hexagon::DoubleRegsRegClass;
1938 } else {
1939 llvm_unreachable("Cannot handle this register class");
1940 }
1941
1942 unsigned NewReg = MRI.createVirtualRegister(TRC);
1943 return NewReg;
1944}
1945
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001946bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001947 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1948}
1949
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001950bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1951 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001952 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1953}
1954
Krzysztof Parzyszek998df2c2018-03-23 20:43:02 +00001955bool HexagonInstrInfo::isBaseImmOffset(const MachineInstr &MI) const {
1956 return getAddrMode(MI) == HexagonII::BaseImmOffset;
1957}
1958
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001959bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001960 return !isTC1(MI) && !isTC2Early(MI) && !MI.getDesc().mayLoad() &&
1961 !MI.getDesc().mayStore() &&
1962 MI.getDesc().getOpcode() != Hexagon::S2_allocframe &&
1963 MI.getDesc().getOpcode() != Hexagon::L2_deallocframe &&
1964 !isMemOp(MI) && !MI.isBranch() && !MI.isReturn() && !MI.isCall();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001965}
1966
Sanjay Patele4b9f502015-12-07 19:21:39 +00001967// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001968bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001969 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001970}
1971
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001972// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1973// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001974bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1975 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001976 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1977 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001978 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001979
1980 unsigned isExtendable =
1981 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1982 if (!isExtendable)
1983 return false;
1984
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001985 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001986 return false;
1987
1988 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001989 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001990 // Use MO operand flags to determine if MO
1991 // has the HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00001992 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001993 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001994 // If this is a Machine BB address we are talking about, and it is
1995 // not marked as extended, say so.
1996 if (MO.isMBB())
1997 return false;
1998
1999 // We could be using an instruction with an extendable immediate and shoehorn
2000 // a global address into it. If it is a global address it will be constant
2001 // extended. We do this for COMBINE.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002002 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00002003 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002004 return true;
2005
2006 // If the extendable operand is not 'Immediate' type, the instruction should
2007 // have 'isExtended' flag set.
2008 assert(MO.isImm() && "Extendable operand must be Immediate type");
2009
2010 int MinValue = getMinValue(MI);
2011 int MaxValue = getMaxValue(MI);
2012 int ImmValue = MO.getImm();
2013
2014 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002015}
2016
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002017bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
2018 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002019 case Hexagon::L4_return:
2020 case Hexagon::L4_return_t:
2021 case Hexagon::L4_return_f:
2022 case Hexagon::L4_return_tnew_pnt:
2023 case Hexagon::L4_return_fnew_pnt:
2024 case Hexagon::L4_return_tnew_pt:
2025 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002026 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002027 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002028 return false;
2029}
2030
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002031// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002032bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
2033 const MachineInstr &ConsMI) const {
2034 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002035 return false;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00002036 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002037
2038 SmallVector<unsigned, 4> DefsA;
2039 SmallVector<unsigned, 4> DefsB;
2040 SmallVector<unsigned, 8> UsesA;
2041 SmallVector<unsigned, 8> UsesB;
2042
2043 parseOperands(ProdMI, DefsA, UsesA);
2044 parseOperands(ConsMI, DefsB, UsesB);
2045
2046 for (auto &RegA : DefsA)
2047 for (auto &RegB : UsesB) {
2048 // True data dependency.
2049 if (RegA == RegB)
2050 return true;
2051
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002052 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002053 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
2054 if (RegB == *SubRegs)
2055 return true;
2056
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002057 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002058 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
2059 if (RegA == *SubRegs)
2060 return true;
2061 }
2062
2063 return false;
2064}
2065
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002066// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002067bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2068 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002069 case Hexagon::V6_vL32b_cur_pi:
2070 case Hexagon::V6_vL32b_cur_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002071 return true;
2072 }
2073 return false;
2074}
2075
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002076// Returns true, if any one of the operands is a dot new
2077// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002078bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2079 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002080 return true;
2081
2082 return false;
2083}
2084
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002085/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002086bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2087 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002088 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2089 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2090 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2091}
2092
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002093bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2094 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002095 return true;
2096
2097 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002098 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002099 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002100}
2101
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002102bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2103 return (Opcode == Hexagon::ENDLOOP0 ||
2104 Opcode == Hexagon::ENDLOOP1);
2105}
2106
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002107bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2108 switch(OpType) {
2109 case MachineOperand::MO_MachineBasicBlock:
2110 case MachineOperand::MO_GlobalAddress:
2111 case MachineOperand::MO_ExternalSymbol:
2112 case MachineOperand::MO_JumpTableIndex:
2113 case MachineOperand::MO_ConstantPoolIndex:
2114 case MachineOperand::MO_BlockAddress:
2115 return true;
2116 default:
2117 return false;
2118 }
2119}
2120
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002121bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2122 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002123 const uint64_t F = MID.TSFlags;
2124 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2125 return true;
2126
2127 // TODO: This is largely obsolete now. Will need to be removed
2128 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002129 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002130 // PS_fi and PS_fia remain special cases.
2131 case Hexagon::PS_fi:
2132 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002133 return true;
2134 default:
2135 return false;
2136 }
2137 return false;
2138}
2139
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002140// This returns true in two cases:
2141// - The OP code itself indicates that this is an extended instruction.
2142// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002143bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002144 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002145 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002146 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2147 return true;
2148 // Use MO operand flags to determine if one of MI's operands
2149 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00002150 for (const MachineOperand &MO : MI.operands())
2151 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002152 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002153 return false;
2154}
2155
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002156bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2157 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002158 const uint64_t F = get(Opcode).TSFlags;
2159 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2160}
2161
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002162// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002163bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2164 const MachineInstr &J) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002165 if (!isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002166 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002167 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002168 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002169 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002170}
2171
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002172bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2173 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002174 case Hexagon::J2_callr:
2175 case Hexagon::J2_callrf:
2176 case Hexagon::J2_callrt:
2177 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002178 return true;
2179 }
2180 return false;
2181}
2182
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002183bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2184 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002185 case Hexagon::L4_return:
2186 case Hexagon::L4_return_t:
2187 case Hexagon::L4_return_f:
2188 case Hexagon::L4_return_fnew_pnt:
2189 case Hexagon::L4_return_fnew_pt:
2190 case Hexagon::L4_return_tnew_pnt:
2191 case Hexagon::L4_return_tnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002192 return true;
2193 }
2194 return false;
2195}
2196
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002197bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2198 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002199 case Hexagon::J2_jumpr:
2200 case Hexagon::J2_jumprt:
2201 case Hexagon::J2_jumprf:
2202 case Hexagon::J2_jumprtnewpt:
2203 case Hexagon::J2_jumprfnewpt:
2204 case Hexagon::J2_jumprtnew:
2205 case Hexagon::J2_jumprfnew:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002206 return true;
2207 }
2208 return false;
2209}
2210
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002211// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002212// Use abs estimate as oppose to the exact number.
2213// TODO: This will need to be changed to use MC level
2214// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002215bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002216 unsigned offset) const {
2217 // This selection of jump instructions matches to that what
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002218 // analyzeBranch can parse, plus NVJ.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002219 if (isNewValueJump(MI)) // r9:2
2220 return isInt<11>(offset);
2221
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002222 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002223 // Still missing Jump to address condition on register value.
2224 default:
2225 return false;
2226 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2227 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002228 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002229 return isInt<24>(offset);
2230 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2231 case Hexagon::J2_jumpf:
2232 case Hexagon::J2_jumptnew:
2233 case Hexagon::J2_jumptnewpt:
2234 case Hexagon::J2_jumpfnew:
2235 case Hexagon::J2_jumpfnewpt:
2236 case Hexagon::J2_callt:
2237 case Hexagon::J2_callf:
2238 return isInt<17>(offset);
2239 case Hexagon::J2_loop0i:
2240 case Hexagon::J2_loop0iext:
2241 case Hexagon::J2_loop0r:
2242 case Hexagon::J2_loop0rext:
2243 case Hexagon::J2_loop1i:
2244 case Hexagon::J2_loop1iext:
2245 case Hexagon::J2_loop1r:
2246 case Hexagon::J2_loop1rext:
2247 return isInt<9>(offset);
2248 // TODO: Add all the compound branches here. Can we do this in Relation model?
2249 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2250 case Hexagon::J4_cmpeqi_tp1_jump_nt:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002251 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
2252 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002253 return isInt<11>(offset);
2254 }
2255}
2256
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002257bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2258 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002259 bool isLate = isLateResultInstr(LRMI);
2260 bool isEarly = isEarlySourceInstr(ESMI);
2261
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002262 LLVM_DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
2263 LLVM_DEBUG(LRMI.dump());
2264 LLVM_DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
2265 LLVM_DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002266
2267 if (isLate && isEarly) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002268 LLVM_DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002269 return true;
2270 }
2271
2272 return false;
2273}
2274
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002275bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2276 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002277 case TargetOpcode::EXTRACT_SUBREG:
2278 case TargetOpcode::INSERT_SUBREG:
2279 case TargetOpcode::SUBREG_TO_REG:
2280 case TargetOpcode::REG_SEQUENCE:
2281 case TargetOpcode::IMPLICIT_DEF:
2282 case TargetOpcode::COPY:
2283 case TargetOpcode::INLINEASM:
2284 case TargetOpcode::PHI:
2285 return false;
2286 default:
2287 break;
2288 }
2289
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002290 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002291 return !is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002292}
2293
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002294bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002295 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2296 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002297 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002298}
2299
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002300bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2301 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002302 return Opcode == Hexagon::J2_loop0i ||
2303 Opcode == Hexagon::J2_loop0r ||
2304 Opcode == Hexagon::J2_loop0iext ||
2305 Opcode == Hexagon::J2_loop0rext ||
2306 Opcode == Hexagon::J2_loop1i ||
2307 Opcode == Hexagon::J2_loop1r ||
2308 Opcode == Hexagon::J2_loop1iext ||
2309 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002310}
2311
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002312bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2313 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002314 default: return false;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002315 case Hexagon::L4_iadd_memopw_io:
2316 case Hexagon::L4_isub_memopw_io:
2317 case Hexagon::L4_add_memopw_io:
2318 case Hexagon::L4_sub_memopw_io:
2319 case Hexagon::L4_and_memopw_io:
2320 case Hexagon::L4_or_memopw_io:
2321 case Hexagon::L4_iadd_memoph_io:
2322 case Hexagon::L4_isub_memoph_io:
2323 case Hexagon::L4_add_memoph_io:
2324 case Hexagon::L4_sub_memoph_io:
2325 case Hexagon::L4_and_memoph_io:
2326 case Hexagon::L4_or_memoph_io:
2327 case Hexagon::L4_iadd_memopb_io:
2328 case Hexagon::L4_isub_memopb_io:
2329 case Hexagon::L4_add_memopb_io:
2330 case Hexagon::L4_sub_memopb_io:
2331 case Hexagon::L4_and_memopb_io:
2332 case Hexagon::L4_or_memopb_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002333 case Hexagon::L4_ior_memopb_io:
2334 case Hexagon::L4_ior_memoph_io:
2335 case Hexagon::L4_ior_memopw_io:
2336 case Hexagon::L4_iand_memopb_io:
2337 case Hexagon::L4_iand_memoph_io:
2338 case Hexagon::L4_iand_memopw_io:
2339 return true;
2340 }
2341 return false;
2342}
2343
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002344bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2345 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002346 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2347}
2348
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002349bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2350 const uint64_t F = get(Opcode).TSFlags;
2351 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2352}
2353
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002354bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002355 return isNewValueJump(MI) || isNewValueStore(MI);
2356}
2357
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002358bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2359 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002360}
2361
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002362bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2363 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2364}
2365
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002366bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2367 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002368 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2369}
2370
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002371bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2372 const uint64_t F = get(Opcode).TSFlags;
2373 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2374}
2375
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002376// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002377bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002378 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002379 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002380 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2381 == OperandNum;
2382}
2383
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002384bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2385 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002386 assert(isPredicated(MI));
2387 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2388}
2389
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002390bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2391 const uint64_t F = get(Opcode).TSFlags;
2392 assert(isPredicated(Opcode));
2393 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2394}
2395
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002396bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2397 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002398 return !((F >> HexagonII::PredicatedFalsePos) &
2399 HexagonII::PredicatedFalseMask);
2400}
2401
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002402bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2403 const uint64_t F = get(Opcode).TSFlags;
2404 // Make sure that the instruction is predicated.
2405 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2406 return !((F >> HexagonII::PredicatedFalsePos) &
2407 HexagonII::PredicatedFalseMask);
2408}
2409
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002410bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2411 const uint64_t F = get(Opcode).TSFlags;
2412 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2413}
2414
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002415bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2416 const uint64_t F = get(Opcode).TSFlags;
2417 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2418}
2419
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002420bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2421 const uint64_t F = get(Opcode).TSFlags;
2422 assert(get(Opcode).isBranch() &&
2423 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2424 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2425}
2426
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002427bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2428 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2429 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2430 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2431 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002432}
2433
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002434bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2435 switch (MI.getOpcode()) {
2436 // Byte
2437 case Hexagon::L2_loadrb_io:
2438 case Hexagon::L4_loadrb_ur:
2439 case Hexagon::L4_loadrb_ap:
2440 case Hexagon::L2_loadrb_pr:
2441 case Hexagon::L2_loadrb_pbr:
2442 case Hexagon::L2_loadrb_pi:
2443 case Hexagon::L2_loadrb_pci:
2444 case Hexagon::L2_loadrb_pcr:
2445 case Hexagon::L2_loadbsw2_io:
2446 case Hexagon::L4_loadbsw2_ur:
2447 case Hexagon::L4_loadbsw2_ap:
2448 case Hexagon::L2_loadbsw2_pr:
2449 case Hexagon::L2_loadbsw2_pbr:
2450 case Hexagon::L2_loadbsw2_pi:
2451 case Hexagon::L2_loadbsw2_pci:
2452 case Hexagon::L2_loadbsw2_pcr:
2453 case Hexagon::L2_loadbsw4_io:
2454 case Hexagon::L4_loadbsw4_ur:
2455 case Hexagon::L4_loadbsw4_ap:
2456 case Hexagon::L2_loadbsw4_pr:
2457 case Hexagon::L2_loadbsw4_pbr:
2458 case Hexagon::L2_loadbsw4_pi:
2459 case Hexagon::L2_loadbsw4_pci:
2460 case Hexagon::L2_loadbsw4_pcr:
2461 case Hexagon::L4_loadrb_rr:
2462 case Hexagon::L2_ploadrbt_io:
2463 case Hexagon::L2_ploadrbt_pi:
2464 case Hexagon::L2_ploadrbf_io:
2465 case Hexagon::L2_ploadrbf_pi:
2466 case Hexagon::L2_ploadrbtnew_io:
2467 case Hexagon::L2_ploadrbfnew_io:
2468 case Hexagon::L4_ploadrbt_rr:
2469 case Hexagon::L4_ploadrbf_rr:
2470 case Hexagon::L4_ploadrbtnew_rr:
2471 case Hexagon::L4_ploadrbfnew_rr:
2472 case Hexagon::L2_ploadrbtnew_pi:
2473 case Hexagon::L2_ploadrbfnew_pi:
2474 case Hexagon::L4_ploadrbt_abs:
2475 case Hexagon::L4_ploadrbf_abs:
2476 case Hexagon::L4_ploadrbtnew_abs:
2477 case Hexagon::L4_ploadrbfnew_abs:
2478 case Hexagon::L2_loadrbgp:
2479 // Half
2480 case Hexagon::L2_loadrh_io:
2481 case Hexagon::L4_loadrh_ur:
2482 case Hexagon::L4_loadrh_ap:
2483 case Hexagon::L2_loadrh_pr:
2484 case Hexagon::L2_loadrh_pbr:
2485 case Hexagon::L2_loadrh_pi:
2486 case Hexagon::L2_loadrh_pci:
2487 case Hexagon::L2_loadrh_pcr:
2488 case Hexagon::L4_loadrh_rr:
2489 case Hexagon::L2_ploadrht_io:
2490 case Hexagon::L2_ploadrht_pi:
2491 case Hexagon::L2_ploadrhf_io:
2492 case Hexagon::L2_ploadrhf_pi:
2493 case Hexagon::L2_ploadrhtnew_io:
2494 case Hexagon::L2_ploadrhfnew_io:
2495 case Hexagon::L4_ploadrht_rr:
2496 case Hexagon::L4_ploadrhf_rr:
2497 case Hexagon::L4_ploadrhtnew_rr:
2498 case Hexagon::L4_ploadrhfnew_rr:
2499 case Hexagon::L2_ploadrhtnew_pi:
2500 case Hexagon::L2_ploadrhfnew_pi:
2501 case Hexagon::L4_ploadrht_abs:
2502 case Hexagon::L4_ploadrhf_abs:
2503 case Hexagon::L4_ploadrhtnew_abs:
2504 case Hexagon::L4_ploadrhfnew_abs:
2505 case Hexagon::L2_loadrhgp:
2506 return true;
2507 default:
2508 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002509 }
2510}
2511
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002512bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2513 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002514 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2515}
2516
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002517bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2518 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002519 case Hexagon::STriw_pred:
2520 case Hexagon::LDriw_pred:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002521 return true;
2522 default:
2523 return false;
2524 }
2525}
2526
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002527bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2528 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002529 return false;
2530
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002531 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002532 if (Op.isGlobal() || Op.isSymbol())
2533 return true;
2534 return false;
2535}
2536
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002537// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002538bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2539 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002540 return is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002541}
2542
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002543bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2544 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002545 return is_TC2(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002546}
2547
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002548bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2549 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002550 return is_TC2early(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002551}
2552
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002553bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2554 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002555 return is_TC4x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002556}
2557
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002558// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002559bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2560 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002561 if (mayBeCurLoad(MI1)) {
2562 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002563 unsigned DstReg = MI1.getOperand(0).getReg();
2564 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002565 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002566 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002567 return true;
2568 }
2569 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002570 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2571 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2572 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002573 return true;
2574 return false;
2575}
2576
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002577bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002578 const uint64_t V = getType(MI);
2579 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2580}
2581
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002582// Check if the Offset is a valid auto-inc imm by Load/Store Type.
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002583bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, int Offset) const {
2584 int Size = VT.getSizeInBits() / 8;
2585 if (Offset % Size != 0)
2586 return false;
2587 int Count = Offset / Size;
2588
2589 switch (VT.getSimpleVT().SimpleTy) {
2590 // For scalars the auto-inc is s4
2591 case MVT::i8:
2592 case MVT::i16:
2593 case MVT::i32:
2594 case MVT::i64:
Brendon Cahoone5ed5632018-05-18 18:14:44 +00002595 case MVT::f32:
2596 case MVT::f64:
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002597 case MVT::v2i16:
2598 case MVT::v2i32:
2599 case MVT::v4i8:
2600 case MVT::v4i16:
2601 case MVT::v8i8:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002602 return isInt<4>(Count);
2603 // For HVX vectors the auto-inc is s3
2604 case MVT::v64i8:
2605 case MVT::v32i16:
2606 case MVT::v16i32:
2607 case MVT::v8i64:
2608 case MVT::v128i8:
2609 case MVT::v64i16:
2610 case MVT::v32i32:
2611 case MVT::v16i64:
2612 return isInt<3>(Count);
2613 default:
2614 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002615 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002616
2617 llvm_unreachable("Not an valid type!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002618}
2619
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002620bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002621 const TargetRegisterInfo *TRI, bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002622 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002623 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002624 // inserted to calculate the final address. Due to this reason, the function
2625 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002626 // We used to assert if the offset was not properly aligned, however,
2627 // there are cases where a misaligned pointer recast can cause this
2628 // problem, and we need to allow for it. The front end warns of such
2629 // misaligns with respect to load size.
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002630 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002631 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002632 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002633 case Hexagon::PS_vstorerw_nt_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002634 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002635 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002636 case Hexagon::PS_vloadrw_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002637 case Hexagon::V6_vL32b_ai:
2638 case Hexagon::V6_vS32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002639 case Hexagon::V6_vL32b_nt_ai:
2640 case Hexagon::V6_vS32b_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002641 case Hexagon::V6_vL32Ub_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002642 case Hexagon::V6_vS32Ub_ai: {
2643 unsigned VectorSize = TRI->getSpillSize(Hexagon::HvxVRRegClass);
2644 assert(isPowerOf2_32(VectorSize));
2645 if (Offset & (VectorSize-1))
2646 return false;
2647 return isInt<4>(Offset >> Log2_32(VectorSize));
2648 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002649
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002650 case Hexagon::J2_loop0i:
2651 case Hexagon::J2_loop1i:
2652 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002653
2654 case Hexagon::S4_storeirb_io:
2655 case Hexagon::S4_storeirbt_io:
2656 case Hexagon::S4_storeirbf_io:
2657 return isUInt<6>(Offset);
2658
2659 case Hexagon::S4_storeirh_io:
2660 case Hexagon::S4_storeirht_io:
2661 case Hexagon::S4_storeirhf_io:
2662 return isShiftedUInt<6,1>(Offset);
2663
2664 case Hexagon::S4_storeiri_io:
2665 case Hexagon::S4_storeirit_io:
2666 case Hexagon::S4_storeirif_io:
2667 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002668 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002669
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002670 if (Extend)
2671 return true;
2672
2673 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002674 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002675 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002676 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2677 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2678
Colin LeMahieu947cd702014-12-23 20:44:59 +00002679 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002680 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002681 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2682 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2683
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002684 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002685 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002686 case Hexagon::S2_storerh_io:
Krzysztof Parzyszekd10df492017-05-03 15:36:51 +00002687 case Hexagon::S2_storerf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002688 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2689 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2690
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002691 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002692 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002693 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002694 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2695 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2696
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002697 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002698 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2699 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2700
Eugene Zelenko3b873362017-09-28 22:27:31 +00002701 case Hexagon::L4_iadd_memopw_io:
2702 case Hexagon::L4_isub_memopw_io:
2703 case Hexagon::L4_add_memopw_io:
2704 case Hexagon::L4_sub_memopw_io:
2705 case Hexagon::L4_and_memopw_io:
2706 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002707 return (0 <= Offset && Offset <= 255);
2708
Eugene Zelenko3b873362017-09-28 22:27:31 +00002709 case Hexagon::L4_iadd_memoph_io:
2710 case Hexagon::L4_isub_memoph_io:
2711 case Hexagon::L4_add_memoph_io:
2712 case Hexagon::L4_sub_memoph_io:
2713 case Hexagon::L4_and_memoph_io:
2714 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002715 return (0 <= Offset && Offset <= 127);
2716
Eugene Zelenko3b873362017-09-28 22:27:31 +00002717 case Hexagon::L4_iadd_memopb_io:
2718 case Hexagon::L4_isub_memopb_io:
2719 case Hexagon::L4_add_memopb_io:
2720 case Hexagon::L4_sub_memopb_io:
2721 case Hexagon::L4_and_memopb_io:
2722 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002723 return (0 <= Offset && Offset <= 63);
2724
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002725 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002726 // any size. Later pass knows how to handle it.
2727 case Hexagon::STriw_pred:
2728 case Hexagon::LDriw_pred:
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00002729 case Hexagon::STriw_ctr:
2730 case Hexagon::LDriw_ctr:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002731 return true;
2732
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002733 case Hexagon::PS_fi:
2734 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002735 case Hexagon::INLINEASM:
2736 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002737
2738 case Hexagon::L2_ploadrbt_io:
2739 case Hexagon::L2_ploadrbf_io:
2740 case Hexagon::L2_ploadrubt_io:
2741 case Hexagon::L2_ploadrubf_io:
2742 case Hexagon::S2_pstorerbt_io:
2743 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002744 return isUInt<6>(Offset);
2745
2746 case Hexagon::L2_ploadrht_io:
2747 case Hexagon::L2_ploadrhf_io:
2748 case Hexagon::L2_ploadruht_io:
2749 case Hexagon::L2_ploadruhf_io:
2750 case Hexagon::S2_pstorerht_io:
2751 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002752 return isShiftedUInt<6,1>(Offset);
2753
2754 case Hexagon::L2_ploadrit_io:
2755 case Hexagon::L2_ploadrif_io:
2756 case Hexagon::S2_pstorerit_io:
2757 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002758 return isShiftedUInt<6,2>(Offset);
2759
2760 case Hexagon::L2_ploadrdt_io:
2761 case Hexagon::L2_ploadrdf_io:
2762 case Hexagon::S2_pstorerdt_io:
2763 case Hexagon::S2_pstorerdf_io:
2764 return isShiftedUInt<6,3>(Offset);
2765 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002766
Benjamin Kramerb6684012011-12-27 11:41:05 +00002767 llvm_unreachable("No offset range is defined for this opcode. "
2768 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002769}
2770
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002771bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002772 return isHVXVec(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002773}
2774
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002775bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2776 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002777 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2778 return
2779 V == HexagonII::TypeCVI_VA ||
2780 V == HexagonII::TypeCVI_VA_DV;
2781}
Andrew Trickd06df962012-02-01 22:13:57 +00002782
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002783bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2784 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002785 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2786 return true;
2787
2788 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2789 return true;
2790
2791 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002792 return true;
2793
2794 return false;
2795}
Jyotsna Verma84256432013-03-01 17:37:13 +00002796
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002797bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2798 switch (MI.getOpcode()) {
2799 // Byte
2800 case Hexagon::L2_loadrub_io:
2801 case Hexagon::L4_loadrub_ur:
2802 case Hexagon::L4_loadrub_ap:
2803 case Hexagon::L2_loadrub_pr:
2804 case Hexagon::L2_loadrub_pbr:
2805 case Hexagon::L2_loadrub_pi:
2806 case Hexagon::L2_loadrub_pci:
2807 case Hexagon::L2_loadrub_pcr:
2808 case Hexagon::L2_loadbzw2_io:
2809 case Hexagon::L4_loadbzw2_ur:
2810 case Hexagon::L4_loadbzw2_ap:
2811 case Hexagon::L2_loadbzw2_pr:
2812 case Hexagon::L2_loadbzw2_pbr:
2813 case Hexagon::L2_loadbzw2_pi:
2814 case Hexagon::L2_loadbzw2_pci:
2815 case Hexagon::L2_loadbzw2_pcr:
2816 case Hexagon::L2_loadbzw4_io:
2817 case Hexagon::L4_loadbzw4_ur:
2818 case Hexagon::L4_loadbzw4_ap:
2819 case Hexagon::L2_loadbzw4_pr:
2820 case Hexagon::L2_loadbzw4_pbr:
2821 case Hexagon::L2_loadbzw4_pi:
2822 case Hexagon::L2_loadbzw4_pci:
2823 case Hexagon::L2_loadbzw4_pcr:
2824 case Hexagon::L4_loadrub_rr:
2825 case Hexagon::L2_ploadrubt_io:
2826 case Hexagon::L2_ploadrubt_pi:
2827 case Hexagon::L2_ploadrubf_io:
2828 case Hexagon::L2_ploadrubf_pi:
2829 case Hexagon::L2_ploadrubtnew_io:
2830 case Hexagon::L2_ploadrubfnew_io:
2831 case Hexagon::L4_ploadrubt_rr:
2832 case Hexagon::L4_ploadrubf_rr:
2833 case Hexagon::L4_ploadrubtnew_rr:
2834 case Hexagon::L4_ploadrubfnew_rr:
2835 case Hexagon::L2_ploadrubtnew_pi:
2836 case Hexagon::L2_ploadrubfnew_pi:
2837 case Hexagon::L4_ploadrubt_abs:
2838 case Hexagon::L4_ploadrubf_abs:
2839 case Hexagon::L4_ploadrubtnew_abs:
2840 case Hexagon::L4_ploadrubfnew_abs:
2841 case Hexagon::L2_loadrubgp:
2842 // Half
2843 case Hexagon::L2_loadruh_io:
2844 case Hexagon::L4_loadruh_ur:
2845 case Hexagon::L4_loadruh_ap:
2846 case Hexagon::L2_loadruh_pr:
2847 case Hexagon::L2_loadruh_pbr:
2848 case Hexagon::L2_loadruh_pi:
2849 case Hexagon::L2_loadruh_pci:
2850 case Hexagon::L2_loadruh_pcr:
2851 case Hexagon::L4_loadruh_rr:
2852 case Hexagon::L2_ploadruht_io:
2853 case Hexagon::L2_ploadruht_pi:
2854 case Hexagon::L2_ploadruhf_io:
2855 case Hexagon::L2_ploadruhf_pi:
2856 case Hexagon::L2_ploadruhtnew_io:
2857 case Hexagon::L2_ploadruhfnew_io:
2858 case Hexagon::L4_ploadruht_rr:
2859 case Hexagon::L4_ploadruhf_rr:
2860 case Hexagon::L4_ploadruhtnew_rr:
2861 case Hexagon::L4_ploadruhfnew_rr:
2862 case Hexagon::L2_ploadruhtnew_pi:
2863 case Hexagon::L2_ploadruhfnew_pi:
2864 case Hexagon::L4_ploadruht_abs:
2865 case Hexagon::L4_ploadruhf_abs:
2866 case Hexagon::L4_ploadruhtnew_abs:
2867 case Hexagon::L4_ploadruhfnew_abs:
2868 case Hexagon::L2_loadruhgp:
2869 return true;
2870 default:
2871 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002872 }
2873}
2874
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002875// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002876bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2877 const MachineInstr &MI2) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002878 if (isHVXVec(MI1) && isHVXVec(MI2))
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002879 if (!isVecUsableNextPacket(MI1, MI2))
2880 return true;
2881 return false;
2882}
2883
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002884/// Get the base register and byte offset of a load/store instr.
Brendon Cahoon254f8892016-07-29 16:44:44 +00002885bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2886 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2887 const {
2888 unsigned AccessSize = 0;
2889 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002890 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002891 Offset = OffsetVal;
2892 return BaseReg != 0;
2893}
2894
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002895/// Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002896bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2897 const MachineInstr &Second) const {
Krzysztof Parzyszek4763c2d2017-05-03 15:33:09 +00002898 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2899 const MachineOperand &Op = Second.getOperand(0);
2900 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2901 return true;
2902 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002903 if (DisableNVSchedule)
2904 return false;
2905 if (mayBeNewStore(Second)) {
2906 // Make sure the definition of the first instruction is the value being
2907 // stored.
2908 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002909 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002910 if (!Stored.isReg())
2911 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002912 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2913 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002914 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2915 return true;
2916 }
2917 }
2918 return false;
2919}
2920
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002921bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2922 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002923 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002924}
2925
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002926bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2927 for (auto &I : *B)
2928 if (I.isEHLabel())
2929 return true;
2930 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002931}
2932
Jyotsna Verma84256432013-03-01 17:37:13 +00002933// Returns true if an instruction can be converted into a non-extended
2934// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002935bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002936 short NonExtOpcode;
2937 // Check if the instruction has a register form that uses register in place
2938 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002939 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002940 return true;
2941
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002942 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002943 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002944
2945 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002946 case HexagonII::Absolute:
Jyotsna Verma84256432013-03-01 17:37:13 +00002947 // Load/store with absolute addressing mode can be converted into
2948 // base+offset mode.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002949 NonExtOpcode = Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002950 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002951 case HexagonII::BaseImmOffset:
Jyotsna Verma84256432013-03-01 17:37:13 +00002952 // Load/store with base+offset addressing mode can be converted into
2953 // base+register offset addressing mode. However left shift operand should
2954 // be set to 0.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002955 NonExtOpcode = Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002956 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002957 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002958 NonExtOpcode = Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002959 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002960 default:
2961 return false;
2962 }
2963 if (NonExtOpcode < 0)
2964 return false;
2965 return true;
2966 }
2967 return false;
2968}
2969
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002970bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2971 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002972 Hexagon::InstrType_Pseudo) >= 0;
2973}
2974
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002975bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2976 const {
2977 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2978 while (I != E) {
2979 if (I->isBarrier())
2980 return true;
2981 ++I;
2982 }
2983 return false;
2984}
2985
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002986// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002987bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002988 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002989 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00002990 Subtarget.hasV60Ops();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002991}
2992
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002993// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002994bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
Krzysztof Parzyszekf66f7612018-05-14 20:41:04 +00002995 if (MI.mayStore() && !Subtarget.useNewValueStores())
2996 return false;
2997
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002998 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002999 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3000}
3001
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003002bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3003 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003004 // There is no stall when ProdMI is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003005 if (!isHVXVec(ProdMI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003006 return false;
3007
3008 // There is no stall when ProdMI and ConsMI are not dependent.
3009 if (!isDependent(ProdMI, ConsMI))
3010 return false;
3011
3012 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3013 // are scheduled in consecutive packets.
3014 if (isVecUsableNextPacket(ProdMI, ConsMI))
3015 return false;
3016
3017 return true;
3018}
3019
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003020bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003021 MachineBasicBlock::const_instr_iterator BII) const {
3022 // There is no stall when I is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003023 if (!isHVXVec(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003024 return false;
3025
3026 MachineBasicBlock::const_instr_iterator MII = BII;
3027 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3028
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003029 if (!MII->isBundle())
3030 return producesStall(*MII, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003031
3032 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003033 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003034 if (producesStall(J, MI))
3035 return true;
3036 }
3037 return false;
3038}
3039
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003040bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003041 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00003042 for (const MachineOperand &MO : MI.operands()) {
3043 // Predicate register must be explicitly defined.
3044 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
3045 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003046 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00003047 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003048 }
3049
Krzysztof Parzyszekdd9415d2018-06-11 18:45:52 +00003050 // Instruction that produce late predicate cannot be used as sources of
3051 // dot-new.
3052 switch (MI.getOpcode()) {
3053 case Hexagon::A4_addp_c:
3054 case Hexagon::A4_subp_c:
3055 case Hexagon::A4_tlbmatch:
3056 case Hexagon::A5_ACS:
3057 case Hexagon::F2_sfinvsqrta:
3058 case Hexagon::F2_sfrecipa:
3059 case Hexagon::J2_endloop0:
3060 case Hexagon::J2_endloop01:
3061 case Hexagon::J2_ploop1si:
3062 case Hexagon::J2_ploop1sr:
3063 case Hexagon::J2_ploop2si:
3064 case Hexagon::J2_ploop2sr:
3065 case Hexagon::J2_ploop3si:
3066 case Hexagon::J2_ploop3sr:
3067 case Hexagon::S2_cabacdecbin:
3068 case Hexagon::S2_storew_locked:
3069 case Hexagon::S4_stored_locked:
3070 return false;
3071 }
3072 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003073}
3074
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003075bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003076 return Opcode == Hexagon::J2_jumpt ||
3077 Opcode == Hexagon::J2_jumptpt ||
3078 Opcode == Hexagon::J2_jumpf ||
3079 Opcode == Hexagon::J2_jumpfpt ||
3080 Opcode == Hexagon::J2_jumptnew ||
3081 Opcode == Hexagon::J2_jumpfnew ||
3082 Opcode == Hexagon::J2_jumptnewpt ||
3083 Opcode == Hexagon::J2_jumpfnewpt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003084}
3085
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003086bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3087 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3088 return false;
3089 return !isPredicatedTrue(Cond[0].getImm());
3090}
3091
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003092unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3093 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003094 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3095}
3096
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003097// Returns the base register in a memory access (load/store). The offset is
3098// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003099// If the base register has a subregister or the offset field does not contain
3100// an immediate value, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003101unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003102 int &Offset, unsigned &AccessSize) const {
3103 // Return if it is not a base+offset type instruction or a MemOp.
3104 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3105 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003106 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003107 return 0;
3108
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003109 AccessSize = getMemAccessSize(MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003110
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003111 unsigned BasePos = 0, OffsetPos = 0;
3112 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003113 return 0;
3114
3115 // Post increment updates its EA after the mem access,
3116 // so we need to treat its offset as zero.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003117 if (isPostIncrement(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003118 Offset = 0;
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003119 } else {
3120 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
3121 if (!OffsetOp.isImm())
3122 return 0;
3123 Offset = OffsetOp.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003124 }
3125
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003126 const MachineOperand &BaseOp = MI.getOperand(BasePos);
3127 if (BaseOp.getSubReg() != 0)
3128 return 0;
3129 return BaseOp.getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003130}
3131
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003132/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003133bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003134 unsigned &BasePos, unsigned &OffsetPos) const {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003135 if (!isAddrModeWithOffset(MI) && !isPostIncrement(MI))
3136 return false;
3137
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003138 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003139 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003140 BasePos = 0;
3141 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003142 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003143 BasePos = 0;
3144 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003145 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003146 BasePos = 1;
3147 OffsetPos = 2;
3148 } else
3149 return false;
3150
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003151 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003152 BasePos++;
3153 OffsetPos++;
3154 }
3155 if (isPostIncrement(MI)) {
3156 BasePos++;
3157 OffsetPos++;
3158 }
3159
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003160 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003161 return false;
3162
3163 return true;
3164}
3165
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003166// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003167// e.g. jump_t t1 (i1)
3168// jump t2 (i2)
3169// Jumpers = {i2, i1}
3170SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3171 MachineBasicBlock& MBB) const {
3172 SmallVector<MachineInstr*, 2> Jumpers;
3173 // If the block has no terminators, it just falls into the block after it.
3174 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3175 if (I == MBB.instr_begin())
3176 return Jumpers;
3177
3178 // A basic block may looks like this:
3179 //
3180 // [ insn
3181 // EH_LABEL
3182 // insn
3183 // insn
3184 // insn
3185 // EH_LABEL
3186 // insn ]
3187 //
3188 // It has two succs but does not have a terminator
3189 // Don't know how to handle it.
3190 do {
3191 --I;
3192 if (I->isEHLabel())
3193 return Jumpers;
3194 } while (I != MBB.instr_begin());
3195
3196 I = MBB.instr_end();
3197 --I;
3198
Shiva Chen801bf7e2018-05-09 02:42:00 +00003199 while (I->isDebugInstr()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003200 if (I == MBB.instr_begin())
3201 return Jumpers;
3202 --I;
3203 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003204 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003205 return Jumpers;
3206
3207 // Get the last instruction in the block.
3208 MachineInstr *LastInst = &*I;
3209 Jumpers.push_back(LastInst);
3210 MachineInstr *SecondLastInst = nullptr;
3211 // Find one more terminator if present.
3212 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003213 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003214 if (!SecondLastInst) {
3215 SecondLastInst = &*I;
3216 Jumpers.push_back(SecondLastInst);
3217 } else // This is a third branch.
3218 return Jumpers;
3219 }
3220 if (I == MBB.instr_begin())
3221 break;
3222 --I;
3223 } while (true);
3224 return Jumpers;
3225}
3226
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003227// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003228unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3229 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003230 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3231}
3232
3233// See if instruction could potentially be a duplex candidate.
3234// If so, return its group. Zero otherwise.
3235HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003236 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003237 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3238
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003239 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003240 default:
3241 return HexagonII::HCG_None;
3242 //
3243 // Compound pairs.
3244 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3245 // "Rd16=#U6 ; jump #r9:2"
3246 // "Rd16=Rs16 ; jump #r9:2"
3247 //
3248 case Hexagon::C2_cmpeq:
3249 case Hexagon::C2_cmpgt:
3250 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003251 DstReg = MI.getOperand(0).getReg();
3252 Src1Reg = MI.getOperand(1).getReg();
3253 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003254 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3255 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3256 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3257 return HexagonII::HCG_A;
3258 break;
3259 case Hexagon::C2_cmpeqi:
3260 case Hexagon::C2_cmpgti:
3261 case Hexagon::C2_cmpgtui:
3262 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003263 DstReg = MI.getOperand(0).getReg();
3264 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003265 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3266 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003267 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3268 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3269 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003270 return HexagonII::HCG_A;
3271 break;
3272 case Hexagon::A2_tfr:
3273 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003274 DstReg = MI.getOperand(0).getReg();
3275 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003276 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3277 return HexagonII::HCG_A;
3278 break;
3279 case Hexagon::A2_tfrsi:
3280 // Rd = #u6
3281 // Do not test for #u6 size since the const is getting extended
3282 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003283 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003284 if (isIntRegForSubInst(DstReg))
3285 return HexagonII::HCG_A;
3286 break;
3287 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003288 DstReg = MI.getOperand(0).getReg();
3289 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003290 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3291 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003292 MI.getOperand(2).isImm() &&
3293 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003294 return HexagonII::HCG_A;
3295 break;
3296 // The fact that .new form is used pretty much guarantees
3297 // that predicate register will match. Nevertheless,
3298 // there could be some false positives without additional
3299 // checking.
3300 case Hexagon::J2_jumptnew:
3301 case Hexagon::J2_jumpfnew:
3302 case Hexagon::J2_jumptnewpt:
3303 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003304 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003305 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3306 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3307 return HexagonII::HCG_B;
3308 break;
3309 // Transfer and jump:
3310 // Rd=#U6 ; jump #r9:2
3311 // Rd=Rs ; jump #r9:2
3312 // Do not test for jump range here.
3313 case Hexagon::J2_jump:
3314 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003315 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003316 return HexagonII::HCG_C;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003317 }
3318
3319 return HexagonII::HCG_None;
3320}
3321
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003322// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003323unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3324 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003325 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3326 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003327 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3328 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003329 return -1u;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003330 unsigned DestReg = GA.getOperand(0).getReg();
3331 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003332 return -1u;
3333 if (DestReg != Hexagon::P0 && DestReg != Hexagon::P1)
3334 return -1u;
3335 // The value compared against must be either u5 or -1.
3336 const MachineOperand &CmpOp = GA.getOperand(2);
3337 if (!CmpOp.isImm())
3338 return -1u;
3339 int V = CmpOp.getImm();
3340 if (V == -1)
3341 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqn1_tp0_jump_nt
3342 : Hexagon::J4_cmpeqn1_tp1_jump_nt;
3343 if (!isUInt<5>(V))
3344 return -1u;
3345 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqi_tp0_jump_nt
3346 : Hexagon::J4_cmpeqi_tp1_jump_nt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003347}
3348
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003349int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3350 enum Hexagon::PredSense inPredSense;
3351 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3352 Hexagon::PredSense_true;
3353 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3354 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3355 return CondOpcode;
3356
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003357 llvm_unreachable("Unexpected predicable instruction");
3358}
3359
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003360// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003361int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3362 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003363 default: llvm_unreachable("Unknown .cur type");
3364 case Hexagon::V6_vL32b_pi:
3365 return Hexagon::V6_vL32b_cur_pi;
3366 case Hexagon::V6_vL32b_ai:
3367 return Hexagon::V6_vL32b_cur_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003368 case Hexagon::V6_vL32b_nt_pi:
3369 return Hexagon::V6_vL32b_nt_cur_pi;
3370 case Hexagon::V6_vL32b_nt_ai:
3371 return Hexagon::V6_vL32b_nt_cur_ai;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003372 }
3373 return 0;
3374}
3375
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003376// Return the regular version of the .cur instruction.
3377int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3378 switch (MI.getOpcode()) {
3379 default: llvm_unreachable("Unknown .cur type");
3380 case Hexagon::V6_vL32b_cur_pi:
3381 return Hexagon::V6_vL32b_pi;
3382 case Hexagon::V6_vL32b_cur_ai:
3383 return Hexagon::V6_vL32b_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003384 case Hexagon::V6_vL32b_nt_cur_pi:
3385 return Hexagon::V6_vL32b_nt_pi;
3386 case Hexagon::V6_vL32b_nt_cur_ai:
3387 return Hexagon::V6_vL32b_nt_ai;
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003388 }
3389 return 0;
3390}
3391
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003392// The diagram below shows the steps involved in the conversion of a predicated
3393// store instruction to its .new predicated new-value form.
3394//
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003395// Note: It doesn't include conditional new-value stores as they can't be
3396// converted to .new predicate.
3397//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003398// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3399// ^ ^
3400// / \ (not OK. it will cause new-value store to be
3401// / X conditional on p0.new while R2 producer is
3402// / \ on p0)
3403// / \.
3404// p.new store p.old NV store
3405// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3406// ^ ^
3407// \ /
3408// \ /
3409// \ /
3410// p.old store
3411// [if (p0)memw(R0+#0)=R2]
3412//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003413// The following set of instructions further explains the scenario where
3414// conditional new-value store becomes invalid when promoted to .new predicate
3415// form.
3416//
3417// { 1) if (p0) r0 = add(r1, r2)
3418// 2) p0 = cmp.eq(r3, #0) }
3419//
3420// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3421// the first two instructions because in instr 1, r0 is conditional on old value
3422// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3423// is not valid for new-value stores.
3424// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3425// from the "Conditional Store" list. Because a predicated new value store
3426// would NOT be promoted to a double dot new store. See diagram below:
3427// This function returns yes for those stores that are predicated but not
3428// yet promoted to predicate dot new instructions.
3429//
3430// +---------------------+
3431// /-----| if (p0) memw(..)=r0 |---------\~
3432// || +---------------------+ ||
3433// promote || /\ /\ || promote
3434// || /||\ /||\ ||
3435// \||/ demote || \||/
3436// \/ || || \/
3437// +-------------------------+ || +-------------------------+
3438// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3439// +-------------------------+ || +-------------------------+
3440// || || ||
3441// || demote \||/
3442// promote || \/ NOT possible
3443// || || /\~
3444// \||/ || /||\~
3445// \/ || ||
3446// +-----------------------------+
3447// | if (p0.new) memw(..)=r0.new |
3448// +-----------------------------+
3449// Double Dot New Store
3450//
3451// Returns the most basic instruction for the .new predicated instructions and
3452// new-value stores.
3453// For example, all of the following instructions will be converted back to the
3454// same instruction:
3455// 1) if (p0.new) memw(R0+#0) = R1.new --->
3456// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3457// 3) if (p0.new) memw(R0+#0) = R1 --->
3458//
3459// To understand the translation of instruction 1 to its original form, consider
3460// a packet with 3 instructions.
3461// { p0 = cmp.eq(R0,R1)
3462// if (p0.new) R2 = add(R3, R4)
3463// R5 = add (R3, R1)
3464// }
3465// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3466//
3467// This instruction can be part of the previous packet only if both p0 and R2
3468// are promoted to .new values. This promotion happens in steps, first
3469// predicate register is promoted to .new and in the next iteration R2 is
3470// promoted. Therefore, in case of dependence check failure (due to R5) during
3471// next iteration, it should be converted back to its most basic form.
3472
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003473// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003474int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3475 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003476 if (NVOpcode >= 0) // Valid new-value store instruction.
3477 return NVOpcode;
3478
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003479 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003480 default:
Eugene Zelenko3b873362017-09-28 22:27:31 +00003481 report_fatal_error(std::string("Unknown .new type: ") +
3482 std::to_string(MI.getOpcode()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003483 case Hexagon::S4_storerb_ur:
3484 return Hexagon::S4_storerbnew_ur;
3485
3486 case Hexagon::S2_storerb_pci:
3487 return Hexagon::S2_storerb_pci;
3488
3489 case Hexagon::S2_storeri_pci:
3490 return Hexagon::S2_storeri_pci;
3491
3492 case Hexagon::S2_storerh_pci:
3493 return Hexagon::S2_storerh_pci;
3494
3495 case Hexagon::S2_storerd_pci:
3496 return Hexagon::S2_storerd_pci;
3497
3498 case Hexagon::S2_storerf_pci:
3499 return Hexagon::S2_storerf_pci;
3500
3501 case Hexagon::V6_vS32b_ai:
3502 return Hexagon::V6_vS32b_new_ai;
3503
3504 case Hexagon::V6_vS32b_pi:
3505 return Hexagon::V6_vS32b_new_pi;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003506 }
3507 return 0;
3508}
3509
3510// Returns the opcode to use when converting MI, which is a conditional jump,
3511// into a conditional instruction which uses the .new value of the predicate.
3512// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003513// If MBPI is null, all edges will be treated as equally likely for the
3514// purposes of establishing a predication hint.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003515int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003516 const MachineBranchProbabilityInfo *MBPI) const {
3517 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003518 const MachineBasicBlock *Src = MI.getParent();
3519 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003520 bool Taken = false;
3521 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003522
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003523 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3524 const MachineBasicBlock *Dst) {
3525 if (MBPI)
3526 return MBPI->getEdgeProbability(Src, Dst);
3527 return BranchProbability(1, Src->succ_size());
3528 };
3529
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003530 if (BrTarget.isMBB()) {
3531 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003532 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003533 } else {
3534 // The branch target is not a basic block (most likely a function).
3535 // Since BPI only gives probabilities for targets that are basic blocks,
3536 // try to identify another target of this branch (potentially a fall-
3537 // -through) and check the probability of that target.
3538 //
3539 // The only handled branch combinations are:
3540 // - one conditional branch,
3541 // - one conditional branch followed by one unconditional branch.
3542 // Otherwise, assume not-taken.
3543 assert(MI.isConditionalBranch());
3544 const MachineBasicBlock &B = *MI.getParent();
3545 bool SawCond = false, Bad = false;
3546 for (const MachineInstr &I : B) {
3547 if (!I.isBranch())
3548 continue;
3549 if (I.isConditionalBranch()) {
3550 SawCond = true;
3551 if (&I != &MI) {
3552 Bad = true;
3553 break;
3554 }
3555 }
3556 if (I.isUnconditionalBranch() && !SawCond) {
3557 Bad = true;
3558 break;
3559 }
3560 }
3561 if (!Bad) {
3562 MachineBasicBlock::const_instr_iterator It(MI);
3563 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3564 if (NextIt == B.instr_end()) {
3565 // If this branch is the last, look for the fall-through block.
3566 for (const MachineBasicBlock *SB : B.successors()) {
3567 if (!B.isLayoutSuccessor(SB))
3568 continue;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003569 Taken = getEdgeProbability(Src, SB) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003570 break;
3571 }
3572 } else {
3573 assert(NextIt->isUnconditionalBranch());
3574 // Find the first MBB operand and assume it's the target.
3575 const MachineBasicBlock *BT = nullptr;
3576 for (const MachineOperand &Op : NextIt->operands()) {
3577 if (!Op.isMBB())
3578 continue;
3579 BT = Op.getMBB();
3580 break;
3581 }
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003582 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003583 }
3584 } // if (!Bad)
3585 }
3586
3587 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003588
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003589 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003590 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003591 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003592 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003593 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003594
3595 default:
3596 llvm_unreachable("Unexpected jump instruction.");
3597 }
3598}
3599
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003600// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003601int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003602 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003603 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003604 // Condtional Jumps
3605 case Hexagon::J2_jumpt:
3606 case Hexagon::J2_jumpf:
3607 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003608 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003609
3610 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3611 if (NewOpcode >= 0)
3612 return NewOpcode;
Krzysztof Parzyszek066e8b52017-06-02 14:07:06 +00003613 return 0;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003614}
3615
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003616int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
3617 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003618 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3619 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003620 // All Hexagon architectures have prediction bits on dot-new branches,
3621 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3622 // to pick the right opcode when converting back to dot-old.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003623 if (!Subtarget.getFeatureBits()[Hexagon::ArchV60]) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003624 switch (NewOp) {
3625 case Hexagon::J2_jumptpt:
3626 NewOp = Hexagon::J2_jumpt;
3627 break;
3628 case Hexagon::J2_jumpfpt:
3629 NewOp = Hexagon::J2_jumpf;
3630 break;
3631 case Hexagon::J2_jumprtpt:
3632 NewOp = Hexagon::J2_jumprt;
3633 break;
3634 case Hexagon::J2_jumprfpt:
3635 NewOp = Hexagon::J2_jumprf;
3636 break;
3637 }
3638 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003639 assert(NewOp >= 0 &&
3640 "Couldn't change predicate new instruction to its old form.");
3641 }
3642
3643 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3644 NewOp = Hexagon::getNonNVStore(NewOp);
3645 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3646 }
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003647
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00003648 if (Subtarget.hasV60Ops())
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003649 return NewOp;
3650
3651 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3652 switch (NewOp) {
3653 case Hexagon::J2_jumpfpt:
3654 return Hexagon::J2_jumpf;
3655 case Hexagon::J2_jumptpt:
3656 return Hexagon::J2_jumpt;
3657 case Hexagon::J2_jumprfpt:
3658 return Hexagon::J2_jumprf;
3659 case Hexagon::J2_jumprtpt:
3660 return Hexagon::J2_jumprt;
3661 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003662 return NewOp;
3663}
3664
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003665// See if instruction could potentially be a duplex candidate.
3666// If so, return its group. Zero otherwise.
3667HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003668 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003669 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003670 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003671
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003672 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003673 default:
3674 return HexagonII::HSIG_None;
3675 //
3676 // Group L1:
3677 //
3678 // Rd = memw(Rs+#u4:2)
3679 // Rd = memub(Rs+#u4:0)
3680 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003681 DstReg = MI.getOperand(0).getReg();
3682 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003683 // Special case this one from Group L2.
3684 // Rd = memw(r29+#u5:2)
3685 if (isIntRegForSubInst(DstReg)) {
3686 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3687 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003688 MI.getOperand(2).isImm() &&
3689 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003690 return HexagonII::HSIG_L2;
3691 // Rd = memw(Rs+#u4:2)
3692 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003693 (MI.getOperand(2).isImm() &&
3694 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003695 return HexagonII::HSIG_L1;
3696 }
3697 break;
3698 case Hexagon::L2_loadrub_io:
3699 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003700 DstReg = MI.getOperand(0).getReg();
3701 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003702 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003703 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003704 return HexagonII::HSIG_L1;
3705 break;
3706 //
3707 // Group L2:
3708 //
3709 // Rd = memh/memuh(Rs+#u3:1)
3710 // Rd = memb(Rs+#u3:0)
3711 // Rd = memw(r29+#u5:2) - Handled above.
3712 // Rdd = memd(r29+#u5:3)
3713 // deallocframe
3714 // [if ([!]p0[.new])] dealloc_return
3715 // [if ([!]p0[.new])] jumpr r31
3716 case Hexagon::L2_loadrh_io:
3717 case Hexagon::L2_loadruh_io:
3718 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003719 DstReg = MI.getOperand(0).getReg();
3720 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003721 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003722 MI.getOperand(2).isImm() &&
3723 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003724 return HexagonII::HSIG_L2;
3725 break;
3726 case Hexagon::L2_loadrb_io:
3727 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003728 DstReg = MI.getOperand(0).getReg();
3729 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003730 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003731 MI.getOperand(2).isImm() &&
3732 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003733 return HexagonII::HSIG_L2;
3734 break;
3735 case Hexagon::L2_loadrd_io:
3736 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003737 DstReg = MI.getOperand(0).getReg();
3738 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003739 if (isDblRegForSubInst(DstReg, HRI) &&
3740 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3741 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003742 MI.getOperand(2).isImm() &&
3743 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003744 return HexagonII::HSIG_L2;
3745 break;
3746 // dealloc_return is not documented in Hexagon Manual, but marked
3747 // with A_SUBINSN attribute in iset_v4classic.py.
3748 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003749 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003750 case Hexagon::L4_return:
3751 case Hexagon::L2_deallocframe:
3752 return HexagonII::HSIG_L2;
3753 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003754 case Hexagon::PS_jmpret:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003755 case Hexagon::SL2_jumpr31:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003756 // jumpr r31
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003757 // Actual form JMPR implicit-def %pc, implicit %r31, implicit internal %r0
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003758 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003759 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3760 return HexagonII::HSIG_L2;
3761 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003762 case Hexagon::PS_jmprett:
3763 case Hexagon::PS_jmpretf:
3764 case Hexagon::PS_jmprettnewpt:
3765 case Hexagon::PS_jmpretfnewpt:
3766 case Hexagon::PS_jmprettnew:
3767 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003768 case Hexagon::SL2_jumpr31_t:
3769 case Hexagon::SL2_jumpr31_f:
3770 case Hexagon::SL2_jumpr31_tnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003771 DstReg = MI.getOperand(1).getReg();
3772 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003773 // [if ([!]p0[.new])] jumpr r31
3774 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3775 (Hexagon::P0 == SrcReg)) &&
3776 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3777 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003778 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00003779 case Hexagon::L4_return_t:
3780 case Hexagon::L4_return_f:
3781 case Hexagon::L4_return_tnew_pnt:
3782 case Hexagon::L4_return_fnew_pnt:
3783 case Hexagon::L4_return_tnew_pt:
3784 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003785 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003786 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003787 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3788 return HexagonII::HSIG_L2;
3789 break;
3790 //
3791 // Group S1:
3792 //
3793 // memw(Rs+#u4:2) = Rt
3794 // memb(Rs+#u4:0) = Rt
3795 case Hexagon::S2_storeri_io:
3796 // Special case this one from Group S2.
3797 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003798 Src1Reg = MI.getOperand(0).getReg();
3799 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003800 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3801 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003802 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3803 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003804 return HexagonII::HSIG_S2;
3805 // memw(Rs+#u4:2) = Rt
3806 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003807 MI.getOperand(1).isImm() &&
3808 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003809 return HexagonII::HSIG_S1;
3810 break;
3811 case Hexagon::S2_storerb_io:
3812 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003813 Src1Reg = MI.getOperand(0).getReg();
3814 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003815 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003816 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003817 return HexagonII::HSIG_S1;
3818 break;
3819 //
3820 // Group S2:
3821 //
3822 // memh(Rs+#u3:1) = Rt
3823 // memw(r29+#u5:2) = Rt
3824 // memd(r29+#s6:3) = Rtt
3825 // memw(Rs+#u4:2) = #U1
3826 // memb(Rs+#u4) = #U1
3827 // allocframe(#u5:3)
3828 case Hexagon::S2_storerh_io:
3829 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003830 Src1Reg = MI.getOperand(0).getReg();
3831 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003832 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003833 MI.getOperand(1).isImm() &&
3834 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003835 return HexagonII::HSIG_S1;
3836 break;
3837 case Hexagon::S2_storerd_io:
3838 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003839 Src1Reg = MI.getOperand(0).getReg();
3840 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003841 if (isDblRegForSubInst(Src2Reg, HRI) &&
3842 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003843 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3844 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003845 return HexagonII::HSIG_S2;
3846 break;
3847 case Hexagon::S4_storeiri_io:
3848 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003849 Src1Reg = MI.getOperand(0).getReg();
3850 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3851 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3852 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003853 return HexagonII::HSIG_S2;
3854 break;
3855 case Hexagon::S4_storeirb_io:
3856 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003857 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003858 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003859 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3860 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003861 return HexagonII::HSIG_S2;
3862 break;
3863 case Hexagon::S2_allocframe:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003864 if (MI.getOperand(2).isImm() &&
3865 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003866 return HexagonII::HSIG_S1;
3867 break;
3868 //
3869 // Group A:
3870 //
3871 // Rx = add(Rx,#s7)
3872 // Rd = Rs
3873 // Rd = #u6
3874 // Rd = #-1
3875 // if ([!]P0[.new]) Rd = #0
3876 // Rd = add(r29,#u6:2)
3877 // Rx = add(Rx,Rs)
3878 // P0 = cmp.eq(Rs,#u2)
3879 // Rdd = combine(#0,Rs)
3880 // Rdd = combine(Rs,#0)
3881 // Rdd = combine(#u2,#U2)
3882 // Rd = add(Rs,#1)
3883 // Rd = add(Rs,#-1)
3884 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3885 // Rd = and(Rs,#1)
3886 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003887 DstReg = MI.getOperand(0).getReg();
3888 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003889 if (isIntRegForSubInst(DstReg)) {
3890 // Rd = add(r29,#u6:2)
3891 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003892 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3893 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003894 return HexagonII::HSIG_A;
3895 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003896 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3897 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003898 return HexagonII::HSIG_A;
3899 // Rd = add(Rs,#1)
3900 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003901 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3902 ((MI.getOperand(2).getImm() == 1) ||
3903 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003904 return HexagonII::HSIG_A;
3905 }
3906 break;
3907 case Hexagon::A2_add:
3908 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003909 DstReg = MI.getOperand(0).getReg();
3910 Src1Reg = MI.getOperand(1).getReg();
3911 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003912 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3913 isIntRegForSubInst(Src2Reg))
3914 return HexagonII::HSIG_A;
3915 break;
3916 case Hexagon::A2_andir:
3917 // Same as zxtb.
3918 // Rd16=and(Rs16,#255)
3919 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003920 DstReg = MI.getOperand(0).getReg();
3921 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003922 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003923 MI.getOperand(2).isImm() &&
3924 ((MI.getOperand(2).getImm() == 1) ||
3925 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003926 return HexagonII::HSIG_A;
3927 break;
3928 case Hexagon::A2_tfr:
3929 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003930 DstReg = MI.getOperand(0).getReg();
3931 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003932 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3933 return HexagonII::HSIG_A;
3934 break;
3935 case Hexagon::A2_tfrsi:
3936 // Rd = #u6
3937 // Do not test for #u6 size since the const is getting extended
3938 // regardless and compound could be formed.
3939 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003940 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003941 if (isIntRegForSubInst(DstReg))
3942 return HexagonII::HSIG_A;
3943 break;
3944 case Hexagon::C2_cmoveit:
3945 case Hexagon::C2_cmovenewit:
3946 case Hexagon::C2_cmoveif:
3947 case Hexagon::C2_cmovenewif:
3948 // if ([!]P0[.new]) Rd = #0
3949 // Actual form:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003950 // %r16 = C2_cmovenewit internal %p0, 0, implicit undef %r16;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003951 DstReg = MI.getOperand(0).getReg();
3952 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003953 if (isIntRegForSubInst(DstReg) &&
3954 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003955 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003956 return HexagonII::HSIG_A;
3957 break;
3958 case Hexagon::C2_cmpeqi:
3959 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003960 DstReg = MI.getOperand(0).getReg();
3961 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003962 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3963 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003964 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003965 return HexagonII::HSIG_A;
3966 break;
3967 case Hexagon::A2_combineii:
3968 case Hexagon::A4_combineii:
3969 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003970 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003971 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003972 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3973 (MI.getOperand(1).isGlobal() &&
3974 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3975 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3976 (MI.getOperand(2).isGlobal() &&
3977 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003978 return HexagonII::HSIG_A;
3979 break;
3980 case Hexagon::A4_combineri:
3981 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003982 DstReg = MI.getOperand(0).getReg();
3983 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003984 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003985 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3986 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003987 return HexagonII::HSIG_A;
3988 break;
3989 case Hexagon::A4_combineir:
3990 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003991 DstReg = MI.getOperand(0).getReg();
3992 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003993 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003994 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3995 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003996 return HexagonII::HSIG_A;
3997 break;
3998 case Hexagon::A2_sxtb:
3999 case Hexagon::A2_sxth:
4000 case Hexagon::A2_zxtb:
4001 case Hexagon::A2_zxth:
4002 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004003 DstReg = MI.getOperand(0).getReg();
4004 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004005 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
4006 return HexagonII::HSIG_A;
4007 break;
4008 }
4009
4010 return HexagonII::HSIG_None;
4011}
4012
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004013short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
4014 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004015}
4016
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004017unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004018 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004019 // Default to one cycle for no itinerary. However, an "empty" itinerary may
4020 // still have a MinLatency property, which getStageLatency checks.
4021 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004022 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004023
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004024 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004025 return 0;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004026 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
4027}
4028
4029/// getOperandLatency - Compute and return the use operand latency of a given
4030/// pair of def and use.
4031/// In most cases, the static scheduling itinerary was enough to determine the
4032/// operand latency. But it may not be possible for instructions with variable
4033/// number of defs / uses.
4034///
4035/// This is a raw interface to the itinerary that may be directly overriden by
4036/// a target. Use computeOperandLatency to get the best estimate of latency.
4037int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4038 const MachineInstr &DefMI,
4039 unsigned DefIdx,
4040 const MachineInstr &UseMI,
4041 unsigned UseIdx) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004042 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004043
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004044 // Get DefIdx and UseIdx for super registers.
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00004045 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004046
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00004047 if (DefMO.isReg() && HRI.isPhysicalRegister(DefMO.getReg())) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004048 if (DefMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004049 for (MCSuperRegIterator SR(DefMO.getReg(), &HRI); SR.isValid(); ++SR) {
4050 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004051 if (Idx != -1) {
4052 DefIdx = Idx;
4053 break;
4054 }
4055 }
4056 }
4057
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00004058 const MachineOperand &UseMO = UseMI.getOperand(UseIdx);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004059 if (UseMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004060 for (MCSuperRegIterator SR(UseMO.getReg(), &HRI); SR.isValid(); ++SR) {
4061 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004062 if (Idx != -1) {
4063 UseIdx = Idx;
4064 break;
4065 }
4066 }
4067 }
4068 }
4069
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004070 int Latency = TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
4071 UseMI, UseIdx);
4072 if (!Latency)
4073 // We should never have 0 cycle latency between two instructions unless
4074 // they can be packetized together. However, this decision can't be made
4075 // here.
4076 Latency = 1;
4077 return Latency;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004078}
4079
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004080// inverts the predication logic.
4081// p -> NotP
4082// NotP -> P
4083bool HexagonInstrInfo::getInvertedPredSense(
4084 SmallVectorImpl<MachineOperand> &Cond) const {
4085 if (Cond.empty())
4086 return false;
4087 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4088 Cond[0].setImm(Opc);
4089 return true;
4090}
4091
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004092unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4093 int InvPredOpcode;
4094 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4095 : Hexagon::getTruePredOpcode(Opc);
4096 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4097 return InvPredOpcode;
4098
4099 llvm_unreachable("Unexpected predicated instruction");
4100}
4101
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004102// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004103int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4104 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004105 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4106 & HexagonII::ExtentSignedMask;
4107 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4108 & HexagonII::ExtentBitsMask;
4109
4110 if (isSigned) // if value is signed
4111 return ~(-1U << (bits - 1));
4112 else
4113 return ~(-1U << bits);
4114}
4115
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004116
4117bool HexagonInstrInfo::isAddrModeWithOffset(const MachineInstr &MI) const {
4118 switch (MI.getOpcode()) {
4119 case Hexagon::L2_loadrbgp:
4120 case Hexagon::L2_loadrdgp:
4121 case Hexagon::L2_loadrhgp:
4122 case Hexagon::L2_loadrigp:
4123 case Hexagon::L2_loadrubgp:
4124 case Hexagon::L2_loadruhgp:
4125 case Hexagon::S2_storerbgp:
4126 case Hexagon::S2_storerbnewgp:
4127 case Hexagon::S2_storerhgp:
4128 case Hexagon::S2_storerhnewgp:
4129 case Hexagon::S2_storerigp:
4130 case Hexagon::S2_storerinewgp:
4131 case Hexagon::S2_storerdgp:
4132 case Hexagon::S2_storerfgp:
4133 return true;
4134 }
4135 const uint64_t F = MI.getDesc().TSFlags;
4136 unsigned addrMode =
4137 ((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
4138 // Disallow any base+offset instruction. The assembler does not yet reorder
4139 // based up any zero offset instruction.
4140 return (addrMode == HexagonII::BaseRegOffset ||
4141 addrMode == HexagonII::BaseImmOffset ||
4142 addrMode == HexagonII::BaseLongOffset);
4143}
4144
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004145unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004146 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00004147
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004148 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004149 unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
4150 unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
4151 if (Size != 0)
4152 return Size;
4153
4154 // Handle vector access sizes.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004155 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004156 switch (S) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004157 case HexagonII::HVXVectorAccess:
4158 return HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004159 default:
4160 llvm_unreachable("Unexpected instruction");
4161 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004162}
4163
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004164// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004165int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4166 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004167 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4168 & HexagonII::ExtentSignedMask;
4169 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4170 & HexagonII::ExtentBitsMask;
4171
4172 if (isSigned) // if value is signed
4173 return -1U << (bits - 1);
4174 else
4175 return 0;
4176}
4177
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004178// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004179short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004180 // Check if the instruction has a register form that uses register in place
4181 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004182 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004183 if (NonExtOpcode >= 0)
4184 return NonExtOpcode;
4185
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004186 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004187 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004188 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00004189 case HexagonII::Absolute:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004190 return Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Eugene Zelenko3b873362017-09-28 22:27:31 +00004191 case HexagonII::BaseImmOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004192 return Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004193 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004194 return Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004195
Jyotsna Verma84256432013-03-01 17:37:13 +00004196 default:
4197 return -1;
4198 }
4199 }
4200 return -1;
4201}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004202
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004203bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004204 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004205 if (Cond.empty())
4206 return false;
4207 assert(Cond.size() == 2);
4208 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00004209 LLVM_DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004210 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004211 }
4212 PredReg = Cond[1].getReg();
4213 PredRegPos = 1;
4214 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4215 PredRegFlags = 0;
4216 if (Cond[1].isImplicit())
4217 PredRegFlags = RegState::Implicit;
4218 if (Cond[1].isUndef())
4219 PredRegFlags |= RegState::Undef;
4220 return true;
4221}
4222
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004223short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4224 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004225}
4226
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004227short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4228 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004229}
4230
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004231// Return the number of bytes required to encode the instruction.
4232// Hexagon instructions are fixed length, 4 bytes, unless they
4233// use a constant extender, which requires another 4 bytes.
4234// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004235unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
Shiva Chen801bf7e2018-05-09 02:42:00 +00004236 if (MI.isDebugInstr() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004237 return 0;
4238
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004239 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004240 if (!Size)
4241 // Assume the default insn size in case it cannot be determined
4242 // for whatever reason.
4243 Size = HEXAGON_INSTR_SIZE;
4244
4245 if (isConstExtended(MI) || isExtended(MI))
4246 Size += HEXAGON_INSTR_SIZE;
4247
4248 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004249 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4250 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004251 const MachineFunction *MF = MBB.getParent();
4252 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4253
4254 // Count the number of register definitions to find the asm string.
4255 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004256 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004257 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004258 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004259
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004260 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004261 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004262 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004263 Size = getInlineAsmLength(AsmStr, *MAI);
4264 }
4265
4266 return Size;
4267}
4268
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004269uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4270 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004271 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4272}
4273
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004274unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004275 const InstrItineraryData &II = *Subtarget.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004276 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004277
4278 return IS.getUnits();
4279}
4280
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004281// Calculate size of the basic block without debug instructions.
4282unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4283 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4284}
4285
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004286unsigned HexagonInstrInfo::nonDbgBundleSize(
4287 MachineBasicBlock::const_iterator BundleHead) const {
4288 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004289 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004290 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004291 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004292}
4293
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004294/// immediateExtend - Changes the instruction in place to one using an immediate
4295/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004296void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004297 assert((isExtendable(MI)||isConstExtended(MI)) &&
4298 "Instruction must be extendable");
4299 // Find which operand is extendable.
4300 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004301 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004302 // This needs to be something we understand.
4303 assert((MO.isMBB() || MO.isImm()) &&
4304 "Branch with unknown extendable field type");
4305 // Mark given operand as extended.
4306 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4307}
4308
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004309bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004310 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00004311 LLVM_DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to "
4312 << printMBBReference(*NewTarget);
4313 MI.dump(););
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004314 assert(MI.isBranch());
4315 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4316 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004317 // In general branch target is the last operand,
4318 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004319 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004320 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004321 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4322 MI.getOperand(TargetPos).setMBB(NewTarget);
4323 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004324 NewOpcode = reversePrediction(NewOpcode);
4325 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004326 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004327 return true;
4328}
4329
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004330void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4331 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4332 MachineFunction::iterator A = MF.begin();
4333 MachineBasicBlock &B = *A;
4334 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004335 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004336 MachineInstr *NewMI;
4337
4338 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4339 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004340 NewMI = BuildMI(B, I, DL, get(insn));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00004341 LLVM_DEBUG(dbgs() << "\n"
4342 << getName(NewMI->getOpcode())
4343 << " Class: " << NewMI->getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004344 NewMI->eraseFromParent();
4345 }
4346 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4347}
4348
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004349// inverts the predication logic.
4350// p -> NotP
4351// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004352bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00004353 LLVM_DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004354 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004355 return true;
4356}
4357
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004358// Reverse the branch prediction.
4359unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4360 int PredRevOpcode = -1;
4361 if (isPredictedTaken(Opcode))
4362 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4363 else
4364 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4365 assert(PredRevOpcode > 0);
4366 return PredRevOpcode;
4367}
4368
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004369// TODO: Add more rigorous validation.
4370bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4371 const {
4372 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4373}
4374
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004375void HexagonInstrInfo::
4376setBundleNoShuf(MachineBasicBlock::instr_iterator MIB) const {
4377 assert(MIB->isBundle());
4378 MachineOperand &Operand = MIB->getOperand(0);
4379 if (Operand.isImm())
4380 Operand.setImm(Operand.getImm() | memShufDisabledMask);
4381 else
4382 MIB->addOperand(MachineOperand::CreateImm(memShufDisabledMask));
4383}
4384
4385bool HexagonInstrInfo::getBundleNoShuf(const MachineInstr &MIB) const {
4386 assert(MIB.isBundle());
4387 const MachineOperand &Operand = MIB.getOperand(0);
4388 return (Operand.isImm() && (Operand.getImm() & memShufDisabledMask) != 0);
4389}
4390
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004391// Addressing mode relations.
4392short HexagonInstrInfo::changeAddrMode_abs_io(short Opc) const {
4393 return Opc >= 0 ? Hexagon::changeAddrMode_abs_io(Opc) : Opc;
4394}
4395
4396short HexagonInstrInfo::changeAddrMode_io_abs(short Opc) const {
4397 return Opc >= 0 ? Hexagon::changeAddrMode_io_abs(Opc) : Opc;
4398}
4399
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004400short HexagonInstrInfo::changeAddrMode_io_pi(short Opc) const {
4401 return Opc >= 0 ? Hexagon::changeAddrMode_io_pi(Opc) : Opc;
4402}
4403
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004404short HexagonInstrInfo::changeAddrMode_io_rr(short Opc) const {
4405 return Opc >= 0 ? Hexagon::changeAddrMode_io_rr(Opc) : Opc;
4406}
4407
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004408short HexagonInstrInfo::changeAddrMode_pi_io(short Opc) const {
4409 return Opc >= 0 ? Hexagon::changeAddrMode_pi_io(Opc) : Opc;
4410}
4411
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004412short HexagonInstrInfo::changeAddrMode_rr_io(short Opc) const {
4413 return Opc >= 0 ? Hexagon::changeAddrMode_rr_io(Opc) : Opc;
4414}
4415
4416short HexagonInstrInfo::changeAddrMode_rr_ur(short Opc) const {
4417 return Opc >= 0 ? Hexagon::changeAddrMode_rr_ur(Opc) : Opc;
4418}
4419
4420short HexagonInstrInfo::changeAddrMode_ur_rr(short Opc) const {
4421 return Opc >= 0 ? Hexagon::changeAddrMode_ur_rr(Opc) : Opc;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004422}