blob: 69642659916869d9e72738885e9b6207a0376e19 [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 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000214
215 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
216 I->getOperand(0).isMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000217 // Delete the JMP if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000218 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000219 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
220 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
221 I->eraseFromParent();
222 I = MBB.instr_end();
223 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000224 return false;
225 --I;
226 }
227 if (!isUnpredicatedTerminator(I))
228 return false;
229
230 // Get the last instruction in the block.
231 MachineInstr *LastInst = I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000232 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000233 // Find one more terminator if present.
234 do {
235 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
236 if (!SecondLastInst)
237 SecondLastInst = I;
238 else
239 // This is a third branch.
240 return true;
241 }
242 if (I == MBB.instr_begin())
243 break;
244 --I;
245 } while(I);
246
247 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000248 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
249 // If the branch target is not a basic block, it could be a tail call.
250 // (It is, if the target is a function.)
251 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
252 return true;
253 if (SecLastOpcode == Hexagon::J2_jump &&
254 !SecondLastInst->getOperand(0).isMBB())
255 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000256
257 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
258 bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000259
260 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000261 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000262 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000263 TBB = LastInst->getOperand(0).getMBB();
264 return false;
265 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000266 if (LastOpcode == Hexagon::ENDLOOP0) {
267 TBB = LastInst->getOperand(0).getMBB();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000268 Cond.push_back(LastInst->getOperand(0));
269 return false;
270 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000271 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000272 TBB = LastInst->getOperand(1).getMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000273 if (LastOpcodeHasNot) {
274 Cond.push_back(MachineOperand::CreateImm(0));
275 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000276 Cond.push_back(LastInst->getOperand(0));
277 return false;
278 }
279 // Otherwise, don't know what this is.
280 return true;
281 }
282
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000283 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
284 bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000285 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000286 TBB = SecondLastInst->getOperand(1).getMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000287 if (SecLastOpcodeHasNot)
288 Cond.push_back(MachineOperand::CreateImm(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000289 Cond.push_back(SecondLastInst->getOperand(0));
290 FBB = LastInst->getOperand(0).getMBB();
291 return false;
292 }
293
294 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
295 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000296 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000297 TBB = SecondLastInst->getOperand(0).getMBB();
298 I = LastInst;
299 if (AllowModify)
300 I->eraseFromParent();
301 return false;
302 }
303
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000304 // If the block ends with an ENDLOOP, and JMP, handle it.
305 if (SecLastOpcode == Hexagon::ENDLOOP0 &&
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000306 LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000307 TBB = SecondLastInst->getOperand(0).getMBB();
308 Cond.push_back(SecondLastInst->getOperand(0));
309 FBB = LastInst->getOperand(0).getMBB();
310 return false;
311 }
312
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000313 // Otherwise, can't handle this.
314 return true;
315}
316
317
318unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000319 int BOpc = Hexagon::J2_jump;
320 int BccOpc = Hexagon::J2_jumpt;
321 int BccOpcNot = Hexagon::J2_jumpf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000322
323 MachineBasicBlock::iterator I = MBB.end();
324 if (I == MBB.begin()) return 0;
325 --I;
326 if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc &&
327 I->getOpcode() != BccOpcNot)
328 return 0;
329
330 // Remove the branch.
331 I->eraseFromParent();
332
333 I = MBB.end();
334
335 if (I == MBB.begin()) return 1;
336 --I;
337 if (I->getOpcode() != BccOpc && I->getOpcode() != BccOpcNot)
338 return 1;
339
340 // Remove the branch.
341 I->eraseFromParent();
342 return 2;
343}
344
345
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000346/// \brief For a comparison instruction, return the source registers in
347/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
348/// compares against in CmpValue. Return true if the comparison instruction
349/// can be analyzed.
350bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
351 unsigned &SrcReg, unsigned &SrcReg2,
352 int &Mask, int &Value) const {
353 unsigned Opc = MI->getOpcode();
354
355 // Set mask and the first source register.
356 switch (Opc) {
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000357 case Hexagon::C2_cmpeqp:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000358 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000359 case Hexagon::C2_cmpeq:
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000360 case Hexagon::C2_cmpgtp:
361 case Hexagon::C2_cmpgtup:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000362 case Hexagon::C2_cmpgtui:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000363 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000364 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000365 case Hexagon::C2_cmpgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000366 SrcReg = MI->getOperand(1).getReg();
367 Mask = ~0;
368 break;
Colin LeMahieufa947902015-01-14 16:49:12 +0000369 case Hexagon::A4_cmpbeqi:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000370 case Hexagon::A4_cmpbeq:
371 case Hexagon::A4_cmpbgtui:
372 case Hexagon::A4_cmpbgtu:
373 case Hexagon::A4_cmpbgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000374 SrcReg = MI->getOperand(1).getReg();
375 Mask = 0xFF;
376 break;
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000377 case Hexagon::A4_cmpheqi:
378 case Hexagon::A4_cmpheq:
379 case Hexagon::A4_cmphgtui:
380 case Hexagon::A4_cmphgtu:
381 case Hexagon::A4_cmphgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000382 SrcReg = MI->getOperand(1).getReg();
383 Mask = 0xFFFF;
384 break;
385 }
386
387 // Set the value/second source register.
388 switch (Opc) {
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000389 case Hexagon::C2_cmpeqp:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000390 case Hexagon::C2_cmpeq:
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000391 case Hexagon::C2_cmpgtp:
392 case Hexagon::C2_cmpgtup:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000393 case Hexagon::C2_cmpgtu:
394 case Hexagon::C2_cmpgt:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000395 case Hexagon::A4_cmpbeq:
396 case Hexagon::A4_cmpbgtu:
397 case Hexagon::A4_cmpbgt:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000398 case Hexagon::A4_cmpheq:
399 case Hexagon::A4_cmphgtu:
400 case Hexagon::A4_cmphgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000401 SrcReg2 = MI->getOperand(2).getReg();
402 return true;
403
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000404 case Hexagon::C2_cmpeqi:
405 case Hexagon::C2_cmpgtui:
406 case Hexagon::C2_cmpgti:
Colin LeMahieufa947902015-01-14 16:49:12 +0000407 case Hexagon::A4_cmpbeqi:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000408 case Hexagon::A4_cmpbgtui:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000409 case Hexagon::A4_cmpheqi:
410 case Hexagon::A4_cmphgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000411 SrcReg2 = 0;
412 Value = MI->getOperand(2).getImm();
413 return true;
414 }
415
416 return false;
417}
418
419
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000420void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
421 MachineBasicBlock::iterator I, DebugLoc DL,
422 unsigned DestReg, unsigned SrcReg,
423 bool KillSrc) const {
424 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000425 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000426 return;
427 }
428 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000429 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000430 return;
431 }
432 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
433 // Map Pd = Ps to Pd = or(Ps, Ps).
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000434 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000435 DestReg).addReg(SrcReg).addReg(SrcReg);
436 return;
437 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000438 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
439 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000440 // We can have an overlap between single and double reg: r1:0 = r0.
441 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
442 // r1:0 = r0
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 } else {
446 // r1:0 = r1 or no overlap.
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000447 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000448 Hexagon::subreg_loreg))).addReg(SrcReg);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000449 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000450 Hexagon::subreg_hireg))).addImm(0);
451 }
452 return;
453 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000454 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000455 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000456 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000458 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000459 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
460 Hexagon::IntRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000461 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000462 addReg(SrcReg, getKillRegState(KillSrc));
463 return;
464 }
465 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
466 Hexagon::PredRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000467 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000468 addReg(SrcReg, getKillRegState(KillSrc));
469 return;
470 }
Sirish Pande30804c22012-02-15 18:52:27 +0000471
472 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000473}
474
475
476void HexagonInstrInfo::
477storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
478 unsigned SrcReg, bool isKill, int FI,
479 const TargetRegisterClass *RC,
480 const TargetRegisterInfo *TRI) const {
481
482 DebugLoc DL = MBB.findDebugLoc(I);
483 MachineFunction &MF = *MBB.getParent();
484 MachineFrameInfo &MFI = *MF.getFrameInfo();
485 unsigned Align = MFI.getObjectAlignment(FI);
486
487 MachineMemOperand *MMO =
488 MF.getMachineMemOperand(
489 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
490 MachineMemOperand::MOStore,
491 MFI.getObjectSize(FI),
492 Align);
493
Craig Topperc7242e02012-04-20 07:30:17 +0000494 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000495 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000496 .addFrameIndex(FI).addImm(0)
497 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000498 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000499 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000500 .addFrameIndex(FI).addImm(0)
501 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000502 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000503 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
504 .addFrameIndex(FI).addImm(0)
505 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
506 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000507 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000508 }
509}
510
511
512void HexagonInstrInfo::storeRegToAddr(
513 MachineFunction &MF, unsigned SrcReg,
514 bool isKill,
515 SmallVectorImpl<MachineOperand> &Addr,
516 const TargetRegisterClass *RC,
517 SmallVectorImpl<MachineInstr*> &NewMIs) const
518{
Craig Toppere55c5562012-02-07 02:50:20 +0000519 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000520}
521
522
523void HexagonInstrInfo::
524loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
525 unsigned DestReg, int FI,
526 const TargetRegisterClass *RC,
527 const TargetRegisterInfo *TRI) const {
528 DebugLoc DL = MBB.findDebugLoc(I);
529 MachineFunction &MF = *MBB.getParent();
530 MachineFrameInfo &MFI = *MF.getFrameInfo();
531 unsigned Align = MFI.getObjectAlignment(FI);
532
533 MachineMemOperand *MMO =
534 MF.getMachineMemOperand(
535 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
536 MachineMemOperand::MOLoad,
537 MFI.getObjectSize(FI),
538 Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000539 if (RC == &Hexagon::IntRegsRegClass) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000540 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000541 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000542 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000543 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000544 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000545 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000546 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
547 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
548 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000549 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000550 }
551}
552
553
554void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
555 SmallVectorImpl<MachineOperand> &Addr,
556 const TargetRegisterClass *RC,
557 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Craig Toppere55c5562012-02-07 02:50:20 +0000558 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000559}
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000560bool
561HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
562 unsigned Opc = MI->getOpcode();
563
564 switch (Opc) {
565 case Hexagon::TCRETURNi:
566 MI->setDesc(get(Hexagon::J2_jump));
567 return true;
568 case Hexagon::TCRETURNr:
569 MI->setDesc(get(Hexagon::J2_jumpr));
570 return true;
571 }
572
573 return false;
574}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000575
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000576MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
Benjamin Kramerf1362f62015-02-28 12:04:00 +0000577 MachineInstr *MI,
578 ArrayRef<unsigned> Ops,
579 int FI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000580 // Hexagon_TODO: Implement.
Craig Topper062a2ba2014-04-25 05:30:21 +0000581 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000582}
583
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000584unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
585
586 MachineRegisterInfo &RegInfo = MF->getRegInfo();
587 const TargetRegisterClass *TRC;
Sirish Pande69295b82012-05-10 20:20:25 +0000588 if (VT == MVT::i1) {
Craig Topperc7242e02012-04-20 07:30:17 +0000589 TRC = &Hexagon::PredRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000590 } else if (VT == MVT::i32 || VT == MVT::f32) {
Craig Topperc7242e02012-04-20 07:30:17 +0000591 TRC = &Hexagon::IntRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000592 } else if (VT == MVT::i64 || VT == MVT::f64) {
Craig Topperc7242e02012-04-20 07:30:17 +0000593 TRC = &Hexagon::DoubleRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000594 } else {
Benjamin Kramerb6684012011-12-27 11:41:05 +0000595 llvm_unreachable("Cannot handle this register class");
Sirish Pande69295b82012-05-10 20:20:25 +0000596 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000597
598 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
599 return NewReg;
600}
601
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000602bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000603 const MCInstrDesc &MID = MI->getDesc();
604 const uint64_t F = MID.TSFlags;
605 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
606 return true;
607
608 // TODO: This is largely obsolete now. Will need to be removed
609 // in consecutive patches.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000610 switch(MI->getOpcode()) {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000611 // TFR_FI Remains a special case.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000612 case Hexagon::TFR_FI:
613 return true;
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000614 default:
615 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000616 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000617 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000618}
619
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000620// This returns true in two cases:
621// - The OP code itself indicates that this is an extended instruction.
622// - One of MOs has been marked with HMOTF_ConstExtended flag.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000623bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000624 // First check if this is permanently extended op code.
625 const uint64_t F = MI->getDesc().TSFlags;
626 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
627 return true;
628 // Use MO operand flags to determine if one of MI's operands
629 // has HMOTF_ConstExtended flag set.
630 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
631 E = MI->operands_end(); I != E; ++I) {
632 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Sirish Pande69295b82012-05-10 20:20:25 +0000633 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000634 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000635 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000636}
637
Jyotsna Verma84c47102013-05-06 18:49:23 +0000638bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
639 return MI->getDesc().isBranch();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000640}
641
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000642bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
643 if (isNewValueJump(MI))
644 return true;
645
646 if (isNewValueStore(MI))
647 return true;
648
649 return false;
650}
651
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000652bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
653 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
654}
Andrew Trickd06df962012-02-01 22:13:57 +0000655
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000656bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
657 bool isPred = MI->getDesc().isPredicable();
658
659 if (!isPred)
660 return false;
661
662 const int Opc = MI->getOpcode();
663
664 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000665 case Hexagon::A2_tfrsi:
Colin LeMahieu2efa2d02015-03-09 21:48:13 +0000666 return (isOperandExtended(MI, 1) && isConstExtended(MI)) || isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000667
Colin LeMahieubda31b42014-12-29 20:44:51 +0000668 case Hexagon::S2_storerd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000669 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000670
Colin LeMahieubda31b42014-12-29 20:44:51 +0000671 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000672 case Hexagon::S2_storerinew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000673 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000674
Colin LeMahieubda31b42014-12-29 20:44:51 +0000675 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000676 case Hexagon::S2_storerhnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000677 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000678
Colin LeMahieubda31b42014-12-29 20:44:51 +0000679 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000680 case Hexagon::S2_storerbnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000681 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000682
Colin LeMahieu947cd702014-12-23 20:44:59 +0000683 case Hexagon::L2_loadrd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000684 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000685
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000686 case Hexagon::L2_loadri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000687 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000688
Colin LeMahieu8e39cad2014-12-23 17:25:57 +0000689 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +0000690 case Hexagon::L2_loadruh_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000691 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000692
Colin LeMahieu4b1eac42014-12-22 21:40:43 +0000693 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +0000694 case Hexagon::L2_loadrub_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000695 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000696
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000697 case Hexagon::L2_loadrd_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000698 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000699
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000700 case Hexagon::L2_loadri_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000701 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000702
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000703 case Hexagon::L2_loadrh_pi:
704 case Hexagon::L2_loadruh_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000705 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000706
Colin LeMahieu96976a12014-12-26 18:57:13 +0000707 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +0000708 case Hexagon::L2_loadrub_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000709 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000710
Colin LeMahieu2bad4a72014-12-30 21:01:38 +0000711 case Hexagon::S4_storeirb_io:
712 case Hexagon::S4_storeirh_io:
713 case Hexagon::S4_storeiri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000714 return (isUInt<6>(MI->getOperand(1).getImm()) &&
715 isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000716
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000717 case Hexagon::A2_addi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000718 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000719
Colin LeMahieu3b3197e2014-11-24 17:44:19 +0000720 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +0000721 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +0000722 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +0000723 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +0000724 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +0000725 case Hexagon::A2_zxth:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +0000726 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000727 }
728
729 return true;
730}
731
Sirish Pande8bb97452012-05-12 05:54:15 +0000732// This function performs the following inversiones:
733//
734// cPt ---> cNotPt
735// cNotPt ---> cPt
736//
Sirish Pande30804c22012-02-15 18:52:27 +0000737unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
Jyotsna Verma84c47102013-05-06 18:49:23 +0000738 int InvPredOpcode;
739 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
740 : Hexagon::getTruePredOpcode(Opc);
741 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
742 return InvPredOpcode;
743
Sirish Pande30804c22012-02-15 18:52:27 +0000744 switch(Opc) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000745 default: llvm_unreachable("Unexpected predicated instruction");
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000746 case Hexagon::C2_ccombinewt:
747 return Hexagon::C2_ccombinewf;
748 case Hexagon::C2_ccombinewf:
749 return Hexagon::C2_ccombinewt;
Sirish Pande30804c22012-02-15 18:52:27 +0000750
Jyotsna Verma978e9722013-05-09 18:25:44 +0000751 // Dealloc_return.
Colin LeMahieu14455532015-01-06 16:15:15 +0000752 case Hexagon::L4_return_t:
753 return Hexagon::L4_return_f;
754 case Hexagon::L4_return_f:
755 return Hexagon::L4_return_t;
Sirish Pande30804c22012-02-15 18:52:27 +0000756 }
757}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000758
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000759// New Value Store instructions.
760bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
761 const uint64_t F = MI->getDesc().TSFlags;
762
763 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
764}
765
766bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
767 const uint64_t F = get(Opcode).TSFlags;
768
769 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
770}
Andrew Trickd06df962012-02-01 22:13:57 +0000771
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000772int HexagonInstrInfo::
773getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
Pranav Bhandarkar34b60182012-11-01 19:13:23 +0000774 enum Hexagon::PredSense inPredSense;
775 inPredSense = invertPredicate ? Hexagon::PredSense_false :
776 Hexagon::PredSense_true;
777 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
778 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
779 return CondOpcode;
780
781 // This switch case will be removed once all the instructions have been
782 // modified to use relation maps.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000783 switch(Opc) {
Sirish Pande69295b82012-05-10 20:20:25 +0000784 case Hexagon::TFRI_f:
785 return !invertPredicate ? Hexagon::TFRI_cPt_f :
786 Hexagon::TFRI_cNotPt_f;
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000787 case Hexagon::A2_combinew:
788 return !invertPredicate ? Hexagon::C2_ccombinewt :
789 Hexagon::C2_ccombinewf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000790
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000791 // DEALLOC_RETURN.
Colin LeMahieu14455532015-01-06 16:15:15 +0000792 case Hexagon::L4_return:
793 return !invertPredicate ? Hexagon::L4_return_t:
794 Hexagon::L4_return_f;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000795 }
Benjamin Kramerb6684012011-12-27 11:41:05 +0000796 llvm_unreachable("Unexpected predicable instruction");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000797}
798
799
800bool HexagonInstrInfo::
801PredicateInstruction(MachineInstr *MI,
802 const SmallVectorImpl<MachineOperand> &Cond) const {
803 int Opc = MI->getOpcode();
804 assert (isPredicable(MI) && "Expected predicable instruction");
805 bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
806 (Cond[0].getImm() == 0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000807
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000808 // This will change MI's opcode to its predicate version.
809 // However, its operand list is still the old one, i.e. the
810 // non-predicate one.
811 MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
812
813 int oper = -1;
814 unsigned int GAIdx = 0;
815
816 // Indicates whether the current MI has a GlobalAddress operand
817 bool hasGAOpnd = false;
818 std::vector<MachineOperand> tmpOpnds;
819
820 // Indicates whether we need to shift operands to right.
821 bool needShift = true;
822
823 // The predicate is ALWAYS the FIRST input operand !!!
824 if (MI->getNumOperands() == 0) {
825 // The non-predicate version of MI does not take any operands,
826 // i.e. no outs and no ins. In this condition, the predicate
827 // operand will be directly placed at Operands[0]. No operand
828 // shift is needed.
829 // Example: BARRIER
830 needShift = false;
831 oper = -1;
832 }
833 else if ( MI->getOperand(MI->getNumOperands()-1).isReg()
834 && MI->getOperand(MI->getNumOperands()-1).isDef()
835 && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
836 // The non-predicate version of MI does not have any input operands.
837 // In this condition, we extend the length of Operands[] by one and
838 // copy the original last operand to the newly allocated slot.
839 // At this moment, it is just a place holder. Later, we will put
840 // predicate operand directly into it. No operand shift is needed.
841 // Example: r0=BARRIER (this is a faked insn used here for illustration)
842 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
843 needShift = false;
844 oper = MI->getNumOperands() - 2;
845 }
846 else {
847 // We need to right shift all input operands by one. Duplicate the
848 // last operand into the newly allocated slot.
849 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
850 }
851
852 if (needShift)
853 {
854 // Operands[ MI->getNumOperands() - 2 ] has been copied into
855 // Operands[ MI->getNumOperands() - 1 ], so we start from
856 // Operands[ MI->getNumOperands() - 3 ].
857 // oper is a signed int.
858 // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
859 for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
860 {
861 MachineOperand &MO = MI->getOperand(oper);
862
863 // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4] Opnd[5] Opnd[6] Opnd[7]
864 // <Def0> <Def1> <Use0> <Use1> <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
865 // /\~
866 // /||\~
867 // ||
868 // Predicate Operand here
869 if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
870 break;
871 }
872 if (MO.isReg()) {
873 MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
874 MO.isImplicit(), MO.isKill(),
875 MO.isDead(), MO.isUndef(),
876 MO.isDebug());
877 }
878 else if (MO.isImm()) {
879 MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
880 }
881 else if (MO.isGlobal()) {
882 // MI can not have more than one GlobalAddress operand.
883 assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
884
885 // There is no member function called "ChangeToGlobalAddress" in the
886 // MachineOperand class (not like "ChangeToRegister" and
887 // "ChangeToImmediate"). So we have to remove them from Operands[] list
888 // first, and then add them back after we have inserted the predicate
889 // operand. tmpOpnds[] is to remember these operands before we remove
890 // them.
891 tmpOpnds.push_back(MO);
892
893 // Operands[oper] is a GlobalAddress operand;
894 // Operands[oper+1] has been copied into Operands[oper+2];
895 hasGAOpnd = true;
896 GAIdx = oper;
897 continue;
898 }
899 else {
Craig Topperd3c02f12015-01-05 10:15:49 +0000900 llvm_unreachable("Unexpected operand type");
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000901 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000902 }
903 }
904
905 int regPos = invertJump ? 1 : 0;
906 MachineOperand PredMO = Cond[regPos];
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000907
908 // [oper] now points to the last explicit Def. Predicate operand must be
909 // located at [oper+1]. See diagram above.
910 // This assumes that the predicate is always the first operand,
911 // i.e. Operands[0+numResults], in the set of inputs
912 // It is better to have an assert here to check this. But I don't know how
913 // to write this assert because findFirstPredOperandIdx() would return -1
914 if (oper < -1) oper = -1;
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000915
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000916 MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000917 PredMO.isImplicit(), false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000918 PredMO.isDead(), PredMO.isUndef(),
919 PredMO.isDebug());
920
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000921 MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
922 RegInfo.clearKillFlags(PredMO.getReg());
923
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000924 if (hasGAOpnd)
925 {
926 unsigned int i;
927
928 // Operands[GAIdx] is the original GlobalAddress operand, which is
929 // already copied into tmpOpnds[0].
930 // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
931 // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
932 // so we start from [GAIdx+2]
933 for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
934 tmpOpnds.push_back(MI->getOperand(i));
935
936 // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
937 // It is very important that we always remove from the end of Operands[]
938 // MI->getNumOperands() is at least 2 if program goes to here.
939 for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
940 MI->RemoveOperand(i);
941
942 for (i = 0; i < tmpOpnds.size(); ++i)
943 MI->addOperand(tmpOpnds[i]);
944 }
945
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000946 return true;
947}
948
949
950bool
951HexagonInstrInfo::
952isProfitableToIfCvt(MachineBasicBlock &MBB,
Kay Tiong Khoof2949212012-06-13 15:53:04 +0000953 unsigned NumCycles,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000954 unsigned ExtraPredCycles,
955 const BranchProbability &Probability) const {
956 return true;
957}
958
959
960bool
961HexagonInstrInfo::
962isProfitableToIfCvt(MachineBasicBlock &TMBB,
963 unsigned NumTCycles,
964 unsigned ExtraTCycles,
965 MachineBasicBlock &FMBB,
966 unsigned NumFCycles,
967 unsigned ExtraFCycles,
968 const BranchProbability &Probability) const {
969 return true;
970}
971
Jyotsna Verma84c47102013-05-06 18:49:23 +0000972// Returns true if an instruction is predicated irrespective of the predicate
973// sense. For example, all of the following will return true.
974// if (p0) R1 = add(R2, R3)
975// if (!p0) R1 = add(R2, R3)
976// if (p0.new) R1 = add(R2, R3)
977// if (!p0.new) R1 = add(R2, R3)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000978bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoon6f358372012-02-08 18:25:47 +0000979 const uint64_t F = MI->getDesc().TSFlags;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000980
Brendon Cahoon6f358372012-02-08 18:25:47 +0000981 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000982}
983
Jyotsna Verma84c47102013-05-06 18:49:23 +0000984bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
985 const uint64_t F = get(Opcode).TSFlags;
986
987 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
988}
989
990bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
991 const uint64_t F = MI->getDesc().TSFlags;
992
993 assert(isPredicated(MI));
994 return (!((F >> HexagonII::PredicatedFalsePos) &
995 HexagonII::PredicatedFalseMask));
996}
997
998bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
999 const uint64_t F = get(Opcode).TSFlags;
1000
1001 // Make sure that the instruction is predicated.
1002 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1003 return (!((F >> HexagonII::PredicatedFalsePos) &
1004 HexagonII::PredicatedFalseMask));
1005}
1006
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001007bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1008 const uint64_t F = MI->getDesc().TSFlags;
1009
1010 assert(isPredicated(MI));
1011 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1012}
1013
Jyotsna Verma84c47102013-05-06 18:49:23 +00001014bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1015 const uint64_t F = get(Opcode).TSFlags;
1016
1017 assert(isPredicated(Opcode));
1018 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1019}
1020
Jyotsna Verma438cec52013-05-10 20:58:11 +00001021// Returns true, if a ST insn can be promoted to a new-value store.
1022bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
Jyotsna Verma438cec52013-05-10 20:58:11 +00001023 const uint64_t F = MI->getDesc().TSFlags;
1024
1025 return ((F >> HexagonII::mayNVStorePos) &
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001026 HexagonII::mayNVStoreMask);
Jyotsna Verma438cec52013-05-10 20:58:11 +00001027}
1028
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001029bool
1030HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1031 std::vector<MachineOperand> &Pred) const {
1032 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1033 MachineOperand MO = MI->getOperand(oper);
1034 if (MO.isReg() && MO.isDef()) {
1035 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
Craig Topperc7242e02012-04-20 07:30:17 +00001036 if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001037 Pred.push_back(MO);
1038 return true;
1039 }
1040 }
1041 }
1042 return false;
1043}
1044
1045
1046bool
1047HexagonInstrInfo::
1048SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1049 const SmallVectorImpl<MachineOperand> &Pred2) const {
1050 // TODO: Fix this
1051 return false;
1052}
1053
1054
1055//
1056// We indicate that we want to reverse the branch by
1057// inserting a 0 at the beginning of the Cond vector.
1058//
1059bool HexagonInstrInfo::
1060ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1061 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1062 Cond.erase(Cond.begin());
1063 } else {
1064 Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1065 }
1066 return false;
1067}
1068
1069
1070bool HexagonInstrInfo::
1071isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1072 const BranchProbability &Probability) const {
1073 return (NumInstrs <= 4);
1074}
1075
1076bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1077 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001078 default: return false;
Colin LeMahieu14455532015-01-06 16:15:15 +00001079 case Hexagon::L4_return:
1080 case Hexagon::L4_return_t:
1081 case Hexagon::L4_return_f:
1082 case Hexagon::L4_return_tnew_pnt:
1083 case Hexagon::L4_return_fnew_pnt:
1084 case Hexagon::L4_return_tnew_pt:
1085 case Hexagon::L4_return_fnew_pt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001086 return true;
1087 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001088}
1089
1090
1091bool HexagonInstrInfo::
1092isValidOffset(const int Opcode, const int Offset) const {
1093 // This function is to check whether the "Offset" is in the correct range of
1094 // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1095 // inserted to calculate the final address. Due to this reason, the function
1096 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00001097 // We used to assert if the offset was not properly aligned, however,
1098 // there are cases where a misaligned pointer recast can cause this
1099 // problem, and we need to allow for it. The front end warns of such
1100 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001101
1102 switch(Opcode) {
1103
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001104 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001105 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001106 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1107 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1108
Colin LeMahieu947cd702014-12-23 20:44:59 +00001109 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001110 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001111 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1112 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1113
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001114 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001115 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001116 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001117 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1118 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1119
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001120 case Hexagon::L2_loadrb_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001121 case Hexagon::S2_storerb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001122 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001123 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1124 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1125
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001126 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001127 case Hexagon::TFR_FI:
1128 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1129 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1130
Colin LeMahieudacf0572015-01-05 21:36:38 +00001131 case Hexagon::L4_iadd_memopw_io:
1132 case Hexagon::L4_isub_memopw_io:
1133 case Hexagon::L4_add_memopw_io:
1134 case Hexagon::L4_sub_memopw_io:
1135 case Hexagon::L4_and_memopw_io:
1136 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001137 return (0 <= Offset && Offset <= 255);
1138
Colin LeMahieudacf0572015-01-05 21:36:38 +00001139 case Hexagon::L4_iadd_memoph_io:
1140 case Hexagon::L4_isub_memoph_io:
1141 case Hexagon::L4_add_memoph_io:
1142 case Hexagon::L4_sub_memoph_io:
1143 case Hexagon::L4_and_memoph_io:
1144 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001145 return (0 <= Offset && Offset <= 127);
1146
Colin LeMahieudacf0572015-01-05 21:36:38 +00001147 case Hexagon::L4_iadd_memopb_io:
1148 case Hexagon::L4_isub_memopb_io:
1149 case Hexagon::L4_add_memopb_io:
1150 case Hexagon::L4_sub_memopb_io:
1151 case Hexagon::L4_and_memopb_io:
1152 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001153 return (0 <= Offset && Offset <= 63);
1154
1155 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1156 // any size. Later pass knows how to handle it.
1157 case Hexagon::STriw_pred:
1158 case Hexagon::LDriw_pred:
1159 return true;
1160
Colin LeMahieu5ccbb122014-12-19 00:06:53 +00001161 case Hexagon::J2_loop0i:
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +00001162 return isUInt<10>(Offset);
1163
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001164 // INLINEASM is very special.
1165 case Hexagon::INLINEASM:
1166 return true;
1167 }
1168
Benjamin Kramerb6684012011-12-27 11:41:05 +00001169 llvm_unreachable("No offset range is defined for this opcode. "
1170 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001171}
1172
1173
1174//
1175// Check if the Offset is a valid auto-inc imm by Load/Store Type.
1176//
1177bool HexagonInstrInfo::
1178isValidAutoIncImm(const EVT VT, const int Offset) const {
1179
1180 if (VT == MVT::i64) {
1181 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1182 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1183 (Offset & 0x7) == 0);
1184 }
1185 if (VT == MVT::i32) {
1186 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1187 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1188 (Offset & 0x3) == 0);
1189 }
1190 if (VT == MVT::i16) {
1191 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1192 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1193 (Offset & 0x1) == 0);
1194 }
1195 if (VT == MVT::i8) {
1196 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1197 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1198 }
Craig Toppere55c5562012-02-07 02:50:20 +00001199 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001200}
1201
1202
1203bool HexagonInstrInfo::
1204isMemOp(const MachineInstr *MI) const {
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001205// return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1206
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001207 switch (MI->getOpcode())
1208 {
Colin LeMahieudacf0572015-01-05 21:36:38 +00001209 default: return false;
1210 case Hexagon::L4_iadd_memopw_io:
1211 case Hexagon::L4_isub_memopw_io:
1212 case Hexagon::L4_add_memopw_io:
1213 case Hexagon::L4_sub_memopw_io:
1214 case Hexagon::L4_and_memopw_io:
1215 case Hexagon::L4_or_memopw_io:
1216 case Hexagon::L4_iadd_memoph_io:
1217 case Hexagon::L4_isub_memoph_io:
1218 case Hexagon::L4_add_memoph_io:
1219 case Hexagon::L4_sub_memoph_io:
1220 case Hexagon::L4_and_memoph_io:
1221 case Hexagon::L4_or_memoph_io:
1222 case Hexagon::L4_iadd_memopb_io:
1223 case Hexagon::L4_isub_memopb_io:
1224 case Hexagon::L4_add_memopb_io:
1225 case Hexagon::L4_sub_memopb_io:
1226 case Hexagon::L4_and_memopb_io:
1227 case Hexagon::L4_or_memopb_io:
1228 case Hexagon::L4_ior_memopb_io:
1229 case Hexagon::L4_ior_memoph_io:
1230 case Hexagon::L4_ior_memopw_io:
1231 case Hexagon::L4_iand_memopb_io:
1232 case Hexagon::L4_iand_memoph_io:
1233 case Hexagon::L4_iand_memopw_io:
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001234 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001235 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001236 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001237}
1238
1239
1240bool HexagonInstrInfo::
1241isSpillPredRegOp(const MachineInstr *MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001242 switch (MI->getOpcode()) {
1243 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001244 case Hexagon::STriw_pred :
1245 case Hexagon::LDriw_pred :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001246 return true;
Sirish Pande2c7bf002012-04-23 17:49:28 +00001247 }
Sirish Pande4bd20c52012-05-12 05:10:30 +00001248}
1249
1250bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1251 switch (MI->getOpcode()) {
Sirish Pande8bb97452012-05-12 05:54:15 +00001252 default: return false;
Colin LeMahieu902157c2014-11-25 18:20:52 +00001253 case Hexagon::C2_cmpeq:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001254 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001255 case Hexagon::C2_cmpgt:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001256 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001257 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001258 case Hexagon::C2_cmpgtui:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001259 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +00001260 }
Sirish Pande2c7bf002012-04-23 17:49:28 +00001261}
1262
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001263bool HexagonInstrInfo::
1264isConditionalTransfer (const MachineInstr *MI) const {
1265 switch (MI->getOpcode()) {
1266 default: return false;
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001267 case Hexagon::A2_tfrt:
1268 case Hexagon::A2_tfrf:
1269 case Hexagon::C2_cmoveit:
1270 case Hexagon::C2_cmoveif:
1271 case Hexagon::A2_tfrtnew:
1272 case Hexagon::A2_tfrfnew:
1273 case Hexagon::C2_cmovenewit:
1274 case Hexagon::C2_cmovenewif:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001275 return true;
1276 }
1277}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001278
1279bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001280 switch (MI->getOpcode())
1281 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001282 default: return false;
Colin LeMahieuefa74e02014-11-18 20:28:11 +00001283 case Hexagon::A2_paddf:
1284 case Hexagon::A2_paddfnew:
1285 case Hexagon::A2_paddt:
1286 case Hexagon::A2_paddtnew:
Colin LeMahieu44fd1c82014-11-18 22:45:47 +00001287 case Hexagon::A2_pandf:
1288 case Hexagon::A2_pandfnew:
1289 case Hexagon::A2_pandt:
1290 case Hexagon::A2_pandtnew:
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001291 case Hexagon::A4_paslhf:
1292 case Hexagon::A4_paslhfnew:
1293 case Hexagon::A4_paslht:
1294 case Hexagon::A4_paslhtnew:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001295 case Hexagon::A4_pasrhf:
1296 case Hexagon::A4_pasrhfnew:
1297 case Hexagon::A4_pasrht:
1298 case Hexagon::A4_pasrhtnew:
Colin LeMahieu21866542014-11-19 22:58:04 +00001299 case Hexagon::A2_porf:
1300 case Hexagon::A2_porfnew:
1301 case Hexagon::A2_port:
1302 case Hexagon::A2_portnew:
Colin LeMahieue88447d2014-11-21 21:19:18 +00001303 case Hexagon::A2_psubf:
1304 case Hexagon::A2_psubfnew:
1305 case Hexagon::A2_psubt:
1306 case Hexagon::A2_psubtnew:
Colin LeMahieuac006432014-11-19 23:22:23 +00001307 case Hexagon::A2_pxorf:
1308 case Hexagon::A2_pxorfnew:
1309 case Hexagon::A2_pxort:
1310 case Hexagon::A2_pxortnew:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001311 case Hexagon::A4_psxthf:
1312 case Hexagon::A4_psxthfnew:
1313 case Hexagon::A4_psxtht:
1314 case Hexagon::A4_psxthtnew:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001315 case Hexagon::A4_psxtbf:
1316 case Hexagon::A4_psxtbfnew:
1317 case Hexagon::A4_psxtbt:
1318 case Hexagon::A4_psxtbtnew:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001319 case Hexagon::A4_pzxtbf:
1320 case Hexagon::A4_pzxtbfnew:
1321 case Hexagon::A4_pzxtbt:
1322 case Hexagon::A4_pzxtbtnew:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001323 case Hexagon::A4_pzxthf:
1324 case Hexagon::A4_pzxthfnew:
1325 case Hexagon::A4_pzxtht:
1326 case Hexagon::A4_pzxthtnew:
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001327 case Hexagon::A2_paddit:
1328 case Hexagon::A2_paddif:
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001329 case Hexagon::C2_ccombinewt:
1330 case Hexagon::C2_ccombinewf:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001331 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001332 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001333}
1334
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001335bool HexagonInstrInfo::
1336isConditionalLoad (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001337 switch (MI->getOpcode())
1338 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001339 default: return false;
Colin LeMahieu947cd702014-12-23 20:44:59 +00001340 case Hexagon::L2_ploadrdt_io :
1341 case Hexagon::L2_ploadrdf_io:
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001342 case Hexagon::L2_ploadrit_io:
1343 case Hexagon::L2_ploadrif_io:
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001344 case Hexagon::L2_ploadrht_io:
1345 case Hexagon::L2_ploadrhf_io:
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001346 case Hexagon::L2_ploadrbt_io:
1347 case Hexagon::L2_ploadrbf_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001348 case Hexagon::L2_ploadruht_io:
1349 case Hexagon::L2_ploadruhf_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001350 case Hexagon::L2_ploadrubt_io:
1351 case Hexagon::L2_ploadrubf_io:
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:
Colin LeMahieu9161d472014-12-30 18:58:47 +00001364 case Hexagon::L4_ploadrdt_rr:
1365 case Hexagon::L4_ploadrdf_rr:
1366 case Hexagon::L4_ploadrbt_rr:
1367 case Hexagon::L4_ploadrbf_rr:
1368 case Hexagon::L4_ploadrubt_rr:
1369 case Hexagon::L4_ploadrubf_rr:
1370 case Hexagon::L4_ploadrht_rr:
1371 case Hexagon::L4_ploadrhf_rr:
1372 case Hexagon::L4_ploadruht_rr:
1373 case Hexagon::L4_ploadruhf_rr:
1374 case Hexagon::L4_ploadrit_rr:
1375 case Hexagon::L4_ploadrif_rr:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001376 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001377 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001378}
Andrew Trickd06df962012-02-01 22:13:57 +00001379
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001380// Returns true if an instruction is a conditional store.
1381//
1382// Note: It doesn't include conditional new-value stores as they can't be
1383// converted to .new predicate.
1384//
1385// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1386// ^ ^
1387// / \ (not OK. it will cause new-value store to be
1388// / X conditional on p0.new while R2 producer is
1389// / \ on p0)
1390// / \.
1391// p.new store p.old NV store
1392// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1393// ^ ^
1394// \ /
1395// \ /
1396// \ /
1397// p.old store
1398// [if (p0)memw(R0+#0)=R2]
1399//
1400// The above diagram shows the steps involoved in the conversion of a predicated
1401// store instruction to its .new predicated new-value form.
1402//
1403// The following set of instructions further explains the scenario where
1404// conditional new-value store becomes invalid when promoted to .new predicate
1405// form.
1406//
1407// { 1) if (p0) r0 = add(r1, r2)
1408// 2) p0 = cmp.eq(r3, #0) }
1409//
1410// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1411// the first two instructions because in instr 1, r0 is conditional on old value
1412// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1413// is not valid for new-value stores.
1414bool HexagonInstrInfo::
1415isConditionalStore (const MachineInstr* MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001416 switch (MI->getOpcode())
1417 {
1418 default: return false;
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001419 case Hexagon::S4_storeirbt_io:
1420 case Hexagon::S4_storeirbf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001421 case Hexagon::S4_pstorerbt_rr:
1422 case Hexagon::S4_pstorerbf_rr:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001423 case Hexagon::S2_pstorerbt_io:
1424 case Hexagon::S2_pstorerbf_io:
Colin LeMahieu3d34afb2014-12-29 19:42:14 +00001425 case Hexagon::S2_pstorerbt_pi:
1426 case Hexagon::S2_pstorerbf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001427 case Hexagon::S2_pstorerdt_io:
1428 case Hexagon::S2_pstorerdf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001429 case Hexagon::S4_pstorerdt_rr:
1430 case Hexagon::S4_pstorerdf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001431 case Hexagon::S2_pstorerdt_pi:
1432 case Hexagon::S2_pstorerdf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001433 case Hexagon::S2_pstorerht_io:
1434 case Hexagon::S2_pstorerhf_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001435 case Hexagon::S4_storeirht_io:
1436 case Hexagon::S4_storeirhf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001437 case Hexagon::S4_pstorerht_rr:
1438 case Hexagon::S4_pstorerhf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001439 case Hexagon::S2_pstorerht_pi:
1440 case Hexagon::S2_pstorerhf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001441 case Hexagon::S2_pstorerit_io:
1442 case Hexagon::S2_pstorerif_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001443 case Hexagon::S4_storeirit_io:
1444 case Hexagon::S4_storeirif_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001445 case Hexagon::S4_pstorerit_rr:
1446 case Hexagon::S4_pstorerif_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001447 case Hexagon::S2_pstorerit_pi:
1448 case Hexagon::S2_pstorerif_pi:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001449
1450 // V4 global address store before promoting to dot new.
Colin LeMahieu14455532015-01-06 16:15:15 +00001451 case Hexagon::S4_pstorerdt_abs:
1452 case Hexagon::S4_pstorerdf_abs:
1453 case Hexagon::S4_pstorerbt_abs:
1454 case Hexagon::S4_pstorerbf_abs:
1455 case Hexagon::S4_pstorerht_abs:
1456 case Hexagon::S4_pstorerhf_abs:
1457 case Hexagon::S4_pstorerit_abs:
1458 case Hexagon::S4_pstorerif_abs:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001459 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001460
1461 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1462 // from the "Conditional Store" list. Because a predicated new value store
1463 // would NOT be promoted to a double dot new store. See diagram below:
1464 // This function returns yes for those stores that are predicated but not
1465 // yet promoted to predicate dot new instructions.
1466 //
1467 // +---------------------+
1468 // /-----| if (p0) memw(..)=r0 |---------\~
1469 // || +---------------------+ ||
1470 // promote || /\ /\ || promote
1471 // || /||\ /||\ ||
1472 // \||/ demote || \||/
1473 // \/ || || \/
1474 // +-------------------------+ || +-------------------------+
1475 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1476 // +-------------------------+ || +-------------------------+
1477 // || || ||
1478 // || demote \||/
1479 // promote || \/ NOT possible
1480 // || || /\~
1481 // \||/ || /||\~
1482 // \/ || ||
1483 // +-----------------------------+
1484 // | if (p0.new) memw(..)=r0.new |
1485 // +-----------------------------+
1486 // Double Dot New Store
1487 //
1488 }
1489}
1490
Jyotsna Verma84c47102013-05-06 18:49:23 +00001491
1492bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1493 if (isNewValue(MI) && isBranch(MI))
1494 return true;
1495 return false;
1496}
1497
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001498bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1499 return (getAddrMode(MI) == HexagonII::PostInc);
1500}
1501
Jyotsna Verma84c47102013-05-06 18:49:23 +00001502bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1503 const uint64_t F = MI->getDesc().TSFlags;
1504 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1505}
1506
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001507// Returns true, if any one of the operands is a dot new
1508// insn, whether it is predicated dot new or register dot new.
1509bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1510 return (isNewValueInst(MI) ||
1511 (isPredicated(MI) && isPredicatedNew(MI)));
1512}
1513
Jyotsna Verma438cec52013-05-10 20:58:11 +00001514// Returns the most basic instruction for the .new predicated instructions and
1515// new-value stores.
1516// For example, all of the following instructions will be converted back to the
1517// same instruction:
1518// 1) if (p0.new) memw(R0+#0) = R1.new --->
1519// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1520// 3) if (p0.new) memw(R0+#0) = R1 --->
1521//
1522
1523int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1524 int NewOp = opc;
1525 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1526 NewOp = Hexagon::getPredOldOpcode(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001527 assert(NewOp >= 0 &&
1528 "Couldn't change predicate new instruction to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001529 }
1530
Alp Tokerf907b892013-12-05 05:44:44 +00001531 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
Jyotsna Verma438cec52013-05-10 20:58:11 +00001532 NewOp = Hexagon::getNonNVStore(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001533 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001534 }
1535 return NewOp;
1536}
1537
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001538// Return the new value instruction for a given store.
1539int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1540 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1541 if (NVOpcode >= 0) // Valid new-value store instruction.
1542 return NVOpcode;
1543
1544 switch (MI->getOpcode()) {
1545 default: llvm_unreachable("Unknown .new type");
1546 // store new value byte
Colin LeMahieuc0434462015-02-04 17:52:06 +00001547 case Hexagon::S4_storerb_ur:
1548 return Hexagon::S4_storerbnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001549
Colin LeMahieuc0434462015-02-04 17:52:06 +00001550 case Hexagon::S4_storerh_ur:
1551 return Hexagon::S4_storerhnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001552
Colin LeMahieuc0434462015-02-04 17:52:06 +00001553 case Hexagon::S4_storeri_ur:
1554 return Hexagon::S4_storerinew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001555
1556 }
1557 return 0;
1558}
1559
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001560// Return .new predicate version for an instruction.
1561int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1562 const MachineBranchProbabilityInfo
1563 *MBPI) const {
1564
1565 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1566 if (NewOpcode >= 0) // Valid predicate new instruction
1567 return NewOpcode;
1568
1569 switch (MI->getOpcode()) {
1570 default: llvm_unreachable("Unknown .new type");
1571 // Condtional Jumps
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001572 case Hexagon::J2_jumpt:
1573 case Hexagon::J2_jumpf:
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001574 return getDotNewPredJumpOp(MI, MBPI);
1575
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001576 case Hexagon::J2_jumprt:
1577 return Hexagon::J2_jumptnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001578
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001579 case Hexagon::J2_jumprf:
1580 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001581
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001582 case Hexagon::JMPrett:
1583 return Hexagon::J2_jumprtnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001584
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001585 case Hexagon::JMPretf:
1586 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001587
1588
1589 // Conditional combine
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001590 case Hexagon::C2_ccombinewt:
1591 return Hexagon::C2_ccombinewnewt;
1592 case Hexagon::C2_ccombinewf:
1593 return Hexagon::C2_ccombinewnewf;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001594 }
1595}
1596
1597
Jyotsna Verma84256432013-03-01 17:37:13 +00001598unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1599 const uint64_t F = MI->getDesc().TSFlags;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001600
Jyotsna Verma84256432013-03-01 17:37:13 +00001601 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1602}
1603
1604/// immediateExtend - Changes the instruction in place to one using an immediate
1605/// extender.
1606void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1607 assert((isExtendable(MI)||isConstExtended(MI)) &&
1608 "Instruction must be extendable");
1609 // Find which operand is extendable.
1610 short ExtOpNum = getCExtOpNum(MI);
1611 MachineOperand &MO = MI->getOperand(ExtOpNum);
1612 // This needs to be something we understand.
1613 assert((MO.isMBB() || MO.isImm()) &&
1614 "Branch with unknown extendable field type");
1615 // Mark given operand as extended.
1616 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1617}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001618
Eric Christopher143f02c2014-10-09 01:59:35 +00001619DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1620 const TargetSubtargetInfo &STI) const {
1621 const InstrItineraryData *II = STI.getInstrItineraryData();
1622 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
Andrew Trickd06df962012-02-01 22:13:57 +00001623}
1624
1625bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1626 const MachineBasicBlock *MBB,
1627 const MachineFunction &MF) const {
1628 // Debug info is never a scheduling boundary. It's necessary to be explicit
1629 // due to the special treatment of IT instructions below, otherwise a
1630 // dbg_value followed by an IT will result in the IT instruction being
1631 // considered a scheduling hazard, which is wrong. It should be the actual
1632 // instruction preceding the dbg_value instruction(s), just like it is
1633 // when debug info is not present.
1634 if (MI->isDebugValue())
1635 return false;
1636
1637 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001638 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
Andrew Trickd06df962012-02-01 22:13:57 +00001639 return true;
1640
1641 return false;
1642}
Jyotsna Verma84256432013-03-01 17:37:13 +00001643
1644bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001645 const uint64_t F = MI->getDesc().TSFlags;
1646 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1647 if (isExtended) // Instruction must be extended.
1648 return true;
1649
1650 unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1651 & HexagonII::ExtendableMask;
1652 if (!isExtendable)
1653 return false;
1654
1655 short ExtOpNum = getCExtOpNum(MI);
1656 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1657 // Use MO operand flags to determine if MO
1658 // has the HMOTF_ConstExtended flag set.
1659 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1660 return true;
1661 // If this is a Machine BB address we are talking about, and it is
1662 // not marked as extended, say so.
1663 if (MO.isMBB())
1664 return false;
1665
1666 // We could be using an instruction with an extendable immediate and shoehorn
1667 // a global address into it. If it is a global address it will be constant
1668 // extended. We do this for COMBINE.
1669 // We currently only handle isGlobal() because it is the only kind of
1670 // object we are going to end up with here for now.
1671 // In the future we probably should add isSymbol(), etc.
Colin LeMahieu2efa2d02015-03-09 21:48:13 +00001672 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress())
Jyotsna Verma84256432013-03-01 17:37:13 +00001673 return true;
1674
1675 // If the extendable operand is not 'Immediate' type, the instruction should
1676 // have 'isExtended' flag set.
1677 assert(MO.isImm() && "Extendable operand must be Immediate type");
1678
1679 int MinValue = getMinValue(MI);
1680 int MaxValue = getMaxValue(MI);
1681 int ImmValue = MO.getImm();
1682
1683 return (ImmValue < MinValue || ImmValue > MaxValue);
1684}
1685
Jyotsna Verma1d297502013-05-02 15:39:30 +00001686// Returns the opcode to use when converting MI, which is a conditional jump,
1687// into a conditional instruction which uses the .new value of the predicate.
1688// We also use branch probabilities to add a hint to the jump.
1689int
1690HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1691 const
1692 MachineBranchProbabilityInfo *MBPI) const {
1693
1694 // We assume that block can have at most two successors.
1695 bool taken = false;
1696 MachineBasicBlock *Src = MI->getParent();
1697 MachineOperand *BrTarget = &MI->getOperand(1);
1698 MachineBasicBlock *Dst = BrTarget->getMBB();
1699
1700 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1701 if (Prediction >= BranchProbability(1,2))
1702 taken = true;
1703
1704 switch (MI->getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001705 case Hexagon::J2_jumpt:
1706 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1707 case Hexagon::J2_jumpf:
1708 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Jyotsna Verma1d297502013-05-02 15:39:30 +00001709
1710 default:
1711 llvm_unreachable("Unexpected jump instruction.");
1712 }
1713}
Jyotsna Verma84256432013-03-01 17:37:13 +00001714// Returns true if a particular operand is extendable for an instruction.
1715bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1716 unsigned short OperandNum) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001717 const uint64_t F = MI->getDesc().TSFlags;
1718
1719 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1720 == OperandNum;
1721}
1722
1723// Returns Operand Index for the constant extended instruction.
1724unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1725 const uint64_t F = MI->getDesc().TSFlags;
1726 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1727}
1728
1729// Returns the min value that doesn't need to be extended.
1730int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1731 const uint64_t F = MI->getDesc().TSFlags;
1732 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1733 & HexagonII::ExtentSignedMask;
1734 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1735 & HexagonII::ExtentBitsMask;
1736
1737 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001738 return -1U << (bits - 1);
Jyotsna Verma84256432013-03-01 17:37:13 +00001739 else
1740 return 0;
1741}
1742
1743// Returns the max value that doesn't need to be extended.
1744int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1745 const uint64_t F = MI->getDesc().TSFlags;
1746 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1747 & HexagonII::ExtentSignedMask;
1748 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1749 & HexagonII::ExtentBitsMask;
1750
1751 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001752 return ~(-1U << (bits - 1));
Jyotsna Verma84256432013-03-01 17:37:13 +00001753 else
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001754 return ~(-1U << bits);
Jyotsna Verma84256432013-03-01 17:37:13 +00001755}
1756
1757// Returns true if an instruction can be converted into a non-extended
1758// equivalent instruction.
1759bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1760
1761 short NonExtOpcode;
1762 // Check if the instruction has a register form that uses register in place
1763 // of the extended operand, if so return that as the non-extended form.
1764 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1765 return true;
1766
1767 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001768 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001769
1770 switch (getAddrMode(MI)) {
1771 case HexagonII::Absolute :
1772 // Load/store with absolute addressing mode can be converted into
1773 // base+offset mode.
1774 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1775 break;
1776 case HexagonII::BaseImmOffset :
1777 // Load/store with base+offset addressing mode can be converted into
1778 // base+register offset addressing mode. However left shift operand should
1779 // be set to 0.
1780 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1781 break;
1782 default:
1783 return false;
1784 }
1785 if (NonExtOpcode < 0)
1786 return false;
1787 return true;
1788 }
1789 return false;
1790}
1791
1792// Returns opcode of the non-extended equivalent instruction.
1793short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1794
1795 // Check if the instruction has a register form that uses register in place
1796 // of the extended operand, if so return that as the non-extended form.
1797 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1798 if (NonExtOpcode >= 0)
1799 return NonExtOpcode;
1800
1801 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001802 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001803 switch (getAddrMode(MI)) {
1804 case HexagonII::Absolute :
1805 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1806 case HexagonII::BaseImmOffset :
1807 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1808 default:
1809 return -1;
1810 }
1811 }
1812 return -1;
1813}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001814
1815bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001816 return (Opcode == Hexagon::J2_jumpt) ||
1817 (Opcode == Hexagon::J2_jumpf) ||
1818 (Opcode == Hexagon::J2_jumptnewpt) ||
1819 (Opcode == Hexagon::J2_jumpfnewpt) ||
1820 (Opcode == Hexagon::J2_jumpt) ||
1821 (Opcode == Hexagon::J2_jumpf);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001822}
1823
1824bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001825 return (Opcode == Hexagon::J2_jumpf) ||
1826 (Opcode == Hexagon::J2_jumpfnewpt) ||
1827 (Opcode == Hexagon::J2_jumpfnew);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001828}