blob: c3cf05541aae8a3ec450b0e054f9fc2befb57012 [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 Parzyszekfb338242015-10-06 15:49:14 +0000960 case Hexagon::MUX64_rr: {
961 const MachineOperand &Op0 = MI->getOperand(0);
962 const MachineOperand &Op1 = MI->getOperand(1);
963 const MachineOperand &Op2 = MI->getOperand(2);
964 const MachineOperand &Op3 = MI->getOperand(3);
965 unsigned Rd = Op0.getReg();
966 unsigned Pu = Op1.getReg();
967 unsigned Rs = Op2.getReg();
968 unsigned Rt = Op3.getReg();
969 DebugLoc DL = MI->getDebugLoc();
970 unsigned K1 = getKillRegState(Op1.isKill());
971 unsigned K2 = getKillRegState(Op2.isKill());
972 unsigned K3 = getKillRegState(Op3.isKill());
973 if (Rd != Rs)
974 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
975 .addReg(Pu, (Rd == Rt) ? K1 : 0)
976 .addReg(Rs, K2);
977 if (Rd != Rt)
978 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
979 .addReg(Pu, K1)
980 .addReg(Rt, K3);
981 MBB.erase(MI);
982 return true;
983 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000984 case Hexagon::TCRETURNi:
985 MI->setDesc(get(Hexagon::J2_jump));
986 return true;
987 case Hexagon::TCRETURNr:
988 MI->setDesc(get(Hexagon::J2_jumpr));
989 return true;
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +0000990 case Hexagon::TFRI_f:
991 case Hexagon::TFRI_cPt_f:
992 case Hexagon::TFRI_cNotPt_f: {
993 unsigned Opx = (Opc == Hexagon::TFRI_f) ? 1 : 2;
994 APFloat FVal = MI->getOperand(Opx).getFPImm()->getValueAPF();
995 APInt IVal = FVal.bitcastToAPInt();
996 MI->RemoveOperand(Opx);
997 unsigned NewOpc = (Opc == Hexagon::TFRI_f) ? Hexagon::A2_tfrsi :
998 (Opc == Hexagon::TFRI_cPt_f) ? Hexagon::C2_cmoveit :
999 Hexagon::C2_cmoveif;
1000 MI->setDesc(get(NewOpc));
1001 MI->addOperand(MachineOperand::CreateImm(IVal.getZExtValue()));
1002 return true;
1003 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001004 }
1005
1006 return false;
1007}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001008
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001009
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001010// We indicate that we want to reverse the branch by
1011// inserting the reversed branching opcode.
1012bool HexagonInstrInfo::ReverseBranchCondition(
1013 SmallVectorImpl<MachineOperand> &Cond) const {
1014 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001015 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001016 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1017 unsigned opcode = Cond[0].getImm();
1018 //unsigned temp;
1019 assert(get(opcode).isBranch() && "Should be a branching condition.");
1020 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001021 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001022 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1023 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001024 return false;
1025}
1026
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001027
1028void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1029 MachineBasicBlock::iterator MI) const {
1030 DebugLoc DL;
1031 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1032}
1033
1034
1035// Returns true if an instruction is predicated irrespective of the predicate
1036// sense. For example, all of the following will return true.
1037// if (p0) R1 = add(R2, R3)
1038// if (!p0) R1 = add(R2, R3)
1039// if (p0.new) R1 = add(R2, R3)
1040// if (!p0.new) R1 = add(R2, R3)
1041// Note: New-value stores are not included here as in the current
1042// implementation, we don't need to check their predicate sense.
1043bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00001044 const uint64_t F = MI->getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001045 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001046}
1047
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001048
1049bool HexagonInstrInfo::PredicateInstruction(MachineInstr *MI,
1050 ArrayRef<MachineOperand> Cond) const {
1051 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1052 isEndLoopN(Cond[0].getImm())) {
1053 DEBUG(dbgs() << "\nCannot predicate:"; MI->dump(););
1054 return false;
1055 }
1056 int Opc = MI->getOpcode();
1057 assert (isPredicable(MI) && "Expected predicable instruction");
1058 bool invertJump = predOpcodeHasNot(Cond);
1059
1060 // We have to predicate MI "in place", i.e. after this function returns,
1061 // MI will need to be transformed into a predicated form. To avoid com-
1062 // plicated manipulations with the operands (handling tied operands,
1063 // etc.), build a new temporary instruction, then overwrite MI with it.
1064
1065 MachineBasicBlock &B = *MI->getParent();
1066 DebugLoc DL = MI->getDebugLoc();
1067 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1068 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
1069 unsigned NOp = 0, NumOps = MI->getNumOperands();
1070 while (NOp < NumOps) {
1071 MachineOperand &Op = MI->getOperand(NOp);
1072 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1073 break;
1074 T.addOperand(Op);
1075 NOp++;
1076 }
1077
1078 unsigned PredReg, PredRegPos, PredRegFlags;
1079 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1080 (void)GotPredReg;
1081 assert(GotPredReg);
1082 T.addReg(PredReg, PredRegFlags);
1083 while (NOp < NumOps)
1084 T.addOperand(MI->getOperand(NOp++));
1085
1086 MI->setDesc(get(PredOpc));
1087 while (unsigned n = MI->getNumOperands())
1088 MI->RemoveOperand(n-1);
1089 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
1090 MI->addOperand(T->getOperand(i));
1091
1092 MachineBasicBlock::instr_iterator TI = T->getIterator();
1093 B.erase(TI);
1094
1095 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1096 MRI.clearKillFlags(PredReg);
1097 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001098}
1099
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001100
1101bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1102 ArrayRef<MachineOperand> Pred2) const {
1103 // TODO: Fix this
1104 return false;
1105}
1106
1107
1108bool HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1109 std::vector<MachineOperand> &Pred) const {
1110 auto &HRI = getRegisterInfo();
1111 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1112 MachineOperand MO = MI->getOperand(oper);
1113 if (MO.isReg() && MO.isDef()) {
1114 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1115 if (RC == &Hexagon::PredRegsRegClass) {
1116 Pred.push_back(MO);
1117 return true;
1118 }
1119 }
1120 }
1121 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001122}
Andrew Trickd06df962012-02-01 22:13:57 +00001123
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001124bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
1125 bool isPred = MI->getDesc().isPredicable();
1126
1127 if (!isPred)
1128 return false;
1129
1130 const int Opc = MI->getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001131 int NumOperands = MI->getNumOperands();
1132
1133 // Keep a flag for upto 4 operands in the instructions, to indicate if
1134 // that operand has been constant extended.
1135 bool OpCExtended[4];
1136 if (NumOperands > 4)
1137 NumOperands = 4;
1138
1139 for (int i = 0; i < NumOperands; i++)
1140 OpCExtended[i] = (isOperandExtended(MI, i) && isConstExtended(MI));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001141
1142 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001143 case Hexagon::A2_tfrsi:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001144 return (isOperandExtended(MI, 1) && isConstExtended(MI)) ||
1145 isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001146
Colin LeMahieubda31b42014-12-29 20:44:51 +00001147 case Hexagon::S2_storerd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001148 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001149
Colin LeMahieubda31b42014-12-29 20:44:51 +00001150 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001151 case Hexagon::S2_storerinew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001152 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001153
Colin LeMahieubda31b42014-12-29 20:44:51 +00001154 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001155 case Hexagon::S2_storerhnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001156 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001157
Colin LeMahieubda31b42014-12-29 20:44:51 +00001158 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001159 case Hexagon::S2_storerbnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001160 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001161
Colin LeMahieu947cd702014-12-23 20:44:59 +00001162 case Hexagon::L2_loadrd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001163 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001164
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001165 case Hexagon::L2_loadri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001166 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001167
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001168 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001169 case Hexagon::L2_loadruh_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001170 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001171
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001172 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001173 case Hexagon::L2_loadrub_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001174 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001175
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001176 case Hexagon::L2_loadrd_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001177 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001178
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001179 case Hexagon::L2_loadri_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001180 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001181
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001182 case Hexagon::L2_loadrh_pi:
1183 case Hexagon::L2_loadruh_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001184 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001185
Colin LeMahieu96976a12014-12-26 18:57:13 +00001186 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +00001187 case Hexagon::L2_loadrub_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001188 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001189
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001190 case Hexagon::S4_storeirb_io:
1191 case Hexagon::S4_storeirh_io:
1192 case Hexagon::S4_storeiri_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001193 return (OpCExtended[1] || isUInt<6>(MI->getOperand(1).getImm())) &&
1194 (OpCExtended[2] || isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001195
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001196 case Hexagon::A2_addi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001197 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001198
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001199 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001200 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001201 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001202 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001203 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001204 case Hexagon::A2_zxth:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001205 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001206 }
1207
1208 return true;
1209}
1210
Jyotsna Verma84c47102013-05-06 18:49:23 +00001211
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001212bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1213 const MachineBasicBlock *MBB, const MachineFunction &MF) const {
1214 // Debug info is never a scheduling boundary. It's necessary to be explicit
1215 // due to the special treatment of IT instructions below, otherwise a
1216 // dbg_value followed by an IT will result in the IT instruction being
1217 // considered a scheduling hazard, which is wrong. It should be the actual
1218 // instruction preceding the dbg_value instruction(s), just like it is
1219 // when debug info is not present.
1220 if (MI->isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001221 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001222
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001223 // Throwing call is a boundary.
1224 if (MI->isCall()) {
1225 // If any of the block's successors is a landing pad, this could be a
1226 // throwing call.
1227 for (auto I : MBB->successors())
1228 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001229 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001230 }
1231
1232 // Don't mess around with no return calls.
1233 if (MI->getOpcode() == Hexagon::CALLv3nr)
1234 return true;
1235
1236 // Terminators and labels can't be scheduled around.
1237 if (MI->getDesc().isTerminator() || MI->isPosition())
1238 return true;
1239
1240 if (MI->isInlineAsm() && !ScheduleInlineAsm)
1241 return true;
1242
1243 return false;
1244}
1245
1246
1247/// Measure the specified inline asm to determine an approximation of its
1248/// length.
1249/// Comments (which run till the next SeparatorString or newline) do not
1250/// count as an instruction.
1251/// Any other non-whitespace text is considered an instruction, with
1252/// multiple instructions separated by SeparatorString or newlines.
1253/// Variable-length instructions are not handled here; this function
1254/// may be overloaded in the target code to do that.
1255/// Hexagon counts the number of ##'s and adjust for that many
1256/// constant exenders.
1257unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1258 const MCAsmInfo &MAI) const {
1259 StringRef AStr(Str);
1260 // Count the number of instructions in the asm.
1261 bool atInsnStart = true;
1262 unsigned Length = 0;
1263 for (; *Str; ++Str) {
1264 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1265 strlen(MAI.getSeparatorString())) == 0)
1266 atInsnStart = true;
1267 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1268 Length += MAI.getMaxInstLength();
1269 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001270 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001271 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1272 strlen(MAI.getCommentString())) == 0)
1273 atInsnStart = false;
1274 }
1275
1276 // Add to size number of constant extenders seen * 4.
1277 StringRef Occ("##");
1278 Length += AStr.count(Occ)*4;
1279 return Length;
1280}
1281
1282
1283ScheduleHazardRecognizer*
1284HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1285 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
1286 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1287}
1288
1289
1290/// \brief For a comparison instruction, return the source registers in
1291/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1292/// compares against in CmpValue. Return true if the comparison instruction
1293/// can be analyzed.
1294bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
1295 unsigned &SrcReg, unsigned &SrcReg2, int &Mask, int &Value) const {
1296 unsigned Opc = MI->getOpcode();
1297
1298 // Set mask and the first source register.
1299 switch (Opc) {
1300 case Hexagon::C2_cmpeq:
1301 case Hexagon::C2_cmpeqp:
1302 case Hexagon::C2_cmpgt:
1303 case Hexagon::C2_cmpgtp:
1304 case Hexagon::C2_cmpgtu:
1305 case Hexagon::C2_cmpgtup:
1306 case Hexagon::C4_cmpneq:
1307 case Hexagon::C4_cmplte:
1308 case Hexagon::C4_cmplteu:
1309 case Hexagon::C2_cmpeqi:
1310 case Hexagon::C2_cmpgti:
1311 case Hexagon::C2_cmpgtui:
1312 case Hexagon::C4_cmpneqi:
1313 case Hexagon::C4_cmplteui:
1314 case Hexagon::C4_cmpltei:
1315 SrcReg = MI->getOperand(1).getReg();
1316 Mask = ~0;
1317 break;
1318 case Hexagon::A4_cmpbeq:
1319 case Hexagon::A4_cmpbgt:
1320 case Hexagon::A4_cmpbgtu:
1321 case Hexagon::A4_cmpbeqi:
1322 case Hexagon::A4_cmpbgti:
1323 case Hexagon::A4_cmpbgtui:
1324 SrcReg = MI->getOperand(1).getReg();
1325 Mask = 0xFF;
1326 break;
1327 case Hexagon::A4_cmpheq:
1328 case Hexagon::A4_cmphgt:
1329 case Hexagon::A4_cmphgtu:
1330 case Hexagon::A4_cmpheqi:
1331 case Hexagon::A4_cmphgti:
1332 case Hexagon::A4_cmphgtui:
1333 SrcReg = MI->getOperand(1).getReg();
1334 Mask = 0xFFFF;
1335 break;
1336 }
1337
1338 // Set the value/second source register.
1339 switch (Opc) {
1340 case Hexagon::C2_cmpeq:
1341 case Hexagon::C2_cmpeqp:
1342 case Hexagon::C2_cmpgt:
1343 case Hexagon::C2_cmpgtp:
1344 case Hexagon::C2_cmpgtu:
1345 case Hexagon::C2_cmpgtup:
1346 case Hexagon::A4_cmpbeq:
1347 case Hexagon::A4_cmpbgt:
1348 case Hexagon::A4_cmpbgtu:
1349 case Hexagon::A4_cmpheq:
1350 case Hexagon::A4_cmphgt:
1351 case Hexagon::A4_cmphgtu:
1352 case Hexagon::C4_cmpneq:
1353 case Hexagon::C4_cmplte:
1354 case Hexagon::C4_cmplteu:
1355 SrcReg2 = MI->getOperand(2).getReg();
1356 return true;
1357
1358 case Hexagon::C2_cmpeqi:
1359 case Hexagon::C2_cmpgtui:
1360 case Hexagon::C2_cmpgti:
1361 case Hexagon::C4_cmpneqi:
1362 case Hexagon::C4_cmplteui:
1363 case Hexagon::C4_cmpltei:
1364 case Hexagon::A4_cmpbeqi:
1365 case Hexagon::A4_cmpbgti:
1366 case Hexagon::A4_cmpbgtui:
1367 case Hexagon::A4_cmpheqi:
1368 case Hexagon::A4_cmphgti:
1369 case Hexagon::A4_cmphgtui:
1370 SrcReg2 = 0;
1371 Value = MI->getOperand(2).getImm();
1372 return true;
1373 }
1374
1375 return false;
1376}
1377
1378
1379unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1380 const MachineInstr *MI, unsigned *PredCost) const {
1381 return getInstrTimingClassLatency(ItinData, MI);
1382}
1383
1384
1385DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1386 const TargetSubtargetInfo &STI) const {
1387 const InstrItineraryData *II = STI.getInstrItineraryData();
1388 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1389}
1390
1391
1392// Inspired by this pair:
1393// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1394// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1395// Currently AA considers the addresses in these instructions to be aliasing.
1396bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr *MIa,
1397 MachineInstr *MIb, AliasAnalysis *AA) const {
1398 int OffsetA = 0, OffsetB = 0;
1399 unsigned SizeA = 0, SizeB = 0;
1400
1401 if (MIa->hasUnmodeledSideEffects() || MIb->hasUnmodeledSideEffects() ||
1402 MIa->hasOrderedMemoryRef() || MIa->hasOrderedMemoryRef())
1403 return false;
1404
1405 // Instructions that are pure loads, not loads and stores like memops are not
1406 // dependent.
1407 if (MIa->mayLoad() && !isMemOp(MIa) && MIb->mayLoad() && !isMemOp(MIb))
1408 return true;
1409
1410 // Get base, offset, and access size in MIa.
1411 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
1412 if (!BaseRegA || !SizeA)
1413 return false;
1414
1415 // Get base, offset, and access size in MIb.
1416 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
1417 if (!BaseRegB || !SizeB)
1418 return false;
1419
1420 if (BaseRegA != BaseRegB)
1421 return false;
1422
1423 // This is a mem access with the same base register and known offsets from it.
1424 // Reason about it.
1425 if (OffsetA > OffsetB) {
1426 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1427 return (SizeB <= offDiff);
1428 } else if (OffsetA < OffsetB) {
1429 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1430 return (SizeA <= offDiff);
1431 }
1432
1433 return false;
1434}
1435
1436
1437unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
1438 MachineRegisterInfo &MRI = MF->getRegInfo();
1439 const TargetRegisterClass *TRC;
1440 if (VT == MVT::i1) {
1441 TRC = &Hexagon::PredRegsRegClass;
1442 } else if (VT == MVT::i32 || VT == MVT::f32) {
1443 TRC = &Hexagon::IntRegsRegClass;
1444 } else if (VT == MVT::i64 || VT == MVT::f64) {
1445 TRC = &Hexagon::DoubleRegsRegClass;
1446 } else {
1447 llvm_unreachable("Cannot handle this register class");
1448 }
1449
1450 unsigned NewReg = MRI.createVirtualRegister(TRC);
1451 return NewReg;
1452}
1453
1454
1455bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr* MI) const {
1456 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1457}
1458
1459
1460bool HexagonInstrInfo::isAccumulator(const MachineInstr *MI) const {
1461 const uint64_t F = MI->getDesc().TSFlags;
1462 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1463}
1464
1465
1466bool HexagonInstrInfo::isComplex(const MachineInstr *MI) const {
1467 const MachineFunction *MF = MI->getParent()->getParent();
1468 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1469 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1470
1471 if (!(isTC1(MI))
1472 && !(QII->isTC2Early(MI))
1473 && !(MI->getDesc().mayLoad())
1474 && !(MI->getDesc().mayStore())
1475 && (MI->getDesc().getOpcode() != Hexagon::S2_allocframe)
1476 && (MI->getDesc().getOpcode() != Hexagon::L2_deallocframe)
1477 && !(QII->isMemOp(MI))
1478 && !(MI->isBranch())
1479 && !(MI->isReturn())
1480 && !MI->isCall())
1481 return true;
1482
1483 return false;
1484}
1485
1486
Sanjay Patele4b9f502015-12-07 19:21:39 +00001487// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001488bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr *MI) const {
1489 return (getType(MI) == HexagonII::TypeCOMPOUND && MI->isBranch());
1490}
1491
1492
1493bool HexagonInstrInfo::isCondInst(const MachineInstr *MI) const {
1494 return (MI->isBranch() && isPredicated(MI)) ||
1495 isConditionalTransfer(MI) ||
1496 isConditionalALU32(MI) ||
1497 isConditionalLoad(MI) ||
1498 // Predicated stores which don't have a .new on any operands.
1499 (MI->mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1500 !isPredicatedNew(MI));
1501}
1502
1503
1504bool HexagonInstrInfo::isConditionalALU32(const MachineInstr* MI) const {
1505 switch (MI->getOpcode()) {
1506 case Hexagon::A2_paddf:
1507 case Hexagon::A2_paddfnew:
1508 case Hexagon::A2_paddif:
1509 case Hexagon::A2_paddifnew:
1510 case Hexagon::A2_paddit:
1511 case Hexagon::A2_padditnew:
1512 case Hexagon::A2_paddt:
1513 case Hexagon::A2_paddtnew:
1514 case Hexagon::A2_pandf:
1515 case Hexagon::A2_pandfnew:
1516 case Hexagon::A2_pandt:
1517 case Hexagon::A2_pandtnew:
1518 case Hexagon::A2_porf:
1519 case Hexagon::A2_porfnew:
1520 case Hexagon::A2_port:
1521 case Hexagon::A2_portnew:
1522 case Hexagon::A2_psubf:
1523 case Hexagon::A2_psubfnew:
1524 case Hexagon::A2_psubt:
1525 case Hexagon::A2_psubtnew:
1526 case Hexagon::A2_pxorf:
1527 case Hexagon::A2_pxorfnew:
1528 case Hexagon::A2_pxort:
1529 case Hexagon::A2_pxortnew:
1530 case Hexagon::A4_paslhf:
1531 case Hexagon::A4_paslhfnew:
1532 case Hexagon::A4_paslht:
1533 case Hexagon::A4_paslhtnew:
1534 case Hexagon::A4_pasrhf:
1535 case Hexagon::A4_pasrhfnew:
1536 case Hexagon::A4_pasrht:
1537 case Hexagon::A4_pasrhtnew:
1538 case Hexagon::A4_psxtbf:
1539 case Hexagon::A4_psxtbfnew:
1540 case Hexagon::A4_psxtbt:
1541 case Hexagon::A4_psxtbtnew:
1542 case Hexagon::A4_psxthf:
1543 case Hexagon::A4_psxthfnew:
1544 case Hexagon::A4_psxtht:
1545 case Hexagon::A4_psxthtnew:
1546 case Hexagon::A4_pzxtbf:
1547 case Hexagon::A4_pzxtbfnew:
1548 case Hexagon::A4_pzxtbt:
1549 case Hexagon::A4_pzxtbtnew:
1550 case Hexagon::A4_pzxthf:
1551 case Hexagon::A4_pzxthfnew:
1552 case Hexagon::A4_pzxtht:
1553 case Hexagon::A4_pzxthtnew:
1554 case Hexagon::C2_ccombinewf:
1555 case Hexagon::C2_ccombinewt:
1556 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001557 }
1558 return false;
1559}
1560
1561
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001562// FIXME - Function name and it's functionality don't match.
1563// It should be renamed to hasPredNewOpcode()
1564bool HexagonInstrInfo::isConditionalLoad(const MachineInstr* MI) const {
1565 if (!MI->getDesc().mayLoad() || !isPredicated(MI))
1566 return false;
1567
1568 int PNewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1569 // Instruction with valid predicated-new opcode can be promoted to .new.
1570 return PNewOpcode >= 0;
1571}
1572
1573
1574// Returns true if an instruction is a conditional store.
1575//
1576// Note: It doesn't include conditional new-value stores as they can't be
1577// converted to .new predicate.
1578bool HexagonInstrInfo::isConditionalStore(const MachineInstr* MI) const {
1579 switch (MI->getOpcode()) {
1580 default: return false;
1581 case Hexagon::S4_storeirbt_io:
1582 case Hexagon::S4_storeirbf_io:
1583 case Hexagon::S4_pstorerbt_rr:
1584 case Hexagon::S4_pstorerbf_rr:
1585 case Hexagon::S2_pstorerbt_io:
1586 case Hexagon::S2_pstorerbf_io:
1587 case Hexagon::S2_pstorerbt_pi:
1588 case Hexagon::S2_pstorerbf_pi:
1589 case Hexagon::S2_pstorerdt_io:
1590 case Hexagon::S2_pstorerdf_io:
1591 case Hexagon::S4_pstorerdt_rr:
1592 case Hexagon::S4_pstorerdf_rr:
1593 case Hexagon::S2_pstorerdt_pi:
1594 case Hexagon::S2_pstorerdf_pi:
1595 case Hexagon::S2_pstorerht_io:
1596 case Hexagon::S2_pstorerhf_io:
1597 case Hexagon::S4_storeirht_io:
1598 case Hexagon::S4_storeirhf_io:
1599 case Hexagon::S4_pstorerht_rr:
1600 case Hexagon::S4_pstorerhf_rr:
1601 case Hexagon::S2_pstorerht_pi:
1602 case Hexagon::S2_pstorerhf_pi:
1603 case Hexagon::S2_pstorerit_io:
1604 case Hexagon::S2_pstorerif_io:
1605 case Hexagon::S4_storeirit_io:
1606 case Hexagon::S4_storeirif_io:
1607 case Hexagon::S4_pstorerit_rr:
1608 case Hexagon::S4_pstorerif_rr:
1609 case Hexagon::S2_pstorerit_pi:
1610 case Hexagon::S2_pstorerif_pi:
1611
1612 // V4 global address store before promoting to dot new.
1613 case Hexagon::S4_pstorerdt_abs:
1614 case Hexagon::S4_pstorerdf_abs:
1615 case Hexagon::S4_pstorerbt_abs:
1616 case Hexagon::S4_pstorerbf_abs:
1617 case Hexagon::S4_pstorerht_abs:
1618 case Hexagon::S4_pstorerhf_abs:
1619 case Hexagon::S4_pstorerit_abs:
1620 case Hexagon::S4_pstorerif_abs:
1621 return true;
1622
1623 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1624 // from the "Conditional Store" list. Because a predicated new value store
1625 // would NOT be promoted to a double dot new store.
1626 // This function returns yes for those stores that are predicated but not
1627 // yet promoted to predicate dot new instructions.
1628 }
1629}
1630
1631
1632bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr *MI) const {
1633 switch (MI->getOpcode()) {
1634 case Hexagon::A2_tfrt:
1635 case Hexagon::A2_tfrf:
1636 case Hexagon::C2_cmoveit:
1637 case Hexagon::C2_cmoveif:
1638 case Hexagon::A2_tfrtnew:
1639 case Hexagon::A2_tfrfnew:
1640 case Hexagon::C2_cmovenewit:
1641 case Hexagon::C2_cmovenewif:
1642 case Hexagon::A2_tfrpt:
1643 case Hexagon::A2_tfrpf:
1644 return true;
1645
1646 default:
1647 return false;
1648 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001649 return false;
1650}
1651
1652
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001653// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1654// isFPImm and later getFPImm as well.
1655bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
1656 const uint64_t F = MI->getDesc().TSFlags;
1657 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1658 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001659 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001660
1661 unsigned isExtendable =
1662 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1663 if (!isExtendable)
1664 return false;
1665
1666 if (MI->isCall())
1667 return false;
1668
1669 short ExtOpNum = getCExtOpNum(MI);
1670 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1671 // Use MO operand flags to determine if MO
1672 // has the HMOTF_ConstExtended flag set.
1673 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001674 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001675 // If this is a Machine BB address we are talking about, and it is
1676 // not marked as extended, say so.
1677 if (MO.isMBB())
1678 return false;
1679
1680 // We could be using an instruction with an extendable immediate and shoehorn
1681 // a global address into it. If it is a global address it will be constant
1682 // extended. We do this for COMBINE.
1683 // We currently only handle isGlobal() because it is the only kind of
1684 // object we are going to end up with here for now.
1685 // In the future we probably should add isSymbol(), etc.
1686 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1687 MO.isJTI() || MO.isCPI())
1688 return true;
1689
1690 // If the extendable operand is not 'Immediate' type, the instruction should
1691 // have 'isExtended' flag set.
1692 assert(MO.isImm() && "Extendable operand must be Immediate type");
1693
1694 int MinValue = getMinValue(MI);
1695 int MaxValue = getMaxValue(MI);
1696 int ImmValue = MO.getImm();
1697
1698 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001699}
1700
1701
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001702bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1703 switch (MI->getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001704 case Hexagon::L4_return :
1705 case Hexagon::L4_return_t :
1706 case Hexagon::L4_return_f :
1707 case Hexagon::L4_return_tnew_pnt :
1708 case Hexagon::L4_return_fnew_pnt :
1709 case Hexagon::L4_return_tnew_pt :
1710 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001711 return true;
1712 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001713 return false;
1714}
1715
1716
1717// Return true when ConsMI uses a register defined by ProdMI.
1718bool HexagonInstrInfo::isDependent(const MachineInstr *ProdMI,
1719 const MachineInstr *ConsMI) const {
1720 const MCInstrDesc &ProdMCID = ProdMI->getDesc();
1721 if (!ProdMCID.getNumDefs())
1722 return false;
1723
1724 auto &HRI = getRegisterInfo();
1725
1726 SmallVector<unsigned, 4> DefsA;
1727 SmallVector<unsigned, 4> DefsB;
1728 SmallVector<unsigned, 8> UsesA;
1729 SmallVector<unsigned, 8> UsesB;
1730
1731 parseOperands(ProdMI, DefsA, UsesA);
1732 parseOperands(ConsMI, DefsB, UsesB);
1733
1734 for (auto &RegA : DefsA)
1735 for (auto &RegB : UsesB) {
1736 // True data dependency.
1737 if (RegA == RegB)
1738 return true;
1739
1740 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1741 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1742 if (RegB == *SubRegs)
1743 return true;
1744
1745 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1746 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1747 if (RegA == *SubRegs)
1748 return true;
1749 }
1750
1751 return false;
1752}
1753
1754
1755// Returns true if the instruction is alread a .cur.
1756bool HexagonInstrInfo::isDotCurInst(const MachineInstr* MI) const {
1757 switch (MI->getOpcode()) {
1758 case Hexagon::V6_vL32b_cur_pi:
1759 case Hexagon::V6_vL32b_cur_ai:
1760 case Hexagon::V6_vL32b_cur_pi_128B:
1761 case Hexagon::V6_vL32b_cur_ai_128B:
1762 return true;
1763 }
1764 return false;
1765}
1766
1767
1768// Returns true, if any one of the operands is a dot new
1769// insn, whether it is predicated dot new or register dot new.
1770bool HexagonInstrInfo::isDotNewInst(const MachineInstr* MI) const {
1771 if (isNewValueInst(MI) ||
1772 (isPredicated(MI) && isPredicatedNew(MI)))
1773 return true;
1774
1775 return false;
1776}
1777
1778
1779/// Symmetrical. See if these two instructions are fit for duplex pair.
1780bool HexagonInstrInfo::isDuplexPair(const MachineInstr *MIa,
1781 const MachineInstr *MIb) const {
1782 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1783 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1784 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1785}
1786
1787
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001788bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001789 if (!MI)
1790 return false;
1791
1792 if (MI->mayLoad() || MI->mayStore() || MI->isCompare())
1793 return true;
1794
1795 // Multiply
1796 unsigned SchedClass = MI->getDesc().getSchedClass();
1797 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
1798 return true;
1799 return false;
1800}
1801
1802
1803bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1804 return (Opcode == Hexagon::ENDLOOP0 ||
1805 Opcode == Hexagon::ENDLOOP1);
1806}
1807
1808
1809bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1810 switch(OpType) {
1811 case MachineOperand::MO_MachineBasicBlock:
1812 case MachineOperand::MO_GlobalAddress:
1813 case MachineOperand::MO_ExternalSymbol:
1814 case MachineOperand::MO_JumpTableIndex:
1815 case MachineOperand::MO_ConstantPoolIndex:
1816 case MachineOperand::MO_BlockAddress:
1817 return true;
1818 default:
1819 return false;
1820 }
1821}
1822
1823
1824bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
1825 const MCInstrDesc &MID = MI->getDesc();
1826 const uint64_t F = MID.TSFlags;
1827 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1828 return true;
1829
1830 // TODO: This is largely obsolete now. Will need to be removed
1831 // in consecutive patches.
1832 switch(MI->getOpcode()) {
1833 // TFR_FI Remains a special case.
1834 case Hexagon::TFR_FI:
1835 return true;
1836 default:
1837 return false;
1838 }
1839 return false;
1840}
1841
1842
1843// This returns true in two cases:
1844// - The OP code itself indicates that this is an extended instruction.
1845// - One of MOs has been marked with HMOTF_ConstExtended flag.
1846bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
1847 // First check if this is permanently extended op code.
1848 const uint64_t F = MI->getDesc().TSFlags;
1849 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
1850 return true;
1851 // Use MO operand flags to determine if one of MI's operands
1852 // has HMOTF_ConstExtended flag set.
1853 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
1854 E = MI->operands_end(); I != E; ++I) {
1855 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1856 return true;
1857 }
1858 return false;
1859}
1860
1861
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001862bool HexagonInstrInfo::isFloat(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001863 unsigned Opcode = MI->getOpcode();
1864 const uint64_t F = get(Opcode).TSFlags;
1865 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
1866}
1867
1868
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001869// No V60 HVX VMEM with A_INDIRECT.
1870bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr *I,
1871 const MachineInstr *J) const {
1872 if (!isV60VectorInstruction(I))
1873 return false;
1874 if (!I->mayLoad() && !I->mayStore())
1875 return false;
1876 return J->isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
1877}
1878
1879
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001880bool HexagonInstrInfo::isIndirectCall(const MachineInstr *MI) const {
1881 switch (MI->getOpcode()) {
1882 case Hexagon::J2_callr :
1883 case Hexagon::J2_callrf :
1884 case Hexagon::J2_callrt :
1885 return true;
1886 }
1887 return false;
1888}
1889
1890
1891bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr *MI) const {
1892 switch (MI->getOpcode()) {
1893 case Hexagon::L4_return :
1894 case Hexagon::L4_return_t :
1895 case Hexagon::L4_return_f :
1896 case Hexagon::L4_return_fnew_pnt :
1897 case Hexagon::L4_return_fnew_pt :
1898 case Hexagon::L4_return_tnew_pnt :
1899 case Hexagon::L4_return_tnew_pt :
1900 return true;
1901 }
1902 return false;
1903}
1904
1905
1906bool HexagonInstrInfo::isJumpR(const MachineInstr *MI) const {
1907 switch (MI->getOpcode()) {
1908 case Hexagon::J2_jumpr :
1909 case Hexagon::J2_jumprt :
1910 case Hexagon::J2_jumprf :
1911 case Hexagon::J2_jumprtnewpt :
1912 case Hexagon::J2_jumprfnewpt :
1913 case Hexagon::J2_jumprtnew :
1914 case Hexagon::J2_jumprfnew :
1915 return true;
1916 }
1917 return false;
1918}
1919
1920
1921// Return true if a given MI can accomodate given offset.
1922// Use abs estimate as oppose to the exact number.
1923// TODO: This will need to be changed to use MC level
1924// definition of instruction extendable field size.
1925bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr *MI,
1926 unsigned offset) const {
1927 // This selection of jump instructions matches to that what
1928 // AnalyzeBranch can parse, plus NVJ.
1929 if (isNewValueJump(MI)) // r9:2
1930 return isInt<11>(offset);
1931
1932 switch (MI->getOpcode()) {
1933 // Still missing Jump to address condition on register value.
1934 default:
1935 return false;
1936 case Hexagon::J2_jump: // bits<24> dst; // r22:2
1937 case Hexagon::J2_call:
1938 case Hexagon::CALLv3nr:
1939 return isInt<24>(offset);
1940 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
1941 case Hexagon::J2_jumpf:
1942 case Hexagon::J2_jumptnew:
1943 case Hexagon::J2_jumptnewpt:
1944 case Hexagon::J2_jumpfnew:
1945 case Hexagon::J2_jumpfnewpt:
1946 case Hexagon::J2_callt:
1947 case Hexagon::J2_callf:
1948 return isInt<17>(offset);
1949 case Hexagon::J2_loop0i:
1950 case Hexagon::J2_loop0iext:
1951 case Hexagon::J2_loop0r:
1952 case Hexagon::J2_loop0rext:
1953 case Hexagon::J2_loop1i:
1954 case Hexagon::J2_loop1iext:
1955 case Hexagon::J2_loop1r:
1956 case Hexagon::J2_loop1rext:
1957 return isInt<9>(offset);
1958 // TODO: Add all the compound branches here. Can we do this in Relation model?
1959 case Hexagon::J4_cmpeqi_tp0_jump_nt:
1960 case Hexagon::J4_cmpeqi_tp1_jump_nt:
1961 return isInt<11>(offset);
1962 }
1963}
1964
1965
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001966bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr *LRMI,
1967 const MachineInstr *ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001968 if (!LRMI || !ESMI)
1969 return false;
1970
1971 bool isLate = isLateResultInstr(LRMI);
1972 bool isEarly = isEarlySourceInstr(ESMI);
1973
1974 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
1975 DEBUG(LRMI->dump());
1976 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
1977 DEBUG(ESMI->dump());
1978
1979 if (isLate && isEarly) {
1980 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
1981 return true;
1982 }
1983
1984 return false;
1985}
1986
1987
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001988bool HexagonInstrInfo::isLateResultInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001989 if (!MI)
1990 return false;
1991
1992 switch (MI->getOpcode()) {
1993 case TargetOpcode::EXTRACT_SUBREG:
1994 case TargetOpcode::INSERT_SUBREG:
1995 case TargetOpcode::SUBREG_TO_REG:
1996 case TargetOpcode::REG_SEQUENCE:
1997 case TargetOpcode::IMPLICIT_DEF:
1998 case TargetOpcode::COPY:
1999 case TargetOpcode::INLINEASM:
2000 case TargetOpcode::PHI:
2001 return false;
2002 default:
2003 break;
2004 }
2005
2006 unsigned SchedClass = MI->getDesc().getSchedClass();
2007
2008 switch (SchedClass) {
2009 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2010 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2011 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2012 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2013 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2014 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2015 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2016 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2017 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2018 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2019 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2020 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2021 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2022 return false;
2023 }
2024 return true;
2025}
2026
2027
2028bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr *MI) const {
2029 if (!MI)
2030 return false;
2031
2032 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2033 // resource, but all operands can be received late like an ALU instruction.
2034 return MI->getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
2035}
2036
2037
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002038bool HexagonInstrInfo::isLoopN(const MachineInstr *MI) const {
2039 unsigned Opcode = MI->getOpcode();
2040 return Opcode == Hexagon::J2_loop0i ||
2041 Opcode == Hexagon::J2_loop0r ||
2042 Opcode == Hexagon::J2_loop0iext ||
2043 Opcode == Hexagon::J2_loop0rext ||
2044 Opcode == Hexagon::J2_loop1i ||
2045 Opcode == Hexagon::J2_loop1r ||
2046 Opcode == Hexagon::J2_loop1iext ||
2047 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002048}
2049
2050
2051bool HexagonInstrInfo::isMemOp(const MachineInstr *MI) const {
2052 switch (MI->getOpcode()) {
2053 default: return false;
2054 case Hexagon::L4_iadd_memopw_io :
2055 case Hexagon::L4_isub_memopw_io :
2056 case Hexagon::L4_add_memopw_io :
2057 case Hexagon::L4_sub_memopw_io :
2058 case Hexagon::L4_and_memopw_io :
2059 case Hexagon::L4_or_memopw_io :
2060 case Hexagon::L4_iadd_memoph_io :
2061 case Hexagon::L4_isub_memoph_io :
2062 case Hexagon::L4_add_memoph_io :
2063 case Hexagon::L4_sub_memoph_io :
2064 case Hexagon::L4_and_memoph_io :
2065 case Hexagon::L4_or_memoph_io :
2066 case Hexagon::L4_iadd_memopb_io :
2067 case Hexagon::L4_isub_memopb_io :
2068 case Hexagon::L4_add_memopb_io :
2069 case Hexagon::L4_sub_memopb_io :
2070 case Hexagon::L4_and_memopb_io :
2071 case Hexagon::L4_or_memopb_io :
2072 case Hexagon::L4_ior_memopb_io:
2073 case Hexagon::L4_ior_memoph_io:
2074 case Hexagon::L4_ior_memopw_io:
2075 case Hexagon::L4_iand_memopb_io:
2076 case Hexagon::L4_iand_memoph_io:
2077 case Hexagon::L4_iand_memopw_io:
2078 return true;
2079 }
2080 return false;
2081}
2082
2083
2084bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
2085 const uint64_t F = MI->getDesc().TSFlags;
2086 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2087}
2088
2089
2090bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2091 const uint64_t F = get(Opcode).TSFlags;
2092 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2093}
2094
2095
2096bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
2097 return isNewValueJump(MI) || isNewValueStore(MI);
2098}
2099
2100
2101bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
2102 return isNewValue(MI) && MI->isBranch();
2103}
2104
2105
2106bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2107 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2108}
2109
2110
2111bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
2112 const uint64_t F = MI->getDesc().TSFlags;
2113 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2114}
2115
2116
2117bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2118 const uint64_t F = get(Opcode).TSFlags;
2119 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2120}
2121
2122
2123// Returns true if a particular operand is extendable for an instruction.
2124bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
2125 unsigned OperandNum) const {
2126 const uint64_t F = MI->getDesc().TSFlags;
2127 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2128 == OperandNum;
2129}
2130
2131
2132bool HexagonInstrInfo::isPostIncrement(const MachineInstr* MI) const {
2133 return getAddrMode(MI) == HexagonII::PostInc;
2134}
2135
2136
2137bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
2138 const uint64_t F = MI->getDesc().TSFlags;
2139 assert(isPredicated(MI));
2140 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2141}
2142
2143
2144bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2145 const uint64_t F = get(Opcode).TSFlags;
2146 assert(isPredicated(Opcode));
2147 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2148}
2149
2150
2151bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
2152 const uint64_t F = MI->getDesc().TSFlags;
2153 return !((F >> HexagonII::PredicatedFalsePos) &
2154 HexagonII::PredicatedFalseMask);
2155}
2156
2157
2158bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2159 const uint64_t F = get(Opcode).TSFlags;
2160 // Make sure that the instruction is predicated.
2161 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2162 return !((F >> HexagonII::PredicatedFalsePos) &
2163 HexagonII::PredicatedFalseMask);
2164}
2165
2166
2167bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2168 const uint64_t F = get(Opcode).TSFlags;
2169 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2170}
2171
2172
2173bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2174 const uint64_t F = get(Opcode).TSFlags;
2175 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2176}
2177
2178
2179bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2180 const uint64_t F = get(Opcode).TSFlags;
2181 assert(get(Opcode).isBranch() &&
2182 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2183 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2184}
2185
2186
2187bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
2188 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2189 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT;
2190}
2191
2192
2193bool HexagonInstrInfo::isSolo(const MachineInstr* MI) const {
2194 const uint64_t F = MI->getDesc().TSFlags;
2195 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2196}
2197
2198
2199bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr *MI) const {
2200 switch (MI->getOpcode()) {
2201 case Hexagon::STriw_pred :
2202 case Hexagon::LDriw_pred :
2203 return true;
2204 default:
2205 return false;
2206 }
2207}
2208
2209
2210// Returns true when SU has a timing class TC1.
2211bool HexagonInstrInfo::isTC1(const MachineInstr *MI) const {
2212 unsigned SchedClass = MI->getDesc().getSchedClass();
2213 switch (SchedClass) {
2214 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2215 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2216 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2217 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2218 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2219 //case Hexagon::Sched::M_tc_1_SLOT23:
2220 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2221 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2222 return true;
2223
2224 default:
2225 return false;
2226 }
2227}
2228
2229
2230bool HexagonInstrInfo::isTC2(const MachineInstr *MI) const {
2231 unsigned SchedClass = MI->getDesc().getSchedClass();
2232 switch (SchedClass) {
2233 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2234 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2235 case Hexagon::Sched::CR_tc_2_SLOT3:
2236 case Hexagon::Sched::M_tc_2_SLOT23:
2237 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2238 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2239 return true;
2240
2241 default:
2242 return false;
2243 }
2244}
2245
2246
2247bool HexagonInstrInfo::isTC2Early(const MachineInstr *MI) const {
2248 unsigned SchedClass = MI->getDesc().getSchedClass();
2249 switch (SchedClass) {
2250 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2251 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2252 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2253 case Hexagon::Sched::CR_tc_2early_SLOT23:
2254 case Hexagon::Sched::CR_tc_2early_SLOT3:
2255 case Hexagon::Sched::J_tc_2early_SLOT0123:
2256 case Hexagon::Sched::J_tc_2early_SLOT2:
2257 case Hexagon::Sched::J_tc_2early_SLOT23:
2258 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2259 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2260 return true;
2261
2262 default:
2263 return false;
2264 }
2265}
2266
2267
2268bool HexagonInstrInfo::isTC4x(const MachineInstr *MI) const {
2269 if (!MI)
2270 return false;
2271
2272 unsigned SchedClass = MI->getDesc().getSchedClass();
2273 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2274}
2275
2276
2277bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr *MI) const {
2278 if (!MI)
2279 return false;
2280
2281 const uint64_t V = getType(MI);
2282 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2283}
2284
2285
2286// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2287//
2288bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2289 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2290 VT == MVT::v32i16 || VT == MVT::v64i8) {
2291 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2292 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2293 (Offset & 0x3f) == 0);
2294 }
2295 // 128B
2296 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2297 VT == MVT::v64i16 || VT == MVT::v128i8) {
2298 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2299 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2300 (Offset & 0x7f) == 0);
2301 }
2302 if (VT == MVT::i64) {
2303 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2304 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2305 (Offset & 0x7) == 0);
2306 }
2307 if (VT == MVT::i32) {
2308 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2309 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2310 (Offset & 0x3) == 0);
2311 }
2312 if (VT == MVT::i16) {
2313 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2314 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2315 (Offset & 0x1) == 0);
2316 }
2317 if (VT == MVT::i8) {
2318 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2319 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2320 }
2321 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002322}
2323
2324
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002325bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2326 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002327 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002328 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002329 // inserted to calculate the final address. Due to this reason, the function
2330 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002331 // We used to assert if the offset was not properly aligned, however,
2332 // there are cases where a misaligned pointer recast can cause this
2333 // problem, and we need to allow for it. The front end warns of such
2334 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002335
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002336 switch (Opcode) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002337 case Hexagon::STriq_pred_V6:
2338 case Hexagon::STriq_pred_vec_V6:
2339 case Hexagon::STriv_pseudo_V6:
2340 case Hexagon::STrivv_pseudo_V6:
2341 case Hexagon::LDriq_pred_V6:
2342 case Hexagon::LDriq_pred_vec_V6:
2343 case Hexagon::LDriv_pseudo_V6:
2344 case Hexagon::LDrivv_pseudo_V6:
2345 case Hexagon::LDrivv_indexed:
2346 case Hexagon::STrivv_indexed:
2347 case Hexagon::V6_vL32b_ai:
2348 case Hexagon::V6_vS32b_ai:
2349 case Hexagon::V6_vL32Ub_ai:
2350 case Hexagon::V6_vS32Ub_ai:
2351 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2352 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2353
2354 case Hexagon::STriq_pred_V6_128B:
2355 case Hexagon::STriq_pred_vec_V6_128B:
2356 case Hexagon::STriv_pseudo_V6_128B:
2357 case Hexagon::STrivv_pseudo_V6_128B:
2358 case Hexagon::LDriq_pred_V6_128B:
2359 case Hexagon::LDriq_pred_vec_V6_128B:
2360 case Hexagon::LDriv_pseudo_V6_128B:
2361 case Hexagon::LDrivv_pseudo_V6_128B:
2362 case Hexagon::LDrivv_indexed_128B:
2363 case Hexagon::STrivv_indexed_128B:
2364 case Hexagon::V6_vL32b_ai_128B:
2365 case Hexagon::V6_vS32b_ai_128B:
2366 case Hexagon::V6_vL32Ub_ai_128B:
2367 case Hexagon::V6_vS32Ub_ai_128B:
2368 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2369 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2370
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002371 case Hexagon::J2_loop0i:
2372 case Hexagon::J2_loop1i:
2373 return isUInt<10>(Offset);
2374 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002375
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002376 if (Extend)
2377 return true;
2378
2379 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002380 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002381 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002382 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2383 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2384
Colin LeMahieu947cd702014-12-23 20:44:59 +00002385 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002386 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002387 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2388 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2389
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002390 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002391 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002392 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002393 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2394 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2395
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002396 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002397 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002398 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002399 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2400 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2401
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002402 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002403 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2404 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2405
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002406 case Hexagon::L4_iadd_memopw_io :
2407 case Hexagon::L4_isub_memopw_io :
2408 case Hexagon::L4_add_memopw_io :
2409 case Hexagon::L4_sub_memopw_io :
2410 case Hexagon::L4_and_memopw_io :
2411 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002412 return (0 <= Offset && Offset <= 255);
2413
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002414 case Hexagon::L4_iadd_memoph_io :
2415 case Hexagon::L4_isub_memoph_io :
2416 case Hexagon::L4_add_memoph_io :
2417 case Hexagon::L4_sub_memoph_io :
2418 case Hexagon::L4_and_memoph_io :
2419 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002420 return (0 <= Offset && Offset <= 127);
2421
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002422 case Hexagon::L4_iadd_memopb_io :
2423 case Hexagon::L4_isub_memopb_io :
2424 case Hexagon::L4_add_memopb_io :
2425 case Hexagon::L4_sub_memopb_io :
2426 case Hexagon::L4_and_memopb_io :
2427 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002428 return (0 <= Offset && Offset <= 63);
2429
2430 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
2431 // any size. Later pass knows how to handle it.
2432 case Hexagon::STriw_pred:
2433 case Hexagon::LDriw_pred:
2434 return true;
2435
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002436 case Hexagon::TFR_FI:
2437 case Hexagon::TFR_FIA:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002438 case Hexagon::INLINEASM:
2439 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002440
2441 case Hexagon::L2_ploadrbt_io:
2442 case Hexagon::L2_ploadrbf_io:
2443 case Hexagon::L2_ploadrubt_io:
2444 case Hexagon::L2_ploadrubf_io:
2445 case Hexagon::S2_pstorerbt_io:
2446 case Hexagon::S2_pstorerbf_io:
2447 case Hexagon::S4_storeirb_io:
2448 case Hexagon::S4_storeirbt_io:
2449 case Hexagon::S4_storeirbf_io:
2450 return isUInt<6>(Offset);
2451
2452 case Hexagon::L2_ploadrht_io:
2453 case Hexagon::L2_ploadrhf_io:
2454 case Hexagon::L2_ploadruht_io:
2455 case Hexagon::L2_ploadruhf_io:
2456 case Hexagon::S2_pstorerht_io:
2457 case Hexagon::S2_pstorerhf_io:
2458 case Hexagon::S4_storeirh_io:
2459 case Hexagon::S4_storeirht_io:
2460 case Hexagon::S4_storeirhf_io:
2461 return isShiftedUInt<6,1>(Offset);
2462
2463 case Hexagon::L2_ploadrit_io:
2464 case Hexagon::L2_ploadrif_io:
2465 case Hexagon::S2_pstorerit_io:
2466 case Hexagon::S2_pstorerif_io:
2467 case Hexagon::S4_storeiri_io:
2468 case Hexagon::S4_storeirit_io:
2469 case Hexagon::S4_storeirif_io:
2470 return isShiftedUInt<6,2>(Offset);
2471
2472 case Hexagon::L2_ploadrdt_io:
2473 case Hexagon::L2_ploadrdf_io:
2474 case Hexagon::S2_pstorerdt_io:
2475 case Hexagon::S2_pstorerdf_io:
2476 return isShiftedUInt<6,3>(Offset);
2477 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002478
Benjamin Kramerb6684012011-12-27 11:41:05 +00002479 llvm_unreachable("No offset range is defined for this opcode. "
2480 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002481}
2482
2483
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002484bool HexagonInstrInfo::isVecAcc(const MachineInstr *MI) const {
2485 return MI && isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002486}
2487
2488
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002489bool HexagonInstrInfo::isVecALU(const MachineInstr *MI) const {
2490 if (!MI)
Andrew Trickd06df962012-02-01 22:13:57 +00002491 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002492 const uint64_t F = get(MI->getOpcode()).TSFlags;
2493 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2494 return
2495 V == HexagonII::TypeCVI_VA ||
2496 V == HexagonII::TypeCVI_VA_DV;
2497}
Andrew Trickd06df962012-02-01 22:13:57 +00002498
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002499
2500bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr *ProdMI,
2501 const MachineInstr *ConsMI) const {
2502 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2503 return true;
2504
2505 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2506 return true;
2507
2508 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002509 return true;
2510
2511 return false;
2512}
Jyotsna Verma84256432013-03-01 17:37:13 +00002513
Jyotsna Verma84256432013-03-01 17:37:13 +00002514
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002515/// \brief Can these instructions execute at the same time in a bundle.
2516bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr *First,
2517 const MachineInstr *Second) const {
2518 if (DisableNVSchedule)
2519 return false;
2520 if (mayBeNewStore(Second)) {
2521 // Make sure the definition of the first instruction is the value being
2522 // stored.
2523 const MachineOperand &Stored =
2524 Second->getOperand(Second->getNumOperands() - 1);
2525 if (!Stored.isReg())
2526 return false;
2527 for (unsigned i = 0, e = First->getNumOperands(); i < e; ++i) {
2528 const MachineOperand &Op = First->getOperand(i);
2529 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2530 return true;
2531 }
2532 }
2533 return false;
2534}
2535
2536
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002537bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2538 for (auto &I : *B)
2539 if (I.isEHLabel())
2540 return true;
2541 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002542}
2543
Jyotsna Verma84256432013-03-01 17:37:13 +00002544
2545// Returns true if an instruction can be converted into a non-extended
2546// equivalent instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002547bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002548 short NonExtOpcode;
2549 // Check if the instruction has a register form that uses register in place
2550 // of the extended operand, if so return that as the non-extended form.
2551 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2552 return true;
2553
2554 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002555 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002556
2557 switch (getAddrMode(MI)) {
2558 case HexagonII::Absolute :
2559 // Load/store with absolute addressing mode can be converted into
2560 // base+offset mode.
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00002561 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002562 break;
2563 case HexagonII::BaseImmOffset :
2564 // Load/store with base+offset addressing mode can be converted into
2565 // base+register offset addressing mode. However left shift operand should
2566 // be set to 0.
2567 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2568 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002569 case HexagonII::BaseLongOffset:
2570 NonExtOpcode = Hexagon::getRegShlForm(MI->getOpcode());
2571 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002572 default:
2573 return false;
2574 }
2575 if (NonExtOpcode < 0)
2576 return false;
2577 return true;
2578 }
2579 return false;
2580}
2581
Jyotsna Verma84256432013-03-01 17:37:13 +00002582
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002583bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002584 return Hexagon::getRealHWInstr(MI->getOpcode(),
2585 Hexagon::InstrType_Pseudo) >= 0;
2586}
2587
2588
2589bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2590 const {
2591 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2592 while (I != E) {
2593 if (I->isBarrier())
2594 return true;
2595 ++I;
2596 }
2597 return false;
2598}
2599
2600
2601// Returns true, if a LD insn can be promoted to a cur load.
2602bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr *MI) const {
2603 auto &HST = MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2604 const uint64_t F = MI->getDesc().TSFlags;
2605 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2606 HST.hasV60TOps();
2607}
2608
2609
2610// Returns true, if a ST insn can be promoted to a new-value store.
2611bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
2612 const uint64_t F = MI->getDesc().TSFlags;
2613 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2614}
2615
2616
2617bool HexagonInstrInfo::producesStall(const MachineInstr *ProdMI,
2618 const MachineInstr *ConsMI) const {
2619 // There is no stall when ProdMI is not a V60 vector.
2620 if (!isV60VectorInstruction(ProdMI))
2621 return false;
2622
2623 // There is no stall when ProdMI and ConsMI are not dependent.
2624 if (!isDependent(ProdMI, ConsMI))
2625 return false;
2626
2627 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2628 // are scheduled in consecutive packets.
2629 if (isVecUsableNextPacket(ProdMI, ConsMI))
2630 return false;
2631
2632 return true;
2633}
2634
2635
2636bool HexagonInstrInfo::producesStall(const MachineInstr *MI,
2637 MachineBasicBlock::const_instr_iterator BII) const {
2638 // There is no stall when I is not a V60 vector.
2639 if (!isV60VectorInstruction(MI))
2640 return false;
2641
2642 MachineBasicBlock::const_instr_iterator MII = BII;
2643 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2644
2645 if (!(*MII).isBundle()) {
2646 const MachineInstr *J = &*MII;
2647 if (!isV60VectorInstruction(J))
2648 return false;
2649 else if (isVecUsableNextPacket(J, MI))
2650 return false;
2651 return true;
2652 }
2653
2654 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
2655 const MachineInstr *J = &*MII;
2656 if (producesStall(J, MI))
2657 return true;
2658 }
2659 return false;
2660}
2661
2662
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002663bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002664 unsigned PredReg) const {
2665 for (unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002666 const MachineOperand &MO = MI->getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002667 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
2668 return false; // Predicate register must be explicitly defined.
2669 }
2670
2671 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2672 // memd_locked cannot be used as .new as well,
2673 // but we don't seem to have these instructions defined.
2674 return MI->getOpcode() != Hexagon::A4_tlbmatch;
2675}
2676
2677
2678bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
2679 return (Opcode == Hexagon::J2_jumpt) ||
2680 (Opcode == Hexagon::J2_jumpf) ||
2681 (Opcode == Hexagon::J2_jumptnew) ||
2682 (Opcode == Hexagon::J2_jumpfnew) ||
2683 (Opcode == Hexagon::J2_jumptnewpt) ||
2684 (Opcode == Hexagon::J2_jumpfnewpt);
2685}
2686
2687
2688bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2689 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2690 return false;
2691 return !isPredicatedTrue(Cond[0].getImm());
2692}
2693
2694
2695unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
2696 const uint64_t F = MI->getDesc().TSFlags;
2697 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
2698}
2699
2700
2701// Returns the base register in a memory access (load/store). The offset is
2702// returned in Offset and the access size is returned in AccessSize.
2703unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr *MI,
2704 int &Offset, unsigned &AccessSize) const {
2705 // Return if it is not a base+offset type instruction or a MemOp.
2706 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
2707 getAddrMode(MI) != HexagonII::BaseLongOffset &&
2708 !isMemOp(MI) && !isPostIncrement(MI))
2709 return 0;
2710
2711 // Since it is a memory access instruction, getMemAccessSize() should never
2712 // return 0.
2713 assert (getMemAccessSize(MI) &&
2714 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
2715
2716 // Return Values of getMemAccessSize() are
2717 // 0 - Checked in the assert above.
2718 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
2719 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
2720 AccessSize = (1U << (getMemAccessSize(MI) - 1));
2721
2722 unsigned basePos = 0, offsetPos = 0;
2723 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
2724 return 0;
2725
2726 // Post increment updates its EA after the mem access,
2727 // so we need to treat its offset as zero.
2728 if (isPostIncrement(MI))
2729 Offset = 0;
2730 else {
2731 Offset = MI->getOperand(offsetPos).getImm();
2732 }
2733
2734 return MI->getOperand(basePos).getReg();
2735}
2736
2737
2738/// Return the position of the base and offset operands for this instruction.
2739bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr *MI,
2740 unsigned &BasePos, unsigned &OffsetPos) const {
2741 // Deal with memops first.
2742 if (isMemOp(MI)) {
2743 assert (MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
2744 "Bad Memop.");
2745 BasePos = 0;
2746 OffsetPos = 1;
2747 } else if (MI->mayStore()) {
2748 BasePos = 0;
2749 OffsetPos = 1;
2750 } else if (MI->mayLoad()) {
2751 BasePos = 1;
2752 OffsetPos = 2;
2753 } else
2754 return false;
2755
2756 if (isPredicated(MI)) {
2757 BasePos++;
2758 OffsetPos++;
2759 }
2760 if (isPostIncrement(MI)) {
2761 BasePos++;
2762 OffsetPos++;
2763 }
2764
2765 if (!MI->getOperand(BasePos).isReg() || !MI->getOperand(OffsetPos).isImm())
2766 return false;
2767
2768 return true;
2769}
2770
2771
2772// Inserts branching instructions in reverse order of their occurence.
2773// e.g. jump_t t1 (i1)
2774// jump t2 (i2)
2775// Jumpers = {i2, i1}
2776SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
2777 MachineBasicBlock& MBB) const {
2778 SmallVector<MachineInstr*, 2> Jumpers;
2779 // If the block has no terminators, it just falls into the block after it.
2780 MachineBasicBlock::instr_iterator I = MBB.instr_end();
2781 if (I == MBB.instr_begin())
2782 return Jumpers;
2783
2784 // A basic block may looks like this:
2785 //
2786 // [ insn
2787 // EH_LABEL
2788 // insn
2789 // insn
2790 // insn
2791 // EH_LABEL
2792 // insn ]
2793 //
2794 // It has two succs but does not have a terminator
2795 // Don't know how to handle it.
2796 do {
2797 --I;
2798 if (I->isEHLabel())
2799 return Jumpers;
2800 } while (I != MBB.instr_begin());
2801
2802 I = MBB.instr_end();
2803 --I;
2804
2805 while (I->isDebugValue()) {
2806 if (I == MBB.instr_begin())
2807 return Jumpers;
2808 --I;
2809 }
2810 if (!isUnpredicatedTerminator(&*I))
2811 return Jumpers;
2812
2813 // Get the last instruction in the block.
2814 MachineInstr *LastInst = &*I;
2815 Jumpers.push_back(LastInst);
2816 MachineInstr *SecondLastInst = nullptr;
2817 // Find one more terminator if present.
2818 do {
2819 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
2820 if (!SecondLastInst) {
2821 SecondLastInst = &*I;
2822 Jumpers.push_back(SecondLastInst);
2823 } else // This is a third branch.
2824 return Jumpers;
2825 }
2826 if (I == MBB.instr_begin())
2827 break;
2828 --I;
2829 } while (true);
2830 return Jumpers;
2831}
2832
2833
2834// Returns Operand Index for the constant extended instruction.
2835unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
2836 const uint64_t F = MI->getDesc().TSFlags;
2837 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
2838}
2839
2840// See if instruction could potentially be a duplex candidate.
2841// If so, return its group. Zero otherwise.
2842HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
2843 const MachineInstr *MI) const {
2844 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
2845
2846 switch (MI->getOpcode()) {
2847 default:
2848 return HexagonII::HCG_None;
2849 //
2850 // Compound pairs.
2851 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
2852 // "Rd16=#U6 ; jump #r9:2"
2853 // "Rd16=Rs16 ; jump #r9:2"
2854 //
2855 case Hexagon::C2_cmpeq:
2856 case Hexagon::C2_cmpgt:
2857 case Hexagon::C2_cmpgtu:
2858 DstReg = MI->getOperand(0).getReg();
2859 Src1Reg = MI->getOperand(1).getReg();
2860 Src2Reg = MI->getOperand(2).getReg();
2861 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2862 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2863 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
2864 return HexagonII::HCG_A;
2865 break;
2866 case Hexagon::C2_cmpeqi:
2867 case Hexagon::C2_cmpgti:
2868 case Hexagon::C2_cmpgtui:
2869 // P0 = cmp.eq(Rs,#u2)
2870 DstReg = MI->getOperand(0).getReg();
2871 SrcReg = MI->getOperand(1).getReg();
2872 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2873 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2874 isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
2875 ((isUInt<5>(MI->getOperand(2).getImm())) ||
2876 (MI->getOperand(2).getImm() == -1)))
2877 return HexagonII::HCG_A;
2878 break;
2879 case Hexagon::A2_tfr:
2880 // Rd = Rs
2881 DstReg = MI->getOperand(0).getReg();
2882 SrcReg = MI->getOperand(1).getReg();
2883 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
2884 return HexagonII::HCG_A;
2885 break;
2886 case Hexagon::A2_tfrsi:
2887 // Rd = #u6
2888 // Do not test for #u6 size since the const is getting extended
2889 // regardless and compound could be formed.
2890 DstReg = MI->getOperand(0).getReg();
2891 if (isIntRegForSubInst(DstReg))
2892 return HexagonII::HCG_A;
2893 break;
2894 case Hexagon::S2_tstbit_i:
2895 DstReg = MI->getOperand(0).getReg();
2896 Src1Reg = MI->getOperand(1).getReg();
2897 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2898 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2899 MI->getOperand(2).isImm() &&
2900 isIntRegForSubInst(Src1Reg) && (MI->getOperand(2).getImm() == 0))
2901 return HexagonII::HCG_A;
2902 break;
2903 // The fact that .new form is used pretty much guarantees
2904 // that predicate register will match. Nevertheless,
2905 // there could be some false positives without additional
2906 // checking.
2907 case Hexagon::J2_jumptnew:
2908 case Hexagon::J2_jumpfnew:
2909 case Hexagon::J2_jumptnewpt:
2910 case Hexagon::J2_jumpfnewpt:
2911 Src1Reg = MI->getOperand(0).getReg();
2912 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
2913 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
2914 return HexagonII::HCG_B;
2915 break;
2916 // Transfer and jump:
2917 // Rd=#U6 ; jump #r9:2
2918 // Rd=Rs ; jump #r9:2
2919 // Do not test for jump range here.
2920 case Hexagon::J2_jump:
2921 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
2922 return HexagonII::HCG_C;
2923 break;
2924 }
2925
2926 return HexagonII::HCG_None;
2927}
2928
2929
2930// Returns -1 when there is no opcode found.
2931unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr *GA,
2932 const MachineInstr *GB) const {
2933 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
2934 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
2935 if ((GA->getOpcode() != Hexagon::C2_cmpeqi) ||
2936 (GB->getOpcode() != Hexagon::J2_jumptnew))
2937 return -1;
2938 unsigned DestReg = GA->getOperand(0).getReg();
2939 if (!GB->readsRegister(DestReg))
2940 return -1;
2941 if (DestReg == Hexagon::P0)
2942 return Hexagon::J4_cmpeqi_tp0_jump_nt;
2943 if (DestReg == Hexagon::P1)
2944 return Hexagon::J4_cmpeqi_tp1_jump_nt;
2945 return -1;
2946}
2947
2948
2949int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
2950 enum Hexagon::PredSense inPredSense;
2951 inPredSense = invertPredicate ? Hexagon::PredSense_false :
2952 Hexagon::PredSense_true;
2953 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
2954 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
2955 return CondOpcode;
2956
2957 // This switch case will be removed once all the instructions have been
2958 // modified to use relation maps.
2959 switch(Opc) {
2960 case Hexagon::TFRI_f:
2961 return !invertPredicate ? Hexagon::TFRI_cPt_f :
2962 Hexagon::TFRI_cNotPt_f;
2963 }
2964
2965 llvm_unreachable("Unexpected predicable instruction");
2966}
2967
2968
2969// Return the cur value instruction for a given store.
2970int HexagonInstrInfo::getDotCurOp(const MachineInstr* MI) const {
2971 switch (MI->getOpcode()) {
2972 default: llvm_unreachable("Unknown .cur type");
2973 case Hexagon::V6_vL32b_pi:
2974 return Hexagon::V6_vL32b_cur_pi;
2975 case Hexagon::V6_vL32b_ai:
2976 return Hexagon::V6_vL32b_cur_ai;
2977 //128B
2978 case Hexagon::V6_vL32b_pi_128B:
2979 return Hexagon::V6_vL32b_cur_pi_128B;
2980 case Hexagon::V6_vL32b_ai_128B:
2981 return Hexagon::V6_vL32b_cur_ai_128B;
2982 }
2983 return 0;
2984}
2985
2986
2987
2988// The diagram below shows the steps involved in the conversion of a predicated
2989// store instruction to its .new predicated new-value form.
2990//
2991// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
2992// ^ ^
2993// / \ (not OK. it will cause new-value store to be
2994// / X conditional on p0.new while R2 producer is
2995// / \ on p0)
2996// / \.
2997// p.new store p.old NV store
2998// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
2999// ^ ^
3000// \ /
3001// \ /
3002// \ /
3003// p.old store
3004// [if (p0)memw(R0+#0)=R2]
3005//
3006//
3007// The following set of instructions further explains the scenario where
3008// conditional new-value store becomes invalid when promoted to .new predicate
3009// form.
3010//
3011// { 1) if (p0) r0 = add(r1, r2)
3012// 2) p0 = cmp.eq(r3, #0) }
3013//
3014// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3015// the first two instructions because in instr 1, r0 is conditional on old value
3016// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3017// is not valid for new-value stores.
3018// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3019// from the "Conditional Store" list. Because a predicated new value store
3020// would NOT be promoted to a double dot new store. See diagram below:
3021// This function returns yes for those stores that are predicated but not
3022// yet promoted to predicate dot new instructions.
3023//
3024// +---------------------+
3025// /-----| if (p0) memw(..)=r0 |---------\~
3026// || +---------------------+ ||
3027// promote || /\ /\ || promote
3028// || /||\ /||\ ||
3029// \||/ demote || \||/
3030// \/ || || \/
3031// +-------------------------+ || +-------------------------+
3032// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3033// +-------------------------+ || +-------------------------+
3034// || || ||
3035// || demote \||/
3036// promote || \/ NOT possible
3037// || || /\~
3038// \||/ || /||\~
3039// \/ || ||
3040// +-----------------------------+
3041// | if (p0.new) memw(..)=r0.new |
3042// +-----------------------------+
3043// Double Dot New Store
3044//
3045// Returns the most basic instruction for the .new predicated instructions and
3046// new-value stores.
3047// For example, all of the following instructions will be converted back to the
3048// same instruction:
3049// 1) if (p0.new) memw(R0+#0) = R1.new --->
3050// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3051// 3) if (p0.new) memw(R0+#0) = R1 --->
3052//
3053// To understand the translation of instruction 1 to its original form, consider
3054// a packet with 3 instructions.
3055// { p0 = cmp.eq(R0,R1)
3056// if (p0.new) R2 = add(R3, R4)
3057// R5 = add (R3, R1)
3058// }
3059// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3060//
3061// This instruction can be part of the previous packet only if both p0 and R2
3062// are promoted to .new values. This promotion happens in steps, first
3063// predicate register is promoted to .new and in the next iteration R2 is
3064// promoted. Therefore, in case of dependence check failure (due to R5) during
3065// next iteration, it should be converted back to its most basic form.
3066
3067
3068// Return the new value instruction for a given store.
3069int HexagonInstrInfo::getDotNewOp(const MachineInstr* MI) const {
3070 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
3071 if (NVOpcode >= 0) // Valid new-value store instruction.
3072 return NVOpcode;
3073
3074 switch (MI->getOpcode()) {
3075 default: llvm_unreachable("Unknown .new type");
3076 case Hexagon::S4_storerb_ur:
3077 return Hexagon::S4_storerbnew_ur;
3078
3079 case Hexagon::S2_storerb_pci:
3080 return Hexagon::S2_storerb_pci;
3081
3082 case Hexagon::S2_storeri_pci:
3083 return Hexagon::S2_storeri_pci;
3084
3085 case Hexagon::S2_storerh_pci:
3086 return Hexagon::S2_storerh_pci;
3087
3088 case Hexagon::S2_storerd_pci:
3089 return Hexagon::S2_storerd_pci;
3090
3091 case Hexagon::S2_storerf_pci:
3092 return Hexagon::S2_storerf_pci;
3093
3094 case Hexagon::V6_vS32b_ai:
3095 return Hexagon::V6_vS32b_new_ai;
3096
3097 case Hexagon::V6_vS32b_pi:
3098 return Hexagon::V6_vS32b_new_pi;
3099
3100 // 128B
3101 case Hexagon::V6_vS32b_ai_128B:
3102 return Hexagon::V6_vS32b_new_ai_128B;
3103
3104 case Hexagon::V6_vS32b_pi_128B:
3105 return Hexagon::V6_vS32b_new_pi_128B;
3106 }
3107 return 0;
3108}
3109
3110// Returns the opcode to use when converting MI, which is a conditional jump,
3111// into a conditional instruction which uses the .new value of the predicate.
3112// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003113int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003114 const MachineBranchProbabilityInfo *MBPI) const {
3115 // We assume that block can have at most two successors.
3116 bool taken = false;
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003117 const MachineBasicBlock *Src = MI->getParent();
3118 const MachineOperand *BrTarget = &MI->getOperand(1);
3119 const MachineBasicBlock *Dst = BrTarget->getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003120
3121 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3122 if (Prediction >= BranchProbability(1,2))
3123 taken = true;
3124
3125 switch (MI->getOpcode()) {
3126 case Hexagon::J2_jumpt:
3127 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3128 case Hexagon::J2_jumpf:
3129 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3130
3131 default:
3132 llvm_unreachable("Unexpected jump instruction.");
3133 }
3134}
3135
3136
3137// Return .new predicate version for an instruction.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003138int HexagonInstrInfo::getDotNewPredOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003139 const MachineBranchProbabilityInfo *MBPI) const {
3140 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
3141 if (NewOpcode >= 0) // Valid predicate new instruction
3142 return NewOpcode;
3143
3144 switch (MI->getOpcode()) {
3145 // Condtional Jumps
3146 case Hexagon::J2_jumpt:
3147 case Hexagon::J2_jumpf:
3148 return getDotNewPredJumpOp(MI, MBPI);
3149
3150 default:
3151 assert(0 && "Unknown .new type");
3152 }
3153 return 0;
3154}
3155
3156
3157int HexagonInstrInfo::getDotOldOp(const int opc) const {
3158 int NewOp = opc;
3159 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3160 NewOp = Hexagon::getPredOldOpcode(NewOp);
3161 assert(NewOp >= 0 &&
3162 "Couldn't change predicate new instruction to its old form.");
3163 }
3164
3165 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3166 NewOp = Hexagon::getNonNVStore(NewOp);
3167 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3168 }
3169 return NewOp;
3170}
3171
3172
3173// See if instruction could potentially be a duplex candidate.
3174// If so, return its group. Zero otherwise.
3175HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3176 const MachineInstr *MI) const {
3177 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3178 auto &HRI = getRegisterInfo();
3179
3180 switch (MI->getOpcode()) {
3181 default:
3182 return HexagonII::HSIG_None;
3183 //
3184 // Group L1:
3185 //
3186 // Rd = memw(Rs+#u4:2)
3187 // Rd = memub(Rs+#u4:0)
3188 case Hexagon::L2_loadri_io:
3189 DstReg = MI->getOperand(0).getReg();
3190 SrcReg = MI->getOperand(1).getReg();
3191 // Special case this one from Group L2.
3192 // Rd = memw(r29+#u5:2)
3193 if (isIntRegForSubInst(DstReg)) {
3194 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3195 HRI.getStackRegister() == SrcReg &&
3196 MI->getOperand(2).isImm() &&
3197 isShiftedUInt<5,2>(MI->getOperand(2).getImm()))
3198 return HexagonII::HSIG_L2;
3199 // Rd = memw(Rs+#u4:2)
3200 if (isIntRegForSubInst(SrcReg) &&
3201 (MI->getOperand(2).isImm() &&
3202 isShiftedUInt<4,2>(MI->getOperand(2).getImm())))
3203 return HexagonII::HSIG_L1;
3204 }
3205 break;
3206 case Hexagon::L2_loadrub_io:
3207 // Rd = memub(Rs+#u4:0)
3208 DstReg = MI->getOperand(0).getReg();
3209 SrcReg = MI->getOperand(1).getReg();
3210 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3211 MI->getOperand(2).isImm() && isUInt<4>(MI->getOperand(2).getImm()))
3212 return HexagonII::HSIG_L1;
3213 break;
3214 //
3215 // Group L2:
3216 //
3217 // Rd = memh/memuh(Rs+#u3:1)
3218 // Rd = memb(Rs+#u3:0)
3219 // Rd = memw(r29+#u5:2) - Handled above.
3220 // Rdd = memd(r29+#u5:3)
3221 // deallocframe
3222 // [if ([!]p0[.new])] dealloc_return
3223 // [if ([!]p0[.new])] jumpr r31
3224 case Hexagon::L2_loadrh_io:
3225 case Hexagon::L2_loadruh_io:
3226 // Rd = memh/memuh(Rs+#u3:1)
3227 DstReg = MI->getOperand(0).getReg();
3228 SrcReg = MI->getOperand(1).getReg();
3229 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3230 MI->getOperand(2).isImm() &&
3231 isShiftedUInt<3,1>(MI->getOperand(2).getImm()))
3232 return HexagonII::HSIG_L2;
3233 break;
3234 case Hexagon::L2_loadrb_io:
3235 // Rd = memb(Rs+#u3:0)
3236 DstReg = MI->getOperand(0).getReg();
3237 SrcReg = MI->getOperand(1).getReg();
3238 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3239 MI->getOperand(2).isImm() &&
3240 isUInt<3>(MI->getOperand(2).getImm()))
3241 return HexagonII::HSIG_L2;
3242 break;
3243 case Hexagon::L2_loadrd_io:
3244 // Rdd = memd(r29+#u5:3)
3245 DstReg = MI->getOperand(0).getReg();
3246 SrcReg = MI->getOperand(1).getReg();
3247 if (isDblRegForSubInst(DstReg, HRI) &&
3248 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3249 HRI.getStackRegister() == SrcReg &&
3250 MI->getOperand(2).isImm() &&
3251 isShiftedUInt<5,3>(MI->getOperand(2).getImm()))
3252 return HexagonII::HSIG_L2;
3253 break;
3254 // dealloc_return is not documented in Hexagon Manual, but marked
3255 // with A_SUBINSN attribute in iset_v4classic.py.
3256 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3257 case Hexagon::L4_return:
3258 case Hexagon::L2_deallocframe:
3259 return HexagonII::HSIG_L2;
3260 case Hexagon::EH_RETURN_JMPR:
3261 case Hexagon::JMPret :
3262 // jumpr r31
3263 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
3264 DstReg = MI->getOperand(0).getReg();
3265 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3266 return HexagonII::HSIG_L2;
3267 break;
3268 case Hexagon::JMPrett:
3269 case Hexagon::JMPretf:
3270 case Hexagon::JMPrettnewpt:
3271 case Hexagon::JMPretfnewpt :
3272 case Hexagon::JMPrettnew :
3273 case Hexagon::JMPretfnew :
3274 DstReg = MI->getOperand(1).getReg();
3275 SrcReg = MI->getOperand(0).getReg();
3276 // [if ([!]p0[.new])] jumpr r31
3277 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3278 (Hexagon::P0 == SrcReg)) &&
3279 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3280 return HexagonII::HSIG_L2;
3281 break;
3282 case Hexagon::L4_return_t :
3283 case Hexagon::L4_return_f :
3284 case Hexagon::L4_return_tnew_pnt :
3285 case Hexagon::L4_return_fnew_pnt :
3286 case Hexagon::L4_return_tnew_pt :
3287 case Hexagon::L4_return_fnew_pt :
3288 // [if ([!]p0[.new])] dealloc_return
3289 SrcReg = MI->getOperand(0).getReg();
3290 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3291 return HexagonII::HSIG_L2;
3292 break;
3293 //
3294 // Group S1:
3295 //
3296 // memw(Rs+#u4:2) = Rt
3297 // memb(Rs+#u4:0) = Rt
3298 case Hexagon::S2_storeri_io:
3299 // Special case this one from Group S2.
3300 // memw(r29+#u5:2) = Rt
3301 Src1Reg = MI->getOperand(0).getReg();
3302 Src2Reg = MI->getOperand(2).getReg();
3303 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3304 isIntRegForSubInst(Src2Reg) &&
3305 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3306 isShiftedUInt<5,2>(MI->getOperand(1).getImm()))
3307 return HexagonII::HSIG_S2;
3308 // memw(Rs+#u4:2) = Rt
3309 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3310 MI->getOperand(1).isImm() &&
3311 isShiftedUInt<4,2>(MI->getOperand(1).getImm()))
3312 return HexagonII::HSIG_S1;
3313 break;
3314 case Hexagon::S2_storerb_io:
3315 // memb(Rs+#u4:0) = Rt
3316 Src1Reg = MI->getOperand(0).getReg();
3317 Src2Reg = MI->getOperand(2).getReg();
3318 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3319 MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()))
3320 return HexagonII::HSIG_S1;
3321 break;
3322 //
3323 // Group S2:
3324 //
3325 // memh(Rs+#u3:1) = Rt
3326 // memw(r29+#u5:2) = Rt
3327 // memd(r29+#s6:3) = Rtt
3328 // memw(Rs+#u4:2) = #U1
3329 // memb(Rs+#u4) = #U1
3330 // allocframe(#u5:3)
3331 case Hexagon::S2_storerh_io:
3332 // memh(Rs+#u3:1) = Rt
3333 Src1Reg = MI->getOperand(0).getReg();
3334 Src2Reg = MI->getOperand(2).getReg();
3335 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3336 MI->getOperand(1).isImm() &&
3337 isShiftedUInt<3,1>(MI->getOperand(1).getImm()))
3338 return HexagonII::HSIG_S1;
3339 break;
3340 case Hexagon::S2_storerd_io:
3341 // memd(r29+#s6:3) = Rtt
3342 Src1Reg = MI->getOperand(0).getReg();
3343 Src2Reg = MI->getOperand(2).getReg();
3344 if (isDblRegForSubInst(Src2Reg, HRI) &&
3345 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3346 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3347 isShiftedInt<6,3>(MI->getOperand(1).getImm()))
3348 return HexagonII::HSIG_S2;
3349 break;
3350 case Hexagon::S4_storeiri_io:
3351 // memw(Rs+#u4:2) = #U1
3352 Src1Reg = MI->getOperand(0).getReg();
3353 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3354 isShiftedUInt<4,2>(MI->getOperand(1).getImm()) &&
3355 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3356 return HexagonII::HSIG_S2;
3357 break;
3358 case Hexagon::S4_storeirb_io:
3359 // memb(Rs+#u4) = #U1
3360 Src1Reg = MI->getOperand(0).getReg();
3361 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3362 isUInt<4>(MI->getOperand(1).getImm()) && MI->getOperand(2).isImm() &&
3363 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3364 return HexagonII::HSIG_S2;
3365 break;
3366 case Hexagon::S2_allocframe:
3367 if (MI->getOperand(0).isImm() &&
3368 isShiftedUInt<5,3>(MI->getOperand(0).getImm()))
3369 return HexagonII::HSIG_S1;
3370 break;
3371 //
3372 // Group A:
3373 //
3374 // Rx = add(Rx,#s7)
3375 // Rd = Rs
3376 // Rd = #u6
3377 // Rd = #-1
3378 // if ([!]P0[.new]) Rd = #0
3379 // Rd = add(r29,#u6:2)
3380 // Rx = add(Rx,Rs)
3381 // P0 = cmp.eq(Rs,#u2)
3382 // Rdd = combine(#0,Rs)
3383 // Rdd = combine(Rs,#0)
3384 // Rdd = combine(#u2,#U2)
3385 // Rd = add(Rs,#1)
3386 // Rd = add(Rs,#-1)
3387 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3388 // Rd = and(Rs,#1)
3389 case Hexagon::A2_addi:
3390 DstReg = MI->getOperand(0).getReg();
3391 SrcReg = MI->getOperand(1).getReg();
3392 if (isIntRegForSubInst(DstReg)) {
3393 // Rd = add(r29,#u6:2)
3394 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3395 HRI.getStackRegister() == SrcReg && MI->getOperand(2).isImm() &&
3396 isShiftedUInt<6,2>(MI->getOperand(2).getImm()))
3397 return HexagonII::HSIG_A;
3398 // Rx = add(Rx,#s7)
3399 if ((DstReg == SrcReg) && MI->getOperand(2).isImm() &&
3400 isInt<7>(MI->getOperand(2).getImm()))
3401 return HexagonII::HSIG_A;
3402 // Rd = add(Rs,#1)
3403 // Rd = add(Rs,#-1)
3404 if (isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3405 ((MI->getOperand(2).getImm() == 1) ||
3406 (MI->getOperand(2).getImm() == -1)))
3407 return HexagonII::HSIG_A;
3408 }
3409 break;
3410 case Hexagon::A2_add:
3411 // Rx = add(Rx,Rs)
3412 DstReg = MI->getOperand(0).getReg();
3413 Src1Reg = MI->getOperand(1).getReg();
3414 Src2Reg = MI->getOperand(2).getReg();
3415 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3416 isIntRegForSubInst(Src2Reg))
3417 return HexagonII::HSIG_A;
3418 break;
3419 case Hexagon::A2_andir:
3420 // Same as zxtb.
3421 // Rd16=and(Rs16,#255)
3422 // Rd16=and(Rs16,#1)
3423 DstReg = MI->getOperand(0).getReg();
3424 SrcReg = MI->getOperand(1).getReg();
3425 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3426 MI->getOperand(2).isImm() &&
3427 ((MI->getOperand(2).getImm() == 1) ||
3428 (MI->getOperand(2).getImm() == 255)))
3429 return HexagonII::HSIG_A;
3430 break;
3431 case Hexagon::A2_tfr:
3432 // Rd = Rs
3433 DstReg = MI->getOperand(0).getReg();
3434 SrcReg = MI->getOperand(1).getReg();
3435 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3436 return HexagonII::HSIG_A;
3437 break;
3438 case Hexagon::A2_tfrsi:
3439 // Rd = #u6
3440 // Do not test for #u6 size since the const is getting extended
3441 // regardless and compound could be formed.
3442 // Rd = #-1
3443 DstReg = MI->getOperand(0).getReg();
3444 if (isIntRegForSubInst(DstReg))
3445 return HexagonII::HSIG_A;
3446 break;
3447 case Hexagon::C2_cmoveit:
3448 case Hexagon::C2_cmovenewit:
3449 case Hexagon::C2_cmoveif:
3450 case Hexagon::C2_cmovenewif:
3451 // if ([!]P0[.new]) Rd = #0
3452 // Actual form:
3453 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
3454 DstReg = MI->getOperand(0).getReg();
3455 SrcReg = MI->getOperand(1).getReg();
3456 if (isIntRegForSubInst(DstReg) &&
3457 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
3458 MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0)
3459 return HexagonII::HSIG_A;
3460 break;
3461 case Hexagon::C2_cmpeqi:
3462 // P0 = cmp.eq(Rs,#u2)
3463 DstReg = MI->getOperand(0).getReg();
3464 SrcReg = MI->getOperand(1).getReg();
3465 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3466 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
3467 MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm()))
3468 return HexagonII::HSIG_A;
3469 break;
3470 case Hexagon::A2_combineii:
3471 case Hexagon::A4_combineii:
3472 // Rdd = combine(#u2,#U2)
3473 DstReg = MI->getOperand(0).getReg();
3474 if (isDblRegForSubInst(DstReg, HRI) &&
3475 ((MI->getOperand(1).isImm() && isUInt<2>(MI->getOperand(1).getImm())) ||
3476 (MI->getOperand(1).isGlobal() &&
3477 isUInt<2>(MI->getOperand(1).getOffset()))) &&
3478 ((MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm())) ||
3479 (MI->getOperand(2).isGlobal() &&
3480 isUInt<2>(MI->getOperand(2).getOffset()))))
3481 return HexagonII::HSIG_A;
3482 break;
3483 case Hexagon::A4_combineri:
3484 // Rdd = combine(Rs,#0)
3485 DstReg = MI->getOperand(0).getReg();
3486 SrcReg = MI->getOperand(1).getReg();
3487 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3488 ((MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) ||
3489 (MI->getOperand(2).isGlobal() && MI->getOperand(2).getOffset() == 0)))
3490 return HexagonII::HSIG_A;
3491 break;
3492 case Hexagon::A4_combineir:
3493 // Rdd = combine(#0,Rs)
3494 DstReg = MI->getOperand(0).getReg();
3495 SrcReg = MI->getOperand(2).getReg();
3496 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3497 ((MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0) ||
3498 (MI->getOperand(1).isGlobal() && MI->getOperand(1).getOffset() == 0)))
3499 return HexagonII::HSIG_A;
3500 break;
3501 case Hexagon::A2_sxtb:
3502 case Hexagon::A2_sxth:
3503 case Hexagon::A2_zxtb:
3504 case Hexagon::A2_zxth:
3505 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3506 DstReg = MI->getOperand(0).getReg();
3507 SrcReg = MI->getOperand(1).getReg();
3508 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3509 return HexagonII::HSIG_A;
3510 break;
3511 }
3512
3513 return HexagonII::HSIG_None;
3514}
3515
3516
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003517short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003518 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Real);
3519}
3520
3521
3522// Return first non-debug instruction in the basic block.
3523MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3524 const {
3525 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
3526 MachineInstr *MI = &*MII;
3527 if (MI->isDebugValue())
3528 continue;
3529 return MI;
3530 }
3531 return nullptr;
3532}
3533
3534
3535unsigned HexagonInstrInfo::getInstrTimingClassLatency(
3536 const InstrItineraryData *ItinData, const MachineInstr *MI) const {
3537 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3538 // still have a MinLatency property, which getStageLatency checks.
3539 if (!ItinData)
3540 return getInstrLatency(ItinData, MI);
3541
3542 // Get the latency embedded in the itinerary. If we're not using timing class
3543 // latencies or if we using BSB scheduling, then restrict the maximum latency
3544 // to 1 (that is, either 0 or 1).
3545 if (MI->isTransient())
3546 return 0;
3547 unsigned Latency = ItinData->getStageLatency(MI->getDesc().getSchedClass());
3548 if (!EnableTimingClassLatency ||
3549 MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>().
3550 useBSBScheduling())
3551 if (Latency > 1)
3552 Latency = 1;
3553 return Latency;
3554}
3555
3556
3557// inverts the predication logic.
3558// p -> NotP
3559// NotP -> P
3560bool HexagonInstrInfo::getInvertedPredSense(
3561 SmallVectorImpl<MachineOperand> &Cond) const {
3562 if (Cond.empty())
3563 return false;
3564 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3565 Cond[0].setImm(Opc);
3566 return true;
3567}
3568
3569
3570unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3571 int InvPredOpcode;
3572 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3573 : Hexagon::getTruePredOpcode(Opc);
3574 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3575 return InvPredOpcode;
3576
3577 llvm_unreachable("Unexpected predicated instruction");
3578}
3579
3580
3581// Returns the max value that doesn't need to be extended.
3582int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
3583 const uint64_t F = MI->getDesc().TSFlags;
3584 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3585 & HexagonII::ExtentSignedMask;
3586 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3587 & HexagonII::ExtentBitsMask;
3588
3589 if (isSigned) // if value is signed
3590 return ~(-1U << (bits - 1));
3591 else
3592 return ~(-1U << bits);
3593}
3594
3595
3596unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr* MI) const {
3597 const uint64_t F = MI->getDesc().TSFlags;
3598 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3599}
3600
3601
3602// Returns the min value that doesn't need to be extended.
3603int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
3604 const uint64_t F = MI->getDesc().TSFlags;
3605 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3606 & HexagonII::ExtentSignedMask;
3607 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3608 & HexagonII::ExtentBitsMask;
3609
3610 if (isSigned) // if value is signed
3611 return -1U << (bits - 1);
3612 else
3613 return 0;
3614}
3615
3616
3617// Returns opcode of the non-extended equivalent instruction.
3618short HexagonInstrInfo::getNonExtOpcode(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003619 // Check if the instruction has a register form that uses register in place
3620 // of the extended operand, if so return that as the non-extended form.
3621 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
3622 if (NonExtOpcode >= 0)
3623 return NonExtOpcode;
3624
3625 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003626 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003627 switch (getAddrMode(MI)) {
3628 case HexagonII::Absolute :
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00003629 return Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003630 case HexagonII::BaseImmOffset :
3631 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003632 case HexagonII::BaseLongOffset:
3633 return Hexagon::getRegShlForm(MI->getOpcode());
3634
Jyotsna Verma84256432013-03-01 17:37:13 +00003635 default:
3636 return -1;
3637 }
3638 }
3639 return -1;
3640}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00003641
Brendon Cahoondf43e682015-05-08 16:16:29 +00003642
Ahmed Bougachac88bf542015-06-11 19:30:37 +00003643bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003644 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00003645 if (Cond.empty())
3646 return false;
3647 assert(Cond.size() == 2);
3648 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
3649 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3650 return false;
3651 }
3652 PredReg = Cond[1].getReg();
3653 PredRegPos = 1;
3654 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3655 PredRegFlags = 0;
3656 if (Cond[1].isImplicit())
3657 PredRegFlags = RegState::Implicit;
3658 if (Cond[1].isUndef())
3659 PredRegFlags |= RegState::Undef;
3660 return true;
3661}
3662
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003663
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003664short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003665 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Pseudo);
3666}
3667
3668
3669short HexagonInstrInfo::getRegForm(const MachineInstr *MI) const {
3670 return Hexagon::getRegForm(MI->getOpcode());
3671}
3672
3673
3674// Return the number of bytes required to encode the instruction.
3675// Hexagon instructions are fixed length, 4 bytes, unless they
3676// use a constant extender, which requires another 4 bytes.
3677// For debug instructions and prolog labels, return 0.
3678unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
3679 if (MI->isDebugValue() || MI->isPosition())
3680 return 0;
3681
3682 unsigned Size = MI->getDesc().getSize();
3683 if (!Size)
3684 // Assume the default insn size in case it cannot be determined
3685 // for whatever reason.
3686 Size = HEXAGON_INSTR_SIZE;
3687
3688 if (isConstExtended(MI) || isExtended(MI))
3689 Size += HEXAGON_INSTR_SIZE;
3690
3691 // Try and compute number of instructions in asm.
3692 if (BranchRelaxAsmLarge && MI->getOpcode() == Hexagon::INLINEASM) {
3693 const MachineBasicBlock &MBB = *MI->getParent();
3694 const MachineFunction *MF = MBB.getParent();
3695 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
3696
3697 // Count the number of register definitions to find the asm string.
3698 unsigned NumDefs = 0;
3699 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
3700 ++NumDefs)
3701 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
3702
3703 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
3704 // Disassemble the AsmStr and approximate number of instructions.
3705 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
3706 Size = getInlineAsmLength(AsmStr, *MAI);
3707 }
3708
3709 return Size;
3710}
3711
3712
3713uint64_t HexagonInstrInfo::getType(const MachineInstr* MI) const {
3714 const uint64_t F = MI->getDesc().TSFlags;
3715 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
3716}
3717
3718
3719unsigned HexagonInstrInfo::getUnits(const MachineInstr* MI) const {
3720 const TargetSubtargetInfo &ST = MI->getParent()->getParent()->getSubtarget();
3721 const InstrItineraryData &II = *ST.getInstrItineraryData();
3722 const InstrStage &IS = *II.beginStage(MI->getDesc().getSchedClass());
3723
3724 return IS.getUnits();
3725}
3726
3727
3728unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
3729 const uint64_t F = get(Opcode).TSFlags;
3730 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
3731}
3732
3733
3734// Calculate size of the basic block without debug instructions.
3735unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
3736 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
3737}
3738
3739
3740unsigned HexagonInstrInfo::nonDbgBundleSize(
3741 MachineBasicBlock::const_iterator BundleHead) const {
3742 assert(BundleHead->isBundle() && "Not a bundle header");
3743 auto MII = BundleHead.getInstrIterator();
3744 // Skip the bundle header.
3745 return nonDbgMICount(++MII, getBundleEnd(BundleHead));
3746}
3747
3748
3749/// immediateExtend - Changes the instruction in place to one using an immediate
3750/// extender.
3751void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
3752 assert((isExtendable(MI)||isConstExtended(MI)) &&
3753 "Instruction must be extendable");
3754 // Find which operand is extendable.
3755 short ExtOpNum = getCExtOpNum(MI);
3756 MachineOperand &MO = MI->getOperand(ExtOpNum);
3757 // This needs to be something we understand.
3758 assert((MO.isMBB() || MO.isImm()) &&
3759 "Branch with unknown extendable field type");
3760 // Mark given operand as extended.
3761 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
3762}
3763
3764
3765bool HexagonInstrInfo::invertAndChangeJumpTarget(
3766 MachineInstr* MI, MachineBasicBlock* NewTarget) const {
3767 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
3768 << NewTarget->getNumber(); MI->dump(););
3769 assert(MI->isBranch());
3770 unsigned NewOpcode = getInvertedPredicatedOpcode(MI->getOpcode());
3771 int TargetPos = MI->getNumOperands() - 1;
3772 // In general branch target is the last operand,
3773 // but some implicit defs added at the end might change it.
3774 while ((TargetPos > -1) && !MI->getOperand(TargetPos).isMBB())
3775 --TargetPos;
3776 assert((TargetPos >= 0) && MI->getOperand(TargetPos).isMBB());
3777 MI->getOperand(TargetPos).setMBB(NewTarget);
3778 if (EnableBranchPrediction && isPredicatedNew(MI)) {
3779 NewOpcode = reversePrediction(NewOpcode);
3780 }
3781 MI->setDesc(get(NewOpcode));
3782 return true;
3783}
3784
3785
3786void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
3787 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
3788 MachineFunction::iterator A = MF.begin();
3789 MachineBasicBlock &B = *A;
3790 MachineBasicBlock::iterator I = B.begin();
3791 MachineInstr *MI = &*I;
3792 DebugLoc DL = MI->getDebugLoc();
3793 MachineInstr *NewMI;
3794
3795 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
3796 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
3797 NewMI = BuildMI(B, MI, DL, get(insn));
3798 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
3799 " Class: " << NewMI->getDesc().getSchedClass());
3800 NewMI->eraseFromParent();
3801 }
3802 /* --- The code above is used to generate complete set of Hexagon Insn --- */
3803}
3804
3805
3806// inverts the predication logic.
3807// p -> NotP
3808// NotP -> P
3809bool HexagonInstrInfo::reversePredSense(MachineInstr* MI) const {
3810 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI->dump());
3811 MI->setDesc(get(getInvertedPredicatedOpcode(MI->getOpcode())));
3812 return true;
3813}
3814
3815
3816// Reverse the branch prediction.
3817unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
3818 int PredRevOpcode = -1;
3819 if (isPredictedTaken(Opcode))
3820 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
3821 else
3822 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
3823 assert(PredRevOpcode > 0);
3824 return PredRevOpcode;
3825}
3826
3827
3828// TODO: Add more rigorous validation.
3829bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
3830 const {
3831 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
3832}
3833