blob: fd10fc789feed7d604910c8de54be5785e106c7f [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Hexagon implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "HexagonInstrInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000015#include "Hexagon.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000016#include "HexagonHazardRecognizer.h"
Craig Topperb25fda92012-03-17 18:46:09 +000017#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "HexagonSubtarget.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000019#include "llvm/ADT/SmallPtrSet.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000021#include "llvm/ADT/StringRef.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000022#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000023#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
31#include "llvm/CodeGen/MachineLoopInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000033#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000035#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000036#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000037#include "llvm/MC/MCInstrDesc.h"
38#include "llvm/MC/MCInstrItineraries.h"
39#include "llvm/MC/MCRegisterInfo.h"
40#include "llvm/Support/BranchProbability.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000041#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000042#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000043#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000044#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000045#include "llvm/Support/raw_ostream.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetSubtargetInfo.h"
48#include <cassert>
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000049#include <cctype>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000050#include <cstdint>
51#include <cstring>
52#include <iterator>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054using namespace llvm;
55
Chandler Carruthe96dd892014-04-21 22:55:11 +000056#define DEBUG_TYPE "hexagon-instrinfo"
57
Chandler Carruthd174b722014-04-22 02:03:14 +000058#define GET_INSTRINFO_CTOR_DTOR
59#define GET_INSTRMAP_INFO
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +000060#include "HexagonDepTimingClasses.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000061#include "HexagonGenDFAPacketizer.inc"
62#include "HexagonGenInstrInfo.inc"
Chandler Carruthd174b722014-04-22 02:03:14 +000063
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000064cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000065 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
66 "packetization boundary."));
67
68static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
69 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
70
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000071static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
72 cl::Hidden, cl::ZeroOrMore, cl::init(false),
73 cl::desc("Disable schedule adjustment for new value stores."));
74
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000075static cl::opt<bool> EnableTimingClassLatency(
76 "enable-timing-class-latency", cl::Hidden, cl::init(false),
77 cl::desc("Enable timing class latency"));
78
79static cl::opt<bool> EnableALUForwarding(
80 "enable-alu-forwarding", cl::Hidden, cl::init(true),
81 cl::desc("Enable vec alu forwarding"));
82
83static cl::opt<bool> EnableACCForwarding(
84 "enable-acc-forwarding", cl::Hidden, cl::init(true),
85 cl::desc("Enable vec acc forwarding"));
86
87static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
88 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
89
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000090static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
91 cl::init(true), cl::Hidden, cl::ZeroOrMore,
92 cl::desc("Use the DFA based hazard recognizer."));
93
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094///
95/// Constants for Hexagon instructions.
96///
97const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000098const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000100const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000101const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000102const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000104const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000106const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000107const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000108const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000110const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000112const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000113const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000114const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +0000115const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
116const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
117const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
118const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000119
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000120// Pin the vtable to this file.
121void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000122
123HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000124 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000125 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000126
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000127static bool isIntRegForSubInst(unsigned Reg) {
128 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
129 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000130}
131
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000132static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000133 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
134 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000135}
136
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000137/// Calculate number of instructions excluding the debug instructions.
138static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
139 MachineBasicBlock::const_instr_iterator MIE) {
140 unsigned Count = 0;
141 for (; MIB != MIE; ++MIB) {
142 if (!MIB->isDebugValue())
143 ++Count;
144 }
145 return Count;
146}
147
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000148/// Find the hardware loop instruction used to set-up the specified loop.
149/// On Hexagon, we have two instructions used to set-up the hardware loop
150/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
151/// to indicate the end of a loop.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000152static MachineInstr *findLoopInstr(MachineBasicBlock *BB, unsigned EndLoopOp,
153 MachineBasicBlock *TargetBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000154 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000155 unsigned LOOPi;
156 unsigned LOOPr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000157 if (EndLoopOp == Hexagon::ENDLOOP0) {
158 LOOPi = Hexagon::J2_loop0i;
159 LOOPr = Hexagon::J2_loop0r;
160 } else { // EndLoopOp == Hexagon::EndLOOP1
161 LOOPi = Hexagon::J2_loop1i;
162 LOOPr = Hexagon::J2_loop1r;
163 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000164
Brendon Cahoondf43e682015-05-08 16:16:29 +0000165 // The loop set-up instruction will be in a predecessor block
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000166 for (MachineBasicBlock *PB : BB->predecessors()) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000167 // If this has been visited, already skip it.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000168 if (!Visited.insert(PB).second)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000169 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000170 if (PB == BB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000171 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000172 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
173 unsigned Opc = I->getOpcode();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000174 if (Opc == LOOPi || Opc == LOOPr)
175 return &*I;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000176 // We've reached a different loop, which means the loop01 has been
177 // removed.
178 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000179 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000180 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000181 // Check the predecessors for the LOOP instruction.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000182 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
183 return Loop;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000184 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000185 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000186}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000187
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000188/// Gather register def/uses from MI.
189/// This treats possible (predicated) defs as actually happening ones
190/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000191static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000192 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
193 Defs.clear();
194 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000195
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000196 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
197 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000198
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000199 if (!MO.isReg())
200 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000201
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000202 unsigned Reg = MO.getReg();
203 if (!Reg)
204 continue;
205
206 if (MO.isUse())
207 Uses.push_back(MO.getReg());
208
209 if (MO.isDef())
210 Defs.push_back(MO.getReg());
211 }
212}
213
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000214// Position dependent, so check twice for swap.
215static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
216 switch (Ga) {
217 case HexagonII::HSIG_None:
218 default:
219 return false;
220 case HexagonII::HSIG_L1:
221 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
222 case HexagonII::HSIG_L2:
223 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
224 Gb == HexagonII::HSIG_A);
225 case HexagonII::HSIG_S1:
226 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
227 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
228 case HexagonII::HSIG_S2:
229 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
230 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
231 Gb == HexagonII::HSIG_A);
232 case HexagonII::HSIG_A:
233 return (Gb == HexagonII::HSIG_A);
234 case HexagonII::HSIG_Compound:
235 return (Gb == HexagonII::HSIG_Compound);
236 }
237 return false;
238}
239
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000240/// isLoadFromStackSlot - If the specified machine instruction is a direct
241/// load from a stack slot, return the virtual or physical register number of
242/// the destination along with the FrameIndex of the loaded stack slot. If
243/// not, return 0. This predicate must return 0 if the instruction has
244/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000245unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000246 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000247 switch (MI.getOpcode()) {
248 default:
249 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000250 case Hexagon::L2_loadri_io:
251 case Hexagon::L2_loadrd_io:
252 case Hexagon::V6_vL32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +0000253 case Hexagon::V6_vL32b_nt_ai:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000254 case Hexagon::V6_vL32b_ai_128B:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +0000255 case Hexagon::V6_vL32b_nt_ai_128B:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000256 case Hexagon::V6_vL32Ub_ai:
257 case Hexagon::V6_vL32Ub_ai_128B:
258 case Hexagon::LDriw_pred:
259 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000260 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000261 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +0000262 case Hexagon::PS_vloadrw_nt_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000263 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +0000264 case Hexagon::PS_vloadrw_ai_128B:
265 case Hexagon::PS_vloadrw_nt_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000266 const MachineOperand OpFI = MI.getOperand(1);
267 if (!OpFI.isFI())
268 return 0;
269 const MachineOperand OpOff = MI.getOperand(2);
270 if (!OpOff.isImm() || OpOff.getImm() != 0)
271 return 0;
272 FrameIndex = OpFI.getIndex();
273 return MI.getOperand(0).getReg();
274 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000275
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000276 case Hexagon::L2_ploadrit_io:
277 case Hexagon::L2_ploadrif_io:
278 case Hexagon::L2_ploadrdt_io:
279 case Hexagon::L2_ploadrdf_io: {
280 const MachineOperand OpFI = MI.getOperand(2);
281 if (!OpFI.isFI())
282 return 0;
283 const MachineOperand OpOff = MI.getOperand(3);
284 if (!OpOff.isImm() || OpOff.getImm() != 0)
285 return 0;
286 FrameIndex = OpFI.getIndex();
287 return MI.getOperand(0).getReg();
288 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000289 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000290
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000291 return 0;
292}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000293
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000294/// isStoreToStackSlot - If the specified machine instruction is a direct
295/// store to a stack slot, return the virtual or physical register number of
296/// the source reg along with the FrameIndex of the loaded stack slot. If
297/// not, return 0. This predicate must return 0 if the instruction has
298/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000299unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000300 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000301 switch (MI.getOpcode()) {
302 default:
303 break;
304 case Hexagon::S2_storerb_io:
305 case Hexagon::S2_storerh_io:
306 case Hexagon::S2_storeri_io:
307 case Hexagon::S2_storerd_io:
308 case Hexagon::V6_vS32b_ai:
309 case Hexagon::V6_vS32b_ai_128B:
310 case Hexagon::V6_vS32Ub_ai:
311 case Hexagon::V6_vS32Ub_ai_128B:
312 case Hexagon::STriw_pred:
313 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000314 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000315 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000316 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000317 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000318 const MachineOperand &OpFI = MI.getOperand(0);
319 if (!OpFI.isFI())
320 return 0;
321 const MachineOperand &OpOff = MI.getOperand(1);
322 if (!OpOff.isImm() || OpOff.getImm() != 0)
323 return 0;
324 FrameIndex = OpFI.getIndex();
325 return MI.getOperand(2).getReg();
326 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000327
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000328 case Hexagon::S2_pstorerbt_io:
329 case Hexagon::S2_pstorerbf_io:
330 case Hexagon::S2_pstorerht_io:
331 case Hexagon::S2_pstorerhf_io:
332 case Hexagon::S2_pstorerit_io:
333 case Hexagon::S2_pstorerif_io:
334 case Hexagon::S2_pstorerdt_io:
335 case Hexagon::S2_pstorerdf_io: {
336 const MachineOperand &OpFI = MI.getOperand(1);
337 if (!OpFI.isFI())
338 return 0;
339 const MachineOperand &OpOff = MI.getOperand(2);
340 if (!OpOff.isImm() || OpOff.getImm() != 0)
341 return 0;
342 FrameIndex = OpFI.getIndex();
343 return MI.getOperand(3).getReg();
344 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000345 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000346
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000347 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000348}
349
Brendon Cahoondf43e682015-05-08 16:16:29 +0000350/// This function can analyze one/two way branching only and should (mostly) be
351/// called by target independent side.
352/// First entry is always the opcode of the branching instruction, except when
353/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
354/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
355/// e.g. Jump_c p will have
356/// Cond[0] = Jump_c
357/// Cond[1] = p
358/// HW-loop ENDLOOP:
359/// Cond[0] = ENDLOOP
360/// Cond[1] = MBB
361/// New value jump:
362/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
363/// Cond[1] = R
364/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000365///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000366bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000367 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000368 MachineBasicBlock *&FBB,
369 SmallVectorImpl<MachineOperand> &Cond,
370 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000371 TBB = nullptr;
372 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000373 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000374
375 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000376 MachineBasicBlock::instr_iterator I = MBB.instr_end();
377 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000378 return false;
379
380 // A basic block may looks like this:
381 //
382 // [ insn
383 // EH_LABEL
384 // insn
385 // insn
386 // insn
387 // EH_LABEL
388 // insn ]
389 //
390 // It has two succs but does not have a terminator
391 // Don't know how to handle it.
392 do {
393 --I;
394 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000395 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000397 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000399 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000400 --I;
401
402 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000403 if (I == MBB.instr_begin())
404 return false;
405 --I;
406 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000407
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000408 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
409 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000410 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000411 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000412 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000413 DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000414 I->eraseFromParent();
415 I = MBB.instr_end();
416 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417 return false;
418 --I;
419 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000420 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000421 return false;
422
423 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000424 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000425 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000426 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000427 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000428 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000429 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000430 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000431 else
432 // This is a third branch.
433 return true;
434 }
435 if (I == MBB.instr_begin())
436 break;
437 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000438 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000439
440 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000441 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
442 // If the branch target is not a basic block, it could be a tail call.
443 // (It is, if the target is a function.)
444 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
445 return true;
446 if (SecLastOpcode == Hexagon::J2_jump &&
447 !SecondLastInst->getOperand(0).isMBB())
448 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000449
450 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000451 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000452
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000453 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
454 return true;
455
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000456 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000457 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000458 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 TBB = LastInst->getOperand(0).getMBB();
460 return false;
461 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000462 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000463 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000464 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465 Cond.push_back(LastInst->getOperand(0));
466 return false;
467 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000468 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000469 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000470 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000471 Cond.push_back(LastInst->getOperand(0));
472 return false;
473 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000474 // Only supporting rr/ri versions of new-value jumps.
475 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
476 TBB = LastInst->getOperand(2).getMBB();
477 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
478 Cond.push_back(LastInst->getOperand(0));
479 Cond.push_back(LastInst->getOperand(1));
480 return false;
481 }
482 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
483 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000484 // Otherwise, don't know what this is.
485 return true;
486 }
487
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000488 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000489 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000490 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000491 if (!SecondLastInst->getOperand(1).isMBB())
492 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000493 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000494 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 Cond.push_back(SecondLastInst->getOperand(0));
496 FBB = LastInst->getOperand(0).getMBB();
497 return false;
498 }
499
Brendon Cahoondf43e682015-05-08 16:16:29 +0000500 // Only supporting rr/ri versions of new-value jumps.
501 if (SecLastOpcodeHasNVJump &&
502 (SecondLastInst->getNumExplicitOperands() == 3) &&
503 (LastOpcode == Hexagon::J2_jump)) {
504 TBB = SecondLastInst->getOperand(2).getMBB();
505 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
506 Cond.push_back(SecondLastInst->getOperand(0));
507 Cond.push_back(SecondLastInst->getOperand(1));
508 FBB = LastInst->getOperand(0).getMBB();
509 return false;
510 }
511
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000512 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
513 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000514 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000515 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000516 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000517 if (AllowModify)
518 I->eraseFromParent();
519 return false;
520 }
521
Brendon Cahoondf43e682015-05-08 16:16:29 +0000522 // If the block ends with an ENDLOOP, and J2_jump, handle it.
523 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000524 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000525 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000526 Cond.push_back(SecondLastInst->getOperand(0));
527 FBB = LastInst->getOperand(0).getMBB();
528 return false;
529 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000530 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
531 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000532 // Otherwise, can't handle this.
533 return true;
534}
535
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000536unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000537 int *BytesRemoved) const {
538 assert(!BytesRemoved && "code size not handled");
539
Brendon Cahoondf43e682015-05-08 16:16:29 +0000540 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000541 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000542 unsigned Count = 0;
543 while (I != MBB.begin()) {
544 --I;
545 if (I->isDebugValue())
546 continue;
547 // Only removing branches from end of MBB.
548 if (!I->isBranch())
549 return Count;
550 if (Count && (I->getOpcode() == Hexagon::J2_jump))
551 llvm_unreachable("Malformed basic block: unconditional branch not last");
552 MBB.erase(&MBB.back());
553 I = MBB.end();
554 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000555 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000556 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000557}
558
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000559unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000560 MachineBasicBlock *TBB,
561 MachineBasicBlock *FBB,
562 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000563 const DebugLoc &DL,
564 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000565 unsigned BOpc = Hexagon::J2_jump;
566 unsigned BccOpc = Hexagon::J2_jumpt;
567 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000568 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000569 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000570
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000571 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000572 // If we want to reverse the branch an odd number of times, we want
573 // J2_jumpf.
574 if (!Cond.empty() && Cond[0].isImm())
575 BccOpc = Cond[0].getImm();
576
577 if (!FBB) {
578 if (Cond.empty()) {
579 // Due to a bug in TailMerging/CFG Optimization, we need to add a
580 // special case handling of a predicated jump followed by an
581 // unconditional jump. If not, Tail Merging and CFG Optimization go
582 // into an infinite loop.
583 MachineBasicBlock *NewTBB, *NewFBB;
584 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000585 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000586 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000587 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
588 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000589 reverseBranchCondition(Cond);
590 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000591 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000592 }
593 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
594 } else if (isEndLoopN(Cond[0].getImm())) {
595 int EndLoopOp = Cond[0].getImm();
596 assert(Cond[1].isMBB());
597 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
598 // Check for it, and change the BB target if needed.
599 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000600 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
601 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000602 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
603 Loop->getOperand(0).setMBB(TBB);
604 // Add the ENDLOOP after the finding the LOOP0.
605 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
606 } else if (isNewValueJump(Cond[0].getImm())) {
607 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
608 // New value jump
609 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
610 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
611 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
612 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
613 if (Cond[2].isReg()) {
614 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
615 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
616 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
617 } else if(Cond[2].isImm()) {
618 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
619 addImm(Cond[2].getImm()).addMBB(TBB);
620 } else
621 llvm_unreachable("Invalid condition for branching");
622 } else {
623 assert((Cond.size() == 2) && "Malformed cond vector");
624 const MachineOperand &RO = Cond[1];
625 unsigned Flags = getUndefRegState(RO.isUndef());
626 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
627 }
628 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000629 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000630 assert((!Cond.empty()) &&
631 "Cond. cannot be empty when multiple branchings are required");
632 assert((!isNewValueJump(Cond[0].getImm())) &&
633 "NV-jump cannot be inserted with another branch");
634 // Special case for hardware loops. The condition is a basic block.
635 if (isEndLoopN(Cond[0].getImm())) {
636 int EndLoopOp = Cond[0].getImm();
637 assert(Cond[1].isMBB());
638 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
639 // Check for it, and change the BB target if needed.
640 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000641 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
642 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000643 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
644 Loop->getOperand(0).setMBB(TBB);
645 // Add the ENDLOOP after the finding the LOOP0.
646 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
647 } else {
648 const MachineOperand &RO = Cond[1];
649 unsigned Flags = getUndefRegState(RO.isUndef());
650 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000651 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000652 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000653
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000654 return 2;
655}
656
Brendon Cahoon254f8892016-07-29 16:44:44 +0000657/// Analyze the loop code to find the loop induction variable and compare used
658/// to compute the number of iterations. Currently, we analyze loop that are
659/// controlled using hardware loops. In this case, the induction variable
660/// instruction is null. For all other cases, this function returns true, which
661/// means we're unable to analyze it.
662bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
663 MachineInstr *&IndVarInst,
664 MachineInstr *&CmpInst) const {
665
666 MachineBasicBlock *LoopEnd = L.getBottomBlock();
667 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
668 // We really "analyze" only hardware loops right now.
669 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
670 IndVarInst = nullptr;
671 CmpInst = &*I;
672 return false;
673 }
674 return true;
675}
676
677/// Generate code to reduce the loop iteration by one and check if the loop is
678/// finished. Return the value/register of the new loop count. this function
679/// assumes the nth iteration is peeled first.
680unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000681 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000682 SmallVectorImpl<MachineOperand> &Cond,
683 SmallVectorImpl<MachineInstr *> &PrevInsts,
684 unsigned Iter, unsigned MaxIter) const {
685 // We expect a hardware loop currently. This means that IndVar is set
686 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000687 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000688 && "Expecting a hardware loop");
689 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000690 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000691 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000692 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(),
693 Cmp.getOperand(0).getMBB(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000694 if (!Loop)
695 return 0;
696 // If the loop trip count is a compile-time value, then just change the
697 // value.
698 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
699 Loop->getOpcode() == Hexagon::J2_loop1i) {
700 int64_t Offset = Loop->getOperand(1).getImm();
701 if (Offset <= 1)
702 Loop->eraseFromParent();
703 else
704 Loop->getOperand(1).setImm(Offset - 1);
705 return Offset - 1;
706 }
707 // The loop trip count is a run-time value. We generate code to subtract
708 // one from the trip count, and update the loop instruction.
709 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
710 unsigned LoopCount = Loop->getOperand(1).getReg();
711 // Check if we're done with the loop.
712 unsigned LoopEnd = createVR(MF, MVT::i1);
713 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
714 addReg(LoopCount).addImm(1);
715 unsigned NewLoopCount = createVR(MF, MVT::i32);
716 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
717 addReg(LoopCount).addImm(-1);
718 // Update the previously generated instructions with the new loop counter.
719 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
720 E = PrevInsts.end(); I != E; ++I)
721 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
722 PrevInsts.clear();
723 PrevInsts.push_back(NewCmp);
724 PrevInsts.push_back(NewAdd);
725 // Insert the new loop instruction if this is the last time the loop is
726 // decremented.
727 if (Iter == MaxIter)
728 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
729 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
730 // Delete the old loop instruction.
731 if (Iter == 0)
732 Loop->eraseFromParent();
733 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
734 Cond.push_back(NewCmp->getOperand(0));
735 return NewLoopCount;
736}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000737
738bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
739 unsigned NumCycles, unsigned ExtraPredCycles,
740 BranchProbability Probability) const {
741 return nonDbgBBSize(&MBB) <= 3;
742}
743
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000744bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
745 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
746 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
747 const {
748 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
749}
750
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000751bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
752 unsigned NumInstrs, BranchProbability Probability) const {
753 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000754}
755
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000756void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000757 MachineBasicBlock::iterator I,
758 const DebugLoc &DL, unsigned DestReg,
759 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000760 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000761 unsigned KillFlag = getKillRegState(KillSrc);
762
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000763 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000764 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000765 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000766 return;
767 }
768 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000769 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
770 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000771 return;
772 }
773 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
774 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000775 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
776 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000777 return;
778 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000779 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000780 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000781 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
782 .addReg(SrcReg, KillFlag);
783 return;
784 }
785 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
786 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
787 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
788 .addReg(SrcReg, KillFlag);
789 return;
790 }
791 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
792 Hexagon::IntRegsRegClass.contains(SrcReg)) {
793 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
794 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000795 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000796 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000797 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
798 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000799 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
800 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000801 return;
802 }
803 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
804 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000805 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
806 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000807 return;
808 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000809 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
810 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000811 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
812 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000813 return;
814 }
815 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
816 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000817 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000818 return;
819 }
820 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000821 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
822 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000823 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000824 .addReg(HiSrc, KillFlag)
825 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000826 return;
827 }
828 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000829 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
830 .addReg(SrcReg)
831 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000832 return;
833 }
834 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000835 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000836 llvm_unreachable("Unimplemented pred to vec");
837 return;
838 }
839 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
840 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
841 llvm_unreachable("Unimplemented vec to pred");
842 return;
843 }
844 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000845 unsigned HiDst = HRI.getSubReg(DestReg, Hexagon::vsub_hi);
846 unsigned LoDst = HRI.getSubReg(DestReg, Hexagon::vsub_lo);
847 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
848 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
849 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), HiDst)
850 .addReg(HiSrc, KillFlag);
851 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), LoDst)
852 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000853 return;
854 }
Sirish Pande30804c22012-02-15 18:52:27 +0000855
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000856#ifndef NDEBUG
857 // Show the invalid registers to ease debugging.
858 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
859 << ": " << PrintReg(DestReg, &HRI)
860 << " = " << PrintReg(SrcReg, &HRI) << '\n';
861#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000862 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000863}
864
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000865void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
866 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
867 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000868 DebugLoc DL = MBB.findDebugLoc(I);
869 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000870 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000872 unsigned KillFlag = getKillRegState(isKill);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000873 bool HasAlloca = MFI.hasVarSizedObjects();
874 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
875 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000876
Alex Lorenze40c8a22015-08-11 23:09:45 +0000877 MachineMemOperand *MMO = MF.getMachineMemOperand(
878 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
879 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000880
Craig Topperc7242e02012-04-20 07:30:17 +0000881 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000882 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000883 .addFrameIndex(FI).addImm(0)
884 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000885 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000886 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000887 .addFrameIndex(FI).addImm(0)
888 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000889 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000890 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000891 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000892 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000893 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
894 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
895 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000896 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
897 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000898 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai_128B))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000899 .addFrameIndex(FI).addImm(0)
900 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
901 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000902 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000903 .addFrameIndex(FI).addImm(0)
904 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
905 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000906 // If there are variable-sized objects, spills will not be aligned.
907 if (HasAlloca)
908 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000909 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
910 : Hexagon::V6_vS32b_ai_128B;
911 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000912 .addFrameIndex(FI).addImm(0)
913 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
914 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000915 // If there are variable-sized objects, spills will not be aligned.
916 if (HasAlloca)
917 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000918 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
919 : Hexagon::V6_vS32b_ai;
920 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000921 .addFrameIndex(FI).addImm(0)
922 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
923 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000924 // If there are variable-sized objects, spills will not be aligned.
925 if (HasAlloca)
926 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000927 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
928 : Hexagon::PS_vstorerw_ai;
929 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000930 .addFrameIndex(FI).addImm(0)
931 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
932 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000933 // If there are variable-sized objects, spills will not be aligned.
934 if (HasAlloca)
935 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000936 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
937 : Hexagon::PS_vstorerw_ai_128B;
938 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000939 .addFrameIndex(FI).addImm(0)
940 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000941 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000942 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000943 }
944}
945
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000946void HexagonInstrInfo::loadRegFromStackSlot(
947 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
948 int FI, const TargetRegisterClass *RC,
949 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950 DebugLoc DL = MBB.findDebugLoc(I);
951 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000952 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000953 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000954 bool HasAlloca = MFI.hasVarSizedObjects();
955 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
956 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000957
Alex Lorenze40c8a22015-08-11 23:09:45 +0000958 MachineMemOperand *MMO = MF.getMachineMemOperand(
959 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
960 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000961
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000962 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000963 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000964 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000965 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000966 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000967 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000968 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000969 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000970 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
971 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
972 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
973 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000974 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000975 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000976 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
977 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000978 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000979 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
980 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000981 // If there are variable-sized objects, spills will not be aligned.
982 if (HasAlloca)
983 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000984 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
985 : Hexagon::PS_vloadrw_ai_128B;
986 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000987 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
988 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000989 // If there are variable-sized objects, spills will not be aligned.
990 if (HasAlloca)
991 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000992 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
993 : Hexagon::V6_vL32b_ai_128B;
994 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000995 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
996 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000997 // If there are variable-sized objects, spills will not be aligned.
998 if (HasAlloca)
999 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001000 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
1001 : Hexagon::V6_vL32b_ai;
1002 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +00001003 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1004 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +00001005 // If there are variable-sized objects, spills will not be aligned.
1006 if (HasAlloca)
1007 Align = HFI.getStackAlignment();
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001008 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
1009 : Hexagon::PS_vloadrw_ai;
1010 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +00001011 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001012 } else {
Craig Toppere55c5562012-02-07 02:50:20 +00001013 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001014 }
1015}
1016
Ron Lieberman88159e52016-09-02 22:56:24 +00001017static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
1018 const MachineBasicBlock &B = *MI.getParent();
1019 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +00001020 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +00001021 for (auto I = B.rbegin(); I != E; ++I)
1022 Regs.stepBackward(*I);
1023}
1024
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001025/// expandPostRAPseudo - This function is called for all pseudo instructions
1026/// that remain after register allocation. Many pseudo instructions are
1027/// created to help register allocation. This is the place to convert them
1028/// into real instructions. The target can edit MI in place, or it can insert
1029/// new instructions and erase MI. The function should return true if
1030/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001031bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001032 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001033 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1034 MachineBasicBlock &MBB = *MI.getParent();
1035 DebugLoc DL = MI.getDebugLoc();
1036 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001037 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001038
1039 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001040 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001041 MachineOperand &MD = MI.getOperand(0);
1042 MachineOperand &MS = MI.getOperand(1);
1043 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001044 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1045 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001046 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001047 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001048 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001049 return true;
1050 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001051 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001052 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001053 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001054 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001055 MBB.erase(MI);
1056 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001057 case Hexagon::V6_vassignp_128B:
1058 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001059 unsigned SrcReg = MI.getOperand(1).getReg();
1060 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001061 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1062 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001063 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1064 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001065 MBB.erase(MI);
1066 return true;
1067 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001068 case Hexagon::V6_lo_128B:
1069 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001070 unsigned SrcReg = MI.getOperand(1).getReg();
1071 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001072 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001073 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001074 MBB.erase(MI);
1075 MRI.clearKillFlags(SrcSubLo);
1076 return true;
1077 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001078 case Hexagon::V6_hi_128B:
1079 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001080 unsigned SrcReg = MI.getOperand(1).getReg();
1081 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001082 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001083 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001084 MBB.erase(MI);
1085 MRI.clearKillFlags(SrcSubHi);
1086 return true;
1087 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001088 case Hexagon::PS_vstorerw_ai:
1089 case Hexagon::PS_vstorerwu_ai:
1090 case Hexagon::PS_vstorerw_ai_128B:
1091 case Hexagon::PS_vstorerwu_ai_128B: {
1092 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1093 Opc == Hexagon::PS_vstorerwu_ai_128B);
1094 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1095 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001096 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001097 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1098 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001099 unsigned NewOpc;
1100 if (Aligned)
1101 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1102 : Hexagon::V6_vS32b_ai;
1103 else
1104 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1105 : Hexagon::V6_vS32Ub_ai;
1106
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001107 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001108 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001109 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001110 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001111 .addImm(MI.getOperand(1).getImm())
1112 .addReg(SrcSubLo)
1113 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001114 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001115 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001116 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001117 // The Vectors are indexed in multiples of vector size.
1118 .addImm(MI.getOperand(1).getImm() + Offset)
1119 .addReg(SrcSubHi)
1120 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001121 MBB.erase(MI);
1122 return true;
1123 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001124 case Hexagon::PS_vloadrw_ai:
1125 case Hexagon::PS_vloadrwu_ai:
1126 case Hexagon::PS_vloadrw_ai_128B:
1127 case Hexagon::PS_vloadrwu_ai_128B: {
1128 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1129 Opc == Hexagon::PS_vloadrwu_ai_128B);
1130 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1131 Opc == Hexagon::PS_vloadrw_ai_128B);
1132 unsigned NewOpc;
1133 if (Aligned)
1134 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1135 : Hexagon::V6_vL32b_ai;
1136 else
1137 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1138 : Hexagon::V6_vL32Ub_ai;
1139
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001140 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001141 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Diana Picus116bbab2017-01-13 09:58:52 +00001142 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1143 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Krzysztof Parzyszek4be9d922017-05-03 15:26:13 +00001144 .add(MI.getOperand(1))
1145 .addImm(MI.getOperand(2).getImm())
1146 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001147 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001148 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1149 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001150 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001151 .addImm(MI.getOperand(2).getImm() + Offset)
1152 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001153 MBB.erase(MI);
1154 return true;
1155 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001156 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001157 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001158 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1159 .addReg(Reg, RegState::Undef)
1160 .addReg(Reg, RegState::Undef);
1161 MBB.erase(MI);
1162 return true;
1163 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001164 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001165 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001166 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1167 .addReg(Reg, RegState::Undef)
1168 .addReg(Reg, RegState::Undef);
1169 MBB.erase(MI);
1170 return true;
1171 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001172 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001173 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001174 unsigned DstReg = MI.getOperand(0).getReg();
1175 unsigned Src1Reg = MI.getOperand(1).getReg();
1176 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001177 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1178 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1179 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1180 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001181 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001182 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001183 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001184 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001185 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001186 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001187 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001188 .addReg(Src2SubLo);
1189 MBB.erase(MI);
1190 MRI.clearKillFlags(Src1SubHi);
1191 MRI.clearKillFlags(Src1SubLo);
1192 MRI.clearKillFlags(Src2SubHi);
1193 MRI.clearKillFlags(Src2SubLo);
1194 return true;
1195 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001196 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001197 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001198 unsigned DstReg = MI.getOperand(0).getReg();
1199 unsigned Src1Reg = MI.getOperand(1).getReg();
1200 unsigned Src2Reg = MI.getOperand(2).getReg();
1201 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001202 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1203 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1204 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1205 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1206 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1207 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001208 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001209 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001210 .addReg(Src1SubHi)
1211 .addReg(Src2SubHi)
1212 .addReg(Src3SubHi);
1213 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001214 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001215 .addReg(Src1SubLo)
1216 .addReg(Src2SubLo)
1217 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001218 MBB.erase(MI);
1219 MRI.clearKillFlags(Src1SubHi);
1220 MRI.clearKillFlags(Src1SubLo);
1221 MRI.clearKillFlags(Src2SubHi);
1222 MRI.clearKillFlags(Src2SubLo);
1223 MRI.clearKillFlags(Src3SubHi);
1224 MRI.clearKillFlags(Src3SubLo);
1225 return true;
1226 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001227 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001228 const MachineOperand &Op0 = MI.getOperand(0);
1229 const MachineOperand &Op1 = MI.getOperand(1);
1230 const MachineOperand &Op2 = MI.getOperand(2);
1231 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001232 unsigned Rd = Op0.getReg();
1233 unsigned Pu = Op1.getReg();
1234 unsigned Rs = Op2.getReg();
1235 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001236 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001237 unsigned K1 = getKillRegState(Op1.isKill());
1238 unsigned K2 = getKillRegState(Op2.isKill());
1239 unsigned K3 = getKillRegState(Op3.isKill());
1240 if (Rd != Rs)
1241 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1242 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1243 .addReg(Rs, K2);
1244 if (Rd != Rt)
1245 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1246 .addReg(Pu, K1)
1247 .addReg(Rt, K3);
1248 MBB.erase(MI);
1249 return true;
1250 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001251 case Hexagon::PS_vselect:
1252 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001253 const MachineOperand &Op0 = MI.getOperand(0);
1254 const MachineOperand &Op1 = MI.getOperand(1);
1255 const MachineOperand &Op2 = MI.getOperand(2);
1256 const MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001257 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001258 getLiveRegsAt(LiveAtMI, MI);
1259 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001260 unsigned PReg = Op1.getReg();
1261 assert(Op1.getSubReg() == 0);
1262 unsigned PState = getRegState(Op1);
1263
Ron Lieberman88159e52016-09-02 22:56:24 +00001264 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001265 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1266 : PState;
Ron Lieberman88159e52016-09-02 22:56:24 +00001267 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001268 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001269 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001270 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001271 if (IsDestLive)
1272 T.addReg(Op0.getReg(), RegState::Implicit);
1273 IsDestLive = true;
1274 }
1275 if (Op0.getReg() != Op3.getReg()) {
1276 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001277 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001278 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001279 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001280 if (IsDestLive)
1281 T.addReg(Op0.getReg(), RegState::Implicit);
1282 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001283 MBB.erase(MI);
1284 return true;
1285 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001286 case Hexagon::PS_wselect:
1287 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001288 MachineOperand &Op0 = MI.getOperand(0);
1289 MachineOperand &Op1 = MI.getOperand(1);
1290 MachineOperand &Op2 = MI.getOperand(2);
1291 MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001292 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001293 getLiveRegsAt(LiveAtMI, MI);
1294 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001295 unsigned PReg = Op1.getReg();
1296 assert(Op1.getSubReg() == 0);
1297 unsigned PState = getRegState(Op1);
Ron Lieberman88159e52016-09-02 22:56:24 +00001298
1299 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001300 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1301 : PState;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001302 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1303 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001304 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001305 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001306 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001307 .add(Op1)
1308 .addReg(SrcHi)
1309 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001310 if (IsDestLive)
1311 T.addReg(Op0.getReg(), RegState::Implicit);
1312 IsDestLive = true;
1313 }
1314 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001315 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1316 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001317 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001318 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001319 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001320 .addReg(SrcHi)
1321 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001322 if (IsDestLive)
1323 T.addReg(Op0.getReg(), RegState::Implicit);
1324 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001325 MBB.erase(MI);
1326 return true;
1327 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001328 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001329 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001330 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001331 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001332 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001333 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001334 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001335 case Hexagon::PS_jmprett:
1336 MI.setDesc(get(Hexagon::J2_jumprt));
1337 return true;
1338 case Hexagon::PS_jmpretf:
1339 MI.setDesc(get(Hexagon::J2_jumprf));
1340 return true;
1341 case Hexagon::PS_jmprettnewpt:
1342 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1343 return true;
1344 case Hexagon::PS_jmpretfnewpt:
1345 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1346 return true;
1347 case Hexagon::PS_jmprettnew:
1348 MI.setDesc(get(Hexagon::J2_jumprtnew));
1349 return true;
1350 case Hexagon::PS_jmpretfnew:
1351 MI.setDesc(get(Hexagon::J2_jumprfnew));
1352 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001353 }
1354
1355 return false;
1356}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001357
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001358// We indicate that we want to reverse the branch by
1359// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001360bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001361 SmallVectorImpl<MachineOperand> &Cond) const {
1362 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001363 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001364 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1365 unsigned opcode = Cond[0].getImm();
1366 //unsigned temp;
1367 assert(get(opcode).isBranch() && "Should be a branching condition.");
1368 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001369 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001370 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1371 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001372 return false;
1373}
1374
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001375void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1376 MachineBasicBlock::iterator MI) const {
1377 DebugLoc DL;
1378 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1379}
1380
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001381bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1382 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001383}
1384
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001385// Returns true if an instruction is predicated irrespective of the predicate
1386// sense. For example, all of the following will return true.
1387// if (p0) R1 = add(R2, R3)
1388// if (!p0) R1 = add(R2, R3)
1389// if (p0.new) R1 = add(R2, R3)
1390// if (!p0.new) R1 = add(R2, R3)
1391// Note: New-value stores are not included here as in the current
1392// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001393bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1394 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001395 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001396}
1397
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001398bool HexagonInstrInfo::PredicateInstruction(
1399 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001400 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1401 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001402 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001403 return false;
1404 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001405 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001406 assert (isPredicable(MI) && "Expected predicable instruction");
1407 bool invertJump = predOpcodeHasNot(Cond);
1408
1409 // We have to predicate MI "in place", i.e. after this function returns,
1410 // MI will need to be transformed into a predicated form. To avoid com-
1411 // plicated manipulations with the operands (handling tied operands,
1412 // etc.), build a new temporary instruction, then overwrite MI with it.
1413
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001414 MachineBasicBlock &B = *MI.getParent();
1415 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001416 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1417 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001418 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001419 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001420 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001421 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1422 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001423 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001424 NOp++;
1425 }
1426
1427 unsigned PredReg, PredRegPos, PredRegFlags;
1428 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1429 (void)GotPredReg;
1430 assert(GotPredReg);
1431 T.addReg(PredReg, PredRegFlags);
1432 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001433 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001434
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001435 MI.setDesc(get(PredOpc));
1436 while (unsigned n = MI.getNumOperands())
1437 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001438 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001439 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001440
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001441 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001442 B.erase(TI);
1443
1444 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1445 MRI.clearKillFlags(PredReg);
1446 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001447}
1448
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001449bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1450 ArrayRef<MachineOperand> Pred2) const {
1451 // TODO: Fix this
1452 return false;
1453}
1454
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001455bool HexagonInstrInfo::DefinesPredicate(
1456 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001457 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001458 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1459 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001460 if (MO.isReg()) {
1461 if (!MO.isDef())
1462 continue;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001463 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1464 if (RC == &Hexagon::PredRegsRegClass) {
1465 Pred.push_back(MO);
1466 return true;
1467 }
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001468 continue;
1469 } else if (MO.isRegMask()) {
1470 for (unsigned PR : Hexagon::PredRegsRegClass) {
1471 if (!MI.modifiesRegister(PR, &HRI))
1472 continue;
1473 Pred.push_back(MO);
1474 return true;
1475 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001476 }
1477 }
1478 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001479}
Andrew Trickd06df962012-02-01 22:13:57 +00001480
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001481bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001482 if (!MI.getDesc().isPredicable())
1483 return false;
1484
1485 if (MI.isCall() || isTailCall(MI)) {
1486 const MachineFunction &MF = *MI.getParent()->getParent();
1487 if (!MF.getSubtarget<HexagonSubtarget>().usePredicatedCalls())
1488 return false;
1489 }
1490 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001491}
1492
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001493bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1494 const MachineBasicBlock *MBB,
1495 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001496 // Debug info is never a scheduling boundary. It's necessary to be explicit
1497 // due to the special treatment of IT instructions below, otherwise a
1498 // dbg_value followed by an IT will result in the IT instruction being
1499 // considered a scheduling hazard, which is wrong. It should be the actual
1500 // instruction preceding the dbg_value instruction(s), just like it is
1501 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001502 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001503 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001504
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001505 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001506 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001507 // Don't mess around with no return calls.
1508 if (doesNotReturn(MI))
1509 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001510 // If any of the block's successors is a landing pad, this could be a
1511 // throwing call.
1512 for (auto I : MBB->successors())
1513 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001514 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001515 }
1516
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001517 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001518 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001519 return true;
1520
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001521 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1522 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001523
1524 return false;
1525}
1526
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001527/// Measure the specified inline asm to determine an approximation of its
1528/// length.
1529/// Comments (which run till the next SeparatorString or newline) do not
1530/// count as an instruction.
1531/// Any other non-whitespace text is considered an instruction, with
1532/// multiple instructions separated by SeparatorString or newlines.
1533/// Variable-length instructions are not handled here; this function
1534/// may be overloaded in the target code to do that.
1535/// Hexagon counts the number of ##'s and adjust for that many
1536/// constant exenders.
1537unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1538 const MCAsmInfo &MAI) const {
1539 StringRef AStr(Str);
1540 // Count the number of instructions in the asm.
1541 bool atInsnStart = true;
1542 unsigned Length = 0;
1543 for (; *Str; ++Str) {
1544 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1545 strlen(MAI.getSeparatorString())) == 0)
1546 atInsnStart = true;
1547 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1548 Length += MAI.getMaxInstLength();
1549 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001550 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001551 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1552 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001553 atInsnStart = false;
1554 }
1555
1556 // Add to size number of constant extenders seen * 4.
1557 StringRef Occ("##");
1558 Length += AStr.count(Occ)*4;
1559 return Length;
1560}
1561
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001562ScheduleHazardRecognizer*
1563HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1564 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001565 if (UseDFAHazardRec) {
1566 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1567 return new HexagonHazardRecognizer(II, this, HST);
1568 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001569 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1570}
1571
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001572/// \brief For a comparison instruction, return the source registers in
1573/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1574/// compares against in CmpValue. Return true if the comparison instruction
1575/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001576bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1577 unsigned &SrcReg2, int &Mask,
1578 int &Value) const {
1579 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001580
1581 // Set mask and the first source register.
1582 switch (Opc) {
1583 case Hexagon::C2_cmpeq:
1584 case Hexagon::C2_cmpeqp:
1585 case Hexagon::C2_cmpgt:
1586 case Hexagon::C2_cmpgtp:
1587 case Hexagon::C2_cmpgtu:
1588 case Hexagon::C2_cmpgtup:
1589 case Hexagon::C4_cmpneq:
1590 case Hexagon::C4_cmplte:
1591 case Hexagon::C4_cmplteu:
1592 case Hexagon::C2_cmpeqi:
1593 case Hexagon::C2_cmpgti:
1594 case Hexagon::C2_cmpgtui:
1595 case Hexagon::C4_cmpneqi:
1596 case Hexagon::C4_cmplteui:
1597 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001598 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001599 Mask = ~0;
1600 break;
1601 case Hexagon::A4_cmpbeq:
1602 case Hexagon::A4_cmpbgt:
1603 case Hexagon::A4_cmpbgtu:
1604 case Hexagon::A4_cmpbeqi:
1605 case Hexagon::A4_cmpbgti:
1606 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001607 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001608 Mask = 0xFF;
1609 break;
1610 case Hexagon::A4_cmpheq:
1611 case Hexagon::A4_cmphgt:
1612 case Hexagon::A4_cmphgtu:
1613 case Hexagon::A4_cmpheqi:
1614 case Hexagon::A4_cmphgti:
1615 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001616 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001617 Mask = 0xFFFF;
1618 break;
1619 }
1620
1621 // Set the value/second source register.
1622 switch (Opc) {
1623 case Hexagon::C2_cmpeq:
1624 case Hexagon::C2_cmpeqp:
1625 case Hexagon::C2_cmpgt:
1626 case Hexagon::C2_cmpgtp:
1627 case Hexagon::C2_cmpgtu:
1628 case Hexagon::C2_cmpgtup:
1629 case Hexagon::A4_cmpbeq:
1630 case Hexagon::A4_cmpbgt:
1631 case Hexagon::A4_cmpbgtu:
1632 case Hexagon::A4_cmpheq:
1633 case Hexagon::A4_cmphgt:
1634 case Hexagon::A4_cmphgtu:
1635 case Hexagon::C4_cmpneq:
1636 case Hexagon::C4_cmplte:
1637 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001638 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001639 return true;
1640
1641 case Hexagon::C2_cmpeqi:
1642 case Hexagon::C2_cmpgtui:
1643 case Hexagon::C2_cmpgti:
1644 case Hexagon::C4_cmpneqi:
1645 case Hexagon::C4_cmplteui:
1646 case Hexagon::C4_cmpltei:
1647 case Hexagon::A4_cmpbeqi:
1648 case Hexagon::A4_cmpbgti:
1649 case Hexagon::A4_cmpbgtui:
1650 case Hexagon::A4_cmpheqi:
1651 case Hexagon::A4_cmphgti:
1652 case Hexagon::A4_cmphgtui:
1653 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001654 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001655 return true;
1656 }
1657
1658 return false;
1659}
1660
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001661unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001662 const MachineInstr &MI,
1663 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001664 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001665}
1666
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001667
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001668DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1669 const TargetSubtargetInfo &STI) const {
1670 const InstrItineraryData *II = STI.getInstrItineraryData();
1671 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1672}
1673
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001674// Inspired by this pair:
1675// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1676// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1677// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001678bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1679 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001680 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1681 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001682 return false;
1683
1684 // Instructions that are pure loads, not loads and stores like memops are not
1685 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001686 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001687 return true;
1688
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001689 // Get the base register in MIa.
1690 unsigned BasePosA, OffsetPosA;
1691 if (!getBaseAndOffsetPosition(MIa, BasePosA, OffsetPosA))
1692 return false;
1693 const MachineOperand &BaseA = MIa.getOperand(BasePosA);
1694 unsigned BaseRegA = BaseA.getReg();
1695 unsigned BaseSubA = BaseA.getSubReg();
1696
1697 // Get the base register in MIb.
1698 unsigned BasePosB, OffsetPosB;
1699 if (!getBaseAndOffsetPosition(MIb, BasePosB, OffsetPosB))
1700 return false;
1701 const MachineOperand &BaseB = MIb.getOperand(BasePosB);
1702 unsigned BaseRegB = BaseB.getReg();
1703 unsigned BaseSubB = BaseB.getSubReg();
1704
1705 if (BaseRegA != BaseRegB || BaseSubA != BaseSubB)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001706 return false;
1707
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001708 // Get the access sizes.
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00001709 unsigned SizeA = getMemAccessSize(MIa);
1710 unsigned SizeB = getMemAccessSize(MIb);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001711
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001712 // Get the offsets. Handle immediates only for now.
1713 const MachineOperand &OffA = MIa.getOperand(OffsetPosA);
1714 const MachineOperand &OffB = MIb.getOperand(OffsetPosB);
1715 if (!MIa.getOperand(OffsetPosA).isImm() ||
1716 !MIb.getOperand(OffsetPosB).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001717 return false;
Krzysztof Parzyszekac019942017-07-19 19:17:32 +00001718 int OffsetA = isPostIncrement(MIa) ? 0 : OffA.getImm();
1719 int OffsetB = isPostIncrement(MIb) ? 0 : OffB.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001720
1721 // This is a mem access with the same base register and known offsets from it.
1722 // Reason about it.
1723 if (OffsetA > OffsetB) {
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001724 uint64_t OffDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1725 return SizeB <= OffDiff;
1726 }
1727 if (OffsetA < OffsetB) {
1728 uint64_t OffDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1729 return SizeA <= OffDiff;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001730 }
1731
1732 return false;
1733}
1734
Brendon Cahoon254f8892016-07-29 16:44:44 +00001735/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001736bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001737 int &Value) const {
1738 if (isPostIncrement(MI)) {
1739 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001740 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001741 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001742 if (MI.getOpcode() == Hexagon::A2_addi) {
1743 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001744 return true;
1745 }
1746
1747 return false;
1748}
1749
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001750std::pair<unsigned, unsigned>
1751HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1752 return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
1753 TF & HexagonII::MO_Bitmasks);
1754}
1755
1756ArrayRef<std::pair<unsigned, const char*>>
1757HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1758 using namespace HexagonII;
1759 static const std::pair<unsigned, const char*> Flags[] = {
1760 {MO_PCREL, "hexagon-pcrel"},
1761 {MO_GOT, "hexagon-got"},
1762 {MO_LO16, "hexagon-lo16"},
1763 {MO_HI16, "hexagon-hi16"},
1764 {MO_GPREL, "hexagon-gprel"},
1765 {MO_GDGOT, "hexagon-gdgot"},
1766 {MO_GDPLT, "hexagon-gdplt"},
1767 {MO_IE, "hexagon-ie"},
1768 {MO_IEGOT, "hexagon-iegot"},
1769 {MO_TPREL, "hexagon-tprel"}
1770 };
1771 return makeArrayRef(Flags);
1772}
1773
1774ArrayRef<std::pair<unsigned, const char*>>
1775HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1776 using namespace HexagonII;
1777 static const std::pair<unsigned, const char*> Flags[] = {
1778 {HMOTF_ConstExtended, "hexagon-ext"}
1779 };
1780 return makeArrayRef(Flags);
1781}
1782
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001783unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001784 MachineRegisterInfo &MRI = MF->getRegInfo();
1785 const TargetRegisterClass *TRC;
1786 if (VT == MVT::i1) {
1787 TRC = &Hexagon::PredRegsRegClass;
1788 } else if (VT == MVT::i32 || VT == MVT::f32) {
1789 TRC = &Hexagon::IntRegsRegClass;
1790 } else if (VT == MVT::i64 || VT == MVT::f64) {
1791 TRC = &Hexagon::DoubleRegsRegClass;
1792 } else {
1793 llvm_unreachable("Cannot handle this register class");
1794 }
1795
1796 unsigned NewReg = MRI.createVirtualRegister(TRC);
1797 return NewReg;
1798}
1799
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001800bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001801 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1802}
1803
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001804bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1805 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001806 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1807}
1808
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001809bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1810 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001811 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1812 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1813
1814 if (!(isTC1(MI))
1815 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001816 && !(MI.getDesc().mayLoad())
1817 && !(MI.getDesc().mayStore())
1818 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1819 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001820 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001821 && !(MI.isBranch())
1822 && !(MI.isReturn())
1823 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001824 return true;
1825
1826 return false;
1827}
1828
Sanjay Patele4b9f502015-12-07 19:21:39 +00001829// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001830bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001831 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001832}
1833
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001834// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1835// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001836bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1837 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001838 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1839 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001840 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001841
1842 unsigned isExtendable =
1843 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1844 if (!isExtendable)
1845 return false;
1846
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001847 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001848 return false;
1849
1850 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001851 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001852 // Use MO operand flags to determine if MO
1853 // has the HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00001854 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001855 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001856 // If this is a Machine BB address we are talking about, and it is
1857 // not marked as extended, say so.
1858 if (MO.isMBB())
1859 return false;
1860
1861 // We could be using an instruction with an extendable immediate and shoehorn
1862 // a global address into it. If it is a global address it will be constant
1863 // extended. We do this for COMBINE.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001864 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001865 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001866 return true;
1867
1868 // If the extendable operand is not 'Immediate' type, the instruction should
1869 // have 'isExtended' flag set.
1870 assert(MO.isImm() && "Extendable operand must be Immediate type");
1871
1872 int MinValue = getMinValue(MI);
1873 int MaxValue = getMaxValue(MI);
1874 int ImmValue = MO.getImm();
1875
1876 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001877}
1878
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001879bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1880 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001881 case Hexagon::L4_return :
1882 case Hexagon::L4_return_t :
1883 case Hexagon::L4_return_f :
1884 case Hexagon::L4_return_tnew_pnt :
1885 case Hexagon::L4_return_fnew_pnt :
1886 case Hexagon::L4_return_tnew_pt :
1887 case Hexagon::L4_return_fnew_pt :
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00001888 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001889 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001890 return false;
1891}
1892
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001893// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001894bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1895 const MachineInstr &ConsMI) const {
1896 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001897 return false;
1898
1899 auto &HRI = getRegisterInfo();
1900
1901 SmallVector<unsigned, 4> DefsA;
1902 SmallVector<unsigned, 4> DefsB;
1903 SmallVector<unsigned, 8> UsesA;
1904 SmallVector<unsigned, 8> UsesB;
1905
1906 parseOperands(ProdMI, DefsA, UsesA);
1907 parseOperands(ConsMI, DefsB, UsesB);
1908
1909 for (auto &RegA : DefsA)
1910 for (auto &RegB : UsesB) {
1911 // True data dependency.
1912 if (RegA == RegB)
1913 return true;
1914
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001915 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001916 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1917 if (RegB == *SubRegs)
1918 return true;
1919
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001920 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001921 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1922 if (RegA == *SubRegs)
1923 return true;
1924 }
1925
1926 return false;
1927}
1928
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001929// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001930bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1931 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001932 case Hexagon::V6_vL32b_cur_pi:
1933 case Hexagon::V6_vL32b_cur_ai:
1934 case Hexagon::V6_vL32b_cur_pi_128B:
1935 case Hexagon::V6_vL32b_cur_ai_128B:
1936 return true;
1937 }
1938 return false;
1939}
1940
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001941// Returns true, if any one of the operands is a dot new
1942// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001943bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
1944 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001945 return true;
1946
1947 return false;
1948}
1949
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001950/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001951bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
1952 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001953 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1954 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1955 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1956}
1957
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001958bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
1959 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001960 return true;
1961
1962 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001963 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001964 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001965}
1966
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001967bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1968 return (Opcode == Hexagon::ENDLOOP0 ||
1969 Opcode == Hexagon::ENDLOOP1);
1970}
1971
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001972bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1973 switch(OpType) {
1974 case MachineOperand::MO_MachineBasicBlock:
1975 case MachineOperand::MO_GlobalAddress:
1976 case MachineOperand::MO_ExternalSymbol:
1977 case MachineOperand::MO_JumpTableIndex:
1978 case MachineOperand::MO_ConstantPoolIndex:
1979 case MachineOperand::MO_BlockAddress:
1980 return true;
1981 default:
1982 return false;
1983 }
1984}
1985
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001986bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
1987 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001988 const uint64_t F = MID.TSFlags;
1989 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1990 return true;
1991
1992 // TODO: This is largely obsolete now. Will need to be removed
1993 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001994 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001995 // PS_fi and PS_fia remain special cases.
1996 case Hexagon::PS_fi:
1997 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001998 return true;
1999 default:
2000 return false;
2001 }
2002 return false;
2003}
2004
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002005// This returns true in two cases:
2006// - The OP code itself indicates that this is an extended instruction.
2007// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002008bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002009 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002010 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002011 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2012 return true;
2013 // Use MO operand flags to determine if one of MI's operands
2014 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00002015 for (const MachineOperand &MO : MI.operands())
2016 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002017 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002018 return false;
2019}
2020
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002021bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2022 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002023 const uint64_t F = get(Opcode).TSFlags;
2024 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2025}
2026
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002027// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002028bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2029 const MachineInstr &J) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002030 if (!isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002031 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002032 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002033 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002034 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002035}
2036
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002037bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2038 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002039 case Hexagon::J2_callr :
2040 case Hexagon::J2_callrf :
2041 case Hexagon::J2_callrt :
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00002042 case Hexagon::PS_call_nr :
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002043 return true;
2044 }
2045 return false;
2046}
2047
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002048bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2049 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002050 case Hexagon::L4_return :
2051 case Hexagon::L4_return_t :
2052 case Hexagon::L4_return_f :
2053 case Hexagon::L4_return_fnew_pnt :
2054 case Hexagon::L4_return_fnew_pt :
2055 case Hexagon::L4_return_tnew_pnt :
2056 case Hexagon::L4_return_tnew_pt :
2057 return true;
2058 }
2059 return false;
2060}
2061
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002062bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2063 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002064 case Hexagon::J2_jumpr :
2065 case Hexagon::J2_jumprt :
2066 case Hexagon::J2_jumprf :
2067 case Hexagon::J2_jumprtnewpt :
2068 case Hexagon::J2_jumprfnewpt :
2069 case Hexagon::J2_jumprtnew :
2070 case Hexagon::J2_jumprfnew :
2071 return true;
2072 }
2073 return false;
2074}
2075
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002076// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002077// Use abs estimate as oppose to the exact number.
2078// TODO: This will need to be changed to use MC level
2079// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002080bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002081 unsigned offset) const {
2082 // This selection of jump instructions matches to that what
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00002083 // analyzeBranch can parse, plus NVJ.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002084 if (isNewValueJump(MI)) // r9:2
2085 return isInt<11>(offset);
2086
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002087 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002088 // Still missing Jump to address condition on register value.
2089 default:
2090 return false;
2091 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2092 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002093 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002094 return isInt<24>(offset);
2095 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2096 case Hexagon::J2_jumpf:
2097 case Hexagon::J2_jumptnew:
2098 case Hexagon::J2_jumptnewpt:
2099 case Hexagon::J2_jumpfnew:
2100 case Hexagon::J2_jumpfnewpt:
2101 case Hexagon::J2_callt:
2102 case Hexagon::J2_callf:
2103 return isInt<17>(offset);
2104 case Hexagon::J2_loop0i:
2105 case Hexagon::J2_loop0iext:
2106 case Hexagon::J2_loop0r:
2107 case Hexagon::J2_loop0rext:
2108 case Hexagon::J2_loop1i:
2109 case Hexagon::J2_loop1iext:
2110 case Hexagon::J2_loop1r:
2111 case Hexagon::J2_loop1rext:
2112 return isInt<9>(offset);
2113 // TODO: Add all the compound branches here. Can we do this in Relation model?
2114 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2115 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2116 return isInt<11>(offset);
2117 }
2118}
2119
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002120bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2121 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002122 bool isLate = isLateResultInstr(LRMI);
2123 bool isEarly = isEarlySourceInstr(ESMI);
2124
2125 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002126 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002127 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002128 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002129
2130 if (isLate && isEarly) {
2131 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2132 return true;
2133 }
2134
2135 return false;
2136}
2137
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002138bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2139 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002140 case TargetOpcode::EXTRACT_SUBREG:
2141 case TargetOpcode::INSERT_SUBREG:
2142 case TargetOpcode::SUBREG_TO_REG:
2143 case TargetOpcode::REG_SEQUENCE:
2144 case TargetOpcode::IMPLICIT_DEF:
2145 case TargetOpcode::COPY:
2146 case TargetOpcode::INLINEASM:
2147 case TargetOpcode::PHI:
2148 return false;
2149 default:
2150 break;
2151 }
2152
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002153 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002154 return !is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002155}
2156
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002157bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002158 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2159 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002160 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002161}
2162
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002163bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2164 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002165 return Opcode == Hexagon::J2_loop0i ||
2166 Opcode == Hexagon::J2_loop0r ||
2167 Opcode == Hexagon::J2_loop0iext ||
2168 Opcode == Hexagon::J2_loop0rext ||
2169 Opcode == Hexagon::J2_loop1i ||
2170 Opcode == Hexagon::J2_loop1r ||
2171 Opcode == Hexagon::J2_loop1iext ||
2172 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002173}
2174
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002175bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2176 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002177 default: return false;
2178 case Hexagon::L4_iadd_memopw_io :
2179 case Hexagon::L4_isub_memopw_io :
2180 case Hexagon::L4_add_memopw_io :
2181 case Hexagon::L4_sub_memopw_io :
2182 case Hexagon::L4_and_memopw_io :
2183 case Hexagon::L4_or_memopw_io :
2184 case Hexagon::L4_iadd_memoph_io :
2185 case Hexagon::L4_isub_memoph_io :
2186 case Hexagon::L4_add_memoph_io :
2187 case Hexagon::L4_sub_memoph_io :
2188 case Hexagon::L4_and_memoph_io :
2189 case Hexagon::L4_or_memoph_io :
2190 case Hexagon::L4_iadd_memopb_io :
2191 case Hexagon::L4_isub_memopb_io :
2192 case Hexagon::L4_add_memopb_io :
2193 case Hexagon::L4_sub_memopb_io :
2194 case Hexagon::L4_and_memopb_io :
2195 case Hexagon::L4_or_memopb_io :
2196 case Hexagon::L4_ior_memopb_io:
2197 case Hexagon::L4_ior_memoph_io:
2198 case Hexagon::L4_ior_memopw_io:
2199 case Hexagon::L4_iand_memopb_io:
2200 case Hexagon::L4_iand_memoph_io:
2201 case Hexagon::L4_iand_memopw_io:
2202 return true;
2203 }
2204 return false;
2205}
2206
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002207bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2208 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002209 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2210}
2211
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002212bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2213 const uint64_t F = get(Opcode).TSFlags;
2214 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2215}
2216
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002217bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002218 return isNewValueJump(MI) || isNewValueStore(MI);
2219}
2220
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002221bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2222 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002223}
2224
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002225bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2226 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2227}
2228
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002229bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2230 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002231 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2232}
2233
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002234bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2235 const uint64_t F = get(Opcode).TSFlags;
2236 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2237}
2238
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002239// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002240bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002241 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002242 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002243 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2244 == OperandNum;
2245}
2246
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002247bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2248 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002249 assert(isPredicated(MI));
2250 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2251}
2252
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002253bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2254 const uint64_t F = get(Opcode).TSFlags;
2255 assert(isPredicated(Opcode));
2256 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2257}
2258
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002259bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2260 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002261 return !((F >> HexagonII::PredicatedFalsePos) &
2262 HexagonII::PredicatedFalseMask);
2263}
2264
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002265bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2266 const uint64_t F = get(Opcode).TSFlags;
2267 // Make sure that the instruction is predicated.
2268 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2269 return !((F >> HexagonII::PredicatedFalsePos) &
2270 HexagonII::PredicatedFalseMask);
2271}
2272
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002273bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2274 const uint64_t F = get(Opcode).TSFlags;
2275 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2276}
2277
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002278bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2279 const uint64_t F = get(Opcode).TSFlags;
2280 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2281}
2282
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002283bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2284 const uint64_t F = get(Opcode).TSFlags;
2285 assert(get(Opcode).isBranch() &&
2286 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2287 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2288}
2289
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002290bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2291 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2292 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2293 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2294 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002295}
2296
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002297bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2298 switch (MI.getOpcode()) {
2299 // Byte
2300 case Hexagon::L2_loadrb_io:
2301 case Hexagon::L4_loadrb_ur:
2302 case Hexagon::L4_loadrb_ap:
2303 case Hexagon::L2_loadrb_pr:
2304 case Hexagon::L2_loadrb_pbr:
2305 case Hexagon::L2_loadrb_pi:
2306 case Hexagon::L2_loadrb_pci:
2307 case Hexagon::L2_loadrb_pcr:
2308 case Hexagon::L2_loadbsw2_io:
2309 case Hexagon::L4_loadbsw2_ur:
2310 case Hexagon::L4_loadbsw2_ap:
2311 case Hexagon::L2_loadbsw2_pr:
2312 case Hexagon::L2_loadbsw2_pbr:
2313 case Hexagon::L2_loadbsw2_pi:
2314 case Hexagon::L2_loadbsw2_pci:
2315 case Hexagon::L2_loadbsw2_pcr:
2316 case Hexagon::L2_loadbsw4_io:
2317 case Hexagon::L4_loadbsw4_ur:
2318 case Hexagon::L4_loadbsw4_ap:
2319 case Hexagon::L2_loadbsw4_pr:
2320 case Hexagon::L2_loadbsw4_pbr:
2321 case Hexagon::L2_loadbsw4_pi:
2322 case Hexagon::L2_loadbsw4_pci:
2323 case Hexagon::L2_loadbsw4_pcr:
2324 case Hexagon::L4_loadrb_rr:
2325 case Hexagon::L2_ploadrbt_io:
2326 case Hexagon::L2_ploadrbt_pi:
2327 case Hexagon::L2_ploadrbf_io:
2328 case Hexagon::L2_ploadrbf_pi:
2329 case Hexagon::L2_ploadrbtnew_io:
2330 case Hexagon::L2_ploadrbfnew_io:
2331 case Hexagon::L4_ploadrbt_rr:
2332 case Hexagon::L4_ploadrbf_rr:
2333 case Hexagon::L4_ploadrbtnew_rr:
2334 case Hexagon::L4_ploadrbfnew_rr:
2335 case Hexagon::L2_ploadrbtnew_pi:
2336 case Hexagon::L2_ploadrbfnew_pi:
2337 case Hexagon::L4_ploadrbt_abs:
2338 case Hexagon::L4_ploadrbf_abs:
2339 case Hexagon::L4_ploadrbtnew_abs:
2340 case Hexagon::L4_ploadrbfnew_abs:
2341 case Hexagon::L2_loadrbgp:
2342 // Half
2343 case Hexagon::L2_loadrh_io:
2344 case Hexagon::L4_loadrh_ur:
2345 case Hexagon::L4_loadrh_ap:
2346 case Hexagon::L2_loadrh_pr:
2347 case Hexagon::L2_loadrh_pbr:
2348 case Hexagon::L2_loadrh_pi:
2349 case Hexagon::L2_loadrh_pci:
2350 case Hexagon::L2_loadrh_pcr:
2351 case Hexagon::L4_loadrh_rr:
2352 case Hexagon::L2_ploadrht_io:
2353 case Hexagon::L2_ploadrht_pi:
2354 case Hexagon::L2_ploadrhf_io:
2355 case Hexagon::L2_ploadrhf_pi:
2356 case Hexagon::L2_ploadrhtnew_io:
2357 case Hexagon::L2_ploadrhfnew_io:
2358 case Hexagon::L4_ploadrht_rr:
2359 case Hexagon::L4_ploadrhf_rr:
2360 case Hexagon::L4_ploadrhtnew_rr:
2361 case Hexagon::L4_ploadrhfnew_rr:
2362 case Hexagon::L2_ploadrhtnew_pi:
2363 case Hexagon::L2_ploadrhfnew_pi:
2364 case Hexagon::L4_ploadrht_abs:
2365 case Hexagon::L4_ploadrhf_abs:
2366 case Hexagon::L4_ploadrhtnew_abs:
2367 case Hexagon::L4_ploadrhfnew_abs:
2368 case Hexagon::L2_loadrhgp:
2369 return true;
2370 default:
2371 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002372 }
2373}
2374
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002375bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2376 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002377 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2378}
2379
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002380bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2381 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002382 case Hexagon::STriw_pred :
2383 case Hexagon::LDriw_pred :
2384 return true;
2385 default:
2386 return false;
2387 }
2388}
2389
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002390bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2391 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002392 return false;
2393
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002394 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002395 if (Op.isGlobal() || Op.isSymbol())
2396 return true;
2397 return false;
2398}
2399
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002400// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002401bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2402 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002403 return is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002404}
2405
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002406bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2407 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002408 return is_TC2(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002409}
2410
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002411bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2412 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002413 return is_TC2early(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002414}
2415
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002416bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2417 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002418 return is_TC4x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002419}
2420
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002421// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002422bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2423 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002424 if (mayBeCurLoad(MI1)) {
2425 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002426 unsigned DstReg = MI1.getOperand(0).getReg();
2427 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002428 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002429 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002430 return true;
2431 }
2432 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002433 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2434 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2435 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002436 return true;
2437 return false;
2438}
2439
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002440bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002441 const uint64_t V = getType(MI);
2442 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2443}
2444
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002445// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2446//
2447bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2448 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2449 VT == MVT::v32i16 || VT == MVT::v64i8) {
2450 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2451 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2452 (Offset & 0x3f) == 0);
2453 }
2454 // 128B
2455 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2456 VT == MVT::v64i16 || VT == MVT::v128i8) {
2457 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2458 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2459 (Offset & 0x7f) == 0);
2460 }
2461 if (VT == MVT::i64) {
2462 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2463 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2464 (Offset & 0x7) == 0);
2465 }
2466 if (VT == MVT::i32) {
2467 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2468 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2469 (Offset & 0x3) == 0);
2470 }
2471 if (VT == MVT::i16) {
2472 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2473 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2474 (Offset & 0x1) == 0);
2475 }
2476 if (VT == MVT::i8) {
2477 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2478 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2479 }
2480 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002481}
2482
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002483bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2484 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002485 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002486 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002487 // inserted to calculate the final address. Due to this reason, the function
2488 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002489 // We used to assert if the offset was not properly aligned, however,
2490 // there are cases where a misaligned pointer recast can cause this
2491 // problem, and we need to allow for it. The front end warns of such
2492 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002493
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002494 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002495 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002496 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002497 case Hexagon::PS_vstorerw_nt_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002498 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002499 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002500 case Hexagon::PS_vloadrw_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002501 case Hexagon::V6_vL32b_ai:
2502 case Hexagon::V6_vS32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002503 case Hexagon::V6_vL32b_nt_ai:
2504 case Hexagon::V6_vS32b_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002505 case Hexagon::V6_vL32Ub_ai:
2506 case Hexagon::V6_vS32Ub_ai:
Krzysztof Parzyszek918e6d72017-06-26 14:17:58 +00002507 return isShiftedInt<4,6>(Offset);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002508
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002509 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002510 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002511 case Hexagon::PS_vstorerw_nt_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002512 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002513 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002514 case Hexagon::PS_vloadrw_nt_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002515 case Hexagon::V6_vL32b_ai_128B:
2516 case Hexagon::V6_vS32b_ai_128B:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002517 case Hexagon::V6_vL32b_nt_ai_128B:
2518 case Hexagon::V6_vS32b_nt_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002519 case Hexagon::V6_vL32Ub_ai_128B:
2520 case Hexagon::V6_vS32Ub_ai_128B:
Krzysztof Parzyszek918e6d72017-06-26 14:17:58 +00002521 return isShiftedInt<4,7>(Offset);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002522
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002523 case Hexagon::J2_loop0i:
2524 case Hexagon::J2_loop1i:
2525 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002526
2527 case Hexagon::S4_storeirb_io:
2528 case Hexagon::S4_storeirbt_io:
2529 case Hexagon::S4_storeirbf_io:
2530 return isUInt<6>(Offset);
2531
2532 case Hexagon::S4_storeirh_io:
2533 case Hexagon::S4_storeirht_io:
2534 case Hexagon::S4_storeirhf_io:
2535 return isShiftedUInt<6,1>(Offset);
2536
2537 case Hexagon::S4_storeiri_io:
2538 case Hexagon::S4_storeirit_io:
2539 case Hexagon::S4_storeirif_io:
2540 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002541 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002542
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002543 if (Extend)
2544 return true;
2545
2546 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002547 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002548 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002549 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2550 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2551
Colin LeMahieu947cd702014-12-23 20:44:59 +00002552 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002553 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002554 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2555 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2556
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002557 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002558 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002559 case Hexagon::S2_storerh_io:
Krzysztof Parzyszekd10df492017-05-03 15:36:51 +00002560 case Hexagon::S2_storerf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002561 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2562 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2563
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002564 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002565 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002566 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002567 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2568 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2569
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002570 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002571 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2572 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2573
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002574 case Hexagon::L4_iadd_memopw_io :
2575 case Hexagon::L4_isub_memopw_io :
2576 case Hexagon::L4_add_memopw_io :
2577 case Hexagon::L4_sub_memopw_io :
2578 case Hexagon::L4_and_memopw_io :
2579 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002580 return (0 <= Offset && Offset <= 255);
2581
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002582 case Hexagon::L4_iadd_memoph_io :
2583 case Hexagon::L4_isub_memoph_io :
2584 case Hexagon::L4_add_memoph_io :
2585 case Hexagon::L4_sub_memoph_io :
2586 case Hexagon::L4_and_memoph_io :
2587 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002588 return (0 <= Offset && Offset <= 127);
2589
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002590 case Hexagon::L4_iadd_memopb_io :
2591 case Hexagon::L4_isub_memopb_io :
2592 case Hexagon::L4_add_memopb_io :
2593 case Hexagon::L4_sub_memopb_io :
2594 case Hexagon::L4_and_memopb_io :
2595 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002596 return (0 <= Offset && Offset <= 63);
2597
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002598 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002599 // any size. Later pass knows how to handle it.
2600 case Hexagon::STriw_pred:
2601 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002602 case Hexagon::STriw_mod:
2603 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002604 return true;
2605
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002606 case Hexagon::PS_fi:
2607 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002608 case Hexagon::INLINEASM:
2609 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002610
2611 case Hexagon::L2_ploadrbt_io:
2612 case Hexagon::L2_ploadrbf_io:
2613 case Hexagon::L2_ploadrubt_io:
2614 case Hexagon::L2_ploadrubf_io:
2615 case Hexagon::S2_pstorerbt_io:
2616 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002617 return isUInt<6>(Offset);
2618
2619 case Hexagon::L2_ploadrht_io:
2620 case Hexagon::L2_ploadrhf_io:
2621 case Hexagon::L2_ploadruht_io:
2622 case Hexagon::L2_ploadruhf_io:
2623 case Hexagon::S2_pstorerht_io:
2624 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002625 return isShiftedUInt<6,1>(Offset);
2626
2627 case Hexagon::L2_ploadrit_io:
2628 case Hexagon::L2_ploadrif_io:
2629 case Hexagon::S2_pstorerit_io:
2630 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002631 return isShiftedUInt<6,2>(Offset);
2632
2633 case Hexagon::L2_ploadrdt_io:
2634 case Hexagon::L2_ploadrdf_io:
2635 case Hexagon::S2_pstorerdt_io:
2636 case Hexagon::S2_pstorerdf_io:
2637 return isShiftedUInt<6,3>(Offset);
2638 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002639
Benjamin Kramerb6684012011-12-27 11:41:05 +00002640 llvm_unreachable("No offset range is defined for this opcode. "
2641 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002642}
2643
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002644bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002645 return isHVXVec(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002646}
2647
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002648bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2649 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002650 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2651 return
2652 V == HexagonII::TypeCVI_VA ||
2653 V == HexagonII::TypeCVI_VA_DV;
2654}
Andrew Trickd06df962012-02-01 22:13:57 +00002655
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002656bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2657 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002658 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2659 return true;
2660
2661 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2662 return true;
2663
2664 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002665 return true;
2666
2667 return false;
2668}
Jyotsna Verma84256432013-03-01 17:37:13 +00002669
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002670bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2671 switch (MI.getOpcode()) {
2672 // Byte
2673 case Hexagon::L2_loadrub_io:
2674 case Hexagon::L4_loadrub_ur:
2675 case Hexagon::L4_loadrub_ap:
2676 case Hexagon::L2_loadrub_pr:
2677 case Hexagon::L2_loadrub_pbr:
2678 case Hexagon::L2_loadrub_pi:
2679 case Hexagon::L2_loadrub_pci:
2680 case Hexagon::L2_loadrub_pcr:
2681 case Hexagon::L2_loadbzw2_io:
2682 case Hexagon::L4_loadbzw2_ur:
2683 case Hexagon::L4_loadbzw2_ap:
2684 case Hexagon::L2_loadbzw2_pr:
2685 case Hexagon::L2_loadbzw2_pbr:
2686 case Hexagon::L2_loadbzw2_pi:
2687 case Hexagon::L2_loadbzw2_pci:
2688 case Hexagon::L2_loadbzw2_pcr:
2689 case Hexagon::L2_loadbzw4_io:
2690 case Hexagon::L4_loadbzw4_ur:
2691 case Hexagon::L4_loadbzw4_ap:
2692 case Hexagon::L2_loadbzw4_pr:
2693 case Hexagon::L2_loadbzw4_pbr:
2694 case Hexagon::L2_loadbzw4_pi:
2695 case Hexagon::L2_loadbzw4_pci:
2696 case Hexagon::L2_loadbzw4_pcr:
2697 case Hexagon::L4_loadrub_rr:
2698 case Hexagon::L2_ploadrubt_io:
2699 case Hexagon::L2_ploadrubt_pi:
2700 case Hexagon::L2_ploadrubf_io:
2701 case Hexagon::L2_ploadrubf_pi:
2702 case Hexagon::L2_ploadrubtnew_io:
2703 case Hexagon::L2_ploadrubfnew_io:
2704 case Hexagon::L4_ploadrubt_rr:
2705 case Hexagon::L4_ploadrubf_rr:
2706 case Hexagon::L4_ploadrubtnew_rr:
2707 case Hexagon::L4_ploadrubfnew_rr:
2708 case Hexagon::L2_ploadrubtnew_pi:
2709 case Hexagon::L2_ploadrubfnew_pi:
2710 case Hexagon::L4_ploadrubt_abs:
2711 case Hexagon::L4_ploadrubf_abs:
2712 case Hexagon::L4_ploadrubtnew_abs:
2713 case Hexagon::L4_ploadrubfnew_abs:
2714 case Hexagon::L2_loadrubgp:
2715 // Half
2716 case Hexagon::L2_loadruh_io:
2717 case Hexagon::L4_loadruh_ur:
2718 case Hexagon::L4_loadruh_ap:
2719 case Hexagon::L2_loadruh_pr:
2720 case Hexagon::L2_loadruh_pbr:
2721 case Hexagon::L2_loadruh_pi:
2722 case Hexagon::L2_loadruh_pci:
2723 case Hexagon::L2_loadruh_pcr:
2724 case Hexagon::L4_loadruh_rr:
2725 case Hexagon::L2_ploadruht_io:
2726 case Hexagon::L2_ploadruht_pi:
2727 case Hexagon::L2_ploadruhf_io:
2728 case Hexagon::L2_ploadruhf_pi:
2729 case Hexagon::L2_ploadruhtnew_io:
2730 case Hexagon::L2_ploadruhfnew_io:
2731 case Hexagon::L4_ploadruht_rr:
2732 case Hexagon::L4_ploadruhf_rr:
2733 case Hexagon::L4_ploadruhtnew_rr:
2734 case Hexagon::L4_ploadruhfnew_rr:
2735 case Hexagon::L2_ploadruhtnew_pi:
2736 case Hexagon::L2_ploadruhfnew_pi:
2737 case Hexagon::L4_ploadruht_abs:
2738 case Hexagon::L4_ploadruhf_abs:
2739 case Hexagon::L4_ploadruhtnew_abs:
2740 case Hexagon::L4_ploadruhfnew_abs:
2741 case Hexagon::L2_loadruhgp:
2742 return true;
2743 default:
2744 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002745 }
2746}
2747
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002748// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002749bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2750 const MachineInstr &MI2) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002751 if (isHVXVec(MI1) && isHVXVec(MI2))
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002752 if (!isVecUsableNextPacket(MI1, MI2))
2753 return true;
2754 return false;
2755}
2756
Brendon Cahoon254f8892016-07-29 16:44:44 +00002757/// \brief Get the base register and byte offset of a load/store instr.
2758bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2759 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2760 const {
2761 unsigned AccessSize = 0;
2762 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002763 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002764 Offset = OffsetVal;
2765 return BaseReg != 0;
2766}
2767
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002768/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002769bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2770 const MachineInstr &Second) const {
Krzysztof Parzyszek4763c2d2017-05-03 15:33:09 +00002771 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2772 const MachineOperand &Op = Second.getOperand(0);
2773 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2774 return true;
2775 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002776 if (DisableNVSchedule)
2777 return false;
2778 if (mayBeNewStore(Second)) {
2779 // Make sure the definition of the first instruction is the value being
2780 // stored.
2781 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002782 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002783 if (!Stored.isReg())
2784 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002785 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2786 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002787 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2788 return true;
2789 }
2790 }
2791 return false;
2792}
2793
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002794bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2795 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002796 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002797}
2798
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002799bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2800 for (auto &I : *B)
2801 if (I.isEHLabel())
2802 return true;
2803 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002804}
2805
Jyotsna Verma84256432013-03-01 17:37:13 +00002806// Returns true if an instruction can be converted into a non-extended
2807// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002808bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002809 short NonExtOpcode;
2810 // Check if the instruction has a register form that uses register in place
2811 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002812 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002813 return true;
2814
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002815 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002816 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002817
2818 switch (getAddrMode(MI)) {
2819 case HexagonII::Absolute :
2820 // Load/store with absolute addressing mode can be converted into
2821 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002822 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002823 break;
2824 case HexagonII::BaseImmOffset :
2825 // Load/store with base+offset addressing mode can be converted into
2826 // base+register offset addressing mode. However left shift operand should
2827 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002828 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002829 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002830 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002831 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002832 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002833 default:
2834 return false;
2835 }
2836 if (NonExtOpcode < 0)
2837 return false;
2838 return true;
2839 }
2840 return false;
2841}
2842
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002843bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2844 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002845 Hexagon::InstrType_Pseudo) >= 0;
2846}
2847
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002848bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2849 const {
2850 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2851 while (I != E) {
2852 if (I->isBarrier())
2853 return true;
2854 ++I;
2855 }
2856 return false;
2857}
2858
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002859// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002860bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
2861 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2862 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002863 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2864 HST.hasV60TOps();
2865}
2866
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002867// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002868bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2869 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002870 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2871}
2872
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002873bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2874 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002875 // There is no stall when ProdMI is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002876 if (!isHVXVec(ProdMI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002877 return false;
2878
2879 // There is no stall when ProdMI and ConsMI are not dependent.
2880 if (!isDependent(ProdMI, ConsMI))
2881 return false;
2882
2883 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2884 // are scheduled in consecutive packets.
2885 if (isVecUsableNextPacket(ProdMI, ConsMI))
2886 return false;
2887
2888 return true;
2889}
2890
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002891bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002892 MachineBasicBlock::const_instr_iterator BII) const {
2893 // There is no stall when I is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002894 if (!isHVXVec(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002895 return false;
2896
2897 MachineBasicBlock::const_instr_iterator MII = BII;
2898 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2899
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002900 if (!(*MII).isBundle()) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002901 const MachineInstr &J = *MII;
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002902 return producesStall(J, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002903 }
2904
2905 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002906 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002907 if (producesStall(J, MI))
2908 return true;
2909 }
2910 return false;
2911}
2912
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002913bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002914 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002915 for (const MachineOperand &MO : MI.operands()) {
2916 // Predicate register must be explicitly defined.
2917 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
2918 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002919 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002920 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002921 }
2922
2923 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2924 // memd_locked cannot be used as .new as well,
2925 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002926 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002927}
2928
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002929bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00002930 return Opcode == Hexagon::J2_jumpt ||
2931 Opcode == Hexagon::J2_jumptpt ||
2932 Opcode == Hexagon::J2_jumpf ||
2933 Opcode == Hexagon::J2_jumpfpt ||
2934 Opcode == Hexagon::J2_jumptnew ||
2935 Opcode == Hexagon::J2_jumpfnew ||
2936 Opcode == Hexagon::J2_jumptnewpt ||
2937 Opcode == Hexagon::J2_jumpfnewpt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002938}
2939
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002940bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2941 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2942 return false;
2943 return !isPredicatedTrue(Cond[0].getImm());
2944}
2945
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002946short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
2947 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00002948}
2949
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002950unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
2951 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002952 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
2953}
2954
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002955// Returns the base register in a memory access (load/store). The offset is
2956// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002957// If the base register has a subregister or the offset field does not contain
2958// an immediate value, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002959unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002960 int &Offset, unsigned &AccessSize) const {
2961 // Return if it is not a base+offset type instruction or a MemOp.
2962 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
2963 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002964 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002965 return 0;
2966
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00002967 AccessSize = getMemAccessSize(MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002968
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002969 unsigned BasePos = 0, OffsetPos = 0;
2970 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002971 return 0;
2972
2973 // Post increment updates its EA after the mem access,
2974 // so we need to treat its offset as zero.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002975 if (isPostIncrement(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002976 Offset = 0;
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002977 } else {
2978 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
2979 if (!OffsetOp.isImm())
2980 return 0;
2981 Offset = OffsetOp.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002982 }
2983
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002984 const MachineOperand &BaseOp = MI.getOperand(BasePos);
2985 if (BaseOp.getSubReg() != 0)
2986 return 0;
2987 return BaseOp.getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002988}
2989
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002990/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002991bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002992 unsigned &BasePos, unsigned &OffsetPos) const {
2993 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002994 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002995 BasePos = 0;
2996 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002997 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002998 BasePos = 0;
2999 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003000 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003001 BasePos = 1;
3002 OffsetPos = 2;
3003 } else
3004 return false;
3005
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003006 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003007 BasePos++;
3008 OffsetPos++;
3009 }
3010 if (isPostIncrement(MI)) {
3011 BasePos++;
3012 OffsetPos++;
3013 }
3014
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003015 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003016 return false;
3017
3018 return true;
3019}
3020
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003021// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003022// e.g. jump_t t1 (i1)
3023// jump t2 (i2)
3024// Jumpers = {i2, i1}
3025SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3026 MachineBasicBlock& MBB) const {
3027 SmallVector<MachineInstr*, 2> Jumpers;
3028 // If the block has no terminators, it just falls into the block after it.
3029 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3030 if (I == MBB.instr_begin())
3031 return Jumpers;
3032
3033 // A basic block may looks like this:
3034 //
3035 // [ insn
3036 // EH_LABEL
3037 // insn
3038 // insn
3039 // insn
3040 // EH_LABEL
3041 // insn ]
3042 //
3043 // It has two succs but does not have a terminator
3044 // Don't know how to handle it.
3045 do {
3046 --I;
3047 if (I->isEHLabel())
3048 return Jumpers;
3049 } while (I != MBB.instr_begin());
3050
3051 I = MBB.instr_end();
3052 --I;
3053
3054 while (I->isDebugValue()) {
3055 if (I == MBB.instr_begin())
3056 return Jumpers;
3057 --I;
3058 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003059 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003060 return Jumpers;
3061
3062 // Get the last instruction in the block.
3063 MachineInstr *LastInst = &*I;
3064 Jumpers.push_back(LastInst);
3065 MachineInstr *SecondLastInst = nullptr;
3066 // Find one more terminator if present.
3067 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003068 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003069 if (!SecondLastInst) {
3070 SecondLastInst = &*I;
3071 Jumpers.push_back(SecondLastInst);
3072 } else // This is a third branch.
3073 return Jumpers;
3074 }
3075 if (I == MBB.instr_begin())
3076 break;
3077 --I;
3078 } while (true);
3079 return Jumpers;
3080}
3081
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003082short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3083 if (Opcode < 0)
3084 return -1;
3085 return Hexagon::getBaseWithLongOffset(Opcode);
3086}
3087
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003088short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3089 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003090}
3091
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003092short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3093 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003094}
3095
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003096// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003097unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3098 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003099 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3100}
3101
3102// See if instruction could potentially be a duplex candidate.
3103// If so, return its group. Zero otherwise.
3104HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003105 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003106 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3107
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003108 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003109 default:
3110 return HexagonII::HCG_None;
3111 //
3112 // Compound pairs.
3113 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3114 // "Rd16=#U6 ; jump #r9:2"
3115 // "Rd16=Rs16 ; jump #r9:2"
3116 //
3117 case Hexagon::C2_cmpeq:
3118 case Hexagon::C2_cmpgt:
3119 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003120 DstReg = MI.getOperand(0).getReg();
3121 Src1Reg = MI.getOperand(1).getReg();
3122 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003123 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3124 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3125 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3126 return HexagonII::HCG_A;
3127 break;
3128 case Hexagon::C2_cmpeqi:
3129 case Hexagon::C2_cmpgti:
3130 case Hexagon::C2_cmpgtui:
3131 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003132 DstReg = MI.getOperand(0).getReg();
3133 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003134 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3135 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003136 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3137 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3138 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003139 return HexagonII::HCG_A;
3140 break;
3141 case Hexagon::A2_tfr:
3142 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003143 DstReg = MI.getOperand(0).getReg();
3144 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003145 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3146 return HexagonII::HCG_A;
3147 break;
3148 case Hexagon::A2_tfrsi:
3149 // Rd = #u6
3150 // Do not test for #u6 size since the const is getting extended
3151 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003152 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003153 if (isIntRegForSubInst(DstReg))
3154 return HexagonII::HCG_A;
3155 break;
3156 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003157 DstReg = MI.getOperand(0).getReg();
3158 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003159 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3160 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003161 MI.getOperand(2).isImm() &&
3162 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003163 return HexagonII::HCG_A;
3164 break;
3165 // The fact that .new form is used pretty much guarantees
3166 // that predicate register will match. Nevertheless,
3167 // there could be some false positives without additional
3168 // checking.
3169 case Hexagon::J2_jumptnew:
3170 case Hexagon::J2_jumpfnew:
3171 case Hexagon::J2_jumptnewpt:
3172 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003173 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003174 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3175 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3176 return HexagonII::HCG_B;
3177 break;
3178 // Transfer and jump:
3179 // Rd=#U6 ; jump #r9:2
3180 // Rd=Rs ; jump #r9:2
3181 // Do not test for jump range here.
3182 case Hexagon::J2_jump:
3183 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003184 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003185 return HexagonII::HCG_C;
3186 break;
3187 }
3188
3189 return HexagonII::HCG_None;
3190}
3191
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003192// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003193unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3194 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003195 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3196 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003197 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3198 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003199 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003200 unsigned DestReg = GA.getOperand(0).getReg();
3201 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003202 return -1;
3203 if (DestReg == Hexagon::P0)
3204 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3205 if (DestReg == Hexagon::P1)
3206 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3207 return -1;
3208}
3209
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003210int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3211 enum Hexagon::PredSense inPredSense;
3212 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3213 Hexagon::PredSense_true;
3214 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3215 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3216 return CondOpcode;
3217
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003218 llvm_unreachable("Unexpected predicable instruction");
3219}
3220
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003221// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003222int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3223 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003224 default: llvm_unreachable("Unknown .cur type");
3225 case Hexagon::V6_vL32b_pi:
3226 return Hexagon::V6_vL32b_cur_pi;
3227 case Hexagon::V6_vL32b_ai:
3228 return Hexagon::V6_vL32b_cur_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003229 case Hexagon::V6_vL32b_nt_pi:
3230 return Hexagon::V6_vL32b_nt_cur_pi;
3231 case Hexagon::V6_vL32b_nt_ai:
3232 return Hexagon::V6_vL32b_nt_cur_ai;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003233 //128B
3234 case Hexagon::V6_vL32b_pi_128B:
3235 return Hexagon::V6_vL32b_cur_pi_128B;
3236 case Hexagon::V6_vL32b_ai_128B:
3237 return Hexagon::V6_vL32b_cur_ai_128B;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003238 case Hexagon::V6_vL32b_nt_pi_128B:
3239 return Hexagon::V6_vL32b_nt_cur_pi_128B;
3240 case Hexagon::V6_vL32b_nt_ai_128B:
3241 return Hexagon::V6_vL32b_nt_cur_ai_128B;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003242 }
3243 return 0;
3244}
3245
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003246// Return the regular version of the .cur instruction.
3247int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3248 switch (MI.getOpcode()) {
3249 default: llvm_unreachable("Unknown .cur type");
3250 case Hexagon::V6_vL32b_cur_pi:
3251 return Hexagon::V6_vL32b_pi;
3252 case Hexagon::V6_vL32b_cur_ai:
3253 return Hexagon::V6_vL32b_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003254 case Hexagon::V6_vL32b_nt_cur_pi:
3255 return Hexagon::V6_vL32b_nt_pi;
3256 case Hexagon::V6_vL32b_nt_cur_ai:
3257 return Hexagon::V6_vL32b_nt_ai;
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003258 //128B
3259 case Hexagon::V6_vL32b_cur_pi_128B:
3260 return Hexagon::V6_vL32b_pi_128B;
3261 case Hexagon::V6_vL32b_cur_ai_128B:
3262 return Hexagon::V6_vL32b_ai_128B;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003263 case Hexagon::V6_vL32b_nt_cur_pi_128B:
3264 return Hexagon::V6_vL32b_nt_pi_128B;
3265 case Hexagon::V6_vL32b_nt_cur_ai_128B:
3266 return Hexagon::V6_vL32b_nt_ai_128B;
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003267 }
3268 return 0;
3269}
3270
3271
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003272// The diagram below shows the steps involved in the conversion of a predicated
3273// store instruction to its .new predicated new-value form.
3274//
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003275// Note: It doesn't include conditional new-value stores as they can't be
3276// converted to .new predicate.
3277//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003278// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3279// ^ ^
3280// / \ (not OK. it will cause new-value store to be
3281// / X conditional on p0.new while R2 producer is
3282// / \ on p0)
3283// / \.
3284// p.new store p.old NV store
3285// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3286// ^ ^
3287// \ /
3288// \ /
3289// \ /
3290// p.old store
3291// [if (p0)memw(R0+#0)=R2]
3292//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003293// The following set of instructions further explains the scenario where
3294// conditional new-value store becomes invalid when promoted to .new predicate
3295// form.
3296//
3297// { 1) if (p0) r0 = add(r1, r2)
3298// 2) p0 = cmp.eq(r3, #0) }
3299//
3300// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3301// the first two instructions because in instr 1, r0 is conditional on old value
3302// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3303// is not valid for new-value stores.
3304// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3305// from the "Conditional Store" list. Because a predicated new value store
3306// would NOT be promoted to a double dot new store. See diagram below:
3307// This function returns yes for those stores that are predicated but not
3308// yet promoted to predicate dot new instructions.
3309//
3310// +---------------------+
3311// /-----| if (p0) memw(..)=r0 |---------\~
3312// || +---------------------+ ||
3313// promote || /\ /\ || promote
3314// || /||\ /||\ ||
3315// \||/ demote || \||/
3316// \/ || || \/
3317// +-------------------------+ || +-------------------------+
3318// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3319// +-------------------------+ || +-------------------------+
3320// || || ||
3321// || demote \||/
3322// promote || \/ NOT possible
3323// || || /\~
3324// \||/ || /||\~
3325// \/ || ||
3326// +-----------------------------+
3327// | if (p0.new) memw(..)=r0.new |
3328// +-----------------------------+
3329// Double Dot New Store
3330//
3331// Returns the most basic instruction for the .new predicated instructions and
3332// new-value stores.
3333// For example, all of the following instructions will be converted back to the
3334// same instruction:
3335// 1) if (p0.new) memw(R0+#0) = R1.new --->
3336// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3337// 3) if (p0.new) memw(R0+#0) = R1 --->
3338//
3339// To understand the translation of instruction 1 to its original form, consider
3340// a packet with 3 instructions.
3341// { p0 = cmp.eq(R0,R1)
3342// if (p0.new) R2 = add(R3, R4)
3343// R5 = add (R3, R1)
3344// }
3345// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3346//
3347// This instruction can be part of the previous packet only if both p0 and R2
3348// are promoted to .new values. This promotion happens in steps, first
3349// predicate register is promoted to .new and in the next iteration R2 is
3350// promoted. Therefore, in case of dependence check failure (due to R5) during
3351// next iteration, it should be converted back to its most basic form.
3352
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003353// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003354int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3355 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003356 if (NVOpcode >= 0) // Valid new-value store instruction.
3357 return NVOpcode;
3358
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003359 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003360 default:
3361 llvm::report_fatal_error(std::string("Unknown .new type: ") +
3362 std::to_string(MI.getOpcode()).c_str());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003363 case Hexagon::S4_storerb_ur:
3364 return Hexagon::S4_storerbnew_ur;
3365
3366 case Hexagon::S2_storerb_pci:
3367 return Hexagon::S2_storerb_pci;
3368
3369 case Hexagon::S2_storeri_pci:
3370 return Hexagon::S2_storeri_pci;
3371
3372 case Hexagon::S2_storerh_pci:
3373 return Hexagon::S2_storerh_pci;
3374
3375 case Hexagon::S2_storerd_pci:
3376 return Hexagon::S2_storerd_pci;
3377
3378 case Hexagon::S2_storerf_pci:
3379 return Hexagon::S2_storerf_pci;
3380
3381 case Hexagon::V6_vS32b_ai:
3382 return Hexagon::V6_vS32b_new_ai;
3383
3384 case Hexagon::V6_vS32b_pi:
3385 return Hexagon::V6_vS32b_new_pi;
3386
3387 // 128B
3388 case Hexagon::V6_vS32b_ai_128B:
3389 return Hexagon::V6_vS32b_new_ai_128B;
3390
3391 case Hexagon::V6_vS32b_pi_128B:
3392 return Hexagon::V6_vS32b_new_pi_128B;
3393 }
3394 return 0;
3395}
3396
3397// Returns the opcode to use when converting MI, which is a conditional jump,
3398// into a conditional instruction which uses the .new value of the predicate.
3399// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003400// If MBPI is null, all edges will be treated as equally likely for the
3401// purposes of establishing a predication hint.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003402int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003403 const MachineBranchProbabilityInfo *MBPI) const {
3404 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003405 const MachineBasicBlock *Src = MI.getParent();
3406 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003407 bool Taken = false;
3408 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003409
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003410 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3411 const MachineBasicBlock *Dst) {
3412 if (MBPI)
3413 return MBPI->getEdgeProbability(Src, Dst);
3414 return BranchProbability(1, Src->succ_size());
3415 };
3416
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003417 if (BrTarget.isMBB()) {
3418 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003419 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003420 } else {
3421 // The branch target is not a basic block (most likely a function).
3422 // Since BPI only gives probabilities for targets that are basic blocks,
3423 // try to identify another target of this branch (potentially a fall-
3424 // -through) and check the probability of that target.
3425 //
3426 // The only handled branch combinations are:
3427 // - one conditional branch,
3428 // - one conditional branch followed by one unconditional branch.
3429 // Otherwise, assume not-taken.
3430 assert(MI.isConditionalBranch());
3431 const MachineBasicBlock &B = *MI.getParent();
3432 bool SawCond = false, Bad = false;
3433 for (const MachineInstr &I : B) {
3434 if (!I.isBranch())
3435 continue;
3436 if (I.isConditionalBranch()) {
3437 SawCond = true;
3438 if (&I != &MI) {
3439 Bad = true;
3440 break;
3441 }
3442 }
3443 if (I.isUnconditionalBranch() && !SawCond) {
3444 Bad = true;
3445 break;
3446 }
3447 }
3448 if (!Bad) {
3449 MachineBasicBlock::const_instr_iterator It(MI);
3450 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3451 if (NextIt == B.instr_end()) {
3452 // If this branch is the last, look for the fall-through block.
3453 for (const MachineBasicBlock *SB : B.successors()) {
3454 if (!B.isLayoutSuccessor(SB))
3455 continue;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003456 Taken = getEdgeProbability(Src, SB) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003457 break;
3458 }
3459 } else {
3460 assert(NextIt->isUnconditionalBranch());
3461 // Find the first MBB operand and assume it's the target.
3462 const MachineBasicBlock *BT = nullptr;
3463 for (const MachineOperand &Op : NextIt->operands()) {
3464 if (!Op.isMBB())
3465 continue;
3466 BT = Op.getMBB();
3467 break;
3468 }
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003469 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003470 }
3471 } // if (!Bad)
3472 }
3473
3474 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003475
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003476 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003477 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003478 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003479 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003480 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003481
3482 default:
3483 llvm_unreachable("Unexpected jump instruction.");
3484 }
3485}
3486
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003487// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003488int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003489 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003490 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003491 // Condtional Jumps
3492 case Hexagon::J2_jumpt:
3493 case Hexagon::J2_jumpf:
3494 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003495 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003496
3497 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3498 if (NewOpcode >= 0)
3499 return NewOpcode;
Krzysztof Parzyszek066e8b52017-06-02 14:07:06 +00003500 return 0;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003501}
3502
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003503int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003504 const MachineFunction &MF = *MI.getParent()->getParent();
3505 const HexagonSubtarget &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003506 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003507 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3508 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003509 // All Hexagon architectures have prediction bits on dot-new branches,
3510 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3511 // to pick the right opcode when converting back to dot-old.
3512 if (!HST.getFeatureBits()[Hexagon::ArchV60]) {
3513 switch (NewOp) {
3514 case Hexagon::J2_jumptpt:
3515 NewOp = Hexagon::J2_jumpt;
3516 break;
3517 case Hexagon::J2_jumpfpt:
3518 NewOp = Hexagon::J2_jumpf;
3519 break;
3520 case Hexagon::J2_jumprtpt:
3521 NewOp = Hexagon::J2_jumprt;
3522 break;
3523 case Hexagon::J2_jumprfpt:
3524 NewOp = Hexagon::J2_jumprf;
3525 break;
3526 }
3527 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003528 assert(NewOp >= 0 &&
3529 "Couldn't change predicate new instruction to its old form.");
3530 }
3531
3532 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3533 NewOp = Hexagon::getNonNVStore(NewOp);
3534 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3535 }
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003536
3537 if (HST.hasV60TOps())
3538 return NewOp;
3539
3540 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3541 switch (NewOp) {
3542 case Hexagon::J2_jumpfpt:
3543 return Hexagon::J2_jumpf;
3544 case Hexagon::J2_jumptpt:
3545 return Hexagon::J2_jumpt;
3546 case Hexagon::J2_jumprfpt:
3547 return Hexagon::J2_jumprf;
3548 case Hexagon::J2_jumprtpt:
3549 return Hexagon::J2_jumprt;
3550 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003551 return NewOp;
3552}
3553
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003554// See if instruction could potentially be a duplex candidate.
3555// If so, return its group. Zero otherwise.
3556HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003557 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003558 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3559 auto &HRI = getRegisterInfo();
3560
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003561 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003562 default:
3563 return HexagonII::HSIG_None;
3564 //
3565 // Group L1:
3566 //
3567 // Rd = memw(Rs+#u4:2)
3568 // Rd = memub(Rs+#u4:0)
3569 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003570 DstReg = MI.getOperand(0).getReg();
3571 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003572 // Special case this one from Group L2.
3573 // Rd = memw(r29+#u5:2)
3574 if (isIntRegForSubInst(DstReg)) {
3575 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3576 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003577 MI.getOperand(2).isImm() &&
3578 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003579 return HexagonII::HSIG_L2;
3580 // Rd = memw(Rs+#u4:2)
3581 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003582 (MI.getOperand(2).isImm() &&
3583 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003584 return HexagonII::HSIG_L1;
3585 }
3586 break;
3587 case Hexagon::L2_loadrub_io:
3588 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003589 DstReg = MI.getOperand(0).getReg();
3590 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003591 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003592 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003593 return HexagonII::HSIG_L1;
3594 break;
3595 //
3596 // Group L2:
3597 //
3598 // Rd = memh/memuh(Rs+#u3:1)
3599 // Rd = memb(Rs+#u3:0)
3600 // Rd = memw(r29+#u5:2) - Handled above.
3601 // Rdd = memd(r29+#u5:3)
3602 // deallocframe
3603 // [if ([!]p0[.new])] dealloc_return
3604 // [if ([!]p0[.new])] jumpr r31
3605 case Hexagon::L2_loadrh_io:
3606 case Hexagon::L2_loadruh_io:
3607 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003608 DstReg = MI.getOperand(0).getReg();
3609 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003610 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003611 MI.getOperand(2).isImm() &&
3612 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003613 return HexagonII::HSIG_L2;
3614 break;
3615 case Hexagon::L2_loadrb_io:
3616 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003617 DstReg = MI.getOperand(0).getReg();
3618 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003619 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003620 MI.getOperand(2).isImm() &&
3621 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003622 return HexagonII::HSIG_L2;
3623 break;
3624 case Hexagon::L2_loadrd_io:
3625 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003626 DstReg = MI.getOperand(0).getReg();
3627 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003628 if (isDblRegForSubInst(DstReg, HRI) &&
3629 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3630 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003631 MI.getOperand(2).isImm() &&
3632 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003633 return HexagonII::HSIG_L2;
3634 break;
3635 // dealloc_return is not documented in Hexagon Manual, but marked
3636 // with A_SUBINSN attribute in iset_v4classic.py.
3637 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003638 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003639 case Hexagon::L4_return:
3640 case Hexagon::L2_deallocframe:
3641 return HexagonII::HSIG_L2;
3642 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003643 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003644 // jumpr r31
3645 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003646 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003647 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3648 return HexagonII::HSIG_L2;
3649 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003650 case Hexagon::PS_jmprett:
3651 case Hexagon::PS_jmpretf:
3652 case Hexagon::PS_jmprettnewpt:
3653 case Hexagon::PS_jmpretfnewpt:
3654 case Hexagon::PS_jmprettnew:
3655 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003656 DstReg = MI.getOperand(1).getReg();
3657 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003658 // [if ([!]p0[.new])] jumpr r31
3659 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3660 (Hexagon::P0 == SrcReg)) &&
3661 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3662 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003663 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003664 case Hexagon::L4_return_t :
3665 case Hexagon::L4_return_f :
3666 case Hexagon::L4_return_tnew_pnt :
3667 case Hexagon::L4_return_fnew_pnt :
3668 case Hexagon::L4_return_tnew_pt :
3669 case Hexagon::L4_return_fnew_pt :
3670 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003671 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003672 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3673 return HexagonII::HSIG_L2;
3674 break;
3675 //
3676 // Group S1:
3677 //
3678 // memw(Rs+#u4:2) = Rt
3679 // memb(Rs+#u4:0) = Rt
3680 case Hexagon::S2_storeri_io:
3681 // Special case this one from Group S2.
3682 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003683 Src1Reg = MI.getOperand(0).getReg();
3684 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003685 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3686 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003687 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3688 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003689 return HexagonII::HSIG_S2;
3690 // memw(Rs+#u4:2) = Rt
3691 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003692 MI.getOperand(1).isImm() &&
3693 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003694 return HexagonII::HSIG_S1;
3695 break;
3696 case Hexagon::S2_storerb_io:
3697 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003698 Src1Reg = MI.getOperand(0).getReg();
3699 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003700 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003701 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003702 return HexagonII::HSIG_S1;
3703 break;
3704 //
3705 // Group S2:
3706 //
3707 // memh(Rs+#u3:1) = Rt
3708 // memw(r29+#u5:2) = Rt
3709 // memd(r29+#s6:3) = Rtt
3710 // memw(Rs+#u4:2) = #U1
3711 // memb(Rs+#u4) = #U1
3712 // allocframe(#u5:3)
3713 case Hexagon::S2_storerh_io:
3714 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003715 Src1Reg = MI.getOperand(0).getReg();
3716 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003717 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003718 MI.getOperand(1).isImm() &&
3719 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003720 return HexagonII::HSIG_S1;
3721 break;
3722 case Hexagon::S2_storerd_io:
3723 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003724 Src1Reg = MI.getOperand(0).getReg();
3725 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003726 if (isDblRegForSubInst(Src2Reg, HRI) &&
3727 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003728 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3729 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003730 return HexagonII::HSIG_S2;
3731 break;
3732 case Hexagon::S4_storeiri_io:
3733 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003734 Src1Reg = MI.getOperand(0).getReg();
3735 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3736 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3737 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003738 return HexagonII::HSIG_S2;
3739 break;
3740 case Hexagon::S4_storeirb_io:
3741 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003742 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003743 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003744 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3745 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003746 return HexagonII::HSIG_S2;
3747 break;
3748 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003749 if (MI.getOperand(0).isImm() &&
3750 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003751 return HexagonII::HSIG_S1;
3752 break;
3753 //
3754 // Group A:
3755 //
3756 // Rx = add(Rx,#s7)
3757 // Rd = Rs
3758 // Rd = #u6
3759 // Rd = #-1
3760 // if ([!]P0[.new]) Rd = #0
3761 // Rd = add(r29,#u6:2)
3762 // Rx = add(Rx,Rs)
3763 // P0 = cmp.eq(Rs,#u2)
3764 // Rdd = combine(#0,Rs)
3765 // Rdd = combine(Rs,#0)
3766 // Rdd = combine(#u2,#U2)
3767 // Rd = add(Rs,#1)
3768 // Rd = add(Rs,#-1)
3769 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3770 // Rd = and(Rs,#1)
3771 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003772 DstReg = MI.getOperand(0).getReg();
3773 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003774 if (isIntRegForSubInst(DstReg)) {
3775 // Rd = add(r29,#u6:2)
3776 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003777 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3778 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003779 return HexagonII::HSIG_A;
3780 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003781 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3782 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003783 return HexagonII::HSIG_A;
3784 // Rd = add(Rs,#1)
3785 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003786 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3787 ((MI.getOperand(2).getImm() == 1) ||
3788 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003789 return HexagonII::HSIG_A;
3790 }
3791 break;
3792 case Hexagon::A2_add:
3793 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003794 DstReg = MI.getOperand(0).getReg();
3795 Src1Reg = MI.getOperand(1).getReg();
3796 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003797 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3798 isIntRegForSubInst(Src2Reg))
3799 return HexagonII::HSIG_A;
3800 break;
3801 case Hexagon::A2_andir:
3802 // Same as zxtb.
3803 // Rd16=and(Rs16,#255)
3804 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003805 DstReg = MI.getOperand(0).getReg();
3806 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003807 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003808 MI.getOperand(2).isImm() &&
3809 ((MI.getOperand(2).getImm() == 1) ||
3810 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003811 return HexagonII::HSIG_A;
3812 break;
3813 case Hexagon::A2_tfr:
3814 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003815 DstReg = MI.getOperand(0).getReg();
3816 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003817 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3818 return HexagonII::HSIG_A;
3819 break;
3820 case Hexagon::A2_tfrsi:
3821 // Rd = #u6
3822 // Do not test for #u6 size since the const is getting extended
3823 // regardless and compound could be formed.
3824 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003825 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003826 if (isIntRegForSubInst(DstReg))
3827 return HexagonII::HSIG_A;
3828 break;
3829 case Hexagon::C2_cmoveit:
3830 case Hexagon::C2_cmovenewit:
3831 case Hexagon::C2_cmoveif:
3832 case Hexagon::C2_cmovenewif:
3833 // if ([!]P0[.new]) Rd = #0
3834 // Actual form:
3835 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003836 DstReg = MI.getOperand(0).getReg();
3837 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003838 if (isIntRegForSubInst(DstReg) &&
3839 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003840 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003841 return HexagonII::HSIG_A;
3842 break;
3843 case Hexagon::C2_cmpeqi:
3844 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003845 DstReg = MI.getOperand(0).getReg();
3846 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003847 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3848 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003849 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003850 return HexagonII::HSIG_A;
3851 break;
3852 case Hexagon::A2_combineii:
3853 case Hexagon::A4_combineii:
3854 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003855 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003856 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003857 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3858 (MI.getOperand(1).isGlobal() &&
3859 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3860 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3861 (MI.getOperand(2).isGlobal() &&
3862 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003863 return HexagonII::HSIG_A;
3864 break;
3865 case Hexagon::A4_combineri:
3866 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003867 DstReg = MI.getOperand(0).getReg();
3868 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003869 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003870 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3871 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003872 return HexagonII::HSIG_A;
3873 break;
3874 case Hexagon::A4_combineir:
3875 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003876 DstReg = MI.getOperand(0).getReg();
3877 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003878 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003879 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3880 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003881 return HexagonII::HSIG_A;
3882 break;
3883 case Hexagon::A2_sxtb:
3884 case Hexagon::A2_sxth:
3885 case Hexagon::A2_zxtb:
3886 case Hexagon::A2_zxth:
3887 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003888 DstReg = MI.getOperand(0).getReg();
3889 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003890 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3891 return HexagonII::HSIG_A;
3892 break;
3893 }
3894
3895 return HexagonII::HSIG_None;
3896}
3897
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003898short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3899 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003900}
3901
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003902unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003903 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003904 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3905 // still have a MinLatency property, which getStageLatency checks.
3906 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003907 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003908
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003909 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003910 return 0;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003911 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
3912}
3913
3914/// getOperandLatency - Compute and return the use operand latency of a given
3915/// pair of def and use.
3916/// In most cases, the static scheduling itinerary was enough to determine the
3917/// operand latency. But it may not be possible for instructions with variable
3918/// number of defs / uses.
3919///
3920/// This is a raw interface to the itinerary that may be directly overriden by
3921/// a target. Use computeOperandLatency to get the best estimate of latency.
3922int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3923 const MachineInstr &DefMI,
3924 unsigned DefIdx,
3925 const MachineInstr &UseMI,
3926 unsigned UseIdx) const {
3927 auto &RI = getRegisterInfo();
3928 // Get DefIdx and UseIdx for super registers.
3929 MachineOperand DefMO = DefMI.getOperand(DefIdx);
3930
3931 if (RI.isPhysicalRegister(DefMO.getReg())) {
3932 if (DefMO.isImplicit()) {
3933 for (MCSuperRegIterator SR(DefMO.getReg(), &RI); SR.isValid(); ++SR) {
3934 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &RI);
3935 if (Idx != -1) {
3936 DefIdx = Idx;
3937 break;
3938 }
3939 }
3940 }
3941
3942 MachineOperand UseMO = UseMI.getOperand(UseIdx);
3943 if (UseMO.isImplicit()) {
3944 for (MCSuperRegIterator SR(UseMO.getReg(), &RI); SR.isValid(); ++SR) {
3945 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &RI);
3946 if (Idx != -1) {
3947 UseIdx = Idx;
3948 break;
3949 }
3950 }
3951 }
3952 }
3953
3954 return TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
3955 UseMI, UseIdx);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003956}
3957
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003958// inverts the predication logic.
3959// p -> NotP
3960// NotP -> P
3961bool HexagonInstrInfo::getInvertedPredSense(
3962 SmallVectorImpl<MachineOperand> &Cond) const {
3963 if (Cond.empty())
3964 return false;
3965 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3966 Cond[0].setImm(Opc);
3967 return true;
3968}
3969
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003970unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3971 int InvPredOpcode;
3972 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3973 : Hexagon::getTruePredOpcode(Opc);
3974 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3975 return InvPredOpcode;
3976
3977 llvm_unreachable("Unexpected predicated instruction");
3978}
3979
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003980// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003981int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
3982 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003983 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3984 & HexagonII::ExtentSignedMask;
3985 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3986 & HexagonII::ExtentBitsMask;
3987
3988 if (isSigned) // if value is signed
3989 return ~(-1U << (bits - 1));
3990 else
3991 return ~(-1U << bits);
3992}
3993
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003994unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003995 using namespace HexagonII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003996 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003997 unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
3998 unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
3999 if (Size != 0)
4000 return Size;
4001
4002 // Handle vector access sizes.
4003 switch (S) {
4004 case HexagonII::Vector64Access:
4005 return 64;
4006 case HexagonII::Vector128Access:
4007 return 128;
4008 default:
4009 llvm_unreachable("Unexpected instruction");
4010 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004011}
4012
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004013// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004014int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4015 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004016 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4017 & HexagonII::ExtentSignedMask;
4018 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4019 & HexagonII::ExtentBitsMask;
4020
4021 if (isSigned) // if value is signed
4022 return -1U << (bits - 1);
4023 else
4024 return 0;
4025}
4026
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004027// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004028short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004029 // Check if the instruction has a register form that uses register in place
4030 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004031 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004032 if (NonExtOpcode >= 0)
4033 return NonExtOpcode;
4034
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004035 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004036 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004037 switch (getAddrMode(MI)) {
4038 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004039 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004040 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004041 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004042 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004043 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004044
Jyotsna Verma84256432013-03-01 17:37:13 +00004045 default:
4046 return -1;
4047 }
4048 }
4049 return -1;
4050}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004051
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004052bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004053 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004054 if (Cond.empty())
4055 return false;
4056 assert(Cond.size() == 2);
4057 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004058 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4059 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004060 }
4061 PredReg = Cond[1].getReg();
4062 PredRegPos = 1;
4063 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4064 PredRegFlags = 0;
4065 if (Cond[1].isImplicit())
4066 PredRegFlags = RegState::Implicit;
4067 if (Cond[1].isUndef())
4068 PredRegFlags |= RegState::Undef;
4069 return true;
4070}
4071
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004072short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4073 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004074}
4075
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004076short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4077 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004078}
4079
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004080// Return the number of bytes required to encode the instruction.
4081// Hexagon instructions are fixed length, 4 bytes, unless they
4082// use a constant extender, which requires another 4 bytes.
4083// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004084unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4085 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004086 return 0;
4087
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004088 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004089 if (!Size)
4090 // Assume the default insn size in case it cannot be determined
4091 // for whatever reason.
4092 Size = HEXAGON_INSTR_SIZE;
4093
4094 if (isConstExtended(MI) || isExtended(MI))
4095 Size += HEXAGON_INSTR_SIZE;
4096
4097 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004098 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4099 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004100 const MachineFunction *MF = MBB.getParent();
4101 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4102
4103 // Count the number of register definitions to find the asm string.
4104 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004105 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004106 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004107 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004108
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004109 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004110 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004111 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004112 Size = getInlineAsmLength(AsmStr, *MAI);
4113 }
4114
4115 return Size;
4116}
4117
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004118uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4119 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004120 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4121}
4122
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004123unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4124 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004125 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004126 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004127
4128 return IS.getUnits();
4129}
4130
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004131// Calculate size of the basic block without debug instructions.
4132unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4133 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4134}
4135
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004136unsigned HexagonInstrInfo::nonDbgBundleSize(
4137 MachineBasicBlock::const_iterator BundleHead) const {
4138 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004139 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004140 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004141 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004142}
4143
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004144/// immediateExtend - Changes the instruction in place to one using an immediate
4145/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004146void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004147 assert((isExtendable(MI)||isConstExtended(MI)) &&
4148 "Instruction must be extendable");
4149 // Find which operand is extendable.
4150 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004151 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004152 // This needs to be something we understand.
4153 assert((MO.isMBB() || MO.isImm()) &&
4154 "Branch with unknown extendable field type");
4155 // Mark given operand as extended.
4156 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4157}
4158
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004159bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004160 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004161 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004162 << NewTarget->getNumber(); MI.dump(););
4163 assert(MI.isBranch());
4164 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4165 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004166 // In general branch target is the last operand,
4167 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004168 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004169 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004170 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4171 MI.getOperand(TargetPos).setMBB(NewTarget);
4172 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004173 NewOpcode = reversePrediction(NewOpcode);
4174 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004175 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004176 return true;
4177}
4178
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004179void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4180 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4181 MachineFunction::iterator A = MF.begin();
4182 MachineBasicBlock &B = *A;
4183 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004184 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004185 MachineInstr *NewMI;
4186
4187 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4188 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004189 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004190 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4191 " Class: " << NewMI->getDesc().getSchedClass());
4192 NewMI->eraseFromParent();
4193 }
4194 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4195}
4196
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004197// inverts the predication logic.
4198// p -> NotP
4199// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004200bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4201 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4202 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004203 return true;
4204}
4205
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004206// Reverse the branch prediction.
4207unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4208 int PredRevOpcode = -1;
4209 if (isPredictedTaken(Opcode))
4210 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4211 else
4212 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4213 assert(PredRevOpcode > 0);
4214 return PredRevOpcode;
4215}
4216
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004217// TODO: Add more rigorous validation.
4218bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4219 const {
4220 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4221}
4222
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004223short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4224 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004225}