blob: fbf1ca90055f4df0479667e859b833062a4b9b22 [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
118
119unsigned
120HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
121 MachineBasicBlock *FBB,
122 const SmallVectorImpl<MachineOperand> &Cond,
123 DebugLoc DL) const{
124
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000125 int BOpc = Hexagon::J2_jump;
126 int BccOpc = Hexagon::J2_jumpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000127
128 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
129
130 int regPos = 0;
131 // Check if ReverseBranchCondition has asked to reverse this branch
132 // If we want to reverse the branch an odd number of times, we want
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000133 // JMP_f.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000134 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000135 BccOpc = Hexagon::J2_jumpf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000136 regPos = 1;
137 }
138
Craig Topper062a2ba2014-04-25 05:30:21 +0000139 if (!FBB) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000140 if (Cond.empty()) {
141 // Due to a bug in TailMerging/CFG Optimization, we need to add a
142 // special case handling of a predicated jump followed by an
143 // unconditional jump. If not, Tail Merging and CFG Optimization go
144 // into an infinite loop.
145 MachineBasicBlock *NewTBB, *NewFBB;
146 SmallVector<MachineOperand, 4> Cond;
147 MachineInstr *Term = MBB.getFirstTerminator();
148 if (isPredicated(Term) && !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond,
149 false)) {
150 MachineBasicBlock *NextBB =
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000151 std::next(MachineFunction::iterator(&MBB));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000152 if (NewTBB == NextBB) {
153 ReverseBranchCondition(Cond);
154 RemoveBranch(MBB);
Craig Topper062a2ba2014-04-25 05:30:21 +0000155 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000156 }
157 }
158 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
159 } else {
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000160 // If Cond[0] is a basic block, insert ENDLOOP0.
161 if (Cond[0].isMBB())
162 BuildMI(&MBB, DL, get(Hexagon::ENDLOOP0)).addMBB(Cond[0].getMBB());
163 else
164 BuildMI(&MBB, DL,
165 get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000166 }
167 return 1;
168 }
169
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000170 // We don't handle ENDLOOP0 with a conditional branch in AnalyzeBranch.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000171 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
172 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000173 return 2;
174}
175
176
177bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
178 MachineBasicBlock *&TBB,
179 MachineBasicBlock *&FBB,
180 SmallVectorImpl<MachineOperand> &Cond,
181 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000182 TBB = nullptr;
183 FBB = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000184
185 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000186 MachineBasicBlock::instr_iterator I = MBB.instr_end();
187 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000188 return false;
189
190 // A basic block may looks like this:
191 //
192 // [ insn
193 // EH_LABEL
194 // insn
195 // insn
196 // insn
197 // EH_LABEL
198 // insn ]
199 //
200 // It has two succs but does not have a terminator
201 // Don't know how to handle it.
202 do {
203 --I;
204 if (I->isEHLabel())
205 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000206 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000207
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000208 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000209 --I;
210
211 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000212 if (I == MBB.instr_begin())
213 return false;
214 --I;
215 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000216
217 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
218 I->getOperand(0).isMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000219 // Delete the JMP if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000220 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000221 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
222 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
223 I->eraseFromParent();
224 I = MBB.instr_end();
225 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000226 return false;
227 --I;
228 }
229 if (!isUnpredicatedTerminator(I))
230 return false;
231
232 // Get the last instruction in the block.
233 MachineInstr *LastInst = I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000234 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000235 // Find one more terminator if present.
236 do {
237 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
238 if (!SecondLastInst)
239 SecondLastInst = I;
240 else
241 // This is a third branch.
242 return true;
243 }
244 if (I == MBB.instr_begin())
245 break;
246 --I;
247 } while(I);
248
249 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000250 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
251 // If the branch target is not a basic block, it could be a tail call.
252 // (It is, if the target is a function.)
253 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
254 return true;
255 if (SecLastOpcode == Hexagon::J2_jump &&
256 !SecondLastInst->getOperand(0).isMBB())
257 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000258
259 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
260 bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000261
262 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000263 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000264 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000265 TBB = LastInst->getOperand(0).getMBB();
266 return false;
267 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000268 if (LastOpcode == Hexagon::ENDLOOP0) {
269 TBB = LastInst->getOperand(0).getMBB();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000270 Cond.push_back(LastInst->getOperand(0));
271 return false;
272 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000273 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000274 TBB = LastInst->getOperand(1).getMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000275 if (LastOpcodeHasNot) {
276 Cond.push_back(MachineOperand::CreateImm(0));
277 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000278 Cond.push_back(LastInst->getOperand(0));
279 return false;
280 }
281 // Otherwise, don't know what this is.
282 return true;
283 }
284
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000285 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
286 bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000287 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000288 TBB = SecondLastInst->getOperand(1).getMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000289 if (SecLastOpcodeHasNot)
290 Cond.push_back(MachineOperand::CreateImm(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000291 Cond.push_back(SecondLastInst->getOperand(0));
292 FBB = LastInst->getOperand(0).getMBB();
293 return false;
294 }
295
296 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
297 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000298 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000299 TBB = SecondLastInst->getOperand(0).getMBB();
300 I = LastInst;
301 if (AllowModify)
302 I->eraseFromParent();
303 return false;
304 }
305
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000306 // If the block ends with an ENDLOOP, and JMP, handle it.
307 if (SecLastOpcode == Hexagon::ENDLOOP0 &&
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000308 LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000309 TBB = SecondLastInst->getOperand(0).getMBB();
310 Cond.push_back(SecondLastInst->getOperand(0));
311 FBB = LastInst->getOperand(0).getMBB();
312 return false;
313 }
314
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000315 // Otherwise, can't handle this.
316 return true;
317}
318
319
320unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000321 MachineBasicBlock::iterator I = MBB.end();
322 if (I == MBB.begin()) return 0;
323 --I;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000324 unsigned Opc1 = I->getOpcode();
325 switch (Opc1) {
326 case Hexagon::J2_jump:
327 case Hexagon::J2_jumpt:
328 case Hexagon::J2_jumpf:
329 case Hexagon::ENDLOOP0:
330 I->eraseFromParent();
331 break;
332 default:
333 return 0;
334 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000335
336 I = MBB.end();
337
338 if (I == MBB.begin()) return 1;
339 --I;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000340 unsigned Opc2 = I->getOpcode();
341 switch (Opc2) {
342 case Hexagon::J2_jumpt:
343 case Hexagon::J2_jumpf:
344 case Hexagon::ENDLOOP0:
345 I->eraseFromParent();
346 return 2;
347 default:
348 return 1;
349 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000350}
351
352
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000353/// \brief For a comparison instruction, return the source registers in
354/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
355/// compares against in CmpValue. Return true if the comparison instruction
356/// can be analyzed.
357bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
358 unsigned &SrcReg, unsigned &SrcReg2,
359 int &Mask, int &Value) const {
360 unsigned Opc = MI->getOpcode();
361
362 // Set mask and the first source register.
363 switch (Opc) {
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000364 case Hexagon::C2_cmpeqp:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000365 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000366 case Hexagon::C2_cmpeq:
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000367 case Hexagon::C2_cmpgtp:
368 case Hexagon::C2_cmpgtup:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000369 case Hexagon::C2_cmpgtui:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000370 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000371 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000372 case Hexagon::C2_cmpgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000373 SrcReg = MI->getOperand(1).getReg();
374 Mask = ~0;
375 break;
Colin LeMahieufa947902015-01-14 16:49:12 +0000376 case Hexagon::A4_cmpbeqi:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000377 case Hexagon::A4_cmpbeq:
378 case Hexagon::A4_cmpbgtui:
379 case Hexagon::A4_cmpbgtu:
380 case Hexagon::A4_cmpbgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000381 SrcReg = MI->getOperand(1).getReg();
382 Mask = 0xFF;
383 break;
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000384 case Hexagon::A4_cmpheqi:
385 case Hexagon::A4_cmpheq:
386 case Hexagon::A4_cmphgtui:
387 case Hexagon::A4_cmphgtu:
388 case Hexagon::A4_cmphgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000389 SrcReg = MI->getOperand(1).getReg();
390 Mask = 0xFFFF;
391 break;
392 }
393
394 // Set the value/second source register.
395 switch (Opc) {
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000396 case Hexagon::C2_cmpeqp:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000397 case Hexagon::C2_cmpeq:
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000398 case Hexagon::C2_cmpgtp:
399 case Hexagon::C2_cmpgtup:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000400 case Hexagon::C2_cmpgtu:
401 case Hexagon::C2_cmpgt:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000402 case Hexagon::A4_cmpbeq:
403 case Hexagon::A4_cmpbgtu:
404 case Hexagon::A4_cmpbgt:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000405 case Hexagon::A4_cmpheq:
406 case Hexagon::A4_cmphgtu:
407 case Hexagon::A4_cmphgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000408 SrcReg2 = MI->getOperand(2).getReg();
409 return true;
410
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000411 case Hexagon::C2_cmpeqi:
412 case Hexagon::C2_cmpgtui:
413 case Hexagon::C2_cmpgti:
Colin LeMahieufa947902015-01-14 16:49:12 +0000414 case Hexagon::A4_cmpbeqi:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000415 case Hexagon::A4_cmpbgtui:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000416 case Hexagon::A4_cmpheqi:
417 case Hexagon::A4_cmphgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000418 SrcReg2 = 0;
419 Value = MI->getOperand(2).getImm();
420 return true;
421 }
422
423 return false;
424}
425
426
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000427void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
428 MachineBasicBlock::iterator I, DebugLoc DL,
429 unsigned DestReg, unsigned SrcReg,
430 bool KillSrc) const {
431 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000432 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000433 return;
434 }
435 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000436 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000437 return;
438 }
439 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
440 // Map Pd = Ps to Pd = or(Ps, Ps).
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000441 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000442 DestReg).addReg(SrcReg).addReg(SrcReg);
443 return;
444 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000445 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
446 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000447 // We can have an overlap between single and double reg: r1:0 = r0.
448 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
449 // r1:0 = r0
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000450 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451 Hexagon::subreg_hireg))).addImm(0);
452 } else {
453 // r1:0 = r1 or no overlap.
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000454 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455 Hexagon::subreg_loreg))).addReg(SrcReg);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000456 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 Hexagon::subreg_hireg))).addImm(0);
458 }
459 return;
460 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000461 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000462 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000463 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000464 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000465 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000466 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
467 Hexagon::IntRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000468 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000469 addReg(SrcReg, getKillRegState(KillSrc));
470 return;
471 }
472 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
473 Hexagon::PredRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000474 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000475 addReg(SrcReg, getKillRegState(KillSrc));
476 return;
477 }
Sirish Pande30804c22012-02-15 18:52:27 +0000478
479 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000480}
481
482
483void HexagonInstrInfo::
484storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
485 unsigned SrcReg, bool isKill, int FI,
486 const TargetRegisterClass *RC,
487 const TargetRegisterInfo *TRI) const {
488
489 DebugLoc DL = MBB.findDebugLoc(I);
490 MachineFunction &MF = *MBB.getParent();
491 MachineFrameInfo &MFI = *MF.getFrameInfo();
492 unsigned Align = MFI.getObjectAlignment(FI);
493
494 MachineMemOperand *MMO =
495 MF.getMachineMemOperand(
496 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
497 MachineMemOperand::MOStore,
498 MFI.getObjectSize(FI),
499 Align);
500
Craig Topperc7242e02012-04-20 07:30:17 +0000501 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000502 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000503 .addFrameIndex(FI).addImm(0)
504 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000505 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000506 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000507 .addFrameIndex(FI).addImm(0)
508 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000509 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000510 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
511 .addFrameIndex(FI).addImm(0)
512 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
513 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000514 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000515 }
516}
517
518
519void HexagonInstrInfo::storeRegToAddr(
520 MachineFunction &MF, unsigned SrcReg,
521 bool isKill,
522 SmallVectorImpl<MachineOperand> &Addr,
523 const TargetRegisterClass *RC,
524 SmallVectorImpl<MachineInstr*> &NewMIs) const
525{
Craig Toppere55c5562012-02-07 02:50:20 +0000526 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000527}
528
529
530void HexagonInstrInfo::
531loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
532 unsigned DestReg, int FI,
533 const TargetRegisterClass *RC,
534 const TargetRegisterInfo *TRI) const {
535 DebugLoc DL = MBB.findDebugLoc(I);
536 MachineFunction &MF = *MBB.getParent();
537 MachineFrameInfo &MFI = *MF.getFrameInfo();
538 unsigned Align = MFI.getObjectAlignment(FI);
539
540 MachineMemOperand *MMO =
541 MF.getMachineMemOperand(
542 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
543 MachineMemOperand::MOLoad,
544 MFI.getObjectSize(FI),
545 Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000546 if (RC == &Hexagon::IntRegsRegClass) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000547 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000548 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000549 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000550 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000551 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000552 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000553 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
554 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
555 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000556 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000557 }
558}
559
560
561void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
562 SmallVectorImpl<MachineOperand> &Addr,
563 const TargetRegisterClass *RC,
564 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Craig Toppere55c5562012-02-07 02:50:20 +0000565 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000566}
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000567bool
568HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000569 const HexagonRegisterInfo &TRI = getRegisterInfo();
570 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000571 MachineBasicBlock &MBB = *MI->getParent();
572 DebugLoc DL = MI->getDebugLoc();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000573 unsigned Opc = MI->getOpcode();
574
575 switch (Opc) {
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000576 case Hexagon::TFR_PdTrue: {
577 unsigned Reg = MI->getOperand(0).getReg();
578 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
579 .addReg(Reg, RegState::Undef)
580 .addReg(Reg, RegState::Undef);
581 MBB.erase(MI);
582 return true;
583 }
584 case Hexagon::TFR_PdFalse: {
585 unsigned Reg = MI->getOperand(0).getReg();
586 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
587 .addReg(Reg, RegState::Undef)
588 .addReg(Reg, RegState::Undef);
589 MBB.erase(MI);
590 return true;
591 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000592 case Hexagon::VMULW: {
593 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
594 unsigned DstReg = MI->getOperand(0).getReg();
595 unsigned Src1Reg = MI->getOperand(1).getReg();
596 unsigned Src2Reg = MI->getOperand(2).getReg();
597 unsigned Src1SubHi = TRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
598 unsigned Src1SubLo = TRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
599 unsigned Src2SubHi = TRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
600 unsigned Src2SubLo = TRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
601 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
602 TRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
603 .addReg(Src2SubHi);
604 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
605 TRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
606 .addReg(Src2SubLo);
607 MBB.erase(MI);
608 MRI.clearKillFlags(Src1SubHi);
609 MRI.clearKillFlags(Src1SubLo);
610 MRI.clearKillFlags(Src2SubHi);
611 MRI.clearKillFlags(Src2SubLo);
612 return true;
613 }
614 case Hexagon::VMULW_ACC: {
615 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
616 unsigned DstReg = MI->getOperand(0).getReg();
617 unsigned Src1Reg = MI->getOperand(1).getReg();
618 unsigned Src2Reg = MI->getOperand(2).getReg();
619 unsigned Src3Reg = MI->getOperand(3).getReg();
620 unsigned Src1SubHi = TRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
621 unsigned Src1SubLo = TRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
622 unsigned Src2SubHi = TRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
623 unsigned Src2SubLo = TRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
624 unsigned Src3SubHi = TRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
625 unsigned Src3SubLo = TRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
626 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
627 TRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
628 .addReg(Src2SubHi).addReg(Src3SubHi);
629 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
630 TRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
631 .addReg(Src2SubLo).addReg(Src3SubLo);
632 MBB.erase(MI);
633 MRI.clearKillFlags(Src1SubHi);
634 MRI.clearKillFlags(Src1SubLo);
635 MRI.clearKillFlags(Src2SubHi);
636 MRI.clearKillFlags(Src2SubLo);
637 MRI.clearKillFlags(Src3SubHi);
638 MRI.clearKillFlags(Src3SubLo);
639 return true;
640 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000641 case Hexagon::TCRETURNi:
642 MI->setDesc(get(Hexagon::J2_jump));
643 return true;
644 case Hexagon::TCRETURNr:
645 MI->setDesc(get(Hexagon::J2_jumpr));
646 return true;
647 }
648
649 return false;
650}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000651
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000652MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000653 MachineInstr *MI,
654 ArrayRef<unsigned> Ops,
655 int FI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000656 // Hexagon_TODO: Implement.
Craig Topper062a2ba2014-04-25 05:30:21 +0000657 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000658}
659
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000660unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
661
662 MachineRegisterInfo &RegInfo = MF->getRegInfo();
663 const TargetRegisterClass *TRC;
Sirish Pande69295b82012-05-10 20:20:25 +0000664 if (VT == MVT::i1) {
Craig Topperc7242e02012-04-20 07:30:17 +0000665 TRC = &Hexagon::PredRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000666 } else if (VT == MVT::i32 || VT == MVT::f32) {
Craig Topperc7242e02012-04-20 07:30:17 +0000667 TRC = &Hexagon::IntRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000668 } else if (VT == MVT::i64 || VT == MVT::f64) {
Craig Topperc7242e02012-04-20 07:30:17 +0000669 TRC = &Hexagon::DoubleRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000670 } else {
Benjamin Kramerb6684012011-12-27 11:41:05 +0000671 llvm_unreachable("Cannot handle this register class");
Sirish Pande69295b82012-05-10 20:20:25 +0000672 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000673
674 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
675 return NewReg;
676}
677
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000678bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000679 const MCInstrDesc &MID = MI->getDesc();
680 const uint64_t F = MID.TSFlags;
681 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
682 return true;
683
684 // TODO: This is largely obsolete now. Will need to be removed
685 // in consecutive patches.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000686 switch(MI->getOpcode()) {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000687 // TFR_FI Remains a special case.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000688 case Hexagon::TFR_FI:
689 return true;
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000690 default:
691 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000692 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000693 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000694}
695
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000696// This returns true in two cases:
697// - The OP code itself indicates that this is an extended instruction.
698// - One of MOs has been marked with HMOTF_ConstExtended flag.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000699bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000700 // First check if this is permanently extended op code.
701 const uint64_t F = MI->getDesc().TSFlags;
702 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
703 return true;
704 // Use MO operand flags to determine if one of MI's operands
705 // has HMOTF_ConstExtended flag set.
706 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
707 E = MI->operands_end(); I != E; ++I) {
708 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Sirish Pande69295b82012-05-10 20:20:25 +0000709 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000710 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000711 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000712}
713
Jyotsna Verma84c47102013-05-06 18:49:23 +0000714bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
715 return MI->getDesc().isBranch();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000716}
717
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000718bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
719 if (isNewValueJump(MI))
720 return true;
721
722 if (isNewValueStore(MI))
723 return true;
724
725 return false;
726}
727
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000728bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
729 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
730}
Andrew Trickd06df962012-02-01 22:13:57 +0000731
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000732bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
733 bool isPred = MI->getDesc().isPredicable();
734
735 if (!isPred)
736 return false;
737
738 const int Opc = MI->getOpcode();
739
740 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000741 case Hexagon::A2_tfrsi:
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000742 return (isOperandExtended(MI, 1) && isConstExtended(MI)) || isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000743
Colin LeMahieubda31b42014-12-29 20:44:51 +0000744 case Hexagon::S2_storerd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000745 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000746
Colin LeMahieubda31b42014-12-29 20:44:51 +0000747 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000748 case Hexagon::S2_storerinew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000749 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000750
Colin LeMahieubda31b42014-12-29 20:44:51 +0000751 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000752 case Hexagon::S2_storerhnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000753 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000754
Colin LeMahieubda31b42014-12-29 20:44:51 +0000755 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000756 case Hexagon::S2_storerbnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000757 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000758
Colin LeMahieu947cd702014-12-23 20:44:59 +0000759 case Hexagon::L2_loadrd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000760 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000761
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000762 case Hexagon::L2_loadri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000763 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000764
Colin LeMahieu8e39cad2014-12-23 17:25:57 +0000765 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +0000766 case Hexagon::L2_loadruh_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000767 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000768
Colin LeMahieu4b1eac42014-12-22 21:40:43 +0000769 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +0000770 case Hexagon::L2_loadrub_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000771 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000772
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000773 case Hexagon::L2_loadrd_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000774 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000775
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000776 case Hexagon::L2_loadri_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000777 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000778
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000779 case Hexagon::L2_loadrh_pi:
780 case Hexagon::L2_loadruh_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000781 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000782
Colin LeMahieu96976a12014-12-26 18:57:13 +0000783 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +0000784 case Hexagon::L2_loadrub_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000785 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000786
Colin LeMahieu2bad4a72014-12-30 21:01:38 +0000787 case Hexagon::S4_storeirb_io:
788 case Hexagon::S4_storeirh_io:
789 case Hexagon::S4_storeiri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000790 return (isUInt<6>(MI->getOperand(1).getImm()) &&
791 isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000793 case Hexagon::A2_addi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000794 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000795
Colin LeMahieu3b3197e2014-11-24 17:44:19 +0000796 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +0000797 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +0000798 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +0000799 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +0000800 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +0000801 case Hexagon::A2_zxth:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +0000802 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000803 }
804
805 return true;
806}
807
Sirish Pande8bb97452012-05-12 05:54:15 +0000808// This function performs the following inversiones:
809//
810// cPt ---> cNotPt
811// cNotPt ---> cPt
812//
Sirish Pande30804c22012-02-15 18:52:27 +0000813unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
Jyotsna Verma84c47102013-05-06 18:49:23 +0000814 int InvPredOpcode;
815 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
816 : Hexagon::getTruePredOpcode(Opc);
817 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
818 return InvPredOpcode;
819
Sirish Pande30804c22012-02-15 18:52:27 +0000820 switch(Opc) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000821 default: llvm_unreachable("Unexpected predicated instruction");
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000822 case Hexagon::C2_ccombinewt:
823 return Hexagon::C2_ccombinewf;
824 case Hexagon::C2_ccombinewf:
825 return Hexagon::C2_ccombinewt;
Sirish Pande30804c22012-02-15 18:52:27 +0000826
Jyotsna Verma978e9722013-05-09 18:25:44 +0000827 // Dealloc_return.
Colin LeMahieu14455532015-01-06 16:15:15 +0000828 case Hexagon::L4_return_t:
829 return Hexagon::L4_return_f;
830 case Hexagon::L4_return_f:
831 return Hexagon::L4_return_t;
Sirish Pande30804c22012-02-15 18:52:27 +0000832 }
833}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000834
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000835// New Value Store instructions.
836bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
837 const uint64_t F = MI->getDesc().TSFlags;
838
839 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
840}
841
842bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
843 const uint64_t F = get(Opcode).TSFlags;
844
845 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
846}
Andrew Trickd06df962012-02-01 22:13:57 +0000847
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000848int HexagonInstrInfo::
849getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
Pranav Bhandarkar34b60182012-11-01 19:13:23 +0000850 enum Hexagon::PredSense inPredSense;
851 inPredSense = invertPredicate ? Hexagon::PredSense_false :
852 Hexagon::PredSense_true;
853 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
854 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
855 return CondOpcode;
856
857 // This switch case will be removed once all the instructions have been
858 // modified to use relation maps.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000859 switch(Opc) {
Sirish Pande69295b82012-05-10 20:20:25 +0000860 case Hexagon::TFRI_f:
861 return !invertPredicate ? Hexagon::TFRI_cPt_f :
862 Hexagon::TFRI_cNotPt_f;
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000863 case Hexagon::A2_combinew:
864 return !invertPredicate ? Hexagon::C2_ccombinewt :
865 Hexagon::C2_ccombinewf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000866
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000867 // DEALLOC_RETURN.
Colin LeMahieu14455532015-01-06 16:15:15 +0000868 case Hexagon::L4_return:
869 return !invertPredicate ? Hexagon::L4_return_t:
870 Hexagon::L4_return_f;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871 }
Benjamin Kramerb6684012011-12-27 11:41:05 +0000872 llvm_unreachable("Unexpected predicable instruction");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000873}
874
875
876bool HexagonInstrInfo::
877PredicateInstruction(MachineInstr *MI,
878 const SmallVectorImpl<MachineOperand> &Cond) const {
879 int Opc = MI->getOpcode();
880 assert (isPredicable(MI) && "Expected predicable instruction");
881 bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
882 (Cond[0].getImm() == 0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000883
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000884 // This will change MI's opcode to its predicate version.
885 // However, its operand list is still the old one, i.e. the
886 // non-predicate one.
887 MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
888
889 int oper = -1;
890 unsigned int GAIdx = 0;
891
892 // Indicates whether the current MI has a GlobalAddress operand
893 bool hasGAOpnd = false;
894 std::vector<MachineOperand> tmpOpnds;
895
896 // Indicates whether we need to shift operands to right.
897 bool needShift = true;
898
899 // The predicate is ALWAYS the FIRST input operand !!!
900 if (MI->getNumOperands() == 0) {
901 // The non-predicate version of MI does not take any operands,
902 // i.e. no outs and no ins. In this condition, the predicate
903 // operand will be directly placed at Operands[0]. No operand
904 // shift is needed.
905 // Example: BARRIER
906 needShift = false;
907 oper = -1;
908 }
909 else if ( MI->getOperand(MI->getNumOperands()-1).isReg()
910 && MI->getOperand(MI->getNumOperands()-1).isDef()
911 && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
912 // The non-predicate version of MI does not have any input operands.
913 // In this condition, we extend the length of Operands[] by one and
914 // copy the original last operand to the newly allocated slot.
915 // At this moment, it is just a place holder. Later, we will put
916 // predicate operand directly into it. No operand shift is needed.
917 // Example: r0=BARRIER (this is a faked insn used here for illustration)
918 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
919 needShift = false;
920 oper = MI->getNumOperands() - 2;
921 }
922 else {
923 // We need to right shift all input operands by one. Duplicate the
924 // last operand into the newly allocated slot.
925 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
926 }
927
928 if (needShift)
929 {
930 // Operands[ MI->getNumOperands() - 2 ] has been copied into
931 // Operands[ MI->getNumOperands() - 1 ], so we start from
932 // Operands[ MI->getNumOperands() - 3 ].
933 // oper is a signed int.
934 // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
935 for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
936 {
937 MachineOperand &MO = MI->getOperand(oper);
938
939 // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4] Opnd[5] Opnd[6] Opnd[7]
940 // <Def0> <Def1> <Use0> <Use1> <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
941 // /\~
942 // /||\~
943 // ||
944 // Predicate Operand here
945 if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
946 break;
947 }
948 if (MO.isReg()) {
949 MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
950 MO.isImplicit(), MO.isKill(),
951 MO.isDead(), MO.isUndef(),
952 MO.isDebug());
953 }
954 else if (MO.isImm()) {
955 MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
956 }
957 else if (MO.isGlobal()) {
958 // MI can not have more than one GlobalAddress operand.
959 assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
960
961 // There is no member function called "ChangeToGlobalAddress" in the
962 // MachineOperand class (not like "ChangeToRegister" and
963 // "ChangeToImmediate"). So we have to remove them from Operands[] list
964 // first, and then add them back after we have inserted the predicate
965 // operand. tmpOpnds[] is to remember these operands before we remove
966 // them.
967 tmpOpnds.push_back(MO);
968
969 // Operands[oper] is a GlobalAddress operand;
970 // Operands[oper+1] has been copied into Operands[oper+2];
971 hasGAOpnd = true;
972 GAIdx = oper;
973 continue;
974 }
975 else {
Craig Topperd3c02f12015-01-05 10:15:49 +0000976 llvm_unreachable("Unexpected operand type");
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000977 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000978 }
979 }
980
981 int regPos = invertJump ? 1 : 0;
982 MachineOperand PredMO = Cond[regPos];
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000983
984 // [oper] now points to the last explicit Def. Predicate operand must be
985 // located at [oper+1]. See diagram above.
986 // This assumes that the predicate is always the first operand,
987 // i.e. Operands[0+numResults], in the set of inputs
988 // It is better to have an assert here to check this. But I don't know how
989 // to write this assert because findFirstPredOperandIdx() would return -1
990 if (oper < -1) oper = -1;
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000991
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000992 MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000993 PredMO.isImplicit(), false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000994 PredMO.isDead(), PredMO.isUndef(),
995 PredMO.isDebug());
996
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000997 MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
998 RegInfo.clearKillFlags(PredMO.getReg());
999
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001000 if (hasGAOpnd)
1001 {
1002 unsigned int i;
1003
1004 // Operands[GAIdx] is the original GlobalAddress operand, which is
1005 // already copied into tmpOpnds[0].
1006 // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
1007 // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
1008 // so we start from [GAIdx+2]
1009 for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
1010 tmpOpnds.push_back(MI->getOperand(i));
1011
1012 // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
1013 // It is very important that we always remove from the end of Operands[]
1014 // MI->getNumOperands() is at least 2 if program goes to here.
1015 for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
1016 MI->RemoveOperand(i);
1017
1018 for (i = 0; i < tmpOpnds.size(); ++i)
1019 MI->addOperand(tmpOpnds[i]);
1020 }
1021
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001022 return true;
1023}
1024
1025
1026bool
1027HexagonInstrInfo::
1028isProfitableToIfCvt(MachineBasicBlock &MBB,
Kay Tiong Khoof2949212012-06-13 15:53:04 +00001029 unsigned NumCycles,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001030 unsigned ExtraPredCycles,
1031 const BranchProbability &Probability) const {
1032 return true;
1033}
1034
1035
1036bool
1037HexagonInstrInfo::
1038isProfitableToIfCvt(MachineBasicBlock &TMBB,
1039 unsigned NumTCycles,
1040 unsigned ExtraTCycles,
1041 MachineBasicBlock &FMBB,
1042 unsigned NumFCycles,
1043 unsigned ExtraFCycles,
1044 const BranchProbability &Probability) const {
1045 return true;
1046}
1047
Jyotsna Verma84c47102013-05-06 18:49:23 +00001048// Returns true if an instruction is predicated irrespective of the predicate
1049// sense. For example, all of the following will return true.
1050// if (p0) R1 = add(R2, R3)
1051// if (!p0) R1 = add(R2, R3)
1052// if (p0.new) R1 = add(R2, R3)
1053// if (!p0.new) R1 = add(R2, R3)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001054bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoon6f358372012-02-08 18:25:47 +00001055 const uint64_t F = MI->getDesc().TSFlags;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001056
Brendon Cahoon6f358372012-02-08 18:25:47 +00001057 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001058}
1059
Jyotsna Verma84c47102013-05-06 18:49:23 +00001060bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
1061 const uint64_t F = get(Opcode).TSFlags;
1062
1063 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1064}
1065
1066bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
1067 const uint64_t F = MI->getDesc().TSFlags;
1068
1069 assert(isPredicated(MI));
1070 return (!((F >> HexagonII::PredicatedFalsePos) &
1071 HexagonII::PredicatedFalseMask));
1072}
1073
1074bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
1075 const uint64_t F = get(Opcode).TSFlags;
1076
1077 // Make sure that the instruction is predicated.
1078 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1079 return (!((F >> HexagonII::PredicatedFalsePos) &
1080 HexagonII::PredicatedFalseMask));
1081}
1082
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001083bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1084 const uint64_t F = MI->getDesc().TSFlags;
1085
1086 assert(isPredicated(MI));
1087 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1088}
1089
Jyotsna Verma84c47102013-05-06 18:49:23 +00001090bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1091 const uint64_t F = get(Opcode).TSFlags;
1092
1093 assert(isPredicated(Opcode));
1094 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1095}
1096
Jyotsna Verma438cec52013-05-10 20:58:11 +00001097// Returns true, if a ST insn can be promoted to a new-value store.
1098bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
Jyotsna Verma438cec52013-05-10 20:58:11 +00001099 const uint64_t F = MI->getDesc().TSFlags;
1100
1101 return ((F >> HexagonII::mayNVStorePos) &
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001102 HexagonII::mayNVStoreMask);
Jyotsna Verma438cec52013-05-10 20:58:11 +00001103}
1104
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001105bool
1106HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1107 std::vector<MachineOperand> &Pred) const {
1108 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1109 MachineOperand MO = MI->getOperand(oper);
1110 if (MO.isReg() && MO.isDef()) {
1111 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
Craig Topperc7242e02012-04-20 07:30:17 +00001112 if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001113 Pred.push_back(MO);
1114 return true;
1115 }
1116 }
1117 }
1118 return false;
1119}
1120
1121
1122bool
1123HexagonInstrInfo::
1124SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1125 const SmallVectorImpl<MachineOperand> &Pred2) const {
1126 // TODO: Fix this
1127 return false;
1128}
1129
1130
1131//
1132// We indicate that we want to reverse the branch by
1133// inserting a 0 at the beginning of the Cond vector.
1134//
1135bool HexagonInstrInfo::
1136ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001137 if (!Cond.empty() && Cond[0].isMBB())
1138 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001139 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1140 Cond.erase(Cond.begin());
1141 } else {
1142 Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1143 }
1144 return false;
1145}
1146
1147
1148bool HexagonInstrInfo::
1149isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1150 const BranchProbability &Probability) const {
1151 return (NumInstrs <= 4);
1152}
1153
1154bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1155 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001156 default: return false;
Colin LeMahieu14455532015-01-06 16:15:15 +00001157 case Hexagon::L4_return:
1158 case Hexagon::L4_return_t:
1159 case Hexagon::L4_return_f:
1160 case Hexagon::L4_return_tnew_pnt:
1161 case Hexagon::L4_return_fnew_pnt:
1162 case Hexagon::L4_return_tnew_pt:
1163 case Hexagon::L4_return_fnew_pt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001164 return true;
1165 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001166}
1167
1168
1169bool HexagonInstrInfo::
1170isValidOffset(const int Opcode, const int Offset) const {
1171 // This function is to check whether the "Offset" is in the correct range of
1172 // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1173 // inserted to calculate the final address. Due to this reason, the function
1174 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00001175 // We used to assert if the offset was not properly aligned, however,
1176 // there are cases where a misaligned pointer recast can cause this
1177 // problem, and we need to allow for it. The front end warns of such
1178 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001179
1180 switch(Opcode) {
1181
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001182 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001183 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001184 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1185 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1186
Colin LeMahieu947cd702014-12-23 20:44:59 +00001187 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001188 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001189 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1190 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1191
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001192 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001193 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001194 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001195 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1196 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1197
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001198 case Hexagon::L2_loadrb_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001199 case Hexagon::S2_storerb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001200 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001201 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1202 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1203
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001204 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001205 case Hexagon::TFR_FI:
1206 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1207 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1208
Colin LeMahieudacf0572015-01-05 21:36:38 +00001209 case Hexagon::L4_iadd_memopw_io:
1210 case Hexagon::L4_isub_memopw_io:
1211 case Hexagon::L4_add_memopw_io:
1212 case Hexagon::L4_sub_memopw_io:
1213 case Hexagon::L4_and_memopw_io:
1214 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001215 return (0 <= Offset && Offset <= 255);
1216
Colin LeMahieudacf0572015-01-05 21:36:38 +00001217 case Hexagon::L4_iadd_memoph_io:
1218 case Hexagon::L4_isub_memoph_io:
1219 case Hexagon::L4_add_memoph_io:
1220 case Hexagon::L4_sub_memoph_io:
1221 case Hexagon::L4_and_memoph_io:
1222 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001223 return (0 <= Offset && Offset <= 127);
1224
Colin LeMahieudacf0572015-01-05 21:36:38 +00001225 case Hexagon::L4_iadd_memopb_io:
1226 case Hexagon::L4_isub_memopb_io:
1227 case Hexagon::L4_add_memopb_io:
1228 case Hexagon::L4_sub_memopb_io:
1229 case Hexagon::L4_and_memopb_io:
1230 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001231 return (0 <= Offset && Offset <= 63);
1232
1233 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1234 // any size. Later pass knows how to handle it.
1235 case Hexagon::STriw_pred:
1236 case Hexagon::LDriw_pred:
1237 return true;
1238
Colin LeMahieu5ccbb122014-12-19 00:06:53 +00001239 case Hexagon::J2_loop0i:
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +00001240 return isUInt<10>(Offset);
1241
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001242 // INLINEASM is very special.
1243 case Hexagon::INLINEASM:
1244 return true;
1245 }
1246
Benjamin Kramerb6684012011-12-27 11:41:05 +00001247 llvm_unreachable("No offset range is defined for this opcode. "
1248 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001249}
1250
1251
1252//
1253// Check if the Offset is a valid auto-inc imm by Load/Store Type.
1254//
1255bool HexagonInstrInfo::
1256isValidAutoIncImm(const EVT VT, const int Offset) const {
1257
1258 if (VT == MVT::i64) {
1259 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1260 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1261 (Offset & 0x7) == 0);
1262 }
1263 if (VT == MVT::i32) {
1264 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1265 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1266 (Offset & 0x3) == 0);
1267 }
1268 if (VT == MVT::i16) {
1269 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1270 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1271 (Offset & 0x1) == 0);
1272 }
1273 if (VT == MVT::i8) {
1274 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1275 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1276 }
Craig Toppere55c5562012-02-07 02:50:20 +00001277 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001278}
1279
1280
1281bool HexagonInstrInfo::
1282isMemOp(const MachineInstr *MI) const {
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001283// return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1284
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001285 switch (MI->getOpcode())
1286 {
Colin LeMahieudacf0572015-01-05 21:36:38 +00001287 default: return false;
1288 case Hexagon::L4_iadd_memopw_io:
1289 case Hexagon::L4_isub_memopw_io:
1290 case Hexagon::L4_add_memopw_io:
1291 case Hexagon::L4_sub_memopw_io:
1292 case Hexagon::L4_and_memopw_io:
1293 case Hexagon::L4_or_memopw_io:
1294 case Hexagon::L4_iadd_memoph_io:
1295 case Hexagon::L4_isub_memoph_io:
1296 case Hexagon::L4_add_memoph_io:
1297 case Hexagon::L4_sub_memoph_io:
1298 case Hexagon::L4_and_memoph_io:
1299 case Hexagon::L4_or_memoph_io:
1300 case Hexagon::L4_iadd_memopb_io:
1301 case Hexagon::L4_isub_memopb_io:
1302 case Hexagon::L4_add_memopb_io:
1303 case Hexagon::L4_sub_memopb_io:
1304 case Hexagon::L4_and_memopb_io:
1305 case Hexagon::L4_or_memopb_io:
1306 case Hexagon::L4_ior_memopb_io:
1307 case Hexagon::L4_ior_memoph_io:
1308 case Hexagon::L4_ior_memopw_io:
1309 case Hexagon::L4_iand_memopb_io:
1310 case Hexagon::L4_iand_memoph_io:
1311 case Hexagon::L4_iand_memopw_io:
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001312 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001313 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001314 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001315}
1316
1317
1318bool HexagonInstrInfo::
1319isSpillPredRegOp(const MachineInstr *MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001320 switch (MI->getOpcode()) {
1321 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001322 case Hexagon::STriw_pred :
1323 case Hexagon::LDriw_pred :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001324 return true;
Sirish Pande2c7bf002012-04-23 17:49:28 +00001325 }
Sirish Pande4bd20c52012-05-12 05:10:30 +00001326}
1327
1328bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1329 switch (MI->getOpcode()) {
Sirish Pande8bb97452012-05-12 05:54:15 +00001330 default: return false;
Colin LeMahieu902157c2014-11-25 18:20:52 +00001331 case Hexagon::C2_cmpeq:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001332 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001333 case Hexagon::C2_cmpgt:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001334 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001335 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001336 case Hexagon::C2_cmpgtui:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001337 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +00001338 }
Sirish Pande2c7bf002012-04-23 17:49:28 +00001339}
1340
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001341bool HexagonInstrInfo::
1342isConditionalTransfer (const MachineInstr *MI) const {
1343 switch (MI->getOpcode()) {
1344 default: return false;
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001345 case Hexagon::A2_tfrt:
1346 case Hexagon::A2_tfrf:
1347 case Hexagon::C2_cmoveit:
1348 case Hexagon::C2_cmoveif:
1349 case Hexagon::A2_tfrtnew:
1350 case Hexagon::A2_tfrfnew:
1351 case Hexagon::C2_cmovenewit:
1352 case Hexagon::C2_cmovenewif:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001353 return true;
1354 }
1355}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001356
1357bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001358 switch (MI->getOpcode())
1359 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001360 default: return false;
Colin LeMahieuefa74e02014-11-18 20:28:11 +00001361 case Hexagon::A2_paddf:
1362 case Hexagon::A2_paddfnew:
1363 case Hexagon::A2_paddt:
1364 case Hexagon::A2_paddtnew:
Colin LeMahieu44fd1c82014-11-18 22:45:47 +00001365 case Hexagon::A2_pandf:
1366 case Hexagon::A2_pandfnew:
1367 case Hexagon::A2_pandt:
1368 case Hexagon::A2_pandtnew:
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001369 case Hexagon::A4_paslhf:
1370 case Hexagon::A4_paslhfnew:
1371 case Hexagon::A4_paslht:
1372 case Hexagon::A4_paslhtnew:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001373 case Hexagon::A4_pasrhf:
1374 case Hexagon::A4_pasrhfnew:
1375 case Hexagon::A4_pasrht:
1376 case Hexagon::A4_pasrhtnew:
Colin LeMahieu21866542014-11-19 22:58:04 +00001377 case Hexagon::A2_porf:
1378 case Hexagon::A2_porfnew:
1379 case Hexagon::A2_port:
1380 case Hexagon::A2_portnew:
Colin LeMahieue88447d2014-11-21 21:19:18 +00001381 case Hexagon::A2_psubf:
1382 case Hexagon::A2_psubfnew:
1383 case Hexagon::A2_psubt:
1384 case Hexagon::A2_psubtnew:
Colin LeMahieuac006432014-11-19 23:22:23 +00001385 case Hexagon::A2_pxorf:
1386 case Hexagon::A2_pxorfnew:
1387 case Hexagon::A2_pxort:
1388 case Hexagon::A2_pxortnew:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001389 case Hexagon::A4_psxthf:
1390 case Hexagon::A4_psxthfnew:
1391 case Hexagon::A4_psxtht:
1392 case Hexagon::A4_psxthtnew:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001393 case Hexagon::A4_psxtbf:
1394 case Hexagon::A4_psxtbfnew:
1395 case Hexagon::A4_psxtbt:
1396 case Hexagon::A4_psxtbtnew:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001397 case Hexagon::A4_pzxtbf:
1398 case Hexagon::A4_pzxtbfnew:
1399 case Hexagon::A4_pzxtbt:
1400 case Hexagon::A4_pzxtbtnew:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001401 case Hexagon::A4_pzxthf:
1402 case Hexagon::A4_pzxthfnew:
1403 case Hexagon::A4_pzxtht:
1404 case Hexagon::A4_pzxthtnew:
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001405 case Hexagon::A2_paddit:
1406 case Hexagon::A2_paddif:
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001407 case Hexagon::C2_ccombinewt:
1408 case Hexagon::C2_ccombinewf:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001409 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001410 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001411}
1412
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001413bool HexagonInstrInfo::
1414isConditionalLoad (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001415 switch (MI->getOpcode())
1416 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001417 default: return false;
Colin LeMahieu947cd702014-12-23 20:44:59 +00001418 case Hexagon::L2_ploadrdt_io :
1419 case Hexagon::L2_ploadrdf_io:
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001420 case Hexagon::L2_ploadrit_io:
1421 case Hexagon::L2_ploadrif_io:
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001422 case Hexagon::L2_ploadrht_io:
1423 case Hexagon::L2_ploadrhf_io:
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001424 case Hexagon::L2_ploadrbt_io:
1425 case Hexagon::L2_ploadrbf_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001426 case Hexagon::L2_ploadruht_io:
1427 case Hexagon::L2_ploadruhf_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001428 case Hexagon::L2_ploadrubt_io:
1429 case Hexagon::L2_ploadrubf_io:
Colin LeMahieu9161d472014-12-30 18:58:47 +00001430 case Hexagon::L2_ploadrdt_pi:
1431 case Hexagon::L2_ploadrdf_pi:
1432 case Hexagon::L2_ploadrit_pi:
1433 case Hexagon::L2_ploadrif_pi:
1434 case Hexagon::L2_ploadrht_pi:
1435 case Hexagon::L2_ploadrhf_pi:
1436 case Hexagon::L2_ploadrbt_pi:
1437 case Hexagon::L2_ploadrbf_pi:
1438 case Hexagon::L2_ploadruht_pi:
1439 case Hexagon::L2_ploadruhf_pi:
1440 case Hexagon::L2_ploadrubt_pi:
1441 case Hexagon::L2_ploadrubf_pi:
Colin LeMahieu9161d472014-12-30 18:58:47 +00001442 case Hexagon::L4_ploadrdt_rr:
1443 case Hexagon::L4_ploadrdf_rr:
1444 case Hexagon::L4_ploadrbt_rr:
1445 case Hexagon::L4_ploadrbf_rr:
1446 case Hexagon::L4_ploadrubt_rr:
1447 case Hexagon::L4_ploadrubf_rr:
1448 case Hexagon::L4_ploadrht_rr:
1449 case Hexagon::L4_ploadrhf_rr:
1450 case Hexagon::L4_ploadruht_rr:
1451 case Hexagon::L4_ploadruhf_rr:
1452 case Hexagon::L4_ploadrit_rr:
1453 case Hexagon::L4_ploadrif_rr:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001454 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001455 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001456}
Andrew Trickd06df962012-02-01 22:13:57 +00001457
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001458// Returns true if an instruction is a conditional store.
1459//
1460// Note: It doesn't include conditional new-value stores as they can't be
1461// converted to .new predicate.
1462//
1463// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1464// ^ ^
1465// / \ (not OK. it will cause new-value store to be
1466// / X conditional on p0.new while R2 producer is
1467// / \ on p0)
1468// / \.
1469// p.new store p.old NV store
1470// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1471// ^ ^
1472// \ /
1473// \ /
1474// \ /
1475// p.old store
1476// [if (p0)memw(R0+#0)=R2]
1477//
1478// The above diagram shows the steps involoved in the conversion of a predicated
1479// store instruction to its .new predicated new-value form.
1480//
1481// The following set of instructions further explains the scenario where
1482// conditional new-value store becomes invalid when promoted to .new predicate
1483// form.
1484//
1485// { 1) if (p0) r0 = add(r1, r2)
1486// 2) p0 = cmp.eq(r3, #0) }
1487//
1488// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1489// the first two instructions because in instr 1, r0 is conditional on old value
1490// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1491// is not valid for new-value stores.
1492bool HexagonInstrInfo::
1493isConditionalStore (const MachineInstr* MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001494 switch (MI->getOpcode())
1495 {
1496 default: return false;
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001497 case Hexagon::S4_storeirbt_io:
1498 case Hexagon::S4_storeirbf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001499 case Hexagon::S4_pstorerbt_rr:
1500 case Hexagon::S4_pstorerbf_rr:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001501 case Hexagon::S2_pstorerbt_io:
1502 case Hexagon::S2_pstorerbf_io:
Colin LeMahieu3d34afb2014-12-29 19:42:14 +00001503 case Hexagon::S2_pstorerbt_pi:
1504 case Hexagon::S2_pstorerbf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001505 case Hexagon::S2_pstorerdt_io:
1506 case Hexagon::S2_pstorerdf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001507 case Hexagon::S4_pstorerdt_rr:
1508 case Hexagon::S4_pstorerdf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001509 case Hexagon::S2_pstorerdt_pi:
1510 case Hexagon::S2_pstorerdf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001511 case Hexagon::S2_pstorerht_io:
1512 case Hexagon::S2_pstorerhf_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001513 case Hexagon::S4_storeirht_io:
1514 case Hexagon::S4_storeirhf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001515 case Hexagon::S4_pstorerht_rr:
1516 case Hexagon::S4_pstorerhf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001517 case Hexagon::S2_pstorerht_pi:
1518 case Hexagon::S2_pstorerhf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001519 case Hexagon::S2_pstorerit_io:
1520 case Hexagon::S2_pstorerif_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001521 case Hexagon::S4_storeirit_io:
1522 case Hexagon::S4_storeirif_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001523 case Hexagon::S4_pstorerit_rr:
1524 case Hexagon::S4_pstorerif_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001525 case Hexagon::S2_pstorerit_pi:
1526 case Hexagon::S2_pstorerif_pi:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001527
1528 // V4 global address store before promoting to dot new.
Colin LeMahieu14455532015-01-06 16:15:15 +00001529 case Hexagon::S4_pstorerdt_abs:
1530 case Hexagon::S4_pstorerdf_abs:
1531 case Hexagon::S4_pstorerbt_abs:
1532 case Hexagon::S4_pstorerbf_abs:
1533 case Hexagon::S4_pstorerht_abs:
1534 case Hexagon::S4_pstorerhf_abs:
1535 case Hexagon::S4_pstorerit_abs:
1536 case Hexagon::S4_pstorerif_abs:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001537 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001538
1539 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1540 // from the "Conditional Store" list. Because a predicated new value store
1541 // would NOT be promoted to a double dot new store. See diagram below:
1542 // This function returns yes for those stores that are predicated but not
1543 // yet promoted to predicate dot new instructions.
1544 //
1545 // +---------------------+
1546 // /-----| if (p0) memw(..)=r0 |---------\~
1547 // || +---------------------+ ||
1548 // promote || /\ /\ || promote
1549 // || /||\ /||\ ||
1550 // \||/ demote || \||/
1551 // \/ || || \/
1552 // +-------------------------+ || +-------------------------+
1553 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1554 // +-------------------------+ || +-------------------------+
1555 // || || ||
1556 // || demote \||/
1557 // promote || \/ NOT possible
1558 // || || /\~
1559 // \||/ || /||\~
1560 // \/ || ||
1561 // +-----------------------------+
1562 // | if (p0.new) memw(..)=r0.new |
1563 // +-----------------------------+
1564 // Double Dot New Store
1565 //
1566 }
1567}
1568
Jyotsna Verma84c47102013-05-06 18:49:23 +00001569
1570bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1571 if (isNewValue(MI) && isBranch(MI))
1572 return true;
1573 return false;
1574}
1575
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001576bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1577 return (getAddrMode(MI) == HexagonII::PostInc);
1578}
1579
Jyotsna Verma84c47102013-05-06 18:49:23 +00001580bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1581 const uint64_t F = MI->getDesc().TSFlags;
1582 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1583}
1584
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001585// Returns true, if any one of the operands is a dot new
1586// insn, whether it is predicated dot new or register dot new.
1587bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1588 return (isNewValueInst(MI) ||
1589 (isPredicated(MI) && isPredicatedNew(MI)));
1590}
1591
Jyotsna Verma438cec52013-05-10 20:58:11 +00001592// Returns the most basic instruction for the .new predicated instructions and
1593// new-value stores.
1594// For example, all of the following instructions will be converted back to the
1595// same instruction:
1596// 1) if (p0.new) memw(R0+#0) = R1.new --->
1597// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1598// 3) if (p0.new) memw(R0+#0) = R1 --->
1599//
1600
1601int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1602 int NewOp = opc;
1603 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1604 NewOp = Hexagon::getPredOldOpcode(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001605 assert(NewOp >= 0 &&
1606 "Couldn't change predicate new instruction to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001607 }
1608
Alp Tokerf907b892013-12-05 05:44:44 +00001609 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
Jyotsna Verma438cec52013-05-10 20:58:11 +00001610 NewOp = Hexagon::getNonNVStore(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001611 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001612 }
1613 return NewOp;
1614}
1615
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001616// Return the new value instruction for a given store.
1617int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1618 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1619 if (NVOpcode >= 0) // Valid new-value store instruction.
1620 return NVOpcode;
1621
1622 switch (MI->getOpcode()) {
1623 default: llvm_unreachable("Unknown .new type");
Colin LeMahieuc0434462015-02-04 17:52:06 +00001624 case Hexagon::S4_storerb_ur:
1625 return Hexagon::S4_storerbnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001626
Colin LeMahieuc0434462015-02-04 17:52:06 +00001627 case Hexagon::S4_storerh_ur:
1628 return Hexagon::S4_storerhnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001629
Colin LeMahieuc0434462015-02-04 17:52:06 +00001630 case Hexagon::S4_storeri_ur:
1631 return Hexagon::S4_storerinew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001632
Krzysztof Parzyszek47ab1f22015-03-18 16:23:44 +00001633 case Hexagon::S2_storerb_pci:
1634 return Hexagon::S2_storerb_pci;
1635
1636 case Hexagon::S2_storeri_pci:
1637 return Hexagon::S2_storeri_pci;
1638
1639 case Hexagon::S2_storerh_pci:
1640 return Hexagon::S2_storerh_pci;
1641
1642 case Hexagon::S2_storerd_pci:
1643 return Hexagon::S2_storerd_pci;
1644
1645 case Hexagon::S2_storerf_pci:
1646 return Hexagon::S2_storerf_pci;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001647 }
1648 return 0;
1649}
1650
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001651// Return .new predicate version for an instruction.
1652int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1653 const MachineBranchProbabilityInfo
1654 *MBPI) const {
1655
1656 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1657 if (NewOpcode >= 0) // Valid predicate new instruction
1658 return NewOpcode;
1659
1660 switch (MI->getOpcode()) {
1661 default: llvm_unreachable("Unknown .new type");
1662 // Condtional Jumps
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001663 case Hexagon::J2_jumpt:
1664 case Hexagon::J2_jumpf:
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001665 return getDotNewPredJumpOp(MI, MBPI);
1666
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001667 case Hexagon::J2_jumprt:
1668 return Hexagon::J2_jumptnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001669
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001670 case Hexagon::J2_jumprf:
1671 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001672
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001673 case Hexagon::JMPrett:
1674 return Hexagon::J2_jumprtnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001675
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001676 case Hexagon::JMPretf:
1677 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001678
1679
1680 // Conditional combine
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001681 case Hexagon::C2_ccombinewt:
1682 return Hexagon::C2_ccombinewnewt;
1683 case Hexagon::C2_ccombinewf:
1684 return Hexagon::C2_ccombinewnewf;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001685 }
1686}
1687
1688
Jyotsna Verma84256432013-03-01 17:37:13 +00001689unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1690 const uint64_t F = MI->getDesc().TSFlags;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001691
Jyotsna Verma84256432013-03-01 17:37:13 +00001692 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1693}
1694
1695/// immediateExtend - Changes the instruction in place to one using an immediate
1696/// extender.
1697void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1698 assert((isExtendable(MI)||isConstExtended(MI)) &&
1699 "Instruction must be extendable");
1700 // Find which operand is extendable.
1701 short ExtOpNum = getCExtOpNum(MI);
1702 MachineOperand &MO = MI->getOperand(ExtOpNum);
1703 // This needs to be something we understand.
1704 assert((MO.isMBB() || MO.isImm()) &&
1705 "Branch with unknown extendable field type");
1706 // Mark given operand as extended.
1707 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1708}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001709
Eric Christopher143f02c2014-10-09 01:59:35 +00001710DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1711 const TargetSubtargetInfo &STI) const {
1712 const InstrItineraryData *II = STI.getInstrItineraryData();
1713 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
Andrew Trickd06df962012-02-01 22:13:57 +00001714}
1715
1716bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1717 const MachineBasicBlock *MBB,
1718 const MachineFunction &MF) const {
1719 // Debug info is never a scheduling boundary. It's necessary to be explicit
1720 // due to the special treatment of IT instructions below, otherwise a
1721 // dbg_value followed by an IT will result in the IT instruction being
1722 // considered a scheduling hazard, which is wrong. It should be the actual
1723 // instruction preceding the dbg_value instruction(s), just like it is
1724 // when debug info is not present.
1725 if (MI->isDebugValue())
1726 return false;
1727
1728 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001729 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
Andrew Trickd06df962012-02-01 22:13:57 +00001730 return true;
1731
1732 return false;
1733}
Jyotsna Verma84256432013-03-01 17:37:13 +00001734
1735bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001736 const uint64_t F = MI->getDesc().TSFlags;
1737 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1738 if (isExtended) // Instruction must be extended.
1739 return true;
1740
1741 unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1742 & HexagonII::ExtendableMask;
1743 if (!isExtendable)
1744 return false;
1745
1746 short ExtOpNum = getCExtOpNum(MI);
1747 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1748 // Use MO operand flags to determine if MO
1749 // has the HMOTF_ConstExtended flag set.
1750 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1751 return true;
1752 // If this is a Machine BB address we are talking about, and it is
1753 // not marked as extended, say so.
1754 if (MO.isMBB())
1755 return false;
1756
1757 // We could be using an instruction with an extendable immediate and shoehorn
1758 // a global address into it. If it is a global address it will be constant
1759 // extended. We do this for COMBINE.
1760 // We currently only handle isGlobal() because it is the only kind of
1761 // object we are going to end up with here for now.
1762 // In the future we probably should add isSymbol(), etc.
Colin LeMahieu2efa2d02015-03-09 21:48:13 +00001763 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress())
Jyotsna Verma84256432013-03-01 17:37:13 +00001764 return true;
1765
1766 // If the extendable operand is not 'Immediate' type, the instruction should
1767 // have 'isExtended' flag set.
1768 assert(MO.isImm() && "Extendable operand must be Immediate type");
1769
1770 int MinValue = getMinValue(MI);
1771 int MaxValue = getMaxValue(MI);
1772 int ImmValue = MO.getImm();
1773
1774 return (ImmValue < MinValue || ImmValue > MaxValue);
1775}
1776
Jyotsna Verma1d297502013-05-02 15:39:30 +00001777// Returns the opcode to use when converting MI, which is a conditional jump,
1778// into a conditional instruction which uses the .new value of the predicate.
1779// We also use branch probabilities to add a hint to the jump.
1780int
1781HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1782 const
1783 MachineBranchProbabilityInfo *MBPI) const {
1784
1785 // We assume that block can have at most two successors.
1786 bool taken = false;
1787 MachineBasicBlock *Src = MI->getParent();
1788 MachineOperand *BrTarget = &MI->getOperand(1);
1789 MachineBasicBlock *Dst = BrTarget->getMBB();
1790
1791 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1792 if (Prediction >= BranchProbability(1,2))
1793 taken = true;
1794
1795 switch (MI->getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001796 case Hexagon::J2_jumpt:
1797 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1798 case Hexagon::J2_jumpf:
1799 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Jyotsna Verma1d297502013-05-02 15:39:30 +00001800
1801 default:
1802 llvm_unreachable("Unexpected jump instruction.");
1803 }
1804}
Jyotsna Verma84256432013-03-01 17:37:13 +00001805// Returns true if a particular operand is extendable for an instruction.
1806bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1807 unsigned short OperandNum) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001808 const uint64_t F = MI->getDesc().TSFlags;
1809
1810 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1811 == OperandNum;
1812}
1813
1814// Returns Operand Index for the constant extended instruction.
1815unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1816 const uint64_t F = MI->getDesc().TSFlags;
1817 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1818}
1819
1820// Returns the min value that doesn't need to be extended.
1821int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1822 const uint64_t F = MI->getDesc().TSFlags;
1823 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1824 & HexagonII::ExtentSignedMask;
1825 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1826 & HexagonII::ExtentBitsMask;
1827
1828 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001829 return -1U << (bits - 1);
Jyotsna Verma84256432013-03-01 17:37:13 +00001830 else
1831 return 0;
1832}
1833
1834// Returns the max value that doesn't need to be extended.
1835int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1836 const uint64_t F = MI->getDesc().TSFlags;
1837 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1838 & HexagonII::ExtentSignedMask;
1839 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1840 & HexagonII::ExtentBitsMask;
1841
1842 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001843 return ~(-1U << (bits - 1));
Jyotsna Verma84256432013-03-01 17:37:13 +00001844 else
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001845 return ~(-1U << bits);
Jyotsna Verma84256432013-03-01 17:37:13 +00001846}
1847
1848// Returns true if an instruction can be converted into a non-extended
1849// equivalent instruction.
1850bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1851
1852 short NonExtOpcode;
1853 // Check if the instruction has a register form that uses register in place
1854 // of the extended operand, if so return that as the non-extended form.
1855 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1856 return true;
1857
1858 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001859 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001860
1861 switch (getAddrMode(MI)) {
1862 case HexagonII::Absolute :
1863 // Load/store with absolute addressing mode can be converted into
1864 // base+offset mode.
1865 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1866 break;
1867 case HexagonII::BaseImmOffset :
1868 // Load/store with base+offset addressing mode can be converted into
1869 // base+register offset addressing mode. However left shift operand should
1870 // be set to 0.
1871 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1872 break;
1873 default:
1874 return false;
1875 }
1876 if (NonExtOpcode < 0)
1877 return false;
1878 return true;
1879 }
1880 return false;
1881}
1882
1883// Returns opcode of the non-extended equivalent instruction.
1884short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1885
1886 // Check if the instruction has a register form that uses register in place
1887 // of the extended operand, if so return that as the non-extended form.
1888 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1889 if (NonExtOpcode >= 0)
1890 return NonExtOpcode;
1891
1892 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001893 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001894 switch (getAddrMode(MI)) {
1895 case HexagonII::Absolute :
1896 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1897 case HexagonII::BaseImmOffset :
1898 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1899 default:
1900 return -1;
1901 }
1902 }
1903 return -1;
1904}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001905
1906bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001907 return (Opcode == Hexagon::J2_jumpt) ||
1908 (Opcode == Hexagon::J2_jumpf) ||
1909 (Opcode == Hexagon::J2_jumptnewpt) ||
1910 (Opcode == Hexagon::J2_jumpfnewpt) ||
1911 (Opcode == Hexagon::J2_jumpt) ||
1912 (Opcode == Hexagon::J2_jumpf);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001913}
1914
1915bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001916 return (Opcode == Hexagon::J2_jumpf) ||
1917 (Opcode == Hexagon::J2_jumpfnewpt) ||
1918 (Opcode == Hexagon::J2_jumpfnew);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001919}