blob: 813f4f81b415e0123276e487d9e26f349321f545 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Hexagon implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000014#include "Hexagon.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000015#include "HexagonHazardRecognizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonInstrInfo.h"
Craig Topperb25fda92012-03-17 18:46:09 +000017#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "HexagonSubtarget.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000019#include "llvm/ADT/SmallPtrSet.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000021#include "llvm/ADT/StringRef.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000022#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000023#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
31#include "llvm/CodeGen/MachineLoopInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000033#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000035#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000036#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000037#include "llvm/MC/MCInstrDesc.h"
38#include "llvm/MC/MCInstrItineraries.h"
39#include "llvm/MC/MCRegisterInfo.h"
40#include "llvm/Support/BranchProbability.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000041#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000042#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000043#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000044#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000045#include "llvm/Support/raw_ostream.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetSubtargetInfo.h"
48#include <cassert>
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000049#include <cctype>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000050#include <cstdint>
51#include <cstring>
52#include <iterator>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054using namespace llvm;
55
Chandler Carruthe96dd892014-04-21 22:55:11 +000056#define DEBUG_TYPE "hexagon-instrinfo"
57
Chandler Carruthd174b722014-04-22 02:03:14 +000058#define GET_INSTRINFO_CTOR_DTOR
59#define GET_INSTRMAP_INFO
60#include "HexagonGenInstrInfo.inc"
61#include "HexagonGenDFAPacketizer.inc"
62
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000063cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000064 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
65 "packetization boundary."));
66
67static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
68 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
69
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000070static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
71 cl::Hidden, cl::ZeroOrMore, cl::init(false),
72 cl::desc("Disable schedule adjustment for new value stores."));
73
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000074static cl::opt<bool> EnableTimingClassLatency(
75 "enable-timing-class-latency", cl::Hidden, cl::init(false),
76 cl::desc("Enable timing class latency"));
77
78static cl::opt<bool> EnableALUForwarding(
79 "enable-alu-forwarding", cl::Hidden, cl::init(true),
80 cl::desc("Enable vec alu forwarding"));
81
82static cl::opt<bool> EnableACCForwarding(
83 "enable-acc-forwarding", cl::Hidden, cl::init(true),
84 cl::desc("Enable vec acc forwarding"));
85
86static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
87 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
88
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000089static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
90 cl::init(true), cl::Hidden, cl::ZeroOrMore,
91 cl::desc("Use the DFA based hazard recognizer."));
92
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093///
94/// Constants for Hexagon instructions.
95///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000096const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
97const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
98const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
99const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000100const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000101const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000103const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000105const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000107const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000108const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000109const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000111const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000112const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000113const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000115const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000116const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000117const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +0000118const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
119const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
120const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
121const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000122
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000123// Pin the vtable to this file.
124void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000125
126HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000127 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000128 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000130static bool isIntRegForSubInst(unsigned Reg) {
131 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
132 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000133}
134
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000135static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000136 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
137 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000138}
139
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000140/// Calculate number of instructions excluding the debug instructions.
141static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
142 MachineBasicBlock::const_instr_iterator MIE) {
143 unsigned Count = 0;
144 for (; MIB != MIE; ++MIB) {
145 if (!MIB->isDebugValue())
146 ++Count;
147 }
148 return Count;
149}
150
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000151/// Find the hardware loop instruction used to set-up the specified loop.
152/// On Hexagon, we have two instructions used to set-up the hardware loop
153/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
154/// to indicate the end of a loop.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000155static MachineInstr *findLoopInstr(MachineBasicBlock *BB, unsigned EndLoopOp,
156 MachineBasicBlock *TargetBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000157 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000158 unsigned LOOPi;
159 unsigned LOOPr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000160 if (EndLoopOp == Hexagon::ENDLOOP0) {
161 LOOPi = Hexagon::J2_loop0i;
162 LOOPr = Hexagon::J2_loop0r;
163 } else { // EndLoopOp == Hexagon::EndLOOP1
164 LOOPi = Hexagon::J2_loop1i;
165 LOOPr = Hexagon::J2_loop1r;
166 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000167
Brendon Cahoondf43e682015-05-08 16:16:29 +0000168 // The loop set-up instruction will be in a predecessor block
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000169 for (MachineBasicBlock *PB : BB->predecessors()) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000170 // If this has been visited, already skip it.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000171 if (!Visited.insert(PB).second)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000172 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000173 if (PB == BB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000174 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000175 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
176 unsigned Opc = I->getOpcode();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000177 if (Opc == LOOPi || Opc == LOOPr)
178 return &*I;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000179 // We've reached a different loop, which means the loop01 has been
180 // removed.
181 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000182 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000183 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000184 // Check the predecessors for the LOOP instruction.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000185 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
186 return Loop;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000187 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000188 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000189}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000190
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000191/// Gather register def/uses from MI.
192/// This treats possible (predicated) defs as actually happening ones
193/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000194static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000195 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
196 Defs.clear();
197 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000198
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000199 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
200 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000201
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000202 if (!MO.isReg())
203 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000204
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000205 unsigned Reg = MO.getReg();
206 if (!Reg)
207 continue;
208
209 if (MO.isUse())
210 Uses.push_back(MO.getReg());
211
212 if (MO.isDef())
213 Defs.push_back(MO.getReg());
214 }
215}
216
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000217// Position dependent, so check twice for swap.
218static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
219 switch (Ga) {
220 case HexagonII::HSIG_None:
221 default:
222 return false;
223 case HexagonII::HSIG_L1:
224 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
225 case HexagonII::HSIG_L2:
226 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
227 Gb == HexagonII::HSIG_A);
228 case HexagonII::HSIG_S1:
229 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
230 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
231 case HexagonII::HSIG_S2:
232 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
233 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
234 Gb == HexagonII::HSIG_A);
235 case HexagonII::HSIG_A:
236 return (Gb == HexagonII::HSIG_A);
237 case HexagonII::HSIG_Compound:
238 return (Gb == HexagonII::HSIG_Compound);
239 }
240 return false;
241}
242
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000243/// isLoadFromStackSlot - If the specified machine instruction is a direct
244/// load from a stack slot, return the virtual or physical register number of
245/// the destination along with the FrameIndex of the loaded stack slot. If
246/// not, return 0. This predicate must return 0 if the instruction has
247/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000248unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000249 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000250 switch (MI.getOpcode()) {
251 default:
252 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000253 case Hexagon::L2_loadri_io:
254 case Hexagon::L2_loadrd_io:
255 case Hexagon::V6_vL32b_ai:
256 case Hexagon::V6_vL32b_ai_128B:
257 case Hexagon::V6_vL32Ub_ai:
258 case Hexagon::V6_vL32Ub_ai_128B:
259 case Hexagon::LDriw_pred:
260 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000261 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000262 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000263 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000264 case Hexagon::PS_vloadrw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000265 const MachineOperand OpFI = MI.getOperand(1);
266 if (!OpFI.isFI())
267 return 0;
268 const MachineOperand OpOff = MI.getOperand(2);
269 if (!OpOff.isImm() || OpOff.getImm() != 0)
270 return 0;
271 FrameIndex = OpFI.getIndex();
272 return MI.getOperand(0).getReg();
273 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000274
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000275 case Hexagon::L2_ploadrit_io:
276 case Hexagon::L2_ploadrif_io:
277 case Hexagon::L2_ploadrdt_io:
278 case Hexagon::L2_ploadrdf_io: {
279 const MachineOperand OpFI = MI.getOperand(2);
280 if (!OpFI.isFI())
281 return 0;
282 const MachineOperand OpOff = MI.getOperand(3);
283 if (!OpOff.isImm() || OpOff.getImm() != 0)
284 return 0;
285 FrameIndex = OpFI.getIndex();
286 return MI.getOperand(0).getReg();
287 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000288 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000289
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000290 return 0;
291}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000292
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000293/// isStoreToStackSlot - If the specified machine instruction is a direct
294/// store to a stack slot, return the virtual or physical register number of
295/// the source reg along with the FrameIndex of the loaded stack slot. If
296/// not, return 0. This predicate must return 0 if the instruction has
297/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000298unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000299 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000300 switch (MI.getOpcode()) {
301 default:
302 break;
303 case Hexagon::S2_storerb_io:
304 case Hexagon::S2_storerh_io:
305 case Hexagon::S2_storeri_io:
306 case Hexagon::S2_storerd_io:
307 case Hexagon::V6_vS32b_ai:
308 case Hexagon::V6_vS32b_ai_128B:
309 case Hexagon::V6_vS32Ub_ai:
310 case Hexagon::V6_vS32Ub_ai_128B:
311 case Hexagon::STriw_pred:
312 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000313 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000314 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000315 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000316 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000317 const MachineOperand &OpFI = MI.getOperand(0);
318 if (!OpFI.isFI())
319 return 0;
320 const MachineOperand &OpOff = MI.getOperand(1);
321 if (!OpOff.isImm() || OpOff.getImm() != 0)
322 return 0;
323 FrameIndex = OpFI.getIndex();
324 return MI.getOperand(2).getReg();
325 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000326
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000327 case Hexagon::S2_pstorerbt_io:
328 case Hexagon::S2_pstorerbf_io:
329 case Hexagon::S2_pstorerht_io:
330 case Hexagon::S2_pstorerhf_io:
331 case Hexagon::S2_pstorerit_io:
332 case Hexagon::S2_pstorerif_io:
333 case Hexagon::S2_pstorerdt_io:
334 case Hexagon::S2_pstorerdf_io: {
335 const MachineOperand &OpFI = MI.getOperand(1);
336 if (!OpFI.isFI())
337 return 0;
338 const MachineOperand &OpOff = MI.getOperand(2);
339 if (!OpOff.isImm() || OpOff.getImm() != 0)
340 return 0;
341 FrameIndex = OpFI.getIndex();
342 return MI.getOperand(3).getReg();
343 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000344 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000345
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000346 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000347}
348
Brendon Cahoondf43e682015-05-08 16:16:29 +0000349/// This function can analyze one/two way branching only and should (mostly) be
350/// called by target independent side.
351/// First entry is always the opcode of the branching instruction, except when
352/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
353/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
354/// e.g. Jump_c p will have
355/// Cond[0] = Jump_c
356/// Cond[1] = p
357/// HW-loop ENDLOOP:
358/// Cond[0] = ENDLOOP
359/// Cond[1] = MBB
360/// New value jump:
361/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
362/// Cond[1] = R
363/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000364///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000365bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000366 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000367 MachineBasicBlock *&FBB,
368 SmallVectorImpl<MachineOperand> &Cond,
369 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000370 TBB = nullptr;
371 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000372 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000373
374 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000375 MachineBasicBlock::instr_iterator I = MBB.instr_end();
376 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377 return false;
378
379 // A basic block may looks like this:
380 //
381 // [ insn
382 // EH_LABEL
383 // insn
384 // insn
385 // insn
386 // EH_LABEL
387 // insn ]
388 //
389 // It has two succs but does not have a terminator
390 // Don't know how to handle it.
391 do {
392 --I;
393 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000394 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000395 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000396 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000397
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000398 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000399 --I;
400
401 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000402 if (I == MBB.instr_begin())
403 return false;
404 --I;
405 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000406
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000407 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
408 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000409 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000410 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000411 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000412 DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000413 I->eraseFromParent();
414 I = MBB.instr_end();
415 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000416 return false;
417 --I;
418 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000419 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000420 return false;
421
422 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000423 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000424 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000425 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000426 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000427 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000428 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000429 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000430 else
431 // This is a third branch.
432 return true;
433 }
434 if (I == MBB.instr_begin())
435 break;
436 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000437 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000438
439 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000440 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
441 // If the branch target is not a basic block, it could be a tail call.
442 // (It is, if the target is a function.)
443 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
444 return true;
445 if (SecLastOpcode == Hexagon::J2_jump &&
446 !SecondLastInst->getOperand(0).isMBB())
447 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000448
449 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000450 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000452 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
453 return true;
454
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000456 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000457 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000458 TBB = LastInst->getOperand(0).getMBB();
459 return false;
460 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000461 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000462 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000463 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000464 Cond.push_back(LastInst->getOperand(0));
465 return false;
466 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000467 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000468 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000469 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000470 Cond.push_back(LastInst->getOperand(0));
471 return false;
472 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000473 // Only supporting rr/ri versions of new-value jumps.
474 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
475 TBB = LastInst->getOperand(2).getMBB();
476 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
477 Cond.push_back(LastInst->getOperand(0));
478 Cond.push_back(LastInst->getOperand(1));
479 return false;
480 }
481 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
482 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000483 // Otherwise, don't know what this is.
484 return true;
485 }
486
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000487 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000488 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000489 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000490 if (!SecondLastInst->getOperand(1).isMBB())
491 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000492 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000493 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000494 Cond.push_back(SecondLastInst->getOperand(0));
495 FBB = LastInst->getOperand(0).getMBB();
496 return false;
497 }
498
Brendon Cahoondf43e682015-05-08 16:16:29 +0000499 // Only supporting rr/ri versions of new-value jumps.
500 if (SecLastOpcodeHasNVJump &&
501 (SecondLastInst->getNumExplicitOperands() == 3) &&
502 (LastOpcode == Hexagon::J2_jump)) {
503 TBB = SecondLastInst->getOperand(2).getMBB();
504 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
505 Cond.push_back(SecondLastInst->getOperand(0));
506 Cond.push_back(SecondLastInst->getOperand(1));
507 FBB = LastInst->getOperand(0).getMBB();
508 return false;
509 }
510
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000511 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
512 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000513 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000514 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000515 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000516 if (AllowModify)
517 I->eraseFromParent();
518 return false;
519 }
520
Brendon Cahoondf43e682015-05-08 16:16:29 +0000521 // If the block ends with an ENDLOOP, and J2_jump, handle it.
522 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000523 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000524 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000525 Cond.push_back(SecondLastInst->getOperand(0));
526 FBB = LastInst->getOperand(0).getMBB();
527 return false;
528 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000529 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
530 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000531 // Otherwise, can't handle this.
532 return true;
533}
534
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000535unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000536 int *BytesRemoved) const {
537 assert(!BytesRemoved && "code size not handled");
538
Brendon Cahoondf43e682015-05-08 16:16:29 +0000539 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000540 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000541 unsigned Count = 0;
542 while (I != MBB.begin()) {
543 --I;
544 if (I->isDebugValue())
545 continue;
546 // Only removing branches from end of MBB.
547 if (!I->isBranch())
548 return Count;
549 if (Count && (I->getOpcode() == Hexagon::J2_jump))
550 llvm_unreachable("Malformed basic block: unconditional branch not last");
551 MBB.erase(&MBB.back());
552 I = MBB.end();
553 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000554 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000555 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000556}
557
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000558unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000559 MachineBasicBlock *TBB,
560 MachineBasicBlock *FBB,
561 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000562 const DebugLoc &DL,
563 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000564 unsigned BOpc = Hexagon::J2_jump;
565 unsigned BccOpc = Hexagon::J2_jumpt;
566 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000567 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000568 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000569
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000570 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000571 // If we want to reverse the branch an odd number of times, we want
572 // J2_jumpf.
573 if (!Cond.empty() && Cond[0].isImm())
574 BccOpc = Cond[0].getImm();
575
576 if (!FBB) {
577 if (Cond.empty()) {
578 // Due to a bug in TailMerging/CFG Optimization, we need to add a
579 // special case handling of a predicated jump followed by an
580 // unconditional jump. If not, Tail Merging and CFG Optimization go
581 // into an infinite loop.
582 MachineBasicBlock *NewTBB, *NewFBB;
583 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000584 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000585 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000586 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
587 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000588 reverseBranchCondition(Cond);
589 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000590 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000591 }
592 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
593 } else if (isEndLoopN(Cond[0].getImm())) {
594 int EndLoopOp = Cond[0].getImm();
595 assert(Cond[1].isMBB());
596 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
597 // Check for it, and change the BB target if needed.
598 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000599 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
600 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000601 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
602 Loop->getOperand(0).setMBB(TBB);
603 // Add the ENDLOOP after the finding the LOOP0.
604 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
605 } else if (isNewValueJump(Cond[0].getImm())) {
606 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
607 // New value jump
608 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
609 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
610 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
611 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
612 if (Cond[2].isReg()) {
613 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
614 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
615 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
616 } else if(Cond[2].isImm()) {
617 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
618 addImm(Cond[2].getImm()).addMBB(TBB);
619 } else
620 llvm_unreachable("Invalid condition for branching");
621 } else {
622 assert((Cond.size() == 2) && "Malformed cond vector");
623 const MachineOperand &RO = Cond[1];
624 unsigned Flags = getUndefRegState(RO.isUndef());
625 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
626 }
627 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000628 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000629 assert((!Cond.empty()) &&
630 "Cond. cannot be empty when multiple branchings are required");
631 assert((!isNewValueJump(Cond[0].getImm())) &&
632 "NV-jump cannot be inserted with another branch");
633 // Special case for hardware loops. The condition is a basic block.
634 if (isEndLoopN(Cond[0].getImm())) {
635 int EndLoopOp = Cond[0].getImm();
636 assert(Cond[1].isMBB());
637 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
638 // Check for it, and change the BB target if needed.
639 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000640 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
641 VisitedBBs);
642if (Loop == 0) {
643 MachineFunction &MF = *MBB.getParent();
644 MF.print(dbgs(), 0);
645}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000646 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
647 Loop->getOperand(0).setMBB(TBB);
648 // Add the ENDLOOP after the finding the LOOP0.
649 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
650 } else {
651 const MachineOperand &RO = Cond[1];
652 unsigned Flags = getUndefRegState(RO.isUndef());
653 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000654 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000655 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000656
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000657 return 2;
658}
659
Brendon Cahoon254f8892016-07-29 16:44:44 +0000660/// Analyze the loop code to find the loop induction variable and compare used
661/// to compute the number of iterations. Currently, we analyze loop that are
662/// controlled using hardware loops. In this case, the induction variable
663/// instruction is null. For all other cases, this function returns true, which
664/// means we're unable to analyze it.
665bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
666 MachineInstr *&IndVarInst,
667 MachineInstr *&CmpInst) const {
668
669 MachineBasicBlock *LoopEnd = L.getBottomBlock();
670 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
671 // We really "analyze" only hardware loops right now.
672 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
673 IndVarInst = nullptr;
674 CmpInst = &*I;
675 return false;
676 }
677 return true;
678}
679
680/// Generate code to reduce the loop iteration by one and check if the loop is
681/// finished. Return the value/register of the new loop count. this function
682/// assumes the nth iteration is peeled first.
683unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000684 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000685 SmallVectorImpl<MachineOperand> &Cond,
686 SmallVectorImpl<MachineInstr *> &PrevInsts,
687 unsigned Iter, unsigned MaxIter) const {
688 // We expect a hardware loop currently. This means that IndVar is set
689 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000690 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000691 && "Expecting a hardware loop");
692 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000693 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000694 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000695 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(),
696 Cmp.getOperand(0).getMBB(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000697 if (!Loop)
698 return 0;
699 // If the loop trip count is a compile-time value, then just change the
700 // value.
701 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
702 Loop->getOpcode() == Hexagon::J2_loop1i) {
703 int64_t Offset = Loop->getOperand(1).getImm();
704 if (Offset <= 1)
705 Loop->eraseFromParent();
706 else
707 Loop->getOperand(1).setImm(Offset - 1);
708 return Offset - 1;
709 }
710 // The loop trip count is a run-time value. We generate code to subtract
711 // one from the trip count, and update the loop instruction.
712 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
713 unsigned LoopCount = Loop->getOperand(1).getReg();
714 // Check if we're done with the loop.
715 unsigned LoopEnd = createVR(MF, MVT::i1);
716 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
717 addReg(LoopCount).addImm(1);
718 unsigned NewLoopCount = createVR(MF, MVT::i32);
719 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
720 addReg(LoopCount).addImm(-1);
721 // Update the previously generated instructions with the new loop counter.
722 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
723 E = PrevInsts.end(); I != E; ++I)
724 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
725 PrevInsts.clear();
726 PrevInsts.push_back(NewCmp);
727 PrevInsts.push_back(NewAdd);
728 // Insert the new loop instruction if this is the last time the loop is
729 // decremented.
730 if (Iter == MaxIter)
731 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
732 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
733 // Delete the old loop instruction.
734 if (Iter == 0)
735 Loop->eraseFromParent();
736 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
737 Cond.push_back(NewCmp->getOperand(0));
738 return NewLoopCount;
739}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000740
741bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
742 unsigned NumCycles, unsigned ExtraPredCycles,
743 BranchProbability Probability) const {
744 return nonDbgBBSize(&MBB) <= 3;
745}
746
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000747bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
748 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
749 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
750 const {
751 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
752}
753
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000754bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
755 unsigned NumInstrs, BranchProbability Probability) const {
756 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000757}
758
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000759void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000760 MachineBasicBlock::iterator I,
761 const DebugLoc &DL, unsigned DestReg,
762 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000763 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000764 unsigned KillFlag = getKillRegState(KillSrc);
765
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000766 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000767 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000768 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000769 return;
770 }
771 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000772 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
773 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774 return;
775 }
776 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
777 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000778 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
779 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000780 return;
781 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000782 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000783 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000784 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
785 .addReg(SrcReg, KillFlag);
786 return;
787 }
788 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
789 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
790 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
791 .addReg(SrcReg, KillFlag);
792 return;
793 }
794 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
795 Hexagon::IntRegsRegClass.contains(SrcReg)) {
796 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
797 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000798 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000799 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000800 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
801 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000802 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
803 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000804 return;
805 }
806 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
807 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000808 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
809 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000810 return;
811 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000812 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
813 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000814 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
815 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000816 return;
817 }
818 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
819 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000820 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000821 return;
822 }
823 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000824 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
825 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000826 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000827 .addReg(HiSrc, KillFlag)
828 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000829 return;
830 }
831 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000832 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
833 .addReg(SrcReg)
834 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000835 return;
836 }
837 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000838 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000839 llvm_unreachable("Unimplemented pred to vec");
840 return;
841 }
842 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
843 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
844 llvm_unreachable("Unimplemented vec to pred");
845 return;
846 }
847 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000848 unsigned HiDst = HRI.getSubReg(DestReg, Hexagon::vsub_hi);
849 unsigned LoDst = HRI.getSubReg(DestReg, Hexagon::vsub_lo);
850 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
851 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
852 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), HiDst)
853 .addReg(HiSrc, KillFlag);
854 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), LoDst)
855 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000856 return;
857 }
Sirish Pande30804c22012-02-15 18:52:27 +0000858
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000859#ifndef NDEBUG
860 // Show the invalid registers to ease debugging.
861 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
862 << ": " << PrintReg(DestReg, &HRI)
863 << " = " << PrintReg(SrcReg, &HRI) << '\n';
864#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000865 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000866}
867
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000868void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
869 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
870 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 DebugLoc DL = MBB.findDebugLoc(I);
872 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000873 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000874 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000875 unsigned KillFlag = getKillRegState(isKill);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000876
Alex Lorenze40c8a22015-08-11 23:09:45 +0000877 MachineMemOperand *MMO = MF.getMachineMemOperand(
878 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
879 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000880
Craig Topperc7242e02012-04-20 07:30:17 +0000881 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000882 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_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::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000886 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000887 .addFrameIndex(FI).addImm(0)
888 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000889 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000890 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000891 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000892 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000893 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
894 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
895 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000896 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
897 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000898 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai_128B))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000899 .addFrameIndex(FI).addImm(0)
900 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
901 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000902 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000903 .addFrameIndex(FI).addImm(0)
904 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
905 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000906 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
907 : Hexagon::V6_vS32b_ai_128B;
908 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000909 .addFrameIndex(FI).addImm(0)
910 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
911 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000912 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
913 : Hexagon::V6_vS32b_ai;
914 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000915 .addFrameIndex(FI).addImm(0)
916 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
917 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000918 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
919 : Hexagon::PS_vstorerw_ai;
920 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000921 .addFrameIndex(FI).addImm(0)
922 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
923 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000924 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
925 : Hexagon::PS_vstorerw_ai_128B;
926 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000927 .addFrameIndex(FI).addImm(0)
928 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000929 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000930 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000931 }
932}
933
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000934void HexagonInstrInfo::loadRegFromStackSlot(
935 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
936 int FI, const TargetRegisterClass *RC,
937 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000938 DebugLoc DL = MBB.findDebugLoc(I);
939 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000940 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000941 unsigned Align = MFI.getObjectAlignment(FI);
942
Alex Lorenze40c8a22015-08-11 23:09:45 +0000943 MachineMemOperand *MMO = MF.getMachineMemOperand(
944 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
945 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000946
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000947 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000948 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000949 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000950 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000951 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000952 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000953 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000954 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000955 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
956 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
957 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
958 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000959 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000960 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000961 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
962 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000963 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000964 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
965 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000966 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
967 : Hexagon::PS_vloadrw_ai_128B;
968 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000969 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
970 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000971 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
972 : Hexagon::V6_vL32b_ai_128B;
973 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000974 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
975 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000976 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
977 : Hexagon::V6_vL32b_ai;
978 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000979 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
980 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000981 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
982 : Hexagon::PS_vloadrw_ai;
983 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000984 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000985 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000986 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000987 }
988}
989
Ron Lieberman88159e52016-09-02 22:56:24 +0000990static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
991 const MachineBasicBlock &B = *MI.getParent();
992 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000993 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +0000994 for (auto I = B.rbegin(); I != E; ++I)
995 Regs.stepBackward(*I);
996}
997
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000998/// expandPostRAPseudo - This function is called for all pseudo instructions
999/// that remain after register allocation. Many pseudo instructions are
1000/// created to help register allocation. This is the place to convert them
1001/// into real instructions. The target can edit MI in place, or it can insert
1002/// new instructions and erase MI. The function should return true if
1003/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001004bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001005 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001006 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1007 MachineBasicBlock &MBB = *MI.getParent();
1008 DebugLoc DL = MI.getDebugLoc();
1009 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001010 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001011
1012 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001013 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001014 MachineOperand &MD = MI.getOperand(0);
1015 MachineOperand &MS = MI.getOperand(1);
1016 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001017 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1018 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001019 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001020 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001021 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001022 return true;
1023 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001024 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001025 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001026 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001027 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001028 MBB.erase(MI);
1029 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001030 case Hexagon::V6_vassignp_128B:
1031 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001032 unsigned SrcReg = MI.getOperand(1).getReg();
1033 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001034 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1035 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001036 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1037 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001038 MBB.erase(MI);
1039 return true;
1040 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001041 case Hexagon::V6_lo_128B:
1042 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001043 unsigned SrcReg = MI.getOperand(1).getReg();
1044 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001045 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001046 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001047 MBB.erase(MI);
1048 MRI.clearKillFlags(SrcSubLo);
1049 return true;
1050 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001051 case Hexagon::V6_hi_128B:
1052 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001053 unsigned SrcReg = MI.getOperand(1).getReg();
1054 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001055 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001056 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001057 MBB.erase(MI);
1058 MRI.clearKillFlags(SrcSubHi);
1059 return true;
1060 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001061 case Hexagon::PS_vstorerw_ai:
1062 case Hexagon::PS_vstorerwu_ai:
1063 case Hexagon::PS_vstorerw_ai_128B:
1064 case Hexagon::PS_vstorerwu_ai_128B: {
1065 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1066 Opc == Hexagon::PS_vstorerwu_ai_128B);
1067 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1068 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001069 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001070 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1071 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001072 unsigned NewOpc;
1073 if (Aligned)
1074 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1075 : Hexagon::V6_vS32b_ai;
1076 else
1077 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1078 : Hexagon::V6_vS32Ub_ai;
1079
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001080 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001081 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001082 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001083 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001084 .addImm(MI.getOperand(1).getImm())
1085 .addReg(SrcSubLo)
1086 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001087 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001088 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001089 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001090 // The Vectors are indexed in multiples of vector size.
1091 .addImm(MI.getOperand(1).getImm() + Offset)
1092 .addReg(SrcSubHi)
1093 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001094 MBB.erase(MI);
1095 return true;
1096 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001097 case Hexagon::PS_vloadrw_ai:
1098 case Hexagon::PS_vloadrwu_ai:
1099 case Hexagon::PS_vloadrw_ai_128B:
1100 case Hexagon::PS_vloadrwu_ai_128B: {
1101 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1102 Opc == Hexagon::PS_vloadrwu_ai_128B);
1103 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1104 Opc == Hexagon::PS_vloadrw_ai_128B);
1105 unsigned NewOpc;
1106 if (Aligned)
1107 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1108 : Hexagon::V6_vL32b_ai;
1109 else
1110 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1111 : Hexagon::V6_vL32Ub_ai;
1112
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001113 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001114 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Diana Picus116bbab2017-01-13 09:58:52 +00001115 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1116 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
1117 .add(MI.getOperand(1))
1118 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001119 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001120 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1121 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001122 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001123 .addImm(MI.getOperand(2).getImm() + Offset)
1124 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001125 MBB.erase(MI);
1126 return true;
1127 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001128 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001129 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001130 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1131 .addReg(Reg, RegState::Undef)
1132 .addReg(Reg, RegState::Undef);
1133 MBB.erase(MI);
1134 return true;
1135 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001136 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001137 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001138 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1139 .addReg(Reg, RegState::Undef)
1140 .addReg(Reg, RegState::Undef);
1141 MBB.erase(MI);
1142 return true;
1143 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001144 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001145 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001146 unsigned DstReg = MI.getOperand(0).getReg();
1147 unsigned Src1Reg = MI.getOperand(1).getReg();
1148 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001149 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1150 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1151 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1152 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001153 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001154 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001155 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001156 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001157 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001158 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001159 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001160 .addReg(Src2SubLo);
1161 MBB.erase(MI);
1162 MRI.clearKillFlags(Src1SubHi);
1163 MRI.clearKillFlags(Src1SubLo);
1164 MRI.clearKillFlags(Src2SubHi);
1165 MRI.clearKillFlags(Src2SubLo);
1166 return true;
1167 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001168 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001169 // Expand 64-bit vector multiply with addition into 2 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();
1173 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001174 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1175 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1176 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1177 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1178 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1179 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001180 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001181 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001182 .addReg(Src1SubHi)
1183 .addReg(Src2SubHi)
1184 .addReg(Src3SubHi);
1185 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001186 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001187 .addReg(Src1SubLo)
1188 .addReg(Src2SubLo)
1189 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001190 MBB.erase(MI);
1191 MRI.clearKillFlags(Src1SubHi);
1192 MRI.clearKillFlags(Src1SubLo);
1193 MRI.clearKillFlags(Src2SubHi);
1194 MRI.clearKillFlags(Src2SubLo);
1195 MRI.clearKillFlags(Src3SubHi);
1196 MRI.clearKillFlags(Src3SubLo);
1197 return true;
1198 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001199 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001200 const MachineOperand &Op0 = MI.getOperand(0);
1201 const MachineOperand &Op1 = MI.getOperand(1);
1202 const MachineOperand &Op2 = MI.getOperand(2);
1203 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001204 unsigned Rd = Op0.getReg();
1205 unsigned Pu = Op1.getReg();
1206 unsigned Rs = Op2.getReg();
1207 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001208 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001209 unsigned K1 = getKillRegState(Op1.isKill());
1210 unsigned K2 = getKillRegState(Op2.isKill());
1211 unsigned K3 = getKillRegState(Op3.isKill());
1212 if (Rd != Rs)
1213 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1214 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1215 .addReg(Rs, K2);
1216 if (Rd != Rt)
1217 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1218 .addReg(Pu, K1)
1219 .addReg(Rt, K3);
1220 MBB.erase(MI);
1221 return true;
1222 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001223 case Hexagon::PS_vselect:
1224 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001225 const MachineOperand &Op0 = MI.getOperand(0);
1226 const MachineOperand &Op1 = MI.getOperand(1);
1227 const MachineOperand &Op2 = MI.getOperand(2);
1228 const MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001229 LivePhysRegs LiveAtMI(&HRI);
1230 getLiveRegsAt(LiveAtMI, MI);
1231 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1232 if (Op0.getReg() != Op2.getReg()) {
1233 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001234 .add(Op0)
1235 .add(Op1)
1236 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001237 if (IsDestLive)
1238 T.addReg(Op0.getReg(), RegState::Implicit);
1239 IsDestLive = true;
1240 }
1241 if (Op0.getReg() != Op3.getReg()) {
1242 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001243 .add(Op0)
1244 .add(Op1)
1245 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001246 if (IsDestLive)
1247 T.addReg(Op0.getReg(), RegState::Implicit);
1248 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001249 MBB.erase(MI);
1250 return true;
1251 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001252 case Hexagon::PS_wselect:
1253 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001254 MachineOperand &Op0 = MI.getOperand(0);
1255 MachineOperand &Op1 = MI.getOperand(1);
1256 MachineOperand &Op2 = MI.getOperand(2);
1257 MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001258 LivePhysRegs LiveAtMI(&HRI);
1259 getLiveRegsAt(LiveAtMI, MI);
1260 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1261
1262 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001263 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1264 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001265 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001266 .add(Op0)
1267 .add(Op1)
1268 .addReg(SrcHi)
1269 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001270 if (IsDestLive)
1271 T.addReg(Op0.getReg(), RegState::Implicit);
1272 IsDestLive = true;
1273 }
1274 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001275 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1276 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001277 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001278 .add(Op0)
1279 .add(Op1)
1280 .addReg(SrcHi)
1281 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001282 if (IsDestLive)
1283 T.addReg(Op0.getReg(), RegState::Implicit);
1284 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001285 MBB.erase(MI);
1286 return true;
1287 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001288 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001289 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001290 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001291 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001292 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001293 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001294 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001295 case Hexagon::PS_jmprett:
1296 MI.setDesc(get(Hexagon::J2_jumprt));
1297 return true;
1298 case Hexagon::PS_jmpretf:
1299 MI.setDesc(get(Hexagon::J2_jumprf));
1300 return true;
1301 case Hexagon::PS_jmprettnewpt:
1302 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1303 return true;
1304 case Hexagon::PS_jmpretfnewpt:
1305 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1306 return true;
1307 case Hexagon::PS_jmprettnew:
1308 MI.setDesc(get(Hexagon::J2_jumprtnew));
1309 return true;
1310 case Hexagon::PS_jmpretfnew:
1311 MI.setDesc(get(Hexagon::J2_jumprfnew));
1312 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001313 }
1314
1315 return false;
1316}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001317
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001318// We indicate that we want to reverse the branch by
1319// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001320bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001321 SmallVectorImpl<MachineOperand> &Cond) const {
1322 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001323 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001324 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1325 unsigned opcode = Cond[0].getImm();
1326 //unsigned temp;
1327 assert(get(opcode).isBranch() && "Should be a branching condition.");
1328 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001329 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001330 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1331 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001332 return false;
1333}
1334
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001335void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1336 MachineBasicBlock::iterator MI) const {
1337 DebugLoc DL;
1338 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1339}
1340
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001341bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1342 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001343}
1344
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001345// Returns true if an instruction is predicated irrespective of the predicate
1346// sense. For example, all of the following will return true.
1347// if (p0) R1 = add(R2, R3)
1348// if (!p0) R1 = add(R2, R3)
1349// if (p0.new) R1 = add(R2, R3)
1350// if (!p0.new) R1 = add(R2, R3)
1351// Note: New-value stores are not included here as in the current
1352// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001353bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1354 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001355 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001356}
1357
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001358bool HexagonInstrInfo::PredicateInstruction(
1359 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001360 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1361 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001362 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001363 return false;
1364 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001365 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001366 assert (isPredicable(MI) && "Expected predicable instruction");
1367 bool invertJump = predOpcodeHasNot(Cond);
1368
1369 // We have to predicate MI "in place", i.e. after this function returns,
1370 // MI will need to be transformed into a predicated form. To avoid com-
1371 // plicated manipulations with the operands (handling tied operands,
1372 // etc.), build a new temporary instruction, then overwrite MI with it.
1373
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001374 MachineBasicBlock &B = *MI.getParent();
1375 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001376 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1377 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001378 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001379 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001380 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001381 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1382 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001383 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001384 NOp++;
1385 }
1386
1387 unsigned PredReg, PredRegPos, PredRegFlags;
1388 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1389 (void)GotPredReg;
1390 assert(GotPredReg);
1391 T.addReg(PredReg, PredRegFlags);
1392 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001393 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001394
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001395 MI.setDesc(get(PredOpc));
1396 while (unsigned n = MI.getNumOperands())
1397 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001398 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001399 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001400
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001401 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001402 B.erase(TI);
1403
1404 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1405 MRI.clearKillFlags(PredReg);
1406 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001407}
1408
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001409bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1410 ArrayRef<MachineOperand> Pred2) const {
1411 // TODO: Fix this
1412 return false;
1413}
1414
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001415bool HexagonInstrInfo::DefinesPredicate(
1416 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001417 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001418 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1419 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001420 if (MO.isReg() && MO.isDef()) {
1421 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1422 if (RC == &Hexagon::PredRegsRegClass) {
1423 Pred.push_back(MO);
1424 return true;
1425 }
1426 }
1427 }
1428 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001429}
Andrew Trickd06df962012-02-01 22:13:57 +00001430
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001431bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001432 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001433}
1434
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001435bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1436 const MachineBasicBlock *MBB,
1437 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001438 // Debug info is never a scheduling boundary. It's necessary to be explicit
1439 // due to the special treatment of IT instructions below, otherwise a
1440 // dbg_value followed by an IT will result in the IT instruction being
1441 // considered a scheduling hazard, which is wrong. It should be the actual
1442 // instruction preceding the dbg_value instruction(s), just like it is
1443 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001444 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001445 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001446
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001447 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001448 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001449 // Don't mess around with no return calls.
1450 if (doesNotReturn(MI))
1451 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001452 // If any of the block's successors is a landing pad, this could be a
1453 // throwing call.
1454 for (auto I : MBB->successors())
1455 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001456 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001457 }
1458
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001459 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001460 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001461 return true;
1462
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001463 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1464 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001465
1466 return false;
1467}
1468
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001469/// Measure the specified inline asm to determine an approximation of its
1470/// length.
1471/// Comments (which run till the next SeparatorString or newline) do not
1472/// count as an instruction.
1473/// Any other non-whitespace text is considered an instruction, with
1474/// multiple instructions separated by SeparatorString or newlines.
1475/// Variable-length instructions are not handled here; this function
1476/// may be overloaded in the target code to do that.
1477/// Hexagon counts the number of ##'s and adjust for that many
1478/// constant exenders.
1479unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1480 const MCAsmInfo &MAI) const {
1481 StringRef AStr(Str);
1482 // Count the number of instructions in the asm.
1483 bool atInsnStart = true;
1484 unsigned Length = 0;
1485 for (; *Str; ++Str) {
1486 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1487 strlen(MAI.getSeparatorString())) == 0)
1488 atInsnStart = true;
1489 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1490 Length += MAI.getMaxInstLength();
1491 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001492 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001493 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1494 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001495 atInsnStart = false;
1496 }
1497
1498 // Add to size number of constant extenders seen * 4.
1499 StringRef Occ("##");
1500 Length += AStr.count(Occ)*4;
1501 return Length;
1502}
1503
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001504ScheduleHazardRecognizer*
1505HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1506 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001507 if (UseDFAHazardRec) {
1508 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1509 return new HexagonHazardRecognizer(II, this, HST);
1510 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001511 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1512}
1513
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001514/// \brief For a comparison instruction, return the source registers in
1515/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1516/// compares against in CmpValue. Return true if the comparison instruction
1517/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001518bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1519 unsigned &SrcReg2, int &Mask,
1520 int &Value) const {
1521 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001522
1523 // Set mask and the first source register.
1524 switch (Opc) {
1525 case Hexagon::C2_cmpeq:
1526 case Hexagon::C2_cmpeqp:
1527 case Hexagon::C2_cmpgt:
1528 case Hexagon::C2_cmpgtp:
1529 case Hexagon::C2_cmpgtu:
1530 case Hexagon::C2_cmpgtup:
1531 case Hexagon::C4_cmpneq:
1532 case Hexagon::C4_cmplte:
1533 case Hexagon::C4_cmplteu:
1534 case Hexagon::C2_cmpeqi:
1535 case Hexagon::C2_cmpgti:
1536 case Hexagon::C2_cmpgtui:
1537 case Hexagon::C4_cmpneqi:
1538 case Hexagon::C4_cmplteui:
1539 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001540 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001541 Mask = ~0;
1542 break;
1543 case Hexagon::A4_cmpbeq:
1544 case Hexagon::A4_cmpbgt:
1545 case Hexagon::A4_cmpbgtu:
1546 case Hexagon::A4_cmpbeqi:
1547 case Hexagon::A4_cmpbgti:
1548 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001549 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001550 Mask = 0xFF;
1551 break;
1552 case Hexagon::A4_cmpheq:
1553 case Hexagon::A4_cmphgt:
1554 case Hexagon::A4_cmphgtu:
1555 case Hexagon::A4_cmpheqi:
1556 case Hexagon::A4_cmphgti:
1557 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001558 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001559 Mask = 0xFFFF;
1560 break;
1561 }
1562
1563 // Set the value/second source register.
1564 switch (Opc) {
1565 case Hexagon::C2_cmpeq:
1566 case Hexagon::C2_cmpeqp:
1567 case Hexagon::C2_cmpgt:
1568 case Hexagon::C2_cmpgtp:
1569 case Hexagon::C2_cmpgtu:
1570 case Hexagon::C2_cmpgtup:
1571 case Hexagon::A4_cmpbeq:
1572 case Hexagon::A4_cmpbgt:
1573 case Hexagon::A4_cmpbgtu:
1574 case Hexagon::A4_cmpheq:
1575 case Hexagon::A4_cmphgt:
1576 case Hexagon::A4_cmphgtu:
1577 case Hexagon::C4_cmpneq:
1578 case Hexagon::C4_cmplte:
1579 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001580 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001581 return true;
1582
1583 case Hexagon::C2_cmpeqi:
1584 case Hexagon::C2_cmpgtui:
1585 case Hexagon::C2_cmpgti:
1586 case Hexagon::C4_cmpneqi:
1587 case Hexagon::C4_cmplteui:
1588 case Hexagon::C4_cmpltei:
1589 case Hexagon::A4_cmpbeqi:
1590 case Hexagon::A4_cmpbgti:
1591 case Hexagon::A4_cmpbgtui:
1592 case Hexagon::A4_cmpheqi:
1593 case Hexagon::A4_cmphgti:
1594 case Hexagon::A4_cmphgtui:
1595 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001596 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001597 return true;
1598 }
1599
1600 return false;
1601}
1602
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001603unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001604 const MachineInstr &MI,
1605 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001606 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001607}
1608
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001609DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1610 const TargetSubtargetInfo &STI) const {
1611 const InstrItineraryData *II = STI.getInstrItineraryData();
1612 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1613}
1614
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001615// Inspired by this pair:
1616// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1617// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1618// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001619bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1620 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001621 int OffsetA = 0, OffsetB = 0;
1622 unsigned SizeA = 0, SizeB = 0;
1623
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001624 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1625 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001626 return false;
1627
1628 // Instructions that are pure loads, not loads and stores like memops are not
1629 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001630 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001631 return true;
1632
1633 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001634 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001635 if (!BaseRegA || !SizeA)
1636 return false;
1637
1638 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001639 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001640 if (!BaseRegB || !SizeB)
1641 return false;
1642
1643 if (BaseRegA != BaseRegB)
1644 return false;
1645
1646 // This is a mem access with the same base register and known offsets from it.
1647 // Reason about it.
1648 if (OffsetA > OffsetB) {
1649 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1650 return (SizeB <= offDiff);
1651 } else if (OffsetA < OffsetB) {
1652 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1653 return (SizeA <= offDiff);
1654 }
1655
1656 return false;
1657}
1658
Brendon Cahoon254f8892016-07-29 16:44:44 +00001659/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001660bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001661 int &Value) const {
1662 if (isPostIncrement(MI)) {
1663 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001664 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001665 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001666 if (MI.getOpcode() == Hexagon::A2_addi) {
1667 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001668 return true;
1669 }
1670
1671 return false;
1672}
1673
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001674unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001675 MachineRegisterInfo &MRI = MF->getRegInfo();
1676 const TargetRegisterClass *TRC;
1677 if (VT == MVT::i1) {
1678 TRC = &Hexagon::PredRegsRegClass;
1679 } else if (VT == MVT::i32 || VT == MVT::f32) {
1680 TRC = &Hexagon::IntRegsRegClass;
1681 } else if (VT == MVT::i64 || VT == MVT::f64) {
1682 TRC = &Hexagon::DoubleRegsRegClass;
1683 } else {
1684 llvm_unreachable("Cannot handle this register class");
1685 }
1686
1687 unsigned NewReg = MRI.createVirtualRegister(TRC);
1688 return NewReg;
1689}
1690
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001691bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001692 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1693}
1694
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001695bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1696 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001697 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1698}
1699
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001700bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1701 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001702 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1703 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1704
1705 if (!(isTC1(MI))
1706 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001707 && !(MI.getDesc().mayLoad())
1708 && !(MI.getDesc().mayStore())
1709 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1710 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001711 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001712 && !(MI.isBranch())
1713 && !(MI.isReturn())
1714 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001715 return true;
1716
1717 return false;
1718}
1719
Sanjay Patele4b9f502015-12-07 19:21:39 +00001720// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001721bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001722 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001723}
1724
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001725bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1726 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001727 isConditionalTransfer(MI) ||
1728 isConditionalALU32(MI) ||
1729 isConditionalLoad(MI) ||
1730 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001731 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1732 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001733}
1734
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001735bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1736 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001737 case Hexagon::A2_paddf:
1738 case Hexagon::A2_paddfnew:
1739 case Hexagon::A2_paddif:
1740 case Hexagon::A2_paddifnew:
1741 case Hexagon::A2_paddit:
1742 case Hexagon::A2_padditnew:
1743 case Hexagon::A2_paddt:
1744 case Hexagon::A2_paddtnew:
1745 case Hexagon::A2_pandf:
1746 case Hexagon::A2_pandfnew:
1747 case Hexagon::A2_pandt:
1748 case Hexagon::A2_pandtnew:
1749 case Hexagon::A2_porf:
1750 case Hexagon::A2_porfnew:
1751 case Hexagon::A2_port:
1752 case Hexagon::A2_portnew:
1753 case Hexagon::A2_psubf:
1754 case Hexagon::A2_psubfnew:
1755 case Hexagon::A2_psubt:
1756 case Hexagon::A2_psubtnew:
1757 case Hexagon::A2_pxorf:
1758 case Hexagon::A2_pxorfnew:
1759 case Hexagon::A2_pxort:
1760 case Hexagon::A2_pxortnew:
1761 case Hexagon::A4_paslhf:
1762 case Hexagon::A4_paslhfnew:
1763 case Hexagon::A4_paslht:
1764 case Hexagon::A4_paslhtnew:
1765 case Hexagon::A4_pasrhf:
1766 case Hexagon::A4_pasrhfnew:
1767 case Hexagon::A4_pasrht:
1768 case Hexagon::A4_pasrhtnew:
1769 case Hexagon::A4_psxtbf:
1770 case Hexagon::A4_psxtbfnew:
1771 case Hexagon::A4_psxtbt:
1772 case Hexagon::A4_psxtbtnew:
1773 case Hexagon::A4_psxthf:
1774 case Hexagon::A4_psxthfnew:
1775 case Hexagon::A4_psxtht:
1776 case Hexagon::A4_psxthtnew:
1777 case Hexagon::A4_pzxtbf:
1778 case Hexagon::A4_pzxtbfnew:
1779 case Hexagon::A4_pzxtbt:
1780 case Hexagon::A4_pzxtbtnew:
1781 case Hexagon::A4_pzxthf:
1782 case Hexagon::A4_pzxthfnew:
1783 case Hexagon::A4_pzxtht:
1784 case Hexagon::A4_pzxthtnew:
1785 case Hexagon::C2_ccombinewf:
1786 case Hexagon::C2_ccombinewt:
1787 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001788 }
1789 return false;
1790}
1791
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001792// FIXME - Function name and it's functionality don't match.
1793// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001794bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1795 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001796 return false;
1797
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001798 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001799 // Instruction with valid predicated-new opcode can be promoted to .new.
1800 return PNewOpcode >= 0;
1801}
1802
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001803// Returns true if an instruction is a conditional store.
1804//
1805// Note: It doesn't include conditional new-value stores as they can't be
1806// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001807bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1808 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001809 default: return false;
1810 case Hexagon::S4_storeirbt_io:
1811 case Hexagon::S4_storeirbf_io:
1812 case Hexagon::S4_pstorerbt_rr:
1813 case Hexagon::S4_pstorerbf_rr:
1814 case Hexagon::S2_pstorerbt_io:
1815 case Hexagon::S2_pstorerbf_io:
1816 case Hexagon::S2_pstorerbt_pi:
1817 case Hexagon::S2_pstorerbf_pi:
1818 case Hexagon::S2_pstorerdt_io:
1819 case Hexagon::S2_pstorerdf_io:
1820 case Hexagon::S4_pstorerdt_rr:
1821 case Hexagon::S4_pstorerdf_rr:
1822 case Hexagon::S2_pstorerdt_pi:
1823 case Hexagon::S2_pstorerdf_pi:
1824 case Hexagon::S2_pstorerht_io:
1825 case Hexagon::S2_pstorerhf_io:
1826 case Hexagon::S4_storeirht_io:
1827 case Hexagon::S4_storeirhf_io:
1828 case Hexagon::S4_pstorerht_rr:
1829 case Hexagon::S4_pstorerhf_rr:
1830 case Hexagon::S2_pstorerht_pi:
1831 case Hexagon::S2_pstorerhf_pi:
1832 case Hexagon::S2_pstorerit_io:
1833 case Hexagon::S2_pstorerif_io:
1834 case Hexagon::S4_storeirit_io:
1835 case Hexagon::S4_storeirif_io:
1836 case Hexagon::S4_pstorerit_rr:
1837 case Hexagon::S4_pstorerif_rr:
1838 case Hexagon::S2_pstorerit_pi:
1839 case Hexagon::S2_pstorerif_pi:
1840
1841 // V4 global address store before promoting to dot new.
1842 case Hexagon::S4_pstorerdt_abs:
1843 case Hexagon::S4_pstorerdf_abs:
1844 case Hexagon::S4_pstorerbt_abs:
1845 case Hexagon::S4_pstorerbf_abs:
1846 case Hexagon::S4_pstorerht_abs:
1847 case Hexagon::S4_pstorerhf_abs:
1848 case Hexagon::S4_pstorerit_abs:
1849 case Hexagon::S4_pstorerif_abs:
1850 return true;
1851
1852 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1853 // from the "Conditional Store" list. Because a predicated new value store
1854 // would NOT be promoted to a double dot new store.
1855 // This function returns yes for those stores that are predicated but not
1856 // yet promoted to predicate dot new instructions.
1857 }
1858}
1859
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001860bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1861 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001862 case Hexagon::A2_tfrt:
1863 case Hexagon::A2_tfrf:
1864 case Hexagon::C2_cmoveit:
1865 case Hexagon::C2_cmoveif:
1866 case Hexagon::A2_tfrtnew:
1867 case Hexagon::A2_tfrfnew:
1868 case Hexagon::C2_cmovenewit:
1869 case Hexagon::C2_cmovenewif:
1870 case Hexagon::A2_tfrpt:
1871 case Hexagon::A2_tfrpf:
1872 return true;
1873
1874 default:
1875 return false;
1876 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001877 return false;
1878}
1879
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001880// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1881// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001882bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1883 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001884 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1885 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001886 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001887
1888 unsigned isExtendable =
1889 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1890 if (!isExtendable)
1891 return false;
1892
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001893 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001894 return false;
1895
1896 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001897 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001898 // Use MO operand flags to determine if MO
1899 // has the HMOTF_ConstExtended flag set.
1900 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001901 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001902 // If this is a Machine BB address we are talking about, and it is
1903 // not marked as extended, say so.
1904 if (MO.isMBB())
1905 return false;
1906
1907 // We could be using an instruction with an extendable immediate and shoehorn
1908 // a global address into it. If it is a global address it will be constant
1909 // extended. We do this for COMBINE.
1910 // We currently only handle isGlobal() because it is the only kind of
1911 // object we are going to end up with here for now.
1912 // In the future we probably should add isSymbol(), etc.
1913 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001914 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001915 return true;
1916
1917 // If the extendable operand is not 'Immediate' type, the instruction should
1918 // have 'isExtended' flag set.
1919 assert(MO.isImm() && "Extendable operand must be Immediate type");
1920
1921 int MinValue = getMinValue(MI);
1922 int MaxValue = getMaxValue(MI);
1923 int ImmValue = MO.getImm();
1924
1925 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001926}
1927
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001928bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1929 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001930 case Hexagon::L4_return :
1931 case Hexagon::L4_return_t :
1932 case Hexagon::L4_return_f :
1933 case Hexagon::L4_return_tnew_pnt :
1934 case Hexagon::L4_return_fnew_pnt :
1935 case Hexagon::L4_return_tnew_pt :
1936 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001937 return true;
1938 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001939 return false;
1940}
1941
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001942// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001943bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1944 const MachineInstr &ConsMI) const {
1945 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001946 return false;
1947
1948 auto &HRI = getRegisterInfo();
1949
1950 SmallVector<unsigned, 4> DefsA;
1951 SmallVector<unsigned, 4> DefsB;
1952 SmallVector<unsigned, 8> UsesA;
1953 SmallVector<unsigned, 8> UsesB;
1954
1955 parseOperands(ProdMI, DefsA, UsesA);
1956 parseOperands(ConsMI, DefsB, UsesB);
1957
1958 for (auto &RegA : DefsA)
1959 for (auto &RegB : UsesB) {
1960 // True data dependency.
1961 if (RegA == RegB)
1962 return true;
1963
1964 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1965 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1966 if (RegB == *SubRegs)
1967 return true;
1968
1969 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1970 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1971 if (RegA == *SubRegs)
1972 return true;
1973 }
1974
1975 return false;
1976}
1977
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001978// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001979bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1980 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001981 case Hexagon::V6_vL32b_cur_pi:
1982 case Hexagon::V6_vL32b_cur_ai:
1983 case Hexagon::V6_vL32b_cur_pi_128B:
1984 case Hexagon::V6_vL32b_cur_ai_128B:
1985 return true;
1986 }
1987 return false;
1988}
1989
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001990// Returns true, if any one of the operands is a dot new
1991// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001992bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
1993 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001994 return true;
1995
1996 return false;
1997}
1998
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001999/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002000bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2001 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002002 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2003 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2004 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2005}
2006
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002007bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2008 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002009 return true;
2010
2011 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002012 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002013 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2014 return true;
2015 return false;
2016}
2017
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002018bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2019 return (Opcode == Hexagon::ENDLOOP0 ||
2020 Opcode == Hexagon::ENDLOOP1);
2021}
2022
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002023bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2024 switch(OpType) {
2025 case MachineOperand::MO_MachineBasicBlock:
2026 case MachineOperand::MO_GlobalAddress:
2027 case MachineOperand::MO_ExternalSymbol:
2028 case MachineOperand::MO_JumpTableIndex:
2029 case MachineOperand::MO_ConstantPoolIndex:
2030 case MachineOperand::MO_BlockAddress:
2031 return true;
2032 default:
2033 return false;
2034 }
2035}
2036
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002037bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2038 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002039 const uint64_t F = MID.TSFlags;
2040 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2041 return true;
2042
2043 // TODO: This is largely obsolete now. Will need to be removed
2044 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002045 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002046 // PS_fi and PS_fia remain special cases.
2047 case Hexagon::PS_fi:
2048 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002049 return true;
2050 default:
2051 return false;
2052 }
2053 return false;
2054}
2055
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002056// This returns true in two cases:
2057// - The OP code itself indicates that this is an extended instruction.
2058// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002059bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002060 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002061 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002062 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2063 return true;
2064 // Use MO operand flags to determine if one of MI's operands
2065 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002066 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
2067 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002068 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2069 return true;
2070 }
2071 return false;
2072}
2073
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002074bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2075 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002076 const uint64_t F = get(Opcode).TSFlags;
2077 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2078}
2079
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002080// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002081bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2082 const MachineInstr &J) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002083 if (!isV60VectorInstruction(I))
2084 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002085 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002086 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002087 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002088}
2089
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002090bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2091 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002092 case Hexagon::J2_callr :
2093 case Hexagon::J2_callrf :
2094 case Hexagon::J2_callrt :
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00002095 case Hexagon::PS_call_nr :
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002096 return true;
2097 }
2098 return false;
2099}
2100
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002101bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2102 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002103 case Hexagon::L4_return :
2104 case Hexagon::L4_return_t :
2105 case Hexagon::L4_return_f :
2106 case Hexagon::L4_return_fnew_pnt :
2107 case Hexagon::L4_return_fnew_pt :
2108 case Hexagon::L4_return_tnew_pnt :
2109 case Hexagon::L4_return_tnew_pt :
2110 return true;
2111 }
2112 return false;
2113}
2114
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002115bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2116 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002117 case Hexagon::J2_jumpr :
2118 case Hexagon::J2_jumprt :
2119 case Hexagon::J2_jumprf :
2120 case Hexagon::J2_jumprtnewpt :
2121 case Hexagon::J2_jumprfnewpt :
2122 case Hexagon::J2_jumprtnew :
2123 case Hexagon::J2_jumprfnew :
2124 return true;
2125 }
2126 return false;
2127}
2128
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002129// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002130// Use abs estimate as oppose to the exact number.
2131// TODO: This will need to be changed to use MC level
2132// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002133bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002134 unsigned offset) const {
2135 // This selection of jump instructions matches to that what
2136 // AnalyzeBranch can parse, plus NVJ.
2137 if (isNewValueJump(MI)) // r9:2
2138 return isInt<11>(offset);
2139
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002140 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002141 // Still missing Jump to address condition on register value.
2142 default:
2143 return false;
2144 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2145 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002146 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002147 return isInt<24>(offset);
2148 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2149 case Hexagon::J2_jumpf:
2150 case Hexagon::J2_jumptnew:
2151 case Hexagon::J2_jumptnewpt:
2152 case Hexagon::J2_jumpfnew:
2153 case Hexagon::J2_jumpfnewpt:
2154 case Hexagon::J2_callt:
2155 case Hexagon::J2_callf:
2156 return isInt<17>(offset);
2157 case Hexagon::J2_loop0i:
2158 case Hexagon::J2_loop0iext:
2159 case Hexagon::J2_loop0r:
2160 case Hexagon::J2_loop0rext:
2161 case Hexagon::J2_loop1i:
2162 case Hexagon::J2_loop1iext:
2163 case Hexagon::J2_loop1r:
2164 case Hexagon::J2_loop1rext:
2165 return isInt<9>(offset);
2166 // TODO: Add all the compound branches here. Can we do this in Relation model?
2167 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2168 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2169 return isInt<11>(offset);
2170 }
2171}
2172
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002173bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2174 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002175 bool isLate = isLateResultInstr(LRMI);
2176 bool isEarly = isEarlySourceInstr(ESMI);
2177
2178 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002179 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002180 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002181 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002182
2183 if (isLate && isEarly) {
2184 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2185 return true;
2186 }
2187
2188 return false;
2189}
2190
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002191bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2192 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002193 case TargetOpcode::EXTRACT_SUBREG:
2194 case TargetOpcode::INSERT_SUBREG:
2195 case TargetOpcode::SUBREG_TO_REG:
2196 case TargetOpcode::REG_SEQUENCE:
2197 case TargetOpcode::IMPLICIT_DEF:
2198 case TargetOpcode::COPY:
2199 case TargetOpcode::INLINEASM:
2200 case TargetOpcode::PHI:
2201 return false;
2202 default:
2203 break;
2204 }
2205
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002206 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002207
2208 switch (SchedClass) {
2209 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2210 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2211 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2212 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2213 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2214 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2215 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2216 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2217 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2218 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2219 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2220 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2221 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2222 return false;
2223 }
2224 return true;
2225}
2226
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002227bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002228 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2229 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002230 return MI.getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002231}
2232
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002233bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2234 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002235 return Opcode == Hexagon::J2_loop0i ||
2236 Opcode == Hexagon::J2_loop0r ||
2237 Opcode == Hexagon::J2_loop0iext ||
2238 Opcode == Hexagon::J2_loop0rext ||
2239 Opcode == Hexagon::J2_loop1i ||
2240 Opcode == Hexagon::J2_loop1r ||
2241 Opcode == Hexagon::J2_loop1iext ||
2242 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002243}
2244
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002245bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2246 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002247 default: return false;
2248 case Hexagon::L4_iadd_memopw_io :
2249 case Hexagon::L4_isub_memopw_io :
2250 case Hexagon::L4_add_memopw_io :
2251 case Hexagon::L4_sub_memopw_io :
2252 case Hexagon::L4_and_memopw_io :
2253 case Hexagon::L4_or_memopw_io :
2254 case Hexagon::L4_iadd_memoph_io :
2255 case Hexagon::L4_isub_memoph_io :
2256 case Hexagon::L4_add_memoph_io :
2257 case Hexagon::L4_sub_memoph_io :
2258 case Hexagon::L4_and_memoph_io :
2259 case Hexagon::L4_or_memoph_io :
2260 case Hexagon::L4_iadd_memopb_io :
2261 case Hexagon::L4_isub_memopb_io :
2262 case Hexagon::L4_add_memopb_io :
2263 case Hexagon::L4_sub_memopb_io :
2264 case Hexagon::L4_and_memopb_io :
2265 case Hexagon::L4_or_memopb_io :
2266 case Hexagon::L4_ior_memopb_io:
2267 case Hexagon::L4_ior_memoph_io:
2268 case Hexagon::L4_ior_memopw_io:
2269 case Hexagon::L4_iand_memopb_io:
2270 case Hexagon::L4_iand_memoph_io:
2271 case Hexagon::L4_iand_memopw_io:
2272 return true;
2273 }
2274 return false;
2275}
2276
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002277bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2278 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002279 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2280}
2281
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002282bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2283 const uint64_t F = get(Opcode).TSFlags;
2284 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2285}
2286
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002287bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002288 return isNewValueJump(MI) || isNewValueStore(MI);
2289}
2290
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002291bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2292 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002293}
2294
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002295bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2296 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2297}
2298
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002299bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2300 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002301 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2302}
2303
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002304bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2305 const uint64_t F = get(Opcode).TSFlags;
2306 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2307}
2308
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002309// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002310bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002311 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002312 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002313 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2314 == OperandNum;
2315}
2316
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002317bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2318 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002319 assert(isPredicated(MI));
2320 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2321}
2322
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002323bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2324 const uint64_t F = get(Opcode).TSFlags;
2325 assert(isPredicated(Opcode));
2326 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2327}
2328
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002329bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2330 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002331 return !((F >> HexagonII::PredicatedFalsePos) &
2332 HexagonII::PredicatedFalseMask);
2333}
2334
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002335bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2336 const uint64_t F = get(Opcode).TSFlags;
2337 // Make sure that the instruction is predicated.
2338 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2339 return !((F >> HexagonII::PredicatedFalsePos) &
2340 HexagonII::PredicatedFalseMask);
2341}
2342
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002343bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2344 const uint64_t F = get(Opcode).TSFlags;
2345 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2346}
2347
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002348bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2349 const uint64_t F = get(Opcode).TSFlags;
2350 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2351}
2352
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002353bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2354 const uint64_t F = get(Opcode).TSFlags;
2355 assert(get(Opcode).isBranch() &&
2356 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2357 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2358}
2359
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002360bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2361 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2362 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2363 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2364 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002365}
2366
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002367bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2368 switch (MI.getOpcode()) {
2369 // Byte
2370 case Hexagon::L2_loadrb_io:
2371 case Hexagon::L4_loadrb_ur:
2372 case Hexagon::L4_loadrb_ap:
2373 case Hexagon::L2_loadrb_pr:
2374 case Hexagon::L2_loadrb_pbr:
2375 case Hexagon::L2_loadrb_pi:
2376 case Hexagon::L2_loadrb_pci:
2377 case Hexagon::L2_loadrb_pcr:
2378 case Hexagon::L2_loadbsw2_io:
2379 case Hexagon::L4_loadbsw2_ur:
2380 case Hexagon::L4_loadbsw2_ap:
2381 case Hexagon::L2_loadbsw2_pr:
2382 case Hexagon::L2_loadbsw2_pbr:
2383 case Hexagon::L2_loadbsw2_pi:
2384 case Hexagon::L2_loadbsw2_pci:
2385 case Hexagon::L2_loadbsw2_pcr:
2386 case Hexagon::L2_loadbsw4_io:
2387 case Hexagon::L4_loadbsw4_ur:
2388 case Hexagon::L4_loadbsw4_ap:
2389 case Hexagon::L2_loadbsw4_pr:
2390 case Hexagon::L2_loadbsw4_pbr:
2391 case Hexagon::L2_loadbsw4_pi:
2392 case Hexagon::L2_loadbsw4_pci:
2393 case Hexagon::L2_loadbsw4_pcr:
2394 case Hexagon::L4_loadrb_rr:
2395 case Hexagon::L2_ploadrbt_io:
2396 case Hexagon::L2_ploadrbt_pi:
2397 case Hexagon::L2_ploadrbf_io:
2398 case Hexagon::L2_ploadrbf_pi:
2399 case Hexagon::L2_ploadrbtnew_io:
2400 case Hexagon::L2_ploadrbfnew_io:
2401 case Hexagon::L4_ploadrbt_rr:
2402 case Hexagon::L4_ploadrbf_rr:
2403 case Hexagon::L4_ploadrbtnew_rr:
2404 case Hexagon::L4_ploadrbfnew_rr:
2405 case Hexagon::L2_ploadrbtnew_pi:
2406 case Hexagon::L2_ploadrbfnew_pi:
2407 case Hexagon::L4_ploadrbt_abs:
2408 case Hexagon::L4_ploadrbf_abs:
2409 case Hexagon::L4_ploadrbtnew_abs:
2410 case Hexagon::L4_ploadrbfnew_abs:
2411 case Hexagon::L2_loadrbgp:
2412 // Half
2413 case Hexagon::L2_loadrh_io:
2414 case Hexagon::L4_loadrh_ur:
2415 case Hexagon::L4_loadrh_ap:
2416 case Hexagon::L2_loadrh_pr:
2417 case Hexagon::L2_loadrh_pbr:
2418 case Hexagon::L2_loadrh_pi:
2419 case Hexagon::L2_loadrh_pci:
2420 case Hexagon::L2_loadrh_pcr:
2421 case Hexagon::L4_loadrh_rr:
2422 case Hexagon::L2_ploadrht_io:
2423 case Hexagon::L2_ploadrht_pi:
2424 case Hexagon::L2_ploadrhf_io:
2425 case Hexagon::L2_ploadrhf_pi:
2426 case Hexagon::L2_ploadrhtnew_io:
2427 case Hexagon::L2_ploadrhfnew_io:
2428 case Hexagon::L4_ploadrht_rr:
2429 case Hexagon::L4_ploadrhf_rr:
2430 case Hexagon::L4_ploadrhtnew_rr:
2431 case Hexagon::L4_ploadrhfnew_rr:
2432 case Hexagon::L2_ploadrhtnew_pi:
2433 case Hexagon::L2_ploadrhfnew_pi:
2434 case Hexagon::L4_ploadrht_abs:
2435 case Hexagon::L4_ploadrhf_abs:
2436 case Hexagon::L4_ploadrhtnew_abs:
2437 case Hexagon::L4_ploadrhfnew_abs:
2438 case Hexagon::L2_loadrhgp:
2439 return true;
2440 default:
2441 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002442 }
2443}
2444
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002445bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2446 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002447 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2448}
2449
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002450bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2451 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002452 case Hexagon::STriw_pred :
2453 case Hexagon::LDriw_pred :
2454 return true;
2455 default:
2456 return false;
2457 }
2458}
2459
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002460bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2461 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002462 return false;
2463
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002464 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002465 if (Op.isGlobal() || Op.isSymbol())
2466 return true;
2467 return false;
2468}
2469
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002470// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002471bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2472 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002473 switch (SchedClass) {
2474 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2475 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2476 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2477 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2478 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2479 //case Hexagon::Sched::M_tc_1_SLOT23:
2480 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2481 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2482 return true;
2483
2484 default:
2485 return false;
2486 }
2487}
2488
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002489bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2490 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002491 switch (SchedClass) {
2492 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2493 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2494 case Hexagon::Sched::CR_tc_2_SLOT3:
2495 case Hexagon::Sched::M_tc_2_SLOT23:
2496 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2497 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2498 return true;
2499
2500 default:
2501 return false;
2502 }
2503}
2504
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002505bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2506 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002507 switch (SchedClass) {
2508 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2509 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2510 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2511 case Hexagon::Sched::CR_tc_2early_SLOT23:
2512 case Hexagon::Sched::CR_tc_2early_SLOT3:
2513 case Hexagon::Sched::J_tc_2early_SLOT0123:
2514 case Hexagon::Sched::J_tc_2early_SLOT2:
2515 case Hexagon::Sched::J_tc_2early_SLOT23:
2516 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2517 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2518 return true;
2519
2520 default:
2521 return false;
2522 }
2523}
2524
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002525bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2526 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002527 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2528}
2529
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002530// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002531bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2532 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002533 if (mayBeCurLoad(MI1)) {
2534 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002535 unsigned DstReg = MI1.getOperand(0).getReg();
2536 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002537 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002538 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002539 return true;
2540 }
2541 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002542 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2543 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2544 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002545 return true;
2546 return false;
2547}
2548
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002549bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002550 const uint64_t V = getType(MI);
2551 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2552}
2553
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002554// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2555//
2556bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2557 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2558 VT == MVT::v32i16 || VT == MVT::v64i8) {
2559 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2560 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2561 (Offset & 0x3f) == 0);
2562 }
2563 // 128B
2564 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2565 VT == MVT::v64i16 || VT == MVT::v128i8) {
2566 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2567 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2568 (Offset & 0x7f) == 0);
2569 }
2570 if (VT == MVT::i64) {
2571 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2572 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2573 (Offset & 0x7) == 0);
2574 }
2575 if (VT == MVT::i32) {
2576 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2577 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2578 (Offset & 0x3) == 0);
2579 }
2580 if (VT == MVT::i16) {
2581 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2582 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2583 (Offset & 0x1) == 0);
2584 }
2585 if (VT == MVT::i8) {
2586 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2587 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2588 }
2589 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002590}
2591
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002592bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2593 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002594 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002595 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002596 // inserted to calculate the final address. Due to this reason, the function
2597 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002598 // We used to assert if the offset was not properly aligned, however,
2599 // there are cases where a misaligned pointer recast can cause this
2600 // problem, and we need to allow for it. The front end warns of such
2601 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002602
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002603 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002604 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002605 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002606 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002607 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002608 case Hexagon::V6_vL32b_ai:
2609 case Hexagon::V6_vS32b_ai:
2610 case Hexagon::V6_vL32Ub_ai:
2611 case Hexagon::V6_vS32Ub_ai:
2612 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2613 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2614
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002615 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002616 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002617 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002618 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002619 case Hexagon::V6_vL32b_ai_128B:
2620 case Hexagon::V6_vS32b_ai_128B:
2621 case Hexagon::V6_vL32Ub_ai_128B:
2622 case Hexagon::V6_vS32Ub_ai_128B:
2623 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2624 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2625
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002626 case Hexagon::J2_loop0i:
2627 case Hexagon::J2_loop1i:
2628 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002629
2630 case Hexagon::S4_storeirb_io:
2631 case Hexagon::S4_storeirbt_io:
2632 case Hexagon::S4_storeirbf_io:
2633 return isUInt<6>(Offset);
2634
2635 case Hexagon::S4_storeirh_io:
2636 case Hexagon::S4_storeirht_io:
2637 case Hexagon::S4_storeirhf_io:
2638 return isShiftedUInt<6,1>(Offset);
2639
2640 case Hexagon::S4_storeiri_io:
2641 case Hexagon::S4_storeirit_io:
2642 case Hexagon::S4_storeirif_io:
2643 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002644 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002645
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002646 if (Extend)
2647 return true;
2648
2649 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002650 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002651 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002652 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2653 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2654
Colin LeMahieu947cd702014-12-23 20:44:59 +00002655 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002656 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002657 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2658 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2659
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002660 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002661 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002662 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002663 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2664 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2665
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002666 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002667 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002668 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002669 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2670 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2671
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002672 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002673 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2674 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2675
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002676 case Hexagon::L4_iadd_memopw_io :
2677 case Hexagon::L4_isub_memopw_io :
2678 case Hexagon::L4_add_memopw_io :
2679 case Hexagon::L4_sub_memopw_io :
2680 case Hexagon::L4_and_memopw_io :
2681 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002682 return (0 <= Offset && Offset <= 255);
2683
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002684 case Hexagon::L4_iadd_memoph_io :
2685 case Hexagon::L4_isub_memoph_io :
2686 case Hexagon::L4_add_memoph_io :
2687 case Hexagon::L4_sub_memoph_io :
2688 case Hexagon::L4_and_memoph_io :
2689 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002690 return (0 <= Offset && Offset <= 127);
2691
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002692 case Hexagon::L4_iadd_memopb_io :
2693 case Hexagon::L4_isub_memopb_io :
2694 case Hexagon::L4_add_memopb_io :
2695 case Hexagon::L4_sub_memopb_io :
2696 case Hexagon::L4_and_memopb_io :
2697 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002698 return (0 <= Offset && Offset <= 63);
2699
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002700 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002701 // any size. Later pass knows how to handle it.
2702 case Hexagon::STriw_pred:
2703 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002704 case Hexagon::STriw_mod:
2705 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002706 return true;
2707
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002708 case Hexagon::PS_fi:
2709 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002710 case Hexagon::INLINEASM:
2711 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002712
2713 case Hexagon::L2_ploadrbt_io:
2714 case Hexagon::L2_ploadrbf_io:
2715 case Hexagon::L2_ploadrubt_io:
2716 case Hexagon::L2_ploadrubf_io:
2717 case Hexagon::S2_pstorerbt_io:
2718 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002719 return isUInt<6>(Offset);
2720
2721 case Hexagon::L2_ploadrht_io:
2722 case Hexagon::L2_ploadrhf_io:
2723 case Hexagon::L2_ploadruht_io:
2724 case Hexagon::L2_ploadruhf_io:
2725 case Hexagon::S2_pstorerht_io:
2726 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002727 return isShiftedUInt<6,1>(Offset);
2728
2729 case Hexagon::L2_ploadrit_io:
2730 case Hexagon::L2_ploadrif_io:
2731 case Hexagon::S2_pstorerit_io:
2732 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002733 return isShiftedUInt<6,2>(Offset);
2734
2735 case Hexagon::L2_ploadrdt_io:
2736 case Hexagon::L2_ploadrdf_io:
2737 case Hexagon::S2_pstorerdt_io:
2738 case Hexagon::S2_pstorerdf_io:
2739 return isShiftedUInt<6,3>(Offset);
2740 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002741
Benjamin Kramerb6684012011-12-27 11:41:05 +00002742 llvm_unreachable("No offset range is defined for this opcode. "
2743 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002744}
2745
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002746bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2747 return isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002748}
2749
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002750bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2751 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002752 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2753 return
2754 V == HexagonII::TypeCVI_VA ||
2755 V == HexagonII::TypeCVI_VA_DV;
2756}
Andrew Trickd06df962012-02-01 22:13:57 +00002757
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002758bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2759 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002760 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2761 return true;
2762
2763 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2764 return true;
2765
2766 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002767 return true;
2768
2769 return false;
2770}
Jyotsna Verma84256432013-03-01 17:37:13 +00002771
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002772bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2773 switch (MI.getOpcode()) {
2774 // Byte
2775 case Hexagon::L2_loadrub_io:
2776 case Hexagon::L4_loadrub_ur:
2777 case Hexagon::L4_loadrub_ap:
2778 case Hexagon::L2_loadrub_pr:
2779 case Hexagon::L2_loadrub_pbr:
2780 case Hexagon::L2_loadrub_pi:
2781 case Hexagon::L2_loadrub_pci:
2782 case Hexagon::L2_loadrub_pcr:
2783 case Hexagon::L2_loadbzw2_io:
2784 case Hexagon::L4_loadbzw2_ur:
2785 case Hexagon::L4_loadbzw2_ap:
2786 case Hexagon::L2_loadbzw2_pr:
2787 case Hexagon::L2_loadbzw2_pbr:
2788 case Hexagon::L2_loadbzw2_pi:
2789 case Hexagon::L2_loadbzw2_pci:
2790 case Hexagon::L2_loadbzw2_pcr:
2791 case Hexagon::L2_loadbzw4_io:
2792 case Hexagon::L4_loadbzw4_ur:
2793 case Hexagon::L4_loadbzw4_ap:
2794 case Hexagon::L2_loadbzw4_pr:
2795 case Hexagon::L2_loadbzw4_pbr:
2796 case Hexagon::L2_loadbzw4_pi:
2797 case Hexagon::L2_loadbzw4_pci:
2798 case Hexagon::L2_loadbzw4_pcr:
2799 case Hexagon::L4_loadrub_rr:
2800 case Hexagon::L2_ploadrubt_io:
2801 case Hexagon::L2_ploadrubt_pi:
2802 case Hexagon::L2_ploadrubf_io:
2803 case Hexagon::L2_ploadrubf_pi:
2804 case Hexagon::L2_ploadrubtnew_io:
2805 case Hexagon::L2_ploadrubfnew_io:
2806 case Hexagon::L4_ploadrubt_rr:
2807 case Hexagon::L4_ploadrubf_rr:
2808 case Hexagon::L4_ploadrubtnew_rr:
2809 case Hexagon::L4_ploadrubfnew_rr:
2810 case Hexagon::L2_ploadrubtnew_pi:
2811 case Hexagon::L2_ploadrubfnew_pi:
2812 case Hexagon::L4_ploadrubt_abs:
2813 case Hexagon::L4_ploadrubf_abs:
2814 case Hexagon::L4_ploadrubtnew_abs:
2815 case Hexagon::L4_ploadrubfnew_abs:
2816 case Hexagon::L2_loadrubgp:
2817 // Half
2818 case Hexagon::L2_loadruh_io:
2819 case Hexagon::L4_loadruh_ur:
2820 case Hexagon::L4_loadruh_ap:
2821 case Hexagon::L2_loadruh_pr:
2822 case Hexagon::L2_loadruh_pbr:
2823 case Hexagon::L2_loadruh_pi:
2824 case Hexagon::L2_loadruh_pci:
2825 case Hexagon::L2_loadruh_pcr:
2826 case Hexagon::L4_loadruh_rr:
2827 case Hexagon::L2_ploadruht_io:
2828 case Hexagon::L2_ploadruht_pi:
2829 case Hexagon::L2_ploadruhf_io:
2830 case Hexagon::L2_ploadruhf_pi:
2831 case Hexagon::L2_ploadruhtnew_io:
2832 case Hexagon::L2_ploadruhfnew_io:
2833 case Hexagon::L4_ploadruht_rr:
2834 case Hexagon::L4_ploadruhf_rr:
2835 case Hexagon::L4_ploadruhtnew_rr:
2836 case Hexagon::L4_ploadruhfnew_rr:
2837 case Hexagon::L2_ploadruhtnew_pi:
2838 case Hexagon::L2_ploadruhfnew_pi:
2839 case Hexagon::L4_ploadruht_abs:
2840 case Hexagon::L4_ploadruhf_abs:
2841 case Hexagon::L4_ploadruhtnew_abs:
2842 case Hexagon::L4_ploadruhfnew_abs:
2843 case Hexagon::L2_loadruhgp:
2844 return true;
2845 default:
2846 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002847 }
2848}
2849
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002850// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002851bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2852 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002853 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2854 if (!isVecUsableNextPacket(MI1, MI2))
2855 return true;
2856 return false;
2857}
2858
Brendon Cahoon254f8892016-07-29 16:44:44 +00002859/// \brief Get the base register and byte offset of a load/store instr.
2860bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2861 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2862 const {
2863 unsigned AccessSize = 0;
2864 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002865 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002866 Offset = OffsetVal;
2867 return BaseReg != 0;
2868}
2869
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002870/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002871bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2872 const MachineInstr &Second) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002873 if (DisableNVSchedule)
2874 return false;
2875 if (mayBeNewStore(Second)) {
2876 // Make sure the definition of the first instruction is the value being
2877 // stored.
2878 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002879 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002880 if (!Stored.isReg())
2881 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002882 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2883 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002884 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2885 return true;
2886 }
2887 }
2888 return false;
2889}
2890
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002891bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2892 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002893 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002894}
2895
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002896bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2897 for (auto &I : *B)
2898 if (I.isEHLabel())
2899 return true;
2900 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002901}
2902
Jyotsna Verma84256432013-03-01 17:37:13 +00002903// Returns true if an instruction can be converted into a non-extended
2904// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002905bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002906 short NonExtOpcode;
2907 // Check if the instruction has a register form that uses register in place
2908 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002909 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002910 return true;
2911
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002912 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002913 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002914
2915 switch (getAddrMode(MI)) {
2916 case HexagonII::Absolute :
2917 // Load/store with absolute addressing mode can be converted into
2918 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002919 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002920 break;
2921 case HexagonII::BaseImmOffset :
2922 // Load/store with base+offset addressing mode can be converted into
2923 // base+register offset addressing mode. However left shift operand should
2924 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002925 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002926 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002927 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002928 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002929 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002930 default:
2931 return false;
2932 }
2933 if (NonExtOpcode < 0)
2934 return false;
2935 return true;
2936 }
2937 return false;
2938}
2939
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002940bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2941 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002942 Hexagon::InstrType_Pseudo) >= 0;
2943}
2944
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002945bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2946 const {
2947 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2948 while (I != E) {
2949 if (I->isBarrier())
2950 return true;
2951 ++I;
2952 }
2953 return false;
2954}
2955
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002956// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002957bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
2958 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2959 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002960 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2961 HST.hasV60TOps();
2962}
2963
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002964// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002965bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2966 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002967 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2968}
2969
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002970bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2971 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002972 // There is no stall when ProdMI is not a V60 vector.
2973 if (!isV60VectorInstruction(ProdMI))
2974 return false;
2975
2976 // There is no stall when ProdMI and ConsMI are not dependent.
2977 if (!isDependent(ProdMI, ConsMI))
2978 return false;
2979
2980 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2981 // are scheduled in consecutive packets.
2982 if (isVecUsableNextPacket(ProdMI, ConsMI))
2983 return false;
2984
2985 return true;
2986}
2987
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002988bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002989 MachineBasicBlock::const_instr_iterator BII) const {
2990 // There is no stall when I is not a V60 vector.
2991 if (!isV60VectorInstruction(MI))
2992 return false;
2993
2994 MachineBasicBlock::const_instr_iterator MII = BII;
2995 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2996
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002997 if (!MII->isBundle()) {
2998 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002999 if (!isV60VectorInstruction(J))
3000 return false;
3001 else if (isVecUsableNextPacket(J, MI))
3002 return false;
3003 return true;
3004 }
3005
3006 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003007 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003008 if (producesStall(J, MI))
3009 return true;
3010 }
3011 return false;
3012}
3013
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003014bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003015 unsigned PredReg) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003016 for (unsigned opNum = 0; opNum < MI.getNumOperands(); opNum++) {
3017 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003018 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3019 return false; // Predicate register must be explicitly defined.
3020 }
3021
3022 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3023 // memd_locked cannot be used as .new as well,
3024 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003025 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003026}
3027
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003028bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3029 return (Opcode == Hexagon::J2_jumpt) ||
3030 (Opcode == Hexagon::J2_jumpf) ||
3031 (Opcode == Hexagon::J2_jumptnew) ||
3032 (Opcode == Hexagon::J2_jumpfnew) ||
3033 (Opcode == Hexagon::J2_jumptnewpt) ||
3034 (Opcode == Hexagon::J2_jumpfnewpt);
3035}
3036
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003037bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3038 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3039 return false;
3040 return !isPredicatedTrue(Cond[0].getImm());
3041}
3042
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003043short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3044 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003045}
3046
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003047unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3048 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003049 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3050}
3051
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003052// Returns the base register in a memory access (load/store). The offset is
3053// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003054unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003055 int &Offset, unsigned &AccessSize) const {
3056 // Return if it is not a base+offset type instruction or a MemOp.
3057 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3058 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003059 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003060 return 0;
3061
3062 // Since it is a memory access instruction, getMemAccessSize() should never
3063 // return 0.
3064 assert (getMemAccessSize(MI) &&
3065 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3066
3067 // Return Values of getMemAccessSize() are
3068 // 0 - Checked in the assert above.
3069 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3070 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3071 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3072
3073 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003074 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003075 return 0;
3076
3077 // Post increment updates its EA after the mem access,
3078 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003079 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003080 Offset = 0;
3081 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003082 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003083 }
3084
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003085 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003086}
3087
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003088/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003089bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003090 unsigned &BasePos, unsigned &OffsetPos) const {
3091 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003092 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003093 BasePos = 0;
3094 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003095 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003096 BasePos = 0;
3097 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003098 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003099 BasePos = 1;
3100 OffsetPos = 2;
3101 } else
3102 return false;
3103
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003104 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003105 BasePos++;
3106 OffsetPos++;
3107 }
3108 if (isPostIncrement(MI)) {
3109 BasePos++;
3110 OffsetPos++;
3111 }
3112
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003113 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003114 return false;
3115
3116 return true;
3117}
3118
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003119// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003120// e.g. jump_t t1 (i1)
3121// jump t2 (i2)
3122// Jumpers = {i2, i1}
3123SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3124 MachineBasicBlock& MBB) const {
3125 SmallVector<MachineInstr*, 2> Jumpers;
3126 // If the block has no terminators, it just falls into the block after it.
3127 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3128 if (I == MBB.instr_begin())
3129 return Jumpers;
3130
3131 // A basic block may looks like this:
3132 //
3133 // [ insn
3134 // EH_LABEL
3135 // insn
3136 // insn
3137 // insn
3138 // EH_LABEL
3139 // insn ]
3140 //
3141 // It has two succs but does not have a terminator
3142 // Don't know how to handle it.
3143 do {
3144 --I;
3145 if (I->isEHLabel())
3146 return Jumpers;
3147 } while (I != MBB.instr_begin());
3148
3149 I = MBB.instr_end();
3150 --I;
3151
3152 while (I->isDebugValue()) {
3153 if (I == MBB.instr_begin())
3154 return Jumpers;
3155 --I;
3156 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003157 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003158 return Jumpers;
3159
3160 // Get the last instruction in the block.
3161 MachineInstr *LastInst = &*I;
3162 Jumpers.push_back(LastInst);
3163 MachineInstr *SecondLastInst = nullptr;
3164 // Find one more terminator if present.
3165 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003166 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003167 if (!SecondLastInst) {
3168 SecondLastInst = &*I;
3169 Jumpers.push_back(SecondLastInst);
3170 } else // This is a third branch.
3171 return Jumpers;
3172 }
3173 if (I == MBB.instr_begin())
3174 break;
3175 --I;
3176 } while (true);
3177 return Jumpers;
3178}
3179
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003180short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3181 if (Opcode < 0)
3182 return -1;
3183 return Hexagon::getBaseWithLongOffset(Opcode);
3184}
3185
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003186short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3187 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003188}
3189
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003190short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3191 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003192}
3193
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003194// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003195unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3196 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003197 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3198}
3199
3200// See if instruction could potentially be a duplex candidate.
3201// If so, return its group. Zero otherwise.
3202HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003203 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003204 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3205
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003206 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003207 default:
3208 return HexagonII::HCG_None;
3209 //
3210 // Compound pairs.
3211 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3212 // "Rd16=#U6 ; jump #r9:2"
3213 // "Rd16=Rs16 ; jump #r9:2"
3214 //
3215 case Hexagon::C2_cmpeq:
3216 case Hexagon::C2_cmpgt:
3217 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003218 DstReg = MI.getOperand(0).getReg();
3219 Src1Reg = MI.getOperand(1).getReg();
3220 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003221 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3222 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3223 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3224 return HexagonII::HCG_A;
3225 break;
3226 case Hexagon::C2_cmpeqi:
3227 case Hexagon::C2_cmpgti:
3228 case Hexagon::C2_cmpgtui:
3229 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003230 DstReg = MI.getOperand(0).getReg();
3231 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003232 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3233 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003234 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3235 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3236 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003237 return HexagonII::HCG_A;
3238 break;
3239 case Hexagon::A2_tfr:
3240 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003241 DstReg = MI.getOperand(0).getReg();
3242 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003243 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3244 return HexagonII::HCG_A;
3245 break;
3246 case Hexagon::A2_tfrsi:
3247 // Rd = #u6
3248 // Do not test for #u6 size since the const is getting extended
3249 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003250 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003251 if (isIntRegForSubInst(DstReg))
3252 return HexagonII::HCG_A;
3253 break;
3254 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003255 DstReg = MI.getOperand(0).getReg();
3256 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003257 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3258 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003259 MI.getOperand(2).isImm() &&
3260 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003261 return HexagonII::HCG_A;
3262 break;
3263 // The fact that .new form is used pretty much guarantees
3264 // that predicate register will match. Nevertheless,
3265 // there could be some false positives without additional
3266 // checking.
3267 case Hexagon::J2_jumptnew:
3268 case Hexagon::J2_jumpfnew:
3269 case Hexagon::J2_jumptnewpt:
3270 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003271 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003272 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3273 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3274 return HexagonII::HCG_B;
3275 break;
3276 // Transfer and jump:
3277 // Rd=#U6 ; jump #r9:2
3278 // Rd=Rs ; jump #r9:2
3279 // Do not test for jump range here.
3280 case Hexagon::J2_jump:
3281 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003282 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003283 return HexagonII::HCG_C;
3284 break;
3285 }
3286
3287 return HexagonII::HCG_None;
3288}
3289
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003290// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003291unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3292 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003293 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3294 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003295 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3296 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003297 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003298 unsigned DestReg = GA.getOperand(0).getReg();
3299 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003300 return -1;
3301 if (DestReg == Hexagon::P0)
3302 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3303 if (DestReg == Hexagon::P1)
3304 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3305 return -1;
3306}
3307
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003308int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3309 enum Hexagon::PredSense inPredSense;
3310 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3311 Hexagon::PredSense_true;
3312 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3313 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3314 return CondOpcode;
3315
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003316 llvm_unreachable("Unexpected predicable instruction");
3317}
3318
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003319// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003320int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3321 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003322 default: llvm_unreachable("Unknown .cur type");
3323 case Hexagon::V6_vL32b_pi:
3324 return Hexagon::V6_vL32b_cur_pi;
3325 case Hexagon::V6_vL32b_ai:
3326 return Hexagon::V6_vL32b_cur_ai;
3327 //128B
3328 case Hexagon::V6_vL32b_pi_128B:
3329 return Hexagon::V6_vL32b_cur_pi_128B;
3330 case Hexagon::V6_vL32b_ai_128B:
3331 return Hexagon::V6_vL32b_cur_ai_128B;
3332 }
3333 return 0;
3334}
3335
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003336// The diagram below shows the steps involved in the conversion of a predicated
3337// store instruction to its .new predicated new-value form.
3338//
3339// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3340// ^ ^
3341// / \ (not OK. it will cause new-value store to be
3342// / X conditional on p0.new while R2 producer is
3343// / \ on p0)
3344// / \.
3345// p.new store p.old NV store
3346// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3347// ^ ^
3348// \ /
3349// \ /
3350// \ /
3351// p.old store
3352// [if (p0)memw(R0+#0)=R2]
3353//
3354//
3355// The following set of instructions further explains the scenario where
3356// conditional new-value store becomes invalid when promoted to .new predicate
3357// form.
3358//
3359// { 1) if (p0) r0 = add(r1, r2)
3360// 2) p0 = cmp.eq(r3, #0) }
3361//
3362// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3363// the first two instructions because in instr 1, r0 is conditional on old value
3364// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3365// is not valid for new-value stores.
3366// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3367// from the "Conditional Store" list. Because a predicated new value store
3368// would NOT be promoted to a double dot new store. See diagram below:
3369// This function returns yes for those stores that are predicated but not
3370// yet promoted to predicate dot new instructions.
3371//
3372// +---------------------+
3373// /-----| if (p0) memw(..)=r0 |---------\~
3374// || +---------------------+ ||
3375// promote || /\ /\ || promote
3376// || /||\ /||\ ||
3377// \||/ demote || \||/
3378// \/ || || \/
3379// +-------------------------+ || +-------------------------+
3380// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3381// +-------------------------+ || +-------------------------+
3382// || || ||
3383// || demote \||/
3384// promote || \/ NOT possible
3385// || || /\~
3386// \||/ || /||\~
3387// \/ || ||
3388// +-----------------------------+
3389// | if (p0.new) memw(..)=r0.new |
3390// +-----------------------------+
3391// Double Dot New Store
3392//
3393// Returns the most basic instruction for the .new predicated instructions and
3394// new-value stores.
3395// For example, all of the following instructions will be converted back to the
3396// same instruction:
3397// 1) if (p0.new) memw(R0+#0) = R1.new --->
3398// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3399// 3) if (p0.new) memw(R0+#0) = R1 --->
3400//
3401// To understand the translation of instruction 1 to its original form, consider
3402// a packet with 3 instructions.
3403// { p0 = cmp.eq(R0,R1)
3404// if (p0.new) R2 = add(R3, R4)
3405// R5 = add (R3, R1)
3406// }
3407// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3408//
3409// This instruction can be part of the previous packet only if both p0 and R2
3410// are promoted to .new values. This promotion happens in steps, first
3411// predicate register is promoted to .new and in the next iteration R2 is
3412// promoted. Therefore, in case of dependence check failure (due to R5) during
3413// next iteration, it should be converted back to its most basic form.
3414
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003415// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003416int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3417 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003418 if (NVOpcode >= 0) // Valid new-value store instruction.
3419 return NVOpcode;
3420
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003421 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003422 default: llvm_unreachable("Unknown .new type");
3423 case Hexagon::S4_storerb_ur:
3424 return Hexagon::S4_storerbnew_ur;
3425
3426 case Hexagon::S2_storerb_pci:
3427 return Hexagon::S2_storerb_pci;
3428
3429 case Hexagon::S2_storeri_pci:
3430 return Hexagon::S2_storeri_pci;
3431
3432 case Hexagon::S2_storerh_pci:
3433 return Hexagon::S2_storerh_pci;
3434
3435 case Hexagon::S2_storerd_pci:
3436 return Hexagon::S2_storerd_pci;
3437
3438 case Hexagon::S2_storerf_pci:
3439 return Hexagon::S2_storerf_pci;
3440
3441 case Hexagon::V6_vS32b_ai:
3442 return Hexagon::V6_vS32b_new_ai;
3443
3444 case Hexagon::V6_vS32b_pi:
3445 return Hexagon::V6_vS32b_new_pi;
3446
3447 // 128B
3448 case Hexagon::V6_vS32b_ai_128B:
3449 return Hexagon::V6_vS32b_new_ai_128B;
3450
3451 case Hexagon::V6_vS32b_pi_128B:
3452 return Hexagon::V6_vS32b_new_pi_128B;
3453 }
3454 return 0;
3455}
3456
3457// Returns the opcode to use when converting MI, which is a conditional jump,
3458// into a conditional instruction which uses the .new value of the predicate.
3459// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003460int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003461 const MachineBranchProbabilityInfo *MBPI) const {
3462 // We assume that block can have at most two successors.
3463 bool taken = false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003464 const MachineBasicBlock *Src = MI.getParent();
3465 const MachineOperand &BrTarget = MI.getOperand(1);
3466 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003467
3468 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3469 if (Prediction >= BranchProbability(1,2))
3470 taken = true;
3471
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003472 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003473 case Hexagon::J2_jumpt:
3474 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3475 case Hexagon::J2_jumpf:
3476 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3477
3478 default:
3479 llvm_unreachable("Unexpected jump instruction.");
3480 }
3481}
3482
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003483// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003484int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003485 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003486 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003487 if (NewOpcode >= 0) // Valid predicate new instruction
3488 return NewOpcode;
3489
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003490 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003491 // Condtional Jumps
3492 case Hexagon::J2_jumpt:
3493 case Hexagon::J2_jumpf:
3494 return getDotNewPredJumpOp(MI, MBPI);
3495
3496 default:
3497 assert(0 && "Unknown .new type");
3498 }
3499 return 0;
3500}
3501
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003502int HexagonInstrInfo::getDotOldOp(const int opc) const {
3503 int NewOp = opc;
3504 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3505 NewOp = Hexagon::getPredOldOpcode(NewOp);
3506 assert(NewOp >= 0 &&
3507 "Couldn't change predicate new instruction to its old form.");
3508 }
3509
3510 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3511 NewOp = Hexagon::getNonNVStore(NewOp);
3512 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3513 }
3514 return NewOp;
3515}
3516
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003517// See if instruction could potentially be a duplex candidate.
3518// If so, return its group. Zero otherwise.
3519HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003520 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003521 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3522 auto &HRI = getRegisterInfo();
3523
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003524 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003525 default:
3526 return HexagonII::HSIG_None;
3527 //
3528 // Group L1:
3529 //
3530 // Rd = memw(Rs+#u4:2)
3531 // Rd = memub(Rs+#u4:0)
3532 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003533 DstReg = MI.getOperand(0).getReg();
3534 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003535 // Special case this one from Group L2.
3536 // Rd = memw(r29+#u5:2)
3537 if (isIntRegForSubInst(DstReg)) {
3538 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3539 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003540 MI.getOperand(2).isImm() &&
3541 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003542 return HexagonII::HSIG_L2;
3543 // Rd = memw(Rs+#u4:2)
3544 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003545 (MI.getOperand(2).isImm() &&
3546 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003547 return HexagonII::HSIG_L1;
3548 }
3549 break;
3550 case Hexagon::L2_loadrub_io:
3551 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003552 DstReg = MI.getOperand(0).getReg();
3553 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003554 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003555 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003556 return HexagonII::HSIG_L1;
3557 break;
3558 //
3559 // Group L2:
3560 //
3561 // Rd = memh/memuh(Rs+#u3:1)
3562 // Rd = memb(Rs+#u3:0)
3563 // Rd = memw(r29+#u5:2) - Handled above.
3564 // Rdd = memd(r29+#u5:3)
3565 // deallocframe
3566 // [if ([!]p0[.new])] dealloc_return
3567 // [if ([!]p0[.new])] jumpr r31
3568 case Hexagon::L2_loadrh_io:
3569 case Hexagon::L2_loadruh_io:
3570 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003571 DstReg = MI.getOperand(0).getReg();
3572 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003573 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003574 MI.getOperand(2).isImm() &&
3575 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003576 return HexagonII::HSIG_L2;
3577 break;
3578 case Hexagon::L2_loadrb_io:
3579 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003580 DstReg = MI.getOperand(0).getReg();
3581 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003582 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003583 MI.getOperand(2).isImm() &&
3584 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003585 return HexagonII::HSIG_L2;
3586 break;
3587 case Hexagon::L2_loadrd_io:
3588 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003589 DstReg = MI.getOperand(0).getReg();
3590 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003591 if (isDblRegForSubInst(DstReg, HRI) &&
3592 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3593 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003594 MI.getOperand(2).isImm() &&
3595 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003596 return HexagonII::HSIG_L2;
3597 break;
3598 // dealloc_return is not documented in Hexagon Manual, but marked
3599 // with A_SUBINSN attribute in iset_v4classic.py.
3600 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003601 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003602 case Hexagon::L4_return:
3603 case Hexagon::L2_deallocframe:
3604 return HexagonII::HSIG_L2;
3605 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003606 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003607 // jumpr r31
3608 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003609 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003610 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3611 return HexagonII::HSIG_L2;
3612 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003613 case Hexagon::PS_jmprett:
3614 case Hexagon::PS_jmpretf:
3615 case Hexagon::PS_jmprettnewpt:
3616 case Hexagon::PS_jmpretfnewpt:
3617 case Hexagon::PS_jmprettnew:
3618 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003619 DstReg = MI.getOperand(1).getReg();
3620 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003621 // [if ([!]p0[.new])] jumpr r31
3622 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3623 (Hexagon::P0 == SrcReg)) &&
3624 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3625 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003626 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003627 case Hexagon::L4_return_t :
3628 case Hexagon::L4_return_f :
3629 case Hexagon::L4_return_tnew_pnt :
3630 case Hexagon::L4_return_fnew_pnt :
3631 case Hexagon::L4_return_tnew_pt :
3632 case Hexagon::L4_return_fnew_pt :
3633 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003634 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003635 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3636 return HexagonII::HSIG_L2;
3637 break;
3638 //
3639 // Group S1:
3640 //
3641 // memw(Rs+#u4:2) = Rt
3642 // memb(Rs+#u4:0) = Rt
3643 case Hexagon::S2_storeri_io:
3644 // Special case this one from Group S2.
3645 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003646 Src1Reg = MI.getOperand(0).getReg();
3647 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003648 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3649 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003650 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3651 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003652 return HexagonII::HSIG_S2;
3653 // memw(Rs+#u4:2) = Rt
3654 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003655 MI.getOperand(1).isImm() &&
3656 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003657 return HexagonII::HSIG_S1;
3658 break;
3659 case Hexagon::S2_storerb_io:
3660 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003661 Src1Reg = MI.getOperand(0).getReg();
3662 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003663 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003664 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003665 return HexagonII::HSIG_S1;
3666 break;
3667 //
3668 // Group S2:
3669 //
3670 // memh(Rs+#u3:1) = Rt
3671 // memw(r29+#u5:2) = Rt
3672 // memd(r29+#s6:3) = Rtt
3673 // memw(Rs+#u4:2) = #U1
3674 // memb(Rs+#u4) = #U1
3675 // allocframe(#u5:3)
3676 case Hexagon::S2_storerh_io:
3677 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003678 Src1Reg = MI.getOperand(0).getReg();
3679 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003680 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003681 MI.getOperand(1).isImm() &&
3682 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003683 return HexagonII::HSIG_S1;
3684 break;
3685 case Hexagon::S2_storerd_io:
3686 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003687 Src1Reg = MI.getOperand(0).getReg();
3688 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003689 if (isDblRegForSubInst(Src2Reg, HRI) &&
3690 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003691 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3692 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003693 return HexagonII::HSIG_S2;
3694 break;
3695 case Hexagon::S4_storeiri_io:
3696 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003697 Src1Reg = MI.getOperand(0).getReg();
3698 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3699 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3700 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003701 return HexagonII::HSIG_S2;
3702 break;
3703 case Hexagon::S4_storeirb_io:
3704 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003705 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003706 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003707 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3708 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003709 return HexagonII::HSIG_S2;
3710 break;
3711 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003712 if (MI.getOperand(0).isImm() &&
3713 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003714 return HexagonII::HSIG_S1;
3715 break;
3716 //
3717 // Group A:
3718 //
3719 // Rx = add(Rx,#s7)
3720 // Rd = Rs
3721 // Rd = #u6
3722 // Rd = #-1
3723 // if ([!]P0[.new]) Rd = #0
3724 // Rd = add(r29,#u6:2)
3725 // Rx = add(Rx,Rs)
3726 // P0 = cmp.eq(Rs,#u2)
3727 // Rdd = combine(#0,Rs)
3728 // Rdd = combine(Rs,#0)
3729 // Rdd = combine(#u2,#U2)
3730 // Rd = add(Rs,#1)
3731 // Rd = add(Rs,#-1)
3732 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3733 // Rd = and(Rs,#1)
3734 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003735 DstReg = MI.getOperand(0).getReg();
3736 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003737 if (isIntRegForSubInst(DstReg)) {
3738 // Rd = add(r29,#u6:2)
3739 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003740 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3741 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003742 return HexagonII::HSIG_A;
3743 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003744 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3745 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003746 return HexagonII::HSIG_A;
3747 // Rd = add(Rs,#1)
3748 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003749 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3750 ((MI.getOperand(2).getImm() == 1) ||
3751 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003752 return HexagonII::HSIG_A;
3753 }
3754 break;
3755 case Hexagon::A2_add:
3756 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003757 DstReg = MI.getOperand(0).getReg();
3758 Src1Reg = MI.getOperand(1).getReg();
3759 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003760 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3761 isIntRegForSubInst(Src2Reg))
3762 return HexagonII::HSIG_A;
3763 break;
3764 case Hexagon::A2_andir:
3765 // Same as zxtb.
3766 // Rd16=and(Rs16,#255)
3767 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003768 DstReg = MI.getOperand(0).getReg();
3769 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003770 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003771 MI.getOperand(2).isImm() &&
3772 ((MI.getOperand(2).getImm() == 1) ||
3773 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003774 return HexagonII::HSIG_A;
3775 break;
3776 case Hexagon::A2_tfr:
3777 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003778 DstReg = MI.getOperand(0).getReg();
3779 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003780 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3781 return HexagonII::HSIG_A;
3782 break;
3783 case Hexagon::A2_tfrsi:
3784 // Rd = #u6
3785 // Do not test for #u6 size since the const is getting extended
3786 // regardless and compound could be formed.
3787 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003788 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003789 if (isIntRegForSubInst(DstReg))
3790 return HexagonII::HSIG_A;
3791 break;
3792 case Hexagon::C2_cmoveit:
3793 case Hexagon::C2_cmovenewit:
3794 case Hexagon::C2_cmoveif:
3795 case Hexagon::C2_cmovenewif:
3796 // if ([!]P0[.new]) Rd = #0
3797 // Actual form:
3798 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003799 DstReg = MI.getOperand(0).getReg();
3800 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003801 if (isIntRegForSubInst(DstReg) &&
3802 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003803 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003804 return HexagonII::HSIG_A;
3805 break;
3806 case Hexagon::C2_cmpeqi:
3807 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003808 DstReg = MI.getOperand(0).getReg();
3809 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003810 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3811 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003812 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003813 return HexagonII::HSIG_A;
3814 break;
3815 case Hexagon::A2_combineii:
3816 case Hexagon::A4_combineii:
3817 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003818 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003819 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003820 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3821 (MI.getOperand(1).isGlobal() &&
3822 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3823 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3824 (MI.getOperand(2).isGlobal() &&
3825 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003826 return HexagonII::HSIG_A;
3827 break;
3828 case Hexagon::A4_combineri:
3829 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003830 DstReg = MI.getOperand(0).getReg();
3831 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003832 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003833 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3834 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003835 return HexagonII::HSIG_A;
3836 break;
3837 case Hexagon::A4_combineir:
3838 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003839 DstReg = MI.getOperand(0).getReg();
3840 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003841 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003842 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3843 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003844 return HexagonII::HSIG_A;
3845 break;
3846 case Hexagon::A2_sxtb:
3847 case Hexagon::A2_sxth:
3848 case Hexagon::A2_zxtb:
3849 case Hexagon::A2_zxth:
3850 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003851 DstReg = MI.getOperand(0).getReg();
3852 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003853 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3854 return HexagonII::HSIG_A;
3855 break;
3856 }
3857
3858 return HexagonII::HSIG_None;
3859}
3860
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003861short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3862 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003863}
3864
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003865// Return first non-debug instruction in the basic block.
3866MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3867 const {
3868 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003869 MachineInstr &MI = *MII;
3870 if (MI.isDebugValue())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003871 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003872 return &MI;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003873 }
3874 return nullptr;
3875}
3876
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003877unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003878 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003879 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3880 // still have a MinLatency property, which getStageLatency checks.
3881 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003882 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003883
3884 // Get the latency embedded in the itinerary. If we're not using timing class
3885 // latencies or if we using BSB scheduling, then restrict the maximum latency
3886 // to 1 (that is, either 0 or 1).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003887 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003888 return 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003889 unsigned Latency = ItinData->getStageLatency(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003890 if (!EnableTimingClassLatency ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003891 MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>().
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003892 useBSBScheduling())
3893 if (Latency > 1)
3894 Latency = 1;
3895 return Latency;
3896}
3897
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003898// inverts the predication logic.
3899// p -> NotP
3900// NotP -> P
3901bool HexagonInstrInfo::getInvertedPredSense(
3902 SmallVectorImpl<MachineOperand> &Cond) const {
3903 if (Cond.empty())
3904 return false;
3905 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3906 Cond[0].setImm(Opc);
3907 return true;
3908}
3909
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003910unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3911 int InvPredOpcode;
3912 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3913 : Hexagon::getTruePredOpcode(Opc);
3914 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3915 return InvPredOpcode;
3916
3917 llvm_unreachable("Unexpected predicated instruction");
3918}
3919
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003920// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003921int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
3922 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003923 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3924 & HexagonII::ExtentSignedMask;
3925 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3926 & HexagonII::ExtentBitsMask;
3927
3928 if (isSigned) // if value is signed
3929 return ~(-1U << (bits - 1));
3930 else
3931 return ~(-1U << bits);
3932}
3933
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003934unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
3935 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003936 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3937}
3938
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003939// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003940int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
3941 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003942 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3943 & HexagonII::ExtentSignedMask;
3944 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3945 & HexagonII::ExtentBitsMask;
3946
3947 if (isSigned) // if value is signed
3948 return -1U << (bits - 1);
3949 else
3950 return 0;
3951}
3952
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003953// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003954short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003955 // Check if the instruction has a register form that uses register in place
3956 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003957 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003958 if (NonExtOpcode >= 0)
3959 return NonExtOpcode;
3960
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003961 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003962 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003963 switch (getAddrMode(MI)) {
3964 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003965 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003966 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003967 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003968 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003969 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003970
Jyotsna Verma84256432013-03-01 17:37:13 +00003971 default:
3972 return -1;
3973 }
3974 }
3975 return -1;
3976}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00003977
Ahmed Bougachac88bf542015-06-11 19:30:37 +00003978bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003979 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00003980 if (Cond.empty())
3981 return false;
3982 assert(Cond.size() == 2);
3983 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003984 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3985 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00003986 }
3987 PredReg = Cond[1].getReg();
3988 PredRegPos = 1;
3989 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3990 PredRegFlags = 0;
3991 if (Cond[1].isImplicit())
3992 PredRegFlags = RegState::Implicit;
3993 if (Cond[1].isUndef())
3994 PredRegFlags |= RegState::Undef;
3995 return true;
3996}
3997
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003998short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
3999 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004000}
4001
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004002short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4003 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004004}
4005
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004006// Return the number of bytes required to encode the instruction.
4007// Hexagon instructions are fixed length, 4 bytes, unless they
4008// use a constant extender, which requires another 4 bytes.
4009// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004010unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4011 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004012 return 0;
4013
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004014 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004015 if (!Size)
4016 // Assume the default insn size in case it cannot be determined
4017 // for whatever reason.
4018 Size = HEXAGON_INSTR_SIZE;
4019
4020 if (isConstExtended(MI) || isExtended(MI))
4021 Size += HEXAGON_INSTR_SIZE;
4022
4023 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004024 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4025 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004026 const MachineFunction *MF = MBB.getParent();
4027 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4028
4029 // Count the number of register definitions to find the asm string.
4030 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004031 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004032 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004033 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004034
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004035 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004036 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004037 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004038 Size = getInlineAsmLength(AsmStr, *MAI);
4039 }
4040
4041 return Size;
4042}
4043
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004044uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4045 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004046 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4047}
4048
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004049unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4050 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004051 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004052 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004053
4054 return IS.getUnits();
4055}
4056
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004057unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4058 const uint64_t F = get(Opcode).TSFlags;
4059 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4060}
4061
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004062// Calculate size of the basic block without debug instructions.
4063unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4064 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4065}
4066
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004067unsigned HexagonInstrInfo::nonDbgBundleSize(
4068 MachineBasicBlock::const_iterator BundleHead) const {
4069 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004070 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004071 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004072 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004073}
4074
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004075/// immediateExtend - Changes the instruction in place to one using an immediate
4076/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004077void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004078 assert((isExtendable(MI)||isConstExtended(MI)) &&
4079 "Instruction must be extendable");
4080 // Find which operand is extendable.
4081 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004082 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004083 // This needs to be something we understand.
4084 assert((MO.isMBB() || MO.isImm()) &&
4085 "Branch with unknown extendable field type");
4086 // Mark given operand as extended.
4087 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4088}
4089
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004090bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004091 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004092 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004093 << NewTarget->getNumber(); MI.dump(););
4094 assert(MI.isBranch());
4095 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4096 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004097 // In general branch target is the last operand,
4098 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004099 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004100 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004101 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4102 MI.getOperand(TargetPos).setMBB(NewTarget);
4103 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004104 NewOpcode = reversePrediction(NewOpcode);
4105 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004106 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004107 return true;
4108}
4109
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004110void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4111 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4112 MachineFunction::iterator A = MF.begin();
4113 MachineBasicBlock &B = *A;
4114 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004115 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004116 MachineInstr *NewMI;
4117
4118 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4119 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004120 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004121 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4122 " Class: " << NewMI->getDesc().getSchedClass());
4123 NewMI->eraseFromParent();
4124 }
4125 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4126}
4127
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004128// inverts the predication logic.
4129// p -> NotP
4130// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004131bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4132 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4133 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004134 return true;
4135}
4136
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004137// Reverse the branch prediction.
4138unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4139 int PredRevOpcode = -1;
4140 if (isPredictedTaken(Opcode))
4141 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4142 else
4143 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4144 assert(PredRevOpcode > 0);
4145 return PredRevOpcode;
4146}
4147
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004148// TODO: Add more rigorous validation.
4149bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4150 const {
4151 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4152}
4153
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004154short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4155 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004156}