blob: 545dd15dde22a41149868adb69ec5bd66f7f4a5b [file] [log] [blame]
Eugene Zelenko3b873362017-09-28 22:27:31 +00001//===- HexagonInstrInfo.cpp - Hexagon Instruction Information -------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tony Linthicum1213a7a2011-12-12 21:14:40 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the Hexagon implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth6bda14b2017-06-06 11:49:48 +000013#include "HexagonInstrInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000014#include "Hexagon.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000015#include "HexagonFrameLowering.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000016#include "HexagonHazardRecognizer.h"
Craig Topperb25fda92012-03-17 18:46:09 +000017#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "HexagonSubtarget.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000019#include "llvm/ADT/ArrayRef.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000020#include "llvm/ADT/SmallPtrSet.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000022#include "llvm/ADT/StringRef.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000023#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000024#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000025#include "llvm/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000031#include "llvm/CodeGen/MachineInstrBundle.h"
32#include "llvm/CodeGen/MachineLoopInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000034#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000036#include "llvm/CodeGen/ScheduleDAG.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetOpcodes.h"
39#include "llvm/CodeGen/TargetRegisterInfo.h"
40#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000041#include "llvm/IR/DebugLoc.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000042#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000043#include "llvm/MC/MCInstrDesc.h"
44#include "llvm/MC/MCInstrItineraries.h"
45#include "llvm/MC/MCRegisterInfo.h"
46#include "llvm/Support/BranchProbability.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000047#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000048#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000049#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000050#include "llvm/Support/MachineValueType.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000051#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000052#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000053#include "llvm/Target/TargetMachine.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000054#include <cassert>
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000055#include <cctype>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000056#include <cstdint>
57#include <cstring>
58#include <iterator>
Eugene Zelenko3b873362017-09-28 22:27:31 +000059#include <string>
60#include <utility>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000061
Tony Linthicum1213a7a2011-12-12 21:14:40 +000062using namespace llvm;
63
Chandler Carruthe96dd892014-04-21 22:55:11 +000064#define DEBUG_TYPE "hexagon-instrinfo"
65
Chandler Carruthd174b722014-04-22 02:03:14 +000066#define GET_INSTRINFO_CTOR_DTOR
67#define GET_INSTRMAP_INFO
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +000068#include "HexagonDepTimingClasses.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000069#include "HexagonGenDFAPacketizer.inc"
70#include "HexagonGenInstrInfo.inc"
Chandler Carruthd174b722014-04-22 02:03:14 +000071
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000072cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000073 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
74 "packetization boundary."));
75
76static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
77 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
78
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000079static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
80 cl::Hidden, cl::ZeroOrMore, cl::init(false),
81 cl::desc("Disable schedule adjustment for new value stores."));
82
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000083static cl::opt<bool> EnableTimingClassLatency(
84 "enable-timing-class-latency", cl::Hidden, cl::init(false),
85 cl::desc("Enable timing class latency"));
86
87static cl::opt<bool> EnableALUForwarding(
88 "enable-alu-forwarding", cl::Hidden, cl::init(true),
89 cl::desc("Enable vec alu forwarding"));
90
91static cl::opt<bool> EnableACCForwarding(
92 "enable-acc-forwarding", cl::Hidden, cl::init(true),
93 cl::desc("Enable vec acc forwarding"));
94
95static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
96 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
97
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000098static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
99 cl::init(true), cl::Hidden, cl::ZeroOrMore,
100 cl::desc("Use the DFA based hazard recognizer."));
101
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102/// Constants for Hexagon instructions.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000104const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000106const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000107const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000108const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000110const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000112const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000113
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000114// Pin the vtable to this file.
115void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000116
117HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000118 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
119 Subtarget(ST) {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000120
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000121static bool isIntRegForSubInst(unsigned Reg) {
122 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
123 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000124}
125
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000126static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000127 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
128 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129}
130
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000131/// Calculate number of instructions excluding the debug instructions.
132static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
133 MachineBasicBlock::const_instr_iterator MIE) {
134 unsigned Count = 0;
135 for (; MIB != MIE; ++MIB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000136 if (!MIB->isDebugInstr())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000137 ++Count;
138 }
139 return Count;
140}
141
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000142/// Find the hardware loop instruction used to set-up the specified loop.
143/// On Hexagon, we have two instructions used to set-up the hardware loop
144/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
145/// to indicate the end of a loop.
Krzysztof Parzyszek998df2c2018-03-23 20:43:02 +0000146MachineInstr *HexagonInstrInfo::findLoopInstr(MachineBasicBlock *BB,
147 unsigned EndLoopOp, MachineBasicBlock *TargetBB,
148 SmallPtrSet<MachineBasicBlock *, 8> &Visited) const {
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000149 unsigned LOOPi;
150 unsigned LOOPr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000151 if (EndLoopOp == Hexagon::ENDLOOP0) {
152 LOOPi = Hexagon::J2_loop0i;
153 LOOPr = Hexagon::J2_loop0r;
154 } else { // EndLoopOp == Hexagon::EndLOOP1
155 LOOPi = Hexagon::J2_loop1i;
156 LOOPr = Hexagon::J2_loop1r;
157 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000158
Brendon Cahoondf43e682015-05-08 16:16:29 +0000159 // The loop set-up instruction will be in a predecessor block
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000160 for (MachineBasicBlock *PB : BB->predecessors()) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000161 // If this has been visited, already skip it.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000162 if (!Visited.insert(PB).second)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000163 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000164 if (PB == BB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000165 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000166 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
167 unsigned Opc = I->getOpcode();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000168 if (Opc == LOOPi || Opc == LOOPr)
169 return &*I;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000170 // We've reached a different loop, which means the loop01 has been
171 // removed.
172 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000173 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000174 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000175 // Check the predecessors for the LOOP instruction.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000176 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
177 return Loop;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000178 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000179 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000180}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000181
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000182/// Gather register def/uses from MI.
183/// This treats possible (predicated) defs as actually happening ones
184/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000185static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000186 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
187 Defs.clear();
188 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000189
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000190 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
191 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000192
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000193 if (!MO.isReg())
194 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000195
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000196 unsigned Reg = MO.getReg();
197 if (!Reg)
198 continue;
199
200 if (MO.isUse())
201 Uses.push_back(MO.getReg());
202
203 if (MO.isDef())
204 Defs.push_back(MO.getReg());
205 }
206}
207
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000208// Position dependent, so check twice for swap.
209static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
210 switch (Ga) {
211 case HexagonII::HSIG_None:
212 default:
213 return false;
214 case HexagonII::HSIG_L1:
215 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
216 case HexagonII::HSIG_L2:
217 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
218 Gb == HexagonII::HSIG_A);
219 case HexagonII::HSIG_S1:
220 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
221 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
222 case HexagonII::HSIG_S2:
223 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
224 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
225 Gb == HexagonII::HSIG_A);
226 case HexagonII::HSIG_A:
227 return (Gb == HexagonII::HSIG_A);
228 case HexagonII::HSIG_Compound:
229 return (Gb == HexagonII::HSIG_Compound);
230 }
231 return false;
232}
233
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000234/// isLoadFromStackSlot - If the specified machine instruction is a direct
235/// load from a stack slot, return the virtual or physical register number of
236/// the destination along with the FrameIndex of the loaded stack slot. If
237/// not, return 0. This predicate must return 0 if the instruction has
238/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000239unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000240 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000241 switch (MI.getOpcode()) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000242 default:
243 break;
244 case Hexagon::L2_loadri_io:
245 case Hexagon::L2_loadrd_io:
246 case Hexagon::V6_vL32b_ai:
247 case Hexagon::V6_vL32b_nt_ai:
248 case Hexagon::V6_vL32Ub_ai:
249 case Hexagon::LDriw_pred:
250 case Hexagon::LDriw_ctr:
251 case Hexagon::PS_vloadrq_ai:
252 case Hexagon::PS_vloadrw_ai:
253 case Hexagon::PS_vloadrw_nt_ai: {
254 const MachineOperand OpFI = MI.getOperand(1);
255 if (!OpFI.isFI())
256 return 0;
257 const MachineOperand OpOff = MI.getOperand(2);
258 if (!OpOff.isImm() || OpOff.getImm() != 0)
259 return 0;
260 FrameIndex = OpFI.getIndex();
261 return MI.getOperand(0).getReg();
262 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000263
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000264 case Hexagon::L2_ploadrit_io:
265 case Hexagon::L2_ploadrif_io:
266 case Hexagon::L2_ploadrdt_io:
267 case Hexagon::L2_ploadrdf_io: {
268 const MachineOperand OpFI = MI.getOperand(2);
269 if (!OpFI.isFI())
270 return 0;
271 const MachineOperand OpOff = MI.getOperand(3);
272 if (!OpOff.isImm() || OpOff.getImm() != 0)
273 return 0;
274 FrameIndex = OpFI.getIndex();
275 return MI.getOperand(0).getReg();
276 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000277 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000278
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000279 return 0;
280}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000281
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000282/// isStoreToStackSlot - If the specified machine instruction is a direct
283/// store to a stack slot, return the virtual or physical register number of
284/// the source reg along with the FrameIndex of the loaded stack slot. If
285/// not, return 0. This predicate must return 0 if the instruction has
286/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000287unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000288 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000289 switch (MI.getOpcode()) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000290 default:
291 break;
292 case Hexagon::S2_storerb_io:
293 case Hexagon::S2_storerh_io:
294 case Hexagon::S2_storeri_io:
295 case Hexagon::S2_storerd_io:
296 case Hexagon::V6_vS32b_ai:
297 case Hexagon::V6_vS32Ub_ai:
298 case Hexagon::STriw_pred:
299 case Hexagon::STriw_ctr:
300 case Hexagon::PS_vstorerq_ai:
301 case Hexagon::PS_vstorerw_ai: {
302 const MachineOperand &OpFI = MI.getOperand(0);
303 if (!OpFI.isFI())
304 return 0;
305 const MachineOperand &OpOff = MI.getOperand(1);
306 if (!OpOff.isImm() || OpOff.getImm() != 0)
307 return 0;
308 FrameIndex = OpFI.getIndex();
309 return MI.getOperand(2).getReg();
310 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000311
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +0000312 case Hexagon::S2_pstorerbt_io:
313 case Hexagon::S2_pstorerbf_io:
314 case Hexagon::S2_pstorerht_io:
315 case Hexagon::S2_pstorerhf_io:
316 case Hexagon::S2_pstorerit_io:
317 case Hexagon::S2_pstorerif_io:
318 case Hexagon::S2_pstorerdt_io:
319 case Hexagon::S2_pstorerdf_io: {
320 const MachineOperand &OpFI = MI.getOperand(1);
321 if (!OpFI.isFI())
322 return 0;
323 const MachineOperand &OpOff = MI.getOperand(2);
324 if (!OpOff.isImm() || OpOff.getImm() != 0)
325 return 0;
326 FrameIndex = OpFI.getIndex();
327 return MI.getOperand(3).getReg();
328 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000329 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000330
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000331 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000332}
333
Krzysztof Parzyszek275ffa42018-01-23 19:08:40 +0000334/// This function checks if the instruction or bundle of instructions
335/// has load from stack slot and returns frameindex and machine memory
336/// operand of that instruction if true.
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000337bool HexagonInstrInfo::hasLoadFromStackSlot(
338 const MachineInstr &MI,
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000339 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
Krzysztof Parzyszek275ffa42018-01-23 19:08:40 +0000340 if (MI.isBundle()) {
341 const MachineBasicBlock *MBB = MI.getParent();
342 MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
343 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000344 if (TargetInstrInfo::hasLoadFromStackSlot(*MII, Accesses))
Krzysztof Parzyszek275ffa42018-01-23 19:08:40 +0000345 return true;
346 return false;
347 }
348
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000349 return TargetInstrInfo::hasLoadFromStackSlot(MI, Accesses);
Krzysztof Parzyszek275ffa42018-01-23 19:08:40 +0000350}
351
352/// This function checks if the instruction or bundle of instructions
353/// has store to stack slot and returns frameindex and machine memory
354/// operand of that instruction if true.
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000355bool HexagonInstrInfo::hasStoreToStackSlot(
356 const MachineInstr &MI,
Sander de Smalenc91b27d2018-09-05 08:59:50 +0000357 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
Krzysztof Parzyszek275ffa42018-01-23 19:08:40 +0000358 if (MI.isBundle()) {
359 const MachineBasicBlock *MBB = MI.getParent();
360 MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
361 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000362 if (TargetInstrInfo::hasStoreToStackSlot(*MII, Accesses))
Krzysztof Parzyszek275ffa42018-01-23 19:08:40 +0000363 return true;
364 return false;
365 }
366
Sander de Smalen6cab60f2018-09-03 09:15:58 +0000367 return TargetInstrInfo::hasStoreToStackSlot(MI, Accesses);
Krzysztof Parzyszek275ffa42018-01-23 19:08:40 +0000368}
369
Brendon Cahoondf43e682015-05-08 16:16:29 +0000370/// This function can analyze one/two way branching only and should (mostly) be
371/// called by target independent side.
372/// First entry is always the opcode of the branching instruction, except when
373/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
374/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
375/// e.g. Jump_c p will have
376/// Cond[0] = Jump_c
377/// Cond[1] = p
378/// HW-loop ENDLOOP:
379/// Cond[0] = ENDLOOP
380/// Cond[1] = MBB
381/// New value jump:
382/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
383/// Cond[1] = R
384/// Cond[2] = Imm
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000385bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000386 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000387 MachineBasicBlock *&FBB,
388 SmallVectorImpl<MachineOperand> &Cond,
389 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000390 TBB = nullptr;
391 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000392 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000393
394 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000395 MachineBasicBlock::instr_iterator I = MBB.instr_end();
396 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000397 return false;
398
399 // A basic block may looks like this:
400 //
401 // [ insn
402 // EH_LABEL
403 // insn
404 // insn
405 // insn
406 // EH_LABEL
407 // insn ]
408 //
409 // It has two succs but does not have a terminator
410 // Don't know how to handle it.
411 do {
412 --I;
413 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000414 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000415 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000416 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000418 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000419 --I;
420
Shiva Chen801bf7e2018-05-09 02:42:00 +0000421 while (I->isDebugInstr()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000422 if (I == MBB.instr_begin())
423 return false;
424 --I;
425 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000426
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000427 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
428 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000429 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000430 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000431 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000432 LLVM_DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000433 I->eraseFromParent();
434 I = MBB.instr_end();
435 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000436 return false;
437 --I;
438 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000439 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000440 return false;
441
442 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000443 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000444 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000445 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000446 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000447 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000448 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000449 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000450 else
451 // This is a third branch.
452 return true;
453 }
454 if (I == MBB.instr_begin())
455 break;
456 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000457 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000458
459 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000460 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
461 // If the branch target is not a basic block, it could be a tail call.
462 // (It is, if the target is a function.)
463 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
464 return true;
465 if (SecLastOpcode == Hexagon::J2_jump &&
466 !SecondLastInst->getOperand(0).isMBB())
467 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000468
469 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000470 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000471
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000472 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
473 return true;
474
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000475 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000476 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000477 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000478 TBB = LastInst->getOperand(0).getMBB();
479 return false;
480 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000481 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000482 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000483 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000484 Cond.push_back(LastInst->getOperand(0));
485 return false;
486 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000487 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000488 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000489 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000490 Cond.push_back(LastInst->getOperand(0));
491 return false;
492 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000493 // Only supporting rr/ri versions of new-value jumps.
494 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
495 TBB = LastInst->getOperand(2).getMBB();
496 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
497 Cond.push_back(LastInst->getOperand(0));
498 Cond.push_back(LastInst->getOperand(1));
499 return false;
500 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000501 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
502 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000503 // Otherwise, don't know what this is.
504 return true;
505 }
506
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000507 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000508 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000509 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000510 if (!SecondLastInst->getOperand(1).isMBB())
511 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000512 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000513 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000514 Cond.push_back(SecondLastInst->getOperand(0));
515 FBB = LastInst->getOperand(0).getMBB();
516 return false;
517 }
518
Brendon Cahoondf43e682015-05-08 16:16:29 +0000519 // Only supporting rr/ri versions of new-value jumps.
520 if (SecLastOpcodeHasNVJump &&
521 (SecondLastInst->getNumExplicitOperands() == 3) &&
522 (LastOpcode == Hexagon::J2_jump)) {
523 TBB = SecondLastInst->getOperand(2).getMBB();
524 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
525 Cond.push_back(SecondLastInst->getOperand(0));
526 Cond.push_back(SecondLastInst->getOperand(1));
527 FBB = LastInst->getOperand(0).getMBB();
528 return false;
529 }
530
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000531 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
532 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000533 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000534 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000535 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000536 if (AllowModify)
537 I->eraseFromParent();
538 return false;
539 }
540
Brendon Cahoondf43e682015-05-08 16:16:29 +0000541 // If the block ends with an ENDLOOP, and J2_jump, handle it.
542 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000543 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000544 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000545 Cond.push_back(SecondLastInst->getOperand(0));
546 FBB = LastInst->getOperand(0).getMBB();
547 return false;
548 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000549 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
550 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000551 // Otherwise, can't handle this.
552 return true;
553}
554
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000555unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000556 int *BytesRemoved) const {
557 assert(!BytesRemoved && "code size not handled");
558
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000559 LLVM_DEBUG(dbgs() << "\nRemoving branches out of " << printMBBReference(MBB));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000560 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000561 unsigned Count = 0;
562 while (I != MBB.begin()) {
563 --I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000564 if (I->isDebugInstr())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000565 continue;
566 // Only removing branches from end of MBB.
567 if (!I->isBranch())
568 return Count;
569 if (Count && (I->getOpcode() == Hexagon::J2_jump))
570 llvm_unreachable("Malformed basic block: unconditional branch not last");
571 MBB.erase(&MBB.back());
572 I = MBB.end();
573 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000574 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000575 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000576}
577
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000578unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000579 MachineBasicBlock *TBB,
580 MachineBasicBlock *FBB,
581 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000582 const DebugLoc &DL,
583 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000584 unsigned BOpc = Hexagon::J2_jump;
585 unsigned BccOpc = Hexagon::J2_jumpt;
586 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000587 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000588 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000589
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000590 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000591 // If we want to reverse the branch an odd number of times, we want
592 // J2_jumpf.
593 if (!Cond.empty() && Cond[0].isImm())
594 BccOpc = Cond[0].getImm();
595
596 if (!FBB) {
597 if (Cond.empty()) {
598 // Due to a bug in TailMerging/CFG Optimization, we need to add a
599 // special case handling of a predicated jump followed by an
600 // unconditional jump. If not, Tail Merging and CFG Optimization go
601 // into an infinite loop.
602 MachineBasicBlock *NewTBB, *NewFBB;
603 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000604 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000605 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000606 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
607 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000608 reverseBranchCondition(Cond);
609 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000610 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000611 }
612 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
613 } else if (isEndLoopN(Cond[0].getImm())) {
614 int EndLoopOp = Cond[0].getImm();
615 assert(Cond[1].isMBB());
616 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
617 // Check for it, and change the BB target if needed.
618 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000619 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
620 VisitedBBs);
Eugene Zelenko3b873362017-09-28 22:27:31 +0000621 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000622 Loop->getOperand(0).setMBB(TBB);
623 // Add the ENDLOOP after the finding the LOOP0.
624 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
625 } else if (isNewValueJump(Cond[0].getImm())) {
626 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
627 // New value jump
628 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
629 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
630 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000631 LLVM_DEBUG(dbgs() << "\nInserting NVJump for "
632 << 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 Parzyszek62c48052018-04-05 14:25:52 +00001019 auto RealCirc = [&](unsigned Opc, bool HasImm, unsigned MxOp) {
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001020 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
Chandler Carruthc73c0302018-08-16 21:30:05 +00001088 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc))
1089 .add(MI.getOperand(0))
1090 .addImm(MI.getOperand(1).getImm())
1091 .addReg(SrcSubLo)
1092 .cloneMemRefs(MI);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001093 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001094 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001095 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001096 // The Vectors are indexed in multiples of vector size.
1097 .addImm(MI.getOperand(1).getImm() + Offset)
1098 .addReg(SrcSubHi)
Chandler Carruthc73c0302018-08-16 21:30:05 +00001099 .cloneMemRefs(MI);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001100 MBB.erase(MI);
1101 return true;
1102 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001103 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001104 case Hexagon::PS_vloadrwu_ai: {
1105 bool Aligned = Opc == Hexagon::PS_vloadrw_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001106 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001107 unsigned NewOpc = Aligned ? Hexagon::V6_vL32b_ai : Hexagon::V6_vL32Ub_ai;
1108 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1109
Diana Picus116bbab2017-01-13 09:58:52 +00001110 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1111 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Chandler Carruthc73c0302018-08-16 21:30:05 +00001112 .add(MI.getOperand(1))
1113 .addImm(MI.getOperand(2).getImm())
1114 .cloneMemRefs(MI);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001115 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001116 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1117 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001118 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001119 .addImm(MI.getOperand(2).getImm() + Offset)
Chandler Carruthc73c0302018-08-16 21:30:05 +00001120 .cloneMemRefs(MI);
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001121 MBB.erase(MI);
1122 return true;
1123 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001124 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001125 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001126 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1127 .addReg(Reg, RegState::Undef)
1128 .addReg(Reg, RegState::Undef);
1129 MBB.erase(MI);
1130 return true;
1131 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001132 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001133 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001134 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1135 .addReg(Reg, RegState::Undef)
1136 .addReg(Reg, RegState::Undef);
1137 MBB.erase(MI);
1138 return true;
1139 }
Krzysztof Parzyszek9b48e8d2018-02-09 19:10:46 +00001140 case Hexagon::PS_qtrue: {
1141 BuildMI(MBB, MI, DL, get(Hexagon::V6_veqw), MI.getOperand(0).getReg())
1142 .addReg(Hexagon::V0, RegState::Undef)
1143 .addReg(Hexagon::V0, RegState::Undef);
1144 MBB.erase(MI);
1145 return true;
1146 }
1147 case Hexagon::PS_qfalse: {
1148 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgtw), MI.getOperand(0).getReg())
1149 .addReg(Hexagon::V0, RegState::Undef)
1150 .addReg(Hexagon::V0, RegState::Undef);
1151 MBB.erase(MI);
1152 return true;
1153 }
Krzysztof Parzyszekc1e712b2018-06-06 19:34:40 +00001154 case Hexagon::PS_vdd0: {
1155 unsigned Vd = MI.getOperand(0).getReg();
1156 BuildMI(MBB, MI, DL, get(Hexagon::V6_vsubw_dv), Vd)
1157 .addReg(Vd, RegState::Undef)
1158 .addReg(Vd, RegState::Undef);
1159 MBB.erase(MI);
1160 return true;
1161 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001162 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001163 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001164 unsigned DstReg = MI.getOperand(0).getReg();
1165 unsigned Src1Reg = MI.getOperand(1).getReg();
1166 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001167 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1168 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1169 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1170 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001171 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001172 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001173 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001174 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001175 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001176 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001177 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001178 .addReg(Src2SubLo);
1179 MBB.erase(MI);
1180 MRI.clearKillFlags(Src1SubHi);
1181 MRI.clearKillFlags(Src1SubLo);
1182 MRI.clearKillFlags(Src2SubHi);
1183 MRI.clearKillFlags(Src2SubLo);
1184 return true;
1185 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001186 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001187 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001188 unsigned DstReg = MI.getOperand(0).getReg();
1189 unsigned Src1Reg = MI.getOperand(1).getReg();
1190 unsigned Src2Reg = MI.getOperand(2).getReg();
1191 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001192 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1193 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1194 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1195 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1196 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1197 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001198 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001199 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001200 .addReg(Src1SubHi)
1201 .addReg(Src2SubHi)
1202 .addReg(Src3SubHi);
1203 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001204 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001205 .addReg(Src1SubLo)
1206 .addReg(Src2SubLo)
1207 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001208 MBB.erase(MI);
1209 MRI.clearKillFlags(Src1SubHi);
1210 MRI.clearKillFlags(Src1SubLo);
1211 MRI.clearKillFlags(Src2SubHi);
1212 MRI.clearKillFlags(Src2SubLo);
1213 MRI.clearKillFlags(Src3SubHi);
1214 MRI.clearKillFlags(Src3SubLo);
1215 return true;
1216 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001217 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001218 const MachineOperand &Op0 = MI.getOperand(0);
1219 const MachineOperand &Op1 = MI.getOperand(1);
1220 const MachineOperand &Op2 = MI.getOperand(2);
1221 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001222 unsigned Rd = Op0.getReg();
1223 unsigned Pu = Op1.getReg();
1224 unsigned Rs = Op2.getReg();
1225 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001226 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001227 unsigned K1 = getKillRegState(Op1.isKill());
1228 unsigned K2 = getKillRegState(Op2.isKill());
1229 unsigned K3 = getKillRegState(Op3.isKill());
1230 if (Rd != Rs)
1231 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1232 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1233 .addReg(Rs, K2);
1234 if (Rd != Rt)
1235 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1236 .addReg(Pu, K1)
1237 .addReg(Rt, K3);
1238 MBB.erase(MI);
1239 return true;
1240 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001241 case Hexagon::PS_vselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001242 const MachineOperand &Op0 = MI.getOperand(0);
1243 const MachineOperand &Op1 = MI.getOperand(1);
1244 const MachineOperand &Op2 = MI.getOperand(2);
1245 const MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001246 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001247 getLiveRegsAt(LiveAtMI, MI);
1248 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001249 unsigned PReg = Op1.getReg();
1250 assert(Op1.getSubReg() == 0);
1251 unsigned PState = getRegState(Op1);
1252
Ron Lieberman88159e52016-09-02 22:56:24 +00001253 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001254 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1255 : PState;
Ron Lieberman88159e52016-09-02 22:56:24 +00001256 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001257 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001258 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001259 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001260 if (IsDestLive)
1261 T.addReg(Op0.getReg(), RegState::Implicit);
1262 IsDestLive = true;
1263 }
1264 if (Op0.getReg() != Op3.getReg()) {
1265 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001266 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001267 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001268 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001269 if (IsDestLive)
1270 T.addReg(Op0.getReg(), RegState::Implicit);
1271 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001272 MBB.erase(MI);
1273 return true;
1274 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001275 case Hexagon::PS_wselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001276 MachineOperand &Op0 = MI.getOperand(0);
1277 MachineOperand &Op1 = MI.getOperand(1);
1278 MachineOperand &Op2 = MI.getOperand(2);
1279 MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001280 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001281 getLiveRegsAt(LiveAtMI, MI);
1282 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001283 unsigned PReg = Op1.getReg();
1284 assert(Op1.getSubReg() == 0);
1285 unsigned PState = getRegState(Op1);
Ron Lieberman88159e52016-09-02 22:56:24 +00001286
1287 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001288 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1289 : PState;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001290 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1291 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001292 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001293 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001294 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001295 .addReg(SrcHi)
1296 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001297 if (IsDestLive)
1298 T.addReg(Op0.getReg(), RegState::Implicit);
1299 IsDestLive = true;
1300 }
1301 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001302 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1303 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001304 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001305 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001306 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001307 .addReg(SrcHi)
1308 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001309 if (IsDestLive)
1310 T.addReg(Op0.getReg(), RegState::Implicit);
1311 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001312 MBB.erase(MI);
1313 return true;
1314 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001315
Krzysztof Parzyszekf6e875b2019-02-21 19:42:39 +00001316 case Hexagon::PS_crash: {
1317 // Generate a misaligned load that is guaranteed to cause a crash.
1318 class CrashPseudoSourceValue : public PseudoSourceValue {
1319 public:
1320 CrashPseudoSourceValue(const TargetInstrInfo &TII)
1321 : PseudoSourceValue(TargetCustom, TII) {}
1322
1323 bool isConstant(const MachineFrameInfo *) const override {
1324 return false;
1325 }
1326 bool isAliased(const MachineFrameInfo *) const override {
1327 return false;
1328 }
1329 bool mayAlias(const MachineFrameInfo *) const override {
1330 return false;
1331 }
1332 void printCustom(raw_ostream &OS) const override {
1333 OS << "MisalignedCrash";
1334 }
1335 };
1336
1337 static const CrashPseudoSourceValue CrashPSV(*this);
1338 MachineMemOperand *MMO = MF.getMachineMemOperand(
1339 MachinePointerInfo(&CrashPSV),
1340 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 8, 1);
1341 BuildMI(MBB, MI, DL, get(Hexagon::PS_loadrdabs), Hexagon::D13)
1342 .addImm(0xBADC0FEE) // Misaligned load.
1343 .addMemOperand(MMO);
1344 MBB.erase(MI);
1345 return true;
1346 }
1347
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001348 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001349 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001350 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001351 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001352 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001353 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001354 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001355 case Hexagon::PS_jmprett:
1356 MI.setDesc(get(Hexagon::J2_jumprt));
1357 return true;
1358 case Hexagon::PS_jmpretf:
1359 MI.setDesc(get(Hexagon::J2_jumprf));
1360 return true;
1361 case Hexagon::PS_jmprettnewpt:
1362 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1363 return true;
1364 case Hexagon::PS_jmpretfnewpt:
1365 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1366 return true;
1367 case Hexagon::PS_jmprettnew:
1368 MI.setDesc(get(Hexagon::J2_jumprtnew));
1369 return true;
1370 case Hexagon::PS_jmpretfnew:
1371 MI.setDesc(get(Hexagon::J2_jumprfnew));
1372 return true;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001373
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001374 case Hexagon::PS_loadrub_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001375 return RealCirc(Hexagon::L2_loadrub_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001376 case Hexagon::PS_loadrb_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001377 return RealCirc(Hexagon::L2_loadrb_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001378 case Hexagon::PS_loadruh_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001379 return RealCirc(Hexagon::L2_loadruh_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001380 case Hexagon::PS_loadrh_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001381 return RealCirc(Hexagon::L2_loadrh_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001382 case Hexagon::PS_loadri_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001383 return RealCirc(Hexagon::L2_loadri_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001384 case Hexagon::PS_loadrd_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001385 return RealCirc(Hexagon::L2_loadrd_pci, /*HasImm*/true, /*MxOp*/4);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001386 case Hexagon::PS_loadrub_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001387 return RealCirc(Hexagon::L2_loadrub_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001388 case Hexagon::PS_loadrb_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001389 return RealCirc(Hexagon::L2_loadrb_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001390 case Hexagon::PS_loadruh_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001391 return RealCirc(Hexagon::L2_loadruh_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001392 case Hexagon::PS_loadrh_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001393 return RealCirc(Hexagon::L2_loadrh_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001394 case Hexagon::PS_loadri_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001395 return RealCirc(Hexagon::L2_loadri_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001396 case Hexagon::PS_loadrd_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001397 return RealCirc(Hexagon::L2_loadrd_pcr, /*HasImm*/false, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001398 case Hexagon::PS_storerb_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001399 return RealCirc(Hexagon::S2_storerb_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001400 case Hexagon::PS_storerh_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001401 return RealCirc(Hexagon::S2_storerh_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001402 case Hexagon::PS_storerf_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001403 return RealCirc(Hexagon::S2_storerf_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001404 case Hexagon::PS_storeri_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001405 return RealCirc(Hexagon::S2_storeri_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001406 case Hexagon::PS_storerd_pci:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001407 return RealCirc(Hexagon::S2_storerd_pci, /*HasImm*/true, /*MxOp*/3);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001408 case Hexagon::PS_storerb_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001409 return RealCirc(Hexagon::S2_storerb_pcr, /*HasImm*/false, /*MxOp*/2);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001410 case Hexagon::PS_storerh_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001411 return RealCirc(Hexagon::S2_storerh_pcr, /*HasImm*/false, /*MxOp*/2);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001412 case Hexagon::PS_storerf_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001413 return RealCirc(Hexagon::S2_storerf_pcr, /*HasImm*/false, /*MxOp*/2);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001414 case Hexagon::PS_storeri_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001415 return RealCirc(Hexagon::S2_storeri_pcr, /*HasImm*/false, /*MxOp*/2);
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00001416 case Hexagon::PS_storerd_pcr:
Krzysztof Parzyszek62c48052018-04-05 14:25:52 +00001417 return RealCirc(Hexagon::S2_storerd_pcr, /*HasImm*/false, /*MxOp*/2);
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001418 }
1419
1420 return false;
1421}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001422
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +00001423MachineBasicBlock::instr_iterator
1424HexagonInstrInfo::expandVGatherPseudo(MachineInstr &MI) const {
1425 MachineBasicBlock &MBB = *MI.getParent();
1426 const DebugLoc &DL = MI.getDebugLoc();
1427 unsigned Opc = MI.getOpcode();
1428 MachineBasicBlock::iterator First;
1429
1430 switch (Opc) {
1431 case Hexagon::V6_vgathermh_pseudo:
1432 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermh))
1433 .add(MI.getOperand(1))
1434 .add(MI.getOperand(2))
1435 .add(MI.getOperand(3));
1436 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1437 .add(MI.getOperand(0))
1438 .addImm(0)
1439 .addReg(Hexagon::VTMP);
1440 MBB.erase(MI);
1441 return First.getInstrIterator();
1442
1443 case Hexagon::V6_vgathermw_pseudo:
1444 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermw))
1445 .add(MI.getOperand(1))
1446 .add(MI.getOperand(2))
1447 .add(MI.getOperand(3));
1448 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1449 .add(MI.getOperand(0))
1450 .addImm(0)
1451 .addReg(Hexagon::VTMP);
1452 MBB.erase(MI);
1453 return First.getInstrIterator();
1454
1455 case Hexagon::V6_vgathermhw_pseudo:
1456 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhw))
1457 .add(MI.getOperand(1))
1458 .add(MI.getOperand(2))
1459 .add(MI.getOperand(3));
1460 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1461 .add(MI.getOperand(0))
1462 .addImm(0)
1463 .addReg(Hexagon::VTMP);
1464 MBB.erase(MI);
1465 return First.getInstrIterator();
1466
1467 case Hexagon::V6_vgathermhq_pseudo:
1468 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhq))
1469 .add(MI.getOperand(1))
1470 .add(MI.getOperand(2))
1471 .add(MI.getOperand(3))
1472 .add(MI.getOperand(4));
1473 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1474 .add(MI.getOperand(0))
1475 .addImm(0)
1476 .addReg(Hexagon::VTMP);
1477 MBB.erase(MI);
1478 return First.getInstrIterator();
1479
1480 case Hexagon::V6_vgathermwq_pseudo:
1481 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermwq))
1482 .add(MI.getOperand(1))
1483 .add(MI.getOperand(2))
1484 .add(MI.getOperand(3))
1485 .add(MI.getOperand(4));
1486 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1487 .add(MI.getOperand(0))
1488 .addImm(0)
1489 .addReg(Hexagon::VTMP);
1490 MBB.erase(MI);
1491 return First.getInstrIterator();
1492
1493 case Hexagon::V6_vgathermhwq_pseudo:
1494 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhwq))
1495 .add(MI.getOperand(1))
1496 .add(MI.getOperand(2))
1497 .add(MI.getOperand(3))
1498 .add(MI.getOperand(4));
1499 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1500 .add(MI.getOperand(0))
1501 .addImm(0)
1502 .addReg(Hexagon::VTMP);
1503 MBB.erase(MI);
1504 return First.getInstrIterator();
1505 }
1506
1507 return MI.getIterator();
1508}
1509
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001510// We indicate that we want to reverse the branch by
1511// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001512bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001513 SmallVectorImpl<MachineOperand> &Cond) const {
1514 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001515 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001516 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1517 unsigned opcode = Cond[0].getImm();
1518 //unsigned temp;
1519 assert(get(opcode).isBranch() && "Should be a branching condition.");
1520 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001521 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001522 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1523 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001524 return false;
1525}
1526
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001527void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1528 MachineBasicBlock::iterator MI) const {
1529 DebugLoc DL;
1530 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1531}
1532
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001533bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1534 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001535}
1536
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001537// Returns true if an instruction is predicated irrespective of the predicate
1538// sense. For example, all of the following will return true.
1539// if (p0) R1 = add(R2, R3)
1540// if (!p0) R1 = add(R2, R3)
1541// if (p0.new) R1 = add(R2, R3)
1542// if (!p0.new) R1 = add(R2, R3)
1543// Note: New-value stores are not included here as in the current
1544// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001545bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1546 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001547 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001548}
1549
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001550bool HexagonInstrInfo::PredicateInstruction(
1551 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001552 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1553 isEndLoopN(Cond[0].getImm())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001554 LLVM_DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001555 return false;
1556 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001557 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001558 assert (isPredicable(MI) && "Expected predicable instruction");
1559 bool invertJump = predOpcodeHasNot(Cond);
1560
1561 // We have to predicate MI "in place", i.e. after this function returns,
1562 // MI will need to be transformed into a predicated form. To avoid com-
1563 // plicated manipulations with the operands (handling tied operands,
1564 // etc.), build a new temporary instruction, then overwrite MI with it.
1565
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001566 MachineBasicBlock &B = *MI.getParent();
1567 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001568 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1569 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001570 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001571 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001572 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001573 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1574 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001575 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001576 NOp++;
1577 }
1578
1579 unsigned PredReg, PredRegPos, PredRegFlags;
1580 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1581 (void)GotPredReg;
1582 assert(GotPredReg);
1583 T.addReg(PredReg, PredRegFlags);
1584 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001585 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001586
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001587 MI.setDesc(get(PredOpc));
1588 while (unsigned n = MI.getNumOperands())
1589 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001590 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001591 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001592
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001593 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001594 B.erase(TI);
1595
1596 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1597 MRI.clearKillFlags(PredReg);
1598 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001599}
1600
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001601bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1602 ArrayRef<MachineOperand> Pred2) const {
1603 // TODO: Fix this
1604 return false;
1605}
1606
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001607bool HexagonInstrInfo::DefinesPredicate(MachineInstr &MI,
1608 std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001609 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001610
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001611 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1612 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001613 if (MO.isReg()) {
1614 if (!MO.isDef())
1615 continue;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001616 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1617 if (RC == &Hexagon::PredRegsRegClass) {
1618 Pred.push_back(MO);
1619 return true;
1620 }
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001621 continue;
1622 } else if (MO.isRegMask()) {
1623 for (unsigned PR : Hexagon::PredRegsRegClass) {
1624 if (!MI.modifiesRegister(PR, &HRI))
1625 continue;
1626 Pred.push_back(MO);
1627 return true;
1628 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001629 }
1630 }
1631 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001632}
Andrew Trickd06df962012-02-01 22:13:57 +00001633
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001634bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001635 if (!MI.getDesc().isPredicable())
1636 return false;
1637
1638 if (MI.isCall() || isTailCall(MI)) {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001639 if (!Subtarget.usePredicatedCalls())
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001640 return false;
1641 }
Krzysztof Parzyszek8c53c952017-10-18 17:36:46 +00001642
1643 // HVX loads are not predicable on v60, but are on v62.
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00001644 if (!Subtarget.hasV62Ops()) {
Krzysztof Parzyszek8c53c952017-10-18 17:36:46 +00001645 switch (MI.getOpcode()) {
1646 case Hexagon::V6_vL32b_ai:
1647 case Hexagon::V6_vL32b_pi:
1648 case Hexagon::V6_vL32b_ppu:
1649 case Hexagon::V6_vL32b_cur_ai:
1650 case Hexagon::V6_vL32b_cur_pi:
1651 case Hexagon::V6_vL32b_cur_ppu:
1652 case Hexagon::V6_vL32b_nt_ai:
1653 case Hexagon::V6_vL32b_nt_pi:
1654 case Hexagon::V6_vL32b_nt_ppu:
1655 case Hexagon::V6_vL32b_tmp_ai:
1656 case Hexagon::V6_vL32b_tmp_pi:
1657 case Hexagon::V6_vL32b_tmp_ppu:
1658 case Hexagon::V6_vL32b_nt_cur_ai:
1659 case Hexagon::V6_vL32b_nt_cur_pi:
1660 case Hexagon::V6_vL32b_nt_cur_ppu:
1661 case Hexagon::V6_vL32b_nt_tmp_ai:
1662 case Hexagon::V6_vL32b_nt_tmp_pi:
1663 case Hexagon::V6_vL32b_nt_tmp_ppu:
1664 return false;
1665 }
1666 }
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001667 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001668}
1669
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001670bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1671 const MachineBasicBlock *MBB,
1672 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001673 // Debug info is never a scheduling boundary. It's necessary to be explicit
1674 // due to the special treatment of IT instructions below, otherwise a
1675 // dbg_value followed by an IT will result in the IT instruction being
1676 // considered a scheduling hazard, which is wrong. It should be the actual
1677 // instruction preceding the dbg_value instruction(s), just like it is
1678 // when debug info is not present.
Shiva Chen801bf7e2018-05-09 02:42:00 +00001679 if (MI.isDebugInstr())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001680 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001681
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001682 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001683 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001684 // Don't mess around with no return calls.
1685 if (doesNotReturn(MI))
1686 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001687 // If any of the block's successors is a landing pad, this could be a
1688 // throwing call.
1689 for (auto I : MBB->successors())
1690 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001691 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001692 }
1693
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001694 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001695 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001696 return true;
1697
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001698 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1699 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001700
1701 return false;
1702}
1703
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001704/// Measure the specified inline asm to determine an approximation of its
1705/// length.
1706/// Comments (which run till the next SeparatorString or newline) do not
1707/// count as an instruction.
1708/// Any other non-whitespace text is considered an instruction, with
1709/// multiple instructions separated by SeparatorString or newlines.
1710/// Variable-length instructions are not handled here; this function
1711/// may be overloaded in the target code to do that.
1712/// Hexagon counts the number of ##'s and adjust for that many
1713/// constant exenders.
1714unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1715 const MCAsmInfo &MAI) const {
1716 StringRef AStr(Str);
1717 // Count the number of instructions in the asm.
1718 bool atInsnStart = true;
1719 unsigned Length = 0;
1720 for (; *Str; ++Str) {
1721 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1722 strlen(MAI.getSeparatorString())) == 0)
1723 atInsnStart = true;
1724 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1725 Length += MAI.getMaxInstLength();
1726 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001727 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001728 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1729 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001730 atInsnStart = false;
1731 }
1732
1733 // Add to size number of constant extenders seen * 4.
1734 StringRef Occ("##");
1735 Length += AStr.count(Occ)*4;
1736 return Length;
1737}
1738
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001739ScheduleHazardRecognizer*
1740HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1741 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001742 if (UseDFAHazardRec)
1743 return new HexagonHazardRecognizer(II, this, Subtarget);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001744 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1745}
1746
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001747/// For a comparison instruction, return the source registers in
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001748/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1749/// compares against in CmpValue. Return true if the comparison instruction
1750/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001751bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1752 unsigned &SrcReg2, int &Mask,
1753 int &Value) const {
1754 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001755
1756 // Set mask and the first source register.
1757 switch (Opc) {
1758 case Hexagon::C2_cmpeq:
1759 case Hexagon::C2_cmpeqp:
1760 case Hexagon::C2_cmpgt:
1761 case Hexagon::C2_cmpgtp:
1762 case Hexagon::C2_cmpgtu:
1763 case Hexagon::C2_cmpgtup:
1764 case Hexagon::C4_cmpneq:
1765 case Hexagon::C4_cmplte:
1766 case Hexagon::C4_cmplteu:
1767 case Hexagon::C2_cmpeqi:
1768 case Hexagon::C2_cmpgti:
1769 case Hexagon::C2_cmpgtui:
1770 case Hexagon::C4_cmpneqi:
1771 case Hexagon::C4_cmplteui:
1772 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001773 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001774 Mask = ~0;
1775 break;
1776 case Hexagon::A4_cmpbeq:
1777 case Hexagon::A4_cmpbgt:
1778 case Hexagon::A4_cmpbgtu:
1779 case Hexagon::A4_cmpbeqi:
1780 case Hexagon::A4_cmpbgti:
1781 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001782 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001783 Mask = 0xFF;
1784 break;
1785 case Hexagon::A4_cmpheq:
1786 case Hexagon::A4_cmphgt:
1787 case Hexagon::A4_cmphgtu:
1788 case Hexagon::A4_cmpheqi:
1789 case Hexagon::A4_cmphgti:
1790 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001791 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001792 Mask = 0xFFFF;
1793 break;
1794 }
1795
1796 // Set the value/second source register.
1797 switch (Opc) {
1798 case Hexagon::C2_cmpeq:
1799 case Hexagon::C2_cmpeqp:
1800 case Hexagon::C2_cmpgt:
1801 case Hexagon::C2_cmpgtp:
1802 case Hexagon::C2_cmpgtu:
1803 case Hexagon::C2_cmpgtup:
1804 case Hexagon::A4_cmpbeq:
1805 case Hexagon::A4_cmpbgt:
1806 case Hexagon::A4_cmpbgtu:
1807 case Hexagon::A4_cmpheq:
1808 case Hexagon::A4_cmphgt:
1809 case Hexagon::A4_cmphgtu:
1810 case Hexagon::C4_cmpneq:
1811 case Hexagon::C4_cmplte:
1812 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001813 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001814 return true;
1815
1816 case Hexagon::C2_cmpeqi:
1817 case Hexagon::C2_cmpgtui:
1818 case Hexagon::C2_cmpgti:
1819 case Hexagon::C4_cmpneqi:
1820 case Hexagon::C4_cmplteui:
1821 case Hexagon::C4_cmpltei:
1822 case Hexagon::A4_cmpbeqi:
1823 case Hexagon::A4_cmpbgti:
1824 case Hexagon::A4_cmpbgtui:
1825 case Hexagon::A4_cmpheqi:
1826 case Hexagon::A4_cmphgti:
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001827 case Hexagon::A4_cmphgtui: {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001828 SrcReg2 = 0;
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001829 const MachineOperand &Op2 = MI.getOperand(2);
1830 if (!Op2.isImm())
1831 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001832 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001833 return true;
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001834 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001835 }
1836
1837 return false;
1838}
1839
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001840unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001841 const MachineInstr &MI,
1842 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001843 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001844}
1845
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001846DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1847 const TargetSubtargetInfo &STI) const {
1848 const InstrItineraryData *II = STI.getInstrItineraryData();
1849 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1850}
1851
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001852// Inspired by this pair:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001853// %r13 = L2_loadri_io %r29, 136; mem:LD4[FixedStack0]
1854// S2_storeri_io %r29, 132, killed %r1; flags: mem:ST4[FixedStack1]
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001855// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001856bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
Bjorn Pettersson238c9d6302019-04-19 09:08:38 +00001857 const MachineInstr &MIa, const MachineInstr &MIb,
1858 AliasAnalysis *AA) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001859 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1860 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001861 return false;
1862
1863 // Instructions that are pure loads, not loads and stores like memops are not
1864 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001865 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001866 return true;
1867
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001868 // Get the base register in MIa.
1869 unsigned BasePosA, OffsetPosA;
1870 if (!getBaseAndOffsetPosition(MIa, BasePosA, OffsetPosA))
1871 return false;
1872 const MachineOperand &BaseA = MIa.getOperand(BasePosA);
1873 unsigned BaseRegA = BaseA.getReg();
1874 unsigned BaseSubA = BaseA.getSubReg();
1875
1876 // Get the base register in MIb.
1877 unsigned BasePosB, OffsetPosB;
1878 if (!getBaseAndOffsetPosition(MIb, BasePosB, OffsetPosB))
1879 return false;
1880 const MachineOperand &BaseB = MIb.getOperand(BasePosB);
1881 unsigned BaseRegB = BaseB.getReg();
1882 unsigned BaseSubB = BaseB.getSubReg();
1883
1884 if (BaseRegA != BaseRegB || BaseSubA != BaseSubB)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001885 return false;
1886
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001887 // Get the access sizes.
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00001888 unsigned SizeA = getMemAccessSize(MIa);
1889 unsigned SizeB = getMemAccessSize(MIb);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001890
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001891 // Get the offsets. Handle immediates only for now.
1892 const MachineOperand &OffA = MIa.getOperand(OffsetPosA);
1893 const MachineOperand &OffB = MIb.getOperand(OffsetPosB);
1894 if (!MIa.getOperand(OffsetPosA).isImm() ||
1895 !MIb.getOperand(OffsetPosB).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001896 return false;
Krzysztof Parzyszekac019942017-07-19 19:17:32 +00001897 int OffsetA = isPostIncrement(MIa) ? 0 : OffA.getImm();
1898 int OffsetB = isPostIncrement(MIb) ? 0 : OffB.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001899
1900 // This is a mem access with the same base register and known offsets from it.
1901 // Reason about it.
1902 if (OffsetA > OffsetB) {
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001903 uint64_t OffDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1904 return SizeB <= OffDiff;
1905 }
1906 if (OffsetA < OffsetB) {
1907 uint64_t OffDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1908 return SizeA <= OffDiff;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001909 }
1910
1911 return false;
1912}
1913
Brendon Cahoon254f8892016-07-29 16:44:44 +00001914/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001915bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001916 int &Value) const {
1917 if (isPostIncrement(MI)) {
Krzysztof Parzyszek12bdcab2017-10-11 15:59:51 +00001918 unsigned BasePos = 0, OffsetPos = 0;
1919 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
1920 return false;
1921 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
1922 if (OffsetOp.isImm()) {
1923 Value = OffsetOp.getImm();
1924 return true;
1925 }
Krzysztof Parzyszekbf626192017-10-11 16:15:31 +00001926 } else if (MI.getOpcode() == Hexagon::A2_addi) {
1927 const MachineOperand &AddOp = MI.getOperand(2);
1928 if (AddOp.isImm()) {
1929 Value = AddOp.getImm();
1930 return true;
1931 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00001932 }
1933
1934 return false;
1935}
1936
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001937std::pair<unsigned, unsigned>
1938HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1939 return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
1940 TF & HexagonII::MO_Bitmasks);
1941}
1942
1943ArrayRef<std::pair<unsigned, const char*>>
1944HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1945 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00001946
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001947 static const std::pair<unsigned, const char*> Flags[] = {
1948 {MO_PCREL, "hexagon-pcrel"},
1949 {MO_GOT, "hexagon-got"},
1950 {MO_LO16, "hexagon-lo16"},
1951 {MO_HI16, "hexagon-hi16"},
1952 {MO_GPREL, "hexagon-gprel"},
1953 {MO_GDGOT, "hexagon-gdgot"},
1954 {MO_GDPLT, "hexagon-gdplt"},
1955 {MO_IE, "hexagon-ie"},
1956 {MO_IEGOT, "hexagon-iegot"},
1957 {MO_TPREL, "hexagon-tprel"}
1958 };
1959 return makeArrayRef(Flags);
1960}
1961
1962ArrayRef<std::pair<unsigned, const char*>>
1963HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1964 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00001965
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001966 static const std::pair<unsigned, const char*> Flags[] = {
1967 {HMOTF_ConstExtended, "hexagon-ext"}
1968 };
1969 return makeArrayRef(Flags);
1970}
1971
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001972unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001973 MachineRegisterInfo &MRI = MF->getRegInfo();
1974 const TargetRegisterClass *TRC;
1975 if (VT == MVT::i1) {
1976 TRC = &Hexagon::PredRegsRegClass;
1977 } else if (VT == MVT::i32 || VT == MVT::f32) {
1978 TRC = &Hexagon::IntRegsRegClass;
1979 } else if (VT == MVT::i64 || VT == MVT::f64) {
1980 TRC = &Hexagon::DoubleRegsRegClass;
1981 } else {
1982 llvm_unreachable("Cannot handle this register class");
1983 }
1984
1985 unsigned NewReg = MRI.createVirtualRegister(TRC);
1986 return NewReg;
1987}
1988
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001989bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001990 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1991}
1992
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001993bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1994 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001995 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1996}
1997
Krzysztof Parzyszek998df2c2018-03-23 20:43:02 +00001998bool HexagonInstrInfo::isBaseImmOffset(const MachineInstr &MI) const {
1999 return getAddrMode(MI) == HexagonII::BaseImmOffset;
2000}
2001
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002002bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00002003 return !isTC1(MI) && !isTC2Early(MI) && !MI.getDesc().mayLoad() &&
2004 !MI.getDesc().mayStore() &&
2005 MI.getDesc().getOpcode() != Hexagon::S2_allocframe &&
2006 MI.getDesc().getOpcode() != Hexagon::L2_deallocframe &&
2007 !isMemOp(MI) && !MI.isBranch() && !MI.isReturn() && !MI.isCall();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002008}
2009
Sanjay Patele4b9f502015-12-07 19:21:39 +00002010// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002011bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00002012 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002013}
2014
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002015// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
2016// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002017bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
2018 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002019 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
2020 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00002021 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002022
2023 unsigned isExtendable =
2024 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
2025 if (!isExtendable)
2026 return false;
2027
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002028 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002029 return false;
2030
2031 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002032 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002033 // Use MO operand flags to determine if MO
2034 // has the HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00002035 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00002036 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002037 // If this is a Machine BB address we are talking about, and it is
2038 // not marked as extended, say so.
2039 if (MO.isMBB())
2040 return false;
2041
2042 // We could be using an instruction with an extendable immediate and shoehorn
2043 // a global address into it. If it is a global address it will be constant
2044 // extended. We do this for COMBINE.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002045 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00002046 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002047 return true;
2048
2049 // If the extendable operand is not 'Immediate' type, the instruction should
2050 // have 'isExtended' flag set.
2051 assert(MO.isImm() && "Extendable operand must be Immediate type");
2052
2053 int MinValue = getMinValue(MI);
2054 int MaxValue = getMaxValue(MI);
2055 int ImmValue = MO.getImm();
2056
2057 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002058}
2059
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002060bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
2061 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002062 case Hexagon::L4_return:
2063 case Hexagon::L4_return_t:
2064 case Hexagon::L4_return_f:
2065 case Hexagon::L4_return_tnew_pnt:
2066 case Hexagon::L4_return_fnew_pnt:
2067 case Hexagon::L4_return_tnew_pt:
2068 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002069 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002070 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002071 return false;
2072}
2073
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002074// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002075bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
2076 const MachineInstr &ConsMI) const {
2077 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002078 return false;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00002079 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002080
2081 SmallVector<unsigned, 4> DefsA;
2082 SmallVector<unsigned, 4> DefsB;
2083 SmallVector<unsigned, 8> UsesA;
2084 SmallVector<unsigned, 8> UsesB;
2085
2086 parseOperands(ProdMI, DefsA, UsesA);
2087 parseOperands(ConsMI, DefsB, UsesB);
2088
2089 for (auto &RegA : DefsA)
2090 for (auto &RegB : UsesB) {
2091 // True data dependency.
2092 if (RegA == RegB)
2093 return true;
2094
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002095 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002096 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
2097 if (RegB == *SubRegs)
2098 return true;
2099
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002100 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002101 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
2102 if (RegA == *SubRegs)
2103 return true;
2104 }
2105
2106 return false;
2107}
2108
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002109// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002110bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2111 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002112 case Hexagon::V6_vL32b_cur_pi:
2113 case Hexagon::V6_vL32b_cur_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002114 return true;
2115 }
2116 return false;
2117}
2118
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002119// Returns true, if any one of the operands is a dot new
2120// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002121bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2122 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002123 return true;
2124
2125 return false;
2126}
2127
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002128/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002129bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2130 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002131 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2132 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2133 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2134}
2135
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002136bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2137 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002138 return true;
2139
2140 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002141 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002142 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002143}
2144
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002145bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2146 return (Opcode == Hexagon::ENDLOOP0 ||
2147 Opcode == Hexagon::ENDLOOP1);
2148}
2149
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002150bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2151 switch(OpType) {
2152 case MachineOperand::MO_MachineBasicBlock:
2153 case MachineOperand::MO_GlobalAddress:
2154 case MachineOperand::MO_ExternalSymbol:
2155 case MachineOperand::MO_JumpTableIndex:
2156 case MachineOperand::MO_ConstantPoolIndex:
2157 case MachineOperand::MO_BlockAddress:
2158 return true;
2159 default:
2160 return false;
2161 }
2162}
2163
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002164bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2165 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002166 const uint64_t F = MID.TSFlags;
2167 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2168 return true;
2169
2170 // TODO: This is largely obsolete now. Will need to be removed
2171 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002172 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002173 // PS_fi and PS_fia remain special cases.
2174 case Hexagon::PS_fi:
2175 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002176 return true;
2177 default:
2178 return false;
2179 }
2180 return false;
2181}
2182
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002183// This returns true in two cases:
2184// - The OP code itself indicates that this is an extended instruction.
2185// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002186bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002187 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002188 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002189 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2190 return true;
2191 // Use MO operand flags to determine if one of MI's operands
2192 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00002193 for (const MachineOperand &MO : MI.operands())
2194 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002195 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002196 return false;
2197}
2198
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002199bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2200 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002201 const uint64_t F = get(Opcode).TSFlags;
2202 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2203}
2204
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002205// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002206bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2207 const MachineInstr &J) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002208 if (!isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002209 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002210 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002211 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002212 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002213}
2214
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002215bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2216 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002217 case Hexagon::J2_callr:
2218 case Hexagon::J2_callrf:
2219 case Hexagon::J2_callrt:
2220 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002221 return true;
2222 }
2223 return false;
2224}
2225
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002226bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2227 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002228 case Hexagon::L4_return:
2229 case Hexagon::L4_return_t:
2230 case Hexagon::L4_return_f:
2231 case Hexagon::L4_return_fnew_pnt:
2232 case Hexagon::L4_return_fnew_pt:
2233 case Hexagon::L4_return_tnew_pnt:
2234 case Hexagon::L4_return_tnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002235 return true;
2236 }
2237 return false;
2238}
2239
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002240bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2241 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002242 case Hexagon::J2_jumpr:
2243 case Hexagon::J2_jumprt:
2244 case Hexagon::J2_jumprf:
2245 case Hexagon::J2_jumprtnewpt:
2246 case Hexagon::J2_jumprfnewpt:
2247 case Hexagon::J2_jumprtnew:
2248 case Hexagon::J2_jumprfnew:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002249 return true;
2250 }
2251 return false;
2252}
2253
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002254// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002255// Use abs estimate as oppose to the exact number.
2256// TODO: This will need to be changed to use MC level
2257// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002258bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002259 unsigned offset) const {
2260 // This selection of jump instructions matches to that what
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002261 // analyzeBranch can parse, plus NVJ.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002262 if (isNewValueJump(MI)) // r9:2
2263 return isInt<11>(offset);
2264
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002265 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002266 // Still missing Jump to address condition on register value.
2267 default:
2268 return false;
2269 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2270 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002271 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002272 return isInt<24>(offset);
2273 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2274 case Hexagon::J2_jumpf:
2275 case Hexagon::J2_jumptnew:
2276 case Hexagon::J2_jumptnewpt:
2277 case Hexagon::J2_jumpfnew:
2278 case Hexagon::J2_jumpfnewpt:
2279 case Hexagon::J2_callt:
2280 case Hexagon::J2_callf:
2281 return isInt<17>(offset);
2282 case Hexagon::J2_loop0i:
2283 case Hexagon::J2_loop0iext:
2284 case Hexagon::J2_loop0r:
2285 case Hexagon::J2_loop0rext:
2286 case Hexagon::J2_loop1i:
2287 case Hexagon::J2_loop1iext:
2288 case Hexagon::J2_loop1r:
2289 case Hexagon::J2_loop1rext:
2290 return isInt<9>(offset);
2291 // TODO: Add all the compound branches here. Can we do this in Relation model?
2292 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2293 case Hexagon::J4_cmpeqi_tp1_jump_nt:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002294 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
2295 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002296 return isInt<11>(offset);
2297 }
2298}
2299
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002300bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2301 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002302 bool isLate = isLateResultInstr(LRMI);
2303 bool isEarly = isEarlySourceInstr(ESMI);
2304
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002305 LLVM_DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
2306 LLVM_DEBUG(LRMI.dump());
2307 LLVM_DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
2308 LLVM_DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002309
2310 if (isLate && isEarly) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002311 LLVM_DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002312 return true;
2313 }
2314
2315 return false;
2316}
2317
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002318bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2319 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002320 case TargetOpcode::EXTRACT_SUBREG:
2321 case TargetOpcode::INSERT_SUBREG:
2322 case TargetOpcode::SUBREG_TO_REG:
2323 case TargetOpcode::REG_SEQUENCE:
2324 case TargetOpcode::IMPLICIT_DEF:
2325 case TargetOpcode::COPY:
2326 case TargetOpcode::INLINEASM:
2327 case TargetOpcode::PHI:
2328 return false;
2329 default:
2330 break;
2331 }
2332
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002333 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002334 return !is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002335}
2336
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002337bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002338 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2339 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002340 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002341}
2342
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002343bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2344 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002345 return Opcode == Hexagon::J2_loop0i ||
2346 Opcode == Hexagon::J2_loop0r ||
2347 Opcode == Hexagon::J2_loop0iext ||
2348 Opcode == Hexagon::J2_loop0rext ||
2349 Opcode == Hexagon::J2_loop1i ||
2350 Opcode == Hexagon::J2_loop1r ||
2351 Opcode == Hexagon::J2_loop1iext ||
2352 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002353}
2354
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002355bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2356 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002357 default: return false;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002358 case Hexagon::L4_iadd_memopw_io:
2359 case Hexagon::L4_isub_memopw_io:
2360 case Hexagon::L4_add_memopw_io:
2361 case Hexagon::L4_sub_memopw_io:
2362 case Hexagon::L4_and_memopw_io:
2363 case Hexagon::L4_or_memopw_io:
2364 case Hexagon::L4_iadd_memoph_io:
2365 case Hexagon::L4_isub_memoph_io:
2366 case Hexagon::L4_add_memoph_io:
2367 case Hexagon::L4_sub_memoph_io:
2368 case Hexagon::L4_and_memoph_io:
2369 case Hexagon::L4_or_memoph_io:
2370 case Hexagon::L4_iadd_memopb_io:
2371 case Hexagon::L4_isub_memopb_io:
2372 case Hexagon::L4_add_memopb_io:
2373 case Hexagon::L4_sub_memopb_io:
2374 case Hexagon::L4_and_memopb_io:
2375 case Hexagon::L4_or_memopb_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002376 case Hexagon::L4_ior_memopb_io:
2377 case Hexagon::L4_ior_memoph_io:
2378 case Hexagon::L4_ior_memopw_io:
2379 case Hexagon::L4_iand_memopb_io:
2380 case Hexagon::L4_iand_memoph_io:
2381 case Hexagon::L4_iand_memopw_io:
2382 return true;
2383 }
2384 return false;
2385}
2386
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002387bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2388 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002389 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2390}
2391
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002392bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2393 const uint64_t F = get(Opcode).TSFlags;
2394 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2395}
2396
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002397bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002398 return isNewValueJump(MI) || isNewValueStore(MI);
2399}
2400
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002401bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2402 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002403}
2404
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002405bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2406 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2407}
2408
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002409bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2410 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002411 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2412}
2413
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002414bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2415 const uint64_t F = get(Opcode).TSFlags;
2416 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2417}
2418
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002419// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002420bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002421 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002422 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002423 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2424 == OperandNum;
2425}
2426
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002427bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2428 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002429 assert(isPredicated(MI));
2430 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2431}
2432
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002433bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2434 const uint64_t F = get(Opcode).TSFlags;
2435 assert(isPredicated(Opcode));
2436 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2437}
2438
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002439bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2440 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002441 return !((F >> HexagonII::PredicatedFalsePos) &
2442 HexagonII::PredicatedFalseMask);
2443}
2444
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002445bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2446 const uint64_t F = get(Opcode).TSFlags;
2447 // Make sure that the instruction is predicated.
2448 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2449 return !((F >> HexagonII::PredicatedFalsePos) &
2450 HexagonII::PredicatedFalseMask);
2451}
2452
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002453bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2454 const uint64_t F = get(Opcode).TSFlags;
2455 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2456}
2457
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002458bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2459 const uint64_t F = get(Opcode).TSFlags;
Krzysztof Parzyszek03671542019-01-23 15:36:33 +00002460 return (F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002461}
2462
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002463bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2464 const uint64_t F = get(Opcode).TSFlags;
2465 assert(get(Opcode).isBranch() &&
2466 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2467 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2468}
2469
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002470bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2471 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2472 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2473 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2474 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002475}
2476
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002477bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2478 switch (MI.getOpcode()) {
2479 // Byte
2480 case Hexagon::L2_loadrb_io:
2481 case Hexagon::L4_loadrb_ur:
2482 case Hexagon::L4_loadrb_ap:
2483 case Hexagon::L2_loadrb_pr:
2484 case Hexagon::L2_loadrb_pbr:
2485 case Hexagon::L2_loadrb_pi:
2486 case Hexagon::L2_loadrb_pci:
2487 case Hexagon::L2_loadrb_pcr:
2488 case Hexagon::L2_loadbsw2_io:
2489 case Hexagon::L4_loadbsw2_ur:
2490 case Hexagon::L4_loadbsw2_ap:
2491 case Hexagon::L2_loadbsw2_pr:
2492 case Hexagon::L2_loadbsw2_pbr:
2493 case Hexagon::L2_loadbsw2_pi:
2494 case Hexagon::L2_loadbsw2_pci:
2495 case Hexagon::L2_loadbsw2_pcr:
2496 case Hexagon::L2_loadbsw4_io:
2497 case Hexagon::L4_loadbsw4_ur:
2498 case Hexagon::L4_loadbsw4_ap:
2499 case Hexagon::L2_loadbsw4_pr:
2500 case Hexagon::L2_loadbsw4_pbr:
2501 case Hexagon::L2_loadbsw4_pi:
2502 case Hexagon::L2_loadbsw4_pci:
2503 case Hexagon::L2_loadbsw4_pcr:
2504 case Hexagon::L4_loadrb_rr:
2505 case Hexagon::L2_ploadrbt_io:
2506 case Hexagon::L2_ploadrbt_pi:
2507 case Hexagon::L2_ploadrbf_io:
2508 case Hexagon::L2_ploadrbf_pi:
2509 case Hexagon::L2_ploadrbtnew_io:
2510 case Hexagon::L2_ploadrbfnew_io:
2511 case Hexagon::L4_ploadrbt_rr:
2512 case Hexagon::L4_ploadrbf_rr:
2513 case Hexagon::L4_ploadrbtnew_rr:
2514 case Hexagon::L4_ploadrbfnew_rr:
2515 case Hexagon::L2_ploadrbtnew_pi:
2516 case Hexagon::L2_ploadrbfnew_pi:
2517 case Hexagon::L4_ploadrbt_abs:
2518 case Hexagon::L4_ploadrbf_abs:
2519 case Hexagon::L4_ploadrbtnew_abs:
2520 case Hexagon::L4_ploadrbfnew_abs:
2521 case Hexagon::L2_loadrbgp:
2522 // Half
2523 case Hexagon::L2_loadrh_io:
2524 case Hexagon::L4_loadrh_ur:
2525 case Hexagon::L4_loadrh_ap:
2526 case Hexagon::L2_loadrh_pr:
2527 case Hexagon::L2_loadrh_pbr:
2528 case Hexagon::L2_loadrh_pi:
2529 case Hexagon::L2_loadrh_pci:
2530 case Hexagon::L2_loadrh_pcr:
2531 case Hexagon::L4_loadrh_rr:
2532 case Hexagon::L2_ploadrht_io:
2533 case Hexagon::L2_ploadrht_pi:
2534 case Hexagon::L2_ploadrhf_io:
2535 case Hexagon::L2_ploadrhf_pi:
2536 case Hexagon::L2_ploadrhtnew_io:
2537 case Hexagon::L2_ploadrhfnew_io:
2538 case Hexagon::L4_ploadrht_rr:
2539 case Hexagon::L4_ploadrhf_rr:
2540 case Hexagon::L4_ploadrhtnew_rr:
2541 case Hexagon::L4_ploadrhfnew_rr:
2542 case Hexagon::L2_ploadrhtnew_pi:
2543 case Hexagon::L2_ploadrhfnew_pi:
2544 case Hexagon::L4_ploadrht_abs:
2545 case Hexagon::L4_ploadrhf_abs:
2546 case Hexagon::L4_ploadrhtnew_abs:
2547 case Hexagon::L4_ploadrhfnew_abs:
2548 case Hexagon::L2_loadrhgp:
2549 return true;
2550 default:
2551 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002552 }
2553}
2554
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002555bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2556 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002557 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2558}
2559
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002560bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2561 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002562 case Hexagon::STriw_pred:
2563 case Hexagon::LDriw_pred:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002564 return true;
2565 default:
2566 return false;
2567 }
2568}
2569
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002570bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2571 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002572 return false;
2573
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002574 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002575 if (Op.isGlobal() || Op.isSymbol())
2576 return true;
2577 return false;
2578}
2579
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002580// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002581bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2582 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002583 return is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002584}
2585
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002586bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2587 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002588 return is_TC2(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002589}
2590
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002591bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2592 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002593 return is_TC2early(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002594}
2595
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002596bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2597 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002598 return is_TC4x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002599}
2600
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002601// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002602bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2603 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002604 if (mayBeCurLoad(MI1)) {
2605 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002606 unsigned DstReg = MI1.getOperand(0).getReg();
2607 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002608 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002609 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002610 return true;
2611 }
2612 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002613 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2614 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2615 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002616 return true;
2617 return false;
2618}
2619
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002620bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002621 const uint64_t V = getType(MI);
2622 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2623}
2624
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002625// Check if the Offset is a valid auto-inc imm by Load/Store Type.
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002626bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, int Offset) const {
2627 int Size = VT.getSizeInBits() / 8;
2628 if (Offset % Size != 0)
2629 return false;
2630 int Count = Offset / Size;
2631
2632 switch (VT.getSimpleVT().SimpleTy) {
2633 // For scalars the auto-inc is s4
2634 case MVT::i8:
2635 case MVT::i16:
2636 case MVT::i32:
2637 case MVT::i64:
Brendon Cahoone5ed5632018-05-18 18:14:44 +00002638 case MVT::f32:
2639 case MVT::f64:
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002640 case MVT::v2i16:
2641 case MVT::v2i32:
2642 case MVT::v4i8:
2643 case MVT::v4i16:
2644 case MVT::v8i8:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002645 return isInt<4>(Count);
2646 // For HVX vectors the auto-inc is s3
2647 case MVT::v64i8:
2648 case MVT::v32i16:
2649 case MVT::v16i32:
2650 case MVT::v8i64:
2651 case MVT::v128i8:
2652 case MVT::v64i16:
2653 case MVT::v32i32:
2654 case MVT::v16i64:
2655 return isInt<3>(Count);
2656 default:
2657 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002658 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002659
2660 llvm_unreachable("Not an valid type!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002661}
2662
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002663bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002664 const TargetRegisterInfo *TRI, bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002665 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002666 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002667 // inserted to calculate the final address. Due to this reason, the function
2668 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002669 // We used to assert if the offset was not properly aligned, however,
2670 // there are cases where a misaligned pointer recast can cause this
2671 // problem, and we need to allow for it. The front end warns of such
2672 // misaligns with respect to load size.
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002673 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002674 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002675 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002676 case Hexagon::PS_vstorerw_nt_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002677 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002678 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002679 case Hexagon::PS_vloadrw_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002680 case Hexagon::V6_vL32b_ai:
2681 case Hexagon::V6_vS32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002682 case Hexagon::V6_vL32b_nt_ai:
2683 case Hexagon::V6_vS32b_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002684 case Hexagon::V6_vL32Ub_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002685 case Hexagon::V6_vS32Ub_ai: {
2686 unsigned VectorSize = TRI->getSpillSize(Hexagon::HvxVRRegClass);
2687 assert(isPowerOf2_32(VectorSize));
2688 if (Offset & (VectorSize-1))
2689 return false;
2690 return isInt<4>(Offset >> Log2_32(VectorSize));
2691 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002692
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002693 case Hexagon::J2_loop0i:
2694 case Hexagon::J2_loop1i:
2695 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002696
2697 case Hexagon::S4_storeirb_io:
2698 case Hexagon::S4_storeirbt_io:
2699 case Hexagon::S4_storeirbf_io:
2700 return isUInt<6>(Offset);
2701
2702 case Hexagon::S4_storeirh_io:
2703 case Hexagon::S4_storeirht_io:
2704 case Hexagon::S4_storeirhf_io:
2705 return isShiftedUInt<6,1>(Offset);
2706
2707 case Hexagon::S4_storeiri_io:
2708 case Hexagon::S4_storeirit_io:
2709 case Hexagon::S4_storeirif_io:
2710 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002711 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002712
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002713 if (Extend)
2714 return true;
2715
2716 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002717 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002718 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002719 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2720 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2721
Colin LeMahieu947cd702014-12-23 20:44:59 +00002722 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002723 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002724 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2725 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2726
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002727 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002728 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002729 case Hexagon::S2_storerh_io:
Krzysztof Parzyszekd10df492017-05-03 15:36:51 +00002730 case Hexagon::S2_storerf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002731 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2732 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2733
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002734 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002735 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002736 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002737 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2738 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2739
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002740 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002741 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2742 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2743
Eugene Zelenko3b873362017-09-28 22:27:31 +00002744 case Hexagon::L4_iadd_memopw_io:
2745 case Hexagon::L4_isub_memopw_io:
2746 case Hexagon::L4_add_memopw_io:
2747 case Hexagon::L4_sub_memopw_io:
2748 case Hexagon::L4_and_memopw_io:
2749 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002750 return (0 <= Offset && Offset <= 255);
2751
Eugene Zelenko3b873362017-09-28 22:27:31 +00002752 case Hexagon::L4_iadd_memoph_io:
2753 case Hexagon::L4_isub_memoph_io:
2754 case Hexagon::L4_add_memoph_io:
2755 case Hexagon::L4_sub_memoph_io:
2756 case Hexagon::L4_and_memoph_io:
2757 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002758 return (0 <= Offset && Offset <= 127);
2759
Eugene Zelenko3b873362017-09-28 22:27:31 +00002760 case Hexagon::L4_iadd_memopb_io:
2761 case Hexagon::L4_isub_memopb_io:
2762 case Hexagon::L4_add_memopb_io:
2763 case Hexagon::L4_sub_memopb_io:
2764 case Hexagon::L4_and_memopb_io:
2765 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002766 return (0 <= Offset && Offset <= 63);
2767
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002768 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002769 // any size. Later pass knows how to handle it.
2770 case Hexagon::STriw_pred:
2771 case Hexagon::LDriw_pred:
Krzysztof Parzyszek440ba3a2018-03-28 19:38:29 +00002772 case Hexagon::STriw_ctr:
2773 case Hexagon::LDriw_ctr:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002774 return true;
2775
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002776 case Hexagon::PS_fi:
2777 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002778 case Hexagon::INLINEASM:
2779 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002780
2781 case Hexagon::L2_ploadrbt_io:
2782 case Hexagon::L2_ploadrbf_io:
2783 case Hexagon::L2_ploadrubt_io:
2784 case Hexagon::L2_ploadrubf_io:
2785 case Hexagon::S2_pstorerbt_io:
2786 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002787 return isUInt<6>(Offset);
2788
2789 case Hexagon::L2_ploadrht_io:
2790 case Hexagon::L2_ploadrhf_io:
2791 case Hexagon::L2_ploadruht_io:
2792 case Hexagon::L2_ploadruhf_io:
2793 case Hexagon::S2_pstorerht_io:
2794 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002795 return isShiftedUInt<6,1>(Offset);
2796
2797 case Hexagon::L2_ploadrit_io:
2798 case Hexagon::L2_ploadrif_io:
2799 case Hexagon::S2_pstorerit_io:
2800 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002801 return isShiftedUInt<6,2>(Offset);
2802
2803 case Hexagon::L2_ploadrdt_io:
2804 case Hexagon::L2_ploadrdf_io:
2805 case Hexagon::S2_pstorerdt_io:
2806 case Hexagon::S2_pstorerdf_io:
2807 return isShiftedUInt<6,3>(Offset);
2808 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002809
Benjamin Kramerb6684012011-12-27 11:41:05 +00002810 llvm_unreachable("No offset range is defined for this opcode. "
2811 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002812}
2813
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002814bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002815 return isHVXVec(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002816}
2817
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002818bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2819 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002820 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2821 return
2822 V == HexagonII::TypeCVI_VA ||
2823 V == HexagonII::TypeCVI_VA_DV;
2824}
Andrew Trickd06df962012-02-01 22:13:57 +00002825
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002826bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2827 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002828 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2829 return true;
2830
2831 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2832 return true;
2833
2834 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002835 return true;
2836
2837 return false;
2838}
Jyotsna Verma84256432013-03-01 17:37:13 +00002839
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002840bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2841 switch (MI.getOpcode()) {
2842 // Byte
2843 case Hexagon::L2_loadrub_io:
2844 case Hexagon::L4_loadrub_ur:
2845 case Hexagon::L4_loadrub_ap:
2846 case Hexagon::L2_loadrub_pr:
2847 case Hexagon::L2_loadrub_pbr:
2848 case Hexagon::L2_loadrub_pi:
2849 case Hexagon::L2_loadrub_pci:
2850 case Hexagon::L2_loadrub_pcr:
2851 case Hexagon::L2_loadbzw2_io:
2852 case Hexagon::L4_loadbzw2_ur:
2853 case Hexagon::L4_loadbzw2_ap:
2854 case Hexagon::L2_loadbzw2_pr:
2855 case Hexagon::L2_loadbzw2_pbr:
2856 case Hexagon::L2_loadbzw2_pi:
2857 case Hexagon::L2_loadbzw2_pci:
2858 case Hexagon::L2_loadbzw2_pcr:
2859 case Hexagon::L2_loadbzw4_io:
2860 case Hexagon::L4_loadbzw4_ur:
2861 case Hexagon::L4_loadbzw4_ap:
2862 case Hexagon::L2_loadbzw4_pr:
2863 case Hexagon::L2_loadbzw4_pbr:
2864 case Hexagon::L2_loadbzw4_pi:
2865 case Hexagon::L2_loadbzw4_pci:
2866 case Hexagon::L2_loadbzw4_pcr:
2867 case Hexagon::L4_loadrub_rr:
2868 case Hexagon::L2_ploadrubt_io:
2869 case Hexagon::L2_ploadrubt_pi:
2870 case Hexagon::L2_ploadrubf_io:
2871 case Hexagon::L2_ploadrubf_pi:
2872 case Hexagon::L2_ploadrubtnew_io:
2873 case Hexagon::L2_ploadrubfnew_io:
2874 case Hexagon::L4_ploadrubt_rr:
2875 case Hexagon::L4_ploadrubf_rr:
2876 case Hexagon::L4_ploadrubtnew_rr:
2877 case Hexagon::L4_ploadrubfnew_rr:
2878 case Hexagon::L2_ploadrubtnew_pi:
2879 case Hexagon::L2_ploadrubfnew_pi:
2880 case Hexagon::L4_ploadrubt_abs:
2881 case Hexagon::L4_ploadrubf_abs:
2882 case Hexagon::L4_ploadrubtnew_abs:
2883 case Hexagon::L4_ploadrubfnew_abs:
2884 case Hexagon::L2_loadrubgp:
2885 // Half
2886 case Hexagon::L2_loadruh_io:
2887 case Hexagon::L4_loadruh_ur:
2888 case Hexagon::L4_loadruh_ap:
2889 case Hexagon::L2_loadruh_pr:
2890 case Hexagon::L2_loadruh_pbr:
2891 case Hexagon::L2_loadruh_pi:
2892 case Hexagon::L2_loadruh_pci:
2893 case Hexagon::L2_loadruh_pcr:
2894 case Hexagon::L4_loadruh_rr:
2895 case Hexagon::L2_ploadruht_io:
2896 case Hexagon::L2_ploadruht_pi:
2897 case Hexagon::L2_ploadruhf_io:
2898 case Hexagon::L2_ploadruhf_pi:
2899 case Hexagon::L2_ploadruhtnew_io:
2900 case Hexagon::L2_ploadruhfnew_io:
2901 case Hexagon::L4_ploadruht_rr:
2902 case Hexagon::L4_ploadruhf_rr:
2903 case Hexagon::L4_ploadruhtnew_rr:
2904 case Hexagon::L4_ploadruhfnew_rr:
2905 case Hexagon::L2_ploadruhtnew_pi:
2906 case Hexagon::L2_ploadruhfnew_pi:
2907 case Hexagon::L4_ploadruht_abs:
2908 case Hexagon::L4_ploadruhf_abs:
2909 case Hexagon::L4_ploadruhtnew_abs:
2910 case Hexagon::L4_ploadruhfnew_abs:
2911 case Hexagon::L2_loadruhgp:
2912 return true;
2913 default:
2914 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002915 }
2916}
2917
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002918// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002919bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2920 const MachineInstr &MI2) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002921 if (isHVXVec(MI1) && isHVXVec(MI2))
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002922 if (!isVecUsableNextPacket(MI1, MI2))
2923 return true;
2924 return false;
2925}
2926
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002927/// Get the base register and byte offset of a load/store instr.
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00002928bool HexagonInstrInfo::getMemOperandWithOffset(
Bjorn Pettersson238c9d6302019-04-19 09:08:38 +00002929 const MachineInstr &LdSt, const MachineOperand *&BaseOp, int64_t &Offset,
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00002930 const TargetRegisterInfo *TRI) const {
Brendon Cahoon254f8892016-07-29 16:44:44 +00002931 unsigned AccessSize = 0;
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00002932 BaseOp = getBaseAndOffset(LdSt, Offset, AccessSize);
Simon Pilgrim74c371d2018-12-07 11:10:03 +00002933 assert((!BaseOp || BaseOp->isReg()) &&
2934 "getMemOperandWithOffset only supports base "
2935 "operands of type register.");
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00002936 return BaseOp != nullptr;
Brendon Cahoon254f8892016-07-29 16:44:44 +00002937}
2938
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002939/// Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002940bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2941 const MachineInstr &Second) const {
Krzysztof Parzyszek4763c2d2017-05-03 15:33:09 +00002942 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2943 const MachineOperand &Op = Second.getOperand(0);
2944 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2945 return true;
2946 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002947 if (DisableNVSchedule)
2948 return false;
2949 if (mayBeNewStore(Second)) {
2950 // Make sure the definition of the first instruction is the value being
2951 // stored.
2952 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002953 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002954 if (!Stored.isReg())
2955 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002956 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2957 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002958 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2959 return true;
2960 }
2961 }
2962 return false;
2963}
2964
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002965bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2966 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002967 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002968}
2969
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002970bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2971 for (auto &I : *B)
2972 if (I.isEHLabel())
2973 return true;
2974 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002975}
2976
Jyotsna Verma84256432013-03-01 17:37:13 +00002977// Returns true if an instruction can be converted into a non-extended
2978// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002979bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002980 short NonExtOpcode;
2981 // Check if the instruction has a register form that uses register in place
2982 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002983 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002984 return true;
2985
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002986 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002987 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002988
2989 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002990 case HexagonII::Absolute:
Jyotsna Verma84256432013-03-01 17:37:13 +00002991 // Load/store with absolute addressing mode can be converted into
2992 // base+offset mode.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002993 NonExtOpcode = Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002994 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002995 case HexagonII::BaseImmOffset:
Jyotsna Verma84256432013-03-01 17:37:13 +00002996 // Load/store with base+offset addressing mode can be converted into
2997 // base+register offset addressing mode. However left shift operand should
2998 // be set to 0.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002999 NonExtOpcode = Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003000 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003001 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00003002 NonExtOpcode = Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003003 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00003004 default:
3005 return false;
3006 }
3007 if (NonExtOpcode < 0)
3008 return false;
3009 return true;
3010 }
3011 return false;
3012}
3013
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003014bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
3015 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003016 Hexagon::InstrType_Pseudo) >= 0;
3017}
3018
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003019bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
3020 const {
3021 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3022 while (I != E) {
3023 if (I->isBarrier())
3024 return true;
3025 ++I;
3026 }
3027 return false;
3028}
3029
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003030// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003031bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003032 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003033 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00003034 Subtarget.hasV60Ops();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003035}
3036
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003037// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003038bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
Krzysztof Parzyszekf66f7612018-05-14 20:41:04 +00003039 if (MI.mayStore() && !Subtarget.useNewValueStores())
3040 return false;
3041
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003042 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003043 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3044}
3045
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003046bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3047 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003048 // There is no stall when ProdMI is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003049 if (!isHVXVec(ProdMI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003050 return false;
3051
3052 // There is no stall when ProdMI and ConsMI are not dependent.
3053 if (!isDependent(ProdMI, ConsMI))
3054 return false;
3055
3056 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3057 // are scheduled in consecutive packets.
3058 if (isVecUsableNextPacket(ProdMI, ConsMI))
3059 return false;
3060
3061 return true;
3062}
3063
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003064bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003065 MachineBasicBlock::const_instr_iterator BII) const {
3066 // There is no stall when I is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003067 if (!isHVXVec(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003068 return false;
3069
3070 MachineBasicBlock::const_instr_iterator MII = BII;
3071 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3072
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003073 if (!MII->isBundle())
3074 return producesStall(*MII, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003075
3076 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003077 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003078 if (producesStall(J, MI))
3079 return true;
3080 }
3081 return false;
3082}
3083
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003084bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003085 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00003086 for (const MachineOperand &MO : MI.operands()) {
3087 // Predicate register must be explicitly defined.
3088 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
3089 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003090 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00003091 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003092 }
3093
Krzysztof Parzyszekdd9415d2018-06-11 18:45:52 +00003094 // Instruction that produce late predicate cannot be used as sources of
3095 // dot-new.
3096 switch (MI.getOpcode()) {
3097 case Hexagon::A4_addp_c:
3098 case Hexagon::A4_subp_c:
3099 case Hexagon::A4_tlbmatch:
3100 case Hexagon::A5_ACS:
3101 case Hexagon::F2_sfinvsqrta:
3102 case Hexagon::F2_sfrecipa:
3103 case Hexagon::J2_endloop0:
3104 case Hexagon::J2_endloop01:
3105 case Hexagon::J2_ploop1si:
3106 case Hexagon::J2_ploop1sr:
3107 case Hexagon::J2_ploop2si:
3108 case Hexagon::J2_ploop2sr:
3109 case Hexagon::J2_ploop3si:
3110 case Hexagon::J2_ploop3sr:
3111 case Hexagon::S2_cabacdecbin:
3112 case Hexagon::S2_storew_locked:
3113 case Hexagon::S4_stored_locked:
3114 return false;
3115 }
3116 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003117}
3118
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003119bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003120 return Opcode == Hexagon::J2_jumpt ||
3121 Opcode == Hexagon::J2_jumptpt ||
3122 Opcode == Hexagon::J2_jumpf ||
3123 Opcode == Hexagon::J2_jumpfpt ||
3124 Opcode == Hexagon::J2_jumptnew ||
3125 Opcode == Hexagon::J2_jumpfnew ||
3126 Opcode == Hexagon::J2_jumptnewpt ||
3127 Opcode == Hexagon::J2_jumpfnewpt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003128}
3129
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003130bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3131 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3132 return false;
3133 return !isPredicatedTrue(Cond[0].getImm());
3134}
3135
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003136unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3137 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003138 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3139}
3140
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003141// Returns the base register in a memory access (load/store). The offset is
3142// returned in Offset and the access size is returned in AccessSize.
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00003143// If the base operand has a subregister or the offset field does not contain
3144// an immediate value, return nullptr.
3145MachineOperand *HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
3146 int64_t &Offset,
3147 unsigned &AccessSize) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003148 // Return if it is not a base+offset type instruction or a MemOp.
3149 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3150 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003151 !isMemOp(MI) && !isPostIncrement(MI))
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00003152 return nullptr;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003153
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003154 AccessSize = getMemAccessSize(MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003155
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003156 unsigned BasePos = 0, OffsetPos = 0;
3157 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00003158 return nullptr;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003159
3160 // Post increment updates its EA after the mem access,
3161 // so we need to treat its offset as zero.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003162 if (isPostIncrement(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003163 Offset = 0;
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003164 } else {
3165 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
3166 if (!OffsetOp.isImm())
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00003167 return nullptr;
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003168 Offset = OffsetOp.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003169 }
3170
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003171 const MachineOperand &BaseOp = MI.getOperand(BasePos);
3172 if (BaseOp.getSubReg() != 0)
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00003173 return nullptr;
3174 return &const_cast<MachineOperand&>(BaseOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003175}
3176
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003177/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003178bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003179 unsigned &BasePos, unsigned &OffsetPos) const {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003180 if (!isAddrModeWithOffset(MI) && !isPostIncrement(MI))
3181 return false;
3182
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003183 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003184 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003185 BasePos = 0;
3186 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003187 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003188 BasePos = 0;
3189 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003190 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003191 BasePos = 1;
3192 OffsetPos = 2;
3193 } else
3194 return false;
3195
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003196 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003197 BasePos++;
3198 OffsetPos++;
3199 }
3200 if (isPostIncrement(MI)) {
3201 BasePos++;
3202 OffsetPos++;
3203 }
3204
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003205 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003206 return false;
3207
3208 return true;
3209}
3210
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003211// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003212// e.g. jump_t t1 (i1)
3213// jump t2 (i2)
3214// Jumpers = {i2, i1}
3215SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3216 MachineBasicBlock& MBB) const {
3217 SmallVector<MachineInstr*, 2> Jumpers;
3218 // If the block has no terminators, it just falls into the block after it.
3219 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3220 if (I == MBB.instr_begin())
3221 return Jumpers;
3222
3223 // A basic block may looks like this:
3224 //
3225 // [ insn
3226 // EH_LABEL
3227 // insn
3228 // insn
3229 // insn
3230 // EH_LABEL
3231 // insn ]
3232 //
3233 // It has two succs but does not have a terminator
3234 // Don't know how to handle it.
3235 do {
3236 --I;
3237 if (I->isEHLabel())
3238 return Jumpers;
3239 } while (I != MBB.instr_begin());
3240
3241 I = MBB.instr_end();
3242 --I;
3243
Shiva Chen801bf7e2018-05-09 02:42:00 +00003244 while (I->isDebugInstr()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003245 if (I == MBB.instr_begin())
3246 return Jumpers;
3247 --I;
3248 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003249 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003250 return Jumpers;
3251
3252 // Get the last instruction in the block.
3253 MachineInstr *LastInst = &*I;
3254 Jumpers.push_back(LastInst);
3255 MachineInstr *SecondLastInst = nullptr;
3256 // Find one more terminator if present.
3257 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003258 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003259 if (!SecondLastInst) {
3260 SecondLastInst = &*I;
3261 Jumpers.push_back(SecondLastInst);
3262 } else // This is a third branch.
3263 return Jumpers;
3264 }
3265 if (I == MBB.instr_begin())
3266 break;
3267 --I;
3268 } while (true);
3269 return Jumpers;
3270}
3271
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003272// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003273unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3274 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003275 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3276}
3277
3278// See if instruction could potentially be a duplex candidate.
3279// If so, return its group. Zero otherwise.
3280HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003281 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003282 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3283
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003284 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003285 default:
3286 return HexagonII::HCG_None;
3287 //
3288 // Compound pairs.
3289 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3290 // "Rd16=#U6 ; jump #r9:2"
3291 // "Rd16=Rs16 ; jump #r9:2"
3292 //
3293 case Hexagon::C2_cmpeq:
3294 case Hexagon::C2_cmpgt:
3295 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003296 DstReg = MI.getOperand(0).getReg();
3297 Src1Reg = MI.getOperand(1).getReg();
3298 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003299 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3300 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3301 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3302 return HexagonII::HCG_A;
3303 break;
3304 case Hexagon::C2_cmpeqi:
3305 case Hexagon::C2_cmpgti:
3306 case Hexagon::C2_cmpgtui:
3307 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003308 DstReg = MI.getOperand(0).getReg();
3309 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003310 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3311 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003312 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3313 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3314 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003315 return HexagonII::HCG_A;
3316 break;
3317 case Hexagon::A2_tfr:
3318 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003319 DstReg = MI.getOperand(0).getReg();
3320 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003321 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3322 return HexagonII::HCG_A;
3323 break;
3324 case Hexagon::A2_tfrsi:
3325 // Rd = #u6
3326 // Do not test for #u6 size since the const is getting extended
3327 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003328 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003329 if (isIntRegForSubInst(DstReg))
3330 return HexagonII::HCG_A;
3331 break;
3332 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003333 DstReg = MI.getOperand(0).getReg();
3334 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003335 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3336 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003337 MI.getOperand(2).isImm() &&
3338 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003339 return HexagonII::HCG_A;
3340 break;
3341 // The fact that .new form is used pretty much guarantees
3342 // that predicate register will match. Nevertheless,
3343 // there could be some false positives without additional
3344 // checking.
3345 case Hexagon::J2_jumptnew:
3346 case Hexagon::J2_jumpfnew:
3347 case Hexagon::J2_jumptnewpt:
3348 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003349 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003350 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3351 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3352 return HexagonII::HCG_B;
3353 break;
3354 // Transfer and jump:
3355 // Rd=#U6 ; jump #r9:2
3356 // Rd=Rs ; jump #r9:2
3357 // Do not test for jump range here.
3358 case Hexagon::J2_jump:
3359 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003360 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003361 return HexagonII::HCG_C;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003362 }
3363
3364 return HexagonII::HCG_None;
3365}
3366
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003367// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003368unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3369 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003370 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3371 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003372 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3373 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003374 return -1u;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003375 unsigned DestReg = GA.getOperand(0).getReg();
3376 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003377 return -1u;
3378 if (DestReg != Hexagon::P0 && DestReg != Hexagon::P1)
3379 return -1u;
3380 // The value compared against must be either u5 or -1.
3381 const MachineOperand &CmpOp = GA.getOperand(2);
3382 if (!CmpOp.isImm())
3383 return -1u;
3384 int V = CmpOp.getImm();
3385 if (V == -1)
3386 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqn1_tp0_jump_nt
3387 : Hexagon::J4_cmpeqn1_tp1_jump_nt;
3388 if (!isUInt<5>(V))
3389 return -1u;
3390 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqi_tp0_jump_nt
3391 : Hexagon::J4_cmpeqi_tp1_jump_nt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003392}
3393
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003394int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3395 enum Hexagon::PredSense inPredSense;
3396 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3397 Hexagon::PredSense_true;
3398 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3399 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3400 return CondOpcode;
3401
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003402 llvm_unreachable("Unexpected predicable instruction");
3403}
3404
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003405// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003406int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3407 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003408 default: llvm_unreachable("Unknown .cur type");
3409 case Hexagon::V6_vL32b_pi:
3410 return Hexagon::V6_vL32b_cur_pi;
3411 case Hexagon::V6_vL32b_ai:
3412 return Hexagon::V6_vL32b_cur_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003413 case Hexagon::V6_vL32b_nt_pi:
3414 return Hexagon::V6_vL32b_nt_cur_pi;
3415 case Hexagon::V6_vL32b_nt_ai:
3416 return Hexagon::V6_vL32b_nt_cur_ai;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003417 }
3418 return 0;
3419}
3420
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003421// Return the regular version of the .cur instruction.
3422int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3423 switch (MI.getOpcode()) {
3424 default: llvm_unreachable("Unknown .cur type");
3425 case Hexagon::V6_vL32b_cur_pi:
3426 return Hexagon::V6_vL32b_pi;
3427 case Hexagon::V6_vL32b_cur_ai:
3428 return Hexagon::V6_vL32b_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003429 case Hexagon::V6_vL32b_nt_cur_pi:
3430 return Hexagon::V6_vL32b_nt_pi;
3431 case Hexagon::V6_vL32b_nt_cur_ai:
3432 return Hexagon::V6_vL32b_nt_ai;
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003433 }
3434 return 0;
3435}
3436
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003437// The diagram below shows the steps involved in the conversion of a predicated
3438// store instruction to its .new predicated new-value form.
3439//
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003440// Note: It doesn't include conditional new-value stores as they can't be
3441// converted to .new predicate.
3442//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003443// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3444// ^ ^
3445// / \ (not OK. it will cause new-value store to be
3446// / X conditional on p0.new while R2 producer is
3447// / \ on p0)
3448// / \.
3449// p.new store p.old NV store
3450// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3451// ^ ^
3452// \ /
3453// \ /
3454// \ /
3455// p.old store
3456// [if (p0)memw(R0+#0)=R2]
3457//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003458// The following set of instructions further explains the scenario where
3459// conditional new-value store becomes invalid when promoted to .new predicate
3460// form.
3461//
3462// { 1) if (p0) r0 = add(r1, r2)
3463// 2) p0 = cmp.eq(r3, #0) }
3464//
3465// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3466// the first two instructions because in instr 1, r0 is conditional on old value
3467// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3468// is not valid for new-value stores.
3469// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3470// from the "Conditional Store" list. Because a predicated new value store
3471// would NOT be promoted to a double dot new store. See diagram below:
3472// This function returns yes for those stores that are predicated but not
3473// yet promoted to predicate dot new instructions.
3474//
3475// +---------------------+
3476// /-----| if (p0) memw(..)=r0 |---------\~
3477// || +---------------------+ ||
3478// promote || /\ /\ || promote
3479// || /||\ /||\ ||
3480// \||/ demote || \||/
3481// \/ || || \/
3482// +-------------------------+ || +-------------------------+
3483// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3484// +-------------------------+ || +-------------------------+
3485// || || ||
3486// || demote \||/
3487// promote || \/ NOT possible
3488// || || /\~
3489// \||/ || /||\~
3490// \/ || ||
3491// +-----------------------------+
3492// | if (p0.new) memw(..)=r0.new |
3493// +-----------------------------+
3494// Double Dot New Store
3495//
3496// Returns the most basic instruction for the .new predicated instructions and
3497// new-value stores.
3498// For example, all of the following instructions will be converted back to the
3499// same instruction:
3500// 1) if (p0.new) memw(R0+#0) = R1.new --->
3501// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3502// 3) if (p0.new) memw(R0+#0) = R1 --->
3503//
3504// To understand the translation of instruction 1 to its original form, consider
3505// a packet with 3 instructions.
3506// { p0 = cmp.eq(R0,R1)
3507// if (p0.new) R2 = add(R3, R4)
3508// R5 = add (R3, R1)
3509// }
3510// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3511//
3512// This instruction can be part of the previous packet only if both p0 and R2
3513// are promoted to .new values. This promotion happens in steps, first
3514// predicate register is promoted to .new and in the next iteration R2 is
3515// promoted. Therefore, in case of dependence check failure (due to R5) during
3516// next iteration, it should be converted back to its most basic form.
3517
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003518// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003519int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3520 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003521 if (NVOpcode >= 0) // Valid new-value store instruction.
3522 return NVOpcode;
3523
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003524 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003525 default:
Eugene Zelenko3b873362017-09-28 22:27:31 +00003526 report_fatal_error(std::string("Unknown .new type: ") +
3527 std::to_string(MI.getOpcode()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003528 case Hexagon::S4_storerb_ur:
3529 return Hexagon::S4_storerbnew_ur;
3530
3531 case Hexagon::S2_storerb_pci:
3532 return Hexagon::S2_storerb_pci;
3533
3534 case Hexagon::S2_storeri_pci:
3535 return Hexagon::S2_storeri_pci;
3536
3537 case Hexagon::S2_storerh_pci:
3538 return Hexagon::S2_storerh_pci;
3539
3540 case Hexagon::S2_storerd_pci:
3541 return Hexagon::S2_storerd_pci;
3542
3543 case Hexagon::S2_storerf_pci:
3544 return Hexagon::S2_storerf_pci;
3545
3546 case Hexagon::V6_vS32b_ai:
3547 return Hexagon::V6_vS32b_new_ai;
3548
3549 case Hexagon::V6_vS32b_pi:
3550 return Hexagon::V6_vS32b_new_pi;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003551 }
3552 return 0;
3553}
3554
3555// Returns the opcode to use when converting MI, which is a conditional jump,
3556// into a conditional instruction which uses the .new value of the predicate.
3557// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003558// If MBPI is null, all edges will be treated as equally likely for the
3559// purposes of establishing a predication hint.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003560int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003561 const MachineBranchProbabilityInfo *MBPI) const {
3562 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003563 const MachineBasicBlock *Src = MI.getParent();
3564 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003565 bool Taken = false;
3566 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003567
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003568 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3569 const MachineBasicBlock *Dst) {
3570 if (MBPI)
3571 return MBPI->getEdgeProbability(Src, Dst);
3572 return BranchProbability(1, Src->succ_size());
3573 };
3574
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003575 if (BrTarget.isMBB()) {
3576 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003577 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003578 } else {
3579 // The branch target is not a basic block (most likely a function).
3580 // Since BPI only gives probabilities for targets that are basic blocks,
3581 // try to identify another target of this branch (potentially a fall-
3582 // -through) and check the probability of that target.
3583 //
3584 // The only handled branch combinations are:
3585 // - one conditional branch,
3586 // - one conditional branch followed by one unconditional branch.
3587 // Otherwise, assume not-taken.
3588 assert(MI.isConditionalBranch());
3589 const MachineBasicBlock &B = *MI.getParent();
3590 bool SawCond = false, Bad = false;
3591 for (const MachineInstr &I : B) {
3592 if (!I.isBranch())
3593 continue;
3594 if (I.isConditionalBranch()) {
3595 SawCond = true;
3596 if (&I != &MI) {
3597 Bad = true;
3598 break;
3599 }
3600 }
3601 if (I.isUnconditionalBranch() && !SawCond) {
3602 Bad = true;
3603 break;
3604 }
3605 }
3606 if (!Bad) {
3607 MachineBasicBlock::const_instr_iterator It(MI);
3608 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3609 if (NextIt == B.instr_end()) {
3610 // If this branch is the last, look for the fall-through block.
3611 for (const MachineBasicBlock *SB : B.successors()) {
3612 if (!B.isLayoutSuccessor(SB))
3613 continue;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003614 Taken = getEdgeProbability(Src, SB) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003615 break;
3616 }
3617 } else {
3618 assert(NextIt->isUnconditionalBranch());
3619 // Find the first MBB operand and assume it's the target.
3620 const MachineBasicBlock *BT = nullptr;
3621 for (const MachineOperand &Op : NextIt->operands()) {
3622 if (!Op.isMBB())
3623 continue;
3624 BT = Op.getMBB();
3625 break;
3626 }
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003627 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003628 }
3629 } // if (!Bad)
3630 }
3631
3632 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003633
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003634 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003635 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003636 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003637 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003638 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003639
3640 default:
3641 llvm_unreachable("Unexpected jump instruction.");
3642 }
3643}
3644
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003645// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003646int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003647 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003648 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003649 // Condtional Jumps
3650 case Hexagon::J2_jumpt:
3651 case Hexagon::J2_jumpf:
3652 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003653 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003654
3655 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3656 if (NewOpcode >= 0)
3657 return NewOpcode;
Krzysztof Parzyszek066e8b52017-06-02 14:07:06 +00003658 return 0;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003659}
3660
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003661int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
3662 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003663 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3664 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003665 // All Hexagon architectures have prediction bits on dot-new branches,
3666 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3667 // to pick the right opcode when converting back to dot-old.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003668 if (!Subtarget.getFeatureBits()[Hexagon::ArchV60]) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003669 switch (NewOp) {
3670 case Hexagon::J2_jumptpt:
3671 NewOp = Hexagon::J2_jumpt;
3672 break;
3673 case Hexagon::J2_jumpfpt:
3674 NewOp = Hexagon::J2_jumpf;
3675 break;
3676 case Hexagon::J2_jumprtpt:
3677 NewOp = Hexagon::J2_jumprt;
3678 break;
3679 case Hexagon::J2_jumprfpt:
3680 NewOp = Hexagon::J2_jumprf;
3681 break;
3682 }
3683 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003684 assert(NewOp >= 0 &&
3685 "Couldn't change predicate new instruction to its old form.");
3686 }
3687
3688 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3689 NewOp = Hexagon::getNonNVStore(NewOp);
3690 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3691 }
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003692
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00003693 if (Subtarget.hasV60Ops())
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003694 return NewOp;
3695
3696 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3697 switch (NewOp) {
3698 case Hexagon::J2_jumpfpt:
3699 return Hexagon::J2_jumpf;
3700 case Hexagon::J2_jumptpt:
3701 return Hexagon::J2_jumpt;
3702 case Hexagon::J2_jumprfpt:
3703 return Hexagon::J2_jumprf;
3704 case Hexagon::J2_jumprtpt:
3705 return Hexagon::J2_jumprt;
3706 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003707 return NewOp;
3708}
3709
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003710// See if instruction could potentially be a duplex candidate.
3711// If so, return its group. Zero otherwise.
3712HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003713 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003714 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003715 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003716
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003717 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003718 default:
3719 return HexagonII::HSIG_None;
3720 //
3721 // Group L1:
3722 //
3723 // Rd = memw(Rs+#u4:2)
3724 // Rd = memub(Rs+#u4:0)
3725 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003726 DstReg = MI.getOperand(0).getReg();
3727 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003728 // Special case this one from Group L2.
3729 // Rd = memw(r29+#u5:2)
3730 if (isIntRegForSubInst(DstReg)) {
3731 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3732 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003733 MI.getOperand(2).isImm() &&
3734 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003735 return HexagonII::HSIG_L2;
3736 // Rd = memw(Rs+#u4:2)
3737 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003738 (MI.getOperand(2).isImm() &&
3739 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003740 return HexagonII::HSIG_L1;
3741 }
3742 break;
3743 case Hexagon::L2_loadrub_io:
3744 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003745 DstReg = MI.getOperand(0).getReg();
3746 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003747 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003748 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003749 return HexagonII::HSIG_L1;
3750 break;
3751 //
3752 // Group L2:
3753 //
3754 // Rd = memh/memuh(Rs+#u3:1)
3755 // Rd = memb(Rs+#u3:0)
3756 // Rd = memw(r29+#u5:2) - Handled above.
3757 // Rdd = memd(r29+#u5:3)
3758 // deallocframe
3759 // [if ([!]p0[.new])] dealloc_return
3760 // [if ([!]p0[.new])] jumpr r31
3761 case Hexagon::L2_loadrh_io:
3762 case Hexagon::L2_loadruh_io:
3763 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003764 DstReg = MI.getOperand(0).getReg();
3765 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003766 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003767 MI.getOperand(2).isImm() &&
3768 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003769 return HexagonII::HSIG_L2;
3770 break;
3771 case Hexagon::L2_loadrb_io:
3772 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003773 DstReg = MI.getOperand(0).getReg();
3774 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003775 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003776 MI.getOperand(2).isImm() &&
3777 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003778 return HexagonII::HSIG_L2;
3779 break;
3780 case Hexagon::L2_loadrd_io:
3781 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003782 DstReg = MI.getOperand(0).getReg();
3783 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003784 if (isDblRegForSubInst(DstReg, HRI) &&
3785 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3786 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003787 MI.getOperand(2).isImm() &&
3788 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003789 return HexagonII::HSIG_L2;
3790 break;
3791 // dealloc_return is not documented in Hexagon Manual, but marked
3792 // with A_SUBINSN attribute in iset_v4classic.py.
3793 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003794 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003795 case Hexagon::L4_return:
3796 case Hexagon::L2_deallocframe:
3797 return HexagonII::HSIG_L2;
3798 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003799 case Hexagon::PS_jmpret:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003800 case Hexagon::SL2_jumpr31:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003801 // jumpr r31
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003802 // Actual form JMPR implicit-def %pc, implicit %r31, implicit internal %r0
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003803 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003804 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3805 return HexagonII::HSIG_L2;
3806 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003807 case Hexagon::PS_jmprett:
3808 case Hexagon::PS_jmpretf:
3809 case Hexagon::PS_jmprettnewpt:
3810 case Hexagon::PS_jmpretfnewpt:
3811 case Hexagon::PS_jmprettnew:
3812 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003813 case Hexagon::SL2_jumpr31_t:
3814 case Hexagon::SL2_jumpr31_f:
3815 case Hexagon::SL2_jumpr31_tnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003816 DstReg = MI.getOperand(1).getReg();
3817 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003818 // [if ([!]p0[.new])] jumpr r31
3819 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3820 (Hexagon::P0 == SrcReg)) &&
3821 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3822 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003823 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00003824 case Hexagon::L4_return_t:
3825 case Hexagon::L4_return_f:
3826 case Hexagon::L4_return_tnew_pnt:
3827 case Hexagon::L4_return_fnew_pnt:
3828 case Hexagon::L4_return_tnew_pt:
3829 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003830 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003831 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003832 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3833 return HexagonII::HSIG_L2;
3834 break;
3835 //
3836 // Group S1:
3837 //
3838 // memw(Rs+#u4:2) = Rt
3839 // memb(Rs+#u4:0) = Rt
3840 case Hexagon::S2_storeri_io:
3841 // Special case this one from Group S2.
3842 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003843 Src1Reg = MI.getOperand(0).getReg();
3844 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003845 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3846 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003847 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3848 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003849 return HexagonII::HSIG_S2;
3850 // memw(Rs+#u4:2) = Rt
3851 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003852 MI.getOperand(1).isImm() &&
3853 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003854 return HexagonII::HSIG_S1;
3855 break;
3856 case Hexagon::S2_storerb_io:
3857 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003858 Src1Reg = MI.getOperand(0).getReg();
3859 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003860 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003861 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003862 return HexagonII::HSIG_S1;
3863 break;
3864 //
3865 // Group S2:
3866 //
3867 // memh(Rs+#u3:1) = Rt
3868 // memw(r29+#u5:2) = Rt
3869 // memd(r29+#s6:3) = Rtt
3870 // memw(Rs+#u4:2) = #U1
3871 // memb(Rs+#u4) = #U1
3872 // allocframe(#u5:3)
3873 case Hexagon::S2_storerh_io:
3874 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003875 Src1Reg = MI.getOperand(0).getReg();
3876 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003877 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003878 MI.getOperand(1).isImm() &&
3879 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003880 return HexagonII::HSIG_S1;
3881 break;
3882 case Hexagon::S2_storerd_io:
3883 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003884 Src1Reg = MI.getOperand(0).getReg();
3885 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003886 if (isDblRegForSubInst(Src2Reg, HRI) &&
3887 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003888 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3889 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003890 return HexagonII::HSIG_S2;
3891 break;
3892 case Hexagon::S4_storeiri_io:
3893 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003894 Src1Reg = MI.getOperand(0).getReg();
3895 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3896 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3897 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003898 return HexagonII::HSIG_S2;
3899 break;
3900 case Hexagon::S4_storeirb_io:
3901 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003902 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003903 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003904 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3905 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003906 return HexagonII::HSIG_S2;
3907 break;
3908 case Hexagon::S2_allocframe:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003909 if (MI.getOperand(2).isImm() &&
3910 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003911 return HexagonII::HSIG_S1;
3912 break;
3913 //
3914 // Group A:
3915 //
3916 // Rx = add(Rx,#s7)
3917 // Rd = Rs
3918 // Rd = #u6
3919 // Rd = #-1
3920 // if ([!]P0[.new]) Rd = #0
3921 // Rd = add(r29,#u6:2)
3922 // Rx = add(Rx,Rs)
3923 // P0 = cmp.eq(Rs,#u2)
3924 // Rdd = combine(#0,Rs)
3925 // Rdd = combine(Rs,#0)
3926 // Rdd = combine(#u2,#U2)
3927 // Rd = add(Rs,#1)
3928 // Rd = add(Rs,#-1)
3929 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3930 // Rd = and(Rs,#1)
3931 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003932 DstReg = MI.getOperand(0).getReg();
3933 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003934 if (isIntRegForSubInst(DstReg)) {
3935 // Rd = add(r29,#u6:2)
3936 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003937 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3938 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003939 return HexagonII::HSIG_A;
3940 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003941 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3942 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003943 return HexagonII::HSIG_A;
3944 // Rd = add(Rs,#1)
3945 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003946 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3947 ((MI.getOperand(2).getImm() == 1) ||
3948 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003949 return HexagonII::HSIG_A;
3950 }
3951 break;
3952 case Hexagon::A2_add:
3953 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003954 DstReg = MI.getOperand(0).getReg();
3955 Src1Reg = MI.getOperand(1).getReg();
3956 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003957 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3958 isIntRegForSubInst(Src2Reg))
3959 return HexagonII::HSIG_A;
3960 break;
3961 case Hexagon::A2_andir:
3962 // Same as zxtb.
3963 // Rd16=and(Rs16,#255)
3964 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003965 DstReg = MI.getOperand(0).getReg();
3966 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003967 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003968 MI.getOperand(2).isImm() &&
3969 ((MI.getOperand(2).getImm() == 1) ||
3970 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003971 return HexagonII::HSIG_A;
3972 break;
3973 case Hexagon::A2_tfr:
3974 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003975 DstReg = MI.getOperand(0).getReg();
3976 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003977 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3978 return HexagonII::HSIG_A;
3979 break;
3980 case Hexagon::A2_tfrsi:
3981 // Rd = #u6
3982 // Do not test for #u6 size since the const is getting extended
3983 // regardless and compound could be formed.
3984 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003985 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003986 if (isIntRegForSubInst(DstReg))
3987 return HexagonII::HSIG_A;
3988 break;
3989 case Hexagon::C2_cmoveit:
3990 case Hexagon::C2_cmovenewit:
3991 case Hexagon::C2_cmoveif:
3992 case Hexagon::C2_cmovenewif:
3993 // if ([!]P0[.new]) Rd = #0
3994 // Actual form:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003995 // %r16 = C2_cmovenewit internal %p0, 0, implicit undef %r16;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003996 DstReg = MI.getOperand(0).getReg();
3997 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003998 if (isIntRegForSubInst(DstReg) &&
3999 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004000 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004001 return HexagonII::HSIG_A;
4002 break;
4003 case Hexagon::C2_cmpeqi:
4004 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004005 DstReg = MI.getOperand(0).getReg();
4006 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004007 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
4008 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004009 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004010 return HexagonII::HSIG_A;
4011 break;
4012 case Hexagon::A2_combineii:
4013 case Hexagon::A4_combineii:
4014 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004015 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004016 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004017 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
4018 (MI.getOperand(1).isGlobal() &&
4019 isUInt<2>(MI.getOperand(1).getOffset()))) &&
4020 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
4021 (MI.getOperand(2).isGlobal() &&
4022 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004023 return HexagonII::HSIG_A;
4024 break;
4025 case Hexagon::A4_combineri:
4026 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004027 DstReg = MI.getOperand(0).getReg();
4028 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004029 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004030 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
4031 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004032 return HexagonII::HSIG_A;
4033 break;
4034 case Hexagon::A4_combineir:
4035 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004036 DstReg = MI.getOperand(0).getReg();
4037 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004038 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004039 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
4040 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004041 return HexagonII::HSIG_A;
4042 break;
4043 case Hexagon::A2_sxtb:
4044 case Hexagon::A2_sxth:
4045 case Hexagon::A2_zxtb:
4046 case Hexagon::A2_zxth:
4047 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004048 DstReg = MI.getOperand(0).getReg();
4049 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004050 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
4051 return HexagonII::HSIG_A;
4052 break;
4053 }
4054
4055 return HexagonII::HSIG_None;
4056}
4057
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004058short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
4059 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004060}
4061
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004062unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004063 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004064 // Default to one cycle for no itinerary. However, an "empty" itinerary may
4065 // still have a MinLatency property, which getStageLatency checks.
4066 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004067 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004068
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004069 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004070 return 0;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004071 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
4072}
4073
4074/// getOperandLatency - Compute and return the use operand latency of a given
4075/// pair of def and use.
4076/// In most cases, the static scheduling itinerary was enough to determine the
4077/// operand latency. But it may not be possible for instructions with variable
4078/// number of defs / uses.
4079///
4080/// This is a raw interface to the itinerary that may be directly overriden by
4081/// a target. Use computeOperandLatency to get the best estimate of latency.
4082int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4083 const MachineInstr &DefMI,
4084 unsigned DefIdx,
4085 const MachineInstr &UseMI,
4086 unsigned UseIdx) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004087 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004088
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004089 // Get DefIdx and UseIdx for super registers.
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00004090 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004091
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00004092 if (DefMO.isReg() && HRI.isPhysicalRegister(DefMO.getReg())) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004093 if (DefMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004094 for (MCSuperRegIterator SR(DefMO.getReg(), &HRI); SR.isValid(); ++SR) {
4095 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004096 if (Idx != -1) {
4097 DefIdx = Idx;
4098 break;
4099 }
4100 }
4101 }
4102
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00004103 const MachineOperand &UseMO = UseMI.getOperand(UseIdx);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004104 if (UseMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004105 for (MCSuperRegIterator SR(UseMO.getReg(), &HRI); SR.isValid(); ++SR) {
4106 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00004107 if (Idx != -1) {
4108 UseIdx = Idx;
4109 break;
4110 }
4111 }
4112 }
4113 }
4114
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004115 int Latency = TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
4116 UseMI, UseIdx);
4117 if (!Latency)
4118 // We should never have 0 cycle latency between two instructions unless
4119 // they can be packetized together. However, this decision can't be made
4120 // here.
4121 Latency = 1;
4122 return Latency;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004123}
4124
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004125// inverts the predication logic.
4126// p -> NotP
4127// NotP -> P
4128bool HexagonInstrInfo::getInvertedPredSense(
4129 SmallVectorImpl<MachineOperand> &Cond) const {
4130 if (Cond.empty())
4131 return false;
4132 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4133 Cond[0].setImm(Opc);
4134 return true;
4135}
4136
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004137unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4138 int InvPredOpcode;
4139 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4140 : Hexagon::getTruePredOpcode(Opc);
4141 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4142 return InvPredOpcode;
4143
4144 llvm_unreachable("Unexpected predicated instruction");
4145}
4146
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004147// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004148int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4149 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004150 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4151 & HexagonII::ExtentSignedMask;
4152 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4153 & HexagonII::ExtentBitsMask;
4154
4155 if (isSigned) // if value is signed
4156 return ~(-1U << (bits - 1));
4157 else
4158 return ~(-1U << bits);
4159}
4160
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004161
4162bool HexagonInstrInfo::isAddrModeWithOffset(const MachineInstr &MI) const {
4163 switch (MI.getOpcode()) {
4164 case Hexagon::L2_loadrbgp:
4165 case Hexagon::L2_loadrdgp:
4166 case Hexagon::L2_loadrhgp:
4167 case Hexagon::L2_loadrigp:
4168 case Hexagon::L2_loadrubgp:
4169 case Hexagon::L2_loadruhgp:
4170 case Hexagon::S2_storerbgp:
4171 case Hexagon::S2_storerbnewgp:
4172 case Hexagon::S2_storerhgp:
4173 case Hexagon::S2_storerhnewgp:
4174 case Hexagon::S2_storerigp:
4175 case Hexagon::S2_storerinewgp:
4176 case Hexagon::S2_storerdgp:
4177 case Hexagon::S2_storerfgp:
4178 return true;
4179 }
4180 const uint64_t F = MI.getDesc().TSFlags;
4181 unsigned addrMode =
4182 ((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
4183 // Disallow any base+offset instruction. The assembler does not yet reorder
4184 // based up any zero offset instruction.
4185 return (addrMode == HexagonII::BaseRegOffset ||
4186 addrMode == HexagonII::BaseImmOffset ||
4187 addrMode == HexagonII::BaseLongOffset);
4188}
4189
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004190unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004191 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00004192
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004193 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004194 unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
4195 unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
4196 if (Size != 0)
4197 return Size;
4198
4199 // Handle vector access sizes.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004200 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004201 switch (S) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004202 case HexagonII::HVXVectorAccess:
4203 return HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004204 default:
4205 llvm_unreachable("Unexpected instruction");
4206 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004207}
4208
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004209// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004210int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4211 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004212 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4213 & HexagonII::ExtentSignedMask;
4214 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4215 & HexagonII::ExtentBitsMask;
4216
4217 if (isSigned) // if value is signed
4218 return -1U << (bits - 1);
4219 else
4220 return 0;
4221}
4222
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004223// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004224short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004225 // Check if the instruction has a register form that uses register in place
4226 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004227 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004228 if (NonExtOpcode >= 0)
4229 return NonExtOpcode;
4230
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004231 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004232 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004233 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00004234 case HexagonII::Absolute:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004235 return Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Eugene Zelenko3b873362017-09-28 22:27:31 +00004236 case HexagonII::BaseImmOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004237 return Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004238 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004239 return Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004240
Jyotsna Verma84256432013-03-01 17:37:13 +00004241 default:
4242 return -1;
4243 }
4244 }
4245 return -1;
4246}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004247
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004248bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004249 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004250 if (Cond.empty())
4251 return false;
4252 assert(Cond.size() == 2);
4253 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00004254 LLVM_DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004255 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004256 }
4257 PredReg = Cond[1].getReg();
4258 PredRegPos = 1;
4259 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4260 PredRegFlags = 0;
4261 if (Cond[1].isImplicit())
4262 PredRegFlags = RegState::Implicit;
4263 if (Cond[1].isUndef())
4264 PredRegFlags |= RegState::Undef;
4265 return true;
4266}
4267
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004268short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4269 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004270}
4271
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004272short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4273 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004274}
4275
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004276// Return the number of bytes required to encode the instruction.
4277// Hexagon instructions are fixed length, 4 bytes, unless they
4278// use a constant extender, which requires another 4 bytes.
4279// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004280unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
Shiva Chen801bf7e2018-05-09 02:42:00 +00004281 if (MI.isDebugInstr() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004282 return 0;
4283
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004284 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004285 if (!Size)
4286 // Assume the default insn size in case it cannot be determined
4287 // for whatever reason.
4288 Size = HEXAGON_INSTR_SIZE;
4289
4290 if (isConstExtended(MI) || isExtended(MI))
4291 Size += HEXAGON_INSTR_SIZE;
4292
4293 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004294 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4295 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004296 const MachineFunction *MF = MBB.getParent();
4297 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4298
4299 // Count the number of register definitions to find the asm string.
4300 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004301 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004302 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004303 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004304
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004305 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004306 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004307 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004308 Size = getInlineAsmLength(AsmStr, *MAI);
4309 }
4310
4311 return Size;
4312}
4313
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004314uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4315 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004316 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4317}
4318
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004319unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004320 const InstrItineraryData &II = *Subtarget.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004321 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004322
4323 return IS.getUnits();
4324}
4325
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004326// Calculate size of the basic block without debug instructions.
4327unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4328 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4329}
4330
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004331unsigned HexagonInstrInfo::nonDbgBundleSize(
4332 MachineBasicBlock::const_iterator BundleHead) const {
4333 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004334 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004335 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004336 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004337}
4338
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004339/// immediateExtend - Changes the instruction in place to one using an immediate
4340/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004341void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004342 assert((isExtendable(MI)||isConstExtended(MI)) &&
4343 "Instruction must be extendable");
4344 // Find which operand is extendable.
4345 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004346 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004347 // This needs to be something we understand.
4348 assert((MO.isMBB() || MO.isImm()) &&
4349 "Branch with unknown extendable field type");
4350 // Mark given operand as extended.
4351 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4352}
4353
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004354bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004355 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00004356 LLVM_DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to "
4357 << printMBBReference(*NewTarget);
4358 MI.dump(););
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004359 assert(MI.isBranch());
4360 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4361 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004362 // In general branch target is the last operand,
4363 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004364 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004365 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004366 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4367 MI.getOperand(TargetPos).setMBB(NewTarget);
4368 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004369 NewOpcode = reversePrediction(NewOpcode);
4370 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004371 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004372 return true;
4373}
4374
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004375void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4376 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4377 MachineFunction::iterator A = MF.begin();
4378 MachineBasicBlock &B = *A;
4379 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004380 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004381 MachineInstr *NewMI;
4382
4383 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4384 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004385 NewMI = BuildMI(B, I, DL, get(insn));
Nicola Zaghend34e60c2018-05-14 12:53:11 +00004386 LLVM_DEBUG(dbgs() << "\n"
4387 << getName(NewMI->getOpcode())
4388 << " Class: " << NewMI->getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004389 NewMI->eraseFromParent();
4390 }
4391 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4392}
4393
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004394// inverts the predication logic.
4395// p -> NotP
4396// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004397bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00004398 LLVM_DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004399 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004400 return true;
4401}
4402
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004403// Reverse the branch prediction.
4404unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4405 int PredRevOpcode = -1;
4406 if (isPredictedTaken(Opcode))
4407 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4408 else
4409 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4410 assert(PredRevOpcode > 0);
4411 return PredRevOpcode;
4412}
4413
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004414// TODO: Add more rigorous validation.
4415bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4416 const {
4417 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4418}
4419
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004420void HexagonInstrInfo::
4421setBundleNoShuf(MachineBasicBlock::instr_iterator MIB) const {
4422 assert(MIB->isBundle());
4423 MachineOperand &Operand = MIB->getOperand(0);
4424 if (Operand.isImm())
4425 Operand.setImm(Operand.getImm() | memShufDisabledMask);
4426 else
4427 MIB->addOperand(MachineOperand::CreateImm(memShufDisabledMask));
4428}
4429
4430bool HexagonInstrInfo::getBundleNoShuf(const MachineInstr &MIB) const {
4431 assert(MIB.isBundle());
4432 const MachineOperand &Operand = MIB.getOperand(0);
4433 return (Operand.isImm() && (Operand.getImm() & memShufDisabledMask) != 0);
4434}
4435
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004436// Addressing mode relations.
4437short HexagonInstrInfo::changeAddrMode_abs_io(short Opc) const {
4438 return Opc >= 0 ? Hexagon::changeAddrMode_abs_io(Opc) : Opc;
4439}
4440
4441short HexagonInstrInfo::changeAddrMode_io_abs(short Opc) const {
4442 return Opc >= 0 ? Hexagon::changeAddrMode_io_abs(Opc) : Opc;
4443}
4444
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004445short HexagonInstrInfo::changeAddrMode_io_pi(short Opc) const {
4446 return Opc >= 0 ? Hexagon::changeAddrMode_io_pi(Opc) : Opc;
4447}
4448
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004449short HexagonInstrInfo::changeAddrMode_io_rr(short Opc) const {
4450 return Opc >= 0 ? Hexagon::changeAddrMode_io_rr(Opc) : Opc;
4451}
4452
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004453short HexagonInstrInfo::changeAddrMode_pi_io(short Opc) const {
4454 return Opc >= 0 ? Hexagon::changeAddrMode_pi_io(Opc) : Opc;
4455}
4456
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004457short HexagonInstrInfo::changeAddrMode_rr_io(short Opc) const {
4458 return Opc >= 0 ? Hexagon::changeAddrMode_rr_io(Opc) : Opc;
4459}
4460
4461short HexagonInstrInfo::changeAddrMode_rr_ur(short Opc) const {
4462 return Opc >= 0 ? Hexagon::changeAddrMode_rr_ur(Opc) : Opc;
4463}
4464
4465short HexagonInstrInfo::changeAddrMode_ur_rr(short Opc) const {
4466 return Opc >= 0 ? Hexagon::changeAddrMode_ur_rr(Opc) : Opc;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004467}