blob: c911b041ebd10fe0701f93cb49fbf984edad3601 [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
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014#include "HexagonInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "Hexagon.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonSubtarget.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000020#include "llvm/CodeGen/DFAPacketizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000026#include "llvm/MC/MCAsmInfo.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000027#include "llvm/Support/Debug.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000028#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000029#include "llvm/Support/raw_ostream.h"
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000030#include <cctype>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000031
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032using namespace llvm;
33
Chandler Carruthe96dd892014-04-21 22:55:11 +000034#define DEBUG_TYPE "hexagon-instrinfo"
35
Chandler Carruthd174b722014-04-22 02:03:14 +000036#define GET_INSTRINFO_CTOR_DTOR
37#define GET_INSTRMAP_INFO
38#include "HexagonGenInstrInfo.inc"
39#include "HexagonGenDFAPacketizer.inc"
40
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000041using namespace llvm;
42
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000043cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000044 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
45 "packetization boundary."));
46
47static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
48 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
49
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000050static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
51 cl::Hidden, cl::ZeroOrMore, cl::init(false),
52 cl::desc("Disable schedule adjustment for new value stores."));
53
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000054static cl::opt<bool> EnableTimingClassLatency(
55 "enable-timing-class-latency", cl::Hidden, cl::init(false),
56 cl::desc("Enable timing class latency"));
57
58static cl::opt<bool> EnableALUForwarding(
59 "enable-alu-forwarding", cl::Hidden, cl::init(true),
60 cl::desc("Enable vec alu forwarding"));
61
62static cl::opt<bool> EnableACCForwarding(
63 "enable-acc-forwarding", cl::Hidden, cl::init(true),
64 cl::desc("Enable vec acc forwarding"));
65
66static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
67 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
68
Tony Linthicum1213a7a2011-12-12 21:14:40 +000069///
70/// Constants for Hexagon instructions.
71///
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000072const int Hexagon_MEMV_OFFSET_MAX_128B = 2047; // #s7
73const int Hexagon_MEMV_OFFSET_MIN_128B = -2048; // #s7
74const int Hexagon_MEMV_OFFSET_MAX = 1023; // #s6
75const int Hexagon_MEMV_OFFSET_MIN = -1024; // #s6
Tony Linthicum1213a7a2011-12-12 21:14:40 +000076const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000077const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000078const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000079const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000080const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000081const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000082const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000083const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000085const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000086const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000087const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000089const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000091const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000092const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000093const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000094const int Hexagon_MEMV_AUTOINC_MAX = 192;
95const int Hexagon_MEMV_AUTOINC_MIN = -256;
96const int Hexagon_MEMV_AUTOINC_MAX_128B = 384;
97const int Hexagon_MEMV_AUTOINC_MIN_128B = -512;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000098
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000099// Pin the vtable to this file.
100void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000101
102HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000103 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000104 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105
106
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000107static bool isIntRegForSubInst(unsigned Reg) {
108 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
109 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110}
111
112
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000113static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
114 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_loreg)) &&
115 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_hireg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000116}
117
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000118
119/// Calculate number of instructions excluding the debug instructions.
120static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
121 MachineBasicBlock::const_instr_iterator MIE) {
122 unsigned Count = 0;
123 for (; MIB != MIE; ++MIB) {
124 if (!MIB->isDebugValue())
125 ++Count;
126 }
127 return Count;
128}
129
130
131/// Find the hardware loop instruction used to set-up the specified loop.
132/// On Hexagon, we have two instructions used to set-up the hardware loop
133/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
134/// to indicate the end of a loop.
135static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
136 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000137 int LOOPi;
138 int LOOPr;
139 if (EndLoopOp == Hexagon::ENDLOOP0) {
140 LOOPi = Hexagon::J2_loop0i;
141 LOOPr = Hexagon::J2_loop0r;
142 } else { // EndLoopOp == Hexagon::EndLOOP1
143 LOOPi = Hexagon::J2_loop1i;
144 LOOPr = Hexagon::J2_loop1r;
145 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000146
Brendon Cahoondf43e682015-05-08 16:16:29 +0000147 // The loop set-up instruction will be in a predecessor block
148 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
149 PE = BB->pred_end(); PB != PE; ++PB) {
150 // If this has been visited, already skip it.
151 if (!Visited.insert(*PB).second)
152 continue;
153 if (*PB == BB)
154 continue;
155 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
156 E = (*PB)->instr_rend(); I != E; ++I) {
157 int Opc = I->getOpcode();
158 if (Opc == LOOPi || Opc == LOOPr)
159 return &*I;
160 // We've reached a different loop, which means the loop0 has been removed.
161 if (Opc == EndLoopOp)
162 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000163 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000164 // Check the predecessors for the LOOP instruction.
165 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
166 if (loop)
167 return loop;
168 }
169 return 0;
170}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000171
Brendon Cahoondf43e682015-05-08 16:16:29 +0000172
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000173/// Gather register def/uses from MI.
174/// This treats possible (predicated) defs as actually happening ones
175/// (conservatively).
176static inline void parseOperands(const MachineInstr *MI,
177 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
178 Defs.clear();
179 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000180
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000181 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
182 const MachineOperand &MO = MI->getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000183
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000184 if (!MO.isReg())
185 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000186
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000187 unsigned Reg = MO.getReg();
188 if (!Reg)
189 continue;
190
191 if (MO.isUse())
192 Uses.push_back(MO.getReg());
193
194 if (MO.isDef())
195 Defs.push_back(MO.getReg());
196 }
197}
198
199
200// Position dependent, so check twice for swap.
201static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
202 switch (Ga) {
203 case HexagonII::HSIG_None:
204 default:
205 return false;
206 case HexagonII::HSIG_L1:
207 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
208 case HexagonII::HSIG_L2:
209 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
210 Gb == HexagonII::HSIG_A);
211 case HexagonII::HSIG_S1:
212 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
213 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
214 case HexagonII::HSIG_S2:
215 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
216 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
217 Gb == HexagonII::HSIG_A);
218 case HexagonII::HSIG_A:
219 return (Gb == HexagonII::HSIG_A);
220 case HexagonII::HSIG_Compound:
221 return (Gb == HexagonII::HSIG_Compound);
222 }
223 return false;
224}
225
226
227
228/// isLoadFromStackSlot - If the specified machine instruction is a direct
229/// load from a stack slot, return the virtual or physical register number of
230/// the destination along with the FrameIndex of the loaded stack slot. If
231/// not, return 0. This predicate must return 0 if the instruction has
232/// any side effects other than loading from the stack slot.
233unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
234 int &FrameIndex) const {
235 switch (MI->getOpcode()) {
236 default: break;
237 case Hexagon::L2_loadri_io:
238 case Hexagon::L2_loadrd_io:
239 case Hexagon::L2_loadrh_io:
240 case Hexagon::L2_loadrb_io:
241 case Hexagon::L2_loadrub_io:
242 if (MI->getOperand(2).isFI() &&
243 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
244 FrameIndex = MI->getOperand(2).getIndex();
245 return MI->getOperand(0).getReg();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000246 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000247 break;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000248 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000249 return 0;
250}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000251
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000252
253/// isStoreToStackSlot - If the specified machine instruction is a direct
254/// store to a stack slot, return the virtual or physical register number of
255/// the source reg along with the FrameIndex of the loaded stack slot. If
256/// not, return 0. This predicate must return 0 if the instruction has
257/// any side effects other than storing to the stack slot.
258unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
259 int &FrameIndex) const {
260 switch (MI->getOpcode()) {
261 default: break;
262 case Hexagon::S2_storeri_io:
263 case Hexagon::S2_storerd_io:
264 case Hexagon::S2_storerh_io:
265 case Hexagon::S2_storerb_io:
266 if (MI->getOperand(2).isFI() &&
267 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
268 FrameIndex = MI->getOperand(0).getIndex();
269 return MI->getOperand(2).getReg();
270 }
271 break;
272 }
273 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000274}
275
276
Brendon Cahoondf43e682015-05-08 16:16:29 +0000277/// This function can analyze one/two way branching only and should (mostly) be
278/// called by target independent side.
279/// First entry is always the opcode of the branching instruction, except when
280/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
281/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
282/// e.g. Jump_c p will have
283/// Cond[0] = Jump_c
284/// Cond[1] = p
285/// HW-loop ENDLOOP:
286/// Cond[0] = ENDLOOP
287/// Cond[1] = MBB
288/// New value jump:
289/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
290/// Cond[1] = R
291/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000292///
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000293bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
294 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000295 MachineBasicBlock *&FBB,
296 SmallVectorImpl<MachineOperand> &Cond,
297 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000298 TBB = nullptr;
299 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000300 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000301
302 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000303 MachineBasicBlock::instr_iterator I = MBB.instr_end();
304 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000305 return false;
306
307 // A basic block may looks like this:
308 //
309 // [ insn
310 // EH_LABEL
311 // insn
312 // insn
313 // insn
314 // EH_LABEL
315 // insn ]
316 //
317 // It has two succs but does not have a terminator
318 // Don't know how to handle it.
319 do {
320 --I;
321 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000322 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000323 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000324 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000325
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000326 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000327 --I;
328
329 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000330 if (I == MBB.instr_begin())
331 return false;
332 --I;
333 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000334
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000335 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
336 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000337 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000338 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000339 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
340 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
341 I->eraseFromParent();
342 I = MBB.instr_end();
343 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000344 return false;
345 --I;
346 }
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000347 if (!isUnpredicatedTerminator(&*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000348 return false;
349
350 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000351 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000352 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000353 // Find one more terminator if present.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000354 for (;;) {
355 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000356 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000357 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000358 else
359 // This is a third branch.
360 return true;
361 }
362 if (I == MBB.instr_begin())
363 break;
364 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000365 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000366
367 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000368 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
369 // If the branch target is not a basic block, it could be a tail call.
370 // (It is, if the target is a function.)
371 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
372 return true;
373 if (SecLastOpcode == Hexagon::J2_jump &&
374 !SecondLastInst->getOperand(0).isMBB())
375 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000376
377 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000378 bool LastOpcodeHasNVJump = isNewValueJump(LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000379
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000380 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
381 return true;
382
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000383 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000384 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000385 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000386 TBB = LastInst->getOperand(0).getMBB();
387 return false;
388 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000389 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000390 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000391 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000392 Cond.push_back(LastInst->getOperand(0));
393 return false;
394 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000395 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000397 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 Cond.push_back(LastInst->getOperand(0));
399 return false;
400 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000401 // Only supporting rr/ri versions of new-value jumps.
402 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
403 TBB = LastInst->getOperand(2).getMBB();
404 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
405 Cond.push_back(LastInst->getOperand(0));
406 Cond.push_back(LastInst->getOperand(1));
407 return false;
408 }
409 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
410 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000411 // Otherwise, don't know what this is.
412 return true;
413 }
414
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000415 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000416 bool SecLastOpcodeHasNVJump = isNewValueJump(SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000417 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000418 if (!SecondLastInst->getOperand(1).isMBB())
419 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000420 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000421 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000422 Cond.push_back(SecondLastInst->getOperand(0));
423 FBB = LastInst->getOperand(0).getMBB();
424 return false;
425 }
426
Brendon Cahoondf43e682015-05-08 16:16:29 +0000427 // Only supporting rr/ri versions of new-value jumps.
428 if (SecLastOpcodeHasNVJump &&
429 (SecondLastInst->getNumExplicitOperands() == 3) &&
430 (LastOpcode == Hexagon::J2_jump)) {
431 TBB = SecondLastInst->getOperand(2).getMBB();
432 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
433 Cond.push_back(SecondLastInst->getOperand(0));
434 Cond.push_back(SecondLastInst->getOperand(1));
435 FBB = LastInst->getOperand(0).getMBB();
436 return false;
437 }
438
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000439 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
440 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000441 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000442 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000443 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000444 if (AllowModify)
445 I->eraseFromParent();
446 return false;
447 }
448
Brendon Cahoondf43e682015-05-08 16:16:29 +0000449 // If the block ends with an ENDLOOP, and J2_jump, handle it.
450 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000451 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000452 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000453 Cond.push_back(SecondLastInst->getOperand(0));
454 FBB = LastInst->getOperand(0).getMBB();
455 return false;
456 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000457 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
458 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 // Otherwise, can't handle this.
460 return true;
461}
462
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000463
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000464unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000465 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000466 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000467 unsigned Count = 0;
468 while (I != MBB.begin()) {
469 --I;
470 if (I->isDebugValue())
471 continue;
472 // Only removing branches from end of MBB.
473 if (!I->isBranch())
474 return Count;
475 if (Count && (I->getOpcode() == Hexagon::J2_jump))
476 llvm_unreachable("Malformed basic block: unconditional branch not last");
477 MBB.erase(&MBB.back());
478 I = MBB.end();
479 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000480 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000481 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000482}
483
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000484
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000485unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
486 MachineBasicBlock *TBB, MachineBasicBlock *FBB,
487 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
488 unsigned BOpc = Hexagon::J2_jump;
489 unsigned BccOpc = Hexagon::J2_jumpt;
490 assert(validateBranchCond(Cond) && "Invalid branching condition");
491 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
492
493 // Check if ReverseBranchCondition has asked to reverse this branch
494 // If we want to reverse the branch an odd number of times, we want
495 // J2_jumpf.
496 if (!Cond.empty() && Cond[0].isImm())
497 BccOpc = Cond[0].getImm();
498
499 if (!FBB) {
500 if (Cond.empty()) {
501 // Due to a bug in TailMerging/CFG Optimization, we need to add a
502 // special case handling of a predicated jump followed by an
503 // unconditional jump. If not, Tail Merging and CFG Optimization go
504 // into an infinite loop.
505 MachineBasicBlock *NewTBB, *NewFBB;
506 SmallVector<MachineOperand, 4> Cond;
507 MachineInstr *Term = MBB.getFirstTerminator();
508 if (Term != MBB.end() && isPredicated(Term) &&
509 !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond, false)) {
510 MachineBasicBlock *NextBB = &*++MBB.getIterator();
511 if (NewTBB == NextBB) {
512 ReverseBranchCondition(Cond);
513 RemoveBranch(MBB);
514 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
515 }
516 }
517 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
518 } else if (isEndLoopN(Cond[0].getImm())) {
519 int EndLoopOp = Cond[0].getImm();
520 assert(Cond[1].isMBB());
521 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
522 // Check for it, and change the BB target if needed.
523 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
524 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
525 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
526 Loop->getOperand(0).setMBB(TBB);
527 // Add the ENDLOOP after the finding the LOOP0.
528 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
529 } else if (isNewValueJump(Cond[0].getImm())) {
530 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
531 // New value jump
532 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
533 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
534 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
535 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
536 if (Cond[2].isReg()) {
537 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
538 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
539 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
540 } else if(Cond[2].isImm()) {
541 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
542 addImm(Cond[2].getImm()).addMBB(TBB);
543 } else
544 llvm_unreachable("Invalid condition for branching");
545 } else {
546 assert((Cond.size() == 2) && "Malformed cond vector");
547 const MachineOperand &RO = Cond[1];
548 unsigned Flags = getUndefRegState(RO.isUndef());
549 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
550 }
551 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000552 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000553 assert((!Cond.empty()) &&
554 "Cond. cannot be empty when multiple branchings are required");
555 assert((!isNewValueJump(Cond[0].getImm())) &&
556 "NV-jump cannot be inserted with another branch");
557 // Special case for hardware loops. The condition is a basic block.
558 if (isEndLoopN(Cond[0].getImm())) {
559 int EndLoopOp = Cond[0].getImm();
560 assert(Cond[1].isMBB());
561 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
562 // Check for it, and change the BB target if needed.
563 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
564 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
565 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
566 Loop->getOperand(0).setMBB(TBB);
567 // Add the ENDLOOP after the finding the LOOP0.
568 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
569 } else {
570 const MachineOperand &RO = Cond[1];
571 unsigned Flags = getUndefRegState(RO.isUndef());
572 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000573 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000574 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000575
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000576 return 2;
577}
578
579
580bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
581 unsigned NumCycles, unsigned ExtraPredCycles,
582 BranchProbability Probability) const {
583 return nonDbgBBSize(&MBB) <= 3;
584}
585
586
587bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
588 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
589 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
590 const {
591 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
592}
593
594
595bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
596 unsigned NumInstrs, BranchProbability Probability) const {
597 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000598}
599
600
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000601void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000602 MachineBasicBlock::iterator I, DebugLoc DL, unsigned DestReg,
603 unsigned SrcReg, bool KillSrc) const {
604 auto &HRI = getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000605 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000606 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000607 return;
608 }
609 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000610 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000611 return;
612 }
613 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
614 // Map Pd = Ps to Pd = or(Ps, Ps).
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000615 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000616 DestReg).addReg(SrcReg).addReg(SrcReg);
617 return;
618 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000619 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
620 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000621 // We can have an overlap between single and double reg: r1:0 = r0.
622 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
623 // r1:0 = r0
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000624 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000625 Hexagon::subreg_hireg))).addImm(0);
626 } else {
627 // r1:0 = r1 or no overlap.
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000628 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000629 Hexagon::subreg_loreg))).addReg(SrcReg);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000630 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000631 Hexagon::subreg_hireg))).addImm(0);
632 }
633 return;
634 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000635 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000636 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000637 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000638 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000639 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000640 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
641 Hexagon::IntRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000642 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000643 addReg(SrcReg, getKillRegState(KillSrc));
644 return;
645 }
646 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
647 Hexagon::PredRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000648 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000649 addReg(SrcReg, getKillRegState(KillSrc));
650 return;
651 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000652 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
653 Hexagon::IntRegsRegClass.contains(DestReg)) {
654 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
655 addReg(SrcReg, getKillRegState(KillSrc));
656 return;
657 }
658 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
659 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
660 addReg(SrcReg, getKillRegState(KillSrc));
661 return;
662 }
663 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
664 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg).
665 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg),
666 getKillRegState(KillSrc)).
667 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg),
668 getKillRegState(KillSrc));
669 return;
670 }
671 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
672 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg).
673 addReg(SrcReg).
674 addReg(SrcReg, getKillRegState(KillSrc));
675 return;
676 }
677 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
678 Hexagon::VectorRegsRegClass.contains(DestReg)) {
679 llvm_unreachable("Unimplemented pred to vec");
680 return;
681 }
682 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
683 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
684 llvm_unreachable("Unimplemented vec to pred");
685 return;
686 }
687 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
688 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and),
689 HRI.getSubReg(DestReg, Hexagon::subreg_hireg)).
690 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg),
691 getKillRegState(KillSrc));
692 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and),
693 HRI.getSubReg(DestReg, Hexagon::subreg_loreg)).
694 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg),
695 getKillRegState(KillSrc));
696 return;
697 }
Sirish Pande30804c22012-02-15 18:52:27 +0000698
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000699#ifndef NDEBUG
700 // Show the invalid registers to ease debugging.
701 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
702 << ": " << PrintReg(DestReg, &HRI)
703 << " = " << PrintReg(SrcReg, &HRI) << '\n';
704#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000705 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000706}
707
708
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000709void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
710 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
711 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000712 DebugLoc DL = MBB.findDebugLoc(I);
713 MachineFunction &MF = *MBB.getParent();
714 MachineFrameInfo &MFI = *MF.getFrameInfo();
715 unsigned Align = MFI.getObjectAlignment(FI);
716
Alex Lorenze40c8a22015-08-11 23:09:45 +0000717 MachineMemOperand *MMO = MF.getMachineMemOperand(
718 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
719 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000720
Craig Topperc7242e02012-04-20 07:30:17 +0000721 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000722 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000723 .addFrameIndex(FI).addImm(0)
724 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000725 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000726 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000727 .addFrameIndex(FI).addImm(0)
728 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000729 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000730 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
731 .addFrameIndex(FI).addImm(0)
732 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
733 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000734 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000735 }
736}
737
738
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000739void HexagonInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
740 MachineBasicBlock::iterator I, unsigned DestReg, int FI,
741 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000742 DebugLoc DL = MBB.findDebugLoc(I);
743 MachineFunction &MF = *MBB.getParent();
744 MachineFrameInfo &MFI = *MF.getFrameInfo();
745 unsigned Align = MFI.getObjectAlignment(FI);
746
Alex Lorenze40c8a22015-08-11 23:09:45 +0000747 MachineMemOperand *MMO = MF.getMachineMemOperand(
748 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
749 MFI.getObjectSize(FI), Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000750 if (RC == &Hexagon::IntRegsRegClass) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000751 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000753 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000754 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000755 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000756 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000757 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
758 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
759 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000760 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000761 }
762}
763
764
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000765/// expandPostRAPseudo - This function is called for all pseudo instructions
766/// that remain after register allocation. Many pseudo instructions are
767/// created to help register allocation. This is the place to convert them
768/// into real instructions. The target can edit MI in place, or it can insert
769/// new instructions and erase MI. The function should return true if
770/// anything was changed.
771bool HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI)
772 const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000773 const HexagonRegisterInfo &HRI = getRegisterInfo();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000774 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000775 MachineBasicBlock &MBB = *MI->getParent();
776 DebugLoc DL = MI->getDebugLoc();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000777 unsigned Opc = MI->getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000778 const unsigned VecOffset = 1;
779 bool Is128B = false;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000780
781 switch (Opc) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000782 case Hexagon::ALIGNA:
783 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI->getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000784 .addReg(HRI.getFrameRegister())
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000785 .addImm(-MI->getOperand(1).getImm());
786 MBB.erase(MI);
787 return true;
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000788 case Hexagon::HEXAGON_V6_vassignp_128B:
789 case Hexagon::HEXAGON_V6_vassignp: {
790 unsigned SrcReg = MI->getOperand(1).getReg();
791 unsigned DstReg = MI->getOperand(0).getReg();
792 if (SrcReg != DstReg)
793 copyPhysReg(MBB, MI, DL, DstReg, SrcReg, MI->getOperand(1).isKill());
794 MBB.erase(MI);
795 return true;
796 }
797 case Hexagon::HEXAGON_V6_lo_128B:
798 case Hexagon::HEXAGON_V6_lo: {
799 unsigned SrcReg = MI->getOperand(1).getReg();
800 unsigned DstReg = MI->getOperand(0).getReg();
801 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
802 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI->getOperand(1).isKill());
803 MBB.erase(MI);
804 MRI.clearKillFlags(SrcSubLo);
805 return true;
806 }
807 case Hexagon::HEXAGON_V6_hi_128B:
808 case Hexagon::HEXAGON_V6_hi: {
809 unsigned SrcReg = MI->getOperand(1).getReg();
810 unsigned DstReg = MI->getOperand(0).getReg();
811 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
812 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI->getOperand(1).isKill());
813 MBB.erase(MI);
814 MRI.clearKillFlags(SrcSubHi);
815 return true;
816 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000817 case Hexagon::STrivv_indexed_128B:
818 Is128B = true;
819 case Hexagon::STrivv_indexed: {
820 unsigned SrcReg = MI->getOperand(2).getReg();
821 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
822 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
823 unsigned NewOpcd = Is128B ? Hexagon::V6_vS32b_ai_128B
824 : Hexagon::V6_vS32b_ai;
825 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
826 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpcd))
827 .addOperand(MI->getOperand(0))
828 .addImm(MI->getOperand(1).getImm())
829 .addReg(SrcSubLo)
830 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
831 MI1New->getOperand(0).setIsKill(false);
832 BuildMI(MBB, MI, DL, get(NewOpcd))
833 .addOperand(MI->getOperand(0))
834 // The Vectors are indexed in multiples of vector size.
835 .addImm(MI->getOperand(1).getImm()+Offset)
836 .addReg(SrcSubHi)
837 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
838 MBB.erase(MI);
839 return true;
840 }
841 case Hexagon::LDrivv_pseudo_V6_128B:
842 case Hexagon::LDrivv_indexed_128B:
843 Is128B = true;
844 case Hexagon::LDrivv_pseudo_V6:
845 case Hexagon::LDrivv_indexed: {
846 unsigned NewOpcd = Is128B ? Hexagon::V6_vL32b_ai_128B
847 : Hexagon::V6_vL32b_ai;
848 unsigned DstReg = MI->getOperand(0).getReg();
849 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
850 MachineInstr *MI1New =
851 BuildMI(MBB, MI, DL, get(NewOpcd),
852 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
853 .addOperand(MI->getOperand(1))
854 .addImm(MI->getOperand(2).getImm());
855 MI1New->getOperand(1).setIsKill(false);
856 BuildMI(MBB, MI, DL, get(NewOpcd),
857 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
858 .addOperand(MI->getOperand(1))
859 // The Vectors are indexed in multiples of vector size.
860 .addImm(MI->getOperand(2).getImm() + Offset)
861 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
862 MBB.erase(MI);
863 return true;
864 }
865 case Hexagon::LDriv_pseudo_V6_128B:
866 Is128B = true;
867 case Hexagon::LDriv_pseudo_V6: {
868 unsigned DstReg = MI->getOperand(0).getReg();
869 unsigned NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
870 : Hexagon::V6_vL32b_ai;
871 int32_t Off = MI->getOperand(2).getImm();
872 int32_t Idx = Off;
873 BuildMI(MBB, MI, DL, get(NewOpc), DstReg)
874 .addOperand(MI->getOperand(1))
875 .addImm(Idx)
876 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
877 MBB.erase(MI);
878 return true;
879 }
880 case Hexagon::STriv_pseudo_V6_128B:
881 Is128B = true;
882 case Hexagon::STriv_pseudo_V6: {
883 unsigned NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
884 : Hexagon::V6_vS32b_ai;
885 int32_t Off = MI->getOperand(1).getImm();
886 int32_t Idx = Is128B ? (Off >> 7) : (Off >> 6);
887 BuildMI(MBB, MI, DL, get(NewOpc))
888 .addOperand(MI->getOperand(0))
889 .addImm(Idx)
890 .addOperand(MI->getOperand(2))
891 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
892 MBB.erase(MI);
893 return true;
894 }
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000895 case Hexagon::TFR_PdTrue: {
896 unsigned Reg = MI->getOperand(0).getReg();
897 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
898 .addReg(Reg, RegState::Undef)
899 .addReg(Reg, RegState::Undef);
900 MBB.erase(MI);
901 return true;
902 }
903 case Hexagon::TFR_PdFalse: {
904 unsigned Reg = MI->getOperand(0).getReg();
905 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
906 .addReg(Reg, RegState::Undef)
907 .addReg(Reg, RegState::Undef);
908 MBB.erase(MI);
909 return true;
910 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000911 case Hexagon::VMULW: {
912 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
913 unsigned DstReg = MI->getOperand(0).getReg();
914 unsigned Src1Reg = MI->getOperand(1).getReg();
915 unsigned Src2Reg = MI->getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000916 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
917 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
918 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
919 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000920 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000921 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000922 .addReg(Src2SubHi);
923 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000924 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000925 .addReg(Src2SubLo);
926 MBB.erase(MI);
927 MRI.clearKillFlags(Src1SubHi);
928 MRI.clearKillFlags(Src1SubLo);
929 MRI.clearKillFlags(Src2SubHi);
930 MRI.clearKillFlags(Src2SubLo);
931 return true;
932 }
933 case Hexagon::VMULW_ACC: {
934 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
935 unsigned DstReg = MI->getOperand(0).getReg();
936 unsigned Src1Reg = MI->getOperand(1).getReg();
937 unsigned Src2Reg = MI->getOperand(2).getReg();
938 unsigned Src3Reg = MI->getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000939 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
940 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
941 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
942 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
943 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
944 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000945 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000946 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000947 .addReg(Src2SubHi).addReg(Src3SubHi);
948 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000949 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000950 .addReg(Src2SubLo).addReg(Src3SubLo);
951 MBB.erase(MI);
952 MRI.clearKillFlags(Src1SubHi);
953 MRI.clearKillFlags(Src1SubLo);
954 MRI.clearKillFlags(Src2SubHi);
955 MRI.clearKillFlags(Src2SubLo);
956 MRI.clearKillFlags(Src3SubHi);
957 MRI.clearKillFlags(Src3SubLo);
958 return true;
959 }
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +0000960 case Hexagon::Insert4: {
961 unsigned DstReg = MI->getOperand(0).getReg();
962 unsigned Src1Reg = MI->getOperand(1).getReg();
963 unsigned Src2Reg = MI->getOperand(2).getReg();
964 unsigned Src3Reg = MI->getOperand(3).getReg();
965 unsigned Src4Reg = MI->getOperand(4).getReg();
966 unsigned Src1RegIsKill = getKillRegState(MI->getOperand(1).isKill());
967 unsigned Src2RegIsKill = getKillRegState(MI->getOperand(2).isKill());
968 unsigned Src3RegIsKill = getKillRegState(MI->getOperand(3).isKill());
969 unsigned Src4RegIsKill = getKillRegState(MI->getOperand(4).isKill());
970 unsigned DstSubHi = HRI.getSubReg(DstReg, Hexagon::subreg_hireg);
971 unsigned DstSubLo = HRI.getSubReg(DstReg, Hexagon::subreg_loreg);
972 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::S2_insert),
973 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(DstSubLo)
974 .addReg(Src1Reg, Src1RegIsKill).addImm(16).addImm(0);
975 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::S2_insert),
976 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(DstSubLo)
977 .addReg(Src2Reg, Src2RegIsKill).addImm(16).addImm(16);
978 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::S2_insert),
979 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(DstSubHi)
980 .addReg(Src3Reg, Src3RegIsKill).addImm(16).addImm(0);
981 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::S2_insert),
982 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(DstSubHi)
983 .addReg(Src4Reg, Src4RegIsKill).addImm(16).addImm(16);
984 MBB.erase(MI);
985 MRI.clearKillFlags(DstReg);
986 MRI.clearKillFlags(DstSubHi);
987 MRI.clearKillFlags(DstSubLo);
988 return true;
989 }
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000990 case Hexagon::MUX64_rr: {
991 const MachineOperand &Op0 = MI->getOperand(0);
992 const MachineOperand &Op1 = MI->getOperand(1);
993 const MachineOperand &Op2 = MI->getOperand(2);
994 const MachineOperand &Op3 = MI->getOperand(3);
995 unsigned Rd = Op0.getReg();
996 unsigned Pu = Op1.getReg();
997 unsigned Rs = Op2.getReg();
998 unsigned Rt = Op3.getReg();
999 DebugLoc DL = MI->getDebugLoc();
1000 unsigned K1 = getKillRegState(Op1.isKill());
1001 unsigned K2 = getKillRegState(Op2.isKill());
1002 unsigned K3 = getKillRegState(Op3.isKill());
1003 if (Rd != Rs)
1004 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1005 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1006 .addReg(Rs, K2);
1007 if (Rd != Rt)
1008 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1009 .addReg(Pu, K1)
1010 .addReg(Rt, K3);
1011 MBB.erase(MI);
1012 return true;
1013 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001014 case Hexagon::TCRETURNi:
1015 MI->setDesc(get(Hexagon::J2_jump));
1016 return true;
1017 case Hexagon::TCRETURNr:
1018 MI->setDesc(get(Hexagon::J2_jumpr));
1019 return true;
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001020 case Hexagon::TFRI_f:
1021 case Hexagon::TFRI_cPt_f:
1022 case Hexagon::TFRI_cNotPt_f: {
1023 unsigned Opx = (Opc == Hexagon::TFRI_f) ? 1 : 2;
1024 APFloat FVal = MI->getOperand(Opx).getFPImm()->getValueAPF();
1025 APInt IVal = FVal.bitcastToAPInt();
1026 MI->RemoveOperand(Opx);
1027 unsigned NewOpc = (Opc == Hexagon::TFRI_f) ? Hexagon::A2_tfrsi :
1028 (Opc == Hexagon::TFRI_cPt_f) ? Hexagon::C2_cmoveit :
1029 Hexagon::C2_cmoveif;
1030 MI->setDesc(get(NewOpc));
1031 MI->addOperand(MachineOperand::CreateImm(IVal.getZExtValue()));
1032 return true;
1033 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001034 }
1035
1036 return false;
1037}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001038
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001039
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001040// We indicate that we want to reverse the branch by
1041// inserting the reversed branching opcode.
1042bool HexagonInstrInfo::ReverseBranchCondition(
1043 SmallVectorImpl<MachineOperand> &Cond) const {
1044 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001045 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001046 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1047 unsigned opcode = Cond[0].getImm();
1048 //unsigned temp;
1049 assert(get(opcode).isBranch() && "Should be a branching condition.");
1050 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001051 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001052 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1053 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001054 return false;
1055}
1056
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001057
1058void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1059 MachineBasicBlock::iterator MI) const {
1060 DebugLoc DL;
1061 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1062}
1063
1064
1065// Returns true if an instruction is predicated irrespective of the predicate
1066// sense. For example, all of the following will return true.
1067// if (p0) R1 = add(R2, R3)
1068// if (!p0) R1 = add(R2, R3)
1069// if (p0.new) R1 = add(R2, R3)
1070// if (!p0.new) R1 = add(R2, R3)
1071// Note: New-value stores are not included here as in the current
1072// implementation, we don't need to check their predicate sense.
1073bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00001074 const uint64_t F = MI->getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001075 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001076}
1077
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001078
1079bool HexagonInstrInfo::PredicateInstruction(MachineInstr *MI,
1080 ArrayRef<MachineOperand> Cond) const {
1081 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1082 isEndLoopN(Cond[0].getImm())) {
1083 DEBUG(dbgs() << "\nCannot predicate:"; MI->dump(););
1084 return false;
1085 }
1086 int Opc = MI->getOpcode();
1087 assert (isPredicable(MI) && "Expected predicable instruction");
1088 bool invertJump = predOpcodeHasNot(Cond);
1089
1090 // We have to predicate MI "in place", i.e. after this function returns,
1091 // MI will need to be transformed into a predicated form. To avoid com-
1092 // plicated manipulations with the operands (handling tied operands,
1093 // etc.), build a new temporary instruction, then overwrite MI with it.
1094
1095 MachineBasicBlock &B = *MI->getParent();
1096 DebugLoc DL = MI->getDebugLoc();
1097 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1098 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
1099 unsigned NOp = 0, NumOps = MI->getNumOperands();
1100 while (NOp < NumOps) {
1101 MachineOperand &Op = MI->getOperand(NOp);
1102 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1103 break;
1104 T.addOperand(Op);
1105 NOp++;
1106 }
1107
1108 unsigned PredReg, PredRegPos, PredRegFlags;
1109 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1110 (void)GotPredReg;
1111 assert(GotPredReg);
1112 T.addReg(PredReg, PredRegFlags);
1113 while (NOp < NumOps)
1114 T.addOperand(MI->getOperand(NOp++));
1115
1116 MI->setDesc(get(PredOpc));
1117 while (unsigned n = MI->getNumOperands())
1118 MI->RemoveOperand(n-1);
1119 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
1120 MI->addOperand(T->getOperand(i));
1121
1122 MachineBasicBlock::instr_iterator TI = T->getIterator();
1123 B.erase(TI);
1124
1125 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1126 MRI.clearKillFlags(PredReg);
1127 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001128}
1129
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001130
1131bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1132 ArrayRef<MachineOperand> Pred2) const {
1133 // TODO: Fix this
1134 return false;
1135}
1136
1137
1138bool HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1139 std::vector<MachineOperand> &Pred) const {
1140 auto &HRI = getRegisterInfo();
1141 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1142 MachineOperand MO = MI->getOperand(oper);
1143 if (MO.isReg() && MO.isDef()) {
1144 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1145 if (RC == &Hexagon::PredRegsRegClass) {
1146 Pred.push_back(MO);
1147 return true;
1148 }
1149 }
1150 }
1151 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001152}
Andrew Trickd06df962012-02-01 22:13:57 +00001153
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001154bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
1155 bool isPred = MI->getDesc().isPredicable();
1156
1157 if (!isPred)
1158 return false;
1159
1160 const int Opc = MI->getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001161 int NumOperands = MI->getNumOperands();
1162
1163 // Keep a flag for upto 4 operands in the instructions, to indicate if
1164 // that operand has been constant extended.
1165 bool OpCExtended[4];
1166 if (NumOperands > 4)
1167 NumOperands = 4;
1168
1169 for (int i = 0; i < NumOperands; i++)
1170 OpCExtended[i] = (isOperandExtended(MI, i) && isConstExtended(MI));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001171
1172 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001173 case Hexagon::A2_tfrsi:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001174 return (isOperandExtended(MI, 1) && isConstExtended(MI)) ||
1175 isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001176
Colin LeMahieubda31b42014-12-29 20:44:51 +00001177 case Hexagon::S2_storerd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001178 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001179
Colin LeMahieubda31b42014-12-29 20:44:51 +00001180 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001181 case Hexagon::S2_storerinew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001182 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001183
Colin LeMahieubda31b42014-12-29 20:44:51 +00001184 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001185 case Hexagon::S2_storerhnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001186 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001187
Colin LeMahieubda31b42014-12-29 20:44:51 +00001188 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001189 case Hexagon::S2_storerbnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001190 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001191
Colin LeMahieu947cd702014-12-23 20:44:59 +00001192 case Hexagon::L2_loadrd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001193 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001194
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001195 case Hexagon::L2_loadri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001196 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001197
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001198 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001199 case Hexagon::L2_loadruh_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001200 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001201
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001202 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001203 case Hexagon::L2_loadrub_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001204 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001205
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001206 case Hexagon::L2_loadrd_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001207 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001208
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001209 case Hexagon::L2_loadri_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001210 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001211
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001212 case Hexagon::L2_loadrh_pi:
1213 case Hexagon::L2_loadruh_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001214 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001215
Colin LeMahieu96976a12014-12-26 18:57:13 +00001216 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +00001217 case Hexagon::L2_loadrub_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001218 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001219
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001220 case Hexagon::S4_storeirb_io:
1221 case Hexagon::S4_storeirh_io:
1222 case Hexagon::S4_storeiri_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001223 return (OpCExtended[1] || isUInt<6>(MI->getOperand(1).getImm())) &&
1224 (OpCExtended[2] || isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001225
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001226 case Hexagon::A2_addi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001227 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001228
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001229 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001230 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001231 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001232 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001233 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001234 case Hexagon::A2_zxth:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001235 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001236 }
1237
1238 return true;
1239}
1240
Jyotsna Verma84c47102013-05-06 18:49:23 +00001241
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001242bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1243 const MachineBasicBlock *MBB, const MachineFunction &MF) const {
1244 // Debug info is never a scheduling boundary. It's necessary to be explicit
1245 // due to the special treatment of IT instructions below, otherwise a
1246 // dbg_value followed by an IT will result in the IT instruction being
1247 // considered a scheduling hazard, which is wrong. It should be the actual
1248 // instruction preceding the dbg_value instruction(s), just like it is
1249 // when debug info is not present.
1250 if (MI->isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001251 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001252
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001253 // Throwing call is a boundary.
1254 if (MI->isCall()) {
1255 // If any of the block's successors is a landing pad, this could be a
1256 // throwing call.
1257 for (auto I : MBB->successors())
1258 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001259 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001260 }
1261
1262 // Don't mess around with no return calls.
1263 if (MI->getOpcode() == Hexagon::CALLv3nr)
1264 return true;
1265
1266 // Terminators and labels can't be scheduled around.
1267 if (MI->getDesc().isTerminator() || MI->isPosition())
1268 return true;
1269
1270 if (MI->isInlineAsm() && !ScheduleInlineAsm)
1271 return true;
1272
1273 return false;
1274}
1275
1276
1277/// Measure the specified inline asm to determine an approximation of its
1278/// length.
1279/// Comments (which run till the next SeparatorString or newline) do not
1280/// count as an instruction.
1281/// Any other non-whitespace text is considered an instruction, with
1282/// multiple instructions separated by SeparatorString or newlines.
1283/// Variable-length instructions are not handled here; this function
1284/// may be overloaded in the target code to do that.
1285/// Hexagon counts the number of ##'s and adjust for that many
1286/// constant exenders.
1287unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1288 const MCAsmInfo &MAI) const {
1289 StringRef AStr(Str);
1290 // Count the number of instructions in the asm.
1291 bool atInsnStart = true;
1292 unsigned Length = 0;
1293 for (; *Str; ++Str) {
1294 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1295 strlen(MAI.getSeparatorString())) == 0)
1296 atInsnStart = true;
1297 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1298 Length += MAI.getMaxInstLength();
1299 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001300 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001301 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1302 strlen(MAI.getCommentString())) == 0)
1303 atInsnStart = false;
1304 }
1305
1306 // Add to size number of constant extenders seen * 4.
1307 StringRef Occ("##");
1308 Length += AStr.count(Occ)*4;
1309 return Length;
1310}
1311
1312
1313ScheduleHazardRecognizer*
1314HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1315 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
1316 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1317}
1318
1319
1320/// \brief For a comparison instruction, return the source registers in
1321/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1322/// compares against in CmpValue. Return true if the comparison instruction
1323/// can be analyzed.
1324bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
1325 unsigned &SrcReg, unsigned &SrcReg2, int &Mask, int &Value) const {
1326 unsigned Opc = MI->getOpcode();
1327
1328 // Set mask and the first source register.
1329 switch (Opc) {
1330 case Hexagon::C2_cmpeq:
1331 case Hexagon::C2_cmpeqp:
1332 case Hexagon::C2_cmpgt:
1333 case Hexagon::C2_cmpgtp:
1334 case Hexagon::C2_cmpgtu:
1335 case Hexagon::C2_cmpgtup:
1336 case Hexagon::C4_cmpneq:
1337 case Hexagon::C4_cmplte:
1338 case Hexagon::C4_cmplteu:
1339 case Hexagon::C2_cmpeqi:
1340 case Hexagon::C2_cmpgti:
1341 case Hexagon::C2_cmpgtui:
1342 case Hexagon::C4_cmpneqi:
1343 case Hexagon::C4_cmplteui:
1344 case Hexagon::C4_cmpltei:
1345 SrcReg = MI->getOperand(1).getReg();
1346 Mask = ~0;
1347 break;
1348 case Hexagon::A4_cmpbeq:
1349 case Hexagon::A4_cmpbgt:
1350 case Hexagon::A4_cmpbgtu:
1351 case Hexagon::A4_cmpbeqi:
1352 case Hexagon::A4_cmpbgti:
1353 case Hexagon::A4_cmpbgtui:
1354 SrcReg = MI->getOperand(1).getReg();
1355 Mask = 0xFF;
1356 break;
1357 case Hexagon::A4_cmpheq:
1358 case Hexagon::A4_cmphgt:
1359 case Hexagon::A4_cmphgtu:
1360 case Hexagon::A4_cmpheqi:
1361 case Hexagon::A4_cmphgti:
1362 case Hexagon::A4_cmphgtui:
1363 SrcReg = MI->getOperand(1).getReg();
1364 Mask = 0xFFFF;
1365 break;
1366 }
1367
1368 // Set the value/second source register.
1369 switch (Opc) {
1370 case Hexagon::C2_cmpeq:
1371 case Hexagon::C2_cmpeqp:
1372 case Hexagon::C2_cmpgt:
1373 case Hexagon::C2_cmpgtp:
1374 case Hexagon::C2_cmpgtu:
1375 case Hexagon::C2_cmpgtup:
1376 case Hexagon::A4_cmpbeq:
1377 case Hexagon::A4_cmpbgt:
1378 case Hexagon::A4_cmpbgtu:
1379 case Hexagon::A4_cmpheq:
1380 case Hexagon::A4_cmphgt:
1381 case Hexagon::A4_cmphgtu:
1382 case Hexagon::C4_cmpneq:
1383 case Hexagon::C4_cmplte:
1384 case Hexagon::C4_cmplteu:
1385 SrcReg2 = MI->getOperand(2).getReg();
1386 return true;
1387
1388 case Hexagon::C2_cmpeqi:
1389 case Hexagon::C2_cmpgtui:
1390 case Hexagon::C2_cmpgti:
1391 case Hexagon::C4_cmpneqi:
1392 case Hexagon::C4_cmplteui:
1393 case Hexagon::C4_cmpltei:
1394 case Hexagon::A4_cmpbeqi:
1395 case Hexagon::A4_cmpbgti:
1396 case Hexagon::A4_cmpbgtui:
1397 case Hexagon::A4_cmpheqi:
1398 case Hexagon::A4_cmphgti:
1399 case Hexagon::A4_cmphgtui:
1400 SrcReg2 = 0;
1401 Value = MI->getOperand(2).getImm();
1402 return true;
1403 }
1404
1405 return false;
1406}
1407
1408
1409unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1410 const MachineInstr *MI, unsigned *PredCost) const {
1411 return getInstrTimingClassLatency(ItinData, MI);
1412}
1413
1414
1415DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1416 const TargetSubtargetInfo &STI) const {
1417 const InstrItineraryData *II = STI.getInstrItineraryData();
1418 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1419}
1420
1421
1422// Inspired by this pair:
1423// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1424// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1425// Currently AA considers the addresses in these instructions to be aliasing.
1426bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr *MIa,
1427 MachineInstr *MIb, AliasAnalysis *AA) const {
1428 int OffsetA = 0, OffsetB = 0;
1429 unsigned SizeA = 0, SizeB = 0;
1430
1431 if (MIa->hasUnmodeledSideEffects() || MIb->hasUnmodeledSideEffects() ||
1432 MIa->hasOrderedMemoryRef() || MIa->hasOrderedMemoryRef())
1433 return false;
1434
1435 // Instructions that are pure loads, not loads and stores like memops are not
1436 // dependent.
1437 if (MIa->mayLoad() && !isMemOp(MIa) && MIb->mayLoad() && !isMemOp(MIb))
1438 return true;
1439
1440 // Get base, offset, and access size in MIa.
1441 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
1442 if (!BaseRegA || !SizeA)
1443 return false;
1444
1445 // Get base, offset, and access size in MIb.
1446 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
1447 if (!BaseRegB || !SizeB)
1448 return false;
1449
1450 if (BaseRegA != BaseRegB)
1451 return false;
1452
1453 // This is a mem access with the same base register and known offsets from it.
1454 // Reason about it.
1455 if (OffsetA > OffsetB) {
1456 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1457 return (SizeB <= offDiff);
1458 } else if (OffsetA < OffsetB) {
1459 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1460 return (SizeA <= offDiff);
1461 }
1462
1463 return false;
1464}
1465
1466
1467unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
1468 MachineRegisterInfo &MRI = MF->getRegInfo();
1469 const TargetRegisterClass *TRC;
1470 if (VT == MVT::i1) {
1471 TRC = &Hexagon::PredRegsRegClass;
1472 } else if (VT == MVT::i32 || VT == MVT::f32) {
1473 TRC = &Hexagon::IntRegsRegClass;
1474 } else if (VT == MVT::i64 || VT == MVT::f64) {
1475 TRC = &Hexagon::DoubleRegsRegClass;
1476 } else {
1477 llvm_unreachable("Cannot handle this register class");
1478 }
1479
1480 unsigned NewReg = MRI.createVirtualRegister(TRC);
1481 return NewReg;
1482}
1483
1484
1485bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr* MI) const {
1486 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1487}
1488
1489
1490bool HexagonInstrInfo::isAccumulator(const MachineInstr *MI) const {
1491 const uint64_t F = MI->getDesc().TSFlags;
1492 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1493}
1494
1495
1496bool HexagonInstrInfo::isComplex(const MachineInstr *MI) const {
1497 const MachineFunction *MF = MI->getParent()->getParent();
1498 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1499 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1500
1501 if (!(isTC1(MI))
1502 && !(QII->isTC2Early(MI))
1503 && !(MI->getDesc().mayLoad())
1504 && !(MI->getDesc().mayStore())
1505 && (MI->getDesc().getOpcode() != Hexagon::S2_allocframe)
1506 && (MI->getDesc().getOpcode() != Hexagon::L2_deallocframe)
1507 && !(QII->isMemOp(MI))
1508 && !(MI->isBranch())
1509 && !(MI->isReturn())
1510 && !MI->isCall())
1511 return true;
1512
1513 return false;
1514}
1515
1516
Sanjay Patele4b9f502015-12-07 19:21:39 +00001517// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001518bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr *MI) const {
1519 return (getType(MI) == HexagonII::TypeCOMPOUND && MI->isBranch());
1520}
1521
1522
1523bool HexagonInstrInfo::isCondInst(const MachineInstr *MI) const {
1524 return (MI->isBranch() && isPredicated(MI)) ||
1525 isConditionalTransfer(MI) ||
1526 isConditionalALU32(MI) ||
1527 isConditionalLoad(MI) ||
1528 // Predicated stores which don't have a .new on any operands.
1529 (MI->mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1530 !isPredicatedNew(MI));
1531}
1532
1533
1534bool HexagonInstrInfo::isConditionalALU32(const MachineInstr* MI) const {
1535 switch (MI->getOpcode()) {
1536 case Hexagon::A2_paddf:
1537 case Hexagon::A2_paddfnew:
1538 case Hexagon::A2_paddif:
1539 case Hexagon::A2_paddifnew:
1540 case Hexagon::A2_paddit:
1541 case Hexagon::A2_padditnew:
1542 case Hexagon::A2_paddt:
1543 case Hexagon::A2_paddtnew:
1544 case Hexagon::A2_pandf:
1545 case Hexagon::A2_pandfnew:
1546 case Hexagon::A2_pandt:
1547 case Hexagon::A2_pandtnew:
1548 case Hexagon::A2_porf:
1549 case Hexagon::A2_porfnew:
1550 case Hexagon::A2_port:
1551 case Hexagon::A2_portnew:
1552 case Hexagon::A2_psubf:
1553 case Hexagon::A2_psubfnew:
1554 case Hexagon::A2_psubt:
1555 case Hexagon::A2_psubtnew:
1556 case Hexagon::A2_pxorf:
1557 case Hexagon::A2_pxorfnew:
1558 case Hexagon::A2_pxort:
1559 case Hexagon::A2_pxortnew:
1560 case Hexagon::A4_paslhf:
1561 case Hexagon::A4_paslhfnew:
1562 case Hexagon::A4_paslht:
1563 case Hexagon::A4_paslhtnew:
1564 case Hexagon::A4_pasrhf:
1565 case Hexagon::A4_pasrhfnew:
1566 case Hexagon::A4_pasrht:
1567 case Hexagon::A4_pasrhtnew:
1568 case Hexagon::A4_psxtbf:
1569 case Hexagon::A4_psxtbfnew:
1570 case Hexagon::A4_psxtbt:
1571 case Hexagon::A4_psxtbtnew:
1572 case Hexagon::A4_psxthf:
1573 case Hexagon::A4_psxthfnew:
1574 case Hexagon::A4_psxtht:
1575 case Hexagon::A4_psxthtnew:
1576 case Hexagon::A4_pzxtbf:
1577 case Hexagon::A4_pzxtbfnew:
1578 case Hexagon::A4_pzxtbt:
1579 case Hexagon::A4_pzxtbtnew:
1580 case Hexagon::A4_pzxthf:
1581 case Hexagon::A4_pzxthfnew:
1582 case Hexagon::A4_pzxtht:
1583 case Hexagon::A4_pzxthtnew:
1584 case Hexagon::C2_ccombinewf:
1585 case Hexagon::C2_ccombinewt:
1586 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001587 }
1588 return false;
1589}
1590
1591
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001592// FIXME - Function name and it's functionality don't match.
1593// It should be renamed to hasPredNewOpcode()
1594bool HexagonInstrInfo::isConditionalLoad(const MachineInstr* MI) const {
1595 if (!MI->getDesc().mayLoad() || !isPredicated(MI))
1596 return false;
1597
1598 int PNewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1599 // Instruction with valid predicated-new opcode can be promoted to .new.
1600 return PNewOpcode >= 0;
1601}
1602
1603
1604// Returns true if an instruction is a conditional store.
1605//
1606// Note: It doesn't include conditional new-value stores as they can't be
1607// converted to .new predicate.
1608bool HexagonInstrInfo::isConditionalStore(const MachineInstr* MI) const {
1609 switch (MI->getOpcode()) {
1610 default: return false;
1611 case Hexagon::S4_storeirbt_io:
1612 case Hexagon::S4_storeirbf_io:
1613 case Hexagon::S4_pstorerbt_rr:
1614 case Hexagon::S4_pstorerbf_rr:
1615 case Hexagon::S2_pstorerbt_io:
1616 case Hexagon::S2_pstorerbf_io:
1617 case Hexagon::S2_pstorerbt_pi:
1618 case Hexagon::S2_pstorerbf_pi:
1619 case Hexagon::S2_pstorerdt_io:
1620 case Hexagon::S2_pstorerdf_io:
1621 case Hexagon::S4_pstorerdt_rr:
1622 case Hexagon::S4_pstorerdf_rr:
1623 case Hexagon::S2_pstorerdt_pi:
1624 case Hexagon::S2_pstorerdf_pi:
1625 case Hexagon::S2_pstorerht_io:
1626 case Hexagon::S2_pstorerhf_io:
1627 case Hexagon::S4_storeirht_io:
1628 case Hexagon::S4_storeirhf_io:
1629 case Hexagon::S4_pstorerht_rr:
1630 case Hexagon::S4_pstorerhf_rr:
1631 case Hexagon::S2_pstorerht_pi:
1632 case Hexagon::S2_pstorerhf_pi:
1633 case Hexagon::S2_pstorerit_io:
1634 case Hexagon::S2_pstorerif_io:
1635 case Hexagon::S4_storeirit_io:
1636 case Hexagon::S4_storeirif_io:
1637 case Hexagon::S4_pstorerit_rr:
1638 case Hexagon::S4_pstorerif_rr:
1639 case Hexagon::S2_pstorerit_pi:
1640 case Hexagon::S2_pstorerif_pi:
1641
1642 // V4 global address store before promoting to dot new.
1643 case Hexagon::S4_pstorerdt_abs:
1644 case Hexagon::S4_pstorerdf_abs:
1645 case Hexagon::S4_pstorerbt_abs:
1646 case Hexagon::S4_pstorerbf_abs:
1647 case Hexagon::S4_pstorerht_abs:
1648 case Hexagon::S4_pstorerhf_abs:
1649 case Hexagon::S4_pstorerit_abs:
1650 case Hexagon::S4_pstorerif_abs:
1651 return true;
1652
1653 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1654 // from the "Conditional Store" list. Because a predicated new value store
1655 // would NOT be promoted to a double dot new store.
1656 // This function returns yes for those stores that are predicated but not
1657 // yet promoted to predicate dot new instructions.
1658 }
1659}
1660
1661
1662bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr *MI) const {
1663 switch (MI->getOpcode()) {
1664 case Hexagon::A2_tfrt:
1665 case Hexagon::A2_tfrf:
1666 case Hexagon::C2_cmoveit:
1667 case Hexagon::C2_cmoveif:
1668 case Hexagon::A2_tfrtnew:
1669 case Hexagon::A2_tfrfnew:
1670 case Hexagon::C2_cmovenewit:
1671 case Hexagon::C2_cmovenewif:
1672 case Hexagon::A2_tfrpt:
1673 case Hexagon::A2_tfrpf:
1674 return true;
1675
1676 default:
1677 return false;
1678 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001679 return false;
1680}
1681
1682
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001683// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1684// isFPImm and later getFPImm as well.
1685bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
1686 const uint64_t F = MI->getDesc().TSFlags;
1687 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1688 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001689 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001690
1691 unsigned isExtendable =
1692 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1693 if (!isExtendable)
1694 return false;
1695
1696 if (MI->isCall())
1697 return false;
1698
1699 short ExtOpNum = getCExtOpNum(MI);
1700 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1701 // Use MO operand flags to determine if MO
1702 // has the HMOTF_ConstExtended flag set.
1703 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001704 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001705 // If this is a Machine BB address we are talking about, and it is
1706 // not marked as extended, say so.
1707 if (MO.isMBB())
1708 return false;
1709
1710 // We could be using an instruction with an extendable immediate and shoehorn
1711 // a global address into it. If it is a global address it will be constant
1712 // extended. We do this for COMBINE.
1713 // We currently only handle isGlobal() because it is the only kind of
1714 // object we are going to end up with here for now.
1715 // In the future we probably should add isSymbol(), etc.
1716 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1717 MO.isJTI() || MO.isCPI())
1718 return true;
1719
1720 // If the extendable operand is not 'Immediate' type, the instruction should
1721 // have 'isExtended' flag set.
1722 assert(MO.isImm() && "Extendable operand must be Immediate type");
1723
1724 int MinValue = getMinValue(MI);
1725 int MaxValue = getMaxValue(MI);
1726 int ImmValue = MO.getImm();
1727
1728 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001729}
1730
1731
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001732bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1733 switch (MI->getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001734 case Hexagon::L4_return :
1735 case Hexagon::L4_return_t :
1736 case Hexagon::L4_return_f :
1737 case Hexagon::L4_return_tnew_pnt :
1738 case Hexagon::L4_return_fnew_pnt :
1739 case Hexagon::L4_return_tnew_pt :
1740 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001741 return true;
1742 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001743 return false;
1744}
1745
1746
1747// Return true when ConsMI uses a register defined by ProdMI.
1748bool HexagonInstrInfo::isDependent(const MachineInstr *ProdMI,
1749 const MachineInstr *ConsMI) const {
1750 const MCInstrDesc &ProdMCID = ProdMI->getDesc();
1751 if (!ProdMCID.getNumDefs())
1752 return false;
1753
1754 auto &HRI = getRegisterInfo();
1755
1756 SmallVector<unsigned, 4> DefsA;
1757 SmallVector<unsigned, 4> DefsB;
1758 SmallVector<unsigned, 8> UsesA;
1759 SmallVector<unsigned, 8> UsesB;
1760
1761 parseOperands(ProdMI, DefsA, UsesA);
1762 parseOperands(ConsMI, DefsB, UsesB);
1763
1764 for (auto &RegA : DefsA)
1765 for (auto &RegB : UsesB) {
1766 // True data dependency.
1767 if (RegA == RegB)
1768 return true;
1769
1770 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1771 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1772 if (RegB == *SubRegs)
1773 return true;
1774
1775 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1776 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1777 if (RegA == *SubRegs)
1778 return true;
1779 }
1780
1781 return false;
1782}
1783
1784
1785// Returns true if the instruction is alread a .cur.
1786bool HexagonInstrInfo::isDotCurInst(const MachineInstr* MI) const {
1787 switch (MI->getOpcode()) {
1788 case Hexagon::V6_vL32b_cur_pi:
1789 case Hexagon::V6_vL32b_cur_ai:
1790 case Hexagon::V6_vL32b_cur_pi_128B:
1791 case Hexagon::V6_vL32b_cur_ai_128B:
1792 return true;
1793 }
1794 return false;
1795}
1796
1797
1798// Returns true, if any one of the operands is a dot new
1799// insn, whether it is predicated dot new or register dot new.
1800bool HexagonInstrInfo::isDotNewInst(const MachineInstr* MI) const {
1801 if (isNewValueInst(MI) ||
1802 (isPredicated(MI) && isPredicatedNew(MI)))
1803 return true;
1804
1805 return false;
1806}
1807
1808
1809/// Symmetrical. See if these two instructions are fit for duplex pair.
1810bool HexagonInstrInfo::isDuplexPair(const MachineInstr *MIa,
1811 const MachineInstr *MIb) const {
1812 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1813 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1814 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1815}
1816
1817
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001818bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001819 if (!MI)
1820 return false;
1821
1822 if (MI->mayLoad() || MI->mayStore() || MI->isCompare())
1823 return true;
1824
1825 // Multiply
1826 unsigned SchedClass = MI->getDesc().getSchedClass();
1827 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
1828 return true;
1829 return false;
1830}
1831
1832
1833bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1834 return (Opcode == Hexagon::ENDLOOP0 ||
1835 Opcode == Hexagon::ENDLOOP1);
1836}
1837
1838
1839bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1840 switch(OpType) {
1841 case MachineOperand::MO_MachineBasicBlock:
1842 case MachineOperand::MO_GlobalAddress:
1843 case MachineOperand::MO_ExternalSymbol:
1844 case MachineOperand::MO_JumpTableIndex:
1845 case MachineOperand::MO_ConstantPoolIndex:
1846 case MachineOperand::MO_BlockAddress:
1847 return true;
1848 default:
1849 return false;
1850 }
1851}
1852
1853
1854bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
1855 const MCInstrDesc &MID = MI->getDesc();
1856 const uint64_t F = MID.TSFlags;
1857 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1858 return true;
1859
1860 // TODO: This is largely obsolete now. Will need to be removed
1861 // in consecutive patches.
1862 switch(MI->getOpcode()) {
1863 // TFR_FI Remains a special case.
1864 case Hexagon::TFR_FI:
1865 return true;
1866 default:
1867 return false;
1868 }
1869 return false;
1870}
1871
1872
1873// This returns true in two cases:
1874// - The OP code itself indicates that this is an extended instruction.
1875// - One of MOs has been marked with HMOTF_ConstExtended flag.
1876bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
1877 // First check if this is permanently extended op code.
1878 const uint64_t F = MI->getDesc().TSFlags;
1879 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
1880 return true;
1881 // Use MO operand flags to determine if one of MI's operands
1882 // has HMOTF_ConstExtended flag set.
1883 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
1884 E = MI->operands_end(); I != E; ++I) {
1885 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1886 return true;
1887 }
1888 return false;
1889}
1890
1891
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001892bool HexagonInstrInfo::isFloat(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001893 unsigned Opcode = MI->getOpcode();
1894 const uint64_t F = get(Opcode).TSFlags;
1895 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
1896}
1897
1898
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001899// No V60 HVX VMEM with A_INDIRECT.
1900bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr *I,
1901 const MachineInstr *J) const {
1902 if (!isV60VectorInstruction(I))
1903 return false;
1904 if (!I->mayLoad() && !I->mayStore())
1905 return false;
1906 return J->isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
1907}
1908
1909
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001910bool HexagonInstrInfo::isIndirectCall(const MachineInstr *MI) const {
1911 switch (MI->getOpcode()) {
1912 case Hexagon::J2_callr :
1913 case Hexagon::J2_callrf :
1914 case Hexagon::J2_callrt :
1915 return true;
1916 }
1917 return false;
1918}
1919
1920
1921bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr *MI) const {
1922 switch (MI->getOpcode()) {
1923 case Hexagon::L4_return :
1924 case Hexagon::L4_return_t :
1925 case Hexagon::L4_return_f :
1926 case Hexagon::L4_return_fnew_pnt :
1927 case Hexagon::L4_return_fnew_pt :
1928 case Hexagon::L4_return_tnew_pnt :
1929 case Hexagon::L4_return_tnew_pt :
1930 return true;
1931 }
1932 return false;
1933}
1934
1935
1936bool HexagonInstrInfo::isJumpR(const MachineInstr *MI) const {
1937 switch (MI->getOpcode()) {
1938 case Hexagon::J2_jumpr :
1939 case Hexagon::J2_jumprt :
1940 case Hexagon::J2_jumprf :
1941 case Hexagon::J2_jumprtnewpt :
1942 case Hexagon::J2_jumprfnewpt :
1943 case Hexagon::J2_jumprtnew :
1944 case Hexagon::J2_jumprfnew :
1945 return true;
1946 }
1947 return false;
1948}
1949
1950
1951// Return true if a given MI can accomodate given offset.
1952// Use abs estimate as oppose to the exact number.
1953// TODO: This will need to be changed to use MC level
1954// definition of instruction extendable field size.
1955bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr *MI,
1956 unsigned offset) const {
1957 // This selection of jump instructions matches to that what
1958 // AnalyzeBranch can parse, plus NVJ.
1959 if (isNewValueJump(MI)) // r9:2
1960 return isInt<11>(offset);
1961
1962 switch (MI->getOpcode()) {
1963 // Still missing Jump to address condition on register value.
1964 default:
1965 return false;
1966 case Hexagon::J2_jump: // bits<24> dst; // r22:2
1967 case Hexagon::J2_call:
1968 case Hexagon::CALLv3nr:
1969 return isInt<24>(offset);
1970 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
1971 case Hexagon::J2_jumpf:
1972 case Hexagon::J2_jumptnew:
1973 case Hexagon::J2_jumptnewpt:
1974 case Hexagon::J2_jumpfnew:
1975 case Hexagon::J2_jumpfnewpt:
1976 case Hexagon::J2_callt:
1977 case Hexagon::J2_callf:
1978 return isInt<17>(offset);
1979 case Hexagon::J2_loop0i:
1980 case Hexagon::J2_loop0iext:
1981 case Hexagon::J2_loop0r:
1982 case Hexagon::J2_loop0rext:
1983 case Hexagon::J2_loop1i:
1984 case Hexagon::J2_loop1iext:
1985 case Hexagon::J2_loop1r:
1986 case Hexagon::J2_loop1rext:
1987 return isInt<9>(offset);
1988 // TODO: Add all the compound branches here. Can we do this in Relation model?
1989 case Hexagon::J4_cmpeqi_tp0_jump_nt:
1990 case Hexagon::J4_cmpeqi_tp1_jump_nt:
1991 return isInt<11>(offset);
1992 }
1993}
1994
1995
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001996bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr *LRMI,
1997 const MachineInstr *ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001998 if (!LRMI || !ESMI)
1999 return false;
2000
2001 bool isLate = isLateResultInstr(LRMI);
2002 bool isEarly = isEarlySourceInstr(ESMI);
2003
2004 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
2005 DEBUG(LRMI->dump());
2006 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
2007 DEBUG(ESMI->dump());
2008
2009 if (isLate && isEarly) {
2010 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2011 return true;
2012 }
2013
2014 return false;
2015}
2016
2017
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002018bool HexagonInstrInfo::isLateResultInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002019 if (!MI)
2020 return false;
2021
2022 switch (MI->getOpcode()) {
2023 case TargetOpcode::EXTRACT_SUBREG:
2024 case TargetOpcode::INSERT_SUBREG:
2025 case TargetOpcode::SUBREG_TO_REG:
2026 case TargetOpcode::REG_SEQUENCE:
2027 case TargetOpcode::IMPLICIT_DEF:
2028 case TargetOpcode::COPY:
2029 case TargetOpcode::INLINEASM:
2030 case TargetOpcode::PHI:
2031 return false;
2032 default:
2033 break;
2034 }
2035
2036 unsigned SchedClass = MI->getDesc().getSchedClass();
2037
2038 switch (SchedClass) {
2039 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2040 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2041 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2042 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2043 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2044 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2045 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2046 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2047 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2048 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2049 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2050 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2051 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2052 return false;
2053 }
2054 return true;
2055}
2056
2057
2058bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr *MI) const {
2059 if (!MI)
2060 return false;
2061
2062 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2063 // resource, but all operands can be received late like an ALU instruction.
2064 return MI->getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
2065}
2066
2067
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002068bool HexagonInstrInfo::isLoopN(const MachineInstr *MI) const {
2069 unsigned Opcode = MI->getOpcode();
2070 return Opcode == Hexagon::J2_loop0i ||
2071 Opcode == Hexagon::J2_loop0r ||
2072 Opcode == Hexagon::J2_loop0iext ||
2073 Opcode == Hexagon::J2_loop0rext ||
2074 Opcode == Hexagon::J2_loop1i ||
2075 Opcode == Hexagon::J2_loop1r ||
2076 Opcode == Hexagon::J2_loop1iext ||
2077 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002078}
2079
2080
2081bool HexagonInstrInfo::isMemOp(const MachineInstr *MI) const {
2082 switch (MI->getOpcode()) {
2083 default: return false;
2084 case Hexagon::L4_iadd_memopw_io :
2085 case Hexagon::L4_isub_memopw_io :
2086 case Hexagon::L4_add_memopw_io :
2087 case Hexagon::L4_sub_memopw_io :
2088 case Hexagon::L4_and_memopw_io :
2089 case Hexagon::L4_or_memopw_io :
2090 case Hexagon::L4_iadd_memoph_io :
2091 case Hexagon::L4_isub_memoph_io :
2092 case Hexagon::L4_add_memoph_io :
2093 case Hexagon::L4_sub_memoph_io :
2094 case Hexagon::L4_and_memoph_io :
2095 case Hexagon::L4_or_memoph_io :
2096 case Hexagon::L4_iadd_memopb_io :
2097 case Hexagon::L4_isub_memopb_io :
2098 case Hexagon::L4_add_memopb_io :
2099 case Hexagon::L4_sub_memopb_io :
2100 case Hexagon::L4_and_memopb_io :
2101 case Hexagon::L4_or_memopb_io :
2102 case Hexagon::L4_ior_memopb_io:
2103 case Hexagon::L4_ior_memoph_io:
2104 case Hexagon::L4_ior_memopw_io:
2105 case Hexagon::L4_iand_memopb_io:
2106 case Hexagon::L4_iand_memoph_io:
2107 case Hexagon::L4_iand_memopw_io:
2108 return true;
2109 }
2110 return false;
2111}
2112
2113
2114bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
2115 const uint64_t F = MI->getDesc().TSFlags;
2116 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2117}
2118
2119
2120bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2121 const uint64_t F = get(Opcode).TSFlags;
2122 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2123}
2124
2125
2126bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
2127 return isNewValueJump(MI) || isNewValueStore(MI);
2128}
2129
2130
2131bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
2132 return isNewValue(MI) && MI->isBranch();
2133}
2134
2135
2136bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2137 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2138}
2139
2140
2141bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
2142 const uint64_t F = MI->getDesc().TSFlags;
2143 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2144}
2145
2146
2147bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2148 const uint64_t F = get(Opcode).TSFlags;
2149 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2150}
2151
2152
2153// Returns true if a particular operand is extendable for an instruction.
2154bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
2155 unsigned OperandNum) const {
2156 const uint64_t F = MI->getDesc().TSFlags;
2157 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2158 == OperandNum;
2159}
2160
2161
2162bool HexagonInstrInfo::isPostIncrement(const MachineInstr* MI) const {
2163 return getAddrMode(MI) == HexagonII::PostInc;
2164}
2165
2166
2167bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
2168 const uint64_t F = MI->getDesc().TSFlags;
2169 assert(isPredicated(MI));
2170 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2171}
2172
2173
2174bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2175 const uint64_t F = get(Opcode).TSFlags;
2176 assert(isPredicated(Opcode));
2177 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2178}
2179
2180
2181bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
2182 const uint64_t F = MI->getDesc().TSFlags;
2183 return !((F >> HexagonII::PredicatedFalsePos) &
2184 HexagonII::PredicatedFalseMask);
2185}
2186
2187
2188bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2189 const uint64_t F = get(Opcode).TSFlags;
2190 // Make sure that the instruction is predicated.
2191 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2192 return !((F >> HexagonII::PredicatedFalsePos) &
2193 HexagonII::PredicatedFalseMask);
2194}
2195
2196
2197bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2198 const uint64_t F = get(Opcode).TSFlags;
2199 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2200}
2201
2202
2203bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2204 const uint64_t F = get(Opcode).TSFlags;
2205 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2206}
2207
2208
2209bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2210 const uint64_t F = get(Opcode).TSFlags;
2211 assert(get(Opcode).isBranch() &&
2212 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2213 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2214}
2215
2216
2217bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
2218 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2219 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT;
2220}
2221
2222
2223bool HexagonInstrInfo::isSolo(const MachineInstr* MI) const {
2224 const uint64_t F = MI->getDesc().TSFlags;
2225 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2226}
2227
2228
2229bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr *MI) const {
2230 switch (MI->getOpcode()) {
2231 case Hexagon::STriw_pred :
2232 case Hexagon::LDriw_pred :
2233 return true;
2234 default:
2235 return false;
2236 }
2237}
2238
2239
2240// Returns true when SU has a timing class TC1.
2241bool HexagonInstrInfo::isTC1(const MachineInstr *MI) const {
2242 unsigned SchedClass = MI->getDesc().getSchedClass();
2243 switch (SchedClass) {
2244 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2245 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2246 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2247 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2248 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2249 //case Hexagon::Sched::M_tc_1_SLOT23:
2250 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2251 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2252 return true;
2253
2254 default:
2255 return false;
2256 }
2257}
2258
2259
2260bool HexagonInstrInfo::isTC2(const MachineInstr *MI) const {
2261 unsigned SchedClass = MI->getDesc().getSchedClass();
2262 switch (SchedClass) {
2263 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2264 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2265 case Hexagon::Sched::CR_tc_2_SLOT3:
2266 case Hexagon::Sched::M_tc_2_SLOT23:
2267 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2268 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2269 return true;
2270
2271 default:
2272 return false;
2273 }
2274}
2275
2276
2277bool HexagonInstrInfo::isTC2Early(const MachineInstr *MI) const {
2278 unsigned SchedClass = MI->getDesc().getSchedClass();
2279 switch (SchedClass) {
2280 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2281 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2282 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2283 case Hexagon::Sched::CR_tc_2early_SLOT23:
2284 case Hexagon::Sched::CR_tc_2early_SLOT3:
2285 case Hexagon::Sched::J_tc_2early_SLOT0123:
2286 case Hexagon::Sched::J_tc_2early_SLOT2:
2287 case Hexagon::Sched::J_tc_2early_SLOT23:
2288 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2289 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2290 return true;
2291
2292 default:
2293 return false;
2294 }
2295}
2296
2297
2298bool HexagonInstrInfo::isTC4x(const MachineInstr *MI) const {
2299 if (!MI)
2300 return false;
2301
2302 unsigned SchedClass = MI->getDesc().getSchedClass();
2303 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2304}
2305
2306
2307bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr *MI) const {
2308 if (!MI)
2309 return false;
2310
2311 const uint64_t V = getType(MI);
2312 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2313}
2314
2315
2316// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2317//
2318bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2319 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2320 VT == MVT::v32i16 || VT == MVT::v64i8) {
2321 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2322 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2323 (Offset & 0x3f) == 0);
2324 }
2325 // 128B
2326 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2327 VT == MVT::v64i16 || VT == MVT::v128i8) {
2328 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2329 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2330 (Offset & 0x7f) == 0);
2331 }
2332 if (VT == MVT::i64) {
2333 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2334 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2335 (Offset & 0x7) == 0);
2336 }
2337 if (VT == MVT::i32) {
2338 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2339 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2340 (Offset & 0x3) == 0);
2341 }
2342 if (VT == MVT::i16) {
2343 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2344 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2345 (Offset & 0x1) == 0);
2346 }
2347 if (VT == MVT::i8) {
2348 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2349 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2350 }
2351 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002352}
2353
2354
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002355bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2356 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002357 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002358 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002359 // inserted to calculate the final address. Due to this reason, the function
2360 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002361 // We used to assert if the offset was not properly aligned, however,
2362 // there are cases where a misaligned pointer recast can cause this
2363 // problem, and we need to allow for it. The front end warns of such
2364 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002365
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002366 switch (Opcode) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002367 case Hexagon::STriq_pred_V6:
2368 case Hexagon::STriq_pred_vec_V6:
2369 case Hexagon::STriv_pseudo_V6:
2370 case Hexagon::STrivv_pseudo_V6:
2371 case Hexagon::LDriq_pred_V6:
2372 case Hexagon::LDriq_pred_vec_V6:
2373 case Hexagon::LDriv_pseudo_V6:
2374 case Hexagon::LDrivv_pseudo_V6:
2375 case Hexagon::LDrivv_indexed:
2376 case Hexagon::STrivv_indexed:
2377 case Hexagon::V6_vL32b_ai:
2378 case Hexagon::V6_vS32b_ai:
2379 case Hexagon::V6_vL32Ub_ai:
2380 case Hexagon::V6_vS32Ub_ai:
2381 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2382 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2383
2384 case Hexagon::STriq_pred_V6_128B:
2385 case Hexagon::STriq_pred_vec_V6_128B:
2386 case Hexagon::STriv_pseudo_V6_128B:
2387 case Hexagon::STrivv_pseudo_V6_128B:
2388 case Hexagon::LDriq_pred_V6_128B:
2389 case Hexagon::LDriq_pred_vec_V6_128B:
2390 case Hexagon::LDriv_pseudo_V6_128B:
2391 case Hexagon::LDrivv_pseudo_V6_128B:
2392 case Hexagon::LDrivv_indexed_128B:
2393 case Hexagon::STrivv_indexed_128B:
2394 case Hexagon::V6_vL32b_ai_128B:
2395 case Hexagon::V6_vS32b_ai_128B:
2396 case Hexagon::V6_vL32Ub_ai_128B:
2397 case Hexagon::V6_vS32Ub_ai_128B:
2398 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2399 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2400
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002401 case Hexagon::J2_loop0i:
2402 case Hexagon::J2_loop1i:
2403 return isUInt<10>(Offset);
2404 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002405
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002406 if (Extend)
2407 return true;
2408
2409 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002410 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002411 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002412 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2413 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2414
Colin LeMahieu947cd702014-12-23 20:44:59 +00002415 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002416 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002417 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2418 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2419
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002420 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002421 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002422 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002423 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2424 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2425
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002426 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002427 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002428 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002429 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2430 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2431
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002432 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002433 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2434 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2435
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002436 case Hexagon::L4_iadd_memopw_io :
2437 case Hexagon::L4_isub_memopw_io :
2438 case Hexagon::L4_add_memopw_io :
2439 case Hexagon::L4_sub_memopw_io :
2440 case Hexagon::L4_and_memopw_io :
2441 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002442 return (0 <= Offset && Offset <= 255);
2443
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002444 case Hexagon::L4_iadd_memoph_io :
2445 case Hexagon::L4_isub_memoph_io :
2446 case Hexagon::L4_add_memoph_io :
2447 case Hexagon::L4_sub_memoph_io :
2448 case Hexagon::L4_and_memoph_io :
2449 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002450 return (0 <= Offset && Offset <= 127);
2451
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002452 case Hexagon::L4_iadd_memopb_io :
2453 case Hexagon::L4_isub_memopb_io :
2454 case Hexagon::L4_add_memopb_io :
2455 case Hexagon::L4_sub_memopb_io :
2456 case Hexagon::L4_and_memopb_io :
2457 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002458 return (0 <= Offset && Offset <= 63);
2459
2460 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
2461 // any size. Later pass knows how to handle it.
2462 case Hexagon::STriw_pred:
2463 case Hexagon::LDriw_pred:
2464 return true;
2465
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002466 case Hexagon::TFR_FI:
2467 case Hexagon::TFR_FIA:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002468 case Hexagon::INLINEASM:
2469 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002470
2471 case Hexagon::L2_ploadrbt_io:
2472 case Hexagon::L2_ploadrbf_io:
2473 case Hexagon::L2_ploadrubt_io:
2474 case Hexagon::L2_ploadrubf_io:
2475 case Hexagon::S2_pstorerbt_io:
2476 case Hexagon::S2_pstorerbf_io:
2477 case Hexagon::S4_storeirb_io:
2478 case Hexagon::S4_storeirbt_io:
2479 case Hexagon::S4_storeirbf_io:
2480 return isUInt<6>(Offset);
2481
2482 case Hexagon::L2_ploadrht_io:
2483 case Hexagon::L2_ploadrhf_io:
2484 case Hexagon::L2_ploadruht_io:
2485 case Hexagon::L2_ploadruhf_io:
2486 case Hexagon::S2_pstorerht_io:
2487 case Hexagon::S2_pstorerhf_io:
2488 case Hexagon::S4_storeirh_io:
2489 case Hexagon::S4_storeirht_io:
2490 case Hexagon::S4_storeirhf_io:
2491 return isShiftedUInt<6,1>(Offset);
2492
2493 case Hexagon::L2_ploadrit_io:
2494 case Hexagon::L2_ploadrif_io:
2495 case Hexagon::S2_pstorerit_io:
2496 case Hexagon::S2_pstorerif_io:
2497 case Hexagon::S4_storeiri_io:
2498 case Hexagon::S4_storeirit_io:
2499 case Hexagon::S4_storeirif_io:
2500 return isShiftedUInt<6,2>(Offset);
2501
2502 case Hexagon::L2_ploadrdt_io:
2503 case Hexagon::L2_ploadrdf_io:
2504 case Hexagon::S2_pstorerdt_io:
2505 case Hexagon::S2_pstorerdf_io:
2506 return isShiftedUInt<6,3>(Offset);
2507 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002508
Benjamin Kramerb6684012011-12-27 11:41:05 +00002509 llvm_unreachable("No offset range is defined for this opcode. "
2510 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002511}
2512
2513
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002514bool HexagonInstrInfo::isVecAcc(const MachineInstr *MI) const {
2515 return MI && isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002516}
2517
2518
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002519bool HexagonInstrInfo::isVecALU(const MachineInstr *MI) const {
2520 if (!MI)
Andrew Trickd06df962012-02-01 22:13:57 +00002521 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002522 const uint64_t F = get(MI->getOpcode()).TSFlags;
2523 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2524 return
2525 V == HexagonII::TypeCVI_VA ||
2526 V == HexagonII::TypeCVI_VA_DV;
2527}
Andrew Trickd06df962012-02-01 22:13:57 +00002528
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002529
2530bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr *ProdMI,
2531 const MachineInstr *ConsMI) const {
2532 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2533 return true;
2534
2535 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2536 return true;
2537
2538 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002539 return true;
2540
2541 return false;
2542}
Jyotsna Verma84256432013-03-01 17:37:13 +00002543
Jyotsna Verma84256432013-03-01 17:37:13 +00002544
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002545/// \brief Can these instructions execute at the same time in a bundle.
2546bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr *First,
2547 const MachineInstr *Second) const {
2548 if (DisableNVSchedule)
2549 return false;
2550 if (mayBeNewStore(Second)) {
2551 // Make sure the definition of the first instruction is the value being
2552 // stored.
2553 const MachineOperand &Stored =
2554 Second->getOperand(Second->getNumOperands() - 1);
2555 if (!Stored.isReg())
2556 return false;
2557 for (unsigned i = 0, e = First->getNumOperands(); i < e; ++i) {
2558 const MachineOperand &Op = First->getOperand(i);
2559 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2560 return true;
2561 }
2562 }
2563 return false;
2564}
2565
2566
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002567bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2568 for (auto &I : *B)
2569 if (I.isEHLabel())
2570 return true;
2571 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002572}
2573
Jyotsna Verma84256432013-03-01 17:37:13 +00002574
2575// Returns true if an instruction can be converted into a non-extended
2576// equivalent instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002577bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002578 short NonExtOpcode;
2579 // Check if the instruction has a register form that uses register in place
2580 // of the extended operand, if so return that as the non-extended form.
2581 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2582 return true;
2583
2584 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002585 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002586
2587 switch (getAddrMode(MI)) {
2588 case HexagonII::Absolute :
2589 // Load/store with absolute addressing mode can be converted into
2590 // base+offset mode.
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00002591 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002592 break;
2593 case HexagonII::BaseImmOffset :
2594 // Load/store with base+offset addressing mode can be converted into
2595 // base+register offset addressing mode. However left shift operand should
2596 // be set to 0.
2597 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2598 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002599 case HexagonII::BaseLongOffset:
2600 NonExtOpcode = Hexagon::getRegShlForm(MI->getOpcode());
2601 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002602 default:
2603 return false;
2604 }
2605 if (NonExtOpcode < 0)
2606 return false;
2607 return true;
2608 }
2609 return false;
2610}
2611
Jyotsna Verma84256432013-03-01 17:37:13 +00002612
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002613bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002614 return Hexagon::getRealHWInstr(MI->getOpcode(),
2615 Hexagon::InstrType_Pseudo) >= 0;
2616}
2617
2618
2619bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2620 const {
2621 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2622 while (I != E) {
2623 if (I->isBarrier())
2624 return true;
2625 ++I;
2626 }
2627 return false;
2628}
2629
2630
2631// Returns true, if a LD insn can be promoted to a cur load.
2632bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr *MI) const {
2633 auto &HST = MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2634 const uint64_t F = MI->getDesc().TSFlags;
2635 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2636 HST.hasV60TOps();
2637}
2638
2639
2640// Returns true, if a ST insn can be promoted to a new-value store.
2641bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
2642 const uint64_t F = MI->getDesc().TSFlags;
2643 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2644}
2645
2646
2647bool HexagonInstrInfo::producesStall(const MachineInstr *ProdMI,
2648 const MachineInstr *ConsMI) const {
2649 // There is no stall when ProdMI is not a V60 vector.
2650 if (!isV60VectorInstruction(ProdMI))
2651 return false;
2652
2653 // There is no stall when ProdMI and ConsMI are not dependent.
2654 if (!isDependent(ProdMI, ConsMI))
2655 return false;
2656
2657 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2658 // are scheduled in consecutive packets.
2659 if (isVecUsableNextPacket(ProdMI, ConsMI))
2660 return false;
2661
2662 return true;
2663}
2664
2665
2666bool HexagonInstrInfo::producesStall(const MachineInstr *MI,
2667 MachineBasicBlock::const_instr_iterator BII) const {
2668 // There is no stall when I is not a V60 vector.
2669 if (!isV60VectorInstruction(MI))
2670 return false;
2671
2672 MachineBasicBlock::const_instr_iterator MII = BII;
2673 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2674
2675 if (!(*MII).isBundle()) {
2676 const MachineInstr *J = &*MII;
2677 if (!isV60VectorInstruction(J))
2678 return false;
2679 else if (isVecUsableNextPacket(J, MI))
2680 return false;
2681 return true;
2682 }
2683
2684 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
2685 const MachineInstr *J = &*MII;
2686 if (producesStall(J, MI))
2687 return true;
2688 }
2689 return false;
2690}
2691
2692
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002693bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002694 unsigned PredReg) const {
2695 for (unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002696 const MachineOperand &MO = MI->getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002697 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
2698 return false; // Predicate register must be explicitly defined.
2699 }
2700
2701 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2702 // memd_locked cannot be used as .new as well,
2703 // but we don't seem to have these instructions defined.
2704 return MI->getOpcode() != Hexagon::A4_tlbmatch;
2705}
2706
2707
2708bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
2709 return (Opcode == Hexagon::J2_jumpt) ||
2710 (Opcode == Hexagon::J2_jumpf) ||
2711 (Opcode == Hexagon::J2_jumptnew) ||
2712 (Opcode == Hexagon::J2_jumpfnew) ||
2713 (Opcode == Hexagon::J2_jumptnewpt) ||
2714 (Opcode == Hexagon::J2_jumpfnewpt);
2715}
2716
2717
2718bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2719 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2720 return false;
2721 return !isPredicatedTrue(Cond[0].getImm());
2722}
2723
2724
2725unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
2726 const uint64_t F = MI->getDesc().TSFlags;
2727 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
2728}
2729
2730
2731// Returns the base register in a memory access (load/store). The offset is
2732// returned in Offset and the access size is returned in AccessSize.
2733unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr *MI,
2734 int &Offset, unsigned &AccessSize) const {
2735 // Return if it is not a base+offset type instruction or a MemOp.
2736 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
2737 getAddrMode(MI) != HexagonII::BaseLongOffset &&
2738 !isMemOp(MI) && !isPostIncrement(MI))
2739 return 0;
2740
2741 // Since it is a memory access instruction, getMemAccessSize() should never
2742 // return 0.
2743 assert (getMemAccessSize(MI) &&
2744 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
2745
2746 // Return Values of getMemAccessSize() are
2747 // 0 - Checked in the assert above.
2748 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
2749 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
2750 AccessSize = (1U << (getMemAccessSize(MI) - 1));
2751
2752 unsigned basePos = 0, offsetPos = 0;
2753 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
2754 return 0;
2755
2756 // Post increment updates its EA after the mem access,
2757 // so we need to treat its offset as zero.
2758 if (isPostIncrement(MI))
2759 Offset = 0;
2760 else {
2761 Offset = MI->getOperand(offsetPos).getImm();
2762 }
2763
2764 return MI->getOperand(basePos).getReg();
2765}
2766
2767
2768/// Return the position of the base and offset operands for this instruction.
2769bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr *MI,
2770 unsigned &BasePos, unsigned &OffsetPos) const {
2771 // Deal with memops first.
2772 if (isMemOp(MI)) {
2773 assert (MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
2774 "Bad Memop.");
2775 BasePos = 0;
2776 OffsetPos = 1;
2777 } else if (MI->mayStore()) {
2778 BasePos = 0;
2779 OffsetPos = 1;
2780 } else if (MI->mayLoad()) {
2781 BasePos = 1;
2782 OffsetPos = 2;
2783 } else
2784 return false;
2785
2786 if (isPredicated(MI)) {
2787 BasePos++;
2788 OffsetPos++;
2789 }
2790 if (isPostIncrement(MI)) {
2791 BasePos++;
2792 OffsetPos++;
2793 }
2794
2795 if (!MI->getOperand(BasePos).isReg() || !MI->getOperand(OffsetPos).isImm())
2796 return false;
2797
2798 return true;
2799}
2800
2801
2802// Inserts branching instructions in reverse order of their occurence.
2803// e.g. jump_t t1 (i1)
2804// jump t2 (i2)
2805// Jumpers = {i2, i1}
2806SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
2807 MachineBasicBlock& MBB) const {
2808 SmallVector<MachineInstr*, 2> Jumpers;
2809 // If the block has no terminators, it just falls into the block after it.
2810 MachineBasicBlock::instr_iterator I = MBB.instr_end();
2811 if (I == MBB.instr_begin())
2812 return Jumpers;
2813
2814 // A basic block may looks like this:
2815 //
2816 // [ insn
2817 // EH_LABEL
2818 // insn
2819 // insn
2820 // insn
2821 // EH_LABEL
2822 // insn ]
2823 //
2824 // It has two succs but does not have a terminator
2825 // Don't know how to handle it.
2826 do {
2827 --I;
2828 if (I->isEHLabel())
2829 return Jumpers;
2830 } while (I != MBB.instr_begin());
2831
2832 I = MBB.instr_end();
2833 --I;
2834
2835 while (I->isDebugValue()) {
2836 if (I == MBB.instr_begin())
2837 return Jumpers;
2838 --I;
2839 }
2840 if (!isUnpredicatedTerminator(&*I))
2841 return Jumpers;
2842
2843 // Get the last instruction in the block.
2844 MachineInstr *LastInst = &*I;
2845 Jumpers.push_back(LastInst);
2846 MachineInstr *SecondLastInst = nullptr;
2847 // Find one more terminator if present.
2848 do {
2849 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
2850 if (!SecondLastInst) {
2851 SecondLastInst = &*I;
2852 Jumpers.push_back(SecondLastInst);
2853 } else // This is a third branch.
2854 return Jumpers;
2855 }
2856 if (I == MBB.instr_begin())
2857 break;
2858 --I;
2859 } while (true);
2860 return Jumpers;
2861}
2862
2863
2864// Returns Operand Index for the constant extended instruction.
2865unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
2866 const uint64_t F = MI->getDesc().TSFlags;
2867 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
2868}
2869
2870// See if instruction could potentially be a duplex candidate.
2871// If so, return its group. Zero otherwise.
2872HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
2873 const MachineInstr *MI) const {
2874 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
2875
2876 switch (MI->getOpcode()) {
2877 default:
2878 return HexagonII::HCG_None;
2879 //
2880 // Compound pairs.
2881 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
2882 // "Rd16=#U6 ; jump #r9:2"
2883 // "Rd16=Rs16 ; jump #r9:2"
2884 //
2885 case Hexagon::C2_cmpeq:
2886 case Hexagon::C2_cmpgt:
2887 case Hexagon::C2_cmpgtu:
2888 DstReg = MI->getOperand(0).getReg();
2889 Src1Reg = MI->getOperand(1).getReg();
2890 Src2Reg = MI->getOperand(2).getReg();
2891 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2892 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2893 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
2894 return HexagonII::HCG_A;
2895 break;
2896 case Hexagon::C2_cmpeqi:
2897 case Hexagon::C2_cmpgti:
2898 case Hexagon::C2_cmpgtui:
2899 // P0 = cmp.eq(Rs,#u2)
2900 DstReg = MI->getOperand(0).getReg();
2901 SrcReg = MI->getOperand(1).getReg();
2902 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2903 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2904 isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
2905 ((isUInt<5>(MI->getOperand(2).getImm())) ||
2906 (MI->getOperand(2).getImm() == -1)))
2907 return HexagonII::HCG_A;
2908 break;
2909 case Hexagon::A2_tfr:
2910 // Rd = Rs
2911 DstReg = MI->getOperand(0).getReg();
2912 SrcReg = MI->getOperand(1).getReg();
2913 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
2914 return HexagonII::HCG_A;
2915 break;
2916 case Hexagon::A2_tfrsi:
2917 // Rd = #u6
2918 // Do not test for #u6 size since the const is getting extended
2919 // regardless and compound could be formed.
2920 DstReg = MI->getOperand(0).getReg();
2921 if (isIntRegForSubInst(DstReg))
2922 return HexagonII::HCG_A;
2923 break;
2924 case Hexagon::S2_tstbit_i:
2925 DstReg = MI->getOperand(0).getReg();
2926 Src1Reg = MI->getOperand(1).getReg();
2927 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2928 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2929 MI->getOperand(2).isImm() &&
2930 isIntRegForSubInst(Src1Reg) && (MI->getOperand(2).getImm() == 0))
2931 return HexagonII::HCG_A;
2932 break;
2933 // The fact that .new form is used pretty much guarantees
2934 // that predicate register will match. Nevertheless,
2935 // there could be some false positives without additional
2936 // checking.
2937 case Hexagon::J2_jumptnew:
2938 case Hexagon::J2_jumpfnew:
2939 case Hexagon::J2_jumptnewpt:
2940 case Hexagon::J2_jumpfnewpt:
2941 Src1Reg = MI->getOperand(0).getReg();
2942 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
2943 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
2944 return HexagonII::HCG_B;
2945 break;
2946 // Transfer and jump:
2947 // Rd=#U6 ; jump #r9:2
2948 // Rd=Rs ; jump #r9:2
2949 // Do not test for jump range here.
2950 case Hexagon::J2_jump:
2951 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
2952 return HexagonII::HCG_C;
2953 break;
2954 }
2955
2956 return HexagonII::HCG_None;
2957}
2958
2959
2960// Returns -1 when there is no opcode found.
2961unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr *GA,
2962 const MachineInstr *GB) const {
2963 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
2964 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
2965 if ((GA->getOpcode() != Hexagon::C2_cmpeqi) ||
2966 (GB->getOpcode() != Hexagon::J2_jumptnew))
2967 return -1;
2968 unsigned DestReg = GA->getOperand(0).getReg();
2969 if (!GB->readsRegister(DestReg))
2970 return -1;
2971 if (DestReg == Hexagon::P0)
2972 return Hexagon::J4_cmpeqi_tp0_jump_nt;
2973 if (DestReg == Hexagon::P1)
2974 return Hexagon::J4_cmpeqi_tp1_jump_nt;
2975 return -1;
2976}
2977
2978
2979int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
2980 enum Hexagon::PredSense inPredSense;
2981 inPredSense = invertPredicate ? Hexagon::PredSense_false :
2982 Hexagon::PredSense_true;
2983 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
2984 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
2985 return CondOpcode;
2986
2987 // This switch case will be removed once all the instructions have been
2988 // modified to use relation maps.
2989 switch(Opc) {
2990 case Hexagon::TFRI_f:
2991 return !invertPredicate ? Hexagon::TFRI_cPt_f :
2992 Hexagon::TFRI_cNotPt_f;
2993 }
2994
2995 llvm_unreachable("Unexpected predicable instruction");
2996}
2997
2998
2999// Return the cur value instruction for a given store.
3000int HexagonInstrInfo::getDotCurOp(const MachineInstr* MI) const {
3001 switch (MI->getOpcode()) {
3002 default: llvm_unreachable("Unknown .cur type");
3003 case Hexagon::V6_vL32b_pi:
3004 return Hexagon::V6_vL32b_cur_pi;
3005 case Hexagon::V6_vL32b_ai:
3006 return Hexagon::V6_vL32b_cur_ai;
3007 //128B
3008 case Hexagon::V6_vL32b_pi_128B:
3009 return Hexagon::V6_vL32b_cur_pi_128B;
3010 case Hexagon::V6_vL32b_ai_128B:
3011 return Hexagon::V6_vL32b_cur_ai_128B;
3012 }
3013 return 0;
3014}
3015
3016
3017
3018// The diagram below shows the steps involved in the conversion of a predicated
3019// store instruction to its .new predicated new-value form.
3020//
3021// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3022// ^ ^
3023// / \ (not OK. it will cause new-value store to be
3024// / X conditional on p0.new while R2 producer is
3025// / \ on p0)
3026// / \.
3027// p.new store p.old NV store
3028// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3029// ^ ^
3030// \ /
3031// \ /
3032// \ /
3033// p.old store
3034// [if (p0)memw(R0+#0)=R2]
3035//
3036//
3037// The following set of instructions further explains the scenario where
3038// conditional new-value store becomes invalid when promoted to .new predicate
3039// form.
3040//
3041// { 1) if (p0) r0 = add(r1, r2)
3042// 2) p0 = cmp.eq(r3, #0) }
3043//
3044// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3045// the first two instructions because in instr 1, r0 is conditional on old value
3046// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3047// is not valid for new-value stores.
3048// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3049// from the "Conditional Store" list. Because a predicated new value store
3050// would NOT be promoted to a double dot new store. See diagram below:
3051// This function returns yes for those stores that are predicated but not
3052// yet promoted to predicate dot new instructions.
3053//
3054// +---------------------+
3055// /-----| if (p0) memw(..)=r0 |---------\~
3056// || +---------------------+ ||
3057// promote || /\ /\ || promote
3058// || /||\ /||\ ||
3059// \||/ demote || \||/
3060// \/ || || \/
3061// +-------------------------+ || +-------------------------+
3062// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3063// +-------------------------+ || +-------------------------+
3064// || || ||
3065// || demote \||/
3066// promote || \/ NOT possible
3067// || || /\~
3068// \||/ || /||\~
3069// \/ || ||
3070// +-----------------------------+
3071// | if (p0.new) memw(..)=r0.new |
3072// +-----------------------------+
3073// Double Dot New Store
3074//
3075// Returns the most basic instruction for the .new predicated instructions and
3076// new-value stores.
3077// For example, all of the following instructions will be converted back to the
3078// same instruction:
3079// 1) if (p0.new) memw(R0+#0) = R1.new --->
3080// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3081// 3) if (p0.new) memw(R0+#0) = R1 --->
3082//
3083// To understand the translation of instruction 1 to its original form, consider
3084// a packet with 3 instructions.
3085// { p0 = cmp.eq(R0,R1)
3086// if (p0.new) R2 = add(R3, R4)
3087// R5 = add (R3, R1)
3088// }
3089// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3090//
3091// This instruction can be part of the previous packet only if both p0 and R2
3092// are promoted to .new values. This promotion happens in steps, first
3093// predicate register is promoted to .new and in the next iteration R2 is
3094// promoted. Therefore, in case of dependence check failure (due to R5) during
3095// next iteration, it should be converted back to its most basic form.
3096
3097
3098// Return the new value instruction for a given store.
3099int HexagonInstrInfo::getDotNewOp(const MachineInstr* MI) const {
3100 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
3101 if (NVOpcode >= 0) // Valid new-value store instruction.
3102 return NVOpcode;
3103
3104 switch (MI->getOpcode()) {
3105 default: llvm_unreachable("Unknown .new type");
3106 case Hexagon::S4_storerb_ur:
3107 return Hexagon::S4_storerbnew_ur;
3108
3109 case Hexagon::S2_storerb_pci:
3110 return Hexagon::S2_storerb_pci;
3111
3112 case Hexagon::S2_storeri_pci:
3113 return Hexagon::S2_storeri_pci;
3114
3115 case Hexagon::S2_storerh_pci:
3116 return Hexagon::S2_storerh_pci;
3117
3118 case Hexagon::S2_storerd_pci:
3119 return Hexagon::S2_storerd_pci;
3120
3121 case Hexagon::S2_storerf_pci:
3122 return Hexagon::S2_storerf_pci;
3123
3124 case Hexagon::V6_vS32b_ai:
3125 return Hexagon::V6_vS32b_new_ai;
3126
3127 case Hexagon::V6_vS32b_pi:
3128 return Hexagon::V6_vS32b_new_pi;
3129
3130 // 128B
3131 case Hexagon::V6_vS32b_ai_128B:
3132 return Hexagon::V6_vS32b_new_ai_128B;
3133
3134 case Hexagon::V6_vS32b_pi_128B:
3135 return Hexagon::V6_vS32b_new_pi_128B;
3136 }
3137 return 0;
3138}
3139
3140// Returns the opcode to use when converting MI, which is a conditional jump,
3141// into a conditional instruction which uses the .new value of the predicate.
3142// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003143int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003144 const MachineBranchProbabilityInfo *MBPI) const {
3145 // We assume that block can have at most two successors.
3146 bool taken = false;
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003147 const MachineBasicBlock *Src = MI->getParent();
3148 const MachineOperand *BrTarget = &MI->getOperand(1);
3149 const MachineBasicBlock *Dst = BrTarget->getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003150
3151 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3152 if (Prediction >= BranchProbability(1,2))
3153 taken = true;
3154
3155 switch (MI->getOpcode()) {
3156 case Hexagon::J2_jumpt:
3157 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3158 case Hexagon::J2_jumpf:
3159 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3160
3161 default:
3162 llvm_unreachable("Unexpected jump instruction.");
3163 }
3164}
3165
3166
3167// Return .new predicate version for an instruction.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003168int HexagonInstrInfo::getDotNewPredOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003169 const MachineBranchProbabilityInfo *MBPI) const {
3170 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
3171 if (NewOpcode >= 0) // Valid predicate new instruction
3172 return NewOpcode;
3173
3174 switch (MI->getOpcode()) {
3175 // Condtional Jumps
3176 case Hexagon::J2_jumpt:
3177 case Hexagon::J2_jumpf:
3178 return getDotNewPredJumpOp(MI, MBPI);
3179
3180 default:
3181 assert(0 && "Unknown .new type");
3182 }
3183 return 0;
3184}
3185
3186
3187int HexagonInstrInfo::getDotOldOp(const int opc) const {
3188 int NewOp = opc;
3189 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3190 NewOp = Hexagon::getPredOldOpcode(NewOp);
3191 assert(NewOp >= 0 &&
3192 "Couldn't change predicate new instruction to its old form.");
3193 }
3194
3195 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3196 NewOp = Hexagon::getNonNVStore(NewOp);
3197 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3198 }
3199 return NewOp;
3200}
3201
3202
3203// See if instruction could potentially be a duplex candidate.
3204// If so, return its group. Zero otherwise.
3205HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3206 const MachineInstr *MI) const {
3207 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3208 auto &HRI = getRegisterInfo();
3209
3210 switch (MI->getOpcode()) {
3211 default:
3212 return HexagonII::HSIG_None;
3213 //
3214 // Group L1:
3215 //
3216 // Rd = memw(Rs+#u4:2)
3217 // Rd = memub(Rs+#u4:0)
3218 case Hexagon::L2_loadri_io:
3219 DstReg = MI->getOperand(0).getReg();
3220 SrcReg = MI->getOperand(1).getReg();
3221 // Special case this one from Group L2.
3222 // Rd = memw(r29+#u5:2)
3223 if (isIntRegForSubInst(DstReg)) {
3224 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3225 HRI.getStackRegister() == SrcReg &&
3226 MI->getOperand(2).isImm() &&
3227 isShiftedUInt<5,2>(MI->getOperand(2).getImm()))
3228 return HexagonII::HSIG_L2;
3229 // Rd = memw(Rs+#u4:2)
3230 if (isIntRegForSubInst(SrcReg) &&
3231 (MI->getOperand(2).isImm() &&
3232 isShiftedUInt<4,2>(MI->getOperand(2).getImm())))
3233 return HexagonII::HSIG_L1;
3234 }
3235 break;
3236 case Hexagon::L2_loadrub_io:
3237 // Rd = memub(Rs+#u4:0)
3238 DstReg = MI->getOperand(0).getReg();
3239 SrcReg = MI->getOperand(1).getReg();
3240 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3241 MI->getOperand(2).isImm() && isUInt<4>(MI->getOperand(2).getImm()))
3242 return HexagonII::HSIG_L1;
3243 break;
3244 //
3245 // Group L2:
3246 //
3247 // Rd = memh/memuh(Rs+#u3:1)
3248 // Rd = memb(Rs+#u3:0)
3249 // Rd = memw(r29+#u5:2) - Handled above.
3250 // Rdd = memd(r29+#u5:3)
3251 // deallocframe
3252 // [if ([!]p0[.new])] dealloc_return
3253 // [if ([!]p0[.new])] jumpr r31
3254 case Hexagon::L2_loadrh_io:
3255 case Hexagon::L2_loadruh_io:
3256 // Rd = memh/memuh(Rs+#u3:1)
3257 DstReg = MI->getOperand(0).getReg();
3258 SrcReg = MI->getOperand(1).getReg();
3259 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3260 MI->getOperand(2).isImm() &&
3261 isShiftedUInt<3,1>(MI->getOperand(2).getImm()))
3262 return HexagonII::HSIG_L2;
3263 break;
3264 case Hexagon::L2_loadrb_io:
3265 // Rd = memb(Rs+#u3:0)
3266 DstReg = MI->getOperand(0).getReg();
3267 SrcReg = MI->getOperand(1).getReg();
3268 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3269 MI->getOperand(2).isImm() &&
3270 isUInt<3>(MI->getOperand(2).getImm()))
3271 return HexagonII::HSIG_L2;
3272 break;
3273 case Hexagon::L2_loadrd_io:
3274 // Rdd = memd(r29+#u5:3)
3275 DstReg = MI->getOperand(0).getReg();
3276 SrcReg = MI->getOperand(1).getReg();
3277 if (isDblRegForSubInst(DstReg, HRI) &&
3278 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3279 HRI.getStackRegister() == SrcReg &&
3280 MI->getOperand(2).isImm() &&
3281 isShiftedUInt<5,3>(MI->getOperand(2).getImm()))
3282 return HexagonII::HSIG_L2;
3283 break;
3284 // dealloc_return is not documented in Hexagon Manual, but marked
3285 // with A_SUBINSN attribute in iset_v4classic.py.
3286 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3287 case Hexagon::L4_return:
3288 case Hexagon::L2_deallocframe:
3289 return HexagonII::HSIG_L2;
3290 case Hexagon::EH_RETURN_JMPR:
3291 case Hexagon::JMPret :
3292 // jumpr r31
3293 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
3294 DstReg = MI->getOperand(0).getReg();
3295 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3296 return HexagonII::HSIG_L2;
3297 break;
3298 case Hexagon::JMPrett:
3299 case Hexagon::JMPretf:
3300 case Hexagon::JMPrettnewpt:
3301 case Hexagon::JMPretfnewpt :
3302 case Hexagon::JMPrettnew :
3303 case Hexagon::JMPretfnew :
3304 DstReg = MI->getOperand(1).getReg();
3305 SrcReg = MI->getOperand(0).getReg();
3306 // [if ([!]p0[.new])] jumpr r31
3307 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3308 (Hexagon::P0 == SrcReg)) &&
3309 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3310 return HexagonII::HSIG_L2;
3311 break;
3312 case Hexagon::L4_return_t :
3313 case Hexagon::L4_return_f :
3314 case Hexagon::L4_return_tnew_pnt :
3315 case Hexagon::L4_return_fnew_pnt :
3316 case Hexagon::L4_return_tnew_pt :
3317 case Hexagon::L4_return_fnew_pt :
3318 // [if ([!]p0[.new])] dealloc_return
3319 SrcReg = MI->getOperand(0).getReg();
3320 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3321 return HexagonII::HSIG_L2;
3322 break;
3323 //
3324 // Group S1:
3325 //
3326 // memw(Rs+#u4:2) = Rt
3327 // memb(Rs+#u4:0) = Rt
3328 case Hexagon::S2_storeri_io:
3329 // Special case this one from Group S2.
3330 // memw(r29+#u5:2) = Rt
3331 Src1Reg = MI->getOperand(0).getReg();
3332 Src2Reg = MI->getOperand(2).getReg();
3333 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3334 isIntRegForSubInst(Src2Reg) &&
3335 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3336 isShiftedUInt<5,2>(MI->getOperand(1).getImm()))
3337 return HexagonII::HSIG_S2;
3338 // memw(Rs+#u4:2) = Rt
3339 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3340 MI->getOperand(1).isImm() &&
3341 isShiftedUInt<4,2>(MI->getOperand(1).getImm()))
3342 return HexagonII::HSIG_S1;
3343 break;
3344 case Hexagon::S2_storerb_io:
3345 // memb(Rs+#u4:0) = Rt
3346 Src1Reg = MI->getOperand(0).getReg();
3347 Src2Reg = MI->getOperand(2).getReg();
3348 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3349 MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()))
3350 return HexagonII::HSIG_S1;
3351 break;
3352 //
3353 // Group S2:
3354 //
3355 // memh(Rs+#u3:1) = Rt
3356 // memw(r29+#u5:2) = Rt
3357 // memd(r29+#s6:3) = Rtt
3358 // memw(Rs+#u4:2) = #U1
3359 // memb(Rs+#u4) = #U1
3360 // allocframe(#u5:3)
3361 case Hexagon::S2_storerh_io:
3362 // memh(Rs+#u3:1) = Rt
3363 Src1Reg = MI->getOperand(0).getReg();
3364 Src2Reg = MI->getOperand(2).getReg();
3365 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3366 MI->getOperand(1).isImm() &&
3367 isShiftedUInt<3,1>(MI->getOperand(1).getImm()))
3368 return HexagonII::HSIG_S1;
3369 break;
3370 case Hexagon::S2_storerd_io:
3371 // memd(r29+#s6:3) = Rtt
3372 Src1Reg = MI->getOperand(0).getReg();
3373 Src2Reg = MI->getOperand(2).getReg();
3374 if (isDblRegForSubInst(Src2Reg, HRI) &&
3375 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3376 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3377 isShiftedInt<6,3>(MI->getOperand(1).getImm()))
3378 return HexagonII::HSIG_S2;
3379 break;
3380 case Hexagon::S4_storeiri_io:
3381 // memw(Rs+#u4:2) = #U1
3382 Src1Reg = MI->getOperand(0).getReg();
3383 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3384 isShiftedUInt<4,2>(MI->getOperand(1).getImm()) &&
3385 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3386 return HexagonII::HSIG_S2;
3387 break;
3388 case Hexagon::S4_storeirb_io:
3389 // memb(Rs+#u4) = #U1
3390 Src1Reg = MI->getOperand(0).getReg();
3391 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3392 isUInt<4>(MI->getOperand(1).getImm()) && MI->getOperand(2).isImm() &&
3393 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3394 return HexagonII::HSIG_S2;
3395 break;
3396 case Hexagon::S2_allocframe:
3397 if (MI->getOperand(0).isImm() &&
3398 isShiftedUInt<5,3>(MI->getOperand(0).getImm()))
3399 return HexagonII::HSIG_S1;
3400 break;
3401 //
3402 // Group A:
3403 //
3404 // Rx = add(Rx,#s7)
3405 // Rd = Rs
3406 // Rd = #u6
3407 // Rd = #-1
3408 // if ([!]P0[.new]) Rd = #0
3409 // Rd = add(r29,#u6:2)
3410 // Rx = add(Rx,Rs)
3411 // P0 = cmp.eq(Rs,#u2)
3412 // Rdd = combine(#0,Rs)
3413 // Rdd = combine(Rs,#0)
3414 // Rdd = combine(#u2,#U2)
3415 // Rd = add(Rs,#1)
3416 // Rd = add(Rs,#-1)
3417 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3418 // Rd = and(Rs,#1)
3419 case Hexagon::A2_addi:
3420 DstReg = MI->getOperand(0).getReg();
3421 SrcReg = MI->getOperand(1).getReg();
3422 if (isIntRegForSubInst(DstReg)) {
3423 // Rd = add(r29,#u6:2)
3424 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3425 HRI.getStackRegister() == SrcReg && MI->getOperand(2).isImm() &&
3426 isShiftedUInt<6,2>(MI->getOperand(2).getImm()))
3427 return HexagonII::HSIG_A;
3428 // Rx = add(Rx,#s7)
3429 if ((DstReg == SrcReg) && MI->getOperand(2).isImm() &&
3430 isInt<7>(MI->getOperand(2).getImm()))
3431 return HexagonII::HSIG_A;
3432 // Rd = add(Rs,#1)
3433 // Rd = add(Rs,#-1)
3434 if (isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3435 ((MI->getOperand(2).getImm() == 1) ||
3436 (MI->getOperand(2).getImm() == -1)))
3437 return HexagonII::HSIG_A;
3438 }
3439 break;
3440 case Hexagon::A2_add:
3441 // Rx = add(Rx,Rs)
3442 DstReg = MI->getOperand(0).getReg();
3443 Src1Reg = MI->getOperand(1).getReg();
3444 Src2Reg = MI->getOperand(2).getReg();
3445 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3446 isIntRegForSubInst(Src2Reg))
3447 return HexagonII::HSIG_A;
3448 break;
3449 case Hexagon::A2_andir:
3450 // Same as zxtb.
3451 // Rd16=and(Rs16,#255)
3452 // Rd16=and(Rs16,#1)
3453 DstReg = MI->getOperand(0).getReg();
3454 SrcReg = MI->getOperand(1).getReg();
3455 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3456 MI->getOperand(2).isImm() &&
3457 ((MI->getOperand(2).getImm() == 1) ||
3458 (MI->getOperand(2).getImm() == 255)))
3459 return HexagonII::HSIG_A;
3460 break;
3461 case Hexagon::A2_tfr:
3462 // Rd = Rs
3463 DstReg = MI->getOperand(0).getReg();
3464 SrcReg = MI->getOperand(1).getReg();
3465 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3466 return HexagonII::HSIG_A;
3467 break;
3468 case Hexagon::A2_tfrsi:
3469 // Rd = #u6
3470 // Do not test for #u6 size since the const is getting extended
3471 // regardless and compound could be formed.
3472 // Rd = #-1
3473 DstReg = MI->getOperand(0).getReg();
3474 if (isIntRegForSubInst(DstReg))
3475 return HexagonII::HSIG_A;
3476 break;
3477 case Hexagon::C2_cmoveit:
3478 case Hexagon::C2_cmovenewit:
3479 case Hexagon::C2_cmoveif:
3480 case Hexagon::C2_cmovenewif:
3481 // if ([!]P0[.new]) Rd = #0
3482 // Actual form:
3483 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
3484 DstReg = MI->getOperand(0).getReg();
3485 SrcReg = MI->getOperand(1).getReg();
3486 if (isIntRegForSubInst(DstReg) &&
3487 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
3488 MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0)
3489 return HexagonII::HSIG_A;
3490 break;
3491 case Hexagon::C2_cmpeqi:
3492 // P0 = cmp.eq(Rs,#u2)
3493 DstReg = MI->getOperand(0).getReg();
3494 SrcReg = MI->getOperand(1).getReg();
3495 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3496 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
3497 MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm()))
3498 return HexagonII::HSIG_A;
3499 break;
3500 case Hexagon::A2_combineii:
3501 case Hexagon::A4_combineii:
3502 // Rdd = combine(#u2,#U2)
3503 DstReg = MI->getOperand(0).getReg();
3504 if (isDblRegForSubInst(DstReg, HRI) &&
3505 ((MI->getOperand(1).isImm() && isUInt<2>(MI->getOperand(1).getImm())) ||
3506 (MI->getOperand(1).isGlobal() &&
3507 isUInt<2>(MI->getOperand(1).getOffset()))) &&
3508 ((MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm())) ||
3509 (MI->getOperand(2).isGlobal() &&
3510 isUInt<2>(MI->getOperand(2).getOffset()))))
3511 return HexagonII::HSIG_A;
3512 break;
3513 case Hexagon::A4_combineri:
3514 // Rdd = combine(Rs,#0)
3515 DstReg = MI->getOperand(0).getReg();
3516 SrcReg = MI->getOperand(1).getReg();
3517 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3518 ((MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) ||
3519 (MI->getOperand(2).isGlobal() && MI->getOperand(2).getOffset() == 0)))
3520 return HexagonII::HSIG_A;
3521 break;
3522 case Hexagon::A4_combineir:
3523 // Rdd = combine(#0,Rs)
3524 DstReg = MI->getOperand(0).getReg();
3525 SrcReg = MI->getOperand(2).getReg();
3526 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3527 ((MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0) ||
3528 (MI->getOperand(1).isGlobal() && MI->getOperand(1).getOffset() == 0)))
3529 return HexagonII::HSIG_A;
3530 break;
3531 case Hexagon::A2_sxtb:
3532 case Hexagon::A2_sxth:
3533 case Hexagon::A2_zxtb:
3534 case Hexagon::A2_zxth:
3535 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3536 DstReg = MI->getOperand(0).getReg();
3537 SrcReg = MI->getOperand(1).getReg();
3538 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3539 return HexagonII::HSIG_A;
3540 break;
3541 }
3542
3543 return HexagonII::HSIG_None;
3544}
3545
3546
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003547short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003548 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Real);
3549}
3550
3551
3552// Return first non-debug instruction in the basic block.
3553MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3554 const {
3555 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
3556 MachineInstr *MI = &*MII;
3557 if (MI->isDebugValue())
3558 continue;
3559 return MI;
3560 }
3561 return nullptr;
3562}
3563
3564
3565unsigned HexagonInstrInfo::getInstrTimingClassLatency(
3566 const InstrItineraryData *ItinData, const MachineInstr *MI) const {
3567 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3568 // still have a MinLatency property, which getStageLatency checks.
3569 if (!ItinData)
3570 return getInstrLatency(ItinData, MI);
3571
3572 // Get the latency embedded in the itinerary. If we're not using timing class
3573 // latencies or if we using BSB scheduling, then restrict the maximum latency
3574 // to 1 (that is, either 0 or 1).
3575 if (MI->isTransient())
3576 return 0;
3577 unsigned Latency = ItinData->getStageLatency(MI->getDesc().getSchedClass());
3578 if (!EnableTimingClassLatency ||
3579 MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>().
3580 useBSBScheduling())
3581 if (Latency > 1)
3582 Latency = 1;
3583 return Latency;
3584}
3585
3586
3587// inverts the predication logic.
3588// p -> NotP
3589// NotP -> P
3590bool HexagonInstrInfo::getInvertedPredSense(
3591 SmallVectorImpl<MachineOperand> &Cond) const {
3592 if (Cond.empty())
3593 return false;
3594 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3595 Cond[0].setImm(Opc);
3596 return true;
3597}
3598
3599
3600unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3601 int InvPredOpcode;
3602 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3603 : Hexagon::getTruePredOpcode(Opc);
3604 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3605 return InvPredOpcode;
3606
3607 llvm_unreachable("Unexpected predicated instruction");
3608}
3609
3610
3611// Returns the max value that doesn't need to be extended.
3612int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
3613 const uint64_t F = MI->getDesc().TSFlags;
3614 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3615 & HexagonII::ExtentSignedMask;
3616 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3617 & HexagonII::ExtentBitsMask;
3618
3619 if (isSigned) // if value is signed
3620 return ~(-1U << (bits - 1));
3621 else
3622 return ~(-1U << bits);
3623}
3624
3625
3626unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr* MI) const {
3627 const uint64_t F = MI->getDesc().TSFlags;
3628 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3629}
3630
3631
3632// Returns the min value that doesn't need to be extended.
3633int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
3634 const uint64_t F = MI->getDesc().TSFlags;
3635 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3636 & HexagonII::ExtentSignedMask;
3637 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3638 & HexagonII::ExtentBitsMask;
3639
3640 if (isSigned) // if value is signed
3641 return -1U << (bits - 1);
3642 else
3643 return 0;
3644}
3645
3646
3647// Returns opcode of the non-extended equivalent instruction.
3648short HexagonInstrInfo::getNonExtOpcode(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003649 // Check if the instruction has a register form that uses register in place
3650 // of the extended operand, if so return that as the non-extended form.
3651 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
3652 if (NonExtOpcode >= 0)
3653 return NonExtOpcode;
3654
3655 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003656 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003657 switch (getAddrMode(MI)) {
3658 case HexagonII::Absolute :
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00003659 return Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003660 case HexagonII::BaseImmOffset :
3661 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003662 case HexagonII::BaseLongOffset:
3663 return Hexagon::getRegShlForm(MI->getOpcode());
3664
Jyotsna Verma84256432013-03-01 17:37:13 +00003665 default:
3666 return -1;
3667 }
3668 }
3669 return -1;
3670}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00003671
Brendon Cahoondf43e682015-05-08 16:16:29 +00003672
Ahmed Bougachac88bf542015-06-11 19:30:37 +00003673bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003674 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00003675 if (Cond.empty())
3676 return false;
3677 assert(Cond.size() == 2);
3678 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
3679 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3680 return false;
3681 }
3682 PredReg = Cond[1].getReg();
3683 PredRegPos = 1;
3684 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3685 PredRegFlags = 0;
3686 if (Cond[1].isImplicit())
3687 PredRegFlags = RegState::Implicit;
3688 if (Cond[1].isUndef())
3689 PredRegFlags |= RegState::Undef;
3690 return true;
3691}
3692
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003693
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003694short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003695 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Pseudo);
3696}
3697
3698
3699short HexagonInstrInfo::getRegForm(const MachineInstr *MI) const {
3700 return Hexagon::getRegForm(MI->getOpcode());
3701}
3702
3703
3704// Return the number of bytes required to encode the instruction.
3705// Hexagon instructions are fixed length, 4 bytes, unless they
3706// use a constant extender, which requires another 4 bytes.
3707// For debug instructions and prolog labels, return 0.
3708unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
3709 if (MI->isDebugValue() || MI->isPosition())
3710 return 0;
3711
3712 unsigned Size = MI->getDesc().getSize();
3713 if (!Size)
3714 // Assume the default insn size in case it cannot be determined
3715 // for whatever reason.
3716 Size = HEXAGON_INSTR_SIZE;
3717
3718 if (isConstExtended(MI) || isExtended(MI))
3719 Size += HEXAGON_INSTR_SIZE;
3720
3721 // Try and compute number of instructions in asm.
3722 if (BranchRelaxAsmLarge && MI->getOpcode() == Hexagon::INLINEASM) {
3723 const MachineBasicBlock &MBB = *MI->getParent();
3724 const MachineFunction *MF = MBB.getParent();
3725 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
3726
3727 // Count the number of register definitions to find the asm string.
3728 unsigned NumDefs = 0;
3729 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
3730 ++NumDefs)
3731 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
3732
3733 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
3734 // Disassemble the AsmStr and approximate number of instructions.
3735 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
3736 Size = getInlineAsmLength(AsmStr, *MAI);
3737 }
3738
3739 return Size;
3740}
3741
3742
3743uint64_t HexagonInstrInfo::getType(const MachineInstr* MI) const {
3744 const uint64_t F = MI->getDesc().TSFlags;
3745 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
3746}
3747
3748
3749unsigned HexagonInstrInfo::getUnits(const MachineInstr* MI) const {
3750 const TargetSubtargetInfo &ST = MI->getParent()->getParent()->getSubtarget();
3751 const InstrItineraryData &II = *ST.getInstrItineraryData();
3752 const InstrStage &IS = *II.beginStage(MI->getDesc().getSchedClass());
3753
3754 return IS.getUnits();
3755}
3756
3757
3758unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
3759 const uint64_t F = get(Opcode).TSFlags;
3760 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
3761}
3762
3763
3764// Calculate size of the basic block without debug instructions.
3765unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
3766 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
3767}
3768
3769
3770unsigned HexagonInstrInfo::nonDbgBundleSize(
3771 MachineBasicBlock::const_iterator BundleHead) const {
3772 assert(BundleHead->isBundle() && "Not a bundle header");
3773 auto MII = BundleHead.getInstrIterator();
3774 // Skip the bundle header.
3775 return nonDbgMICount(++MII, getBundleEnd(BundleHead));
3776}
3777
3778
3779/// immediateExtend - Changes the instruction in place to one using an immediate
3780/// extender.
3781void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
3782 assert((isExtendable(MI)||isConstExtended(MI)) &&
3783 "Instruction must be extendable");
3784 // Find which operand is extendable.
3785 short ExtOpNum = getCExtOpNum(MI);
3786 MachineOperand &MO = MI->getOperand(ExtOpNum);
3787 // This needs to be something we understand.
3788 assert((MO.isMBB() || MO.isImm()) &&
3789 "Branch with unknown extendable field type");
3790 // Mark given operand as extended.
3791 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
3792}
3793
3794
3795bool HexagonInstrInfo::invertAndChangeJumpTarget(
3796 MachineInstr* MI, MachineBasicBlock* NewTarget) const {
3797 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
3798 << NewTarget->getNumber(); MI->dump(););
3799 assert(MI->isBranch());
3800 unsigned NewOpcode = getInvertedPredicatedOpcode(MI->getOpcode());
3801 int TargetPos = MI->getNumOperands() - 1;
3802 // In general branch target is the last operand,
3803 // but some implicit defs added at the end might change it.
3804 while ((TargetPos > -1) && !MI->getOperand(TargetPos).isMBB())
3805 --TargetPos;
3806 assert((TargetPos >= 0) && MI->getOperand(TargetPos).isMBB());
3807 MI->getOperand(TargetPos).setMBB(NewTarget);
3808 if (EnableBranchPrediction && isPredicatedNew(MI)) {
3809 NewOpcode = reversePrediction(NewOpcode);
3810 }
3811 MI->setDesc(get(NewOpcode));
3812 return true;
3813}
3814
3815
3816void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
3817 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
3818 MachineFunction::iterator A = MF.begin();
3819 MachineBasicBlock &B = *A;
3820 MachineBasicBlock::iterator I = B.begin();
3821 MachineInstr *MI = &*I;
3822 DebugLoc DL = MI->getDebugLoc();
3823 MachineInstr *NewMI;
3824
3825 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
3826 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
3827 NewMI = BuildMI(B, MI, DL, get(insn));
3828 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
3829 " Class: " << NewMI->getDesc().getSchedClass());
3830 NewMI->eraseFromParent();
3831 }
3832 /* --- The code above is used to generate complete set of Hexagon Insn --- */
3833}
3834
3835
3836// inverts the predication logic.
3837// p -> NotP
3838// NotP -> P
3839bool HexagonInstrInfo::reversePredSense(MachineInstr* MI) const {
3840 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI->dump());
3841 MI->setDesc(get(getInvertedPredicatedOpcode(MI->getOpcode())));
3842 return true;
3843}
3844
3845
3846// Reverse the branch prediction.
3847unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
3848 int PredRevOpcode = -1;
3849 if (isPredictedTaken(Opcode))
3850 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
3851 else
3852 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
3853 assert(PredRevOpcode > 0);
3854 return PredRevOpcode;
3855}
3856
3857
3858// TODO: Add more rigorous validation.
3859bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
3860 const {
3861 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
3862}
3863