blob: 9b7567c36bfd25f8c777ba6d3bb7257197afb1f1 [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"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000026#include "llvm/Support/Debug.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000027#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000028#include "llvm/Support/raw_ostream.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029
Tony Linthicum1213a7a2011-12-12 21:14:40 +000030using namespace llvm;
31
Chandler Carruthe96dd892014-04-21 22:55:11 +000032#define DEBUG_TYPE "hexagon-instrinfo"
33
Chandler Carruthd174b722014-04-22 02:03:14 +000034#define GET_INSTRINFO_CTOR_DTOR
35#define GET_INSTRMAP_INFO
36#include "HexagonGenInstrInfo.inc"
37#include "HexagonGenDFAPacketizer.inc"
38
Tony Linthicum1213a7a2011-12-12 21:14:40 +000039///
40/// Constants for Hexagon instructions.
41///
42const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000043const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000044const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000045const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000046const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000047const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000048const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000049const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000050const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000051const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000052const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000053const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000055const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000056const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000057const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000058const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000059const int Hexagon_MEMB_AUTOINC_MIN = -8;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000060
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000061// Pin the vtable to this file.
62void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +000063
64HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +000065 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
66 RI(), Subtarget(ST) {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +000067
68/// isLoadFromStackSlot - If the specified machine instruction is a direct
69/// load from a stack slot, return the virtual or physical register number of
70/// the destination along with the FrameIndex of the loaded stack slot. If
71/// not, return 0. This predicate must return 0 if the instruction has
72/// any side effects other than loading from the stack slot.
73unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
74 int &FrameIndex) const {
75
76
77 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000078 default: break;
Colin LeMahieu026e88d2014-12-23 20:02:16 +000079 case Hexagon::L2_loadri_io:
Colin LeMahieu947cd702014-12-23 20:44:59 +000080 case Hexagon::L2_loadrd_io:
Colin LeMahieu8e39cad2014-12-23 17:25:57 +000081 case Hexagon::L2_loadrh_io:
Colin LeMahieu4b1eac42014-12-22 21:40:43 +000082 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +000083 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084 if (MI->getOperand(2).isFI() &&
85 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
86 FrameIndex = MI->getOperand(2).getIndex();
87 return MI->getOperand(0).getReg();
88 }
89 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000091 return 0;
92}
93
94
95/// isStoreToStackSlot - If the specified machine instruction is a direct
96/// store to a stack slot, return the virtual or physical register number of
97/// the source reg along with the FrameIndex of the loaded stack slot. If
98/// not, return 0. This predicate must return 0 if the instruction has
99/// any side effects other than storing to the stack slot.
100unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
101 int &FrameIndex) const {
102 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000103 default: break;
Colin LeMahieubda31b42014-12-29 20:44:51 +0000104 case Hexagon::S2_storeri_io:
105 case Hexagon::S2_storerd_io:
106 case Hexagon::S2_storerh_io:
107 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000108 if (MI->getOperand(2).isFI() &&
109 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
Sirish Pande8bb97452012-05-12 05:54:15 +0000110 FrameIndex = MI->getOperand(0).getIndex();
111 return MI->getOperand(2).getReg();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000112 }
113 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000115 return 0;
116}
117
Brendon Cahoondf43e682015-05-08 16:16:29 +0000118// Find the hardware loop instruction used to set-up the specified loop.
119// On Hexagon, we have two instructions used to set-up the hardware loop
120// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
121// to indicate the end of a loop.
122static MachineInstr *
123findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
124 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
125 int LOOPi;
126 int LOOPr;
127 if (EndLoopOp == Hexagon::ENDLOOP0) {
128 LOOPi = Hexagon::J2_loop0i;
129 LOOPr = Hexagon::J2_loop0r;
130 } else { // EndLoopOp == Hexagon::EndLOOP1
131 LOOPi = Hexagon::J2_loop1i;
132 LOOPr = Hexagon::J2_loop1r;
133 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000134
Brendon Cahoondf43e682015-05-08 16:16:29 +0000135 // The loop set-up instruction will be in a predecessor block
136 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
137 PE = BB->pred_end(); PB != PE; ++PB) {
138 // If this has been visited, already skip it.
139 if (!Visited.insert(*PB).second)
140 continue;
141 if (*PB == BB)
142 continue;
143 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
144 E = (*PB)->instr_rend(); I != E; ++I) {
145 int Opc = I->getOpcode();
146 if (Opc == LOOPi || Opc == LOOPr)
147 return &*I;
148 // We've reached a different loop, which means the loop0 has been removed.
149 if (Opc == EndLoopOp)
150 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000151 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000152 // Check the predecessors for the LOOP instruction.
153 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
154 if (loop)
155 return loop;
156 }
157 return 0;
158}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000159
Brendon Cahoondf43e682015-05-08 16:16:29 +0000160unsigned HexagonInstrInfo::InsertBranch(
161 MachineBasicBlock &MBB,MachineBasicBlock *TBB, MachineBasicBlock *FBB,
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000162 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000163
164 Opcode_t BOpc = Hexagon::J2_jump;
165 Opcode_t BccOpc = Hexagon::J2_jumpt;
166
167 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
168
169 // Check if ReverseBranchCondition has asked to reverse this branch
170 // If we want to reverse the branch an odd number of times, we want
171 // J2_jumpf.
172 if (!Cond.empty() && Cond[0].isImm())
173 BccOpc = Cond[0].getImm();
174
175 if (!FBB) {
176 if (Cond.empty()) {
177 // Due to a bug in TailMerging/CFG Optimization, we need to add a
178 // special case handling of a predicated jump followed by an
179 // unconditional jump. If not, Tail Merging and CFG Optimization go
180 // into an infinite loop.
181 MachineBasicBlock *NewTBB, *NewFBB;
182 SmallVector<MachineOperand, 4> Cond;
183 MachineInstr *Term = MBB.getFirstTerminator();
184 if (Term != MBB.end() && isPredicated(Term) &&
185 !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond, false)) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000186 MachineBasicBlock *NextBB = &*++MBB.getIterator();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000187 if (NewTBB == NextBB) {
188 ReverseBranchCondition(Cond);
189 RemoveBranch(MBB);
190 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000191 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000192 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000193 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
194 } else if (isEndLoopN(Cond[0].getImm())) {
195 int EndLoopOp = Cond[0].getImm();
196 assert(Cond[1].isMBB());
197 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
198 // Check for it, and change the BB target if needed.
199 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
200 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
201 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
202 Loop->getOperand(0).setMBB(TBB);
203 // Add the ENDLOOP after the finding the LOOP0.
204 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
205 } else if (isNewValueJump(Cond[0].getImm())) {
206 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
207 // New value jump
208 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
209 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
210 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
211 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
212 if (Cond[2].isReg()) {
213 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
214 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
215 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
216 } else if(Cond[2].isImm()) {
217 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
218 addImm(Cond[2].getImm()).addMBB(TBB);
219 } else
220 llvm_unreachable("Invalid condition for branching");
221 } else {
222 assert((Cond.size() == 2) && "Malformed cond vector");
223 const MachineOperand &RO = Cond[1];
224 unsigned Flags = getUndefRegState(RO.isUndef());
225 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000226 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000227 return 1;
228 }
229 assert((!Cond.empty()) &&
230 "Cond. cannot be empty when multiple branchings are required");
231 assert((!isNewValueJump(Cond[0].getImm())) &&
232 "NV-jump cannot be inserted with another branch");
233 // Special case for hardware loops. The condition is a basic block.
234 if (isEndLoopN(Cond[0].getImm())) {
235 int EndLoopOp = Cond[0].getImm();
236 assert(Cond[1].isMBB());
237 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
238 // Check for it, and change the BB target if needed.
239 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
240 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
241 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
242 Loop->getOperand(0).setMBB(TBB);
243 // Add the ENDLOOP after the finding the LOOP0.
244 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
245 } else {
246 const MachineOperand &RO = Cond[1];
247 unsigned Flags = getUndefRegState(RO.isUndef());
248 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
249 }
250 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000251
Brendon Cahoondf43e682015-05-08 16:16:29 +0000252 return 2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000253}
254
255
Brendon Cahoondf43e682015-05-08 16:16:29 +0000256/// This function can analyze one/two way branching only and should (mostly) be
257/// called by target independent side.
258/// First entry is always the opcode of the branching instruction, except when
259/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
260/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
261/// e.g. Jump_c p will have
262/// Cond[0] = Jump_c
263/// Cond[1] = p
264/// HW-loop ENDLOOP:
265/// Cond[0] = ENDLOOP
266/// Cond[1] = MBB
267/// New value jump:
268/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
269/// Cond[1] = R
270/// Cond[2] = Imm
271/// @note Related function is \fn findInstrPredicate which fills in
272/// Cond. vector when a predicated instruction is passed to it.
273/// We follow same protocol in that case too.
274///
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000275bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
276 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000277 MachineBasicBlock *&FBB,
278 SmallVectorImpl<MachineOperand> &Cond,
279 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000280 TBB = nullptr;
281 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000282 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000283
284 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000285 MachineBasicBlock::instr_iterator I = MBB.instr_end();
286 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000287 return false;
288
289 // A basic block may looks like this:
290 //
291 // [ insn
292 // EH_LABEL
293 // insn
294 // insn
295 // insn
296 // EH_LABEL
297 // insn ]
298 //
299 // It has two succs but does not have a terminator
300 // Don't know how to handle it.
301 do {
302 --I;
303 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000304 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000305 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000306 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000307
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000308 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000309 --I;
310
311 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000312 if (I == MBB.instr_begin())
313 return false;
314 --I;
315 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000316
317 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
318 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000319 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000320 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000321 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
322 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
323 I->eraseFromParent();
324 I = MBB.instr_end();
325 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000326 return false;
327 --I;
328 }
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000329 if (!isUnpredicatedTerminator(&*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000330 return false;
331
332 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000333 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000334 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000335 // Find one more terminator if present.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000336 for (;;) {
337 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000338 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000339 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000340 else
341 // This is a third branch.
342 return true;
343 }
344 if (I == MBB.instr_begin())
345 break;
346 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000347 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000348
349 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000350 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
351 // If the branch target is not a basic block, it could be a tail call.
352 // (It is, if the target is a function.)
353 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
354 return true;
355 if (SecLastOpcode == Hexagon::J2_jump &&
356 !SecondLastInst->getOperand(0).isMBB())
357 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000358
359 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000360 bool LastOpcodeHasNVJump = isNewValueJump(LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000361
362 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000363 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000364 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000365 TBB = LastInst->getOperand(0).getMBB();
366 return false;
367 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000368 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000369 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000370 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000371 Cond.push_back(LastInst->getOperand(0));
372 return false;
373 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000374 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000375 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000376 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377 Cond.push_back(LastInst->getOperand(0));
378 return false;
379 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000380 // Only supporting rr/ri versions of new-value jumps.
381 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
382 TBB = LastInst->getOperand(2).getMBB();
383 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
384 Cond.push_back(LastInst->getOperand(0));
385 Cond.push_back(LastInst->getOperand(1));
386 return false;
387 }
388 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
389 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000390 // Otherwise, don't know what this is.
391 return true;
392 }
393
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000394 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000395 bool SecLastOpcodeHasNVJump = isNewValueJump(SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000396 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000397 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000398 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000399 Cond.push_back(SecondLastInst->getOperand(0));
400 FBB = LastInst->getOperand(0).getMBB();
401 return false;
402 }
403
Brendon Cahoondf43e682015-05-08 16:16:29 +0000404 // Only supporting rr/ri versions of new-value jumps.
405 if (SecLastOpcodeHasNVJump &&
406 (SecondLastInst->getNumExplicitOperands() == 3) &&
407 (LastOpcode == Hexagon::J2_jump)) {
408 TBB = SecondLastInst->getOperand(2).getMBB();
409 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
410 Cond.push_back(SecondLastInst->getOperand(0));
411 Cond.push_back(SecondLastInst->getOperand(1));
412 FBB = LastInst->getOperand(0).getMBB();
413 return false;
414 }
415
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000416 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
417 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000418 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000419 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000420 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000421 if (AllowModify)
422 I->eraseFromParent();
423 return false;
424 }
425
Brendon Cahoondf43e682015-05-08 16:16:29 +0000426 // If the block ends with an ENDLOOP, and J2_jump, handle it.
427 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000428 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000429 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000430 Cond.push_back(SecondLastInst->getOperand(0));
431 FBB = LastInst->getOperand(0).getMBB();
432 return false;
433 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000434 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
435 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000436 // Otherwise, can't handle this.
437 return true;
438}
439
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000440unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000441 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000442 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000443 unsigned Count = 0;
444 while (I != MBB.begin()) {
445 --I;
446 if (I->isDebugValue())
447 continue;
448 // Only removing branches from end of MBB.
449 if (!I->isBranch())
450 return Count;
451 if (Count && (I->getOpcode() == Hexagon::J2_jump))
452 llvm_unreachable("Malformed basic block: unconditional branch not last");
453 MBB.erase(&MBB.back());
454 I = MBB.end();
455 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000456 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000457 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000458}
459
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000460/// \brief For a comparison instruction, return the source registers in
461/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
462/// compares against in CmpValue. Return true if the comparison instruction
463/// can be analyzed.
464bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
465 unsigned &SrcReg, unsigned &SrcReg2,
466 int &Mask, int &Value) const {
467 unsigned Opc = MI->getOpcode();
468
469 // Set mask and the first source register.
470 switch (Opc) {
Colin LeMahieu902157c2014-11-25 18:20:52 +0000471 case Hexagon::C2_cmpeq:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000472 case Hexagon::C2_cmpeqp:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000473 case Hexagon::C2_cmpgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000474 case Hexagon::C2_cmpgtp:
475 case Hexagon::C2_cmpgtu:
476 case Hexagon::C2_cmpgtup:
477 case Hexagon::C4_cmpneq:
478 case Hexagon::C4_cmplte:
479 case Hexagon::C4_cmplteu:
480 case Hexagon::C2_cmpeqi:
481 case Hexagon::C2_cmpgti:
482 case Hexagon::C2_cmpgtui:
483 case Hexagon::C4_cmpneqi:
484 case Hexagon::C4_cmplteui:
485 case Hexagon::C4_cmpltei:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000486 SrcReg = MI->getOperand(1).getReg();
487 Mask = ~0;
488 break;
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000489 case Hexagon::A4_cmpbeq:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000490 case Hexagon::A4_cmpbgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000491 case Hexagon::A4_cmpbgtu:
492 case Hexagon::A4_cmpbeqi:
493 case Hexagon::A4_cmpbgti:
494 case Hexagon::A4_cmpbgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000495 SrcReg = MI->getOperand(1).getReg();
496 Mask = 0xFF;
497 break;
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000498 case Hexagon::A4_cmpheq:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000499 case Hexagon::A4_cmphgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000500 case Hexagon::A4_cmphgtu:
501 case Hexagon::A4_cmpheqi:
502 case Hexagon::A4_cmphgti:
503 case Hexagon::A4_cmphgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000504 SrcReg = MI->getOperand(1).getReg();
505 Mask = 0xFFFF;
506 break;
507 }
508
509 // Set the value/second source register.
510 switch (Opc) {
Colin LeMahieu902157c2014-11-25 18:20:52 +0000511 case Hexagon::C2_cmpeq:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000512 case Hexagon::C2_cmpeqp:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000513 case Hexagon::C2_cmpgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000514 case Hexagon::C2_cmpgtp:
515 case Hexagon::C2_cmpgtu:
516 case Hexagon::C2_cmpgtup:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000517 case Hexagon::A4_cmpbeq:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000518 case Hexagon::A4_cmpbgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000519 case Hexagon::A4_cmpbgtu:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000520 case Hexagon::A4_cmpheq:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000521 case Hexagon::A4_cmphgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000522 case Hexagon::A4_cmphgtu:
523 case Hexagon::C4_cmpneq:
524 case Hexagon::C4_cmplte:
525 case Hexagon::C4_cmplteu:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000526 SrcReg2 = MI->getOperand(2).getReg();
527 return true;
528
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000529 case Hexagon::C2_cmpeqi:
530 case Hexagon::C2_cmpgtui:
531 case Hexagon::C2_cmpgti:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000532 case Hexagon::C4_cmpneqi:
533 case Hexagon::C4_cmplteui:
534 case Hexagon::C4_cmpltei:
Colin LeMahieufa947902015-01-14 16:49:12 +0000535 case Hexagon::A4_cmpbeqi:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000536 case Hexagon::A4_cmpbgti:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000537 case Hexagon::A4_cmpbgtui:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000538 case Hexagon::A4_cmpheqi:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000539 case Hexagon::A4_cmphgti:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000540 case Hexagon::A4_cmphgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000541 SrcReg2 = 0;
542 Value = MI->getOperand(2).getImm();
543 return true;
544 }
545
546 return false;
547}
548
549
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000550void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
551 MachineBasicBlock::iterator I, DebugLoc DL,
552 unsigned DestReg, unsigned SrcReg,
553 bool KillSrc) const {
554 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000555 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000556 return;
557 }
558 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000559 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000560 return;
561 }
562 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
563 // Map Pd = Ps to Pd = or(Ps, Ps).
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000564 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000565 DestReg).addReg(SrcReg).addReg(SrcReg);
566 return;
567 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000568 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
569 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000570 // We can have an overlap between single and double reg: r1:0 = r0.
571 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
572 // r1:0 = r0
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000573 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000574 Hexagon::subreg_hireg))).addImm(0);
575 } else {
576 // r1:0 = r1 or no overlap.
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000577 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000578 Hexagon::subreg_loreg))).addReg(SrcReg);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000579 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000580 Hexagon::subreg_hireg))).addImm(0);
581 }
582 return;
583 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000584 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000585 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000586 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000587 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000588 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000589 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
590 Hexagon::IntRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000591 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000592 addReg(SrcReg, getKillRegState(KillSrc));
593 return;
594 }
595 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
596 Hexagon::PredRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000597 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000598 addReg(SrcReg, getKillRegState(KillSrc));
599 return;
600 }
Sirish Pande30804c22012-02-15 18:52:27 +0000601
602 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000603}
604
605
606void HexagonInstrInfo::
607storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
608 unsigned SrcReg, bool isKill, int FI,
609 const TargetRegisterClass *RC,
610 const TargetRegisterInfo *TRI) const {
611
612 DebugLoc DL = MBB.findDebugLoc(I);
613 MachineFunction &MF = *MBB.getParent();
614 MachineFrameInfo &MFI = *MF.getFrameInfo();
615 unsigned Align = MFI.getObjectAlignment(FI);
616
Alex Lorenze40c8a22015-08-11 23:09:45 +0000617 MachineMemOperand *MMO = MF.getMachineMemOperand(
618 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
619 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000620
Craig Topperc7242e02012-04-20 07:30:17 +0000621 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000622 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000623 .addFrameIndex(FI).addImm(0)
624 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000625 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000626 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000627 .addFrameIndex(FI).addImm(0)
628 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000629 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000630 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
631 .addFrameIndex(FI).addImm(0)
632 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
633 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000634 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000635 }
636}
637
638
639void HexagonInstrInfo::storeRegToAddr(
640 MachineFunction &MF, unsigned SrcReg,
641 bool isKill,
642 SmallVectorImpl<MachineOperand> &Addr,
643 const TargetRegisterClass *RC,
644 SmallVectorImpl<MachineInstr*> &NewMIs) const
645{
Craig Toppere55c5562012-02-07 02:50:20 +0000646 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000647}
648
649
650void HexagonInstrInfo::
651loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
652 unsigned DestReg, int FI,
653 const TargetRegisterClass *RC,
654 const TargetRegisterInfo *TRI) const {
655 DebugLoc DL = MBB.findDebugLoc(I);
656 MachineFunction &MF = *MBB.getParent();
657 MachineFrameInfo &MFI = *MF.getFrameInfo();
658 unsigned Align = MFI.getObjectAlignment(FI);
659
Alex Lorenze40c8a22015-08-11 23:09:45 +0000660 MachineMemOperand *MMO = MF.getMachineMemOperand(
661 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
662 MFI.getObjectSize(FI), Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000663 if (RC == &Hexagon::IntRegsRegClass) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000664 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000665 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000666 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000667 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000668 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000669 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000670 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
671 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
672 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000673 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000674 }
675}
676
677
678void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
679 SmallVectorImpl<MachineOperand> &Addr,
680 const TargetRegisterClass *RC,
681 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Craig Toppere55c5562012-02-07 02:50:20 +0000682 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000683}
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000684bool
685HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000686 const HexagonRegisterInfo &HRI = getRegisterInfo();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000687 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000688 MachineBasicBlock &MBB = *MI->getParent();
689 DebugLoc DL = MI->getDebugLoc();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000690 unsigned Opc = MI->getOpcode();
691
692 switch (Opc) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000693 case Hexagon::ALIGNA:
694 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI->getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000695 .addReg(HRI.getFrameRegister())
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000696 .addImm(-MI->getOperand(1).getImm());
697 MBB.erase(MI);
698 return true;
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000699 case Hexagon::TFR_PdTrue: {
700 unsigned Reg = MI->getOperand(0).getReg();
701 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
702 .addReg(Reg, RegState::Undef)
703 .addReg(Reg, RegState::Undef);
704 MBB.erase(MI);
705 return true;
706 }
707 case Hexagon::TFR_PdFalse: {
708 unsigned Reg = MI->getOperand(0).getReg();
709 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
710 .addReg(Reg, RegState::Undef)
711 .addReg(Reg, RegState::Undef);
712 MBB.erase(MI);
713 return true;
714 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000715 case Hexagon::VMULW: {
716 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
717 unsigned DstReg = MI->getOperand(0).getReg();
718 unsigned Src1Reg = MI->getOperand(1).getReg();
719 unsigned Src2Reg = MI->getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000720 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
721 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
722 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
723 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000724 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000725 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000726 .addReg(Src2SubHi);
727 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000728 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000729 .addReg(Src2SubLo);
730 MBB.erase(MI);
731 MRI.clearKillFlags(Src1SubHi);
732 MRI.clearKillFlags(Src1SubLo);
733 MRI.clearKillFlags(Src2SubHi);
734 MRI.clearKillFlags(Src2SubLo);
735 return true;
736 }
737 case Hexagon::VMULW_ACC: {
738 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
739 unsigned DstReg = MI->getOperand(0).getReg();
740 unsigned Src1Reg = MI->getOperand(1).getReg();
741 unsigned Src2Reg = MI->getOperand(2).getReg();
742 unsigned Src3Reg = MI->getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000743 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
744 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
745 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
746 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
747 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
748 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000749 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000750 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000751 .addReg(Src2SubHi).addReg(Src3SubHi);
752 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000753 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000754 .addReg(Src2SubLo).addReg(Src3SubLo);
755 MBB.erase(MI);
756 MRI.clearKillFlags(Src1SubHi);
757 MRI.clearKillFlags(Src1SubLo);
758 MRI.clearKillFlags(Src2SubHi);
759 MRI.clearKillFlags(Src2SubLo);
760 MRI.clearKillFlags(Src3SubHi);
761 MRI.clearKillFlags(Src3SubLo);
762 return true;
763 }
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000764 case Hexagon::MUX64_rr: {
765 const MachineOperand &Op0 = MI->getOperand(0);
766 const MachineOperand &Op1 = MI->getOperand(1);
767 const MachineOperand &Op2 = MI->getOperand(2);
768 const MachineOperand &Op3 = MI->getOperand(3);
769 unsigned Rd = Op0.getReg();
770 unsigned Pu = Op1.getReg();
771 unsigned Rs = Op2.getReg();
772 unsigned Rt = Op3.getReg();
773 DebugLoc DL = MI->getDebugLoc();
774 unsigned K1 = getKillRegState(Op1.isKill());
775 unsigned K2 = getKillRegState(Op2.isKill());
776 unsigned K3 = getKillRegState(Op3.isKill());
777 if (Rd != Rs)
778 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
779 .addReg(Pu, (Rd == Rt) ? K1 : 0)
780 .addReg(Rs, K2);
781 if (Rd != Rt)
782 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
783 .addReg(Pu, K1)
784 .addReg(Rt, K3);
785 MBB.erase(MI);
786 return true;
787 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000788 case Hexagon::TCRETURNi:
789 MI->setDesc(get(Hexagon::J2_jump));
790 return true;
791 case Hexagon::TCRETURNr:
792 MI->setDesc(get(Hexagon::J2_jumpr));
793 return true;
794 }
795
796 return false;
797}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000798
Keno Fischere70b31f2015-06-08 20:09:58 +0000799MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(
800 MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
801 MachineBasicBlock::iterator InsertPt, int FI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000802 // Hexagon_TODO: Implement.
Craig Topper062a2ba2014-04-25 05:30:21 +0000803 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000804}
805
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000806unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
807
808 MachineRegisterInfo &RegInfo = MF->getRegInfo();
809 const TargetRegisterClass *TRC;
Sirish Pande69295b82012-05-10 20:20:25 +0000810 if (VT == MVT::i1) {
Craig Topperc7242e02012-04-20 07:30:17 +0000811 TRC = &Hexagon::PredRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000812 } else if (VT == MVT::i32 || VT == MVT::f32) {
Craig Topperc7242e02012-04-20 07:30:17 +0000813 TRC = &Hexagon::IntRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000814 } else if (VT == MVT::i64 || VT == MVT::f64) {
Craig Topperc7242e02012-04-20 07:30:17 +0000815 TRC = &Hexagon::DoubleRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000816 } else {
Benjamin Kramerb6684012011-12-27 11:41:05 +0000817 llvm_unreachable("Cannot handle this register class");
Sirish Pande69295b82012-05-10 20:20:25 +0000818 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000819
820 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
821 return NewReg;
822}
823
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000824bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000825 const MCInstrDesc &MID = MI->getDesc();
826 const uint64_t F = MID.TSFlags;
827 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
828 return true;
829
830 // TODO: This is largely obsolete now. Will need to be removed
831 // in consecutive patches.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000832 switch(MI->getOpcode()) {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000833 // TFR_FI Remains a special case.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000834 case Hexagon::TFR_FI:
835 return true;
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000836 default:
837 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000838 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000839 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000840}
841
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000842// This returns true in two cases:
843// - The OP code itself indicates that this is an extended instruction.
844// - One of MOs has been marked with HMOTF_ConstExtended flag.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000845bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000846 // First check if this is permanently extended op code.
847 const uint64_t F = MI->getDesc().TSFlags;
848 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
849 return true;
850 // Use MO operand flags to determine if one of MI's operands
851 // has HMOTF_ConstExtended flag set.
852 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
853 E = MI->operands_end(); I != E; ++I) {
854 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Sirish Pande69295b82012-05-10 20:20:25 +0000855 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000856 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000857 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000858}
859
Jyotsna Verma84c47102013-05-06 18:49:23 +0000860bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
861 return MI->getDesc().isBranch();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000862}
863
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000864bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
865 if (isNewValueJump(MI))
866 return true;
867
868 if (isNewValueStore(MI))
869 return true;
870
871 return false;
872}
873
Brendon Cahoondf43e682015-05-08 16:16:29 +0000874bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
875 const uint64_t F = MI->getDesc().TSFlags;
876 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
877}
878
879bool HexagonInstrInfo::isNewValue(Opcode_t Opcode) const {
880 const uint64_t F = get(Opcode).TSFlags;
881 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
882}
883
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000884bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
885 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
886}
Andrew Trickd06df962012-02-01 22:13:57 +0000887
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000888bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
889 bool isPred = MI->getDesc().isPredicable();
890
891 if (!isPred)
892 return false;
893
894 const int Opc = MI->getOpcode();
895
896 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000897 case Hexagon::A2_tfrsi:
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000898 return (isOperandExtended(MI, 1) && isConstExtended(MI)) || isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000899
Colin LeMahieubda31b42014-12-29 20:44:51 +0000900 case Hexagon::S2_storerd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000901 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000902
Colin LeMahieubda31b42014-12-29 20:44:51 +0000903 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000904 case Hexagon::S2_storerinew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000905 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000906
Colin LeMahieubda31b42014-12-29 20:44:51 +0000907 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000908 case Hexagon::S2_storerhnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000909 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000910
Colin LeMahieubda31b42014-12-29 20:44:51 +0000911 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000912 case Hexagon::S2_storerbnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000913 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000914
Colin LeMahieu947cd702014-12-23 20:44:59 +0000915 case Hexagon::L2_loadrd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000916 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000917
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000918 case Hexagon::L2_loadri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000919 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000920
Colin LeMahieu8e39cad2014-12-23 17:25:57 +0000921 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +0000922 case Hexagon::L2_loadruh_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000923 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000924
Colin LeMahieu4b1eac42014-12-22 21:40:43 +0000925 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +0000926 case Hexagon::L2_loadrub_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000927 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000928
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000929 case Hexagon::L2_loadrd_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000930 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000931
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000932 case Hexagon::L2_loadri_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000933 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000934
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000935 case Hexagon::L2_loadrh_pi:
936 case Hexagon::L2_loadruh_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000937 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000938
Colin LeMahieu96976a12014-12-26 18:57:13 +0000939 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +0000940 case Hexagon::L2_loadrub_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000941 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000942
Colin LeMahieu2bad4a72014-12-30 21:01:38 +0000943 case Hexagon::S4_storeirb_io:
944 case Hexagon::S4_storeirh_io:
945 case Hexagon::S4_storeiri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000946 return (isUInt<6>(MI->getOperand(1).getImm()) &&
947 isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000948
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000949 case Hexagon::A2_addi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000950 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000951
Colin LeMahieu3b3197e2014-11-24 17:44:19 +0000952 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +0000953 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +0000954 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +0000955 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +0000956 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +0000957 case Hexagon::A2_zxth:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +0000958 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000959 }
960
961 return true;
962}
963
Sirish Pande8bb97452012-05-12 05:54:15 +0000964// This function performs the following inversiones:
965//
966// cPt ---> cNotPt
967// cNotPt ---> cPt
968//
Sirish Pande30804c22012-02-15 18:52:27 +0000969unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
Jyotsna Verma84c47102013-05-06 18:49:23 +0000970 int InvPredOpcode;
971 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
972 : Hexagon::getTruePredOpcode(Opc);
973 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
974 return InvPredOpcode;
975
Sirish Pande30804c22012-02-15 18:52:27 +0000976 switch(Opc) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000977 default: llvm_unreachable("Unexpected predicated instruction");
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000978 case Hexagon::C2_ccombinewt:
979 return Hexagon::C2_ccombinewf;
980 case Hexagon::C2_ccombinewf:
981 return Hexagon::C2_ccombinewt;
Sirish Pande30804c22012-02-15 18:52:27 +0000982
Jyotsna Verma978e9722013-05-09 18:25:44 +0000983 // Dealloc_return.
Colin LeMahieu14455532015-01-06 16:15:15 +0000984 case Hexagon::L4_return_t:
985 return Hexagon::L4_return_f;
986 case Hexagon::L4_return_f:
987 return Hexagon::L4_return_t;
Sirish Pande30804c22012-02-15 18:52:27 +0000988 }
989}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000990
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000991// New Value Store instructions.
992bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
993 const uint64_t F = MI->getDesc().TSFlags;
994
995 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
996}
997
998bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
999 const uint64_t F = get(Opcode).TSFlags;
1000
1001 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
1002}
Andrew Trickd06df962012-02-01 22:13:57 +00001003
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001004int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
Pranav Bhandarkar34b60182012-11-01 19:13:23 +00001005 enum Hexagon::PredSense inPredSense;
1006 inPredSense = invertPredicate ? Hexagon::PredSense_false :
1007 Hexagon::PredSense_true;
1008 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
1009 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
1010 return CondOpcode;
1011
1012 // This switch case will be removed once all the instructions have been
1013 // modified to use relation maps.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001014 switch(Opc) {
Sirish Pande69295b82012-05-10 20:20:25 +00001015 case Hexagon::TFRI_f:
1016 return !invertPredicate ? Hexagon::TFRI_cPt_f :
1017 Hexagon::TFRI_cNotPt_f;
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001018 case Hexagon::A2_combinew:
1019 return !invertPredicate ? Hexagon::C2_ccombinewt :
1020 Hexagon::C2_ccombinewf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001021
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001022 // DEALLOC_RETURN.
Colin LeMahieu14455532015-01-06 16:15:15 +00001023 case Hexagon::L4_return:
1024 return !invertPredicate ? Hexagon::L4_return_t:
1025 Hexagon::L4_return_f;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001026 }
Benjamin Kramerb6684012011-12-27 11:41:05 +00001027 llvm_unreachable("Unexpected predicable instruction");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001028}
1029
1030
1031bool HexagonInstrInfo::
1032PredicateInstruction(MachineInstr *MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001033 ArrayRef<MachineOperand> Cond) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00001034 if (Cond.empty() || isEndLoopN(Cond[0].getImm())) {
1035 DEBUG(dbgs() << "\nCannot predicate:"; MI->dump(););
1036 return false;
1037 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001038 int Opc = MI->getOpcode();
1039 assert (isPredicable(MI) && "Expected predicable instruction");
Brendon Cahoondf43e682015-05-08 16:16:29 +00001040 bool invertJump = predOpcodeHasNot(Cond);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001041
Brendon Cahoondf43e682015-05-08 16:16:29 +00001042 // We have to predicate MI "in place", i.e. after this function returns,
1043 // MI will need to be transformed into a predicated form. To avoid com-
1044 // plicated manipulations with the operands (handling tied operands,
1045 // etc.), build a new temporary instruction, then overwrite MI with it.
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001046
Brendon Cahoondf43e682015-05-08 16:16:29 +00001047 MachineBasicBlock &B = *MI->getParent();
1048 DebugLoc DL = MI->getDebugLoc();
1049 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1050 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
1051 unsigned NOp = 0, NumOps = MI->getNumOperands();
1052 while (NOp < NumOps) {
1053 MachineOperand &Op = MI->getOperand(NOp);
1054 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1055 break;
1056 T.addOperand(Op);
1057 NOp++;
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001058 }
1059
Brendon Cahoondf43e682015-05-08 16:16:29 +00001060 unsigned PredReg, PredRegPos, PredRegFlags;
1061 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1062 (void)GotPredReg;
1063 assert(GotPredReg);
1064 T.addReg(PredReg, PredRegFlags);
1065 while (NOp < NumOps)
1066 T.addOperand(MI->getOperand(NOp++));
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001067
Brendon Cahoondf43e682015-05-08 16:16:29 +00001068 MI->setDesc(get(PredOpc));
1069 while (unsigned n = MI->getNumOperands())
1070 MI->RemoveOperand(n-1);
1071 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
1072 MI->addOperand(T->getOperand(i));
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001073
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +00001074 MachineBasicBlock::instr_iterator TI = T->getIterator();
Brendon Cahoondf43e682015-05-08 16:16:29 +00001075 B.erase(TI);
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001076
Brendon Cahoondf43e682015-05-08 16:16:29 +00001077 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1078 MRI.clearKillFlags(PredReg);
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001079
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001080 return true;
1081}
1082
1083
1084bool
1085HexagonInstrInfo::
1086isProfitableToIfCvt(MachineBasicBlock &MBB,
Kay Tiong Khoof2949212012-06-13 15:53:04 +00001087 unsigned NumCycles,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001088 unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001089 BranchProbability Probability) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001090 return true;
1091}
1092
1093
1094bool
1095HexagonInstrInfo::
1096isProfitableToIfCvt(MachineBasicBlock &TMBB,
1097 unsigned NumTCycles,
1098 unsigned ExtraTCycles,
1099 MachineBasicBlock &FMBB,
1100 unsigned NumFCycles,
1101 unsigned ExtraFCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001102 BranchProbability Probability) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001103 return true;
1104}
1105
Jyotsna Verma84c47102013-05-06 18:49:23 +00001106// Returns true if an instruction is predicated irrespective of the predicate
1107// sense. For example, all of the following will return true.
1108// if (p0) R1 = add(R2, R3)
1109// if (!p0) R1 = add(R2, R3)
1110// if (p0.new) R1 = add(R2, R3)
1111// if (!p0.new) R1 = add(R2, R3)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001112bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoon6f358372012-02-08 18:25:47 +00001113 const uint64_t F = MI->getDesc().TSFlags;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001114
Brendon Cahoon6f358372012-02-08 18:25:47 +00001115 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001116}
1117
Jyotsna Verma84c47102013-05-06 18:49:23 +00001118bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
1119 const uint64_t F = get(Opcode).TSFlags;
1120
1121 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1122}
1123
1124bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
1125 const uint64_t F = MI->getDesc().TSFlags;
1126
1127 assert(isPredicated(MI));
1128 return (!((F >> HexagonII::PredicatedFalsePos) &
1129 HexagonII::PredicatedFalseMask));
1130}
1131
1132bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
1133 const uint64_t F = get(Opcode).TSFlags;
1134
1135 // Make sure that the instruction is predicated.
1136 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1137 return (!((F >> HexagonII::PredicatedFalsePos) &
1138 HexagonII::PredicatedFalseMask));
1139}
1140
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001141bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1142 const uint64_t F = MI->getDesc().TSFlags;
1143
1144 assert(isPredicated(MI));
1145 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1146}
1147
Jyotsna Verma84c47102013-05-06 18:49:23 +00001148bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1149 const uint64_t F = get(Opcode).TSFlags;
1150
1151 assert(isPredicated(Opcode));
1152 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1153}
1154
Jyotsna Verma438cec52013-05-10 20:58:11 +00001155// Returns true, if a ST insn can be promoted to a new-value store.
1156bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
Jyotsna Verma438cec52013-05-10 20:58:11 +00001157 const uint64_t F = MI->getDesc().TSFlags;
1158
1159 return ((F >> HexagonII::mayNVStorePos) &
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001160 HexagonII::mayNVStoreMask);
Jyotsna Verma438cec52013-05-10 20:58:11 +00001161}
1162
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001163bool
1164HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1165 std::vector<MachineOperand> &Pred) const {
1166 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1167 MachineOperand MO = MI->getOperand(oper);
1168 if (MO.isReg() && MO.isDef()) {
1169 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
Craig Topperc7242e02012-04-20 07:30:17 +00001170 if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001171 Pred.push_back(MO);
1172 return true;
1173 }
1174 }
1175 }
1176 return false;
1177}
1178
1179
1180bool
1181HexagonInstrInfo::
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001182SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1183 ArrayRef<MachineOperand> Pred2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001184 // TODO: Fix this
1185 return false;
1186}
1187
1188
1189//
1190// We indicate that we want to reverse the branch by
Brendon Cahoondf43e682015-05-08 16:16:29 +00001191// inserting the reversed branching opcode.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001192//
Brendon Cahoondf43e682015-05-08 16:16:29 +00001193bool HexagonInstrInfo::ReverseBranchCondition(
1194 SmallVectorImpl<MachineOperand> &Cond) const {
1195 if (Cond.empty())
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001196 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001197 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1198 Opcode_t opcode = Cond[0].getImm();
1199 //unsigned temp;
1200 assert(get(opcode).isBranch() && "Should be a branching condition.");
1201 if (isEndLoopN(opcode))
1202 return true;
1203 Opcode_t NewOpcode = getInvertedPredicatedOpcode(opcode);
1204 Cond[0].setImm(NewOpcode);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001205 return false;
1206}
1207
1208
1209bool HexagonInstrInfo::
1210isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
Cong Houc536bd92015-09-10 23:10:42 +00001211 BranchProbability Probability) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001212 return (NumInstrs <= 4);
1213}
1214
1215bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1216 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001217 default: return false;
Colin LeMahieu14455532015-01-06 16:15:15 +00001218 case Hexagon::L4_return:
1219 case Hexagon::L4_return_t:
1220 case Hexagon::L4_return_f:
1221 case Hexagon::L4_return_tnew_pnt:
1222 case Hexagon::L4_return_fnew_pnt:
1223 case Hexagon::L4_return_tnew_pt:
1224 case Hexagon::L4_return_fnew_pt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001225 return true;
1226 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001227}
1228
1229
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001230bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
1231 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001232 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001233 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001234 // inserted to calculate the final address. Due to this reason, the function
1235 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00001236 // We used to assert if the offset was not properly aligned, however,
1237 // there are cases where a misaligned pointer recast can cause this
1238 // problem, and we need to allow for it. The front end warns of such
1239 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001240
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001241 switch (Opcode) {
1242 case Hexagon::J2_loop0i:
1243 case Hexagon::J2_loop1i:
1244 return isUInt<10>(Offset);
1245 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001246
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001247 if (Extend)
1248 return true;
1249
1250 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001251 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001252 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001253 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1254 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1255
Colin LeMahieu947cd702014-12-23 20:44:59 +00001256 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001257 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001258 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1259 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1260
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001261 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001262 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001263 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001264 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1265 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1266
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001267 case Hexagon::L2_loadrb_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001268 case Hexagon::S2_storerb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001269 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001270 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1271 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1272
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001273 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001274 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1275 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1276
Colin LeMahieudacf0572015-01-05 21:36:38 +00001277 case Hexagon::L4_iadd_memopw_io:
1278 case Hexagon::L4_isub_memopw_io:
1279 case Hexagon::L4_add_memopw_io:
1280 case Hexagon::L4_sub_memopw_io:
1281 case Hexagon::L4_and_memopw_io:
1282 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001283 return (0 <= Offset && Offset <= 255);
1284
Colin LeMahieudacf0572015-01-05 21:36:38 +00001285 case Hexagon::L4_iadd_memoph_io:
1286 case Hexagon::L4_isub_memoph_io:
1287 case Hexagon::L4_add_memoph_io:
1288 case Hexagon::L4_sub_memoph_io:
1289 case Hexagon::L4_and_memoph_io:
1290 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001291 return (0 <= Offset && Offset <= 127);
1292
Colin LeMahieudacf0572015-01-05 21:36:38 +00001293 case Hexagon::L4_iadd_memopb_io:
1294 case Hexagon::L4_isub_memopb_io:
1295 case Hexagon::L4_add_memopb_io:
1296 case Hexagon::L4_sub_memopb_io:
1297 case Hexagon::L4_and_memopb_io:
1298 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001299 return (0 <= Offset && Offset <= 63);
1300
1301 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1302 // any size. Later pass knows how to handle it.
1303 case Hexagon::STriw_pred:
1304 case Hexagon::LDriw_pred:
1305 return true;
1306
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001307 case Hexagon::TFR_FI:
1308 case Hexagon::TFR_FIA:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001309 case Hexagon::INLINEASM:
1310 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001311
1312 case Hexagon::L2_ploadrbt_io:
1313 case Hexagon::L2_ploadrbf_io:
1314 case Hexagon::L2_ploadrubt_io:
1315 case Hexagon::L2_ploadrubf_io:
1316 case Hexagon::S2_pstorerbt_io:
1317 case Hexagon::S2_pstorerbf_io:
1318 case Hexagon::S4_storeirb_io:
1319 case Hexagon::S4_storeirbt_io:
1320 case Hexagon::S4_storeirbf_io:
1321 return isUInt<6>(Offset);
1322
1323 case Hexagon::L2_ploadrht_io:
1324 case Hexagon::L2_ploadrhf_io:
1325 case Hexagon::L2_ploadruht_io:
1326 case Hexagon::L2_ploadruhf_io:
1327 case Hexagon::S2_pstorerht_io:
1328 case Hexagon::S2_pstorerhf_io:
1329 case Hexagon::S4_storeirh_io:
1330 case Hexagon::S4_storeirht_io:
1331 case Hexagon::S4_storeirhf_io:
1332 return isShiftedUInt<6,1>(Offset);
1333
1334 case Hexagon::L2_ploadrit_io:
1335 case Hexagon::L2_ploadrif_io:
1336 case Hexagon::S2_pstorerit_io:
1337 case Hexagon::S2_pstorerif_io:
1338 case Hexagon::S4_storeiri_io:
1339 case Hexagon::S4_storeirit_io:
1340 case Hexagon::S4_storeirif_io:
1341 return isShiftedUInt<6,2>(Offset);
1342
1343 case Hexagon::L2_ploadrdt_io:
1344 case Hexagon::L2_ploadrdf_io:
1345 case Hexagon::S2_pstorerdt_io:
1346 case Hexagon::S2_pstorerdf_io:
1347 return isShiftedUInt<6,3>(Offset);
1348 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001349
Benjamin Kramerb6684012011-12-27 11:41:05 +00001350 llvm_unreachable("No offset range is defined for this opcode. "
1351 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001352}
1353
1354
1355//
1356// Check if the Offset is a valid auto-inc imm by Load/Store Type.
1357//
1358bool HexagonInstrInfo::
1359isValidAutoIncImm(const EVT VT, const int Offset) const {
1360
1361 if (VT == MVT::i64) {
1362 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1363 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1364 (Offset & 0x7) == 0);
1365 }
1366 if (VT == MVT::i32) {
1367 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1368 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1369 (Offset & 0x3) == 0);
1370 }
1371 if (VT == MVT::i16) {
1372 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1373 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1374 (Offset & 0x1) == 0);
1375 }
1376 if (VT == MVT::i8) {
1377 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1378 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1379 }
Craig Toppere55c5562012-02-07 02:50:20 +00001380 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001381}
1382
1383
1384bool HexagonInstrInfo::
1385isMemOp(const MachineInstr *MI) const {
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001386// return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1387
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001388 switch (MI->getOpcode())
1389 {
Colin LeMahieudacf0572015-01-05 21:36:38 +00001390 default: return false;
1391 case Hexagon::L4_iadd_memopw_io:
1392 case Hexagon::L4_isub_memopw_io:
1393 case Hexagon::L4_add_memopw_io:
1394 case Hexagon::L4_sub_memopw_io:
1395 case Hexagon::L4_and_memopw_io:
1396 case Hexagon::L4_or_memopw_io:
1397 case Hexagon::L4_iadd_memoph_io:
1398 case Hexagon::L4_isub_memoph_io:
1399 case Hexagon::L4_add_memoph_io:
1400 case Hexagon::L4_sub_memoph_io:
1401 case Hexagon::L4_and_memoph_io:
1402 case Hexagon::L4_or_memoph_io:
1403 case Hexagon::L4_iadd_memopb_io:
1404 case Hexagon::L4_isub_memopb_io:
1405 case Hexagon::L4_add_memopb_io:
1406 case Hexagon::L4_sub_memopb_io:
1407 case Hexagon::L4_and_memopb_io:
1408 case Hexagon::L4_or_memopb_io:
1409 case Hexagon::L4_ior_memopb_io:
1410 case Hexagon::L4_ior_memoph_io:
1411 case Hexagon::L4_ior_memopw_io:
1412 case Hexagon::L4_iand_memopb_io:
1413 case Hexagon::L4_iand_memoph_io:
1414 case Hexagon::L4_iand_memopw_io:
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001415 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001416 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001417 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001418}
1419
1420
1421bool HexagonInstrInfo::
1422isSpillPredRegOp(const MachineInstr *MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001423 switch (MI->getOpcode()) {
1424 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001425 case Hexagon::STriw_pred :
1426 case Hexagon::LDriw_pred :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001427 return true;
Sirish Pande2c7bf002012-04-23 17:49:28 +00001428 }
Sirish Pande4bd20c52012-05-12 05:10:30 +00001429}
1430
1431bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1432 switch (MI->getOpcode()) {
Sirish Pande8bb97452012-05-12 05:54:15 +00001433 default: return false;
Colin LeMahieu902157c2014-11-25 18:20:52 +00001434 case Hexagon::C2_cmpeq:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001435 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001436 case Hexagon::C2_cmpgt:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001437 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001438 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001439 case Hexagon::C2_cmpgtui:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001440 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +00001441 }
Sirish Pande2c7bf002012-04-23 17:49:28 +00001442}
1443
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001444bool HexagonInstrInfo::
1445isConditionalTransfer (const MachineInstr *MI) const {
1446 switch (MI->getOpcode()) {
1447 default: return false;
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001448 case Hexagon::A2_tfrt:
1449 case Hexagon::A2_tfrf:
1450 case Hexagon::C2_cmoveit:
1451 case Hexagon::C2_cmoveif:
1452 case Hexagon::A2_tfrtnew:
1453 case Hexagon::A2_tfrfnew:
1454 case Hexagon::C2_cmovenewit:
1455 case Hexagon::C2_cmovenewif:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001456 return true;
1457 }
1458}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001459
1460bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001461 switch (MI->getOpcode())
1462 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001463 default: return false;
Colin LeMahieuefa74e02014-11-18 20:28:11 +00001464 case Hexagon::A2_paddf:
1465 case Hexagon::A2_paddfnew:
1466 case Hexagon::A2_paddt:
1467 case Hexagon::A2_paddtnew:
Colin LeMahieu44fd1c82014-11-18 22:45:47 +00001468 case Hexagon::A2_pandf:
1469 case Hexagon::A2_pandfnew:
1470 case Hexagon::A2_pandt:
1471 case Hexagon::A2_pandtnew:
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001472 case Hexagon::A4_paslhf:
1473 case Hexagon::A4_paslhfnew:
1474 case Hexagon::A4_paslht:
1475 case Hexagon::A4_paslhtnew:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001476 case Hexagon::A4_pasrhf:
1477 case Hexagon::A4_pasrhfnew:
1478 case Hexagon::A4_pasrht:
1479 case Hexagon::A4_pasrhtnew:
Colin LeMahieu21866542014-11-19 22:58:04 +00001480 case Hexagon::A2_porf:
1481 case Hexagon::A2_porfnew:
1482 case Hexagon::A2_port:
1483 case Hexagon::A2_portnew:
Colin LeMahieue88447d2014-11-21 21:19:18 +00001484 case Hexagon::A2_psubf:
1485 case Hexagon::A2_psubfnew:
1486 case Hexagon::A2_psubt:
1487 case Hexagon::A2_psubtnew:
Colin LeMahieuac006432014-11-19 23:22:23 +00001488 case Hexagon::A2_pxorf:
1489 case Hexagon::A2_pxorfnew:
1490 case Hexagon::A2_pxort:
1491 case Hexagon::A2_pxortnew:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001492 case Hexagon::A4_psxthf:
1493 case Hexagon::A4_psxthfnew:
1494 case Hexagon::A4_psxtht:
1495 case Hexagon::A4_psxthtnew:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001496 case Hexagon::A4_psxtbf:
1497 case Hexagon::A4_psxtbfnew:
1498 case Hexagon::A4_psxtbt:
1499 case Hexagon::A4_psxtbtnew:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001500 case Hexagon::A4_pzxtbf:
1501 case Hexagon::A4_pzxtbfnew:
1502 case Hexagon::A4_pzxtbt:
1503 case Hexagon::A4_pzxtbtnew:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001504 case Hexagon::A4_pzxthf:
1505 case Hexagon::A4_pzxthfnew:
1506 case Hexagon::A4_pzxtht:
1507 case Hexagon::A4_pzxthtnew:
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001508 case Hexagon::A2_paddit:
1509 case Hexagon::A2_paddif:
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001510 case Hexagon::C2_ccombinewt:
1511 case Hexagon::C2_ccombinewf:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001512 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001513 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001514}
1515
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001516bool HexagonInstrInfo::
1517isConditionalLoad (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001518 switch (MI->getOpcode())
1519 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001520 default: return false;
Colin LeMahieu947cd702014-12-23 20:44:59 +00001521 case Hexagon::L2_ploadrdt_io :
1522 case Hexagon::L2_ploadrdf_io:
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001523 case Hexagon::L2_ploadrit_io:
1524 case Hexagon::L2_ploadrif_io:
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001525 case Hexagon::L2_ploadrht_io:
1526 case Hexagon::L2_ploadrhf_io:
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001527 case Hexagon::L2_ploadrbt_io:
1528 case Hexagon::L2_ploadrbf_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001529 case Hexagon::L2_ploadruht_io:
1530 case Hexagon::L2_ploadruhf_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001531 case Hexagon::L2_ploadrubt_io:
1532 case Hexagon::L2_ploadrubf_io:
Colin LeMahieu9161d472014-12-30 18:58:47 +00001533 case Hexagon::L2_ploadrdt_pi:
1534 case Hexagon::L2_ploadrdf_pi:
1535 case Hexagon::L2_ploadrit_pi:
1536 case Hexagon::L2_ploadrif_pi:
1537 case Hexagon::L2_ploadrht_pi:
1538 case Hexagon::L2_ploadrhf_pi:
1539 case Hexagon::L2_ploadrbt_pi:
1540 case Hexagon::L2_ploadrbf_pi:
1541 case Hexagon::L2_ploadruht_pi:
1542 case Hexagon::L2_ploadruhf_pi:
1543 case Hexagon::L2_ploadrubt_pi:
1544 case Hexagon::L2_ploadrubf_pi:
Colin LeMahieu9161d472014-12-30 18:58:47 +00001545 case Hexagon::L4_ploadrdt_rr:
1546 case Hexagon::L4_ploadrdf_rr:
1547 case Hexagon::L4_ploadrbt_rr:
1548 case Hexagon::L4_ploadrbf_rr:
1549 case Hexagon::L4_ploadrubt_rr:
1550 case Hexagon::L4_ploadrubf_rr:
1551 case Hexagon::L4_ploadrht_rr:
1552 case Hexagon::L4_ploadrhf_rr:
1553 case Hexagon::L4_ploadruht_rr:
1554 case Hexagon::L4_ploadruhf_rr:
1555 case Hexagon::L4_ploadrit_rr:
1556 case Hexagon::L4_ploadrif_rr:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001557 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001558 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001559}
Andrew Trickd06df962012-02-01 22:13:57 +00001560
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001561// Returns true if an instruction is a conditional store.
1562//
1563// Note: It doesn't include conditional new-value stores as they can't be
1564// converted to .new predicate.
1565//
1566// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1567// ^ ^
1568// / \ (not OK. it will cause new-value store to be
1569// / X conditional on p0.new while R2 producer is
1570// / \ on p0)
1571// / \.
1572// p.new store p.old NV store
1573// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1574// ^ ^
1575// \ /
1576// \ /
1577// \ /
1578// p.old store
1579// [if (p0)memw(R0+#0)=R2]
1580//
1581// The above diagram shows the steps involoved in the conversion of a predicated
1582// store instruction to its .new predicated new-value form.
1583//
1584// The following set of instructions further explains the scenario where
1585// conditional new-value store becomes invalid when promoted to .new predicate
1586// form.
1587//
1588// { 1) if (p0) r0 = add(r1, r2)
1589// 2) p0 = cmp.eq(r3, #0) }
1590//
1591// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1592// the first two instructions because in instr 1, r0 is conditional on old value
1593// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1594// is not valid for new-value stores.
1595bool HexagonInstrInfo::
1596isConditionalStore (const MachineInstr* MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001597 switch (MI->getOpcode())
1598 {
1599 default: return false;
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001600 case Hexagon::S4_storeirbt_io:
1601 case Hexagon::S4_storeirbf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001602 case Hexagon::S4_pstorerbt_rr:
1603 case Hexagon::S4_pstorerbf_rr:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001604 case Hexagon::S2_pstorerbt_io:
1605 case Hexagon::S2_pstorerbf_io:
Colin LeMahieu3d34afb2014-12-29 19:42:14 +00001606 case Hexagon::S2_pstorerbt_pi:
1607 case Hexagon::S2_pstorerbf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001608 case Hexagon::S2_pstorerdt_io:
1609 case Hexagon::S2_pstorerdf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001610 case Hexagon::S4_pstorerdt_rr:
1611 case Hexagon::S4_pstorerdf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001612 case Hexagon::S2_pstorerdt_pi:
1613 case Hexagon::S2_pstorerdf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001614 case Hexagon::S2_pstorerht_io:
1615 case Hexagon::S2_pstorerhf_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001616 case Hexagon::S4_storeirht_io:
1617 case Hexagon::S4_storeirhf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001618 case Hexagon::S4_pstorerht_rr:
1619 case Hexagon::S4_pstorerhf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001620 case Hexagon::S2_pstorerht_pi:
1621 case Hexagon::S2_pstorerhf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001622 case Hexagon::S2_pstorerit_io:
1623 case Hexagon::S2_pstorerif_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001624 case Hexagon::S4_storeirit_io:
1625 case Hexagon::S4_storeirif_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001626 case Hexagon::S4_pstorerit_rr:
1627 case Hexagon::S4_pstorerif_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001628 case Hexagon::S2_pstorerit_pi:
1629 case Hexagon::S2_pstorerif_pi:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001630
1631 // V4 global address store before promoting to dot new.
Colin LeMahieu14455532015-01-06 16:15:15 +00001632 case Hexagon::S4_pstorerdt_abs:
1633 case Hexagon::S4_pstorerdf_abs:
1634 case Hexagon::S4_pstorerbt_abs:
1635 case Hexagon::S4_pstorerbf_abs:
1636 case Hexagon::S4_pstorerht_abs:
1637 case Hexagon::S4_pstorerhf_abs:
1638 case Hexagon::S4_pstorerit_abs:
1639 case Hexagon::S4_pstorerif_abs:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001640 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001641
1642 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1643 // from the "Conditional Store" list. Because a predicated new value store
1644 // would NOT be promoted to a double dot new store. See diagram below:
1645 // This function returns yes for those stores that are predicated but not
1646 // yet promoted to predicate dot new instructions.
1647 //
1648 // +---------------------+
1649 // /-----| if (p0) memw(..)=r0 |---------\~
1650 // || +---------------------+ ||
1651 // promote || /\ /\ || promote
1652 // || /||\ /||\ ||
1653 // \||/ demote || \||/
1654 // \/ || || \/
1655 // +-------------------------+ || +-------------------------+
1656 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1657 // +-------------------------+ || +-------------------------+
1658 // || || ||
1659 // || demote \||/
1660 // promote || \/ NOT possible
1661 // || || /\~
1662 // \||/ || /||\~
1663 // \/ || ||
1664 // +-----------------------------+
1665 // | if (p0.new) memw(..)=r0.new |
1666 // +-----------------------------+
1667 // Double Dot New Store
1668 //
1669 }
1670}
1671
Jyotsna Verma84c47102013-05-06 18:49:23 +00001672
1673bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1674 if (isNewValue(MI) && isBranch(MI))
1675 return true;
1676 return false;
1677}
1678
Brendon Cahoondf43e682015-05-08 16:16:29 +00001679bool HexagonInstrInfo::isNewValueJump(Opcode_t Opcode) const {
1680 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001681}
1682
Brendon Cahoondf43e682015-05-08 16:16:29 +00001683bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1684 return (getAddrMode(MI) == HexagonII::PostInc);
Jyotsna Verma84c47102013-05-06 18:49:23 +00001685}
1686
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001687// Returns true, if any one of the operands is a dot new
1688// insn, whether it is predicated dot new or register dot new.
1689bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1690 return (isNewValueInst(MI) ||
1691 (isPredicated(MI) && isPredicatedNew(MI)));
1692}
1693
Jyotsna Verma438cec52013-05-10 20:58:11 +00001694// Returns the most basic instruction for the .new predicated instructions and
1695// new-value stores.
1696// For example, all of the following instructions will be converted back to the
1697// same instruction:
1698// 1) if (p0.new) memw(R0+#0) = R1.new --->
1699// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1700// 3) if (p0.new) memw(R0+#0) = R1 --->
1701//
1702
1703int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1704 int NewOp = opc;
1705 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1706 NewOp = Hexagon::getPredOldOpcode(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001707 assert(NewOp >= 0 &&
1708 "Couldn't change predicate new instruction to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001709 }
1710
Alp Tokerf907b892013-12-05 05:44:44 +00001711 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
Jyotsna Verma438cec52013-05-10 20:58:11 +00001712 NewOp = Hexagon::getNonNVStore(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001713 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001714 }
1715 return NewOp;
1716}
1717
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001718// Return the new value instruction for a given store.
1719int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1720 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1721 if (NVOpcode >= 0) // Valid new-value store instruction.
1722 return NVOpcode;
1723
1724 switch (MI->getOpcode()) {
1725 default: llvm_unreachable("Unknown .new type");
Colin LeMahieuc0434462015-02-04 17:52:06 +00001726 case Hexagon::S4_storerb_ur:
1727 return Hexagon::S4_storerbnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001728
Colin LeMahieuc0434462015-02-04 17:52:06 +00001729 case Hexagon::S4_storerh_ur:
1730 return Hexagon::S4_storerhnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001731
Colin LeMahieuc0434462015-02-04 17:52:06 +00001732 case Hexagon::S4_storeri_ur:
1733 return Hexagon::S4_storerinew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001734
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001735 case Hexagon::S2_storerb_pci:
1736 return Hexagon::S2_storerb_pci;
1737
1738 case Hexagon::S2_storeri_pci:
1739 return Hexagon::S2_storeri_pci;
1740
1741 case Hexagon::S2_storerh_pci:
1742 return Hexagon::S2_storerh_pci;
1743
1744 case Hexagon::S2_storerd_pci:
1745 return Hexagon::S2_storerd_pci;
1746
1747 case Hexagon::S2_storerf_pci:
1748 return Hexagon::S2_storerf_pci;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001749 }
1750 return 0;
1751}
1752
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001753// Return .new predicate version for an instruction.
1754int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1755 const MachineBranchProbabilityInfo
1756 *MBPI) const {
1757
1758 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1759 if (NewOpcode >= 0) // Valid predicate new instruction
1760 return NewOpcode;
1761
1762 switch (MI->getOpcode()) {
1763 default: llvm_unreachable("Unknown .new type");
1764 // Condtional Jumps
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001765 case Hexagon::J2_jumpt:
1766 case Hexagon::J2_jumpf:
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001767 return getDotNewPredJumpOp(MI, MBPI);
1768
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001769 case Hexagon::J2_jumprt:
1770 return Hexagon::J2_jumptnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001771
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001772 case Hexagon::J2_jumprf:
1773 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001774
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001775 case Hexagon::JMPrett:
1776 return Hexagon::J2_jumprtnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001777
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001778 case Hexagon::JMPretf:
1779 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001780
1781
1782 // Conditional combine
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001783 case Hexagon::C2_ccombinewt:
1784 return Hexagon::C2_ccombinewnewt;
1785 case Hexagon::C2_ccombinewf:
1786 return Hexagon::C2_ccombinewnewf;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001787 }
1788}
1789
1790
Jyotsna Verma84256432013-03-01 17:37:13 +00001791unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1792 const uint64_t F = MI->getDesc().TSFlags;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001793
Jyotsna Verma84256432013-03-01 17:37:13 +00001794 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1795}
1796
1797/// immediateExtend - Changes the instruction in place to one using an immediate
1798/// extender.
1799void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1800 assert((isExtendable(MI)||isConstExtended(MI)) &&
1801 "Instruction must be extendable");
1802 // Find which operand is extendable.
1803 short ExtOpNum = getCExtOpNum(MI);
1804 MachineOperand &MO = MI->getOperand(ExtOpNum);
1805 // This needs to be something we understand.
1806 assert((MO.isMBB() || MO.isImm()) &&
1807 "Branch with unknown extendable field type");
1808 // Mark given operand as extended.
1809 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1810}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001811
Eric Christopher143f02c2014-10-09 01:59:35 +00001812DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1813 const TargetSubtargetInfo &STI) const {
1814 const InstrItineraryData *II = STI.getInstrItineraryData();
1815 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
Andrew Trickd06df962012-02-01 22:13:57 +00001816}
1817
1818bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1819 const MachineBasicBlock *MBB,
1820 const MachineFunction &MF) const {
1821 // Debug info is never a scheduling boundary. It's necessary to be explicit
1822 // due to the special treatment of IT instructions below, otherwise a
1823 // dbg_value followed by an IT will result in the IT instruction being
1824 // considered a scheduling hazard, which is wrong. It should be the actual
1825 // instruction preceding the dbg_value instruction(s), just like it is
1826 // when debug info is not present.
1827 if (MI->isDebugValue())
1828 return false;
1829
1830 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001831 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
Andrew Trickd06df962012-02-01 22:13:57 +00001832 return true;
1833
1834 return false;
1835}
Jyotsna Verma84256432013-03-01 17:37:13 +00001836
Brendon Cahoon55bdeb72015-04-27 14:16:43 +00001837bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001838 const uint64_t F = MI->getDesc().TSFlags;
1839 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1840 if (isExtended) // Instruction must be extended.
1841 return true;
1842
Brendon Cahoon55bdeb72015-04-27 14:16:43 +00001843 unsigned isExtendable =
1844 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
Jyotsna Verma84256432013-03-01 17:37:13 +00001845 if (!isExtendable)
1846 return false;
1847
1848 short ExtOpNum = getCExtOpNum(MI);
1849 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1850 // Use MO operand flags to determine if MO
1851 // has the HMOTF_ConstExtended flag set.
1852 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1853 return true;
1854 // If this is a Machine BB address we are talking about, and it is
1855 // not marked as extended, say so.
1856 if (MO.isMBB())
1857 return false;
1858
1859 // We could be using an instruction with an extendable immediate and shoehorn
1860 // a global address into it. If it is a global address it will be constant
1861 // extended. We do this for COMBINE.
1862 // We currently only handle isGlobal() because it is the only kind of
1863 // object we are going to end up with here for now.
1864 // In the future we probably should add isSymbol(), etc.
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +00001865 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1866 MO.isJTI() || MO.isCPI())
Jyotsna Verma84256432013-03-01 17:37:13 +00001867 return true;
1868
1869 // If the extendable operand is not 'Immediate' type, the instruction should
1870 // have 'isExtended' flag set.
1871 assert(MO.isImm() && "Extendable operand must be Immediate type");
1872
1873 int MinValue = getMinValue(MI);
1874 int MaxValue = getMaxValue(MI);
1875 int ImmValue = MO.getImm();
1876
1877 return (ImmValue < MinValue || ImmValue > MaxValue);
1878}
1879
Brendon Cahoon55bdeb72015-04-27 14:16:43 +00001880// Return the number of bytes required to encode the instruction.
1881// Hexagon instructions are fixed length, 4 bytes, unless they
1882// use a constant extender, which requires another 4 bytes.
1883// For debug instructions and prolog labels, return 0.
1884unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
1885
1886 if (MI->isDebugValue() || MI->isPosition())
1887 return 0;
1888
1889 unsigned Size = MI->getDesc().getSize();
1890 if (!Size)
1891 // Assume the default insn size in case it cannot be determined
1892 // for whatever reason.
1893 Size = HEXAGON_INSTR_SIZE;
1894
1895 if (isConstExtended(MI) || isExtended(MI))
1896 Size += HEXAGON_INSTR_SIZE;
1897
1898 return Size;
1899}
1900
Jyotsna Verma1d297502013-05-02 15:39:30 +00001901// Returns the opcode to use when converting MI, which is a conditional jump,
1902// into a conditional instruction which uses the .new value of the predicate.
1903// We also use branch probabilities to add a hint to the jump.
1904int
1905HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1906 const
1907 MachineBranchProbabilityInfo *MBPI) const {
1908
1909 // We assume that block can have at most two successors.
1910 bool taken = false;
1911 MachineBasicBlock *Src = MI->getParent();
1912 MachineOperand *BrTarget = &MI->getOperand(1);
1913 MachineBasicBlock *Dst = BrTarget->getMBB();
1914
1915 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1916 if (Prediction >= BranchProbability(1,2))
1917 taken = true;
1918
1919 switch (MI->getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001920 case Hexagon::J2_jumpt:
1921 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1922 case Hexagon::J2_jumpf:
1923 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Jyotsna Verma1d297502013-05-02 15:39:30 +00001924
1925 default:
1926 llvm_unreachable("Unexpected jump instruction.");
1927 }
1928}
Jyotsna Verma84256432013-03-01 17:37:13 +00001929// Returns true if a particular operand is extendable for an instruction.
1930bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1931 unsigned short OperandNum) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001932 const uint64_t F = MI->getDesc().TSFlags;
1933
1934 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1935 == OperandNum;
1936}
1937
1938// Returns Operand Index for the constant extended instruction.
1939unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1940 const uint64_t F = MI->getDesc().TSFlags;
1941 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1942}
1943
1944// Returns the min value that doesn't need to be extended.
1945int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1946 const uint64_t F = MI->getDesc().TSFlags;
1947 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1948 & HexagonII::ExtentSignedMask;
1949 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1950 & HexagonII::ExtentBitsMask;
1951
1952 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001953 return -1U << (bits - 1);
Jyotsna Verma84256432013-03-01 17:37:13 +00001954 else
1955 return 0;
1956}
1957
1958// Returns the max value that doesn't need to be extended.
1959int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1960 const uint64_t F = MI->getDesc().TSFlags;
1961 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1962 & HexagonII::ExtentSignedMask;
1963 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1964 & HexagonII::ExtentBitsMask;
1965
1966 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001967 return ~(-1U << (bits - 1));
Jyotsna Verma84256432013-03-01 17:37:13 +00001968 else
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001969 return ~(-1U << bits);
Jyotsna Verma84256432013-03-01 17:37:13 +00001970}
1971
1972// Returns true if an instruction can be converted into a non-extended
1973// equivalent instruction.
1974bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1975
1976 short NonExtOpcode;
1977 // Check if the instruction has a register form that uses register in place
1978 // of the extended operand, if so return that as the non-extended form.
1979 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1980 return true;
1981
1982 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001983 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001984
1985 switch (getAddrMode(MI)) {
1986 case HexagonII::Absolute :
1987 // Load/store with absolute addressing mode can be converted into
1988 // base+offset mode.
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00001989 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00001990 break;
1991 case HexagonII::BaseImmOffset :
1992 // Load/store with base+offset addressing mode can be converted into
1993 // base+register offset addressing mode. However left shift operand should
1994 // be set to 0.
1995 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1996 break;
1997 default:
1998 return false;
1999 }
2000 if (NonExtOpcode < 0)
2001 return false;
2002 return true;
2003 }
2004 return false;
2005}
2006
2007// Returns opcode of the non-extended equivalent instruction.
2008short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
2009
2010 // Check if the instruction has a register form that uses register in place
2011 // of the extended operand, if so return that as the non-extended form.
2012 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
2013 if (NonExtOpcode >= 0)
2014 return NonExtOpcode;
2015
2016 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002017 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002018 switch (getAddrMode(MI)) {
2019 case HexagonII::Absolute :
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00002020 return Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002021 case HexagonII::BaseImmOffset :
2022 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
2023 default:
2024 return -1;
2025 }
2026 }
2027 return -1;
2028}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002029
2030bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00002031 return (Opcode == Hexagon::J2_jumpt) ||
2032 (Opcode == Hexagon::J2_jumpf) ||
2033 (Opcode == Hexagon::J2_jumptnewpt) ||
2034 (Opcode == Hexagon::J2_jumpfnewpt) ||
2035 (Opcode == Hexagon::J2_jumpt) ||
2036 (Opcode == Hexagon::J2_jumpf);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002037}
2038
Ahmed Bougachac88bf542015-06-11 19:30:37 +00002039bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00002040 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2041 return false;
2042 return !isPredicatedTrue(Cond[0].getImm());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002043}
Brendon Cahoondf43e682015-05-08 16:16:29 +00002044
2045bool HexagonInstrInfo::isEndLoopN(Opcode_t Opcode) const {
2046 return (Opcode == Hexagon::ENDLOOP0 ||
2047 Opcode == Hexagon::ENDLOOP1);
2048}
2049
Ahmed Bougachac88bf542015-06-11 19:30:37 +00002050bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Brendon Cahoondf43e682015-05-08 16:16:29 +00002051 unsigned &PredReg, unsigned &PredRegPos,
2052 unsigned &PredRegFlags) const {
2053 if (Cond.empty())
2054 return false;
2055 assert(Cond.size() == 2);
2056 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
2057 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
2058 return false;
2059 }
2060 PredReg = Cond[1].getReg();
2061 PredRegPos = 1;
2062 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
2063 PredRegFlags = 0;
2064 if (Cond[1].isImplicit())
2065 PredRegFlags = RegState::Implicit;
2066 if (Cond[1].isUndef())
2067 PredRegFlags |= RegState::Undef;
2068 return true;
2069}
2070