blob: c76ef340148957697dc1c9f99df580a76022dd2d [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)
65 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Bill Wendling4a7a4082013-06-07 06:19:56 +000066 RI(ST), Subtarget(ST) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000067}
68
69
70/// isLoadFromStackSlot - If the specified machine instruction is a direct
71/// load from a stack slot, return the virtual or physical register number of
72/// the destination along with the FrameIndex of the loaded stack slot. If
73/// not, return 0. This predicate must return 0 if the instruction has
74/// any side effects other than loading from the stack slot.
75unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
76 int &FrameIndex) const {
77
78
79 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000080 default: break;
Colin LeMahieu026e88d2014-12-23 20:02:16 +000081 case Hexagon::L2_loadri_io:
Colin LeMahieu947cd702014-12-23 20:44:59 +000082 case Hexagon::L2_loadrd_io:
Colin LeMahieu8e39cad2014-12-23 17:25:57 +000083 case Hexagon::L2_loadrh_io:
Colin LeMahieu4b1eac42014-12-22 21:40:43 +000084 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +000085 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +000086 if (MI->getOperand(2).isFI() &&
87 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
88 FrameIndex = MI->getOperand(2).getIndex();
89 return MI->getOperand(0).getReg();
90 }
91 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000092 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093 return 0;
94}
95
96
97/// isStoreToStackSlot - If the specified machine instruction is a direct
98/// store to a stack slot, return the virtual or physical register number of
99/// the source reg along with the FrameIndex of the loaded stack slot. If
100/// not, return 0. This predicate must return 0 if the instruction has
101/// any side effects other than storing to the stack slot.
102unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
103 int &FrameIndex) const {
104 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000105 default: break;
Colin LeMahieubda31b42014-12-29 20:44:51 +0000106 case Hexagon::S2_storeri_io:
107 case Hexagon::S2_storerd_io:
108 case Hexagon::S2_storerh_io:
109 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110 if (MI->getOperand(2).isFI() &&
111 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
Sirish Pande8bb97452012-05-12 05:54:15 +0000112 FrameIndex = MI->getOperand(0).getIndex();
113 return MI->getOperand(2).getReg();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114 }
115 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000116 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000117 return 0;
118}
119
120
121unsigned
122HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
123 MachineBasicBlock *FBB,
124 const SmallVectorImpl<MachineOperand> &Cond,
125 DebugLoc DL) const{
126
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000127 int BOpc = Hexagon::J2_jump;
128 int BccOpc = Hexagon::J2_jumpt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129
130 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
131
132 int regPos = 0;
133 // Check if ReverseBranchCondition has asked to reverse this branch
134 // If we want to reverse the branch an odd number of times, we want
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000135 // JMP_f.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000136 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000137 BccOpc = Hexagon::J2_jumpf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000138 regPos = 1;
139 }
140
Craig Topper062a2ba2014-04-25 05:30:21 +0000141 if (!FBB) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000142 if (Cond.empty()) {
143 // Due to a bug in TailMerging/CFG Optimization, we need to add a
144 // special case handling of a predicated jump followed by an
145 // unconditional jump. If not, Tail Merging and CFG Optimization go
146 // into an infinite loop.
147 MachineBasicBlock *NewTBB, *NewFBB;
148 SmallVector<MachineOperand, 4> Cond;
149 MachineInstr *Term = MBB.getFirstTerminator();
150 if (isPredicated(Term) && !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond,
151 false)) {
152 MachineBasicBlock *NextBB =
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000153 std::next(MachineFunction::iterator(&MBB));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000154 if (NewTBB == NextBB) {
155 ReverseBranchCondition(Cond);
156 RemoveBranch(MBB);
Craig Topper062a2ba2014-04-25 05:30:21 +0000157 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000158 }
159 }
160 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
161 } else {
162 BuildMI(&MBB, DL,
163 get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
164 }
165 return 1;
166 }
167
168 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
169 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
170
171 return 2;
172}
173
174
175bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
176 MachineBasicBlock *&TBB,
177 MachineBasicBlock *&FBB,
178 SmallVectorImpl<MachineOperand> &Cond,
179 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000180 TBB = nullptr;
181 FBB = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000182
183 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000184 MachineBasicBlock::instr_iterator I = MBB.instr_end();
185 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000186 return false;
187
188 // A basic block may looks like this:
189 //
190 // [ insn
191 // EH_LABEL
192 // insn
193 // insn
194 // insn
195 // EH_LABEL
196 // insn ]
197 //
198 // It has two succs but does not have a terminator
199 // Don't know how to handle it.
200 do {
201 --I;
202 if (I->isEHLabel())
203 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000204 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000205
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000206 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000207 --I;
208
209 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000210 if (I == MBB.instr_begin())
211 return false;
212 --I;
213 }
214
215 // Delete the JMP if it's equivalent to a fall-through.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000216 if (AllowModify && I->getOpcode() == Hexagon::J2_jump &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000217 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
218 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
219 I->eraseFromParent();
220 I = MBB.instr_end();
221 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000222 return false;
223 --I;
224 }
225 if (!isUnpredicatedTerminator(I))
226 return false;
227
228 // Get the last instruction in the block.
229 MachineInstr *LastInst = I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000230 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000231 // Find one more terminator if present.
232 do {
233 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
234 if (!SecondLastInst)
235 SecondLastInst = I;
236 else
237 // This is a third branch.
238 return true;
239 }
240 if (I == MBB.instr_begin())
241 break;
242 --I;
243 } while(I);
244
245 int LastOpcode = LastInst->getOpcode();
246
247 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
248 bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000249
250 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000251 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000252 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000253 TBB = LastInst->getOperand(0).getMBB();
254 return false;
255 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000256 if (LastOpcode == Hexagon::ENDLOOP0) {
257 TBB = LastInst->getOperand(0).getMBB();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000258 Cond.push_back(LastInst->getOperand(0));
259 return false;
260 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000261 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000262 TBB = LastInst->getOperand(1).getMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000263 if (LastOpcodeHasNot) {
264 Cond.push_back(MachineOperand::CreateImm(0));
265 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000266 Cond.push_back(LastInst->getOperand(0));
267 return false;
268 }
269 // Otherwise, don't know what this is.
270 return true;
271 }
272
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000273 int SecLastOpcode = SecondLastInst->getOpcode();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000274
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000275 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
276 bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000277 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000278 TBB = SecondLastInst->getOperand(1).getMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000279 if (SecLastOpcodeHasNot)
280 Cond.push_back(MachineOperand::CreateImm(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000281 Cond.push_back(SecondLastInst->getOperand(0));
282 FBB = LastInst->getOperand(0).getMBB();
283 return false;
284 }
285
286 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
287 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000288 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000289 TBB = SecondLastInst->getOperand(0).getMBB();
290 I = LastInst;
291 if (AllowModify)
292 I->eraseFromParent();
293 return false;
294 }
295
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000296 // If the block ends with an ENDLOOP, and JMP, handle it.
297 if (SecLastOpcode == Hexagon::ENDLOOP0 &&
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000298 LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000299 TBB = SecondLastInst->getOperand(0).getMBB();
300 Cond.push_back(SecondLastInst->getOperand(0));
301 FBB = LastInst->getOperand(0).getMBB();
302 return false;
303 }
304
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000305 // Otherwise, can't handle this.
306 return true;
307}
308
309
310unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000311 int BOpc = Hexagon::J2_jump;
312 int BccOpc = Hexagon::J2_jumpt;
313 int BccOpcNot = Hexagon::J2_jumpf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000314
315 MachineBasicBlock::iterator I = MBB.end();
316 if (I == MBB.begin()) return 0;
317 --I;
318 if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc &&
319 I->getOpcode() != BccOpcNot)
320 return 0;
321
322 // Remove the branch.
323 I->eraseFromParent();
324
325 I = MBB.end();
326
327 if (I == MBB.begin()) return 1;
328 --I;
329 if (I->getOpcode() != BccOpc && I->getOpcode() != BccOpcNot)
330 return 1;
331
332 // Remove the branch.
333 I->eraseFromParent();
334 return 2;
335}
336
337
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000338/// \brief For a comparison instruction, return the source registers in
339/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
340/// compares against in CmpValue. Return true if the comparison instruction
341/// can be analyzed.
342bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
343 unsigned &SrcReg, unsigned &SrcReg2,
344 int &Mask, int &Value) const {
345 unsigned Opc = MI->getOpcode();
346
347 // Set mask and the first source register.
348 switch (Opc) {
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000349 case Hexagon::C2_cmpeqp:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000350 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000351 case Hexagon::C2_cmpeq:
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000352 case Hexagon::C2_cmpgtp:
353 case Hexagon::C2_cmpgtup:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000354 case Hexagon::C2_cmpgtui:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000355 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000356 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000357 case Hexagon::C2_cmpgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000358 SrcReg = MI->getOperand(1).getReg();
359 Mask = ~0;
360 break;
Colin LeMahieufa947902015-01-14 16:49:12 +0000361 case Hexagon::A4_cmpbeqi:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000362 case Hexagon::A4_cmpbeq:
363 case Hexagon::A4_cmpbgtui:
364 case Hexagon::A4_cmpbgtu:
365 case Hexagon::A4_cmpbgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000366 SrcReg = MI->getOperand(1).getReg();
367 Mask = 0xFF;
368 break;
369 case Hexagon::CMPhEQri_V4:
370 case Hexagon::CMPhEQrr_shl_V4:
371 case Hexagon::CMPhEQrr_xor_V4:
372 case Hexagon::CMPhGTUri_V4:
373 case Hexagon::CMPhGTUrr_V4:
374 case Hexagon::CMPhGTrr_shl_V4:
375 SrcReg = MI->getOperand(1).getReg();
376 Mask = 0xFFFF;
377 break;
378 }
379
380 // Set the value/second source register.
381 switch (Opc) {
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000382 case Hexagon::C2_cmpeqp:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000383 case Hexagon::C2_cmpeq:
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000384 case Hexagon::C2_cmpgtp:
385 case Hexagon::C2_cmpgtup:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000386 case Hexagon::C2_cmpgtu:
387 case Hexagon::C2_cmpgt:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000388 case Hexagon::A4_cmpbeq:
389 case Hexagon::A4_cmpbgtu:
390 case Hexagon::A4_cmpbgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000391 case Hexagon::CMPhEQrr_shl_V4:
392 case Hexagon::CMPhEQrr_xor_V4:
393 case Hexagon::CMPhGTUrr_V4:
394 case Hexagon::CMPhGTrr_shl_V4:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000395 SrcReg2 = MI->getOperand(2).getReg();
396 return true;
397
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000398 case Hexagon::C2_cmpeqi:
399 case Hexagon::C2_cmpgtui:
400 case Hexagon::C2_cmpgti:
Colin LeMahieufa947902015-01-14 16:49:12 +0000401 case Hexagon::A4_cmpbeqi:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000402 case Hexagon::A4_cmpbgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000403 case Hexagon::CMPhEQri_V4:
404 case Hexagon::CMPhGTUri_V4:
405 SrcReg2 = 0;
406 Value = MI->getOperand(2).getImm();
407 return true;
408 }
409
410 return false;
411}
412
413
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000414void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
415 MachineBasicBlock::iterator I, DebugLoc DL,
416 unsigned DestReg, unsigned SrcReg,
417 bool KillSrc) const {
418 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000419 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000420 return;
421 }
422 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000423 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000424 return;
425 }
426 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
427 // Map Pd = Ps to Pd = or(Ps, Ps).
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000428 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000429 DestReg).addReg(SrcReg).addReg(SrcReg);
430 return;
431 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000432 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
433 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000434 // We can have an overlap between single and double reg: r1:0 = r0.
435 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
436 // r1:0 = r0
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000437 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000438 Hexagon::subreg_hireg))).addImm(0);
439 } else {
440 // r1:0 = r1 or no overlap.
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000441 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000442 Hexagon::subreg_loreg))).addReg(SrcReg);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000443 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000444 Hexagon::subreg_hireg))).addImm(0);
445 }
446 return;
447 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000448 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000449 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000450 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000452 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000453 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
454 Hexagon::IntRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000455 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000456 addReg(SrcReg, getKillRegState(KillSrc));
457 return;
458 }
459 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
460 Hexagon::PredRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000461 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000462 addReg(SrcReg, getKillRegState(KillSrc));
463 return;
464 }
Sirish Pande30804c22012-02-15 18:52:27 +0000465
466 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000467}
468
469
470void HexagonInstrInfo::
471storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
472 unsigned SrcReg, bool isKill, int FI,
473 const TargetRegisterClass *RC,
474 const TargetRegisterInfo *TRI) const {
475
476 DebugLoc DL = MBB.findDebugLoc(I);
477 MachineFunction &MF = *MBB.getParent();
478 MachineFrameInfo &MFI = *MF.getFrameInfo();
479 unsigned Align = MFI.getObjectAlignment(FI);
480
481 MachineMemOperand *MMO =
482 MF.getMachineMemOperand(
483 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
484 MachineMemOperand::MOStore,
485 MFI.getObjectSize(FI),
486 Align);
487
Craig Topperc7242e02012-04-20 07:30:17 +0000488 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000489 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000490 .addFrameIndex(FI).addImm(0)
491 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000492 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000493 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000494 .addFrameIndex(FI).addImm(0)
495 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000496 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000497 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
498 .addFrameIndex(FI).addImm(0)
499 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
500 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000501 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000502 }
503}
504
505
506void HexagonInstrInfo::storeRegToAddr(
507 MachineFunction &MF, unsigned SrcReg,
508 bool isKill,
509 SmallVectorImpl<MachineOperand> &Addr,
510 const TargetRegisterClass *RC,
511 SmallVectorImpl<MachineInstr*> &NewMIs) const
512{
Craig Toppere55c5562012-02-07 02:50:20 +0000513 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000514}
515
516
517void HexagonInstrInfo::
518loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
519 unsigned DestReg, int FI,
520 const TargetRegisterClass *RC,
521 const TargetRegisterInfo *TRI) const {
522 DebugLoc DL = MBB.findDebugLoc(I);
523 MachineFunction &MF = *MBB.getParent();
524 MachineFrameInfo &MFI = *MF.getFrameInfo();
525 unsigned Align = MFI.getObjectAlignment(FI);
526
527 MachineMemOperand *MMO =
528 MF.getMachineMemOperand(
529 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
530 MachineMemOperand::MOLoad,
531 MFI.getObjectSize(FI),
532 Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000533 if (RC == &Hexagon::IntRegsRegClass) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000534 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000535 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000536 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000537 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000538 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000539 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000540 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
541 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
542 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000543 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000544 }
545}
546
547
548void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
549 SmallVectorImpl<MachineOperand> &Addr,
550 const TargetRegisterClass *RC,
551 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Craig Toppere55c5562012-02-07 02:50:20 +0000552 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000553}
554
555
556MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
557 MachineInstr* MI,
558 const SmallVectorImpl<unsigned> &Ops,
559 int FI) const {
560 // Hexagon_TODO: Implement.
Craig Topper062a2ba2014-04-25 05:30:21 +0000561 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000562}
563
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000564unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
565
566 MachineRegisterInfo &RegInfo = MF->getRegInfo();
567 const TargetRegisterClass *TRC;
Sirish Pande69295b82012-05-10 20:20:25 +0000568 if (VT == MVT::i1) {
Craig Topperc7242e02012-04-20 07:30:17 +0000569 TRC = &Hexagon::PredRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000570 } else if (VT == MVT::i32 || VT == MVT::f32) {
Craig Topperc7242e02012-04-20 07:30:17 +0000571 TRC = &Hexagon::IntRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000572 } else if (VT == MVT::i64 || VT == MVT::f64) {
Craig Topperc7242e02012-04-20 07:30:17 +0000573 TRC = &Hexagon::DoubleRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000574 } else {
Benjamin Kramerb6684012011-12-27 11:41:05 +0000575 llvm_unreachable("Cannot handle this register class");
Sirish Pande69295b82012-05-10 20:20:25 +0000576 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000577
578 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
579 return NewReg;
580}
581
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000582bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000583 // Constant extenders are allowed only for V4 and above.
584 if (!Subtarget.hasV4TOps())
585 return false;
586
587 const MCInstrDesc &MID = MI->getDesc();
588 const uint64_t F = MID.TSFlags;
589 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
590 return true;
591
592 // TODO: This is largely obsolete now. Will need to be removed
593 // in consecutive patches.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000594 switch(MI->getOpcode()) {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000595 // TFR_FI Remains a special case.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000596 case Hexagon::TFR_FI:
597 return true;
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000598 default:
599 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000600 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000601 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000602}
603
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000604// This returns true in two cases:
605// - The OP code itself indicates that this is an extended instruction.
606// - One of MOs has been marked with HMOTF_ConstExtended flag.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000607bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000608 // First check if this is permanently extended op code.
609 const uint64_t F = MI->getDesc().TSFlags;
610 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
611 return true;
612 // Use MO operand flags to determine if one of MI's operands
613 // has HMOTF_ConstExtended flag set.
614 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
615 E = MI->operands_end(); I != E; ++I) {
616 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Sirish Pande69295b82012-05-10 20:20:25 +0000617 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000618 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000619 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000620}
621
Jyotsna Verma84c47102013-05-06 18:49:23 +0000622bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
623 return MI->getDesc().isBranch();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000624}
625
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000626bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
627 if (isNewValueJump(MI))
628 return true;
629
630 if (isNewValueStore(MI))
631 return true;
632
633 return false;
634}
635
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000636bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
637 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
638}
Andrew Trickd06df962012-02-01 22:13:57 +0000639
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000640bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
641 bool isPred = MI->getDesc().isPredicable();
642
643 if (!isPred)
644 return false;
645
646 const int Opc = MI->getOpcode();
647
648 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000649 case Hexagon::A2_tfrsi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000650 return isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000651
Colin LeMahieubda31b42014-12-29 20:44:51 +0000652 case Hexagon::S2_storerd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000653 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000654
Colin LeMahieubda31b42014-12-29 20:44:51 +0000655 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000656 case Hexagon::S2_storerinew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000657 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000658
Colin LeMahieubda31b42014-12-29 20:44:51 +0000659 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000660 case Hexagon::S2_storerhnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000661 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000662
Colin LeMahieubda31b42014-12-29 20:44:51 +0000663 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000664 case Hexagon::S2_storerbnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000665 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000666
Colin LeMahieu947cd702014-12-23 20:44:59 +0000667 case Hexagon::L2_loadrd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000668 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000669
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000670 case Hexagon::L2_loadri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000671 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000672
Colin LeMahieu8e39cad2014-12-23 17:25:57 +0000673 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +0000674 case Hexagon::L2_loadruh_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000675 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000676
Colin LeMahieu4b1eac42014-12-22 21:40:43 +0000677 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +0000678 case Hexagon::L2_loadrub_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000679 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000680
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000681 case Hexagon::L2_loadrd_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000682 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000683
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000684 case Hexagon::L2_loadri_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000685 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000686
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000687 case Hexagon::L2_loadrh_pi:
688 case Hexagon::L2_loadruh_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000689 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000690
Colin LeMahieu96976a12014-12-26 18:57:13 +0000691 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +0000692 case Hexagon::L2_loadrub_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000693 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000694
Colin LeMahieu2bad4a72014-12-30 21:01:38 +0000695 case Hexagon::S4_storeirb_io:
696 case Hexagon::S4_storeirh_io:
697 case Hexagon::S4_storeiri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000698 return (isUInt<6>(MI->getOperand(1).getImm()) &&
699 isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000700
701 case Hexagon::ADD_ri:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000702 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000703
Colin LeMahieu3b3197e2014-11-24 17:44:19 +0000704 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +0000705 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +0000706 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +0000707 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +0000708 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +0000709 case Hexagon::A2_zxth:
Sirish Pande8bb97452012-05-12 05:54:15 +0000710 return Subtarget.hasV4TOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000711 }
712
713 return true;
714}
715
Sirish Pande8bb97452012-05-12 05:54:15 +0000716// This function performs the following inversiones:
717//
718// cPt ---> cNotPt
719// cNotPt ---> cPt
720//
Sirish Pande30804c22012-02-15 18:52:27 +0000721unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
Jyotsna Verma84c47102013-05-06 18:49:23 +0000722 int InvPredOpcode;
723 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
724 : Hexagon::getTruePredOpcode(Opc);
725 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
726 return InvPredOpcode;
727
Sirish Pande30804c22012-02-15 18:52:27 +0000728 switch(Opc) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000729 default: llvm_unreachable("Unexpected predicated instruction");
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000730 case Hexagon::C2_ccombinewt:
731 return Hexagon::C2_ccombinewf;
732 case Hexagon::C2_ccombinewf:
733 return Hexagon::C2_ccombinewt;
Sirish Pande30804c22012-02-15 18:52:27 +0000734
Jyotsna Verma978e9722013-05-09 18:25:44 +0000735 // Dealloc_return.
Colin LeMahieu14455532015-01-06 16:15:15 +0000736 case Hexagon::L4_return_t:
737 return Hexagon::L4_return_f;
738 case Hexagon::L4_return_f:
739 return Hexagon::L4_return_t;
Sirish Pande30804c22012-02-15 18:52:27 +0000740 }
741}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000742
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000743// New Value Store instructions.
744bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
745 const uint64_t F = MI->getDesc().TSFlags;
746
747 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
748}
749
750bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
751 const uint64_t F = get(Opcode).TSFlags;
752
753 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
754}
Andrew Trickd06df962012-02-01 22:13:57 +0000755
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000756int HexagonInstrInfo::
757getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
Pranav Bhandarkar34b60182012-11-01 19:13:23 +0000758 enum Hexagon::PredSense inPredSense;
759 inPredSense = invertPredicate ? Hexagon::PredSense_false :
760 Hexagon::PredSense_true;
761 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
762 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
763 return CondOpcode;
764
765 // This switch case will be removed once all the instructions have been
766 // modified to use relation maps.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000767 switch(Opc) {
Sirish Pande69295b82012-05-10 20:20:25 +0000768 case Hexagon::TFRI_f:
769 return !invertPredicate ? Hexagon::TFRI_cPt_f :
770 Hexagon::TFRI_cNotPt_f;
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000771 case Hexagon::A2_combinew:
772 return !invertPredicate ? Hexagon::C2_ccombinewt :
773 Hexagon::C2_ccombinewf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000775 // Word.
Jyotsna Verma978e9722013-05-09 18:25:44 +0000776 case Hexagon::STriw_f:
Colin LeMahieubda31b42014-12-29 20:44:51 +0000777 return !invertPredicate ? Hexagon::S2_pstorerit_io:
778 Hexagon::S2_pstorerif_io;
Jyotsna Verma978e9722013-05-09 18:25:44 +0000779 case Hexagon::STriw_indexed_f:
Colin LeMahieubda31b42014-12-29 20:44:51 +0000780 return !invertPredicate ? Hexagon::S2_pstorerit_io:
781 Hexagon::S2_pstorerif_io;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000782
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000783 // DEALLOC_RETURN.
Colin LeMahieu14455532015-01-06 16:15:15 +0000784 case Hexagon::L4_return:
785 return !invertPredicate ? Hexagon::L4_return_t:
786 Hexagon::L4_return_f;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000787 }
Benjamin Kramerb6684012011-12-27 11:41:05 +0000788 llvm_unreachable("Unexpected predicable instruction");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000789}
790
791
792bool HexagonInstrInfo::
793PredicateInstruction(MachineInstr *MI,
794 const SmallVectorImpl<MachineOperand> &Cond) const {
795 int Opc = MI->getOpcode();
796 assert (isPredicable(MI) && "Expected predicable instruction");
797 bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
798 (Cond[0].getImm() == 0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000799
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000800 // This will change MI's opcode to its predicate version.
801 // However, its operand list is still the old one, i.e. the
802 // non-predicate one.
803 MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
804
805 int oper = -1;
806 unsigned int GAIdx = 0;
807
808 // Indicates whether the current MI has a GlobalAddress operand
809 bool hasGAOpnd = false;
810 std::vector<MachineOperand> tmpOpnds;
811
812 // Indicates whether we need to shift operands to right.
813 bool needShift = true;
814
815 // The predicate is ALWAYS the FIRST input operand !!!
816 if (MI->getNumOperands() == 0) {
817 // The non-predicate version of MI does not take any operands,
818 // i.e. no outs and no ins. In this condition, the predicate
819 // operand will be directly placed at Operands[0]. No operand
820 // shift is needed.
821 // Example: BARRIER
822 needShift = false;
823 oper = -1;
824 }
825 else if ( MI->getOperand(MI->getNumOperands()-1).isReg()
826 && MI->getOperand(MI->getNumOperands()-1).isDef()
827 && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
828 // The non-predicate version of MI does not have any input operands.
829 // In this condition, we extend the length of Operands[] by one and
830 // copy the original last operand to the newly allocated slot.
831 // At this moment, it is just a place holder. Later, we will put
832 // predicate operand directly into it. No operand shift is needed.
833 // Example: r0=BARRIER (this is a faked insn used here for illustration)
834 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
835 needShift = false;
836 oper = MI->getNumOperands() - 2;
837 }
838 else {
839 // We need to right shift all input operands by one. Duplicate the
840 // last operand into the newly allocated slot.
841 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
842 }
843
844 if (needShift)
845 {
846 // Operands[ MI->getNumOperands() - 2 ] has been copied into
847 // Operands[ MI->getNumOperands() - 1 ], so we start from
848 // Operands[ MI->getNumOperands() - 3 ].
849 // oper is a signed int.
850 // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
851 for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
852 {
853 MachineOperand &MO = MI->getOperand(oper);
854
855 // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4] Opnd[5] Opnd[6] Opnd[7]
856 // <Def0> <Def1> <Use0> <Use1> <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
857 // /\~
858 // /||\~
859 // ||
860 // Predicate Operand here
861 if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
862 break;
863 }
864 if (MO.isReg()) {
865 MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
866 MO.isImplicit(), MO.isKill(),
867 MO.isDead(), MO.isUndef(),
868 MO.isDebug());
869 }
870 else if (MO.isImm()) {
871 MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
872 }
873 else if (MO.isGlobal()) {
874 // MI can not have more than one GlobalAddress operand.
875 assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
876
877 // There is no member function called "ChangeToGlobalAddress" in the
878 // MachineOperand class (not like "ChangeToRegister" and
879 // "ChangeToImmediate"). So we have to remove them from Operands[] list
880 // first, and then add them back after we have inserted the predicate
881 // operand. tmpOpnds[] is to remember these operands before we remove
882 // them.
883 tmpOpnds.push_back(MO);
884
885 // Operands[oper] is a GlobalAddress operand;
886 // Operands[oper+1] has been copied into Operands[oper+2];
887 hasGAOpnd = true;
888 GAIdx = oper;
889 continue;
890 }
891 else {
Craig Topperd3c02f12015-01-05 10:15:49 +0000892 llvm_unreachable("Unexpected operand type");
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000893 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000894 }
895 }
896
897 int regPos = invertJump ? 1 : 0;
898 MachineOperand PredMO = Cond[regPos];
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000899
900 // [oper] now points to the last explicit Def. Predicate operand must be
901 // located at [oper+1]. See diagram above.
902 // This assumes that the predicate is always the first operand,
903 // i.e. Operands[0+numResults], in the set of inputs
904 // It is better to have an assert here to check this. But I don't know how
905 // to write this assert because findFirstPredOperandIdx() would return -1
906 if (oper < -1) oper = -1;
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000907
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000908 MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000909 PredMO.isImplicit(), false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000910 PredMO.isDead(), PredMO.isUndef(),
911 PredMO.isDebug());
912
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000913 MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
914 RegInfo.clearKillFlags(PredMO.getReg());
915
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000916 if (hasGAOpnd)
917 {
918 unsigned int i;
919
920 // Operands[GAIdx] is the original GlobalAddress operand, which is
921 // already copied into tmpOpnds[0].
922 // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
923 // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
924 // so we start from [GAIdx+2]
925 for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
926 tmpOpnds.push_back(MI->getOperand(i));
927
928 // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
929 // It is very important that we always remove from the end of Operands[]
930 // MI->getNumOperands() is at least 2 if program goes to here.
931 for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
932 MI->RemoveOperand(i);
933
934 for (i = 0; i < tmpOpnds.size(); ++i)
935 MI->addOperand(tmpOpnds[i]);
936 }
937
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000938 return true;
939}
940
941
942bool
943HexagonInstrInfo::
944isProfitableToIfCvt(MachineBasicBlock &MBB,
Kay Tiong Khoof2949212012-06-13 15:53:04 +0000945 unsigned NumCycles,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000946 unsigned ExtraPredCycles,
947 const BranchProbability &Probability) const {
948 return true;
949}
950
951
952bool
953HexagonInstrInfo::
954isProfitableToIfCvt(MachineBasicBlock &TMBB,
955 unsigned NumTCycles,
956 unsigned ExtraTCycles,
957 MachineBasicBlock &FMBB,
958 unsigned NumFCycles,
959 unsigned ExtraFCycles,
960 const BranchProbability &Probability) const {
961 return true;
962}
963
Jyotsna Verma84c47102013-05-06 18:49:23 +0000964// Returns true if an instruction is predicated irrespective of the predicate
965// sense. For example, all of the following will return true.
966// if (p0) R1 = add(R2, R3)
967// if (!p0) R1 = add(R2, R3)
968// if (p0.new) R1 = add(R2, R3)
969// if (!p0.new) R1 = add(R2, R3)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000970bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoon6f358372012-02-08 18:25:47 +0000971 const uint64_t F = MI->getDesc().TSFlags;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000972
Brendon Cahoon6f358372012-02-08 18:25:47 +0000973 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000974}
975
Jyotsna Verma84c47102013-05-06 18:49:23 +0000976bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
977 const uint64_t F = get(Opcode).TSFlags;
978
979 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
980}
981
982bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
983 const uint64_t F = MI->getDesc().TSFlags;
984
985 assert(isPredicated(MI));
986 return (!((F >> HexagonII::PredicatedFalsePos) &
987 HexagonII::PredicatedFalseMask));
988}
989
990bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
991 const uint64_t F = get(Opcode).TSFlags;
992
993 // Make sure that the instruction is predicated.
994 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
995 return (!((F >> HexagonII::PredicatedFalsePos) &
996 HexagonII::PredicatedFalseMask));
997}
998
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000999bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1000 const uint64_t F = MI->getDesc().TSFlags;
1001
1002 assert(isPredicated(MI));
1003 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1004}
1005
Jyotsna Verma84c47102013-05-06 18:49:23 +00001006bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1007 const uint64_t F = get(Opcode).TSFlags;
1008
1009 assert(isPredicated(Opcode));
1010 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1011}
1012
Jyotsna Verma438cec52013-05-10 20:58:11 +00001013// Returns true, if a ST insn can be promoted to a new-value store.
1014bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
1015 const HexagonRegisterInfo& QRI = getRegisterInfo();
1016 const uint64_t F = MI->getDesc().TSFlags;
1017
1018 return ((F >> HexagonII::mayNVStorePos) &
1019 HexagonII::mayNVStoreMask &
1020 QRI.Subtarget.hasV4TOps());
1021}
1022
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001023bool
1024HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1025 std::vector<MachineOperand> &Pred) const {
1026 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1027 MachineOperand MO = MI->getOperand(oper);
1028 if (MO.isReg() && MO.isDef()) {
1029 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
Craig Topperc7242e02012-04-20 07:30:17 +00001030 if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001031 Pred.push_back(MO);
1032 return true;
1033 }
1034 }
1035 }
1036 return false;
1037}
1038
1039
1040bool
1041HexagonInstrInfo::
1042SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1043 const SmallVectorImpl<MachineOperand> &Pred2) const {
1044 // TODO: Fix this
1045 return false;
1046}
1047
1048
1049//
1050// We indicate that we want to reverse the branch by
1051// inserting a 0 at the beginning of the Cond vector.
1052//
1053bool HexagonInstrInfo::
1054ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1055 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1056 Cond.erase(Cond.begin());
1057 } else {
1058 Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1059 }
1060 return false;
1061}
1062
1063
1064bool HexagonInstrInfo::
1065isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1066 const BranchProbability &Probability) const {
1067 return (NumInstrs <= 4);
1068}
1069
1070bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1071 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001072 default: return false;
Colin LeMahieu14455532015-01-06 16:15:15 +00001073 case Hexagon::L4_return:
1074 case Hexagon::L4_return_t:
1075 case Hexagon::L4_return_f:
1076 case Hexagon::L4_return_tnew_pnt:
1077 case Hexagon::L4_return_fnew_pnt:
1078 case Hexagon::L4_return_tnew_pt:
1079 case Hexagon::L4_return_fnew_pt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001080 return true;
1081 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001082}
1083
1084
1085bool HexagonInstrInfo::
1086isValidOffset(const int Opcode, const int Offset) const {
1087 // This function is to check whether the "Offset" is in the correct range of
1088 // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1089 // inserted to calculate the final address. Due to this reason, the function
1090 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00001091 // We used to assert if the offset was not properly aligned, however,
1092 // there are cases where a misaligned pointer recast can cause this
1093 // problem, and we need to allow for it. The front end warns of such
1094 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001095
1096 switch(Opcode) {
1097
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001098 case Hexagon::L2_loadri_io:
Sirish Pande69295b82012-05-10 20:20:25 +00001099 case Hexagon::LDriw_f:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001100 case Hexagon::S2_storeri_io:
Sirish Pande69295b82012-05-10 20:20:25 +00001101 case Hexagon::STriw_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001102 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1103 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1104
Colin LeMahieu947cd702014-12-23 20:44:59 +00001105 case Hexagon::L2_loadrd_io:
Sirish Pande69295b82012-05-10 20:20:25 +00001106 case Hexagon::LDrid_f:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001107 case Hexagon::S2_storerd_io:
Sirish Pande69295b82012-05-10 20:20:25 +00001108 case Hexagon::STrid_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001109 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1110 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1111
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001112 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001113 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001114 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001115 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1116 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1117
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001118 case Hexagon::L2_loadrb_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001119 case Hexagon::S2_storerb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001120 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001121 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1122 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1123
1124 case Hexagon::ADD_ri:
1125 case Hexagon::TFR_FI:
1126 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1127 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1128
Colin LeMahieudacf0572015-01-05 21:36:38 +00001129 case Hexagon::L4_iadd_memopw_io:
1130 case Hexagon::L4_isub_memopw_io:
1131 case Hexagon::L4_add_memopw_io:
1132 case Hexagon::L4_sub_memopw_io:
1133 case Hexagon::L4_and_memopw_io:
1134 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001135 return (0 <= Offset && Offset <= 255);
1136
Colin LeMahieudacf0572015-01-05 21:36:38 +00001137 case Hexagon::L4_iadd_memoph_io:
1138 case Hexagon::L4_isub_memoph_io:
1139 case Hexagon::L4_add_memoph_io:
1140 case Hexagon::L4_sub_memoph_io:
1141 case Hexagon::L4_and_memoph_io:
1142 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001143 return (0 <= Offset && Offset <= 127);
1144
Colin LeMahieudacf0572015-01-05 21:36:38 +00001145 case Hexagon::L4_iadd_memopb_io:
1146 case Hexagon::L4_isub_memopb_io:
1147 case Hexagon::L4_add_memopb_io:
1148 case Hexagon::L4_sub_memopb_io:
1149 case Hexagon::L4_and_memopb_io:
1150 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001151 return (0 <= Offset && Offset <= 63);
1152
1153 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1154 // any size. Later pass knows how to handle it.
1155 case Hexagon::STriw_pred:
1156 case Hexagon::LDriw_pred:
1157 return true;
1158
Colin LeMahieu5ccbb122014-12-19 00:06:53 +00001159 case Hexagon::J2_loop0i:
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +00001160 return isUInt<10>(Offset);
1161
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001162 // INLINEASM is very special.
1163 case Hexagon::INLINEASM:
1164 return true;
1165 }
1166
Benjamin Kramerb6684012011-12-27 11:41:05 +00001167 llvm_unreachable("No offset range is defined for this opcode. "
1168 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001169}
1170
1171
1172//
1173// Check if the Offset is a valid auto-inc imm by Load/Store Type.
1174//
1175bool HexagonInstrInfo::
1176isValidAutoIncImm(const EVT VT, const int Offset) const {
1177
1178 if (VT == MVT::i64) {
1179 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1180 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1181 (Offset & 0x7) == 0);
1182 }
1183 if (VT == MVT::i32) {
1184 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1185 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1186 (Offset & 0x3) == 0);
1187 }
1188 if (VT == MVT::i16) {
1189 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1190 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1191 (Offset & 0x1) == 0);
1192 }
1193 if (VT == MVT::i8) {
1194 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1195 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1196 }
Craig Toppere55c5562012-02-07 02:50:20 +00001197 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001198}
1199
1200
1201bool HexagonInstrInfo::
1202isMemOp(const MachineInstr *MI) const {
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001203// return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1204
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001205 switch (MI->getOpcode())
1206 {
Colin LeMahieudacf0572015-01-05 21:36:38 +00001207 default: return false;
1208 case Hexagon::L4_iadd_memopw_io:
1209 case Hexagon::L4_isub_memopw_io:
1210 case Hexagon::L4_add_memopw_io:
1211 case Hexagon::L4_sub_memopw_io:
1212 case Hexagon::L4_and_memopw_io:
1213 case Hexagon::L4_or_memopw_io:
1214 case Hexagon::L4_iadd_memoph_io:
1215 case Hexagon::L4_isub_memoph_io:
1216 case Hexagon::L4_add_memoph_io:
1217 case Hexagon::L4_sub_memoph_io:
1218 case Hexagon::L4_and_memoph_io:
1219 case Hexagon::L4_or_memoph_io:
1220 case Hexagon::L4_iadd_memopb_io:
1221 case Hexagon::L4_isub_memopb_io:
1222 case Hexagon::L4_add_memopb_io:
1223 case Hexagon::L4_sub_memopb_io:
1224 case Hexagon::L4_and_memopb_io:
1225 case Hexagon::L4_or_memopb_io:
1226 case Hexagon::L4_ior_memopb_io:
1227 case Hexagon::L4_ior_memoph_io:
1228 case Hexagon::L4_ior_memopw_io:
1229 case Hexagon::L4_iand_memopb_io:
1230 case Hexagon::L4_iand_memoph_io:
1231 case Hexagon::L4_iand_memopw_io:
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001232 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001233 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001234 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001235}
1236
1237
1238bool HexagonInstrInfo::
1239isSpillPredRegOp(const MachineInstr *MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001240 switch (MI->getOpcode()) {
1241 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001242 case Hexagon::STriw_pred :
1243 case Hexagon::LDriw_pred :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001244 return true;
Sirish Pande2c7bf002012-04-23 17:49:28 +00001245 }
Sirish Pande4bd20c52012-05-12 05:10:30 +00001246}
1247
1248bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1249 switch (MI->getOpcode()) {
Sirish Pande8bb97452012-05-12 05:54:15 +00001250 default: return false;
Colin LeMahieu902157c2014-11-25 18:20:52 +00001251 case Hexagon::C2_cmpeq:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001252 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001253 case Hexagon::C2_cmpgt:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001254 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001255 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001256 case Hexagon::C2_cmpgtui:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001257 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +00001258 }
Sirish Pande2c7bf002012-04-23 17:49:28 +00001259}
1260
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001261bool HexagonInstrInfo::
1262isConditionalTransfer (const MachineInstr *MI) const {
1263 switch (MI->getOpcode()) {
1264 default: return false;
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001265 case Hexagon::A2_tfrt:
1266 case Hexagon::A2_tfrf:
1267 case Hexagon::C2_cmoveit:
1268 case Hexagon::C2_cmoveif:
1269 case Hexagon::A2_tfrtnew:
1270 case Hexagon::A2_tfrfnew:
1271 case Hexagon::C2_cmovenewit:
1272 case Hexagon::C2_cmovenewif:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001273 return true;
1274 }
1275}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001276
1277bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001278 switch (MI->getOpcode())
1279 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001280 default: return false;
Colin LeMahieuefa74e02014-11-18 20:28:11 +00001281 case Hexagon::A2_paddf:
1282 case Hexagon::A2_paddfnew:
1283 case Hexagon::A2_paddt:
1284 case Hexagon::A2_paddtnew:
Colin LeMahieu44fd1c82014-11-18 22:45:47 +00001285 case Hexagon::A2_pandf:
1286 case Hexagon::A2_pandfnew:
1287 case Hexagon::A2_pandt:
1288 case Hexagon::A2_pandtnew:
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001289 case Hexagon::A4_paslhf:
1290 case Hexagon::A4_paslhfnew:
1291 case Hexagon::A4_paslht:
1292 case Hexagon::A4_paslhtnew:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001293 case Hexagon::A4_pasrhf:
1294 case Hexagon::A4_pasrhfnew:
1295 case Hexagon::A4_pasrht:
1296 case Hexagon::A4_pasrhtnew:
Colin LeMahieu21866542014-11-19 22:58:04 +00001297 case Hexagon::A2_porf:
1298 case Hexagon::A2_porfnew:
1299 case Hexagon::A2_port:
1300 case Hexagon::A2_portnew:
Colin LeMahieue88447d2014-11-21 21:19:18 +00001301 case Hexagon::A2_psubf:
1302 case Hexagon::A2_psubfnew:
1303 case Hexagon::A2_psubt:
1304 case Hexagon::A2_psubtnew:
Colin LeMahieuac006432014-11-19 23:22:23 +00001305 case Hexagon::A2_pxorf:
1306 case Hexagon::A2_pxorfnew:
1307 case Hexagon::A2_pxort:
1308 case Hexagon::A2_pxortnew:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001309 case Hexagon::A4_psxthf:
1310 case Hexagon::A4_psxthfnew:
1311 case Hexagon::A4_psxtht:
1312 case Hexagon::A4_psxthtnew:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001313 case Hexagon::A4_psxtbf:
1314 case Hexagon::A4_psxtbfnew:
1315 case Hexagon::A4_psxtbt:
1316 case Hexagon::A4_psxtbtnew:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001317 case Hexagon::A4_pzxtbf:
1318 case Hexagon::A4_pzxtbfnew:
1319 case Hexagon::A4_pzxtbt:
1320 case Hexagon::A4_pzxtbtnew:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001321 case Hexagon::A4_pzxthf:
1322 case Hexagon::A4_pzxthfnew:
1323 case Hexagon::A4_pzxtht:
1324 case Hexagon::A4_pzxthtnew:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001325 case Hexagon::ADD_ri_cPt:
1326 case Hexagon::ADD_ri_cNotPt:
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001327 case Hexagon::C2_ccombinewt:
1328 case Hexagon::C2_ccombinewf:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001329 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001330 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001331}
1332
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001333bool HexagonInstrInfo::
1334isConditionalLoad (const MachineInstr* MI) const {
1335 const HexagonRegisterInfo& QRI = getRegisterInfo();
1336 switch (MI->getOpcode())
1337 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001338 default: return false;
Colin LeMahieu947cd702014-12-23 20:44:59 +00001339 case Hexagon::L2_ploadrdt_io :
1340 case Hexagon::L2_ploadrdf_io:
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001341 case Hexagon::L2_ploadrit_io:
1342 case Hexagon::L2_ploadrif_io:
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001343 case Hexagon::L2_ploadrht_io:
1344 case Hexagon::L2_ploadrhf_io:
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001345 case Hexagon::L2_ploadrbt_io:
1346 case Hexagon::L2_ploadrbf_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001347 case Hexagon::L2_ploadruht_io:
1348 case Hexagon::L2_ploadruhf_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001349 case Hexagon::L2_ploadrubt_io:
1350 case Hexagon::L2_ploadrubf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001351 return true;
Colin LeMahieu9161d472014-12-30 18:58:47 +00001352 case Hexagon::L2_ploadrdt_pi:
1353 case Hexagon::L2_ploadrdf_pi:
1354 case Hexagon::L2_ploadrit_pi:
1355 case Hexagon::L2_ploadrif_pi:
1356 case Hexagon::L2_ploadrht_pi:
1357 case Hexagon::L2_ploadrhf_pi:
1358 case Hexagon::L2_ploadrbt_pi:
1359 case Hexagon::L2_ploadrbf_pi:
1360 case Hexagon::L2_ploadruht_pi:
1361 case Hexagon::L2_ploadruhf_pi:
1362 case Hexagon::L2_ploadrubt_pi:
1363 case Hexagon::L2_ploadrubf_pi:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001364 return QRI.Subtarget.hasV4TOps();
Colin LeMahieu9161d472014-12-30 18:58:47 +00001365 case Hexagon::L4_ploadrdt_rr:
1366 case Hexagon::L4_ploadrdf_rr:
1367 case Hexagon::L4_ploadrbt_rr:
1368 case Hexagon::L4_ploadrbf_rr:
1369 case Hexagon::L4_ploadrubt_rr:
1370 case Hexagon::L4_ploadrubf_rr:
1371 case Hexagon::L4_ploadrht_rr:
1372 case Hexagon::L4_ploadrhf_rr:
1373 case Hexagon::L4_ploadruht_rr:
1374 case Hexagon::L4_ploadruhf_rr:
1375 case Hexagon::L4_ploadrit_rr:
1376 case Hexagon::L4_ploadrif_rr:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001377 return QRI.Subtarget.hasV4TOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001378 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001379}
Andrew Trickd06df962012-02-01 22:13:57 +00001380
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001381// Returns true if an instruction is a conditional store.
1382//
1383// Note: It doesn't include conditional new-value stores as they can't be
1384// converted to .new predicate.
1385//
1386// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1387// ^ ^
1388// / \ (not OK. it will cause new-value store to be
1389// / X conditional on p0.new while R2 producer is
1390// / \ on p0)
1391// / \.
1392// p.new store p.old NV store
1393// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1394// ^ ^
1395// \ /
1396// \ /
1397// \ /
1398// p.old store
1399// [if (p0)memw(R0+#0)=R2]
1400//
1401// The above diagram shows the steps involoved in the conversion of a predicated
1402// store instruction to its .new predicated new-value form.
1403//
1404// The following set of instructions further explains the scenario where
1405// conditional new-value store becomes invalid when promoted to .new predicate
1406// form.
1407//
1408// { 1) if (p0) r0 = add(r1, r2)
1409// 2) p0 = cmp.eq(r3, #0) }
1410//
1411// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1412// the first two instructions because in instr 1, r0 is conditional on old value
1413// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1414// is not valid for new-value stores.
1415bool HexagonInstrInfo::
1416isConditionalStore (const MachineInstr* MI) const {
1417 const HexagonRegisterInfo& QRI = getRegisterInfo();
1418 switch (MI->getOpcode())
1419 {
1420 default: return false;
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001421 case Hexagon::S4_storeirbt_io:
1422 case Hexagon::S4_storeirbf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001423 case Hexagon::S4_pstorerbt_rr:
1424 case Hexagon::S4_pstorerbf_rr:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001425 case Hexagon::S2_pstorerbt_io:
1426 case Hexagon::S2_pstorerbf_io:
Colin LeMahieu3d34afb2014-12-29 19:42:14 +00001427 case Hexagon::S2_pstorerbt_pi:
1428 case Hexagon::S2_pstorerbf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001429 case Hexagon::S2_pstorerdt_io:
1430 case Hexagon::S2_pstorerdf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001431 case Hexagon::S4_pstorerdt_rr:
1432 case Hexagon::S4_pstorerdf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001433 case Hexagon::S2_pstorerdt_pi:
1434 case Hexagon::S2_pstorerdf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001435 case Hexagon::S2_pstorerht_io:
1436 case Hexagon::S2_pstorerhf_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001437 case Hexagon::S4_storeirht_io:
1438 case Hexagon::S4_storeirhf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001439 case Hexagon::S4_pstorerht_rr:
1440 case Hexagon::S4_pstorerhf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001441 case Hexagon::S2_pstorerht_pi:
1442 case Hexagon::S2_pstorerhf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001443 case Hexagon::S2_pstorerit_io:
1444 case Hexagon::S2_pstorerif_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001445 case Hexagon::S4_storeirit_io:
1446 case Hexagon::S4_storeirif_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001447 case Hexagon::S4_pstorerit_rr:
1448 case Hexagon::S4_pstorerif_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001449 case Hexagon::S2_pstorerit_pi:
1450 case Hexagon::S2_pstorerif_pi:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001451 return QRI.Subtarget.hasV4TOps();
1452
1453 // V4 global address store before promoting to dot new.
Colin LeMahieu14455532015-01-06 16:15:15 +00001454 case Hexagon::S4_pstorerdt_abs:
1455 case Hexagon::S4_pstorerdf_abs:
1456 case Hexagon::S4_pstorerbt_abs:
1457 case Hexagon::S4_pstorerbf_abs:
1458 case Hexagon::S4_pstorerht_abs:
1459 case Hexagon::S4_pstorerhf_abs:
1460 case Hexagon::S4_pstorerit_abs:
1461 case Hexagon::S4_pstorerif_abs:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001462 return QRI.Subtarget.hasV4TOps();
1463
1464 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1465 // from the "Conditional Store" list. Because a predicated new value store
1466 // would NOT be promoted to a double dot new store. See diagram below:
1467 // This function returns yes for those stores that are predicated but not
1468 // yet promoted to predicate dot new instructions.
1469 //
1470 // +---------------------+
1471 // /-----| if (p0) memw(..)=r0 |---------\~
1472 // || +---------------------+ ||
1473 // promote || /\ /\ || promote
1474 // || /||\ /||\ ||
1475 // \||/ demote || \||/
1476 // \/ || || \/
1477 // +-------------------------+ || +-------------------------+
1478 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1479 // +-------------------------+ || +-------------------------+
1480 // || || ||
1481 // || demote \||/
1482 // promote || \/ NOT possible
1483 // || || /\~
1484 // \||/ || /||\~
1485 // \/ || ||
1486 // +-----------------------------+
1487 // | if (p0.new) memw(..)=r0.new |
1488 // +-----------------------------+
1489 // Double Dot New Store
1490 //
1491 }
1492}
1493
Jyotsna Verma84c47102013-05-06 18:49:23 +00001494
1495bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1496 if (isNewValue(MI) && isBranch(MI))
1497 return true;
1498 return false;
1499}
1500
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001501bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1502 return (getAddrMode(MI) == HexagonII::PostInc);
1503}
1504
Jyotsna Verma84c47102013-05-06 18:49:23 +00001505bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1506 const uint64_t F = MI->getDesc().TSFlags;
1507 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1508}
1509
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001510// Returns true, if any one of the operands is a dot new
1511// insn, whether it is predicated dot new or register dot new.
1512bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1513 return (isNewValueInst(MI) ||
1514 (isPredicated(MI) && isPredicatedNew(MI)));
1515}
1516
Jyotsna Verma438cec52013-05-10 20:58:11 +00001517// Returns the most basic instruction for the .new predicated instructions and
1518// new-value stores.
1519// For example, all of the following instructions will be converted back to the
1520// same instruction:
1521// 1) if (p0.new) memw(R0+#0) = R1.new --->
1522// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1523// 3) if (p0.new) memw(R0+#0) = R1 --->
1524//
1525
1526int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1527 int NewOp = opc;
1528 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1529 NewOp = Hexagon::getPredOldOpcode(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001530 assert(NewOp >= 0 &&
1531 "Couldn't change predicate new instruction to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001532 }
1533
Alp Tokerf907b892013-12-05 05:44:44 +00001534 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
Jyotsna Verma438cec52013-05-10 20:58:11 +00001535 NewOp = Hexagon::getNonNVStore(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001536 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001537 }
1538 return NewOp;
1539}
1540
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001541// Return the new value instruction for a given store.
1542int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1543 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1544 if (NVOpcode >= 0) // Valid new-value store instruction.
1545 return NVOpcode;
1546
1547 switch (MI->getOpcode()) {
1548 default: llvm_unreachable("Unknown .new type");
1549 // store new value byte
1550 case Hexagon::STrib_shl_V4:
1551 return Hexagon::STrib_shl_nv_V4;
1552
1553 case Hexagon::STrih_shl_V4:
1554 return Hexagon::STrih_shl_nv_V4;
1555
1556 case Hexagon::STriw_f:
Colin LeMahieu90148902014-12-30 22:28:31 +00001557 return Hexagon::S2_storerinew_io;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001558
1559 case Hexagon::STriw_indexed_f:
Colin LeMahieu90148902014-12-30 22:28:31 +00001560 return Hexagon::S4_storerinew_rr;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001561
1562 case Hexagon::STriw_shl_V4:
1563 return Hexagon::STriw_shl_nv_V4;
1564
1565 }
1566 return 0;
1567}
1568
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001569// Return .new predicate version for an instruction.
1570int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1571 const MachineBranchProbabilityInfo
1572 *MBPI) const {
1573
1574 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1575 if (NewOpcode >= 0) // Valid predicate new instruction
1576 return NewOpcode;
1577
1578 switch (MI->getOpcode()) {
1579 default: llvm_unreachable("Unknown .new type");
1580 // Condtional Jumps
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001581 case Hexagon::J2_jumpt:
1582 case Hexagon::J2_jumpf:
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001583 return getDotNewPredJumpOp(MI, MBPI);
1584
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001585 case Hexagon::J2_jumprt:
1586 return Hexagon::J2_jumptnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001587
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001588 case Hexagon::J2_jumprf:
1589 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001590
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001591 case Hexagon::JMPrett:
1592 return Hexagon::J2_jumprtnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001593
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001594 case Hexagon::JMPretf:
1595 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001596
1597
1598 // Conditional combine
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001599 case Hexagon::C2_ccombinewt:
1600 return Hexagon::C2_ccombinewnewt;
1601 case Hexagon::C2_ccombinewf:
1602 return Hexagon::C2_ccombinewnewf;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001603 }
1604}
1605
1606
Jyotsna Verma84256432013-03-01 17:37:13 +00001607unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1608 const uint64_t F = MI->getDesc().TSFlags;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001609
Jyotsna Verma84256432013-03-01 17:37:13 +00001610 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1611}
1612
1613/// immediateExtend - Changes the instruction in place to one using an immediate
1614/// extender.
1615void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1616 assert((isExtendable(MI)||isConstExtended(MI)) &&
1617 "Instruction must be extendable");
1618 // Find which operand is extendable.
1619 short ExtOpNum = getCExtOpNum(MI);
1620 MachineOperand &MO = MI->getOperand(ExtOpNum);
1621 // This needs to be something we understand.
1622 assert((MO.isMBB() || MO.isImm()) &&
1623 "Branch with unknown extendable field type");
1624 // Mark given operand as extended.
1625 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1626}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001627
Eric Christopher143f02c2014-10-09 01:59:35 +00001628DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1629 const TargetSubtargetInfo &STI) const {
1630 const InstrItineraryData *II = STI.getInstrItineraryData();
1631 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
Andrew Trickd06df962012-02-01 22:13:57 +00001632}
1633
1634bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1635 const MachineBasicBlock *MBB,
1636 const MachineFunction &MF) const {
1637 // Debug info is never a scheduling boundary. It's necessary to be explicit
1638 // due to the special treatment of IT instructions below, otherwise a
1639 // dbg_value followed by an IT will result in the IT instruction being
1640 // considered a scheduling hazard, which is wrong. It should be the actual
1641 // instruction preceding the dbg_value instruction(s), just like it is
1642 // when debug info is not present.
1643 if (MI->isDebugValue())
1644 return false;
1645
1646 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001647 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
Andrew Trickd06df962012-02-01 22:13:57 +00001648 return true;
1649
1650 return false;
1651}
Jyotsna Verma84256432013-03-01 17:37:13 +00001652
1653bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
1654
1655 // Constant extenders are allowed only for V4 and above.
1656 if (!Subtarget.hasV4TOps())
1657 return false;
1658
1659 const uint64_t F = MI->getDesc().TSFlags;
1660 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1661 if (isExtended) // Instruction must be extended.
1662 return true;
1663
1664 unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1665 & HexagonII::ExtendableMask;
1666 if (!isExtendable)
1667 return false;
1668
1669 short ExtOpNum = getCExtOpNum(MI);
1670 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1671 // Use MO operand flags to determine if MO
1672 // has the HMOTF_ConstExtended flag set.
1673 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1674 return true;
1675 // If this is a Machine BB address we are talking about, and it is
1676 // not marked as extended, say so.
1677 if (MO.isMBB())
1678 return false;
1679
1680 // We could be using an instruction with an extendable immediate and shoehorn
1681 // a global address into it. If it is a global address it will be constant
1682 // extended. We do this for COMBINE.
1683 // We currently only handle isGlobal() because it is the only kind of
1684 // object we are going to end up with here for now.
1685 // In the future we probably should add isSymbol(), etc.
1686 if (MO.isGlobal() || MO.isSymbol())
1687 return true;
1688
1689 // If the extendable operand is not 'Immediate' type, the instruction should
1690 // have 'isExtended' flag set.
1691 assert(MO.isImm() && "Extendable operand must be Immediate type");
1692
1693 int MinValue = getMinValue(MI);
1694 int MaxValue = getMaxValue(MI);
1695 int ImmValue = MO.getImm();
1696
1697 return (ImmValue < MinValue || ImmValue > MaxValue);
1698}
1699
Jyotsna Verma1d297502013-05-02 15:39:30 +00001700// Returns the opcode to use when converting MI, which is a conditional jump,
1701// into a conditional instruction which uses the .new value of the predicate.
1702// We also use branch probabilities to add a hint to the jump.
1703int
1704HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1705 const
1706 MachineBranchProbabilityInfo *MBPI) const {
1707
1708 // We assume that block can have at most two successors.
1709 bool taken = false;
1710 MachineBasicBlock *Src = MI->getParent();
1711 MachineOperand *BrTarget = &MI->getOperand(1);
1712 MachineBasicBlock *Dst = BrTarget->getMBB();
1713
1714 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1715 if (Prediction >= BranchProbability(1,2))
1716 taken = true;
1717
1718 switch (MI->getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001719 case Hexagon::J2_jumpt:
1720 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1721 case Hexagon::J2_jumpf:
1722 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Jyotsna Verma1d297502013-05-02 15:39:30 +00001723
1724 default:
1725 llvm_unreachable("Unexpected jump instruction.");
1726 }
1727}
Jyotsna Verma84256432013-03-01 17:37:13 +00001728// Returns true if a particular operand is extendable for an instruction.
1729bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1730 unsigned short OperandNum) const {
1731 // Constant extenders are allowed only for V4 and above.
1732 if (!Subtarget.hasV4TOps())
1733 return false;
1734
1735 const uint64_t F = MI->getDesc().TSFlags;
1736
1737 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1738 == OperandNum;
1739}
1740
1741// Returns Operand Index for the constant extended instruction.
1742unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1743 const uint64_t F = MI->getDesc().TSFlags;
1744 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1745}
1746
1747// Returns the min value that doesn't need to be extended.
1748int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1749 const uint64_t F = MI->getDesc().TSFlags;
1750 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1751 & HexagonII::ExtentSignedMask;
1752 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1753 & HexagonII::ExtentBitsMask;
1754
1755 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001756 return -1U << (bits - 1);
Jyotsna Verma84256432013-03-01 17:37:13 +00001757 else
1758 return 0;
1759}
1760
1761// Returns the max value that doesn't need to be extended.
1762int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1763 const uint64_t F = MI->getDesc().TSFlags;
1764 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1765 & HexagonII::ExtentSignedMask;
1766 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1767 & HexagonII::ExtentBitsMask;
1768
1769 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001770 return ~(-1U << (bits - 1));
Jyotsna Verma84256432013-03-01 17:37:13 +00001771 else
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001772 return ~(-1U << bits);
Jyotsna Verma84256432013-03-01 17:37:13 +00001773}
1774
1775// Returns true if an instruction can be converted into a non-extended
1776// equivalent instruction.
1777bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1778
1779 short NonExtOpcode;
1780 // Check if the instruction has a register form that uses register in place
1781 // of the extended operand, if so return that as the non-extended form.
1782 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1783 return true;
1784
1785 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001786 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001787
1788 switch (getAddrMode(MI)) {
1789 case HexagonII::Absolute :
1790 // Load/store with absolute addressing mode can be converted into
1791 // base+offset mode.
1792 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1793 break;
1794 case HexagonII::BaseImmOffset :
1795 // Load/store with base+offset addressing mode can be converted into
1796 // base+register offset addressing mode. However left shift operand should
1797 // be set to 0.
1798 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1799 break;
1800 default:
1801 return false;
1802 }
1803 if (NonExtOpcode < 0)
1804 return false;
1805 return true;
1806 }
1807 return false;
1808}
1809
1810// Returns opcode of the non-extended equivalent instruction.
1811short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1812
1813 // Check if the instruction has a register form that uses register in place
1814 // of the extended operand, if so return that as the non-extended form.
1815 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1816 if (NonExtOpcode >= 0)
1817 return NonExtOpcode;
1818
1819 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001820 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001821 switch (getAddrMode(MI)) {
1822 case HexagonII::Absolute :
1823 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1824 case HexagonII::BaseImmOffset :
1825 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1826 default:
1827 return -1;
1828 }
1829 }
1830 return -1;
1831}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001832
1833bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001834 return (Opcode == Hexagon::J2_jumpt) ||
1835 (Opcode == Hexagon::J2_jumpf) ||
1836 (Opcode == Hexagon::J2_jumptnewpt) ||
1837 (Opcode == Hexagon::J2_jumpfnewpt) ||
1838 (Opcode == Hexagon::J2_jumpt) ||
1839 (Opcode == Hexagon::J2_jumpf);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001840}
1841
1842bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001843 return (Opcode == Hexagon::J2_jumpf) ||
1844 (Opcode == Hexagon::J2_jumpfnewpt) ||
1845 (Opcode == Hexagon::J2_jumpfnew);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001846}