blob: 105b6caf98fb007cfd37e7a13b20a78723f4230b [file] [log] [blame]
Eugene Zelenko3b873362017-09-28 22:27:31 +00001//===- HexagonInstrInfo.cpp - Hexagon Instruction Information -------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Hexagon implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "HexagonInstrInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000015#include "Hexagon.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000016#include "HexagonFrameLowering.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000017#include "HexagonHazardRecognizer.h"
Craig Topperb25fda92012-03-17 18:46:09 +000018#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "HexagonSubtarget.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000020#include "llvm/ADT/ArrayRef.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000021#include "llvm/ADT/SmallPtrSet.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000022#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000023#include "llvm/ADT/StringRef.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000024#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000025#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000026#include "llvm/CodeGen/MachineBasicBlock.h"
27#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000029#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000032#include "llvm/CodeGen/MachineInstrBundle.h"
33#include "llvm/CodeGen/MachineLoopInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000035#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000037#include "llvm/CodeGen/MachineValueType.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000038#include "llvm/CodeGen/ScheduleDAG.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000039#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/CodeGen/TargetOpcodes.h"
41#include "llvm/CodeGen/TargetRegisterInfo.h"
42#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000043#include "llvm/IR/DebugLoc.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000044#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000045#include "llvm/MC/MCInstrDesc.h"
46#include "llvm/MC/MCInstrItineraries.h"
47#include "llvm/MC/MCRegisterInfo.h"
48#include "llvm/Support/BranchProbability.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000049#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000050#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000051#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000052#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000053#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000054#include "llvm/Target/TargetMachine.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000055#include <cassert>
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000056#include <cctype>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000057#include <cstdint>
58#include <cstring>
59#include <iterator>
Eugene Zelenko3b873362017-09-28 22:27:31 +000060#include <string>
61#include <utility>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000062
Tony Linthicum1213a7a2011-12-12 21:14:40 +000063using namespace llvm;
64
Chandler Carruthe96dd892014-04-21 22:55:11 +000065#define DEBUG_TYPE "hexagon-instrinfo"
66
Chandler Carruthd174b722014-04-22 02:03:14 +000067#define GET_INSTRINFO_CTOR_DTOR
68#define GET_INSTRMAP_INFO
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +000069#include "HexagonDepTimingClasses.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000070#include "HexagonGenDFAPacketizer.inc"
71#include "HexagonGenInstrInfo.inc"
Chandler Carruthd174b722014-04-22 02:03:14 +000072
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000073cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000074 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
75 "packetization boundary."));
76
77static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
78 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
79
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000080static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
81 cl::Hidden, cl::ZeroOrMore, cl::init(false),
82 cl::desc("Disable schedule adjustment for new value stores."));
83
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000084static cl::opt<bool> EnableTimingClassLatency(
85 "enable-timing-class-latency", cl::Hidden, cl::init(false),
86 cl::desc("Enable timing class latency"));
87
88static cl::opt<bool> EnableALUForwarding(
89 "enable-alu-forwarding", cl::Hidden, cl::init(true),
90 cl::desc("Enable vec alu forwarding"));
91
92static cl::opt<bool> EnableACCForwarding(
93 "enable-acc-forwarding", cl::Hidden, cl::init(true),
94 cl::desc("Enable vec acc forwarding"));
95
96static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
97 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
98
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000099static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
100 cl::init(true), cl::Hidden, cl::ZeroOrMore,
101 cl::desc("Use the DFA based hazard recognizer."));
102
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103/// Constants for Hexagon instructions.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000105const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000107const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000108const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000109const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000111const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000112const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000113const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000115// Pin the vtable to this file.
116void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000117
118HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000119 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
120 Subtarget(ST) {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000121
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000122static bool isIntRegForSubInst(unsigned Reg) {
123 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
124 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000125}
126
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000127static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000128 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
129 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000130}
131
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000132/// Calculate number of instructions excluding the debug instructions.
133static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
134 MachineBasicBlock::const_instr_iterator MIE) {
135 unsigned Count = 0;
136 for (; MIB != MIE; ++MIB) {
137 if (!MIB->isDebugValue())
138 ++Count;
139 }
140 return Count;
141}
142
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000143/// Find the hardware loop instruction used to set-up the specified loop.
144/// On Hexagon, we have two instructions used to set-up the hardware loop
145/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
146/// to indicate the end of a loop.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000147static MachineInstr *findLoopInstr(MachineBasicBlock *BB, unsigned EndLoopOp,
148 MachineBasicBlock *TargetBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000149 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000150 unsigned LOOPi;
151 unsigned LOOPr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000152 if (EndLoopOp == Hexagon::ENDLOOP0) {
153 LOOPi = Hexagon::J2_loop0i;
154 LOOPr = Hexagon::J2_loop0r;
155 } else { // EndLoopOp == Hexagon::EndLOOP1
156 LOOPi = Hexagon::J2_loop1i;
157 LOOPr = Hexagon::J2_loop1r;
158 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000159
Brendon Cahoondf43e682015-05-08 16:16:29 +0000160 // The loop set-up instruction will be in a predecessor block
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000161 for (MachineBasicBlock *PB : BB->predecessors()) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000162 // If this has been visited, already skip it.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000163 if (!Visited.insert(PB).second)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000164 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000165 if (PB == BB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000166 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000167 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
168 unsigned Opc = I->getOpcode();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000169 if (Opc == LOOPi || Opc == LOOPr)
170 return &*I;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000171 // We've reached a different loop, which means the loop01 has been
172 // removed.
173 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000174 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000175 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000176 // Check the predecessors for the LOOP instruction.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000177 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
178 return Loop;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000179 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000180 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000181}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000182
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000183/// Gather register def/uses from MI.
184/// This treats possible (predicated) defs as actually happening ones
185/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000186static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000187 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
188 Defs.clear();
189 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000190
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000191 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
192 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000193
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000194 if (!MO.isReg())
195 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000196
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000197 unsigned Reg = MO.getReg();
198 if (!Reg)
199 continue;
200
201 if (MO.isUse())
202 Uses.push_back(MO.getReg());
203
204 if (MO.isDef())
205 Defs.push_back(MO.getReg());
206 }
207}
208
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000209// Position dependent, so check twice for swap.
210static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
211 switch (Ga) {
212 case HexagonII::HSIG_None:
213 default:
214 return false;
215 case HexagonII::HSIG_L1:
216 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
217 case HexagonII::HSIG_L2:
218 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
219 Gb == HexagonII::HSIG_A);
220 case HexagonII::HSIG_S1:
221 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
222 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
223 case HexagonII::HSIG_S2:
224 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
225 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
226 Gb == HexagonII::HSIG_A);
227 case HexagonII::HSIG_A:
228 return (Gb == HexagonII::HSIG_A);
229 case HexagonII::HSIG_Compound:
230 return (Gb == HexagonII::HSIG_Compound);
231 }
232 return false;
233}
234
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000235/// isLoadFromStackSlot - If the specified machine instruction is a direct
236/// load from a stack slot, return the virtual or physical register number of
237/// the destination along with the FrameIndex of the loaded stack slot. If
238/// not, return 0. This predicate must return 0 if the instruction has
239/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000240unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000241 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000242 switch (MI.getOpcode()) {
243 default:
244 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000245 case Hexagon::L2_loadri_io:
246 case Hexagon::L2_loadrd_io:
247 case Hexagon::V6_vL32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +0000248 case Hexagon::V6_vL32b_nt_ai:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000249 case Hexagon::V6_vL32Ub_ai:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000250 case Hexagon::LDriw_pred:
251 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000252 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000253 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000254 case Hexagon::PS_vloadrw_nt_ai: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000255 const MachineOperand OpFI = MI.getOperand(1);
256 if (!OpFI.isFI())
257 return 0;
258 const MachineOperand OpOff = MI.getOperand(2);
259 if (!OpOff.isImm() || OpOff.getImm() != 0)
260 return 0;
261 FrameIndex = OpFI.getIndex();
262 return MI.getOperand(0).getReg();
263 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000264
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000265 case Hexagon::L2_ploadrit_io:
266 case Hexagon::L2_ploadrif_io:
267 case Hexagon::L2_ploadrdt_io:
268 case Hexagon::L2_ploadrdf_io: {
269 const MachineOperand OpFI = MI.getOperand(2);
270 if (!OpFI.isFI())
271 return 0;
272 const MachineOperand OpOff = MI.getOperand(3);
273 if (!OpOff.isImm() || OpOff.getImm() != 0)
274 return 0;
275 FrameIndex = OpFI.getIndex();
276 return MI.getOperand(0).getReg();
277 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000278 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000279
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000280 return 0;
281}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000282
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000283/// isStoreToStackSlot - If the specified machine instruction is a direct
284/// store to a stack slot, return the virtual or physical register number of
285/// the source reg along with the FrameIndex of the loaded stack slot. If
286/// not, return 0. This predicate must return 0 if the instruction has
287/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000288unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000289 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000290 switch (MI.getOpcode()) {
291 default:
292 break;
293 case Hexagon::S2_storerb_io:
294 case Hexagon::S2_storerh_io:
295 case Hexagon::S2_storeri_io:
296 case Hexagon::S2_storerd_io:
297 case Hexagon::V6_vS32b_ai:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000298 case Hexagon::V6_vS32Ub_ai:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000299 case Hexagon::STriw_pred:
300 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000301 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000302 case Hexagon::PS_vstorerw_ai: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000303 const MachineOperand &OpFI = MI.getOperand(0);
304 if (!OpFI.isFI())
305 return 0;
306 const MachineOperand &OpOff = MI.getOperand(1);
307 if (!OpOff.isImm() || OpOff.getImm() != 0)
308 return 0;
309 FrameIndex = OpFI.getIndex();
310 return MI.getOperand(2).getReg();
311 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000312
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000313 case Hexagon::S2_pstorerbt_io:
314 case Hexagon::S2_pstorerbf_io:
315 case Hexagon::S2_pstorerht_io:
316 case Hexagon::S2_pstorerhf_io:
317 case Hexagon::S2_pstorerit_io:
318 case Hexagon::S2_pstorerif_io:
319 case Hexagon::S2_pstorerdt_io:
320 case Hexagon::S2_pstorerdf_io: {
321 const MachineOperand &OpFI = MI.getOperand(1);
322 if (!OpFI.isFI())
323 return 0;
324 const MachineOperand &OpOff = MI.getOperand(2);
325 if (!OpOff.isImm() || OpOff.getImm() != 0)
326 return 0;
327 FrameIndex = OpFI.getIndex();
328 return MI.getOperand(3).getReg();
329 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000330 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000331
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000332 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000333}
334
Krzysztof Parzyszek275ffa42018-01-23 19:08:40 +0000335/// This function checks if the instruction or bundle of instructions
336/// has load from stack slot and returns frameindex and machine memory
337/// operand of that instruction if true.
338bool HexagonInstrInfo::hasLoadFromStackSlot(const MachineInstr &MI,
339 const MachineMemOperand *&MMO,
340 int &FrameIndex) const {
341 if (MI.isBundle()) {
342 const MachineBasicBlock *MBB = MI.getParent();
343 MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
344 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
345 if (TargetInstrInfo::hasLoadFromStackSlot(*MII, MMO, FrameIndex))
346 return true;
347 return false;
348 }
349
350 return TargetInstrInfo::hasLoadFromStackSlot(MI, MMO, FrameIndex);
351}
352
353/// This function checks if the instruction or bundle of instructions
354/// has store to stack slot and returns frameindex and machine memory
355/// operand of that instruction if true.
356bool HexagonInstrInfo::hasStoreToStackSlot(const MachineInstr &MI,
357 const MachineMemOperand *&MMO,
358 int &FrameIndex) const {
359 if (MI.isBundle()) {
360 const MachineBasicBlock *MBB = MI.getParent();
361 MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
362 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
363 if (TargetInstrInfo::hasStoreToStackSlot(*MII, MMO, FrameIndex))
364 return true;
365 return false;
366 }
367
368 return TargetInstrInfo::hasStoreToStackSlot(MI, MMO, FrameIndex);
369}
370
Brendon Cahoondf43e682015-05-08 16:16:29 +0000371/// This function can analyze one/two way branching only and should (mostly) be
372/// called by target independent side.
373/// First entry is always the opcode of the branching instruction, except when
374/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
375/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
376/// e.g. Jump_c p will have
377/// Cond[0] = Jump_c
378/// Cond[1] = p
379/// HW-loop ENDLOOP:
380/// Cond[0] = ENDLOOP
381/// Cond[1] = MBB
382/// New value jump:
383/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
384/// Cond[1] = R
385/// Cond[2] = Imm
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000386bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000387 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000388 MachineBasicBlock *&FBB,
389 SmallVectorImpl<MachineOperand> &Cond,
390 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000391 TBB = nullptr;
392 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000393 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000394
395 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000396 MachineBasicBlock::instr_iterator I = MBB.instr_end();
397 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 return false;
399
400 // A basic block may looks like this:
401 //
402 // [ insn
403 // EH_LABEL
404 // insn
405 // insn
406 // insn
407 // EH_LABEL
408 // insn ]
409 //
410 // It has two succs but does not have a terminator
411 // Don't know how to handle it.
412 do {
413 --I;
414 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000415 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000416 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000417 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000418
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000419 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000420 --I;
421
422 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000423 if (I == MBB.instr_begin())
424 return false;
425 --I;
426 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000427
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000428 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
429 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000430 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000431 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000432 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000433 DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000434 I->eraseFromParent();
435 I = MBB.instr_end();
436 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000437 return false;
438 --I;
439 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000440 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000441 return false;
442
443 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000444 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000445 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000446 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000447 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000448 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000449 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000450 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000451 else
452 // This is a third branch.
453 return true;
454 }
455 if (I == MBB.instr_begin())
456 break;
457 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000458 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000459
460 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000461 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
462 // If the branch target is not a basic block, it could be a tail call.
463 // (It is, if the target is a function.)
464 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
465 return true;
466 if (SecLastOpcode == Hexagon::J2_jump &&
467 !SecondLastInst->getOperand(0).isMBB())
468 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000469
470 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000471 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000472
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000473 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
474 return true;
475
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000476 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000477 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000478 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000479 TBB = LastInst->getOperand(0).getMBB();
480 return false;
481 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000482 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000483 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000484 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000485 Cond.push_back(LastInst->getOperand(0));
486 return false;
487 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000488 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000489 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000490 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000491 Cond.push_back(LastInst->getOperand(0));
492 return false;
493 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000494 // Only supporting rr/ri versions of new-value jumps.
495 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
496 TBB = LastInst->getOperand(2).getMBB();
497 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
498 Cond.push_back(LastInst->getOperand(0));
499 Cond.push_back(LastInst->getOperand(1));
500 return false;
501 }
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000502 DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000503 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000504 // Otherwise, don't know what this is.
505 return true;
506 }
507
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000508 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000509 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000510 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000511 if (!SecondLastInst->getOperand(1).isMBB())
512 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000513 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000514 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000515 Cond.push_back(SecondLastInst->getOperand(0));
516 FBB = LastInst->getOperand(0).getMBB();
517 return false;
518 }
519
Brendon Cahoondf43e682015-05-08 16:16:29 +0000520 // Only supporting rr/ri versions of new-value jumps.
521 if (SecLastOpcodeHasNVJump &&
522 (SecondLastInst->getNumExplicitOperands() == 3) &&
523 (LastOpcode == Hexagon::J2_jump)) {
524 TBB = SecondLastInst->getOperand(2).getMBB();
525 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
526 Cond.push_back(SecondLastInst->getOperand(0));
527 Cond.push_back(SecondLastInst->getOperand(1));
528 FBB = LastInst->getOperand(0).getMBB();
529 return false;
530 }
531
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000532 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
533 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000534 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000535 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000536 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 if (AllowModify)
538 I->eraseFromParent();
539 return false;
540 }
541
Brendon Cahoondf43e682015-05-08 16:16:29 +0000542 // If the block ends with an ENDLOOP, and J2_jump, handle it.
543 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000544 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000545 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000546 Cond.push_back(SecondLastInst->getOperand(0));
547 FBB = LastInst->getOperand(0).getMBB();
548 return false;
549 }
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000550 DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000551 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000552 // Otherwise, can't handle this.
553 return true;
554}
555
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000556unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000557 int *BytesRemoved) const {
558 assert(!BytesRemoved && "code size not handled");
559
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000560 DEBUG(dbgs() << "\nRemoving branches out of " << printMBBReference(MBB));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000562 unsigned Count = 0;
563 while (I != MBB.begin()) {
564 --I;
565 if (I->isDebugValue())
566 continue;
567 // Only removing branches from end of MBB.
568 if (!I->isBranch())
569 return Count;
570 if (Count && (I->getOpcode() == Hexagon::J2_jump))
571 llvm_unreachable("Malformed basic block: unconditional branch not last");
572 MBB.erase(&MBB.back());
573 I = MBB.end();
574 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000575 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000576 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000577}
578
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000579unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000580 MachineBasicBlock *TBB,
581 MachineBasicBlock *FBB,
582 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000583 const DebugLoc &DL,
584 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000585 unsigned BOpc = Hexagon::J2_jump;
586 unsigned BccOpc = Hexagon::J2_jumpt;
587 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000588 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000589 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000590
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000591 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000592 // If we want to reverse the branch an odd number of times, we want
593 // J2_jumpf.
594 if (!Cond.empty() && Cond[0].isImm())
595 BccOpc = Cond[0].getImm();
596
597 if (!FBB) {
598 if (Cond.empty()) {
599 // Due to a bug in TailMerging/CFG Optimization, we need to add a
600 // special case handling of a predicated jump followed by an
601 // unconditional jump. If not, Tail Merging and CFG Optimization go
602 // into an infinite loop.
603 MachineBasicBlock *NewTBB, *NewFBB;
604 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000605 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000606 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000607 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
608 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000609 reverseBranchCondition(Cond);
610 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000611 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000612 }
613 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
614 } else if (isEndLoopN(Cond[0].getImm())) {
615 int EndLoopOp = Cond[0].getImm();
616 assert(Cond[1].isMBB());
617 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
618 // Check for it, and change the BB target if needed.
619 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000620 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
621 VisitedBBs);
Eugene Zelenko3b873362017-09-28 22:27:31 +0000622 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000623 Loop->getOperand(0).setMBB(TBB);
624 // Add the ENDLOOP after the finding the LOOP0.
625 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
626 } else if (isNewValueJump(Cond[0].getImm())) {
627 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
628 // New value jump
629 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
630 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
631 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000632 DEBUG(dbgs() << "\nInserting NVJump for " << printMBBReference(MBB););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000633 if (Cond[2].isReg()) {
634 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
635 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
636 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
637 } else if(Cond[2].isImm()) {
638 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
639 addImm(Cond[2].getImm()).addMBB(TBB);
640 } else
641 llvm_unreachable("Invalid condition for branching");
642 } else {
643 assert((Cond.size() == 2) && "Malformed cond vector");
644 const MachineOperand &RO = Cond[1];
645 unsigned Flags = getUndefRegState(RO.isUndef());
646 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
647 }
648 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000649 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000650 assert((!Cond.empty()) &&
651 "Cond. cannot be empty when multiple branchings are required");
652 assert((!isNewValueJump(Cond[0].getImm())) &&
653 "NV-jump cannot be inserted with another branch");
654 // Special case for hardware loops. The condition is a basic block.
655 if (isEndLoopN(Cond[0].getImm())) {
656 int EndLoopOp = Cond[0].getImm();
657 assert(Cond[1].isMBB());
658 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
659 // Check for it, and change the BB target if needed.
660 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000661 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
662 VisitedBBs);
Eugene Zelenko3b873362017-09-28 22:27:31 +0000663 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000664 Loop->getOperand(0).setMBB(TBB);
665 // Add the ENDLOOP after the finding the LOOP0.
666 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
667 } else {
668 const MachineOperand &RO = Cond[1];
669 unsigned Flags = getUndefRegState(RO.isUndef());
670 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000671 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000672 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000673
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000674 return 2;
675}
676
Brendon Cahoon254f8892016-07-29 16:44:44 +0000677/// Analyze the loop code to find the loop induction variable and compare used
678/// to compute the number of iterations. Currently, we analyze loop that are
679/// controlled using hardware loops. In this case, the induction variable
680/// instruction is null. For all other cases, this function returns true, which
681/// means we're unable to analyze it.
682bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
683 MachineInstr *&IndVarInst,
684 MachineInstr *&CmpInst) const {
685
686 MachineBasicBlock *LoopEnd = L.getBottomBlock();
687 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
688 // We really "analyze" only hardware loops right now.
689 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
690 IndVarInst = nullptr;
691 CmpInst = &*I;
692 return false;
693 }
694 return true;
695}
696
697/// Generate code to reduce the loop iteration by one and check if the loop is
698/// finished. Return the value/register of the new loop count. this function
699/// assumes the nth iteration is peeled first.
700unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000701 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000702 SmallVectorImpl<MachineOperand> &Cond,
703 SmallVectorImpl<MachineInstr *> &PrevInsts,
704 unsigned Iter, unsigned MaxIter) const {
705 // We expect a hardware loop currently. This means that IndVar is set
706 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000707 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000708 && "Expecting a hardware loop");
709 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000710 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000711 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000712 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(),
713 Cmp.getOperand(0).getMBB(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000714 if (!Loop)
715 return 0;
716 // If the loop trip count is a compile-time value, then just change the
717 // value.
718 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
719 Loop->getOpcode() == Hexagon::J2_loop1i) {
720 int64_t Offset = Loop->getOperand(1).getImm();
721 if (Offset <= 1)
722 Loop->eraseFromParent();
723 else
724 Loop->getOperand(1).setImm(Offset - 1);
725 return Offset - 1;
726 }
727 // The loop trip count is a run-time value. We generate code to subtract
728 // one from the trip count, and update the loop instruction.
729 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
730 unsigned LoopCount = Loop->getOperand(1).getReg();
731 // Check if we're done with the loop.
732 unsigned LoopEnd = createVR(MF, MVT::i1);
733 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
734 addReg(LoopCount).addImm(1);
735 unsigned NewLoopCount = createVR(MF, MVT::i32);
736 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
737 addReg(LoopCount).addImm(-1);
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000738 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000739 // Update the previously generated instructions with the new loop counter.
740 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
741 E = PrevInsts.end(); I != E; ++I)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000742 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, HRI);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000743 PrevInsts.clear();
744 PrevInsts.push_back(NewCmp);
745 PrevInsts.push_back(NewAdd);
746 // Insert the new loop instruction if this is the last time the loop is
747 // decremented.
748 if (Iter == MaxIter)
749 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
750 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
751 // Delete the old loop instruction.
752 if (Iter == 0)
753 Loop->eraseFromParent();
754 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
755 Cond.push_back(NewCmp->getOperand(0));
756 return NewLoopCount;
757}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000758
759bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
760 unsigned NumCycles, unsigned ExtraPredCycles,
761 BranchProbability Probability) const {
762 return nonDbgBBSize(&MBB) <= 3;
763}
764
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000765bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
766 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
767 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
768 const {
769 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
770}
771
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000772bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
773 unsigned NumInstrs, BranchProbability Probability) const {
774 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000775}
776
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000777void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000778 MachineBasicBlock::iterator I,
779 const DebugLoc &DL, unsigned DestReg,
780 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000781 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000782 unsigned KillFlag = getKillRegState(KillSrc);
783
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000784 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000785 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000786 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000787 return;
788 }
789 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000790 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
791 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792 return;
793 }
794 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
795 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000796 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
797 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000798 return;
799 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000800 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000801 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000802 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
803 .addReg(SrcReg, KillFlag);
804 return;
805 }
806 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
807 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
808 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
809 .addReg(SrcReg, KillFlag);
810 return;
811 }
812 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
813 Hexagon::IntRegsRegClass.contains(SrcReg)) {
814 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
815 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000816 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000817 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000818 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
819 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000820 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
821 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000822 return;
823 }
824 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
825 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000826 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
827 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000828 return;
829 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000830 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
831 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000832 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
833 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000834 return;
835 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000836 if (Hexagon::HvxVRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000837 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000838 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000839 return;
840 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000841 if (Hexagon::HvxWRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000842 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
843 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000844 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000845 .addReg(HiSrc, KillFlag)
846 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000847 return;
848 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000849 if (Hexagon::HvxQRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000850 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
851 .addReg(SrcReg)
852 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000853 return;
854 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000855 if (Hexagon::HvxQRRegClass.contains(SrcReg) &&
856 Hexagon::HvxVRRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000857 llvm_unreachable("Unimplemented pred to vec");
858 return;
859 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000860 if (Hexagon::HvxQRRegClass.contains(DestReg) &&
861 Hexagon::HvxVRRegClass.contains(SrcReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000862 llvm_unreachable("Unimplemented vec to pred");
863 return;
864 }
Sirish Pande30804c22012-02-15 18:52:27 +0000865
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000866#ifndef NDEBUG
867 // Show the invalid registers to ease debugging.
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000868 dbgs() << "Invalid registers for copy in " << printMBBReference(MBB) << ": "
869 << printReg(DestReg, &HRI) << " = " << printReg(SrcReg, &HRI) << '\n';
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000870#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000871 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000872}
873
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000874void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
875 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
876 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000877 DebugLoc DL = MBB.findDebugLoc(I);
878 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000879 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000880 unsigned SlotAlign = MFI.getObjectAlignment(FI);
881 unsigned RegAlign = TRI->getSpillAlignment(*RC);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000882 unsigned KillFlag = getKillRegState(isKill);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000883 bool HasAlloca = MFI.hasVarSizedObjects();
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000884 const HexagonFrameLowering &HFI = *Subtarget.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000885
Alex Lorenze40c8a22015-08-11 23:09:45 +0000886 MachineMemOperand *MMO = MF.getMachineMemOperand(
887 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000888 MFI.getObjectSize(FI), SlotAlign);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000889
Craig Topperc7242e02012-04-20 07:30:17 +0000890 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000891 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000892 .addFrameIndex(FI).addImm(0)
893 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000894 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000895 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000896 .addFrameIndex(FI).addImm(0)
897 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000898 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000899 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000900 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000901 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000902 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
903 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
904 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000905 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000906 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000907 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000908 .addFrameIndex(FI).addImm(0)
909 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000910 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000911 // If there are variable-sized objects, spills will not be aligned.
912 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000913 SlotAlign = HFI.getStackAlignment();
914 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vS32Ub_ai
915 : Hexagon::V6_vS32b_ai;
916 MachineMemOperand *MMOA = MF.getMachineMemOperand(
917 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
918 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000919 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000920 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000921 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
922 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000923 // If there are variable-sized objects, spills will not be aligned.
924 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000925 SlotAlign = HFI.getStackAlignment();
926 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vstorerwu_ai
927 : Hexagon::PS_vstorerw_ai;
928 MachineMemOperand *MMOA = MF.getMachineMemOperand(
929 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
930 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000931 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000932 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000933 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000934 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000935 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000936 }
937}
938
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000939void HexagonInstrInfo::loadRegFromStackSlot(
940 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
941 int FI, const TargetRegisterClass *RC,
942 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000943 DebugLoc DL = MBB.findDebugLoc(I);
944 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000945 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000946 unsigned SlotAlign = MFI.getObjectAlignment(FI);
947 unsigned RegAlign = TRI->getSpillAlignment(*RC);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000948 bool HasAlloca = MFI.hasVarSizedObjects();
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +0000949 const HexagonFrameLowering &HFI = *Subtarget.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950
Alex Lorenze40c8a22015-08-11 23:09:45 +0000951 MachineMemOperand *MMO = MF.getMachineMemOperand(
952 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000953 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000954
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000955 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000956 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000957 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000958 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000959 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000960 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000961 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000962 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000963 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
964 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
965 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
966 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000967 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000968 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000969 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000970 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000971 // If there are variable-sized objects, spills will not be aligned.
972 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000973 SlotAlign = HFI.getStackAlignment();
974 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vL32Ub_ai
975 : Hexagon::V6_vL32b_ai;
976 MachineMemOperand *MMOA = MF.getMachineMemOperand(
977 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
978 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000979 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000980 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
981 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000982 // If there are variable-sized objects, spills will not be aligned.
983 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000984 SlotAlign = HFI.getStackAlignment();
985 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vloadrwu_ai
986 : Hexagon::PS_vloadrw_ai;
987 MachineMemOperand *MMOA = MF.getMachineMemOperand(
988 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
989 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000990 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000991 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000992 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000993 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000994 }
995}
996
Ron Lieberman88159e52016-09-02 22:56:24 +0000997static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
998 const MachineBasicBlock &B = *MI.getParent();
999 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +00001000 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +00001001 for (auto I = B.rbegin(); I != E; ++I)
1002 Regs.stepBackward(*I);
1003}
1004
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001005/// expandPostRAPseudo - This function is called for all pseudo instructions
1006/// that remain after register allocation. Many pseudo instructions are
1007/// created to help register allocation. This is the place to convert them
1008/// into real instructions. The target can edit MI in place, or it can insert
1009/// new instructions and erase MI. The function should return true if
1010/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001011bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001012 MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001013 MachineFunction &MF = *MBB.getParent();
1014 MachineRegisterInfo &MRI = MF.getRegInfo();
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001015 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001016 DebugLoc DL = MI.getDebugLoc();
1017 unsigned Opc = MI.getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001018
1019 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001020 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001021 MachineOperand &MD = MI.getOperand(0);
1022 MachineOperand &MS = MI.getOperand(1);
1023 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001024 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1025 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001026 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001027 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001028 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001029 return true;
1030 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001031 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001032 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001033 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001034 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001035 MBB.erase(MI);
1036 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001037 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001038 unsigned SrcReg = MI.getOperand(1).getReg();
1039 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001040 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1041 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001042 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1043 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001044 MBB.erase(MI);
1045 return true;
1046 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001047 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001048 unsigned SrcReg = MI.getOperand(1).getReg();
1049 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001050 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001051 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001052 MBB.erase(MI);
1053 MRI.clearKillFlags(SrcSubLo);
1054 return true;
1055 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001056 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001057 unsigned SrcReg = MI.getOperand(1).getReg();
1058 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001059 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001060 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001061 MBB.erase(MI);
1062 MRI.clearKillFlags(SrcSubHi);
1063 return true;
1064 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001065 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001066 case Hexagon::PS_vstorerwu_ai: {
1067 bool Aligned = Opc == Hexagon::PS_vstorerw_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001068 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001069 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1070 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001071 unsigned NewOpc = Aligned ? Hexagon::V6_vS32b_ai : Hexagon::V6_vS32Ub_ai;
1072 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001073
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001074 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001075 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001076 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001077 .addImm(MI.getOperand(1).getImm())
1078 .addReg(SrcSubLo)
1079 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001080 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001081 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001082 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001083 // The Vectors are indexed in multiples of vector size.
1084 .addImm(MI.getOperand(1).getImm() + Offset)
1085 .addReg(SrcSubHi)
1086 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001087 MBB.erase(MI);
1088 return true;
1089 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001090 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001091 case Hexagon::PS_vloadrwu_ai: {
1092 bool Aligned = Opc == Hexagon::PS_vloadrw_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001093 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001094 unsigned NewOpc = Aligned ? Hexagon::V6_vL32b_ai : Hexagon::V6_vL32Ub_ai;
1095 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1096
Diana Picus116bbab2017-01-13 09:58:52 +00001097 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1098 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Krzysztof Parzyszek4be9d922017-05-03 15:26:13 +00001099 .add(MI.getOperand(1))
1100 .addImm(MI.getOperand(2).getImm())
1101 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001102 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001103 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1104 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001105 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001106 .addImm(MI.getOperand(2).getImm() + Offset)
1107 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001108 MBB.erase(MI);
1109 return true;
1110 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001111 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001112 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001113 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1114 .addReg(Reg, RegState::Undef)
1115 .addReg(Reg, RegState::Undef);
1116 MBB.erase(MI);
1117 return true;
1118 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001119 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001120 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001121 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1122 .addReg(Reg, RegState::Undef)
1123 .addReg(Reg, RegState::Undef);
1124 MBB.erase(MI);
1125 return true;
1126 }
Krzysztof Parzyszek9b48e8d2018-02-09 19:10:46 +00001127 case Hexagon::PS_qtrue: {
1128 BuildMI(MBB, MI, DL, get(Hexagon::V6_veqw), MI.getOperand(0).getReg())
1129 .addReg(Hexagon::V0, RegState::Undef)
1130 .addReg(Hexagon::V0, RegState::Undef);
1131 MBB.erase(MI);
1132 return true;
1133 }
1134 case Hexagon::PS_qfalse: {
1135 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgtw), MI.getOperand(0).getReg())
1136 .addReg(Hexagon::V0, RegState::Undef)
1137 .addReg(Hexagon::V0, RegState::Undef);
1138 MBB.erase(MI);
1139 return true;
1140 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001141 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001142 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001143 unsigned DstReg = MI.getOperand(0).getReg();
1144 unsigned Src1Reg = MI.getOperand(1).getReg();
1145 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001146 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1147 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1148 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1149 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001150 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001151 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001152 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001153 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001154 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001155 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001156 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001157 .addReg(Src2SubLo);
1158 MBB.erase(MI);
1159 MRI.clearKillFlags(Src1SubHi);
1160 MRI.clearKillFlags(Src1SubLo);
1161 MRI.clearKillFlags(Src2SubHi);
1162 MRI.clearKillFlags(Src2SubLo);
1163 return true;
1164 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001165 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001166 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001167 unsigned DstReg = MI.getOperand(0).getReg();
1168 unsigned Src1Reg = MI.getOperand(1).getReg();
1169 unsigned Src2Reg = MI.getOperand(2).getReg();
1170 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001171 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1172 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1173 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1174 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1175 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1176 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001177 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001178 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001179 .addReg(Src1SubHi)
1180 .addReg(Src2SubHi)
1181 .addReg(Src3SubHi);
1182 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001183 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001184 .addReg(Src1SubLo)
1185 .addReg(Src2SubLo)
1186 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001187 MBB.erase(MI);
1188 MRI.clearKillFlags(Src1SubHi);
1189 MRI.clearKillFlags(Src1SubLo);
1190 MRI.clearKillFlags(Src2SubHi);
1191 MRI.clearKillFlags(Src2SubLo);
1192 MRI.clearKillFlags(Src3SubHi);
1193 MRI.clearKillFlags(Src3SubLo);
1194 return true;
1195 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001196 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001197 const MachineOperand &Op0 = MI.getOperand(0);
1198 const MachineOperand &Op1 = MI.getOperand(1);
1199 const MachineOperand &Op2 = MI.getOperand(2);
1200 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001201 unsigned Rd = Op0.getReg();
1202 unsigned Pu = Op1.getReg();
1203 unsigned Rs = Op2.getReg();
1204 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001205 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001206 unsigned K1 = getKillRegState(Op1.isKill());
1207 unsigned K2 = getKillRegState(Op2.isKill());
1208 unsigned K3 = getKillRegState(Op3.isKill());
1209 if (Rd != Rs)
1210 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1211 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1212 .addReg(Rs, K2);
1213 if (Rd != Rt)
1214 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1215 .addReg(Pu, K1)
1216 .addReg(Rt, K3);
1217 MBB.erase(MI);
1218 return true;
1219 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001220 case Hexagon::PS_vselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001221 const MachineOperand &Op0 = MI.getOperand(0);
1222 const MachineOperand &Op1 = MI.getOperand(1);
1223 const MachineOperand &Op2 = MI.getOperand(2);
1224 const MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001225 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001226 getLiveRegsAt(LiveAtMI, MI);
1227 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001228 unsigned PReg = Op1.getReg();
1229 assert(Op1.getSubReg() == 0);
1230 unsigned PState = getRegState(Op1);
1231
Ron Lieberman88159e52016-09-02 22:56:24 +00001232 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001233 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1234 : PState;
Ron Lieberman88159e52016-09-02 22:56:24 +00001235 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001236 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001237 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001238 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001239 if (IsDestLive)
1240 T.addReg(Op0.getReg(), RegState::Implicit);
1241 IsDestLive = true;
1242 }
1243 if (Op0.getReg() != Op3.getReg()) {
1244 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001245 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001246 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001247 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001248 if (IsDestLive)
1249 T.addReg(Op0.getReg(), RegState::Implicit);
1250 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001251 MBB.erase(MI);
1252 return true;
1253 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001254 case Hexagon::PS_wselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001255 MachineOperand &Op0 = MI.getOperand(0);
1256 MachineOperand &Op1 = MI.getOperand(1);
1257 MachineOperand &Op2 = MI.getOperand(2);
1258 MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001259 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001260 getLiveRegsAt(LiveAtMI, MI);
1261 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001262 unsigned PReg = Op1.getReg();
1263 assert(Op1.getSubReg() == 0);
1264 unsigned PState = getRegState(Op1);
Ron Lieberman88159e52016-09-02 22:56:24 +00001265
1266 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001267 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1268 : PState;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001269 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1270 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001271 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001272 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001273 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001274 .add(Op1)
1275 .addReg(SrcHi)
1276 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001277 if (IsDestLive)
1278 T.addReg(Op0.getReg(), RegState::Implicit);
1279 IsDestLive = true;
1280 }
1281 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001282 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1283 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001284 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001285 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001286 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001287 .addReg(SrcHi)
1288 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001289 if (IsDestLive)
1290 T.addReg(Op0.getReg(), RegState::Implicit);
1291 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001292 MBB.erase(MI);
1293 return true;
1294 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001295
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001296 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001297 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001298 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001299 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001300 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001301 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001302 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001303 case Hexagon::PS_jmprett:
1304 MI.setDesc(get(Hexagon::J2_jumprt));
1305 return true;
1306 case Hexagon::PS_jmpretf:
1307 MI.setDesc(get(Hexagon::J2_jumprf));
1308 return true;
1309 case Hexagon::PS_jmprettnewpt:
1310 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1311 return true;
1312 case Hexagon::PS_jmpretfnewpt:
1313 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1314 return true;
1315 case Hexagon::PS_jmprettnew:
1316 MI.setDesc(get(Hexagon::J2_jumprtnew));
1317 return true;
1318 case Hexagon::PS_jmpretfnew:
1319 MI.setDesc(get(Hexagon::J2_jumprfnew));
1320 return true;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001321
1322 case Hexagon::V6_vgathermh_pseudo:
1323 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermh))
1324 .add(MI.getOperand(1))
1325 .add(MI.getOperand(2))
1326 .add(MI.getOperand(3));
1327 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1328 .add(MI.getOperand(0))
1329 .addImm(0)
1330 .addReg(Hexagon::VTMP);
1331 MBB.erase(MI);
1332 return true;
1333
1334 case Hexagon::V6_vgathermw_pseudo:
1335 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermw))
1336 .add(MI.getOperand(1))
1337 .add(MI.getOperand(2))
1338 .add(MI.getOperand(3));
1339 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1340 .add(MI.getOperand(0))
1341 .addImm(0)
1342 .addReg(Hexagon::VTMP);
1343 MBB.erase(MI);
1344 return true;
1345
1346 case Hexagon::V6_vgathermhw_pseudo:
1347 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhw))
1348 .add(MI.getOperand(1))
1349 .add(MI.getOperand(2))
1350 .add(MI.getOperand(3));
1351 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1352 .add(MI.getOperand(0))
1353 .addImm(0)
1354 .addReg(Hexagon::VTMP);
1355 MBB.erase(MI);
1356 return true;
1357
1358 case Hexagon::V6_vgathermhq_pseudo:
1359 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhq))
1360 .add(MI.getOperand(1))
1361 .add(MI.getOperand(2))
1362 .add(MI.getOperand(3))
1363 .add(MI.getOperand(4));
1364 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1365 .add(MI.getOperand(0))
1366 .addImm(0)
1367 .addReg(Hexagon::VTMP);
1368 MBB.erase(MI);
1369 return true;
1370
1371 case Hexagon::V6_vgathermwq_pseudo:
1372 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermwq))
1373 .add(MI.getOperand(1))
1374 .add(MI.getOperand(2))
1375 .add(MI.getOperand(3))
1376 .add(MI.getOperand(4));
1377 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1378 .add(MI.getOperand(0))
1379 .addImm(0)
1380 .addReg(Hexagon::VTMP);
1381 MBB.erase(MI);
1382 return true;
1383
1384 case Hexagon::V6_vgathermhwq_pseudo:
1385 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhwq))
1386 .add(MI.getOperand(1))
1387 .add(MI.getOperand(2))
1388 .add(MI.getOperand(3))
1389 .add(MI.getOperand(4));
1390 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1391 .add(MI.getOperand(0))
1392 .addImm(0)
1393 .addReg(Hexagon::VTMP);
1394 MBB.erase(MI);
1395 return true;
1396
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001397 }
1398
1399 return false;
1400}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001401
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001402// We indicate that we want to reverse the branch by
1403// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001404bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001405 SmallVectorImpl<MachineOperand> &Cond) const {
1406 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001407 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001408 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1409 unsigned opcode = Cond[0].getImm();
1410 //unsigned temp;
1411 assert(get(opcode).isBranch() && "Should be a branching condition.");
1412 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001413 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001414 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1415 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001416 return false;
1417}
1418
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001419void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1420 MachineBasicBlock::iterator MI) const {
1421 DebugLoc DL;
1422 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1423}
1424
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001425bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1426 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001427}
1428
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001429// Returns true if an instruction is predicated irrespective of the predicate
1430// sense. For example, all of the following will return true.
1431// if (p0) R1 = add(R2, R3)
1432// if (!p0) R1 = add(R2, R3)
1433// if (p0.new) R1 = add(R2, R3)
1434// if (!p0.new) R1 = add(R2, R3)
1435// Note: New-value stores are not included here as in the current
1436// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001437bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1438 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001439 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001440}
1441
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001442bool HexagonInstrInfo::PredicateInstruction(
1443 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001444 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1445 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001446 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001447 return false;
1448 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001449 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001450 assert (isPredicable(MI) && "Expected predicable instruction");
1451 bool invertJump = predOpcodeHasNot(Cond);
1452
1453 // We have to predicate MI "in place", i.e. after this function returns,
1454 // MI will need to be transformed into a predicated form. To avoid com-
1455 // plicated manipulations with the operands (handling tied operands,
1456 // etc.), build a new temporary instruction, then overwrite MI with it.
1457
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001458 MachineBasicBlock &B = *MI.getParent();
1459 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001460 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1461 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001462 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001463 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001464 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001465 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1466 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001467 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001468 NOp++;
1469 }
1470
1471 unsigned PredReg, PredRegPos, PredRegFlags;
1472 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1473 (void)GotPredReg;
1474 assert(GotPredReg);
1475 T.addReg(PredReg, PredRegFlags);
1476 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001477 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001478
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001479 MI.setDesc(get(PredOpc));
1480 while (unsigned n = MI.getNumOperands())
1481 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001482 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001483 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001484
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001485 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001486 B.erase(TI);
1487
1488 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1489 MRI.clearKillFlags(PredReg);
1490 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001491}
1492
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001493bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1494 ArrayRef<MachineOperand> Pred2) const {
1495 // TODO: Fix this
1496 return false;
1497}
1498
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001499bool HexagonInstrInfo::DefinesPredicate(MachineInstr &MI,
1500 std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001501 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001502
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001503 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1504 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001505 if (MO.isReg()) {
1506 if (!MO.isDef())
1507 continue;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001508 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1509 if (RC == &Hexagon::PredRegsRegClass) {
1510 Pred.push_back(MO);
1511 return true;
1512 }
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001513 continue;
1514 } else if (MO.isRegMask()) {
1515 for (unsigned PR : Hexagon::PredRegsRegClass) {
1516 if (!MI.modifiesRegister(PR, &HRI))
1517 continue;
1518 Pred.push_back(MO);
1519 return true;
1520 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001521 }
1522 }
1523 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001524}
Andrew Trickd06df962012-02-01 22:13:57 +00001525
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001526bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001527 if (!MI.getDesc().isPredicable())
1528 return false;
1529
1530 if (MI.isCall() || isTailCall(MI)) {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001531 if (!Subtarget.usePredicatedCalls())
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001532 return false;
1533 }
Krzysztof Parzyszek8c53c952017-10-18 17:36:46 +00001534
1535 // HVX loads are not predicable on v60, but are on v62.
1536 if (!Subtarget.hasV62TOps()) {
1537 switch (MI.getOpcode()) {
1538 case Hexagon::V6_vL32b_ai:
1539 case Hexagon::V6_vL32b_pi:
1540 case Hexagon::V6_vL32b_ppu:
1541 case Hexagon::V6_vL32b_cur_ai:
1542 case Hexagon::V6_vL32b_cur_pi:
1543 case Hexagon::V6_vL32b_cur_ppu:
1544 case Hexagon::V6_vL32b_nt_ai:
1545 case Hexagon::V6_vL32b_nt_pi:
1546 case Hexagon::V6_vL32b_nt_ppu:
1547 case Hexagon::V6_vL32b_tmp_ai:
1548 case Hexagon::V6_vL32b_tmp_pi:
1549 case Hexagon::V6_vL32b_tmp_ppu:
1550 case Hexagon::V6_vL32b_nt_cur_ai:
1551 case Hexagon::V6_vL32b_nt_cur_pi:
1552 case Hexagon::V6_vL32b_nt_cur_ppu:
1553 case Hexagon::V6_vL32b_nt_tmp_ai:
1554 case Hexagon::V6_vL32b_nt_tmp_pi:
1555 case Hexagon::V6_vL32b_nt_tmp_ppu:
1556 return false;
1557 }
1558 }
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001559 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001560}
1561
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001562bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1563 const MachineBasicBlock *MBB,
1564 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001565 // Debug info is never a scheduling boundary. It's necessary to be explicit
1566 // due to the special treatment of IT instructions below, otherwise a
1567 // dbg_value followed by an IT will result in the IT instruction being
1568 // considered a scheduling hazard, which is wrong. It should be the actual
1569 // instruction preceding the dbg_value instruction(s), just like it is
1570 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001571 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001572 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001573
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001574 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001575 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001576 // Don't mess around with no return calls.
1577 if (doesNotReturn(MI))
1578 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001579 // If any of the block's successors is a landing pad, this could be a
1580 // throwing call.
1581 for (auto I : MBB->successors())
1582 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001583 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001584 }
1585
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001586 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001587 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001588 return true;
1589
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001590 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1591 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001592
1593 return false;
1594}
1595
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001596/// Measure the specified inline asm to determine an approximation of its
1597/// length.
1598/// Comments (which run till the next SeparatorString or newline) do not
1599/// count as an instruction.
1600/// Any other non-whitespace text is considered an instruction, with
1601/// multiple instructions separated by SeparatorString or newlines.
1602/// Variable-length instructions are not handled here; this function
1603/// may be overloaded in the target code to do that.
1604/// Hexagon counts the number of ##'s and adjust for that many
1605/// constant exenders.
1606unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1607 const MCAsmInfo &MAI) const {
1608 StringRef AStr(Str);
1609 // Count the number of instructions in the asm.
1610 bool atInsnStart = true;
1611 unsigned Length = 0;
1612 for (; *Str; ++Str) {
1613 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1614 strlen(MAI.getSeparatorString())) == 0)
1615 atInsnStart = true;
1616 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1617 Length += MAI.getMaxInstLength();
1618 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001619 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001620 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1621 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001622 atInsnStart = false;
1623 }
1624
1625 // Add to size number of constant extenders seen * 4.
1626 StringRef Occ("##");
1627 Length += AStr.count(Occ)*4;
1628 return Length;
1629}
1630
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001631ScheduleHazardRecognizer*
1632HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1633 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001634 if (UseDFAHazardRec)
1635 return new HexagonHazardRecognizer(II, this, Subtarget);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001636 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1637}
1638
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001639/// \brief For a comparison instruction, return the source registers in
1640/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1641/// compares against in CmpValue. Return true if the comparison instruction
1642/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001643bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1644 unsigned &SrcReg2, int &Mask,
1645 int &Value) const {
1646 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001647
1648 // Set mask and the first source register.
1649 switch (Opc) {
1650 case Hexagon::C2_cmpeq:
1651 case Hexagon::C2_cmpeqp:
1652 case Hexagon::C2_cmpgt:
1653 case Hexagon::C2_cmpgtp:
1654 case Hexagon::C2_cmpgtu:
1655 case Hexagon::C2_cmpgtup:
1656 case Hexagon::C4_cmpneq:
1657 case Hexagon::C4_cmplte:
1658 case Hexagon::C4_cmplteu:
1659 case Hexagon::C2_cmpeqi:
1660 case Hexagon::C2_cmpgti:
1661 case Hexagon::C2_cmpgtui:
1662 case Hexagon::C4_cmpneqi:
1663 case Hexagon::C4_cmplteui:
1664 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001665 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001666 Mask = ~0;
1667 break;
1668 case Hexagon::A4_cmpbeq:
1669 case Hexagon::A4_cmpbgt:
1670 case Hexagon::A4_cmpbgtu:
1671 case Hexagon::A4_cmpbeqi:
1672 case Hexagon::A4_cmpbgti:
1673 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001674 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001675 Mask = 0xFF;
1676 break;
1677 case Hexagon::A4_cmpheq:
1678 case Hexagon::A4_cmphgt:
1679 case Hexagon::A4_cmphgtu:
1680 case Hexagon::A4_cmpheqi:
1681 case Hexagon::A4_cmphgti:
1682 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001683 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001684 Mask = 0xFFFF;
1685 break;
1686 }
1687
1688 // Set the value/second source register.
1689 switch (Opc) {
1690 case Hexagon::C2_cmpeq:
1691 case Hexagon::C2_cmpeqp:
1692 case Hexagon::C2_cmpgt:
1693 case Hexagon::C2_cmpgtp:
1694 case Hexagon::C2_cmpgtu:
1695 case Hexagon::C2_cmpgtup:
1696 case Hexagon::A4_cmpbeq:
1697 case Hexagon::A4_cmpbgt:
1698 case Hexagon::A4_cmpbgtu:
1699 case Hexagon::A4_cmpheq:
1700 case Hexagon::A4_cmphgt:
1701 case Hexagon::A4_cmphgtu:
1702 case Hexagon::C4_cmpneq:
1703 case Hexagon::C4_cmplte:
1704 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001705 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001706 return true;
1707
1708 case Hexagon::C2_cmpeqi:
1709 case Hexagon::C2_cmpgtui:
1710 case Hexagon::C2_cmpgti:
1711 case Hexagon::C4_cmpneqi:
1712 case Hexagon::C4_cmplteui:
1713 case Hexagon::C4_cmpltei:
1714 case Hexagon::A4_cmpbeqi:
1715 case Hexagon::A4_cmpbgti:
1716 case Hexagon::A4_cmpbgtui:
1717 case Hexagon::A4_cmpheqi:
1718 case Hexagon::A4_cmphgti:
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001719 case Hexagon::A4_cmphgtui: {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001720 SrcReg2 = 0;
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001721 const MachineOperand &Op2 = MI.getOperand(2);
1722 if (!Op2.isImm())
1723 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001724 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001725 return true;
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001726 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001727 }
1728
1729 return false;
1730}
1731
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001732unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001733 const MachineInstr &MI,
1734 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001735 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001736}
1737
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001738DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1739 const TargetSubtargetInfo &STI) const {
1740 const InstrItineraryData *II = STI.getInstrItineraryData();
1741 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1742}
1743
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001744// Inspired by this pair:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001745// %r13 = L2_loadri_io %r29, 136; mem:LD4[FixedStack0]
1746// S2_storeri_io %r29, 132, killed %r1; flags: mem:ST4[FixedStack1]
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001747// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001748bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1749 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001750 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1751 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001752 return false;
1753
1754 // Instructions that are pure loads, not loads and stores like memops are not
1755 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001756 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001757 return true;
1758
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001759 // Get the base register in MIa.
1760 unsigned BasePosA, OffsetPosA;
1761 if (!getBaseAndOffsetPosition(MIa, BasePosA, OffsetPosA))
1762 return false;
1763 const MachineOperand &BaseA = MIa.getOperand(BasePosA);
1764 unsigned BaseRegA = BaseA.getReg();
1765 unsigned BaseSubA = BaseA.getSubReg();
1766
1767 // Get the base register in MIb.
1768 unsigned BasePosB, OffsetPosB;
1769 if (!getBaseAndOffsetPosition(MIb, BasePosB, OffsetPosB))
1770 return false;
1771 const MachineOperand &BaseB = MIb.getOperand(BasePosB);
1772 unsigned BaseRegB = BaseB.getReg();
1773 unsigned BaseSubB = BaseB.getSubReg();
1774
1775 if (BaseRegA != BaseRegB || BaseSubA != BaseSubB)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001776 return false;
1777
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001778 // Get the access sizes.
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00001779 unsigned SizeA = getMemAccessSize(MIa);
1780 unsigned SizeB = getMemAccessSize(MIb);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001781
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001782 // Get the offsets. Handle immediates only for now.
1783 const MachineOperand &OffA = MIa.getOperand(OffsetPosA);
1784 const MachineOperand &OffB = MIb.getOperand(OffsetPosB);
1785 if (!MIa.getOperand(OffsetPosA).isImm() ||
1786 !MIb.getOperand(OffsetPosB).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001787 return false;
Krzysztof Parzyszekac019942017-07-19 19:17:32 +00001788 int OffsetA = isPostIncrement(MIa) ? 0 : OffA.getImm();
1789 int OffsetB = isPostIncrement(MIb) ? 0 : OffB.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001790
1791 // This is a mem access with the same base register and known offsets from it.
1792 // Reason about it.
1793 if (OffsetA > OffsetB) {
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001794 uint64_t OffDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1795 return SizeB <= OffDiff;
1796 }
1797 if (OffsetA < OffsetB) {
1798 uint64_t OffDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1799 return SizeA <= OffDiff;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001800 }
1801
1802 return false;
1803}
1804
Brendon Cahoon254f8892016-07-29 16:44:44 +00001805/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001806bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001807 int &Value) const {
1808 if (isPostIncrement(MI)) {
Krzysztof Parzyszek12bdcab2017-10-11 15:59:51 +00001809 unsigned BasePos = 0, OffsetPos = 0;
1810 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
1811 return false;
1812 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
1813 if (OffsetOp.isImm()) {
1814 Value = OffsetOp.getImm();
1815 return true;
1816 }
Krzysztof Parzyszekbf626192017-10-11 16:15:31 +00001817 } else if (MI.getOpcode() == Hexagon::A2_addi) {
1818 const MachineOperand &AddOp = MI.getOperand(2);
1819 if (AddOp.isImm()) {
1820 Value = AddOp.getImm();
1821 return true;
1822 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00001823 }
1824
1825 return false;
1826}
1827
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001828std::pair<unsigned, unsigned>
1829HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1830 return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
1831 TF & HexagonII::MO_Bitmasks);
1832}
1833
1834ArrayRef<std::pair<unsigned, const char*>>
1835HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1836 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00001837
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001838 static const std::pair<unsigned, const char*> Flags[] = {
1839 {MO_PCREL, "hexagon-pcrel"},
1840 {MO_GOT, "hexagon-got"},
1841 {MO_LO16, "hexagon-lo16"},
1842 {MO_HI16, "hexagon-hi16"},
1843 {MO_GPREL, "hexagon-gprel"},
1844 {MO_GDGOT, "hexagon-gdgot"},
1845 {MO_GDPLT, "hexagon-gdplt"},
1846 {MO_IE, "hexagon-ie"},
1847 {MO_IEGOT, "hexagon-iegot"},
1848 {MO_TPREL, "hexagon-tprel"}
1849 };
1850 return makeArrayRef(Flags);
1851}
1852
1853ArrayRef<std::pair<unsigned, const char*>>
1854HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1855 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00001856
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001857 static const std::pair<unsigned, const char*> Flags[] = {
1858 {HMOTF_ConstExtended, "hexagon-ext"}
1859 };
1860 return makeArrayRef(Flags);
1861}
1862
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001863unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001864 MachineRegisterInfo &MRI = MF->getRegInfo();
1865 const TargetRegisterClass *TRC;
1866 if (VT == MVT::i1) {
1867 TRC = &Hexagon::PredRegsRegClass;
1868 } else if (VT == MVT::i32 || VT == MVT::f32) {
1869 TRC = &Hexagon::IntRegsRegClass;
1870 } else if (VT == MVT::i64 || VT == MVT::f64) {
1871 TRC = &Hexagon::DoubleRegsRegClass;
1872 } else {
1873 llvm_unreachable("Cannot handle this register class");
1874 }
1875
1876 unsigned NewReg = MRI.createVirtualRegister(TRC);
1877 return NewReg;
1878}
1879
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001880bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001881 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1882}
1883
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001884bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1885 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001886 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1887}
1888
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001889bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001890 return !isTC1(MI) && !isTC2Early(MI) && !MI.getDesc().mayLoad() &&
1891 !MI.getDesc().mayStore() &&
1892 MI.getDesc().getOpcode() != Hexagon::S2_allocframe &&
1893 MI.getDesc().getOpcode() != Hexagon::L2_deallocframe &&
1894 !isMemOp(MI) && !MI.isBranch() && !MI.isReturn() && !MI.isCall();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001895}
1896
Sanjay Patele4b9f502015-12-07 19:21:39 +00001897// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001898bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001899 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001900}
1901
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001902// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1903// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001904bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1905 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001906 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1907 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001908 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001909
1910 unsigned isExtendable =
1911 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1912 if (!isExtendable)
1913 return false;
1914
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001915 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001916 return false;
1917
1918 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001919 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001920 // Use MO operand flags to determine if MO
1921 // has the HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00001922 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001923 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001924 // If this is a Machine BB address we are talking about, and it is
1925 // not marked as extended, say so.
1926 if (MO.isMBB())
1927 return false;
1928
1929 // We could be using an instruction with an extendable immediate and shoehorn
1930 // a global address into it. If it is a global address it will be constant
1931 // extended. We do this for COMBINE.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001932 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001933 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001934 return true;
1935
1936 // If the extendable operand is not 'Immediate' type, the instruction should
1937 // have 'isExtended' flag set.
1938 assert(MO.isImm() && "Extendable operand must be Immediate type");
1939
1940 int MinValue = getMinValue(MI);
1941 int MaxValue = getMaxValue(MI);
1942 int ImmValue = MO.getImm();
1943
1944 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001945}
1946
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001947bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1948 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00001949 case Hexagon::L4_return:
1950 case Hexagon::L4_return_t:
1951 case Hexagon::L4_return_f:
1952 case Hexagon::L4_return_tnew_pnt:
1953 case Hexagon::L4_return_fnew_pnt:
1954 case Hexagon::L4_return_tnew_pt:
1955 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00001956 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001957 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001958 return false;
1959}
1960
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001961// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001962bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1963 const MachineInstr &ConsMI) const {
1964 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001965 return false;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001966 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001967
1968 SmallVector<unsigned, 4> DefsA;
1969 SmallVector<unsigned, 4> DefsB;
1970 SmallVector<unsigned, 8> UsesA;
1971 SmallVector<unsigned, 8> UsesB;
1972
1973 parseOperands(ProdMI, DefsA, UsesA);
1974 parseOperands(ConsMI, DefsB, UsesB);
1975
1976 for (auto &RegA : DefsA)
1977 for (auto &RegB : UsesB) {
1978 // True data dependency.
1979 if (RegA == RegB)
1980 return true;
1981
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001982 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001983 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1984 if (RegB == *SubRegs)
1985 return true;
1986
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001987 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001988 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1989 if (RegA == *SubRegs)
1990 return true;
1991 }
1992
1993 return false;
1994}
1995
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001996// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001997bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1998 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001999 case Hexagon::V6_vL32b_cur_pi:
2000 case Hexagon::V6_vL32b_cur_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002001 return true;
2002 }
2003 return false;
2004}
2005
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002006// Returns true, if any one of the operands is a dot new
2007// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002008bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2009 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002010 return true;
2011
2012 return false;
2013}
2014
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002015/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002016bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2017 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002018 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2019 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2020 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2021}
2022
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002023bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2024 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002025 return true;
2026
2027 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002028 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002029 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002030}
2031
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002032bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2033 return (Opcode == Hexagon::ENDLOOP0 ||
2034 Opcode == Hexagon::ENDLOOP1);
2035}
2036
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002037bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2038 switch(OpType) {
2039 case MachineOperand::MO_MachineBasicBlock:
2040 case MachineOperand::MO_GlobalAddress:
2041 case MachineOperand::MO_ExternalSymbol:
2042 case MachineOperand::MO_JumpTableIndex:
2043 case MachineOperand::MO_ConstantPoolIndex:
2044 case MachineOperand::MO_BlockAddress:
2045 return true;
2046 default:
2047 return false;
2048 }
2049}
2050
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002051bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2052 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002053 const uint64_t F = MID.TSFlags;
2054 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2055 return true;
2056
2057 // TODO: This is largely obsolete now. Will need to be removed
2058 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002059 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002060 // PS_fi and PS_fia remain special cases.
2061 case Hexagon::PS_fi:
2062 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002063 return true;
2064 default:
2065 return false;
2066 }
2067 return false;
2068}
2069
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002070// This returns true in two cases:
2071// - The OP code itself indicates that this is an extended instruction.
2072// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002073bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002074 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002075 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002076 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2077 return true;
2078 // Use MO operand flags to determine if one of MI's operands
2079 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00002080 for (const MachineOperand &MO : MI.operands())
2081 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002082 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002083 return false;
2084}
2085
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002086bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2087 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002088 const uint64_t F = get(Opcode).TSFlags;
2089 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2090}
2091
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002092// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002093bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2094 const MachineInstr &J) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002095 if (!isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002096 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002097 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002098 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002099 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002100}
2101
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002102bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2103 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002104 case Hexagon::J2_callr:
2105 case Hexagon::J2_callrf:
2106 case Hexagon::J2_callrt:
2107 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002108 return true;
2109 }
2110 return false;
2111}
2112
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002113bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2114 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002115 case Hexagon::L4_return:
2116 case Hexagon::L4_return_t:
2117 case Hexagon::L4_return_f:
2118 case Hexagon::L4_return_fnew_pnt:
2119 case Hexagon::L4_return_fnew_pt:
2120 case Hexagon::L4_return_tnew_pnt:
2121 case Hexagon::L4_return_tnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002122 return true;
2123 }
2124 return false;
2125}
2126
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002127bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2128 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002129 case Hexagon::J2_jumpr:
2130 case Hexagon::J2_jumprt:
2131 case Hexagon::J2_jumprf:
2132 case Hexagon::J2_jumprtnewpt:
2133 case Hexagon::J2_jumprfnewpt:
2134 case Hexagon::J2_jumprtnew:
2135 case Hexagon::J2_jumprfnew:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002136 return true;
2137 }
2138 return false;
2139}
2140
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002141// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002142// Use abs estimate as oppose to the exact number.
2143// TODO: This will need to be changed to use MC level
2144// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002145bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002146 unsigned offset) const {
2147 // This selection of jump instructions matches to that what
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002148 // analyzeBranch can parse, plus NVJ.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002149 if (isNewValueJump(MI)) // r9:2
2150 return isInt<11>(offset);
2151
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002152 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002153 // Still missing Jump to address condition on register value.
2154 default:
2155 return false;
2156 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2157 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002158 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002159 return isInt<24>(offset);
2160 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2161 case Hexagon::J2_jumpf:
2162 case Hexagon::J2_jumptnew:
2163 case Hexagon::J2_jumptnewpt:
2164 case Hexagon::J2_jumpfnew:
2165 case Hexagon::J2_jumpfnewpt:
2166 case Hexagon::J2_callt:
2167 case Hexagon::J2_callf:
2168 return isInt<17>(offset);
2169 case Hexagon::J2_loop0i:
2170 case Hexagon::J2_loop0iext:
2171 case Hexagon::J2_loop0r:
2172 case Hexagon::J2_loop0rext:
2173 case Hexagon::J2_loop1i:
2174 case Hexagon::J2_loop1iext:
2175 case Hexagon::J2_loop1r:
2176 case Hexagon::J2_loop1rext:
2177 return isInt<9>(offset);
2178 // TODO: Add all the compound branches here. Can we do this in Relation model?
2179 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2180 case Hexagon::J4_cmpeqi_tp1_jump_nt:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002181 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
2182 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002183 return isInt<11>(offset);
2184 }
2185}
2186
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002187bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2188 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002189 bool isLate = isLateResultInstr(LRMI);
2190 bool isEarly = isEarlySourceInstr(ESMI);
2191
2192 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002193 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002194 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002195 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002196
2197 if (isLate && isEarly) {
2198 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2199 return true;
2200 }
2201
2202 return false;
2203}
2204
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002205bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2206 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002207 case TargetOpcode::EXTRACT_SUBREG:
2208 case TargetOpcode::INSERT_SUBREG:
2209 case TargetOpcode::SUBREG_TO_REG:
2210 case TargetOpcode::REG_SEQUENCE:
2211 case TargetOpcode::IMPLICIT_DEF:
2212 case TargetOpcode::COPY:
2213 case TargetOpcode::INLINEASM:
2214 case TargetOpcode::PHI:
2215 return false;
2216 default:
2217 break;
2218 }
2219
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002220 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002221 return !is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002222}
2223
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002224bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002225 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2226 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002227 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002228}
2229
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002230bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2231 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002232 return Opcode == Hexagon::J2_loop0i ||
2233 Opcode == Hexagon::J2_loop0r ||
2234 Opcode == Hexagon::J2_loop0iext ||
2235 Opcode == Hexagon::J2_loop0rext ||
2236 Opcode == Hexagon::J2_loop1i ||
2237 Opcode == Hexagon::J2_loop1r ||
2238 Opcode == Hexagon::J2_loop1iext ||
2239 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002240}
2241
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002242bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2243 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002244 default: return false;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002245 case Hexagon::L4_iadd_memopw_io:
2246 case Hexagon::L4_isub_memopw_io:
2247 case Hexagon::L4_add_memopw_io:
2248 case Hexagon::L4_sub_memopw_io:
2249 case Hexagon::L4_and_memopw_io:
2250 case Hexagon::L4_or_memopw_io:
2251 case Hexagon::L4_iadd_memoph_io:
2252 case Hexagon::L4_isub_memoph_io:
2253 case Hexagon::L4_add_memoph_io:
2254 case Hexagon::L4_sub_memoph_io:
2255 case Hexagon::L4_and_memoph_io:
2256 case Hexagon::L4_or_memoph_io:
2257 case Hexagon::L4_iadd_memopb_io:
2258 case Hexagon::L4_isub_memopb_io:
2259 case Hexagon::L4_add_memopb_io:
2260 case Hexagon::L4_sub_memopb_io:
2261 case Hexagon::L4_and_memopb_io:
2262 case Hexagon::L4_or_memopb_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002263 case Hexagon::L4_ior_memopb_io:
2264 case Hexagon::L4_ior_memoph_io:
2265 case Hexagon::L4_ior_memopw_io:
2266 case Hexagon::L4_iand_memopb_io:
2267 case Hexagon::L4_iand_memoph_io:
2268 case Hexagon::L4_iand_memopw_io:
2269 return true;
2270 }
2271 return false;
2272}
2273
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002274bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2275 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002276 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2277}
2278
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002279bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2280 const uint64_t F = get(Opcode).TSFlags;
2281 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2282}
2283
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002284bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002285 return isNewValueJump(MI) || isNewValueStore(MI);
2286}
2287
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002288bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2289 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002290}
2291
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002292bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2293 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2294}
2295
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002296bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2297 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002298 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2299}
2300
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002301bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2302 const uint64_t F = get(Opcode).TSFlags;
2303 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2304}
2305
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002306// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002307bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002308 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002309 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002310 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2311 == OperandNum;
2312}
2313
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002314bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2315 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002316 assert(isPredicated(MI));
2317 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2318}
2319
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002320bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2321 const uint64_t F = get(Opcode).TSFlags;
2322 assert(isPredicated(Opcode));
2323 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2324}
2325
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002326bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2327 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002328 return !((F >> HexagonII::PredicatedFalsePos) &
2329 HexagonII::PredicatedFalseMask);
2330}
2331
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002332bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2333 const uint64_t F = get(Opcode).TSFlags;
2334 // Make sure that the instruction is predicated.
2335 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2336 return !((F >> HexagonII::PredicatedFalsePos) &
2337 HexagonII::PredicatedFalseMask);
2338}
2339
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002340bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2341 const uint64_t F = get(Opcode).TSFlags;
2342 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2343}
2344
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002345bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2346 const uint64_t F = get(Opcode).TSFlags;
2347 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2348}
2349
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002350bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2351 const uint64_t F = get(Opcode).TSFlags;
2352 assert(get(Opcode).isBranch() &&
2353 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2354 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2355}
2356
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002357bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2358 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2359 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2360 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2361 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002362}
2363
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002364bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2365 switch (MI.getOpcode()) {
2366 // Byte
2367 case Hexagon::L2_loadrb_io:
2368 case Hexagon::L4_loadrb_ur:
2369 case Hexagon::L4_loadrb_ap:
2370 case Hexagon::L2_loadrb_pr:
2371 case Hexagon::L2_loadrb_pbr:
2372 case Hexagon::L2_loadrb_pi:
2373 case Hexagon::L2_loadrb_pci:
2374 case Hexagon::L2_loadrb_pcr:
2375 case Hexagon::L2_loadbsw2_io:
2376 case Hexagon::L4_loadbsw2_ur:
2377 case Hexagon::L4_loadbsw2_ap:
2378 case Hexagon::L2_loadbsw2_pr:
2379 case Hexagon::L2_loadbsw2_pbr:
2380 case Hexagon::L2_loadbsw2_pi:
2381 case Hexagon::L2_loadbsw2_pci:
2382 case Hexagon::L2_loadbsw2_pcr:
2383 case Hexagon::L2_loadbsw4_io:
2384 case Hexagon::L4_loadbsw4_ur:
2385 case Hexagon::L4_loadbsw4_ap:
2386 case Hexagon::L2_loadbsw4_pr:
2387 case Hexagon::L2_loadbsw4_pbr:
2388 case Hexagon::L2_loadbsw4_pi:
2389 case Hexagon::L2_loadbsw4_pci:
2390 case Hexagon::L2_loadbsw4_pcr:
2391 case Hexagon::L4_loadrb_rr:
2392 case Hexagon::L2_ploadrbt_io:
2393 case Hexagon::L2_ploadrbt_pi:
2394 case Hexagon::L2_ploadrbf_io:
2395 case Hexagon::L2_ploadrbf_pi:
2396 case Hexagon::L2_ploadrbtnew_io:
2397 case Hexagon::L2_ploadrbfnew_io:
2398 case Hexagon::L4_ploadrbt_rr:
2399 case Hexagon::L4_ploadrbf_rr:
2400 case Hexagon::L4_ploadrbtnew_rr:
2401 case Hexagon::L4_ploadrbfnew_rr:
2402 case Hexagon::L2_ploadrbtnew_pi:
2403 case Hexagon::L2_ploadrbfnew_pi:
2404 case Hexagon::L4_ploadrbt_abs:
2405 case Hexagon::L4_ploadrbf_abs:
2406 case Hexagon::L4_ploadrbtnew_abs:
2407 case Hexagon::L4_ploadrbfnew_abs:
2408 case Hexagon::L2_loadrbgp:
2409 // Half
2410 case Hexagon::L2_loadrh_io:
2411 case Hexagon::L4_loadrh_ur:
2412 case Hexagon::L4_loadrh_ap:
2413 case Hexagon::L2_loadrh_pr:
2414 case Hexagon::L2_loadrh_pbr:
2415 case Hexagon::L2_loadrh_pi:
2416 case Hexagon::L2_loadrh_pci:
2417 case Hexagon::L2_loadrh_pcr:
2418 case Hexagon::L4_loadrh_rr:
2419 case Hexagon::L2_ploadrht_io:
2420 case Hexagon::L2_ploadrht_pi:
2421 case Hexagon::L2_ploadrhf_io:
2422 case Hexagon::L2_ploadrhf_pi:
2423 case Hexagon::L2_ploadrhtnew_io:
2424 case Hexagon::L2_ploadrhfnew_io:
2425 case Hexagon::L4_ploadrht_rr:
2426 case Hexagon::L4_ploadrhf_rr:
2427 case Hexagon::L4_ploadrhtnew_rr:
2428 case Hexagon::L4_ploadrhfnew_rr:
2429 case Hexagon::L2_ploadrhtnew_pi:
2430 case Hexagon::L2_ploadrhfnew_pi:
2431 case Hexagon::L4_ploadrht_abs:
2432 case Hexagon::L4_ploadrhf_abs:
2433 case Hexagon::L4_ploadrhtnew_abs:
2434 case Hexagon::L4_ploadrhfnew_abs:
2435 case Hexagon::L2_loadrhgp:
2436 return true;
2437 default:
2438 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002439 }
2440}
2441
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002442bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2443 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002444 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2445}
2446
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002447bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2448 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002449 case Hexagon::STriw_pred:
2450 case Hexagon::LDriw_pred:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002451 return true;
2452 default:
2453 return false;
2454 }
2455}
2456
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002457bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2458 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002459 return false;
2460
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002461 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002462 if (Op.isGlobal() || Op.isSymbol())
2463 return true;
2464 return false;
2465}
2466
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002467// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002468bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2469 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002470 return is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002471}
2472
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002473bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2474 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002475 return is_TC2(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002476}
2477
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002478bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2479 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002480 return is_TC2early(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002481}
2482
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002483bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2484 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002485 return is_TC4x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002486}
2487
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002488// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002489bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2490 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002491 if (mayBeCurLoad(MI1)) {
2492 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002493 unsigned DstReg = MI1.getOperand(0).getReg();
2494 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002495 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002496 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002497 return true;
2498 }
2499 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002500 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2501 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2502 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002503 return true;
2504 return false;
2505}
2506
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002507bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002508 const uint64_t V = getType(MI);
2509 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2510}
2511
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002512// Check if the Offset is a valid auto-inc imm by Load/Store Type.
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002513bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, int Offset) const {
2514 int Size = VT.getSizeInBits() / 8;
2515 if (Offset % Size != 0)
2516 return false;
2517 int Count = Offset / Size;
2518
2519 switch (VT.getSimpleVT().SimpleTy) {
2520 // For scalars the auto-inc is s4
2521 case MVT::i8:
2522 case MVT::i16:
2523 case MVT::i32:
2524 case MVT::i64:
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002525 case MVT::v2i16:
2526 case MVT::v2i32:
2527 case MVT::v4i8:
2528 case MVT::v4i16:
2529 case MVT::v8i8:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002530 return isInt<4>(Count);
2531 // For HVX vectors the auto-inc is s3
2532 case MVT::v64i8:
2533 case MVT::v32i16:
2534 case MVT::v16i32:
2535 case MVT::v8i64:
2536 case MVT::v128i8:
2537 case MVT::v64i16:
2538 case MVT::v32i32:
2539 case MVT::v16i64:
2540 return isInt<3>(Count);
2541 default:
2542 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002543 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002544
2545 llvm_unreachable("Not an valid type!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002546}
2547
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002548bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002549 const TargetRegisterInfo *TRI, bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002550 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002551 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002552 // inserted to calculate the final address. Due to this reason, the function
2553 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002554 // We used to assert if the offset was not properly aligned, however,
2555 // there are cases where a misaligned pointer recast can cause this
2556 // problem, and we need to allow for it. The front end warns of such
2557 // misaligns with respect to load size.
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002558 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002559 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002560 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002561 case Hexagon::PS_vstorerw_nt_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002562 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002563 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002564 case Hexagon::PS_vloadrw_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002565 case Hexagon::V6_vL32b_ai:
2566 case Hexagon::V6_vS32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002567 case Hexagon::V6_vL32b_nt_ai:
2568 case Hexagon::V6_vS32b_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002569 case Hexagon::V6_vL32Ub_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002570 case Hexagon::V6_vS32Ub_ai: {
2571 unsigned VectorSize = TRI->getSpillSize(Hexagon::HvxVRRegClass);
2572 assert(isPowerOf2_32(VectorSize));
2573 if (Offset & (VectorSize-1))
2574 return false;
2575 return isInt<4>(Offset >> Log2_32(VectorSize));
2576 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002577
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002578 case Hexagon::J2_loop0i:
2579 case Hexagon::J2_loop1i:
2580 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002581
2582 case Hexagon::S4_storeirb_io:
2583 case Hexagon::S4_storeirbt_io:
2584 case Hexagon::S4_storeirbf_io:
2585 return isUInt<6>(Offset);
2586
2587 case Hexagon::S4_storeirh_io:
2588 case Hexagon::S4_storeirht_io:
2589 case Hexagon::S4_storeirhf_io:
2590 return isShiftedUInt<6,1>(Offset);
2591
2592 case Hexagon::S4_storeiri_io:
2593 case Hexagon::S4_storeirit_io:
2594 case Hexagon::S4_storeirif_io:
2595 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002596 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002597
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002598 if (Extend)
2599 return true;
2600
2601 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002602 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002603 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002604 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2605 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2606
Colin LeMahieu947cd702014-12-23 20:44:59 +00002607 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002608 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002609 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2610 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2611
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002612 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002613 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002614 case Hexagon::S2_storerh_io:
Krzysztof Parzyszekd10df492017-05-03 15:36:51 +00002615 case Hexagon::S2_storerf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002616 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2617 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2618
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002619 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002620 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002621 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002622 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2623 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2624
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002625 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002626 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2627 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2628
Eugene Zelenko3b873362017-09-28 22:27:31 +00002629 case Hexagon::L4_iadd_memopw_io:
2630 case Hexagon::L4_isub_memopw_io:
2631 case Hexagon::L4_add_memopw_io:
2632 case Hexagon::L4_sub_memopw_io:
2633 case Hexagon::L4_and_memopw_io:
2634 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002635 return (0 <= Offset && Offset <= 255);
2636
Eugene Zelenko3b873362017-09-28 22:27:31 +00002637 case Hexagon::L4_iadd_memoph_io:
2638 case Hexagon::L4_isub_memoph_io:
2639 case Hexagon::L4_add_memoph_io:
2640 case Hexagon::L4_sub_memoph_io:
2641 case Hexagon::L4_and_memoph_io:
2642 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002643 return (0 <= Offset && Offset <= 127);
2644
Eugene Zelenko3b873362017-09-28 22:27:31 +00002645 case Hexagon::L4_iadd_memopb_io:
2646 case Hexagon::L4_isub_memopb_io:
2647 case Hexagon::L4_add_memopb_io:
2648 case Hexagon::L4_sub_memopb_io:
2649 case Hexagon::L4_and_memopb_io:
2650 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002651 return (0 <= Offset && Offset <= 63);
2652
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002653 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002654 // any size. Later pass knows how to handle it.
2655 case Hexagon::STriw_pred:
2656 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002657 case Hexagon::STriw_mod:
2658 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002659 return true;
2660
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002661 case Hexagon::PS_fi:
2662 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002663 case Hexagon::INLINEASM:
2664 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002665
2666 case Hexagon::L2_ploadrbt_io:
2667 case Hexagon::L2_ploadrbf_io:
2668 case Hexagon::L2_ploadrubt_io:
2669 case Hexagon::L2_ploadrubf_io:
2670 case Hexagon::S2_pstorerbt_io:
2671 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002672 return isUInt<6>(Offset);
2673
2674 case Hexagon::L2_ploadrht_io:
2675 case Hexagon::L2_ploadrhf_io:
2676 case Hexagon::L2_ploadruht_io:
2677 case Hexagon::L2_ploadruhf_io:
2678 case Hexagon::S2_pstorerht_io:
2679 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002680 return isShiftedUInt<6,1>(Offset);
2681
2682 case Hexagon::L2_ploadrit_io:
2683 case Hexagon::L2_ploadrif_io:
2684 case Hexagon::S2_pstorerit_io:
2685 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002686 return isShiftedUInt<6,2>(Offset);
2687
2688 case Hexagon::L2_ploadrdt_io:
2689 case Hexagon::L2_ploadrdf_io:
2690 case Hexagon::S2_pstorerdt_io:
2691 case Hexagon::S2_pstorerdf_io:
2692 return isShiftedUInt<6,3>(Offset);
2693 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002694
Benjamin Kramerb6684012011-12-27 11:41:05 +00002695 llvm_unreachable("No offset range is defined for this opcode. "
2696 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002697}
2698
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002699bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002700 return isHVXVec(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002701}
2702
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002703bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2704 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002705 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2706 return
2707 V == HexagonII::TypeCVI_VA ||
2708 V == HexagonII::TypeCVI_VA_DV;
2709}
Andrew Trickd06df962012-02-01 22:13:57 +00002710
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002711bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2712 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002713 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2714 return true;
2715
2716 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2717 return true;
2718
2719 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002720 return true;
2721
2722 return false;
2723}
Jyotsna Verma84256432013-03-01 17:37:13 +00002724
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002725bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2726 switch (MI.getOpcode()) {
2727 // Byte
2728 case Hexagon::L2_loadrub_io:
2729 case Hexagon::L4_loadrub_ur:
2730 case Hexagon::L4_loadrub_ap:
2731 case Hexagon::L2_loadrub_pr:
2732 case Hexagon::L2_loadrub_pbr:
2733 case Hexagon::L2_loadrub_pi:
2734 case Hexagon::L2_loadrub_pci:
2735 case Hexagon::L2_loadrub_pcr:
2736 case Hexagon::L2_loadbzw2_io:
2737 case Hexagon::L4_loadbzw2_ur:
2738 case Hexagon::L4_loadbzw2_ap:
2739 case Hexagon::L2_loadbzw2_pr:
2740 case Hexagon::L2_loadbzw2_pbr:
2741 case Hexagon::L2_loadbzw2_pi:
2742 case Hexagon::L2_loadbzw2_pci:
2743 case Hexagon::L2_loadbzw2_pcr:
2744 case Hexagon::L2_loadbzw4_io:
2745 case Hexagon::L4_loadbzw4_ur:
2746 case Hexagon::L4_loadbzw4_ap:
2747 case Hexagon::L2_loadbzw4_pr:
2748 case Hexagon::L2_loadbzw4_pbr:
2749 case Hexagon::L2_loadbzw4_pi:
2750 case Hexagon::L2_loadbzw4_pci:
2751 case Hexagon::L2_loadbzw4_pcr:
2752 case Hexagon::L4_loadrub_rr:
2753 case Hexagon::L2_ploadrubt_io:
2754 case Hexagon::L2_ploadrubt_pi:
2755 case Hexagon::L2_ploadrubf_io:
2756 case Hexagon::L2_ploadrubf_pi:
2757 case Hexagon::L2_ploadrubtnew_io:
2758 case Hexagon::L2_ploadrubfnew_io:
2759 case Hexagon::L4_ploadrubt_rr:
2760 case Hexagon::L4_ploadrubf_rr:
2761 case Hexagon::L4_ploadrubtnew_rr:
2762 case Hexagon::L4_ploadrubfnew_rr:
2763 case Hexagon::L2_ploadrubtnew_pi:
2764 case Hexagon::L2_ploadrubfnew_pi:
2765 case Hexagon::L4_ploadrubt_abs:
2766 case Hexagon::L4_ploadrubf_abs:
2767 case Hexagon::L4_ploadrubtnew_abs:
2768 case Hexagon::L4_ploadrubfnew_abs:
2769 case Hexagon::L2_loadrubgp:
2770 // Half
2771 case Hexagon::L2_loadruh_io:
2772 case Hexagon::L4_loadruh_ur:
2773 case Hexagon::L4_loadruh_ap:
2774 case Hexagon::L2_loadruh_pr:
2775 case Hexagon::L2_loadruh_pbr:
2776 case Hexagon::L2_loadruh_pi:
2777 case Hexagon::L2_loadruh_pci:
2778 case Hexagon::L2_loadruh_pcr:
2779 case Hexagon::L4_loadruh_rr:
2780 case Hexagon::L2_ploadruht_io:
2781 case Hexagon::L2_ploadruht_pi:
2782 case Hexagon::L2_ploadruhf_io:
2783 case Hexagon::L2_ploadruhf_pi:
2784 case Hexagon::L2_ploadruhtnew_io:
2785 case Hexagon::L2_ploadruhfnew_io:
2786 case Hexagon::L4_ploadruht_rr:
2787 case Hexagon::L4_ploadruhf_rr:
2788 case Hexagon::L4_ploadruhtnew_rr:
2789 case Hexagon::L4_ploadruhfnew_rr:
2790 case Hexagon::L2_ploadruhtnew_pi:
2791 case Hexagon::L2_ploadruhfnew_pi:
2792 case Hexagon::L4_ploadruht_abs:
2793 case Hexagon::L4_ploadruhf_abs:
2794 case Hexagon::L4_ploadruhtnew_abs:
2795 case Hexagon::L4_ploadruhfnew_abs:
2796 case Hexagon::L2_loadruhgp:
2797 return true;
2798 default:
2799 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002800 }
2801}
2802
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002803// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002804bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2805 const MachineInstr &MI2) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002806 if (isHVXVec(MI1) && isHVXVec(MI2))
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002807 if (!isVecUsableNextPacket(MI1, MI2))
2808 return true;
2809 return false;
2810}
2811
Brendon Cahoon254f8892016-07-29 16:44:44 +00002812/// \brief Get the base register and byte offset of a load/store instr.
2813bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2814 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2815 const {
2816 unsigned AccessSize = 0;
2817 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002818 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002819 Offset = OffsetVal;
2820 return BaseReg != 0;
2821}
2822
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002823/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002824bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2825 const MachineInstr &Second) const {
Krzysztof Parzyszek4763c2d2017-05-03 15:33:09 +00002826 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2827 const MachineOperand &Op = Second.getOperand(0);
2828 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2829 return true;
2830 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002831 if (DisableNVSchedule)
2832 return false;
2833 if (mayBeNewStore(Second)) {
2834 // Make sure the definition of the first instruction is the value being
2835 // stored.
2836 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002837 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002838 if (!Stored.isReg())
2839 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002840 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2841 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002842 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2843 return true;
2844 }
2845 }
2846 return false;
2847}
2848
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002849bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2850 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002851 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002852}
2853
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002854bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2855 for (auto &I : *B)
2856 if (I.isEHLabel())
2857 return true;
2858 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002859}
2860
Jyotsna Verma84256432013-03-01 17:37:13 +00002861// Returns true if an instruction can be converted into a non-extended
2862// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002863bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002864 short NonExtOpcode;
2865 // Check if the instruction has a register form that uses register in place
2866 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002867 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002868 return true;
2869
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002870 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002871 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002872
2873 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002874 case HexagonII::Absolute:
Jyotsna Verma84256432013-03-01 17:37:13 +00002875 // Load/store with absolute addressing mode can be converted into
2876 // base+offset mode.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002877 NonExtOpcode = Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002878 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002879 case HexagonII::BaseImmOffset:
Jyotsna Verma84256432013-03-01 17:37:13 +00002880 // Load/store with base+offset addressing mode can be converted into
2881 // base+register offset addressing mode. However left shift operand should
2882 // be set to 0.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002883 NonExtOpcode = Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002884 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002885 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002886 NonExtOpcode = Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002887 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002888 default:
2889 return false;
2890 }
2891 if (NonExtOpcode < 0)
2892 return false;
2893 return true;
2894 }
2895 return false;
2896}
2897
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002898bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2899 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002900 Hexagon::InstrType_Pseudo) >= 0;
2901}
2902
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002903bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2904 const {
2905 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2906 while (I != E) {
2907 if (I->isBarrier())
2908 return true;
2909 ++I;
2910 }
2911 return false;
2912}
2913
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002914// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002915bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002916 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002917 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00002918 Subtarget.hasV60TOps();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002919}
2920
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002921// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002922bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2923 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002924 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2925}
2926
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002927bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2928 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002929 // There is no stall when ProdMI is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002930 if (!isHVXVec(ProdMI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002931 return false;
2932
2933 // There is no stall when ProdMI and ConsMI are not dependent.
2934 if (!isDependent(ProdMI, ConsMI))
2935 return false;
2936
2937 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2938 // are scheduled in consecutive packets.
2939 if (isVecUsableNextPacket(ProdMI, ConsMI))
2940 return false;
2941
2942 return true;
2943}
2944
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002945bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002946 MachineBasicBlock::const_instr_iterator BII) const {
2947 // There is no stall when I is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002948 if (!isHVXVec(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002949 return false;
2950
2951 MachineBasicBlock::const_instr_iterator MII = BII;
2952 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2953
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002954 if (!MII->isBundle())
2955 return producesStall(*MII, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002956
2957 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002958 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002959 if (producesStall(J, MI))
2960 return true;
2961 }
2962 return false;
2963}
2964
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002965bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002966 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002967 for (const MachineOperand &MO : MI.operands()) {
2968 // Predicate register must be explicitly defined.
2969 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
2970 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002971 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002972 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002973 }
2974
2975 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2976 // memd_locked cannot be used as .new as well,
2977 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002978 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002979}
2980
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002981bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00002982 return Opcode == Hexagon::J2_jumpt ||
2983 Opcode == Hexagon::J2_jumptpt ||
2984 Opcode == Hexagon::J2_jumpf ||
2985 Opcode == Hexagon::J2_jumpfpt ||
2986 Opcode == Hexagon::J2_jumptnew ||
2987 Opcode == Hexagon::J2_jumpfnew ||
2988 Opcode == Hexagon::J2_jumptnewpt ||
2989 Opcode == Hexagon::J2_jumpfnewpt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002990}
2991
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002992bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2993 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2994 return false;
2995 return !isPredicatedTrue(Cond[0].getImm());
2996}
2997
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002998unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
2999 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003000 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3001}
3002
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003003// Returns the base register in a memory access (load/store). The offset is
3004// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003005// If the base register has a subregister or the offset field does not contain
3006// an immediate value, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003007unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003008 int &Offset, unsigned &AccessSize) const {
3009 // Return if it is not a base+offset type instruction or a MemOp.
3010 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3011 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003012 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003013 return 0;
3014
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003015 AccessSize = getMemAccessSize(MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003016
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003017 unsigned BasePos = 0, OffsetPos = 0;
3018 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003019 return 0;
3020
3021 // Post increment updates its EA after the mem access,
3022 // so we need to treat its offset as zero.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003023 if (isPostIncrement(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003024 Offset = 0;
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003025 } else {
3026 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
3027 if (!OffsetOp.isImm())
3028 return 0;
3029 Offset = OffsetOp.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003030 }
3031
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003032 const MachineOperand &BaseOp = MI.getOperand(BasePos);
3033 if (BaseOp.getSubReg() != 0)
3034 return 0;
3035 return BaseOp.getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003036}
3037
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003038/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003039bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003040 unsigned &BasePos, unsigned &OffsetPos) const {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003041 if (!isAddrModeWithOffset(MI) && !isPostIncrement(MI))
3042 return false;
3043
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003044 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003045 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003046 BasePos = 0;
3047 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003048 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003049 BasePos = 0;
3050 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003051 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003052 BasePos = 1;
3053 OffsetPos = 2;
3054 } else
3055 return false;
3056
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003057 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003058 BasePos++;
3059 OffsetPos++;
3060 }
3061 if (isPostIncrement(MI)) {
3062 BasePos++;
3063 OffsetPos++;
3064 }
3065
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003066 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003067 return false;
3068
3069 return true;
3070}
3071
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003072// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003073// e.g. jump_t t1 (i1)
3074// jump t2 (i2)
3075// Jumpers = {i2, i1}
3076SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3077 MachineBasicBlock& MBB) const {
3078 SmallVector<MachineInstr*, 2> Jumpers;
3079 // If the block has no terminators, it just falls into the block after it.
3080 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3081 if (I == MBB.instr_begin())
3082 return Jumpers;
3083
3084 // A basic block may looks like this:
3085 //
3086 // [ insn
3087 // EH_LABEL
3088 // insn
3089 // insn
3090 // insn
3091 // EH_LABEL
3092 // insn ]
3093 //
3094 // It has two succs but does not have a terminator
3095 // Don't know how to handle it.
3096 do {
3097 --I;
3098 if (I->isEHLabel())
3099 return Jumpers;
3100 } while (I != MBB.instr_begin());
3101
3102 I = MBB.instr_end();
3103 --I;
3104
3105 while (I->isDebugValue()) {
3106 if (I == MBB.instr_begin())
3107 return Jumpers;
3108 --I;
3109 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003110 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003111 return Jumpers;
3112
3113 // Get the last instruction in the block.
3114 MachineInstr *LastInst = &*I;
3115 Jumpers.push_back(LastInst);
3116 MachineInstr *SecondLastInst = nullptr;
3117 // Find one more terminator if present.
3118 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003119 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003120 if (!SecondLastInst) {
3121 SecondLastInst = &*I;
3122 Jumpers.push_back(SecondLastInst);
3123 } else // This is a third branch.
3124 return Jumpers;
3125 }
3126 if (I == MBB.instr_begin())
3127 break;
3128 --I;
3129 } while (true);
3130 return Jumpers;
3131}
3132
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003133// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003134unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3135 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003136 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3137}
3138
3139// See if instruction could potentially be a duplex candidate.
3140// If so, return its group. Zero otherwise.
3141HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003142 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003143 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3144
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003145 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003146 default:
3147 return HexagonII::HCG_None;
3148 //
3149 // Compound pairs.
3150 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3151 // "Rd16=#U6 ; jump #r9:2"
3152 // "Rd16=Rs16 ; jump #r9:2"
3153 //
3154 case Hexagon::C2_cmpeq:
3155 case Hexagon::C2_cmpgt:
3156 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003157 DstReg = MI.getOperand(0).getReg();
3158 Src1Reg = MI.getOperand(1).getReg();
3159 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003160 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3161 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3162 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3163 return HexagonII::HCG_A;
3164 break;
3165 case Hexagon::C2_cmpeqi:
3166 case Hexagon::C2_cmpgti:
3167 case Hexagon::C2_cmpgtui:
3168 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003169 DstReg = MI.getOperand(0).getReg();
3170 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003171 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3172 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003173 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3174 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3175 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003176 return HexagonII::HCG_A;
3177 break;
3178 case Hexagon::A2_tfr:
3179 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003180 DstReg = MI.getOperand(0).getReg();
3181 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003182 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3183 return HexagonII::HCG_A;
3184 break;
3185 case Hexagon::A2_tfrsi:
3186 // Rd = #u6
3187 // Do not test for #u6 size since the const is getting extended
3188 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003189 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003190 if (isIntRegForSubInst(DstReg))
3191 return HexagonII::HCG_A;
3192 break;
3193 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003194 DstReg = MI.getOperand(0).getReg();
3195 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003196 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3197 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003198 MI.getOperand(2).isImm() &&
3199 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003200 return HexagonII::HCG_A;
3201 break;
3202 // The fact that .new form is used pretty much guarantees
3203 // that predicate register will match. Nevertheless,
3204 // there could be some false positives without additional
3205 // checking.
3206 case Hexagon::J2_jumptnew:
3207 case Hexagon::J2_jumpfnew:
3208 case Hexagon::J2_jumptnewpt:
3209 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003210 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003211 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3212 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3213 return HexagonII::HCG_B;
3214 break;
3215 // Transfer and jump:
3216 // Rd=#U6 ; jump #r9:2
3217 // Rd=Rs ; jump #r9:2
3218 // Do not test for jump range here.
3219 case Hexagon::J2_jump:
3220 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003221 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003222 return HexagonII::HCG_C;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003223 }
3224
3225 return HexagonII::HCG_None;
3226}
3227
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003228// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003229unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3230 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003231 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3232 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003233 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3234 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003235 return -1u;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003236 unsigned DestReg = GA.getOperand(0).getReg();
3237 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003238 return -1u;
3239 if (DestReg != Hexagon::P0 && DestReg != Hexagon::P1)
3240 return -1u;
3241 // The value compared against must be either u5 or -1.
3242 const MachineOperand &CmpOp = GA.getOperand(2);
3243 if (!CmpOp.isImm())
3244 return -1u;
3245 int V = CmpOp.getImm();
3246 if (V == -1)
3247 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqn1_tp0_jump_nt
3248 : Hexagon::J4_cmpeqn1_tp1_jump_nt;
3249 if (!isUInt<5>(V))
3250 return -1u;
3251 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqi_tp0_jump_nt
3252 : Hexagon::J4_cmpeqi_tp1_jump_nt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003253}
3254
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003255int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3256 enum Hexagon::PredSense inPredSense;
3257 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3258 Hexagon::PredSense_true;
3259 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3260 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3261 return CondOpcode;
3262
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003263 llvm_unreachable("Unexpected predicable instruction");
3264}
3265
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003266// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003267int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3268 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003269 default: llvm_unreachable("Unknown .cur type");
3270 case Hexagon::V6_vL32b_pi:
3271 return Hexagon::V6_vL32b_cur_pi;
3272 case Hexagon::V6_vL32b_ai:
3273 return Hexagon::V6_vL32b_cur_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003274 case Hexagon::V6_vL32b_nt_pi:
3275 return Hexagon::V6_vL32b_nt_cur_pi;
3276 case Hexagon::V6_vL32b_nt_ai:
3277 return Hexagon::V6_vL32b_nt_cur_ai;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003278 }
3279 return 0;
3280}
3281
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003282// Return the regular version of the .cur instruction.
3283int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3284 switch (MI.getOpcode()) {
3285 default: llvm_unreachable("Unknown .cur type");
3286 case Hexagon::V6_vL32b_cur_pi:
3287 return Hexagon::V6_vL32b_pi;
3288 case Hexagon::V6_vL32b_cur_ai:
3289 return Hexagon::V6_vL32b_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003290 case Hexagon::V6_vL32b_nt_cur_pi:
3291 return Hexagon::V6_vL32b_nt_pi;
3292 case Hexagon::V6_vL32b_nt_cur_ai:
3293 return Hexagon::V6_vL32b_nt_ai;
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003294 }
3295 return 0;
3296}
3297
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003298// The diagram below shows the steps involved in the conversion of a predicated
3299// store instruction to its .new predicated new-value form.
3300//
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003301// Note: It doesn't include conditional new-value stores as they can't be
3302// converted to .new predicate.
3303//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003304// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3305// ^ ^
3306// / \ (not OK. it will cause new-value store to be
3307// / X conditional on p0.new while R2 producer is
3308// / \ on p0)
3309// / \.
3310// p.new store p.old NV store
3311// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3312// ^ ^
3313// \ /
3314// \ /
3315// \ /
3316// p.old store
3317// [if (p0)memw(R0+#0)=R2]
3318//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003319// The following set of instructions further explains the scenario where
3320// conditional new-value store becomes invalid when promoted to .new predicate
3321// form.
3322//
3323// { 1) if (p0) r0 = add(r1, r2)
3324// 2) p0 = cmp.eq(r3, #0) }
3325//
3326// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3327// the first two instructions because in instr 1, r0 is conditional on old value
3328// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3329// is not valid for new-value stores.
3330// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3331// from the "Conditional Store" list. Because a predicated new value store
3332// would NOT be promoted to a double dot new store. See diagram below:
3333// This function returns yes for those stores that are predicated but not
3334// yet promoted to predicate dot new instructions.
3335//
3336// +---------------------+
3337// /-----| if (p0) memw(..)=r0 |---------\~
3338// || +---------------------+ ||
3339// promote || /\ /\ || promote
3340// || /||\ /||\ ||
3341// \||/ demote || \||/
3342// \/ || || \/
3343// +-------------------------+ || +-------------------------+
3344// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3345// +-------------------------+ || +-------------------------+
3346// || || ||
3347// || demote \||/
3348// promote || \/ NOT possible
3349// || || /\~
3350// \||/ || /||\~
3351// \/ || ||
3352// +-----------------------------+
3353// | if (p0.new) memw(..)=r0.new |
3354// +-----------------------------+
3355// Double Dot New Store
3356//
3357// Returns the most basic instruction for the .new predicated instructions and
3358// new-value stores.
3359// For example, all of the following instructions will be converted back to the
3360// same instruction:
3361// 1) if (p0.new) memw(R0+#0) = R1.new --->
3362// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3363// 3) if (p0.new) memw(R0+#0) = R1 --->
3364//
3365// To understand the translation of instruction 1 to its original form, consider
3366// a packet with 3 instructions.
3367// { p0 = cmp.eq(R0,R1)
3368// if (p0.new) R2 = add(R3, R4)
3369// R5 = add (R3, R1)
3370// }
3371// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3372//
3373// This instruction can be part of the previous packet only if both p0 and R2
3374// are promoted to .new values. This promotion happens in steps, first
3375// predicate register is promoted to .new and in the next iteration R2 is
3376// promoted. Therefore, in case of dependence check failure (due to R5) during
3377// next iteration, it should be converted back to its most basic form.
3378
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003379// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003380int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3381 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003382 if (NVOpcode >= 0) // Valid new-value store instruction.
3383 return NVOpcode;
3384
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003385 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003386 default:
Eugene Zelenko3b873362017-09-28 22:27:31 +00003387 report_fatal_error(std::string("Unknown .new type: ") +
3388 std::to_string(MI.getOpcode()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003389 case Hexagon::S4_storerb_ur:
3390 return Hexagon::S4_storerbnew_ur;
3391
3392 case Hexagon::S2_storerb_pci:
3393 return Hexagon::S2_storerb_pci;
3394
3395 case Hexagon::S2_storeri_pci:
3396 return Hexagon::S2_storeri_pci;
3397
3398 case Hexagon::S2_storerh_pci:
3399 return Hexagon::S2_storerh_pci;
3400
3401 case Hexagon::S2_storerd_pci:
3402 return Hexagon::S2_storerd_pci;
3403
3404 case Hexagon::S2_storerf_pci:
3405 return Hexagon::S2_storerf_pci;
3406
3407 case Hexagon::V6_vS32b_ai:
3408 return Hexagon::V6_vS32b_new_ai;
3409
3410 case Hexagon::V6_vS32b_pi:
3411 return Hexagon::V6_vS32b_new_pi;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003412 }
3413 return 0;
3414}
3415
3416// Returns the opcode to use when converting MI, which is a conditional jump,
3417// into a conditional instruction which uses the .new value of the predicate.
3418// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003419// If MBPI is null, all edges will be treated as equally likely for the
3420// purposes of establishing a predication hint.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003421int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003422 const MachineBranchProbabilityInfo *MBPI) const {
3423 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003424 const MachineBasicBlock *Src = MI.getParent();
3425 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003426 bool Taken = false;
3427 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003428
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003429 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3430 const MachineBasicBlock *Dst) {
3431 if (MBPI)
3432 return MBPI->getEdgeProbability(Src, Dst);
3433 return BranchProbability(1, Src->succ_size());
3434 };
3435
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003436 if (BrTarget.isMBB()) {
3437 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003438 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003439 } else {
3440 // The branch target is not a basic block (most likely a function).
3441 // Since BPI only gives probabilities for targets that are basic blocks,
3442 // try to identify another target of this branch (potentially a fall-
3443 // -through) and check the probability of that target.
3444 //
3445 // The only handled branch combinations are:
3446 // - one conditional branch,
3447 // - one conditional branch followed by one unconditional branch.
3448 // Otherwise, assume not-taken.
3449 assert(MI.isConditionalBranch());
3450 const MachineBasicBlock &B = *MI.getParent();
3451 bool SawCond = false, Bad = false;
3452 for (const MachineInstr &I : B) {
3453 if (!I.isBranch())
3454 continue;
3455 if (I.isConditionalBranch()) {
3456 SawCond = true;
3457 if (&I != &MI) {
3458 Bad = true;
3459 break;
3460 }
3461 }
3462 if (I.isUnconditionalBranch() && !SawCond) {
3463 Bad = true;
3464 break;
3465 }
3466 }
3467 if (!Bad) {
3468 MachineBasicBlock::const_instr_iterator It(MI);
3469 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3470 if (NextIt == B.instr_end()) {
3471 // If this branch is the last, look for the fall-through block.
3472 for (const MachineBasicBlock *SB : B.successors()) {
3473 if (!B.isLayoutSuccessor(SB))
3474 continue;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003475 Taken = getEdgeProbability(Src, SB) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003476 break;
3477 }
3478 } else {
3479 assert(NextIt->isUnconditionalBranch());
3480 // Find the first MBB operand and assume it's the target.
3481 const MachineBasicBlock *BT = nullptr;
3482 for (const MachineOperand &Op : NextIt->operands()) {
3483 if (!Op.isMBB())
3484 continue;
3485 BT = Op.getMBB();
3486 break;
3487 }
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003488 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003489 }
3490 } // if (!Bad)
3491 }
3492
3493 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003494
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003495 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003496 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003497 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003498 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003499 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003500
3501 default:
3502 llvm_unreachable("Unexpected jump instruction.");
3503 }
3504}
3505
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003506// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003507int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003508 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003509 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003510 // Condtional Jumps
3511 case Hexagon::J2_jumpt:
3512 case Hexagon::J2_jumpf:
3513 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003514 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003515
3516 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3517 if (NewOpcode >= 0)
3518 return NewOpcode;
Krzysztof Parzyszek066e8b52017-06-02 14:07:06 +00003519 return 0;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003520}
3521
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003522int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
3523 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003524 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3525 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003526 // All Hexagon architectures have prediction bits on dot-new branches,
3527 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3528 // to pick the right opcode when converting back to dot-old.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003529 if (!Subtarget.getFeatureBits()[Hexagon::ArchV60]) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003530 switch (NewOp) {
3531 case Hexagon::J2_jumptpt:
3532 NewOp = Hexagon::J2_jumpt;
3533 break;
3534 case Hexagon::J2_jumpfpt:
3535 NewOp = Hexagon::J2_jumpf;
3536 break;
3537 case Hexagon::J2_jumprtpt:
3538 NewOp = Hexagon::J2_jumprt;
3539 break;
3540 case Hexagon::J2_jumprfpt:
3541 NewOp = Hexagon::J2_jumprf;
3542 break;
3543 }
3544 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003545 assert(NewOp >= 0 &&
3546 "Couldn't change predicate new instruction to its old form.");
3547 }
3548
3549 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3550 NewOp = Hexagon::getNonNVStore(NewOp);
3551 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3552 }
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003553
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003554 if (Subtarget.hasV60TOps())
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003555 return NewOp;
3556
3557 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3558 switch (NewOp) {
3559 case Hexagon::J2_jumpfpt:
3560 return Hexagon::J2_jumpf;
3561 case Hexagon::J2_jumptpt:
3562 return Hexagon::J2_jumpt;
3563 case Hexagon::J2_jumprfpt:
3564 return Hexagon::J2_jumprf;
3565 case Hexagon::J2_jumprtpt:
3566 return Hexagon::J2_jumprt;
3567 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003568 return NewOp;
3569}
3570
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003571// See if instruction could potentially be a duplex candidate.
3572// If so, return its group. Zero otherwise.
3573HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003574 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003575 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003576 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003577
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003578 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003579 default:
3580 return HexagonII::HSIG_None;
3581 //
3582 // Group L1:
3583 //
3584 // Rd = memw(Rs+#u4:2)
3585 // Rd = memub(Rs+#u4:0)
3586 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003587 DstReg = MI.getOperand(0).getReg();
3588 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003589 // Special case this one from Group L2.
3590 // Rd = memw(r29+#u5:2)
3591 if (isIntRegForSubInst(DstReg)) {
3592 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3593 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003594 MI.getOperand(2).isImm() &&
3595 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003596 return HexagonII::HSIG_L2;
3597 // Rd = memw(Rs+#u4:2)
3598 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003599 (MI.getOperand(2).isImm() &&
3600 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003601 return HexagonII::HSIG_L1;
3602 }
3603 break;
3604 case Hexagon::L2_loadrub_io:
3605 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003606 DstReg = MI.getOperand(0).getReg();
3607 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003608 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003609 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003610 return HexagonII::HSIG_L1;
3611 break;
3612 //
3613 // Group L2:
3614 //
3615 // Rd = memh/memuh(Rs+#u3:1)
3616 // Rd = memb(Rs+#u3:0)
3617 // Rd = memw(r29+#u5:2) - Handled above.
3618 // Rdd = memd(r29+#u5:3)
3619 // deallocframe
3620 // [if ([!]p0[.new])] dealloc_return
3621 // [if ([!]p0[.new])] jumpr r31
3622 case Hexagon::L2_loadrh_io:
3623 case Hexagon::L2_loadruh_io:
3624 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003625 DstReg = MI.getOperand(0).getReg();
3626 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003627 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003628 MI.getOperand(2).isImm() &&
3629 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003630 return HexagonII::HSIG_L2;
3631 break;
3632 case Hexagon::L2_loadrb_io:
3633 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003634 DstReg = MI.getOperand(0).getReg();
3635 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003636 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003637 MI.getOperand(2).isImm() &&
3638 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003639 return HexagonII::HSIG_L2;
3640 break;
3641 case Hexagon::L2_loadrd_io:
3642 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003643 DstReg = MI.getOperand(0).getReg();
3644 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003645 if (isDblRegForSubInst(DstReg, HRI) &&
3646 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3647 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003648 MI.getOperand(2).isImm() &&
3649 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003650 return HexagonII::HSIG_L2;
3651 break;
3652 // dealloc_return is not documented in Hexagon Manual, but marked
3653 // with A_SUBINSN attribute in iset_v4classic.py.
3654 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003655 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003656 case Hexagon::L4_return:
3657 case Hexagon::L2_deallocframe:
3658 return HexagonII::HSIG_L2;
3659 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003660 case Hexagon::PS_jmpret:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003661 case Hexagon::SL2_jumpr31:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003662 // jumpr r31
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003663 // Actual form JMPR implicit-def %pc, implicit %r31, implicit internal %r0
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003664 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003665 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3666 return HexagonII::HSIG_L2;
3667 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003668 case Hexagon::PS_jmprett:
3669 case Hexagon::PS_jmpretf:
3670 case Hexagon::PS_jmprettnewpt:
3671 case Hexagon::PS_jmpretfnewpt:
3672 case Hexagon::PS_jmprettnew:
3673 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003674 case Hexagon::SL2_jumpr31_t:
3675 case Hexagon::SL2_jumpr31_f:
3676 case Hexagon::SL2_jumpr31_tnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003677 DstReg = MI.getOperand(1).getReg();
3678 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003679 // [if ([!]p0[.new])] jumpr r31
3680 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3681 (Hexagon::P0 == SrcReg)) &&
3682 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3683 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003684 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00003685 case Hexagon::L4_return_t:
3686 case Hexagon::L4_return_f:
3687 case Hexagon::L4_return_tnew_pnt:
3688 case Hexagon::L4_return_fnew_pnt:
3689 case Hexagon::L4_return_tnew_pt:
3690 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003691 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003692 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003693 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3694 return HexagonII::HSIG_L2;
3695 break;
3696 //
3697 // Group S1:
3698 //
3699 // memw(Rs+#u4:2) = Rt
3700 // memb(Rs+#u4:0) = Rt
3701 case Hexagon::S2_storeri_io:
3702 // Special case this one from Group S2.
3703 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003704 Src1Reg = MI.getOperand(0).getReg();
3705 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003706 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3707 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003708 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3709 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003710 return HexagonII::HSIG_S2;
3711 // memw(Rs+#u4:2) = Rt
3712 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003713 MI.getOperand(1).isImm() &&
3714 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003715 return HexagonII::HSIG_S1;
3716 break;
3717 case Hexagon::S2_storerb_io:
3718 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003719 Src1Reg = MI.getOperand(0).getReg();
3720 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003721 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003722 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003723 return HexagonII::HSIG_S1;
3724 break;
3725 //
3726 // Group S2:
3727 //
3728 // memh(Rs+#u3:1) = Rt
3729 // memw(r29+#u5:2) = Rt
3730 // memd(r29+#s6:3) = Rtt
3731 // memw(Rs+#u4:2) = #U1
3732 // memb(Rs+#u4) = #U1
3733 // allocframe(#u5:3)
3734 case Hexagon::S2_storerh_io:
3735 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003736 Src1Reg = MI.getOperand(0).getReg();
3737 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003738 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003739 MI.getOperand(1).isImm() &&
3740 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003741 return HexagonII::HSIG_S1;
3742 break;
3743 case Hexagon::S2_storerd_io:
3744 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003745 Src1Reg = MI.getOperand(0).getReg();
3746 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003747 if (isDblRegForSubInst(Src2Reg, HRI) &&
3748 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003749 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3750 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003751 return HexagonII::HSIG_S2;
3752 break;
3753 case Hexagon::S4_storeiri_io:
3754 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003755 Src1Reg = MI.getOperand(0).getReg();
3756 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3757 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3758 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003759 return HexagonII::HSIG_S2;
3760 break;
3761 case Hexagon::S4_storeirb_io:
3762 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003763 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003764 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003765 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3766 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003767 return HexagonII::HSIG_S2;
3768 break;
3769 case Hexagon::S2_allocframe:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003770 if (MI.getOperand(2).isImm() &&
3771 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003772 return HexagonII::HSIG_S1;
3773 break;
3774 //
3775 // Group A:
3776 //
3777 // Rx = add(Rx,#s7)
3778 // Rd = Rs
3779 // Rd = #u6
3780 // Rd = #-1
3781 // if ([!]P0[.new]) Rd = #0
3782 // Rd = add(r29,#u6:2)
3783 // Rx = add(Rx,Rs)
3784 // P0 = cmp.eq(Rs,#u2)
3785 // Rdd = combine(#0,Rs)
3786 // Rdd = combine(Rs,#0)
3787 // Rdd = combine(#u2,#U2)
3788 // Rd = add(Rs,#1)
3789 // Rd = add(Rs,#-1)
3790 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3791 // Rd = and(Rs,#1)
3792 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003793 DstReg = MI.getOperand(0).getReg();
3794 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003795 if (isIntRegForSubInst(DstReg)) {
3796 // Rd = add(r29,#u6:2)
3797 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003798 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3799 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003800 return HexagonII::HSIG_A;
3801 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003802 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3803 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003804 return HexagonII::HSIG_A;
3805 // Rd = add(Rs,#1)
3806 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003807 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3808 ((MI.getOperand(2).getImm() == 1) ||
3809 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003810 return HexagonII::HSIG_A;
3811 }
3812 break;
3813 case Hexagon::A2_add:
3814 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003815 DstReg = MI.getOperand(0).getReg();
3816 Src1Reg = MI.getOperand(1).getReg();
3817 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003818 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3819 isIntRegForSubInst(Src2Reg))
3820 return HexagonII::HSIG_A;
3821 break;
3822 case Hexagon::A2_andir:
3823 // Same as zxtb.
3824 // Rd16=and(Rs16,#255)
3825 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003826 DstReg = MI.getOperand(0).getReg();
3827 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003828 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003829 MI.getOperand(2).isImm() &&
3830 ((MI.getOperand(2).getImm() == 1) ||
3831 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003832 return HexagonII::HSIG_A;
3833 break;
3834 case Hexagon::A2_tfr:
3835 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003836 DstReg = MI.getOperand(0).getReg();
3837 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003838 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3839 return HexagonII::HSIG_A;
3840 break;
3841 case Hexagon::A2_tfrsi:
3842 // Rd = #u6
3843 // Do not test for #u6 size since the const is getting extended
3844 // regardless and compound could be formed.
3845 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003846 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003847 if (isIntRegForSubInst(DstReg))
3848 return HexagonII::HSIG_A;
3849 break;
3850 case Hexagon::C2_cmoveit:
3851 case Hexagon::C2_cmovenewit:
3852 case Hexagon::C2_cmoveif:
3853 case Hexagon::C2_cmovenewif:
3854 // if ([!]P0[.new]) Rd = #0
3855 // Actual form:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003856 // %r16 = C2_cmovenewit internal %p0, 0, implicit undef %r16;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003857 DstReg = MI.getOperand(0).getReg();
3858 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003859 if (isIntRegForSubInst(DstReg) &&
3860 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003861 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003862 return HexagonII::HSIG_A;
3863 break;
3864 case Hexagon::C2_cmpeqi:
3865 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003866 DstReg = MI.getOperand(0).getReg();
3867 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003868 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3869 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003870 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003871 return HexagonII::HSIG_A;
3872 break;
3873 case Hexagon::A2_combineii:
3874 case Hexagon::A4_combineii:
3875 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003876 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003877 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003878 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3879 (MI.getOperand(1).isGlobal() &&
3880 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3881 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3882 (MI.getOperand(2).isGlobal() &&
3883 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003884 return HexagonII::HSIG_A;
3885 break;
3886 case Hexagon::A4_combineri:
3887 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003888 DstReg = MI.getOperand(0).getReg();
3889 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003890 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003891 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3892 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003893 return HexagonII::HSIG_A;
3894 break;
3895 case Hexagon::A4_combineir:
3896 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003897 DstReg = MI.getOperand(0).getReg();
3898 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003899 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003900 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3901 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003902 return HexagonII::HSIG_A;
3903 break;
3904 case Hexagon::A2_sxtb:
3905 case Hexagon::A2_sxth:
3906 case Hexagon::A2_zxtb:
3907 case Hexagon::A2_zxth:
3908 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003909 DstReg = MI.getOperand(0).getReg();
3910 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003911 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3912 return HexagonII::HSIG_A;
3913 break;
3914 }
3915
3916 return HexagonII::HSIG_None;
3917}
3918
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003919short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3920 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003921}
3922
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003923unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003924 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003925 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3926 // still have a MinLatency property, which getStageLatency checks.
3927 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003928 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003929
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003930 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003931 return 0;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003932 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
3933}
3934
3935/// getOperandLatency - Compute and return the use operand latency of a given
3936/// pair of def and use.
3937/// In most cases, the static scheduling itinerary was enough to determine the
3938/// operand latency. But it may not be possible for instructions with variable
3939/// number of defs / uses.
3940///
3941/// This is a raw interface to the itinerary that may be directly overriden by
3942/// a target. Use computeOperandLatency to get the best estimate of latency.
3943int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3944 const MachineInstr &DefMI,
3945 unsigned DefIdx,
3946 const MachineInstr &UseMI,
3947 unsigned UseIdx) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003948 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003949
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003950 // Get DefIdx and UseIdx for super registers.
3951 MachineOperand DefMO = DefMI.getOperand(DefIdx);
3952
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003953 if (HRI.isPhysicalRegister(DefMO.getReg())) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003954 if (DefMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003955 for (MCSuperRegIterator SR(DefMO.getReg(), &HRI); SR.isValid(); ++SR) {
3956 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003957 if (Idx != -1) {
3958 DefIdx = Idx;
3959 break;
3960 }
3961 }
3962 }
3963
3964 MachineOperand UseMO = UseMI.getOperand(UseIdx);
3965 if (UseMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003966 for (MCSuperRegIterator SR(UseMO.getReg(), &HRI); SR.isValid(); ++SR) {
3967 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003968 if (Idx != -1) {
3969 UseIdx = Idx;
3970 break;
3971 }
3972 }
3973 }
3974 }
3975
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003976 int Latency = TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
3977 UseMI, UseIdx);
3978 if (!Latency)
3979 // We should never have 0 cycle latency between two instructions unless
3980 // they can be packetized together. However, this decision can't be made
3981 // here.
3982 Latency = 1;
3983 return Latency;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003984}
3985
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003986// inverts the predication logic.
3987// p -> NotP
3988// NotP -> P
3989bool HexagonInstrInfo::getInvertedPredSense(
3990 SmallVectorImpl<MachineOperand> &Cond) const {
3991 if (Cond.empty())
3992 return false;
3993 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3994 Cond[0].setImm(Opc);
3995 return true;
3996}
3997
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003998unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3999 int InvPredOpcode;
4000 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4001 : Hexagon::getTruePredOpcode(Opc);
4002 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4003 return InvPredOpcode;
4004
4005 llvm_unreachable("Unexpected predicated instruction");
4006}
4007
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004008// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004009int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4010 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004011 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4012 & HexagonII::ExtentSignedMask;
4013 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4014 & HexagonII::ExtentBitsMask;
4015
4016 if (isSigned) // if value is signed
4017 return ~(-1U << (bits - 1));
4018 else
4019 return ~(-1U << bits);
4020}
4021
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004022
4023bool HexagonInstrInfo::isAddrModeWithOffset(const MachineInstr &MI) const {
4024 switch (MI.getOpcode()) {
4025 case Hexagon::L2_loadrbgp:
4026 case Hexagon::L2_loadrdgp:
4027 case Hexagon::L2_loadrhgp:
4028 case Hexagon::L2_loadrigp:
4029 case Hexagon::L2_loadrubgp:
4030 case Hexagon::L2_loadruhgp:
4031 case Hexagon::S2_storerbgp:
4032 case Hexagon::S2_storerbnewgp:
4033 case Hexagon::S2_storerhgp:
4034 case Hexagon::S2_storerhnewgp:
4035 case Hexagon::S2_storerigp:
4036 case Hexagon::S2_storerinewgp:
4037 case Hexagon::S2_storerdgp:
4038 case Hexagon::S2_storerfgp:
4039 return true;
4040 }
4041 const uint64_t F = MI.getDesc().TSFlags;
4042 unsigned addrMode =
4043 ((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
4044 // Disallow any base+offset instruction. The assembler does not yet reorder
4045 // based up any zero offset instruction.
4046 return (addrMode == HexagonII::BaseRegOffset ||
4047 addrMode == HexagonII::BaseImmOffset ||
4048 addrMode == HexagonII::BaseLongOffset);
4049}
4050
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004051unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004052 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00004053
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004054 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004055 unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
4056 unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
4057 if (Size != 0)
4058 return Size;
4059
4060 // Handle vector access sizes.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004061 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004062 switch (S) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004063 case HexagonII::HVXVectorAccess:
4064 return HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004065 default:
4066 llvm_unreachable("Unexpected instruction");
4067 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004068}
4069
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004070// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004071int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4072 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004073 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4074 & HexagonII::ExtentSignedMask;
4075 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4076 & HexagonII::ExtentBitsMask;
4077
4078 if (isSigned) // if value is signed
4079 return -1U << (bits - 1);
4080 else
4081 return 0;
4082}
4083
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004084// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004085short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004086 // Check if the instruction has a register form that uses register in place
4087 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004088 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004089 if (NonExtOpcode >= 0)
4090 return NonExtOpcode;
4091
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004092 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004093 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004094 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00004095 case HexagonII::Absolute:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004096 return Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Eugene Zelenko3b873362017-09-28 22:27:31 +00004097 case HexagonII::BaseImmOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004098 return Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004099 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004100 return Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004101
Jyotsna Verma84256432013-03-01 17:37:13 +00004102 default:
4103 return -1;
4104 }
4105 }
4106 return -1;
4107}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004108
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004109bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004110 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004111 if (Cond.empty())
4112 return false;
4113 assert(Cond.size() == 2);
4114 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004115 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4116 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004117 }
4118 PredReg = Cond[1].getReg();
4119 PredRegPos = 1;
4120 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4121 PredRegFlags = 0;
4122 if (Cond[1].isImplicit())
4123 PredRegFlags = RegState::Implicit;
4124 if (Cond[1].isUndef())
4125 PredRegFlags |= RegState::Undef;
4126 return true;
4127}
4128
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004129short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4130 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004131}
4132
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004133short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4134 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004135}
4136
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004137// Return the number of bytes required to encode the instruction.
4138// Hexagon instructions are fixed length, 4 bytes, unless they
4139// use a constant extender, which requires another 4 bytes.
4140// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004141unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4142 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004143 return 0;
4144
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004145 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004146 if (!Size)
4147 // Assume the default insn size in case it cannot be determined
4148 // for whatever reason.
4149 Size = HEXAGON_INSTR_SIZE;
4150
4151 if (isConstExtended(MI) || isExtended(MI))
4152 Size += HEXAGON_INSTR_SIZE;
4153
4154 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004155 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4156 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004157 const MachineFunction *MF = MBB.getParent();
4158 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4159
4160 // Count the number of register definitions to find the asm string.
4161 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004162 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004163 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004164 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004165
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004166 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004167 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004168 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004169 Size = getInlineAsmLength(AsmStr, *MAI);
4170 }
4171
4172 return Size;
4173}
4174
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004175uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4176 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004177 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4178}
4179
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004180unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004181 const InstrItineraryData &II = *Subtarget.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004182 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004183
4184 return IS.getUnits();
4185}
4186
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004187// Calculate size of the basic block without debug instructions.
4188unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4189 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4190}
4191
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004192unsigned HexagonInstrInfo::nonDbgBundleSize(
4193 MachineBasicBlock::const_iterator BundleHead) const {
4194 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004195 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004196 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004197 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004198}
4199
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004200/// immediateExtend - Changes the instruction in place to one using an immediate
4201/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004202void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004203 assert((isExtendable(MI)||isConstExtended(MI)) &&
4204 "Instruction must be extendable");
4205 // Find which operand is extendable.
4206 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004207 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004208 // This needs to be something we understand.
4209 assert((MO.isMBB() || MO.isImm()) &&
4210 "Branch with unknown extendable field type");
4211 // Mark given operand as extended.
4212 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4213}
4214
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004215bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004216 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00004217 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to "
4218 << printMBBReference(*NewTarget);
4219 MI.dump(););
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004220 assert(MI.isBranch());
4221 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4222 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004223 // In general branch target is the last operand,
4224 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004225 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004226 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004227 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4228 MI.getOperand(TargetPos).setMBB(NewTarget);
4229 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004230 NewOpcode = reversePrediction(NewOpcode);
4231 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004232 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004233 return true;
4234}
4235
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004236void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4237 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4238 MachineFunction::iterator A = MF.begin();
4239 MachineBasicBlock &B = *A;
4240 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004241 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004242 MachineInstr *NewMI;
4243
4244 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4245 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004246 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004247 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4248 " Class: " << NewMI->getDesc().getSchedClass());
4249 NewMI->eraseFromParent();
4250 }
4251 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4252}
4253
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004254// inverts the predication logic.
4255// p -> NotP
4256// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004257bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4258 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4259 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004260 return true;
4261}
4262
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004263// Reverse the branch prediction.
4264unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4265 int PredRevOpcode = -1;
4266 if (isPredictedTaken(Opcode))
4267 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4268 else
4269 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4270 assert(PredRevOpcode > 0);
4271 return PredRevOpcode;
4272}
4273
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004274// TODO: Add more rigorous validation.
4275bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4276 const {
4277 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4278}
4279
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004280void HexagonInstrInfo::
4281setBundleNoShuf(MachineBasicBlock::instr_iterator MIB) const {
4282 assert(MIB->isBundle());
4283 MachineOperand &Operand = MIB->getOperand(0);
4284 if (Operand.isImm())
4285 Operand.setImm(Operand.getImm() | memShufDisabledMask);
4286 else
4287 MIB->addOperand(MachineOperand::CreateImm(memShufDisabledMask));
4288}
4289
4290bool HexagonInstrInfo::getBundleNoShuf(const MachineInstr &MIB) const {
4291 assert(MIB.isBundle());
4292 const MachineOperand &Operand = MIB.getOperand(0);
4293 return (Operand.isImm() && (Operand.getImm() & memShufDisabledMask) != 0);
4294}
4295
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004296// Addressing mode relations.
4297short HexagonInstrInfo::changeAddrMode_abs_io(short Opc) const {
4298 return Opc >= 0 ? Hexagon::changeAddrMode_abs_io(Opc) : Opc;
4299}
4300
4301short HexagonInstrInfo::changeAddrMode_io_abs(short Opc) const {
4302 return Opc >= 0 ? Hexagon::changeAddrMode_io_abs(Opc) : Opc;
4303}
4304
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004305short HexagonInstrInfo::changeAddrMode_io_pi(short Opc) const {
4306 return Opc >= 0 ? Hexagon::changeAddrMode_io_pi(Opc) : Opc;
4307}
4308
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004309short HexagonInstrInfo::changeAddrMode_io_rr(short Opc) const {
4310 return Opc >= 0 ? Hexagon::changeAddrMode_io_rr(Opc) : Opc;
4311}
4312
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004313short HexagonInstrInfo::changeAddrMode_pi_io(short Opc) const {
4314 return Opc >= 0 ? Hexagon::changeAddrMode_pi_io(Opc) : Opc;
4315}
4316
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004317short HexagonInstrInfo::changeAddrMode_rr_io(short Opc) const {
4318 return Opc >= 0 ? Hexagon::changeAddrMode_rr_io(Opc) : Opc;
4319}
4320
4321short HexagonInstrInfo::changeAddrMode_rr_ur(short Opc) const {
4322 return Opc >= 0 ? Hexagon::changeAddrMode_rr_ur(Opc) : Opc;
4323}
4324
4325short HexagonInstrInfo::changeAddrMode_ur_rr(short Opc) const {
4326 return Opc >= 0 ? Hexagon::changeAddrMode_ur_rr(Opc) : Opc;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004327}