blob: 0971300c9620227fa8173a24a3943619881a61bf [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"
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +000062#include "HexagonDepTimingClasses.h"
Chandler Carruthd174b722014-04-22 02:03:14 +000063
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000064cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000065 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
66 "packetization boundary."));
67
68static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
69 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
70
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000071static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
72 cl::Hidden, cl::ZeroOrMore, cl::init(false),
73 cl::desc("Disable schedule adjustment for new value stores."));
74
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000075static cl::opt<bool> EnableTimingClassLatency(
76 "enable-timing-class-latency", cl::Hidden, cl::init(false),
77 cl::desc("Enable timing class latency"));
78
79static cl::opt<bool> EnableALUForwarding(
80 "enable-alu-forwarding", cl::Hidden, cl::init(true),
81 cl::desc("Enable vec alu forwarding"));
82
83static cl::opt<bool> EnableACCForwarding(
84 "enable-acc-forwarding", cl::Hidden, cl::init(true),
85 cl::desc("Enable vec acc forwarding"));
86
87static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
88 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
89
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000090static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
91 cl::init(true), cl::Hidden, cl::ZeroOrMore,
92 cl::desc("Use the DFA based hazard recognizer."));
93
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094///
95/// Constants for Hexagon instructions.
96///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000097const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
98const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
99const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
100const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000101const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000102const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000104const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000106const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000107const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000108const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000110const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000112const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000113const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000114const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000115const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000116const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000117const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000118const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +0000119const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
120const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
121const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
122const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000123
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000124// Pin the vtable to this file.
125void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000126
127HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000128 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000129 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000130
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000131static bool isIntRegForSubInst(unsigned Reg) {
132 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
133 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000134}
135
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000136static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000137 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
138 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000139}
140
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000141/// Calculate number of instructions excluding the debug instructions.
142static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
143 MachineBasicBlock::const_instr_iterator MIE) {
144 unsigned Count = 0;
145 for (; MIB != MIE; ++MIB) {
146 if (!MIB->isDebugValue())
147 ++Count;
148 }
149 return Count;
150}
151
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000152/// Find the hardware loop instruction used to set-up the specified loop.
153/// On Hexagon, we have two instructions used to set-up the hardware loop
154/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
155/// to indicate the end of a loop.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000156static MachineInstr *findLoopInstr(MachineBasicBlock *BB, unsigned EndLoopOp,
157 MachineBasicBlock *TargetBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000158 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000159 unsigned LOOPi;
160 unsigned LOOPr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000161 if (EndLoopOp == Hexagon::ENDLOOP0) {
162 LOOPi = Hexagon::J2_loop0i;
163 LOOPr = Hexagon::J2_loop0r;
164 } else { // EndLoopOp == Hexagon::EndLOOP1
165 LOOPi = Hexagon::J2_loop1i;
166 LOOPr = Hexagon::J2_loop1r;
167 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000168
Brendon Cahoondf43e682015-05-08 16:16:29 +0000169 // The loop set-up instruction will be in a predecessor block
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000170 for (MachineBasicBlock *PB : BB->predecessors()) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000171 // If this has been visited, already skip it.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000172 if (!Visited.insert(PB).second)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000173 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000174 if (PB == BB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000175 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000176 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
177 unsigned Opc = I->getOpcode();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000178 if (Opc == LOOPi || Opc == LOOPr)
179 return &*I;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000180 // We've reached a different loop, which means the loop01 has been
181 // removed.
182 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000183 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000184 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000185 // Check the predecessors for the LOOP instruction.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000186 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
187 return Loop;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000188 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000189 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000190}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000191
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000192/// Gather register def/uses from MI.
193/// This treats possible (predicated) defs as actually happening ones
194/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000195static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000196 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
197 Defs.clear();
198 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000199
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000200 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
201 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000202
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000203 if (!MO.isReg())
204 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000205
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000206 unsigned Reg = MO.getReg();
207 if (!Reg)
208 continue;
209
210 if (MO.isUse())
211 Uses.push_back(MO.getReg());
212
213 if (MO.isDef())
214 Defs.push_back(MO.getReg());
215 }
216}
217
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000218// Position dependent, so check twice for swap.
219static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
220 switch (Ga) {
221 case HexagonII::HSIG_None:
222 default:
223 return false;
224 case HexagonII::HSIG_L1:
225 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
226 case HexagonII::HSIG_L2:
227 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
228 Gb == HexagonII::HSIG_A);
229 case HexagonII::HSIG_S1:
230 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
231 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
232 case HexagonII::HSIG_S2:
233 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
234 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
235 Gb == HexagonII::HSIG_A);
236 case HexagonII::HSIG_A:
237 return (Gb == HexagonII::HSIG_A);
238 case HexagonII::HSIG_Compound:
239 return (Gb == HexagonII::HSIG_Compound);
240 }
241 return false;
242}
243
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000244/// isLoadFromStackSlot - If the specified machine instruction is a direct
245/// load from a stack slot, return the virtual or physical register number of
246/// the destination along with the FrameIndex of the loaded stack slot. If
247/// not, return 0. This predicate must return 0 if the instruction has
248/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000249unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000250 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000251 switch (MI.getOpcode()) {
252 default:
253 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000254 case Hexagon::L2_loadri_io:
255 case Hexagon::L2_loadrd_io:
256 case Hexagon::V6_vL32b_ai:
257 case Hexagon::V6_vL32b_ai_128B:
258 case Hexagon::V6_vL32Ub_ai:
259 case Hexagon::V6_vL32Ub_ai_128B:
260 case Hexagon::LDriw_pred:
261 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000262 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000263 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000264 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000265 case Hexagon::PS_vloadrw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000266 const MachineOperand OpFI = MI.getOperand(1);
267 if (!OpFI.isFI())
268 return 0;
269 const MachineOperand OpOff = MI.getOperand(2);
270 if (!OpOff.isImm() || OpOff.getImm() != 0)
271 return 0;
272 FrameIndex = OpFI.getIndex();
273 return MI.getOperand(0).getReg();
274 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000275
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000276 case Hexagon::L2_ploadrit_io:
277 case Hexagon::L2_ploadrif_io:
278 case Hexagon::L2_ploadrdt_io:
279 case Hexagon::L2_ploadrdf_io: {
280 const MachineOperand OpFI = MI.getOperand(2);
281 if (!OpFI.isFI())
282 return 0;
283 const MachineOperand OpOff = MI.getOperand(3);
284 if (!OpOff.isImm() || OpOff.getImm() != 0)
285 return 0;
286 FrameIndex = OpFI.getIndex();
287 return MI.getOperand(0).getReg();
288 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000289 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000290
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000291 return 0;
292}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000293
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000294/// isStoreToStackSlot - If the specified machine instruction is a direct
295/// store to a stack slot, return the virtual or physical register number of
296/// the source reg along with the FrameIndex of the loaded stack slot. If
297/// not, return 0. This predicate must return 0 if the instruction has
298/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000299unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000300 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000301 switch (MI.getOpcode()) {
302 default:
303 break;
304 case Hexagon::S2_storerb_io:
305 case Hexagon::S2_storerh_io:
306 case Hexagon::S2_storeri_io:
307 case Hexagon::S2_storerd_io:
308 case Hexagon::V6_vS32b_ai:
309 case Hexagon::V6_vS32b_ai_128B:
310 case Hexagon::V6_vS32Ub_ai:
311 case Hexagon::V6_vS32Ub_ai_128B:
312 case Hexagon::STriw_pred:
313 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000314 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000315 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000316 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000317 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000318 const MachineOperand &OpFI = MI.getOperand(0);
319 if (!OpFI.isFI())
320 return 0;
321 const MachineOperand &OpOff = MI.getOperand(1);
322 if (!OpOff.isImm() || OpOff.getImm() != 0)
323 return 0;
324 FrameIndex = OpFI.getIndex();
325 return MI.getOperand(2).getReg();
326 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000327
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000328 case Hexagon::S2_pstorerbt_io:
329 case Hexagon::S2_pstorerbf_io:
330 case Hexagon::S2_pstorerht_io:
331 case Hexagon::S2_pstorerhf_io:
332 case Hexagon::S2_pstorerit_io:
333 case Hexagon::S2_pstorerif_io:
334 case Hexagon::S2_pstorerdt_io:
335 case Hexagon::S2_pstorerdf_io: {
336 const MachineOperand &OpFI = MI.getOperand(1);
337 if (!OpFI.isFI())
338 return 0;
339 const MachineOperand &OpOff = MI.getOperand(2);
340 if (!OpOff.isImm() || OpOff.getImm() != 0)
341 return 0;
342 FrameIndex = OpFI.getIndex();
343 return MI.getOperand(3).getReg();
344 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000345 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000346
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000347 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000348}
349
Brendon Cahoondf43e682015-05-08 16:16:29 +0000350/// This function can analyze one/two way branching only and should (mostly) be
351/// called by target independent side.
352/// First entry is always the opcode of the branching instruction, except when
353/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
354/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
355/// e.g. Jump_c p will have
356/// Cond[0] = Jump_c
357/// Cond[1] = p
358/// HW-loop ENDLOOP:
359/// Cond[0] = ENDLOOP
360/// Cond[1] = MBB
361/// New value jump:
362/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
363/// Cond[1] = R
364/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000365///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000366bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000367 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000368 MachineBasicBlock *&FBB,
369 SmallVectorImpl<MachineOperand> &Cond,
370 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000371 TBB = nullptr;
372 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000373 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000374
375 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000376 MachineBasicBlock::instr_iterator I = MBB.instr_end();
377 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000378 return false;
379
380 // A basic block may looks like this:
381 //
382 // [ insn
383 // EH_LABEL
384 // insn
385 // insn
386 // insn
387 // EH_LABEL
388 // insn ]
389 //
390 // It has two succs but does not have a terminator
391 // Don't know how to handle it.
392 do {
393 --I;
394 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000395 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000397 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000399 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000400 --I;
401
402 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000403 if (I == MBB.instr_begin())
404 return false;
405 --I;
406 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000407
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000408 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
409 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000410 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000411 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000412 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000413 DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000414 I->eraseFromParent();
415 I = MBB.instr_end();
416 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417 return false;
418 --I;
419 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000420 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000421 return false;
422
423 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000424 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000425 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000426 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000427 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000428 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000429 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000430 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000431 else
432 // This is a third branch.
433 return true;
434 }
435 if (I == MBB.instr_begin())
436 break;
437 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000438 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000439
440 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000441 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
442 // If the branch target is not a basic block, it could be a tail call.
443 // (It is, if the target is a function.)
444 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
445 return true;
446 if (SecLastOpcode == Hexagon::J2_jump &&
447 !SecondLastInst->getOperand(0).isMBB())
448 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000449
450 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000451 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000452
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000453 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
454 return true;
455
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000456 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000457 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000458 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 TBB = LastInst->getOperand(0).getMBB();
460 return false;
461 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000462 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000463 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000464 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465 Cond.push_back(LastInst->getOperand(0));
466 return false;
467 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000468 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000469 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000470 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000471 Cond.push_back(LastInst->getOperand(0));
472 return false;
473 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000474 // Only supporting rr/ri versions of new-value jumps.
475 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
476 TBB = LastInst->getOperand(2).getMBB();
477 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
478 Cond.push_back(LastInst->getOperand(0));
479 Cond.push_back(LastInst->getOperand(1));
480 return false;
481 }
482 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
483 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000484 // Otherwise, don't know what this is.
485 return true;
486 }
487
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000488 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000489 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000490 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000491 if (!SecondLastInst->getOperand(1).isMBB())
492 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000493 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000494 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 Cond.push_back(SecondLastInst->getOperand(0));
496 FBB = LastInst->getOperand(0).getMBB();
497 return false;
498 }
499
Brendon Cahoondf43e682015-05-08 16:16:29 +0000500 // Only supporting rr/ri versions of new-value jumps.
501 if (SecLastOpcodeHasNVJump &&
502 (SecondLastInst->getNumExplicitOperands() == 3) &&
503 (LastOpcode == Hexagon::J2_jump)) {
504 TBB = SecondLastInst->getOperand(2).getMBB();
505 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
506 Cond.push_back(SecondLastInst->getOperand(0));
507 Cond.push_back(SecondLastInst->getOperand(1));
508 FBB = LastInst->getOperand(0).getMBB();
509 return false;
510 }
511
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000512 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
513 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000514 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000515 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000516 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000517 if (AllowModify)
518 I->eraseFromParent();
519 return false;
520 }
521
Brendon Cahoondf43e682015-05-08 16:16:29 +0000522 // If the block ends with an ENDLOOP, and J2_jump, handle it.
523 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000524 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000525 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000526 Cond.push_back(SecondLastInst->getOperand(0));
527 FBB = LastInst->getOperand(0).getMBB();
528 return false;
529 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000530 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
531 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000532 // Otherwise, can't handle this.
533 return true;
534}
535
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000536unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000537 int *BytesRemoved) const {
538 assert(!BytesRemoved && "code size not handled");
539
Brendon Cahoondf43e682015-05-08 16:16:29 +0000540 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000541 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000542 unsigned Count = 0;
543 while (I != MBB.begin()) {
544 --I;
545 if (I->isDebugValue())
546 continue;
547 // Only removing branches from end of MBB.
548 if (!I->isBranch())
549 return Count;
550 if (Count && (I->getOpcode() == Hexagon::J2_jump))
551 llvm_unreachable("Malformed basic block: unconditional branch not last");
552 MBB.erase(&MBB.back());
553 I = MBB.end();
554 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000555 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000556 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000557}
558
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000559unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000560 MachineBasicBlock *TBB,
561 MachineBasicBlock *FBB,
562 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000563 const DebugLoc &DL,
564 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000565 unsigned BOpc = Hexagon::J2_jump;
566 unsigned BccOpc = Hexagon::J2_jumpt;
567 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000568 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000569 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000570
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000571 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000572 // If we want to reverse the branch an odd number of times, we want
573 // J2_jumpf.
574 if (!Cond.empty() && Cond[0].isImm())
575 BccOpc = Cond[0].getImm();
576
577 if (!FBB) {
578 if (Cond.empty()) {
579 // Due to a bug in TailMerging/CFG Optimization, we need to add a
580 // special case handling of a predicated jump followed by an
581 // unconditional jump. If not, Tail Merging and CFG Optimization go
582 // into an infinite loop.
583 MachineBasicBlock *NewTBB, *NewFBB;
584 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000585 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000586 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000587 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
588 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000589 reverseBranchCondition(Cond);
590 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000591 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000592 }
593 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
594 } else if (isEndLoopN(Cond[0].getImm())) {
595 int EndLoopOp = Cond[0].getImm();
596 assert(Cond[1].isMBB());
597 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
598 // Check for it, and change the BB target if needed.
599 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000600 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
601 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000602 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
603 Loop->getOperand(0).setMBB(TBB);
604 // Add the ENDLOOP after the finding the LOOP0.
605 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
606 } else if (isNewValueJump(Cond[0].getImm())) {
607 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
608 // New value jump
609 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
610 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
611 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
612 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
613 if (Cond[2].isReg()) {
614 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
615 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
616 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
617 } else if(Cond[2].isImm()) {
618 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
619 addImm(Cond[2].getImm()).addMBB(TBB);
620 } else
621 llvm_unreachable("Invalid condition for branching");
622 } else {
623 assert((Cond.size() == 2) && "Malformed cond vector");
624 const MachineOperand &RO = Cond[1];
625 unsigned Flags = getUndefRegState(RO.isUndef());
626 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
627 }
628 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000629 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000630 assert((!Cond.empty()) &&
631 "Cond. cannot be empty when multiple branchings are required");
632 assert((!isNewValueJump(Cond[0].getImm())) &&
633 "NV-jump cannot be inserted with another branch");
634 // Special case for hardware loops. The condition is a basic block.
635 if (isEndLoopN(Cond[0].getImm())) {
636 int EndLoopOp = Cond[0].getImm();
637 assert(Cond[1].isMBB());
638 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
639 // Check for it, and change the BB target if needed.
640 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000641 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
642 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000643 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
644 Loop->getOperand(0).setMBB(TBB);
645 // Add the ENDLOOP after the finding the LOOP0.
646 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
647 } else {
648 const MachineOperand &RO = Cond[1];
649 unsigned Flags = getUndefRegState(RO.isUndef());
650 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000651 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000652 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000653
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000654 return 2;
655}
656
Brendon Cahoon254f8892016-07-29 16:44:44 +0000657/// Analyze the loop code to find the loop induction variable and compare used
658/// to compute the number of iterations. Currently, we analyze loop that are
659/// controlled using hardware loops. In this case, the induction variable
660/// instruction is null. For all other cases, this function returns true, which
661/// means we're unable to analyze it.
662bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
663 MachineInstr *&IndVarInst,
664 MachineInstr *&CmpInst) const {
665
666 MachineBasicBlock *LoopEnd = L.getBottomBlock();
667 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
668 // We really "analyze" only hardware loops right now.
669 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
670 IndVarInst = nullptr;
671 CmpInst = &*I;
672 return false;
673 }
674 return true;
675}
676
677/// Generate code to reduce the loop iteration by one and check if the loop is
678/// finished. Return the value/register of the new loop count. this function
679/// assumes the nth iteration is peeled first.
680unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000681 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000682 SmallVectorImpl<MachineOperand> &Cond,
683 SmallVectorImpl<MachineInstr *> &PrevInsts,
684 unsigned Iter, unsigned MaxIter) const {
685 // We expect a hardware loop currently. This means that IndVar is set
686 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000687 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000688 && "Expecting a hardware loop");
689 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000690 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000691 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000692 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(),
693 Cmp.getOperand(0).getMBB(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000694 if (!Loop)
695 return 0;
696 // If the loop trip count is a compile-time value, then just change the
697 // value.
698 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
699 Loop->getOpcode() == Hexagon::J2_loop1i) {
700 int64_t Offset = Loop->getOperand(1).getImm();
701 if (Offset <= 1)
702 Loop->eraseFromParent();
703 else
704 Loop->getOperand(1).setImm(Offset - 1);
705 return Offset - 1;
706 }
707 // The loop trip count is a run-time value. We generate code to subtract
708 // one from the trip count, and update the loop instruction.
709 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
710 unsigned LoopCount = Loop->getOperand(1).getReg();
711 // Check if we're done with the loop.
712 unsigned LoopEnd = createVR(MF, MVT::i1);
713 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
714 addReg(LoopCount).addImm(1);
715 unsigned NewLoopCount = createVR(MF, MVT::i32);
716 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
717 addReg(LoopCount).addImm(-1);
718 // Update the previously generated instructions with the new loop counter.
719 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
720 E = PrevInsts.end(); I != E; ++I)
721 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
722 PrevInsts.clear();
723 PrevInsts.push_back(NewCmp);
724 PrevInsts.push_back(NewAdd);
725 // Insert the new loop instruction if this is the last time the loop is
726 // decremented.
727 if (Iter == MaxIter)
728 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
729 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
730 // Delete the old loop instruction.
731 if (Iter == 0)
732 Loop->eraseFromParent();
733 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
734 Cond.push_back(NewCmp->getOperand(0));
735 return NewLoopCount;
736}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000737
738bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
739 unsigned NumCycles, unsigned ExtraPredCycles,
740 BranchProbability Probability) const {
741 return nonDbgBBSize(&MBB) <= 3;
742}
743
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000744bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
745 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
746 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
747 const {
748 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
749}
750
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000751bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
752 unsigned NumInstrs, BranchProbability Probability) const {
753 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000754}
755
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000756void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000757 MachineBasicBlock::iterator I,
758 const DebugLoc &DL, unsigned DestReg,
759 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000760 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000761 unsigned KillFlag = getKillRegState(KillSrc);
762
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000763 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000764 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000765 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000766 return;
767 }
768 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000769 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
770 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000771 return;
772 }
773 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
774 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000775 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
776 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000777 return;
778 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000779 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000780 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000781 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
782 .addReg(SrcReg, KillFlag);
783 return;
784 }
785 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
786 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
787 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
788 .addReg(SrcReg, KillFlag);
789 return;
790 }
791 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
792 Hexagon::IntRegsRegClass.contains(SrcReg)) {
793 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
794 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000795 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000796 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000797 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
798 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000799 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
800 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000801 return;
802 }
803 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
804 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000805 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
806 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000807 return;
808 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000809 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
810 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000811 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
812 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000813 return;
814 }
815 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
816 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000817 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000818 return;
819 }
820 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000821 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
822 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000823 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000824 .addReg(HiSrc, KillFlag)
825 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000826 return;
827 }
828 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000829 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
830 .addReg(SrcReg)
831 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000832 return;
833 }
834 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000835 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000836 llvm_unreachable("Unimplemented pred to vec");
837 return;
838 }
839 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
840 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
841 llvm_unreachable("Unimplemented vec to pred");
842 return;
843 }
844 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000845 unsigned HiDst = HRI.getSubReg(DestReg, Hexagon::vsub_hi);
846 unsigned LoDst = HRI.getSubReg(DestReg, Hexagon::vsub_lo);
847 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
848 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
849 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), HiDst)
850 .addReg(HiSrc, KillFlag);
851 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), LoDst)
852 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000853 return;
854 }
Sirish Pande30804c22012-02-15 18:52:27 +0000855
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000856#ifndef NDEBUG
857 // Show the invalid registers to ease debugging.
858 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
859 << ": " << PrintReg(DestReg, &HRI)
860 << " = " << PrintReg(SrcReg, &HRI) << '\n';
861#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000862 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000863}
864
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000865void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
866 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
867 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000868 DebugLoc DL = MBB.findDebugLoc(I);
869 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000870 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000872 unsigned KillFlag = getKillRegState(isKill);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000873 bool HasAlloca = MFI.hasVarSizedObjects();
874 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
875 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
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 Parzyszek781324f2017-05-03 15:23:53 +0000906 // If there are variable-sized objects, spills will not be aligned.
907 if (HasAlloca)
908 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000909 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
910 : Hexagon::V6_vS32b_ai_128B;
911 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000912 .addFrameIndex(FI).addImm(0)
913 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
914 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000915 // If there are variable-sized objects, spills will not be aligned.
916 if (HasAlloca)
917 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000918 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
919 : Hexagon::V6_vS32b_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::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000924 // If there are variable-sized objects, spills will not be aligned.
925 if (HasAlloca)
926 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000927 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
928 : Hexagon::PS_vstorerw_ai;
929 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000930 .addFrameIndex(FI).addImm(0)
931 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
932 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000933 // If there are variable-sized objects, spills will not be aligned.
934 if (HasAlloca)
935 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000936 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
937 : Hexagon::PS_vstorerw_ai_128B;
938 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000939 .addFrameIndex(FI).addImm(0)
940 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000941 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000942 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000943 }
944}
945
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000946void HexagonInstrInfo::loadRegFromStackSlot(
947 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
948 int FI, const TargetRegisterClass *RC,
949 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950 DebugLoc DL = MBB.findDebugLoc(I);
951 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000952 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000953 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000954 bool HasAlloca = MFI.hasVarSizedObjects();
955 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
956 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000957
Alex Lorenze40c8a22015-08-11 23:09:45 +0000958 MachineMemOperand *MMO = MF.getMachineMemOperand(
959 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
960 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000961
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000962 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000963 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000964 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000965 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000966 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000967 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000968 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000969 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000970 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
971 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
972 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
973 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000974 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000975 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000976 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
977 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000978 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000979 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
980 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000981 // If there are variable-sized objects, spills will not be aligned.
982 if (HasAlloca)
983 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000984 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
985 : Hexagon::PS_vloadrw_ai_128B;
986 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000987 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
988 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000989 // If there are variable-sized objects, spills will not be aligned.
990 if (HasAlloca)
991 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000992 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
993 : Hexagon::V6_vL32b_ai_128B;
994 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000995 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
996 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000997 // If there are variable-sized objects, spills will not be aligned.
998 if (HasAlloca)
999 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001000 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
1001 : Hexagon::V6_vL32b_ai;
1002 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +00001003 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1004 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +00001005 // If there are variable-sized objects, spills will not be aligned.
1006 if (HasAlloca)
1007 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001008 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
1009 : Hexagon::PS_vloadrw_ai;
1010 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +00001011 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001012 } else {
Craig Toppere55c5562012-02-07 02:50:20 +00001013 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001014 }
1015}
1016
Ron Lieberman88159e52016-09-02 22:56:24 +00001017static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
1018 const MachineBasicBlock &B = *MI.getParent();
1019 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +00001020 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +00001021 for (auto I = B.rbegin(); I != E; ++I)
1022 Regs.stepBackward(*I);
1023}
1024
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001025/// expandPostRAPseudo - This function is called for all pseudo instructions
1026/// that remain after register allocation. Many pseudo instructions are
1027/// created to help register allocation. This is the place to convert them
1028/// into real instructions. The target can edit MI in place, or it can insert
1029/// new instructions and erase MI. The function should return true if
1030/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001031bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001032 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001033 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1034 MachineBasicBlock &MBB = *MI.getParent();
1035 DebugLoc DL = MI.getDebugLoc();
1036 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001037 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001038
1039 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001040 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001041 MachineOperand &MD = MI.getOperand(0);
1042 MachineOperand &MS = MI.getOperand(1);
1043 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001044 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1045 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001046 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001047 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001048 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001049 return true;
1050 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001051 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001052 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001053 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001054 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001055 MBB.erase(MI);
1056 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001057 case Hexagon::V6_vassignp_128B:
1058 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001059 unsigned SrcReg = MI.getOperand(1).getReg();
1060 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001061 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1062 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001063 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1064 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001065 MBB.erase(MI);
1066 return true;
1067 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001068 case Hexagon::V6_lo_128B:
1069 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001070 unsigned SrcReg = MI.getOperand(1).getReg();
1071 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001072 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001073 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001074 MBB.erase(MI);
1075 MRI.clearKillFlags(SrcSubLo);
1076 return true;
1077 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001078 case Hexagon::V6_hi_128B:
1079 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001080 unsigned SrcReg = MI.getOperand(1).getReg();
1081 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001082 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001083 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001084 MBB.erase(MI);
1085 MRI.clearKillFlags(SrcSubHi);
1086 return true;
1087 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001088 case Hexagon::PS_vstorerw_ai:
1089 case Hexagon::PS_vstorerwu_ai:
1090 case Hexagon::PS_vstorerw_ai_128B:
1091 case Hexagon::PS_vstorerwu_ai_128B: {
1092 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1093 Opc == Hexagon::PS_vstorerwu_ai_128B);
1094 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1095 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001096 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001097 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1098 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001099 unsigned NewOpc;
1100 if (Aligned)
1101 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1102 : Hexagon::V6_vS32b_ai;
1103 else
1104 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1105 : Hexagon::V6_vS32Ub_ai;
1106
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001107 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001108 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001109 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001110 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001111 .addImm(MI.getOperand(1).getImm())
1112 .addReg(SrcSubLo)
1113 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001114 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001115 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001116 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001117 // The Vectors are indexed in multiples of vector size.
1118 .addImm(MI.getOperand(1).getImm() + Offset)
1119 .addReg(SrcSubHi)
1120 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001121 MBB.erase(MI);
1122 return true;
1123 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001124 case Hexagon::PS_vloadrw_ai:
1125 case Hexagon::PS_vloadrwu_ai:
1126 case Hexagon::PS_vloadrw_ai_128B:
1127 case Hexagon::PS_vloadrwu_ai_128B: {
1128 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1129 Opc == Hexagon::PS_vloadrwu_ai_128B);
1130 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1131 Opc == Hexagon::PS_vloadrw_ai_128B);
1132 unsigned NewOpc;
1133 if (Aligned)
1134 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1135 : Hexagon::V6_vL32b_ai;
1136 else
1137 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1138 : Hexagon::V6_vL32Ub_ai;
1139
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001140 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001141 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Diana Picus116bbab2017-01-13 09:58:52 +00001142 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1143 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Krzysztof Parzyszek4be9d922017-05-03 15:26:13 +00001144 .add(MI.getOperand(1))
1145 .addImm(MI.getOperand(2).getImm())
1146 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001147 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001148 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1149 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001150 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001151 .addImm(MI.getOperand(2).getImm() + Offset)
1152 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001153 MBB.erase(MI);
1154 return true;
1155 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001156 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001157 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001158 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1159 .addReg(Reg, RegState::Undef)
1160 .addReg(Reg, RegState::Undef);
1161 MBB.erase(MI);
1162 return true;
1163 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001164 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001165 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001166 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1167 .addReg(Reg, RegState::Undef)
1168 .addReg(Reg, RegState::Undef);
1169 MBB.erase(MI);
1170 return true;
1171 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001172 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001173 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001174 unsigned DstReg = MI.getOperand(0).getReg();
1175 unsigned Src1Reg = MI.getOperand(1).getReg();
1176 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001177 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1178 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1179 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1180 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001181 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001182 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001183 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001184 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001185 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
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)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001188 .addReg(Src2SubLo);
1189 MBB.erase(MI);
1190 MRI.clearKillFlags(Src1SubHi);
1191 MRI.clearKillFlags(Src1SubLo);
1192 MRI.clearKillFlags(Src2SubHi);
1193 MRI.clearKillFlags(Src2SubLo);
1194 return true;
1195 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001196 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001197 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001198 unsigned DstReg = MI.getOperand(0).getReg();
1199 unsigned Src1Reg = MI.getOperand(1).getReg();
1200 unsigned Src2Reg = MI.getOperand(2).getReg();
1201 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001202 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1203 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1204 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1205 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1206 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1207 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001208 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001209 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001210 .addReg(Src1SubHi)
1211 .addReg(Src2SubHi)
1212 .addReg(Src3SubHi);
1213 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001214 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001215 .addReg(Src1SubLo)
1216 .addReg(Src2SubLo)
1217 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001218 MBB.erase(MI);
1219 MRI.clearKillFlags(Src1SubHi);
1220 MRI.clearKillFlags(Src1SubLo);
1221 MRI.clearKillFlags(Src2SubHi);
1222 MRI.clearKillFlags(Src2SubLo);
1223 MRI.clearKillFlags(Src3SubHi);
1224 MRI.clearKillFlags(Src3SubLo);
1225 return true;
1226 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001227 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001228 const MachineOperand &Op0 = MI.getOperand(0);
1229 const MachineOperand &Op1 = MI.getOperand(1);
1230 const MachineOperand &Op2 = MI.getOperand(2);
1231 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001232 unsigned Rd = Op0.getReg();
1233 unsigned Pu = Op1.getReg();
1234 unsigned Rs = Op2.getReg();
1235 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001236 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001237 unsigned K1 = getKillRegState(Op1.isKill());
1238 unsigned K2 = getKillRegState(Op2.isKill());
1239 unsigned K3 = getKillRegState(Op3.isKill());
1240 if (Rd != Rs)
1241 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1242 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1243 .addReg(Rs, K2);
1244 if (Rd != Rt)
1245 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1246 .addReg(Pu, K1)
1247 .addReg(Rt, K3);
1248 MBB.erase(MI);
1249 return true;
1250 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001251 case Hexagon::PS_vselect:
1252 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001253 const MachineOperand &Op0 = MI.getOperand(0);
1254 const MachineOperand &Op1 = MI.getOperand(1);
1255 const MachineOperand &Op2 = MI.getOperand(2);
1256 const MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001257 LivePhysRegs LiveAtMI(&HRI);
1258 getLiveRegsAt(LiveAtMI, MI);
1259 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1260 if (Op0.getReg() != Op2.getReg()) {
1261 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001262 .add(Op0)
1263 .add(Op1)
1264 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001265 if (IsDestLive)
1266 T.addReg(Op0.getReg(), RegState::Implicit);
1267 IsDestLive = true;
1268 }
1269 if (Op0.getReg() != Op3.getReg()) {
1270 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001271 .add(Op0)
1272 .add(Op1)
1273 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001274 if (IsDestLive)
1275 T.addReg(Op0.getReg(), RegState::Implicit);
1276 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001277 MBB.erase(MI);
1278 return true;
1279 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001280 case Hexagon::PS_wselect:
1281 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001282 MachineOperand &Op0 = MI.getOperand(0);
1283 MachineOperand &Op1 = MI.getOperand(1);
1284 MachineOperand &Op2 = MI.getOperand(2);
1285 MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001286 LivePhysRegs LiveAtMI(&HRI);
1287 getLiveRegsAt(LiveAtMI, MI);
1288 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1289
1290 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001291 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1292 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001293 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001294 .add(Op0)
1295 .add(Op1)
1296 .addReg(SrcHi)
1297 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001298 if (IsDestLive)
1299 T.addReg(Op0.getReg(), RegState::Implicit);
1300 IsDestLive = true;
1301 }
1302 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001303 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1304 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001305 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001306 .add(Op0)
1307 .add(Op1)
1308 .addReg(SrcHi)
1309 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001310 if (IsDestLive)
1311 T.addReg(Op0.getReg(), RegState::Implicit);
1312 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001313 MBB.erase(MI);
1314 return true;
1315 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001316 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001317 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001318 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001319 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001320 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001321 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001322 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001323 case Hexagon::PS_jmprett:
1324 MI.setDesc(get(Hexagon::J2_jumprt));
1325 return true;
1326 case Hexagon::PS_jmpretf:
1327 MI.setDesc(get(Hexagon::J2_jumprf));
1328 return true;
1329 case Hexagon::PS_jmprettnewpt:
1330 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1331 return true;
1332 case Hexagon::PS_jmpretfnewpt:
1333 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1334 return true;
1335 case Hexagon::PS_jmprettnew:
1336 MI.setDesc(get(Hexagon::J2_jumprtnew));
1337 return true;
1338 case Hexagon::PS_jmpretfnew:
1339 MI.setDesc(get(Hexagon::J2_jumprfnew));
1340 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001341 }
1342
1343 return false;
1344}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001345
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001346// We indicate that we want to reverse the branch by
1347// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001348bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001349 SmallVectorImpl<MachineOperand> &Cond) const {
1350 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001351 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001352 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1353 unsigned opcode = Cond[0].getImm();
1354 //unsigned temp;
1355 assert(get(opcode).isBranch() && "Should be a branching condition.");
1356 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001357 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001358 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1359 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001360 return false;
1361}
1362
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001363void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1364 MachineBasicBlock::iterator MI) const {
1365 DebugLoc DL;
1366 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1367}
1368
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001369bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1370 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001371}
1372
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001373// Returns true if an instruction is predicated irrespective of the predicate
1374// sense. For example, all of the following will return true.
1375// if (p0) R1 = add(R2, R3)
1376// if (!p0) R1 = add(R2, R3)
1377// if (p0.new) R1 = add(R2, R3)
1378// if (!p0.new) R1 = add(R2, R3)
1379// Note: New-value stores are not included here as in the current
1380// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001381bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1382 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001383 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001384}
1385
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001386bool HexagonInstrInfo::PredicateInstruction(
1387 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001388 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1389 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001390 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001391 return false;
1392 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001393 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001394 assert (isPredicable(MI) && "Expected predicable instruction");
1395 bool invertJump = predOpcodeHasNot(Cond);
1396
1397 // We have to predicate MI "in place", i.e. after this function returns,
1398 // MI will need to be transformed into a predicated form. To avoid com-
1399 // plicated manipulations with the operands (handling tied operands,
1400 // etc.), build a new temporary instruction, then overwrite MI with it.
1401
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001402 MachineBasicBlock &B = *MI.getParent();
1403 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001404 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1405 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001406 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001407 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001408 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001409 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1410 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001411 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001412 NOp++;
1413 }
1414
1415 unsigned PredReg, PredRegPos, PredRegFlags;
1416 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1417 (void)GotPredReg;
1418 assert(GotPredReg);
1419 T.addReg(PredReg, PredRegFlags);
1420 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001421 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001422
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001423 MI.setDesc(get(PredOpc));
1424 while (unsigned n = MI.getNumOperands())
1425 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001426 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001427 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001428
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001429 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001430 B.erase(TI);
1431
1432 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1433 MRI.clearKillFlags(PredReg);
1434 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001435}
1436
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001437bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1438 ArrayRef<MachineOperand> Pred2) const {
1439 // TODO: Fix this
1440 return false;
1441}
1442
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001443bool HexagonInstrInfo::DefinesPredicate(
1444 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001445 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001446 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1447 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001448 if (MO.isReg()) {
1449 if (!MO.isDef())
1450 continue;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001451 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1452 if (RC == &Hexagon::PredRegsRegClass) {
1453 Pred.push_back(MO);
1454 return true;
1455 }
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001456 continue;
1457 } else if (MO.isRegMask()) {
1458 for (unsigned PR : Hexagon::PredRegsRegClass) {
1459 if (!MI.modifiesRegister(PR, &HRI))
1460 continue;
1461 Pred.push_back(MO);
1462 return true;
1463 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001464 }
1465 }
1466 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001467}
Andrew Trickd06df962012-02-01 22:13:57 +00001468
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001469bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001470 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001471}
1472
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001473bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1474 const MachineBasicBlock *MBB,
1475 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001476 // Debug info is never a scheduling boundary. It's necessary to be explicit
1477 // due to the special treatment of IT instructions below, otherwise a
1478 // dbg_value followed by an IT will result in the IT instruction being
1479 // considered a scheduling hazard, which is wrong. It should be the actual
1480 // instruction preceding the dbg_value instruction(s), just like it is
1481 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001482 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001483 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001484
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001485 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001486 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001487 // Don't mess around with no return calls.
1488 if (doesNotReturn(MI))
1489 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001490 // If any of the block's successors is a landing pad, this could be a
1491 // throwing call.
1492 for (auto I : MBB->successors())
1493 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001494 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001495 }
1496
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001497 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001498 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001499 return true;
1500
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001501 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1502 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001503
1504 return false;
1505}
1506
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001507/// Measure the specified inline asm to determine an approximation of its
1508/// length.
1509/// Comments (which run till the next SeparatorString or newline) do not
1510/// count as an instruction.
1511/// Any other non-whitespace text is considered an instruction, with
1512/// multiple instructions separated by SeparatorString or newlines.
1513/// Variable-length instructions are not handled here; this function
1514/// may be overloaded in the target code to do that.
1515/// Hexagon counts the number of ##'s and adjust for that many
1516/// constant exenders.
1517unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1518 const MCAsmInfo &MAI) const {
1519 StringRef AStr(Str);
1520 // Count the number of instructions in the asm.
1521 bool atInsnStart = true;
1522 unsigned Length = 0;
1523 for (; *Str; ++Str) {
1524 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1525 strlen(MAI.getSeparatorString())) == 0)
1526 atInsnStart = true;
1527 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1528 Length += MAI.getMaxInstLength();
1529 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001530 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001531 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1532 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001533 atInsnStart = false;
1534 }
1535
1536 // Add to size number of constant extenders seen * 4.
1537 StringRef Occ("##");
1538 Length += AStr.count(Occ)*4;
1539 return Length;
1540}
1541
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001542ScheduleHazardRecognizer*
1543HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1544 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001545 if (UseDFAHazardRec) {
1546 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1547 return new HexagonHazardRecognizer(II, this, HST);
1548 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001549 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1550}
1551
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001552/// \brief For a comparison instruction, return the source registers in
1553/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1554/// compares against in CmpValue. Return true if the comparison instruction
1555/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001556bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1557 unsigned &SrcReg2, int &Mask,
1558 int &Value) const {
1559 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001560
1561 // Set mask and the first source register.
1562 switch (Opc) {
1563 case Hexagon::C2_cmpeq:
1564 case Hexagon::C2_cmpeqp:
1565 case Hexagon::C2_cmpgt:
1566 case Hexagon::C2_cmpgtp:
1567 case Hexagon::C2_cmpgtu:
1568 case Hexagon::C2_cmpgtup:
1569 case Hexagon::C4_cmpneq:
1570 case Hexagon::C4_cmplte:
1571 case Hexagon::C4_cmplteu:
1572 case Hexagon::C2_cmpeqi:
1573 case Hexagon::C2_cmpgti:
1574 case Hexagon::C2_cmpgtui:
1575 case Hexagon::C4_cmpneqi:
1576 case Hexagon::C4_cmplteui:
1577 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001578 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001579 Mask = ~0;
1580 break;
1581 case Hexagon::A4_cmpbeq:
1582 case Hexagon::A4_cmpbgt:
1583 case Hexagon::A4_cmpbgtu:
1584 case Hexagon::A4_cmpbeqi:
1585 case Hexagon::A4_cmpbgti:
1586 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001587 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001588 Mask = 0xFF;
1589 break;
1590 case Hexagon::A4_cmpheq:
1591 case Hexagon::A4_cmphgt:
1592 case Hexagon::A4_cmphgtu:
1593 case Hexagon::A4_cmpheqi:
1594 case Hexagon::A4_cmphgti:
1595 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001596 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001597 Mask = 0xFFFF;
1598 break;
1599 }
1600
1601 // Set the value/second source register.
1602 switch (Opc) {
1603 case Hexagon::C2_cmpeq:
1604 case Hexagon::C2_cmpeqp:
1605 case Hexagon::C2_cmpgt:
1606 case Hexagon::C2_cmpgtp:
1607 case Hexagon::C2_cmpgtu:
1608 case Hexagon::C2_cmpgtup:
1609 case Hexagon::A4_cmpbeq:
1610 case Hexagon::A4_cmpbgt:
1611 case Hexagon::A4_cmpbgtu:
1612 case Hexagon::A4_cmpheq:
1613 case Hexagon::A4_cmphgt:
1614 case Hexagon::A4_cmphgtu:
1615 case Hexagon::C4_cmpneq:
1616 case Hexagon::C4_cmplte:
1617 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001618 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001619 return true;
1620
1621 case Hexagon::C2_cmpeqi:
1622 case Hexagon::C2_cmpgtui:
1623 case Hexagon::C2_cmpgti:
1624 case Hexagon::C4_cmpneqi:
1625 case Hexagon::C4_cmplteui:
1626 case Hexagon::C4_cmpltei:
1627 case Hexagon::A4_cmpbeqi:
1628 case Hexagon::A4_cmpbgti:
1629 case Hexagon::A4_cmpbgtui:
1630 case Hexagon::A4_cmpheqi:
1631 case Hexagon::A4_cmphgti:
1632 case Hexagon::A4_cmphgtui:
1633 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001634 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001635 return true;
1636 }
1637
1638 return false;
1639}
1640
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001641unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001642 const MachineInstr &MI,
1643 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001644 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001645}
1646
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001647
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001648DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1649 const TargetSubtargetInfo &STI) const {
1650 const InstrItineraryData *II = STI.getInstrItineraryData();
1651 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1652}
1653
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001654// Inspired by this pair:
1655// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1656// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1657// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001658bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1659 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001660 int OffsetA = 0, OffsetB = 0;
1661 unsigned SizeA = 0, SizeB = 0;
1662
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001663 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1664 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001665 return false;
1666
1667 // Instructions that are pure loads, not loads and stores like memops are not
1668 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001669 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001670 return true;
1671
1672 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001673 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001674 if (!BaseRegA || !SizeA)
1675 return false;
1676
1677 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001678 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001679 if (!BaseRegB || !SizeB)
1680 return false;
1681
1682 if (BaseRegA != BaseRegB)
1683 return false;
1684
1685 // This is a mem access with the same base register and known offsets from it.
1686 // Reason about it.
1687 if (OffsetA > OffsetB) {
1688 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1689 return (SizeB <= offDiff);
1690 } else if (OffsetA < OffsetB) {
1691 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1692 return (SizeA <= offDiff);
1693 }
1694
1695 return false;
1696}
1697
Brendon Cahoon254f8892016-07-29 16:44:44 +00001698/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001699bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001700 int &Value) const {
1701 if (isPostIncrement(MI)) {
1702 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001703 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001704 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001705 if (MI.getOpcode() == Hexagon::A2_addi) {
1706 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001707 return true;
1708 }
1709
1710 return false;
1711}
1712
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001713unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001714 MachineRegisterInfo &MRI = MF->getRegInfo();
1715 const TargetRegisterClass *TRC;
1716 if (VT == MVT::i1) {
1717 TRC = &Hexagon::PredRegsRegClass;
1718 } else if (VT == MVT::i32 || VT == MVT::f32) {
1719 TRC = &Hexagon::IntRegsRegClass;
1720 } else if (VT == MVT::i64 || VT == MVT::f64) {
1721 TRC = &Hexagon::DoubleRegsRegClass;
1722 } else {
1723 llvm_unreachable("Cannot handle this register class");
1724 }
1725
1726 unsigned NewReg = MRI.createVirtualRegister(TRC);
1727 return NewReg;
1728}
1729
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001730bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001731 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1732}
1733
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001734bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1735 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001736 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1737}
1738
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001739bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1740 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001741 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1742 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1743
1744 if (!(isTC1(MI))
1745 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001746 && !(MI.getDesc().mayLoad())
1747 && !(MI.getDesc().mayStore())
1748 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1749 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001750 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001751 && !(MI.isBranch())
1752 && !(MI.isReturn())
1753 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001754 return true;
1755
1756 return false;
1757}
1758
Sanjay Patele4b9f502015-12-07 19:21:39 +00001759// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001760bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001761 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001762}
1763
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001764bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1765 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001766 isConditionalTransfer(MI) ||
1767 isConditionalALU32(MI) ||
1768 isConditionalLoad(MI) ||
1769 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001770 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1771 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001772}
1773
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001774bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1775 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001776 case Hexagon::A2_paddf:
1777 case Hexagon::A2_paddfnew:
1778 case Hexagon::A2_paddif:
1779 case Hexagon::A2_paddifnew:
1780 case Hexagon::A2_paddit:
1781 case Hexagon::A2_padditnew:
1782 case Hexagon::A2_paddt:
1783 case Hexagon::A2_paddtnew:
1784 case Hexagon::A2_pandf:
1785 case Hexagon::A2_pandfnew:
1786 case Hexagon::A2_pandt:
1787 case Hexagon::A2_pandtnew:
1788 case Hexagon::A2_porf:
1789 case Hexagon::A2_porfnew:
1790 case Hexagon::A2_port:
1791 case Hexagon::A2_portnew:
1792 case Hexagon::A2_psubf:
1793 case Hexagon::A2_psubfnew:
1794 case Hexagon::A2_psubt:
1795 case Hexagon::A2_psubtnew:
1796 case Hexagon::A2_pxorf:
1797 case Hexagon::A2_pxorfnew:
1798 case Hexagon::A2_pxort:
1799 case Hexagon::A2_pxortnew:
1800 case Hexagon::A4_paslhf:
1801 case Hexagon::A4_paslhfnew:
1802 case Hexagon::A4_paslht:
1803 case Hexagon::A4_paslhtnew:
1804 case Hexagon::A4_pasrhf:
1805 case Hexagon::A4_pasrhfnew:
1806 case Hexagon::A4_pasrht:
1807 case Hexagon::A4_pasrhtnew:
1808 case Hexagon::A4_psxtbf:
1809 case Hexagon::A4_psxtbfnew:
1810 case Hexagon::A4_psxtbt:
1811 case Hexagon::A4_psxtbtnew:
1812 case Hexagon::A4_psxthf:
1813 case Hexagon::A4_psxthfnew:
1814 case Hexagon::A4_psxtht:
1815 case Hexagon::A4_psxthtnew:
1816 case Hexagon::A4_pzxtbf:
1817 case Hexagon::A4_pzxtbfnew:
1818 case Hexagon::A4_pzxtbt:
1819 case Hexagon::A4_pzxtbtnew:
1820 case Hexagon::A4_pzxthf:
1821 case Hexagon::A4_pzxthfnew:
1822 case Hexagon::A4_pzxtht:
1823 case Hexagon::A4_pzxthtnew:
1824 case Hexagon::C2_ccombinewf:
1825 case Hexagon::C2_ccombinewt:
1826 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001827 }
1828 return false;
1829}
1830
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001831// FIXME - Function name and it's functionality don't match.
1832// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001833bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1834 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001835 return false;
1836
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001837 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001838 // Instruction with valid predicated-new opcode can be promoted to .new.
1839 return PNewOpcode >= 0;
1840}
1841
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001842// Returns true if an instruction is a conditional store.
1843//
1844// Note: It doesn't include conditional new-value stores as they can't be
1845// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001846bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1847 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001848 default: return false;
1849 case Hexagon::S4_storeirbt_io:
1850 case Hexagon::S4_storeirbf_io:
1851 case Hexagon::S4_pstorerbt_rr:
1852 case Hexagon::S4_pstorerbf_rr:
1853 case Hexagon::S2_pstorerbt_io:
1854 case Hexagon::S2_pstorerbf_io:
1855 case Hexagon::S2_pstorerbt_pi:
1856 case Hexagon::S2_pstorerbf_pi:
1857 case Hexagon::S2_pstorerdt_io:
1858 case Hexagon::S2_pstorerdf_io:
1859 case Hexagon::S4_pstorerdt_rr:
1860 case Hexagon::S4_pstorerdf_rr:
1861 case Hexagon::S2_pstorerdt_pi:
1862 case Hexagon::S2_pstorerdf_pi:
1863 case Hexagon::S2_pstorerht_io:
1864 case Hexagon::S2_pstorerhf_io:
1865 case Hexagon::S4_storeirht_io:
1866 case Hexagon::S4_storeirhf_io:
1867 case Hexagon::S4_pstorerht_rr:
1868 case Hexagon::S4_pstorerhf_rr:
1869 case Hexagon::S2_pstorerht_pi:
1870 case Hexagon::S2_pstorerhf_pi:
1871 case Hexagon::S2_pstorerit_io:
1872 case Hexagon::S2_pstorerif_io:
1873 case Hexagon::S4_storeirit_io:
1874 case Hexagon::S4_storeirif_io:
1875 case Hexagon::S4_pstorerit_rr:
1876 case Hexagon::S4_pstorerif_rr:
1877 case Hexagon::S2_pstorerit_pi:
1878 case Hexagon::S2_pstorerif_pi:
1879
1880 // V4 global address store before promoting to dot new.
1881 case Hexagon::S4_pstorerdt_abs:
1882 case Hexagon::S4_pstorerdf_abs:
1883 case Hexagon::S4_pstorerbt_abs:
1884 case Hexagon::S4_pstorerbf_abs:
1885 case Hexagon::S4_pstorerht_abs:
1886 case Hexagon::S4_pstorerhf_abs:
1887 case Hexagon::S4_pstorerit_abs:
1888 case Hexagon::S4_pstorerif_abs:
1889 return true;
1890
1891 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1892 // from the "Conditional Store" list. Because a predicated new value store
1893 // would NOT be promoted to a double dot new store.
1894 // This function returns yes for those stores that are predicated but not
1895 // yet promoted to predicate dot new instructions.
1896 }
1897}
1898
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001899bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1900 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001901 case Hexagon::A2_tfrt:
1902 case Hexagon::A2_tfrf:
1903 case Hexagon::C2_cmoveit:
1904 case Hexagon::C2_cmoveif:
1905 case Hexagon::A2_tfrtnew:
1906 case Hexagon::A2_tfrfnew:
1907 case Hexagon::C2_cmovenewit:
1908 case Hexagon::C2_cmovenewif:
1909 case Hexagon::A2_tfrpt:
1910 case Hexagon::A2_tfrpf:
1911 return true;
1912
1913 default:
1914 return false;
1915 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001916 return false;
1917}
1918
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001919// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1920// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001921bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1922 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001923 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1924 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001925 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001926
1927 unsigned isExtendable =
1928 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1929 if (!isExtendable)
1930 return false;
1931
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001932 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001933 return false;
1934
1935 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001936 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001937 // Use MO operand flags to determine if MO
1938 // has the HMOTF_ConstExtended flag set.
1939 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001940 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001941 // If this is a Machine BB address we are talking about, and it is
1942 // not marked as extended, say so.
1943 if (MO.isMBB())
1944 return false;
1945
1946 // We could be using an instruction with an extendable immediate and shoehorn
1947 // a global address into it. If it is a global address it will be constant
1948 // extended. We do this for COMBINE.
1949 // We currently only handle isGlobal() because it is the only kind of
1950 // object we are going to end up with here for now.
1951 // In the future we probably should add isSymbol(), etc.
1952 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001953 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001954 return true;
1955
1956 // If the extendable operand is not 'Immediate' type, the instruction should
1957 // have 'isExtended' flag set.
1958 assert(MO.isImm() && "Extendable operand must be Immediate type");
1959
1960 int MinValue = getMinValue(MI);
1961 int MaxValue = getMaxValue(MI);
1962 int ImmValue = MO.getImm();
1963
1964 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001965}
1966
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001967bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1968 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001969 case Hexagon::L4_return :
1970 case Hexagon::L4_return_t :
1971 case Hexagon::L4_return_f :
1972 case Hexagon::L4_return_tnew_pnt :
1973 case Hexagon::L4_return_fnew_pnt :
1974 case Hexagon::L4_return_tnew_pt :
1975 case Hexagon::L4_return_fnew_pt :
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00001976 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001977 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001978 return false;
1979}
1980
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001981// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001982bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1983 const MachineInstr &ConsMI) const {
1984 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001985 return false;
1986
1987 auto &HRI = getRegisterInfo();
1988
1989 SmallVector<unsigned, 4> DefsA;
1990 SmallVector<unsigned, 4> DefsB;
1991 SmallVector<unsigned, 8> UsesA;
1992 SmallVector<unsigned, 8> UsesB;
1993
1994 parseOperands(ProdMI, DefsA, UsesA);
1995 parseOperands(ConsMI, DefsB, UsesB);
1996
1997 for (auto &RegA : DefsA)
1998 for (auto &RegB : UsesB) {
1999 // True data dependency.
2000 if (RegA == RegB)
2001 return true;
2002
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002003 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002004 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
2005 if (RegB == *SubRegs)
2006 return true;
2007
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002008 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002009 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
2010 if (RegA == *SubRegs)
2011 return true;
2012 }
2013
2014 return false;
2015}
2016
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002017// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002018bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2019 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002020 case Hexagon::V6_vL32b_cur_pi:
2021 case Hexagon::V6_vL32b_cur_ai:
2022 case Hexagon::V6_vL32b_cur_pi_128B:
2023 case Hexagon::V6_vL32b_cur_ai_128B:
2024 return true;
2025 }
2026 return false;
2027}
2028
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002029// Returns true, if any one of the operands is a dot new
2030// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002031bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2032 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002033 return true;
2034
2035 return false;
2036}
2037
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002038/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002039bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2040 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002041 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2042 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2043 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2044}
2045
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002046bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2047 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002048 return true;
2049
2050 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002051 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002052 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002053}
2054
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002055bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2056 return (Opcode == Hexagon::ENDLOOP0 ||
2057 Opcode == Hexagon::ENDLOOP1);
2058}
2059
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002060bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2061 switch(OpType) {
2062 case MachineOperand::MO_MachineBasicBlock:
2063 case MachineOperand::MO_GlobalAddress:
2064 case MachineOperand::MO_ExternalSymbol:
2065 case MachineOperand::MO_JumpTableIndex:
2066 case MachineOperand::MO_ConstantPoolIndex:
2067 case MachineOperand::MO_BlockAddress:
2068 return true;
2069 default:
2070 return false;
2071 }
2072}
2073
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002074bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2075 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002076 const uint64_t F = MID.TSFlags;
2077 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2078 return true;
2079
2080 // TODO: This is largely obsolete now. Will need to be removed
2081 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002082 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002083 // PS_fi and PS_fia remain special cases.
2084 case Hexagon::PS_fi:
2085 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002086 return true;
2087 default:
2088 return false;
2089 }
2090 return false;
2091}
2092
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002093// This returns true in two cases:
2094// - The OP code itself indicates that this is an extended instruction.
2095// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002096bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002097 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002098 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002099 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2100 return true;
2101 // Use MO operand flags to determine if one of MI's operands
2102 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002103 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
2104 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002105 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2106 return true;
2107 }
2108 return false;
2109}
2110
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002111bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2112 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002113 const uint64_t F = get(Opcode).TSFlags;
2114 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2115}
2116
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002117// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002118bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2119 const MachineInstr &J) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002120 if (!isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002121 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002122 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002123 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002124 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002125}
2126
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002127bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2128 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002129 case Hexagon::J2_callr :
2130 case Hexagon::J2_callrf :
2131 case Hexagon::J2_callrt :
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00002132 case Hexagon::PS_call_nr :
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002133 return true;
2134 }
2135 return false;
2136}
2137
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002138bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2139 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002140 case Hexagon::L4_return :
2141 case Hexagon::L4_return_t :
2142 case Hexagon::L4_return_f :
2143 case Hexagon::L4_return_fnew_pnt :
2144 case Hexagon::L4_return_fnew_pt :
2145 case Hexagon::L4_return_tnew_pnt :
2146 case Hexagon::L4_return_tnew_pt :
2147 return true;
2148 }
2149 return false;
2150}
2151
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002152bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2153 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002154 case Hexagon::J2_jumpr :
2155 case Hexagon::J2_jumprt :
2156 case Hexagon::J2_jumprf :
2157 case Hexagon::J2_jumprtnewpt :
2158 case Hexagon::J2_jumprfnewpt :
2159 case Hexagon::J2_jumprtnew :
2160 case Hexagon::J2_jumprfnew :
2161 return true;
2162 }
2163 return false;
2164}
2165
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002166// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002167// Use abs estimate as oppose to the exact number.
2168// TODO: This will need to be changed to use MC level
2169// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002170bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002171 unsigned offset) const {
2172 // This selection of jump instructions matches to that what
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002173 // analyzeBranch can parse, plus NVJ.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002174 if (isNewValueJump(MI)) // r9:2
2175 return isInt<11>(offset);
2176
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002177 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002178 // Still missing Jump to address condition on register value.
2179 default:
2180 return false;
2181 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2182 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002183 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002184 return isInt<24>(offset);
2185 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2186 case Hexagon::J2_jumpf:
2187 case Hexagon::J2_jumptnew:
2188 case Hexagon::J2_jumptnewpt:
2189 case Hexagon::J2_jumpfnew:
2190 case Hexagon::J2_jumpfnewpt:
2191 case Hexagon::J2_callt:
2192 case Hexagon::J2_callf:
2193 return isInt<17>(offset);
2194 case Hexagon::J2_loop0i:
2195 case Hexagon::J2_loop0iext:
2196 case Hexagon::J2_loop0r:
2197 case Hexagon::J2_loop0rext:
2198 case Hexagon::J2_loop1i:
2199 case Hexagon::J2_loop1iext:
2200 case Hexagon::J2_loop1r:
2201 case Hexagon::J2_loop1rext:
2202 return isInt<9>(offset);
2203 // TODO: Add all the compound branches here. Can we do this in Relation model?
2204 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2205 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2206 return isInt<11>(offset);
2207 }
2208}
2209
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002210bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2211 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002212 bool isLate = isLateResultInstr(LRMI);
2213 bool isEarly = isEarlySourceInstr(ESMI);
2214
2215 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002216 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002217 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002218 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002219
2220 if (isLate && isEarly) {
2221 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2222 return true;
2223 }
2224
2225 return false;
2226}
2227
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002228bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2229 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002230 case TargetOpcode::EXTRACT_SUBREG:
2231 case TargetOpcode::INSERT_SUBREG:
2232 case TargetOpcode::SUBREG_TO_REG:
2233 case TargetOpcode::REG_SEQUENCE:
2234 case TargetOpcode::IMPLICIT_DEF:
2235 case TargetOpcode::COPY:
2236 case TargetOpcode::INLINEASM:
2237 case TargetOpcode::PHI:
2238 return false;
2239 default:
2240 break;
2241 }
2242
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002243 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002244 return !is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002245}
2246
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002247bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002248 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2249 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002250 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002251}
2252
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002253bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2254 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002255 return Opcode == Hexagon::J2_loop0i ||
2256 Opcode == Hexagon::J2_loop0r ||
2257 Opcode == Hexagon::J2_loop0iext ||
2258 Opcode == Hexagon::J2_loop0rext ||
2259 Opcode == Hexagon::J2_loop1i ||
2260 Opcode == Hexagon::J2_loop1r ||
2261 Opcode == Hexagon::J2_loop1iext ||
2262 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002263}
2264
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002265bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2266 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002267 default: return false;
2268 case Hexagon::L4_iadd_memopw_io :
2269 case Hexagon::L4_isub_memopw_io :
2270 case Hexagon::L4_add_memopw_io :
2271 case Hexagon::L4_sub_memopw_io :
2272 case Hexagon::L4_and_memopw_io :
2273 case Hexagon::L4_or_memopw_io :
2274 case Hexagon::L4_iadd_memoph_io :
2275 case Hexagon::L4_isub_memoph_io :
2276 case Hexagon::L4_add_memoph_io :
2277 case Hexagon::L4_sub_memoph_io :
2278 case Hexagon::L4_and_memoph_io :
2279 case Hexagon::L4_or_memoph_io :
2280 case Hexagon::L4_iadd_memopb_io :
2281 case Hexagon::L4_isub_memopb_io :
2282 case Hexagon::L4_add_memopb_io :
2283 case Hexagon::L4_sub_memopb_io :
2284 case Hexagon::L4_and_memopb_io :
2285 case Hexagon::L4_or_memopb_io :
2286 case Hexagon::L4_ior_memopb_io:
2287 case Hexagon::L4_ior_memoph_io:
2288 case Hexagon::L4_ior_memopw_io:
2289 case Hexagon::L4_iand_memopb_io:
2290 case Hexagon::L4_iand_memoph_io:
2291 case Hexagon::L4_iand_memopw_io:
2292 return true;
2293 }
2294 return false;
2295}
2296
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002297bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2298 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002299 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2300}
2301
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002302bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2303 const uint64_t F = get(Opcode).TSFlags;
2304 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2305}
2306
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002307bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002308 return isNewValueJump(MI) || isNewValueStore(MI);
2309}
2310
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002311bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2312 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002313}
2314
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002315bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2316 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2317}
2318
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002319bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2320 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002321 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2322}
2323
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002324bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2325 const uint64_t F = get(Opcode).TSFlags;
2326 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2327}
2328
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002329// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002330bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002331 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002332 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002333 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2334 == OperandNum;
2335}
2336
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002337bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2338 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002339 assert(isPredicated(MI));
2340 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2341}
2342
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002343bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2344 const uint64_t F = get(Opcode).TSFlags;
2345 assert(isPredicated(Opcode));
2346 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2347}
2348
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002349bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2350 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002351 return !((F >> HexagonII::PredicatedFalsePos) &
2352 HexagonII::PredicatedFalseMask);
2353}
2354
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002355bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2356 const uint64_t F = get(Opcode).TSFlags;
2357 // Make sure that the instruction is predicated.
2358 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2359 return !((F >> HexagonII::PredicatedFalsePos) &
2360 HexagonII::PredicatedFalseMask);
2361}
2362
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002363bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2364 const uint64_t F = get(Opcode).TSFlags;
2365 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2366}
2367
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002368bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2369 const uint64_t F = get(Opcode).TSFlags;
2370 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2371}
2372
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002373bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2374 const uint64_t F = get(Opcode).TSFlags;
2375 assert(get(Opcode).isBranch() &&
2376 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2377 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2378}
2379
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002380bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2381 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2382 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2383 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2384 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002385}
2386
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002387bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2388 switch (MI.getOpcode()) {
2389 // Byte
2390 case Hexagon::L2_loadrb_io:
2391 case Hexagon::L4_loadrb_ur:
2392 case Hexagon::L4_loadrb_ap:
2393 case Hexagon::L2_loadrb_pr:
2394 case Hexagon::L2_loadrb_pbr:
2395 case Hexagon::L2_loadrb_pi:
2396 case Hexagon::L2_loadrb_pci:
2397 case Hexagon::L2_loadrb_pcr:
2398 case Hexagon::L2_loadbsw2_io:
2399 case Hexagon::L4_loadbsw2_ur:
2400 case Hexagon::L4_loadbsw2_ap:
2401 case Hexagon::L2_loadbsw2_pr:
2402 case Hexagon::L2_loadbsw2_pbr:
2403 case Hexagon::L2_loadbsw2_pi:
2404 case Hexagon::L2_loadbsw2_pci:
2405 case Hexagon::L2_loadbsw2_pcr:
2406 case Hexagon::L2_loadbsw4_io:
2407 case Hexagon::L4_loadbsw4_ur:
2408 case Hexagon::L4_loadbsw4_ap:
2409 case Hexagon::L2_loadbsw4_pr:
2410 case Hexagon::L2_loadbsw4_pbr:
2411 case Hexagon::L2_loadbsw4_pi:
2412 case Hexagon::L2_loadbsw4_pci:
2413 case Hexagon::L2_loadbsw4_pcr:
2414 case Hexagon::L4_loadrb_rr:
2415 case Hexagon::L2_ploadrbt_io:
2416 case Hexagon::L2_ploadrbt_pi:
2417 case Hexagon::L2_ploadrbf_io:
2418 case Hexagon::L2_ploadrbf_pi:
2419 case Hexagon::L2_ploadrbtnew_io:
2420 case Hexagon::L2_ploadrbfnew_io:
2421 case Hexagon::L4_ploadrbt_rr:
2422 case Hexagon::L4_ploadrbf_rr:
2423 case Hexagon::L4_ploadrbtnew_rr:
2424 case Hexagon::L4_ploadrbfnew_rr:
2425 case Hexagon::L2_ploadrbtnew_pi:
2426 case Hexagon::L2_ploadrbfnew_pi:
2427 case Hexagon::L4_ploadrbt_abs:
2428 case Hexagon::L4_ploadrbf_abs:
2429 case Hexagon::L4_ploadrbtnew_abs:
2430 case Hexagon::L4_ploadrbfnew_abs:
2431 case Hexagon::L2_loadrbgp:
2432 // Half
2433 case Hexagon::L2_loadrh_io:
2434 case Hexagon::L4_loadrh_ur:
2435 case Hexagon::L4_loadrh_ap:
2436 case Hexagon::L2_loadrh_pr:
2437 case Hexagon::L2_loadrh_pbr:
2438 case Hexagon::L2_loadrh_pi:
2439 case Hexagon::L2_loadrh_pci:
2440 case Hexagon::L2_loadrh_pcr:
2441 case Hexagon::L4_loadrh_rr:
2442 case Hexagon::L2_ploadrht_io:
2443 case Hexagon::L2_ploadrht_pi:
2444 case Hexagon::L2_ploadrhf_io:
2445 case Hexagon::L2_ploadrhf_pi:
2446 case Hexagon::L2_ploadrhtnew_io:
2447 case Hexagon::L2_ploadrhfnew_io:
2448 case Hexagon::L4_ploadrht_rr:
2449 case Hexagon::L4_ploadrhf_rr:
2450 case Hexagon::L4_ploadrhtnew_rr:
2451 case Hexagon::L4_ploadrhfnew_rr:
2452 case Hexagon::L2_ploadrhtnew_pi:
2453 case Hexagon::L2_ploadrhfnew_pi:
2454 case Hexagon::L4_ploadrht_abs:
2455 case Hexagon::L4_ploadrhf_abs:
2456 case Hexagon::L4_ploadrhtnew_abs:
2457 case Hexagon::L4_ploadrhfnew_abs:
2458 case Hexagon::L2_loadrhgp:
2459 return true;
2460 default:
2461 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002462 }
2463}
2464
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002465bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2466 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002467 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2468}
2469
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002470bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2471 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002472 case Hexagon::STriw_pred :
2473 case Hexagon::LDriw_pred :
2474 return true;
2475 default:
2476 return false;
2477 }
2478}
2479
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002480bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2481 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002482 return false;
2483
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002484 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002485 if (Op.isGlobal() || Op.isSymbol())
2486 return true;
2487 return false;
2488}
2489
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002490// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002491bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2492 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002493 return is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002494}
2495
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002496bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2497 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002498 return is_TC2(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002499}
2500
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002501bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2502 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002503 return is_TC2early(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002504}
2505
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002506bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2507 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002508 return is_TC4x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002509}
2510
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002511// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002512bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2513 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002514 if (mayBeCurLoad(MI1)) {
2515 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002516 unsigned DstReg = MI1.getOperand(0).getReg();
2517 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002518 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002519 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002520 return true;
2521 }
2522 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002523 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2524 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2525 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002526 return true;
2527 return false;
2528}
2529
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002530bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002531 const uint64_t V = getType(MI);
2532 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2533}
2534
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002535// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2536//
2537bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2538 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2539 VT == MVT::v32i16 || VT == MVT::v64i8) {
2540 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2541 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2542 (Offset & 0x3f) == 0);
2543 }
2544 // 128B
2545 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2546 VT == MVT::v64i16 || VT == MVT::v128i8) {
2547 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2548 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2549 (Offset & 0x7f) == 0);
2550 }
2551 if (VT == MVT::i64) {
2552 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2553 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2554 (Offset & 0x7) == 0);
2555 }
2556 if (VT == MVT::i32) {
2557 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2558 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2559 (Offset & 0x3) == 0);
2560 }
2561 if (VT == MVT::i16) {
2562 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2563 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2564 (Offset & 0x1) == 0);
2565 }
2566 if (VT == MVT::i8) {
2567 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2568 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2569 }
2570 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002571}
2572
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002573bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2574 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002575 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002576 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002577 // inserted to calculate the final address. Due to this reason, the function
2578 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002579 // We used to assert if the offset was not properly aligned, however,
2580 // there are cases where a misaligned pointer recast can cause this
2581 // problem, and we need to allow for it. The front end warns of such
2582 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002583
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002584 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002585 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002586 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002587 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002588 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002589 case Hexagon::V6_vL32b_ai:
2590 case Hexagon::V6_vS32b_ai:
2591 case Hexagon::V6_vL32Ub_ai:
2592 case Hexagon::V6_vS32Ub_ai:
2593 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2594 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2595
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002596 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002597 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002598 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002599 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002600 case Hexagon::V6_vL32b_ai_128B:
2601 case Hexagon::V6_vS32b_ai_128B:
2602 case Hexagon::V6_vL32Ub_ai_128B:
2603 case Hexagon::V6_vS32Ub_ai_128B:
2604 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2605 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2606
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002607 case Hexagon::J2_loop0i:
2608 case Hexagon::J2_loop1i:
2609 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002610
2611 case Hexagon::S4_storeirb_io:
2612 case Hexagon::S4_storeirbt_io:
2613 case Hexagon::S4_storeirbf_io:
2614 return isUInt<6>(Offset);
2615
2616 case Hexagon::S4_storeirh_io:
2617 case Hexagon::S4_storeirht_io:
2618 case Hexagon::S4_storeirhf_io:
2619 return isShiftedUInt<6,1>(Offset);
2620
2621 case Hexagon::S4_storeiri_io:
2622 case Hexagon::S4_storeirit_io:
2623 case Hexagon::S4_storeirif_io:
2624 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002625 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002626
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002627 if (Extend)
2628 return true;
2629
2630 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002631 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002632 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002633 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2634 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2635
Colin LeMahieu947cd702014-12-23 20:44:59 +00002636 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002637 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002638 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2639 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2640
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002641 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002642 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002643 case Hexagon::S2_storerh_io:
Krzysztof Parzyszekd10df492017-05-03 15:36:51 +00002644 case Hexagon::S2_storerf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002645 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2646 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2647
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002648 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002649 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002650 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002651 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2652 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2653
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002654 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002655 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2656 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2657
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002658 case Hexagon::L4_iadd_memopw_io :
2659 case Hexagon::L4_isub_memopw_io :
2660 case Hexagon::L4_add_memopw_io :
2661 case Hexagon::L4_sub_memopw_io :
2662 case Hexagon::L4_and_memopw_io :
2663 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002664 return (0 <= Offset && Offset <= 255);
2665
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002666 case Hexagon::L4_iadd_memoph_io :
2667 case Hexagon::L4_isub_memoph_io :
2668 case Hexagon::L4_add_memoph_io :
2669 case Hexagon::L4_sub_memoph_io :
2670 case Hexagon::L4_and_memoph_io :
2671 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002672 return (0 <= Offset && Offset <= 127);
2673
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002674 case Hexagon::L4_iadd_memopb_io :
2675 case Hexagon::L4_isub_memopb_io :
2676 case Hexagon::L4_add_memopb_io :
2677 case Hexagon::L4_sub_memopb_io :
2678 case Hexagon::L4_and_memopb_io :
2679 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002680 return (0 <= Offset && Offset <= 63);
2681
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002682 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002683 // any size. Later pass knows how to handle it.
2684 case Hexagon::STriw_pred:
2685 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002686 case Hexagon::STriw_mod:
2687 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002688 return true;
2689
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002690 case Hexagon::PS_fi:
2691 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002692 case Hexagon::INLINEASM:
2693 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002694
2695 case Hexagon::L2_ploadrbt_io:
2696 case Hexagon::L2_ploadrbf_io:
2697 case Hexagon::L2_ploadrubt_io:
2698 case Hexagon::L2_ploadrubf_io:
2699 case Hexagon::S2_pstorerbt_io:
2700 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002701 return isUInt<6>(Offset);
2702
2703 case Hexagon::L2_ploadrht_io:
2704 case Hexagon::L2_ploadrhf_io:
2705 case Hexagon::L2_ploadruht_io:
2706 case Hexagon::L2_ploadruhf_io:
2707 case Hexagon::S2_pstorerht_io:
2708 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002709 return isShiftedUInt<6,1>(Offset);
2710
2711 case Hexagon::L2_ploadrit_io:
2712 case Hexagon::L2_ploadrif_io:
2713 case Hexagon::S2_pstorerit_io:
2714 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002715 return isShiftedUInt<6,2>(Offset);
2716
2717 case Hexagon::L2_ploadrdt_io:
2718 case Hexagon::L2_ploadrdf_io:
2719 case Hexagon::S2_pstorerdt_io:
2720 case Hexagon::S2_pstorerdf_io:
2721 return isShiftedUInt<6,3>(Offset);
2722 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002723
Benjamin Kramerb6684012011-12-27 11:41:05 +00002724 llvm_unreachable("No offset range is defined for this opcode. "
2725 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002726}
2727
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002728bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002729 return isHVXVec(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002730}
2731
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002732bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2733 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002734 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2735 return
2736 V == HexagonII::TypeCVI_VA ||
2737 V == HexagonII::TypeCVI_VA_DV;
2738}
Andrew Trickd06df962012-02-01 22:13:57 +00002739
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002740bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2741 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002742 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2743 return true;
2744
2745 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2746 return true;
2747
2748 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002749 return true;
2750
2751 return false;
2752}
Jyotsna Verma84256432013-03-01 17:37:13 +00002753
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002754bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2755 switch (MI.getOpcode()) {
2756 // Byte
2757 case Hexagon::L2_loadrub_io:
2758 case Hexagon::L4_loadrub_ur:
2759 case Hexagon::L4_loadrub_ap:
2760 case Hexagon::L2_loadrub_pr:
2761 case Hexagon::L2_loadrub_pbr:
2762 case Hexagon::L2_loadrub_pi:
2763 case Hexagon::L2_loadrub_pci:
2764 case Hexagon::L2_loadrub_pcr:
2765 case Hexagon::L2_loadbzw2_io:
2766 case Hexagon::L4_loadbzw2_ur:
2767 case Hexagon::L4_loadbzw2_ap:
2768 case Hexagon::L2_loadbzw2_pr:
2769 case Hexagon::L2_loadbzw2_pbr:
2770 case Hexagon::L2_loadbzw2_pi:
2771 case Hexagon::L2_loadbzw2_pci:
2772 case Hexagon::L2_loadbzw2_pcr:
2773 case Hexagon::L2_loadbzw4_io:
2774 case Hexagon::L4_loadbzw4_ur:
2775 case Hexagon::L4_loadbzw4_ap:
2776 case Hexagon::L2_loadbzw4_pr:
2777 case Hexagon::L2_loadbzw4_pbr:
2778 case Hexagon::L2_loadbzw4_pi:
2779 case Hexagon::L2_loadbzw4_pci:
2780 case Hexagon::L2_loadbzw4_pcr:
2781 case Hexagon::L4_loadrub_rr:
2782 case Hexagon::L2_ploadrubt_io:
2783 case Hexagon::L2_ploadrubt_pi:
2784 case Hexagon::L2_ploadrubf_io:
2785 case Hexagon::L2_ploadrubf_pi:
2786 case Hexagon::L2_ploadrubtnew_io:
2787 case Hexagon::L2_ploadrubfnew_io:
2788 case Hexagon::L4_ploadrubt_rr:
2789 case Hexagon::L4_ploadrubf_rr:
2790 case Hexagon::L4_ploadrubtnew_rr:
2791 case Hexagon::L4_ploadrubfnew_rr:
2792 case Hexagon::L2_ploadrubtnew_pi:
2793 case Hexagon::L2_ploadrubfnew_pi:
2794 case Hexagon::L4_ploadrubt_abs:
2795 case Hexagon::L4_ploadrubf_abs:
2796 case Hexagon::L4_ploadrubtnew_abs:
2797 case Hexagon::L4_ploadrubfnew_abs:
2798 case Hexagon::L2_loadrubgp:
2799 // Half
2800 case Hexagon::L2_loadruh_io:
2801 case Hexagon::L4_loadruh_ur:
2802 case Hexagon::L4_loadruh_ap:
2803 case Hexagon::L2_loadruh_pr:
2804 case Hexagon::L2_loadruh_pbr:
2805 case Hexagon::L2_loadruh_pi:
2806 case Hexagon::L2_loadruh_pci:
2807 case Hexagon::L2_loadruh_pcr:
2808 case Hexagon::L4_loadruh_rr:
2809 case Hexagon::L2_ploadruht_io:
2810 case Hexagon::L2_ploadruht_pi:
2811 case Hexagon::L2_ploadruhf_io:
2812 case Hexagon::L2_ploadruhf_pi:
2813 case Hexagon::L2_ploadruhtnew_io:
2814 case Hexagon::L2_ploadruhfnew_io:
2815 case Hexagon::L4_ploadruht_rr:
2816 case Hexagon::L4_ploadruhf_rr:
2817 case Hexagon::L4_ploadruhtnew_rr:
2818 case Hexagon::L4_ploadruhfnew_rr:
2819 case Hexagon::L2_ploadruhtnew_pi:
2820 case Hexagon::L2_ploadruhfnew_pi:
2821 case Hexagon::L4_ploadruht_abs:
2822 case Hexagon::L4_ploadruhf_abs:
2823 case Hexagon::L4_ploadruhtnew_abs:
2824 case Hexagon::L4_ploadruhfnew_abs:
2825 case Hexagon::L2_loadruhgp:
2826 return true;
2827 default:
2828 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002829 }
2830}
2831
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002832// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002833bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2834 const MachineInstr &MI2) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002835 if (isHVXVec(MI1) && isHVXVec(MI2))
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002836 if (!isVecUsableNextPacket(MI1, MI2))
2837 return true;
2838 return false;
2839}
2840
Brendon Cahoon254f8892016-07-29 16:44:44 +00002841/// \brief Get the base register and byte offset of a load/store instr.
2842bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2843 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2844 const {
2845 unsigned AccessSize = 0;
2846 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002847 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002848 Offset = OffsetVal;
2849 return BaseReg != 0;
2850}
2851
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002852/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002853bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2854 const MachineInstr &Second) const {
Krzysztof Parzyszek4763c2d2017-05-03 15:33:09 +00002855 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2856 const MachineOperand &Op = Second.getOperand(0);
2857 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2858 return true;
2859 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002860 if (DisableNVSchedule)
2861 return false;
2862 if (mayBeNewStore(Second)) {
2863 // Make sure the definition of the first instruction is the value being
2864 // stored.
2865 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002866 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002867 if (!Stored.isReg())
2868 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002869 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2870 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002871 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2872 return true;
2873 }
2874 }
2875 return false;
2876}
2877
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002878bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2879 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002880 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002881}
2882
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002883bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2884 for (auto &I : *B)
2885 if (I.isEHLabel())
2886 return true;
2887 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002888}
2889
Jyotsna Verma84256432013-03-01 17:37:13 +00002890// Returns true if an instruction can be converted into a non-extended
2891// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002892bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002893 short NonExtOpcode;
2894 // Check if the instruction has a register form that uses register in place
2895 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002896 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002897 return true;
2898
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002899 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002900 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002901
2902 switch (getAddrMode(MI)) {
2903 case HexagonII::Absolute :
2904 // Load/store with absolute addressing mode can be converted into
2905 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002906 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002907 break;
2908 case HexagonII::BaseImmOffset :
2909 // Load/store with base+offset addressing mode can be converted into
2910 // base+register offset addressing mode. However left shift operand should
2911 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002912 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002913 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002914 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002915 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002916 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002917 default:
2918 return false;
2919 }
2920 if (NonExtOpcode < 0)
2921 return false;
2922 return true;
2923 }
2924 return false;
2925}
2926
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002927bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2928 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002929 Hexagon::InstrType_Pseudo) >= 0;
2930}
2931
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002932bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2933 const {
2934 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2935 while (I != E) {
2936 if (I->isBarrier())
2937 return true;
2938 ++I;
2939 }
2940 return false;
2941}
2942
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002943// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002944bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
2945 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2946 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002947 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2948 HST.hasV60TOps();
2949}
2950
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002951// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002952bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2953 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002954 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2955}
2956
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002957bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2958 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002959 // There is no stall when ProdMI is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002960 if (!isHVXVec(ProdMI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002961 return false;
2962
2963 // There is no stall when ProdMI and ConsMI are not dependent.
2964 if (!isDependent(ProdMI, ConsMI))
2965 return false;
2966
2967 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2968 // are scheduled in consecutive packets.
2969 if (isVecUsableNextPacket(ProdMI, ConsMI))
2970 return false;
2971
2972 return true;
2973}
2974
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002975bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002976 MachineBasicBlock::const_instr_iterator BII) const {
2977 // There is no stall when I is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002978 if (!isHVXVec(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002979 return false;
2980
2981 MachineBasicBlock::const_instr_iterator MII = BII;
2982 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2983
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002984 if (!(*MII).isBundle()) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002985 const MachineInstr &J = *MII;
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002986 return producesStall(J, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002987 }
2988
2989 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002990 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002991 if (producesStall(J, MI))
2992 return true;
2993 }
2994 return false;
2995}
2996
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002997bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002998 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002999 for (const MachineOperand &MO : MI.operands()) {
3000 // Predicate register must be explicitly defined.
3001 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
3002 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003003 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00003004 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003005 }
3006
3007 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3008 // memd_locked cannot be used as .new as well,
3009 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003010 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003011}
3012
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003013bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003014 return Opcode == Hexagon::J2_jumpt ||
3015 Opcode == Hexagon::J2_jumptpt ||
3016 Opcode == Hexagon::J2_jumpf ||
3017 Opcode == Hexagon::J2_jumpfpt ||
3018 Opcode == Hexagon::J2_jumptnew ||
3019 Opcode == Hexagon::J2_jumpfnew ||
3020 Opcode == Hexagon::J2_jumptnewpt ||
3021 Opcode == Hexagon::J2_jumpfnewpt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003022}
3023
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003024bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3025 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3026 return false;
3027 return !isPredicatedTrue(Cond[0].getImm());
3028}
3029
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003030short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3031 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003032}
3033
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003034unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3035 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003036 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3037}
3038
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003039// Returns the base register in a memory access (load/store). The offset is
3040// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003041unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003042 int &Offset, unsigned &AccessSize) const {
3043 // Return if it is not a base+offset type instruction or a MemOp.
3044 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3045 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003046 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003047 return 0;
3048
3049 // Since it is a memory access instruction, getMemAccessSize() should never
3050 // return 0.
3051 assert (getMemAccessSize(MI) &&
3052 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3053
3054 // Return Values of getMemAccessSize() are
3055 // 0 - Checked in the assert above.
3056 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3057 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3058 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3059
3060 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003061 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003062 return 0;
3063
3064 // Post increment updates its EA after the mem access,
3065 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003066 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003067 Offset = 0;
3068 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003069 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003070 }
3071
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003072 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003073}
3074
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003075/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003076bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003077 unsigned &BasePos, unsigned &OffsetPos) const {
3078 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003079 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003080 BasePos = 0;
3081 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003082 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003083 BasePos = 0;
3084 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003085 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003086 BasePos = 1;
3087 OffsetPos = 2;
3088 } else
3089 return false;
3090
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003091 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003092 BasePos++;
3093 OffsetPos++;
3094 }
3095 if (isPostIncrement(MI)) {
3096 BasePos++;
3097 OffsetPos++;
3098 }
3099
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003100 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003101 return false;
3102
3103 return true;
3104}
3105
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003106// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003107// e.g. jump_t t1 (i1)
3108// jump t2 (i2)
3109// Jumpers = {i2, i1}
3110SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3111 MachineBasicBlock& MBB) const {
3112 SmallVector<MachineInstr*, 2> Jumpers;
3113 // If the block has no terminators, it just falls into the block after it.
3114 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3115 if (I == MBB.instr_begin())
3116 return Jumpers;
3117
3118 // A basic block may looks like this:
3119 //
3120 // [ insn
3121 // EH_LABEL
3122 // insn
3123 // insn
3124 // insn
3125 // EH_LABEL
3126 // insn ]
3127 //
3128 // It has two succs but does not have a terminator
3129 // Don't know how to handle it.
3130 do {
3131 --I;
3132 if (I->isEHLabel())
3133 return Jumpers;
3134 } while (I != MBB.instr_begin());
3135
3136 I = MBB.instr_end();
3137 --I;
3138
3139 while (I->isDebugValue()) {
3140 if (I == MBB.instr_begin())
3141 return Jumpers;
3142 --I;
3143 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003144 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003145 return Jumpers;
3146
3147 // Get the last instruction in the block.
3148 MachineInstr *LastInst = &*I;
3149 Jumpers.push_back(LastInst);
3150 MachineInstr *SecondLastInst = nullptr;
3151 // Find one more terminator if present.
3152 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003153 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003154 if (!SecondLastInst) {
3155 SecondLastInst = &*I;
3156 Jumpers.push_back(SecondLastInst);
3157 } else // This is a third branch.
3158 return Jumpers;
3159 }
3160 if (I == MBB.instr_begin())
3161 break;
3162 --I;
3163 } while (true);
3164 return Jumpers;
3165}
3166
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003167short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3168 if (Opcode < 0)
3169 return -1;
3170 return Hexagon::getBaseWithLongOffset(Opcode);
3171}
3172
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003173short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3174 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003175}
3176
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003177short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3178 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003179}
3180
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003181// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003182unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3183 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003184 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3185}
3186
3187// See if instruction could potentially be a duplex candidate.
3188// If so, return its group. Zero otherwise.
3189HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003190 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003191 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3192
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003193 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003194 default:
3195 return HexagonII::HCG_None;
3196 //
3197 // Compound pairs.
3198 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3199 // "Rd16=#U6 ; jump #r9:2"
3200 // "Rd16=Rs16 ; jump #r9:2"
3201 //
3202 case Hexagon::C2_cmpeq:
3203 case Hexagon::C2_cmpgt:
3204 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003205 DstReg = MI.getOperand(0).getReg();
3206 Src1Reg = MI.getOperand(1).getReg();
3207 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003208 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3209 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3210 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3211 return HexagonII::HCG_A;
3212 break;
3213 case Hexagon::C2_cmpeqi:
3214 case Hexagon::C2_cmpgti:
3215 case Hexagon::C2_cmpgtui:
3216 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003217 DstReg = MI.getOperand(0).getReg();
3218 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003219 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3220 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003221 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3222 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3223 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003224 return HexagonII::HCG_A;
3225 break;
3226 case Hexagon::A2_tfr:
3227 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003228 DstReg = MI.getOperand(0).getReg();
3229 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003230 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3231 return HexagonII::HCG_A;
3232 break;
3233 case Hexagon::A2_tfrsi:
3234 // Rd = #u6
3235 // Do not test for #u6 size since the const is getting extended
3236 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003237 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003238 if (isIntRegForSubInst(DstReg))
3239 return HexagonII::HCG_A;
3240 break;
3241 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003242 DstReg = MI.getOperand(0).getReg();
3243 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003244 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3245 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003246 MI.getOperand(2).isImm() &&
3247 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003248 return HexagonII::HCG_A;
3249 break;
3250 // The fact that .new form is used pretty much guarantees
3251 // that predicate register will match. Nevertheless,
3252 // there could be some false positives without additional
3253 // checking.
3254 case Hexagon::J2_jumptnew:
3255 case Hexagon::J2_jumpfnew:
3256 case Hexagon::J2_jumptnewpt:
3257 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003258 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003259 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3260 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3261 return HexagonII::HCG_B;
3262 break;
3263 // Transfer and jump:
3264 // Rd=#U6 ; jump #r9:2
3265 // Rd=Rs ; jump #r9:2
3266 // Do not test for jump range here.
3267 case Hexagon::J2_jump:
3268 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003269 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003270 return HexagonII::HCG_C;
3271 break;
3272 }
3273
3274 return HexagonII::HCG_None;
3275}
3276
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003277// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003278unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3279 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003280 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3281 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003282 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3283 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003284 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003285 unsigned DestReg = GA.getOperand(0).getReg();
3286 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003287 return -1;
3288 if (DestReg == Hexagon::P0)
3289 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3290 if (DestReg == Hexagon::P1)
3291 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3292 return -1;
3293}
3294
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003295int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3296 enum Hexagon::PredSense inPredSense;
3297 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3298 Hexagon::PredSense_true;
3299 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3300 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3301 return CondOpcode;
3302
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003303 llvm_unreachable("Unexpected predicable instruction");
3304}
3305
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003306// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003307int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3308 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003309 default: llvm_unreachable("Unknown .cur type");
3310 case Hexagon::V6_vL32b_pi:
3311 return Hexagon::V6_vL32b_cur_pi;
3312 case Hexagon::V6_vL32b_ai:
3313 return Hexagon::V6_vL32b_cur_ai;
3314 //128B
3315 case Hexagon::V6_vL32b_pi_128B:
3316 return Hexagon::V6_vL32b_cur_pi_128B;
3317 case Hexagon::V6_vL32b_ai_128B:
3318 return Hexagon::V6_vL32b_cur_ai_128B;
3319 }
3320 return 0;
3321}
3322
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003323// Return the regular version of the .cur instruction.
3324int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3325 switch (MI.getOpcode()) {
3326 default: llvm_unreachable("Unknown .cur type");
3327 case Hexagon::V6_vL32b_cur_pi:
3328 return Hexagon::V6_vL32b_pi;
3329 case Hexagon::V6_vL32b_cur_ai:
3330 return Hexagon::V6_vL32b_ai;
3331 //128B
3332 case Hexagon::V6_vL32b_cur_pi_128B:
3333 return Hexagon::V6_vL32b_pi_128B;
3334 case Hexagon::V6_vL32b_cur_ai_128B:
3335 return Hexagon::V6_vL32b_ai_128B;
3336 }
3337 return 0;
3338}
3339
3340
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003341// The diagram below shows the steps involved in the conversion of a predicated
3342// store instruction to its .new predicated new-value form.
3343//
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003344// Note: It doesn't include conditional new-value stores as they can't be
3345// converted to .new predicate.
3346//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003347// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3348// ^ ^
3349// / \ (not OK. it will cause new-value store to be
3350// / X conditional on p0.new while R2 producer is
3351// / \ on p0)
3352// / \.
3353// p.new store p.old NV store
3354// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3355// ^ ^
3356// \ /
3357// \ /
3358// \ /
3359// p.old store
3360// [if (p0)memw(R0+#0)=R2]
3361//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003362// The following set of instructions further explains the scenario where
3363// conditional new-value store becomes invalid when promoted to .new predicate
3364// form.
3365//
3366// { 1) if (p0) r0 = add(r1, r2)
3367// 2) p0 = cmp.eq(r3, #0) }
3368//
3369// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3370// the first two instructions because in instr 1, r0 is conditional on old value
3371// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3372// is not valid for new-value stores.
3373// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3374// from the "Conditional Store" list. Because a predicated new value store
3375// would NOT be promoted to a double dot new store. See diagram below:
3376// This function returns yes for those stores that are predicated but not
3377// yet promoted to predicate dot new instructions.
3378//
3379// +---------------------+
3380// /-----| if (p0) memw(..)=r0 |---------\~
3381// || +---------------------+ ||
3382// promote || /\ /\ || promote
3383// || /||\ /||\ ||
3384// \||/ demote || \||/
3385// \/ || || \/
3386// +-------------------------+ || +-------------------------+
3387// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3388// +-------------------------+ || +-------------------------+
3389// || || ||
3390// || demote \||/
3391// promote || \/ NOT possible
3392// || || /\~
3393// \||/ || /||\~
3394// \/ || ||
3395// +-----------------------------+
3396// | if (p0.new) memw(..)=r0.new |
3397// +-----------------------------+
3398// Double Dot New Store
3399//
3400// Returns the most basic instruction for the .new predicated instructions and
3401// new-value stores.
3402// For example, all of the following instructions will be converted back to the
3403// same instruction:
3404// 1) if (p0.new) memw(R0+#0) = R1.new --->
3405// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3406// 3) if (p0.new) memw(R0+#0) = R1 --->
3407//
3408// To understand the translation of instruction 1 to its original form, consider
3409// a packet with 3 instructions.
3410// { p0 = cmp.eq(R0,R1)
3411// if (p0.new) R2 = add(R3, R4)
3412// R5 = add (R3, R1)
3413// }
3414// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3415//
3416// This instruction can be part of the previous packet only if both p0 and R2
3417// are promoted to .new values. This promotion happens in steps, first
3418// predicate register is promoted to .new and in the next iteration R2 is
3419// promoted. Therefore, in case of dependence check failure (due to R5) during
3420// next iteration, it should be converted back to its most basic form.
3421
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003422// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003423int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3424 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003425 if (NVOpcode >= 0) // Valid new-value store instruction.
3426 return NVOpcode;
3427
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003428 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003429 default:
3430 llvm::report_fatal_error(std::string("Unknown .new type: ") +
3431 std::to_string(MI.getOpcode()).c_str());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003432 case Hexagon::S4_storerb_ur:
3433 return Hexagon::S4_storerbnew_ur;
3434
3435 case Hexagon::S2_storerb_pci:
3436 return Hexagon::S2_storerb_pci;
3437
3438 case Hexagon::S2_storeri_pci:
3439 return Hexagon::S2_storeri_pci;
3440
3441 case Hexagon::S2_storerh_pci:
3442 return Hexagon::S2_storerh_pci;
3443
3444 case Hexagon::S2_storerd_pci:
3445 return Hexagon::S2_storerd_pci;
3446
3447 case Hexagon::S2_storerf_pci:
3448 return Hexagon::S2_storerf_pci;
3449
3450 case Hexagon::V6_vS32b_ai:
3451 return Hexagon::V6_vS32b_new_ai;
3452
3453 case Hexagon::V6_vS32b_pi:
3454 return Hexagon::V6_vS32b_new_pi;
3455
3456 // 128B
3457 case Hexagon::V6_vS32b_ai_128B:
3458 return Hexagon::V6_vS32b_new_ai_128B;
3459
3460 case Hexagon::V6_vS32b_pi_128B:
3461 return Hexagon::V6_vS32b_new_pi_128B;
3462 }
3463 return 0;
3464}
3465
3466// Returns the opcode to use when converting MI, which is a conditional jump,
3467// into a conditional instruction which uses the .new value of the predicate.
3468// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003469int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003470 const MachineBranchProbabilityInfo *MBPI) const {
3471 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003472 const MachineBasicBlock *Src = MI.getParent();
3473 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003474 bool Taken = false;
3475 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003476
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003477 if (BrTarget.isMBB()) {
3478 const MachineBasicBlock *Dst = BrTarget.getMBB();
3479 Taken = MBPI->getEdgeProbability(Src, Dst) >= OneHalf;
3480 } else {
3481 // The branch target is not a basic block (most likely a function).
3482 // Since BPI only gives probabilities for targets that are basic blocks,
3483 // try to identify another target of this branch (potentially a fall-
3484 // -through) and check the probability of that target.
3485 //
3486 // The only handled branch combinations are:
3487 // - one conditional branch,
3488 // - one conditional branch followed by one unconditional branch.
3489 // Otherwise, assume not-taken.
3490 assert(MI.isConditionalBranch());
3491 const MachineBasicBlock &B = *MI.getParent();
3492 bool SawCond = false, Bad = false;
3493 for (const MachineInstr &I : B) {
3494 if (!I.isBranch())
3495 continue;
3496 if (I.isConditionalBranch()) {
3497 SawCond = true;
3498 if (&I != &MI) {
3499 Bad = true;
3500 break;
3501 }
3502 }
3503 if (I.isUnconditionalBranch() && !SawCond) {
3504 Bad = true;
3505 break;
3506 }
3507 }
3508 if (!Bad) {
3509 MachineBasicBlock::const_instr_iterator It(MI);
3510 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3511 if (NextIt == B.instr_end()) {
3512 // If this branch is the last, look for the fall-through block.
3513 for (const MachineBasicBlock *SB : B.successors()) {
3514 if (!B.isLayoutSuccessor(SB))
3515 continue;
3516 Taken = MBPI->getEdgeProbability(Src, SB) < OneHalf;
3517 break;
3518 }
3519 } else {
3520 assert(NextIt->isUnconditionalBranch());
3521 // Find the first MBB operand and assume it's the target.
3522 const MachineBasicBlock *BT = nullptr;
3523 for (const MachineOperand &Op : NextIt->operands()) {
3524 if (!Op.isMBB())
3525 continue;
3526 BT = Op.getMBB();
3527 break;
3528 }
3529 Taken = BT && MBPI->getEdgeProbability(Src, BT) < OneHalf;
3530 }
3531 } // if (!Bad)
3532 }
3533
3534 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003535
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003536 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003537 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003538 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003539 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003540 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003541
3542 default:
3543 llvm_unreachable("Unexpected jump instruction.");
3544 }
3545}
3546
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003547// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003548int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003549 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003550 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003551 // Condtional Jumps
3552 case Hexagon::J2_jumpt:
3553 case Hexagon::J2_jumpf:
3554 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003555 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003556
3557 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3558 if (NewOpcode >= 0)
3559 return NewOpcode;
3560
3561 dbgs() << "Cannot convert to .new: " << getName(MI.getOpcode()) << '\n';
3562 llvm_unreachable(nullptr);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003563}
3564
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003565int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003566 const MachineFunction &MF = *MI.getParent()->getParent();
3567 const HexagonSubtarget &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003568 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003569 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3570 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003571 // All Hexagon architectures have prediction bits on dot-new branches,
3572 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3573 // to pick the right opcode when converting back to dot-old.
3574 if (!HST.getFeatureBits()[Hexagon::ArchV60]) {
3575 switch (NewOp) {
3576 case Hexagon::J2_jumptpt:
3577 NewOp = Hexagon::J2_jumpt;
3578 break;
3579 case Hexagon::J2_jumpfpt:
3580 NewOp = Hexagon::J2_jumpf;
3581 break;
3582 case Hexagon::J2_jumprtpt:
3583 NewOp = Hexagon::J2_jumprt;
3584 break;
3585 case Hexagon::J2_jumprfpt:
3586 NewOp = Hexagon::J2_jumprf;
3587 break;
3588 }
3589 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003590 assert(NewOp >= 0 &&
3591 "Couldn't change predicate new instruction to its old form.");
3592 }
3593
3594 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3595 NewOp = Hexagon::getNonNVStore(NewOp);
3596 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3597 }
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003598
3599 if (HST.hasV60TOps())
3600 return NewOp;
3601
3602 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3603 switch (NewOp) {
3604 case Hexagon::J2_jumpfpt:
3605 return Hexagon::J2_jumpf;
3606 case Hexagon::J2_jumptpt:
3607 return Hexagon::J2_jumpt;
3608 case Hexagon::J2_jumprfpt:
3609 return Hexagon::J2_jumprf;
3610 case Hexagon::J2_jumprtpt:
3611 return Hexagon::J2_jumprt;
3612 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003613 return NewOp;
3614}
3615
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003616// See if instruction could potentially be a duplex candidate.
3617// If so, return its group. Zero otherwise.
3618HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003619 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003620 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3621 auto &HRI = getRegisterInfo();
3622
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003623 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003624 default:
3625 return HexagonII::HSIG_None;
3626 //
3627 // Group L1:
3628 //
3629 // Rd = memw(Rs+#u4:2)
3630 // Rd = memub(Rs+#u4:0)
3631 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003632 DstReg = MI.getOperand(0).getReg();
3633 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003634 // Special case this one from Group L2.
3635 // Rd = memw(r29+#u5:2)
3636 if (isIntRegForSubInst(DstReg)) {
3637 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3638 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003639 MI.getOperand(2).isImm() &&
3640 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003641 return HexagonII::HSIG_L2;
3642 // Rd = memw(Rs+#u4:2)
3643 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003644 (MI.getOperand(2).isImm() &&
3645 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003646 return HexagonII::HSIG_L1;
3647 }
3648 break;
3649 case Hexagon::L2_loadrub_io:
3650 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003651 DstReg = MI.getOperand(0).getReg();
3652 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003653 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003654 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003655 return HexagonII::HSIG_L1;
3656 break;
3657 //
3658 // Group L2:
3659 //
3660 // Rd = memh/memuh(Rs+#u3:1)
3661 // Rd = memb(Rs+#u3:0)
3662 // Rd = memw(r29+#u5:2) - Handled above.
3663 // Rdd = memd(r29+#u5:3)
3664 // deallocframe
3665 // [if ([!]p0[.new])] dealloc_return
3666 // [if ([!]p0[.new])] jumpr r31
3667 case Hexagon::L2_loadrh_io:
3668 case Hexagon::L2_loadruh_io:
3669 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003670 DstReg = MI.getOperand(0).getReg();
3671 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003672 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003673 MI.getOperand(2).isImm() &&
3674 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003675 return HexagonII::HSIG_L2;
3676 break;
3677 case Hexagon::L2_loadrb_io:
3678 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003679 DstReg = MI.getOperand(0).getReg();
3680 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003681 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003682 MI.getOperand(2).isImm() &&
3683 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003684 return HexagonII::HSIG_L2;
3685 break;
3686 case Hexagon::L2_loadrd_io:
3687 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003688 DstReg = MI.getOperand(0).getReg();
3689 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003690 if (isDblRegForSubInst(DstReg, HRI) &&
3691 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3692 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003693 MI.getOperand(2).isImm() &&
3694 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003695 return HexagonII::HSIG_L2;
3696 break;
3697 // dealloc_return is not documented in Hexagon Manual, but marked
3698 // with A_SUBINSN attribute in iset_v4classic.py.
3699 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003700 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003701 case Hexagon::L4_return:
3702 case Hexagon::L2_deallocframe:
3703 return HexagonII::HSIG_L2;
3704 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003705 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003706 // jumpr r31
3707 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003708 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003709 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3710 return HexagonII::HSIG_L2;
3711 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003712 case Hexagon::PS_jmprett:
3713 case Hexagon::PS_jmpretf:
3714 case Hexagon::PS_jmprettnewpt:
3715 case Hexagon::PS_jmpretfnewpt:
3716 case Hexagon::PS_jmprettnew:
3717 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003718 DstReg = MI.getOperand(1).getReg();
3719 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003720 // [if ([!]p0[.new])] jumpr r31
3721 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3722 (Hexagon::P0 == SrcReg)) &&
3723 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3724 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003725 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003726 case Hexagon::L4_return_t :
3727 case Hexagon::L4_return_f :
3728 case Hexagon::L4_return_tnew_pnt :
3729 case Hexagon::L4_return_fnew_pnt :
3730 case Hexagon::L4_return_tnew_pt :
3731 case Hexagon::L4_return_fnew_pt :
3732 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003733 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003734 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3735 return HexagonII::HSIG_L2;
3736 break;
3737 //
3738 // Group S1:
3739 //
3740 // memw(Rs+#u4:2) = Rt
3741 // memb(Rs+#u4:0) = Rt
3742 case Hexagon::S2_storeri_io:
3743 // Special case this one from Group S2.
3744 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003745 Src1Reg = MI.getOperand(0).getReg();
3746 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003747 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3748 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003749 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3750 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003751 return HexagonII::HSIG_S2;
3752 // memw(Rs+#u4:2) = Rt
3753 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003754 MI.getOperand(1).isImm() &&
3755 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003756 return HexagonII::HSIG_S1;
3757 break;
3758 case Hexagon::S2_storerb_io:
3759 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003760 Src1Reg = MI.getOperand(0).getReg();
3761 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003762 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003763 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003764 return HexagonII::HSIG_S1;
3765 break;
3766 //
3767 // Group S2:
3768 //
3769 // memh(Rs+#u3:1) = Rt
3770 // memw(r29+#u5:2) = Rt
3771 // memd(r29+#s6:3) = Rtt
3772 // memw(Rs+#u4:2) = #U1
3773 // memb(Rs+#u4) = #U1
3774 // allocframe(#u5:3)
3775 case Hexagon::S2_storerh_io:
3776 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003777 Src1Reg = MI.getOperand(0).getReg();
3778 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003779 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003780 MI.getOperand(1).isImm() &&
3781 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003782 return HexagonII::HSIG_S1;
3783 break;
3784 case Hexagon::S2_storerd_io:
3785 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003786 Src1Reg = MI.getOperand(0).getReg();
3787 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003788 if (isDblRegForSubInst(Src2Reg, HRI) &&
3789 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003790 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3791 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003792 return HexagonII::HSIG_S2;
3793 break;
3794 case Hexagon::S4_storeiri_io:
3795 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003796 Src1Reg = MI.getOperand(0).getReg();
3797 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3798 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3799 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003800 return HexagonII::HSIG_S2;
3801 break;
3802 case Hexagon::S4_storeirb_io:
3803 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003804 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003805 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003806 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3807 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003808 return HexagonII::HSIG_S2;
3809 break;
3810 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003811 if (MI.getOperand(0).isImm() &&
3812 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003813 return HexagonII::HSIG_S1;
3814 break;
3815 //
3816 // Group A:
3817 //
3818 // Rx = add(Rx,#s7)
3819 // Rd = Rs
3820 // Rd = #u6
3821 // Rd = #-1
3822 // if ([!]P0[.new]) Rd = #0
3823 // Rd = add(r29,#u6:2)
3824 // Rx = add(Rx,Rs)
3825 // P0 = cmp.eq(Rs,#u2)
3826 // Rdd = combine(#0,Rs)
3827 // Rdd = combine(Rs,#0)
3828 // Rdd = combine(#u2,#U2)
3829 // Rd = add(Rs,#1)
3830 // Rd = add(Rs,#-1)
3831 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3832 // Rd = and(Rs,#1)
3833 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003834 DstReg = MI.getOperand(0).getReg();
3835 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003836 if (isIntRegForSubInst(DstReg)) {
3837 // Rd = add(r29,#u6:2)
3838 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003839 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3840 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003841 return HexagonII::HSIG_A;
3842 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003843 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3844 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003845 return HexagonII::HSIG_A;
3846 // Rd = add(Rs,#1)
3847 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003848 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3849 ((MI.getOperand(2).getImm() == 1) ||
3850 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003851 return HexagonII::HSIG_A;
3852 }
3853 break;
3854 case Hexagon::A2_add:
3855 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003856 DstReg = MI.getOperand(0).getReg();
3857 Src1Reg = MI.getOperand(1).getReg();
3858 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003859 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3860 isIntRegForSubInst(Src2Reg))
3861 return HexagonII::HSIG_A;
3862 break;
3863 case Hexagon::A2_andir:
3864 // Same as zxtb.
3865 // Rd16=and(Rs16,#255)
3866 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003867 DstReg = MI.getOperand(0).getReg();
3868 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003869 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003870 MI.getOperand(2).isImm() &&
3871 ((MI.getOperand(2).getImm() == 1) ||
3872 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003873 return HexagonII::HSIG_A;
3874 break;
3875 case Hexagon::A2_tfr:
3876 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003877 DstReg = MI.getOperand(0).getReg();
3878 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003879 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3880 return HexagonII::HSIG_A;
3881 break;
3882 case Hexagon::A2_tfrsi:
3883 // Rd = #u6
3884 // Do not test for #u6 size since the const is getting extended
3885 // regardless and compound could be formed.
3886 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003887 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003888 if (isIntRegForSubInst(DstReg))
3889 return HexagonII::HSIG_A;
3890 break;
3891 case Hexagon::C2_cmoveit:
3892 case Hexagon::C2_cmovenewit:
3893 case Hexagon::C2_cmoveif:
3894 case Hexagon::C2_cmovenewif:
3895 // if ([!]P0[.new]) Rd = #0
3896 // Actual form:
3897 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003898 DstReg = MI.getOperand(0).getReg();
3899 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003900 if (isIntRegForSubInst(DstReg) &&
3901 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003902 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003903 return HexagonII::HSIG_A;
3904 break;
3905 case Hexagon::C2_cmpeqi:
3906 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003907 DstReg = MI.getOperand(0).getReg();
3908 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003909 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3910 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003911 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003912 return HexagonII::HSIG_A;
3913 break;
3914 case Hexagon::A2_combineii:
3915 case Hexagon::A4_combineii:
3916 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003917 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003918 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003919 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3920 (MI.getOperand(1).isGlobal() &&
3921 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3922 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3923 (MI.getOperand(2).isGlobal() &&
3924 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003925 return HexagonII::HSIG_A;
3926 break;
3927 case Hexagon::A4_combineri:
3928 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003929 DstReg = MI.getOperand(0).getReg();
3930 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003931 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003932 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3933 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003934 return HexagonII::HSIG_A;
3935 break;
3936 case Hexagon::A4_combineir:
3937 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003938 DstReg = MI.getOperand(0).getReg();
3939 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003940 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003941 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3942 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003943 return HexagonII::HSIG_A;
3944 break;
3945 case Hexagon::A2_sxtb:
3946 case Hexagon::A2_sxth:
3947 case Hexagon::A2_zxtb:
3948 case Hexagon::A2_zxth:
3949 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003950 DstReg = MI.getOperand(0).getReg();
3951 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003952 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3953 return HexagonII::HSIG_A;
3954 break;
3955 }
3956
3957 return HexagonII::HSIG_None;
3958}
3959
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003960short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3961 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003962}
3963
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003964unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003965 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003966 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3967 // still have a MinLatency property, which getStageLatency checks.
3968 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003969 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003970
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003971 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003972 return 0;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003973 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
3974}
3975
3976/// getOperandLatency - Compute and return the use operand latency of a given
3977/// pair of def and use.
3978/// In most cases, the static scheduling itinerary was enough to determine the
3979/// operand latency. But it may not be possible for instructions with variable
3980/// number of defs / uses.
3981///
3982/// This is a raw interface to the itinerary that may be directly overriden by
3983/// a target. Use computeOperandLatency to get the best estimate of latency.
3984int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3985 const MachineInstr &DefMI,
3986 unsigned DefIdx,
3987 const MachineInstr &UseMI,
3988 unsigned UseIdx) const {
3989 auto &RI = getRegisterInfo();
3990 // Get DefIdx and UseIdx for super registers.
3991 MachineOperand DefMO = DefMI.getOperand(DefIdx);
3992
3993 if (RI.isPhysicalRegister(DefMO.getReg())) {
3994 if (DefMO.isImplicit()) {
3995 for (MCSuperRegIterator SR(DefMO.getReg(), &RI); SR.isValid(); ++SR) {
3996 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &RI);
3997 if (Idx != -1) {
3998 DefIdx = Idx;
3999 break;
4000 }
4001 }
4002 }
4003
4004 MachineOperand UseMO = UseMI.getOperand(UseIdx);
4005 if (UseMO.isImplicit()) {
4006 for (MCSuperRegIterator SR(UseMO.getReg(), &RI); SR.isValid(); ++SR) {
4007 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &RI);
4008 if (Idx != -1) {
4009 UseIdx = Idx;
4010 break;
4011 }
4012 }
4013 }
4014 }
4015
4016 return TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
4017 UseMI, UseIdx);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004018}
4019
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004020// inverts the predication logic.
4021// p -> NotP
4022// NotP -> P
4023bool HexagonInstrInfo::getInvertedPredSense(
4024 SmallVectorImpl<MachineOperand> &Cond) const {
4025 if (Cond.empty())
4026 return false;
4027 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4028 Cond[0].setImm(Opc);
4029 return true;
4030}
4031
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004032unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4033 int InvPredOpcode;
4034 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4035 : Hexagon::getTruePredOpcode(Opc);
4036 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4037 return InvPredOpcode;
4038
4039 llvm_unreachable("Unexpected predicated instruction");
4040}
4041
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004042// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004043int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4044 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004045 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4046 & HexagonII::ExtentSignedMask;
4047 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4048 & HexagonII::ExtentBitsMask;
4049
4050 if (isSigned) // if value is signed
4051 return ~(-1U << (bits - 1));
4052 else
4053 return ~(-1U << bits);
4054}
4055
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004056unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
4057 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004058 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
4059}
4060
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004061// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004062int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4063 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004064 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4065 & HexagonII::ExtentSignedMask;
4066 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4067 & HexagonII::ExtentBitsMask;
4068
4069 if (isSigned) // if value is signed
4070 return -1U << (bits - 1);
4071 else
4072 return 0;
4073}
4074
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004075// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004076short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004077 // Check if the instruction has a register form that uses register in place
4078 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004079 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004080 if (NonExtOpcode >= 0)
4081 return NonExtOpcode;
4082
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004083 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004084 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004085 switch (getAddrMode(MI)) {
4086 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004087 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004088 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004089 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004090 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004091 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004092
Jyotsna Verma84256432013-03-01 17:37:13 +00004093 default:
4094 return -1;
4095 }
4096 }
4097 return -1;
4098}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004099
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004100bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004101 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004102 if (Cond.empty())
4103 return false;
4104 assert(Cond.size() == 2);
4105 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004106 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4107 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004108 }
4109 PredReg = Cond[1].getReg();
4110 PredRegPos = 1;
4111 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4112 PredRegFlags = 0;
4113 if (Cond[1].isImplicit())
4114 PredRegFlags = RegState::Implicit;
4115 if (Cond[1].isUndef())
4116 PredRegFlags |= RegState::Undef;
4117 return true;
4118}
4119
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004120short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4121 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004122}
4123
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004124short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4125 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004126}
4127
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004128// Return the number of bytes required to encode the instruction.
4129// Hexagon instructions are fixed length, 4 bytes, unless they
4130// use a constant extender, which requires another 4 bytes.
4131// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004132unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4133 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004134 return 0;
4135
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004136 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004137 if (!Size)
4138 // Assume the default insn size in case it cannot be determined
4139 // for whatever reason.
4140 Size = HEXAGON_INSTR_SIZE;
4141
4142 if (isConstExtended(MI) || isExtended(MI))
4143 Size += HEXAGON_INSTR_SIZE;
4144
4145 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004146 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4147 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004148 const MachineFunction *MF = MBB.getParent();
4149 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4150
4151 // Count the number of register definitions to find the asm string.
4152 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004153 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004154 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004155 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004156
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004157 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004158 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004159 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004160 Size = getInlineAsmLength(AsmStr, *MAI);
4161 }
4162
4163 return Size;
4164}
4165
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004166uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4167 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004168 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4169}
4170
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004171unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4172 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004173 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004174 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004175
4176 return IS.getUnits();
4177}
4178
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004179// Calculate size of the basic block without debug instructions.
4180unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4181 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4182}
4183
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004184unsigned HexagonInstrInfo::nonDbgBundleSize(
4185 MachineBasicBlock::const_iterator BundleHead) const {
4186 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004187 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004188 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004189 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004190}
4191
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004192/// immediateExtend - Changes the instruction in place to one using an immediate
4193/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004194void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004195 assert((isExtendable(MI)||isConstExtended(MI)) &&
4196 "Instruction must be extendable");
4197 // Find which operand is extendable.
4198 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004199 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004200 // This needs to be something we understand.
4201 assert((MO.isMBB() || MO.isImm()) &&
4202 "Branch with unknown extendable field type");
4203 // Mark given operand as extended.
4204 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4205}
4206
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004207bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004208 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004209 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004210 << NewTarget->getNumber(); MI.dump(););
4211 assert(MI.isBranch());
4212 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4213 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004214 // In general branch target is the last operand,
4215 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004216 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004217 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004218 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4219 MI.getOperand(TargetPos).setMBB(NewTarget);
4220 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004221 NewOpcode = reversePrediction(NewOpcode);
4222 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004223 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004224 return true;
4225}
4226
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004227void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4228 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4229 MachineFunction::iterator A = MF.begin();
4230 MachineBasicBlock &B = *A;
4231 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004232 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004233 MachineInstr *NewMI;
4234
4235 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4236 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004237 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004238 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4239 " Class: " << NewMI->getDesc().getSchedClass());
4240 NewMI->eraseFromParent();
4241 }
4242 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4243}
4244
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004245// inverts the predication logic.
4246// p -> NotP
4247// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004248bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4249 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4250 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004251 return true;
4252}
4253
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004254// Reverse the branch prediction.
4255unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4256 int PredRevOpcode = -1;
4257 if (isPredictedTaken(Opcode))
4258 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4259 else
4260 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4261 assert(PredRevOpcode > 0);
4262 return PredRevOpcode;
4263}
4264
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004265// TODO: Add more rigorous validation.
4266bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4267 const {
4268 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4269}
4270
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004271short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4272 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004273}