blob: 759e978ffbbfa7dcbb4b1271b93cd3e72970daef [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) {
137 if (!MIB->isDebugValue())
138 ++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
422 while (I->isDebugValue()) {
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())) {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000433 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 }
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000502 DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000503 << " 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 }
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000550 DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000551 << " 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
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000560 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;
565 if (I->isDebugValue())
566 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());
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000632 DEBUG(dbgs() << "\nInserting NVJump for " << printMBBReference(MBB););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000633 if (Cond[2].isReg()) {
634 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
635 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
636 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
637 } else if(Cond[2].isImm()) {
638 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
639 addImm(Cond[2].getImm()).addMBB(TBB);
640 } else
641 llvm_unreachable("Invalid condition for branching");
642 } else {
643 assert((Cond.size() == 2) && "Malformed cond vector");
644 const MachineOperand &RO = Cond[1];
645 unsigned Flags = getUndefRegState(RO.isUndef());
646 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
647 }
648 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000649 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000650 assert((!Cond.empty()) &&
651 "Cond. cannot be empty when multiple branchings are required");
652 assert((!isNewValueJump(Cond[0].getImm())) &&
653 "NV-jump cannot be inserted with another branch");
654 // Special case for hardware loops. The condition is a basic block.
655 if (isEndLoopN(Cond[0].getImm())) {
656 int EndLoopOp = Cond[0].getImm();
657 assert(Cond[1].isMBB());
658 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
659 // Check for it, and change the BB target if needed.
660 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000661 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
662 VisitedBBs);
Eugene Zelenko3b873362017-09-28 22:27:31 +0000663 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000664 Loop->getOperand(0).setMBB(TBB);
665 // Add the ENDLOOP after the finding the LOOP0.
666 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
667 } else {
668 const MachineOperand &RO = Cond[1];
669 unsigned Flags = getUndefRegState(RO.isUndef());
670 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000671 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000672 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000673
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000674 return 2;
675}
676
Brendon Cahoon254f8892016-07-29 16:44:44 +0000677/// Analyze the loop code to find the loop induction variable and compare used
678/// to compute the number of iterations. Currently, we analyze loop that are
679/// controlled using hardware loops. In this case, the induction variable
680/// instruction is null. For all other cases, this function returns true, which
681/// means we're unable to analyze it.
682bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
683 MachineInstr *&IndVarInst,
684 MachineInstr *&CmpInst) const {
685
686 MachineBasicBlock *LoopEnd = L.getBottomBlock();
687 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
688 // We really "analyze" only hardware loops right now.
689 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
690 IndVarInst = nullptr;
691 CmpInst = &*I;
692 return false;
693 }
694 return true;
695}
696
697/// Generate code to reduce the loop iteration by one and check if the loop is
698/// finished. Return the value/register of the new loop count. this function
699/// assumes the nth iteration is peeled first.
700unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000701 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000702 SmallVectorImpl<MachineOperand> &Cond,
703 SmallVectorImpl<MachineInstr *> &PrevInsts,
704 unsigned Iter, unsigned MaxIter) const {
705 // We expect a hardware loop currently. This means that IndVar is set
706 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000707 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000708 && "Expecting a hardware loop");
709 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000710 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000711 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000712 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(),
713 Cmp.getOperand(0).getMBB(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000714 if (!Loop)
715 return 0;
716 // If the loop trip count is a compile-time value, then just change the
717 // value.
718 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
719 Loop->getOpcode() == Hexagon::J2_loop1i) {
720 int64_t Offset = Loop->getOperand(1).getImm();
721 if (Offset <= 1)
722 Loop->eraseFromParent();
723 else
724 Loop->getOperand(1).setImm(Offset - 1);
725 return Offset - 1;
726 }
727 // The loop trip count is a run-time value. We generate code to subtract
728 // one from the trip count, and update the loop instruction.
729 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
730 unsigned LoopCount = Loop->getOperand(1).getReg();
731 // Check if we're done with the loop.
732 unsigned LoopEnd = createVR(MF, MVT::i1);
733 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
734 addReg(LoopCount).addImm(1);
735 unsigned NewLoopCount = createVR(MF, MVT::i32);
736 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
737 addReg(LoopCount).addImm(-1);
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000738 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000739 // Update the previously generated instructions with the new loop counter.
740 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
741 E = PrevInsts.end(); I != E; ++I)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000742 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, HRI);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000743 PrevInsts.clear();
744 PrevInsts.push_back(NewCmp);
745 PrevInsts.push_back(NewAdd);
746 // Insert the new loop instruction if this is the last time the loop is
747 // decremented.
748 if (Iter == MaxIter)
749 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
750 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
751 // Delete the old loop instruction.
752 if (Iter == 0)
753 Loop->eraseFromParent();
754 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
755 Cond.push_back(NewCmp->getOperand(0));
756 return NewLoopCount;
757}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000758
759bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
760 unsigned NumCycles, unsigned ExtraPredCycles,
761 BranchProbability Probability) const {
762 return nonDbgBBSize(&MBB) <= 3;
763}
764
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000765bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
766 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
767 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
768 const {
769 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
770}
771
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000772bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
773 unsigned NumInstrs, BranchProbability Probability) const {
774 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000775}
776
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000777void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000778 MachineBasicBlock::iterator I,
779 const DebugLoc &DL, unsigned DestReg,
780 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000781 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000782 unsigned KillFlag = getKillRegState(KillSrc);
783
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000784 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000785 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000786 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000787 return;
788 }
789 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000790 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
791 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792 return;
793 }
794 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
795 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000796 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
797 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000798 return;
799 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000800 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000801 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000802 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
803 .addReg(SrcReg, KillFlag);
804 return;
805 }
806 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
807 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
808 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
809 .addReg(SrcReg, KillFlag);
810 return;
811 }
812 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
813 Hexagon::IntRegsRegClass.contains(SrcReg)) {
814 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
815 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000816 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000817 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000818 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
819 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000820 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
821 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000822 return;
823 }
824 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
825 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000826 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
827 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000828 return;
829 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000830 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
831 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000832 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
833 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000834 return;
835 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000836 if (Hexagon::HvxVRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000837 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000838 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000839 return;
840 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000841 if (Hexagon::HvxWRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000842 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
843 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000844 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000845 .addReg(HiSrc, KillFlag)
846 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000847 return;
848 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000849 if (Hexagon::HvxQRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000850 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
851 .addReg(SrcReg)
852 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000853 return;
854 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000855 if (Hexagon::HvxQRRegClass.contains(SrcReg) &&
856 Hexagon::HvxVRRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000857 llvm_unreachable("Unimplemented pred to vec");
858 return;
859 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000860 if (Hexagon::HvxQRRegClass.contains(DestReg) &&
861 Hexagon::HvxVRRegClass.contains(SrcReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000862 llvm_unreachable("Unimplemented vec to pred");
863 return;
864 }
Sirish Pande30804c22012-02-15 18:52:27 +0000865
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000866#ifndef NDEBUG
867 // Show the invalid registers to ease debugging.
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000868 dbgs() << "Invalid registers for copy in " << printMBBReference(MBB) << ": "
869 << printReg(DestReg, &HRI) << " = " << printReg(SrcReg, &HRI) << '\n';
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000870#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000871 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000872}
873
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000874void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
875 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
876 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000877 DebugLoc DL = MBB.findDebugLoc(I);
878 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000879 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000880 unsigned SlotAlign = MFI.getObjectAlignment(FI);
881 unsigned RegAlign = TRI->getSpillAlignment(*RC);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000882 unsigned KillFlag = getKillRegState(isKill);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000883 bool HasAlloca = MFI.hasVarSizedObjects();
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000884 const HexagonFrameLowering &HFI = *Subtarget.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000885
Alex Lorenze40c8a22015-08-11 23:09:45 +0000886 MachineMemOperand *MMO = MF.getMachineMemOperand(
887 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000888 MFI.getObjectSize(FI), SlotAlign);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000889
Craig Topperc7242e02012-04-20 07:30:17 +0000890 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000891 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000892 .addFrameIndex(FI).addImm(0)
893 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000894 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000895 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000896 .addFrameIndex(FI).addImm(0)
897 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000898 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000899 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000900 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000901 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000902 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000903 BuildMI(MBB, I, DL, get(Hexagon::STriw_ctr))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000904 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000905 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000906 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000907 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000908 .addFrameIndex(FI).addImm(0)
909 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000910 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000911 // If there are variable-sized objects, spills will not be aligned.
912 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000913 SlotAlign = HFI.getStackAlignment();
914 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vS32Ub_ai
915 : Hexagon::V6_vS32b_ai;
916 MachineMemOperand *MMOA = MF.getMachineMemOperand(
917 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
918 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000919 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000920 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000921 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
922 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000923 // If there are variable-sized objects, spills will not be aligned.
924 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000925 SlotAlign = HFI.getStackAlignment();
926 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vstorerwu_ai
927 : Hexagon::PS_vstorerw_ai;
928 MachineMemOperand *MMOA = MF.getMachineMemOperand(
929 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
930 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000931 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000932 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000933 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000934 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000935 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000936 }
937}
938
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000939void HexagonInstrInfo::loadRegFromStackSlot(
940 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
941 int FI, const TargetRegisterClass *RC,
942 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000943 DebugLoc DL = MBB.findDebugLoc(I);
944 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000945 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000946 unsigned SlotAlign = MFI.getObjectAlignment(FI);
947 unsigned RegAlign = TRI->getSpillAlignment(*RC);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000948 bool HasAlloca = MFI.hasVarSizedObjects();
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000949 const HexagonFrameLowering &HFI = *Subtarget.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950
Alex Lorenze40c8a22015-08-11 23:09:45 +0000951 MachineMemOperand *MMO = MF.getMachineMemOperand(
952 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000953 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000954
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000955 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000956 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000957 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000958 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000959 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000960 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000961 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000962 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000963 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
964 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000965 BuildMI(MBB, I, DL, get(Hexagon::LDriw_ctr), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000966 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000967 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000968 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000969 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000970 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000971 // If there are variable-sized objects, spills will not be aligned.
972 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000973 SlotAlign = HFI.getStackAlignment();
974 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vL32Ub_ai
975 : Hexagon::V6_vL32b_ai;
976 MachineMemOperand *MMOA = MF.getMachineMemOperand(
977 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
978 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000979 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000980 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
981 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000982 // If there are variable-sized objects, spills will not be aligned.
983 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000984 SlotAlign = HFI.getStackAlignment();
985 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vloadrwu_ai
986 : Hexagon::PS_vloadrw_ai;
987 MachineMemOperand *MMOA = MF.getMachineMemOperand(
988 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
989 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000990 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000991 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000992 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000993 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000994 }
995}
996
Ron Lieberman88159e52016-09-02 22:56:24 +0000997static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
998 const MachineBasicBlock &B = *MI.getParent();
999 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +00001000 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +00001001 for (auto I = B.rbegin(); I != E; ++I)
1002 Regs.stepBackward(*I);
1003}
1004
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001005/// expandPostRAPseudo - This function is called for all pseudo instructions
1006/// that remain after register allocation. Many pseudo instructions are
1007/// created to help register allocation. This is the place to convert them
1008/// into real instructions. The target can edit MI in place, or it can insert
1009/// new instructions and erase MI. The function should return true if
1010/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001011bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001012 MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001013 MachineFunction &MF = *MBB.getParent();
1014 MachineRegisterInfo &MRI = MF.getRegInfo();
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001015 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001016 DebugLoc DL = MI.getDebugLoc();
1017 unsigned Opc = MI.getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001018
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001019 auto RealCirc = [&](unsigned Opc, bool HasImm = true, unsigned MxOp = 4) {
1020 unsigned Mx = MI.getOperand(MxOp).getReg();
1021 unsigned CSx = (Mx == Hexagon::M0 ? Hexagon::CS0 : Hexagon::CS1);
1022 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrrcr), CSx)
1023 .add(MI.getOperand((HasImm ? 5 : 4)));
1024 auto MIB = BuildMI(MBB, MI, DL, get(Opc)).add(MI.getOperand(0))
1025 .add(MI.getOperand(1)).add(MI.getOperand(2)).add(MI.getOperand(3));
1026 if (HasImm)
1027 MIB.add(MI.getOperand(4));
1028 MIB.addReg(CSx, RegState::Implicit);
1029 MBB.erase(MI);
1030 return true;
1031 };
1032
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001033 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001034 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001035 MachineOperand &MD = MI.getOperand(0);
1036 MachineOperand &MS = MI.getOperand(1);
1037 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001038 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1039 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001040 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001041 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001042 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001043 return true;
1044 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001045 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001046 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001047 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001048 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001049 MBB.erase(MI);
1050 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001051 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001052 unsigned SrcReg = MI.getOperand(1).getReg();
1053 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001054 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1055 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001056 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1057 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001058 MBB.erase(MI);
1059 return true;
1060 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001061 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001062 unsigned SrcReg = MI.getOperand(1).getReg();
1063 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001064 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001065 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001066 MBB.erase(MI);
1067 MRI.clearKillFlags(SrcSubLo);
1068 return true;
1069 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001070 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001071 unsigned SrcReg = MI.getOperand(1).getReg();
1072 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001073 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001074 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001075 MBB.erase(MI);
1076 MRI.clearKillFlags(SrcSubHi);
1077 return true;
1078 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001079 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001080 case Hexagon::PS_vstorerwu_ai: {
1081 bool Aligned = Opc == Hexagon::PS_vstorerw_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001082 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001083 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1084 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001085 unsigned NewOpc = Aligned ? Hexagon::V6_vS32b_ai : Hexagon::V6_vS32Ub_ai;
1086 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001087
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001088 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001089 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001090 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001091 .addImm(MI.getOperand(1).getImm())
1092 .addReg(SrcSubLo)
1093 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
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)
1100 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
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))
Krzysztof Parzyszek4be9d922017-05-03 15:26:13 +00001113 .add(MI.getOperand(1))
1114 .addImm(MI.getOperand(2).getImm())
1115 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
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)
1121 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
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 Parzyszek1d01a792016-08-16 18:08:40 +00001155 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001156 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001157 unsigned DstReg = MI.getOperand(0).getReg();
1158 unsigned Src1Reg = MI.getOperand(1).getReg();
1159 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001160 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1161 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1162 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1163 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001164 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001165 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001166 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001167 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001168 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001169 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001170 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001171 .addReg(Src2SubLo);
1172 MBB.erase(MI);
1173 MRI.clearKillFlags(Src1SubHi);
1174 MRI.clearKillFlags(Src1SubLo);
1175 MRI.clearKillFlags(Src2SubHi);
1176 MRI.clearKillFlags(Src2SubLo);
1177 return true;
1178 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001179 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001180 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001181 unsigned DstReg = MI.getOperand(0).getReg();
1182 unsigned Src1Reg = MI.getOperand(1).getReg();
1183 unsigned Src2Reg = MI.getOperand(2).getReg();
1184 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001185 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1186 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1187 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1188 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1189 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1190 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001191 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001192 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001193 .addReg(Src1SubHi)
1194 .addReg(Src2SubHi)
1195 .addReg(Src3SubHi);
1196 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001197 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001198 .addReg(Src1SubLo)
1199 .addReg(Src2SubLo)
1200 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001201 MBB.erase(MI);
1202 MRI.clearKillFlags(Src1SubHi);
1203 MRI.clearKillFlags(Src1SubLo);
1204 MRI.clearKillFlags(Src2SubHi);
1205 MRI.clearKillFlags(Src2SubLo);
1206 MRI.clearKillFlags(Src3SubHi);
1207 MRI.clearKillFlags(Src3SubLo);
1208 return true;
1209 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001210 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001211 const MachineOperand &Op0 = MI.getOperand(0);
1212 const MachineOperand &Op1 = MI.getOperand(1);
1213 const MachineOperand &Op2 = MI.getOperand(2);
1214 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001215 unsigned Rd = Op0.getReg();
1216 unsigned Pu = Op1.getReg();
1217 unsigned Rs = Op2.getReg();
1218 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001219 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001220 unsigned K1 = getKillRegState(Op1.isKill());
1221 unsigned K2 = getKillRegState(Op2.isKill());
1222 unsigned K3 = getKillRegState(Op3.isKill());
1223 if (Rd != Rs)
1224 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1225 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1226 .addReg(Rs, K2);
1227 if (Rd != Rt)
1228 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1229 .addReg(Pu, K1)
1230 .addReg(Rt, K3);
1231 MBB.erase(MI);
1232 return true;
1233 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001234 case Hexagon::PS_vselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001235 const MachineOperand &Op0 = MI.getOperand(0);
1236 const MachineOperand &Op1 = MI.getOperand(1);
1237 const MachineOperand &Op2 = MI.getOperand(2);
1238 const MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001239 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001240 getLiveRegsAt(LiveAtMI, MI);
1241 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001242 unsigned PReg = Op1.getReg();
1243 assert(Op1.getSubReg() == 0);
1244 unsigned PState = getRegState(Op1);
1245
Ron Lieberman88159e52016-09-02 22:56:24 +00001246 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001247 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1248 : PState;
Ron Lieberman88159e52016-09-02 22:56:24 +00001249 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001250 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001251 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001252 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001253 if (IsDestLive)
1254 T.addReg(Op0.getReg(), RegState::Implicit);
1255 IsDestLive = true;
1256 }
1257 if (Op0.getReg() != Op3.getReg()) {
1258 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001259 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001260 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001261 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001262 if (IsDestLive)
1263 T.addReg(Op0.getReg(), RegState::Implicit);
1264 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001265 MBB.erase(MI);
1266 return true;
1267 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001268 case Hexagon::PS_wselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001269 MachineOperand &Op0 = MI.getOperand(0);
1270 MachineOperand &Op1 = MI.getOperand(1);
1271 MachineOperand &Op2 = MI.getOperand(2);
1272 MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001273 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001274 getLiveRegsAt(LiveAtMI, MI);
1275 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001276 unsigned PReg = Op1.getReg();
1277 assert(Op1.getSubReg() == 0);
1278 unsigned PState = getRegState(Op1);
Ron Lieberman88159e52016-09-02 22:56:24 +00001279
1280 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001281 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1282 : PState;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001283 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1284 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001285 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001286 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001287 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001288 .add(Op1)
1289 .addReg(SrcHi)
1290 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001291 if (IsDestLive)
1292 T.addReg(Op0.getReg(), RegState::Implicit);
1293 IsDestLive = true;
1294 }
1295 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001296 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1297 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001298 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001299 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001300 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001301 .addReg(SrcHi)
1302 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001303 if (IsDestLive)
1304 T.addReg(Op0.getReg(), RegState::Implicit);
1305 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001306 MBB.erase(MI);
1307 return true;
1308 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001309
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001310 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001311 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001312 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001313 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001314 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001315 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001316 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001317 case Hexagon::PS_jmprett:
1318 MI.setDesc(get(Hexagon::J2_jumprt));
1319 return true;
1320 case Hexagon::PS_jmpretf:
1321 MI.setDesc(get(Hexagon::J2_jumprf));
1322 return true;
1323 case Hexagon::PS_jmprettnewpt:
1324 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1325 return true;
1326 case Hexagon::PS_jmpretfnewpt:
1327 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1328 return true;
1329 case Hexagon::PS_jmprettnew:
1330 MI.setDesc(get(Hexagon::J2_jumprtnew));
1331 return true;
1332 case Hexagon::PS_jmpretfnew:
1333 MI.setDesc(get(Hexagon::J2_jumprfnew));
1334 return true;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001335
1336 case Hexagon::V6_vgathermh_pseudo:
1337 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermh))
1338 .add(MI.getOperand(1))
1339 .add(MI.getOperand(2))
1340 .add(MI.getOperand(3));
1341 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1342 .add(MI.getOperand(0))
1343 .addImm(0)
1344 .addReg(Hexagon::VTMP);
1345 MBB.erase(MI);
1346 return true;
1347
1348 case Hexagon::V6_vgathermw_pseudo:
1349 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermw))
1350 .add(MI.getOperand(1))
1351 .add(MI.getOperand(2))
1352 .add(MI.getOperand(3));
1353 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1354 .add(MI.getOperand(0))
1355 .addImm(0)
1356 .addReg(Hexagon::VTMP);
1357 MBB.erase(MI);
1358 return true;
1359
1360 case Hexagon::V6_vgathermhw_pseudo:
1361 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhw))
1362 .add(MI.getOperand(1))
1363 .add(MI.getOperand(2))
1364 .add(MI.getOperand(3));
1365 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1366 .add(MI.getOperand(0))
1367 .addImm(0)
1368 .addReg(Hexagon::VTMP);
1369 MBB.erase(MI);
1370 return true;
1371
1372 case Hexagon::V6_vgathermhq_pseudo:
1373 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhq))
1374 .add(MI.getOperand(1))
1375 .add(MI.getOperand(2))
1376 .add(MI.getOperand(3))
1377 .add(MI.getOperand(4));
1378 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1379 .add(MI.getOperand(0))
1380 .addImm(0)
1381 .addReg(Hexagon::VTMP);
1382 MBB.erase(MI);
1383 return true;
1384
1385 case Hexagon::V6_vgathermwq_pseudo:
1386 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermwq))
1387 .add(MI.getOperand(1))
1388 .add(MI.getOperand(2))
1389 .add(MI.getOperand(3))
1390 .add(MI.getOperand(4));
1391 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1392 .add(MI.getOperand(0))
1393 .addImm(0)
1394 .addReg(Hexagon::VTMP);
1395 MBB.erase(MI);
1396 return true;
1397
1398 case Hexagon::V6_vgathermhwq_pseudo:
1399 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhwq))
1400 .add(MI.getOperand(1))
1401 .add(MI.getOperand(2))
1402 .add(MI.getOperand(3))
1403 .add(MI.getOperand(4));
1404 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1405 .add(MI.getOperand(0))
1406 .addImm(0)
1407 .addReg(Hexagon::VTMP);
1408 MBB.erase(MI);
1409 return true;
1410
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001411 case Hexagon::PS_loadrub_pci:
1412 return RealCirc(Hexagon::L2_loadrub_pci);
1413 case Hexagon::PS_loadrb_pci:
1414 return RealCirc(Hexagon::L2_loadrb_pci);
1415 case Hexagon::PS_loadruh_pci:
1416 return RealCirc(Hexagon::L2_loadruh_pci);
1417 case Hexagon::PS_loadrh_pci:
1418 return RealCirc(Hexagon::L2_loadrh_pci);
1419 case Hexagon::PS_loadri_pci:
1420 return RealCirc(Hexagon::L2_loadri_pci);
1421 case Hexagon::PS_loadrd_pci:
1422 return RealCirc(Hexagon::L2_loadrd_pci);
1423 case Hexagon::PS_loadrub_pcr:
1424 return RealCirc(Hexagon::L2_loadrub_pcr, /*HasImm=*/false, /*MxOp=*/3);
1425 case Hexagon::PS_loadrb_pcr:
1426 return RealCirc(Hexagon::L2_loadrb_pcr, /*HasImm=*/false, /*MxOp=*/3);
1427 case Hexagon::PS_loadruh_pcr:
1428 return RealCirc(Hexagon::L2_loadruh_pcr, /*HasImm=*/false, /*MxOp=*/3);
1429 case Hexagon::PS_loadrh_pcr:
1430 return RealCirc(Hexagon::L2_loadrh_pcr, /*HasImm=*/false, /*MxOp=*/3);
1431 case Hexagon::PS_loadri_pcr:
1432 return RealCirc(Hexagon::L2_loadri_pcr, /*HasImm=*/false, /*MxOp=*/3);
1433 case Hexagon::PS_loadrd_pcr:
1434 return RealCirc(Hexagon::L2_loadrd_pcr, /*HasImm=*/false, /*MxOp=*/3);
1435 case Hexagon::PS_storerb_pci:
1436 return RealCirc(Hexagon::S2_storerb_pci, /*HasImm=*/true, /*MxOp=*/3);
1437 case Hexagon::PS_storerh_pci:
1438 return RealCirc(Hexagon::S2_storerh_pci, /*HasImm=*/true, /*MxOp=*/3);
1439 case Hexagon::PS_storerf_pci:
1440 return RealCirc(Hexagon::S2_storerf_pci, /*HasImm=*/true, /*MxOp=*/3);
1441 case Hexagon::PS_storeri_pci:
1442 return RealCirc(Hexagon::S2_storeri_pci, /*HasImm=*/true, /*MxOp=*/3);
1443 case Hexagon::PS_storerd_pci:
1444 return RealCirc(Hexagon::S2_storerd_pci, /*HasImm=*/true, /*MxOp=*/3);
1445 case Hexagon::PS_storerb_pcr:
1446 return RealCirc(Hexagon::S2_storerb_pcr, /*HasImm=*/false, /*MxOp=*/2);
1447 case Hexagon::PS_storerh_pcr:
1448 return RealCirc(Hexagon::S2_storerh_pcr, /*HasImm=*/false, /*MxOp=*/2);
1449 case Hexagon::PS_storerf_pcr:
1450 return RealCirc(Hexagon::S2_storerf_pcr, /*HasImm=*/false, /*MxOp=*/2);
1451 case Hexagon::PS_storeri_pcr:
1452 return RealCirc(Hexagon::S2_storeri_pcr, /*HasImm=*/false, /*MxOp=*/2);
1453 case Hexagon::PS_storerd_pcr:
1454 return RealCirc(Hexagon::S2_storerd_pcr, /*HasImm=*/false, /*MxOp=*/2);
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001455 }
1456
1457 return false;
1458}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001459
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001460// We indicate that we want to reverse the branch by
1461// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001462bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001463 SmallVectorImpl<MachineOperand> &Cond) const {
1464 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001465 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001466 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1467 unsigned opcode = Cond[0].getImm();
1468 //unsigned temp;
1469 assert(get(opcode).isBranch() && "Should be a branching condition.");
1470 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001471 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001472 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1473 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001474 return false;
1475}
1476
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001477void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1478 MachineBasicBlock::iterator MI) const {
1479 DebugLoc DL;
1480 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1481}
1482
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001483bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1484 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001485}
1486
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001487// Returns true if an instruction is predicated irrespective of the predicate
1488// sense. For example, all of the following will return true.
1489// if (p0) R1 = add(R2, R3)
1490// if (!p0) R1 = add(R2, R3)
1491// if (p0.new) R1 = add(R2, R3)
1492// if (!p0.new) R1 = add(R2, R3)
1493// Note: New-value stores are not included here as in the current
1494// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001495bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1496 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001497 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001498}
1499
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001500bool HexagonInstrInfo::PredicateInstruction(
1501 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001502 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1503 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001504 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001505 return false;
1506 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001507 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001508 assert (isPredicable(MI) && "Expected predicable instruction");
1509 bool invertJump = predOpcodeHasNot(Cond);
1510
1511 // We have to predicate MI "in place", i.e. after this function returns,
1512 // MI will need to be transformed into a predicated form. To avoid com-
1513 // plicated manipulations with the operands (handling tied operands,
1514 // etc.), build a new temporary instruction, then overwrite MI with it.
1515
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001516 MachineBasicBlock &B = *MI.getParent();
1517 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001518 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1519 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001520 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001521 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001522 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001523 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1524 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001525 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001526 NOp++;
1527 }
1528
1529 unsigned PredReg, PredRegPos, PredRegFlags;
1530 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1531 (void)GotPredReg;
1532 assert(GotPredReg);
1533 T.addReg(PredReg, PredRegFlags);
1534 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001535 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001536
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001537 MI.setDesc(get(PredOpc));
1538 while (unsigned n = MI.getNumOperands())
1539 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001540 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001541 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001542
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001543 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001544 B.erase(TI);
1545
1546 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1547 MRI.clearKillFlags(PredReg);
1548 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001549}
1550
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001551bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1552 ArrayRef<MachineOperand> Pred2) const {
1553 // TODO: Fix this
1554 return false;
1555}
1556
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001557bool HexagonInstrInfo::DefinesPredicate(MachineInstr &MI,
1558 std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001559 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001560
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001561 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1562 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001563 if (MO.isReg()) {
1564 if (!MO.isDef())
1565 continue;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001566 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1567 if (RC == &Hexagon::PredRegsRegClass) {
1568 Pred.push_back(MO);
1569 return true;
1570 }
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001571 continue;
1572 } else if (MO.isRegMask()) {
1573 for (unsigned PR : Hexagon::PredRegsRegClass) {
1574 if (!MI.modifiesRegister(PR, &HRI))
1575 continue;
1576 Pred.push_back(MO);
1577 return true;
1578 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001579 }
1580 }
1581 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001582}
Andrew Trickd06df962012-02-01 22:13:57 +00001583
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001584bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001585 if (!MI.getDesc().isPredicable())
1586 return false;
1587
1588 if (MI.isCall() || isTailCall(MI)) {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001589 if (!Subtarget.usePredicatedCalls())
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001590 return false;
1591 }
Krzysztof Parzyszek8c53c952017-10-18 17:36:46 +00001592
1593 // HVX loads are not predicable on v60, but are on v62.
1594 if (!Subtarget.hasV62TOps()) {
1595 switch (MI.getOpcode()) {
1596 case Hexagon::V6_vL32b_ai:
1597 case Hexagon::V6_vL32b_pi:
1598 case Hexagon::V6_vL32b_ppu:
1599 case Hexagon::V6_vL32b_cur_ai:
1600 case Hexagon::V6_vL32b_cur_pi:
1601 case Hexagon::V6_vL32b_cur_ppu:
1602 case Hexagon::V6_vL32b_nt_ai:
1603 case Hexagon::V6_vL32b_nt_pi:
1604 case Hexagon::V6_vL32b_nt_ppu:
1605 case Hexagon::V6_vL32b_tmp_ai:
1606 case Hexagon::V6_vL32b_tmp_pi:
1607 case Hexagon::V6_vL32b_tmp_ppu:
1608 case Hexagon::V6_vL32b_nt_cur_ai:
1609 case Hexagon::V6_vL32b_nt_cur_pi:
1610 case Hexagon::V6_vL32b_nt_cur_ppu:
1611 case Hexagon::V6_vL32b_nt_tmp_ai:
1612 case Hexagon::V6_vL32b_nt_tmp_pi:
1613 case Hexagon::V6_vL32b_nt_tmp_ppu:
1614 return false;
1615 }
1616 }
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001617 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001618}
1619
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001620bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1621 const MachineBasicBlock *MBB,
1622 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001623 // Debug info is never a scheduling boundary. It's necessary to be explicit
1624 // due to the special treatment of IT instructions below, otherwise a
1625 // dbg_value followed by an IT will result in the IT instruction being
1626 // considered a scheduling hazard, which is wrong. It should be the actual
1627 // instruction preceding the dbg_value instruction(s), just like it is
1628 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001629 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001630 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001631
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001632 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001633 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001634 // Don't mess around with no return calls.
1635 if (doesNotReturn(MI))
1636 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001637 // If any of the block's successors is a landing pad, this could be a
1638 // throwing call.
1639 for (auto I : MBB->successors())
1640 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001641 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001642 }
1643
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001644 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001645 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001646 return true;
1647
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001648 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1649 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001650
1651 return false;
1652}
1653
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001654/// Measure the specified inline asm to determine an approximation of its
1655/// length.
1656/// Comments (which run till the next SeparatorString or newline) do not
1657/// count as an instruction.
1658/// Any other non-whitespace text is considered an instruction, with
1659/// multiple instructions separated by SeparatorString or newlines.
1660/// Variable-length instructions are not handled here; this function
1661/// may be overloaded in the target code to do that.
1662/// Hexagon counts the number of ##'s and adjust for that many
1663/// constant exenders.
1664unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1665 const MCAsmInfo &MAI) const {
1666 StringRef AStr(Str);
1667 // Count the number of instructions in the asm.
1668 bool atInsnStart = true;
1669 unsigned Length = 0;
1670 for (; *Str; ++Str) {
1671 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1672 strlen(MAI.getSeparatorString())) == 0)
1673 atInsnStart = true;
1674 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1675 Length += MAI.getMaxInstLength();
1676 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001677 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001678 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1679 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001680 atInsnStart = false;
1681 }
1682
1683 // Add to size number of constant extenders seen * 4.
1684 StringRef Occ("##");
1685 Length += AStr.count(Occ)*4;
1686 return Length;
1687}
1688
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001689ScheduleHazardRecognizer*
1690HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1691 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001692 if (UseDFAHazardRec)
1693 return new HexagonHazardRecognizer(II, this, Subtarget);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001694 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1695}
1696
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001697/// \brief For a comparison instruction, return the source registers in
1698/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1699/// compares against in CmpValue. Return true if the comparison instruction
1700/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001701bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1702 unsigned &SrcReg2, int &Mask,
1703 int &Value) const {
1704 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001705
1706 // Set mask and the first source register.
1707 switch (Opc) {
1708 case Hexagon::C2_cmpeq:
1709 case Hexagon::C2_cmpeqp:
1710 case Hexagon::C2_cmpgt:
1711 case Hexagon::C2_cmpgtp:
1712 case Hexagon::C2_cmpgtu:
1713 case Hexagon::C2_cmpgtup:
1714 case Hexagon::C4_cmpneq:
1715 case Hexagon::C4_cmplte:
1716 case Hexagon::C4_cmplteu:
1717 case Hexagon::C2_cmpeqi:
1718 case Hexagon::C2_cmpgti:
1719 case Hexagon::C2_cmpgtui:
1720 case Hexagon::C4_cmpneqi:
1721 case Hexagon::C4_cmplteui:
1722 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001723 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001724 Mask = ~0;
1725 break;
1726 case Hexagon::A4_cmpbeq:
1727 case Hexagon::A4_cmpbgt:
1728 case Hexagon::A4_cmpbgtu:
1729 case Hexagon::A4_cmpbeqi:
1730 case Hexagon::A4_cmpbgti:
1731 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001732 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001733 Mask = 0xFF;
1734 break;
1735 case Hexagon::A4_cmpheq:
1736 case Hexagon::A4_cmphgt:
1737 case Hexagon::A4_cmphgtu:
1738 case Hexagon::A4_cmpheqi:
1739 case Hexagon::A4_cmphgti:
1740 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001741 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001742 Mask = 0xFFFF;
1743 break;
1744 }
1745
1746 // Set the value/second source register.
1747 switch (Opc) {
1748 case Hexagon::C2_cmpeq:
1749 case Hexagon::C2_cmpeqp:
1750 case Hexagon::C2_cmpgt:
1751 case Hexagon::C2_cmpgtp:
1752 case Hexagon::C2_cmpgtu:
1753 case Hexagon::C2_cmpgtup:
1754 case Hexagon::A4_cmpbeq:
1755 case Hexagon::A4_cmpbgt:
1756 case Hexagon::A4_cmpbgtu:
1757 case Hexagon::A4_cmpheq:
1758 case Hexagon::A4_cmphgt:
1759 case Hexagon::A4_cmphgtu:
1760 case Hexagon::C4_cmpneq:
1761 case Hexagon::C4_cmplte:
1762 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001763 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001764 return true;
1765
1766 case Hexagon::C2_cmpeqi:
1767 case Hexagon::C2_cmpgtui:
1768 case Hexagon::C2_cmpgti:
1769 case Hexagon::C4_cmpneqi:
1770 case Hexagon::C4_cmplteui:
1771 case Hexagon::C4_cmpltei:
1772 case Hexagon::A4_cmpbeqi:
1773 case Hexagon::A4_cmpbgti:
1774 case Hexagon::A4_cmpbgtui:
1775 case Hexagon::A4_cmpheqi:
1776 case Hexagon::A4_cmphgti:
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001777 case Hexagon::A4_cmphgtui: {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001778 SrcReg2 = 0;
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001779 const MachineOperand &Op2 = MI.getOperand(2);
1780 if (!Op2.isImm())
1781 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001782 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001783 return true;
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001784 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001785 }
1786
1787 return false;
1788}
1789
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001790unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001791 const MachineInstr &MI,
1792 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001793 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001794}
1795
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001796DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1797 const TargetSubtargetInfo &STI) const {
1798 const InstrItineraryData *II = STI.getInstrItineraryData();
1799 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1800}
1801
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001802// Inspired by this pair:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001803// %r13 = L2_loadri_io %r29, 136; mem:LD4[FixedStack0]
1804// S2_storeri_io %r29, 132, killed %r1; flags: mem:ST4[FixedStack1]
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001805// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001806bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1807 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001808 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1809 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001810 return false;
1811
1812 // Instructions that are pure loads, not loads and stores like memops are not
1813 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001814 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001815 return true;
1816
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001817 // Get the base register in MIa.
1818 unsigned BasePosA, OffsetPosA;
1819 if (!getBaseAndOffsetPosition(MIa, BasePosA, OffsetPosA))
1820 return false;
1821 const MachineOperand &BaseA = MIa.getOperand(BasePosA);
1822 unsigned BaseRegA = BaseA.getReg();
1823 unsigned BaseSubA = BaseA.getSubReg();
1824
1825 // Get the base register in MIb.
1826 unsigned BasePosB, OffsetPosB;
1827 if (!getBaseAndOffsetPosition(MIb, BasePosB, OffsetPosB))
1828 return false;
1829 const MachineOperand &BaseB = MIb.getOperand(BasePosB);
1830 unsigned BaseRegB = BaseB.getReg();
1831 unsigned BaseSubB = BaseB.getSubReg();
1832
1833 if (BaseRegA != BaseRegB || BaseSubA != BaseSubB)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001834 return false;
1835
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001836 // Get the access sizes.
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00001837 unsigned SizeA = getMemAccessSize(MIa);
1838 unsigned SizeB = getMemAccessSize(MIb);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001839
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001840 // Get the offsets. Handle immediates only for now.
1841 const MachineOperand &OffA = MIa.getOperand(OffsetPosA);
1842 const MachineOperand &OffB = MIb.getOperand(OffsetPosB);
1843 if (!MIa.getOperand(OffsetPosA).isImm() ||
1844 !MIb.getOperand(OffsetPosB).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001845 return false;
Krzysztof Parzyszekac019942017-07-19 19:17:32 +00001846 int OffsetA = isPostIncrement(MIa) ? 0 : OffA.getImm();
1847 int OffsetB = isPostIncrement(MIb) ? 0 : OffB.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001848
1849 // This is a mem access with the same base register and known offsets from it.
1850 // Reason about it.
1851 if (OffsetA > OffsetB) {
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001852 uint64_t OffDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1853 return SizeB <= OffDiff;
1854 }
1855 if (OffsetA < OffsetB) {
1856 uint64_t OffDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1857 return SizeA <= OffDiff;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001858 }
1859
1860 return false;
1861}
1862
Brendon Cahoon254f8892016-07-29 16:44:44 +00001863/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001864bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001865 int &Value) const {
1866 if (isPostIncrement(MI)) {
Krzysztof Parzyszek12bdcab2017-10-11 15:59:51 +00001867 unsigned BasePos = 0, OffsetPos = 0;
1868 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
1869 return false;
1870 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
1871 if (OffsetOp.isImm()) {
1872 Value = OffsetOp.getImm();
1873 return true;
1874 }
Krzysztof Parzyszekbf626192017-10-11 16:15:31 +00001875 } else if (MI.getOpcode() == Hexagon::A2_addi) {
1876 const MachineOperand &AddOp = MI.getOperand(2);
1877 if (AddOp.isImm()) {
1878 Value = AddOp.getImm();
1879 return true;
1880 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00001881 }
1882
1883 return false;
1884}
1885
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001886std::pair<unsigned, unsigned>
1887HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1888 return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
1889 TF & HexagonII::MO_Bitmasks);
1890}
1891
1892ArrayRef<std::pair<unsigned, const char*>>
1893HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1894 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00001895
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001896 static const std::pair<unsigned, const char*> Flags[] = {
1897 {MO_PCREL, "hexagon-pcrel"},
1898 {MO_GOT, "hexagon-got"},
1899 {MO_LO16, "hexagon-lo16"},
1900 {MO_HI16, "hexagon-hi16"},
1901 {MO_GPREL, "hexagon-gprel"},
1902 {MO_GDGOT, "hexagon-gdgot"},
1903 {MO_GDPLT, "hexagon-gdplt"},
1904 {MO_IE, "hexagon-ie"},
1905 {MO_IEGOT, "hexagon-iegot"},
1906 {MO_TPREL, "hexagon-tprel"}
1907 };
1908 return makeArrayRef(Flags);
1909}
1910
1911ArrayRef<std::pair<unsigned, const char*>>
1912HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1913 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00001914
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001915 static const std::pair<unsigned, const char*> Flags[] = {
1916 {HMOTF_ConstExtended, "hexagon-ext"}
1917 };
1918 return makeArrayRef(Flags);
1919}
1920
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001921unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001922 MachineRegisterInfo &MRI = MF->getRegInfo();
1923 const TargetRegisterClass *TRC;
1924 if (VT == MVT::i1) {
1925 TRC = &Hexagon::PredRegsRegClass;
1926 } else if (VT == MVT::i32 || VT == MVT::f32) {
1927 TRC = &Hexagon::IntRegsRegClass;
1928 } else if (VT == MVT::i64 || VT == MVT::f64) {
1929 TRC = &Hexagon::DoubleRegsRegClass;
1930 } else {
1931 llvm_unreachable("Cannot handle this register class");
1932 }
1933
1934 unsigned NewReg = MRI.createVirtualRegister(TRC);
1935 return NewReg;
1936}
1937
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001938bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001939 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1940}
1941
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001942bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1943 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001944 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1945}
1946
Krzysztof Parzyszek998df2c2018-03-23 20:43:02 +00001947bool HexagonInstrInfo::isBaseImmOffset(const MachineInstr &MI) const {
1948 return getAddrMode(MI) == HexagonII::BaseImmOffset;
1949}
1950
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001951bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001952 return !isTC1(MI) && !isTC2Early(MI) && !MI.getDesc().mayLoad() &&
1953 !MI.getDesc().mayStore() &&
1954 MI.getDesc().getOpcode() != Hexagon::S2_allocframe &&
1955 MI.getDesc().getOpcode() != Hexagon::L2_deallocframe &&
1956 !isMemOp(MI) && !MI.isBranch() && !MI.isReturn() && !MI.isCall();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001957}
1958
Sanjay Patele4b9f502015-12-07 19:21:39 +00001959// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001960bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001961 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001962}
1963
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001964// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1965// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001966bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1967 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001968 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1969 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001970 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001971
1972 unsigned isExtendable =
1973 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1974 if (!isExtendable)
1975 return false;
1976
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001977 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001978 return false;
1979
1980 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001981 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001982 // Use MO operand flags to determine if MO
1983 // has the HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00001984 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001985 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001986 // If this is a Machine BB address we are talking about, and it is
1987 // not marked as extended, say so.
1988 if (MO.isMBB())
1989 return false;
1990
1991 // We could be using an instruction with an extendable immediate and shoehorn
1992 // a global address into it. If it is a global address it will be constant
1993 // extended. We do this for COMBINE.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001994 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001995 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001996 return true;
1997
1998 // If the extendable operand is not 'Immediate' type, the instruction should
1999 // have 'isExtended' flag set.
2000 assert(MO.isImm() && "Extendable operand must be Immediate type");
2001
2002 int MinValue = getMinValue(MI);
2003 int MaxValue = getMaxValue(MI);
2004 int ImmValue = MO.getImm();
2005
2006 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002007}
2008
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002009bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
2010 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002011 case Hexagon::L4_return:
2012 case Hexagon::L4_return_t:
2013 case Hexagon::L4_return_f:
2014 case Hexagon::L4_return_tnew_pnt:
2015 case Hexagon::L4_return_fnew_pnt:
2016 case Hexagon::L4_return_tnew_pt:
2017 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002018 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002019 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002020 return false;
2021}
2022
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002023// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002024bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
2025 const MachineInstr &ConsMI) const {
2026 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002027 return false;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00002028 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002029
2030 SmallVector<unsigned, 4> DefsA;
2031 SmallVector<unsigned, 4> DefsB;
2032 SmallVector<unsigned, 8> UsesA;
2033 SmallVector<unsigned, 8> UsesB;
2034
2035 parseOperands(ProdMI, DefsA, UsesA);
2036 parseOperands(ConsMI, DefsB, UsesB);
2037
2038 for (auto &RegA : DefsA)
2039 for (auto &RegB : UsesB) {
2040 // True data dependency.
2041 if (RegA == RegB)
2042 return true;
2043
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002044 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002045 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
2046 if (RegB == *SubRegs)
2047 return true;
2048
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002049 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002050 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
2051 if (RegA == *SubRegs)
2052 return true;
2053 }
2054
2055 return false;
2056}
2057
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002058// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002059bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2060 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002061 case Hexagon::V6_vL32b_cur_pi:
2062 case Hexagon::V6_vL32b_cur_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002063 return true;
2064 }
2065 return false;
2066}
2067
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002068// Returns true, if any one of the operands is a dot new
2069// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002070bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2071 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002072 return true;
2073
2074 return false;
2075}
2076
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002077/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002078bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2079 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002080 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2081 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2082 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2083}
2084
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002085bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2086 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002087 return true;
2088
2089 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002090 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002091 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002092}
2093
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002094bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2095 return (Opcode == Hexagon::ENDLOOP0 ||
2096 Opcode == Hexagon::ENDLOOP1);
2097}
2098
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002099bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2100 switch(OpType) {
2101 case MachineOperand::MO_MachineBasicBlock:
2102 case MachineOperand::MO_GlobalAddress:
2103 case MachineOperand::MO_ExternalSymbol:
2104 case MachineOperand::MO_JumpTableIndex:
2105 case MachineOperand::MO_ConstantPoolIndex:
2106 case MachineOperand::MO_BlockAddress:
2107 return true;
2108 default:
2109 return false;
2110 }
2111}
2112
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002113bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2114 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002115 const uint64_t F = MID.TSFlags;
2116 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2117 return true;
2118
2119 // TODO: This is largely obsolete now. Will need to be removed
2120 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002121 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002122 // PS_fi and PS_fia remain special cases.
2123 case Hexagon::PS_fi:
2124 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002125 return true;
2126 default:
2127 return false;
2128 }
2129 return false;
2130}
2131
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002132// This returns true in two cases:
2133// - The OP code itself indicates that this is an extended instruction.
2134// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002135bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002136 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002137 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002138 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2139 return true;
2140 // Use MO operand flags to determine if one of MI's operands
2141 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00002142 for (const MachineOperand &MO : MI.operands())
2143 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002144 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002145 return false;
2146}
2147
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002148bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2149 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002150 const uint64_t F = get(Opcode).TSFlags;
2151 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2152}
2153
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002154// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002155bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2156 const MachineInstr &J) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002157 if (!isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002158 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002159 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002160 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002161 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002162}
2163
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002164bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2165 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002166 case Hexagon::J2_callr:
2167 case Hexagon::J2_callrf:
2168 case Hexagon::J2_callrt:
2169 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002170 return true;
2171 }
2172 return false;
2173}
2174
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002175bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2176 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002177 case Hexagon::L4_return:
2178 case Hexagon::L4_return_t:
2179 case Hexagon::L4_return_f:
2180 case Hexagon::L4_return_fnew_pnt:
2181 case Hexagon::L4_return_fnew_pt:
2182 case Hexagon::L4_return_tnew_pnt:
2183 case Hexagon::L4_return_tnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002184 return true;
2185 }
2186 return false;
2187}
2188
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002189bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2190 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002191 case Hexagon::J2_jumpr:
2192 case Hexagon::J2_jumprt:
2193 case Hexagon::J2_jumprf:
2194 case Hexagon::J2_jumprtnewpt:
2195 case Hexagon::J2_jumprfnewpt:
2196 case Hexagon::J2_jumprtnew:
2197 case Hexagon::J2_jumprfnew:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002198 return true;
2199 }
2200 return false;
2201}
2202
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002203// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002204// Use abs estimate as oppose to the exact number.
2205// TODO: This will need to be changed to use MC level
2206// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002207bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002208 unsigned offset) const {
2209 // This selection of jump instructions matches to that what
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002210 // analyzeBranch can parse, plus NVJ.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002211 if (isNewValueJump(MI)) // r9:2
2212 return isInt<11>(offset);
2213
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002214 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002215 // Still missing Jump to address condition on register value.
2216 default:
2217 return false;
2218 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2219 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002220 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002221 return isInt<24>(offset);
2222 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2223 case Hexagon::J2_jumpf:
2224 case Hexagon::J2_jumptnew:
2225 case Hexagon::J2_jumptnewpt:
2226 case Hexagon::J2_jumpfnew:
2227 case Hexagon::J2_jumpfnewpt:
2228 case Hexagon::J2_callt:
2229 case Hexagon::J2_callf:
2230 return isInt<17>(offset);
2231 case Hexagon::J2_loop0i:
2232 case Hexagon::J2_loop0iext:
2233 case Hexagon::J2_loop0r:
2234 case Hexagon::J2_loop0rext:
2235 case Hexagon::J2_loop1i:
2236 case Hexagon::J2_loop1iext:
2237 case Hexagon::J2_loop1r:
2238 case Hexagon::J2_loop1rext:
2239 return isInt<9>(offset);
2240 // TODO: Add all the compound branches here. Can we do this in Relation model?
2241 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2242 case Hexagon::J4_cmpeqi_tp1_jump_nt:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002243 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
2244 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002245 return isInt<11>(offset);
2246 }
2247}
2248
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002249bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2250 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002251 bool isLate = isLateResultInstr(LRMI);
2252 bool isEarly = isEarlySourceInstr(ESMI);
2253
2254 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002255 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002256 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002257 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002258
2259 if (isLate && isEarly) {
2260 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2261 return true;
2262 }
2263
2264 return false;
2265}
2266
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002267bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2268 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002269 case TargetOpcode::EXTRACT_SUBREG:
2270 case TargetOpcode::INSERT_SUBREG:
2271 case TargetOpcode::SUBREG_TO_REG:
2272 case TargetOpcode::REG_SEQUENCE:
2273 case TargetOpcode::IMPLICIT_DEF:
2274 case TargetOpcode::COPY:
2275 case TargetOpcode::INLINEASM:
2276 case TargetOpcode::PHI:
2277 return false;
2278 default:
2279 break;
2280 }
2281
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002282 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002283 return !is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002284}
2285
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002286bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002287 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2288 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002289 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002290}
2291
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002292bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2293 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002294 return Opcode == Hexagon::J2_loop0i ||
2295 Opcode == Hexagon::J2_loop0r ||
2296 Opcode == Hexagon::J2_loop0iext ||
2297 Opcode == Hexagon::J2_loop0rext ||
2298 Opcode == Hexagon::J2_loop1i ||
2299 Opcode == Hexagon::J2_loop1r ||
2300 Opcode == Hexagon::J2_loop1iext ||
2301 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002302}
2303
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002304bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2305 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002306 default: return false;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002307 case Hexagon::L4_iadd_memopw_io:
2308 case Hexagon::L4_isub_memopw_io:
2309 case Hexagon::L4_add_memopw_io:
2310 case Hexagon::L4_sub_memopw_io:
2311 case Hexagon::L4_and_memopw_io:
2312 case Hexagon::L4_or_memopw_io:
2313 case Hexagon::L4_iadd_memoph_io:
2314 case Hexagon::L4_isub_memoph_io:
2315 case Hexagon::L4_add_memoph_io:
2316 case Hexagon::L4_sub_memoph_io:
2317 case Hexagon::L4_and_memoph_io:
2318 case Hexagon::L4_or_memoph_io:
2319 case Hexagon::L4_iadd_memopb_io:
2320 case Hexagon::L4_isub_memopb_io:
2321 case Hexagon::L4_add_memopb_io:
2322 case Hexagon::L4_sub_memopb_io:
2323 case Hexagon::L4_and_memopb_io:
2324 case Hexagon::L4_or_memopb_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002325 case Hexagon::L4_ior_memopb_io:
2326 case Hexagon::L4_ior_memoph_io:
2327 case Hexagon::L4_ior_memopw_io:
2328 case Hexagon::L4_iand_memopb_io:
2329 case Hexagon::L4_iand_memoph_io:
2330 case Hexagon::L4_iand_memopw_io:
2331 return true;
2332 }
2333 return false;
2334}
2335
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002336bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2337 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002338 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2339}
2340
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002341bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2342 const uint64_t F = get(Opcode).TSFlags;
2343 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2344}
2345
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002346bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002347 return isNewValueJump(MI) || isNewValueStore(MI);
2348}
2349
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002350bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2351 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002352}
2353
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002354bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2355 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2356}
2357
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002358bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2359 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002360 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2361}
2362
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002363bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2364 const uint64_t F = get(Opcode).TSFlags;
2365 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2366}
2367
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002368// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002369bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002370 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002371 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002372 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2373 == OperandNum;
2374}
2375
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002376bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2377 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002378 assert(isPredicated(MI));
2379 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2380}
2381
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002382bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2383 const uint64_t F = get(Opcode).TSFlags;
2384 assert(isPredicated(Opcode));
2385 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2386}
2387
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002388bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2389 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002390 return !((F >> HexagonII::PredicatedFalsePos) &
2391 HexagonII::PredicatedFalseMask);
2392}
2393
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002394bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2395 const uint64_t F = get(Opcode).TSFlags;
2396 // Make sure that the instruction is predicated.
2397 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2398 return !((F >> HexagonII::PredicatedFalsePos) &
2399 HexagonII::PredicatedFalseMask);
2400}
2401
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002402bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2403 const uint64_t F = get(Opcode).TSFlags;
2404 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2405}
2406
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002407bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2408 const uint64_t F = get(Opcode).TSFlags;
2409 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2410}
2411
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002412bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2413 const uint64_t F = get(Opcode).TSFlags;
2414 assert(get(Opcode).isBranch() &&
2415 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2416 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2417}
2418
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002419bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2420 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2421 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2422 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2423 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002424}
2425
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002426bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2427 switch (MI.getOpcode()) {
2428 // Byte
2429 case Hexagon::L2_loadrb_io:
2430 case Hexagon::L4_loadrb_ur:
2431 case Hexagon::L4_loadrb_ap:
2432 case Hexagon::L2_loadrb_pr:
2433 case Hexagon::L2_loadrb_pbr:
2434 case Hexagon::L2_loadrb_pi:
2435 case Hexagon::L2_loadrb_pci:
2436 case Hexagon::L2_loadrb_pcr:
2437 case Hexagon::L2_loadbsw2_io:
2438 case Hexagon::L4_loadbsw2_ur:
2439 case Hexagon::L4_loadbsw2_ap:
2440 case Hexagon::L2_loadbsw2_pr:
2441 case Hexagon::L2_loadbsw2_pbr:
2442 case Hexagon::L2_loadbsw2_pi:
2443 case Hexagon::L2_loadbsw2_pci:
2444 case Hexagon::L2_loadbsw2_pcr:
2445 case Hexagon::L2_loadbsw4_io:
2446 case Hexagon::L4_loadbsw4_ur:
2447 case Hexagon::L4_loadbsw4_ap:
2448 case Hexagon::L2_loadbsw4_pr:
2449 case Hexagon::L2_loadbsw4_pbr:
2450 case Hexagon::L2_loadbsw4_pi:
2451 case Hexagon::L2_loadbsw4_pci:
2452 case Hexagon::L2_loadbsw4_pcr:
2453 case Hexagon::L4_loadrb_rr:
2454 case Hexagon::L2_ploadrbt_io:
2455 case Hexagon::L2_ploadrbt_pi:
2456 case Hexagon::L2_ploadrbf_io:
2457 case Hexagon::L2_ploadrbf_pi:
2458 case Hexagon::L2_ploadrbtnew_io:
2459 case Hexagon::L2_ploadrbfnew_io:
2460 case Hexagon::L4_ploadrbt_rr:
2461 case Hexagon::L4_ploadrbf_rr:
2462 case Hexagon::L4_ploadrbtnew_rr:
2463 case Hexagon::L4_ploadrbfnew_rr:
2464 case Hexagon::L2_ploadrbtnew_pi:
2465 case Hexagon::L2_ploadrbfnew_pi:
2466 case Hexagon::L4_ploadrbt_abs:
2467 case Hexagon::L4_ploadrbf_abs:
2468 case Hexagon::L4_ploadrbtnew_abs:
2469 case Hexagon::L4_ploadrbfnew_abs:
2470 case Hexagon::L2_loadrbgp:
2471 // Half
2472 case Hexagon::L2_loadrh_io:
2473 case Hexagon::L4_loadrh_ur:
2474 case Hexagon::L4_loadrh_ap:
2475 case Hexagon::L2_loadrh_pr:
2476 case Hexagon::L2_loadrh_pbr:
2477 case Hexagon::L2_loadrh_pi:
2478 case Hexagon::L2_loadrh_pci:
2479 case Hexagon::L2_loadrh_pcr:
2480 case Hexagon::L4_loadrh_rr:
2481 case Hexagon::L2_ploadrht_io:
2482 case Hexagon::L2_ploadrht_pi:
2483 case Hexagon::L2_ploadrhf_io:
2484 case Hexagon::L2_ploadrhf_pi:
2485 case Hexagon::L2_ploadrhtnew_io:
2486 case Hexagon::L2_ploadrhfnew_io:
2487 case Hexagon::L4_ploadrht_rr:
2488 case Hexagon::L4_ploadrhf_rr:
2489 case Hexagon::L4_ploadrhtnew_rr:
2490 case Hexagon::L4_ploadrhfnew_rr:
2491 case Hexagon::L2_ploadrhtnew_pi:
2492 case Hexagon::L2_ploadrhfnew_pi:
2493 case Hexagon::L4_ploadrht_abs:
2494 case Hexagon::L4_ploadrhf_abs:
2495 case Hexagon::L4_ploadrhtnew_abs:
2496 case Hexagon::L4_ploadrhfnew_abs:
2497 case Hexagon::L2_loadrhgp:
2498 return true;
2499 default:
2500 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002501 }
2502}
2503
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002504bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2505 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002506 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2507}
2508
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002509bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2510 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002511 case Hexagon::STriw_pred:
2512 case Hexagon::LDriw_pred:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002513 return true;
2514 default:
2515 return false;
2516 }
2517}
2518
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002519bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2520 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002521 return false;
2522
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002523 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002524 if (Op.isGlobal() || Op.isSymbol())
2525 return true;
2526 return false;
2527}
2528
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002529// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002530bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2531 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002532 return is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002533}
2534
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002535bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2536 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002537 return is_TC2(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002538}
2539
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002540bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2541 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002542 return is_TC2early(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002543}
2544
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002545bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2546 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002547 return is_TC4x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002548}
2549
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002550// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002551bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2552 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002553 if (mayBeCurLoad(MI1)) {
2554 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002555 unsigned DstReg = MI1.getOperand(0).getReg();
2556 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002557 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002558 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002559 return true;
2560 }
2561 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002562 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2563 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2564 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002565 return true;
2566 return false;
2567}
2568
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002569bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002570 const uint64_t V = getType(MI);
2571 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2572}
2573
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002574// Check if the Offset is a valid auto-inc imm by Load/Store Type.
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002575bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, int Offset) const {
2576 int Size = VT.getSizeInBits() / 8;
2577 if (Offset % Size != 0)
2578 return false;
2579 int Count = Offset / Size;
2580
2581 switch (VT.getSimpleVT().SimpleTy) {
2582 // For scalars the auto-inc is s4
2583 case MVT::i8:
2584 case MVT::i16:
2585 case MVT::i32:
2586 case MVT::i64:
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002587 case MVT::v2i16:
2588 case MVT::v2i32:
2589 case MVT::v4i8:
2590 case MVT::v4i16:
2591 case MVT::v8i8:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002592 return isInt<4>(Count);
2593 // For HVX vectors the auto-inc is s3
2594 case MVT::v64i8:
2595 case MVT::v32i16:
2596 case MVT::v16i32:
2597 case MVT::v8i64:
2598 case MVT::v128i8:
2599 case MVT::v64i16:
2600 case MVT::v32i32:
2601 case MVT::v16i64:
2602 return isInt<3>(Count);
2603 default:
2604 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002605 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002606
2607 llvm_unreachable("Not an valid type!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002608}
2609
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002610bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002611 const TargetRegisterInfo *TRI, bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002612 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002613 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002614 // inserted to calculate the final address. Due to this reason, the function
2615 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002616 // We used to assert if the offset was not properly aligned, however,
2617 // there are cases where a misaligned pointer recast can cause this
2618 // problem, and we need to allow for it. The front end warns of such
2619 // misaligns with respect to load size.
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002620 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002621 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002622 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002623 case Hexagon::PS_vstorerw_nt_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002624 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002625 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002626 case Hexagon::PS_vloadrw_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002627 case Hexagon::V6_vL32b_ai:
2628 case Hexagon::V6_vS32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002629 case Hexagon::V6_vL32b_nt_ai:
2630 case Hexagon::V6_vS32b_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002631 case Hexagon::V6_vL32Ub_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002632 case Hexagon::V6_vS32Ub_ai: {
2633 unsigned VectorSize = TRI->getSpillSize(Hexagon::HvxVRRegClass);
2634 assert(isPowerOf2_32(VectorSize));
2635 if (Offset & (VectorSize-1))
2636 return false;
2637 return isInt<4>(Offset >> Log2_32(VectorSize));
2638 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002639
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002640 case Hexagon::J2_loop0i:
2641 case Hexagon::J2_loop1i:
2642 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002643
2644 case Hexagon::S4_storeirb_io:
2645 case Hexagon::S4_storeirbt_io:
2646 case Hexagon::S4_storeirbf_io:
2647 return isUInt<6>(Offset);
2648
2649 case Hexagon::S4_storeirh_io:
2650 case Hexagon::S4_storeirht_io:
2651 case Hexagon::S4_storeirhf_io:
2652 return isShiftedUInt<6,1>(Offset);
2653
2654 case Hexagon::S4_storeiri_io:
2655 case Hexagon::S4_storeirit_io:
2656 case Hexagon::S4_storeirif_io:
2657 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002658 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002659
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002660 if (Extend)
2661 return true;
2662
2663 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002664 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002665 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002666 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2667 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2668
Colin LeMahieu947cd702014-12-23 20:44:59 +00002669 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002670 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002671 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2672 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2673
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002674 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002675 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002676 case Hexagon::S2_storerh_io:
Krzysztof Parzyszekd10df492017-05-03 15:36:51 +00002677 case Hexagon::S2_storerf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002678 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2679 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2680
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002681 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002682 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002683 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002684 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2685 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2686
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002687 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002688 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2689 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2690
Eugene Zelenko3b873362017-09-28 22:27:31 +00002691 case Hexagon::L4_iadd_memopw_io:
2692 case Hexagon::L4_isub_memopw_io:
2693 case Hexagon::L4_add_memopw_io:
2694 case Hexagon::L4_sub_memopw_io:
2695 case Hexagon::L4_and_memopw_io:
2696 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002697 return (0 <= Offset && Offset <= 255);
2698
Eugene Zelenko3b873362017-09-28 22:27:31 +00002699 case Hexagon::L4_iadd_memoph_io:
2700 case Hexagon::L4_isub_memoph_io:
2701 case Hexagon::L4_add_memoph_io:
2702 case Hexagon::L4_sub_memoph_io:
2703 case Hexagon::L4_and_memoph_io:
2704 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002705 return (0 <= Offset && Offset <= 127);
2706
Eugene Zelenko3b873362017-09-28 22:27:31 +00002707 case Hexagon::L4_iadd_memopb_io:
2708 case Hexagon::L4_isub_memopb_io:
2709 case Hexagon::L4_add_memopb_io:
2710 case Hexagon::L4_sub_memopb_io:
2711 case Hexagon::L4_and_memopb_io:
2712 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002713 return (0 <= Offset && Offset <= 63);
2714
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002715 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002716 // any size. Later pass knows how to handle it.
2717 case Hexagon::STriw_pred:
2718 case Hexagon::LDriw_pred:
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00002719 case Hexagon::STriw_ctr:
2720 case Hexagon::LDriw_ctr:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002721 return true;
2722
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002723 case Hexagon::PS_fi:
2724 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002725 case Hexagon::INLINEASM:
2726 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002727
2728 case Hexagon::L2_ploadrbt_io:
2729 case Hexagon::L2_ploadrbf_io:
2730 case Hexagon::L2_ploadrubt_io:
2731 case Hexagon::L2_ploadrubf_io:
2732 case Hexagon::S2_pstorerbt_io:
2733 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002734 return isUInt<6>(Offset);
2735
2736 case Hexagon::L2_ploadrht_io:
2737 case Hexagon::L2_ploadrhf_io:
2738 case Hexagon::L2_ploadruht_io:
2739 case Hexagon::L2_ploadruhf_io:
2740 case Hexagon::S2_pstorerht_io:
2741 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002742 return isShiftedUInt<6,1>(Offset);
2743
2744 case Hexagon::L2_ploadrit_io:
2745 case Hexagon::L2_ploadrif_io:
2746 case Hexagon::S2_pstorerit_io:
2747 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002748 return isShiftedUInt<6,2>(Offset);
2749
2750 case Hexagon::L2_ploadrdt_io:
2751 case Hexagon::L2_ploadrdf_io:
2752 case Hexagon::S2_pstorerdt_io:
2753 case Hexagon::S2_pstorerdf_io:
2754 return isShiftedUInt<6,3>(Offset);
2755 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002756
Benjamin Kramerb6684012011-12-27 11:41:05 +00002757 llvm_unreachable("No offset range is defined for this opcode. "
2758 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002759}
2760
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002761bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002762 return isHVXVec(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002763}
2764
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002765bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2766 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002767 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2768 return
2769 V == HexagonII::TypeCVI_VA ||
2770 V == HexagonII::TypeCVI_VA_DV;
2771}
Andrew Trickd06df962012-02-01 22:13:57 +00002772
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002773bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2774 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002775 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2776 return true;
2777
2778 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2779 return true;
2780
2781 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002782 return true;
2783
2784 return false;
2785}
Jyotsna Verma84256432013-03-01 17:37:13 +00002786
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002787bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2788 switch (MI.getOpcode()) {
2789 // Byte
2790 case Hexagon::L2_loadrub_io:
2791 case Hexagon::L4_loadrub_ur:
2792 case Hexagon::L4_loadrub_ap:
2793 case Hexagon::L2_loadrub_pr:
2794 case Hexagon::L2_loadrub_pbr:
2795 case Hexagon::L2_loadrub_pi:
2796 case Hexagon::L2_loadrub_pci:
2797 case Hexagon::L2_loadrub_pcr:
2798 case Hexagon::L2_loadbzw2_io:
2799 case Hexagon::L4_loadbzw2_ur:
2800 case Hexagon::L4_loadbzw2_ap:
2801 case Hexagon::L2_loadbzw2_pr:
2802 case Hexagon::L2_loadbzw2_pbr:
2803 case Hexagon::L2_loadbzw2_pi:
2804 case Hexagon::L2_loadbzw2_pci:
2805 case Hexagon::L2_loadbzw2_pcr:
2806 case Hexagon::L2_loadbzw4_io:
2807 case Hexagon::L4_loadbzw4_ur:
2808 case Hexagon::L4_loadbzw4_ap:
2809 case Hexagon::L2_loadbzw4_pr:
2810 case Hexagon::L2_loadbzw4_pbr:
2811 case Hexagon::L2_loadbzw4_pi:
2812 case Hexagon::L2_loadbzw4_pci:
2813 case Hexagon::L2_loadbzw4_pcr:
2814 case Hexagon::L4_loadrub_rr:
2815 case Hexagon::L2_ploadrubt_io:
2816 case Hexagon::L2_ploadrubt_pi:
2817 case Hexagon::L2_ploadrubf_io:
2818 case Hexagon::L2_ploadrubf_pi:
2819 case Hexagon::L2_ploadrubtnew_io:
2820 case Hexagon::L2_ploadrubfnew_io:
2821 case Hexagon::L4_ploadrubt_rr:
2822 case Hexagon::L4_ploadrubf_rr:
2823 case Hexagon::L4_ploadrubtnew_rr:
2824 case Hexagon::L4_ploadrubfnew_rr:
2825 case Hexagon::L2_ploadrubtnew_pi:
2826 case Hexagon::L2_ploadrubfnew_pi:
2827 case Hexagon::L4_ploadrubt_abs:
2828 case Hexagon::L4_ploadrubf_abs:
2829 case Hexagon::L4_ploadrubtnew_abs:
2830 case Hexagon::L4_ploadrubfnew_abs:
2831 case Hexagon::L2_loadrubgp:
2832 // Half
2833 case Hexagon::L2_loadruh_io:
2834 case Hexagon::L4_loadruh_ur:
2835 case Hexagon::L4_loadruh_ap:
2836 case Hexagon::L2_loadruh_pr:
2837 case Hexagon::L2_loadruh_pbr:
2838 case Hexagon::L2_loadruh_pi:
2839 case Hexagon::L2_loadruh_pci:
2840 case Hexagon::L2_loadruh_pcr:
2841 case Hexagon::L4_loadruh_rr:
2842 case Hexagon::L2_ploadruht_io:
2843 case Hexagon::L2_ploadruht_pi:
2844 case Hexagon::L2_ploadruhf_io:
2845 case Hexagon::L2_ploadruhf_pi:
2846 case Hexagon::L2_ploadruhtnew_io:
2847 case Hexagon::L2_ploadruhfnew_io:
2848 case Hexagon::L4_ploadruht_rr:
2849 case Hexagon::L4_ploadruhf_rr:
2850 case Hexagon::L4_ploadruhtnew_rr:
2851 case Hexagon::L4_ploadruhfnew_rr:
2852 case Hexagon::L2_ploadruhtnew_pi:
2853 case Hexagon::L2_ploadruhfnew_pi:
2854 case Hexagon::L4_ploadruht_abs:
2855 case Hexagon::L4_ploadruhf_abs:
2856 case Hexagon::L4_ploadruhtnew_abs:
2857 case Hexagon::L4_ploadruhfnew_abs:
2858 case Hexagon::L2_loadruhgp:
2859 return true;
2860 default:
2861 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002862 }
2863}
2864
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002865// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002866bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2867 const MachineInstr &MI2) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002868 if (isHVXVec(MI1) && isHVXVec(MI2))
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002869 if (!isVecUsableNextPacket(MI1, MI2))
2870 return true;
2871 return false;
2872}
2873
Brendon Cahoon254f8892016-07-29 16:44:44 +00002874/// \brief Get the base register and byte offset of a load/store instr.
2875bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2876 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2877 const {
2878 unsigned AccessSize = 0;
2879 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002880 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002881 Offset = OffsetVal;
2882 return BaseReg != 0;
2883}
2884
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002885/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002886bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2887 const MachineInstr &Second) const {
Krzysztof Parzyszek4763c2d2017-05-03 15:33:09 +00002888 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2889 const MachineOperand &Op = Second.getOperand(0);
2890 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2891 return true;
2892 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002893 if (DisableNVSchedule)
2894 return false;
2895 if (mayBeNewStore(Second)) {
2896 // Make sure the definition of the first instruction is the value being
2897 // stored.
2898 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002899 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002900 if (!Stored.isReg())
2901 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002902 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2903 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002904 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2905 return true;
2906 }
2907 }
2908 return false;
2909}
2910
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002911bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2912 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002913 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002914}
2915
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002916bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2917 for (auto &I : *B)
2918 if (I.isEHLabel())
2919 return true;
2920 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002921}
2922
Jyotsna Verma84256432013-03-01 17:37:13 +00002923// Returns true if an instruction can be converted into a non-extended
2924// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002925bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002926 short NonExtOpcode;
2927 // Check if the instruction has a register form that uses register in place
2928 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002929 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002930 return true;
2931
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002932 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002933 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002934
2935 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002936 case HexagonII::Absolute:
Jyotsna Verma84256432013-03-01 17:37:13 +00002937 // Load/store with absolute addressing mode can be converted into
2938 // base+offset mode.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002939 NonExtOpcode = Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002940 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002941 case HexagonII::BaseImmOffset:
Jyotsna Verma84256432013-03-01 17:37:13 +00002942 // Load/store with base+offset addressing mode can be converted into
2943 // base+register offset addressing mode. However left shift operand should
2944 // be set to 0.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002945 NonExtOpcode = Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002946 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002947 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002948 NonExtOpcode = Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002949 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002950 default:
2951 return false;
2952 }
2953 if (NonExtOpcode < 0)
2954 return false;
2955 return true;
2956 }
2957 return false;
2958}
2959
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002960bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2961 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002962 Hexagon::InstrType_Pseudo) >= 0;
2963}
2964
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002965bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2966 const {
2967 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2968 while (I != E) {
2969 if (I->isBarrier())
2970 return true;
2971 ++I;
2972 }
2973 return false;
2974}
2975
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002976// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002977bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002978 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002979 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00002980 Subtarget.hasV60TOps();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002981}
2982
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002983// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002984bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2985 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002986 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2987}
2988
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002989bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2990 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002991 // There is no stall when ProdMI is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002992 if (!isHVXVec(ProdMI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002993 return false;
2994
2995 // There is no stall when ProdMI and ConsMI are not dependent.
2996 if (!isDependent(ProdMI, ConsMI))
2997 return false;
2998
2999 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3000 // are scheduled in consecutive packets.
3001 if (isVecUsableNextPacket(ProdMI, ConsMI))
3002 return false;
3003
3004 return true;
3005}
3006
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003007bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003008 MachineBasicBlock::const_instr_iterator BII) const {
3009 // There is no stall when I is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003010 if (!isHVXVec(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003011 return false;
3012
3013 MachineBasicBlock::const_instr_iterator MII = BII;
3014 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3015
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003016 if (!MII->isBundle())
3017 return producesStall(*MII, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003018
3019 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003020 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003021 if (producesStall(J, MI))
3022 return true;
3023 }
3024 return false;
3025}
3026
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003027bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003028 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00003029 for (const MachineOperand &MO : MI.operands()) {
3030 // Predicate register must be explicitly defined.
3031 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
3032 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003033 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00003034 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003035 }
3036
3037 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3038 // memd_locked cannot be used as .new as well,
3039 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003040 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003041}
3042
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003043bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003044 return Opcode == Hexagon::J2_jumpt ||
3045 Opcode == Hexagon::J2_jumptpt ||
3046 Opcode == Hexagon::J2_jumpf ||
3047 Opcode == Hexagon::J2_jumpfpt ||
3048 Opcode == Hexagon::J2_jumptnew ||
3049 Opcode == Hexagon::J2_jumpfnew ||
3050 Opcode == Hexagon::J2_jumptnewpt ||
3051 Opcode == Hexagon::J2_jumpfnewpt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003052}
3053
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003054bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3055 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3056 return false;
3057 return !isPredicatedTrue(Cond[0].getImm());
3058}
3059
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003060unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3061 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003062 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3063}
3064
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003065// Returns the base register in a memory access (load/store). The offset is
3066// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003067// If the base register has a subregister or the offset field does not contain
3068// an immediate value, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003069unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003070 int &Offset, unsigned &AccessSize) const {
3071 // Return if it is not a base+offset type instruction or a MemOp.
3072 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3073 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003074 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003075 return 0;
3076
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003077 AccessSize = getMemAccessSize(MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003078
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003079 unsigned BasePos = 0, OffsetPos = 0;
3080 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003081 return 0;
3082
3083 // Post increment updates its EA after the mem access,
3084 // so we need to treat its offset as zero.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003085 if (isPostIncrement(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003086 Offset = 0;
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003087 } else {
3088 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
3089 if (!OffsetOp.isImm())
3090 return 0;
3091 Offset = OffsetOp.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003092 }
3093
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003094 const MachineOperand &BaseOp = MI.getOperand(BasePos);
3095 if (BaseOp.getSubReg() != 0)
3096 return 0;
3097 return BaseOp.getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003098}
3099
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003100/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003101bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003102 unsigned &BasePos, unsigned &OffsetPos) const {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003103 if (!isAddrModeWithOffset(MI) && !isPostIncrement(MI))
3104 return false;
3105
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003106 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003107 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003108 BasePos = 0;
3109 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003110 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003111 BasePos = 0;
3112 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003113 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003114 BasePos = 1;
3115 OffsetPos = 2;
3116 } else
3117 return false;
3118
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003119 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003120 BasePos++;
3121 OffsetPos++;
3122 }
3123 if (isPostIncrement(MI)) {
3124 BasePos++;
3125 OffsetPos++;
3126 }
3127
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003128 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003129 return false;
3130
3131 return true;
3132}
3133
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003134// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003135// e.g. jump_t t1 (i1)
3136// jump t2 (i2)
3137// Jumpers = {i2, i1}
3138SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3139 MachineBasicBlock& MBB) const {
3140 SmallVector<MachineInstr*, 2> Jumpers;
3141 // If the block has no terminators, it just falls into the block after it.
3142 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3143 if (I == MBB.instr_begin())
3144 return Jumpers;
3145
3146 // A basic block may looks like this:
3147 //
3148 // [ insn
3149 // EH_LABEL
3150 // insn
3151 // insn
3152 // insn
3153 // EH_LABEL
3154 // insn ]
3155 //
3156 // It has two succs but does not have a terminator
3157 // Don't know how to handle it.
3158 do {
3159 --I;
3160 if (I->isEHLabel())
3161 return Jumpers;
3162 } while (I != MBB.instr_begin());
3163
3164 I = MBB.instr_end();
3165 --I;
3166
3167 while (I->isDebugValue()) {
3168 if (I == MBB.instr_begin())
3169 return Jumpers;
3170 --I;
3171 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003172 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003173 return Jumpers;
3174
3175 // Get the last instruction in the block.
3176 MachineInstr *LastInst = &*I;
3177 Jumpers.push_back(LastInst);
3178 MachineInstr *SecondLastInst = nullptr;
3179 // Find one more terminator if present.
3180 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003181 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003182 if (!SecondLastInst) {
3183 SecondLastInst = &*I;
3184 Jumpers.push_back(SecondLastInst);
3185 } else // This is a third branch.
3186 return Jumpers;
3187 }
3188 if (I == MBB.instr_begin())
3189 break;
3190 --I;
3191 } while (true);
3192 return Jumpers;
3193}
3194
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003195// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003196unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3197 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003198 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3199}
3200
3201// See if instruction could potentially be a duplex candidate.
3202// If so, return its group. Zero otherwise.
3203HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003204 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003205 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3206
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003207 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003208 default:
3209 return HexagonII::HCG_None;
3210 //
3211 // Compound pairs.
3212 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3213 // "Rd16=#U6 ; jump #r9:2"
3214 // "Rd16=Rs16 ; jump #r9:2"
3215 //
3216 case Hexagon::C2_cmpeq:
3217 case Hexagon::C2_cmpgt:
3218 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003219 DstReg = MI.getOperand(0).getReg();
3220 Src1Reg = MI.getOperand(1).getReg();
3221 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003222 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3223 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3224 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3225 return HexagonII::HCG_A;
3226 break;
3227 case Hexagon::C2_cmpeqi:
3228 case Hexagon::C2_cmpgti:
3229 case Hexagon::C2_cmpgtui:
3230 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003231 DstReg = MI.getOperand(0).getReg();
3232 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003233 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3234 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003235 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3236 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3237 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003238 return HexagonII::HCG_A;
3239 break;
3240 case Hexagon::A2_tfr:
3241 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003242 DstReg = MI.getOperand(0).getReg();
3243 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003244 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3245 return HexagonII::HCG_A;
3246 break;
3247 case Hexagon::A2_tfrsi:
3248 // Rd = #u6
3249 // Do not test for #u6 size since the const is getting extended
3250 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003251 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003252 if (isIntRegForSubInst(DstReg))
3253 return HexagonII::HCG_A;
3254 break;
3255 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003256 DstReg = MI.getOperand(0).getReg();
3257 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003258 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3259 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003260 MI.getOperand(2).isImm() &&
3261 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003262 return HexagonII::HCG_A;
3263 break;
3264 // The fact that .new form is used pretty much guarantees
3265 // that predicate register will match. Nevertheless,
3266 // there could be some false positives without additional
3267 // checking.
3268 case Hexagon::J2_jumptnew:
3269 case Hexagon::J2_jumpfnew:
3270 case Hexagon::J2_jumptnewpt:
3271 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003272 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003273 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3274 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3275 return HexagonII::HCG_B;
3276 break;
3277 // Transfer and jump:
3278 // Rd=#U6 ; jump #r9:2
3279 // Rd=Rs ; jump #r9:2
3280 // Do not test for jump range here.
3281 case Hexagon::J2_jump:
3282 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003283 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003284 return HexagonII::HCG_C;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003285 }
3286
3287 return HexagonII::HCG_None;
3288}
3289
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003290// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003291unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3292 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003293 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3294 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003295 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3296 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003297 return -1u;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003298 unsigned DestReg = GA.getOperand(0).getReg();
3299 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003300 return -1u;
3301 if (DestReg != Hexagon::P0 && DestReg != Hexagon::P1)
3302 return -1u;
3303 // The value compared against must be either u5 or -1.
3304 const MachineOperand &CmpOp = GA.getOperand(2);
3305 if (!CmpOp.isImm())
3306 return -1u;
3307 int V = CmpOp.getImm();
3308 if (V == -1)
3309 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqn1_tp0_jump_nt
3310 : Hexagon::J4_cmpeqn1_tp1_jump_nt;
3311 if (!isUInt<5>(V))
3312 return -1u;
3313 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqi_tp0_jump_nt
3314 : Hexagon::J4_cmpeqi_tp1_jump_nt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003315}
3316
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003317int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3318 enum Hexagon::PredSense inPredSense;
3319 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3320 Hexagon::PredSense_true;
3321 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3322 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3323 return CondOpcode;
3324
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003325 llvm_unreachable("Unexpected predicable instruction");
3326}
3327
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003328// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003329int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3330 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003331 default: llvm_unreachable("Unknown .cur type");
3332 case Hexagon::V6_vL32b_pi:
3333 return Hexagon::V6_vL32b_cur_pi;
3334 case Hexagon::V6_vL32b_ai:
3335 return Hexagon::V6_vL32b_cur_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003336 case Hexagon::V6_vL32b_nt_pi:
3337 return Hexagon::V6_vL32b_nt_cur_pi;
3338 case Hexagon::V6_vL32b_nt_ai:
3339 return Hexagon::V6_vL32b_nt_cur_ai;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003340 }
3341 return 0;
3342}
3343
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003344// Return the regular version of the .cur instruction.
3345int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3346 switch (MI.getOpcode()) {
3347 default: llvm_unreachable("Unknown .cur type");
3348 case Hexagon::V6_vL32b_cur_pi:
3349 return Hexagon::V6_vL32b_pi;
3350 case Hexagon::V6_vL32b_cur_ai:
3351 return Hexagon::V6_vL32b_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003352 case Hexagon::V6_vL32b_nt_cur_pi:
3353 return Hexagon::V6_vL32b_nt_pi;
3354 case Hexagon::V6_vL32b_nt_cur_ai:
3355 return Hexagon::V6_vL32b_nt_ai;
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003356 }
3357 return 0;
3358}
3359
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003360// The diagram below shows the steps involved in the conversion of a predicated
3361// store instruction to its .new predicated new-value form.
3362//
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003363// Note: It doesn't include conditional new-value stores as they can't be
3364// converted to .new predicate.
3365//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003366// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3367// ^ ^
3368// / \ (not OK. it will cause new-value store to be
3369// / X conditional on p0.new while R2 producer is
3370// / \ on p0)
3371// / \.
3372// p.new store p.old NV store
3373// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3374// ^ ^
3375// \ /
3376// \ /
3377// \ /
3378// p.old store
3379// [if (p0)memw(R0+#0)=R2]
3380//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003381// The following set of instructions further explains the scenario where
3382// conditional new-value store becomes invalid when promoted to .new predicate
3383// form.
3384//
3385// { 1) if (p0) r0 = add(r1, r2)
3386// 2) p0 = cmp.eq(r3, #0) }
3387//
3388// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3389// the first two instructions because in instr 1, r0 is conditional on old value
3390// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3391// is not valid for new-value stores.
3392// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3393// from the "Conditional Store" list. Because a predicated new value store
3394// would NOT be promoted to a double dot new store. See diagram below:
3395// This function returns yes for those stores that are predicated but not
3396// yet promoted to predicate dot new instructions.
3397//
3398// +---------------------+
3399// /-----| if (p0) memw(..)=r0 |---------\~
3400// || +---------------------+ ||
3401// promote || /\ /\ || promote
3402// || /||\ /||\ ||
3403// \||/ demote || \||/
3404// \/ || || \/
3405// +-------------------------+ || +-------------------------+
3406// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3407// +-------------------------+ || +-------------------------+
3408// || || ||
3409// || demote \||/
3410// promote || \/ NOT possible
3411// || || /\~
3412// \||/ || /||\~
3413// \/ || ||
3414// +-----------------------------+
3415// | if (p0.new) memw(..)=r0.new |
3416// +-----------------------------+
3417// Double Dot New Store
3418//
3419// Returns the most basic instruction for the .new predicated instructions and
3420// new-value stores.
3421// For example, all of the following instructions will be converted back to the
3422// same instruction:
3423// 1) if (p0.new) memw(R0+#0) = R1.new --->
3424// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3425// 3) if (p0.new) memw(R0+#0) = R1 --->
3426//
3427// To understand the translation of instruction 1 to its original form, consider
3428// a packet with 3 instructions.
3429// { p0 = cmp.eq(R0,R1)
3430// if (p0.new) R2 = add(R3, R4)
3431// R5 = add (R3, R1)
3432// }
3433// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3434//
3435// This instruction can be part of the previous packet only if both p0 and R2
3436// are promoted to .new values. This promotion happens in steps, first
3437// predicate register is promoted to .new and in the next iteration R2 is
3438// promoted. Therefore, in case of dependence check failure (due to R5) during
3439// next iteration, it should be converted back to its most basic form.
3440
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003441// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003442int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3443 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003444 if (NVOpcode >= 0) // Valid new-value store instruction.
3445 return NVOpcode;
3446
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003447 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003448 default:
Eugene Zelenko3b873362017-09-28 22:27:31 +00003449 report_fatal_error(std::string("Unknown .new type: ") +
3450 std::to_string(MI.getOpcode()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003451 case Hexagon::S4_storerb_ur:
3452 return Hexagon::S4_storerbnew_ur;
3453
3454 case Hexagon::S2_storerb_pci:
3455 return Hexagon::S2_storerb_pci;
3456
3457 case Hexagon::S2_storeri_pci:
3458 return Hexagon::S2_storeri_pci;
3459
3460 case Hexagon::S2_storerh_pci:
3461 return Hexagon::S2_storerh_pci;
3462
3463 case Hexagon::S2_storerd_pci:
3464 return Hexagon::S2_storerd_pci;
3465
3466 case Hexagon::S2_storerf_pci:
3467 return Hexagon::S2_storerf_pci;
3468
3469 case Hexagon::V6_vS32b_ai:
3470 return Hexagon::V6_vS32b_new_ai;
3471
3472 case Hexagon::V6_vS32b_pi:
3473 return Hexagon::V6_vS32b_new_pi;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003474 }
3475 return 0;
3476}
3477
3478// Returns the opcode to use when converting MI, which is a conditional jump,
3479// into a conditional instruction which uses the .new value of the predicate.
3480// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003481// If MBPI is null, all edges will be treated as equally likely for the
3482// purposes of establishing a predication hint.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003483int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003484 const MachineBranchProbabilityInfo *MBPI) const {
3485 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003486 const MachineBasicBlock *Src = MI.getParent();
3487 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003488 bool Taken = false;
3489 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003490
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003491 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3492 const MachineBasicBlock *Dst) {
3493 if (MBPI)
3494 return MBPI->getEdgeProbability(Src, Dst);
3495 return BranchProbability(1, Src->succ_size());
3496 };
3497
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003498 if (BrTarget.isMBB()) {
3499 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003500 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003501 } else {
3502 // The branch target is not a basic block (most likely a function).
3503 // Since BPI only gives probabilities for targets that are basic blocks,
3504 // try to identify another target of this branch (potentially a fall-
3505 // -through) and check the probability of that target.
3506 //
3507 // The only handled branch combinations are:
3508 // - one conditional branch,
3509 // - one conditional branch followed by one unconditional branch.
3510 // Otherwise, assume not-taken.
3511 assert(MI.isConditionalBranch());
3512 const MachineBasicBlock &B = *MI.getParent();
3513 bool SawCond = false, Bad = false;
3514 for (const MachineInstr &I : B) {
3515 if (!I.isBranch())
3516 continue;
3517 if (I.isConditionalBranch()) {
3518 SawCond = true;
3519 if (&I != &MI) {
3520 Bad = true;
3521 break;
3522 }
3523 }
3524 if (I.isUnconditionalBranch() && !SawCond) {
3525 Bad = true;
3526 break;
3527 }
3528 }
3529 if (!Bad) {
3530 MachineBasicBlock::const_instr_iterator It(MI);
3531 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3532 if (NextIt == B.instr_end()) {
3533 // If this branch is the last, look for the fall-through block.
3534 for (const MachineBasicBlock *SB : B.successors()) {
3535 if (!B.isLayoutSuccessor(SB))
3536 continue;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003537 Taken = getEdgeProbability(Src, SB) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003538 break;
3539 }
3540 } else {
3541 assert(NextIt->isUnconditionalBranch());
3542 // Find the first MBB operand and assume it's the target.
3543 const MachineBasicBlock *BT = nullptr;
3544 for (const MachineOperand &Op : NextIt->operands()) {
3545 if (!Op.isMBB())
3546 continue;
3547 BT = Op.getMBB();
3548 break;
3549 }
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003550 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003551 }
3552 } // if (!Bad)
3553 }
3554
3555 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003556
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003557 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003558 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003559 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003560 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003561 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003562
3563 default:
3564 llvm_unreachable("Unexpected jump instruction.");
3565 }
3566}
3567
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003568// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003569int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003570 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003571 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003572 // Condtional Jumps
3573 case Hexagon::J2_jumpt:
3574 case Hexagon::J2_jumpf:
3575 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003576 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003577
3578 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3579 if (NewOpcode >= 0)
3580 return NewOpcode;
Krzysztof Parzyszek066e8b52017-06-02 14:07:06 +00003581 return 0;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003582}
3583
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003584int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
3585 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003586 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3587 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003588 // All Hexagon architectures have prediction bits on dot-new branches,
3589 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3590 // to pick the right opcode when converting back to dot-old.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003591 if (!Subtarget.getFeatureBits()[Hexagon::ArchV60]) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003592 switch (NewOp) {
3593 case Hexagon::J2_jumptpt:
3594 NewOp = Hexagon::J2_jumpt;
3595 break;
3596 case Hexagon::J2_jumpfpt:
3597 NewOp = Hexagon::J2_jumpf;
3598 break;
3599 case Hexagon::J2_jumprtpt:
3600 NewOp = Hexagon::J2_jumprt;
3601 break;
3602 case Hexagon::J2_jumprfpt:
3603 NewOp = Hexagon::J2_jumprf;
3604 break;
3605 }
3606 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003607 assert(NewOp >= 0 &&
3608 "Couldn't change predicate new instruction to its old form.");
3609 }
3610
3611 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3612 NewOp = Hexagon::getNonNVStore(NewOp);
3613 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3614 }
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003615
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003616 if (Subtarget.hasV60TOps())
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003617 return NewOp;
3618
3619 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3620 switch (NewOp) {
3621 case Hexagon::J2_jumpfpt:
3622 return Hexagon::J2_jumpf;
3623 case Hexagon::J2_jumptpt:
3624 return Hexagon::J2_jumpt;
3625 case Hexagon::J2_jumprfpt:
3626 return Hexagon::J2_jumprf;
3627 case Hexagon::J2_jumprtpt:
3628 return Hexagon::J2_jumprt;
3629 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003630 return NewOp;
3631}
3632
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003633// See if instruction could potentially be a duplex candidate.
3634// If so, return its group. Zero otherwise.
3635HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003636 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003637 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003638 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003639
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003640 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003641 default:
3642 return HexagonII::HSIG_None;
3643 //
3644 // Group L1:
3645 //
3646 // Rd = memw(Rs+#u4:2)
3647 // Rd = memub(Rs+#u4:0)
3648 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003649 DstReg = MI.getOperand(0).getReg();
3650 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003651 // Special case this one from Group L2.
3652 // Rd = memw(r29+#u5:2)
3653 if (isIntRegForSubInst(DstReg)) {
3654 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3655 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003656 MI.getOperand(2).isImm() &&
3657 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003658 return HexagonII::HSIG_L2;
3659 // Rd = memw(Rs+#u4:2)
3660 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003661 (MI.getOperand(2).isImm() &&
3662 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003663 return HexagonII::HSIG_L1;
3664 }
3665 break;
3666 case Hexagon::L2_loadrub_io:
3667 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003668 DstReg = MI.getOperand(0).getReg();
3669 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003670 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003671 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003672 return HexagonII::HSIG_L1;
3673 break;
3674 //
3675 // Group L2:
3676 //
3677 // Rd = memh/memuh(Rs+#u3:1)
3678 // Rd = memb(Rs+#u3:0)
3679 // Rd = memw(r29+#u5:2) - Handled above.
3680 // Rdd = memd(r29+#u5:3)
3681 // deallocframe
3682 // [if ([!]p0[.new])] dealloc_return
3683 // [if ([!]p0[.new])] jumpr r31
3684 case Hexagon::L2_loadrh_io:
3685 case Hexagon::L2_loadruh_io:
3686 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003687 DstReg = MI.getOperand(0).getReg();
3688 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003689 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003690 MI.getOperand(2).isImm() &&
3691 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003692 return HexagonII::HSIG_L2;
3693 break;
3694 case Hexagon::L2_loadrb_io:
3695 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003696 DstReg = MI.getOperand(0).getReg();
3697 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003698 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003699 MI.getOperand(2).isImm() &&
3700 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003701 return HexagonII::HSIG_L2;
3702 break;
3703 case Hexagon::L2_loadrd_io:
3704 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003705 DstReg = MI.getOperand(0).getReg();
3706 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003707 if (isDblRegForSubInst(DstReg, HRI) &&
3708 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3709 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003710 MI.getOperand(2).isImm() &&
3711 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003712 return HexagonII::HSIG_L2;
3713 break;
3714 // dealloc_return is not documented in Hexagon Manual, but marked
3715 // with A_SUBINSN attribute in iset_v4classic.py.
3716 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003717 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003718 case Hexagon::L4_return:
3719 case Hexagon::L2_deallocframe:
3720 return HexagonII::HSIG_L2;
3721 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003722 case Hexagon::PS_jmpret:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003723 case Hexagon::SL2_jumpr31:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003724 // jumpr r31
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003725 // Actual form JMPR implicit-def %pc, implicit %r31, implicit internal %r0
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003726 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003727 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3728 return HexagonII::HSIG_L2;
3729 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003730 case Hexagon::PS_jmprett:
3731 case Hexagon::PS_jmpretf:
3732 case Hexagon::PS_jmprettnewpt:
3733 case Hexagon::PS_jmpretfnewpt:
3734 case Hexagon::PS_jmprettnew:
3735 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003736 case Hexagon::SL2_jumpr31_t:
3737 case Hexagon::SL2_jumpr31_f:
3738 case Hexagon::SL2_jumpr31_tnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003739 DstReg = MI.getOperand(1).getReg();
3740 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003741 // [if ([!]p0[.new])] jumpr r31
3742 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3743 (Hexagon::P0 == SrcReg)) &&
3744 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3745 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003746 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00003747 case Hexagon::L4_return_t:
3748 case Hexagon::L4_return_f:
3749 case Hexagon::L4_return_tnew_pnt:
3750 case Hexagon::L4_return_fnew_pnt:
3751 case Hexagon::L4_return_tnew_pt:
3752 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003753 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003754 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003755 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3756 return HexagonII::HSIG_L2;
3757 break;
3758 //
3759 // Group S1:
3760 //
3761 // memw(Rs+#u4:2) = Rt
3762 // memb(Rs+#u4:0) = Rt
3763 case Hexagon::S2_storeri_io:
3764 // Special case this one from Group S2.
3765 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003766 Src1Reg = MI.getOperand(0).getReg();
3767 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003768 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3769 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003770 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3771 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003772 return HexagonII::HSIG_S2;
3773 // memw(Rs+#u4:2) = Rt
3774 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003775 MI.getOperand(1).isImm() &&
3776 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003777 return HexagonII::HSIG_S1;
3778 break;
3779 case Hexagon::S2_storerb_io:
3780 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003781 Src1Reg = MI.getOperand(0).getReg();
3782 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003783 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003784 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003785 return HexagonII::HSIG_S1;
3786 break;
3787 //
3788 // Group S2:
3789 //
3790 // memh(Rs+#u3:1) = Rt
3791 // memw(r29+#u5:2) = Rt
3792 // memd(r29+#s6:3) = Rtt
3793 // memw(Rs+#u4:2) = #U1
3794 // memb(Rs+#u4) = #U1
3795 // allocframe(#u5:3)
3796 case Hexagon::S2_storerh_io:
3797 // memh(Rs+#u3:1) = 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 (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003801 MI.getOperand(1).isImm() &&
3802 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003803 return HexagonII::HSIG_S1;
3804 break;
3805 case Hexagon::S2_storerd_io:
3806 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003807 Src1Reg = MI.getOperand(0).getReg();
3808 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003809 if (isDblRegForSubInst(Src2Reg, HRI) &&
3810 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003811 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3812 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003813 return HexagonII::HSIG_S2;
3814 break;
3815 case Hexagon::S4_storeiri_io:
3816 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003817 Src1Reg = MI.getOperand(0).getReg();
3818 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3819 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3820 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003821 return HexagonII::HSIG_S2;
3822 break;
3823 case Hexagon::S4_storeirb_io:
3824 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003825 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003826 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003827 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3828 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003829 return HexagonII::HSIG_S2;
3830 break;
3831 case Hexagon::S2_allocframe:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003832 if (MI.getOperand(2).isImm() &&
3833 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003834 return HexagonII::HSIG_S1;
3835 break;
3836 //
3837 // Group A:
3838 //
3839 // Rx = add(Rx,#s7)
3840 // Rd = Rs
3841 // Rd = #u6
3842 // Rd = #-1
3843 // if ([!]P0[.new]) Rd = #0
3844 // Rd = add(r29,#u6:2)
3845 // Rx = add(Rx,Rs)
3846 // P0 = cmp.eq(Rs,#u2)
3847 // Rdd = combine(#0,Rs)
3848 // Rdd = combine(Rs,#0)
3849 // Rdd = combine(#u2,#U2)
3850 // Rd = add(Rs,#1)
3851 // Rd = add(Rs,#-1)
3852 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3853 // Rd = and(Rs,#1)
3854 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003855 DstReg = MI.getOperand(0).getReg();
3856 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003857 if (isIntRegForSubInst(DstReg)) {
3858 // Rd = add(r29,#u6:2)
3859 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003860 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3861 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003862 return HexagonII::HSIG_A;
3863 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003864 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3865 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003866 return HexagonII::HSIG_A;
3867 // Rd = add(Rs,#1)
3868 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003869 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3870 ((MI.getOperand(2).getImm() == 1) ||
3871 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003872 return HexagonII::HSIG_A;
3873 }
3874 break;
3875 case Hexagon::A2_add:
3876 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003877 DstReg = MI.getOperand(0).getReg();
3878 Src1Reg = MI.getOperand(1).getReg();
3879 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003880 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3881 isIntRegForSubInst(Src2Reg))
3882 return HexagonII::HSIG_A;
3883 break;
3884 case Hexagon::A2_andir:
3885 // Same as zxtb.
3886 // Rd16=and(Rs16,#255)
3887 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003888 DstReg = MI.getOperand(0).getReg();
3889 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003890 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003891 MI.getOperand(2).isImm() &&
3892 ((MI.getOperand(2).getImm() == 1) ||
3893 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003894 return HexagonII::HSIG_A;
3895 break;
3896 case Hexagon::A2_tfr:
3897 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003898 DstReg = MI.getOperand(0).getReg();
3899 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003900 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3901 return HexagonII::HSIG_A;
3902 break;
3903 case Hexagon::A2_tfrsi:
3904 // Rd = #u6
3905 // Do not test for #u6 size since the const is getting extended
3906 // regardless and compound could be formed.
3907 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003908 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003909 if (isIntRegForSubInst(DstReg))
3910 return HexagonII::HSIG_A;
3911 break;
3912 case Hexagon::C2_cmoveit:
3913 case Hexagon::C2_cmovenewit:
3914 case Hexagon::C2_cmoveif:
3915 case Hexagon::C2_cmovenewif:
3916 // if ([!]P0[.new]) Rd = #0
3917 // Actual form:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003918 // %r16 = C2_cmovenewit internal %p0, 0, implicit undef %r16;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003919 DstReg = MI.getOperand(0).getReg();
3920 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003921 if (isIntRegForSubInst(DstReg) &&
3922 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003923 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003924 return HexagonII::HSIG_A;
3925 break;
3926 case Hexagon::C2_cmpeqi:
3927 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003928 DstReg = MI.getOperand(0).getReg();
3929 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003930 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3931 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003932 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003933 return HexagonII::HSIG_A;
3934 break;
3935 case Hexagon::A2_combineii:
3936 case Hexagon::A4_combineii:
3937 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003938 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003939 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003940 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3941 (MI.getOperand(1).isGlobal() &&
3942 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3943 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3944 (MI.getOperand(2).isGlobal() &&
3945 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003946 return HexagonII::HSIG_A;
3947 break;
3948 case Hexagon::A4_combineri:
3949 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003950 DstReg = MI.getOperand(0).getReg();
3951 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003952 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003953 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3954 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003955 return HexagonII::HSIG_A;
3956 break;
3957 case Hexagon::A4_combineir:
3958 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003959 DstReg = MI.getOperand(0).getReg();
3960 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003961 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003962 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3963 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003964 return HexagonII::HSIG_A;
3965 break;
3966 case Hexagon::A2_sxtb:
3967 case Hexagon::A2_sxth:
3968 case Hexagon::A2_zxtb:
3969 case Hexagon::A2_zxth:
3970 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003971 DstReg = MI.getOperand(0).getReg();
3972 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003973 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3974 return HexagonII::HSIG_A;
3975 break;
3976 }
3977
3978 return HexagonII::HSIG_None;
3979}
3980
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003981short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3982 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003983}
3984
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003985unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003986 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003987 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3988 // still have a MinLatency property, which getStageLatency checks.
3989 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003990 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003991
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003992 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003993 return 0;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003994 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
3995}
3996
3997/// getOperandLatency - Compute and return the use operand latency of a given
3998/// pair of def and use.
3999/// In most cases, the static scheduling itinerary was enough to determine the
4000/// operand latency. But it may not be possible for instructions with variable
4001/// number of defs / uses.
4002///
4003/// This is a raw interface to the itinerary that may be directly overriden by
4004/// a target. Use computeOperandLatency to get the best estimate of latency.
4005int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4006 const MachineInstr &DefMI,
4007 unsigned DefIdx,
4008 const MachineInstr &UseMI,
4009 unsigned UseIdx) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004010 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004011
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004012 // Get DefIdx and UseIdx for super registers.
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00004013 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004014
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00004015 if (DefMO.isReg() && HRI.isPhysicalRegister(DefMO.getReg())) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004016 if (DefMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004017 for (MCSuperRegIterator SR(DefMO.getReg(), &HRI); SR.isValid(); ++SR) {
4018 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004019 if (Idx != -1) {
4020 DefIdx = Idx;
4021 break;
4022 }
4023 }
4024 }
4025
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00004026 const MachineOperand &UseMO = UseMI.getOperand(UseIdx);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004027 if (UseMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004028 for (MCSuperRegIterator SR(UseMO.getReg(), &HRI); SR.isValid(); ++SR) {
4029 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004030 if (Idx != -1) {
4031 UseIdx = Idx;
4032 break;
4033 }
4034 }
4035 }
4036 }
4037
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004038 int Latency = TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
4039 UseMI, UseIdx);
4040 if (!Latency)
4041 // We should never have 0 cycle latency between two instructions unless
4042 // they can be packetized together. However, this decision can't be made
4043 // here.
4044 Latency = 1;
4045 return Latency;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004046}
4047
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004048// inverts the predication logic.
4049// p -> NotP
4050// NotP -> P
4051bool HexagonInstrInfo::getInvertedPredSense(
4052 SmallVectorImpl<MachineOperand> &Cond) const {
4053 if (Cond.empty())
4054 return false;
4055 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4056 Cond[0].setImm(Opc);
4057 return true;
4058}
4059
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004060unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4061 int InvPredOpcode;
4062 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4063 : Hexagon::getTruePredOpcode(Opc);
4064 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4065 return InvPredOpcode;
4066
4067 llvm_unreachable("Unexpected predicated instruction");
4068}
4069
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004070// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004071int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4072 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004073 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4074 & HexagonII::ExtentSignedMask;
4075 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4076 & HexagonII::ExtentBitsMask;
4077
4078 if (isSigned) // if value is signed
4079 return ~(-1U << (bits - 1));
4080 else
4081 return ~(-1U << bits);
4082}
4083
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004084
4085bool HexagonInstrInfo::isAddrModeWithOffset(const MachineInstr &MI) const {
4086 switch (MI.getOpcode()) {
4087 case Hexagon::L2_loadrbgp:
4088 case Hexagon::L2_loadrdgp:
4089 case Hexagon::L2_loadrhgp:
4090 case Hexagon::L2_loadrigp:
4091 case Hexagon::L2_loadrubgp:
4092 case Hexagon::L2_loadruhgp:
4093 case Hexagon::S2_storerbgp:
4094 case Hexagon::S2_storerbnewgp:
4095 case Hexagon::S2_storerhgp:
4096 case Hexagon::S2_storerhnewgp:
4097 case Hexagon::S2_storerigp:
4098 case Hexagon::S2_storerinewgp:
4099 case Hexagon::S2_storerdgp:
4100 case Hexagon::S2_storerfgp:
4101 return true;
4102 }
4103 const uint64_t F = MI.getDesc().TSFlags;
4104 unsigned addrMode =
4105 ((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
4106 // Disallow any base+offset instruction. The assembler does not yet reorder
4107 // based up any zero offset instruction.
4108 return (addrMode == HexagonII::BaseRegOffset ||
4109 addrMode == HexagonII::BaseImmOffset ||
4110 addrMode == HexagonII::BaseLongOffset);
4111}
4112
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004113unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004114 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00004115
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004116 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004117 unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
4118 unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
4119 if (Size != 0)
4120 return Size;
4121
4122 // Handle vector access sizes.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004123 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004124 switch (S) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004125 case HexagonII::HVXVectorAccess:
4126 return HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004127 default:
4128 llvm_unreachable("Unexpected instruction");
4129 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004130}
4131
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004132// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004133int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4134 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004135 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4136 & HexagonII::ExtentSignedMask;
4137 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4138 & HexagonII::ExtentBitsMask;
4139
4140 if (isSigned) // if value is signed
4141 return -1U << (bits - 1);
4142 else
4143 return 0;
4144}
4145
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004146// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004147short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004148 // Check if the instruction has a register form that uses register in place
4149 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004150 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004151 if (NonExtOpcode >= 0)
4152 return NonExtOpcode;
4153
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004154 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004155 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004156 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00004157 case HexagonII::Absolute:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004158 return Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Eugene Zelenko3b873362017-09-28 22:27:31 +00004159 case HexagonII::BaseImmOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004160 return Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004161 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004162 return Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004163
Jyotsna Verma84256432013-03-01 17:37:13 +00004164 default:
4165 return -1;
4166 }
4167 }
4168 return -1;
4169}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004170
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004171bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004172 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004173 if (Cond.empty())
4174 return false;
4175 assert(Cond.size() == 2);
4176 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004177 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4178 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004179 }
4180 PredReg = Cond[1].getReg();
4181 PredRegPos = 1;
4182 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4183 PredRegFlags = 0;
4184 if (Cond[1].isImplicit())
4185 PredRegFlags = RegState::Implicit;
4186 if (Cond[1].isUndef())
4187 PredRegFlags |= RegState::Undef;
4188 return true;
4189}
4190
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004191short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4192 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004193}
4194
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004195short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4196 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004197}
4198
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004199// Return the number of bytes required to encode the instruction.
4200// Hexagon instructions are fixed length, 4 bytes, unless they
4201// use a constant extender, which requires another 4 bytes.
4202// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004203unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4204 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004205 return 0;
4206
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004207 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004208 if (!Size)
4209 // Assume the default insn size in case it cannot be determined
4210 // for whatever reason.
4211 Size = HEXAGON_INSTR_SIZE;
4212
4213 if (isConstExtended(MI) || isExtended(MI))
4214 Size += HEXAGON_INSTR_SIZE;
4215
4216 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004217 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4218 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004219 const MachineFunction *MF = MBB.getParent();
4220 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4221
4222 // Count the number of register definitions to find the asm string.
4223 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004224 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004225 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004226 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004227
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004228 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004229 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004230 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004231 Size = getInlineAsmLength(AsmStr, *MAI);
4232 }
4233
4234 return Size;
4235}
4236
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004237uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4238 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004239 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4240}
4241
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004242unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004243 const InstrItineraryData &II = *Subtarget.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004244 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004245
4246 return IS.getUnits();
4247}
4248
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004249// Calculate size of the basic block without debug instructions.
4250unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4251 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4252}
4253
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004254unsigned HexagonInstrInfo::nonDbgBundleSize(
4255 MachineBasicBlock::const_iterator BundleHead) const {
4256 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004257 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004258 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004259 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004260}
4261
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004262/// immediateExtend - Changes the instruction in place to one using an immediate
4263/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004264void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004265 assert((isExtendable(MI)||isConstExtended(MI)) &&
4266 "Instruction must be extendable");
4267 // Find which operand is extendable.
4268 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004269 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004270 // This needs to be something we understand.
4271 assert((MO.isMBB() || MO.isImm()) &&
4272 "Branch with unknown extendable field type");
4273 // Mark given operand as extended.
4274 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4275}
4276
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004277bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004278 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00004279 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to "
4280 << printMBBReference(*NewTarget);
4281 MI.dump(););
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004282 assert(MI.isBranch());
4283 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4284 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004285 // In general branch target is the last operand,
4286 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004287 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004288 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004289 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4290 MI.getOperand(TargetPos).setMBB(NewTarget);
4291 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004292 NewOpcode = reversePrediction(NewOpcode);
4293 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004294 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004295 return true;
4296}
4297
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004298void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4299 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4300 MachineFunction::iterator A = MF.begin();
4301 MachineBasicBlock &B = *A;
4302 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004303 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004304 MachineInstr *NewMI;
4305
4306 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4307 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004308 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004309 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4310 " Class: " << NewMI->getDesc().getSchedClass());
4311 NewMI->eraseFromParent();
4312 }
4313 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4314}
4315
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004316// inverts the predication logic.
4317// p -> NotP
4318// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004319bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4320 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4321 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004322 return true;
4323}
4324
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004325// Reverse the branch prediction.
4326unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4327 int PredRevOpcode = -1;
4328 if (isPredictedTaken(Opcode))
4329 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4330 else
4331 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4332 assert(PredRevOpcode > 0);
4333 return PredRevOpcode;
4334}
4335
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004336// TODO: Add more rigorous validation.
4337bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4338 const {
4339 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4340}
4341
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004342void HexagonInstrInfo::
4343setBundleNoShuf(MachineBasicBlock::instr_iterator MIB) const {
4344 assert(MIB->isBundle());
4345 MachineOperand &Operand = MIB->getOperand(0);
4346 if (Operand.isImm())
4347 Operand.setImm(Operand.getImm() | memShufDisabledMask);
4348 else
4349 MIB->addOperand(MachineOperand::CreateImm(memShufDisabledMask));
4350}
4351
4352bool HexagonInstrInfo::getBundleNoShuf(const MachineInstr &MIB) const {
4353 assert(MIB.isBundle());
4354 const MachineOperand &Operand = MIB.getOperand(0);
4355 return (Operand.isImm() && (Operand.getImm() & memShufDisabledMask) != 0);
4356}
4357
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004358// Addressing mode relations.
4359short HexagonInstrInfo::changeAddrMode_abs_io(short Opc) const {
4360 return Opc >= 0 ? Hexagon::changeAddrMode_abs_io(Opc) : Opc;
4361}
4362
4363short HexagonInstrInfo::changeAddrMode_io_abs(short Opc) const {
4364 return Opc >= 0 ? Hexagon::changeAddrMode_io_abs(Opc) : Opc;
4365}
4366
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004367short HexagonInstrInfo::changeAddrMode_io_pi(short Opc) const {
4368 return Opc >= 0 ? Hexagon::changeAddrMode_io_pi(Opc) : Opc;
4369}
4370
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004371short HexagonInstrInfo::changeAddrMode_io_rr(short Opc) const {
4372 return Opc >= 0 ? Hexagon::changeAddrMode_io_rr(Opc) : Opc;
4373}
4374
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004375short HexagonInstrInfo::changeAddrMode_pi_io(short Opc) const {
4376 return Opc >= 0 ? Hexagon::changeAddrMode_pi_io(Opc) : Opc;
4377}
4378
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004379short HexagonInstrInfo::changeAddrMode_rr_io(short Opc) const {
4380 return Opc >= 0 ? Hexagon::changeAddrMode_rr_io(Opc) : Opc;
4381}
4382
4383short HexagonInstrInfo::changeAddrMode_rr_ur(short Opc) const {
4384 return Opc >= 0 ? Hexagon::changeAddrMode_rr_ur(Opc) : Opc;
4385}
4386
4387short HexagonInstrInfo::changeAddrMode_ur_rr(short Opc) const {
4388 return Opc >= 0 ? Hexagon::changeAddrMode_ur_rr(Opc) : Opc;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004389}