blob: da4e5fbb8c03113beec1c1e974a8eba1d17173aa [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Hexagon implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "HexagonInstrInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000015#include "Hexagon.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 Zelenkob2ca1b32017-01-04 02:02:05 +000019#include "llvm/ADT/SmallPtrSet.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000021#include "llvm/ADT/StringRef.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000022#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000023#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
31#include "llvm/CodeGen/MachineLoopInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000033#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000035#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000036#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000037#include "llvm/MC/MCInstrDesc.h"
38#include "llvm/MC/MCInstrItineraries.h"
39#include "llvm/MC/MCRegisterInfo.h"
40#include "llvm/Support/BranchProbability.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000041#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000042#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000043#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000044#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000045#include "llvm/Support/raw_ostream.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetSubtargetInfo.h"
48#include <cassert>
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000049#include <cctype>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000050#include <cstdint>
51#include <cstring>
52#include <iterator>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054using namespace llvm;
55
Chandler Carruthe96dd892014-04-21 22:55:11 +000056#define DEBUG_TYPE "hexagon-instrinfo"
57
Chandler Carruthd174b722014-04-22 02:03:14 +000058#define GET_INSTRINFO_CTOR_DTOR
59#define GET_INSTRMAP_INFO
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +000060#include "HexagonDepTimingClasses.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000061#include "HexagonGenDFAPacketizer.inc"
62#include "HexagonGenInstrInfo.inc"
Chandler Carruthd174b722014-04-22 02:03:14 +000063
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000064cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000065 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
66 "packetization boundary."));
67
68static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
69 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
70
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000071static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
72 cl::Hidden, cl::ZeroOrMore, cl::init(false),
73 cl::desc("Disable schedule adjustment for new value stores."));
74
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000075static cl::opt<bool> EnableTimingClassLatency(
76 "enable-timing-class-latency", cl::Hidden, cl::init(false),
77 cl::desc("Enable timing class latency"));
78
79static cl::opt<bool> EnableALUForwarding(
80 "enable-alu-forwarding", cl::Hidden, cl::init(true),
81 cl::desc("Enable vec alu forwarding"));
82
83static cl::opt<bool> EnableACCForwarding(
84 "enable-acc-forwarding", cl::Hidden, cl::init(true),
85 cl::desc("Enable vec acc forwarding"));
86
87static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
88 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
89
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000090static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
91 cl::init(true), cl::Hidden, cl::ZeroOrMore,
92 cl::desc("Use the DFA based hazard recognizer."));
93
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094///
95/// Constants for Hexagon instructions.
96///
97const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000098const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000100const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000101const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000102const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000104const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000106const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000107const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000108const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000110const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000112const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000113const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000114const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +0000115const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
116const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
117const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
118const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000119
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000120// Pin the vtable to this file.
121void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000122
123HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000124 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000125 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000126
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000127static bool isIntRegForSubInst(unsigned Reg) {
128 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
129 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000130}
131
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000132static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000133 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
134 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000135}
136
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000137/// Calculate number of instructions excluding the debug instructions.
138static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
139 MachineBasicBlock::const_instr_iterator MIE) {
140 unsigned Count = 0;
141 for (; MIB != MIE; ++MIB) {
142 if (!MIB->isDebugValue())
143 ++Count;
144 }
145 return Count;
146}
147
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000148/// Find the hardware loop instruction used to set-up the specified loop.
149/// On Hexagon, we have two instructions used to set-up the hardware loop
150/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
151/// to indicate the end of a loop.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000152static MachineInstr *findLoopInstr(MachineBasicBlock *BB, unsigned EndLoopOp,
153 MachineBasicBlock *TargetBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000154 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000155 unsigned LOOPi;
156 unsigned LOOPr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000157 if (EndLoopOp == Hexagon::ENDLOOP0) {
158 LOOPi = Hexagon::J2_loop0i;
159 LOOPr = Hexagon::J2_loop0r;
160 } else { // EndLoopOp == Hexagon::EndLOOP1
161 LOOPi = Hexagon::J2_loop1i;
162 LOOPr = Hexagon::J2_loop1r;
163 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000164
Brendon Cahoondf43e682015-05-08 16:16:29 +0000165 // The loop set-up instruction will be in a predecessor block
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000166 for (MachineBasicBlock *PB : BB->predecessors()) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000167 // If this has been visited, already skip it.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000168 if (!Visited.insert(PB).second)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000169 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000170 if (PB == BB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000171 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000172 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
173 unsigned Opc = I->getOpcode();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000174 if (Opc == LOOPi || Opc == LOOPr)
175 return &*I;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000176 // We've reached a different loop, which means the loop01 has been
177 // removed.
178 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000179 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000180 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000181 // Check the predecessors for the LOOP instruction.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000182 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
183 return Loop;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000184 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000185 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000186}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000187
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000188/// Gather register def/uses from MI.
189/// This treats possible (predicated) defs as actually happening ones
190/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000191static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000192 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
193 Defs.clear();
194 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000195
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000196 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
197 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000198
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000199 if (!MO.isReg())
200 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000201
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000202 unsigned Reg = MO.getReg();
203 if (!Reg)
204 continue;
205
206 if (MO.isUse())
207 Uses.push_back(MO.getReg());
208
209 if (MO.isDef())
210 Defs.push_back(MO.getReg());
211 }
212}
213
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000214// Position dependent, so check twice for swap.
215static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
216 switch (Ga) {
217 case HexagonII::HSIG_None:
218 default:
219 return false;
220 case HexagonII::HSIG_L1:
221 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
222 case HexagonII::HSIG_L2:
223 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
224 Gb == HexagonII::HSIG_A);
225 case HexagonII::HSIG_S1:
226 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
227 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
228 case HexagonII::HSIG_S2:
229 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
230 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
231 Gb == HexagonII::HSIG_A);
232 case HexagonII::HSIG_A:
233 return (Gb == HexagonII::HSIG_A);
234 case HexagonII::HSIG_Compound:
235 return (Gb == HexagonII::HSIG_Compound);
236 }
237 return false;
238}
239
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000240/// isLoadFromStackSlot - If the specified machine instruction is a direct
241/// load from a stack slot, return the virtual or physical register number of
242/// the destination along with the FrameIndex of the loaded stack slot. If
243/// not, return 0. This predicate must return 0 if the instruction has
244/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000245unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000246 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000247 switch (MI.getOpcode()) {
248 default:
249 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000250 case Hexagon::L2_loadri_io:
251 case Hexagon::L2_loadrd_io:
252 case Hexagon::V6_vL32b_ai:
253 case Hexagon::V6_vL32b_ai_128B:
254 case Hexagon::V6_vL32Ub_ai:
255 case Hexagon::V6_vL32Ub_ai_128B:
256 case Hexagon::LDriw_pred:
257 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000258 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000259 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000260 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000261 case Hexagon::PS_vloadrw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000262 const MachineOperand OpFI = MI.getOperand(1);
263 if (!OpFI.isFI())
264 return 0;
265 const MachineOperand OpOff = MI.getOperand(2);
266 if (!OpOff.isImm() || OpOff.getImm() != 0)
267 return 0;
268 FrameIndex = OpFI.getIndex();
269 return MI.getOperand(0).getReg();
270 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000271
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000272 case Hexagon::L2_ploadrit_io:
273 case Hexagon::L2_ploadrif_io:
274 case Hexagon::L2_ploadrdt_io:
275 case Hexagon::L2_ploadrdf_io: {
276 const MachineOperand OpFI = MI.getOperand(2);
277 if (!OpFI.isFI())
278 return 0;
279 const MachineOperand OpOff = MI.getOperand(3);
280 if (!OpOff.isImm() || OpOff.getImm() != 0)
281 return 0;
282 FrameIndex = OpFI.getIndex();
283 return MI.getOperand(0).getReg();
284 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000285 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000286
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000287 return 0;
288}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000289
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000290/// isStoreToStackSlot - If the specified machine instruction is a direct
291/// store to a stack slot, return the virtual or physical register number of
292/// the source reg along with the FrameIndex of the loaded stack slot. If
293/// not, return 0. This predicate must return 0 if the instruction has
294/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000295unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000296 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000297 switch (MI.getOpcode()) {
298 default:
299 break;
300 case Hexagon::S2_storerb_io:
301 case Hexagon::S2_storerh_io:
302 case Hexagon::S2_storeri_io:
303 case Hexagon::S2_storerd_io:
304 case Hexagon::V6_vS32b_ai:
305 case Hexagon::V6_vS32b_ai_128B:
306 case Hexagon::V6_vS32Ub_ai:
307 case Hexagon::V6_vS32Ub_ai_128B:
308 case Hexagon::STriw_pred:
309 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000310 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000311 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000312 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000313 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000314 const MachineOperand &OpFI = MI.getOperand(0);
315 if (!OpFI.isFI())
316 return 0;
317 const MachineOperand &OpOff = MI.getOperand(1);
318 if (!OpOff.isImm() || OpOff.getImm() != 0)
319 return 0;
320 FrameIndex = OpFI.getIndex();
321 return MI.getOperand(2).getReg();
322 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000323
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000324 case Hexagon::S2_pstorerbt_io:
325 case Hexagon::S2_pstorerbf_io:
326 case Hexagon::S2_pstorerht_io:
327 case Hexagon::S2_pstorerhf_io:
328 case Hexagon::S2_pstorerit_io:
329 case Hexagon::S2_pstorerif_io:
330 case Hexagon::S2_pstorerdt_io:
331 case Hexagon::S2_pstorerdf_io: {
332 const MachineOperand &OpFI = MI.getOperand(1);
333 if (!OpFI.isFI())
334 return 0;
335 const MachineOperand &OpOff = MI.getOperand(2);
336 if (!OpOff.isImm() || OpOff.getImm() != 0)
337 return 0;
338 FrameIndex = OpFI.getIndex();
339 return MI.getOperand(3).getReg();
340 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000341 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000342
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000343 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000344}
345
Brendon Cahoondf43e682015-05-08 16:16:29 +0000346/// This function can analyze one/two way branching only and should (mostly) be
347/// called by target independent side.
348/// First entry is always the opcode of the branching instruction, except when
349/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
350/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
351/// e.g. Jump_c p will have
352/// Cond[0] = Jump_c
353/// Cond[1] = p
354/// HW-loop ENDLOOP:
355/// Cond[0] = ENDLOOP
356/// Cond[1] = MBB
357/// New value jump:
358/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
359/// Cond[1] = R
360/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000361///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000362bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000363 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000364 MachineBasicBlock *&FBB,
365 SmallVectorImpl<MachineOperand> &Cond,
366 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000367 TBB = nullptr;
368 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000369 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000370
371 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000372 MachineBasicBlock::instr_iterator I = MBB.instr_end();
373 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000374 return false;
375
376 // A basic block may looks like this:
377 //
378 // [ insn
379 // EH_LABEL
380 // insn
381 // insn
382 // insn
383 // EH_LABEL
384 // insn ]
385 //
386 // It has two succs but does not have a terminator
387 // Don't know how to handle it.
388 do {
389 --I;
390 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000391 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000392 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000393 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000394
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000395 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396 --I;
397
398 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000399 if (I == MBB.instr_begin())
400 return false;
401 --I;
402 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000403
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000404 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
405 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000406 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000407 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000408 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000409 DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000410 I->eraseFromParent();
411 I = MBB.instr_end();
412 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000413 return false;
414 --I;
415 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000416 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417 return false;
418
419 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000420 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000421 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000422 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000423 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000424 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000425 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000426 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000427 else
428 // This is a third branch.
429 return true;
430 }
431 if (I == MBB.instr_begin())
432 break;
433 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000434 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000435
436 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000437 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
438 // If the branch target is not a basic block, it could be a tail call.
439 // (It is, if the target is a function.)
440 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
441 return true;
442 if (SecLastOpcode == Hexagon::J2_jump &&
443 !SecondLastInst->getOperand(0).isMBB())
444 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000445
446 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000447 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000448
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000449 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
450 return true;
451
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000452 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000453 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000454 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455 TBB = LastInst->getOperand(0).getMBB();
456 return false;
457 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000458 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000459 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000460 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 Cond.push_back(LastInst->getOperand(0));
462 return false;
463 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000464 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000466 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000467 Cond.push_back(LastInst->getOperand(0));
468 return false;
469 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000470 // Only supporting rr/ri versions of new-value jumps.
471 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
472 TBB = LastInst->getOperand(2).getMBB();
473 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
474 Cond.push_back(LastInst->getOperand(0));
475 Cond.push_back(LastInst->getOperand(1));
476 return false;
477 }
478 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
479 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000480 // Otherwise, don't know what this is.
481 return true;
482 }
483
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000484 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000485 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000486 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000487 if (!SecondLastInst->getOperand(1).isMBB())
488 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000489 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000490 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000491 Cond.push_back(SecondLastInst->getOperand(0));
492 FBB = LastInst->getOperand(0).getMBB();
493 return false;
494 }
495
Brendon Cahoondf43e682015-05-08 16:16:29 +0000496 // Only supporting rr/ri versions of new-value jumps.
497 if (SecLastOpcodeHasNVJump &&
498 (SecondLastInst->getNumExplicitOperands() == 3) &&
499 (LastOpcode == Hexagon::J2_jump)) {
500 TBB = SecondLastInst->getOperand(2).getMBB();
501 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
502 Cond.push_back(SecondLastInst->getOperand(0));
503 Cond.push_back(SecondLastInst->getOperand(1));
504 FBB = LastInst->getOperand(0).getMBB();
505 return false;
506 }
507
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000508 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
509 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000510 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000511 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000512 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000513 if (AllowModify)
514 I->eraseFromParent();
515 return false;
516 }
517
Brendon Cahoondf43e682015-05-08 16:16:29 +0000518 // If the block ends with an ENDLOOP, and J2_jump, handle it.
519 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000520 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000521 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000522 Cond.push_back(SecondLastInst->getOperand(0));
523 FBB = LastInst->getOperand(0).getMBB();
524 return false;
525 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000526 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
527 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000528 // Otherwise, can't handle this.
529 return true;
530}
531
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000532unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000533 int *BytesRemoved) const {
534 assert(!BytesRemoved && "code size not handled");
535
Brendon Cahoondf43e682015-05-08 16:16:29 +0000536 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000538 unsigned Count = 0;
539 while (I != MBB.begin()) {
540 --I;
541 if (I->isDebugValue())
542 continue;
543 // Only removing branches from end of MBB.
544 if (!I->isBranch())
545 return Count;
546 if (Count && (I->getOpcode() == Hexagon::J2_jump))
547 llvm_unreachable("Malformed basic block: unconditional branch not last");
548 MBB.erase(&MBB.back());
549 I = MBB.end();
550 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000551 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000552 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000553}
554
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000555unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000556 MachineBasicBlock *TBB,
557 MachineBasicBlock *FBB,
558 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000559 const DebugLoc &DL,
560 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000561 unsigned BOpc = Hexagon::J2_jump;
562 unsigned BccOpc = Hexagon::J2_jumpt;
563 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000564 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000565 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000566
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000567 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000568 // If we want to reverse the branch an odd number of times, we want
569 // J2_jumpf.
570 if (!Cond.empty() && Cond[0].isImm())
571 BccOpc = Cond[0].getImm();
572
573 if (!FBB) {
574 if (Cond.empty()) {
575 // Due to a bug in TailMerging/CFG Optimization, we need to add a
576 // special case handling of a predicated jump followed by an
577 // unconditional jump. If not, Tail Merging and CFG Optimization go
578 // into an infinite loop.
579 MachineBasicBlock *NewTBB, *NewFBB;
580 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000581 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000582 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000583 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
584 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000585 reverseBranchCondition(Cond);
586 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000587 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000588 }
589 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
590 } else if (isEndLoopN(Cond[0].getImm())) {
591 int EndLoopOp = Cond[0].getImm();
592 assert(Cond[1].isMBB());
593 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
594 // Check for it, and change the BB target if needed.
595 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000596 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
597 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000598 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
599 Loop->getOperand(0).setMBB(TBB);
600 // Add the ENDLOOP after the finding the LOOP0.
601 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
602 } else if (isNewValueJump(Cond[0].getImm())) {
603 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
604 // New value jump
605 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
606 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
607 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
608 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
609 if (Cond[2].isReg()) {
610 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
611 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
612 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
613 } else if(Cond[2].isImm()) {
614 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
615 addImm(Cond[2].getImm()).addMBB(TBB);
616 } else
617 llvm_unreachable("Invalid condition for branching");
618 } else {
619 assert((Cond.size() == 2) && "Malformed cond vector");
620 const MachineOperand &RO = Cond[1];
621 unsigned Flags = getUndefRegState(RO.isUndef());
622 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
623 }
624 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000625 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000626 assert((!Cond.empty()) &&
627 "Cond. cannot be empty when multiple branchings are required");
628 assert((!isNewValueJump(Cond[0].getImm())) &&
629 "NV-jump cannot be inserted with another branch");
630 // Special case for hardware loops. The condition is a basic block.
631 if (isEndLoopN(Cond[0].getImm())) {
632 int EndLoopOp = Cond[0].getImm();
633 assert(Cond[1].isMBB());
634 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
635 // Check for it, and change the BB target if needed.
636 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000637 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
638 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000639 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
640 Loop->getOperand(0).setMBB(TBB);
641 // Add the ENDLOOP after the finding the LOOP0.
642 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
643 } else {
644 const MachineOperand &RO = Cond[1];
645 unsigned Flags = getUndefRegState(RO.isUndef());
646 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000647 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000648 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000649
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000650 return 2;
651}
652
Brendon Cahoon254f8892016-07-29 16:44:44 +0000653/// Analyze the loop code to find the loop induction variable and compare used
654/// to compute the number of iterations. Currently, we analyze loop that are
655/// controlled using hardware loops. In this case, the induction variable
656/// instruction is null. For all other cases, this function returns true, which
657/// means we're unable to analyze it.
658bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
659 MachineInstr *&IndVarInst,
660 MachineInstr *&CmpInst) const {
661
662 MachineBasicBlock *LoopEnd = L.getBottomBlock();
663 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
664 // We really "analyze" only hardware loops right now.
665 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
666 IndVarInst = nullptr;
667 CmpInst = &*I;
668 return false;
669 }
670 return true;
671}
672
673/// Generate code to reduce the loop iteration by one and check if the loop is
674/// finished. Return the value/register of the new loop count. this function
675/// assumes the nth iteration is peeled first.
676unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000677 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000678 SmallVectorImpl<MachineOperand> &Cond,
679 SmallVectorImpl<MachineInstr *> &PrevInsts,
680 unsigned Iter, unsigned MaxIter) const {
681 // We expect a hardware loop currently. This means that IndVar is set
682 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000683 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000684 && "Expecting a hardware loop");
685 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000686 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000687 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000688 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(),
689 Cmp.getOperand(0).getMBB(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000690 if (!Loop)
691 return 0;
692 // If the loop trip count is a compile-time value, then just change the
693 // value.
694 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
695 Loop->getOpcode() == Hexagon::J2_loop1i) {
696 int64_t Offset = Loop->getOperand(1).getImm();
697 if (Offset <= 1)
698 Loop->eraseFromParent();
699 else
700 Loop->getOperand(1).setImm(Offset - 1);
701 return Offset - 1;
702 }
703 // The loop trip count is a run-time value. We generate code to subtract
704 // one from the trip count, and update the loop instruction.
705 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
706 unsigned LoopCount = Loop->getOperand(1).getReg();
707 // Check if we're done with the loop.
708 unsigned LoopEnd = createVR(MF, MVT::i1);
709 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
710 addReg(LoopCount).addImm(1);
711 unsigned NewLoopCount = createVR(MF, MVT::i32);
712 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
713 addReg(LoopCount).addImm(-1);
714 // Update the previously generated instructions with the new loop counter.
715 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
716 E = PrevInsts.end(); I != E; ++I)
717 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
718 PrevInsts.clear();
719 PrevInsts.push_back(NewCmp);
720 PrevInsts.push_back(NewAdd);
721 // Insert the new loop instruction if this is the last time the loop is
722 // decremented.
723 if (Iter == MaxIter)
724 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
725 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
726 // Delete the old loop instruction.
727 if (Iter == 0)
728 Loop->eraseFromParent();
729 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
730 Cond.push_back(NewCmp->getOperand(0));
731 return NewLoopCount;
732}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000733
734bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
735 unsigned NumCycles, unsigned ExtraPredCycles,
736 BranchProbability Probability) const {
737 return nonDbgBBSize(&MBB) <= 3;
738}
739
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000740bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
741 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
742 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
743 const {
744 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
745}
746
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000747bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
748 unsigned NumInstrs, BranchProbability Probability) const {
749 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000750}
751
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000753 MachineBasicBlock::iterator I,
754 const DebugLoc &DL, unsigned DestReg,
755 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000756 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000757 unsigned KillFlag = getKillRegState(KillSrc);
758
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000759 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000760 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000761 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000762 return;
763 }
764 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000765 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
766 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000767 return;
768 }
769 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
770 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000771 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
772 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000773 return;
774 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000775 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000776 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000777 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
778 .addReg(SrcReg, KillFlag);
779 return;
780 }
781 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
782 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
783 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
784 .addReg(SrcReg, KillFlag);
785 return;
786 }
787 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
788 Hexagon::IntRegsRegClass.contains(SrcReg)) {
789 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
790 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000791 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000792 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000793 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
794 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000795 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
796 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000797 return;
798 }
799 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
800 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000801 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
802 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000803 return;
804 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000805 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
806 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000807 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
808 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000809 return;
810 }
811 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
812 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000813 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000814 return;
815 }
816 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000817 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
818 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000819 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000820 .addReg(HiSrc, KillFlag)
821 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000822 return;
823 }
824 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000825 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
826 .addReg(SrcReg)
827 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000828 return;
829 }
830 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000831 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000832 llvm_unreachable("Unimplemented pred to vec");
833 return;
834 }
835 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
836 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
837 llvm_unreachable("Unimplemented vec to pred");
838 return;
839 }
840 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000841 unsigned HiDst = HRI.getSubReg(DestReg, Hexagon::vsub_hi);
842 unsigned LoDst = HRI.getSubReg(DestReg, Hexagon::vsub_lo);
843 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
844 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
845 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), HiDst)
846 .addReg(HiSrc, KillFlag);
847 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), LoDst)
848 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000849 return;
850 }
Sirish Pande30804c22012-02-15 18:52:27 +0000851
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000852#ifndef NDEBUG
853 // Show the invalid registers to ease debugging.
854 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
855 << ": " << PrintReg(DestReg, &HRI)
856 << " = " << PrintReg(SrcReg, &HRI) << '\n';
857#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000858 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000859}
860
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000861void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
862 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
863 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000864 DebugLoc DL = MBB.findDebugLoc(I);
865 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000866 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000867 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000868 unsigned KillFlag = getKillRegState(isKill);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000869 bool HasAlloca = MFI.hasVarSizedObjects();
870 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
871 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000872
Alex Lorenze40c8a22015-08-11 23:09:45 +0000873 MachineMemOperand *MMO = MF.getMachineMemOperand(
874 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
875 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000876
Craig Topperc7242e02012-04-20 07:30:17 +0000877 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000878 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000879 .addFrameIndex(FI).addImm(0)
880 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000881 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000882 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000883 .addFrameIndex(FI).addImm(0)
884 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000885 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000886 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000887 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000888 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000889 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
890 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
891 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000892 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
893 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000894 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai_128B))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000895 .addFrameIndex(FI).addImm(0)
896 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
897 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000898 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000899 .addFrameIndex(FI).addImm(0)
900 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
901 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000902 // If there are variable-sized objects, spills will not be aligned.
903 if (HasAlloca)
904 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000905 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
906 : Hexagon::V6_vS32b_ai_128B;
907 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000908 .addFrameIndex(FI).addImm(0)
909 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
910 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000911 // If there are variable-sized objects, spills will not be aligned.
912 if (HasAlloca)
913 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000914 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
915 : Hexagon::V6_vS32b_ai;
916 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000917 .addFrameIndex(FI).addImm(0)
918 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
919 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000920 // If there are variable-sized objects, spills will not be aligned.
921 if (HasAlloca)
922 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000923 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
924 : Hexagon::PS_vstorerw_ai;
925 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000926 .addFrameIndex(FI).addImm(0)
927 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
928 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000929 // If there are variable-sized objects, spills will not be aligned.
930 if (HasAlloca)
931 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000932 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
933 : Hexagon::PS_vstorerw_ai_128B;
934 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000935 .addFrameIndex(FI).addImm(0)
936 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000937 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000938 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000939 }
940}
941
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000942void HexagonInstrInfo::loadRegFromStackSlot(
943 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
944 int FI, const TargetRegisterClass *RC,
945 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000946 DebugLoc DL = MBB.findDebugLoc(I);
947 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000948 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000949 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000950 bool HasAlloca = MFI.hasVarSizedObjects();
951 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
952 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000953
Alex Lorenze40c8a22015-08-11 23:09:45 +0000954 MachineMemOperand *MMO = MF.getMachineMemOperand(
955 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
956 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000957
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000958 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000959 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_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::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000962 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000963 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000964 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000965 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000966 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
967 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
968 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
969 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000970 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000971 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000972 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
973 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000974 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000975 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
976 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000977 // If there are variable-sized objects, spills will not be aligned.
978 if (HasAlloca)
979 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000980 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
981 : Hexagon::PS_vloadrw_ai_128B;
982 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000983 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
984 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000985 // If there are variable-sized objects, spills will not be aligned.
986 if (HasAlloca)
987 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000988 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
989 : Hexagon::V6_vL32b_ai_128B;
990 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000991 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
992 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000993 // If there are variable-sized objects, spills will not be aligned.
994 if (HasAlloca)
995 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000996 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
997 : Hexagon::V6_vL32b_ai;
998 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000999 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1000 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +00001001 // If there are variable-sized objects, spills will not be aligned.
1002 if (HasAlloca)
1003 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001004 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
1005 : Hexagon::PS_vloadrw_ai;
1006 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +00001007 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001008 } else {
Craig Toppere55c5562012-02-07 02:50:20 +00001009 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001010 }
1011}
1012
Ron Lieberman88159e52016-09-02 22:56:24 +00001013static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
1014 const MachineBasicBlock &B = *MI.getParent();
1015 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +00001016 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +00001017 for (auto I = B.rbegin(); I != E; ++I)
1018 Regs.stepBackward(*I);
1019}
1020
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001021/// expandPostRAPseudo - This function is called for all pseudo instructions
1022/// that remain after register allocation. Many pseudo instructions are
1023/// created to help register allocation. This is the place to convert them
1024/// into real instructions. The target can edit MI in place, or it can insert
1025/// new instructions and erase MI. The function should return true if
1026/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001027bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001028 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001029 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1030 MachineBasicBlock &MBB = *MI.getParent();
1031 DebugLoc DL = MI.getDebugLoc();
1032 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001033 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001034
1035 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001036 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001037 MachineOperand &MD = MI.getOperand(0);
1038 MachineOperand &MS = MI.getOperand(1);
1039 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001040 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1041 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001042 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001043 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001044 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001045 return true;
1046 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001047 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001048 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001049 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001050 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001051 MBB.erase(MI);
1052 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001053 case Hexagon::V6_vassignp_128B:
1054 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001055 unsigned SrcReg = MI.getOperand(1).getReg();
1056 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001057 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1058 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001059 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1060 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001061 MBB.erase(MI);
1062 return true;
1063 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001064 case Hexagon::V6_lo_128B:
1065 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001066 unsigned SrcReg = MI.getOperand(1).getReg();
1067 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001068 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001069 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001070 MBB.erase(MI);
1071 MRI.clearKillFlags(SrcSubLo);
1072 return true;
1073 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001074 case Hexagon::V6_hi_128B:
1075 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001076 unsigned SrcReg = MI.getOperand(1).getReg();
1077 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001078 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001079 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001080 MBB.erase(MI);
1081 MRI.clearKillFlags(SrcSubHi);
1082 return true;
1083 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001084 case Hexagon::PS_vstorerw_ai:
1085 case Hexagon::PS_vstorerwu_ai:
1086 case Hexagon::PS_vstorerw_ai_128B:
1087 case Hexagon::PS_vstorerwu_ai_128B: {
1088 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1089 Opc == Hexagon::PS_vstorerwu_ai_128B);
1090 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1091 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001092 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001093 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1094 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001095 unsigned NewOpc;
1096 if (Aligned)
1097 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1098 : Hexagon::V6_vS32b_ai;
1099 else
1100 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1101 : Hexagon::V6_vS32Ub_ai;
1102
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001103 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001104 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001105 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001106 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001107 .addImm(MI.getOperand(1).getImm())
1108 .addReg(SrcSubLo)
1109 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001110 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001111 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001112 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001113 // The Vectors are indexed in multiples of vector size.
1114 .addImm(MI.getOperand(1).getImm() + Offset)
1115 .addReg(SrcSubHi)
1116 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001117 MBB.erase(MI);
1118 return true;
1119 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001120 case Hexagon::PS_vloadrw_ai:
1121 case Hexagon::PS_vloadrwu_ai:
1122 case Hexagon::PS_vloadrw_ai_128B:
1123 case Hexagon::PS_vloadrwu_ai_128B: {
1124 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1125 Opc == Hexagon::PS_vloadrwu_ai_128B);
1126 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1127 Opc == Hexagon::PS_vloadrw_ai_128B);
1128 unsigned NewOpc;
1129 if (Aligned)
1130 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1131 : Hexagon::V6_vL32b_ai;
1132 else
1133 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1134 : Hexagon::V6_vL32Ub_ai;
1135
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001136 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001137 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Diana Picus116bbab2017-01-13 09:58:52 +00001138 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1139 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Krzysztof Parzyszek4be9d922017-05-03 15:26:13 +00001140 .add(MI.getOperand(1))
1141 .addImm(MI.getOperand(2).getImm())
1142 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001143 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001144 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1145 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001146 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001147 .addImm(MI.getOperand(2).getImm() + Offset)
1148 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001149 MBB.erase(MI);
1150 return true;
1151 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001152 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001153 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001154 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1155 .addReg(Reg, RegState::Undef)
1156 .addReg(Reg, RegState::Undef);
1157 MBB.erase(MI);
1158 return true;
1159 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001160 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001161 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001162 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1163 .addReg(Reg, RegState::Undef)
1164 .addReg(Reg, RegState::Undef);
1165 MBB.erase(MI);
1166 return true;
1167 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001168 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001169 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001170 unsigned DstReg = MI.getOperand(0).getReg();
1171 unsigned Src1Reg = MI.getOperand(1).getReg();
1172 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001173 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1174 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1175 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1176 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001177 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001178 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001179 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001180 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001181 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001182 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001183 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001184 .addReg(Src2SubLo);
1185 MBB.erase(MI);
1186 MRI.clearKillFlags(Src1SubHi);
1187 MRI.clearKillFlags(Src1SubLo);
1188 MRI.clearKillFlags(Src2SubHi);
1189 MRI.clearKillFlags(Src2SubLo);
1190 return true;
1191 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001192 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001193 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001194 unsigned DstReg = MI.getOperand(0).getReg();
1195 unsigned Src1Reg = MI.getOperand(1).getReg();
1196 unsigned Src2Reg = MI.getOperand(2).getReg();
1197 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001198 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1199 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1200 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1201 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1202 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1203 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001204 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001205 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001206 .addReg(Src1SubHi)
1207 .addReg(Src2SubHi)
1208 .addReg(Src3SubHi);
1209 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001210 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001211 .addReg(Src1SubLo)
1212 .addReg(Src2SubLo)
1213 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001214 MBB.erase(MI);
1215 MRI.clearKillFlags(Src1SubHi);
1216 MRI.clearKillFlags(Src1SubLo);
1217 MRI.clearKillFlags(Src2SubHi);
1218 MRI.clearKillFlags(Src2SubLo);
1219 MRI.clearKillFlags(Src3SubHi);
1220 MRI.clearKillFlags(Src3SubLo);
1221 return true;
1222 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001223 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001224 const MachineOperand &Op0 = MI.getOperand(0);
1225 const MachineOperand &Op1 = MI.getOperand(1);
1226 const MachineOperand &Op2 = MI.getOperand(2);
1227 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001228 unsigned Rd = Op0.getReg();
1229 unsigned Pu = Op1.getReg();
1230 unsigned Rs = Op2.getReg();
1231 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001232 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001233 unsigned K1 = getKillRegState(Op1.isKill());
1234 unsigned K2 = getKillRegState(Op2.isKill());
1235 unsigned K3 = getKillRegState(Op3.isKill());
1236 if (Rd != Rs)
1237 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1238 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1239 .addReg(Rs, K2);
1240 if (Rd != Rt)
1241 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1242 .addReg(Pu, K1)
1243 .addReg(Rt, K3);
1244 MBB.erase(MI);
1245 return true;
1246 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001247 case Hexagon::PS_vselect:
1248 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001249 const MachineOperand &Op0 = MI.getOperand(0);
1250 const MachineOperand &Op1 = MI.getOperand(1);
1251 const MachineOperand &Op2 = MI.getOperand(2);
1252 const MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001253 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001254 getLiveRegsAt(LiveAtMI, MI);
1255 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001256 unsigned PReg = Op1.getReg();
1257 assert(Op1.getSubReg() == 0);
1258 unsigned PState = getRegState(Op1);
1259
Ron Lieberman88159e52016-09-02 22:56:24 +00001260 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001261 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1262 : PState;
Ron Lieberman88159e52016-09-02 22:56:24 +00001263 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001264 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001265 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001266 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001267 if (IsDestLive)
1268 T.addReg(Op0.getReg(), RegState::Implicit);
1269 IsDestLive = true;
1270 }
1271 if (Op0.getReg() != Op3.getReg()) {
1272 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001273 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001274 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001275 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001276 if (IsDestLive)
1277 T.addReg(Op0.getReg(), RegState::Implicit);
1278 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001279 MBB.erase(MI);
1280 return true;
1281 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001282 case Hexagon::PS_wselect:
1283 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001284 MachineOperand &Op0 = MI.getOperand(0);
1285 MachineOperand &Op1 = MI.getOperand(1);
1286 MachineOperand &Op2 = MI.getOperand(2);
1287 MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001288 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001289 getLiveRegsAt(LiveAtMI, MI);
1290 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001291 unsigned PReg = Op1.getReg();
1292 assert(Op1.getSubReg() == 0);
1293 unsigned PState = getRegState(Op1);
Ron Lieberman88159e52016-09-02 22:56:24 +00001294
1295 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001296 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1297 : PState;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001298 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1299 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001300 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001301 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001302 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001303 .add(Op1)
1304 .addReg(SrcHi)
1305 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001306 if (IsDestLive)
1307 T.addReg(Op0.getReg(), RegState::Implicit);
1308 IsDestLive = true;
1309 }
1310 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001311 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1312 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001313 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001314 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001315 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001316 .addReg(SrcHi)
1317 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001318 if (IsDestLive)
1319 T.addReg(Op0.getReg(), RegState::Implicit);
1320 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001321 MBB.erase(MI);
1322 return true;
1323 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001324 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001325 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001326 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001327 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001328 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001329 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001330 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001331 case Hexagon::PS_jmprett:
1332 MI.setDesc(get(Hexagon::J2_jumprt));
1333 return true;
1334 case Hexagon::PS_jmpretf:
1335 MI.setDesc(get(Hexagon::J2_jumprf));
1336 return true;
1337 case Hexagon::PS_jmprettnewpt:
1338 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1339 return true;
1340 case Hexagon::PS_jmpretfnewpt:
1341 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1342 return true;
1343 case Hexagon::PS_jmprettnew:
1344 MI.setDesc(get(Hexagon::J2_jumprtnew));
1345 return true;
1346 case Hexagon::PS_jmpretfnew:
1347 MI.setDesc(get(Hexagon::J2_jumprfnew));
1348 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001349 }
1350
1351 return false;
1352}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001353
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001354// We indicate that we want to reverse the branch by
1355// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001356bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001357 SmallVectorImpl<MachineOperand> &Cond) const {
1358 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001359 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001360 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1361 unsigned opcode = Cond[0].getImm();
1362 //unsigned temp;
1363 assert(get(opcode).isBranch() && "Should be a branching condition.");
1364 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001365 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001366 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1367 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001368 return false;
1369}
1370
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001371void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1372 MachineBasicBlock::iterator MI) const {
1373 DebugLoc DL;
1374 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1375}
1376
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001377bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1378 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001379}
1380
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001381// Returns true if an instruction is predicated irrespective of the predicate
1382// sense. For example, all of the following will return true.
1383// if (p0) R1 = add(R2, R3)
1384// if (!p0) R1 = add(R2, R3)
1385// if (p0.new) R1 = add(R2, R3)
1386// if (!p0.new) R1 = add(R2, R3)
1387// Note: New-value stores are not included here as in the current
1388// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001389bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1390 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001391 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001392}
1393
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001394bool HexagonInstrInfo::PredicateInstruction(
1395 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001396 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1397 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001398 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001399 return false;
1400 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001401 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001402 assert (isPredicable(MI) && "Expected predicable instruction");
1403 bool invertJump = predOpcodeHasNot(Cond);
1404
1405 // We have to predicate MI "in place", i.e. after this function returns,
1406 // MI will need to be transformed into a predicated form. To avoid com-
1407 // plicated manipulations with the operands (handling tied operands,
1408 // etc.), build a new temporary instruction, then overwrite MI with it.
1409
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001410 MachineBasicBlock &B = *MI.getParent();
1411 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001412 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1413 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001414 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001415 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001416 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001417 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1418 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001419 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001420 NOp++;
1421 }
1422
1423 unsigned PredReg, PredRegPos, PredRegFlags;
1424 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1425 (void)GotPredReg;
1426 assert(GotPredReg);
1427 T.addReg(PredReg, PredRegFlags);
1428 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001429 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001430
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001431 MI.setDesc(get(PredOpc));
1432 while (unsigned n = MI.getNumOperands())
1433 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001434 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001435 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001436
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001437 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001438 B.erase(TI);
1439
1440 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1441 MRI.clearKillFlags(PredReg);
1442 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001443}
1444
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001445bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1446 ArrayRef<MachineOperand> Pred2) const {
1447 // TODO: Fix this
1448 return false;
1449}
1450
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001451bool HexagonInstrInfo::DefinesPredicate(
1452 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001453 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001454 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1455 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001456 if (MO.isReg()) {
1457 if (!MO.isDef())
1458 continue;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001459 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1460 if (RC == &Hexagon::PredRegsRegClass) {
1461 Pred.push_back(MO);
1462 return true;
1463 }
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001464 continue;
1465 } else if (MO.isRegMask()) {
1466 for (unsigned PR : Hexagon::PredRegsRegClass) {
1467 if (!MI.modifiesRegister(PR, &HRI))
1468 continue;
1469 Pred.push_back(MO);
1470 return true;
1471 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001472 }
1473 }
1474 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001475}
Andrew Trickd06df962012-02-01 22:13:57 +00001476
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001477bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001478 if (!MI.getDesc().isPredicable())
1479 return false;
1480
1481 if (MI.isCall() || isTailCall(MI)) {
1482 const MachineFunction &MF = *MI.getParent()->getParent();
1483 if (!MF.getSubtarget<HexagonSubtarget>().usePredicatedCalls())
1484 return false;
1485 }
1486 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001487}
1488
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001489bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1490 const MachineBasicBlock *MBB,
1491 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001492 // Debug info is never a scheduling boundary. It's necessary to be explicit
1493 // due to the special treatment of IT instructions below, otherwise a
1494 // dbg_value followed by an IT will result in the IT instruction being
1495 // considered a scheduling hazard, which is wrong. It should be the actual
1496 // instruction preceding the dbg_value instruction(s), just like it is
1497 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001498 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001499 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001500
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001501 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001502 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001503 // Don't mess around with no return calls.
1504 if (doesNotReturn(MI))
1505 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001506 // If any of the block's successors is a landing pad, this could be a
1507 // throwing call.
1508 for (auto I : MBB->successors())
1509 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001510 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001511 }
1512
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001513 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001514 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001515 return true;
1516
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001517 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1518 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001519
1520 return false;
1521}
1522
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001523/// Measure the specified inline asm to determine an approximation of its
1524/// length.
1525/// Comments (which run till the next SeparatorString or newline) do not
1526/// count as an instruction.
1527/// Any other non-whitespace text is considered an instruction, with
1528/// multiple instructions separated by SeparatorString or newlines.
1529/// Variable-length instructions are not handled here; this function
1530/// may be overloaded in the target code to do that.
1531/// Hexagon counts the number of ##'s and adjust for that many
1532/// constant exenders.
1533unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1534 const MCAsmInfo &MAI) const {
1535 StringRef AStr(Str);
1536 // Count the number of instructions in the asm.
1537 bool atInsnStart = true;
1538 unsigned Length = 0;
1539 for (; *Str; ++Str) {
1540 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1541 strlen(MAI.getSeparatorString())) == 0)
1542 atInsnStart = true;
1543 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1544 Length += MAI.getMaxInstLength();
1545 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001546 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001547 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1548 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001549 atInsnStart = false;
1550 }
1551
1552 // Add to size number of constant extenders seen * 4.
1553 StringRef Occ("##");
1554 Length += AStr.count(Occ)*4;
1555 return Length;
1556}
1557
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001558ScheduleHazardRecognizer*
1559HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1560 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001561 if (UseDFAHazardRec) {
1562 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1563 return new HexagonHazardRecognizer(II, this, HST);
1564 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001565 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1566}
1567
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001568/// \brief For a comparison instruction, return the source registers in
1569/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1570/// compares against in CmpValue. Return true if the comparison instruction
1571/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001572bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1573 unsigned &SrcReg2, int &Mask,
1574 int &Value) const {
1575 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001576
1577 // Set mask and the first source register.
1578 switch (Opc) {
1579 case Hexagon::C2_cmpeq:
1580 case Hexagon::C2_cmpeqp:
1581 case Hexagon::C2_cmpgt:
1582 case Hexagon::C2_cmpgtp:
1583 case Hexagon::C2_cmpgtu:
1584 case Hexagon::C2_cmpgtup:
1585 case Hexagon::C4_cmpneq:
1586 case Hexagon::C4_cmplte:
1587 case Hexagon::C4_cmplteu:
1588 case Hexagon::C2_cmpeqi:
1589 case Hexagon::C2_cmpgti:
1590 case Hexagon::C2_cmpgtui:
1591 case Hexagon::C4_cmpneqi:
1592 case Hexagon::C4_cmplteui:
1593 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001594 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001595 Mask = ~0;
1596 break;
1597 case Hexagon::A4_cmpbeq:
1598 case Hexagon::A4_cmpbgt:
1599 case Hexagon::A4_cmpbgtu:
1600 case Hexagon::A4_cmpbeqi:
1601 case Hexagon::A4_cmpbgti:
1602 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001603 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001604 Mask = 0xFF;
1605 break;
1606 case Hexagon::A4_cmpheq:
1607 case Hexagon::A4_cmphgt:
1608 case Hexagon::A4_cmphgtu:
1609 case Hexagon::A4_cmpheqi:
1610 case Hexagon::A4_cmphgti:
1611 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001612 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001613 Mask = 0xFFFF;
1614 break;
1615 }
1616
1617 // Set the value/second source register.
1618 switch (Opc) {
1619 case Hexagon::C2_cmpeq:
1620 case Hexagon::C2_cmpeqp:
1621 case Hexagon::C2_cmpgt:
1622 case Hexagon::C2_cmpgtp:
1623 case Hexagon::C2_cmpgtu:
1624 case Hexagon::C2_cmpgtup:
1625 case Hexagon::A4_cmpbeq:
1626 case Hexagon::A4_cmpbgt:
1627 case Hexagon::A4_cmpbgtu:
1628 case Hexagon::A4_cmpheq:
1629 case Hexagon::A4_cmphgt:
1630 case Hexagon::A4_cmphgtu:
1631 case Hexagon::C4_cmpneq:
1632 case Hexagon::C4_cmplte:
1633 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001634 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001635 return true;
1636
1637 case Hexagon::C2_cmpeqi:
1638 case Hexagon::C2_cmpgtui:
1639 case Hexagon::C2_cmpgti:
1640 case Hexagon::C4_cmpneqi:
1641 case Hexagon::C4_cmplteui:
1642 case Hexagon::C4_cmpltei:
1643 case Hexagon::A4_cmpbeqi:
1644 case Hexagon::A4_cmpbgti:
1645 case Hexagon::A4_cmpbgtui:
1646 case Hexagon::A4_cmpheqi:
1647 case Hexagon::A4_cmphgti:
1648 case Hexagon::A4_cmphgtui:
1649 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001650 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001651 return true;
1652 }
1653
1654 return false;
1655}
1656
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001657unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001658 const MachineInstr &MI,
1659 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001660 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001661}
1662
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001663
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001664DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1665 const TargetSubtargetInfo &STI) const {
1666 const InstrItineraryData *II = STI.getInstrItineraryData();
1667 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1668}
1669
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001670// Inspired by this pair:
1671// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1672// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1673// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001674bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1675 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001676 int OffsetA = 0, OffsetB = 0;
1677 unsigned SizeA = 0, SizeB = 0;
1678
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001679 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1680 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001681 return false;
1682
1683 // Instructions that are pure loads, not loads and stores like memops are not
1684 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001685 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001686 return true;
1687
1688 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001689 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001690 if (!BaseRegA || !SizeA)
1691 return false;
1692
1693 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001694 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001695 if (!BaseRegB || !SizeB)
1696 return false;
1697
1698 if (BaseRegA != BaseRegB)
1699 return false;
1700
1701 // This is a mem access with the same base register and known offsets from it.
1702 // Reason about it.
1703 if (OffsetA > OffsetB) {
1704 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1705 return (SizeB <= offDiff);
1706 } else if (OffsetA < OffsetB) {
1707 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1708 return (SizeA <= offDiff);
1709 }
1710
1711 return false;
1712}
1713
Brendon Cahoon254f8892016-07-29 16:44:44 +00001714/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001715bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001716 int &Value) const {
1717 if (isPostIncrement(MI)) {
1718 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001719 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001720 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001721 if (MI.getOpcode() == Hexagon::A2_addi) {
1722 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001723 return true;
1724 }
1725
1726 return false;
1727}
1728
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001729std::pair<unsigned, unsigned>
1730HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1731 return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
1732 TF & HexagonII::MO_Bitmasks);
1733}
1734
1735ArrayRef<std::pair<unsigned, const char*>>
1736HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1737 using namespace HexagonII;
1738 static const std::pair<unsigned, const char*> Flags[] = {
1739 {MO_PCREL, "hexagon-pcrel"},
1740 {MO_GOT, "hexagon-got"},
1741 {MO_LO16, "hexagon-lo16"},
1742 {MO_HI16, "hexagon-hi16"},
1743 {MO_GPREL, "hexagon-gprel"},
1744 {MO_GDGOT, "hexagon-gdgot"},
1745 {MO_GDPLT, "hexagon-gdplt"},
1746 {MO_IE, "hexagon-ie"},
1747 {MO_IEGOT, "hexagon-iegot"},
1748 {MO_TPREL, "hexagon-tprel"}
1749 };
1750 return makeArrayRef(Flags);
1751}
1752
1753ArrayRef<std::pair<unsigned, const char*>>
1754HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1755 using namespace HexagonII;
1756 static const std::pair<unsigned, const char*> Flags[] = {
1757 {HMOTF_ConstExtended, "hexagon-ext"}
1758 };
1759 return makeArrayRef(Flags);
1760}
1761
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001762unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001763 MachineRegisterInfo &MRI = MF->getRegInfo();
1764 const TargetRegisterClass *TRC;
1765 if (VT == MVT::i1) {
1766 TRC = &Hexagon::PredRegsRegClass;
1767 } else if (VT == MVT::i32 || VT == MVT::f32) {
1768 TRC = &Hexagon::IntRegsRegClass;
1769 } else if (VT == MVT::i64 || VT == MVT::f64) {
1770 TRC = &Hexagon::DoubleRegsRegClass;
1771 } else {
1772 llvm_unreachable("Cannot handle this register class");
1773 }
1774
1775 unsigned NewReg = MRI.createVirtualRegister(TRC);
1776 return NewReg;
1777}
1778
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001779bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001780 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1781}
1782
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001783bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1784 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001785 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1786}
1787
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001788bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1789 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001790 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1791 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1792
1793 if (!(isTC1(MI))
1794 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001795 && !(MI.getDesc().mayLoad())
1796 && !(MI.getDesc().mayStore())
1797 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1798 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001799 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001800 && !(MI.isBranch())
1801 && !(MI.isReturn())
1802 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001803 return true;
1804
1805 return false;
1806}
1807
Sanjay Patele4b9f502015-12-07 19:21:39 +00001808// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001809bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001810 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001811}
1812
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001813// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1814// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001815bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1816 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001817 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1818 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001819 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001820
1821 unsigned isExtendable =
1822 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1823 if (!isExtendable)
1824 return false;
1825
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001826 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001827 return false;
1828
1829 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001830 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001831 // Use MO operand flags to determine if MO
1832 // has the HMOTF_ConstExtended flag set.
1833 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001834 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001835 // If this is a Machine BB address we are talking about, and it is
1836 // not marked as extended, say so.
1837 if (MO.isMBB())
1838 return false;
1839
1840 // We could be using an instruction with an extendable immediate and shoehorn
1841 // a global address into it. If it is a global address it will be constant
1842 // extended. We do this for COMBINE.
1843 // We currently only handle isGlobal() because it is the only kind of
1844 // object we are going to end up with here for now.
1845 // In the future we probably should add isSymbol(), etc.
1846 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001847 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001848 return true;
1849
1850 // If the extendable operand is not 'Immediate' type, the instruction should
1851 // have 'isExtended' flag set.
1852 assert(MO.isImm() && "Extendable operand must be Immediate type");
1853
1854 int MinValue = getMinValue(MI);
1855 int MaxValue = getMaxValue(MI);
1856 int ImmValue = MO.getImm();
1857
1858 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001859}
1860
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001861bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1862 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001863 case Hexagon::L4_return :
1864 case Hexagon::L4_return_t :
1865 case Hexagon::L4_return_f :
1866 case Hexagon::L4_return_tnew_pnt :
1867 case Hexagon::L4_return_fnew_pnt :
1868 case Hexagon::L4_return_tnew_pt :
1869 case Hexagon::L4_return_fnew_pt :
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00001870 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001871 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001872 return false;
1873}
1874
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001875// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001876bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1877 const MachineInstr &ConsMI) const {
1878 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001879 return false;
1880
1881 auto &HRI = getRegisterInfo();
1882
1883 SmallVector<unsigned, 4> DefsA;
1884 SmallVector<unsigned, 4> DefsB;
1885 SmallVector<unsigned, 8> UsesA;
1886 SmallVector<unsigned, 8> UsesB;
1887
1888 parseOperands(ProdMI, DefsA, UsesA);
1889 parseOperands(ConsMI, DefsB, UsesB);
1890
1891 for (auto &RegA : DefsA)
1892 for (auto &RegB : UsesB) {
1893 // True data dependency.
1894 if (RegA == RegB)
1895 return true;
1896
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001897 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001898 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1899 if (RegB == *SubRegs)
1900 return true;
1901
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001902 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001903 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1904 if (RegA == *SubRegs)
1905 return true;
1906 }
1907
1908 return false;
1909}
1910
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001911// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001912bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1913 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001914 case Hexagon::V6_vL32b_cur_pi:
1915 case Hexagon::V6_vL32b_cur_ai:
1916 case Hexagon::V6_vL32b_cur_pi_128B:
1917 case Hexagon::V6_vL32b_cur_ai_128B:
1918 return true;
1919 }
1920 return false;
1921}
1922
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001923// Returns true, if any one of the operands is a dot new
1924// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001925bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
1926 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001927 return true;
1928
1929 return false;
1930}
1931
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001932/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001933bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
1934 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001935 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1936 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1937 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1938}
1939
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001940bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
1941 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001942 return true;
1943
1944 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001945 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001946 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001947}
1948
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001949bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1950 return (Opcode == Hexagon::ENDLOOP0 ||
1951 Opcode == Hexagon::ENDLOOP1);
1952}
1953
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001954bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1955 switch(OpType) {
1956 case MachineOperand::MO_MachineBasicBlock:
1957 case MachineOperand::MO_GlobalAddress:
1958 case MachineOperand::MO_ExternalSymbol:
1959 case MachineOperand::MO_JumpTableIndex:
1960 case MachineOperand::MO_ConstantPoolIndex:
1961 case MachineOperand::MO_BlockAddress:
1962 return true;
1963 default:
1964 return false;
1965 }
1966}
1967
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001968bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
1969 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001970 const uint64_t F = MID.TSFlags;
1971 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1972 return true;
1973
1974 // TODO: This is largely obsolete now. Will need to be removed
1975 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001976 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001977 // PS_fi and PS_fia remain special cases.
1978 case Hexagon::PS_fi:
1979 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001980 return true;
1981 default:
1982 return false;
1983 }
1984 return false;
1985}
1986
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001987// This returns true in two cases:
1988// - The OP code itself indicates that this is an extended instruction.
1989// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001990bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001991 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001992 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001993 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
1994 return true;
1995 // Use MO operand flags to determine if one of MI's operands
1996 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001997 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
1998 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001999 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2000 return true;
2001 }
2002 return false;
2003}
2004
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002005bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2006 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002007 const uint64_t F = get(Opcode).TSFlags;
2008 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2009}
2010
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002011// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002012bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2013 const MachineInstr &J) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002014 if (!isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002015 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002016 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002017 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002018 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002019}
2020
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002021bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2022 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002023 case Hexagon::J2_callr :
2024 case Hexagon::J2_callrf :
2025 case Hexagon::J2_callrt :
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00002026 case Hexagon::PS_call_nr :
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002027 return true;
2028 }
2029 return false;
2030}
2031
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002032bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2033 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002034 case Hexagon::L4_return :
2035 case Hexagon::L4_return_t :
2036 case Hexagon::L4_return_f :
2037 case Hexagon::L4_return_fnew_pnt :
2038 case Hexagon::L4_return_fnew_pt :
2039 case Hexagon::L4_return_tnew_pnt :
2040 case Hexagon::L4_return_tnew_pt :
2041 return true;
2042 }
2043 return false;
2044}
2045
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002046bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2047 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002048 case Hexagon::J2_jumpr :
2049 case Hexagon::J2_jumprt :
2050 case Hexagon::J2_jumprf :
2051 case Hexagon::J2_jumprtnewpt :
2052 case Hexagon::J2_jumprfnewpt :
2053 case Hexagon::J2_jumprtnew :
2054 case Hexagon::J2_jumprfnew :
2055 return true;
2056 }
2057 return false;
2058}
2059
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002060// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002061// Use abs estimate as oppose to the exact number.
2062// TODO: This will need to be changed to use MC level
2063// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002064bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002065 unsigned offset) const {
2066 // This selection of jump instructions matches to that what
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002067 // analyzeBranch can parse, plus NVJ.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002068 if (isNewValueJump(MI)) // r9:2
2069 return isInt<11>(offset);
2070
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002071 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002072 // Still missing Jump to address condition on register value.
2073 default:
2074 return false;
2075 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2076 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002077 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002078 return isInt<24>(offset);
2079 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2080 case Hexagon::J2_jumpf:
2081 case Hexagon::J2_jumptnew:
2082 case Hexagon::J2_jumptnewpt:
2083 case Hexagon::J2_jumpfnew:
2084 case Hexagon::J2_jumpfnewpt:
2085 case Hexagon::J2_callt:
2086 case Hexagon::J2_callf:
2087 return isInt<17>(offset);
2088 case Hexagon::J2_loop0i:
2089 case Hexagon::J2_loop0iext:
2090 case Hexagon::J2_loop0r:
2091 case Hexagon::J2_loop0rext:
2092 case Hexagon::J2_loop1i:
2093 case Hexagon::J2_loop1iext:
2094 case Hexagon::J2_loop1r:
2095 case Hexagon::J2_loop1rext:
2096 return isInt<9>(offset);
2097 // TODO: Add all the compound branches here. Can we do this in Relation model?
2098 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2099 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2100 return isInt<11>(offset);
2101 }
2102}
2103
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002104bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2105 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002106 bool isLate = isLateResultInstr(LRMI);
2107 bool isEarly = isEarlySourceInstr(ESMI);
2108
2109 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002110 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002111 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002112 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002113
2114 if (isLate && isEarly) {
2115 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2116 return true;
2117 }
2118
2119 return false;
2120}
2121
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002122bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2123 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002124 case TargetOpcode::EXTRACT_SUBREG:
2125 case TargetOpcode::INSERT_SUBREG:
2126 case TargetOpcode::SUBREG_TO_REG:
2127 case TargetOpcode::REG_SEQUENCE:
2128 case TargetOpcode::IMPLICIT_DEF:
2129 case TargetOpcode::COPY:
2130 case TargetOpcode::INLINEASM:
2131 case TargetOpcode::PHI:
2132 return false;
2133 default:
2134 break;
2135 }
2136
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002137 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002138 return !is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002139}
2140
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002141bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002142 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2143 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002144 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002145}
2146
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002147bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2148 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002149 return Opcode == Hexagon::J2_loop0i ||
2150 Opcode == Hexagon::J2_loop0r ||
2151 Opcode == Hexagon::J2_loop0iext ||
2152 Opcode == Hexagon::J2_loop0rext ||
2153 Opcode == Hexagon::J2_loop1i ||
2154 Opcode == Hexagon::J2_loop1r ||
2155 Opcode == Hexagon::J2_loop1iext ||
2156 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002157}
2158
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002159bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2160 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002161 default: return false;
2162 case Hexagon::L4_iadd_memopw_io :
2163 case Hexagon::L4_isub_memopw_io :
2164 case Hexagon::L4_add_memopw_io :
2165 case Hexagon::L4_sub_memopw_io :
2166 case Hexagon::L4_and_memopw_io :
2167 case Hexagon::L4_or_memopw_io :
2168 case Hexagon::L4_iadd_memoph_io :
2169 case Hexagon::L4_isub_memoph_io :
2170 case Hexagon::L4_add_memoph_io :
2171 case Hexagon::L4_sub_memoph_io :
2172 case Hexagon::L4_and_memoph_io :
2173 case Hexagon::L4_or_memoph_io :
2174 case Hexagon::L4_iadd_memopb_io :
2175 case Hexagon::L4_isub_memopb_io :
2176 case Hexagon::L4_add_memopb_io :
2177 case Hexagon::L4_sub_memopb_io :
2178 case Hexagon::L4_and_memopb_io :
2179 case Hexagon::L4_or_memopb_io :
2180 case Hexagon::L4_ior_memopb_io:
2181 case Hexagon::L4_ior_memoph_io:
2182 case Hexagon::L4_ior_memopw_io:
2183 case Hexagon::L4_iand_memopb_io:
2184 case Hexagon::L4_iand_memoph_io:
2185 case Hexagon::L4_iand_memopw_io:
2186 return true;
2187 }
2188 return false;
2189}
2190
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002191bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2192 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002193 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2194}
2195
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002196bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2197 const uint64_t F = get(Opcode).TSFlags;
2198 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2199}
2200
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002201bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002202 return isNewValueJump(MI) || isNewValueStore(MI);
2203}
2204
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002205bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2206 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002207}
2208
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002209bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2210 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2211}
2212
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002213bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2214 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002215 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2216}
2217
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002218bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2219 const uint64_t F = get(Opcode).TSFlags;
2220 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2221}
2222
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002223// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002224bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002225 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002226 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002227 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2228 == OperandNum;
2229}
2230
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002231bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2232 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002233 assert(isPredicated(MI));
2234 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2235}
2236
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002237bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2238 const uint64_t F = get(Opcode).TSFlags;
2239 assert(isPredicated(Opcode));
2240 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2241}
2242
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002243bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2244 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002245 return !((F >> HexagonII::PredicatedFalsePos) &
2246 HexagonII::PredicatedFalseMask);
2247}
2248
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002249bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2250 const uint64_t F = get(Opcode).TSFlags;
2251 // Make sure that the instruction is predicated.
2252 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2253 return !((F >> HexagonII::PredicatedFalsePos) &
2254 HexagonII::PredicatedFalseMask);
2255}
2256
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002257bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2258 const uint64_t F = get(Opcode).TSFlags;
2259 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2260}
2261
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002262bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2263 const uint64_t F = get(Opcode).TSFlags;
2264 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2265}
2266
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002267bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2268 const uint64_t F = get(Opcode).TSFlags;
2269 assert(get(Opcode).isBranch() &&
2270 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2271 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2272}
2273
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002274bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2275 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2276 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2277 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2278 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002279}
2280
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002281bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2282 switch (MI.getOpcode()) {
2283 // Byte
2284 case Hexagon::L2_loadrb_io:
2285 case Hexagon::L4_loadrb_ur:
2286 case Hexagon::L4_loadrb_ap:
2287 case Hexagon::L2_loadrb_pr:
2288 case Hexagon::L2_loadrb_pbr:
2289 case Hexagon::L2_loadrb_pi:
2290 case Hexagon::L2_loadrb_pci:
2291 case Hexagon::L2_loadrb_pcr:
2292 case Hexagon::L2_loadbsw2_io:
2293 case Hexagon::L4_loadbsw2_ur:
2294 case Hexagon::L4_loadbsw2_ap:
2295 case Hexagon::L2_loadbsw2_pr:
2296 case Hexagon::L2_loadbsw2_pbr:
2297 case Hexagon::L2_loadbsw2_pi:
2298 case Hexagon::L2_loadbsw2_pci:
2299 case Hexagon::L2_loadbsw2_pcr:
2300 case Hexagon::L2_loadbsw4_io:
2301 case Hexagon::L4_loadbsw4_ur:
2302 case Hexagon::L4_loadbsw4_ap:
2303 case Hexagon::L2_loadbsw4_pr:
2304 case Hexagon::L2_loadbsw4_pbr:
2305 case Hexagon::L2_loadbsw4_pi:
2306 case Hexagon::L2_loadbsw4_pci:
2307 case Hexagon::L2_loadbsw4_pcr:
2308 case Hexagon::L4_loadrb_rr:
2309 case Hexagon::L2_ploadrbt_io:
2310 case Hexagon::L2_ploadrbt_pi:
2311 case Hexagon::L2_ploadrbf_io:
2312 case Hexagon::L2_ploadrbf_pi:
2313 case Hexagon::L2_ploadrbtnew_io:
2314 case Hexagon::L2_ploadrbfnew_io:
2315 case Hexagon::L4_ploadrbt_rr:
2316 case Hexagon::L4_ploadrbf_rr:
2317 case Hexagon::L4_ploadrbtnew_rr:
2318 case Hexagon::L4_ploadrbfnew_rr:
2319 case Hexagon::L2_ploadrbtnew_pi:
2320 case Hexagon::L2_ploadrbfnew_pi:
2321 case Hexagon::L4_ploadrbt_abs:
2322 case Hexagon::L4_ploadrbf_abs:
2323 case Hexagon::L4_ploadrbtnew_abs:
2324 case Hexagon::L4_ploadrbfnew_abs:
2325 case Hexagon::L2_loadrbgp:
2326 // Half
2327 case Hexagon::L2_loadrh_io:
2328 case Hexagon::L4_loadrh_ur:
2329 case Hexagon::L4_loadrh_ap:
2330 case Hexagon::L2_loadrh_pr:
2331 case Hexagon::L2_loadrh_pbr:
2332 case Hexagon::L2_loadrh_pi:
2333 case Hexagon::L2_loadrh_pci:
2334 case Hexagon::L2_loadrh_pcr:
2335 case Hexagon::L4_loadrh_rr:
2336 case Hexagon::L2_ploadrht_io:
2337 case Hexagon::L2_ploadrht_pi:
2338 case Hexagon::L2_ploadrhf_io:
2339 case Hexagon::L2_ploadrhf_pi:
2340 case Hexagon::L2_ploadrhtnew_io:
2341 case Hexagon::L2_ploadrhfnew_io:
2342 case Hexagon::L4_ploadrht_rr:
2343 case Hexagon::L4_ploadrhf_rr:
2344 case Hexagon::L4_ploadrhtnew_rr:
2345 case Hexagon::L4_ploadrhfnew_rr:
2346 case Hexagon::L2_ploadrhtnew_pi:
2347 case Hexagon::L2_ploadrhfnew_pi:
2348 case Hexagon::L4_ploadrht_abs:
2349 case Hexagon::L4_ploadrhf_abs:
2350 case Hexagon::L4_ploadrhtnew_abs:
2351 case Hexagon::L4_ploadrhfnew_abs:
2352 case Hexagon::L2_loadrhgp:
2353 return true;
2354 default:
2355 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002356 }
2357}
2358
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002359bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2360 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002361 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2362}
2363
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002364bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2365 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002366 case Hexagon::STriw_pred :
2367 case Hexagon::LDriw_pred :
2368 return true;
2369 default:
2370 return false;
2371 }
2372}
2373
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002374bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2375 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002376 return false;
2377
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002378 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002379 if (Op.isGlobal() || Op.isSymbol())
2380 return true;
2381 return false;
2382}
2383
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002384// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002385bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2386 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002387 return is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002388}
2389
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002390bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2391 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002392 return is_TC2(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002393}
2394
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002395bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2396 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002397 return is_TC2early(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002398}
2399
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002400bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2401 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002402 return is_TC4x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002403}
2404
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002405// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002406bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2407 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002408 if (mayBeCurLoad(MI1)) {
2409 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002410 unsigned DstReg = MI1.getOperand(0).getReg();
2411 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002412 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002413 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002414 return true;
2415 }
2416 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002417 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2418 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2419 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002420 return true;
2421 return false;
2422}
2423
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002424bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002425 const uint64_t V = getType(MI);
2426 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2427}
2428
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002429// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2430//
2431bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2432 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2433 VT == MVT::v32i16 || VT == MVT::v64i8) {
2434 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2435 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2436 (Offset & 0x3f) == 0);
2437 }
2438 // 128B
2439 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2440 VT == MVT::v64i16 || VT == MVT::v128i8) {
2441 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2442 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2443 (Offset & 0x7f) == 0);
2444 }
2445 if (VT == MVT::i64) {
2446 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2447 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2448 (Offset & 0x7) == 0);
2449 }
2450 if (VT == MVT::i32) {
2451 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2452 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2453 (Offset & 0x3) == 0);
2454 }
2455 if (VT == MVT::i16) {
2456 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2457 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2458 (Offset & 0x1) == 0);
2459 }
2460 if (VT == MVT::i8) {
2461 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2462 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2463 }
2464 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002465}
2466
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002467bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2468 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002469 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002470 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002471 // inserted to calculate the final address. Due to this reason, the function
2472 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002473 // We used to assert if the offset was not properly aligned, however,
2474 // there are cases where a misaligned pointer recast can cause this
2475 // problem, and we need to allow for it. The front end warns of such
2476 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002477
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002478 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002479 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002480 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002481 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002482 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002483 case Hexagon::V6_vL32b_ai:
2484 case Hexagon::V6_vS32b_ai:
2485 case Hexagon::V6_vL32Ub_ai:
2486 case Hexagon::V6_vS32Ub_ai:
Krzysztof Parzyszek918e6d72017-06-26 14:17:58 +00002487 return isShiftedInt<4,6>(Offset);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002488
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002489 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002490 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002491 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002492 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002493 case Hexagon::V6_vL32b_ai_128B:
2494 case Hexagon::V6_vS32b_ai_128B:
2495 case Hexagon::V6_vL32Ub_ai_128B:
2496 case Hexagon::V6_vS32Ub_ai_128B:
Krzysztof Parzyszek918e6d72017-06-26 14:17:58 +00002497 return isShiftedInt<4,7>(Offset);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002498
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002499 case Hexagon::J2_loop0i:
2500 case Hexagon::J2_loop1i:
2501 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002502
2503 case Hexagon::S4_storeirb_io:
2504 case Hexagon::S4_storeirbt_io:
2505 case Hexagon::S4_storeirbf_io:
2506 return isUInt<6>(Offset);
2507
2508 case Hexagon::S4_storeirh_io:
2509 case Hexagon::S4_storeirht_io:
2510 case Hexagon::S4_storeirhf_io:
2511 return isShiftedUInt<6,1>(Offset);
2512
2513 case Hexagon::S4_storeiri_io:
2514 case Hexagon::S4_storeirit_io:
2515 case Hexagon::S4_storeirif_io:
2516 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002517 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002518
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002519 if (Extend)
2520 return true;
2521
2522 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002523 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002524 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002525 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2526 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2527
Colin LeMahieu947cd702014-12-23 20:44:59 +00002528 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002529 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002530 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2531 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2532
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002533 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002534 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002535 case Hexagon::S2_storerh_io:
Krzysztof Parzyszekd10df492017-05-03 15:36:51 +00002536 case Hexagon::S2_storerf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002537 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2538 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2539
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002540 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002541 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002542 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002543 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2544 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2545
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002546 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002547 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2548 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2549
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002550 case Hexagon::L4_iadd_memopw_io :
2551 case Hexagon::L4_isub_memopw_io :
2552 case Hexagon::L4_add_memopw_io :
2553 case Hexagon::L4_sub_memopw_io :
2554 case Hexagon::L4_and_memopw_io :
2555 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002556 return (0 <= Offset && Offset <= 255);
2557
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002558 case Hexagon::L4_iadd_memoph_io :
2559 case Hexagon::L4_isub_memoph_io :
2560 case Hexagon::L4_add_memoph_io :
2561 case Hexagon::L4_sub_memoph_io :
2562 case Hexagon::L4_and_memoph_io :
2563 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002564 return (0 <= Offset && Offset <= 127);
2565
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002566 case Hexagon::L4_iadd_memopb_io :
2567 case Hexagon::L4_isub_memopb_io :
2568 case Hexagon::L4_add_memopb_io :
2569 case Hexagon::L4_sub_memopb_io :
2570 case Hexagon::L4_and_memopb_io :
2571 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002572 return (0 <= Offset && Offset <= 63);
2573
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002574 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002575 // any size. Later pass knows how to handle it.
2576 case Hexagon::STriw_pred:
2577 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002578 case Hexagon::STriw_mod:
2579 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002580 return true;
2581
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002582 case Hexagon::PS_fi:
2583 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002584 case Hexagon::INLINEASM:
2585 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002586
2587 case Hexagon::L2_ploadrbt_io:
2588 case Hexagon::L2_ploadrbf_io:
2589 case Hexagon::L2_ploadrubt_io:
2590 case Hexagon::L2_ploadrubf_io:
2591 case Hexagon::S2_pstorerbt_io:
2592 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002593 return isUInt<6>(Offset);
2594
2595 case Hexagon::L2_ploadrht_io:
2596 case Hexagon::L2_ploadrhf_io:
2597 case Hexagon::L2_ploadruht_io:
2598 case Hexagon::L2_ploadruhf_io:
2599 case Hexagon::S2_pstorerht_io:
2600 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002601 return isShiftedUInt<6,1>(Offset);
2602
2603 case Hexagon::L2_ploadrit_io:
2604 case Hexagon::L2_ploadrif_io:
2605 case Hexagon::S2_pstorerit_io:
2606 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002607 return isShiftedUInt<6,2>(Offset);
2608
2609 case Hexagon::L2_ploadrdt_io:
2610 case Hexagon::L2_ploadrdf_io:
2611 case Hexagon::S2_pstorerdt_io:
2612 case Hexagon::S2_pstorerdf_io:
2613 return isShiftedUInt<6,3>(Offset);
2614 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002615
Benjamin Kramerb6684012011-12-27 11:41:05 +00002616 llvm_unreachable("No offset range is defined for this opcode. "
2617 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002618}
2619
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002620bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002621 return isHVXVec(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002622}
2623
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002624bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2625 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002626 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2627 return
2628 V == HexagonII::TypeCVI_VA ||
2629 V == HexagonII::TypeCVI_VA_DV;
2630}
Andrew Trickd06df962012-02-01 22:13:57 +00002631
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002632bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2633 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002634 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2635 return true;
2636
2637 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2638 return true;
2639
2640 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002641 return true;
2642
2643 return false;
2644}
Jyotsna Verma84256432013-03-01 17:37:13 +00002645
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002646bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2647 switch (MI.getOpcode()) {
2648 // Byte
2649 case Hexagon::L2_loadrub_io:
2650 case Hexagon::L4_loadrub_ur:
2651 case Hexagon::L4_loadrub_ap:
2652 case Hexagon::L2_loadrub_pr:
2653 case Hexagon::L2_loadrub_pbr:
2654 case Hexagon::L2_loadrub_pi:
2655 case Hexagon::L2_loadrub_pci:
2656 case Hexagon::L2_loadrub_pcr:
2657 case Hexagon::L2_loadbzw2_io:
2658 case Hexagon::L4_loadbzw2_ur:
2659 case Hexagon::L4_loadbzw2_ap:
2660 case Hexagon::L2_loadbzw2_pr:
2661 case Hexagon::L2_loadbzw2_pbr:
2662 case Hexagon::L2_loadbzw2_pi:
2663 case Hexagon::L2_loadbzw2_pci:
2664 case Hexagon::L2_loadbzw2_pcr:
2665 case Hexagon::L2_loadbzw4_io:
2666 case Hexagon::L4_loadbzw4_ur:
2667 case Hexagon::L4_loadbzw4_ap:
2668 case Hexagon::L2_loadbzw4_pr:
2669 case Hexagon::L2_loadbzw4_pbr:
2670 case Hexagon::L2_loadbzw4_pi:
2671 case Hexagon::L2_loadbzw4_pci:
2672 case Hexagon::L2_loadbzw4_pcr:
2673 case Hexagon::L4_loadrub_rr:
2674 case Hexagon::L2_ploadrubt_io:
2675 case Hexagon::L2_ploadrubt_pi:
2676 case Hexagon::L2_ploadrubf_io:
2677 case Hexagon::L2_ploadrubf_pi:
2678 case Hexagon::L2_ploadrubtnew_io:
2679 case Hexagon::L2_ploadrubfnew_io:
2680 case Hexagon::L4_ploadrubt_rr:
2681 case Hexagon::L4_ploadrubf_rr:
2682 case Hexagon::L4_ploadrubtnew_rr:
2683 case Hexagon::L4_ploadrubfnew_rr:
2684 case Hexagon::L2_ploadrubtnew_pi:
2685 case Hexagon::L2_ploadrubfnew_pi:
2686 case Hexagon::L4_ploadrubt_abs:
2687 case Hexagon::L4_ploadrubf_abs:
2688 case Hexagon::L4_ploadrubtnew_abs:
2689 case Hexagon::L4_ploadrubfnew_abs:
2690 case Hexagon::L2_loadrubgp:
2691 // Half
2692 case Hexagon::L2_loadruh_io:
2693 case Hexagon::L4_loadruh_ur:
2694 case Hexagon::L4_loadruh_ap:
2695 case Hexagon::L2_loadruh_pr:
2696 case Hexagon::L2_loadruh_pbr:
2697 case Hexagon::L2_loadruh_pi:
2698 case Hexagon::L2_loadruh_pci:
2699 case Hexagon::L2_loadruh_pcr:
2700 case Hexagon::L4_loadruh_rr:
2701 case Hexagon::L2_ploadruht_io:
2702 case Hexagon::L2_ploadruht_pi:
2703 case Hexagon::L2_ploadruhf_io:
2704 case Hexagon::L2_ploadruhf_pi:
2705 case Hexagon::L2_ploadruhtnew_io:
2706 case Hexagon::L2_ploadruhfnew_io:
2707 case Hexagon::L4_ploadruht_rr:
2708 case Hexagon::L4_ploadruhf_rr:
2709 case Hexagon::L4_ploadruhtnew_rr:
2710 case Hexagon::L4_ploadruhfnew_rr:
2711 case Hexagon::L2_ploadruhtnew_pi:
2712 case Hexagon::L2_ploadruhfnew_pi:
2713 case Hexagon::L4_ploadruht_abs:
2714 case Hexagon::L4_ploadruhf_abs:
2715 case Hexagon::L4_ploadruhtnew_abs:
2716 case Hexagon::L4_ploadruhfnew_abs:
2717 case Hexagon::L2_loadruhgp:
2718 return true;
2719 default:
2720 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002721 }
2722}
2723
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002724// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002725bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2726 const MachineInstr &MI2) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002727 if (isHVXVec(MI1) && isHVXVec(MI2))
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002728 if (!isVecUsableNextPacket(MI1, MI2))
2729 return true;
2730 return false;
2731}
2732
Brendon Cahoon254f8892016-07-29 16:44:44 +00002733/// \brief Get the base register and byte offset of a load/store instr.
2734bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2735 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2736 const {
2737 unsigned AccessSize = 0;
2738 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002739 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002740 Offset = OffsetVal;
2741 return BaseReg != 0;
2742}
2743
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002744/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002745bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2746 const MachineInstr &Second) const {
Krzysztof Parzyszek4763c2d2017-05-03 15:33:09 +00002747 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2748 const MachineOperand &Op = Second.getOperand(0);
2749 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2750 return true;
2751 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002752 if (DisableNVSchedule)
2753 return false;
2754 if (mayBeNewStore(Second)) {
2755 // Make sure the definition of the first instruction is the value being
2756 // stored.
2757 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002758 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002759 if (!Stored.isReg())
2760 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002761 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2762 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002763 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2764 return true;
2765 }
2766 }
2767 return false;
2768}
2769
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002770bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2771 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002772 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002773}
2774
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002775bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2776 for (auto &I : *B)
2777 if (I.isEHLabel())
2778 return true;
2779 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002780}
2781
Jyotsna Verma84256432013-03-01 17:37:13 +00002782// Returns true if an instruction can be converted into a non-extended
2783// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002784bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002785 short NonExtOpcode;
2786 // Check if the instruction has a register form that uses register in place
2787 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002788 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002789 return true;
2790
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002791 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002792 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002793
2794 switch (getAddrMode(MI)) {
2795 case HexagonII::Absolute :
2796 // Load/store with absolute addressing mode can be converted into
2797 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002798 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002799 break;
2800 case HexagonII::BaseImmOffset :
2801 // Load/store with base+offset addressing mode can be converted into
2802 // base+register offset addressing mode. However left shift operand should
2803 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002804 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002805 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002806 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002807 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002808 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002809 default:
2810 return false;
2811 }
2812 if (NonExtOpcode < 0)
2813 return false;
2814 return true;
2815 }
2816 return false;
2817}
2818
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002819bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2820 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002821 Hexagon::InstrType_Pseudo) >= 0;
2822}
2823
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002824bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2825 const {
2826 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2827 while (I != E) {
2828 if (I->isBarrier())
2829 return true;
2830 ++I;
2831 }
2832 return false;
2833}
2834
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002835// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002836bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
2837 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2838 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002839 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2840 HST.hasV60TOps();
2841}
2842
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002843// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002844bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2845 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002846 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2847}
2848
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002849bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2850 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002851 // There is no stall when ProdMI is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002852 if (!isHVXVec(ProdMI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002853 return false;
2854
2855 // There is no stall when ProdMI and ConsMI are not dependent.
2856 if (!isDependent(ProdMI, ConsMI))
2857 return false;
2858
2859 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2860 // are scheduled in consecutive packets.
2861 if (isVecUsableNextPacket(ProdMI, ConsMI))
2862 return false;
2863
2864 return true;
2865}
2866
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002867bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002868 MachineBasicBlock::const_instr_iterator BII) const {
2869 // There is no stall when I is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002870 if (!isHVXVec(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002871 return false;
2872
2873 MachineBasicBlock::const_instr_iterator MII = BII;
2874 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2875
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002876 if (!(*MII).isBundle()) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002877 const MachineInstr &J = *MII;
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002878 return producesStall(J, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002879 }
2880
2881 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002882 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002883 if (producesStall(J, MI))
2884 return true;
2885 }
2886 return false;
2887}
2888
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002889bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002890 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002891 for (const MachineOperand &MO : MI.operands()) {
2892 // Predicate register must be explicitly defined.
2893 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
2894 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002895 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002896 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002897 }
2898
2899 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2900 // memd_locked cannot be used as .new as well,
2901 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002902 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002903}
2904
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002905bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00002906 return Opcode == Hexagon::J2_jumpt ||
2907 Opcode == Hexagon::J2_jumptpt ||
2908 Opcode == Hexagon::J2_jumpf ||
2909 Opcode == Hexagon::J2_jumpfpt ||
2910 Opcode == Hexagon::J2_jumptnew ||
2911 Opcode == Hexagon::J2_jumpfnew ||
2912 Opcode == Hexagon::J2_jumptnewpt ||
2913 Opcode == Hexagon::J2_jumpfnewpt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002914}
2915
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002916bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2917 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2918 return false;
2919 return !isPredicatedTrue(Cond[0].getImm());
2920}
2921
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002922short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
2923 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00002924}
2925
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002926unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
2927 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002928 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
2929}
2930
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002931// Returns the base register in a memory access (load/store). The offset is
2932// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002933unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002934 int &Offset, unsigned &AccessSize) const {
2935 // Return if it is not a base+offset type instruction or a MemOp.
2936 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
2937 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002938 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002939 return 0;
2940
2941 // Since it is a memory access instruction, getMemAccessSize() should never
2942 // return 0.
2943 assert (getMemAccessSize(MI) &&
2944 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
2945
2946 // Return Values of getMemAccessSize() are
2947 // 0 - Checked in the assert above.
2948 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
2949 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
2950 AccessSize = (1U << (getMemAccessSize(MI) - 1));
2951
2952 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002953 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002954 return 0;
2955
2956 // Post increment updates its EA after the mem access,
2957 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002958 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002959 Offset = 0;
2960 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002961 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002962 }
2963
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002964 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002965}
2966
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002967/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002968bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002969 unsigned &BasePos, unsigned &OffsetPos) const {
2970 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002971 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002972 BasePos = 0;
2973 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002974 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002975 BasePos = 0;
2976 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002977 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002978 BasePos = 1;
2979 OffsetPos = 2;
2980 } else
2981 return false;
2982
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002983 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002984 BasePos++;
2985 OffsetPos++;
2986 }
2987 if (isPostIncrement(MI)) {
2988 BasePos++;
2989 OffsetPos++;
2990 }
2991
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002992 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002993 return false;
2994
2995 return true;
2996}
2997
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002998// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002999// e.g. jump_t t1 (i1)
3000// jump t2 (i2)
3001// Jumpers = {i2, i1}
3002SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3003 MachineBasicBlock& MBB) const {
3004 SmallVector<MachineInstr*, 2> Jumpers;
3005 // If the block has no terminators, it just falls into the block after it.
3006 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3007 if (I == MBB.instr_begin())
3008 return Jumpers;
3009
3010 // A basic block may looks like this:
3011 //
3012 // [ insn
3013 // EH_LABEL
3014 // insn
3015 // insn
3016 // insn
3017 // EH_LABEL
3018 // insn ]
3019 //
3020 // It has two succs but does not have a terminator
3021 // Don't know how to handle it.
3022 do {
3023 --I;
3024 if (I->isEHLabel())
3025 return Jumpers;
3026 } while (I != MBB.instr_begin());
3027
3028 I = MBB.instr_end();
3029 --I;
3030
3031 while (I->isDebugValue()) {
3032 if (I == MBB.instr_begin())
3033 return Jumpers;
3034 --I;
3035 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003036 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003037 return Jumpers;
3038
3039 // Get the last instruction in the block.
3040 MachineInstr *LastInst = &*I;
3041 Jumpers.push_back(LastInst);
3042 MachineInstr *SecondLastInst = nullptr;
3043 // Find one more terminator if present.
3044 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003045 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003046 if (!SecondLastInst) {
3047 SecondLastInst = &*I;
3048 Jumpers.push_back(SecondLastInst);
3049 } else // This is a third branch.
3050 return Jumpers;
3051 }
3052 if (I == MBB.instr_begin())
3053 break;
3054 --I;
3055 } while (true);
3056 return Jumpers;
3057}
3058
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003059short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3060 if (Opcode < 0)
3061 return -1;
3062 return Hexagon::getBaseWithLongOffset(Opcode);
3063}
3064
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003065short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3066 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003067}
3068
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003069short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3070 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003071}
3072
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003073// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003074unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3075 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003076 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3077}
3078
3079// See if instruction could potentially be a duplex candidate.
3080// If so, return its group. Zero otherwise.
3081HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003082 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003083 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3084
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003085 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003086 default:
3087 return HexagonII::HCG_None;
3088 //
3089 // Compound pairs.
3090 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3091 // "Rd16=#U6 ; jump #r9:2"
3092 // "Rd16=Rs16 ; jump #r9:2"
3093 //
3094 case Hexagon::C2_cmpeq:
3095 case Hexagon::C2_cmpgt:
3096 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003097 DstReg = MI.getOperand(0).getReg();
3098 Src1Reg = MI.getOperand(1).getReg();
3099 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003100 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3101 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3102 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3103 return HexagonII::HCG_A;
3104 break;
3105 case Hexagon::C2_cmpeqi:
3106 case Hexagon::C2_cmpgti:
3107 case Hexagon::C2_cmpgtui:
3108 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003109 DstReg = MI.getOperand(0).getReg();
3110 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003111 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3112 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003113 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3114 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3115 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003116 return HexagonII::HCG_A;
3117 break;
3118 case Hexagon::A2_tfr:
3119 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003120 DstReg = MI.getOperand(0).getReg();
3121 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003122 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3123 return HexagonII::HCG_A;
3124 break;
3125 case Hexagon::A2_tfrsi:
3126 // Rd = #u6
3127 // Do not test for #u6 size since the const is getting extended
3128 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003129 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003130 if (isIntRegForSubInst(DstReg))
3131 return HexagonII::HCG_A;
3132 break;
3133 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003134 DstReg = MI.getOperand(0).getReg();
3135 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003136 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3137 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003138 MI.getOperand(2).isImm() &&
3139 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003140 return HexagonII::HCG_A;
3141 break;
3142 // The fact that .new form is used pretty much guarantees
3143 // that predicate register will match. Nevertheless,
3144 // there could be some false positives without additional
3145 // checking.
3146 case Hexagon::J2_jumptnew:
3147 case Hexagon::J2_jumpfnew:
3148 case Hexagon::J2_jumptnewpt:
3149 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003150 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003151 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3152 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3153 return HexagonII::HCG_B;
3154 break;
3155 // Transfer and jump:
3156 // Rd=#U6 ; jump #r9:2
3157 // Rd=Rs ; jump #r9:2
3158 // Do not test for jump range here.
3159 case Hexagon::J2_jump:
3160 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003161 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003162 return HexagonII::HCG_C;
3163 break;
3164 }
3165
3166 return HexagonII::HCG_None;
3167}
3168
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003169// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003170unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3171 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003172 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3173 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003174 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3175 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003176 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003177 unsigned DestReg = GA.getOperand(0).getReg();
3178 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003179 return -1;
3180 if (DestReg == Hexagon::P0)
3181 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3182 if (DestReg == Hexagon::P1)
3183 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3184 return -1;
3185}
3186
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003187int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3188 enum Hexagon::PredSense inPredSense;
3189 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3190 Hexagon::PredSense_true;
3191 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3192 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3193 return CondOpcode;
3194
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003195 llvm_unreachable("Unexpected predicable instruction");
3196}
3197
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003198// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003199int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3200 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003201 default: llvm_unreachable("Unknown .cur type");
3202 case Hexagon::V6_vL32b_pi:
3203 return Hexagon::V6_vL32b_cur_pi;
3204 case Hexagon::V6_vL32b_ai:
3205 return Hexagon::V6_vL32b_cur_ai;
3206 //128B
3207 case Hexagon::V6_vL32b_pi_128B:
3208 return Hexagon::V6_vL32b_cur_pi_128B;
3209 case Hexagon::V6_vL32b_ai_128B:
3210 return Hexagon::V6_vL32b_cur_ai_128B;
3211 }
3212 return 0;
3213}
3214
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003215// Return the regular version of the .cur instruction.
3216int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3217 switch (MI.getOpcode()) {
3218 default: llvm_unreachable("Unknown .cur type");
3219 case Hexagon::V6_vL32b_cur_pi:
3220 return Hexagon::V6_vL32b_pi;
3221 case Hexagon::V6_vL32b_cur_ai:
3222 return Hexagon::V6_vL32b_ai;
3223 //128B
3224 case Hexagon::V6_vL32b_cur_pi_128B:
3225 return Hexagon::V6_vL32b_pi_128B;
3226 case Hexagon::V6_vL32b_cur_ai_128B:
3227 return Hexagon::V6_vL32b_ai_128B;
3228 }
3229 return 0;
3230}
3231
3232
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003233// The diagram below shows the steps involved in the conversion of a predicated
3234// store instruction to its .new predicated new-value form.
3235//
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003236// Note: It doesn't include conditional new-value stores as they can't be
3237// converted to .new predicate.
3238//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003239// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3240// ^ ^
3241// / \ (not OK. it will cause new-value store to be
3242// / X conditional on p0.new while R2 producer is
3243// / \ on p0)
3244// / \.
3245// p.new store p.old NV store
3246// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3247// ^ ^
3248// \ /
3249// \ /
3250// \ /
3251// p.old store
3252// [if (p0)memw(R0+#0)=R2]
3253//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003254// The following set of instructions further explains the scenario where
3255// conditional new-value store becomes invalid when promoted to .new predicate
3256// form.
3257//
3258// { 1) if (p0) r0 = add(r1, r2)
3259// 2) p0 = cmp.eq(r3, #0) }
3260//
3261// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3262// the first two instructions because in instr 1, r0 is conditional on old value
3263// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3264// is not valid for new-value stores.
3265// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3266// from the "Conditional Store" list. Because a predicated new value store
3267// would NOT be promoted to a double dot new store. See diagram below:
3268// This function returns yes for those stores that are predicated but not
3269// yet promoted to predicate dot new instructions.
3270//
3271// +---------------------+
3272// /-----| if (p0) memw(..)=r0 |---------\~
3273// || +---------------------+ ||
3274// promote || /\ /\ || promote
3275// || /||\ /||\ ||
3276// \||/ demote || \||/
3277// \/ || || \/
3278// +-------------------------+ || +-------------------------+
3279// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3280// +-------------------------+ || +-------------------------+
3281// || || ||
3282// || demote \||/
3283// promote || \/ NOT possible
3284// || || /\~
3285// \||/ || /||\~
3286// \/ || ||
3287// +-----------------------------+
3288// | if (p0.new) memw(..)=r0.new |
3289// +-----------------------------+
3290// Double Dot New Store
3291//
3292// Returns the most basic instruction for the .new predicated instructions and
3293// new-value stores.
3294// For example, all of the following instructions will be converted back to the
3295// same instruction:
3296// 1) if (p0.new) memw(R0+#0) = R1.new --->
3297// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3298// 3) if (p0.new) memw(R0+#0) = R1 --->
3299//
3300// To understand the translation of instruction 1 to its original form, consider
3301// a packet with 3 instructions.
3302// { p0 = cmp.eq(R0,R1)
3303// if (p0.new) R2 = add(R3, R4)
3304// R5 = add (R3, R1)
3305// }
3306// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3307//
3308// This instruction can be part of the previous packet only if both p0 and R2
3309// are promoted to .new values. This promotion happens in steps, first
3310// predicate register is promoted to .new and in the next iteration R2 is
3311// promoted. Therefore, in case of dependence check failure (due to R5) during
3312// next iteration, it should be converted back to its most basic form.
3313
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003314// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003315int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3316 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003317 if (NVOpcode >= 0) // Valid new-value store instruction.
3318 return NVOpcode;
3319
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003320 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003321 default:
3322 llvm::report_fatal_error(std::string("Unknown .new type: ") +
3323 std::to_string(MI.getOpcode()).c_str());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003324 case Hexagon::S4_storerb_ur:
3325 return Hexagon::S4_storerbnew_ur;
3326
3327 case Hexagon::S2_storerb_pci:
3328 return Hexagon::S2_storerb_pci;
3329
3330 case Hexagon::S2_storeri_pci:
3331 return Hexagon::S2_storeri_pci;
3332
3333 case Hexagon::S2_storerh_pci:
3334 return Hexagon::S2_storerh_pci;
3335
3336 case Hexagon::S2_storerd_pci:
3337 return Hexagon::S2_storerd_pci;
3338
3339 case Hexagon::S2_storerf_pci:
3340 return Hexagon::S2_storerf_pci;
3341
3342 case Hexagon::V6_vS32b_ai:
3343 return Hexagon::V6_vS32b_new_ai;
3344
3345 case Hexagon::V6_vS32b_pi:
3346 return Hexagon::V6_vS32b_new_pi;
3347
3348 // 128B
3349 case Hexagon::V6_vS32b_ai_128B:
3350 return Hexagon::V6_vS32b_new_ai_128B;
3351
3352 case Hexagon::V6_vS32b_pi_128B:
3353 return Hexagon::V6_vS32b_new_pi_128B;
3354 }
3355 return 0;
3356}
3357
3358// Returns the opcode to use when converting MI, which is a conditional jump,
3359// into a conditional instruction which uses the .new value of the predicate.
3360// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003361// If MBPI is null, all edges will be treated as equally likely for the
3362// purposes of establishing a predication hint.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003363int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003364 const MachineBranchProbabilityInfo *MBPI) const {
3365 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003366 const MachineBasicBlock *Src = MI.getParent();
3367 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003368 bool Taken = false;
3369 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003370
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003371 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3372 const MachineBasicBlock *Dst) {
3373 if (MBPI)
3374 return MBPI->getEdgeProbability(Src, Dst);
3375 return BranchProbability(1, Src->succ_size());
3376 };
3377
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003378 if (BrTarget.isMBB()) {
3379 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003380 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003381 } else {
3382 // The branch target is not a basic block (most likely a function).
3383 // Since BPI only gives probabilities for targets that are basic blocks,
3384 // try to identify another target of this branch (potentially a fall-
3385 // -through) and check the probability of that target.
3386 //
3387 // The only handled branch combinations are:
3388 // - one conditional branch,
3389 // - one conditional branch followed by one unconditional branch.
3390 // Otherwise, assume not-taken.
3391 assert(MI.isConditionalBranch());
3392 const MachineBasicBlock &B = *MI.getParent();
3393 bool SawCond = false, Bad = false;
3394 for (const MachineInstr &I : B) {
3395 if (!I.isBranch())
3396 continue;
3397 if (I.isConditionalBranch()) {
3398 SawCond = true;
3399 if (&I != &MI) {
3400 Bad = true;
3401 break;
3402 }
3403 }
3404 if (I.isUnconditionalBranch() && !SawCond) {
3405 Bad = true;
3406 break;
3407 }
3408 }
3409 if (!Bad) {
3410 MachineBasicBlock::const_instr_iterator It(MI);
3411 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3412 if (NextIt == B.instr_end()) {
3413 // If this branch is the last, look for the fall-through block.
3414 for (const MachineBasicBlock *SB : B.successors()) {
3415 if (!B.isLayoutSuccessor(SB))
3416 continue;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003417 Taken = getEdgeProbability(Src, SB) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003418 break;
3419 }
3420 } else {
3421 assert(NextIt->isUnconditionalBranch());
3422 // Find the first MBB operand and assume it's the target.
3423 const MachineBasicBlock *BT = nullptr;
3424 for (const MachineOperand &Op : NextIt->operands()) {
3425 if (!Op.isMBB())
3426 continue;
3427 BT = Op.getMBB();
3428 break;
3429 }
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003430 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003431 }
3432 } // if (!Bad)
3433 }
3434
3435 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003436
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003437 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003438 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003439 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003440 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003441 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003442
3443 default:
3444 llvm_unreachable("Unexpected jump instruction.");
3445 }
3446}
3447
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003448// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003449int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003450 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003451 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003452 // Condtional Jumps
3453 case Hexagon::J2_jumpt:
3454 case Hexagon::J2_jumpf:
3455 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003456 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003457
3458 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3459 if (NewOpcode >= 0)
3460 return NewOpcode;
Krzysztof Parzyszek066e8b52017-06-02 14:07:06 +00003461 return 0;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003462}
3463
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003464int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003465 const MachineFunction &MF = *MI.getParent()->getParent();
3466 const HexagonSubtarget &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003467 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003468 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3469 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003470 // All Hexagon architectures have prediction bits on dot-new branches,
3471 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3472 // to pick the right opcode when converting back to dot-old.
3473 if (!HST.getFeatureBits()[Hexagon::ArchV60]) {
3474 switch (NewOp) {
3475 case Hexagon::J2_jumptpt:
3476 NewOp = Hexagon::J2_jumpt;
3477 break;
3478 case Hexagon::J2_jumpfpt:
3479 NewOp = Hexagon::J2_jumpf;
3480 break;
3481 case Hexagon::J2_jumprtpt:
3482 NewOp = Hexagon::J2_jumprt;
3483 break;
3484 case Hexagon::J2_jumprfpt:
3485 NewOp = Hexagon::J2_jumprf;
3486 break;
3487 }
3488 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003489 assert(NewOp >= 0 &&
3490 "Couldn't change predicate new instruction to its old form.");
3491 }
3492
3493 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3494 NewOp = Hexagon::getNonNVStore(NewOp);
3495 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3496 }
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003497
3498 if (HST.hasV60TOps())
3499 return NewOp;
3500
3501 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3502 switch (NewOp) {
3503 case Hexagon::J2_jumpfpt:
3504 return Hexagon::J2_jumpf;
3505 case Hexagon::J2_jumptpt:
3506 return Hexagon::J2_jumpt;
3507 case Hexagon::J2_jumprfpt:
3508 return Hexagon::J2_jumprf;
3509 case Hexagon::J2_jumprtpt:
3510 return Hexagon::J2_jumprt;
3511 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003512 return NewOp;
3513}
3514
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003515// See if instruction could potentially be a duplex candidate.
3516// If so, return its group. Zero otherwise.
3517HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003518 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003519 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3520 auto &HRI = getRegisterInfo();
3521
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003522 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003523 default:
3524 return HexagonII::HSIG_None;
3525 //
3526 // Group L1:
3527 //
3528 // Rd = memw(Rs+#u4:2)
3529 // Rd = memub(Rs+#u4:0)
3530 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003531 DstReg = MI.getOperand(0).getReg();
3532 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003533 // Special case this one from Group L2.
3534 // Rd = memw(r29+#u5:2)
3535 if (isIntRegForSubInst(DstReg)) {
3536 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3537 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003538 MI.getOperand(2).isImm() &&
3539 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003540 return HexagonII::HSIG_L2;
3541 // Rd = memw(Rs+#u4:2)
3542 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003543 (MI.getOperand(2).isImm() &&
3544 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003545 return HexagonII::HSIG_L1;
3546 }
3547 break;
3548 case Hexagon::L2_loadrub_io:
3549 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003550 DstReg = MI.getOperand(0).getReg();
3551 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003552 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003553 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003554 return HexagonII::HSIG_L1;
3555 break;
3556 //
3557 // Group L2:
3558 //
3559 // Rd = memh/memuh(Rs+#u3:1)
3560 // Rd = memb(Rs+#u3:0)
3561 // Rd = memw(r29+#u5:2) - Handled above.
3562 // Rdd = memd(r29+#u5:3)
3563 // deallocframe
3564 // [if ([!]p0[.new])] dealloc_return
3565 // [if ([!]p0[.new])] jumpr r31
3566 case Hexagon::L2_loadrh_io:
3567 case Hexagon::L2_loadruh_io:
3568 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003569 DstReg = MI.getOperand(0).getReg();
3570 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003571 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003572 MI.getOperand(2).isImm() &&
3573 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003574 return HexagonII::HSIG_L2;
3575 break;
3576 case Hexagon::L2_loadrb_io:
3577 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003578 DstReg = MI.getOperand(0).getReg();
3579 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003580 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003581 MI.getOperand(2).isImm() &&
3582 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003583 return HexagonII::HSIG_L2;
3584 break;
3585 case Hexagon::L2_loadrd_io:
3586 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003587 DstReg = MI.getOperand(0).getReg();
3588 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003589 if (isDblRegForSubInst(DstReg, HRI) &&
3590 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3591 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003592 MI.getOperand(2).isImm() &&
3593 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003594 return HexagonII::HSIG_L2;
3595 break;
3596 // dealloc_return is not documented in Hexagon Manual, but marked
3597 // with A_SUBINSN attribute in iset_v4classic.py.
3598 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003599 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003600 case Hexagon::L4_return:
3601 case Hexagon::L2_deallocframe:
3602 return HexagonII::HSIG_L2;
3603 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003604 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003605 // jumpr r31
3606 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003607 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003608 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3609 return HexagonII::HSIG_L2;
3610 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003611 case Hexagon::PS_jmprett:
3612 case Hexagon::PS_jmpretf:
3613 case Hexagon::PS_jmprettnewpt:
3614 case Hexagon::PS_jmpretfnewpt:
3615 case Hexagon::PS_jmprettnew:
3616 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003617 DstReg = MI.getOperand(1).getReg();
3618 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003619 // [if ([!]p0[.new])] jumpr r31
3620 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3621 (Hexagon::P0 == SrcReg)) &&
3622 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3623 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003624 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003625 case Hexagon::L4_return_t :
3626 case Hexagon::L4_return_f :
3627 case Hexagon::L4_return_tnew_pnt :
3628 case Hexagon::L4_return_fnew_pnt :
3629 case Hexagon::L4_return_tnew_pt :
3630 case Hexagon::L4_return_fnew_pt :
3631 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003632 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003633 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3634 return HexagonII::HSIG_L2;
3635 break;
3636 //
3637 // Group S1:
3638 //
3639 // memw(Rs+#u4:2) = Rt
3640 // memb(Rs+#u4:0) = Rt
3641 case Hexagon::S2_storeri_io:
3642 // Special case this one from Group S2.
3643 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003644 Src1Reg = MI.getOperand(0).getReg();
3645 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003646 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3647 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003648 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3649 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003650 return HexagonII::HSIG_S2;
3651 // memw(Rs+#u4:2) = Rt
3652 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003653 MI.getOperand(1).isImm() &&
3654 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003655 return HexagonII::HSIG_S1;
3656 break;
3657 case Hexagon::S2_storerb_io:
3658 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003659 Src1Reg = MI.getOperand(0).getReg();
3660 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003661 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003662 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003663 return HexagonII::HSIG_S1;
3664 break;
3665 //
3666 // Group S2:
3667 //
3668 // memh(Rs+#u3:1) = Rt
3669 // memw(r29+#u5:2) = Rt
3670 // memd(r29+#s6:3) = Rtt
3671 // memw(Rs+#u4:2) = #U1
3672 // memb(Rs+#u4) = #U1
3673 // allocframe(#u5:3)
3674 case Hexagon::S2_storerh_io:
3675 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003676 Src1Reg = MI.getOperand(0).getReg();
3677 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003678 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003679 MI.getOperand(1).isImm() &&
3680 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003681 return HexagonII::HSIG_S1;
3682 break;
3683 case Hexagon::S2_storerd_io:
3684 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003685 Src1Reg = MI.getOperand(0).getReg();
3686 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003687 if (isDblRegForSubInst(Src2Reg, HRI) &&
3688 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003689 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3690 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003691 return HexagonII::HSIG_S2;
3692 break;
3693 case Hexagon::S4_storeiri_io:
3694 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003695 Src1Reg = MI.getOperand(0).getReg();
3696 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3697 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3698 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003699 return HexagonII::HSIG_S2;
3700 break;
3701 case Hexagon::S4_storeirb_io:
3702 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003703 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003704 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003705 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3706 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003707 return HexagonII::HSIG_S2;
3708 break;
3709 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003710 if (MI.getOperand(0).isImm() &&
3711 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003712 return HexagonII::HSIG_S1;
3713 break;
3714 //
3715 // Group A:
3716 //
3717 // Rx = add(Rx,#s7)
3718 // Rd = Rs
3719 // Rd = #u6
3720 // Rd = #-1
3721 // if ([!]P0[.new]) Rd = #0
3722 // Rd = add(r29,#u6:2)
3723 // Rx = add(Rx,Rs)
3724 // P0 = cmp.eq(Rs,#u2)
3725 // Rdd = combine(#0,Rs)
3726 // Rdd = combine(Rs,#0)
3727 // Rdd = combine(#u2,#U2)
3728 // Rd = add(Rs,#1)
3729 // Rd = add(Rs,#-1)
3730 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3731 // Rd = and(Rs,#1)
3732 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003733 DstReg = MI.getOperand(0).getReg();
3734 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003735 if (isIntRegForSubInst(DstReg)) {
3736 // Rd = add(r29,#u6:2)
3737 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003738 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3739 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003740 return HexagonII::HSIG_A;
3741 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003742 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3743 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003744 return HexagonII::HSIG_A;
3745 // Rd = add(Rs,#1)
3746 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003747 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3748 ((MI.getOperand(2).getImm() == 1) ||
3749 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003750 return HexagonII::HSIG_A;
3751 }
3752 break;
3753 case Hexagon::A2_add:
3754 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003755 DstReg = MI.getOperand(0).getReg();
3756 Src1Reg = MI.getOperand(1).getReg();
3757 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003758 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3759 isIntRegForSubInst(Src2Reg))
3760 return HexagonII::HSIG_A;
3761 break;
3762 case Hexagon::A2_andir:
3763 // Same as zxtb.
3764 // Rd16=and(Rs16,#255)
3765 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003766 DstReg = MI.getOperand(0).getReg();
3767 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003768 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003769 MI.getOperand(2).isImm() &&
3770 ((MI.getOperand(2).getImm() == 1) ||
3771 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003772 return HexagonII::HSIG_A;
3773 break;
3774 case Hexagon::A2_tfr:
3775 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003776 DstReg = MI.getOperand(0).getReg();
3777 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003778 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3779 return HexagonII::HSIG_A;
3780 break;
3781 case Hexagon::A2_tfrsi:
3782 // Rd = #u6
3783 // Do not test for #u6 size since the const is getting extended
3784 // regardless and compound could be formed.
3785 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003786 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003787 if (isIntRegForSubInst(DstReg))
3788 return HexagonII::HSIG_A;
3789 break;
3790 case Hexagon::C2_cmoveit:
3791 case Hexagon::C2_cmovenewit:
3792 case Hexagon::C2_cmoveif:
3793 case Hexagon::C2_cmovenewif:
3794 // if ([!]P0[.new]) Rd = #0
3795 // Actual form:
3796 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003797 DstReg = MI.getOperand(0).getReg();
3798 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003799 if (isIntRegForSubInst(DstReg) &&
3800 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003801 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003802 return HexagonII::HSIG_A;
3803 break;
3804 case Hexagon::C2_cmpeqi:
3805 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003806 DstReg = MI.getOperand(0).getReg();
3807 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003808 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3809 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003810 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003811 return HexagonII::HSIG_A;
3812 break;
3813 case Hexagon::A2_combineii:
3814 case Hexagon::A4_combineii:
3815 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003816 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003817 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003818 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3819 (MI.getOperand(1).isGlobal() &&
3820 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3821 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3822 (MI.getOperand(2).isGlobal() &&
3823 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003824 return HexagonII::HSIG_A;
3825 break;
3826 case Hexagon::A4_combineri:
3827 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003828 DstReg = MI.getOperand(0).getReg();
3829 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003830 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003831 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3832 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003833 return HexagonII::HSIG_A;
3834 break;
3835 case Hexagon::A4_combineir:
3836 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003837 DstReg = MI.getOperand(0).getReg();
3838 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003839 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003840 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3841 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003842 return HexagonII::HSIG_A;
3843 break;
3844 case Hexagon::A2_sxtb:
3845 case Hexagon::A2_sxth:
3846 case Hexagon::A2_zxtb:
3847 case Hexagon::A2_zxth:
3848 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003849 DstReg = MI.getOperand(0).getReg();
3850 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003851 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3852 return HexagonII::HSIG_A;
3853 break;
3854 }
3855
3856 return HexagonII::HSIG_None;
3857}
3858
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003859short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3860 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003861}
3862
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003863unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003864 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003865 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3866 // still have a MinLatency property, which getStageLatency checks.
3867 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003868 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003869
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003870 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003871 return 0;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003872 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
3873}
3874
3875/// getOperandLatency - Compute and return the use operand latency of a given
3876/// pair of def and use.
3877/// In most cases, the static scheduling itinerary was enough to determine the
3878/// operand latency. But it may not be possible for instructions with variable
3879/// number of defs / uses.
3880///
3881/// This is a raw interface to the itinerary that may be directly overriden by
3882/// a target. Use computeOperandLatency to get the best estimate of latency.
3883int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3884 const MachineInstr &DefMI,
3885 unsigned DefIdx,
3886 const MachineInstr &UseMI,
3887 unsigned UseIdx) const {
3888 auto &RI = getRegisterInfo();
3889 // Get DefIdx and UseIdx for super registers.
3890 MachineOperand DefMO = DefMI.getOperand(DefIdx);
3891
3892 if (RI.isPhysicalRegister(DefMO.getReg())) {
3893 if (DefMO.isImplicit()) {
3894 for (MCSuperRegIterator SR(DefMO.getReg(), &RI); SR.isValid(); ++SR) {
3895 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &RI);
3896 if (Idx != -1) {
3897 DefIdx = Idx;
3898 break;
3899 }
3900 }
3901 }
3902
3903 MachineOperand UseMO = UseMI.getOperand(UseIdx);
3904 if (UseMO.isImplicit()) {
3905 for (MCSuperRegIterator SR(UseMO.getReg(), &RI); SR.isValid(); ++SR) {
3906 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &RI);
3907 if (Idx != -1) {
3908 UseIdx = Idx;
3909 break;
3910 }
3911 }
3912 }
3913 }
3914
3915 return TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
3916 UseMI, UseIdx);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003917}
3918
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003919// inverts the predication logic.
3920// p -> NotP
3921// NotP -> P
3922bool HexagonInstrInfo::getInvertedPredSense(
3923 SmallVectorImpl<MachineOperand> &Cond) const {
3924 if (Cond.empty())
3925 return false;
3926 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3927 Cond[0].setImm(Opc);
3928 return true;
3929}
3930
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003931unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3932 int InvPredOpcode;
3933 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3934 : Hexagon::getTruePredOpcode(Opc);
3935 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3936 return InvPredOpcode;
3937
3938 llvm_unreachable("Unexpected predicated instruction");
3939}
3940
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003941// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003942int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
3943 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003944 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3945 & HexagonII::ExtentSignedMask;
3946 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3947 & HexagonII::ExtentBitsMask;
3948
3949 if (isSigned) // if value is signed
3950 return ~(-1U << (bits - 1));
3951 else
3952 return ~(-1U << bits);
3953}
3954
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003955unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
3956 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003957 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3958}
3959
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003960// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003961int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
3962 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003963 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3964 & HexagonII::ExtentSignedMask;
3965 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3966 & HexagonII::ExtentBitsMask;
3967
3968 if (isSigned) // if value is signed
3969 return -1U << (bits - 1);
3970 else
3971 return 0;
3972}
3973
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003974// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003975short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003976 // Check if the instruction has a register form that uses register in place
3977 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003978 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003979 if (NonExtOpcode >= 0)
3980 return NonExtOpcode;
3981
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003982 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003983 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003984 switch (getAddrMode(MI)) {
3985 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003986 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003987 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003988 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003989 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003990 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003991
Jyotsna Verma84256432013-03-01 17:37:13 +00003992 default:
3993 return -1;
3994 }
3995 }
3996 return -1;
3997}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00003998
Ahmed Bougachac88bf542015-06-11 19:30:37 +00003999bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004000 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004001 if (Cond.empty())
4002 return false;
4003 assert(Cond.size() == 2);
4004 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004005 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4006 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004007 }
4008 PredReg = Cond[1].getReg();
4009 PredRegPos = 1;
4010 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4011 PredRegFlags = 0;
4012 if (Cond[1].isImplicit())
4013 PredRegFlags = RegState::Implicit;
4014 if (Cond[1].isUndef())
4015 PredRegFlags |= RegState::Undef;
4016 return true;
4017}
4018
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004019short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4020 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004021}
4022
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004023short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4024 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004025}
4026
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004027// Return the number of bytes required to encode the instruction.
4028// Hexagon instructions are fixed length, 4 bytes, unless they
4029// use a constant extender, which requires another 4 bytes.
4030// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004031unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4032 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004033 return 0;
4034
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004035 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004036 if (!Size)
4037 // Assume the default insn size in case it cannot be determined
4038 // for whatever reason.
4039 Size = HEXAGON_INSTR_SIZE;
4040
4041 if (isConstExtended(MI) || isExtended(MI))
4042 Size += HEXAGON_INSTR_SIZE;
4043
4044 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004045 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4046 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004047 const MachineFunction *MF = MBB.getParent();
4048 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4049
4050 // Count the number of register definitions to find the asm string.
4051 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004052 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004053 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004054 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004055
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004056 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004057 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004058 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004059 Size = getInlineAsmLength(AsmStr, *MAI);
4060 }
4061
4062 return Size;
4063}
4064
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004065uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4066 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004067 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4068}
4069
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004070unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4071 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004072 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004073 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004074
4075 return IS.getUnits();
4076}
4077
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004078// Calculate size of the basic block without debug instructions.
4079unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4080 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4081}
4082
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004083unsigned HexagonInstrInfo::nonDbgBundleSize(
4084 MachineBasicBlock::const_iterator BundleHead) const {
4085 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004086 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004087 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004088 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004089}
4090
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004091/// immediateExtend - Changes the instruction in place to one using an immediate
4092/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004093void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004094 assert((isExtendable(MI)||isConstExtended(MI)) &&
4095 "Instruction must be extendable");
4096 // Find which operand is extendable.
4097 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004098 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004099 // This needs to be something we understand.
4100 assert((MO.isMBB() || MO.isImm()) &&
4101 "Branch with unknown extendable field type");
4102 // Mark given operand as extended.
4103 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4104}
4105
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004106bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004107 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004108 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004109 << NewTarget->getNumber(); MI.dump(););
4110 assert(MI.isBranch());
4111 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4112 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004113 // In general branch target is the last operand,
4114 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004115 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004116 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004117 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4118 MI.getOperand(TargetPos).setMBB(NewTarget);
4119 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004120 NewOpcode = reversePrediction(NewOpcode);
4121 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004122 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004123 return true;
4124}
4125
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004126void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4127 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4128 MachineFunction::iterator A = MF.begin();
4129 MachineBasicBlock &B = *A;
4130 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004131 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004132 MachineInstr *NewMI;
4133
4134 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4135 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004136 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004137 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4138 " Class: " << NewMI->getDesc().getSchedClass());
4139 NewMI->eraseFromParent();
4140 }
4141 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4142}
4143
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004144// inverts the predication logic.
4145// p -> NotP
4146// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004147bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4148 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4149 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004150 return true;
4151}
4152
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004153// Reverse the branch prediction.
4154unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4155 int PredRevOpcode = -1;
4156 if (isPredictedTaken(Opcode))
4157 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4158 else
4159 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4160 assert(PredRevOpcode > 0);
4161 return PredRevOpcode;
4162}
4163
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004164// TODO: Add more rigorous validation.
4165bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4166 const {
4167 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4168}
4169
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004170short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4171 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004172}