blob: 069cd862b8fdebe2a04fce5dd2c69d528cb4a1fd [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 Parzyszek998df2c2018-03-23 20:43:02 +0000147MachineInstr *HexagonInstrInfo::findLoopInstr(MachineBasicBlock *BB,
148 unsigned EndLoopOp, MachineBasicBlock *TargetBB,
149 SmallPtrSet<MachineBasicBlock *, 8> &Visited) const {
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 Parzyszek998df2c2018-03-23 20:43:02 +00001889bool HexagonInstrInfo::isBaseImmOffset(const MachineInstr &MI) const {
1890 return getAddrMode(MI) == HexagonII::BaseImmOffset;
1891}
1892
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001893bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001894 return !isTC1(MI) && !isTC2Early(MI) && !MI.getDesc().mayLoad() &&
1895 !MI.getDesc().mayStore() &&
1896 MI.getDesc().getOpcode() != Hexagon::S2_allocframe &&
1897 MI.getDesc().getOpcode() != Hexagon::L2_deallocframe &&
1898 !isMemOp(MI) && !MI.isBranch() && !MI.isReturn() && !MI.isCall();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001899}
1900
Sanjay Patele4b9f502015-12-07 19:21:39 +00001901// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001902bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001903 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001904}
1905
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001906// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1907// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001908bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1909 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001910 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1911 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001912 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001913
1914 unsigned isExtendable =
1915 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1916 if (!isExtendable)
1917 return false;
1918
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001919 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001920 return false;
1921
1922 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001923 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001924 // Use MO operand flags to determine if MO
1925 // has the HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00001926 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001927 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001928 // If this is a Machine BB address we are talking about, and it is
1929 // not marked as extended, say so.
1930 if (MO.isMBB())
1931 return false;
1932
1933 // We could be using an instruction with an extendable immediate and shoehorn
1934 // a global address into it. If it is a global address it will be constant
1935 // extended. We do this for COMBINE.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001936 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001937 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001938 return true;
1939
1940 // If the extendable operand is not 'Immediate' type, the instruction should
1941 // have 'isExtended' flag set.
1942 assert(MO.isImm() && "Extendable operand must be Immediate type");
1943
1944 int MinValue = getMinValue(MI);
1945 int MaxValue = getMaxValue(MI);
1946 int ImmValue = MO.getImm();
1947
1948 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001949}
1950
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001951bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1952 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00001953 case Hexagon::L4_return:
1954 case Hexagon::L4_return_t:
1955 case Hexagon::L4_return_f:
1956 case Hexagon::L4_return_tnew_pnt:
1957 case Hexagon::L4_return_fnew_pnt:
1958 case Hexagon::L4_return_tnew_pt:
1959 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00001960 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001961 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001962 return false;
1963}
1964
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001965// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001966bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1967 const MachineInstr &ConsMI) const {
1968 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001969 return false;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00001970 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001971
1972 SmallVector<unsigned, 4> DefsA;
1973 SmallVector<unsigned, 4> DefsB;
1974 SmallVector<unsigned, 8> UsesA;
1975 SmallVector<unsigned, 8> UsesB;
1976
1977 parseOperands(ProdMI, DefsA, UsesA);
1978 parseOperands(ConsMI, DefsB, UsesB);
1979
1980 for (auto &RegA : DefsA)
1981 for (auto &RegB : UsesB) {
1982 // True data dependency.
1983 if (RegA == RegB)
1984 return true;
1985
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001986 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001987 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1988 if (RegB == *SubRegs)
1989 return true;
1990
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001991 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001992 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1993 if (RegA == *SubRegs)
1994 return true;
1995 }
1996
1997 return false;
1998}
1999
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002000// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002001bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2002 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002003 case Hexagon::V6_vL32b_cur_pi:
2004 case Hexagon::V6_vL32b_cur_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002005 return true;
2006 }
2007 return false;
2008}
2009
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002010// Returns true, if any one of the operands is a dot new
2011// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002012bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2013 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002014 return true;
2015
2016 return false;
2017}
2018
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002019/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002020bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2021 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002022 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2023 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2024 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2025}
2026
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002027bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2028 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002029 return true;
2030
2031 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002032 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002033 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002034}
2035
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002036bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2037 return (Opcode == Hexagon::ENDLOOP0 ||
2038 Opcode == Hexagon::ENDLOOP1);
2039}
2040
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002041bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2042 switch(OpType) {
2043 case MachineOperand::MO_MachineBasicBlock:
2044 case MachineOperand::MO_GlobalAddress:
2045 case MachineOperand::MO_ExternalSymbol:
2046 case MachineOperand::MO_JumpTableIndex:
2047 case MachineOperand::MO_ConstantPoolIndex:
2048 case MachineOperand::MO_BlockAddress:
2049 return true;
2050 default:
2051 return false;
2052 }
2053}
2054
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002055bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2056 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002057 const uint64_t F = MID.TSFlags;
2058 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2059 return true;
2060
2061 // TODO: This is largely obsolete now. Will need to be removed
2062 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002063 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002064 // PS_fi and PS_fia remain special cases.
2065 case Hexagon::PS_fi:
2066 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002067 return true;
2068 default:
2069 return false;
2070 }
2071 return false;
2072}
2073
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002074// This returns true in two cases:
2075// - The OP code itself indicates that this is an extended instruction.
2076// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002077bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002078 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002079 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002080 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2081 return true;
2082 // Use MO operand flags to determine if one of MI's operands
2083 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00002084 for (const MachineOperand &MO : MI.operands())
2085 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002086 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002087 return false;
2088}
2089
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002090bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2091 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002092 const uint64_t F = get(Opcode).TSFlags;
2093 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2094}
2095
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002096// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002097bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2098 const MachineInstr &J) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002099 if (!isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002100 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002101 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002102 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002103 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002104}
2105
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002106bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2107 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002108 case Hexagon::J2_callr:
2109 case Hexagon::J2_callrf:
2110 case Hexagon::J2_callrt:
2111 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002112 return true;
2113 }
2114 return false;
2115}
2116
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002117bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2118 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002119 case Hexagon::L4_return:
2120 case Hexagon::L4_return_t:
2121 case Hexagon::L4_return_f:
2122 case Hexagon::L4_return_fnew_pnt:
2123 case Hexagon::L4_return_fnew_pt:
2124 case Hexagon::L4_return_tnew_pnt:
2125 case Hexagon::L4_return_tnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002126 return true;
2127 }
2128 return false;
2129}
2130
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002131bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2132 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002133 case Hexagon::J2_jumpr:
2134 case Hexagon::J2_jumprt:
2135 case Hexagon::J2_jumprf:
2136 case Hexagon::J2_jumprtnewpt:
2137 case Hexagon::J2_jumprfnewpt:
2138 case Hexagon::J2_jumprtnew:
2139 case Hexagon::J2_jumprfnew:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002140 return true;
2141 }
2142 return false;
2143}
2144
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002145// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002146// Use abs estimate as oppose to the exact number.
2147// TODO: This will need to be changed to use MC level
2148// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002149bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002150 unsigned offset) const {
2151 // This selection of jump instructions matches to that what
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002152 // analyzeBranch can parse, plus NVJ.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002153 if (isNewValueJump(MI)) // r9:2
2154 return isInt<11>(offset);
2155
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002156 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002157 // Still missing Jump to address condition on register value.
2158 default:
2159 return false;
2160 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2161 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002162 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002163 return isInt<24>(offset);
2164 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2165 case Hexagon::J2_jumpf:
2166 case Hexagon::J2_jumptnew:
2167 case Hexagon::J2_jumptnewpt:
2168 case Hexagon::J2_jumpfnew:
2169 case Hexagon::J2_jumpfnewpt:
2170 case Hexagon::J2_callt:
2171 case Hexagon::J2_callf:
2172 return isInt<17>(offset);
2173 case Hexagon::J2_loop0i:
2174 case Hexagon::J2_loop0iext:
2175 case Hexagon::J2_loop0r:
2176 case Hexagon::J2_loop0rext:
2177 case Hexagon::J2_loop1i:
2178 case Hexagon::J2_loop1iext:
2179 case Hexagon::J2_loop1r:
2180 case Hexagon::J2_loop1rext:
2181 return isInt<9>(offset);
2182 // TODO: Add all the compound branches here. Can we do this in Relation model?
2183 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2184 case Hexagon::J4_cmpeqi_tp1_jump_nt:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002185 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
2186 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002187 return isInt<11>(offset);
2188 }
2189}
2190
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002191bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2192 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002193 bool isLate = isLateResultInstr(LRMI);
2194 bool isEarly = isEarlySourceInstr(ESMI);
2195
2196 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002197 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002198 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002199 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002200
2201 if (isLate && isEarly) {
2202 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2203 return true;
2204 }
2205
2206 return false;
2207}
2208
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002209bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2210 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002211 case TargetOpcode::EXTRACT_SUBREG:
2212 case TargetOpcode::INSERT_SUBREG:
2213 case TargetOpcode::SUBREG_TO_REG:
2214 case TargetOpcode::REG_SEQUENCE:
2215 case TargetOpcode::IMPLICIT_DEF:
2216 case TargetOpcode::COPY:
2217 case TargetOpcode::INLINEASM:
2218 case TargetOpcode::PHI:
2219 return false;
2220 default:
2221 break;
2222 }
2223
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002224 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002225 return !is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002226}
2227
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002228bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002229 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2230 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002231 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002232}
2233
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002234bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2235 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002236 return Opcode == Hexagon::J2_loop0i ||
2237 Opcode == Hexagon::J2_loop0r ||
2238 Opcode == Hexagon::J2_loop0iext ||
2239 Opcode == Hexagon::J2_loop0rext ||
2240 Opcode == Hexagon::J2_loop1i ||
2241 Opcode == Hexagon::J2_loop1r ||
2242 Opcode == Hexagon::J2_loop1iext ||
2243 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002244}
2245
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002246bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2247 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002248 default: return false;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002249 case Hexagon::L4_iadd_memopw_io:
2250 case Hexagon::L4_isub_memopw_io:
2251 case Hexagon::L4_add_memopw_io:
2252 case Hexagon::L4_sub_memopw_io:
2253 case Hexagon::L4_and_memopw_io:
2254 case Hexagon::L4_or_memopw_io:
2255 case Hexagon::L4_iadd_memoph_io:
2256 case Hexagon::L4_isub_memoph_io:
2257 case Hexagon::L4_add_memoph_io:
2258 case Hexagon::L4_sub_memoph_io:
2259 case Hexagon::L4_and_memoph_io:
2260 case Hexagon::L4_or_memoph_io:
2261 case Hexagon::L4_iadd_memopb_io:
2262 case Hexagon::L4_isub_memopb_io:
2263 case Hexagon::L4_add_memopb_io:
2264 case Hexagon::L4_sub_memopb_io:
2265 case Hexagon::L4_and_memopb_io:
2266 case Hexagon::L4_or_memopb_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002267 case Hexagon::L4_ior_memopb_io:
2268 case Hexagon::L4_ior_memoph_io:
2269 case Hexagon::L4_ior_memopw_io:
2270 case Hexagon::L4_iand_memopb_io:
2271 case Hexagon::L4_iand_memoph_io:
2272 case Hexagon::L4_iand_memopw_io:
2273 return true;
2274 }
2275 return false;
2276}
2277
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002278bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2279 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002280 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2281}
2282
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002283bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2284 const uint64_t F = get(Opcode).TSFlags;
2285 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2286}
2287
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002288bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002289 return isNewValueJump(MI) || isNewValueStore(MI);
2290}
2291
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002292bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2293 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002294}
2295
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002296bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2297 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2298}
2299
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002300bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2301 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002302 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2303}
2304
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002305bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2306 const uint64_t F = get(Opcode).TSFlags;
2307 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2308}
2309
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002310// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002311bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002312 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002313 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002314 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2315 == OperandNum;
2316}
2317
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002318bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2319 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002320 assert(isPredicated(MI));
2321 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2322}
2323
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002324bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2325 const uint64_t F = get(Opcode).TSFlags;
2326 assert(isPredicated(Opcode));
2327 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2328}
2329
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002330bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2331 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002332 return !((F >> HexagonII::PredicatedFalsePos) &
2333 HexagonII::PredicatedFalseMask);
2334}
2335
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002336bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2337 const uint64_t F = get(Opcode).TSFlags;
2338 // Make sure that the instruction is predicated.
2339 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2340 return !((F >> HexagonII::PredicatedFalsePos) &
2341 HexagonII::PredicatedFalseMask);
2342}
2343
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002344bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2345 const uint64_t F = get(Opcode).TSFlags;
2346 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2347}
2348
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002349bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2350 const uint64_t F = get(Opcode).TSFlags;
2351 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2352}
2353
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002354bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2355 const uint64_t F = get(Opcode).TSFlags;
2356 assert(get(Opcode).isBranch() &&
2357 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2358 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2359}
2360
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002361bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2362 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2363 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2364 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2365 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002366}
2367
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002368bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2369 switch (MI.getOpcode()) {
2370 // Byte
2371 case Hexagon::L2_loadrb_io:
2372 case Hexagon::L4_loadrb_ur:
2373 case Hexagon::L4_loadrb_ap:
2374 case Hexagon::L2_loadrb_pr:
2375 case Hexagon::L2_loadrb_pbr:
2376 case Hexagon::L2_loadrb_pi:
2377 case Hexagon::L2_loadrb_pci:
2378 case Hexagon::L2_loadrb_pcr:
2379 case Hexagon::L2_loadbsw2_io:
2380 case Hexagon::L4_loadbsw2_ur:
2381 case Hexagon::L4_loadbsw2_ap:
2382 case Hexagon::L2_loadbsw2_pr:
2383 case Hexagon::L2_loadbsw2_pbr:
2384 case Hexagon::L2_loadbsw2_pi:
2385 case Hexagon::L2_loadbsw2_pci:
2386 case Hexagon::L2_loadbsw2_pcr:
2387 case Hexagon::L2_loadbsw4_io:
2388 case Hexagon::L4_loadbsw4_ur:
2389 case Hexagon::L4_loadbsw4_ap:
2390 case Hexagon::L2_loadbsw4_pr:
2391 case Hexagon::L2_loadbsw4_pbr:
2392 case Hexagon::L2_loadbsw4_pi:
2393 case Hexagon::L2_loadbsw4_pci:
2394 case Hexagon::L2_loadbsw4_pcr:
2395 case Hexagon::L4_loadrb_rr:
2396 case Hexagon::L2_ploadrbt_io:
2397 case Hexagon::L2_ploadrbt_pi:
2398 case Hexagon::L2_ploadrbf_io:
2399 case Hexagon::L2_ploadrbf_pi:
2400 case Hexagon::L2_ploadrbtnew_io:
2401 case Hexagon::L2_ploadrbfnew_io:
2402 case Hexagon::L4_ploadrbt_rr:
2403 case Hexagon::L4_ploadrbf_rr:
2404 case Hexagon::L4_ploadrbtnew_rr:
2405 case Hexagon::L4_ploadrbfnew_rr:
2406 case Hexagon::L2_ploadrbtnew_pi:
2407 case Hexagon::L2_ploadrbfnew_pi:
2408 case Hexagon::L4_ploadrbt_abs:
2409 case Hexagon::L4_ploadrbf_abs:
2410 case Hexagon::L4_ploadrbtnew_abs:
2411 case Hexagon::L4_ploadrbfnew_abs:
2412 case Hexagon::L2_loadrbgp:
2413 // Half
2414 case Hexagon::L2_loadrh_io:
2415 case Hexagon::L4_loadrh_ur:
2416 case Hexagon::L4_loadrh_ap:
2417 case Hexagon::L2_loadrh_pr:
2418 case Hexagon::L2_loadrh_pbr:
2419 case Hexagon::L2_loadrh_pi:
2420 case Hexagon::L2_loadrh_pci:
2421 case Hexagon::L2_loadrh_pcr:
2422 case Hexagon::L4_loadrh_rr:
2423 case Hexagon::L2_ploadrht_io:
2424 case Hexagon::L2_ploadrht_pi:
2425 case Hexagon::L2_ploadrhf_io:
2426 case Hexagon::L2_ploadrhf_pi:
2427 case Hexagon::L2_ploadrhtnew_io:
2428 case Hexagon::L2_ploadrhfnew_io:
2429 case Hexagon::L4_ploadrht_rr:
2430 case Hexagon::L4_ploadrhf_rr:
2431 case Hexagon::L4_ploadrhtnew_rr:
2432 case Hexagon::L4_ploadrhfnew_rr:
2433 case Hexagon::L2_ploadrhtnew_pi:
2434 case Hexagon::L2_ploadrhfnew_pi:
2435 case Hexagon::L4_ploadrht_abs:
2436 case Hexagon::L4_ploadrhf_abs:
2437 case Hexagon::L4_ploadrhtnew_abs:
2438 case Hexagon::L4_ploadrhfnew_abs:
2439 case Hexagon::L2_loadrhgp:
2440 return true;
2441 default:
2442 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002443 }
2444}
2445
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002446bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2447 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002448 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2449}
2450
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002451bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2452 switch (MI.getOpcode()) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002453 case Hexagon::STriw_pred:
2454 case Hexagon::LDriw_pred:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002455 return true;
2456 default:
2457 return false;
2458 }
2459}
2460
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002461bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2462 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002463 return false;
2464
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002465 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002466 if (Op.isGlobal() || Op.isSymbol())
2467 return true;
2468 return false;
2469}
2470
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002471// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002472bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2473 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002474 return is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002475}
2476
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002477bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2478 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002479 return is_TC2(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002480}
2481
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002482bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2483 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002484 return is_TC2early(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002485}
2486
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002487bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2488 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002489 return is_TC4x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002490}
2491
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002492// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002493bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2494 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002495 if (mayBeCurLoad(MI1)) {
2496 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002497 unsigned DstReg = MI1.getOperand(0).getReg();
2498 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002499 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002500 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002501 return true;
2502 }
2503 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002504 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2505 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2506 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002507 return true;
2508 return false;
2509}
2510
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002511bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002512 const uint64_t V = getType(MI);
2513 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2514}
2515
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002516// Check if the Offset is a valid auto-inc imm by Load/Store Type.
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002517bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, int Offset) const {
2518 int Size = VT.getSizeInBits() / 8;
2519 if (Offset % Size != 0)
2520 return false;
2521 int Count = Offset / Size;
2522
2523 switch (VT.getSimpleVT().SimpleTy) {
2524 // For scalars the auto-inc is s4
2525 case MVT::i8:
2526 case MVT::i16:
2527 case MVT::i32:
2528 case MVT::i64:
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002529 case MVT::v2i16:
2530 case MVT::v2i32:
2531 case MVT::v4i8:
2532 case MVT::v4i16:
2533 case MVT::v8i8:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002534 return isInt<4>(Count);
2535 // For HVX vectors the auto-inc is s3
2536 case MVT::v64i8:
2537 case MVT::v32i16:
2538 case MVT::v16i32:
2539 case MVT::v8i64:
2540 case MVT::v128i8:
2541 case MVT::v64i16:
2542 case MVT::v32i32:
2543 case MVT::v16i64:
2544 return isInt<3>(Count);
2545 default:
2546 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002547 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002548
2549 llvm_unreachable("Not an valid type!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002550}
2551
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002552bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002553 const TargetRegisterInfo *TRI, bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002554 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002555 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002556 // inserted to calculate the final address. Due to this reason, the function
2557 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002558 // We used to assert if the offset was not properly aligned, however,
2559 // there are cases where a misaligned pointer recast can cause this
2560 // problem, and we need to allow for it. The front end warns of such
2561 // misaligns with respect to load size.
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002562 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002563 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002564 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002565 case Hexagon::PS_vstorerw_nt_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002566 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002567 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002568 case Hexagon::PS_vloadrw_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002569 case Hexagon::V6_vL32b_ai:
2570 case Hexagon::V6_vS32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002571 case Hexagon::V6_vL32b_nt_ai:
2572 case Hexagon::V6_vS32b_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002573 case Hexagon::V6_vL32Ub_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002574 case Hexagon::V6_vS32Ub_ai: {
2575 unsigned VectorSize = TRI->getSpillSize(Hexagon::HvxVRRegClass);
2576 assert(isPowerOf2_32(VectorSize));
2577 if (Offset & (VectorSize-1))
2578 return false;
2579 return isInt<4>(Offset >> Log2_32(VectorSize));
2580 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002581
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002582 case Hexagon::J2_loop0i:
2583 case Hexagon::J2_loop1i:
2584 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002585
2586 case Hexagon::S4_storeirb_io:
2587 case Hexagon::S4_storeirbt_io:
2588 case Hexagon::S4_storeirbf_io:
2589 return isUInt<6>(Offset);
2590
2591 case Hexagon::S4_storeirh_io:
2592 case Hexagon::S4_storeirht_io:
2593 case Hexagon::S4_storeirhf_io:
2594 return isShiftedUInt<6,1>(Offset);
2595
2596 case Hexagon::S4_storeiri_io:
2597 case Hexagon::S4_storeirit_io:
2598 case Hexagon::S4_storeirif_io:
2599 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002600 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002601
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002602 if (Extend)
2603 return true;
2604
2605 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002606 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002607 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002608 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2609 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2610
Colin LeMahieu947cd702014-12-23 20:44:59 +00002611 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002612 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002613 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2614 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2615
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002616 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002617 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002618 case Hexagon::S2_storerh_io:
Krzysztof Parzyszekd10df492017-05-03 15:36:51 +00002619 case Hexagon::S2_storerf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002620 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2621 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2622
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002623 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002624 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002625 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002626 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2627 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2628
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002629 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002630 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2631 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2632
Eugene Zelenko3b873362017-09-28 22:27:31 +00002633 case Hexagon::L4_iadd_memopw_io:
2634 case Hexagon::L4_isub_memopw_io:
2635 case Hexagon::L4_add_memopw_io:
2636 case Hexagon::L4_sub_memopw_io:
2637 case Hexagon::L4_and_memopw_io:
2638 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002639 return (0 <= Offset && Offset <= 255);
2640
Eugene Zelenko3b873362017-09-28 22:27:31 +00002641 case Hexagon::L4_iadd_memoph_io:
2642 case Hexagon::L4_isub_memoph_io:
2643 case Hexagon::L4_add_memoph_io:
2644 case Hexagon::L4_sub_memoph_io:
2645 case Hexagon::L4_and_memoph_io:
2646 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002647 return (0 <= Offset && Offset <= 127);
2648
Eugene Zelenko3b873362017-09-28 22:27:31 +00002649 case Hexagon::L4_iadd_memopb_io:
2650 case Hexagon::L4_isub_memopb_io:
2651 case Hexagon::L4_add_memopb_io:
2652 case Hexagon::L4_sub_memopb_io:
2653 case Hexagon::L4_and_memopb_io:
2654 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002655 return (0 <= Offset && Offset <= 63);
2656
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002657 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002658 // any size. Later pass knows how to handle it.
2659 case Hexagon::STriw_pred:
2660 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002661 case Hexagon::STriw_mod:
2662 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002663 return true;
2664
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002665 case Hexagon::PS_fi:
2666 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002667 case Hexagon::INLINEASM:
2668 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002669
2670 case Hexagon::L2_ploadrbt_io:
2671 case Hexagon::L2_ploadrbf_io:
2672 case Hexagon::L2_ploadrubt_io:
2673 case Hexagon::L2_ploadrubf_io:
2674 case Hexagon::S2_pstorerbt_io:
2675 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002676 return isUInt<6>(Offset);
2677
2678 case Hexagon::L2_ploadrht_io:
2679 case Hexagon::L2_ploadrhf_io:
2680 case Hexagon::L2_ploadruht_io:
2681 case Hexagon::L2_ploadruhf_io:
2682 case Hexagon::S2_pstorerht_io:
2683 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002684 return isShiftedUInt<6,1>(Offset);
2685
2686 case Hexagon::L2_ploadrit_io:
2687 case Hexagon::L2_ploadrif_io:
2688 case Hexagon::S2_pstorerit_io:
2689 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002690 return isShiftedUInt<6,2>(Offset);
2691
2692 case Hexagon::L2_ploadrdt_io:
2693 case Hexagon::L2_ploadrdf_io:
2694 case Hexagon::S2_pstorerdt_io:
2695 case Hexagon::S2_pstorerdf_io:
2696 return isShiftedUInt<6,3>(Offset);
2697 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002698
Benjamin Kramerb6684012011-12-27 11:41:05 +00002699 llvm_unreachable("No offset range is defined for this opcode. "
2700 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002701}
2702
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002703bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002704 return isHVXVec(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002705}
2706
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002707bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2708 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002709 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2710 return
2711 V == HexagonII::TypeCVI_VA ||
2712 V == HexagonII::TypeCVI_VA_DV;
2713}
Andrew Trickd06df962012-02-01 22:13:57 +00002714
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002715bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2716 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002717 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2718 return true;
2719
2720 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2721 return true;
2722
2723 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002724 return true;
2725
2726 return false;
2727}
Jyotsna Verma84256432013-03-01 17:37:13 +00002728
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002729bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2730 switch (MI.getOpcode()) {
2731 // Byte
2732 case Hexagon::L2_loadrub_io:
2733 case Hexagon::L4_loadrub_ur:
2734 case Hexagon::L4_loadrub_ap:
2735 case Hexagon::L2_loadrub_pr:
2736 case Hexagon::L2_loadrub_pbr:
2737 case Hexagon::L2_loadrub_pi:
2738 case Hexagon::L2_loadrub_pci:
2739 case Hexagon::L2_loadrub_pcr:
2740 case Hexagon::L2_loadbzw2_io:
2741 case Hexagon::L4_loadbzw2_ur:
2742 case Hexagon::L4_loadbzw2_ap:
2743 case Hexagon::L2_loadbzw2_pr:
2744 case Hexagon::L2_loadbzw2_pbr:
2745 case Hexagon::L2_loadbzw2_pi:
2746 case Hexagon::L2_loadbzw2_pci:
2747 case Hexagon::L2_loadbzw2_pcr:
2748 case Hexagon::L2_loadbzw4_io:
2749 case Hexagon::L4_loadbzw4_ur:
2750 case Hexagon::L4_loadbzw4_ap:
2751 case Hexagon::L2_loadbzw4_pr:
2752 case Hexagon::L2_loadbzw4_pbr:
2753 case Hexagon::L2_loadbzw4_pi:
2754 case Hexagon::L2_loadbzw4_pci:
2755 case Hexagon::L2_loadbzw4_pcr:
2756 case Hexagon::L4_loadrub_rr:
2757 case Hexagon::L2_ploadrubt_io:
2758 case Hexagon::L2_ploadrubt_pi:
2759 case Hexagon::L2_ploadrubf_io:
2760 case Hexagon::L2_ploadrubf_pi:
2761 case Hexagon::L2_ploadrubtnew_io:
2762 case Hexagon::L2_ploadrubfnew_io:
2763 case Hexagon::L4_ploadrubt_rr:
2764 case Hexagon::L4_ploadrubf_rr:
2765 case Hexagon::L4_ploadrubtnew_rr:
2766 case Hexagon::L4_ploadrubfnew_rr:
2767 case Hexagon::L2_ploadrubtnew_pi:
2768 case Hexagon::L2_ploadrubfnew_pi:
2769 case Hexagon::L4_ploadrubt_abs:
2770 case Hexagon::L4_ploadrubf_abs:
2771 case Hexagon::L4_ploadrubtnew_abs:
2772 case Hexagon::L4_ploadrubfnew_abs:
2773 case Hexagon::L2_loadrubgp:
2774 // Half
2775 case Hexagon::L2_loadruh_io:
2776 case Hexagon::L4_loadruh_ur:
2777 case Hexagon::L4_loadruh_ap:
2778 case Hexagon::L2_loadruh_pr:
2779 case Hexagon::L2_loadruh_pbr:
2780 case Hexagon::L2_loadruh_pi:
2781 case Hexagon::L2_loadruh_pci:
2782 case Hexagon::L2_loadruh_pcr:
2783 case Hexagon::L4_loadruh_rr:
2784 case Hexagon::L2_ploadruht_io:
2785 case Hexagon::L2_ploadruht_pi:
2786 case Hexagon::L2_ploadruhf_io:
2787 case Hexagon::L2_ploadruhf_pi:
2788 case Hexagon::L2_ploadruhtnew_io:
2789 case Hexagon::L2_ploadruhfnew_io:
2790 case Hexagon::L4_ploadruht_rr:
2791 case Hexagon::L4_ploadruhf_rr:
2792 case Hexagon::L4_ploadruhtnew_rr:
2793 case Hexagon::L4_ploadruhfnew_rr:
2794 case Hexagon::L2_ploadruhtnew_pi:
2795 case Hexagon::L2_ploadruhfnew_pi:
2796 case Hexagon::L4_ploadruht_abs:
2797 case Hexagon::L4_ploadruhf_abs:
2798 case Hexagon::L4_ploadruhtnew_abs:
2799 case Hexagon::L4_ploadruhfnew_abs:
2800 case Hexagon::L2_loadruhgp:
2801 return true;
2802 default:
2803 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002804 }
2805}
2806
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002807// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002808bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2809 const MachineInstr &MI2) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002810 if (isHVXVec(MI1) && isHVXVec(MI2))
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002811 if (!isVecUsableNextPacket(MI1, MI2))
2812 return true;
2813 return false;
2814}
2815
Brendon Cahoon254f8892016-07-29 16:44:44 +00002816/// \brief Get the base register and byte offset of a load/store instr.
2817bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2818 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2819 const {
2820 unsigned AccessSize = 0;
2821 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002822 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002823 Offset = OffsetVal;
2824 return BaseReg != 0;
2825}
2826
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002827/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002828bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2829 const MachineInstr &Second) const {
Krzysztof Parzyszek4763c2d2017-05-03 15:33:09 +00002830 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2831 const MachineOperand &Op = Second.getOperand(0);
2832 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2833 return true;
2834 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002835 if (DisableNVSchedule)
2836 return false;
2837 if (mayBeNewStore(Second)) {
2838 // Make sure the definition of the first instruction is the value being
2839 // stored.
2840 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002841 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002842 if (!Stored.isReg())
2843 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002844 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2845 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002846 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2847 return true;
2848 }
2849 }
2850 return false;
2851}
2852
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002853bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2854 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002855 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002856}
2857
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002858bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2859 for (auto &I : *B)
2860 if (I.isEHLabel())
2861 return true;
2862 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002863}
2864
Jyotsna Verma84256432013-03-01 17:37:13 +00002865// Returns true if an instruction can be converted into a non-extended
2866// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002867bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002868 short NonExtOpcode;
2869 // Check if the instruction has a register form that uses register in place
2870 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002871 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002872 return true;
2873
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002874 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002875 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002876
2877 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00002878 case HexagonII::Absolute:
Jyotsna Verma84256432013-03-01 17:37:13 +00002879 // Load/store with absolute addressing mode can be converted into
2880 // base+offset mode.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002881 NonExtOpcode = Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002882 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00002883 case HexagonII::BaseImmOffset:
Jyotsna Verma84256432013-03-01 17:37:13 +00002884 // Load/store with base+offset addressing mode can be converted into
2885 // base+register offset addressing mode. However left shift operand should
2886 // be set to 0.
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002887 NonExtOpcode = Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002888 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002889 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00002890 NonExtOpcode = Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002891 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002892 default:
2893 return false;
2894 }
2895 if (NonExtOpcode < 0)
2896 return false;
2897 return true;
2898 }
2899 return false;
2900}
2901
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002902bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2903 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002904 Hexagon::InstrType_Pseudo) >= 0;
2905}
2906
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002907bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2908 const {
2909 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2910 while (I != E) {
2911 if (I->isBarrier())
2912 return true;
2913 ++I;
2914 }
2915 return false;
2916}
2917
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002918// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002919bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002920 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002921 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00002922 Subtarget.hasV60TOps();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002923}
2924
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002925// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002926bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2927 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002928 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2929}
2930
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002931bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2932 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002933 // There is no stall when ProdMI is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002934 if (!isHVXVec(ProdMI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002935 return false;
2936
2937 // There is no stall when ProdMI and ConsMI are not dependent.
2938 if (!isDependent(ProdMI, ConsMI))
2939 return false;
2940
2941 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2942 // are scheduled in consecutive packets.
2943 if (isVecUsableNextPacket(ProdMI, ConsMI))
2944 return false;
2945
2946 return true;
2947}
2948
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002949bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002950 MachineBasicBlock::const_instr_iterator BII) const {
2951 // There is no stall when I is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002952 if (!isHVXVec(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002953 return false;
2954
2955 MachineBasicBlock::const_instr_iterator MII = BII;
2956 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2957
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00002958 if (!MII->isBundle())
2959 return producesStall(*MII, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002960
2961 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002962 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002963 if (producesStall(J, MI))
2964 return true;
2965 }
2966 return false;
2967}
2968
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002969bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002970 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002971 for (const MachineOperand &MO : MI.operands()) {
2972 // Predicate register must be explicitly defined.
2973 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
2974 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002975 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002976 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002977 }
2978
2979 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2980 // memd_locked cannot be used as .new as well,
2981 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002982 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002983}
2984
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002985bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00002986 return Opcode == Hexagon::J2_jumpt ||
2987 Opcode == Hexagon::J2_jumptpt ||
2988 Opcode == Hexagon::J2_jumpf ||
2989 Opcode == Hexagon::J2_jumpfpt ||
2990 Opcode == Hexagon::J2_jumptnew ||
2991 Opcode == Hexagon::J2_jumpfnew ||
2992 Opcode == Hexagon::J2_jumptnewpt ||
2993 Opcode == Hexagon::J2_jumpfnewpt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002994}
2995
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002996bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2997 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2998 return false;
2999 return !isPredicatedTrue(Cond[0].getImm());
3000}
3001
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003002unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3003 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003004 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3005}
3006
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003007// Returns the base register in a memory access (load/store). The offset is
3008// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003009// If the base register has a subregister or the offset field does not contain
3010// an immediate value, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003011unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003012 int &Offset, unsigned &AccessSize) const {
3013 // Return if it is not a base+offset type instruction or a MemOp.
3014 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3015 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003016 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003017 return 0;
3018
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003019 AccessSize = getMemAccessSize(MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003020
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003021 unsigned BasePos = 0, OffsetPos = 0;
3022 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003023 return 0;
3024
3025 // Post increment updates its EA after the mem access,
3026 // so we need to treat its offset as zero.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003027 if (isPostIncrement(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003028 Offset = 0;
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003029 } else {
3030 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
3031 if (!OffsetOp.isImm())
3032 return 0;
3033 Offset = OffsetOp.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003034 }
3035
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00003036 const MachineOperand &BaseOp = MI.getOperand(BasePos);
3037 if (BaseOp.getSubReg() != 0)
3038 return 0;
3039 return BaseOp.getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003040}
3041
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003042/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003043bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003044 unsigned &BasePos, unsigned &OffsetPos) const {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003045 if (!isAddrModeWithOffset(MI) && !isPostIncrement(MI))
3046 return false;
3047
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003048 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003049 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003050 BasePos = 0;
3051 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003052 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003053 BasePos = 0;
3054 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003055 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003056 BasePos = 1;
3057 OffsetPos = 2;
3058 } else
3059 return false;
3060
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003061 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003062 BasePos++;
3063 OffsetPos++;
3064 }
3065 if (isPostIncrement(MI)) {
3066 BasePos++;
3067 OffsetPos++;
3068 }
3069
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003070 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003071 return false;
3072
3073 return true;
3074}
3075
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003076// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003077// e.g. jump_t t1 (i1)
3078// jump t2 (i2)
3079// Jumpers = {i2, i1}
3080SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3081 MachineBasicBlock& MBB) const {
3082 SmallVector<MachineInstr*, 2> Jumpers;
3083 // If the block has no terminators, it just falls into the block after it.
3084 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3085 if (I == MBB.instr_begin())
3086 return Jumpers;
3087
3088 // A basic block may looks like this:
3089 //
3090 // [ insn
3091 // EH_LABEL
3092 // insn
3093 // insn
3094 // insn
3095 // EH_LABEL
3096 // insn ]
3097 //
3098 // It has two succs but does not have a terminator
3099 // Don't know how to handle it.
3100 do {
3101 --I;
3102 if (I->isEHLabel())
3103 return Jumpers;
3104 } while (I != MBB.instr_begin());
3105
3106 I = MBB.instr_end();
3107 --I;
3108
3109 while (I->isDebugValue()) {
3110 if (I == MBB.instr_begin())
3111 return Jumpers;
3112 --I;
3113 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003114 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003115 return Jumpers;
3116
3117 // Get the last instruction in the block.
3118 MachineInstr *LastInst = &*I;
3119 Jumpers.push_back(LastInst);
3120 MachineInstr *SecondLastInst = nullptr;
3121 // Find one more terminator if present.
3122 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003123 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003124 if (!SecondLastInst) {
3125 SecondLastInst = &*I;
3126 Jumpers.push_back(SecondLastInst);
3127 } else // This is a third branch.
3128 return Jumpers;
3129 }
3130 if (I == MBB.instr_begin())
3131 break;
3132 --I;
3133 } while (true);
3134 return Jumpers;
3135}
3136
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003137// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003138unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3139 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003140 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3141}
3142
3143// See if instruction could potentially be a duplex candidate.
3144// If so, return its group. Zero otherwise.
3145HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003146 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003147 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3148
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003149 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003150 default:
3151 return HexagonII::HCG_None;
3152 //
3153 // Compound pairs.
3154 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3155 // "Rd16=#U6 ; jump #r9:2"
3156 // "Rd16=Rs16 ; jump #r9:2"
3157 //
3158 case Hexagon::C2_cmpeq:
3159 case Hexagon::C2_cmpgt:
3160 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003161 DstReg = MI.getOperand(0).getReg();
3162 Src1Reg = MI.getOperand(1).getReg();
3163 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003164 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3165 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3166 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3167 return HexagonII::HCG_A;
3168 break;
3169 case Hexagon::C2_cmpeqi:
3170 case Hexagon::C2_cmpgti:
3171 case Hexagon::C2_cmpgtui:
3172 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003173 DstReg = MI.getOperand(0).getReg();
3174 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003175 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3176 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003177 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3178 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3179 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003180 return HexagonII::HCG_A;
3181 break;
3182 case Hexagon::A2_tfr:
3183 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003184 DstReg = MI.getOperand(0).getReg();
3185 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003186 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3187 return HexagonII::HCG_A;
3188 break;
3189 case Hexagon::A2_tfrsi:
3190 // Rd = #u6
3191 // Do not test for #u6 size since the const is getting extended
3192 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003193 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003194 if (isIntRegForSubInst(DstReg))
3195 return HexagonII::HCG_A;
3196 break;
3197 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003198 DstReg = MI.getOperand(0).getReg();
3199 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003200 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3201 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003202 MI.getOperand(2).isImm() &&
3203 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003204 return HexagonII::HCG_A;
3205 break;
3206 // The fact that .new form is used pretty much guarantees
3207 // that predicate register will match. Nevertheless,
3208 // there could be some false positives without additional
3209 // checking.
3210 case Hexagon::J2_jumptnew:
3211 case Hexagon::J2_jumpfnew:
3212 case Hexagon::J2_jumptnewpt:
3213 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003214 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003215 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3216 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3217 return HexagonII::HCG_B;
3218 break;
3219 // Transfer and jump:
3220 // Rd=#U6 ; jump #r9:2
3221 // Rd=Rs ; jump #r9:2
3222 // Do not test for jump range here.
3223 case Hexagon::J2_jump:
3224 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003225 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003226 return HexagonII::HCG_C;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003227 }
3228
3229 return HexagonII::HCG_None;
3230}
3231
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003232// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003233unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3234 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003235 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3236 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003237 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3238 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003239 return -1u;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003240 unsigned DestReg = GA.getOperand(0).getReg();
3241 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003242 return -1u;
3243 if (DestReg != Hexagon::P0 && DestReg != Hexagon::P1)
3244 return -1u;
3245 // The value compared against must be either u5 or -1.
3246 const MachineOperand &CmpOp = GA.getOperand(2);
3247 if (!CmpOp.isImm())
3248 return -1u;
3249 int V = CmpOp.getImm();
3250 if (V == -1)
3251 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqn1_tp0_jump_nt
3252 : Hexagon::J4_cmpeqn1_tp1_jump_nt;
3253 if (!isUInt<5>(V))
3254 return -1u;
3255 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqi_tp0_jump_nt
3256 : Hexagon::J4_cmpeqi_tp1_jump_nt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003257}
3258
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003259int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3260 enum Hexagon::PredSense inPredSense;
3261 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3262 Hexagon::PredSense_true;
3263 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3264 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3265 return CondOpcode;
3266
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003267 llvm_unreachable("Unexpected predicable instruction");
3268}
3269
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003270// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003271int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3272 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003273 default: llvm_unreachable("Unknown .cur type");
3274 case Hexagon::V6_vL32b_pi:
3275 return Hexagon::V6_vL32b_cur_pi;
3276 case Hexagon::V6_vL32b_ai:
3277 return Hexagon::V6_vL32b_cur_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003278 case Hexagon::V6_vL32b_nt_pi:
3279 return Hexagon::V6_vL32b_nt_cur_pi;
3280 case Hexagon::V6_vL32b_nt_ai:
3281 return Hexagon::V6_vL32b_nt_cur_ai;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003282 }
3283 return 0;
3284}
3285
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003286// Return the regular version of the .cur instruction.
3287int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3288 switch (MI.getOpcode()) {
3289 default: llvm_unreachable("Unknown .cur type");
3290 case Hexagon::V6_vL32b_cur_pi:
3291 return Hexagon::V6_vL32b_pi;
3292 case Hexagon::V6_vL32b_cur_ai:
3293 return Hexagon::V6_vL32b_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003294 case Hexagon::V6_vL32b_nt_cur_pi:
3295 return Hexagon::V6_vL32b_nt_pi;
3296 case Hexagon::V6_vL32b_nt_cur_ai:
3297 return Hexagon::V6_vL32b_nt_ai;
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003298 }
3299 return 0;
3300}
3301
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003302// The diagram below shows the steps involved in the conversion of a predicated
3303// store instruction to its .new predicated new-value form.
3304//
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003305// Note: It doesn't include conditional new-value stores as they can't be
3306// converted to .new predicate.
3307//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003308// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3309// ^ ^
3310// / \ (not OK. it will cause new-value store to be
3311// / X conditional on p0.new while R2 producer is
3312// / \ on p0)
3313// / \.
3314// p.new store p.old NV store
3315// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3316// ^ ^
3317// \ /
3318// \ /
3319// \ /
3320// p.old store
3321// [if (p0)memw(R0+#0)=R2]
3322//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003323// The following set of instructions further explains the scenario where
3324// conditional new-value store becomes invalid when promoted to .new predicate
3325// form.
3326//
3327// { 1) if (p0) r0 = add(r1, r2)
3328// 2) p0 = cmp.eq(r3, #0) }
3329//
3330// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3331// the first two instructions because in instr 1, r0 is conditional on old value
3332// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3333// is not valid for new-value stores.
3334// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3335// from the "Conditional Store" list. Because a predicated new value store
3336// would NOT be promoted to a double dot new store. See diagram below:
3337// This function returns yes for those stores that are predicated but not
3338// yet promoted to predicate dot new instructions.
3339//
3340// +---------------------+
3341// /-----| if (p0) memw(..)=r0 |---------\~
3342// || +---------------------+ ||
3343// promote || /\ /\ || promote
3344// || /||\ /||\ ||
3345// \||/ demote || \||/
3346// \/ || || \/
3347// +-------------------------+ || +-------------------------+
3348// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3349// +-------------------------+ || +-------------------------+
3350// || || ||
3351// || demote \||/
3352// promote || \/ NOT possible
3353// || || /\~
3354// \||/ || /||\~
3355// \/ || ||
3356// +-----------------------------+
3357// | if (p0.new) memw(..)=r0.new |
3358// +-----------------------------+
3359// Double Dot New Store
3360//
3361// Returns the most basic instruction for the .new predicated instructions and
3362// new-value stores.
3363// For example, all of the following instructions will be converted back to the
3364// same instruction:
3365// 1) if (p0.new) memw(R0+#0) = R1.new --->
3366// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3367// 3) if (p0.new) memw(R0+#0) = R1 --->
3368//
3369// To understand the translation of instruction 1 to its original form, consider
3370// a packet with 3 instructions.
3371// { p0 = cmp.eq(R0,R1)
3372// if (p0.new) R2 = add(R3, R4)
3373// R5 = add (R3, R1)
3374// }
3375// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3376//
3377// This instruction can be part of the previous packet only if both p0 and R2
3378// are promoted to .new values. This promotion happens in steps, first
3379// predicate register is promoted to .new and in the next iteration R2 is
3380// promoted. Therefore, in case of dependence check failure (due to R5) during
3381// next iteration, it should be converted back to its most basic form.
3382
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003383// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003384int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3385 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003386 if (NVOpcode >= 0) // Valid new-value store instruction.
3387 return NVOpcode;
3388
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003389 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003390 default:
Eugene Zelenko3b873362017-09-28 22:27:31 +00003391 report_fatal_error(std::string("Unknown .new type: ") +
3392 std::to_string(MI.getOpcode()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003393 case Hexagon::S4_storerb_ur:
3394 return Hexagon::S4_storerbnew_ur;
3395
3396 case Hexagon::S2_storerb_pci:
3397 return Hexagon::S2_storerb_pci;
3398
3399 case Hexagon::S2_storeri_pci:
3400 return Hexagon::S2_storeri_pci;
3401
3402 case Hexagon::S2_storerh_pci:
3403 return Hexagon::S2_storerh_pci;
3404
3405 case Hexagon::S2_storerd_pci:
3406 return Hexagon::S2_storerd_pci;
3407
3408 case Hexagon::S2_storerf_pci:
3409 return Hexagon::S2_storerf_pci;
3410
3411 case Hexagon::V6_vS32b_ai:
3412 return Hexagon::V6_vS32b_new_ai;
3413
3414 case Hexagon::V6_vS32b_pi:
3415 return Hexagon::V6_vS32b_new_pi;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003416 }
3417 return 0;
3418}
3419
3420// Returns the opcode to use when converting MI, which is a conditional jump,
3421// into a conditional instruction which uses the .new value of the predicate.
3422// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003423// If MBPI is null, all edges will be treated as equally likely for the
3424// purposes of establishing a predication hint.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003425int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003426 const MachineBranchProbabilityInfo *MBPI) const {
3427 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003428 const MachineBasicBlock *Src = MI.getParent();
3429 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003430 bool Taken = false;
3431 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003432
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003433 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3434 const MachineBasicBlock *Dst) {
3435 if (MBPI)
3436 return MBPI->getEdgeProbability(Src, Dst);
3437 return BranchProbability(1, Src->succ_size());
3438 };
3439
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003440 if (BrTarget.isMBB()) {
3441 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003442 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003443 } else {
3444 // The branch target is not a basic block (most likely a function).
3445 // Since BPI only gives probabilities for targets that are basic blocks,
3446 // try to identify another target of this branch (potentially a fall-
3447 // -through) and check the probability of that target.
3448 //
3449 // The only handled branch combinations are:
3450 // - one conditional branch,
3451 // - one conditional branch followed by one unconditional branch.
3452 // Otherwise, assume not-taken.
3453 assert(MI.isConditionalBranch());
3454 const MachineBasicBlock &B = *MI.getParent();
3455 bool SawCond = false, Bad = false;
3456 for (const MachineInstr &I : B) {
3457 if (!I.isBranch())
3458 continue;
3459 if (I.isConditionalBranch()) {
3460 SawCond = true;
3461 if (&I != &MI) {
3462 Bad = true;
3463 break;
3464 }
3465 }
3466 if (I.isUnconditionalBranch() && !SawCond) {
3467 Bad = true;
3468 break;
3469 }
3470 }
3471 if (!Bad) {
3472 MachineBasicBlock::const_instr_iterator It(MI);
3473 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3474 if (NextIt == B.instr_end()) {
3475 // If this branch is the last, look for the fall-through block.
3476 for (const MachineBasicBlock *SB : B.successors()) {
3477 if (!B.isLayoutSuccessor(SB))
3478 continue;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003479 Taken = getEdgeProbability(Src, SB) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003480 break;
3481 }
3482 } else {
3483 assert(NextIt->isUnconditionalBranch());
3484 // Find the first MBB operand and assume it's the target.
3485 const MachineBasicBlock *BT = nullptr;
3486 for (const MachineOperand &Op : NextIt->operands()) {
3487 if (!Op.isMBB())
3488 continue;
3489 BT = Op.getMBB();
3490 break;
3491 }
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003492 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003493 }
3494 } // if (!Bad)
3495 }
3496
3497 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003498
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003499 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003500 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003501 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003502 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003503 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003504
3505 default:
3506 llvm_unreachable("Unexpected jump instruction.");
3507 }
3508}
3509
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003510// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003511int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003512 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003513 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003514 // Condtional Jumps
3515 case Hexagon::J2_jumpt:
3516 case Hexagon::J2_jumpf:
3517 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003518 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003519
3520 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3521 if (NewOpcode >= 0)
3522 return NewOpcode;
Krzysztof Parzyszek066e8b52017-06-02 14:07:06 +00003523 return 0;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003524}
3525
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003526int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
3527 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003528 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3529 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003530 // All Hexagon architectures have prediction bits on dot-new branches,
3531 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3532 // to pick the right opcode when converting back to dot-old.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003533 if (!Subtarget.getFeatureBits()[Hexagon::ArchV60]) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003534 switch (NewOp) {
3535 case Hexagon::J2_jumptpt:
3536 NewOp = Hexagon::J2_jumpt;
3537 break;
3538 case Hexagon::J2_jumpfpt:
3539 NewOp = Hexagon::J2_jumpf;
3540 break;
3541 case Hexagon::J2_jumprtpt:
3542 NewOp = Hexagon::J2_jumprt;
3543 break;
3544 case Hexagon::J2_jumprfpt:
3545 NewOp = Hexagon::J2_jumprf;
3546 break;
3547 }
3548 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003549 assert(NewOp >= 0 &&
3550 "Couldn't change predicate new instruction to its old form.");
3551 }
3552
3553 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3554 NewOp = Hexagon::getNonNVStore(NewOp);
3555 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3556 }
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003557
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003558 if (Subtarget.hasV60TOps())
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003559 return NewOp;
3560
3561 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3562 switch (NewOp) {
3563 case Hexagon::J2_jumpfpt:
3564 return Hexagon::J2_jumpf;
3565 case Hexagon::J2_jumptpt:
3566 return Hexagon::J2_jumpt;
3567 case Hexagon::J2_jumprfpt:
3568 return Hexagon::J2_jumprf;
3569 case Hexagon::J2_jumprtpt:
3570 return Hexagon::J2_jumprt;
3571 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003572 return NewOp;
3573}
3574
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003575// See if instruction could potentially be a duplex candidate.
3576// If so, return its group. Zero otherwise.
3577HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003578 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003579 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003580 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003581
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003582 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003583 default:
3584 return HexagonII::HSIG_None;
3585 //
3586 // Group L1:
3587 //
3588 // Rd = memw(Rs+#u4:2)
3589 // Rd = memub(Rs+#u4:0)
3590 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003591 DstReg = MI.getOperand(0).getReg();
3592 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003593 // Special case this one from Group L2.
3594 // Rd = memw(r29+#u5:2)
3595 if (isIntRegForSubInst(DstReg)) {
3596 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3597 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003598 MI.getOperand(2).isImm() &&
3599 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003600 return HexagonII::HSIG_L2;
3601 // Rd = memw(Rs+#u4:2)
3602 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003603 (MI.getOperand(2).isImm() &&
3604 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003605 return HexagonII::HSIG_L1;
3606 }
3607 break;
3608 case Hexagon::L2_loadrub_io:
3609 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003610 DstReg = MI.getOperand(0).getReg();
3611 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003612 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003613 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003614 return HexagonII::HSIG_L1;
3615 break;
3616 //
3617 // Group L2:
3618 //
3619 // Rd = memh/memuh(Rs+#u3:1)
3620 // Rd = memb(Rs+#u3:0)
3621 // Rd = memw(r29+#u5:2) - Handled above.
3622 // Rdd = memd(r29+#u5:3)
3623 // deallocframe
3624 // [if ([!]p0[.new])] dealloc_return
3625 // [if ([!]p0[.new])] jumpr r31
3626 case Hexagon::L2_loadrh_io:
3627 case Hexagon::L2_loadruh_io:
3628 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003629 DstReg = MI.getOperand(0).getReg();
3630 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003631 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003632 MI.getOperand(2).isImm() &&
3633 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003634 return HexagonII::HSIG_L2;
3635 break;
3636 case Hexagon::L2_loadrb_io:
3637 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003638 DstReg = MI.getOperand(0).getReg();
3639 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003640 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003641 MI.getOperand(2).isImm() &&
3642 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003643 return HexagonII::HSIG_L2;
3644 break;
3645 case Hexagon::L2_loadrd_io:
3646 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003647 DstReg = MI.getOperand(0).getReg();
3648 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003649 if (isDblRegForSubInst(DstReg, HRI) &&
3650 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3651 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003652 MI.getOperand(2).isImm() &&
3653 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003654 return HexagonII::HSIG_L2;
3655 break;
3656 // dealloc_return is not documented in Hexagon Manual, but marked
3657 // with A_SUBINSN attribute in iset_v4classic.py.
3658 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003659 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003660 case Hexagon::L4_return:
3661 case Hexagon::L2_deallocframe:
3662 return HexagonII::HSIG_L2;
3663 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003664 case Hexagon::PS_jmpret:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003665 case Hexagon::SL2_jumpr31:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003666 // jumpr r31
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003667 // Actual form JMPR implicit-def %pc, implicit %r31, implicit internal %r0
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003668 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003669 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3670 return HexagonII::HSIG_L2;
3671 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003672 case Hexagon::PS_jmprett:
3673 case Hexagon::PS_jmpretf:
3674 case Hexagon::PS_jmprettnewpt:
3675 case Hexagon::PS_jmpretfnewpt:
3676 case Hexagon::PS_jmprettnew:
3677 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003678 case Hexagon::SL2_jumpr31_t:
3679 case Hexagon::SL2_jumpr31_f:
3680 case Hexagon::SL2_jumpr31_tnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003681 DstReg = MI.getOperand(1).getReg();
3682 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003683 // [if ([!]p0[.new])] jumpr r31
3684 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3685 (Hexagon::P0 == SrcReg)) &&
3686 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3687 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003688 break;
Eugene Zelenko3b873362017-09-28 22:27:31 +00003689 case Hexagon::L4_return_t:
3690 case Hexagon::L4_return_f:
3691 case Hexagon::L4_return_tnew_pnt:
3692 case Hexagon::L4_return_fnew_pnt:
3693 case Hexagon::L4_return_tnew_pt:
3694 case Hexagon::L4_return_fnew_pt:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003695 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003696 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003697 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3698 return HexagonII::HSIG_L2;
3699 break;
3700 //
3701 // Group S1:
3702 //
3703 // memw(Rs+#u4:2) = Rt
3704 // memb(Rs+#u4:0) = Rt
3705 case Hexagon::S2_storeri_io:
3706 // Special case this one from Group S2.
3707 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003708 Src1Reg = MI.getOperand(0).getReg();
3709 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003710 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3711 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003712 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3713 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003714 return HexagonII::HSIG_S2;
3715 // memw(Rs+#u4:2) = Rt
3716 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003717 MI.getOperand(1).isImm() &&
3718 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003719 return HexagonII::HSIG_S1;
3720 break;
3721 case Hexagon::S2_storerb_io:
3722 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003723 Src1Reg = MI.getOperand(0).getReg();
3724 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003725 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003726 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003727 return HexagonII::HSIG_S1;
3728 break;
3729 //
3730 // Group S2:
3731 //
3732 // memh(Rs+#u3:1) = Rt
3733 // memw(r29+#u5:2) = Rt
3734 // memd(r29+#s6:3) = Rtt
3735 // memw(Rs+#u4:2) = #U1
3736 // memb(Rs+#u4) = #U1
3737 // allocframe(#u5:3)
3738 case Hexagon::S2_storerh_io:
3739 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003740 Src1Reg = MI.getOperand(0).getReg();
3741 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003742 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003743 MI.getOperand(1).isImm() &&
3744 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003745 return HexagonII::HSIG_S1;
3746 break;
3747 case Hexagon::S2_storerd_io:
3748 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003749 Src1Reg = MI.getOperand(0).getReg();
3750 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003751 if (isDblRegForSubInst(Src2Reg, HRI) &&
3752 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003753 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3754 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003755 return HexagonII::HSIG_S2;
3756 break;
3757 case Hexagon::S4_storeiri_io:
3758 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003759 Src1Reg = MI.getOperand(0).getReg();
3760 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3761 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3762 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003763 return HexagonII::HSIG_S2;
3764 break;
3765 case Hexagon::S4_storeirb_io:
3766 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003767 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003768 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003769 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3770 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003771 return HexagonII::HSIG_S2;
3772 break;
3773 case Hexagon::S2_allocframe:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003774 if (MI.getOperand(2).isImm() &&
3775 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003776 return HexagonII::HSIG_S1;
3777 break;
3778 //
3779 // Group A:
3780 //
3781 // Rx = add(Rx,#s7)
3782 // Rd = Rs
3783 // Rd = #u6
3784 // Rd = #-1
3785 // if ([!]P0[.new]) Rd = #0
3786 // Rd = add(r29,#u6:2)
3787 // Rx = add(Rx,Rs)
3788 // P0 = cmp.eq(Rs,#u2)
3789 // Rdd = combine(#0,Rs)
3790 // Rdd = combine(Rs,#0)
3791 // Rdd = combine(#u2,#U2)
3792 // Rd = add(Rs,#1)
3793 // Rd = add(Rs,#-1)
3794 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3795 // Rd = and(Rs,#1)
3796 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003797 DstReg = MI.getOperand(0).getReg();
3798 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003799 if (isIntRegForSubInst(DstReg)) {
3800 // Rd = add(r29,#u6:2)
3801 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003802 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3803 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003804 return HexagonII::HSIG_A;
3805 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003806 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3807 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003808 return HexagonII::HSIG_A;
3809 // Rd = add(Rs,#1)
3810 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003811 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3812 ((MI.getOperand(2).getImm() == 1) ||
3813 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003814 return HexagonII::HSIG_A;
3815 }
3816 break;
3817 case Hexagon::A2_add:
3818 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003819 DstReg = MI.getOperand(0).getReg();
3820 Src1Reg = MI.getOperand(1).getReg();
3821 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003822 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3823 isIntRegForSubInst(Src2Reg))
3824 return HexagonII::HSIG_A;
3825 break;
3826 case Hexagon::A2_andir:
3827 // Same as zxtb.
3828 // Rd16=and(Rs16,#255)
3829 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003830 DstReg = MI.getOperand(0).getReg();
3831 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003832 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003833 MI.getOperand(2).isImm() &&
3834 ((MI.getOperand(2).getImm() == 1) ||
3835 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003836 return HexagonII::HSIG_A;
3837 break;
3838 case Hexagon::A2_tfr:
3839 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003840 DstReg = MI.getOperand(0).getReg();
3841 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003842 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3843 return HexagonII::HSIG_A;
3844 break;
3845 case Hexagon::A2_tfrsi:
3846 // Rd = #u6
3847 // Do not test for #u6 size since the const is getting extended
3848 // regardless and compound could be formed.
3849 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003850 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003851 if (isIntRegForSubInst(DstReg))
3852 return HexagonII::HSIG_A;
3853 break;
3854 case Hexagon::C2_cmoveit:
3855 case Hexagon::C2_cmovenewit:
3856 case Hexagon::C2_cmoveif:
3857 case Hexagon::C2_cmovenewif:
3858 // if ([!]P0[.new]) Rd = #0
3859 // Actual form:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00003860 // %r16 = C2_cmovenewit internal %p0, 0, implicit undef %r16;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003861 DstReg = MI.getOperand(0).getReg();
3862 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003863 if (isIntRegForSubInst(DstReg) &&
3864 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003865 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003866 return HexagonII::HSIG_A;
3867 break;
3868 case Hexagon::C2_cmpeqi:
3869 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003870 DstReg = MI.getOperand(0).getReg();
3871 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003872 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3873 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003874 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003875 return HexagonII::HSIG_A;
3876 break;
3877 case Hexagon::A2_combineii:
3878 case Hexagon::A4_combineii:
3879 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003880 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003881 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003882 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3883 (MI.getOperand(1).isGlobal() &&
3884 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3885 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3886 (MI.getOperand(2).isGlobal() &&
3887 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003888 return HexagonII::HSIG_A;
3889 break;
3890 case Hexagon::A4_combineri:
3891 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003892 DstReg = MI.getOperand(0).getReg();
3893 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003894 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003895 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3896 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003897 return HexagonII::HSIG_A;
3898 break;
3899 case Hexagon::A4_combineir:
3900 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003901 DstReg = MI.getOperand(0).getReg();
3902 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003903 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003904 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3905 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003906 return HexagonII::HSIG_A;
3907 break;
3908 case Hexagon::A2_sxtb:
3909 case Hexagon::A2_sxth:
3910 case Hexagon::A2_zxtb:
3911 case Hexagon::A2_zxth:
3912 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003913 DstReg = MI.getOperand(0).getReg();
3914 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003915 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3916 return HexagonII::HSIG_A;
3917 break;
3918 }
3919
3920 return HexagonII::HSIG_None;
3921}
3922
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003923short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3924 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003925}
3926
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003927unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003928 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003929 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3930 // still have a MinLatency property, which getStageLatency checks.
3931 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003932 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003933
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003934 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003935 return 0;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003936 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
3937}
3938
3939/// getOperandLatency - Compute and return the use operand latency of a given
3940/// pair of def and use.
3941/// In most cases, the static scheduling itinerary was enough to determine the
3942/// operand latency. But it may not be possible for instructions with variable
3943/// number of defs / uses.
3944///
3945/// This is a raw interface to the itinerary that may be directly overriden by
3946/// a target. Use computeOperandLatency to get the best estimate of latency.
3947int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3948 const MachineInstr &DefMI,
3949 unsigned DefIdx,
3950 const MachineInstr &UseMI,
3951 unsigned UseIdx) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00003952 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003953
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003954 // Get DefIdx and UseIdx for super registers.
3955 MachineOperand DefMO = DefMI.getOperand(DefIdx);
3956
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003957 if (HRI.isPhysicalRegister(DefMO.getReg())) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003958 if (DefMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003959 for (MCSuperRegIterator SR(DefMO.getReg(), &HRI); SR.isValid(); ++SR) {
3960 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003961 if (Idx != -1) {
3962 DefIdx = Idx;
3963 break;
3964 }
3965 }
3966 }
3967
3968 MachineOperand UseMO = UseMI.getOperand(UseIdx);
3969 if (UseMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003970 for (MCSuperRegIterator SR(UseMO.getReg(), &HRI); SR.isValid(); ++SR) {
3971 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003972 if (Idx != -1) {
3973 UseIdx = Idx;
3974 break;
3975 }
3976 }
3977 }
3978 }
3979
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00003980 int Latency = TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
3981 UseMI, UseIdx);
3982 if (!Latency)
3983 // We should never have 0 cycle latency between two instructions unless
3984 // they can be packetized together. However, this decision can't be made
3985 // here.
3986 Latency = 1;
3987 return Latency;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003988}
3989
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003990// inverts the predication logic.
3991// p -> NotP
3992// NotP -> P
3993bool HexagonInstrInfo::getInvertedPredSense(
3994 SmallVectorImpl<MachineOperand> &Cond) const {
3995 if (Cond.empty())
3996 return false;
3997 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3998 Cond[0].setImm(Opc);
3999 return true;
4000}
4001
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004002unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4003 int InvPredOpcode;
4004 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4005 : Hexagon::getTruePredOpcode(Opc);
4006 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4007 return InvPredOpcode;
4008
4009 llvm_unreachable("Unexpected predicated instruction");
4010}
4011
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004012// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004013int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4014 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004015 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4016 & HexagonII::ExtentSignedMask;
4017 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4018 & HexagonII::ExtentBitsMask;
4019
4020 if (isSigned) // if value is signed
4021 return ~(-1U << (bits - 1));
4022 else
4023 return ~(-1U << bits);
4024}
4025
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004026
4027bool HexagonInstrInfo::isAddrModeWithOffset(const MachineInstr &MI) const {
4028 switch (MI.getOpcode()) {
4029 case Hexagon::L2_loadrbgp:
4030 case Hexagon::L2_loadrdgp:
4031 case Hexagon::L2_loadrhgp:
4032 case Hexagon::L2_loadrigp:
4033 case Hexagon::L2_loadrubgp:
4034 case Hexagon::L2_loadruhgp:
4035 case Hexagon::S2_storerbgp:
4036 case Hexagon::S2_storerbnewgp:
4037 case Hexagon::S2_storerhgp:
4038 case Hexagon::S2_storerhnewgp:
4039 case Hexagon::S2_storerigp:
4040 case Hexagon::S2_storerinewgp:
4041 case Hexagon::S2_storerdgp:
4042 case Hexagon::S2_storerfgp:
4043 return true;
4044 }
4045 const uint64_t F = MI.getDesc().TSFlags;
4046 unsigned addrMode =
4047 ((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
4048 // Disallow any base+offset instruction. The assembler does not yet reorder
4049 // based up any zero offset instruction.
4050 return (addrMode == HexagonII::BaseRegOffset ||
4051 addrMode == HexagonII::BaseImmOffset ||
4052 addrMode == HexagonII::BaseLongOffset);
4053}
4054
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004055unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004056 using namespace HexagonII;
Eugene Zelenko3b873362017-09-28 22:27:31 +00004057
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004058 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004059 unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
4060 unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
4061 if (Size != 0)
4062 return Size;
4063
4064 // Handle vector access sizes.
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004065 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004066 switch (S) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00004067 case HexagonII::HVXVectorAccess:
4068 return HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00004069 default:
4070 llvm_unreachable("Unexpected instruction");
4071 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004072}
4073
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004074// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004075int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4076 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004077 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4078 & HexagonII::ExtentSignedMask;
4079 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4080 & HexagonII::ExtentBitsMask;
4081
4082 if (isSigned) // if value is signed
4083 return -1U << (bits - 1);
4084 else
4085 return 0;
4086}
4087
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004088// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004089short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004090 // Check if the instruction has a register form that uses register in place
4091 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004092 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004093 if (NonExtOpcode >= 0)
4094 return NonExtOpcode;
4095
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004096 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004097 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004098 switch (getAddrMode(MI)) {
Eugene Zelenko3b873362017-09-28 22:27:31 +00004099 case HexagonII::Absolute:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004100 return Hexagon::changeAddrMode_abs_io(MI.getOpcode());
Eugene Zelenko3b873362017-09-28 22:27:31 +00004101 case HexagonII::BaseImmOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004102 return Hexagon::changeAddrMode_io_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004103 case HexagonII::BaseLongOffset:
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004104 return Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004105
Jyotsna Verma84256432013-03-01 17:37:13 +00004106 default:
4107 return -1;
4108 }
4109 }
4110 return -1;
4111}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004112
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004113bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004114 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004115 if (Cond.empty())
4116 return false;
4117 assert(Cond.size() == 2);
4118 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004119 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4120 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004121 }
4122 PredReg = Cond[1].getReg();
4123 PredRegPos = 1;
4124 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4125 PredRegFlags = 0;
4126 if (Cond[1].isImplicit())
4127 PredRegFlags = RegState::Implicit;
4128 if (Cond[1].isUndef())
4129 PredRegFlags |= RegState::Undef;
4130 return true;
4131}
4132
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004133short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4134 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004135}
4136
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004137short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4138 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004139}
4140
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004141// Return the number of bytes required to encode the instruction.
4142// Hexagon instructions are fixed length, 4 bytes, unless they
4143// use a constant extender, which requires another 4 bytes.
4144// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004145unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4146 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004147 return 0;
4148
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004149 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004150 if (!Size)
4151 // Assume the default insn size in case it cannot be determined
4152 // for whatever reason.
4153 Size = HEXAGON_INSTR_SIZE;
4154
4155 if (isConstExtended(MI) || isExtended(MI))
4156 Size += HEXAGON_INSTR_SIZE;
4157
4158 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004159 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4160 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004161 const MachineFunction *MF = MBB.getParent();
4162 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4163
4164 // Count the number of register definitions to find the asm string.
4165 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004166 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004167 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004168 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004169
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004170 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004171 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004172 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004173 Size = getInlineAsmLength(AsmStr, *MAI);
4174 }
4175
4176 return Size;
4177}
4178
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004179uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4180 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004181 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4182}
4183
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004184unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
Krzysztof Parzyszek4697dde2017-10-04 18:00:15 +00004185 const InstrItineraryData &II = *Subtarget.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004186 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004187
4188 return IS.getUnits();
4189}
4190
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004191// Calculate size of the basic block without debug instructions.
4192unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4193 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4194}
4195
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004196unsigned HexagonInstrInfo::nonDbgBundleSize(
4197 MachineBasicBlock::const_iterator BundleHead) const {
4198 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004199 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004200 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004201 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004202}
4203
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004204/// immediateExtend - Changes the instruction in place to one using an immediate
4205/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004206void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004207 assert((isExtendable(MI)||isConstExtended(MI)) &&
4208 "Instruction must be extendable");
4209 // Find which operand is extendable.
4210 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004211 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004212 // This needs to be something we understand.
4213 assert((MO.isMBB() || MO.isImm()) &&
4214 "Branch with unknown extendable field type");
4215 // Mark given operand as extended.
4216 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4217}
4218
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004219bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004220 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00004221 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to "
4222 << printMBBReference(*NewTarget);
4223 MI.dump(););
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004224 assert(MI.isBranch());
4225 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4226 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004227 // In general branch target is the last operand,
4228 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004229 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004230 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004231 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4232 MI.getOperand(TargetPos).setMBB(NewTarget);
4233 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004234 NewOpcode = reversePrediction(NewOpcode);
4235 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004236 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004237 return true;
4238}
4239
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004240void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4241 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4242 MachineFunction::iterator A = MF.begin();
4243 MachineBasicBlock &B = *A;
4244 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004245 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004246 MachineInstr *NewMI;
4247
4248 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4249 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004250 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004251 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4252 " Class: " << NewMI->getDesc().getSchedClass());
4253 NewMI->eraseFromParent();
4254 }
4255 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4256}
4257
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004258// inverts the predication logic.
4259// p -> NotP
4260// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004261bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4262 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4263 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004264 return true;
4265}
4266
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004267// Reverse the branch prediction.
4268unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4269 int PredRevOpcode = -1;
4270 if (isPredictedTaken(Opcode))
4271 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4272 else
4273 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4274 assert(PredRevOpcode > 0);
4275 return PredRevOpcode;
4276}
4277
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004278// TODO: Add more rigorous validation.
4279bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4280 const {
4281 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4282}
4283
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004284void HexagonInstrInfo::
4285setBundleNoShuf(MachineBasicBlock::instr_iterator MIB) const {
4286 assert(MIB->isBundle());
4287 MachineOperand &Operand = MIB->getOperand(0);
4288 if (Operand.isImm())
4289 Operand.setImm(Operand.getImm() | memShufDisabledMask);
4290 else
4291 MIB->addOperand(MachineOperand::CreateImm(memShufDisabledMask));
4292}
4293
4294bool HexagonInstrInfo::getBundleNoShuf(const MachineInstr &MIB) const {
4295 assert(MIB.isBundle());
4296 const MachineOperand &Operand = MIB.getOperand(0);
4297 return (Operand.isImm() && (Operand.getImm() & memShufDisabledMask) != 0);
4298}
4299
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004300// Addressing mode relations.
4301short HexagonInstrInfo::changeAddrMode_abs_io(short Opc) const {
4302 return Opc >= 0 ? Hexagon::changeAddrMode_abs_io(Opc) : Opc;
4303}
4304
4305short HexagonInstrInfo::changeAddrMode_io_abs(short Opc) const {
4306 return Opc >= 0 ? Hexagon::changeAddrMode_io_abs(Opc) : Opc;
4307}
4308
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004309short HexagonInstrInfo::changeAddrMode_io_pi(short Opc) const {
4310 return Opc >= 0 ? Hexagon::changeAddrMode_io_pi(Opc) : Opc;
4311}
4312
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004313short HexagonInstrInfo::changeAddrMode_io_rr(short Opc) const {
4314 return Opc >= 0 ? Hexagon::changeAddrMode_io_rr(Opc) : Opc;
4315}
4316
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00004317short HexagonInstrInfo::changeAddrMode_pi_io(short Opc) const {
4318 return Opc >= 0 ? Hexagon::changeAddrMode_pi_io(Opc) : Opc;
4319}
4320
Krzysztof Parzyszek7ae3ae92017-10-05 20:01:38 +00004321short HexagonInstrInfo::changeAddrMode_rr_io(short Opc) const {
4322 return Opc >= 0 ? Hexagon::changeAddrMode_rr_io(Opc) : Opc;
4323}
4324
4325short HexagonInstrInfo::changeAddrMode_rr_ur(short Opc) const {
4326 return Opc >= 0 ? Hexagon::changeAddrMode_rr_ur(Opc) : Opc;
4327}
4328
4329short HexagonInstrInfo::changeAddrMode_ur_rr(short Opc) const {
4330 return Opc >= 0 ? Hexagon::changeAddrMode_ur_rr(Opc) : Opc;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004331}