blob: 9e908ba54449e3884babcddae35fabfa6d130678 [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"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000030
Tony Linthicum1213a7a2011-12-12 21:14:40 +000031using namespace llvm;
32
Chandler Carruthe96dd892014-04-21 22:55:11 +000033#define DEBUG_TYPE "hexagon-instrinfo"
34
Chandler Carruthd174b722014-04-22 02:03:14 +000035#define GET_INSTRINFO_CTOR_DTOR
36#define GET_INSTRMAP_INFO
37#include "HexagonGenInstrInfo.inc"
38#include "HexagonGenDFAPacketizer.inc"
39
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000040using namespace llvm;
41
42static cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
43 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
44 "packetization boundary."));
45
46static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
47 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
48
49static cl::opt<bool> EnableTimingClassLatency(
50 "enable-timing-class-latency", cl::Hidden, cl::init(false),
51 cl::desc("Enable timing class latency"));
52
53static cl::opt<bool> EnableALUForwarding(
54 "enable-alu-forwarding", cl::Hidden, cl::init(true),
55 cl::desc("Enable vec alu forwarding"));
56
57static cl::opt<bool> EnableACCForwarding(
58 "enable-acc-forwarding", cl::Hidden, cl::init(true),
59 cl::desc("Enable vec acc forwarding"));
60
61static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
62 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
63
Tony Linthicum1213a7a2011-12-12 21:14:40 +000064///
65/// Constants for Hexagon instructions.
66///
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000067const int Hexagon_MEMV_OFFSET_MAX_128B = 2047; // #s7
68const int Hexagon_MEMV_OFFSET_MIN_128B = -2048; // #s7
69const int Hexagon_MEMV_OFFSET_MAX = 1023; // #s6
70const int Hexagon_MEMV_OFFSET_MIN = -1024; // #s6
Tony Linthicum1213a7a2011-12-12 21:14:40 +000071const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000072const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000073const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000074const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000075const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000076const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000077const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000078const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000079const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000080const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000081const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000082const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000083const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000084const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000086const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000087const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000088const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000089const int Hexagon_MEMV_AUTOINC_MAX = 192;
90const int Hexagon_MEMV_AUTOINC_MIN = -256;
91const int Hexagon_MEMV_AUTOINC_MAX_128B = 384;
92const int Hexagon_MEMV_AUTOINC_MIN_128B = -512;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000094// Pin the vtable to this file.
95void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +000096
97HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +000098 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000099 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000100
101
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000102static bool isIntRegForSubInst(unsigned Reg) {
103 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
104 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105}
106
107
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000108static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
109 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_loreg)) &&
110 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_hireg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111}
112
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000113
114/// Calculate number of instructions excluding the debug instructions.
115static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
116 MachineBasicBlock::const_instr_iterator MIE) {
117 unsigned Count = 0;
118 for (; MIB != MIE; ++MIB) {
119 if (!MIB->isDebugValue())
120 ++Count;
121 }
122 return Count;
123}
124
125
126/// Find the hardware loop instruction used to set-up the specified loop.
127/// On Hexagon, we have two instructions used to set-up the hardware loop
128/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
129/// to indicate the end of a loop.
130static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
131 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000132 int LOOPi;
133 int LOOPr;
134 if (EndLoopOp == Hexagon::ENDLOOP0) {
135 LOOPi = Hexagon::J2_loop0i;
136 LOOPr = Hexagon::J2_loop0r;
137 } else { // EndLoopOp == Hexagon::EndLOOP1
138 LOOPi = Hexagon::J2_loop1i;
139 LOOPr = Hexagon::J2_loop1r;
140 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000141
Brendon Cahoondf43e682015-05-08 16:16:29 +0000142 // The loop set-up instruction will be in a predecessor block
143 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
144 PE = BB->pred_end(); PB != PE; ++PB) {
145 // If this has been visited, already skip it.
146 if (!Visited.insert(*PB).second)
147 continue;
148 if (*PB == BB)
149 continue;
150 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
151 E = (*PB)->instr_rend(); I != E; ++I) {
152 int Opc = I->getOpcode();
153 if (Opc == LOOPi || Opc == LOOPr)
154 return &*I;
155 // We've reached a different loop, which means the loop0 has been removed.
156 if (Opc == EndLoopOp)
157 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000158 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000159 // Check the predecessors for the LOOP instruction.
160 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
161 if (loop)
162 return loop;
163 }
164 return 0;
165}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000166
Brendon Cahoondf43e682015-05-08 16:16:29 +0000167
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000168/// Gather register def/uses from MI.
169/// This treats possible (predicated) defs as actually happening ones
170/// (conservatively).
171static inline void parseOperands(const MachineInstr *MI,
172 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
173 Defs.clear();
174 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000175
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000176 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
177 const MachineOperand &MO = MI->getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000178
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000179 if (!MO.isReg())
180 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000181
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000182 unsigned Reg = MO.getReg();
183 if (!Reg)
184 continue;
185
186 if (MO.isUse())
187 Uses.push_back(MO.getReg());
188
189 if (MO.isDef())
190 Defs.push_back(MO.getReg());
191 }
192}
193
194
195// Position dependent, so check twice for swap.
196static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
197 switch (Ga) {
198 case HexagonII::HSIG_None:
199 default:
200 return false;
201 case HexagonII::HSIG_L1:
202 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
203 case HexagonII::HSIG_L2:
204 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
205 Gb == HexagonII::HSIG_A);
206 case HexagonII::HSIG_S1:
207 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
208 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
209 case HexagonII::HSIG_S2:
210 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
211 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
212 Gb == HexagonII::HSIG_A);
213 case HexagonII::HSIG_A:
214 return (Gb == HexagonII::HSIG_A);
215 case HexagonII::HSIG_Compound:
216 return (Gb == HexagonII::HSIG_Compound);
217 }
218 return false;
219}
220
221
222
223/// isLoadFromStackSlot - If the specified machine instruction is a direct
224/// load from a stack slot, return the virtual or physical register number of
225/// the destination along with the FrameIndex of the loaded stack slot. If
226/// not, return 0. This predicate must return 0 if the instruction has
227/// any side effects other than loading from the stack slot.
228unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
229 int &FrameIndex) const {
230 switch (MI->getOpcode()) {
231 default: break;
232 case Hexagon::L2_loadri_io:
233 case Hexagon::L2_loadrd_io:
234 case Hexagon::L2_loadrh_io:
235 case Hexagon::L2_loadrb_io:
236 case Hexagon::L2_loadrub_io:
237 if (MI->getOperand(2).isFI() &&
238 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
239 FrameIndex = MI->getOperand(2).getIndex();
240 return MI->getOperand(0).getReg();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000241 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000242 break;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000243 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000244 return 0;
245}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000246
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000247
248/// isStoreToStackSlot - If the specified machine instruction is a direct
249/// store to a stack slot, return the virtual or physical register number of
250/// the source reg along with the FrameIndex of the loaded stack slot. If
251/// not, return 0. This predicate must return 0 if the instruction has
252/// any side effects other than storing to the stack slot.
253unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
254 int &FrameIndex) const {
255 switch (MI->getOpcode()) {
256 default: break;
257 case Hexagon::S2_storeri_io:
258 case Hexagon::S2_storerd_io:
259 case Hexagon::S2_storerh_io:
260 case Hexagon::S2_storerb_io:
261 if (MI->getOperand(2).isFI() &&
262 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
263 FrameIndex = MI->getOperand(0).getIndex();
264 return MI->getOperand(2).getReg();
265 }
266 break;
267 }
268 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000269}
270
271
Brendon Cahoondf43e682015-05-08 16:16:29 +0000272/// This function can analyze one/two way branching only and should (mostly) be
273/// called by target independent side.
274/// First entry is always the opcode of the branching instruction, except when
275/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
276/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
277/// e.g. Jump_c p will have
278/// Cond[0] = Jump_c
279/// Cond[1] = p
280/// HW-loop ENDLOOP:
281/// Cond[0] = ENDLOOP
282/// Cond[1] = MBB
283/// New value jump:
284/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
285/// Cond[1] = R
286/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000287///
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000288bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
289 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000290 MachineBasicBlock *&FBB,
291 SmallVectorImpl<MachineOperand> &Cond,
292 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000293 TBB = nullptr;
294 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000295 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000296
297 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000298 MachineBasicBlock::instr_iterator I = MBB.instr_end();
299 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000300 return false;
301
302 // A basic block may looks like this:
303 //
304 // [ insn
305 // EH_LABEL
306 // insn
307 // insn
308 // insn
309 // EH_LABEL
310 // insn ]
311 //
312 // It has two succs but does not have a terminator
313 // Don't know how to handle it.
314 do {
315 --I;
316 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000317 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000318 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000319 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000320
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000321 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000322 --I;
323
324 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000325 if (I == MBB.instr_begin())
326 return false;
327 --I;
328 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000329
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000330 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
331 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000332 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000333 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000334 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
335 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
336 I->eraseFromParent();
337 I = MBB.instr_end();
338 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000339 return false;
340 --I;
341 }
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000342 if (!isUnpredicatedTerminator(&*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000343 return false;
344
345 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000346 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000347 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000348 // Find one more terminator if present.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000349 for (;;) {
350 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000351 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000352 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000353 else
354 // This is a third branch.
355 return true;
356 }
357 if (I == MBB.instr_begin())
358 break;
359 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000360 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000361
362 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000363 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
364 // If the branch target is not a basic block, it could be a tail call.
365 // (It is, if the target is a function.)
366 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
367 return true;
368 if (SecLastOpcode == Hexagon::J2_jump &&
369 !SecondLastInst->getOperand(0).isMBB())
370 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000371
372 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000373 bool LastOpcodeHasNVJump = isNewValueJump(LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000374
375 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000376 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000377 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000378 TBB = LastInst->getOperand(0).getMBB();
379 return false;
380 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000381 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000382 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000383 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000384 Cond.push_back(LastInst->getOperand(0));
385 return false;
386 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000387 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000388 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000389 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000390 Cond.push_back(LastInst->getOperand(0));
391 return false;
392 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000393 // Only supporting rr/ri versions of new-value jumps.
394 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
395 TBB = LastInst->getOperand(2).getMBB();
396 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
397 Cond.push_back(LastInst->getOperand(0));
398 Cond.push_back(LastInst->getOperand(1));
399 return false;
400 }
401 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
402 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000403 // Otherwise, don't know what this is.
404 return true;
405 }
406
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000407 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000408 bool SecLastOpcodeHasNVJump = isNewValueJump(SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000409 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000410 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000411 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000412 Cond.push_back(SecondLastInst->getOperand(0));
413 FBB = LastInst->getOperand(0).getMBB();
414 return false;
415 }
416
Brendon Cahoondf43e682015-05-08 16:16:29 +0000417 // Only supporting rr/ri versions of new-value jumps.
418 if (SecLastOpcodeHasNVJump &&
419 (SecondLastInst->getNumExplicitOperands() == 3) &&
420 (LastOpcode == Hexagon::J2_jump)) {
421 TBB = SecondLastInst->getOperand(2).getMBB();
422 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
423 Cond.push_back(SecondLastInst->getOperand(0));
424 Cond.push_back(SecondLastInst->getOperand(1));
425 FBB = LastInst->getOperand(0).getMBB();
426 return false;
427 }
428
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000429 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
430 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000431 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000432 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000433 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000434 if (AllowModify)
435 I->eraseFromParent();
436 return false;
437 }
438
Brendon Cahoondf43e682015-05-08 16:16:29 +0000439 // If the block ends with an ENDLOOP, and J2_jump, handle it.
440 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000441 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000442 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000443 Cond.push_back(SecondLastInst->getOperand(0));
444 FBB = LastInst->getOperand(0).getMBB();
445 return false;
446 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000447 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
448 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000449 // Otherwise, can't handle this.
450 return true;
451}
452
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000453
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000454unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000455 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000456 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000457 unsigned Count = 0;
458 while (I != MBB.begin()) {
459 --I;
460 if (I->isDebugValue())
461 continue;
462 // Only removing branches from end of MBB.
463 if (!I->isBranch())
464 return Count;
465 if (Count && (I->getOpcode() == Hexagon::J2_jump))
466 llvm_unreachable("Malformed basic block: unconditional branch not last");
467 MBB.erase(&MBB.back());
468 I = MBB.end();
469 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000470 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000471 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000472}
473
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000474
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000475unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
476 MachineBasicBlock *TBB, MachineBasicBlock *FBB,
477 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
478 unsigned BOpc = Hexagon::J2_jump;
479 unsigned BccOpc = Hexagon::J2_jumpt;
480 assert(validateBranchCond(Cond) && "Invalid branching condition");
481 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
482
483 // Check if ReverseBranchCondition has asked to reverse this branch
484 // If we want to reverse the branch an odd number of times, we want
485 // J2_jumpf.
486 if (!Cond.empty() && Cond[0].isImm())
487 BccOpc = Cond[0].getImm();
488
489 if (!FBB) {
490 if (Cond.empty()) {
491 // Due to a bug in TailMerging/CFG Optimization, we need to add a
492 // special case handling of a predicated jump followed by an
493 // unconditional jump. If not, Tail Merging and CFG Optimization go
494 // into an infinite loop.
495 MachineBasicBlock *NewTBB, *NewFBB;
496 SmallVector<MachineOperand, 4> Cond;
497 MachineInstr *Term = MBB.getFirstTerminator();
498 if (Term != MBB.end() && isPredicated(Term) &&
499 !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond, false)) {
500 MachineBasicBlock *NextBB = &*++MBB.getIterator();
501 if (NewTBB == NextBB) {
502 ReverseBranchCondition(Cond);
503 RemoveBranch(MBB);
504 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
505 }
506 }
507 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
508 } else if (isEndLoopN(Cond[0].getImm())) {
509 int EndLoopOp = Cond[0].getImm();
510 assert(Cond[1].isMBB());
511 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
512 // Check for it, and change the BB target if needed.
513 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
514 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
515 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
516 Loop->getOperand(0).setMBB(TBB);
517 // Add the ENDLOOP after the finding the LOOP0.
518 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
519 } else if (isNewValueJump(Cond[0].getImm())) {
520 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
521 // New value jump
522 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
523 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
524 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
525 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
526 if (Cond[2].isReg()) {
527 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
528 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
529 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
530 } else if(Cond[2].isImm()) {
531 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
532 addImm(Cond[2].getImm()).addMBB(TBB);
533 } else
534 llvm_unreachable("Invalid condition for branching");
535 } else {
536 assert((Cond.size() == 2) && "Malformed cond vector");
537 const MachineOperand &RO = Cond[1];
538 unsigned Flags = getUndefRegState(RO.isUndef());
539 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
540 }
541 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000542 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000543 assert((!Cond.empty()) &&
544 "Cond. cannot be empty when multiple branchings are required");
545 assert((!isNewValueJump(Cond[0].getImm())) &&
546 "NV-jump cannot be inserted with another branch");
547 // Special case for hardware loops. The condition is a basic block.
548 if (isEndLoopN(Cond[0].getImm())) {
549 int EndLoopOp = Cond[0].getImm();
550 assert(Cond[1].isMBB());
551 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
552 // Check for it, and change the BB target if needed.
553 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
554 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
555 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
556 Loop->getOperand(0).setMBB(TBB);
557 // Add the ENDLOOP after the finding the LOOP0.
558 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
559 } else {
560 const MachineOperand &RO = Cond[1];
561 unsigned Flags = getUndefRegState(RO.isUndef());
562 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000563 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000564 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000565
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000566 return 2;
567}
568
569
570bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
571 unsigned NumCycles, unsigned ExtraPredCycles,
572 BranchProbability Probability) const {
573 return nonDbgBBSize(&MBB) <= 3;
574}
575
576
577bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
578 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
579 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
580 const {
581 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
582}
583
584
585bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
586 unsigned NumInstrs, BranchProbability Probability) const {
587 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000588}
589
590
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000591void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000592 MachineBasicBlock::iterator I, DebugLoc DL, unsigned DestReg,
593 unsigned SrcReg, bool KillSrc) const {
594 auto &HRI = getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000595 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000596 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000597 return;
598 }
599 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000600 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000601 return;
602 }
603 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
604 // Map Pd = Ps to Pd = or(Ps, Ps).
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000605 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000606 DestReg).addReg(SrcReg).addReg(SrcReg);
607 return;
608 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000609 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
610 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000611 // We can have an overlap between single and double reg: r1:0 = r0.
612 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
613 // r1:0 = r0
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000614 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000615 Hexagon::subreg_hireg))).addImm(0);
616 } else {
617 // r1:0 = r1 or no overlap.
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000618 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000619 Hexagon::subreg_loreg))).addReg(SrcReg);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000620 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000621 Hexagon::subreg_hireg))).addImm(0);
622 }
623 return;
624 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000625 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000626 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000627 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000628 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000629 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000630 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
631 Hexagon::IntRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000632 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000633 addReg(SrcReg, getKillRegState(KillSrc));
634 return;
635 }
636 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
637 Hexagon::PredRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000638 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000639 addReg(SrcReg, getKillRegState(KillSrc));
640 return;
641 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000642 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
643 Hexagon::IntRegsRegClass.contains(DestReg)) {
644 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
645 addReg(SrcReg, getKillRegState(KillSrc));
646 return;
647 }
648 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
649 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
650 addReg(SrcReg, getKillRegState(KillSrc));
651 return;
652 }
653 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
654 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg).
655 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg),
656 getKillRegState(KillSrc)).
657 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg),
658 getKillRegState(KillSrc));
659 return;
660 }
661 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
662 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg).
663 addReg(SrcReg).
664 addReg(SrcReg, getKillRegState(KillSrc));
665 return;
666 }
667 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
668 Hexagon::VectorRegsRegClass.contains(DestReg)) {
669 llvm_unreachable("Unimplemented pred to vec");
670 return;
671 }
672 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
673 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
674 llvm_unreachable("Unimplemented vec to pred");
675 return;
676 }
677 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
678 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and),
679 HRI.getSubReg(DestReg, Hexagon::subreg_hireg)).
680 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg),
681 getKillRegState(KillSrc));
682 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and),
683 HRI.getSubReg(DestReg, Hexagon::subreg_loreg)).
684 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg),
685 getKillRegState(KillSrc));
686 return;
687 }
Sirish Pande30804c22012-02-15 18:52:27 +0000688
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000689#ifndef NDEBUG
690 // Show the invalid registers to ease debugging.
691 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
692 << ": " << PrintReg(DestReg, &HRI)
693 << " = " << PrintReg(SrcReg, &HRI) << '\n';
694#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000695 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000696}
697
698
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000699void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
700 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
701 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000702 DebugLoc DL = MBB.findDebugLoc(I);
703 MachineFunction &MF = *MBB.getParent();
704 MachineFrameInfo &MFI = *MF.getFrameInfo();
705 unsigned Align = MFI.getObjectAlignment(FI);
706
Alex Lorenze40c8a22015-08-11 23:09:45 +0000707 MachineMemOperand *MMO = MF.getMachineMemOperand(
708 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
709 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000710
Craig Topperc7242e02012-04-20 07:30:17 +0000711 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000712 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000713 .addFrameIndex(FI).addImm(0)
714 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000715 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000716 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000717 .addFrameIndex(FI).addImm(0)
718 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000719 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000720 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
721 .addFrameIndex(FI).addImm(0)
722 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
723 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000724 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000725 }
726}
727
728
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000729void HexagonInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
730 MachineBasicBlock::iterator I, unsigned DestReg, int FI,
731 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000732 DebugLoc DL = MBB.findDebugLoc(I);
733 MachineFunction &MF = *MBB.getParent();
734 MachineFrameInfo &MFI = *MF.getFrameInfo();
735 unsigned Align = MFI.getObjectAlignment(FI);
736
Alex Lorenze40c8a22015-08-11 23:09:45 +0000737 MachineMemOperand *MMO = MF.getMachineMemOperand(
738 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
739 MFI.getObjectSize(FI), Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000740 if (RC == &Hexagon::IntRegsRegClass) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000741 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000742 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000743 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000744 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000745 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000746 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000747 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
748 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
749 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000750 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000751 }
752}
753
754
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000755/// expandPostRAPseudo - This function is called for all pseudo instructions
756/// that remain after register allocation. Many pseudo instructions are
757/// created to help register allocation. This is the place to convert them
758/// into real instructions. The target can edit MI in place, or it can insert
759/// new instructions and erase MI. The function should return true if
760/// anything was changed.
761bool HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI)
762 const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000763 const HexagonRegisterInfo &HRI = getRegisterInfo();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000764 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000765 MachineBasicBlock &MBB = *MI->getParent();
766 DebugLoc DL = MI->getDebugLoc();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000767 unsigned Opc = MI->getOpcode();
768
769 switch (Opc) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000770 case Hexagon::ALIGNA:
771 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI->getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000772 .addReg(HRI.getFrameRegister())
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000773 .addImm(-MI->getOperand(1).getImm());
774 MBB.erase(MI);
775 return true;
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000776 case Hexagon::TFR_PdTrue: {
777 unsigned Reg = MI->getOperand(0).getReg();
778 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
779 .addReg(Reg, RegState::Undef)
780 .addReg(Reg, RegState::Undef);
781 MBB.erase(MI);
782 return true;
783 }
784 case Hexagon::TFR_PdFalse: {
785 unsigned Reg = MI->getOperand(0).getReg();
786 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
787 .addReg(Reg, RegState::Undef)
788 .addReg(Reg, RegState::Undef);
789 MBB.erase(MI);
790 return true;
791 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000792 case Hexagon::VMULW: {
793 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
794 unsigned DstReg = MI->getOperand(0).getReg();
795 unsigned Src1Reg = MI->getOperand(1).getReg();
796 unsigned Src2Reg = MI->getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000797 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
798 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
799 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
800 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000801 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000802 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000803 .addReg(Src2SubHi);
804 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000805 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000806 .addReg(Src2SubLo);
807 MBB.erase(MI);
808 MRI.clearKillFlags(Src1SubHi);
809 MRI.clearKillFlags(Src1SubLo);
810 MRI.clearKillFlags(Src2SubHi);
811 MRI.clearKillFlags(Src2SubLo);
812 return true;
813 }
814 case Hexagon::VMULW_ACC: {
815 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
816 unsigned DstReg = MI->getOperand(0).getReg();
817 unsigned Src1Reg = MI->getOperand(1).getReg();
818 unsigned Src2Reg = MI->getOperand(2).getReg();
819 unsigned Src3Reg = MI->getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000820 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
821 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
822 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
823 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
824 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
825 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000826 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000827 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000828 .addReg(Src2SubHi).addReg(Src3SubHi);
829 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000830 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000831 .addReg(Src2SubLo).addReg(Src3SubLo);
832 MBB.erase(MI);
833 MRI.clearKillFlags(Src1SubHi);
834 MRI.clearKillFlags(Src1SubLo);
835 MRI.clearKillFlags(Src2SubHi);
836 MRI.clearKillFlags(Src2SubLo);
837 MRI.clearKillFlags(Src3SubHi);
838 MRI.clearKillFlags(Src3SubLo);
839 return true;
840 }
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000841 case Hexagon::MUX64_rr: {
842 const MachineOperand &Op0 = MI->getOperand(0);
843 const MachineOperand &Op1 = MI->getOperand(1);
844 const MachineOperand &Op2 = MI->getOperand(2);
845 const MachineOperand &Op3 = MI->getOperand(3);
846 unsigned Rd = Op0.getReg();
847 unsigned Pu = Op1.getReg();
848 unsigned Rs = Op2.getReg();
849 unsigned Rt = Op3.getReg();
850 DebugLoc DL = MI->getDebugLoc();
851 unsigned K1 = getKillRegState(Op1.isKill());
852 unsigned K2 = getKillRegState(Op2.isKill());
853 unsigned K3 = getKillRegState(Op3.isKill());
854 if (Rd != Rs)
855 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
856 .addReg(Pu, (Rd == Rt) ? K1 : 0)
857 .addReg(Rs, K2);
858 if (Rd != Rt)
859 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
860 .addReg(Pu, K1)
861 .addReg(Rt, K3);
862 MBB.erase(MI);
863 return true;
864 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000865 case Hexagon::TCRETURNi:
866 MI->setDesc(get(Hexagon::J2_jump));
867 return true;
868 case Hexagon::TCRETURNr:
869 MI->setDesc(get(Hexagon::J2_jumpr));
870 return true;
871 }
872
873 return false;
874}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000875
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000876
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000877// We indicate that we want to reverse the branch by
878// inserting the reversed branching opcode.
879bool HexagonInstrInfo::ReverseBranchCondition(
880 SmallVectorImpl<MachineOperand> &Cond) const {
881 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000882 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000883 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
884 unsigned opcode = Cond[0].getImm();
885 //unsigned temp;
886 assert(get(opcode).isBranch() && "Should be a branching condition.");
887 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000888 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000889 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
890 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000891 return false;
892}
893
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000894
895void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
896 MachineBasicBlock::iterator MI) const {
897 DebugLoc DL;
898 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
899}
900
901
902// Returns true if an instruction is predicated irrespective of the predicate
903// sense. For example, all of the following will return true.
904// if (p0) R1 = add(R2, R3)
905// if (!p0) R1 = add(R2, R3)
906// if (p0.new) R1 = add(R2, R3)
907// if (!p0.new) R1 = add(R2, R3)
908// Note: New-value stores are not included here as in the current
909// implementation, we don't need to check their predicate sense.
910bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000911 const uint64_t F = MI->getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000912 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000913}
914
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000915
916bool HexagonInstrInfo::PredicateInstruction(MachineInstr *MI,
917 ArrayRef<MachineOperand> Cond) const {
918 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
919 isEndLoopN(Cond[0].getImm())) {
920 DEBUG(dbgs() << "\nCannot predicate:"; MI->dump(););
921 return false;
922 }
923 int Opc = MI->getOpcode();
924 assert (isPredicable(MI) && "Expected predicable instruction");
925 bool invertJump = predOpcodeHasNot(Cond);
926
927 // We have to predicate MI "in place", i.e. after this function returns,
928 // MI will need to be transformed into a predicated form. To avoid com-
929 // plicated manipulations with the operands (handling tied operands,
930 // etc.), build a new temporary instruction, then overwrite MI with it.
931
932 MachineBasicBlock &B = *MI->getParent();
933 DebugLoc DL = MI->getDebugLoc();
934 unsigned PredOpc = getCondOpcode(Opc, invertJump);
935 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
936 unsigned NOp = 0, NumOps = MI->getNumOperands();
937 while (NOp < NumOps) {
938 MachineOperand &Op = MI->getOperand(NOp);
939 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
940 break;
941 T.addOperand(Op);
942 NOp++;
943 }
944
945 unsigned PredReg, PredRegPos, PredRegFlags;
946 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
947 (void)GotPredReg;
948 assert(GotPredReg);
949 T.addReg(PredReg, PredRegFlags);
950 while (NOp < NumOps)
951 T.addOperand(MI->getOperand(NOp++));
952
953 MI->setDesc(get(PredOpc));
954 while (unsigned n = MI->getNumOperands())
955 MI->RemoveOperand(n-1);
956 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
957 MI->addOperand(T->getOperand(i));
958
959 MachineBasicBlock::instr_iterator TI = T->getIterator();
960 B.erase(TI);
961
962 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
963 MRI.clearKillFlags(PredReg);
964 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000965}
966
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000967
968bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
969 ArrayRef<MachineOperand> Pred2) const {
970 // TODO: Fix this
971 return false;
972}
973
974
975bool HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
976 std::vector<MachineOperand> &Pred) const {
977 auto &HRI = getRegisterInfo();
978 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
979 MachineOperand MO = MI->getOperand(oper);
980 if (MO.isReg() && MO.isDef()) {
981 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
982 if (RC == &Hexagon::PredRegsRegClass) {
983 Pred.push_back(MO);
984 return true;
985 }
986 }
987 }
988 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000989}
Andrew Trickd06df962012-02-01 22:13:57 +0000990
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000991bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
992 bool isPred = MI->getDesc().isPredicable();
993
994 if (!isPred)
995 return false;
996
997 const int Opc = MI->getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000998 int NumOperands = MI->getNumOperands();
999
1000 // Keep a flag for upto 4 operands in the instructions, to indicate if
1001 // that operand has been constant extended.
1002 bool OpCExtended[4];
1003 if (NumOperands > 4)
1004 NumOperands = 4;
1005
1006 for (int i = 0; i < NumOperands; i++)
1007 OpCExtended[i] = (isOperandExtended(MI, i) && isConstExtended(MI));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001008
1009 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001010 case Hexagon::A2_tfrsi:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001011 return (isOperandExtended(MI, 1) && isConstExtended(MI)) ||
1012 isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001013
Colin LeMahieubda31b42014-12-29 20:44:51 +00001014 case Hexagon::S2_storerd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001015 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001016
Colin LeMahieubda31b42014-12-29 20:44:51 +00001017 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001018 case Hexagon::S2_storerinew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001019 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001020
Colin LeMahieubda31b42014-12-29 20:44:51 +00001021 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001022 case Hexagon::S2_storerhnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001023 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001024
Colin LeMahieubda31b42014-12-29 20:44:51 +00001025 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001026 case Hexagon::S2_storerbnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001027 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001028
Colin LeMahieu947cd702014-12-23 20:44:59 +00001029 case Hexagon::L2_loadrd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001030 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001031
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001032 case Hexagon::L2_loadri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001033 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001034
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001035 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001036 case Hexagon::L2_loadruh_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001037 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001038
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001039 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001040 case Hexagon::L2_loadrub_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001041 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001042
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001043 case Hexagon::L2_loadrd_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001044 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001045
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001046 case Hexagon::L2_loadri_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001047 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001048
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001049 case Hexagon::L2_loadrh_pi:
1050 case Hexagon::L2_loadruh_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001051 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001052
Colin LeMahieu96976a12014-12-26 18:57:13 +00001053 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +00001054 case Hexagon::L2_loadrub_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001055 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001056
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001057 case Hexagon::S4_storeirb_io:
1058 case Hexagon::S4_storeirh_io:
1059 case Hexagon::S4_storeiri_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001060 return (OpCExtended[1] || isUInt<6>(MI->getOperand(1).getImm())) &&
1061 (OpCExtended[2] || isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001062
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001063 case Hexagon::A2_addi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +00001064 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001065
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001066 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001067 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001068 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001069 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001070 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001071 case Hexagon::A2_zxth:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001072 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001073 }
1074
1075 return true;
1076}
1077
Jyotsna Verma84c47102013-05-06 18:49:23 +00001078
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001079bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1080 const MachineBasicBlock *MBB, const MachineFunction &MF) const {
1081 // Debug info is never a scheduling boundary. It's necessary to be explicit
1082 // due to the special treatment of IT instructions below, otherwise a
1083 // dbg_value followed by an IT will result in the IT instruction being
1084 // considered a scheduling hazard, which is wrong. It should be the actual
1085 // instruction preceding the dbg_value instruction(s), just like it is
1086 // when debug info is not present.
1087 if (MI->isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001088 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001089
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001090 // Throwing call is a boundary.
1091 if (MI->isCall()) {
1092 // If any of the block's successors is a landing pad, this could be a
1093 // throwing call.
1094 for (auto I : MBB->successors())
1095 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001096 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001097 }
1098
1099 // Don't mess around with no return calls.
1100 if (MI->getOpcode() == Hexagon::CALLv3nr)
1101 return true;
1102
1103 // Terminators and labels can't be scheduled around.
1104 if (MI->getDesc().isTerminator() || MI->isPosition())
1105 return true;
1106
1107 if (MI->isInlineAsm() && !ScheduleInlineAsm)
1108 return true;
1109
1110 return false;
1111}
1112
1113
1114/// Measure the specified inline asm to determine an approximation of its
1115/// length.
1116/// Comments (which run till the next SeparatorString or newline) do not
1117/// count as an instruction.
1118/// Any other non-whitespace text is considered an instruction, with
1119/// multiple instructions separated by SeparatorString or newlines.
1120/// Variable-length instructions are not handled here; this function
1121/// may be overloaded in the target code to do that.
1122/// Hexagon counts the number of ##'s and adjust for that many
1123/// constant exenders.
1124unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1125 const MCAsmInfo &MAI) const {
1126 StringRef AStr(Str);
1127 // Count the number of instructions in the asm.
1128 bool atInsnStart = true;
1129 unsigned Length = 0;
1130 for (; *Str; ++Str) {
1131 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1132 strlen(MAI.getSeparatorString())) == 0)
1133 atInsnStart = true;
1134 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1135 Length += MAI.getMaxInstLength();
1136 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001137 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001138 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1139 strlen(MAI.getCommentString())) == 0)
1140 atInsnStart = false;
1141 }
1142
1143 // Add to size number of constant extenders seen * 4.
1144 StringRef Occ("##");
1145 Length += AStr.count(Occ)*4;
1146 return Length;
1147}
1148
1149
1150ScheduleHazardRecognizer*
1151HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1152 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
1153 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1154}
1155
1156
1157/// \brief For a comparison instruction, return the source registers in
1158/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1159/// compares against in CmpValue. Return true if the comparison instruction
1160/// can be analyzed.
1161bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
1162 unsigned &SrcReg, unsigned &SrcReg2, int &Mask, int &Value) const {
1163 unsigned Opc = MI->getOpcode();
1164
1165 // Set mask and the first source register.
1166 switch (Opc) {
1167 case Hexagon::C2_cmpeq:
1168 case Hexagon::C2_cmpeqp:
1169 case Hexagon::C2_cmpgt:
1170 case Hexagon::C2_cmpgtp:
1171 case Hexagon::C2_cmpgtu:
1172 case Hexagon::C2_cmpgtup:
1173 case Hexagon::C4_cmpneq:
1174 case Hexagon::C4_cmplte:
1175 case Hexagon::C4_cmplteu:
1176 case Hexagon::C2_cmpeqi:
1177 case Hexagon::C2_cmpgti:
1178 case Hexagon::C2_cmpgtui:
1179 case Hexagon::C4_cmpneqi:
1180 case Hexagon::C4_cmplteui:
1181 case Hexagon::C4_cmpltei:
1182 SrcReg = MI->getOperand(1).getReg();
1183 Mask = ~0;
1184 break;
1185 case Hexagon::A4_cmpbeq:
1186 case Hexagon::A4_cmpbgt:
1187 case Hexagon::A4_cmpbgtu:
1188 case Hexagon::A4_cmpbeqi:
1189 case Hexagon::A4_cmpbgti:
1190 case Hexagon::A4_cmpbgtui:
1191 SrcReg = MI->getOperand(1).getReg();
1192 Mask = 0xFF;
1193 break;
1194 case Hexagon::A4_cmpheq:
1195 case Hexagon::A4_cmphgt:
1196 case Hexagon::A4_cmphgtu:
1197 case Hexagon::A4_cmpheqi:
1198 case Hexagon::A4_cmphgti:
1199 case Hexagon::A4_cmphgtui:
1200 SrcReg = MI->getOperand(1).getReg();
1201 Mask = 0xFFFF;
1202 break;
1203 }
1204
1205 // Set the value/second source register.
1206 switch (Opc) {
1207 case Hexagon::C2_cmpeq:
1208 case Hexagon::C2_cmpeqp:
1209 case Hexagon::C2_cmpgt:
1210 case Hexagon::C2_cmpgtp:
1211 case Hexagon::C2_cmpgtu:
1212 case Hexagon::C2_cmpgtup:
1213 case Hexagon::A4_cmpbeq:
1214 case Hexagon::A4_cmpbgt:
1215 case Hexagon::A4_cmpbgtu:
1216 case Hexagon::A4_cmpheq:
1217 case Hexagon::A4_cmphgt:
1218 case Hexagon::A4_cmphgtu:
1219 case Hexagon::C4_cmpneq:
1220 case Hexagon::C4_cmplte:
1221 case Hexagon::C4_cmplteu:
1222 SrcReg2 = MI->getOperand(2).getReg();
1223 return true;
1224
1225 case Hexagon::C2_cmpeqi:
1226 case Hexagon::C2_cmpgtui:
1227 case Hexagon::C2_cmpgti:
1228 case Hexagon::C4_cmpneqi:
1229 case Hexagon::C4_cmplteui:
1230 case Hexagon::C4_cmpltei:
1231 case Hexagon::A4_cmpbeqi:
1232 case Hexagon::A4_cmpbgti:
1233 case Hexagon::A4_cmpbgtui:
1234 case Hexagon::A4_cmpheqi:
1235 case Hexagon::A4_cmphgti:
1236 case Hexagon::A4_cmphgtui:
1237 SrcReg2 = 0;
1238 Value = MI->getOperand(2).getImm();
1239 return true;
1240 }
1241
1242 return false;
1243}
1244
1245
1246unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1247 const MachineInstr *MI, unsigned *PredCost) const {
1248 return getInstrTimingClassLatency(ItinData, MI);
1249}
1250
1251
1252DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1253 const TargetSubtargetInfo &STI) const {
1254 const InstrItineraryData *II = STI.getInstrItineraryData();
1255 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1256}
1257
1258
1259// Inspired by this pair:
1260// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1261// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1262// Currently AA considers the addresses in these instructions to be aliasing.
1263bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr *MIa,
1264 MachineInstr *MIb, AliasAnalysis *AA) const {
1265 int OffsetA = 0, OffsetB = 0;
1266 unsigned SizeA = 0, SizeB = 0;
1267
1268 if (MIa->hasUnmodeledSideEffects() || MIb->hasUnmodeledSideEffects() ||
1269 MIa->hasOrderedMemoryRef() || MIa->hasOrderedMemoryRef())
1270 return false;
1271
1272 // Instructions that are pure loads, not loads and stores like memops are not
1273 // dependent.
1274 if (MIa->mayLoad() && !isMemOp(MIa) && MIb->mayLoad() && !isMemOp(MIb))
1275 return true;
1276
1277 // Get base, offset, and access size in MIa.
1278 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
1279 if (!BaseRegA || !SizeA)
1280 return false;
1281
1282 // Get base, offset, and access size in MIb.
1283 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
1284 if (!BaseRegB || !SizeB)
1285 return false;
1286
1287 if (BaseRegA != BaseRegB)
1288 return false;
1289
1290 // This is a mem access with the same base register and known offsets from it.
1291 // Reason about it.
1292 if (OffsetA > OffsetB) {
1293 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1294 return (SizeB <= offDiff);
1295 } else if (OffsetA < OffsetB) {
1296 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1297 return (SizeA <= offDiff);
1298 }
1299
1300 return false;
1301}
1302
1303
1304unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
1305 MachineRegisterInfo &MRI = MF->getRegInfo();
1306 const TargetRegisterClass *TRC;
1307 if (VT == MVT::i1) {
1308 TRC = &Hexagon::PredRegsRegClass;
1309 } else if (VT == MVT::i32 || VT == MVT::f32) {
1310 TRC = &Hexagon::IntRegsRegClass;
1311 } else if (VT == MVT::i64 || VT == MVT::f64) {
1312 TRC = &Hexagon::DoubleRegsRegClass;
1313 } else {
1314 llvm_unreachable("Cannot handle this register class");
1315 }
1316
1317 unsigned NewReg = MRI.createVirtualRegister(TRC);
1318 return NewReg;
1319}
1320
1321
1322bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr* MI) const {
1323 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1324}
1325
1326
1327bool HexagonInstrInfo::isAccumulator(const MachineInstr *MI) const {
1328 const uint64_t F = MI->getDesc().TSFlags;
1329 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1330}
1331
1332
1333bool HexagonInstrInfo::isComplex(const MachineInstr *MI) const {
1334 const MachineFunction *MF = MI->getParent()->getParent();
1335 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1336 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1337
1338 if (!(isTC1(MI))
1339 && !(QII->isTC2Early(MI))
1340 && !(MI->getDesc().mayLoad())
1341 && !(MI->getDesc().mayStore())
1342 && (MI->getDesc().getOpcode() != Hexagon::S2_allocframe)
1343 && (MI->getDesc().getOpcode() != Hexagon::L2_deallocframe)
1344 && !(QII->isMemOp(MI))
1345 && !(MI->isBranch())
1346 && !(MI->isReturn())
1347 && !MI->isCall())
1348 return true;
1349
1350 return false;
1351}
1352
1353
1354// Return true if the the instruction is a compund branch instruction.
1355bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr *MI) const {
1356 return (getType(MI) == HexagonII::TypeCOMPOUND && MI->isBranch());
1357}
1358
1359
1360bool HexagonInstrInfo::isCondInst(const MachineInstr *MI) const {
1361 return (MI->isBranch() && isPredicated(MI)) ||
1362 isConditionalTransfer(MI) ||
1363 isConditionalALU32(MI) ||
1364 isConditionalLoad(MI) ||
1365 // Predicated stores which don't have a .new on any operands.
1366 (MI->mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1367 !isPredicatedNew(MI));
1368}
1369
1370
1371bool HexagonInstrInfo::isConditionalALU32(const MachineInstr* MI) const {
1372 switch (MI->getOpcode()) {
1373 case Hexagon::A2_paddf:
1374 case Hexagon::A2_paddfnew:
1375 case Hexagon::A2_paddif:
1376 case Hexagon::A2_paddifnew:
1377 case Hexagon::A2_paddit:
1378 case Hexagon::A2_padditnew:
1379 case Hexagon::A2_paddt:
1380 case Hexagon::A2_paddtnew:
1381 case Hexagon::A2_pandf:
1382 case Hexagon::A2_pandfnew:
1383 case Hexagon::A2_pandt:
1384 case Hexagon::A2_pandtnew:
1385 case Hexagon::A2_porf:
1386 case Hexagon::A2_porfnew:
1387 case Hexagon::A2_port:
1388 case Hexagon::A2_portnew:
1389 case Hexagon::A2_psubf:
1390 case Hexagon::A2_psubfnew:
1391 case Hexagon::A2_psubt:
1392 case Hexagon::A2_psubtnew:
1393 case Hexagon::A2_pxorf:
1394 case Hexagon::A2_pxorfnew:
1395 case Hexagon::A2_pxort:
1396 case Hexagon::A2_pxortnew:
1397 case Hexagon::A4_paslhf:
1398 case Hexagon::A4_paslhfnew:
1399 case Hexagon::A4_paslht:
1400 case Hexagon::A4_paslhtnew:
1401 case Hexagon::A4_pasrhf:
1402 case Hexagon::A4_pasrhfnew:
1403 case Hexagon::A4_pasrht:
1404 case Hexagon::A4_pasrhtnew:
1405 case Hexagon::A4_psxtbf:
1406 case Hexagon::A4_psxtbfnew:
1407 case Hexagon::A4_psxtbt:
1408 case Hexagon::A4_psxtbtnew:
1409 case Hexagon::A4_psxthf:
1410 case Hexagon::A4_psxthfnew:
1411 case Hexagon::A4_psxtht:
1412 case Hexagon::A4_psxthtnew:
1413 case Hexagon::A4_pzxtbf:
1414 case Hexagon::A4_pzxtbfnew:
1415 case Hexagon::A4_pzxtbt:
1416 case Hexagon::A4_pzxtbtnew:
1417 case Hexagon::A4_pzxthf:
1418 case Hexagon::A4_pzxthfnew:
1419 case Hexagon::A4_pzxtht:
1420 case Hexagon::A4_pzxthtnew:
1421 case Hexagon::C2_ccombinewf:
1422 case Hexagon::C2_ccombinewt:
1423 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001424 }
1425 return false;
1426}
1427
1428
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001429// FIXME - Function name and it's functionality don't match.
1430// It should be renamed to hasPredNewOpcode()
1431bool HexagonInstrInfo::isConditionalLoad(const MachineInstr* MI) const {
1432 if (!MI->getDesc().mayLoad() || !isPredicated(MI))
1433 return false;
1434
1435 int PNewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1436 // Instruction with valid predicated-new opcode can be promoted to .new.
1437 return PNewOpcode >= 0;
1438}
1439
1440
1441// Returns true if an instruction is a conditional store.
1442//
1443// Note: It doesn't include conditional new-value stores as they can't be
1444// converted to .new predicate.
1445bool HexagonInstrInfo::isConditionalStore(const MachineInstr* MI) const {
1446 switch (MI->getOpcode()) {
1447 default: return false;
1448 case Hexagon::S4_storeirbt_io:
1449 case Hexagon::S4_storeirbf_io:
1450 case Hexagon::S4_pstorerbt_rr:
1451 case Hexagon::S4_pstorerbf_rr:
1452 case Hexagon::S2_pstorerbt_io:
1453 case Hexagon::S2_pstorerbf_io:
1454 case Hexagon::S2_pstorerbt_pi:
1455 case Hexagon::S2_pstorerbf_pi:
1456 case Hexagon::S2_pstorerdt_io:
1457 case Hexagon::S2_pstorerdf_io:
1458 case Hexagon::S4_pstorerdt_rr:
1459 case Hexagon::S4_pstorerdf_rr:
1460 case Hexagon::S2_pstorerdt_pi:
1461 case Hexagon::S2_pstorerdf_pi:
1462 case Hexagon::S2_pstorerht_io:
1463 case Hexagon::S2_pstorerhf_io:
1464 case Hexagon::S4_storeirht_io:
1465 case Hexagon::S4_storeirhf_io:
1466 case Hexagon::S4_pstorerht_rr:
1467 case Hexagon::S4_pstorerhf_rr:
1468 case Hexagon::S2_pstorerht_pi:
1469 case Hexagon::S2_pstorerhf_pi:
1470 case Hexagon::S2_pstorerit_io:
1471 case Hexagon::S2_pstorerif_io:
1472 case Hexagon::S4_storeirit_io:
1473 case Hexagon::S4_storeirif_io:
1474 case Hexagon::S4_pstorerit_rr:
1475 case Hexagon::S4_pstorerif_rr:
1476 case Hexagon::S2_pstorerit_pi:
1477 case Hexagon::S2_pstorerif_pi:
1478
1479 // V4 global address store before promoting to dot new.
1480 case Hexagon::S4_pstorerdt_abs:
1481 case Hexagon::S4_pstorerdf_abs:
1482 case Hexagon::S4_pstorerbt_abs:
1483 case Hexagon::S4_pstorerbf_abs:
1484 case Hexagon::S4_pstorerht_abs:
1485 case Hexagon::S4_pstorerhf_abs:
1486 case Hexagon::S4_pstorerit_abs:
1487 case Hexagon::S4_pstorerif_abs:
1488 return true;
1489
1490 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1491 // from the "Conditional Store" list. Because a predicated new value store
1492 // would NOT be promoted to a double dot new store.
1493 // This function returns yes for those stores that are predicated but not
1494 // yet promoted to predicate dot new instructions.
1495 }
1496}
1497
1498
1499bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr *MI) const {
1500 switch (MI->getOpcode()) {
1501 case Hexagon::A2_tfrt:
1502 case Hexagon::A2_tfrf:
1503 case Hexagon::C2_cmoveit:
1504 case Hexagon::C2_cmoveif:
1505 case Hexagon::A2_tfrtnew:
1506 case Hexagon::A2_tfrfnew:
1507 case Hexagon::C2_cmovenewit:
1508 case Hexagon::C2_cmovenewif:
1509 case Hexagon::A2_tfrpt:
1510 case Hexagon::A2_tfrpf:
1511 return true;
1512
1513 default:
1514 return false;
1515 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001516 return false;
1517}
1518
1519
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001520// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1521// isFPImm and later getFPImm as well.
1522bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
1523 const uint64_t F = MI->getDesc().TSFlags;
1524 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1525 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001526 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001527
1528 unsigned isExtendable =
1529 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1530 if (!isExtendable)
1531 return false;
1532
1533 if (MI->isCall())
1534 return false;
1535
1536 short ExtOpNum = getCExtOpNum(MI);
1537 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1538 // Use MO operand flags to determine if MO
1539 // has the HMOTF_ConstExtended flag set.
1540 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001541 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001542 // If this is a Machine BB address we are talking about, and it is
1543 // not marked as extended, say so.
1544 if (MO.isMBB())
1545 return false;
1546
1547 // We could be using an instruction with an extendable immediate and shoehorn
1548 // a global address into it. If it is a global address it will be constant
1549 // extended. We do this for COMBINE.
1550 // We currently only handle isGlobal() because it is the only kind of
1551 // object we are going to end up with here for now.
1552 // In the future we probably should add isSymbol(), etc.
1553 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1554 MO.isJTI() || MO.isCPI())
1555 return true;
1556
1557 // If the extendable operand is not 'Immediate' type, the instruction should
1558 // have 'isExtended' flag set.
1559 assert(MO.isImm() && "Extendable operand must be Immediate type");
1560
1561 int MinValue = getMinValue(MI);
1562 int MaxValue = getMaxValue(MI);
1563 int ImmValue = MO.getImm();
1564
1565 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001566}
1567
1568
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001569bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1570 switch (MI->getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001571 case Hexagon::L4_return :
1572 case Hexagon::L4_return_t :
1573 case Hexagon::L4_return_f :
1574 case Hexagon::L4_return_tnew_pnt :
1575 case Hexagon::L4_return_fnew_pnt :
1576 case Hexagon::L4_return_tnew_pt :
1577 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001578 return true;
1579 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001580 return false;
1581}
1582
1583
1584// Return true when ConsMI uses a register defined by ProdMI.
1585bool HexagonInstrInfo::isDependent(const MachineInstr *ProdMI,
1586 const MachineInstr *ConsMI) const {
1587 const MCInstrDesc &ProdMCID = ProdMI->getDesc();
1588 if (!ProdMCID.getNumDefs())
1589 return false;
1590
1591 auto &HRI = getRegisterInfo();
1592
1593 SmallVector<unsigned, 4> DefsA;
1594 SmallVector<unsigned, 4> DefsB;
1595 SmallVector<unsigned, 8> UsesA;
1596 SmallVector<unsigned, 8> UsesB;
1597
1598 parseOperands(ProdMI, DefsA, UsesA);
1599 parseOperands(ConsMI, DefsB, UsesB);
1600
1601 for (auto &RegA : DefsA)
1602 for (auto &RegB : UsesB) {
1603 // True data dependency.
1604 if (RegA == RegB)
1605 return true;
1606
1607 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1608 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1609 if (RegB == *SubRegs)
1610 return true;
1611
1612 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1613 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1614 if (RegA == *SubRegs)
1615 return true;
1616 }
1617
1618 return false;
1619}
1620
1621
1622// Returns true if the instruction is alread a .cur.
1623bool HexagonInstrInfo::isDotCurInst(const MachineInstr* MI) const {
1624 switch (MI->getOpcode()) {
1625 case Hexagon::V6_vL32b_cur_pi:
1626 case Hexagon::V6_vL32b_cur_ai:
1627 case Hexagon::V6_vL32b_cur_pi_128B:
1628 case Hexagon::V6_vL32b_cur_ai_128B:
1629 return true;
1630 }
1631 return false;
1632}
1633
1634
1635// Returns true, if any one of the operands is a dot new
1636// insn, whether it is predicated dot new or register dot new.
1637bool HexagonInstrInfo::isDotNewInst(const MachineInstr* MI) const {
1638 if (isNewValueInst(MI) ||
1639 (isPredicated(MI) && isPredicatedNew(MI)))
1640 return true;
1641
1642 return false;
1643}
1644
1645
1646/// Symmetrical. See if these two instructions are fit for duplex pair.
1647bool HexagonInstrInfo::isDuplexPair(const MachineInstr *MIa,
1648 const MachineInstr *MIb) const {
1649 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1650 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1651 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1652}
1653
1654
1655bool HexagonInstrInfo::isEarlySourceInstr(MachineInstr *MI) const {
1656 if (!MI)
1657 return false;
1658
1659 if (MI->mayLoad() || MI->mayStore() || MI->isCompare())
1660 return true;
1661
1662 // Multiply
1663 unsigned SchedClass = MI->getDesc().getSchedClass();
1664 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
1665 return true;
1666 return false;
1667}
1668
1669
1670bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1671 return (Opcode == Hexagon::ENDLOOP0 ||
1672 Opcode == Hexagon::ENDLOOP1);
1673}
1674
1675
1676bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1677 switch(OpType) {
1678 case MachineOperand::MO_MachineBasicBlock:
1679 case MachineOperand::MO_GlobalAddress:
1680 case MachineOperand::MO_ExternalSymbol:
1681 case MachineOperand::MO_JumpTableIndex:
1682 case MachineOperand::MO_ConstantPoolIndex:
1683 case MachineOperand::MO_BlockAddress:
1684 return true;
1685 default:
1686 return false;
1687 }
1688}
1689
1690
1691bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
1692 const MCInstrDesc &MID = MI->getDesc();
1693 const uint64_t F = MID.TSFlags;
1694 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1695 return true;
1696
1697 // TODO: This is largely obsolete now. Will need to be removed
1698 // in consecutive patches.
1699 switch(MI->getOpcode()) {
1700 // TFR_FI Remains a special case.
1701 case Hexagon::TFR_FI:
1702 return true;
1703 default:
1704 return false;
1705 }
1706 return false;
1707}
1708
1709
1710// This returns true in two cases:
1711// - The OP code itself indicates that this is an extended instruction.
1712// - One of MOs has been marked with HMOTF_ConstExtended flag.
1713bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
1714 // First check if this is permanently extended op code.
1715 const uint64_t F = MI->getDesc().TSFlags;
1716 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
1717 return true;
1718 // Use MO operand flags to determine if one of MI's operands
1719 // has HMOTF_ConstExtended flag set.
1720 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
1721 E = MI->operands_end(); I != E; ++I) {
1722 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1723 return true;
1724 }
1725 return false;
1726}
1727
1728
1729bool HexagonInstrInfo::isFloat(MachineInstr *MI) const {
1730 unsigned Opcode = MI->getOpcode();
1731 const uint64_t F = get(Opcode).TSFlags;
1732 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
1733}
1734
1735
1736bool HexagonInstrInfo::isIndirectCall(const MachineInstr *MI) const {
1737 switch (MI->getOpcode()) {
1738 case Hexagon::J2_callr :
1739 case Hexagon::J2_callrf :
1740 case Hexagon::J2_callrt :
1741 return true;
1742 }
1743 return false;
1744}
1745
1746
1747bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr *MI) const {
1748 switch (MI->getOpcode()) {
1749 case Hexagon::L4_return :
1750 case Hexagon::L4_return_t :
1751 case Hexagon::L4_return_f :
1752 case Hexagon::L4_return_fnew_pnt :
1753 case Hexagon::L4_return_fnew_pt :
1754 case Hexagon::L4_return_tnew_pnt :
1755 case Hexagon::L4_return_tnew_pt :
1756 return true;
1757 }
1758 return false;
1759}
1760
1761
1762bool HexagonInstrInfo::isJumpR(const MachineInstr *MI) const {
1763 switch (MI->getOpcode()) {
1764 case Hexagon::J2_jumpr :
1765 case Hexagon::J2_jumprt :
1766 case Hexagon::J2_jumprf :
1767 case Hexagon::J2_jumprtnewpt :
1768 case Hexagon::J2_jumprfnewpt :
1769 case Hexagon::J2_jumprtnew :
1770 case Hexagon::J2_jumprfnew :
1771 return true;
1772 }
1773 return false;
1774}
1775
1776
1777// Return true if a given MI can accomodate given offset.
1778// Use abs estimate as oppose to the exact number.
1779// TODO: This will need to be changed to use MC level
1780// definition of instruction extendable field size.
1781bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr *MI,
1782 unsigned offset) const {
1783 // This selection of jump instructions matches to that what
1784 // AnalyzeBranch can parse, plus NVJ.
1785 if (isNewValueJump(MI)) // r9:2
1786 return isInt<11>(offset);
1787
1788 switch (MI->getOpcode()) {
1789 // Still missing Jump to address condition on register value.
1790 default:
1791 return false;
1792 case Hexagon::J2_jump: // bits<24> dst; // r22:2
1793 case Hexagon::J2_call:
1794 case Hexagon::CALLv3nr:
1795 return isInt<24>(offset);
1796 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
1797 case Hexagon::J2_jumpf:
1798 case Hexagon::J2_jumptnew:
1799 case Hexagon::J2_jumptnewpt:
1800 case Hexagon::J2_jumpfnew:
1801 case Hexagon::J2_jumpfnewpt:
1802 case Hexagon::J2_callt:
1803 case Hexagon::J2_callf:
1804 return isInt<17>(offset);
1805 case Hexagon::J2_loop0i:
1806 case Hexagon::J2_loop0iext:
1807 case Hexagon::J2_loop0r:
1808 case Hexagon::J2_loop0rext:
1809 case Hexagon::J2_loop1i:
1810 case Hexagon::J2_loop1iext:
1811 case Hexagon::J2_loop1r:
1812 case Hexagon::J2_loop1rext:
1813 return isInt<9>(offset);
1814 // TODO: Add all the compound branches here. Can we do this in Relation model?
1815 case Hexagon::J4_cmpeqi_tp0_jump_nt:
1816 case Hexagon::J4_cmpeqi_tp1_jump_nt:
1817 return isInt<11>(offset);
1818 }
1819}
1820
1821
1822bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(MachineInstr *LRMI,
1823 MachineInstr *ESMI) const {
1824 if (!LRMI || !ESMI)
1825 return false;
1826
1827 bool isLate = isLateResultInstr(LRMI);
1828 bool isEarly = isEarlySourceInstr(ESMI);
1829
1830 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
1831 DEBUG(LRMI->dump());
1832 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
1833 DEBUG(ESMI->dump());
1834
1835 if (isLate && isEarly) {
1836 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
1837 return true;
1838 }
1839
1840 return false;
1841}
1842
1843
1844bool HexagonInstrInfo::isLateResultInstr(MachineInstr *MI) const {
1845 if (!MI)
1846 return false;
1847
1848 switch (MI->getOpcode()) {
1849 case TargetOpcode::EXTRACT_SUBREG:
1850 case TargetOpcode::INSERT_SUBREG:
1851 case TargetOpcode::SUBREG_TO_REG:
1852 case TargetOpcode::REG_SEQUENCE:
1853 case TargetOpcode::IMPLICIT_DEF:
1854 case TargetOpcode::COPY:
1855 case TargetOpcode::INLINEASM:
1856 case TargetOpcode::PHI:
1857 return false;
1858 default:
1859 break;
1860 }
1861
1862 unsigned SchedClass = MI->getDesc().getSchedClass();
1863
1864 switch (SchedClass) {
1865 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
1866 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
1867 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
1868 case Hexagon::Sched::ALU64_tc_1_SLOT23:
1869 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
1870 case Hexagon::Sched::S_2op_tc_1_SLOT23:
1871 case Hexagon::Sched::S_3op_tc_1_SLOT23:
1872 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
1873 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
1874 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
1875 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
1876 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
1877 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
1878 return false;
1879 }
1880 return true;
1881}
1882
1883
1884bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr *MI) const {
1885 if (!MI)
1886 return false;
1887
1888 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
1889 // resource, but all operands can be received late like an ALU instruction.
1890 return MI->getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
1891}
1892
1893
1894bool HexagonInstrInfo::isLoopN(unsigned Opcode) const {
1895 return (Opcode == Hexagon::J2_loop0i ||
1896 Opcode == Hexagon::J2_loop0r ||
1897 Opcode == Hexagon::J2_loop0iext ||
1898 Opcode == Hexagon::J2_loop0rext ||
1899 Opcode == Hexagon::J2_loop1i ||
1900 Opcode == Hexagon::J2_loop1r ||
1901 Opcode == Hexagon::J2_loop1iext ||
1902 Opcode == Hexagon::J2_loop1rext);
1903}
1904
1905
1906bool HexagonInstrInfo::isMemOp(const MachineInstr *MI) const {
1907 switch (MI->getOpcode()) {
1908 default: return false;
1909 case Hexagon::L4_iadd_memopw_io :
1910 case Hexagon::L4_isub_memopw_io :
1911 case Hexagon::L4_add_memopw_io :
1912 case Hexagon::L4_sub_memopw_io :
1913 case Hexagon::L4_and_memopw_io :
1914 case Hexagon::L4_or_memopw_io :
1915 case Hexagon::L4_iadd_memoph_io :
1916 case Hexagon::L4_isub_memoph_io :
1917 case Hexagon::L4_add_memoph_io :
1918 case Hexagon::L4_sub_memoph_io :
1919 case Hexagon::L4_and_memoph_io :
1920 case Hexagon::L4_or_memoph_io :
1921 case Hexagon::L4_iadd_memopb_io :
1922 case Hexagon::L4_isub_memopb_io :
1923 case Hexagon::L4_add_memopb_io :
1924 case Hexagon::L4_sub_memopb_io :
1925 case Hexagon::L4_and_memopb_io :
1926 case Hexagon::L4_or_memopb_io :
1927 case Hexagon::L4_ior_memopb_io:
1928 case Hexagon::L4_ior_memoph_io:
1929 case Hexagon::L4_ior_memopw_io:
1930 case Hexagon::L4_iand_memopb_io:
1931 case Hexagon::L4_iand_memoph_io:
1932 case Hexagon::L4_iand_memopw_io:
1933 return true;
1934 }
1935 return false;
1936}
1937
1938
1939bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1940 const uint64_t F = MI->getDesc().TSFlags;
1941 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
1942}
1943
1944
1945bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
1946 const uint64_t F = get(Opcode).TSFlags;
1947 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
1948}
1949
1950
1951bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
1952 return isNewValueJump(MI) || isNewValueStore(MI);
1953}
1954
1955
1956bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1957 return isNewValue(MI) && MI->isBranch();
1958}
1959
1960
1961bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
1962 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
1963}
1964
1965
1966bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
1967 const uint64_t F = MI->getDesc().TSFlags;
1968 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
1969}
1970
1971
1972bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
1973 const uint64_t F = get(Opcode).TSFlags;
1974 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
1975}
1976
1977
1978// Returns true if a particular operand is extendable for an instruction.
1979bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1980 unsigned OperandNum) const {
1981 const uint64_t F = MI->getDesc().TSFlags;
1982 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1983 == OperandNum;
1984}
1985
1986
1987bool HexagonInstrInfo::isPostIncrement(const MachineInstr* MI) const {
1988 return getAddrMode(MI) == HexagonII::PostInc;
1989}
1990
1991
1992bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1993 const uint64_t F = MI->getDesc().TSFlags;
1994 assert(isPredicated(MI));
1995 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
1996}
1997
1998
1999bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2000 const uint64_t F = get(Opcode).TSFlags;
2001 assert(isPredicated(Opcode));
2002 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2003}
2004
2005
2006bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
2007 const uint64_t F = MI->getDesc().TSFlags;
2008 return !((F >> HexagonII::PredicatedFalsePos) &
2009 HexagonII::PredicatedFalseMask);
2010}
2011
2012
2013bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2014 const uint64_t F = get(Opcode).TSFlags;
2015 // Make sure that the instruction is predicated.
2016 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2017 return !((F >> HexagonII::PredicatedFalsePos) &
2018 HexagonII::PredicatedFalseMask);
2019}
2020
2021
2022bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2023 const uint64_t F = get(Opcode).TSFlags;
2024 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2025}
2026
2027
2028bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2029 const uint64_t F = get(Opcode).TSFlags;
2030 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2031}
2032
2033
2034bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2035 const uint64_t F = get(Opcode).TSFlags;
2036 assert(get(Opcode).isBranch() &&
2037 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2038 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2039}
2040
2041
2042bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
2043 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2044 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT;
2045}
2046
2047
2048bool HexagonInstrInfo::isSolo(const MachineInstr* MI) const {
2049 const uint64_t F = MI->getDesc().TSFlags;
2050 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2051}
2052
2053
2054bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr *MI) const {
2055 switch (MI->getOpcode()) {
2056 case Hexagon::STriw_pred :
2057 case Hexagon::LDriw_pred :
2058 return true;
2059 default:
2060 return false;
2061 }
2062}
2063
2064
2065// Returns true when SU has a timing class TC1.
2066bool HexagonInstrInfo::isTC1(const MachineInstr *MI) const {
2067 unsigned SchedClass = MI->getDesc().getSchedClass();
2068 switch (SchedClass) {
2069 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2070 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2071 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2072 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2073 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2074 //case Hexagon::Sched::M_tc_1_SLOT23:
2075 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2076 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2077 return true;
2078
2079 default:
2080 return false;
2081 }
2082}
2083
2084
2085bool HexagonInstrInfo::isTC2(const MachineInstr *MI) const {
2086 unsigned SchedClass = MI->getDesc().getSchedClass();
2087 switch (SchedClass) {
2088 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2089 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2090 case Hexagon::Sched::CR_tc_2_SLOT3:
2091 case Hexagon::Sched::M_tc_2_SLOT23:
2092 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2093 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2094 return true;
2095
2096 default:
2097 return false;
2098 }
2099}
2100
2101
2102bool HexagonInstrInfo::isTC2Early(const MachineInstr *MI) const {
2103 unsigned SchedClass = MI->getDesc().getSchedClass();
2104 switch (SchedClass) {
2105 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2106 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2107 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2108 case Hexagon::Sched::CR_tc_2early_SLOT23:
2109 case Hexagon::Sched::CR_tc_2early_SLOT3:
2110 case Hexagon::Sched::J_tc_2early_SLOT0123:
2111 case Hexagon::Sched::J_tc_2early_SLOT2:
2112 case Hexagon::Sched::J_tc_2early_SLOT23:
2113 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2114 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2115 return true;
2116
2117 default:
2118 return false;
2119 }
2120}
2121
2122
2123bool HexagonInstrInfo::isTC4x(const MachineInstr *MI) const {
2124 if (!MI)
2125 return false;
2126
2127 unsigned SchedClass = MI->getDesc().getSchedClass();
2128 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2129}
2130
2131
2132bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr *MI) const {
2133 if (!MI)
2134 return false;
2135
2136 const uint64_t V = getType(MI);
2137 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2138}
2139
2140
2141// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2142//
2143bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2144 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2145 VT == MVT::v32i16 || VT == MVT::v64i8) {
2146 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2147 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2148 (Offset & 0x3f) == 0);
2149 }
2150 // 128B
2151 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2152 VT == MVT::v64i16 || VT == MVT::v128i8) {
2153 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2154 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2155 (Offset & 0x7f) == 0);
2156 }
2157 if (VT == MVT::i64) {
2158 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2159 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2160 (Offset & 0x7) == 0);
2161 }
2162 if (VT == MVT::i32) {
2163 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2164 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2165 (Offset & 0x3) == 0);
2166 }
2167 if (VT == MVT::i16) {
2168 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2169 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2170 (Offset & 0x1) == 0);
2171 }
2172 if (VT == MVT::i8) {
2173 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2174 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2175 }
2176 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002177}
2178
2179
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002180bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2181 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002182 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002183 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002184 // inserted to calculate the final address. Due to this reason, the function
2185 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002186 // We used to assert if the offset was not properly aligned, however,
2187 // there are cases where a misaligned pointer recast can cause this
2188 // problem, and we need to allow for it. The front end warns of such
2189 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002190
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002191 switch (Opcode) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002192 case Hexagon::STriq_pred_V6:
2193 case Hexagon::STriq_pred_vec_V6:
2194 case Hexagon::STriv_pseudo_V6:
2195 case Hexagon::STrivv_pseudo_V6:
2196 case Hexagon::LDriq_pred_V6:
2197 case Hexagon::LDriq_pred_vec_V6:
2198 case Hexagon::LDriv_pseudo_V6:
2199 case Hexagon::LDrivv_pseudo_V6:
2200 case Hexagon::LDrivv_indexed:
2201 case Hexagon::STrivv_indexed:
2202 case Hexagon::V6_vL32b_ai:
2203 case Hexagon::V6_vS32b_ai:
2204 case Hexagon::V6_vL32Ub_ai:
2205 case Hexagon::V6_vS32Ub_ai:
2206 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2207 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2208
2209 case Hexagon::STriq_pred_V6_128B:
2210 case Hexagon::STriq_pred_vec_V6_128B:
2211 case Hexagon::STriv_pseudo_V6_128B:
2212 case Hexagon::STrivv_pseudo_V6_128B:
2213 case Hexagon::LDriq_pred_V6_128B:
2214 case Hexagon::LDriq_pred_vec_V6_128B:
2215 case Hexagon::LDriv_pseudo_V6_128B:
2216 case Hexagon::LDrivv_pseudo_V6_128B:
2217 case Hexagon::LDrivv_indexed_128B:
2218 case Hexagon::STrivv_indexed_128B:
2219 case Hexagon::V6_vL32b_ai_128B:
2220 case Hexagon::V6_vS32b_ai_128B:
2221 case Hexagon::V6_vL32Ub_ai_128B:
2222 case Hexagon::V6_vS32Ub_ai_128B:
2223 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2224 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2225
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002226 case Hexagon::J2_loop0i:
2227 case Hexagon::J2_loop1i:
2228 return isUInt<10>(Offset);
2229 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002230
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002231 if (Extend)
2232 return true;
2233
2234 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002235 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002236 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002237 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2238 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2239
Colin LeMahieu947cd702014-12-23 20:44:59 +00002240 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002241 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002242 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2243 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2244
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002245 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002246 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002247 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002248 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2249 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2250
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002251 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002252 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002253 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002254 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2255 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2256
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002257 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002258 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2259 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2260
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002261 case Hexagon::L4_iadd_memopw_io :
2262 case Hexagon::L4_isub_memopw_io :
2263 case Hexagon::L4_add_memopw_io :
2264 case Hexagon::L4_sub_memopw_io :
2265 case Hexagon::L4_and_memopw_io :
2266 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002267 return (0 <= Offset && Offset <= 255);
2268
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002269 case Hexagon::L4_iadd_memoph_io :
2270 case Hexagon::L4_isub_memoph_io :
2271 case Hexagon::L4_add_memoph_io :
2272 case Hexagon::L4_sub_memoph_io :
2273 case Hexagon::L4_and_memoph_io :
2274 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002275 return (0 <= Offset && Offset <= 127);
2276
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002277 case Hexagon::L4_iadd_memopb_io :
2278 case Hexagon::L4_isub_memopb_io :
2279 case Hexagon::L4_add_memopb_io :
2280 case Hexagon::L4_sub_memopb_io :
2281 case Hexagon::L4_and_memopb_io :
2282 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002283 return (0 <= Offset && Offset <= 63);
2284
2285 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
2286 // any size. Later pass knows how to handle it.
2287 case Hexagon::STriw_pred:
2288 case Hexagon::LDriw_pred:
2289 return true;
2290
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002291 case Hexagon::TFR_FI:
2292 case Hexagon::TFR_FIA:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002293 case Hexagon::INLINEASM:
2294 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002295
2296 case Hexagon::L2_ploadrbt_io:
2297 case Hexagon::L2_ploadrbf_io:
2298 case Hexagon::L2_ploadrubt_io:
2299 case Hexagon::L2_ploadrubf_io:
2300 case Hexagon::S2_pstorerbt_io:
2301 case Hexagon::S2_pstorerbf_io:
2302 case Hexagon::S4_storeirb_io:
2303 case Hexagon::S4_storeirbt_io:
2304 case Hexagon::S4_storeirbf_io:
2305 return isUInt<6>(Offset);
2306
2307 case Hexagon::L2_ploadrht_io:
2308 case Hexagon::L2_ploadrhf_io:
2309 case Hexagon::L2_ploadruht_io:
2310 case Hexagon::L2_ploadruhf_io:
2311 case Hexagon::S2_pstorerht_io:
2312 case Hexagon::S2_pstorerhf_io:
2313 case Hexagon::S4_storeirh_io:
2314 case Hexagon::S4_storeirht_io:
2315 case Hexagon::S4_storeirhf_io:
2316 return isShiftedUInt<6,1>(Offset);
2317
2318 case Hexagon::L2_ploadrit_io:
2319 case Hexagon::L2_ploadrif_io:
2320 case Hexagon::S2_pstorerit_io:
2321 case Hexagon::S2_pstorerif_io:
2322 case Hexagon::S4_storeiri_io:
2323 case Hexagon::S4_storeirit_io:
2324 case Hexagon::S4_storeirif_io:
2325 return isShiftedUInt<6,2>(Offset);
2326
2327 case Hexagon::L2_ploadrdt_io:
2328 case Hexagon::L2_ploadrdf_io:
2329 case Hexagon::S2_pstorerdt_io:
2330 case Hexagon::S2_pstorerdf_io:
2331 return isShiftedUInt<6,3>(Offset);
2332 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002333
Benjamin Kramerb6684012011-12-27 11:41:05 +00002334 llvm_unreachable("No offset range is defined for this opcode. "
2335 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002336}
2337
2338
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002339bool HexagonInstrInfo::isVecAcc(const MachineInstr *MI) const {
2340 return MI && isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002341}
2342
2343
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002344bool HexagonInstrInfo::isVecALU(const MachineInstr *MI) const {
2345 if (!MI)
Andrew Trickd06df962012-02-01 22:13:57 +00002346 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002347 const uint64_t F = get(MI->getOpcode()).TSFlags;
2348 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2349 return
2350 V == HexagonII::TypeCVI_VA ||
2351 V == HexagonII::TypeCVI_VA_DV;
2352}
Andrew Trickd06df962012-02-01 22:13:57 +00002353
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002354
2355bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr *ProdMI,
2356 const MachineInstr *ConsMI) const {
2357 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2358 return true;
2359
2360 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2361 return true;
2362
2363 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002364 return true;
2365
2366 return false;
2367}
Jyotsna Verma84256432013-03-01 17:37:13 +00002368
Jyotsna Verma84256432013-03-01 17:37:13 +00002369
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002370bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2371 for (auto &I : *B)
2372 if (I.isEHLabel())
2373 return true;
2374 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002375}
2376
Jyotsna Verma84256432013-03-01 17:37:13 +00002377
2378// Returns true if an instruction can be converted into a non-extended
2379// equivalent instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002380bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002381 short NonExtOpcode;
2382 // Check if the instruction has a register form that uses register in place
2383 // of the extended operand, if so return that as the non-extended form.
2384 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2385 return true;
2386
2387 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002388 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002389
2390 switch (getAddrMode(MI)) {
2391 case HexagonII::Absolute :
2392 // Load/store with absolute addressing mode can be converted into
2393 // base+offset mode.
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00002394 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002395 break;
2396 case HexagonII::BaseImmOffset :
2397 // Load/store with base+offset addressing mode can be converted into
2398 // base+register offset addressing mode. However left shift operand should
2399 // be set to 0.
2400 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2401 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002402 case HexagonII::BaseLongOffset:
2403 NonExtOpcode = Hexagon::getRegShlForm(MI->getOpcode());
2404 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002405 default:
2406 return false;
2407 }
2408 if (NonExtOpcode < 0)
2409 return false;
2410 return true;
2411 }
2412 return false;
2413}
2414
Jyotsna Verma84256432013-03-01 17:37:13 +00002415
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002416bool HexagonInstrInfo::hasPseudoInstrPair(MachineInstr *MI) const {
2417 return Hexagon::getRealHWInstr(MI->getOpcode(),
2418 Hexagon::InstrType_Pseudo) >= 0;
2419}
2420
2421
2422bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2423 const {
2424 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2425 while (I != E) {
2426 if (I->isBarrier())
2427 return true;
2428 ++I;
2429 }
2430 return false;
2431}
2432
2433
2434// Returns true, if a LD insn can be promoted to a cur load.
2435bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr *MI) const {
2436 auto &HST = MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2437 const uint64_t F = MI->getDesc().TSFlags;
2438 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2439 HST.hasV60TOps();
2440}
2441
2442
2443// Returns true, if a ST insn can be promoted to a new-value store.
2444bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
2445 const uint64_t F = MI->getDesc().TSFlags;
2446 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2447}
2448
2449
2450bool HexagonInstrInfo::producesStall(const MachineInstr *ProdMI,
2451 const MachineInstr *ConsMI) const {
2452 // There is no stall when ProdMI is not a V60 vector.
2453 if (!isV60VectorInstruction(ProdMI))
2454 return false;
2455
2456 // There is no stall when ProdMI and ConsMI are not dependent.
2457 if (!isDependent(ProdMI, ConsMI))
2458 return false;
2459
2460 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2461 // are scheduled in consecutive packets.
2462 if (isVecUsableNextPacket(ProdMI, ConsMI))
2463 return false;
2464
2465 return true;
2466}
2467
2468
2469bool HexagonInstrInfo::producesStall(const MachineInstr *MI,
2470 MachineBasicBlock::const_instr_iterator BII) const {
2471 // There is no stall when I is not a V60 vector.
2472 if (!isV60VectorInstruction(MI))
2473 return false;
2474
2475 MachineBasicBlock::const_instr_iterator MII = BII;
2476 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2477
2478 if (!(*MII).isBundle()) {
2479 const MachineInstr *J = &*MII;
2480 if (!isV60VectorInstruction(J))
2481 return false;
2482 else if (isVecUsableNextPacket(J, MI))
2483 return false;
2484 return true;
2485 }
2486
2487 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
2488 const MachineInstr *J = &*MII;
2489 if (producesStall(J, MI))
2490 return true;
2491 }
2492 return false;
2493}
2494
2495
2496bool HexagonInstrInfo::predCanBeUsedAsDotNew(MachineInstr *MI,
2497 unsigned PredReg) const {
2498 for (unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
2499 MachineOperand &MO = MI->getOperand(opNum);
2500 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
2501 return false; // Predicate register must be explicitly defined.
2502 }
2503
2504 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2505 // memd_locked cannot be used as .new as well,
2506 // but we don't seem to have these instructions defined.
2507 return MI->getOpcode() != Hexagon::A4_tlbmatch;
2508}
2509
2510
2511bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
2512 return (Opcode == Hexagon::J2_jumpt) ||
2513 (Opcode == Hexagon::J2_jumpf) ||
2514 (Opcode == Hexagon::J2_jumptnew) ||
2515 (Opcode == Hexagon::J2_jumpfnew) ||
2516 (Opcode == Hexagon::J2_jumptnewpt) ||
2517 (Opcode == Hexagon::J2_jumpfnewpt);
2518}
2519
2520
2521bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2522 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2523 return false;
2524 return !isPredicatedTrue(Cond[0].getImm());
2525}
2526
2527
2528unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
2529 const uint64_t F = MI->getDesc().TSFlags;
2530 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
2531}
2532
2533
2534// Returns the base register in a memory access (load/store). The offset is
2535// returned in Offset and the access size is returned in AccessSize.
2536unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr *MI,
2537 int &Offset, unsigned &AccessSize) const {
2538 // Return if it is not a base+offset type instruction or a MemOp.
2539 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
2540 getAddrMode(MI) != HexagonII::BaseLongOffset &&
2541 !isMemOp(MI) && !isPostIncrement(MI))
2542 return 0;
2543
2544 // Since it is a memory access instruction, getMemAccessSize() should never
2545 // return 0.
2546 assert (getMemAccessSize(MI) &&
2547 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
2548
2549 // Return Values of getMemAccessSize() are
2550 // 0 - Checked in the assert above.
2551 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
2552 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
2553 AccessSize = (1U << (getMemAccessSize(MI) - 1));
2554
2555 unsigned basePos = 0, offsetPos = 0;
2556 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
2557 return 0;
2558
2559 // Post increment updates its EA after the mem access,
2560 // so we need to treat its offset as zero.
2561 if (isPostIncrement(MI))
2562 Offset = 0;
2563 else {
2564 Offset = MI->getOperand(offsetPos).getImm();
2565 }
2566
2567 return MI->getOperand(basePos).getReg();
2568}
2569
2570
2571/// Return the position of the base and offset operands for this instruction.
2572bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr *MI,
2573 unsigned &BasePos, unsigned &OffsetPos) const {
2574 // Deal with memops first.
2575 if (isMemOp(MI)) {
2576 assert (MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
2577 "Bad Memop.");
2578 BasePos = 0;
2579 OffsetPos = 1;
2580 } else if (MI->mayStore()) {
2581 BasePos = 0;
2582 OffsetPos = 1;
2583 } else if (MI->mayLoad()) {
2584 BasePos = 1;
2585 OffsetPos = 2;
2586 } else
2587 return false;
2588
2589 if (isPredicated(MI)) {
2590 BasePos++;
2591 OffsetPos++;
2592 }
2593 if (isPostIncrement(MI)) {
2594 BasePos++;
2595 OffsetPos++;
2596 }
2597
2598 if (!MI->getOperand(BasePos).isReg() || !MI->getOperand(OffsetPos).isImm())
2599 return false;
2600
2601 return true;
2602}
2603
2604
2605// Inserts branching instructions in reverse order of their occurence.
2606// e.g. jump_t t1 (i1)
2607// jump t2 (i2)
2608// Jumpers = {i2, i1}
2609SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
2610 MachineBasicBlock& MBB) const {
2611 SmallVector<MachineInstr*, 2> Jumpers;
2612 // If the block has no terminators, it just falls into the block after it.
2613 MachineBasicBlock::instr_iterator I = MBB.instr_end();
2614 if (I == MBB.instr_begin())
2615 return Jumpers;
2616
2617 // A basic block may looks like this:
2618 //
2619 // [ insn
2620 // EH_LABEL
2621 // insn
2622 // insn
2623 // insn
2624 // EH_LABEL
2625 // insn ]
2626 //
2627 // It has two succs but does not have a terminator
2628 // Don't know how to handle it.
2629 do {
2630 --I;
2631 if (I->isEHLabel())
2632 return Jumpers;
2633 } while (I != MBB.instr_begin());
2634
2635 I = MBB.instr_end();
2636 --I;
2637
2638 while (I->isDebugValue()) {
2639 if (I == MBB.instr_begin())
2640 return Jumpers;
2641 --I;
2642 }
2643 if (!isUnpredicatedTerminator(&*I))
2644 return Jumpers;
2645
2646 // Get the last instruction in the block.
2647 MachineInstr *LastInst = &*I;
2648 Jumpers.push_back(LastInst);
2649 MachineInstr *SecondLastInst = nullptr;
2650 // Find one more terminator if present.
2651 do {
2652 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
2653 if (!SecondLastInst) {
2654 SecondLastInst = &*I;
2655 Jumpers.push_back(SecondLastInst);
2656 } else // This is a third branch.
2657 return Jumpers;
2658 }
2659 if (I == MBB.instr_begin())
2660 break;
2661 --I;
2662 } while (true);
2663 return Jumpers;
2664}
2665
2666
2667// Returns Operand Index for the constant extended instruction.
2668unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
2669 const uint64_t F = MI->getDesc().TSFlags;
2670 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
2671}
2672
2673// See if instruction could potentially be a duplex candidate.
2674// If so, return its group. Zero otherwise.
2675HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
2676 const MachineInstr *MI) const {
2677 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
2678
2679 switch (MI->getOpcode()) {
2680 default:
2681 return HexagonII::HCG_None;
2682 //
2683 // Compound pairs.
2684 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
2685 // "Rd16=#U6 ; jump #r9:2"
2686 // "Rd16=Rs16 ; jump #r9:2"
2687 //
2688 case Hexagon::C2_cmpeq:
2689 case Hexagon::C2_cmpgt:
2690 case Hexagon::C2_cmpgtu:
2691 DstReg = MI->getOperand(0).getReg();
2692 Src1Reg = MI->getOperand(1).getReg();
2693 Src2Reg = MI->getOperand(2).getReg();
2694 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2695 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2696 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
2697 return HexagonII::HCG_A;
2698 break;
2699 case Hexagon::C2_cmpeqi:
2700 case Hexagon::C2_cmpgti:
2701 case Hexagon::C2_cmpgtui:
2702 // P0 = cmp.eq(Rs,#u2)
2703 DstReg = MI->getOperand(0).getReg();
2704 SrcReg = MI->getOperand(1).getReg();
2705 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2706 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2707 isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
2708 ((isUInt<5>(MI->getOperand(2).getImm())) ||
2709 (MI->getOperand(2).getImm() == -1)))
2710 return HexagonII::HCG_A;
2711 break;
2712 case Hexagon::A2_tfr:
2713 // Rd = Rs
2714 DstReg = MI->getOperand(0).getReg();
2715 SrcReg = MI->getOperand(1).getReg();
2716 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
2717 return HexagonII::HCG_A;
2718 break;
2719 case Hexagon::A2_tfrsi:
2720 // Rd = #u6
2721 // Do not test for #u6 size since the const is getting extended
2722 // regardless and compound could be formed.
2723 DstReg = MI->getOperand(0).getReg();
2724 if (isIntRegForSubInst(DstReg))
2725 return HexagonII::HCG_A;
2726 break;
2727 case Hexagon::S2_tstbit_i:
2728 DstReg = MI->getOperand(0).getReg();
2729 Src1Reg = MI->getOperand(1).getReg();
2730 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2731 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2732 MI->getOperand(2).isImm() &&
2733 isIntRegForSubInst(Src1Reg) && (MI->getOperand(2).getImm() == 0))
2734 return HexagonII::HCG_A;
2735 break;
2736 // The fact that .new form is used pretty much guarantees
2737 // that predicate register will match. Nevertheless,
2738 // there could be some false positives without additional
2739 // checking.
2740 case Hexagon::J2_jumptnew:
2741 case Hexagon::J2_jumpfnew:
2742 case Hexagon::J2_jumptnewpt:
2743 case Hexagon::J2_jumpfnewpt:
2744 Src1Reg = MI->getOperand(0).getReg();
2745 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
2746 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
2747 return HexagonII::HCG_B;
2748 break;
2749 // Transfer and jump:
2750 // Rd=#U6 ; jump #r9:2
2751 // Rd=Rs ; jump #r9:2
2752 // Do not test for jump range here.
2753 case Hexagon::J2_jump:
2754 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
2755 return HexagonII::HCG_C;
2756 break;
2757 }
2758
2759 return HexagonII::HCG_None;
2760}
2761
2762
2763// Returns -1 when there is no opcode found.
2764unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr *GA,
2765 const MachineInstr *GB) const {
2766 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
2767 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
2768 if ((GA->getOpcode() != Hexagon::C2_cmpeqi) ||
2769 (GB->getOpcode() != Hexagon::J2_jumptnew))
2770 return -1;
2771 unsigned DestReg = GA->getOperand(0).getReg();
2772 if (!GB->readsRegister(DestReg))
2773 return -1;
2774 if (DestReg == Hexagon::P0)
2775 return Hexagon::J4_cmpeqi_tp0_jump_nt;
2776 if (DestReg == Hexagon::P1)
2777 return Hexagon::J4_cmpeqi_tp1_jump_nt;
2778 return -1;
2779}
2780
2781
2782int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
2783 enum Hexagon::PredSense inPredSense;
2784 inPredSense = invertPredicate ? Hexagon::PredSense_false :
2785 Hexagon::PredSense_true;
2786 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
2787 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
2788 return CondOpcode;
2789
2790 // This switch case will be removed once all the instructions have been
2791 // modified to use relation maps.
2792 switch(Opc) {
2793 case Hexagon::TFRI_f:
2794 return !invertPredicate ? Hexagon::TFRI_cPt_f :
2795 Hexagon::TFRI_cNotPt_f;
2796 }
2797
2798 llvm_unreachable("Unexpected predicable instruction");
2799}
2800
2801
2802// Return the cur value instruction for a given store.
2803int HexagonInstrInfo::getDotCurOp(const MachineInstr* MI) const {
2804 switch (MI->getOpcode()) {
2805 default: llvm_unreachable("Unknown .cur type");
2806 case Hexagon::V6_vL32b_pi:
2807 return Hexagon::V6_vL32b_cur_pi;
2808 case Hexagon::V6_vL32b_ai:
2809 return Hexagon::V6_vL32b_cur_ai;
2810 //128B
2811 case Hexagon::V6_vL32b_pi_128B:
2812 return Hexagon::V6_vL32b_cur_pi_128B;
2813 case Hexagon::V6_vL32b_ai_128B:
2814 return Hexagon::V6_vL32b_cur_ai_128B;
2815 }
2816 return 0;
2817}
2818
2819
2820
2821// The diagram below shows the steps involved in the conversion of a predicated
2822// store instruction to its .new predicated new-value form.
2823//
2824// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
2825// ^ ^
2826// / \ (not OK. it will cause new-value store to be
2827// / X conditional on p0.new while R2 producer is
2828// / \ on p0)
2829// / \.
2830// p.new store p.old NV store
2831// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
2832// ^ ^
2833// \ /
2834// \ /
2835// \ /
2836// p.old store
2837// [if (p0)memw(R0+#0)=R2]
2838//
2839//
2840// The following set of instructions further explains the scenario where
2841// conditional new-value store becomes invalid when promoted to .new predicate
2842// form.
2843//
2844// { 1) if (p0) r0 = add(r1, r2)
2845// 2) p0 = cmp.eq(r3, #0) }
2846//
2847// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
2848// the first two instructions because in instr 1, r0 is conditional on old value
2849// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
2850// is not valid for new-value stores.
2851// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
2852// from the "Conditional Store" list. Because a predicated new value store
2853// would NOT be promoted to a double dot new store. See diagram below:
2854// This function returns yes for those stores that are predicated but not
2855// yet promoted to predicate dot new instructions.
2856//
2857// +---------------------+
2858// /-----| if (p0) memw(..)=r0 |---------\~
2859// || +---------------------+ ||
2860// promote || /\ /\ || promote
2861// || /||\ /||\ ||
2862// \||/ demote || \||/
2863// \/ || || \/
2864// +-------------------------+ || +-------------------------+
2865// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
2866// +-------------------------+ || +-------------------------+
2867// || || ||
2868// || demote \||/
2869// promote || \/ NOT possible
2870// || || /\~
2871// \||/ || /||\~
2872// \/ || ||
2873// +-----------------------------+
2874// | if (p0.new) memw(..)=r0.new |
2875// +-----------------------------+
2876// Double Dot New Store
2877//
2878// Returns the most basic instruction for the .new predicated instructions and
2879// new-value stores.
2880// For example, all of the following instructions will be converted back to the
2881// same instruction:
2882// 1) if (p0.new) memw(R0+#0) = R1.new --->
2883// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
2884// 3) if (p0.new) memw(R0+#0) = R1 --->
2885//
2886// To understand the translation of instruction 1 to its original form, consider
2887// a packet with 3 instructions.
2888// { p0 = cmp.eq(R0,R1)
2889// if (p0.new) R2 = add(R3, R4)
2890// R5 = add (R3, R1)
2891// }
2892// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
2893//
2894// This instruction can be part of the previous packet only if both p0 and R2
2895// are promoted to .new values. This promotion happens in steps, first
2896// predicate register is promoted to .new and in the next iteration R2 is
2897// promoted. Therefore, in case of dependence check failure (due to R5) during
2898// next iteration, it should be converted back to its most basic form.
2899
2900
2901// Return the new value instruction for a given store.
2902int HexagonInstrInfo::getDotNewOp(const MachineInstr* MI) const {
2903 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
2904 if (NVOpcode >= 0) // Valid new-value store instruction.
2905 return NVOpcode;
2906
2907 switch (MI->getOpcode()) {
2908 default: llvm_unreachable("Unknown .new type");
2909 case Hexagon::S4_storerb_ur:
2910 return Hexagon::S4_storerbnew_ur;
2911
2912 case Hexagon::S2_storerb_pci:
2913 return Hexagon::S2_storerb_pci;
2914
2915 case Hexagon::S2_storeri_pci:
2916 return Hexagon::S2_storeri_pci;
2917
2918 case Hexagon::S2_storerh_pci:
2919 return Hexagon::S2_storerh_pci;
2920
2921 case Hexagon::S2_storerd_pci:
2922 return Hexagon::S2_storerd_pci;
2923
2924 case Hexagon::S2_storerf_pci:
2925 return Hexagon::S2_storerf_pci;
2926
2927 case Hexagon::V6_vS32b_ai:
2928 return Hexagon::V6_vS32b_new_ai;
2929
2930 case Hexagon::V6_vS32b_pi:
2931 return Hexagon::V6_vS32b_new_pi;
2932
2933 // 128B
2934 case Hexagon::V6_vS32b_ai_128B:
2935 return Hexagon::V6_vS32b_new_ai_128B;
2936
2937 case Hexagon::V6_vS32b_pi_128B:
2938 return Hexagon::V6_vS32b_new_pi_128B;
2939 }
2940 return 0;
2941}
2942
2943// Returns the opcode to use when converting MI, which is a conditional jump,
2944// into a conditional instruction which uses the .new value of the predicate.
2945// We also use branch probabilities to add a hint to the jump.
2946int HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
2947 const MachineBranchProbabilityInfo *MBPI) const {
2948 // We assume that block can have at most two successors.
2949 bool taken = false;
2950 MachineBasicBlock *Src = MI->getParent();
2951 MachineOperand *BrTarget = &MI->getOperand(1);
2952 MachineBasicBlock *Dst = BrTarget->getMBB();
2953
2954 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
2955 if (Prediction >= BranchProbability(1,2))
2956 taken = true;
2957
2958 switch (MI->getOpcode()) {
2959 case Hexagon::J2_jumpt:
2960 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
2961 case Hexagon::J2_jumpf:
2962 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
2963
2964 default:
2965 llvm_unreachable("Unexpected jump instruction.");
2966 }
2967}
2968
2969
2970// Return .new predicate version for an instruction.
2971int HexagonInstrInfo::getDotNewPredOp(MachineInstr *MI,
2972 const MachineBranchProbabilityInfo *MBPI) const {
2973 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
2974 if (NewOpcode >= 0) // Valid predicate new instruction
2975 return NewOpcode;
2976
2977 switch (MI->getOpcode()) {
2978 // Condtional Jumps
2979 case Hexagon::J2_jumpt:
2980 case Hexagon::J2_jumpf:
2981 return getDotNewPredJumpOp(MI, MBPI);
2982
2983 default:
2984 assert(0 && "Unknown .new type");
2985 }
2986 return 0;
2987}
2988
2989
2990int HexagonInstrInfo::getDotOldOp(const int opc) const {
2991 int NewOp = opc;
2992 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
2993 NewOp = Hexagon::getPredOldOpcode(NewOp);
2994 assert(NewOp >= 0 &&
2995 "Couldn't change predicate new instruction to its old form.");
2996 }
2997
2998 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
2999 NewOp = Hexagon::getNonNVStore(NewOp);
3000 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3001 }
3002 return NewOp;
3003}
3004
3005
3006// See if instruction could potentially be a duplex candidate.
3007// If so, return its group. Zero otherwise.
3008HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3009 const MachineInstr *MI) const {
3010 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3011 auto &HRI = getRegisterInfo();
3012
3013 switch (MI->getOpcode()) {
3014 default:
3015 return HexagonII::HSIG_None;
3016 //
3017 // Group L1:
3018 //
3019 // Rd = memw(Rs+#u4:2)
3020 // Rd = memub(Rs+#u4:0)
3021 case Hexagon::L2_loadri_io:
3022 DstReg = MI->getOperand(0).getReg();
3023 SrcReg = MI->getOperand(1).getReg();
3024 // Special case this one from Group L2.
3025 // Rd = memw(r29+#u5:2)
3026 if (isIntRegForSubInst(DstReg)) {
3027 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3028 HRI.getStackRegister() == SrcReg &&
3029 MI->getOperand(2).isImm() &&
3030 isShiftedUInt<5,2>(MI->getOperand(2).getImm()))
3031 return HexagonII::HSIG_L2;
3032 // Rd = memw(Rs+#u4:2)
3033 if (isIntRegForSubInst(SrcReg) &&
3034 (MI->getOperand(2).isImm() &&
3035 isShiftedUInt<4,2>(MI->getOperand(2).getImm())))
3036 return HexagonII::HSIG_L1;
3037 }
3038 break;
3039 case Hexagon::L2_loadrub_io:
3040 // Rd = memub(Rs+#u4:0)
3041 DstReg = MI->getOperand(0).getReg();
3042 SrcReg = MI->getOperand(1).getReg();
3043 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3044 MI->getOperand(2).isImm() && isUInt<4>(MI->getOperand(2).getImm()))
3045 return HexagonII::HSIG_L1;
3046 break;
3047 //
3048 // Group L2:
3049 //
3050 // Rd = memh/memuh(Rs+#u3:1)
3051 // Rd = memb(Rs+#u3:0)
3052 // Rd = memw(r29+#u5:2) - Handled above.
3053 // Rdd = memd(r29+#u5:3)
3054 // deallocframe
3055 // [if ([!]p0[.new])] dealloc_return
3056 // [if ([!]p0[.new])] jumpr r31
3057 case Hexagon::L2_loadrh_io:
3058 case Hexagon::L2_loadruh_io:
3059 // Rd = memh/memuh(Rs+#u3:1)
3060 DstReg = MI->getOperand(0).getReg();
3061 SrcReg = MI->getOperand(1).getReg();
3062 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3063 MI->getOperand(2).isImm() &&
3064 isShiftedUInt<3,1>(MI->getOperand(2).getImm()))
3065 return HexagonII::HSIG_L2;
3066 break;
3067 case Hexagon::L2_loadrb_io:
3068 // Rd = memb(Rs+#u3:0)
3069 DstReg = MI->getOperand(0).getReg();
3070 SrcReg = MI->getOperand(1).getReg();
3071 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3072 MI->getOperand(2).isImm() &&
3073 isUInt<3>(MI->getOperand(2).getImm()))
3074 return HexagonII::HSIG_L2;
3075 break;
3076 case Hexagon::L2_loadrd_io:
3077 // Rdd = memd(r29+#u5:3)
3078 DstReg = MI->getOperand(0).getReg();
3079 SrcReg = MI->getOperand(1).getReg();
3080 if (isDblRegForSubInst(DstReg, HRI) &&
3081 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3082 HRI.getStackRegister() == SrcReg &&
3083 MI->getOperand(2).isImm() &&
3084 isShiftedUInt<5,3>(MI->getOperand(2).getImm()))
3085 return HexagonII::HSIG_L2;
3086 break;
3087 // dealloc_return is not documented in Hexagon Manual, but marked
3088 // with A_SUBINSN attribute in iset_v4classic.py.
3089 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3090 case Hexagon::L4_return:
3091 case Hexagon::L2_deallocframe:
3092 return HexagonII::HSIG_L2;
3093 case Hexagon::EH_RETURN_JMPR:
3094 case Hexagon::JMPret :
3095 // jumpr r31
3096 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
3097 DstReg = MI->getOperand(0).getReg();
3098 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3099 return HexagonII::HSIG_L2;
3100 break;
3101 case Hexagon::JMPrett:
3102 case Hexagon::JMPretf:
3103 case Hexagon::JMPrettnewpt:
3104 case Hexagon::JMPretfnewpt :
3105 case Hexagon::JMPrettnew :
3106 case Hexagon::JMPretfnew :
3107 DstReg = MI->getOperand(1).getReg();
3108 SrcReg = MI->getOperand(0).getReg();
3109 // [if ([!]p0[.new])] jumpr r31
3110 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3111 (Hexagon::P0 == SrcReg)) &&
3112 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3113 return HexagonII::HSIG_L2;
3114 break;
3115 case Hexagon::L4_return_t :
3116 case Hexagon::L4_return_f :
3117 case Hexagon::L4_return_tnew_pnt :
3118 case Hexagon::L4_return_fnew_pnt :
3119 case Hexagon::L4_return_tnew_pt :
3120 case Hexagon::L4_return_fnew_pt :
3121 // [if ([!]p0[.new])] dealloc_return
3122 SrcReg = MI->getOperand(0).getReg();
3123 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3124 return HexagonII::HSIG_L2;
3125 break;
3126 //
3127 // Group S1:
3128 //
3129 // memw(Rs+#u4:2) = Rt
3130 // memb(Rs+#u4:0) = Rt
3131 case Hexagon::S2_storeri_io:
3132 // Special case this one from Group S2.
3133 // memw(r29+#u5:2) = Rt
3134 Src1Reg = MI->getOperand(0).getReg();
3135 Src2Reg = MI->getOperand(2).getReg();
3136 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3137 isIntRegForSubInst(Src2Reg) &&
3138 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3139 isShiftedUInt<5,2>(MI->getOperand(1).getImm()))
3140 return HexagonII::HSIG_S2;
3141 // memw(Rs+#u4:2) = Rt
3142 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3143 MI->getOperand(1).isImm() &&
3144 isShiftedUInt<4,2>(MI->getOperand(1).getImm()))
3145 return HexagonII::HSIG_S1;
3146 break;
3147 case Hexagon::S2_storerb_io:
3148 // memb(Rs+#u4:0) = Rt
3149 Src1Reg = MI->getOperand(0).getReg();
3150 Src2Reg = MI->getOperand(2).getReg();
3151 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3152 MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()))
3153 return HexagonII::HSIG_S1;
3154 break;
3155 //
3156 // Group S2:
3157 //
3158 // memh(Rs+#u3:1) = Rt
3159 // memw(r29+#u5:2) = Rt
3160 // memd(r29+#s6:3) = Rtt
3161 // memw(Rs+#u4:2) = #U1
3162 // memb(Rs+#u4) = #U1
3163 // allocframe(#u5:3)
3164 case Hexagon::S2_storerh_io:
3165 // memh(Rs+#u3:1) = Rt
3166 Src1Reg = MI->getOperand(0).getReg();
3167 Src2Reg = MI->getOperand(2).getReg();
3168 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3169 MI->getOperand(1).isImm() &&
3170 isShiftedUInt<3,1>(MI->getOperand(1).getImm()))
3171 return HexagonII::HSIG_S1;
3172 break;
3173 case Hexagon::S2_storerd_io:
3174 // memd(r29+#s6:3) = Rtt
3175 Src1Reg = MI->getOperand(0).getReg();
3176 Src2Reg = MI->getOperand(2).getReg();
3177 if (isDblRegForSubInst(Src2Reg, HRI) &&
3178 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3179 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3180 isShiftedInt<6,3>(MI->getOperand(1).getImm()))
3181 return HexagonII::HSIG_S2;
3182 break;
3183 case Hexagon::S4_storeiri_io:
3184 // memw(Rs+#u4:2) = #U1
3185 Src1Reg = MI->getOperand(0).getReg();
3186 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3187 isShiftedUInt<4,2>(MI->getOperand(1).getImm()) &&
3188 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3189 return HexagonII::HSIG_S2;
3190 break;
3191 case Hexagon::S4_storeirb_io:
3192 // memb(Rs+#u4) = #U1
3193 Src1Reg = MI->getOperand(0).getReg();
3194 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3195 isUInt<4>(MI->getOperand(1).getImm()) && MI->getOperand(2).isImm() &&
3196 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3197 return HexagonII::HSIG_S2;
3198 break;
3199 case Hexagon::S2_allocframe:
3200 if (MI->getOperand(0).isImm() &&
3201 isShiftedUInt<5,3>(MI->getOperand(0).getImm()))
3202 return HexagonII::HSIG_S1;
3203 break;
3204 //
3205 // Group A:
3206 //
3207 // Rx = add(Rx,#s7)
3208 // Rd = Rs
3209 // Rd = #u6
3210 // Rd = #-1
3211 // if ([!]P0[.new]) Rd = #0
3212 // Rd = add(r29,#u6:2)
3213 // Rx = add(Rx,Rs)
3214 // P0 = cmp.eq(Rs,#u2)
3215 // Rdd = combine(#0,Rs)
3216 // Rdd = combine(Rs,#0)
3217 // Rdd = combine(#u2,#U2)
3218 // Rd = add(Rs,#1)
3219 // Rd = add(Rs,#-1)
3220 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3221 // Rd = and(Rs,#1)
3222 case Hexagon::A2_addi:
3223 DstReg = MI->getOperand(0).getReg();
3224 SrcReg = MI->getOperand(1).getReg();
3225 if (isIntRegForSubInst(DstReg)) {
3226 // Rd = add(r29,#u6:2)
3227 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3228 HRI.getStackRegister() == SrcReg && MI->getOperand(2).isImm() &&
3229 isShiftedUInt<6,2>(MI->getOperand(2).getImm()))
3230 return HexagonII::HSIG_A;
3231 // Rx = add(Rx,#s7)
3232 if ((DstReg == SrcReg) && MI->getOperand(2).isImm() &&
3233 isInt<7>(MI->getOperand(2).getImm()))
3234 return HexagonII::HSIG_A;
3235 // Rd = add(Rs,#1)
3236 // Rd = add(Rs,#-1)
3237 if (isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3238 ((MI->getOperand(2).getImm() == 1) ||
3239 (MI->getOperand(2).getImm() == -1)))
3240 return HexagonII::HSIG_A;
3241 }
3242 break;
3243 case Hexagon::A2_add:
3244 // Rx = add(Rx,Rs)
3245 DstReg = MI->getOperand(0).getReg();
3246 Src1Reg = MI->getOperand(1).getReg();
3247 Src2Reg = MI->getOperand(2).getReg();
3248 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3249 isIntRegForSubInst(Src2Reg))
3250 return HexagonII::HSIG_A;
3251 break;
3252 case Hexagon::A2_andir:
3253 // Same as zxtb.
3254 // Rd16=and(Rs16,#255)
3255 // Rd16=and(Rs16,#1)
3256 DstReg = MI->getOperand(0).getReg();
3257 SrcReg = MI->getOperand(1).getReg();
3258 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3259 MI->getOperand(2).isImm() &&
3260 ((MI->getOperand(2).getImm() == 1) ||
3261 (MI->getOperand(2).getImm() == 255)))
3262 return HexagonII::HSIG_A;
3263 break;
3264 case Hexagon::A2_tfr:
3265 // Rd = Rs
3266 DstReg = MI->getOperand(0).getReg();
3267 SrcReg = MI->getOperand(1).getReg();
3268 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3269 return HexagonII::HSIG_A;
3270 break;
3271 case Hexagon::A2_tfrsi:
3272 // Rd = #u6
3273 // Do not test for #u6 size since the const is getting extended
3274 // regardless and compound could be formed.
3275 // Rd = #-1
3276 DstReg = MI->getOperand(0).getReg();
3277 if (isIntRegForSubInst(DstReg))
3278 return HexagonII::HSIG_A;
3279 break;
3280 case Hexagon::C2_cmoveit:
3281 case Hexagon::C2_cmovenewit:
3282 case Hexagon::C2_cmoveif:
3283 case Hexagon::C2_cmovenewif:
3284 // if ([!]P0[.new]) Rd = #0
3285 // Actual form:
3286 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
3287 DstReg = MI->getOperand(0).getReg();
3288 SrcReg = MI->getOperand(1).getReg();
3289 if (isIntRegForSubInst(DstReg) &&
3290 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
3291 MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0)
3292 return HexagonII::HSIG_A;
3293 break;
3294 case Hexagon::C2_cmpeqi:
3295 // P0 = cmp.eq(Rs,#u2)
3296 DstReg = MI->getOperand(0).getReg();
3297 SrcReg = MI->getOperand(1).getReg();
3298 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3299 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
3300 MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm()))
3301 return HexagonII::HSIG_A;
3302 break;
3303 case Hexagon::A2_combineii:
3304 case Hexagon::A4_combineii:
3305 // Rdd = combine(#u2,#U2)
3306 DstReg = MI->getOperand(0).getReg();
3307 if (isDblRegForSubInst(DstReg, HRI) &&
3308 ((MI->getOperand(1).isImm() && isUInt<2>(MI->getOperand(1).getImm())) ||
3309 (MI->getOperand(1).isGlobal() &&
3310 isUInt<2>(MI->getOperand(1).getOffset()))) &&
3311 ((MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm())) ||
3312 (MI->getOperand(2).isGlobal() &&
3313 isUInt<2>(MI->getOperand(2).getOffset()))))
3314 return HexagonII::HSIG_A;
3315 break;
3316 case Hexagon::A4_combineri:
3317 // Rdd = combine(Rs,#0)
3318 DstReg = MI->getOperand(0).getReg();
3319 SrcReg = MI->getOperand(1).getReg();
3320 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3321 ((MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) ||
3322 (MI->getOperand(2).isGlobal() && MI->getOperand(2).getOffset() == 0)))
3323 return HexagonII::HSIG_A;
3324 break;
3325 case Hexagon::A4_combineir:
3326 // Rdd = combine(#0,Rs)
3327 DstReg = MI->getOperand(0).getReg();
3328 SrcReg = MI->getOperand(2).getReg();
3329 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3330 ((MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0) ||
3331 (MI->getOperand(1).isGlobal() && MI->getOperand(1).getOffset() == 0)))
3332 return HexagonII::HSIG_A;
3333 break;
3334 case Hexagon::A2_sxtb:
3335 case Hexagon::A2_sxth:
3336 case Hexagon::A2_zxtb:
3337 case Hexagon::A2_zxth:
3338 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3339 DstReg = MI->getOperand(0).getReg();
3340 SrcReg = MI->getOperand(1).getReg();
3341 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3342 return HexagonII::HSIG_A;
3343 break;
3344 }
3345
3346 return HexagonII::HSIG_None;
3347}
3348
3349
3350short HexagonInstrInfo::getEquivalentHWInstr(MachineInstr *MI) const {
3351 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Real);
3352}
3353
3354
3355// Return first non-debug instruction in the basic block.
3356MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3357 const {
3358 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
3359 MachineInstr *MI = &*MII;
3360 if (MI->isDebugValue())
3361 continue;
3362 return MI;
3363 }
3364 return nullptr;
3365}
3366
3367
3368unsigned HexagonInstrInfo::getInstrTimingClassLatency(
3369 const InstrItineraryData *ItinData, const MachineInstr *MI) const {
3370 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3371 // still have a MinLatency property, which getStageLatency checks.
3372 if (!ItinData)
3373 return getInstrLatency(ItinData, MI);
3374
3375 // Get the latency embedded in the itinerary. If we're not using timing class
3376 // latencies or if we using BSB scheduling, then restrict the maximum latency
3377 // to 1 (that is, either 0 or 1).
3378 if (MI->isTransient())
3379 return 0;
3380 unsigned Latency = ItinData->getStageLatency(MI->getDesc().getSchedClass());
3381 if (!EnableTimingClassLatency ||
3382 MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>().
3383 useBSBScheduling())
3384 if (Latency > 1)
3385 Latency = 1;
3386 return Latency;
3387}
3388
3389
3390// inverts the predication logic.
3391// p -> NotP
3392// NotP -> P
3393bool HexagonInstrInfo::getInvertedPredSense(
3394 SmallVectorImpl<MachineOperand> &Cond) const {
3395 if (Cond.empty())
3396 return false;
3397 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3398 Cond[0].setImm(Opc);
3399 return true;
3400}
3401
3402
3403unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3404 int InvPredOpcode;
3405 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3406 : Hexagon::getTruePredOpcode(Opc);
3407 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3408 return InvPredOpcode;
3409
3410 llvm_unreachable("Unexpected predicated instruction");
3411}
3412
3413
3414// Returns the max value that doesn't need to be extended.
3415int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
3416 const uint64_t F = MI->getDesc().TSFlags;
3417 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3418 & HexagonII::ExtentSignedMask;
3419 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3420 & HexagonII::ExtentBitsMask;
3421
3422 if (isSigned) // if value is signed
3423 return ~(-1U << (bits - 1));
3424 else
3425 return ~(-1U << bits);
3426}
3427
3428
3429unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr* MI) const {
3430 const uint64_t F = MI->getDesc().TSFlags;
3431 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3432}
3433
3434
3435// Returns the min value that doesn't need to be extended.
3436int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
3437 const uint64_t F = MI->getDesc().TSFlags;
3438 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3439 & HexagonII::ExtentSignedMask;
3440 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3441 & HexagonII::ExtentBitsMask;
3442
3443 if (isSigned) // if value is signed
3444 return -1U << (bits - 1);
3445 else
3446 return 0;
3447}
3448
3449
3450// Returns opcode of the non-extended equivalent instruction.
3451short HexagonInstrInfo::getNonExtOpcode(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003452 // Check if the instruction has a register form that uses register in place
3453 // of the extended operand, if so return that as the non-extended form.
3454 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
3455 if (NonExtOpcode >= 0)
3456 return NonExtOpcode;
3457
3458 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003459 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003460 switch (getAddrMode(MI)) {
3461 case HexagonII::Absolute :
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00003462 return Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003463 case HexagonII::BaseImmOffset :
3464 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003465 case HexagonII::BaseLongOffset:
3466 return Hexagon::getRegShlForm(MI->getOpcode());
3467
Jyotsna Verma84256432013-03-01 17:37:13 +00003468 default:
3469 return -1;
3470 }
3471 }
3472 return -1;
3473}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00003474
Brendon Cahoondf43e682015-05-08 16:16:29 +00003475
Ahmed Bougachac88bf542015-06-11 19:30:37 +00003476bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003477 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00003478 if (Cond.empty())
3479 return false;
3480 assert(Cond.size() == 2);
3481 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
3482 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3483 return false;
3484 }
3485 PredReg = Cond[1].getReg();
3486 PredRegPos = 1;
3487 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3488 PredRegFlags = 0;
3489 if (Cond[1].isImplicit())
3490 PredRegFlags = RegState::Implicit;
3491 if (Cond[1].isUndef())
3492 PredRegFlags |= RegState::Undef;
3493 return true;
3494}
3495
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003496
3497short HexagonInstrInfo::getPseudoInstrPair(MachineInstr *MI) const {
3498 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Pseudo);
3499}
3500
3501
3502short HexagonInstrInfo::getRegForm(const MachineInstr *MI) const {
3503 return Hexagon::getRegForm(MI->getOpcode());
3504}
3505
3506
3507// Return the number of bytes required to encode the instruction.
3508// Hexagon instructions are fixed length, 4 bytes, unless they
3509// use a constant extender, which requires another 4 bytes.
3510// For debug instructions and prolog labels, return 0.
3511unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
3512 if (MI->isDebugValue() || MI->isPosition())
3513 return 0;
3514
3515 unsigned Size = MI->getDesc().getSize();
3516 if (!Size)
3517 // Assume the default insn size in case it cannot be determined
3518 // for whatever reason.
3519 Size = HEXAGON_INSTR_SIZE;
3520
3521 if (isConstExtended(MI) || isExtended(MI))
3522 Size += HEXAGON_INSTR_SIZE;
3523
3524 // Try and compute number of instructions in asm.
3525 if (BranchRelaxAsmLarge && MI->getOpcode() == Hexagon::INLINEASM) {
3526 const MachineBasicBlock &MBB = *MI->getParent();
3527 const MachineFunction *MF = MBB.getParent();
3528 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
3529
3530 // Count the number of register definitions to find the asm string.
3531 unsigned NumDefs = 0;
3532 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
3533 ++NumDefs)
3534 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
3535
3536 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
3537 // Disassemble the AsmStr and approximate number of instructions.
3538 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
3539 Size = getInlineAsmLength(AsmStr, *MAI);
3540 }
3541
3542 return Size;
3543}
3544
3545
3546uint64_t HexagonInstrInfo::getType(const MachineInstr* MI) const {
3547 const uint64_t F = MI->getDesc().TSFlags;
3548 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
3549}
3550
3551
3552unsigned HexagonInstrInfo::getUnits(const MachineInstr* MI) const {
3553 const TargetSubtargetInfo &ST = MI->getParent()->getParent()->getSubtarget();
3554 const InstrItineraryData &II = *ST.getInstrItineraryData();
3555 const InstrStage &IS = *II.beginStage(MI->getDesc().getSchedClass());
3556
3557 return IS.getUnits();
3558}
3559
3560
3561unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
3562 const uint64_t F = get(Opcode).TSFlags;
3563 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
3564}
3565
3566
3567// Calculate size of the basic block without debug instructions.
3568unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
3569 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
3570}
3571
3572
3573unsigned HexagonInstrInfo::nonDbgBundleSize(
3574 MachineBasicBlock::const_iterator BundleHead) const {
3575 assert(BundleHead->isBundle() && "Not a bundle header");
3576 auto MII = BundleHead.getInstrIterator();
3577 // Skip the bundle header.
3578 return nonDbgMICount(++MII, getBundleEnd(BundleHead));
3579}
3580
3581
3582/// immediateExtend - Changes the instruction in place to one using an immediate
3583/// extender.
3584void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
3585 assert((isExtendable(MI)||isConstExtended(MI)) &&
3586 "Instruction must be extendable");
3587 // Find which operand is extendable.
3588 short ExtOpNum = getCExtOpNum(MI);
3589 MachineOperand &MO = MI->getOperand(ExtOpNum);
3590 // This needs to be something we understand.
3591 assert((MO.isMBB() || MO.isImm()) &&
3592 "Branch with unknown extendable field type");
3593 // Mark given operand as extended.
3594 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
3595}
3596
3597
3598bool HexagonInstrInfo::invertAndChangeJumpTarget(
3599 MachineInstr* MI, MachineBasicBlock* NewTarget) const {
3600 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
3601 << NewTarget->getNumber(); MI->dump(););
3602 assert(MI->isBranch());
3603 unsigned NewOpcode = getInvertedPredicatedOpcode(MI->getOpcode());
3604 int TargetPos = MI->getNumOperands() - 1;
3605 // In general branch target is the last operand,
3606 // but some implicit defs added at the end might change it.
3607 while ((TargetPos > -1) && !MI->getOperand(TargetPos).isMBB())
3608 --TargetPos;
3609 assert((TargetPos >= 0) && MI->getOperand(TargetPos).isMBB());
3610 MI->getOperand(TargetPos).setMBB(NewTarget);
3611 if (EnableBranchPrediction && isPredicatedNew(MI)) {
3612 NewOpcode = reversePrediction(NewOpcode);
3613 }
3614 MI->setDesc(get(NewOpcode));
3615 return true;
3616}
3617
3618
3619void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
3620 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
3621 MachineFunction::iterator A = MF.begin();
3622 MachineBasicBlock &B = *A;
3623 MachineBasicBlock::iterator I = B.begin();
3624 MachineInstr *MI = &*I;
3625 DebugLoc DL = MI->getDebugLoc();
3626 MachineInstr *NewMI;
3627
3628 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
3629 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
3630 NewMI = BuildMI(B, MI, DL, get(insn));
3631 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
3632 " Class: " << NewMI->getDesc().getSchedClass());
3633 NewMI->eraseFromParent();
3634 }
3635 /* --- The code above is used to generate complete set of Hexagon Insn --- */
3636}
3637
3638
3639// inverts the predication logic.
3640// p -> NotP
3641// NotP -> P
3642bool HexagonInstrInfo::reversePredSense(MachineInstr* MI) const {
3643 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI->dump());
3644 MI->setDesc(get(getInvertedPredicatedOpcode(MI->getOpcode())));
3645 return true;
3646}
3647
3648
3649// Reverse the branch prediction.
3650unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
3651 int PredRevOpcode = -1;
3652 if (isPredictedTaken(Opcode))
3653 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
3654 else
3655 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
3656 assert(PredRevOpcode > 0);
3657 return PredRevOpcode;
3658}
3659
3660
3661// TODO: Add more rigorous validation.
3662bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
3663 const {
3664 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
3665}
3666