blob: 29f36ba2a502810e7d7b64e61f1f21f119509e06 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Hexagon implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000014#include "Hexagon.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000015#include "HexagonHazardRecognizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonInstrInfo.h"
Craig Topperb25fda92012-03-17 18:46:09 +000017#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "HexagonSubtarget.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000019#include "llvm/ADT/SmallPtrSet.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000021#include "llvm/ADT/StringRef.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000022#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000023#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
31#include "llvm/CodeGen/MachineLoopInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000033#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000035#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000036#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000037#include "llvm/MC/MCInstrDesc.h"
38#include "llvm/MC/MCInstrItineraries.h"
39#include "llvm/MC/MCRegisterInfo.h"
40#include "llvm/Support/BranchProbability.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000041#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000042#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000043#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000044#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000045#include "llvm/Support/raw_ostream.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetSubtargetInfo.h"
48#include <cassert>
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000049#include <cctype>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000050#include <cstdint>
51#include <cstring>
52#include <iterator>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054using namespace llvm;
55
Chandler Carruthe96dd892014-04-21 22:55:11 +000056#define DEBUG_TYPE "hexagon-instrinfo"
57
Chandler Carruthd174b722014-04-22 02:03:14 +000058#define GET_INSTRINFO_CTOR_DTOR
59#define GET_INSTRMAP_INFO
60#include "HexagonGenInstrInfo.inc"
61#include "HexagonGenDFAPacketizer.inc"
62
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000063cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000064 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
65 "packetization boundary."));
66
67static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
68 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
69
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000070static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
71 cl::Hidden, cl::ZeroOrMore, cl::init(false),
72 cl::desc("Disable schedule adjustment for new value stores."));
73
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000074static cl::opt<bool> EnableTimingClassLatency(
75 "enable-timing-class-latency", cl::Hidden, cl::init(false),
76 cl::desc("Enable timing class latency"));
77
78static cl::opt<bool> EnableALUForwarding(
79 "enable-alu-forwarding", cl::Hidden, cl::init(true),
80 cl::desc("Enable vec alu forwarding"));
81
82static cl::opt<bool> EnableACCForwarding(
83 "enable-acc-forwarding", cl::Hidden, cl::init(true),
84 cl::desc("Enable vec acc forwarding"));
85
86static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
87 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
88
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000089static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
90 cl::init(true), cl::Hidden, cl::ZeroOrMore,
91 cl::desc("Use the DFA based hazard recognizer."));
92
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093///
94/// Constants for Hexagon instructions.
95///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000096const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
97const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
98const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
99const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000100const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000101const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000103const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000105const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000107const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000108const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000109const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000111const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000112const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000113const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000115const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000116const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000117const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +0000118const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
119const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
120const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
121const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000122
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000123// Pin the vtable to this file.
124void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000125
126HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000127 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000128 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000130static bool isIntRegForSubInst(unsigned Reg) {
131 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
132 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000133}
134
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000135static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000136 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
137 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000138}
139
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000140/// Calculate number of instructions excluding the debug instructions.
141static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
142 MachineBasicBlock::const_instr_iterator MIE) {
143 unsigned Count = 0;
144 for (; MIB != MIE; ++MIB) {
145 if (!MIB->isDebugValue())
146 ++Count;
147 }
148 return Count;
149}
150
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000151/// Find the hardware loop instruction used to set-up the specified loop.
152/// On Hexagon, we have two instructions used to set-up the hardware loop
153/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
154/// to indicate the end of a loop.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000155static MachineInstr *findLoopInstr(MachineBasicBlock *BB, unsigned EndLoopOp,
156 MachineBasicBlock *TargetBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000157 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000158 unsigned LOOPi;
159 unsigned LOOPr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000160 if (EndLoopOp == Hexagon::ENDLOOP0) {
161 LOOPi = Hexagon::J2_loop0i;
162 LOOPr = Hexagon::J2_loop0r;
163 } else { // EndLoopOp == Hexagon::EndLOOP1
164 LOOPi = Hexagon::J2_loop1i;
165 LOOPr = Hexagon::J2_loop1r;
166 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000167
Brendon Cahoondf43e682015-05-08 16:16:29 +0000168 // The loop set-up instruction will be in a predecessor block
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000169 for (MachineBasicBlock *PB : BB->predecessors()) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000170 // If this has been visited, already skip it.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000171 if (!Visited.insert(PB).second)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000172 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000173 if (PB == BB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000174 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000175 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
176 unsigned Opc = I->getOpcode();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000177 if (Opc == LOOPi || Opc == LOOPr)
178 return &*I;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000179 // We've reached a different loop, which means the loop01 has been
180 // removed.
181 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000182 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000183 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000184 // Check the predecessors for the LOOP instruction.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000185 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
186 return Loop;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000187 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000188 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000189}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000190
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000191/// Gather register def/uses from MI.
192/// This treats possible (predicated) defs as actually happening ones
193/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000194static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000195 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
196 Defs.clear();
197 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000198
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000199 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
200 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000201
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000202 if (!MO.isReg())
203 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000204
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000205 unsigned Reg = MO.getReg();
206 if (!Reg)
207 continue;
208
209 if (MO.isUse())
210 Uses.push_back(MO.getReg());
211
212 if (MO.isDef())
213 Defs.push_back(MO.getReg());
214 }
215}
216
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000217// Position dependent, so check twice for swap.
218static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
219 switch (Ga) {
220 case HexagonII::HSIG_None:
221 default:
222 return false;
223 case HexagonII::HSIG_L1:
224 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
225 case HexagonII::HSIG_L2:
226 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
227 Gb == HexagonII::HSIG_A);
228 case HexagonII::HSIG_S1:
229 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
230 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
231 case HexagonII::HSIG_S2:
232 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
233 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
234 Gb == HexagonII::HSIG_A);
235 case HexagonII::HSIG_A:
236 return (Gb == HexagonII::HSIG_A);
237 case HexagonII::HSIG_Compound:
238 return (Gb == HexagonII::HSIG_Compound);
239 }
240 return false;
241}
242
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000243/// isLoadFromStackSlot - If the specified machine instruction is a direct
244/// load from a stack slot, return the virtual or physical register number of
245/// the destination along with the FrameIndex of the loaded stack slot. If
246/// not, return 0. This predicate must return 0 if the instruction has
247/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000248unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000249 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000250 switch (MI.getOpcode()) {
251 default:
252 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000253 case Hexagon::L2_loadri_io:
254 case Hexagon::L2_loadrd_io:
255 case Hexagon::V6_vL32b_ai:
256 case Hexagon::V6_vL32b_ai_128B:
257 case Hexagon::V6_vL32Ub_ai:
258 case Hexagon::V6_vL32Ub_ai_128B:
259 case Hexagon::LDriw_pred:
260 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000261 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000262 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000263 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000264 case Hexagon::PS_vloadrw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000265 const MachineOperand OpFI = MI.getOperand(1);
266 if (!OpFI.isFI())
267 return 0;
268 const MachineOperand OpOff = MI.getOperand(2);
269 if (!OpOff.isImm() || OpOff.getImm() != 0)
270 return 0;
271 FrameIndex = OpFI.getIndex();
272 return MI.getOperand(0).getReg();
273 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000274
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000275 case Hexagon::L2_ploadrit_io:
276 case Hexagon::L2_ploadrif_io:
277 case Hexagon::L2_ploadrdt_io:
278 case Hexagon::L2_ploadrdf_io: {
279 const MachineOperand OpFI = MI.getOperand(2);
280 if (!OpFI.isFI())
281 return 0;
282 const MachineOperand OpOff = MI.getOperand(3);
283 if (!OpOff.isImm() || OpOff.getImm() != 0)
284 return 0;
285 FrameIndex = OpFI.getIndex();
286 return MI.getOperand(0).getReg();
287 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000288 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000289
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000290 return 0;
291}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000292
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000293/// isStoreToStackSlot - If the specified machine instruction is a direct
294/// store to a stack slot, return the virtual or physical register number of
295/// the source reg along with the FrameIndex of the loaded stack slot. If
296/// not, return 0. This predicate must return 0 if the instruction has
297/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000298unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000299 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000300 switch (MI.getOpcode()) {
301 default:
302 break;
303 case Hexagon::S2_storerb_io:
304 case Hexagon::S2_storerh_io:
305 case Hexagon::S2_storeri_io:
306 case Hexagon::S2_storerd_io:
307 case Hexagon::V6_vS32b_ai:
308 case Hexagon::V6_vS32b_ai_128B:
309 case Hexagon::V6_vS32Ub_ai:
310 case Hexagon::V6_vS32Ub_ai_128B:
311 case Hexagon::STriw_pred:
312 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000313 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000314 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000315 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000316 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000317 const MachineOperand &OpFI = MI.getOperand(0);
318 if (!OpFI.isFI())
319 return 0;
320 const MachineOperand &OpOff = MI.getOperand(1);
321 if (!OpOff.isImm() || OpOff.getImm() != 0)
322 return 0;
323 FrameIndex = OpFI.getIndex();
324 return MI.getOperand(2).getReg();
325 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000326
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000327 case Hexagon::S2_pstorerbt_io:
328 case Hexagon::S2_pstorerbf_io:
329 case Hexagon::S2_pstorerht_io:
330 case Hexagon::S2_pstorerhf_io:
331 case Hexagon::S2_pstorerit_io:
332 case Hexagon::S2_pstorerif_io:
333 case Hexagon::S2_pstorerdt_io:
334 case Hexagon::S2_pstorerdf_io: {
335 const MachineOperand &OpFI = MI.getOperand(1);
336 if (!OpFI.isFI())
337 return 0;
338 const MachineOperand &OpOff = MI.getOperand(2);
339 if (!OpOff.isImm() || OpOff.getImm() != 0)
340 return 0;
341 FrameIndex = OpFI.getIndex();
342 return MI.getOperand(3).getReg();
343 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000344 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000345
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000346 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000347}
348
Brendon Cahoondf43e682015-05-08 16:16:29 +0000349/// This function can analyze one/two way branching only and should (mostly) be
350/// called by target independent side.
351/// First entry is always the opcode of the branching instruction, except when
352/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
353/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
354/// e.g. Jump_c p will have
355/// Cond[0] = Jump_c
356/// Cond[1] = p
357/// HW-loop ENDLOOP:
358/// Cond[0] = ENDLOOP
359/// Cond[1] = MBB
360/// New value jump:
361/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
362/// Cond[1] = R
363/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000364///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000365bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000366 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000367 MachineBasicBlock *&FBB,
368 SmallVectorImpl<MachineOperand> &Cond,
369 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000370 TBB = nullptr;
371 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000372 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000373
374 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000375 MachineBasicBlock::instr_iterator I = MBB.instr_end();
376 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377 return false;
378
379 // A basic block may looks like this:
380 //
381 // [ insn
382 // EH_LABEL
383 // insn
384 // insn
385 // insn
386 // EH_LABEL
387 // insn ]
388 //
389 // It has two succs but does not have a terminator
390 // Don't know how to handle it.
391 do {
392 --I;
393 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000394 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000395 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000396 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000397
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000398 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000399 --I;
400
401 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000402 if (I == MBB.instr_begin())
403 return false;
404 --I;
405 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000406
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000407 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
408 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000409 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000410 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000411 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000412 DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000413 I->eraseFromParent();
414 I = MBB.instr_end();
415 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000416 return false;
417 --I;
418 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000419 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000420 return false;
421
422 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000423 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000424 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000425 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000426 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000427 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000428 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000429 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000430 else
431 // This is a third branch.
432 return true;
433 }
434 if (I == MBB.instr_begin())
435 break;
436 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000437 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000438
439 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000440 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
441 // If the branch target is not a basic block, it could be a tail call.
442 // (It is, if the target is a function.)
443 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
444 return true;
445 if (SecLastOpcode == Hexagon::J2_jump &&
446 !SecondLastInst->getOperand(0).isMBB())
447 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000448
449 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000450 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000452 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
453 return true;
454
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000456 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000457 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000458 TBB = LastInst->getOperand(0).getMBB();
459 return false;
460 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000461 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000462 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000463 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000464 Cond.push_back(LastInst->getOperand(0));
465 return false;
466 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000467 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000468 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000469 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000470 Cond.push_back(LastInst->getOperand(0));
471 return false;
472 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000473 // Only supporting rr/ri versions of new-value jumps.
474 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
475 TBB = LastInst->getOperand(2).getMBB();
476 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
477 Cond.push_back(LastInst->getOperand(0));
478 Cond.push_back(LastInst->getOperand(1));
479 return false;
480 }
481 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
482 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000483 // Otherwise, don't know what this is.
484 return true;
485 }
486
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000487 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000488 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000489 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000490 if (!SecondLastInst->getOperand(1).isMBB())
491 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000492 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000493 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000494 Cond.push_back(SecondLastInst->getOperand(0));
495 FBB = LastInst->getOperand(0).getMBB();
496 return false;
497 }
498
Brendon Cahoondf43e682015-05-08 16:16:29 +0000499 // Only supporting rr/ri versions of new-value jumps.
500 if (SecLastOpcodeHasNVJump &&
501 (SecondLastInst->getNumExplicitOperands() == 3) &&
502 (LastOpcode == Hexagon::J2_jump)) {
503 TBB = SecondLastInst->getOperand(2).getMBB();
504 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
505 Cond.push_back(SecondLastInst->getOperand(0));
506 Cond.push_back(SecondLastInst->getOperand(1));
507 FBB = LastInst->getOperand(0).getMBB();
508 return false;
509 }
510
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000511 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
512 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000513 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000514 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000515 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000516 if (AllowModify)
517 I->eraseFromParent();
518 return false;
519 }
520
Brendon Cahoondf43e682015-05-08 16:16:29 +0000521 // If the block ends with an ENDLOOP, and J2_jump, handle it.
522 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000523 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000524 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000525 Cond.push_back(SecondLastInst->getOperand(0));
526 FBB = LastInst->getOperand(0).getMBB();
527 return false;
528 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000529 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
530 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000531 // Otherwise, can't handle this.
532 return true;
533}
534
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000535unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000536 int *BytesRemoved) const {
537 assert(!BytesRemoved && "code size not handled");
538
Brendon Cahoondf43e682015-05-08 16:16:29 +0000539 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000540 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000541 unsigned Count = 0;
542 while (I != MBB.begin()) {
543 --I;
544 if (I->isDebugValue())
545 continue;
546 // Only removing branches from end of MBB.
547 if (!I->isBranch())
548 return Count;
549 if (Count && (I->getOpcode() == Hexagon::J2_jump))
550 llvm_unreachable("Malformed basic block: unconditional branch not last");
551 MBB.erase(&MBB.back());
552 I = MBB.end();
553 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000554 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000555 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000556}
557
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000558unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000559 MachineBasicBlock *TBB,
560 MachineBasicBlock *FBB,
561 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000562 const DebugLoc &DL,
563 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000564 unsigned BOpc = Hexagon::J2_jump;
565 unsigned BccOpc = Hexagon::J2_jumpt;
566 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000567 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000568 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000569
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000570 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000571 // If we want to reverse the branch an odd number of times, we want
572 // J2_jumpf.
573 if (!Cond.empty() && Cond[0].isImm())
574 BccOpc = Cond[0].getImm();
575
576 if (!FBB) {
577 if (Cond.empty()) {
578 // Due to a bug in TailMerging/CFG Optimization, we need to add a
579 // special case handling of a predicated jump followed by an
580 // unconditional jump. If not, Tail Merging and CFG Optimization go
581 // into an infinite loop.
582 MachineBasicBlock *NewTBB, *NewFBB;
583 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000584 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000585 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000586 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
587 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000588 reverseBranchCondition(Cond);
589 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000590 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000591 }
592 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
593 } else if (isEndLoopN(Cond[0].getImm())) {
594 int EndLoopOp = Cond[0].getImm();
595 assert(Cond[1].isMBB());
596 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
597 // Check for it, and change the BB target if needed.
598 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000599 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
600 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000601 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
602 Loop->getOperand(0).setMBB(TBB);
603 // Add the ENDLOOP after the finding the LOOP0.
604 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
605 } else if (isNewValueJump(Cond[0].getImm())) {
606 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
607 // New value jump
608 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
609 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
610 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
611 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
612 if (Cond[2].isReg()) {
613 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
614 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
615 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
616 } else if(Cond[2].isImm()) {
617 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
618 addImm(Cond[2].getImm()).addMBB(TBB);
619 } else
620 llvm_unreachable("Invalid condition for branching");
621 } else {
622 assert((Cond.size() == 2) && "Malformed cond vector");
623 const MachineOperand &RO = Cond[1];
624 unsigned Flags = getUndefRegState(RO.isUndef());
625 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
626 }
627 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000628 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000629 assert((!Cond.empty()) &&
630 "Cond. cannot be empty when multiple branchings are required");
631 assert((!isNewValueJump(Cond[0].getImm())) &&
632 "NV-jump cannot be inserted with another branch");
633 // Special case for hardware loops. The condition is a basic block.
634 if (isEndLoopN(Cond[0].getImm())) {
635 int EndLoopOp = Cond[0].getImm();
636 assert(Cond[1].isMBB());
637 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
638 // Check for it, and change the BB target if needed.
639 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000640 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
641 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000642 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
643 Loop->getOperand(0).setMBB(TBB);
644 // Add the ENDLOOP after the finding the LOOP0.
645 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
646 } else {
647 const MachineOperand &RO = Cond[1];
648 unsigned Flags = getUndefRegState(RO.isUndef());
649 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000650 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000651 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000652
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000653 return 2;
654}
655
Brendon Cahoon254f8892016-07-29 16:44:44 +0000656/// Analyze the loop code to find the loop induction variable and compare used
657/// to compute the number of iterations. Currently, we analyze loop that are
658/// controlled using hardware loops. In this case, the induction variable
659/// instruction is null. For all other cases, this function returns true, which
660/// means we're unable to analyze it.
661bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
662 MachineInstr *&IndVarInst,
663 MachineInstr *&CmpInst) const {
664
665 MachineBasicBlock *LoopEnd = L.getBottomBlock();
666 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
667 // We really "analyze" only hardware loops right now.
668 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
669 IndVarInst = nullptr;
670 CmpInst = &*I;
671 return false;
672 }
673 return true;
674}
675
676/// Generate code to reduce the loop iteration by one and check if the loop is
677/// finished. Return the value/register of the new loop count. this function
678/// assumes the nth iteration is peeled first.
679unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000680 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000681 SmallVectorImpl<MachineOperand> &Cond,
682 SmallVectorImpl<MachineInstr *> &PrevInsts,
683 unsigned Iter, unsigned MaxIter) const {
684 // We expect a hardware loop currently. This means that IndVar is set
685 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000686 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000687 && "Expecting a hardware loop");
688 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000689 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000690 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000691 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(),
692 Cmp.getOperand(0).getMBB(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000693 if (!Loop)
694 return 0;
695 // If the loop trip count is a compile-time value, then just change the
696 // value.
697 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
698 Loop->getOpcode() == Hexagon::J2_loop1i) {
699 int64_t Offset = Loop->getOperand(1).getImm();
700 if (Offset <= 1)
701 Loop->eraseFromParent();
702 else
703 Loop->getOperand(1).setImm(Offset - 1);
704 return Offset - 1;
705 }
706 // The loop trip count is a run-time value. We generate code to subtract
707 // one from the trip count, and update the loop instruction.
708 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
709 unsigned LoopCount = Loop->getOperand(1).getReg();
710 // Check if we're done with the loop.
711 unsigned LoopEnd = createVR(MF, MVT::i1);
712 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
713 addReg(LoopCount).addImm(1);
714 unsigned NewLoopCount = createVR(MF, MVT::i32);
715 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
716 addReg(LoopCount).addImm(-1);
717 // Update the previously generated instructions with the new loop counter.
718 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
719 E = PrevInsts.end(); I != E; ++I)
720 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
721 PrevInsts.clear();
722 PrevInsts.push_back(NewCmp);
723 PrevInsts.push_back(NewAdd);
724 // Insert the new loop instruction if this is the last time the loop is
725 // decremented.
726 if (Iter == MaxIter)
727 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
728 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
729 // Delete the old loop instruction.
730 if (Iter == 0)
731 Loop->eraseFromParent();
732 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
733 Cond.push_back(NewCmp->getOperand(0));
734 return NewLoopCount;
735}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000736
737bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
738 unsigned NumCycles, unsigned ExtraPredCycles,
739 BranchProbability Probability) const {
740 return nonDbgBBSize(&MBB) <= 3;
741}
742
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000743bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
744 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
745 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
746 const {
747 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
748}
749
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000750bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
751 unsigned NumInstrs, BranchProbability Probability) const {
752 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000753}
754
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000755void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000756 MachineBasicBlock::iterator I,
757 const DebugLoc &DL, unsigned DestReg,
758 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000759 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000760 unsigned KillFlag = getKillRegState(KillSrc);
761
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000762 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000763 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000764 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000765 return;
766 }
767 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000768 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
769 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000770 return;
771 }
772 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
773 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000774 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
775 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000776 return;
777 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000778 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000779 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000780 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
781 .addReg(SrcReg, KillFlag);
782 return;
783 }
784 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
785 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
786 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
787 .addReg(SrcReg, KillFlag);
788 return;
789 }
790 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
791 Hexagon::IntRegsRegClass.contains(SrcReg)) {
792 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
793 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000794 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000795 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000796 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
797 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000798 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
799 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000800 return;
801 }
802 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
803 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000804 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
805 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000806 return;
807 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000808 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
809 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000810 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
811 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000812 return;
813 }
814 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
815 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000816 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000817 return;
818 }
819 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000820 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
821 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000822 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000823 .addReg(HiSrc, KillFlag)
824 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000825 return;
826 }
827 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000828 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
829 .addReg(SrcReg)
830 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000831 return;
832 }
833 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000834 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000835 llvm_unreachable("Unimplemented pred to vec");
836 return;
837 }
838 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
839 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
840 llvm_unreachable("Unimplemented vec to pred");
841 return;
842 }
843 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000844 unsigned HiDst = HRI.getSubReg(DestReg, Hexagon::vsub_hi);
845 unsigned LoDst = HRI.getSubReg(DestReg, Hexagon::vsub_lo);
846 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
847 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
848 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), HiDst)
849 .addReg(HiSrc, KillFlag);
850 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), LoDst)
851 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000852 return;
853 }
Sirish Pande30804c22012-02-15 18:52:27 +0000854
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000855#ifndef NDEBUG
856 // Show the invalid registers to ease debugging.
857 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
858 << ": " << PrintReg(DestReg, &HRI)
859 << " = " << PrintReg(SrcReg, &HRI) << '\n';
860#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000861 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000862}
863
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000864void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
865 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
866 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000867 DebugLoc DL = MBB.findDebugLoc(I);
868 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000869 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000870 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000871 unsigned KillFlag = getKillRegState(isKill);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000872 bool HasAlloca = MFI.hasVarSizedObjects();
873 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
874 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000875
Alex Lorenze40c8a22015-08-11 23:09:45 +0000876 MachineMemOperand *MMO = MF.getMachineMemOperand(
877 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
878 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879
Craig Topperc7242e02012-04-20 07:30:17 +0000880 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000881 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000882 .addFrameIndex(FI).addImm(0)
883 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000884 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000885 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000886 .addFrameIndex(FI).addImm(0)
887 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000888 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000889 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000890 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000891 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000892 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
893 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
894 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000895 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
896 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000897 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai_128B))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000898 .addFrameIndex(FI).addImm(0)
899 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
900 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000901 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000902 .addFrameIndex(FI).addImm(0)
903 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
904 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000905 // If there are variable-sized objects, spills will not be aligned.
906 if (HasAlloca)
907 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000908 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
909 : Hexagon::V6_vS32b_ai_128B;
910 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000911 .addFrameIndex(FI).addImm(0)
912 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
913 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000914 // If there are variable-sized objects, spills will not be aligned.
915 if (HasAlloca)
916 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000917 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
918 : Hexagon::V6_vS32b_ai;
919 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000920 .addFrameIndex(FI).addImm(0)
921 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
922 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000923 // If there are variable-sized objects, spills will not be aligned.
924 if (HasAlloca)
925 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000926 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
927 : Hexagon::PS_vstorerw_ai;
928 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000929 .addFrameIndex(FI).addImm(0)
930 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
931 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000932 // If there are variable-sized objects, spills will not be aligned.
933 if (HasAlloca)
934 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000935 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
936 : Hexagon::PS_vstorerw_ai_128B;
937 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000938 .addFrameIndex(FI).addImm(0)
939 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000940 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000941 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000942 }
943}
944
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000945void HexagonInstrInfo::loadRegFromStackSlot(
946 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
947 int FI, const TargetRegisterClass *RC,
948 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000949 DebugLoc DL = MBB.findDebugLoc(I);
950 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000951 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000952 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000953 bool HasAlloca = MFI.hasVarSizedObjects();
954 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
955 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000956
Alex Lorenze40c8a22015-08-11 23:09:45 +0000957 MachineMemOperand *MMO = MF.getMachineMemOperand(
958 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
959 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000960
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000961 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000962 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000963 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000964 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000965 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000966 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000967 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000968 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000969 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
970 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
971 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
972 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000973 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000974 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000975 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
976 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000977 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000978 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
979 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000980 // If there are variable-sized objects, spills will not be aligned.
981 if (HasAlloca)
982 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000983 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
984 : Hexagon::PS_vloadrw_ai_128B;
985 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000986 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
987 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000988 // If there are variable-sized objects, spills will not be aligned.
989 if (HasAlloca)
990 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000991 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
992 : Hexagon::V6_vL32b_ai_128B;
993 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000994 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
995 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000996 // If there are variable-sized objects, spills will not be aligned.
997 if (HasAlloca)
998 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000999 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
1000 : Hexagon::V6_vL32b_ai;
1001 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +00001002 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1003 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +00001004 // If there are variable-sized objects, spills will not be aligned.
1005 if (HasAlloca)
1006 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001007 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
1008 : Hexagon::PS_vloadrw_ai;
1009 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +00001010 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001011 } else {
Craig Toppere55c5562012-02-07 02:50:20 +00001012 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001013 }
1014}
1015
Ron Lieberman88159e52016-09-02 22:56:24 +00001016static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
1017 const MachineBasicBlock &B = *MI.getParent();
1018 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +00001019 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +00001020 for (auto I = B.rbegin(); I != E; ++I)
1021 Regs.stepBackward(*I);
1022}
1023
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001024/// expandPostRAPseudo - This function is called for all pseudo instructions
1025/// that remain after register allocation. Many pseudo instructions are
1026/// created to help register allocation. This is the place to convert them
1027/// into real instructions. The target can edit MI in place, or it can insert
1028/// new instructions and erase MI. The function should return true if
1029/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001030bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001031 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001032 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1033 MachineBasicBlock &MBB = *MI.getParent();
1034 DebugLoc DL = MI.getDebugLoc();
1035 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001036 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001037
1038 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001039 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001040 MachineOperand &MD = MI.getOperand(0);
1041 MachineOperand &MS = MI.getOperand(1);
1042 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001043 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1044 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001045 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001046 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001047 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001048 return true;
1049 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001050 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001051 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001052 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001053 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001054 MBB.erase(MI);
1055 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001056 case Hexagon::V6_vassignp_128B:
1057 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001058 unsigned SrcReg = MI.getOperand(1).getReg();
1059 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001060 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1061 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001062 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1063 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001064 MBB.erase(MI);
1065 return true;
1066 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001067 case Hexagon::V6_lo_128B:
1068 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001069 unsigned SrcReg = MI.getOperand(1).getReg();
1070 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001071 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001072 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001073 MBB.erase(MI);
1074 MRI.clearKillFlags(SrcSubLo);
1075 return true;
1076 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001077 case Hexagon::V6_hi_128B:
1078 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001079 unsigned SrcReg = MI.getOperand(1).getReg();
1080 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001081 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001082 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001083 MBB.erase(MI);
1084 MRI.clearKillFlags(SrcSubHi);
1085 return true;
1086 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001087 case Hexagon::PS_vstorerw_ai:
1088 case Hexagon::PS_vstorerwu_ai:
1089 case Hexagon::PS_vstorerw_ai_128B:
1090 case Hexagon::PS_vstorerwu_ai_128B: {
1091 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1092 Opc == Hexagon::PS_vstorerwu_ai_128B);
1093 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1094 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001095 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001096 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1097 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001098 unsigned NewOpc;
1099 if (Aligned)
1100 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1101 : Hexagon::V6_vS32b_ai;
1102 else
1103 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1104 : Hexagon::V6_vS32Ub_ai;
1105
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001106 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001107 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001108 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001109 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001110 .addImm(MI.getOperand(1).getImm())
1111 .addReg(SrcSubLo)
1112 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001113 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001114 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001115 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001116 // The Vectors are indexed in multiples of vector size.
1117 .addImm(MI.getOperand(1).getImm() + Offset)
1118 .addReg(SrcSubHi)
1119 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001120 MBB.erase(MI);
1121 return true;
1122 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001123 case Hexagon::PS_vloadrw_ai:
1124 case Hexagon::PS_vloadrwu_ai:
1125 case Hexagon::PS_vloadrw_ai_128B:
1126 case Hexagon::PS_vloadrwu_ai_128B: {
1127 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1128 Opc == Hexagon::PS_vloadrwu_ai_128B);
1129 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1130 Opc == Hexagon::PS_vloadrw_ai_128B);
1131 unsigned NewOpc;
1132 if (Aligned)
1133 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1134 : Hexagon::V6_vL32b_ai;
1135 else
1136 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1137 : Hexagon::V6_vL32Ub_ai;
1138
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001139 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001140 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Diana Picus116bbab2017-01-13 09:58:52 +00001141 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1142 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Krzysztof Parzyszek4be9d922017-05-03 15:26:13 +00001143 .add(MI.getOperand(1))
1144 .addImm(MI.getOperand(2).getImm())
1145 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001146 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001147 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1148 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001149 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001150 .addImm(MI.getOperand(2).getImm() + Offset)
1151 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001152 MBB.erase(MI);
1153 return true;
1154 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001155 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001156 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001157 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1158 .addReg(Reg, RegState::Undef)
1159 .addReg(Reg, RegState::Undef);
1160 MBB.erase(MI);
1161 return true;
1162 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001163 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001164 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001165 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1166 .addReg(Reg, RegState::Undef)
1167 .addReg(Reg, RegState::Undef);
1168 MBB.erase(MI);
1169 return true;
1170 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001171 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001172 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001173 unsigned DstReg = MI.getOperand(0).getReg();
1174 unsigned Src1Reg = MI.getOperand(1).getReg();
1175 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001176 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1177 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1178 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1179 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001180 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001181 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001182 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001183 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001184 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001185 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001186 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001187 .addReg(Src2SubLo);
1188 MBB.erase(MI);
1189 MRI.clearKillFlags(Src1SubHi);
1190 MRI.clearKillFlags(Src1SubLo);
1191 MRI.clearKillFlags(Src2SubHi);
1192 MRI.clearKillFlags(Src2SubLo);
1193 return true;
1194 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001195 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001196 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001197 unsigned DstReg = MI.getOperand(0).getReg();
1198 unsigned Src1Reg = MI.getOperand(1).getReg();
1199 unsigned Src2Reg = MI.getOperand(2).getReg();
1200 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001201 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1202 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1203 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1204 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1205 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1206 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001207 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001208 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001209 .addReg(Src1SubHi)
1210 .addReg(Src2SubHi)
1211 .addReg(Src3SubHi);
1212 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001213 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001214 .addReg(Src1SubLo)
1215 .addReg(Src2SubLo)
1216 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001217 MBB.erase(MI);
1218 MRI.clearKillFlags(Src1SubHi);
1219 MRI.clearKillFlags(Src1SubLo);
1220 MRI.clearKillFlags(Src2SubHi);
1221 MRI.clearKillFlags(Src2SubLo);
1222 MRI.clearKillFlags(Src3SubHi);
1223 MRI.clearKillFlags(Src3SubLo);
1224 return true;
1225 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001226 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001227 const MachineOperand &Op0 = MI.getOperand(0);
1228 const MachineOperand &Op1 = MI.getOperand(1);
1229 const MachineOperand &Op2 = MI.getOperand(2);
1230 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001231 unsigned Rd = Op0.getReg();
1232 unsigned Pu = Op1.getReg();
1233 unsigned Rs = Op2.getReg();
1234 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001235 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001236 unsigned K1 = getKillRegState(Op1.isKill());
1237 unsigned K2 = getKillRegState(Op2.isKill());
1238 unsigned K3 = getKillRegState(Op3.isKill());
1239 if (Rd != Rs)
1240 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1241 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1242 .addReg(Rs, K2);
1243 if (Rd != Rt)
1244 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1245 .addReg(Pu, K1)
1246 .addReg(Rt, K3);
1247 MBB.erase(MI);
1248 return true;
1249 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001250 case Hexagon::PS_vselect:
1251 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001252 const MachineOperand &Op0 = MI.getOperand(0);
1253 const MachineOperand &Op1 = MI.getOperand(1);
1254 const MachineOperand &Op2 = MI.getOperand(2);
1255 const MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001256 LivePhysRegs LiveAtMI(&HRI);
1257 getLiveRegsAt(LiveAtMI, MI);
1258 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1259 if (Op0.getReg() != Op2.getReg()) {
1260 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001261 .add(Op0)
1262 .add(Op1)
1263 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001264 if (IsDestLive)
1265 T.addReg(Op0.getReg(), RegState::Implicit);
1266 IsDestLive = true;
1267 }
1268 if (Op0.getReg() != Op3.getReg()) {
1269 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001270 .add(Op0)
1271 .add(Op1)
1272 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001273 if (IsDestLive)
1274 T.addReg(Op0.getReg(), RegState::Implicit);
1275 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001276 MBB.erase(MI);
1277 return true;
1278 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001279 case Hexagon::PS_wselect:
1280 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001281 MachineOperand &Op0 = MI.getOperand(0);
1282 MachineOperand &Op1 = MI.getOperand(1);
1283 MachineOperand &Op2 = MI.getOperand(2);
1284 MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001285 LivePhysRegs LiveAtMI(&HRI);
1286 getLiveRegsAt(LiveAtMI, MI);
1287 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1288
1289 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001290 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1291 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001292 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001293 .add(Op0)
1294 .add(Op1)
1295 .addReg(SrcHi)
1296 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001297 if (IsDestLive)
1298 T.addReg(Op0.getReg(), RegState::Implicit);
1299 IsDestLive = true;
1300 }
1301 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001302 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1303 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001304 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001305 .add(Op0)
1306 .add(Op1)
1307 .addReg(SrcHi)
1308 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001309 if (IsDestLive)
1310 T.addReg(Op0.getReg(), RegState::Implicit);
1311 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001312 MBB.erase(MI);
1313 return true;
1314 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001315 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001316 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001317 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001318 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001319 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001320 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001321 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001322 case Hexagon::PS_jmprett:
1323 MI.setDesc(get(Hexagon::J2_jumprt));
1324 return true;
1325 case Hexagon::PS_jmpretf:
1326 MI.setDesc(get(Hexagon::J2_jumprf));
1327 return true;
1328 case Hexagon::PS_jmprettnewpt:
1329 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1330 return true;
1331 case Hexagon::PS_jmpretfnewpt:
1332 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1333 return true;
1334 case Hexagon::PS_jmprettnew:
1335 MI.setDesc(get(Hexagon::J2_jumprtnew));
1336 return true;
1337 case Hexagon::PS_jmpretfnew:
1338 MI.setDesc(get(Hexagon::J2_jumprfnew));
1339 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001340 }
1341
1342 return false;
1343}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001344
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001345// We indicate that we want to reverse the branch by
1346// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001347bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001348 SmallVectorImpl<MachineOperand> &Cond) const {
1349 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001350 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001351 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1352 unsigned opcode = Cond[0].getImm();
1353 //unsigned temp;
1354 assert(get(opcode).isBranch() && "Should be a branching condition.");
1355 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001356 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001357 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1358 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001359 return false;
1360}
1361
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001362void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1363 MachineBasicBlock::iterator MI) const {
1364 DebugLoc DL;
1365 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1366}
1367
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001368bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1369 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001370}
1371
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001372// Returns true if an instruction is predicated irrespective of the predicate
1373// sense. For example, all of the following will return true.
1374// if (p0) R1 = add(R2, R3)
1375// if (!p0) R1 = add(R2, R3)
1376// if (p0.new) R1 = add(R2, R3)
1377// if (!p0.new) R1 = add(R2, R3)
1378// Note: New-value stores are not included here as in the current
1379// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001380bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1381 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001382 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001383}
1384
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001385bool HexagonInstrInfo::PredicateInstruction(
1386 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001387 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1388 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001389 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001390 return false;
1391 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001392 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001393 assert (isPredicable(MI) && "Expected predicable instruction");
1394 bool invertJump = predOpcodeHasNot(Cond);
1395
1396 // We have to predicate MI "in place", i.e. after this function returns,
1397 // MI will need to be transformed into a predicated form. To avoid com-
1398 // plicated manipulations with the operands (handling tied operands,
1399 // etc.), build a new temporary instruction, then overwrite MI with it.
1400
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001401 MachineBasicBlock &B = *MI.getParent();
1402 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001403 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1404 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001405 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001406 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001407 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001408 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1409 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001410 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001411 NOp++;
1412 }
1413
1414 unsigned PredReg, PredRegPos, PredRegFlags;
1415 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1416 (void)GotPredReg;
1417 assert(GotPredReg);
1418 T.addReg(PredReg, PredRegFlags);
1419 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001420 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001421
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001422 MI.setDesc(get(PredOpc));
1423 while (unsigned n = MI.getNumOperands())
1424 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001425 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001426 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001427
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001428 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001429 B.erase(TI);
1430
1431 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1432 MRI.clearKillFlags(PredReg);
1433 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001434}
1435
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001436bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1437 ArrayRef<MachineOperand> Pred2) const {
1438 // TODO: Fix this
1439 return false;
1440}
1441
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001442bool HexagonInstrInfo::DefinesPredicate(
1443 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001444 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001445 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1446 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001447 if (MO.isReg()) {
1448 if (!MO.isDef())
1449 continue;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001450 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1451 if (RC == &Hexagon::PredRegsRegClass) {
1452 Pred.push_back(MO);
1453 return true;
1454 }
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001455 continue;
1456 } else if (MO.isRegMask()) {
1457 for (unsigned PR : Hexagon::PredRegsRegClass) {
1458 if (!MI.modifiesRegister(PR, &HRI))
1459 continue;
1460 Pred.push_back(MO);
1461 return true;
1462 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001463 }
1464 }
1465 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001466}
Andrew Trickd06df962012-02-01 22:13:57 +00001467
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001468bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001469 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001470}
1471
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001472bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1473 const MachineBasicBlock *MBB,
1474 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001475 // Debug info is never a scheduling boundary. It's necessary to be explicit
1476 // due to the special treatment of IT instructions below, otherwise a
1477 // dbg_value followed by an IT will result in the IT instruction being
1478 // considered a scheduling hazard, which is wrong. It should be the actual
1479 // instruction preceding the dbg_value instruction(s), just like it is
1480 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001481 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001482 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001483
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001484 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001485 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001486 // Don't mess around with no return calls.
1487 if (doesNotReturn(MI))
1488 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001489 // If any of the block's successors is a landing pad, this could be a
1490 // throwing call.
1491 for (auto I : MBB->successors())
1492 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001493 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001494 }
1495
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001496 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001497 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001498 return true;
1499
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001500 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1501 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001502
1503 return false;
1504}
1505
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001506/// Measure the specified inline asm to determine an approximation of its
1507/// length.
1508/// Comments (which run till the next SeparatorString or newline) do not
1509/// count as an instruction.
1510/// Any other non-whitespace text is considered an instruction, with
1511/// multiple instructions separated by SeparatorString or newlines.
1512/// Variable-length instructions are not handled here; this function
1513/// may be overloaded in the target code to do that.
1514/// Hexagon counts the number of ##'s and adjust for that many
1515/// constant exenders.
1516unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1517 const MCAsmInfo &MAI) const {
1518 StringRef AStr(Str);
1519 // Count the number of instructions in the asm.
1520 bool atInsnStart = true;
1521 unsigned Length = 0;
1522 for (; *Str; ++Str) {
1523 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1524 strlen(MAI.getSeparatorString())) == 0)
1525 atInsnStart = true;
1526 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1527 Length += MAI.getMaxInstLength();
1528 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001529 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001530 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1531 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001532 atInsnStart = false;
1533 }
1534
1535 // Add to size number of constant extenders seen * 4.
1536 StringRef Occ("##");
1537 Length += AStr.count(Occ)*4;
1538 return Length;
1539}
1540
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001541ScheduleHazardRecognizer*
1542HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1543 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001544 if (UseDFAHazardRec) {
1545 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1546 return new HexagonHazardRecognizer(II, this, HST);
1547 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001548 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1549}
1550
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001551/// \brief For a comparison instruction, return the source registers in
1552/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1553/// compares against in CmpValue. Return true if the comparison instruction
1554/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001555bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1556 unsigned &SrcReg2, int &Mask,
1557 int &Value) const {
1558 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001559
1560 // Set mask and the first source register.
1561 switch (Opc) {
1562 case Hexagon::C2_cmpeq:
1563 case Hexagon::C2_cmpeqp:
1564 case Hexagon::C2_cmpgt:
1565 case Hexagon::C2_cmpgtp:
1566 case Hexagon::C2_cmpgtu:
1567 case Hexagon::C2_cmpgtup:
1568 case Hexagon::C4_cmpneq:
1569 case Hexagon::C4_cmplte:
1570 case Hexagon::C4_cmplteu:
1571 case Hexagon::C2_cmpeqi:
1572 case Hexagon::C2_cmpgti:
1573 case Hexagon::C2_cmpgtui:
1574 case Hexagon::C4_cmpneqi:
1575 case Hexagon::C4_cmplteui:
1576 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001577 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001578 Mask = ~0;
1579 break;
1580 case Hexagon::A4_cmpbeq:
1581 case Hexagon::A4_cmpbgt:
1582 case Hexagon::A4_cmpbgtu:
1583 case Hexagon::A4_cmpbeqi:
1584 case Hexagon::A4_cmpbgti:
1585 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001586 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001587 Mask = 0xFF;
1588 break;
1589 case Hexagon::A4_cmpheq:
1590 case Hexagon::A4_cmphgt:
1591 case Hexagon::A4_cmphgtu:
1592 case Hexagon::A4_cmpheqi:
1593 case Hexagon::A4_cmphgti:
1594 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001595 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001596 Mask = 0xFFFF;
1597 break;
1598 }
1599
1600 // Set the value/second source register.
1601 switch (Opc) {
1602 case Hexagon::C2_cmpeq:
1603 case Hexagon::C2_cmpeqp:
1604 case Hexagon::C2_cmpgt:
1605 case Hexagon::C2_cmpgtp:
1606 case Hexagon::C2_cmpgtu:
1607 case Hexagon::C2_cmpgtup:
1608 case Hexagon::A4_cmpbeq:
1609 case Hexagon::A4_cmpbgt:
1610 case Hexagon::A4_cmpbgtu:
1611 case Hexagon::A4_cmpheq:
1612 case Hexagon::A4_cmphgt:
1613 case Hexagon::A4_cmphgtu:
1614 case Hexagon::C4_cmpneq:
1615 case Hexagon::C4_cmplte:
1616 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001617 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001618 return true;
1619
1620 case Hexagon::C2_cmpeqi:
1621 case Hexagon::C2_cmpgtui:
1622 case Hexagon::C2_cmpgti:
1623 case Hexagon::C4_cmpneqi:
1624 case Hexagon::C4_cmplteui:
1625 case Hexagon::C4_cmpltei:
1626 case Hexagon::A4_cmpbeqi:
1627 case Hexagon::A4_cmpbgti:
1628 case Hexagon::A4_cmpbgtui:
1629 case Hexagon::A4_cmpheqi:
1630 case Hexagon::A4_cmphgti:
1631 case Hexagon::A4_cmphgtui:
1632 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001633 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001634 return true;
1635 }
1636
1637 return false;
1638}
1639
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001640unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001641 const MachineInstr &MI,
1642 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001643 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001644}
1645
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001646DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1647 const TargetSubtargetInfo &STI) const {
1648 const InstrItineraryData *II = STI.getInstrItineraryData();
1649 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1650}
1651
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001652// Inspired by this pair:
1653// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1654// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1655// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001656bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1657 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001658 int OffsetA = 0, OffsetB = 0;
1659 unsigned SizeA = 0, SizeB = 0;
1660
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001661 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1662 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001663 return false;
1664
1665 // Instructions that are pure loads, not loads and stores like memops are not
1666 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001667 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001668 return true;
1669
1670 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001671 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001672 if (!BaseRegA || !SizeA)
1673 return false;
1674
1675 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001676 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001677 if (!BaseRegB || !SizeB)
1678 return false;
1679
1680 if (BaseRegA != BaseRegB)
1681 return false;
1682
1683 // This is a mem access with the same base register and known offsets from it.
1684 // Reason about it.
1685 if (OffsetA > OffsetB) {
1686 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1687 return (SizeB <= offDiff);
1688 } else if (OffsetA < OffsetB) {
1689 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1690 return (SizeA <= offDiff);
1691 }
1692
1693 return false;
1694}
1695
Brendon Cahoon254f8892016-07-29 16:44:44 +00001696/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001697bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001698 int &Value) const {
1699 if (isPostIncrement(MI)) {
1700 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001701 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001702 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001703 if (MI.getOpcode() == Hexagon::A2_addi) {
1704 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001705 return true;
1706 }
1707
1708 return false;
1709}
1710
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001711unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001712 MachineRegisterInfo &MRI = MF->getRegInfo();
1713 const TargetRegisterClass *TRC;
1714 if (VT == MVT::i1) {
1715 TRC = &Hexagon::PredRegsRegClass;
1716 } else if (VT == MVT::i32 || VT == MVT::f32) {
1717 TRC = &Hexagon::IntRegsRegClass;
1718 } else if (VT == MVT::i64 || VT == MVT::f64) {
1719 TRC = &Hexagon::DoubleRegsRegClass;
1720 } else {
1721 llvm_unreachable("Cannot handle this register class");
1722 }
1723
1724 unsigned NewReg = MRI.createVirtualRegister(TRC);
1725 return NewReg;
1726}
1727
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001728bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001729 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1730}
1731
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001732bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1733 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001734 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1735}
1736
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001737bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1738 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001739 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1740 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1741
1742 if (!(isTC1(MI))
1743 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001744 && !(MI.getDesc().mayLoad())
1745 && !(MI.getDesc().mayStore())
1746 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1747 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001748 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001749 && !(MI.isBranch())
1750 && !(MI.isReturn())
1751 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001752 return true;
1753
1754 return false;
1755}
1756
Sanjay Patele4b9f502015-12-07 19:21:39 +00001757// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001758bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001759 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001760}
1761
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001762bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1763 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001764 isConditionalTransfer(MI) ||
1765 isConditionalALU32(MI) ||
1766 isConditionalLoad(MI) ||
1767 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001768 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1769 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001770}
1771
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001772bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1773 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001774 case Hexagon::A2_paddf:
1775 case Hexagon::A2_paddfnew:
1776 case Hexagon::A2_paddif:
1777 case Hexagon::A2_paddifnew:
1778 case Hexagon::A2_paddit:
1779 case Hexagon::A2_padditnew:
1780 case Hexagon::A2_paddt:
1781 case Hexagon::A2_paddtnew:
1782 case Hexagon::A2_pandf:
1783 case Hexagon::A2_pandfnew:
1784 case Hexagon::A2_pandt:
1785 case Hexagon::A2_pandtnew:
1786 case Hexagon::A2_porf:
1787 case Hexagon::A2_porfnew:
1788 case Hexagon::A2_port:
1789 case Hexagon::A2_portnew:
1790 case Hexagon::A2_psubf:
1791 case Hexagon::A2_psubfnew:
1792 case Hexagon::A2_psubt:
1793 case Hexagon::A2_psubtnew:
1794 case Hexagon::A2_pxorf:
1795 case Hexagon::A2_pxorfnew:
1796 case Hexagon::A2_pxort:
1797 case Hexagon::A2_pxortnew:
1798 case Hexagon::A4_paslhf:
1799 case Hexagon::A4_paslhfnew:
1800 case Hexagon::A4_paslht:
1801 case Hexagon::A4_paslhtnew:
1802 case Hexagon::A4_pasrhf:
1803 case Hexagon::A4_pasrhfnew:
1804 case Hexagon::A4_pasrht:
1805 case Hexagon::A4_pasrhtnew:
1806 case Hexagon::A4_psxtbf:
1807 case Hexagon::A4_psxtbfnew:
1808 case Hexagon::A4_psxtbt:
1809 case Hexagon::A4_psxtbtnew:
1810 case Hexagon::A4_psxthf:
1811 case Hexagon::A4_psxthfnew:
1812 case Hexagon::A4_psxtht:
1813 case Hexagon::A4_psxthtnew:
1814 case Hexagon::A4_pzxtbf:
1815 case Hexagon::A4_pzxtbfnew:
1816 case Hexagon::A4_pzxtbt:
1817 case Hexagon::A4_pzxtbtnew:
1818 case Hexagon::A4_pzxthf:
1819 case Hexagon::A4_pzxthfnew:
1820 case Hexagon::A4_pzxtht:
1821 case Hexagon::A4_pzxthtnew:
1822 case Hexagon::C2_ccombinewf:
1823 case Hexagon::C2_ccombinewt:
1824 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001825 }
1826 return false;
1827}
1828
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001829// FIXME - Function name and it's functionality don't match.
1830// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001831bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1832 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001833 return false;
1834
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001835 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001836 // Instruction with valid predicated-new opcode can be promoted to .new.
1837 return PNewOpcode >= 0;
1838}
1839
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001840// Returns true if an instruction is a conditional store.
1841//
1842// Note: It doesn't include conditional new-value stores as they can't be
1843// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001844bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1845 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001846 default: return false;
1847 case Hexagon::S4_storeirbt_io:
1848 case Hexagon::S4_storeirbf_io:
1849 case Hexagon::S4_pstorerbt_rr:
1850 case Hexagon::S4_pstorerbf_rr:
1851 case Hexagon::S2_pstorerbt_io:
1852 case Hexagon::S2_pstorerbf_io:
1853 case Hexagon::S2_pstorerbt_pi:
1854 case Hexagon::S2_pstorerbf_pi:
1855 case Hexagon::S2_pstorerdt_io:
1856 case Hexagon::S2_pstorerdf_io:
1857 case Hexagon::S4_pstorerdt_rr:
1858 case Hexagon::S4_pstorerdf_rr:
1859 case Hexagon::S2_pstorerdt_pi:
1860 case Hexagon::S2_pstorerdf_pi:
1861 case Hexagon::S2_pstorerht_io:
1862 case Hexagon::S2_pstorerhf_io:
1863 case Hexagon::S4_storeirht_io:
1864 case Hexagon::S4_storeirhf_io:
1865 case Hexagon::S4_pstorerht_rr:
1866 case Hexagon::S4_pstorerhf_rr:
1867 case Hexagon::S2_pstorerht_pi:
1868 case Hexagon::S2_pstorerhf_pi:
1869 case Hexagon::S2_pstorerit_io:
1870 case Hexagon::S2_pstorerif_io:
1871 case Hexagon::S4_storeirit_io:
1872 case Hexagon::S4_storeirif_io:
1873 case Hexagon::S4_pstorerit_rr:
1874 case Hexagon::S4_pstorerif_rr:
1875 case Hexagon::S2_pstorerit_pi:
1876 case Hexagon::S2_pstorerif_pi:
1877
1878 // V4 global address store before promoting to dot new.
1879 case Hexagon::S4_pstorerdt_abs:
1880 case Hexagon::S4_pstorerdf_abs:
1881 case Hexagon::S4_pstorerbt_abs:
1882 case Hexagon::S4_pstorerbf_abs:
1883 case Hexagon::S4_pstorerht_abs:
1884 case Hexagon::S4_pstorerhf_abs:
1885 case Hexagon::S4_pstorerit_abs:
1886 case Hexagon::S4_pstorerif_abs:
1887 return true;
1888
1889 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1890 // from the "Conditional Store" list. Because a predicated new value store
1891 // would NOT be promoted to a double dot new store.
1892 // This function returns yes for those stores that are predicated but not
1893 // yet promoted to predicate dot new instructions.
1894 }
1895}
1896
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001897bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1898 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001899 case Hexagon::A2_tfrt:
1900 case Hexagon::A2_tfrf:
1901 case Hexagon::C2_cmoveit:
1902 case Hexagon::C2_cmoveif:
1903 case Hexagon::A2_tfrtnew:
1904 case Hexagon::A2_tfrfnew:
1905 case Hexagon::C2_cmovenewit:
1906 case Hexagon::C2_cmovenewif:
1907 case Hexagon::A2_tfrpt:
1908 case Hexagon::A2_tfrpf:
1909 return true;
1910
1911 default:
1912 return false;
1913 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001914 return false;
1915}
1916
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001917// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1918// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001919bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1920 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001921 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1922 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001923 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001924
1925 unsigned isExtendable =
1926 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1927 if (!isExtendable)
1928 return false;
1929
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001930 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001931 return false;
1932
1933 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001934 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001935 // Use MO operand flags to determine if MO
1936 // has the HMOTF_ConstExtended flag set.
1937 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001938 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001939 // If this is a Machine BB address we are talking about, and it is
1940 // not marked as extended, say so.
1941 if (MO.isMBB())
1942 return false;
1943
1944 // We could be using an instruction with an extendable immediate and shoehorn
1945 // a global address into it. If it is a global address it will be constant
1946 // extended. We do this for COMBINE.
1947 // We currently only handle isGlobal() because it is the only kind of
1948 // object we are going to end up with here for now.
1949 // In the future we probably should add isSymbol(), etc.
1950 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001951 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001952 return true;
1953
1954 // If the extendable operand is not 'Immediate' type, the instruction should
1955 // have 'isExtended' flag set.
1956 assert(MO.isImm() && "Extendable operand must be Immediate type");
1957
1958 int MinValue = getMinValue(MI);
1959 int MaxValue = getMaxValue(MI);
1960 int ImmValue = MO.getImm();
1961
1962 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001963}
1964
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001965bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1966 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001967 case Hexagon::L4_return :
1968 case Hexagon::L4_return_t :
1969 case Hexagon::L4_return_f :
1970 case Hexagon::L4_return_tnew_pnt :
1971 case Hexagon::L4_return_fnew_pnt :
1972 case Hexagon::L4_return_tnew_pt :
1973 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001974 return true;
1975 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001976 return false;
1977}
1978
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001979// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001980bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1981 const MachineInstr &ConsMI) const {
1982 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001983 return false;
1984
1985 auto &HRI = getRegisterInfo();
1986
1987 SmallVector<unsigned, 4> DefsA;
1988 SmallVector<unsigned, 4> DefsB;
1989 SmallVector<unsigned, 8> UsesA;
1990 SmallVector<unsigned, 8> UsesB;
1991
1992 parseOperands(ProdMI, DefsA, UsesA);
1993 parseOperands(ConsMI, DefsB, UsesB);
1994
1995 for (auto &RegA : DefsA)
1996 for (auto &RegB : UsesB) {
1997 // True data dependency.
1998 if (RegA == RegB)
1999 return true;
2000
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002001 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002002 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
2003 if (RegB == *SubRegs)
2004 return true;
2005
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002006 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002007 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
2008 if (RegA == *SubRegs)
2009 return true;
2010 }
2011
2012 return false;
2013}
2014
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002015// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002016bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2017 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002018 case Hexagon::V6_vL32b_cur_pi:
2019 case Hexagon::V6_vL32b_cur_ai:
2020 case Hexagon::V6_vL32b_cur_pi_128B:
2021 case Hexagon::V6_vL32b_cur_ai_128B:
2022 return true;
2023 }
2024 return false;
2025}
2026
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002027// Returns true, if any one of the operands is a dot new
2028// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002029bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2030 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002031 return true;
2032
2033 return false;
2034}
2035
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002036/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002037bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2038 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002039 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2040 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2041 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2042}
2043
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002044bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2045 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002046 return true;
2047
2048 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002049 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002050 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2051 return true;
2052 return false;
2053}
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 Parzyszek56bbf542015-12-16 19:36:12 +00002120 if (!isV60VectorInstruction(I))
2121 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
2173 // AnalyzeBranch can parse, plus NVJ.
2174 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 Parzyszekb9a1c3a2015-11-24 14:55:26 +00002244
2245 switch (SchedClass) {
2246 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2247 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2248 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2249 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2250 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2251 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2252 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2253 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2254 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2255 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2256 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2257 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2258 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2259 return false;
2260 }
2261 return true;
2262}
2263
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002264bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002265 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2266 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002267 return MI.getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002268}
2269
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002270bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2271 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002272 return Opcode == Hexagon::J2_loop0i ||
2273 Opcode == Hexagon::J2_loop0r ||
2274 Opcode == Hexagon::J2_loop0iext ||
2275 Opcode == Hexagon::J2_loop0rext ||
2276 Opcode == Hexagon::J2_loop1i ||
2277 Opcode == Hexagon::J2_loop1r ||
2278 Opcode == Hexagon::J2_loop1iext ||
2279 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002280}
2281
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002282bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2283 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002284 default: return false;
2285 case Hexagon::L4_iadd_memopw_io :
2286 case Hexagon::L4_isub_memopw_io :
2287 case Hexagon::L4_add_memopw_io :
2288 case Hexagon::L4_sub_memopw_io :
2289 case Hexagon::L4_and_memopw_io :
2290 case Hexagon::L4_or_memopw_io :
2291 case Hexagon::L4_iadd_memoph_io :
2292 case Hexagon::L4_isub_memoph_io :
2293 case Hexagon::L4_add_memoph_io :
2294 case Hexagon::L4_sub_memoph_io :
2295 case Hexagon::L4_and_memoph_io :
2296 case Hexagon::L4_or_memoph_io :
2297 case Hexagon::L4_iadd_memopb_io :
2298 case Hexagon::L4_isub_memopb_io :
2299 case Hexagon::L4_add_memopb_io :
2300 case Hexagon::L4_sub_memopb_io :
2301 case Hexagon::L4_and_memopb_io :
2302 case Hexagon::L4_or_memopb_io :
2303 case Hexagon::L4_ior_memopb_io:
2304 case Hexagon::L4_ior_memoph_io:
2305 case Hexagon::L4_ior_memopw_io:
2306 case Hexagon::L4_iand_memopb_io:
2307 case Hexagon::L4_iand_memoph_io:
2308 case Hexagon::L4_iand_memopw_io:
2309 return true;
2310 }
2311 return false;
2312}
2313
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002314bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2315 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002316 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2317}
2318
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002319bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2320 const uint64_t F = get(Opcode).TSFlags;
2321 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2322}
2323
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002324bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002325 return isNewValueJump(MI) || isNewValueStore(MI);
2326}
2327
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002328bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2329 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002330}
2331
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002332bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2333 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2334}
2335
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002336bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2337 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002338 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2339}
2340
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002341bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2342 const uint64_t F = get(Opcode).TSFlags;
2343 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2344}
2345
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002346// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002347bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002348 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002349 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002350 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2351 == OperandNum;
2352}
2353
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002354bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2355 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002356 assert(isPredicated(MI));
2357 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2358}
2359
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002360bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2361 const uint64_t F = get(Opcode).TSFlags;
2362 assert(isPredicated(Opcode));
2363 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2364}
2365
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002366bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2367 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002368 return !((F >> HexagonII::PredicatedFalsePos) &
2369 HexagonII::PredicatedFalseMask);
2370}
2371
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002372bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2373 const uint64_t F = get(Opcode).TSFlags;
2374 // Make sure that the instruction is predicated.
2375 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2376 return !((F >> HexagonII::PredicatedFalsePos) &
2377 HexagonII::PredicatedFalseMask);
2378}
2379
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002380bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2381 const uint64_t F = get(Opcode).TSFlags;
2382 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2383}
2384
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002385bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2386 const uint64_t F = get(Opcode).TSFlags;
2387 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2388}
2389
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002390bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2391 const uint64_t F = get(Opcode).TSFlags;
2392 assert(get(Opcode).isBranch() &&
2393 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2394 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2395}
2396
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002397bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2398 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2399 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2400 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2401 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002402}
2403
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002404bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2405 switch (MI.getOpcode()) {
2406 // Byte
2407 case Hexagon::L2_loadrb_io:
2408 case Hexagon::L4_loadrb_ur:
2409 case Hexagon::L4_loadrb_ap:
2410 case Hexagon::L2_loadrb_pr:
2411 case Hexagon::L2_loadrb_pbr:
2412 case Hexagon::L2_loadrb_pi:
2413 case Hexagon::L2_loadrb_pci:
2414 case Hexagon::L2_loadrb_pcr:
2415 case Hexagon::L2_loadbsw2_io:
2416 case Hexagon::L4_loadbsw2_ur:
2417 case Hexagon::L4_loadbsw2_ap:
2418 case Hexagon::L2_loadbsw2_pr:
2419 case Hexagon::L2_loadbsw2_pbr:
2420 case Hexagon::L2_loadbsw2_pi:
2421 case Hexagon::L2_loadbsw2_pci:
2422 case Hexagon::L2_loadbsw2_pcr:
2423 case Hexagon::L2_loadbsw4_io:
2424 case Hexagon::L4_loadbsw4_ur:
2425 case Hexagon::L4_loadbsw4_ap:
2426 case Hexagon::L2_loadbsw4_pr:
2427 case Hexagon::L2_loadbsw4_pbr:
2428 case Hexagon::L2_loadbsw4_pi:
2429 case Hexagon::L2_loadbsw4_pci:
2430 case Hexagon::L2_loadbsw4_pcr:
2431 case Hexagon::L4_loadrb_rr:
2432 case Hexagon::L2_ploadrbt_io:
2433 case Hexagon::L2_ploadrbt_pi:
2434 case Hexagon::L2_ploadrbf_io:
2435 case Hexagon::L2_ploadrbf_pi:
2436 case Hexagon::L2_ploadrbtnew_io:
2437 case Hexagon::L2_ploadrbfnew_io:
2438 case Hexagon::L4_ploadrbt_rr:
2439 case Hexagon::L4_ploadrbf_rr:
2440 case Hexagon::L4_ploadrbtnew_rr:
2441 case Hexagon::L4_ploadrbfnew_rr:
2442 case Hexagon::L2_ploadrbtnew_pi:
2443 case Hexagon::L2_ploadrbfnew_pi:
2444 case Hexagon::L4_ploadrbt_abs:
2445 case Hexagon::L4_ploadrbf_abs:
2446 case Hexagon::L4_ploadrbtnew_abs:
2447 case Hexagon::L4_ploadrbfnew_abs:
2448 case Hexagon::L2_loadrbgp:
2449 // Half
2450 case Hexagon::L2_loadrh_io:
2451 case Hexagon::L4_loadrh_ur:
2452 case Hexagon::L4_loadrh_ap:
2453 case Hexagon::L2_loadrh_pr:
2454 case Hexagon::L2_loadrh_pbr:
2455 case Hexagon::L2_loadrh_pi:
2456 case Hexagon::L2_loadrh_pci:
2457 case Hexagon::L2_loadrh_pcr:
2458 case Hexagon::L4_loadrh_rr:
2459 case Hexagon::L2_ploadrht_io:
2460 case Hexagon::L2_ploadrht_pi:
2461 case Hexagon::L2_ploadrhf_io:
2462 case Hexagon::L2_ploadrhf_pi:
2463 case Hexagon::L2_ploadrhtnew_io:
2464 case Hexagon::L2_ploadrhfnew_io:
2465 case Hexagon::L4_ploadrht_rr:
2466 case Hexagon::L4_ploadrhf_rr:
2467 case Hexagon::L4_ploadrhtnew_rr:
2468 case Hexagon::L4_ploadrhfnew_rr:
2469 case Hexagon::L2_ploadrhtnew_pi:
2470 case Hexagon::L2_ploadrhfnew_pi:
2471 case Hexagon::L4_ploadrht_abs:
2472 case Hexagon::L4_ploadrhf_abs:
2473 case Hexagon::L4_ploadrhtnew_abs:
2474 case Hexagon::L4_ploadrhfnew_abs:
2475 case Hexagon::L2_loadrhgp:
2476 return true;
2477 default:
2478 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002479 }
2480}
2481
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002482bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2483 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002484 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2485}
2486
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002487bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2488 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002489 case Hexagon::STriw_pred :
2490 case Hexagon::LDriw_pred :
2491 return true;
2492 default:
2493 return false;
2494 }
2495}
2496
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002497bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2498 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002499 return false;
2500
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002501 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002502 if (Op.isGlobal() || Op.isSymbol())
2503 return true;
2504 return false;
2505}
2506
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002507// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002508bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2509 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002510 switch (SchedClass) {
2511 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2512 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2513 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2514 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2515 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2516 //case Hexagon::Sched::M_tc_1_SLOT23:
2517 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2518 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2519 return true;
2520
2521 default:
2522 return false;
2523 }
2524}
2525
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002526bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2527 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002528 switch (SchedClass) {
2529 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2530 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2531 case Hexagon::Sched::CR_tc_2_SLOT3:
2532 case Hexagon::Sched::M_tc_2_SLOT23:
2533 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2534 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2535 return true;
2536
2537 default:
2538 return false;
2539 }
2540}
2541
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002542bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2543 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002544 switch (SchedClass) {
2545 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2546 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2547 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2548 case Hexagon::Sched::CR_tc_2early_SLOT23:
2549 case Hexagon::Sched::CR_tc_2early_SLOT3:
2550 case Hexagon::Sched::J_tc_2early_SLOT0123:
2551 case Hexagon::Sched::J_tc_2early_SLOT2:
2552 case Hexagon::Sched::J_tc_2early_SLOT23:
2553 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2554 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2555 return true;
2556
2557 default:
2558 return false;
2559 }
2560}
2561
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002562bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2563 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002564 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2565}
2566
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002567// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002568bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2569 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002570 if (mayBeCurLoad(MI1)) {
2571 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002572 unsigned DstReg = MI1.getOperand(0).getReg();
2573 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002574 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002575 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002576 return true;
2577 }
2578 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002579 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2580 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2581 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002582 return true;
2583 return false;
2584}
2585
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002586bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002587 const uint64_t V = getType(MI);
2588 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2589}
2590
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002591// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2592//
2593bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2594 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2595 VT == MVT::v32i16 || VT == MVT::v64i8) {
2596 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2597 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2598 (Offset & 0x3f) == 0);
2599 }
2600 // 128B
2601 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2602 VT == MVT::v64i16 || VT == MVT::v128i8) {
2603 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2604 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2605 (Offset & 0x7f) == 0);
2606 }
2607 if (VT == MVT::i64) {
2608 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2609 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2610 (Offset & 0x7) == 0);
2611 }
2612 if (VT == MVT::i32) {
2613 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2614 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2615 (Offset & 0x3) == 0);
2616 }
2617 if (VT == MVT::i16) {
2618 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2619 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2620 (Offset & 0x1) == 0);
2621 }
2622 if (VT == MVT::i8) {
2623 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2624 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2625 }
2626 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002627}
2628
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002629bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2630 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002631 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002632 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002633 // inserted to calculate the final address. Due to this reason, the function
2634 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002635 // We used to assert if the offset was not properly aligned, however,
2636 // there are cases where a misaligned pointer recast can cause this
2637 // problem, and we need to allow for it. The front end warns of such
2638 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002639
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002640 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002641 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002642 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002643 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002644 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002645 case Hexagon::V6_vL32b_ai:
2646 case Hexagon::V6_vS32b_ai:
2647 case Hexagon::V6_vL32Ub_ai:
2648 case Hexagon::V6_vS32Ub_ai:
2649 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2650 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2651
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002652 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002653 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002654 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002655 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002656 case Hexagon::V6_vL32b_ai_128B:
2657 case Hexagon::V6_vS32b_ai_128B:
2658 case Hexagon::V6_vL32Ub_ai_128B:
2659 case Hexagon::V6_vS32Ub_ai_128B:
2660 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2661 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2662
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002663 case Hexagon::J2_loop0i:
2664 case Hexagon::J2_loop1i:
2665 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002666
2667 case Hexagon::S4_storeirb_io:
2668 case Hexagon::S4_storeirbt_io:
2669 case Hexagon::S4_storeirbf_io:
2670 return isUInt<6>(Offset);
2671
2672 case Hexagon::S4_storeirh_io:
2673 case Hexagon::S4_storeirht_io:
2674 case Hexagon::S4_storeirhf_io:
2675 return isShiftedUInt<6,1>(Offset);
2676
2677 case Hexagon::S4_storeiri_io:
2678 case Hexagon::S4_storeirit_io:
2679 case Hexagon::S4_storeirif_io:
2680 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002681 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002682
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002683 if (Extend)
2684 return true;
2685
2686 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002687 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002688 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002689 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2690 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2691
Colin LeMahieu947cd702014-12-23 20:44:59 +00002692 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002693 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002694 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2695 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2696
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002697 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002698 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002699 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002700 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2701 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2702
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002703 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002704 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002705 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002706 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2707 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2708
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002709 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002710 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2711 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2712
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002713 case Hexagon::L4_iadd_memopw_io :
2714 case Hexagon::L4_isub_memopw_io :
2715 case Hexagon::L4_add_memopw_io :
2716 case Hexagon::L4_sub_memopw_io :
2717 case Hexagon::L4_and_memopw_io :
2718 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002719 return (0 <= Offset && Offset <= 255);
2720
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002721 case Hexagon::L4_iadd_memoph_io :
2722 case Hexagon::L4_isub_memoph_io :
2723 case Hexagon::L4_add_memoph_io :
2724 case Hexagon::L4_sub_memoph_io :
2725 case Hexagon::L4_and_memoph_io :
2726 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002727 return (0 <= Offset && Offset <= 127);
2728
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002729 case Hexagon::L4_iadd_memopb_io :
2730 case Hexagon::L4_isub_memopb_io :
2731 case Hexagon::L4_add_memopb_io :
2732 case Hexagon::L4_sub_memopb_io :
2733 case Hexagon::L4_and_memopb_io :
2734 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002735 return (0 <= Offset && Offset <= 63);
2736
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002737 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002738 // any size. Later pass knows how to handle it.
2739 case Hexagon::STriw_pred:
2740 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002741 case Hexagon::STriw_mod:
2742 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002743 return true;
2744
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002745 case Hexagon::PS_fi:
2746 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002747 case Hexagon::INLINEASM:
2748 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002749
2750 case Hexagon::L2_ploadrbt_io:
2751 case Hexagon::L2_ploadrbf_io:
2752 case Hexagon::L2_ploadrubt_io:
2753 case Hexagon::L2_ploadrubf_io:
2754 case Hexagon::S2_pstorerbt_io:
2755 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002756 return isUInt<6>(Offset);
2757
2758 case Hexagon::L2_ploadrht_io:
2759 case Hexagon::L2_ploadrhf_io:
2760 case Hexagon::L2_ploadruht_io:
2761 case Hexagon::L2_ploadruhf_io:
2762 case Hexagon::S2_pstorerht_io:
2763 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002764 return isShiftedUInt<6,1>(Offset);
2765
2766 case Hexagon::L2_ploadrit_io:
2767 case Hexagon::L2_ploadrif_io:
2768 case Hexagon::S2_pstorerit_io:
2769 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002770 return isShiftedUInt<6,2>(Offset);
2771
2772 case Hexagon::L2_ploadrdt_io:
2773 case Hexagon::L2_ploadrdf_io:
2774 case Hexagon::S2_pstorerdt_io:
2775 case Hexagon::S2_pstorerdf_io:
2776 return isShiftedUInt<6,3>(Offset);
2777 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002778
Benjamin Kramerb6684012011-12-27 11:41:05 +00002779 llvm_unreachable("No offset range is defined for this opcode. "
2780 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002781}
2782
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002783bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2784 return isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002785}
2786
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002787bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2788 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002789 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2790 return
2791 V == HexagonII::TypeCVI_VA ||
2792 V == HexagonII::TypeCVI_VA_DV;
2793}
Andrew Trickd06df962012-02-01 22:13:57 +00002794
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002795bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2796 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002797 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2798 return true;
2799
2800 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2801 return true;
2802
2803 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002804 return true;
2805
2806 return false;
2807}
Jyotsna Verma84256432013-03-01 17:37:13 +00002808
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002809bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2810 switch (MI.getOpcode()) {
2811 // Byte
2812 case Hexagon::L2_loadrub_io:
2813 case Hexagon::L4_loadrub_ur:
2814 case Hexagon::L4_loadrub_ap:
2815 case Hexagon::L2_loadrub_pr:
2816 case Hexagon::L2_loadrub_pbr:
2817 case Hexagon::L2_loadrub_pi:
2818 case Hexagon::L2_loadrub_pci:
2819 case Hexagon::L2_loadrub_pcr:
2820 case Hexagon::L2_loadbzw2_io:
2821 case Hexagon::L4_loadbzw2_ur:
2822 case Hexagon::L4_loadbzw2_ap:
2823 case Hexagon::L2_loadbzw2_pr:
2824 case Hexagon::L2_loadbzw2_pbr:
2825 case Hexagon::L2_loadbzw2_pi:
2826 case Hexagon::L2_loadbzw2_pci:
2827 case Hexagon::L2_loadbzw2_pcr:
2828 case Hexagon::L2_loadbzw4_io:
2829 case Hexagon::L4_loadbzw4_ur:
2830 case Hexagon::L4_loadbzw4_ap:
2831 case Hexagon::L2_loadbzw4_pr:
2832 case Hexagon::L2_loadbzw4_pbr:
2833 case Hexagon::L2_loadbzw4_pi:
2834 case Hexagon::L2_loadbzw4_pci:
2835 case Hexagon::L2_loadbzw4_pcr:
2836 case Hexagon::L4_loadrub_rr:
2837 case Hexagon::L2_ploadrubt_io:
2838 case Hexagon::L2_ploadrubt_pi:
2839 case Hexagon::L2_ploadrubf_io:
2840 case Hexagon::L2_ploadrubf_pi:
2841 case Hexagon::L2_ploadrubtnew_io:
2842 case Hexagon::L2_ploadrubfnew_io:
2843 case Hexagon::L4_ploadrubt_rr:
2844 case Hexagon::L4_ploadrubf_rr:
2845 case Hexagon::L4_ploadrubtnew_rr:
2846 case Hexagon::L4_ploadrubfnew_rr:
2847 case Hexagon::L2_ploadrubtnew_pi:
2848 case Hexagon::L2_ploadrubfnew_pi:
2849 case Hexagon::L4_ploadrubt_abs:
2850 case Hexagon::L4_ploadrubf_abs:
2851 case Hexagon::L4_ploadrubtnew_abs:
2852 case Hexagon::L4_ploadrubfnew_abs:
2853 case Hexagon::L2_loadrubgp:
2854 // Half
2855 case Hexagon::L2_loadruh_io:
2856 case Hexagon::L4_loadruh_ur:
2857 case Hexagon::L4_loadruh_ap:
2858 case Hexagon::L2_loadruh_pr:
2859 case Hexagon::L2_loadruh_pbr:
2860 case Hexagon::L2_loadruh_pi:
2861 case Hexagon::L2_loadruh_pci:
2862 case Hexagon::L2_loadruh_pcr:
2863 case Hexagon::L4_loadruh_rr:
2864 case Hexagon::L2_ploadruht_io:
2865 case Hexagon::L2_ploadruht_pi:
2866 case Hexagon::L2_ploadruhf_io:
2867 case Hexagon::L2_ploadruhf_pi:
2868 case Hexagon::L2_ploadruhtnew_io:
2869 case Hexagon::L2_ploadruhfnew_io:
2870 case Hexagon::L4_ploadruht_rr:
2871 case Hexagon::L4_ploadruhf_rr:
2872 case Hexagon::L4_ploadruhtnew_rr:
2873 case Hexagon::L4_ploadruhfnew_rr:
2874 case Hexagon::L2_ploadruhtnew_pi:
2875 case Hexagon::L2_ploadruhfnew_pi:
2876 case Hexagon::L4_ploadruht_abs:
2877 case Hexagon::L4_ploadruhf_abs:
2878 case Hexagon::L4_ploadruhtnew_abs:
2879 case Hexagon::L4_ploadruhfnew_abs:
2880 case Hexagon::L2_loadruhgp:
2881 return true;
2882 default:
2883 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002884 }
2885}
2886
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002887// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002888bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2889 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002890 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2891 if (!isVecUsableNextPacket(MI1, MI2))
2892 return true;
2893 return false;
2894}
2895
Brendon Cahoon254f8892016-07-29 16:44:44 +00002896/// \brief Get the base register and byte offset of a load/store instr.
2897bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2898 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2899 const {
2900 unsigned AccessSize = 0;
2901 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002902 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002903 Offset = OffsetVal;
2904 return BaseReg != 0;
2905}
2906
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002907/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002908bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2909 const MachineInstr &Second) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002910 if (DisableNVSchedule)
2911 return false;
2912 if (mayBeNewStore(Second)) {
2913 // Make sure the definition of the first instruction is the value being
2914 // stored.
2915 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002916 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002917 if (!Stored.isReg())
2918 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002919 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2920 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002921 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2922 return true;
2923 }
2924 }
2925 return false;
2926}
2927
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002928bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2929 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002930 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002931}
2932
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002933bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2934 for (auto &I : *B)
2935 if (I.isEHLabel())
2936 return true;
2937 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002938}
2939
Jyotsna Verma84256432013-03-01 17:37:13 +00002940// Returns true if an instruction can be converted into a non-extended
2941// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002942bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002943 short NonExtOpcode;
2944 // Check if the instruction has a register form that uses register in place
2945 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002946 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002947 return true;
2948
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002949 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002950 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002951
2952 switch (getAddrMode(MI)) {
2953 case HexagonII::Absolute :
2954 // Load/store with absolute addressing mode can be converted into
2955 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002956 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002957 break;
2958 case HexagonII::BaseImmOffset :
2959 // Load/store with base+offset addressing mode can be converted into
2960 // base+register offset addressing mode. However left shift operand should
2961 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002962 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002963 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002964 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002965 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002966 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002967 default:
2968 return false;
2969 }
2970 if (NonExtOpcode < 0)
2971 return false;
2972 return true;
2973 }
2974 return false;
2975}
2976
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002977bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2978 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002979 Hexagon::InstrType_Pseudo) >= 0;
2980}
2981
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002982bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2983 const {
2984 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2985 while (I != E) {
2986 if (I->isBarrier())
2987 return true;
2988 ++I;
2989 }
2990 return false;
2991}
2992
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002993// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002994bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
2995 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2996 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002997 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2998 HST.hasV60TOps();
2999}
3000
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003001// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003002bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
3003 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003004 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3005}
3006
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003007bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3008 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003009 // There is no stall when ProdMI is not a V60 vector.
3010 if (!isV60VectorInstruction(ProdMI))
3011 return false;
3012
3013 // There is no stall when ProdMI and ConsMI are not dependent.
3014 if (!isDependent(ProdMI, ConsMI))
3015 return false;
3016
3017 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3018 // are scheduled in consecutive packets.
3019 if (isVecUsableNextPacket(ProdMI, ConsMI))
3020 return false;
3021
3022 return true;
3023}
3024
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003025bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003026 MachineBasicBlock::const_instr_iterator BII) const {
3027 // There is no stall when I is not a V60 vector.
3028 if (!isV60VectorInstruction(MI))
3029 return false;
3030
3031 MachineBasicBlock::const_instr_iterator MII = BII;
3032 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3033
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00003034 if (!(*MII).isBundle()) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003035 const MachineInstr &J = *MII;
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00003036 return producesStall(J, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003037 }
3038
3039 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003040 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003041 if (producesStall(J, MI))
3042 return true;
3043 }
3044 return false;
3045}
3046
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003047bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003048 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00003049 for (const MachineOperand &MO : MI.operands()) {
3050 // Predicate register must be explicitly defined.
3051 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
3052 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003053 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00003054 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003055 }
3056
3057 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3058 // memd_locked cannot be used as .new as well,
3059 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003060 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003061}
3062
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003063bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3064 return (Opcode == Hexagon::J2_jumpt) ||
3065 (Opcode == Hexagon::J2_jumpf) ||
3066 (Opcode == Hexagon::J2_jumptnew) ||
3067 (Opcode == Hexagon::J2_jumpfnew) ||
3068 (Opcode == Hexagon::J2_jumptnewpt) ||
3069 (Opcode == Hexagon::J2_jumpfnewpt);
3070}
3071
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003072bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3073 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3074 return false;
3075 return !isPredicatedTrue(Cond[0].getImm());
3076}
3077
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003078short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3079 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003080}
3081
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003082unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3083 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003084 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3085}
3086
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003087// Returns the base register in a memory access (load/store). The offset is
3088// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003089unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003090 int &Offset, unsigned &AccessSize) const {
3091 // Return if it is not a base+offset type instruction or a MemOp.
3092 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3093 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003094 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003095 return 0;
3096
3097 // Since it is a memory access instruction, getMemAccessSize() should never
3098 // return 0.
3099 assert (getMemAccessSize(MI) &&
3100 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3101
3102 // Return Values of getMemAccessSize() are
3103 // 0 - Checked in the assert above.
3104 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3105 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3106 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3107
3108 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003109 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003110 return 0;
3111
3112 // Post increment updates its EA after the mem access,
3113 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003114 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003115 Offset = 0;
3116 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003117 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003118 }
3119
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003120 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003121}
3122
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003123/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003124bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003125 unsigned &BasePos, unsigned &OffsetPos) const {
3126 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003127 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003128 BasePos = 0;
3129 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003130 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003131 BasePos = 0;
3132 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003133 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003134 BasePos = 1;
3135 OffsetPos = 2;
3136 } else
3137 return false;
3138
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003139 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003140 BasePos++;
3141 OffsetPos++;
3142 }
3143 if (isPostIncrement(MI)) {
3144 BasePos++;
3145 OffsetPos++;
3146 }
3147
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003148 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003149 return false;
3150
3151 return true;
3152}
3153
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003154// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003155// e.g. jump_t t1 (i1)
3156// jump t2 (i2)
3157// Jumpers = {i2, i1}
3158SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3159 MachineBasicBlock& MBB) const {
3160 SmallVector<MachineInstr*, 2> Jumpers;
3161 // If the block has no terminators, it just falls into the block after it.
3162 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3163 if (I == MBB.instr_begin())
3164 return Jumpers;
3165
3166 // A basic block may looks like this:
3167 //
3168 // [ insn
3169 // EH_LABEL
3170 // insn
3171 // insn
3172 // insn
3173 // EH_LABEL
3174 // insn ]
3175 //
3176 // It has two succs but does not have a terminator
3177 // Don't know how to handle it.
3178 do {
3179 --I;
3180 if (I->isEHLabel())
3181 return Jumpers;
3182 } while (I != MBB.instr_begin());
3183
3184 I = MBB.instr_end();
3185 --I;
3186
3187 while (I->isDebugValue()) {
3188 if (I == MBB.instr_begin())
3189 return Jumpers;
3190 --I;
3191 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003192 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003193 return Jumpers;
3194
3195 // Get the last instruction in the block.
3196 MachineInstr *LastInst = &*I;
3197 Jumpers.push_back(LastInst);
3198 MachineInstr *SecondLastInst = nullptr;
3199 // Find one more terminator if present.
3200 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003201 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003202 if (!SecondLastInst) {
3203 SecondLastInst = &*I;
3204 Jumpers.push_back(SecondLastInst);
3205 } else // This is a third branch.
3206 return Jumpers;
3207 }
3208 if (I == MBB.instr_begin())
3209 break;
3210 --I;
3211 } while (true);
3212 return Jumpers;
3213}
3214
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003215short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3216 if (Opcode < 0)
3217 return -1;
3218 return Hexagon::getBaseWithLongOffset(Opcode);
3219}
3220
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003221short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3222 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003223}
3224
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003225short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3226 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003227}
3228
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003229// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003230unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3231 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003232 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3233}
3234
3235// See if instruction could potentially be a duplex candidate.
3236// If so, return its group. Zero otherwise.
3237HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003238 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003239 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3240
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003241 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003242 default:
3243 return HexagonII::HCG_None;
3244 //
3245 // Compound pairs.
3246 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3247 // "Rd16=#U6 ; jump #r9:2"
3248 // "Rd16=Rs16 ; jump #r9:2"
3249 //
3250 case Hexagon::C2_cmpeq:
3251 case Hexagon::C2_cmpgt:
3252 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003253 DstReg = MI.getOperand(0).getReg();
3254 Src1Reg = MI.getOperand(1).getReg();
3255 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003256 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3257 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3258 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3259 return HexagonII::HCG_A;
3260 break;
3261 case Hexagon::C2_cmpeqi:
3262 case Hexagon::C2_cmpgti:
3263 case Hexagon::C2_cmpgtui:
3264 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003265 DstReg = MI.getOperand(0).getReg();
3266 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003267 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3268 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003269 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3270 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3271 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003272 return HexagonII::HCG_A;
3273 break;
3274 case Hexagon::A2_tfr:
3275 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003276 DstReg = MI.getOperand(0).getReg();
3277 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003278 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3279 return HexagonII::HCG_A;
3280 break;
3281 case Hexagon::A2_tfrsi:
3282 // Rd = #u6
3283 // Do not test for #u6 size since the const is getting extended
3284 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003285 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003286 if (isIntRegForSubInst(DstReg))
3287 return HexagonII::HCG_A;
3288 break;
3289 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003290 DstReg = MI.getOperand(0).getReg();
3291 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003292 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3293 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003294 MI.getOperand(2).isImm() &&
3295 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003296 return HexagonII::HCG_A;
3297 break;
3298 // The fact that .new form is used pretty much guarantees
3299 // that predicate register will match. Nevertheless,
3300 // there could be some false positives without additional
3301 // checking.
3302 case Hexagon::J2_jumptnew:
3303 case Hexagon::J2_jumpfnew:
3304 case Hexagon::J2_jumptnewpt:
3305 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003306 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003307 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3308 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3309 return HexagonII::HCG_B;
3310 break;
3311 // Transfer and jump:
3312 // Rd=#U6 ; jump #r9:2
3313 // Rd=Rs ; jump #r9:2
3314 // Do not test for jump range here.
3315 case Hexagon::J2_jump:
3316 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003317 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003318 return HexagonII::HCG_C;
3319 break;
3320 }
3321
3322 return HexagonII::HCG_None;
3323}
3324
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003325// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003326unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3327 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003328 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3329 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003330 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3331 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003332 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003333 unsigned DestReg = GA.getOperand(0).getReg();
3334 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003335 return -1;
3336 if (DestReg == Hexagon::P0)
3337 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3338 if (DestReg == Hexagon::P1)
3339 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3340 return -1;
3341}
3342
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003343int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3344 enum Hexagon::PredSense inPredSense;
3345 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3346 Hexagon::PredSense_true;
3347 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3348 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3349 return CondOpcode;
3350
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003351 llvm_unreachable("Unexpected predicable instruction");
3352}
3353
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003354// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003355int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3356 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003357 default: llvm_unreachable("Unknown .cur type");
3358 case Hexagon::V6_vL32b_pi:
3359 return Hexagon::V6_vL32b_cur_pi;
3360 case Hexagon::V6_vL32b_ai:
3361 return Hexagon::V6_vL32b_cur_ai;
3362 //128B
3363 case Hexagon::V6_vL32b_pi_128B:
3364 return Hexagon::V6_vL32b_cur_pi_128B;
3365 case Hexagon::V6_vL32b_ai_128B:
3366 return Hexagon::V6_vL32b_cur_ai_128B;
3367 }
3368 return 0;
3369}
3370
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003371// The diagram below shows the steps involved in the conversion of a predicated
3372// store instruction to its .new predicated new-value form.
3373//
3374// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3375// ^ ^
3376// / \ (not OK. it will cause new-value store to be
3377// / X conditional on p0.new while R2 producer is
3378// / \ on p0)
3379// / \.
3380// p.new store p.old NV store
3381// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3382// ^ ^
3383// \ /
3384// \ /
3385// \ /
3386// p.old store
3387// [if (p0)memw(R0+#0)=R2]
3388//
3389//
3390// The following set of instructions further explains the scenario where
3391// conditional new-value store becomes invalid when promoted to .new predicate
3392// form.
3393//
3394// { 1) if (p0) r0 = add(r1, r2)
3395// 2) p0 = cmp.eq(r3, #0) }
3396//
3397// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3398// the first two instructions because in instr 1, r0 is conditional on old value
3399// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3400// is not valid for new-value stores.
3401// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3402// from the "Conditional Store" list. Because a predicated new value store
3403// would NOT be promoted to a double dot new store. See diagram below:
3404// This function returns yes for those stores that are predicated but not
3405// yet promoted to predicate dot new instructions.
3406//
3407// +---------------------+
3408// /-----| if (p0) memw(..)=r0 |---------\~
3409// || +---------------------+ ||
3410// promote || /\ /\ || promote
3411// || /||\ /||\ ||
3412// \||/ demote || \||/
3413// \/ || || \/
3414// +-------------------------+ || +-------------------------+
3415// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3416// +-------------------------+ || +-------------------------+
3417// || || ||
3418// || demote \||/
3419// promote || \/ NOT possible
3420// || || /\~
3421// \||/ || /||\~
3422// \/ || ||
3423// +-----------------------------+
3424// | if (p0.new) memw(..)=r0.new |
3425// +-----------------------------+
3426// Double Dot New Store
3427//
3428// Returns the most basic instruction for the .new predicated instructions and
3429// new-value stores.
3430// For example, all of the following instructions will be converted back to the
3431// same instruction:
3432// 1) if (p0.new) memw(R0+#0) = R1.new --->
3433// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3434// 3) if (p0.new) memw(R0+#0) = R1 --->
3435//
3436// To understand the translation of instruction 1 to its original form, consider
3437// a packet with 3 instructions.
3438// { p0 = cmp.eq(R0,R1)
3439// if (p0.new) R2 = add(R3, R4)
3440// R5 = add (R3, R1)
3441// }
3442// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3443//
3444// This instruction can be part of the previous packet only if both p0 and R2
3445// are promoted to .new values. This promotion happens in steps, first
3446// predicate register is promoted to .new and in the next iteration R2 is
3447// promoted. Therefore, in case of dependence check failure (due to R5) during
3448// next iteration, it should be converted back to its most basic form.
3449
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003450// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003451int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3452 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003453 if (NVOpcode >= 0) // Valid new-value store instruction.
3454 return NVOpcode;
3455
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003456 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003457 default:
3458 llvm::report_fatal_error(std::string("Unknown .new type: ") +
3459 std::to_string(MI.getOpcode()).c_str());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003460 case Hexagon::S4_storerb_ur:
3461 return Hexagon::S4_storerbnew_ur;
3462
3463 case Hexagon::S2_storerb_pci:
3464 return Hexagon::S2_storerb_pci;
3465
3466 case Hexagon::S2_storeri_pci:
3467 return Hexagon::S2_storeri_pci;
3468
3469 case Hexagon::S2_storerh_pci:
3470 return Hexagon::S2_storerh_pci;
3471
3472 case Hexagon::S2_storerd_pci:
3473 return Hexagon::S2_storerd_pci;
3474
3475 case Hexagon::S2_storerf_pci:
3476 return Hexagon::S2_storerf_pci;
3477
3478 case Hexagon::V6_vS32b_ai:
3479 return Hexagon::V6_vS32b_new_ai;
3480
3481 case Hexagon::V6_vS32b_pi:
3482 return Hexagon::V6_vS32b_new_pi;
3483
3484 // 128B
3485 case Hexagon::V6_vS32b_ai_128B:
3486 return Hexagon::V6_vS32b_new_ai_128B;
3487
3488 case Hexagon::V6_vS32b_pi_128B:
3489 return Hexagon::V6_vS32b_new_pi_128B;
3490 }
3491 return 0;
3492}
3493
3494// Returns the opcode to use when converting MI, which is a conditional jump,
3495// into a conditional instruction which uses the .new value of the predicate.
3496// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003497int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003498 const MachineBranchProbabilityInfo *MBPI) const {
3499 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003500 const MachineBasicBlock *Src = MI.getParent();
3501 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003502 bool Taken = false;
3503 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003504
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003505 if (BrTarget.isMBB()) {
3506 const MachineBasicBlock *Dst = BrTarget.getMBB();
3507 Taken = MBPI->getEdgeProbability(Src, Dst) >= OneHalf;
3508 } else {
3509 // The branch target is not a basic block (most likely a function).
3510 // Since BPI only gives probabilities for targets that are basic blocks,
3511 // try to identify another target of this branch (potentially a fall-
3512 // -through) and check the probability of that target.
3513 //
3514 // The only handled branch combinations are:
3515 // - one conditional branch,
3516 // - one conditional branch followed by one unconditional branch.
3517 // Otherwise, assume not-taken.
3518 assert(MI.isConditionalBranch());
3519 const MachineBasicBlock &B = *MI.getParent();
3520 bool SawCond = false, Bad = false;
3521 for (const MachineInstr &I : B) {
3522 if (!I.isBranch())
3523 continue;
3524 if (I.isConditionalBranch()) {
3525 SawCond = true;
3526 if (&I != &MI) {
3527 Bad = true;
3528 break;
3529 }
3530 }
3531 if (I.isUnconditionalBranch() && !SawCond) {
3532 Bad = true;
3533 break;
3534 }
3535 }
3536 if (!Bad) {
3537 MachineBasicBlock::const_instr_iterator It(MI);
3538 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3539 if (NextIt == B.instr_end()) {
3540 // If this branch is the last, look for the fall-through block.
3541 for (const MachineBasicBlock *SB : B.successors()) {
3542 if (!B.isLayoutSuccessor(SB))
3543 continue;
3544 Taken = MBPI->getEdgeProbability(Src, SB) < OneHalf;
3545 break;
3546 }
3547 } else {
3548 assert(NextIt->isUnconditionalBranch());
3549 // Find the first MBB operand and assume it's the target.
3550 const MachineBasicBlock *BT = nullptr;
3551 for (const MachineOperand &Op : NextIt->operands()) {
3552 if (!Op.isMBB())
3553 continue;
3554 BT = Op.getMBB();
3555 break;
3556 }
3557 Taken = BT && MBPI->getEdgeProbability(Src, BT) < OneHalf;
3558 }
3559 } // if (!Bad)
3560 }
3561
3562 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003563
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003564 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003565 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003566 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003567 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003568 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003569
3570 default:
3571 llvm_unreachable("Unexpected jump instruction.");
3572 }
3573}
3574
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003575// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003576int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003577 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003578 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003579 // Condtional Jumps
3580 case Hexagon::J2_jumpt:
3581 case Hexagon::J2_jumpf:
3582 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003583 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003584
3585 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3586 if (NewOpcode >= 0)
3587 return NewOpcode;
3588
3589 dbgs() << "Cannot convert to .new: " << getName(MI.getOpcode()) << '\n';
3590 llvm_unreachable(nullptr);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003591}
3592
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003593int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
3594 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003595 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3596 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003597 const MachineFunction &MF = *MI.getParent()->getParent();
3598 const HexagonSubtarget &HST = MF.getSubtarget<HexagonSubtarget>();
3599 // All Hexagon architectures have prediction bits on dot-new branches,
3600 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3601 // to pick the right opcode when converting back to dot-old.
3602 if (!HST.getFeatureBits()[Hexagon::ArchV60]) {
3603 switch (NewOp) {
3604 case Hexagon::J2_jumptpt:
3605 NewOp = Hexagon::J2_jumpt;
3606 break;
3607 case Hexagon::J2_jumpfpt:
3608 NewOp = Hexagon::J2_jumpf;
3609 break;
3610 case Hexagon::J2_jumprtpt:
3611 NewOp = Hexagon::J2_jumprt;
3612 break;
3613 case Hexagon::J2_jumprfpt:
3614 NewOp = Hexagon::J2_jumprf;
3615 break;
3616 }
3617 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003618 assert(NewOp >= 0 &&
3619 "Couldn't change predicate new instruction to its old form.");
3620 }
3621
3622 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3623 NewOp = Hexagon::getNonNVStore(NewOp);
3624 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3625 }
3626 return NewOp;
3627}
3628
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003629// See if instruction could potentially be a duplex candidate.
3630// If so, return its group. Zero otherwise.
3631HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003632 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003633 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3634 auto &HRI = getRegisterInfo();
3635
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003636 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003637 default:
3638 return HexagonII::HSIG_None;
3639 //
3640 // Group L1:
3641 //
3642 // Rd = memw(Rs+#u4:2)
3643 // Rd = memub(Rs+#u4:0)
3644 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003645 DstReg = MI.getOperand(0).getReg();
3646 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003647 // Special case this one from Group L2.
3648 // Rd = memw(r29+#u5:2)
3649 if (isIntRegForSubInst(DstReg)) {
3650 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3651 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003652 MI.getOperand(2).isImm() &&
3653 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003654 return HexagonII::HSIG_L2;
3655 // Rd = memw(Rs+#u4:2)
3656 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003657 (MI.getOperand(2).isImm() &&
3658 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003659 return HexagonII::HSIG_L1;
3660 }
3661 break;
3662 case Hexagon::L2_loadrub_io:
3663 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003664 DstReg = MI.getOperand(0).getReg();
3665 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003666 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003667 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003668 return HexagonII::HSIG_L1;
3669 break;
3670 //
3671 // Group L2:
3672 //
3673 // Rd = memh/memuh(Rs+#u3:1)
3674 // Rd = memb(Rs+#u3:0)
3675 // Rd = memw(r29+#u5:2) - Handled above.
3676 // Rdd = memd(r29+#u5:3)
3677 // deallocframe
3678 // [if ([!]p0[.new])] dealloc_return
3679 // [if ([!]p0[.new])] jumpr r31
3680 case Hexagon::L2_loadrh_io:
3681 case Hexagon::L2_loadruh_io:
3682 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003683 DstReg = MI.getOperand(0).getReg();
3684 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003685 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003686 MI.getOperand(2).isImm() &&
3687 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003688 return HexagonII::HSIG_L2;
3689 break;
3690 case Hexagon::L2_loadrb_io:
3691 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003692 DstReg = MI.getOperand(0).getReg();
3693 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003694 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003695 MI.getOperand(2).isImm() &&
3696 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003697 return HexagonII::HSIG_L2;
3698 break;
3699 case Hexagon::L2_loadrd_io:
3700 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003701 DstReg = MI.getOperand(0).getReg();
3702 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003703 if (isDblRegForSubInst(DstReg, HRI) &&
3704 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3705 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003706 MI.getOperand(2).isImm() &&
3707 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003708 return HexagonII::HSIG_L2;
3709 break;
3710 // dealloc_return is not documented in Hexagon Manual, but marked
3711 // with A_SUBINSN attribute in iset_v4classic.py.
3712 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003713 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003714 case Hexagon::L4_return:
3715 case Hexagon::L2_deallocframe:
3716 return HexagonII::HSIG_L2;
3717 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003718 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003719 // jumpr r31
3720 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003721 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003722 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3723 return HexagonII::HSIG_L2;
3724 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003725 case Hexagon::PS_jmprett:
3726 case Hexagon::PS_jmpretf:
3727 case Hexagon::PS_jmprettnewpt:
3728 case Hexagon::PS_jmpretfnewpt:
3729 case Hexagon::PS_jmprettnew:
3730 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003731 DstReg = MI.getOperand(1).getReg();
3732 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003733 // [if ([!]p0[.new])] jumpr r31
3734 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3735 (Hexagon::P0 == SrcReg)) &&
3736 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3737 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003738 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003739 case Hexagon::L4_return_t :
3740 case Hexagon::L4_return_f :
3741 case Hexagon::L4_return_tnew_pnt :
3742 case Hexagon::L4_return_fnew_pnt :
3743 case Hexagon::L4_return_tnew_pt :
3744 case Hexagon::L4_return_fnew_pt :
3745 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003746 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003747 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3748 return HexagonII::HSIG_L2;
3749 break;
3750 //
3751 // Group S1:
3752 //
3753 // memw(Rs+#u4:2) = Rt
3754 // memb(Rs+#u4:0) = Rt
3755 case Hexagon::S2_storeri_io:
3756 // Special case this one from Group S2.
3757 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003758 Src1Reg = MI.getOperand(0).getReg();
3759 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003760 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3761 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003762 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3763 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003764 return HexagonII::HSIG_S2;
3765 // memw(Rs+#u4:2) = Rt
3766 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003767 MI.getOperand(1).isImm() &&
3768 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003769 return HexagonII::HSIG_S1;
3770 break;
3771 case Hexagon::S2_storerb_io:
3772 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003773 Src1Reg = MI.getOperand(0).getReg();
3774 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003775 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003776 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003777 return HexagonII::HSIG_S1;
3778 break;
3779 //
3780 // Group S2:
3781 //
3782 // memh(Rs+#u3:1) = Rt
3783 // memw(r29+#u5:2) = Rt
3784 // memd(r29+#s6:3) = Rtt
3785 // memw(Rs+#u4:2) = #U1
3786 // memb(Rs+#u4) = #U1
3787 // allocframe(#u5:3)
3788 case Hexagon::S2_storerh_io:
3789 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003790 Src1Reg = MI.getOperand(0).getReg();
3791 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003792 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003793 MI.getOperand(1).isImm() &&
3794 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003795 return HexagonII::HSIG_S1;
3796 break;
3797 case Hexagon::S2_storerd_io:
3798 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003799 Src1Reg = MI.getOperand(0).getReg();
3800 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003801 if (isDblRegForSubInst(Src2Reg, HRI) &&
3802 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003803 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3804 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003805 return HexagonII::HSIG_S2;
3806 break;
3807 case Hexagon::S4_storeiri_io:
3808 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003809 Src1Reg = MI.getOperand(0).getReg();
3810 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3811 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3812 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003813 return HexagonII::HSIG_S2;
3814 break;
3815 case Hexagon::S4_storeirb_io:
3816 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003817 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003818 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003819 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3820 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003821 return HexagonII::HSIG_S2;
3822 break;
3823 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003824 if (MI.getOperand(0).isImm() &&
3825 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003826 return HexagonII::HSIG_S1;
3827 break;
3828 //
3829 // Group A:
3830 //
3831 // Rx = add(Rx,#s7)
3832 // Rd = Rs
3833 // Rd = #u6
3834 // Rd = #-1
3835 // if ([!]P0[.new]) Rd = #0
3836 // Rd = add(r29,#u6:2)
3837 // Rx = add(Rx,Rs)
3838 // P0 = cmp.eq(Rs,#u2)
3839 // Rdd = combine(#0,Rs)
3840 // Rdd = combine(Rs,#0)
3841 // Rdd = combine(#u2,#U2)
3842 // Rd = add(Rs,#1)
3843 // Rd = add(Rs,#-1)
3844 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3845 // Rd = and(Rs,#1)
3846 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003847 DstReg = MI.getOperand(0).getReg();
3848 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003849 if (isIntRegForSubInst(DstReg)) {
3850 // Rd = add(r29,#u6:2)
3851 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003852 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3853 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003854 return HexagonII::HSIG_A;
3855 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003856 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3857 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003858 return HexagonII::HSIG_A;
3859 // Rd = add(Rs,#1)
3860 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003861 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3862 ((MI.getOperand(2).getImm() == 1) ||
3863 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003864 return HexagonII::HSIG_A;
3865 }
3866 break;
3867 case Hexagon::A2_add:
3868 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003869 DstReg = MI.getOperand(0).getReg();
3870 Src1Reg = MI.getOperand(1).getReg();
3871 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003872 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3873 isIntRegForSubInst(Src2Reg))
3874 return HexagonII::HSIG_A;
3875 break;
3876 case Hexagon::A2_andir:
3877 // Same as zxtb.
3878 // Rd16=and(Rs16,#255)
3879 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003880 DstReg = MI.getOperand(0).getReg();
3881 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003882 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003883 MI.getOperand(2).isImm() &&
3884 ((MI.getOperand(2).getImm() == 1) ||
3885 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003886 return HexagonII::HSIG_A;
3887 break;
3888 case Hexagon::A2_tfr:
3889 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003890 DstReg = MI.getOperand(0).getReg();
3891 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003892 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3893 return HexagonII::HSIG_A;
3894 break;
3895 case Hexagon::A2_tfrsi:
3896 // Rd = #u6
3897 // Do not test for #u6 size since the const is getting extended
3898 // regardless and compound could be formed.
3899 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003900 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003901 if (isIntRegForSubInst(DstReg))
3902 return HexagonII::HSIG_A;
3903 break;
3904 case Hexagon::C2_cmoveit:
3905 case Hexagon::C2_cmovenewit:
3906 case Hexagon::C2_cmoveif:
3907 case Hexagon::C2_cmovenewif:
3908 // if ([!]P0[.new]) Rd = #0
3909 // Actual form:
3910 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003911 DstReg = MI.getOperand(0).getReg();
3912 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003913 if (isIntRegForSubInst(DstReg) &&
3914 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003915 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003916 return HexagonII::HSIG_A;
3917 break;
3918 case Hexagon::C2_cmpeqi:
3919 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003920 DstReg = MI.getOperand(0).getReg();
3921 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003922 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3923 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003924 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003925 return HexagonII::HSIG_A;
3926 break;
3927 case Hexagon::A2_combineii:
3928 case Hexagon::A4_combineii:
3929 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003930 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003931 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003932 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3933 (MI.getOperand(1).isGlobal() &&
3934 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3935 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3936 (MI.getOperand(2).isGlobal() &&
3937 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003938 return HexagonII::HSIG_A;
3939 break;
3940 case Hexagon::A4_combineri:
3941 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003942 DstReg = MI.getOperand(0).getReg();
3943 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003944 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003945 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3946 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003947 return HexagonII::HSIG_A;
3948 break;
3949 case Hexagon::A4_combineir:
3950 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003951 DstReg = MI.getOperand(0).getReg();
3952 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003953 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003954 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3955 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003956 return HexagonII::HSIG_A;
3957 break;
3958 case Hexagon::A2_sxtb:
3959 case Hexagon::A2_sxth:
3960 case Hexagon::A2_zxtb:
3961 case Hexagon::A2_zxth:
3962 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003963 DstReg = MI.getOperand(0).getReg();
3964 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003965 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3966 return HexagonII::HSIG_A;
3967 break;
3968 }
3969
3970 return HexagonII::HSIG_None;
3971}
3972
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003973short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3974 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003975}
3976
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003977// Return first non-debug instruction in the basic block.
3978MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3979 const {
3980 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003981 MachineInstr &MI = *MII;
3982 if (MI.isDebugValue())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003983 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003984 return &MI;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003985 }
3986 return nullptr;
3987}
3988
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003989unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003990 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003991 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3992 // still have a MinLatency property, which getStageLatency checks.
3993 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003994 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003995
3996 // Get the latency embedded in the itinerary. If we're not using timing class
3997 // latencies or if we using BSB scheduling, then restrict the maximum latency
3998 // to 1 (that is, either 0 or 1).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003999 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004000 return 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004001 unsigned Latency = ItinData->getStageLatency(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004002 if (!EnableTimingClassLatency ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004003 MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>().
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004004 useBSBScheduling())
4005 if (Latency > 1)
4006 Latency = 1;
4007 return Latency;
4008}
4009
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004010// inverts the predication logic.
4011// p -> NotP
4012// NotP -> P
4013bool HexagonInstrInfo::getInvertedPredSense(
4014 SmallVectorImpl<MachineOperand> &Cond) const {
4015 if (Cond.empty())
4016 return false;
4017 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4018 Cond[0].setImm(Opc);
4019 return true;
4020}
4021
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004022unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4023 int InvPredOpcode;
4024 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4025 : Hexagon::getTruePredOpcode(Opc);
4026 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4027 return InvPredOpcode;
4028
4029 llvm_unreachable("Unexpected predicated instruction");
4030}
4031
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004032// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004033int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4034 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004035 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4036 & HexagonII::ExtentSignedMask;
4037 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4038 & HexagonII::ExtentBitsMask;
4039
4040 if (isSigned) // if value is signed
4041 return ~(-1U << (bits - 1));
4042 else
4043 return ~(-1U << bits);
4044}
4045
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004046unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
4047 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004048 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
4049}
4050
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004051// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004052int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4053 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004054 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4055 & HexagonII::ExtentSignedMask;
4056 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4057 & HexagonII::ExtentBitsMask;
4058
4059 if (isSigned) // if value is signed
4060 return -1U << (bits - 1);
4061 else
4062 return 0;
4063}
4064
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004065// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004066short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004067 // Check if the instruction has a register form that uses register in place
4068 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004069 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004070 if (NonExtOpcode >= 0)
4071 return NonExtOpcode;
4072
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004073 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004074 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004075 switch (getAddrMode(MI)) {
4076 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004077 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004078 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004079 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004080 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004081 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004082
Jyotsna Verma84256432013-03-01 17:37:13 +00004083 default:
4084 return -1;
4085 }
4086 }
4087 return -1;
4088}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004089
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004090bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004091 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004092 if (Cond.empty())
4093 return false;
4094 assert(Cond.size() == 2);
4095 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004096 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4097 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004098 }
4099 PredReg = Cond[1].getReg();
4100 PredRegPos = 1;
4101 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4102 PredRegFlags = 0;
4103 if (Cond[1].isImplicit())
4104 PredRegFlags = RegState::Implicit;
4105 if (Cond[1].isUndef())
4106 PredRegFlags |= RegState::Undef;
4107 return true;
4108}
4109
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004110short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4111 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004112}
4113
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004114short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4115 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004116}
4117
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004118// Return the number of bytes required to encode the instruction.
4119// Hexagon instructions are fixed length, 4 bytes, unless they
4120// use a constant extender, which requires another 4 bytes.
4121// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004122unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4123 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004124 return 0;
4125
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004126 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004127 if (!Size)
4128 // Assume the default insn size in case it cannot be determined
4129 // for whatever reason.
4130 Size = HEXAGON_INSTR_SIZE;
4131
4132 if (isConstExtended(MI) || isExtended(MI))
4133 Size += HEXAGON_INSTR_SIZE;
4134
4135 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004136 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4137 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004138 const MachineFunction *MF = MBB.getParent();
4139 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4140
4141 // Count the number of register definitions to find the asm string.
4142 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004143 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004144 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004145 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004146
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004147 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004148 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004149 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004150 Size = getInlineAsmLength(AsmStr, *MAI);
4151 }
4152
4153 return Size;
4154}
4155
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004156uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4157 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004158 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4159}
4160
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004161unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4162 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004163 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004164 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004165
4166 return IS.getUnits();
4167}
4168
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004169// Calculate size of the basic block without debug instructions.
4170unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4171 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4172}
4173
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004174unsigned HexagonInstrInfo::nonDbgBundleSize(
4175 MachineBasicBlock::const_iterator BundleHead) const {
4176 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004177 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004178 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004179 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004180}
4181
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004182/// immediateExtend - Changes the instruction in place to one using an immediate
4183/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004184void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004185 assert((isExtendable(MI)||isConstExtended(MI)) &&
4186 "Instruction must be extendable");
4187 // Find which operand is extendable.
4188 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004189 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004190 // This needs to be something we understand.
4191 assert((MO.isMBB() || MO.isImm()) &&
4192 "Branch with unknown extendable field type");
4193 // Mark given operand as extended.
4194 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4195}
4196
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004197bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004198 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004199 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004200 << NewTarget->getNumber(); MI.dump(););
4201 assert(MI.isBranch());
4202 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4203 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004204 // In general branch target is the last operand,
4205 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004206 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004207 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004208 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4209 MI.getOperand(TargetPos).setMBB(NewTarget);
4210 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004211 NewOpcode = reversePrediction(NewOpcode);
4212 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004213 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004214 return true;
4215}
4216
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004217void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4218 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4219 MachineFunction::iterator A = MF.begin();
4220 MachineBasicBlock &B = *A;
4221 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004222 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004223 MachineInstr *NewMI;
4224
4225 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4226 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004227 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004228 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4229 " Class: " << NewMI->getDesc().getSchedClass());
4230 NewMI->eraseFromParent();
4231 }
4232 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4233}
4234
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004235// inverts the predication logic.
4236// p -> NotP
4237// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004238bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4239 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4240 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004241 return true;
4242}
4243
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004244// Reverse the branch prediction.
4245unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4246 int PredRevOpcode = -1;
4247 if (isPredictedTaken(Opcode))
4248 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4249 else
4250 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4251 assert(PredRevOpcode > 0);
4252 return PredRevOpcode;
4253}
4254
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004255// TODO: Add more rigorous validation.
4256bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4257 const {
4258 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4259}
4260
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004261short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4262 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004263}