blob: 23849793445e16886a30ca6cd85bfe796313e76d [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)) {
186 MachineBasicBlock *NextBB =
187 std::next(MachineFunction::iterator(&MBB));
188 if (NewTBB == NextBB) {
189 ReverseBranchCondition(Cond);
190 RemoveBranch(MBB);
191 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000192 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000193 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000194 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
195 } else if (isEndLoopN(Cond[0].getImm())) {
196 int EndLoopOp = Cond[0].getImm();
197 assert(Cond[1].isMBB());
198 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
199 // Check for it, and change the BB target if needed.
200 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
201 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
202 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
203 Loop->getOperand(0).setMBB(TBB);
204 // Add the ENDLOOP after the finding the LOOP0.
205 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
206 } else if (isNewValueJump(Cond[0].getImm())) {
207 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
208 // New value jump
209 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
210 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
211 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
212 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
213 if (Cond[2].isReg()) {
214 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
215 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
216 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
217 } else if(Cond[2].isImm()) {
218 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
219 addImm(Cond[2].getImm()).addMBB(TBB);
220 } else
221 llvm_unreachable("Invalid condition for branching");
222 } else {
223 assert((Cond.size() == 2) && "Malformed cond vector");
224 const MachineOperand &RO = Cond[1];
225 unsigned Flags = getUndefRegState(RO.isUndef());
226 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000227 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000228 return 1;
229 }
230 assert((!Cond.empty()) &&
231 "Cond. cannot be empty when multiple branchings are required");
232 assert((!isNewValueJump(Cond[0].getImm())) &&
233 "NV-jump cannot be inserted with another branch");
234 // Special case for hardware loops. The condition is a basic block.
235 if (isEndLoopN(Cond[0].getImm())) {
236 int EndLoopOp = Cond[0].getImm();
237 assert(Cond[1].isMBB());
238 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
239 // Check for it, and change the BB target if needed.
240 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
241 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
242 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
243 Loop->getOperand(0).setMBB(TBB);
244 // Add the ENDLOOP after the finding the LOOP0.
245 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
246 } else {
247 const MachineOperand &RO = Cond[1];
248 unsigned Flags = getUndefRegState(RO.isUndef());
249 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
250 }
251 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000252
Brendon Cahoondf43e682015-05-08 16:16:29 +0000253 return 2;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000254}
255
256
Brendon Cahoondf43e682015-05-08 16:16:29 +0000257/// This function can analyze one/two way branching only and should (mostly) be
258/// called by target independent side.
259/// First entry is always the opcode of the branching instruction, except when
260/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
261/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
262/// e.g. Jump_c p will have
263/// Cond[0] = Jump_c
264/// Cond[1] = p
265/// HW-loop ENDLOOP:
266/// Cond[0] = ENDLOOP
267/// Cond[1] = MBB
268/// New value jump:
269/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
270/// Cond[1] = R
271/// Cond[2] = Imm
272/// @note Related function is \fn findInstrPredicate which fills in
273/// Cond. vector when a predicated instruction is passed to it.
274/// We follow same protocol in that case too.
275///
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000276bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
277 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000278 MachineBasicBlock *&FBB,
279 SmallVectorImpl<MachineOperand> &Cond,
280 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000281 TBB = nullptr;
282 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000283 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000284
285 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000286 MachineBasicBlock::instr_iterator I = MBB.instr_end();
287 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000288 return false;
289
290 // A basic block may looks like this:
291 //
292 // [ insn
293 // EH_LABEL
294 // insn
295 // insn
296 // insn
297 // EH_LABEL
298 // insn ]
299 //
300 // It has two succs but does not have a terminator
301 // Don't know how to handle it.
302 do {
303 --I;
304 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000305 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000306 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000307 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000308
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000309 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000310 --I;
311
312 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000313 if (I == MBB.instr_begin())
314 return false;
315 --I;
316 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000317
318 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
319 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000320 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000321 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000322 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
323 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
324 I->eraseFromParent();
325 I = MBB.instr_end();
326 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000327 return false;
328 --I;
329 }
330 if (!isUnpredicatedTerminator(I))
331 return false;
332
333 // Get the last instruction in the block.
334 MachineInstr *LastInst = I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000335 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000336 // Find one more terminator if present.
337 do {
338 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
339 if (!SecondLastInst)
340 SecondLastInst = I;
341 else
342 // This is a third branch.
343 return true;
344 }
345 if (I == MBB.instr_begin())
346 break;
347 --I;
348 } while(I);
349
350 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000351 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
352 // If the branch target is not a basic block, it could be a tail call.
353 // (It is, if the target is a function.)
354 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
355 return true;
356 if (SecLastOpcode == Hexagon::J2_jump &&
357 !SecondLastInst->getOperand(0).isMBB())
358 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000359
360 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000361 bool LastOpcodeHasNVJump = isNewValueJump(LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000362
363 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000364 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000365 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000366 TBB = LastInst->getOperand(0).getMBB();
367 return false;
368 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000369 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000370 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000371 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000372 Cond.push_back(LastInst->getOperand(0));
373 return false;
374 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000375 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000376 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000377 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000378 Cond.push_back(LastInst->getOperand(0));
379 return false;
380 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000381 // Only supporting rr/ri versions of new-value jumps.
382 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
383 TBB = LastInst->getOperand(2).getMBB();
384 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
385 Cond.push_back(LastInst->getOperand(0));
386 Cond.push_back(LastInst->getOperand(1));
387 return false;
388 }
389 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
390 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000391 // Otherwise, don't know what this is.
392 return true;
393 }
394
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000395 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000396 bool SecLastOpcodeHasNVJump = isNewValueJump(SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000397 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000399 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000400 Cond.push_back(SecondLastInst->getOperand(0));
401 FBB = LastInst->getOperand(0).getMBB();
402 return false;
403 }
404
Brendon Cahoondf43e682015-05-08 16:16:29 +0000405 // Only supporting rr/ri versions of new-value jumps.
406 if (SecLastOpcodeHasNVJump &&
407 (SecondLastInst->getNumExplicitOperands() == 3) &&
408 (LastOpcode == Hexagon::J2_jump)) {
409 TBB = SecondLastInst->getOperand(2).getMBB();
410 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
411 Cond.push_back(SecondLastInst->getOperand(0));
412 Cond.push_back(SecondLastInst->getOperand(1));
413 FBB = LastInst->getOperand(0).getMBB();
414 return false;
415 }
416
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
418 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000419 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000420 TBB = SecondLastInst->getOperand(0).getMBB();
421 I = LastInst;
422 if (AllowModify)
423 I->eraseFromParent();
424 return false;
425 }
426
Brendon Cahoondf43e682015-05-08 16:16:29 +0000427 // If the block ends with an ENDLOOP, and J2_jump, handle it.
428 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000429 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000430 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000431 Cond.push_back(SecondLastInst->getOperand(0));
432 FBB = LastInst->getOperand(0).getMBB();
433 return false;
434 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000435 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
436 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000437 // Otherwise, can't handle this.
438 return true;
439}
440
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000441unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000442 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000443 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000444 unsigned Count = 0;
445 while (I != MBB.begin()) {
446 --I;
447 if (I->isDebugValue())
448 continue;
449 // Only removing branches from end of MBB.
450 if (!I->isBranch())
451 return Count;
452 if (Count && (I->getOpcode() == Hexagon::J2_jump))
453 llvm_unreachable("Malformed basic block: unconditional branch not last");
454 MBB.erase(&MBB.back());
455 I = MBB.end();
456 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000457 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000458 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459}
460
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000461/// \brief For a comparison instruction, return the source registers in
462/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
463/// compares against in CmpValue. Return true if the comparison instruction
464/// can be analyzed.
465bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
466 unsigned &SrcReg, unsigned &SrcReg2,
467 int &Mask, int &Value) const {
468 unsigned Opc = MI->getOpcode();
469
470 // Set mask and the first source register.
471 switch (Opc) {
Colin LeMahieu902157c2014-11-25 18:20:52 +0000472 case Hexagon::C2_cmpeq:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000473 case Hexagon::C2_cmpeqp:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000474 case Hexagon::C2_cmpgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000475 case Hexagon::C2_cmpgtp:
476 case Hexagon::C2_cmpgtu:
477 case Hexagon::C2_cmpgtup:
478 case Hexagon::C4_cmpneq:
479 case Hexagon::C4_cmplte:
480 case Hexagon::C4_cmplteu:
481 case Hexagon::C2_cmpeqi:
482 case Hexagon::C2_cmpgti:
483 case Hexagon::C2_cmpgtui:
484 case Hexagon::C4_cmpneqi:
485 case Hexagon::C4_cmplteui:
486 case Hexagon::C4_cmpltei:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000487 SrcReg = MI->getOperand(1).getReg();
488 Mask = ~0;
489 break;
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000490 case Hexagon::A4_cmpbeq:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000491 case Hexagon::A4_cmpbgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000492 case Hexagon::A4_cmpbgtu:
493 case Hexagon::A4_cmpbeqi:
494 case Hexagon::A4_cmpbgti:
495 case Hexagon::A4_cmpbgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000496 SrcReg = MI->getOperand(1).getReg();
497 Mask = 0xFF;
498 break;
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000499 case Hexagon::A4_cmpheq:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000500 case Hexagon::A4_cmphgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000501 case Hexagon::A4_cmphgtu:
502 case Hexagon::A4_cmpheqi:
503 case Hexagon::A4_cmphgti:
504 case Hexagon::A4_cmphgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000505 SrcReg = MI->getOperand(1).getReg();
506 Mask = 0xFFFF;
507 break;
508 }
509
510 // Set the value/second source register.
511 switch (Opc) {
Colin LeMahieu902157c2014-11-25 18:20:52 +0000512 case Hexagon::C2_cmpeq:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000513 case Hexagon::C2_cmpeqp:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000514 case Hexagon::C2_cmpgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000515 case Hexagon::C2_cmpgtp:
516 case Hexagon::C2_cmpgtu:
517 case Hexagon::C2_cmpgtup:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000518 case Hexagon::A4_cmpbeq:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000519 case Hexagon::A4_cmpbgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000520 case Hexagon::A4_cmpbgtu:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000521 case Hexagon::A4_cmpheq:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000522 case Hexagon::A4_cmphgt:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000523 case Hexagon::A4_cmphgtu:
524 case Hexagon::C4_cmpneq:
525 case Hexagon::C4_cmplte:
526 case Hexagon::C4_cmplteu:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000527 SrcReg2 = MI->getOperand(2).getReg();
528 return true;
529
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000530 case Hexagon::C2_cmpeqi:
531 case Hexagon::C2_cmpgtui:
532 case Hexagon::C2_cmpgti:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000533 case Hexagon::C4_cmpneqi:
534 case Hexagon::C4_cmplteui:
535 case Hexagon::C4_cmpltei:
Colin LeMahieufa947902015-01-14 16:49:12 +0000536 case Hexagon::A4_cmpbeqi:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000537 case Hexagon::A4_cmpbgti:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000538 case Hexagon::A4_cmpbgtui:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000539 case Hexagon::A4_cmpheqi:
Brendon Cahoondf43e682015-05-08 16:16:29 +0000540 case Hexagon::A4_cmphgti:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000541 case Hexagon::A4_cmphgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000542 SrcReg2 = 0;
543 Value = MI->getOperand(2).getImm();
544 return true;
545 }
546
547 return false;
548}
549
550
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000551void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
552 MachineBasicBlock::iterator I, DebugLoc DL,
553 unsigned DestReg, unsigned SrcReg,
554 bool KillSrc) const {
555 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000556 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000557 return;
558 }
559 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000560 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561 return;
562 }
563 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
564 // Map Pd = Ps to Pd = or(Ps, Ps).
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000565 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000566 DestReg).addReg(SrcReg).addReg(SrcReg);
567 return;
568 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000569 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
570 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000571 // We can have an overlap between single and double reg: r1:0 = r0.
572 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
573 // r1:0 = r0
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000574 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000575 Hexagon::subreg_hireg))).addImm(0);
576 } else {
577 // r1:0 = r1 or no overlap.
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000578 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000579 Hexagon::subreg_loreg))).addReg(SrcReg);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000580 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000581 Hexagon::subreg_hireg))).addImm(0);
582 }
583 return;
584 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000585 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000586 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000587 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000588 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000589 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000590 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
591 Hexagon::IntRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000592 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000593 addReg(SrcReg, getKillRegState(KillSrc));
594 return;
595 }
596 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
597 Hexagon::PredRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000598 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000599 addReg(SrcReg, getKillRegState(KillSrc));
600 return;
601 }
Sirish Pande30804c22012-02-15 18:52:27 +0000602
603 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000604}
605
606
607void HexagonInstrInfo::
608storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
609 unsigned SrcReg, bool isKill, int FI,
610 const TargetRegisterClass *RC,
611 const TargetRegisterInfo *TRI) const {
612
613 DebugLoc DL = MBB.findDebugLoc(I);
614 MachineFunction &MF = *MBB.getParent();
615 MachineFrameInfo &MFI = *MF.getFrameInfo();
616 unsigned Align = MFI.getObjectAlignment(FI);
617
Alex Lorenze40c8a22015-08-11 23:09:45 +0000618 MachineMemOperand *MMO = MF.getMachineMemOperand(
619 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
620 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000621
Craig Topperc7242e02012-04-20 07:30:17 +0000622 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000623 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000624 .addFrameIndex(FI).addImm(0)
625 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000626 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000627 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000628 .addFrameIndex(FI).addImm(0)
629 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000630 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000631 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
632 .addFrameIndex(FI).addImm(0)
633 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
634 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000635 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000636 }
637}
638
639
640void HexagonInstrInfo::storeRegToAddr(
641 MachineFunction &MF, unsigned SrcReg,
642 bool isKill,
643 SmallVectorImpl<MachineOperand> &Addr,
644 const TargetRegisterClass *RC,
645 SmallVectorImpl<MachineInstr*> &NewMIs) const
646{
Craig Toppere55c5562012-02-07 02:50:20 +0000647 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000648}
649
650
651void HexagonInstrInfo::
652loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
653 unsigned DestReg, int FI,
654 const TargetRegisterClass *RC,
655 const TargetRegisterInfo *TRI) const {
656 DebugLoc DL = MBB.findDebugLoc(I);
657 MachineFunction &MF = *MBB.getParent();
658 MachineFrameInfo &MFI = *MF.getFrameInfo();
659 unsigned Align = MFI.getObjectAlignment(FI);
660
Alex Lorenze40c8a22015-08-11 23:09:45 +0000661 MachineMemOperand *MMO = MF.getMachineMemOperand(
662 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
663 MFI.getObjectSize(FI), Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000664 if (RC == &Hexagon::IntRegsRegClass) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000665 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000666 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000667 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000668 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000669 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000670 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000671 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
672 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
673 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000674 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000675 }
676}
677
678
679void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
680 SmallVectorImpl<MachineOperand> &Addr,
681 const TargetRegisterClass *RC,
682 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Craig Toppere55c5562012-02-07 02:50:20 +0000683 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000684}
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000685bool
686HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000687 const HexagonRegisterInfo &HRI = getRegisterInfo();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000688 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000689 MachineBasicBlock &MBB = *MI->getParent();
690 DebugLoc DL = MI->getDebugLoc();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000691 unsigned Opc = MI->getOpcode();
692
693 switch (Opc) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000694 case Hexagon::ALIGNA:
695 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI->getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000696 .addReg(HRI.getFrameRegister())
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000697 .addImm(-MI->getOperand(1).getImm());
698 MBB.erase(MI);
699 return true;
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000700 case Hexagon::TFR_PdTrue: {
701 unsigned Reg = MI->getOperand(0).getReg();
702 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
703 .addReg(Reg, RegState::Undef)
704 .addReg(Reg, RegState::Undef);
705 MBB.erase(MI);
706 return true;
707 }
708 case Hexagon::TFR_PdFalse: {
709 unsigned Reg = MI->getOperand(0).getReg();
710 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
711 .addReg(Reg, RegState::Undef)
712 .addReg(Reg, RegState::Undef);
713 MBB.erase(MI);
714 return true;
715 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000716 case Hexagon::VMULW: {
717 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
718 unsigned DstReg = MI->getOperand(0).getReg();
719 unsigned Src1Reg = MI->getOperand(1).getReg();
720 unsigned Src2Reg = MI->getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000721 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
722 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
723 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
724 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000725 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000726 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000727 .addReg(Src2SubHi);
728 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000729 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000730 .addReg(Src2SubLo);
731 MBB.erase(MI);
732 MRI.clearKillFlags(Src1SubHi);
733 MRI.clearKillFlags(Src1SubLo);
734 MRI.clearKillFlags(Src2SubHi);
735 MRI.clearKillFlags(Src2SubLo);
736 return true;
737 }
738 case Hexagon::VMULW_ACC: {
739 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
740 unsigned DstReg = MI->getOperand(0).getReg();
741 unsigned Src1Reg = MI->getOperand(1).getReg();
742 unsigned Src2Reg = MI->getOperand(2).getReg();
743 unsigned Src3Reg = MI->getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000744 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
745 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
746 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
747 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
748 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
749 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000750 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000751 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000752 .addReg(Src2SubHi).addReg(Src3SubHi);
753 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000754 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000755 .addReg(Src2SubLo).addReg(Src3SubLo);
756 MBB.erase(MI);
757 MRI.clearKillFlags(Src1SubHi);
758 MRI.clearKillFlags(Src1SubLo);
759 MRI.clearKillFlags(Src2SubHi);
760 MRI.clearKillFlags(Src2SubLo);
761 MRI.clearKillFlags(Src3SubHi);
762 MRI.clearKillFlags(Src3SubLo);
763 return true;
764 }
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000765 case Hexagon::MUX64_rr: {
766 const MachineOperand &Op0 = MI->getOperand(0);
767 const MachineOperand &Op1 = MI->getOperand(1);
768 const MachineOperand &Op2 = MI->getOperand(2);
769 const MachineOperand &Op3 = MI->getOperand(3);
770 unsigned Rd = Op0.getReg();
771 unsigned Pu = Op1.getReg();
772 unsigned Rs = Op2.getReg();
773 unsigned Rt = Op3.getReg();
774 DebugLoc DL = MI->getDebugLoc();
775 unsigned K1 = getKillRegState(Op1.isKill());
776 unsigned K2 = getKillRegState(Op2.isKill());
777 unsigned K3 = getKillRegState(Op3.isKill());
778 if (Rd != Rs)
779 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
780 .addReg(Pu, (Rd == Rt) ? K1 : 0)
781 .addReg(Rs, K2);
782 if (Rd != Rt)
783 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
784 .addReg(Pu, K1)
785 .addReg(Rt, K3);
786 MBB.erase(MI);
787 return true;
788 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000789 case Hexagon::TCRETURNi:
790 MI->setDesc(get(Hexagon::J2_jump));
791 return true;
792 case Hexagon::TCRETURNr:
793 MI->setDesc(get(Hexagon::J2_jumpr));
794 return true;
795 }
796
797 return false;
798}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000799
Keno Fischere70b31f2015-06-08 20:09:58 +0000800MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(
801 MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
802 MachineBasicBlock::iterator InsertPt, int FI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000803 // Hexagon_TODO: Implement.
Craig Topper062a2ba2014-04-25 05:30:21 +0000804 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000805}
806
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000807unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
808
809 MachineRegisterInfo &RegInfo = MF->getRegInfo();
810 const TargetRegisterClass *TRC;
Sirish Pande69295b82012-05-10 20:20:25 +0000811 if (VT == MVT::i1) {
Craig Topperc7242e02012-04-20 07:30:17 +0000812 TRC = &Hexagon::PredRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000813 } else if (VT == MVT::i32 || VT == MVT::f32) {
Craig Topperc7242e02012-04-20 07:30:17 +0000814 TRC = &Hexagon::IntRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000815 } else if (VT == MVT::i64 || VT == MVT::f64) {
Craig Topperc7242e02012-04-20 07:30:17 +0000816 TRC = &Hexagon::DoubleRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000817 } else {
Benjamin Kramerb6684012011-12-27 11:41:05 +0000818 llvm_unreachable("Cannot handle this register class");
Sirish Pande69295b82012-05-10 20:20:25 +0000819 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000820
821 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
822 return NewReg;
823}
824
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000825bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000826 const MCInstrDesc &MID = MI->getDesc();
827 const uint64_t F = MID.TSFlags;
828 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
829 return true;
830
831 // TODO: This is largely obsolete now. Will need to be removed
832 // in consecutive patches.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000833 switch(MI->getOpcode()) {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000834 // TFR_FI Remains a special case.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000835 case Hexagon::TFR_FI:
836 return true;
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000837 default:
838 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000839 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000840 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000841}
842
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000843// This returns true in two cases:
844// - The OP code itself indicates that this is an extended instruction.
845// - One of MOs has been marked with HMOTF_ConstExtended flag.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000846bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000847 // First check if this is permanently extended op code.
848 const uint64_t F = MI->getDesc().TSFlags;
849 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
850 return true;
851 // Use MO operand flags to determine if one of MI's operands
852 // has HMOTF_ConstExtended flag set.
853 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
854 E = MI->operands_end(); I != E; ++I) {
855 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Sirish Pande69295b82012-05-10 20:20:25 +0000856 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000857 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000858 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000859}
860
Jyotsna Verma84c47102013-05-06 18:49:23 +0000861bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
862 return MI->getDesc().isBranch();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000863}
864
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000865bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
866 if (isNewValueJump(MI))
867 return true;
868
869 if (isNewValueStore(MI))
870 return true;
871
872 return false;
873}
874
Brendon Cahoondf43e682015-05-08 16:16:29 +0000875bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
876 const uint64_t F = MI->getDesc().TSFlags;
877 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
878}
879
880bool HexagonInstrInfo::isNewValue(Opcode_t Opcode) const {
881 const uint64_t F = get(Opcode).TSFlags;
882 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
883}
884
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000885bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
886 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
887}
Andrew Trickd06df962012-02-01 22:13:57 +0000888
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000889bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
890 bool isPred = MI->getDesc().isPredicable();
891
892 if (!isPred)
893 return false;
894
895 const int Opc = MI->getOpcode();
896
897 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000898 case Hexagon::A2_tfrsi:
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000899 return (isOperandExtended(MI, 1) && isConstExtended(MI)) || isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000900
Colin LeMahieubda31b42014-12-29 20:44:51 +0000901 case Hexagon::S2_storerd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000902 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000903
Colin LeMahieubda31b42014-12-29 20:44:51 +0000904 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000905 case Hexagon::S2_storerinew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000906 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000907
Colin LeMahieubda31b42014-12-29 20:44:51 +0000908 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000909 case Hexagon::S2_storerhnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000910 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000911
Colin LeMahieubda31b42014-12-29 20:44:51 +0000912 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000913 case Hexagon::S2_storerbnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000914 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000915
Colin LeMahieu947cd702014-12-23 20:44:59 +0000916 case Hexagon::L2_loadrd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000917 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000918
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000919 case Hexagon::L2_loadri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000920 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000921
Colin LeMahieu8e39cad2014-12-23 17:25:57 +0000922 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +0000923 case Hexagon::L2_loadruh_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000924 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000925
Colin LeMahieu4b1eac42014-12-22 21:40:43 +0000926 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +0000927 case Hexagon::L2_loadrub_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000928 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000929
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000930 case Hexagon::L2_loadrd_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000931 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000932
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000933 case Hexagon::L2_loadri_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000934 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000935
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000936 case Hexagon::L2_loadrh_pi:
937 case Hexagon::L2_loadruh_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000938 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000939
Colin LeMahieu96976a12014-12-26 18:57:13 +0000940 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +0000941 case Hexagon::L2_loadrub_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000942 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000943
Colin LeMahieu2bad4a72014-12-30 21:01:38 +0000944 case Hexagon::S4_storeirb_io:
945 case Hexagon::S4_storeirh_io:
946 case Hexagon::S4_storeiri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000947 return (isUInt<6>(MI->getOperand(1).getImm()) &&
948 isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000949
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000950 case Hexagon::A2_addi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000951 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000952
Colin LeMahieu3b3197e2014-11-24 17:44:19 +0000953 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +0000954 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +0000955 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +0000956 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +0000957 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +0000958 case Hexagon::A2_zxth:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +0000959 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000960 }
961
962 return true;
963}
964
Sirish Pande8bb97452012-05-12 05:54:15 +0000965// This function performs the following inversiones:
966//
967// cPt ---> cNotPt
968// cNotPt ---> cPt
969//
Sirish Pande30804c22012-02-15 18:52:27 +0000970unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
Jyotsna Verma84c47102013-05-06 18:49:23 +0000971 int InvPredOpcode;
972 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
973 : Hexagon::getTruePredOpcode(Opc);
974 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
975 return InvPredOpcode;
976
Sirish Pande30804c22012-02-15 18:52:27 +0000977 switch(Opc) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000978 default: llvm_unreachable("Unexpected predicated instruction");
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000979 case Hexagon::C2_ccombinewt:
980 return Hexagon::C2_ccombinewf;
981 case Hexagon::C2_ccombinewf:
982 return Hexagon::C2_ccombinewt;
Sirish Pande30804c22012-02-15 18:52:27 +0000983
Jyotsna Verma978e9722013-05-09 18:25:44 +0000984 // Dealloc_return.
Colin LeMahieu14455532015-01-06 16:15:15 +0000985 case Hexagon::L4_return_t:
986 return Hexagon::L4_return_f;
987 case Hexagon::L4_return_f:
988 return Hexagon::L4_return_t;
Sirish Pande30804c22012-02-15 18:52:27 +0000989 }
990}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000991
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000992// New Value Store instructions.
993bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
994 const uint64_t F = MI->getDesc().TSFlags;
995
996 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
997}
998
999bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
1000 const uint64_t F = get(Opcode).TSFlags;
1001
1002 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
1003}
Andrew Trickd06df962012-02-01 22:13:57 +00001004
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +00001005int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
Pranav Bhandarkar34b60182012-11-01 19:13:23 +00001006 enum Hexagon::PredSense inPredSense;
1007 inPredSense = invertPredicate ? Hexagon::PredSense_false :
1008 Hexagon::PredSense_true;
1009 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
1010 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
1011 return CondOpcode;
1012
1013 // This switch case will be removed once all the instructions have been
1014 // modified to use relation maps.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001015 switch(Opc) {
Sirish Pande69295b82012-05-10 20:20:25 +00001016 case Hexagon::TFRI_f:
1017 return !invertPredicate ? Hexagon::TFRI_cPt_f :
1018 Hexagon::TFRI_cNotPt_f;
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001019 case Hexagon::A2_combinew:
1020 return !invertPredicate ? Hexagon::C2_ccombinewt :
1021 Hexagon::C2_ccombinewf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001022
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001023 // DEALLOC_RETURN.
Colin LeMahieu14455532015-01-06 16:15:15 +00001024 case Hexagon::L4_return:
1025 return !invertPredicate ? Hexagon::L4_return_t:
1026 Hexagon::L4_return_f;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001027 }
Benjamin Kramerb6684012011-12-27 11:41:05 +00001028 llvm_unreachable("Unexpected predicable instruction");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001029}
1030
1031
1032bool HexagonInstrInfo::
1033PredicateInstruction(MachineInstr *MI,
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001034 ArrayRef<MachineOperand> Cond) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00001035 if (Cond.empty() || isEndLoopN(Cond[0].getImm())) {
1036 DEBUG(dbgs() << "\nCannot predicate:"; MI->dump(););
1037 return false;
1038 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001039 int Opc = MI->getOpcode();
1040 assert (isPredicable(MI) && "Expected predicable instruction");
Brendon Cahoondf43e682015-05-08 16:16:29 +00001041 bool invertJump = predOpcodeHasNot(Cond);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001042
Brendon Cahoondf43e682015-05-08 16:16:29 +00001043 // We have to predicate MI "in place", i.e. after this function returns,
1044 // MI will need to be transformed into a predicated form. To avoid com-
1045 // plicated manipulations with the operands (handling tied operands,
1046 // etc.), build a new temporary instruction, then overwrite MI with it.
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001047
Brendon Cahoondf43e682015-05-08 16:16:29 +00001048 MachineBasicBlock &B = *MI->getParent();
1049 DebugLoc DL = MI->getDebugLoc();
1050 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1051 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
1052 unsigned NOp = 0, NumOps = MI->getNumOperands();
1053 while (NOp < NumOps) {
1054 MachineOperand &Op = MI->getOperand(NOp);
1055 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1056 break;
1057 T.addOperand(Op);
1058 NOp++;
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001059 }
1060
Brendon Cahoondf43e682015-05-08 16:16:29 +00001061 unsigned PredReg, PredRegPos, PredRegFlags;
1062 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1063 (void)GotPredReg;
1064 assert(GotPredReg);
1065 T.addReg(PredReg, PredRegFlags);
1066 while (NOp < NumOps)
1067 T.addOperand(MI->getOperand(NOp++));
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001068
Brendon Cahoondf43e682015-05-08 16:16:29 +00001069 MI->setDesc(get(PredOpc));
1070 while (unsigned n = MI->getNumOperands())
1071 MI->RemoveOperand(n-1);
1072 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
1073 MI->addOperand(T->getOperand(i));
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001074
Brendon Cahoondf43e682015-05-08 16:16:29 +00001075 MachineBasicBlock::instr_iterator TI = &*T;
1076 B.erase(TI);
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001077
Brendon Cahoondf43e682015-05-08 16:16:29 +00001078 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1079 MRI.clearKillFlags(PredReg);
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001080
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001081 return true;
1082}
1083
1084
1085bool
1086HexagonInstrInfo::
1087isProfitableToIfCvt(MachineBasicBlock &MBB,
Kay Tiong Khoof2949212012-06-13 15:53:04 +00001088 unsigned NumCycles,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001089 unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001090 BranchProbability Probability) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001091 return true;
1092}
1093
1094
1095bool
1096HexagonInstrInfo::
1097isProfitableToIfCvt(MachineBasicBlock &TMBB,
1098 unsigned NumTCycles,
1099 unsigned ExtraTCycles,
1100 MachineBasicBlock &FMBB,
1101 unsigned NumFCycles,
1102 unsigned ExtraFCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001103 BranchProbability Probability) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001104 return true;
1105}
1106
Jyotsna Verma84c47102013-05-06 18:49:23 +00001107// Returns true if an instruction is predicated irrespective of the predicate
1108// sense. For example, all of the following will return true.
1109// if (p0) R1 = add(R2, R3)
1110// if (!p0) R1 = add(R2, R3)
1111// if (p0.new) R1 = add(R2, R3)
1112// if (!p0.new) R1 = add(R2, R3)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001113bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoon6f358372012-02-08 18:25:47 +00001114 const uint64_t F = MI->getDesc().TSFlags;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001115
Brendon Cahoon6f358372012-02-08 18:25:47 +00001116 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001117}
1118
Jyotsna Verma84c47102013-05-06 18:49:23 +00001119bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
1120 const uint64_t F = get(Opcode).TSFlags;
1121
1122 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1123}
1124
1125bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
1126 const uint64_t F = MI->getDesc().TSFlags;
1127
1128 assert(isPredicated(MI));
1129 return (!((F >> HexagonII::PredicatedFalsePos) &
1130 HexagonII::PredicatedFalseMask));
1131}
1132
1133bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
1134 const uint64_t F = get(Opcode).TSFlags;
1135
1136 // Make sure that the instruction is predicated.
1137 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1138 return (!((F >> HexagonII::PredicatedFalsePos) &
1139 HexagonII::PredicatedFalseMask));
1140}
1141
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001142bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1143 const uint64_t F = MI->getDesc().TSFlags;
1144
1145 assert(isPredicated(MI));
1146 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1147}
1148
Jyotsna Verma84c47102013-05-06 18:49:23 +00001149bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1150 const uint64_t F = get(Opcode).TSFlags;
1151
1152 assert(isPredicated(Opcode));
1153 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1154}
1155
Jyotsna Verma438cec52013-05-10 20:58:11 +00001156// Returns true, if a ST insn can be promoted to a new-value store.
1157bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
Jyotsna Verma438cec52013-05-10 20:58:11 +00001158 const uint64_t F = MI->getDesc().TSFlags;
1159
1160 return ((F >> HexagonII::mayNVStorePos) &
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001161 HexagonII::mayNVStoreMask);
Jyotsna Verma438cec52013-05-10 20:58:11 +00001162}
1163
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001164bool
1165HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1166 std::vector<MachineOperand> &Pred) const {
1167 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1168 MachineOperand MO = MI->getOperand(oper);
1169 if (MO.isReg() && MO.isDef()) {
1170 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
Craig Topperc7242e02012-04-20 07:30:17 +00001171 if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001172 Pred.push_back(MO);
1173 return true;
1174 }
1175 }
1176 }
1177 return false;
1178}
1179
1180
1181bool
1182HexagonInstrInfo::
Ahmed Bougachac88bf542015-06-11 19:30:37 +00001183SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1184 ArrayRef<MachineOperand> Pred2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001185 // TODO: Fix this
1186 return false;
1187}
1188
1189
1190//
1191// We indicate that we want to reverse the branch by
Brendon Cahoondf43e682015-05-08 16:16:29 +00001192// inserting the reversed branching opcode.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001193//
Brendon Cahoondf43e682015-05-08 16:16:29 +00001194bool HexagonInstrInfo::ReverseBranchCondition(
1195 SmallVectorImpl<MachineOperand> &Cond) const {
1196 if (Cond.empty())
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001197 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001198 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1199 Opcode_t opcode = Cond[0].getImm();
1200 //unsigned temp;
1201 assert(get(opcode).isBranch() && "Should be a branching condition.");
1202 if (isEndLoopN(opcode))
1203 return true;
1204 Opcode_t NewOpcode = getInvertedPredicatedOpcode(opcode);
1205 Cond[0].setImm(NewOpcode);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001206 return false;
1207}
1208
1209
1210bool HexagonInstrInfo::
1211isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
Cong Houc536bd92015-09-10 23:10:42 +00001212 BranchProbability Probability) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001213 return (NumInstrs <= 4);
1214}
1215
1216bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1217 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001218 default: return false;
Colin LeMahieu14455532015-01-06 16:15:15 +00001219 case Hexagon::L4_return:
1220 case Hexagon::L4_return_t:
1221 case Hexagon::L4_return_f:
1222 case Hexagon::L4_return_tnew_pnt:
1223 case Hexagon::L4_return_fnew_pnt:
1224 case Hexagon::L4_return_tnew_pt:
1225 case Hexagon::L4_return_fnew_pt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001226 return true;
1227 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001228}
1229
1230
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001231bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
1232 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001233 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001234 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001235 // inserted to calculate the final address. Due to this reason, the function
1236 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00001237 // We used to assert if the offset was not properly aligned, however,
1238 // there are cases where a misaligned pointer recast can cause this
1239 // problem, and we need to allow for it. The front end warns of such
1240 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001241
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001242 switch (Opcode) {
1243 case Hexagon::J2_loop0i:
1244 case Hexagon::J2_loop1i:
1245 return isUInt<10>(Offset);
1246 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001247
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001248 if (Extend)
1249 return true;
1250
1251 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001252 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001253 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001254 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1255 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1256
Colin LeMahieu947cd702014-12-23 20:44:59 +00001257 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001258 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001259 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1260 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1261
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001262 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001263 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001264 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001265 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1266 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1267
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001268 case Hexagon::L2_loadrb_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001269 case Hexagon::S2_storerb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001270 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001271 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1272 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1273
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001274 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001275 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1276 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1277
Colin LeMahieudacf0572015-01-05 21:36:38 +00001278 case Hexagon::L4_iadd_memopw_io:
1279 case Hexagon::L4_isub_memopw_io:
1280 case Hexagon::L4_add_memopw_io:
1281 case Hexagon::L4_sub_memopw_io:
1282 case Hexagon::L4_and_memopw_io:
1283 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001284 return (0 <= Offset && Offset <= 255);
1285
Colin LeMahieudacf0572015-01-05 21:36:38 +00001286 case Hexagon::L4_iadd_memoph_io:
1287 case Hexagon::L4_isub_memoph_io:
1288 case Hexagon::L4_add_memoph_io:
1289 case Hexagon::L4_sub_memoph_io:
1290 case Hexagon::L4_and_memoph_io:
1291 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001292 return (0 <= Offset && Offset <= 127);
1293
Colin LeMahieudacf0572015-01-05 21:36:38 +00001294 case Hexagon::L4_iadd_memopb_io:
1295 case Hexagon::L4_isub_memopb_io:
1296 case Hexagon::L4_add_memopb_io:
1297 case Hexagon::L4_sub_memopb_io:
1298 case Hexagon::L4_and_memopb_io:
1299 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001300 return (0 <= Offset && Offset <= 63);
1301
1302 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1303 // any size. Later pass knows how to handle it.
1304 case Hexagon::STriw_pred:
1305 case Hexagon::LDriw_pred:
1306 return true;
1307
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00001308 case Hexagon::TFR_FI:
1309 case Hexagon::TFR_FIA:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001310 case Hexagon::INLINEASM:
1311 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001312
1313 case Hexagon::L2_ploadrbt_io:
1314 case Hexagon::L2_ploadrbf_io:
1315 case Hexagon::L2_ploadrubt_io:
1316 case Hexagon::L2_ploadrubf_io:
1317 case Hexagon::S2_pstorerbt_io:
1318 case Hexagon::S2_pstorerbf_io:
1319 case Hexagon::S4_storeirb_io:
1320 case Hexagon::S4_storeirbt_io:
1321 case Hexagon::S4_storeirbf_io:
1322 return isUInt<6>(Offset);
1323
1324 case Hexagon::L2_ploadrht_io:
1325 case Hexagon::L2_ploadrhf_io:
1326 case Hexagon::L2_ploadruht_io:
1327 case Hexagon::L2_ploadruhf_io:
1328 case Hexagon::S2_pstorerht_io:
1329 case Hexagon::S2_pstorerhf_io:
1330 case Hexagon::S4_storeirh_io:
1331 case Hexagon::S4_storeirht_io:
1332 case Hexagon::S4_storeirhf_io:
1333 return isShiftedUInt<6,1>(Offset);
1334
1335 case Hexagon::L2_ploadrit_io:
1336 case Hexagon::L2_ploadrif_io:
1337 case Hexagon::S2_pstorerit_io:
1338 case Hexagon::S2_pstorerif_io:
1339 case Hexagon::S4_storeiri_io:
1340 case Hexagon::S4_storeirit_io:
1341 case Hexagon::S4_storeirif_io:
1342 return isShiftedUInt<6,2>(Offset);
1343
1344 case Hexagon::L2_ploadrdt_io:
1345 case Hexagon::L2_ploadrdf_io:
1346 case Hexagon::S2_pstorerdt_io:
1347 case Hexagon::S2_pstorerdf_io:
1348 return isShiftedUInt<6,3>(Offset);
1349 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001350
Benjamin Kramerb6684012011-12-27 11:41:05 +00001351 llvm_unreachable("No offset range is defined for this opcode. "
1352 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001353}
1354
1355
1356//
1357// Check if the Offset is a valid auto-inc imm by Load/Store Type.
1358//
1359bool HexagonInstrInfo::
1360isValidAutoIncImm(const EVT VT, const int Offset) const {
1361
1362 if (VT == MVT::i64) {
1363 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1364 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1365 (Offset & 0x7) == 0);
1366 }
1367 if (VT == MVT::i32) {
1368 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1369 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1370 (Offset & 0x3) == 0);
1371 }
1372 if (VT == MVT::i16) {
1373 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1374 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1375 (Offset & 0x1) == 0);
1376 }
1377 if (VT == MVT::i8) {
1378 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1379 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1380 }
Craig Toppere55c5562012-02-07 02:50:20 +00001381 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001382}
1383
1384
1385bool HexagonInstrInfo::
1386isMemOp(const MachineInstr *MI) const {
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001387// return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1388
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001389 switch (MI->getOpcode())
1390 {
Colin LeMahieudacf0572015-01-05 21:36:38 +00001391 default: return false;
1392 case Hexagon::L4_iadd_memopw_io:
1393 case Hexagon::L4_isub_memopw_io:
1394 case Hexagon::L4_add_memopw_io:
1395 case Hexagon::L4_sub_memopw_io:
1396 case Hexagon::L4_and_memopw_io:
1397 case Hexagon::L4_or_memopw_io:
1398 case Hexagon::L4_iadd_memoph_io:
1399 case Hexagon::L4_isub_memoph_io:
1400 case Hexagon::L4_add_memoph_io:
1401 case Hexagon::L4_sub_memoph_io:
1402 case Hexagon::L4_and_memoph_io:
1403 case Hexagon::L4_or_memoph_io:
1404 case Hexagon::L4_iadd_memopb_io:
1405 case Hexagon::L4_isub_memopb_io:
1406 case Hexagon::L4_add_memopb_io:
1407 case Hexagon::L4_sub_memopb_io:
1408 case Hexagon::L4_and_memopb_io:
1409 case Hexagon::L4_or_memopb_io:
1410 case Hexagon::L4_ior_memopb_io:
1411 case Hexagon::L4_ior_memoph_io:
1412 case Hexagon::L4_ior_memopw_io:
1413 case Hexagon::L4_iand_memopb_io:
1414 case Hexagon::L4_iand_memoph_io:
1415 case Hexagon::L4_iand_memopw_io:
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001416 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001417 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001418 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001419}
1420
1421
1422bool HexagonInstrInfo::
1423isSpillPredRegOp(const MachineInstr *MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001424 switch (MI->getOpcode()) {
1425 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001426 case Hexagon::STriw_pred :
1427 case Hexagon::LDriw_pred :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001428 return true;
Sirish Pande2c7bf002012-04-23 17:49:28 +00001429 }
Sirish Pande4bd20c52012-05-12 05:10:30 +00001430}
1431
1432bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1433 switch (MI->getOpcode()) {
Sirish Pande8bb97452012-05-12 05:54:15 +00001434 default: return false;
Colin LeMahieu902157c2014-11-25 18:20:52 +00001435 case Hexagon::C2_cmpeq:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001436 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001437 case Hexagon::C2_cmpgt:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001438 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001439 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001440 case Hexagon::C2_cmpgtui:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001441 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +00001442 }
Sirish Pande2c7bf002012-04-23 17:49:28 +00001443}
1444
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001445bool HexagonInstrInfo::
1446isConditionalTransfer (const MachineInstr *MI) const {
1447 switch (MI->getOpcode()) {
1448 default: return false;
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001449 case Hexagon::A2_tfrt:
1450 case Hexagon::A2_tfrf:
1451 case Hexagon::C2_cmoveit:
1452 case Hexagon::C2_cmoveif:
1453 case Hexagon::A2_tfrtnew:
1454 case Hexagon::A2_tfrfnew:
1455 case Hexagon::C2_cmovenewit:
1456 case Hexagon::C2_cmovenewif:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001457 return true;
1458 }
1459}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001460
1461bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001462 switch (MI->getOpcode())
1463 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001464 default: return false;
Colin LeMahieuefa74e02014-11-18 20:28:11 +00001465 case Hexagon::A2_paddf:
1466 case Hexagon::A2_paddfnew:
1467 case Hexagon::A2_paddt:
1468 case Hexagon::A2_paddtnew:
Colin LeMahieu44fd1c82014-11-18 22:45:47 +00001469 case Hexagon::A2_pandf:
1470 case Hexagon::A2_pandfnew:
1471 case Hexagon::A2_pandt:
1472 case Hexagon::A2_pandtnew:
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001473 case Hexagon::A4_paslhf:
1474 case Hexagon::A4_paslhfnew:
1475 case Hexagon::A4_paslht:
1476 case Hexagon::A4_paslhtnew:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001477 case Hexagon::A4_pasrhf:
1478 case Hexagon::A4_pasrhfnew:
1479 case Hexagon::A4_pasrht:
1480 case Hexagon::A4_pasrhtnew:
Colin LeMahieu21866542014-11-19 22:58:04 +00001481 case Hexagon::A2_porf:
1482 case Hexagon::A2_porfnew:
1483 case Hexagon::A2_port:
1484 case Hexagon::A2_portnew:
Colin LeMahieue88447d2014-11-21 21:19:18 +00001485 case Hexagon::A2_psubf:
1486 case Hexagon::A2_psubfnew:
1487 case Hexagon::A2_psubt:
1488 case Hexagon::A2_psubtnew:
Colin LeMahieuac006432014-11-19 23:22:23 +00001489 case Hexagon::A2_pxorf:
1490 case Hexagon::A2_pxorfnew:
1491 case Hexagon::A2_pxort:
1492 case Hexagon::A2_pxortnew:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001493 case Hexagon::A4_psxthf:
1494 case Hexagon::A4_psxthfnew:
1495 case Hexagon::A4_psxtht:
1496 case Hexagon::A4_psxthtnew:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001497 case Hexagon::A4_psxtbf:
1498 case Hexagon::A4_psxtbfnew:
1499 case Hexagon::A4_psxtbt:
1500 case Hexagon::A4_psxtbtnew:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001501 case Hexagon::A4_pzxtbf:
1502 case Hexagon::A4_pzxtbfnew:
1503 case Hexagon::A4_pzxtbt:
1504 case Hexagon::A4_pzxtbtnew:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001505 case Hexagon::A4_pzxthf:
1506 case Hexagon::A4_pzxthfnew:
1507 case Hexagon::A4_pzxtht:
1508 case Hexagon::A4_pzxthtnew:
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001509 case Hexagon::A2_paddit:
1510 case Hexagon::A2_paddif:
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001511 case Hexagon::C2_ccombinewt:
1512 case Hexagon::C2_ccombinewf:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001513 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001514 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001515}
1516
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001517bool HexagonInstrInfo::
1518isConditionalLoad (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001519 switch (MI->getOpcode())
1520 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001521 default: return false;
Colin LeMahieu947cd702014-12-23 20:44:59 +00001522 case Hexagon::L2_ploadrdt_io :
1523 case Hexagon::L2_ploadrdf_io:
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001524 case Hexagon::L2_ploadrit_io:
1525 case Hexagon::L2_ploadrif_io:
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001526 case Hexagon::L2_ploadrht_io:
1527 case Hexagon::L2_ploadrhf_io:
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001528 case Hexagon::L2_ploadrbt_io:
1529 case Hexagon::L2_ploadrbf_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001530 case Hexagon::L2_ploadruht_io:
1531 case Hexagon::L2_ploadruhf_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001532 case Hexagon::L2_ploadrubt_io:
1533 case Hexagon::L2_ploadrubf_io:
Colin LeMahieu9161d472014-12-30 18:58:47 +00001534 case Hexagon::L2_ploadrdt_pi:
1535 case Hexagon::L2_ploadrdf_pi:
1536 case Hexagon::L2_ploadrit_pi:
1537 case Hexagon::L2_ploadrif_pi:
1538 case Hexagon::L2_ploadrht_pi:
1539 case Hexagon::L2_ploadrhf_pi:
1540 case Hexagon::L2_ploadrbt_pi:
1541 case Hexagon::L2_ploadrbf_pi:
1542 case Hexagon::L2_ploadruht_pi:
1543 case Hexagon::L2_ploadruhf_pi:
1544 case Hexagon::L2_ploadrubt_pi:
1545 case Hexagon::L2_ploadrubf_pi:
Colin LeMahieu9161d472014-12-30 18:58:47 +00001546 case Hexagon::L4_ploadrdt_rr:
1547 case Hexagon::L4_ploadrdf_rr:
1548 case Hexagon::L4_ploadrbt_rr:
1549 case Hexagon::L4_ploadrbf_rr:
1550 case Hexagon::L4_ploadrubt_rr:
1551 case Hexagon::L4_ploadrubf_rr:
1552 case Hexagon::L4_ploadrht_rr:
1553 case Hexagon::L4_ploadrhf_rr:
1554 case Hexagon::L4_ploadruht_rr:
1555 case Hexagon::L4_ploadruhf_rr:
1556 case Hexagon::L4_ploadrit_rr:
1557 case Hexagon::L4_ploadrif_rr:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001558 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001559 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001560}
Andrew Trickd06df962012-02-01 22:13:57 +00001561
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001562// Returns true if an instruction is a conditional store.
1563//
1564// Note: It doesn't include conditional new-value stores as they can't be
1565// converted to .new predicate.
1566//
1567// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1568// ^ ^
1569// / \ (not OK. it will cause new-value store to be
1570// / X conditional on p0.new while R2 producer is
1571// / \ on p0)
1572// / \.
1573// p.new store p.old NV store
1574// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1575// ^ ^
1576// \ /
1577// \ /
1578// \ /
1579// p.old store
1580// [if (p0)memw(R0+#0)=R2]
1581//
1582// The above diagram shows the steps involoved in the conversion of a predicated
1583// store instruction to its .new predicated new-value form.
1584//
1585// The following set of instructions further explains the scenario where
1586// conditional new-value store becomes invalid when promoted to .new predicate
1587// form.
1588//
1589// { 1) if (p0) r0 = add(r1, r2)
1590// 2) p0 = cmp.eq(r3, #0) }
1591//
1592// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1593// the first two instructions because in instr 1, r0 is conditional on old value
1594// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1595// is not valid for new-value stores.
1596bool HexagonInstrInfo::
1597isConditionalStore (const MachineInstr* MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001598 switch (MI->getOpcode())
1599 {
1600 default: return false;
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001601 case Hexagon::S4_storeirbt_io:
1602 case Hexagon::S4_storeirbf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001603 case Hexagon::S4_pstorerbt_rr:
1604 case Hexagon::S4_pstorerbf_rr:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001605 case Hexagon::S2_pstorerbt_io:
1606 case Hexagon::S2_pstorerbf_io:
Colin LeMahieu3d34afb2014-12-29 19:42:14 +00001607 case Hexagon::S2_pstorerbt_pi:
1608 case Hexagon::S2_pstorerbf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001609 case Hexagon::S2_pstorerdt_io:
1610 case Hexagon::S2_pstorerdf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001611 case Hexagon::S4_pstorerdt_rr:
1612 case Hexagon::S4_pstorerdf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001613 case Hexagon::S2_pstorerdt_pi:
1614 case Hexagon::S2_pstorerdf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001615 case Hexagon::S2_pstorerht_io:
1616 case Hexagon::S2_pstorerhf_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001617 case Hexagon::S4_storeirht_io:
1618 case Hexagon::S4_storeirhf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001619 case Hexagon::S4_pstorerht_rr:
1620 case Hexagon::S4_pstorerhf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001621 case Hexagon::S2_pstorerht_pi:
1622 case Hexagon::S2_pstorerhf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001623 case Hexagon::S2_pstorerit_io:
1624 case Hexagon::S2_pstorerif_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001625 case Hexagon::S4_storeirit_io:
1626 case Hexagon::S4_storeirif_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001627 case Hexagon::S4_pstorerit_rr:
1628 case Hexagon::S4_pstorerif_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001629 case Hexagon::S2_pstorerit_pi:
1630 case Hexagon::S2_pstorerif_pi:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001631
1632 // V4 global address store before promoting to dot new.
Colin LeMahieu14455532015-01-06 16:15:15 +00001633 case Hexagon::S4_pstorerdt_abs:
1634 case Hexagon::S4_pstorerdf_abs:
1635 case Hexagon::S4_pstorerbt_abs:
1636 case Hexagon::S4_pstorerbf_abs:
1637 case Hexagon::S4_pstorerht_abs:
1638 case Hexagon::S4_pstorerhf_abs:
1639 case Hexagon::S4_pstorerit_abs:
1640 case Hexagon::S4_pstorerif_abs:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001641 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001642
1643 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1644 // from the "Conditional Store" list. Because a predicated new value store
1645 // would NOT be promoted to a double dot new store. See diagram below:
1646 // This function returns yes for those stores that are predicated but not
1647 // yet promoted to predicate dot new instructions.
1648 //
1649 // +---------------------+
1650 // /-----| if (p0) memw(..)=r0 |---------\~
1651 // || +---------------------+ ||
1652 // promote || /\ /\ || promote
1653 // || /||\ /||\ ||
1654 // \||/ demote || \||/
1655 // \/ || || \/
1656 // +-------------------------+ || +-------------------------+
1657 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1658 // +-------------------------+ || +-------------------------+
1659 // || || ||
1660 // || demote \||/
1661 // promote || \/ NOT possible
1662 // || || /\~
1663 // \||/ || /||\~
1664 // \/ || ||
1665 // +-----------------------------+
1666 // | if (p0.new) memw(..)=r0.new |
1667 // +-----------------------------+
1668 // Double Dot New Store
1669 //
1670 }
1671}
1672
Jyotsna Verma84c47102013-05-06 18:49:23 +00001673
1674bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1675 if (isNewValue(MI) && isBranch(MI))
1676 return true;
1677 return false;
1678}
1679
Brendon Cahoondf43e682015-05-08 16:16:29 +00001680bool HexagonInstrInfo::isNewValueJump(Opcode_t Opcode) const {
1681 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001682}
1683
Brendon Cahoondf43e682015-05-08 16:16:29 +00001684bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1685 return (getAddrMode(MI) == HexagonII::PostInc);
Jyotsna Verma84c47102013-05-06 18:49:23 +00001686}
1687
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001688// Returns true, if any one of the operands is a dot new
1689// insn, whether it is predicated dot new or register dot new.
1690bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1691 return (isNewValueInst(MI) ||
1692 (isPredicated(MI) && isPredicatedNew(MI)));
1693}
1694
Jyotsna Verma438cec52013-05-10 20:58:11 +00001695// Returns the most basic instruction for the .new predicated instructions and
1696// new-value stores.
1697// For example, all of the following instructions will be converted back to the
1698// same instruction:
1699// 1) if (p0.new) memw(R0+#0) = R1.new --->
1700// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1701// 3) if (p0.new) memw(R0+#0) = R1 --->
1702//
1703
1704int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1705 int NewOp = opc;
1706 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1707 NewOp = Hexagon::getPredOldOpcode(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001708 assert(NewOp >= 0 &&
1709 "Couldn't change predicate new instruction to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001710 }
1711
Alp Tokerf907b892013-12-05 05:44:44 +00001712 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
Jyotsna Verma438cec52013-05-10 20:58:11 +00001713 NewOp = Hexagon::getNonNVStore(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001714 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001715 }
1716 return NewOp;
1717}
1718
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001719// Return the new value instruction for a given store.
1720int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1721 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1722 if (NVOpcode >= 0) // Valid new-value store instruction.
1723 return NVOpcode;
1724
1725 switch (MI->getOpcode()) {
1726 default: llvm_unreachable("Unknown .new type");
Colin LeMahieuc0434462015-02-04 17:52:06 +00001727 case Hexagon::S4_storerb_ur:
1728 return Hexagon::S4_storerbnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001729
Colin LeMahieuc0434462015-02-04 17:52:06 +00001730 case Hexagon::S4_storerh_ur:
1731 return Hexagon::S4_storerhnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001732
Colin LeMahieuc0434462015-02-04 17:52:06 +00001733 case Hexagon::S4_storeri_ur:
1734 return Hexagon::S4_storerinew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001735
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001736 case Hexagon::S2_storerb_pci:
1737 return Hexagon::S2_storerb_pci;
1738
1739 case Hexagon::S2_storeri_pci:
1740 return Hexagon::S2_storeri_pci;
1741
1742 case Hexagon::S2_storerh_pci:
1743 return Hexagon::S2_storerh_pci;
1744
1745 case Hexagon::S2_storerd_pci:
1746 return Hexagon::S2_storerd_pci;
1747
1748 case Hexagon::S2_storerf_pci:
1749 return Hexagon::S2_storerf_pci;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001750 }
1751 return 0;
1752}
1753
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001754// Return .new predicate version for an instruction.
1755int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1756 const MachineBranchProbabilityInfo
1757 *MBPI) const {
1758
1759 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1760 if (NewOpcode >= 0) // Valid predicate new instruction
1761 return NewOpcode;
1762
1763 switch (MI->getOpcode()) {
1764 default: llvm_unreachable("Unknown .new type");
1765 // Condtional Jumps
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001766 case Hexagon::J2_jumpt:
1767 case Hexagon::J2_jumpf:
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001768 return getDotNewPredJumpOp(MI, MBPI);
1769
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001770 case Hexagon::J2_jumprt:
1771 return Hexagon::J2_jumptnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001772
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001773 case Hexagon::J2_jumprf:
1774 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001775
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001776 case Hexagon::JMPrett:
1777 return Hexagon::J2_jumprtnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001778
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001779 case Hexagon::JMPretf:
1780 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001781
1782
1783 // Conditional combine
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001784 case Hexagon::C2_ccombinewt:
1785 return Hexagon::C2_ccombinewnewt;
1786 case Hexagon::C2_ccombinewf:
1787 return Hexagon::C2_ccombinewnewf;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001788 }
1789}
1790
1791
Jyotsna Verma84256432013-03-01 17:37:13 +00001792unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1793 const uint64_t F = MI->getDesc().TSFlags;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001794
Jyotsna Verma84256432013-03-01 17:37:13 +00001795 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1796}
1797
1798/// immediateExtend - Changes the instruction in place to one using an immediate
1799/// extender.
1800void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1801 assert((isExtendable(MI)||isConstExtended(MI)) &&
1802 "Instruction must be extendable");
1803 // Find which operand is extendable.
1804 short ExtOpNum = getCExtOpNum(MI);
1805 MachineOperand &MO = MI->getOperand(ExtOpNum);
1806 // This needs to be something we understand.
1807 assert((MO.isMBB() || MO.isImm()) &&
1808 "Branch with unknown extendable field type");
1809 // Mark given operand as extended.
1810 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1811}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001812
Eric Christopher143f02c2014-10-09 01:59:35 +00001813DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1814 const TargetSubtargetInfo &STI) const {
1815 const InstrItineraryData *II = STI.getInstrItineraryData();
1816 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
Andrew Trickd06df962012-02-01 22:13:57 +00001817}
1818
1819bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1820 const MachineBasicBlock *MBB,
1821 const MachineFunction &MF) const {
1822 // Debug info is never a scheduling boundary. It's necessary to be explicit
1823 // due to the special treatment of IT instructions below, otherwise a
1824 // dbg_value followed by an IT will result in the IT instruction being
1825 // considered a scheduling hazard, which is wrong. It should be the actual
1826 // instruction preceding the dbg_value instruction(s), just like it is
1827 // when debug info is not present.
1828 if (MI->isDebugValue())
1829 return false;
1830
1831 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001832 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
Andrew Trickd06df962012-02-01 22:13:57 +00001833 return true;
1834
1835 return false;
1836}
Jyotsna Verma84256432013-03-01 17:37:13 +00001837
Brendon Cahoon55bdeb72015-04-27 14:16:43 +00001838bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001839 const uint64_t F = MI->getDesc().TSFlags;
1840 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1841 if (isExtended) // Instruction must be extended.
1842 return true;
1843
Brendon Cahoon55bdeb72015-04-27 14:16:43 +00001844 unsigned isExtendable =
1845 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
Jyotsna Verma84256432013-03-01 17:37:13 +00001846 if (!isExtendable)
1847 return false;
1848
1849 short ExtOpNum = getCExtOpNum(MI);
1850 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1851 // Use MO operand flags to determine if MO
1852 // has the HMOTF_ConstExtended flag set.
1853 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1854 return true;
1855 // If this is a Machine BB address we are talking about, and it is
1856 // not marked as extended, say so.
1857 if (MO.isMBB())
1858 return false;
1859
1860 // We could be using an instruction with an extendable immediate and shoehorn
1861 // a global address into it. If it is a global address it will be constant
1862 // extended. We do this for COMBINE.
1863 // We currently only handle isGlobal() because it is the only kind of
1864 // object we are going to end up with here for now.
1865 // In the future we probably should add isSymbol(), etc.
Krzysztof Parzyszekcd97c982015-04-22 18:25:53 +00001866 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1867 MO.isJTI() || MO.isCPI())
Jyotsna Verma84256432013-03-01 17:37:13 +00001868 return true;
1869
1870 // If the extendable operand is not 'Immediate' type, the instruction should
1871 // have 'isExtended' flag set.
1872 assert(MO.isImm() && "Extendable operand must be Immediate type");
1873
1874 int MinValue = getMinValue(MI);
1875 int MaxValue = getMaxValue(MI);
1876 int ImmValue = MO.getImm();
1877
1878 return (ImmValue < MinValue || ImmValue > MaxValue);
1879}
1880
Brendon Cahoon55bdeb72015-04-27 14:16:43 +00001881// Return the number of bytes required to encode the instruction.
1882// Hexagon instructions are fixed length, 4 bytes, unless they
1883// use a constant extender, which requires another 4 bytes.
1884// For debug instructions and prolog labels, return 0.
1885unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
1886
1887 if (MI->isDebugValue() || MI->isPosition())
1888 return 0;
1889
1890 unsigned Size = MI->getDesc().getSize();
1891 if (!Size)
1892 // Assume the default insn size in case it cannot be determined
1893 // for whatever reason.
1894 Size = HEXAGON_INSTR_SIZE;
1895
1896 if (isConstExtended(MI) || isExtended(MI))
1897 Size += HEXAGON_INSTR_SIZE;
1898
1899 return Size;
1900}
1901
Jyotsna Verma1d297502013-05-02 15:39:30 +00001902// Returns the opcode to use when converting MI, which is a conditional jump,
1903// into a conditional instruction which uses the .new value of the predicate.
1904// We also use branch probabilities to add a hint to the jump.
1905int
1906HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1907 const
1908 MachineBranchProbabilityInfo *MBPI) const {
1909
1910 // We assume that block can have at most two successors.
1911 bool taken = false;
1912 MachineBasicBlock *Src = MI->getParent();
1913 MachineOperand *BrTarget = &MI->getOperand(1);
1914 MachineBasicBlock *Dst = BrTarget->getMBB();
1915
1916 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1917 if (Prediction >= BranchProbability(1,2))
1918 taken = true;
1919
1920 switch (MI->getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001921 case Hexagon::J2_jumpt:
1922 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1923 case Hexagon::J2_jumpf:
1924 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Jyotsna Verma1d297502013-05-02 15:39:30 +00001925
1926 default:
1927 llvm_unreachable("Unexpected jump instruction.");
1928 }
1929}
Jyotsna Verma84256432013-03-01 17:37:13 +00001930// Returns true if a particular operand is extendable for an instruction.
1931bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1932 unsigned short OperandNum) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001933 const uint64_t F = MI->getDesc().TSFlags;
1934
1935 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1936 == OperandNum;
1937}
1938
1939// Returns Operand Index for the constant extended instruction.
1940unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1941 const uint64_t F = MI->getDesc().TSFlags;
1942 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1943}
1944
1945// Returns the min value that doesn't need to be extended.
1946int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1947 const uint64_t F = MI->getDesc().TSFlags;
1948 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1949 & HexagonII::ExtentSignedMask;
1950 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1951 & HexagonII::ExtentBitsMask;
1952
1953 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001954 return -1U << (bits - 1);
Jyotsna Verma84256432013-03-01 17:37:13 +00001955 else
1956 return 0;
1957}
1958
1959// Returns the max value that doesn't need to be extended.
1960int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1961 const uint64_t F = MI->getDesc().TSFlags;
1962 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1963 & HexagonII::ExtentSignedMask;
1964 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1965 & HexagonII::ExtentBitsMask;
1966
1967 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001968 return ~(-1U << (bits - 1));
Jyotsna Verma84256432013-03-01 17:37:13 +00001969 else
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001970 return ~(-1U << bits);
Jyotsna Verma84256432013-03-01 17:37:13 +00001971}
1972
1973// Returns true if an instruction can be converted into a non-extended
1974// equivalent instruction.
1975bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1976
1977 short NonExtOpcode;
1978 // Check if the instruction has a register form that uses register in place
1979 // of the extended operand, if so return that as the non-extended form.
1980 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1981 return true;
1982
1983 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001984 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001985
1986 switch (getAddrMode(MI)) {
1987 case HexagonII::Absolute :
1988 // Load/store with absolute addressing mode can be converted into
1989 // base+offset mode.
1990 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1991 break;
1992 case HexagonII::BaseImmOffset :
1993 // Load/store with base+offset addressing mode can be converted into
1994 // base+register offset addressing mode. However left shift operand should
1995 // be set to 0.
1996 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1997 break;
1998 default:
1999 return false;
2000 }
2001 if (NonExtOpcode < 0)
2002 return false;
2003 return true;
2004 }
2005 return false;
2006}
2007
2008// Returns opcode of the non-extended equivalent instruction.
2009short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
2010
2011 // Check if the instruction has a register form that uses register in place
2012 // of the extended operand, if so return that as the non-extended form.
2013 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
2014 if (NonExtOpcode >= 0)
2015 return NonExtOpcode;
2016
2017 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002018 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002019 switch (getAddrMode(MI)) {
2020 case HexagonII::Absolute :
2021 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
2022 case HexagonII::BaseImmOffset :
2023 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
2024 default:
2025 return -1;
2026 }
2027 }
2028 return -1;
2029}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002030
2031bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00002032 return (Opcode == Hexagon::J2_jumpt) ||
2033 (Opcode == Hexagon::J2_jumpf) ||
2034 (Opcode == Hexagon::J2_jumptnewpt) ||
2035 (Opcode == Hexagon::J2_jumpfnewpt) ||
2036 (Opcode == Hexagon::J2_jumpt) ||
2037 (Opcode == Hexagon::J2_jumpf);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002038}
2039
Ahmed Bougachac88bf542015-06-11 19:30:37 +00002040bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00002041 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2042 return false;
2043 return !isPredicatedTrue(Cond[0].getImm());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002044}
Brendon Cahoondf43e682015-05-08 16:16:29 +00002045
2046bool HexagonInstrInfo::isEndLoopN(Opcode_t Opcode) const {
2047 return (Opcode == Hexagon::ENDLOOP0 ||
2048 Opcode == Hexagon::ENDLOOP1);
2049}
2050
Ahmed Bougachac88bf542015-06-11 19:30:37 +00002051bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Brendon Cahoondf43e682015-05-08 16:16:29 +00002052 unsigned &PredReg, unsigned &PredRegPos,
2053 unsigned &PredRegFlags) const {
2054 if (Cond.empty())
2055 return false;
2056 assert(Cond.size() == 2);
2057 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
2058 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
2059 return false;
2060 }
2061 PredReg = Cond[1].getReg();
2062 PredRegPos = 1;
2063 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
2064 PredRegFlags = 0;
2065 if (Cond[1].isImplicit())
2066 PredRegFlags = RegState::Implicit;
2067 if (Cond[1].isUndef())
2068 PredRegFlags |= RegState::Undef;
2069 return true;
2070}
2071