blob: 1a330053a1d39e9ef1f4b581a0425119525b2810 [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;
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000369 case Hexagon::A4_cmpheqi:
370 case Hexagon::A4_cmpheq:
371 case Hexagon::A4_cmphgtui:
372 case Hexagon::A4_cmphgtu:
373 case Hexagon::A4_cmphgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000374 SrcReg = MI->getOperand(1).getReg();
375 Mask = 0xFFFF;
376 break;
377 }
378
379 // Set the value/second source register.
380 switch (Opc) {
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000381 case Hexagon::C2_cmpeqp:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000382 case Hexagon::C2_cmpeq:
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000383 case Hexagon::C2_cmpgtp:
384 case Hexagon::C2_cmpgtup:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000385 case Hexagon::C2_cmpgtu:
386 case Hexagon::C2_cmpgt:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000387 case Hexagon::A4_cmpbeq:
388 case Hexagon::A4_cmpbgtu:
389 case Hexagon::A4_cmpbgt:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000390 case Hexagon::A4_cmpheq:
391 case Hexagon::A4_cmphgtu:
392 case Hexagon::A4_cmphgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000393 SrcReg2 = MI->getOperand(2).getReg();
394 return true;
395
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000396 case Hexagon::C2_cmpeqi:
397 case Hexagon::C2_cmpgtui:
398 case Hexagon::C2_cmpgti:
Colin LeMahieufa947902015-01-14 16:49:12 +0000399 case Hexagon::A4_cmpbeqi:
Colin LeMahieuffacc6e2015-01-14 18:05:44 +0000400 case Hexagon::A4_cmpbgtui:
Colin LeMahieuc91fabc2015-01-14 18:26:14 +0000401 case Hexagon::A4_cmpheqi:
402 case Hexagon::A4_cmphgtui:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000403 SrcReg2 = 0;
404 Value = MI->getOperand(2).getImm();
405 return true;
406 }
407
408 return false;
409}
410
411
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000412void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
413 MachineBasicBlock::iterator I, DebugLoc DL,
414 unsigned DestReg, unsigned SrcReg,
415 bool KillSrc) const {
416 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000417 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000418 return;
419 }
420 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000421 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000422 return;
423 }
424 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
425 // Map Pd = Ps to Pd = or(Ps, Ps).
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000426 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000427 DestReg).addReg(SrcReg).addReg(SrcReg);
428 return;
429 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000430 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
431 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000432 // We can have an overlap between single and double reg: r1:0 = r0.
433 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
434 // r1:0 = r0
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000435 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000436 Hexagon::subreg_hireg))).addImm(0);
437 } else {
438 // r1:0 = r1 or no overlap.
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000439 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000440 Hexagon::subreg_loreg))).addReg(SrcReg);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000441 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000442 Hexagon::subreg_hireg))).addImm(0);
443 }
444 return;
445 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000446 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000447 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000448 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000449 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000450 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000451 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
452 Hexagon::IntRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000453 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000454 addReg(SrcReg, getKillRegState(KillSrc));
455 return;
456 }
457 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
458 Hexagon::PredRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000459 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000460 addReg(SrcReg, getKillRegState(KillSrc));
461 return;
462 }
Sirish Pande30804c22012-02-15 18:52:27 +0000463
464 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465}
466
467
468void HexagonInstrInfo::
469storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
470 unsigned SrcReg, bool isKill, int FI,
471 const TargetRegisterClass *RC,
472 const TargetRegisterInfo *TRI) const {
473
474 DebugLoc DL = MBB.findDebugLoc(I);
475 MachineFunction &MF = *MBB.getParent();
476 MachineFrameInfo &MFI = *MF.getFrameInfo();
477 unsigned Align = MFI.getObjectAlignment(FI);
478
479 MachineMemOperand *MMO =
480 MF.getMachineMemOperand(
481 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
482 MachineMemOperand::MOStore,
483 MFI.getObjectSize(FI),
484 Align);
485
Craig Topperc7242e02012-04-20 07:30:17 +0000486 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000487 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000488 .addFrameIndex(FI).addImm(0)
489 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000490 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000491 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000492 .addFrameIndex(FI).addImm(0)
493 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000494 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
496 .addFrameIndex(FI).addImm(0)
497 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
498 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000499 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000500 }
501}
502
503
504void HexagonInstrInfo::storeRegToAddr(
505 MachineFunction &MF, unsigned SrcReg,
506 bool isKill,
507 SmallVectorImpl<MachineOperand> &Addr,
508 const TargetRegisterClass *RC,
509 SmallVectorImpl<MachineInstr*> &NewMIs) const
510{
Craig Toppere55c5562012-02-07 02:50:20 +0000511 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000512}
513
514
515void HexagonInstrInfo::
516loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
517 unsigned DestReg, int FI,
518 const TargetRegisterClass *RC,
519 const TargetRegisterInfo *TRI) const {
520 DebugLoc DL = MBB.findDebugLoc(I);
521 MachineFunction &MF = *MBB.getParent();
522 MachineFrameInfo &MFI = *MF.getFrameInfo();
523 unsigned Align = MFI.getObjectAlignment(FI);
524
525 MachineMemOperand *MMO =
526 MF.getMachineMemOperand(
527 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
528 MachineMemOperand::MOLoad,
529 MFI.getObjectSize(FI),
530 Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000531 if (RC == &Hexagon::IntRegsRegClass) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000532 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000533 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000534 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000535 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000536 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000537 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000538 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
539 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
540 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000541 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000542 }
543}
544
545
546void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
547 SmallVectorImpl<MachineOperand> &Addr,
548 const TargetRegisterClass *RC,
549 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Craig Toppere55c5562012-02-07 02:50:20 +0000550 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000551}
552
553
554MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
555 MachineInstr* MI,
556 const SmallVectorImpl<unsigned> &Ops,
557 int FI) const {
558 // Hexagon_TODO: Implement.
Craig Topper062a2ba2014-04-25 05:30:21 +0000559 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000560}
561
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000562unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
563
564 MachineRegisterInfo &RegInfo = MF->getRegInfo();
565 const TargetRegisterClass *TRC;
Sirish Pande69295b82012-05-10 20:20:25 +0000566 if (VT == MVT::i1) {
Craig Topperc7242e02012-04-20 07:30:17 +0000567 TRC = &Hexagon::PredRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000568 } else if (VT == MVT::i32 || VT == MVT::f32) {
Craig Topperc7242e02012-04-20 07:30:17 +0000569 TRC = &Hexagon::IntRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000570 } else if (VT == MVT::i64 || VT == MVT::f64) {
Craig Topperc7242e02012-04-20 07:30:17 +0000571 TRC = &Hexagon::DoubleRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000572 } else {
Benjamin Kramerb6684012011-12-27 11:41:05 +0000573 llvm_unreachable("Cannot handle this register class");
Sirish Pande69295b82012-05-10 20:20:25 +0000574 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000575
576 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
577 return NewReg;
578}
579
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000580bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000581 const MCInstrDesc &MID = MI->getDesc();
582 const uint64_t F = MID.TSFlags;
583 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
584 return true;
585
586 // TODO: This is largely obsolete now. Will need to be removed
587 // in consecutive patches.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000588 switch(MI->getOpcode()) {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000589 // TFR_FI Remains a special case.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000590 case Hexagon::TFR_FI:
591 return true;
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000592 default:
593 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000594 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000595 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000596}
597
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000598// This returns true in two cases:
599// - The OP code itself indicates that this is an extended instruction.
600// - One of MOs has been marked with HMOTF_ConstExtended flag.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000601bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000602 // First check if this is permanently extended op code.
603 const uint64_t F = MI->getDesc().TSFlags;
604 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
605 return true;
606 // Use MO operand flags to determine if one of MI's operands
607 // has HMOTF_ConstExtended flag set.
608 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
609 E = MI->operands_end(); I != E; ++I) {
610 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Sirish Pande69295b82012-05-10 20:20:25 +0000611 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000612 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000613 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000614}
615
Jyotsna Verma84c47102013-05-06 18:49:23 +0000616bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
617 return MI->getDesc().isBranch();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000618}
619
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000620bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
621 if (isNewValueJump(MI))
622 return true;
623
624 if (isNewValueStore(MI))
625 return true;
626
627 return false;
628}
629
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000630bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
631 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
632}
Andrew Trickd06df962012-02-01 22:13:57 +0000633
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000634bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
635 bool isPred = MI->getDesc().isPredicable();
636
637 if (!isPred)
638 return false;
639
640 const int Opc = MI->getOpcode();
641
642 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000643 case Hexagon::A2_tfrsi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000644 return isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000645
Colin LeMahieubda31b42014-12-29 20:44:51 +0000646 case Hexagon::S2_storerd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000647 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000648
Colin LeMahieubda31b42014-12-29 20:44:51 +0000649 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000650 case Hexagon::S2_storerinew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000651 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000652
Colin LeMahieubda31b42014-12-29 20:44:51 +0000653 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000654 case Hexagon::S2_storerhnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000655 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000656
Colin LeMahieubda31b42014-12-29 20:44:51 +0000657 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +0000658 case Hexagon::S2_storerbnew_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000659 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000660
Colin LeMahieu947cd702014-12-23 20:44:59 +0000661 case Hexagon::L2_loadrd_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000662 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000663
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000664 case Hexagon::L2_loadri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000665 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000666
Colin LeMahieu8e39cad2014-12-23 17:25:57 +0000667 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +0000668 case Hexagon::L2_loadruh_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000669 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000670
Colin LeMahieu4b1eac42014-12-22 21:40:43 +0000671 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +0000672 case Hexagon::L2_loadrub_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000673 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000674
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000675 case Hexagon::L2_loadrd_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000676 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000677
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000678 case Hexagon::L2_loadri_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000679 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000680
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +0000681 case Hexagon::L2_loadrh_pi:
682 case Hexagon::L2_loadruh_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000683 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000684
Colin LeMahieu96976a12014-12-26 18:57:13 +0000685 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +0000686 case Hexagon::L2_loadrub_pi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000687 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000688
Colin LeMahieu2bad4a72014-12-30 21:01:38 +0000689 case Hexagon::S4_storeirb_io:
690 case Hexagon::S4_storeirh_io:
691 case Hexagon::S4_storeiri_io:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000692 return (isUInt<6>(MI->getOperand(1).getImm()) &&
693 isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000694
Colin LeMahieuf297dbe2015-02-05 17:49:13 +0000695 case Hexagon::A2_addi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000696 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000697
Colin LeMahieu3b3197e2014-11-24 17:44:19 +0000698 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +0000699 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +0000700 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +0000701 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +0000702 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +0000703 case Hexagon::A2_zxth:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +0000704 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000705 }
706
707 return true;
708}
709
Sirish Pande8bb97452012-05-12 05:54:15 +0000710// This function performs the following inversiones:
711//
712// cPt ---> cNotPt
713// cNotPt ---> cPt
714//
Sirish Pande30804c22012-02-15 18:52:27 +0000715unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
Jyotsna Verma84c47102013-05-06 18:49:23 +0000716 int InvPredOpcode;
717 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
718 : Hexagon::getTruePredOpcode(Opc);
719 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
720 return InvPredOpcode;
721
Sirish Pande30804c22012-02-15 18:52:27 +0000722 switch(Opc) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000723 default: llvm_unreachable("Unexpected predicated instruction");
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000724 case Hexagon::C2_ccombinewt:
725 return Hexagon::C2_ccombinewf;
726 case Hexagon::C2_ccombinewf:
727 return Hexagon::C2_ccombinewt;
Sirish Pande30804c22012-02-15 18:52:27 +0000728
Jyotsna Verma978e9722013-05-09 18:25:44 +0000729 // Dealloc_return.
Colin LeMahieu14455532015-01-06 16:15:15 +0000730 case Hexagon::L4_return_t:
731 return Hexagon::L4_return_f;
732 case Hexagon::L4_return_f:
733 return Hexagon::L4_return_t;
Sirish Pande30804c22012-02-15 18:52:27 +0000734 }
735}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000736
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000737// New Value Store instructions.
738bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
739 const uint64_t F = MI->getDesc().TSFlags;
740
741 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
742}
743
744bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
745 const uint64_t F = get(Opcode).TSFlags;
746
747 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
748}
Andrew Trickd06df962012-02-01 22:13:57 +0000749
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000750int HexagonInstrInfo::
751getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
Pranav Bhandarkar34b60182012-11-01 19:13:23 +0000752 enum Hexagon::PredSense inPredSense;
753 inPredSense = invertPredicate ? Hexagon::PredSense_false :
754 Hexagon::PredSense_true;
755 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
756 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
757 return CondOpcode;
758
759 // This switch case will be removed once all the instructions have been
760 // modified to use relation maps.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000761 switch(Opc) {
Sirish Pande69295b82012-05-10 20:20:25 +0000762 case Hexagon::TFRI_f:
763 return !invertPredicate ? Hexagon::TFRI_cPt_f :
764 Hexagon::TFRI_cNotPt_f;
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000765 case Hexagon::A2_combinew:
766 return !invertPredicate ? Hexagon::C2_ccombinewt :
767 Hexagon::C2_ccombinewf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000768
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000769 // DEALLOC_RETURN.
Colin LeMahieu14455532015-01-06 16:15:15 +0000770 case Hexagon::L4_return:
771 return !invertPredicate ? Hexagon::L4_return_t:
772 Hexagon::L4_return_f;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000773 }
Benjamin Kramerb6684012011-12-27 11:41:05 +0000774 llvm_unreachable("Unexpected predicable instruction");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000775}
776
777
778bool HexagonInstrInfo::
779PredicateInstruction(MachineInstr *MI,
780 const SmallVectorImpl<MachineOperand> &Cond) const {
781 int Opc = MI->getOpcode();
782 assert (isPredicable(MI) && "Expected predicable instruction");
783 bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
784 (Cond[0].getImm() == 0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000785
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000786 // This will change MI's opcode to its predicate version.
787 // However, its operand list is still the old one, i.e. the
788 // non-predicate one.
789 MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
790
791 int oper = -1;
792 unsigned int GAIdx = 0;
793
794 // Indicates whether the current MI has a GlobalAddress operand
795 bool hasGAOpnd = false;
796 std::vector<MachineOperand> tmpOpnds;
797
798 // Indicates whether we need to shift operands to right.
799 bool needShift = true;
800
801 // The predicate is ALWAYS the FIRST input operand !!!
802 if (MI->getNumOperands() == 0) {
803 // The non-predicate version of MI does not take any operands,
804 // i.e. no outs and no ins. In this condition, the predicate
805 // operand will be directly placed at Operands[0]. No operand
806 // shift is needed.
807 // Example: BARRIER
808 needShift = false;
809 oper = -1;
810 }
811 else if ( MI->getOperand(MI->getNumOperands()-1).isReg()
812 && MI->getOperand(MI->getNumOperands()-1).isDef()
813 && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
814 // The non-predicate version of MI does not have any input operands.
815 // In this condition, we extend the length of Operands[] by one and
816 // copy the original last operand to the newly allocated slot.
817 // At this moment, it is just a place holder. Later, we will put
818 // predicate operand directly into it. No operand shift is needed.
819 // Example: r0=BARRIER (this is a faked insn used here for illustration)
820 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
821 needShift = false;
822 oper = MI->getNumOperands() - 2;
823 }
824 else {
825 // We need to right shift all input operands by one. Duplicate the
826 // last operand into the newly allocated slot.
827 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
828 }
829
830 if (needShift)
831 {
832 // Operands[ MI->getNumOperands() - 2 ] has been copied into
833 // Operands[ MI->getNumOperands() - 1 ], so we start from
834 // Operands[ MI->getNumOperands() - 3 ].
835 // oper is a signed int.
836 // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
837 for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
838 {
839 MachineOperand &MO = MI->getOperand(oper);
840
841 // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4] Opnd[5] Opnd[6] Opnd[7]
842 // <Def0> <Def1> <Use0> <Use1> <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
843 // /\~
844 // /||\~
845 // ||
846 // Predicate Operand here
847 if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
848 break;
849 }
850 if (MO.isReg()) {
851 MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
852 MO.isImplicit(), MO.isKill(),
853 MO.isDead(), MO.isUndef(),
854 MO.isDebug());
855 }
856 else if (MO.isImm()) {
857 MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
858 }
859 else if (MO.isGlobal()) {
860 // MI can not have more than one GlobalAddress operand.
861 assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
862
863 // There is no member function called "ChangeToGlobalAddress" in the
864 // MachineOperand class (not like "ChangeToRegister" and
865 // "ChangeToImmediate"). So we have to remove them from Operands[] list
866 // first, and then add them back after we have inserted the predicate
867 // operand. tmpOpnds[] is to remember these operands before we remove
868 // them.
869 tmpOpnds.push_back(MO);
870
871 // Operands[oper] is a GlobalAddress operand;
872 // Operands[oper+1] has been copied into Operands[oper+2];
873 hasGAOpnd = true;
874 GAIdx = oper;
875 continue;
876 }
877 else {
Craig Topperd3c02f12015-01-05 10:15:49 +0000878 llvm_unreachable("Unexpected operand type");
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000879 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000880 }
881 }
882
883 int regPos = invertJump ? 1 : 0;
884 MachineOperand PredMO = Cond[regPos];
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000885
886 // [oper] now points to the last explicit Def. Predicate operand must be
887 // located at [oper+1]. See diagram above.
888 // This assumes that the predicate is always the first operand,
889 // i.e. Operands[0+numResults], in the set of inputs
890 // It is better to have an assert here to check this. But I don't know how
891 // to write this assert because findFirstPredOperandIdx() would return -1
892 if (oper < -1) oper = -1;
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000893
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000894 MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000895 PredMO.isImplicit(), false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000896 PredMO.isDead(), PredMO.isUndef(),
897 PredMO.isDebug());
898
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000899 MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
900 RegInfo.clearKillFlags(PredMO.getReg());
901
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000902 if (hasGAOpnd)
903 {
904 unsigned int i;
905
906 // Operands[GAIdx] is the original GlobalAddress operand, which is
907 // already copied into tmpOpnds[0].
908 // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
909 // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
910 // so we start from [GAIdx+2]
911 for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
912 tmpOpnds.push_back(MI->getOperand(i));
913
914 // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
915 // It is very important that we always remove from the end of Operands[]
916 // MI->getNumOperands() is at least 2 if program goes to here.
917 for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
918 MI->RemoveOperand(i);
919
920 for (i = 0; i < tmpOpnds.size(); ++i)
921 MI->addOperand(tmpOpnds[i]);
922 }
923
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000924 return true;
925}
926
927
928bool
929HexagonInstrInfo::
930isProfitableToIfCvt(MachineBasicBlock &MBB,
Kay Tiong Khoof2949212012-06-13 15:53:04 +0000931 unsigned NumCycles,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000932 unsigned ExtraPredCycles,
933 const BranchProbability &Probability) const {
934 return true;
935}
936
937
938bool
939HexagonInstrInfo::
940isProfitableToIfCvt(MachineBasicBlock &TMBB,
941 unsigned NumTCycles,
942 unsigned ExtraTCycles,
943 MachineBasicBlock &FMBB,
944 unsigned NumFCycles,
945 unsigned ExtraFCycles,
946 const BranchProbability &Probability) const {
947 return true;
948}
949
Jyotsna Verma84c47102013-05-06 18:49:23 +0000950// Returns true if an instruction is predicated irrespective of the predicate
951// sense. For example, all of the following will return true.
952// if (p0) R1 = add(R2, R3)
953// if (!p0) R1 = add(R2, R3)
954// if (p0.new) R1 = add(R2, R3)
955// if (!p0.new) R1 = add(R2, R3)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000956bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoon6f358372012-02-08 18:25:47 +0000957 const uint64_t F = MI->getDesc().TSFlags;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000958
Brendon Cahoon6f358372012-02-08 18:25:47 +0000959 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000960}
961
Jyotsna Verma84c47102013-05-06 18:49:23 +0000962bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
963 const uint64_t F = get(Opcode).TSFlags;
964
965 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
966}
967
968bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
969 const uint64_t F = MI->getDesc().TSFlags;
970
971 assert(isPredicated(MI));
972 return (!((F >> HexagonII::PredicatedFalsePos) &
973 HexagonII::PredicatedFalseMask));
974}
975
976bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
977 const uint64_t F = get(Opcode).TSFlags;
978
979 // Make sure that the instruction is predicated.
980 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
981 return (!((F >> HexagonII::PredicatedFalsePos) &
982 HexagonII::PredicatedFalseMask));
983}
984
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000985bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
986 const uint64_t F = MI->getDesc().TSFlags;
987
988 assert(isPredicated(MI));
989 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
990}
991
Jyotsna Verma84c47102013-05-06 18:49:23 +0000992bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
993 const uint64_t F = get(Opcode).TSFlags;
994
995 assert(isPredicated(Opcode));
996 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
997}
998
Jyotsna Verma438cec52013-05-10 20:58:11 +0000999// Returns true, if a ST insn can be promoted to a new-value store.
1000bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
1001 const HexagonRegisterInfo& QRI = getRegisterInfo();
1002 const uint64_t F = MI->getDesc().TSFlags;
1003
1004 return ((F >> HexagonII::mayNVStorePos) &
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001005 HexagonII::mayNVStoreMask);
Jyotsna Verma438cec52013-05-10 20:58:11 +00001006}
1007
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001008bool
1009HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1010 std::vector<MachineOperand> &Pred) const {
1011 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1012 MachineOperand MO = MI->getOperand(oper);
1013 if (MO.isReg() && MO.isDef()) {
1014 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
Craig Topperc7242e02012-04-20 07:30:17 +00001015 if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001016 Pred.push_back(MO);
1017 return true;
1018 }
1019 }
1020 }
1021 return false;
1022}
1023
1024
1025bool
1026HexagonInstrInfo::
1027SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1028 const SmallVectorImpl<MachineOperand> &Pred2) const {
1029 // TODO: Fix this
1030 return false;
1031}
1032
1033
1034//
1035// We indicate that we want to reverse the branch by
1036// inserting a 0 at the beginning of the Cond vector.
1037//
1038bool HexagonInstrInfo::
1039ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1040 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1041 Cond.erase(Cond.begin());
1042 } else {
1043 Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1044 }
1045 return false;
1046}
1047
1048
1049bool HexagonInstrInfo::
1050isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1051 const BranchProbability &Probability) const {
1052 return (NumInstrs <= 4);
1053}
1054
1055bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1056 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001057 default: return false;
Colin LeMahieu14455532015-01-06 16:15:15 +00001058 case Hexagon::L4_return:
1059 case Hexagon::L4_return_t:
1060 case Hexagon::L4_return_f:
1061 case Hexagon::L4_return_tnew_pnt:
1062 case Hexagon::L4_return_fnew_pnt:
1063 case Hexagon::L4_return_tnew_pt:
1064 case Hexagon::L4_return_fnew_pt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001065 return true;
1066 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001067}
1068
1069
1070bool HexagonInstrInfo::
1071isValidOffset(const int Opcode, const int Offset) const {
1072 // This function is to check whether the "Offset" is in the correct range of
1073 // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1074 // inserted to calculate the final address. Due to this reason, the function
1075 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00001076 // We used to assert if the offset was not properly aligned, however,
1077 // there are cases where a misaligned pointer recast can cause this
1078 // problem, and we need to allow for it. The front end warns of such
1079 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001080
1081 switch(Opcode) {
1082
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001083 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001084 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001085 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1086 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1087
Colin LeMahieu947cd702014-12-23 20:44:59 +00001088 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001089 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001090 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1091 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1092
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001093 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001094 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001095 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001096 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1097 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1098
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001099 case Hexagon::L2_loadrb_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001100 case Hexagon::S2_storerb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001101 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001102 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1103 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1104
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001105 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001106 case Hexagon::TFR_FI:
1107 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1108 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1109
Colin LeMahieudacf0572015-01-05 21:36:38 +00001110 case Hexagon::L4_iadd_memopw_io:
1111 case Hexagon::L4_isub_memopw_io:
1112 case Hexagon::L4_add_memopw_io:
1113 case Hexagon::L4_sub_memopw_io:
1114 case Hexagon::L4_and_memopw_io:
1115 case Hexagon::L4_or_memopw_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001116 return (0 <= Offset && Offset <= 255);
1117
Colin LeMahieudacf0572015-01-05 21:36:38 +00001118 case Hexagon::L4_iadd_memoph_io:
1119 case Hexagon::L4_isub_memoph_io:
1120 case Hexagon::L4_add_memoph_io:
1121 case Hexagon::L4_sub_memoph_io:
1122 case Hexagon::L4_and_memoph_io:
1123 case Hexagon::L4_or_memoph_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001124 return (0 <= Offset && Offset <= 127);
1125
Colin LeMahieudacf0572015-01-05 21:36:38 +00001126 case Hexagon::L4_iadd_memopb_io:
1127 case Hexagon::L4_isub_memopb_io:
1128 case Hexagon::L4_add_memopb_io:
1129 case Hexagon::L4_sub_memopb_io:
1130 case Hexagon::L4_and_memopb_io:
1131 case Hexagon::L4_or_memopb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001132 return (0 <= Offset && Offset <= 63);
1133
1134 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1135 // any size. Later pass knows how to handle it.
1136 case Hexagon::STriw_pred:
1137 case Hexagon::LDriw_pred:
1138 return true;
1139
Colin LeMahieu5ccbb122014-12-19 00:06:53 +00001140 case Hexagon::J2_loop0i:
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +00001141 return isUInt<10>(Offset);
1142
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001143 // INLINEASM is very special.
1144 case Hexagon::INLINEASM:
1145 return true;
1146 }
1147
Benjamin Kramerb6684012011-12-27 11:41:05 +00001148 llvm_unreachable("No offset range is defined for this opcode. "
1149 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001150}
1151
1152
1153//
1154// Check if the Offset is a valid auto-inc imm by Load/Store Type.
1155//
1156bool HexagonInstrInfo::
1157isValidAutoIncImm(const EVT VT, const int Offset) const {
1158
1159 if (VT == MVT::i64) {
1160 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1161 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1162 (Offset & 0x7) == 0);
1163 }
1164 if (VT == MVT::i32) {
1165 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1166 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1167 (Offset & 0x3) == 0);
1168 }
1169 if (VT == MVT::i16) {
1170 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1171 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1172 (Offset & 0x1) == 0);
1173 }
1174 if (VT == MVT::i8) {
1175 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1176 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1177 }
Craig Toppere55c5562012-02-07 02:50:20 +00001178 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001179}
1180
1181
1182bool HexagonInstrInfo::
1183isMemOp(const MachineInstr *MI) const {
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001184// return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1185
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001186 switch (MI->getOpcode())
1187 {
Colin LeMahieudacf0572015-01-05 21:36:38 +00001188 default: return false;
1189 case Hexagon::L4_iadd_memopw_io:
1190 case Hexagon::L4_isub_memopw_io:
1191 case Hexagon::L4_add_memopw_io:
1192 case Hexagon::L4_sub_memopw_io:
1193 case Hexagon::L4_and_memopw_io:
1194 case Hexagon::L4_or_memopw_io:
1195 case Hexagon::L4_iadd_memoph_io:
1196 case Hexagon::L4_isub_memoph_io:
1197 case Hexagon::L4_add_memoph_io:
1198 case Hexagon::L4_sub_memoph_io:
1199 case Hexagon::L4_and_memoph_io:
1200 case Hexagon::L4_or_memoph_io:
1201 case Hexagon::L4_iadd_memopb_io:
1202 case Hexagon::L4_isub_memopb_io:
1203 case Hexagon::L4_add_memopb_io:
1204 case Hexagon::L4_sub_memopb_io:
1205 case Hexagon::L4_and_memopb_io:
1206 case Hexagon::L4_or_memopb_io:
1207 case Hexagon::L4_ior_memopb_io:
1208 case Hexagon::L4_ior_memoph_io:
1209 case Hexagon::L4_ior_memopw_io:
1210 case Hexagon::L4_iand_memopb_io:
1211 case Hexagon::L4_iand_memoph_io:
1212 case Hexagon::L4_iand_memopw_io:
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001213 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001214 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001215 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001216}
1217
1218
1219bool HexagonInstrInfo::
1220isSpillPredRegOp(const MachineInstr *MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001221 switch (MI->getOpcode()) {
1222 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001223 case Hexagon::STriw_pred :
1224 case Hexagon::LDriw_pred :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001225 return true;
Sirish Pande2c7bf002012-04-23 17:49:28 +00001226 }
Sirish Pande4bd20c52012-05-12 05:10:30 +00001227}
1228
1229bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1230 switch (MI->getOpcode()) {
Sirish Pande8bb97452012-05-12 05:54:15 +00001231 default: return false;
Colin LeMahieu902157c2014-11-25 18:20:52 +00001232 case Hexagon::C2_cmpeq:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001233 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001234 case Hexagon::C2_cmpgt:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001235 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001236 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001237 case Hexagon::C2_cmpgtui:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001238 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +00001239 }
Sirish Pande2c7bf002012-04-23 17:49:28 +00001240}
1241
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001242bool HexagonInstrInfo::
1243isConditionalTransfer (const MachineInstr *MI) const {
1244 switch (MI->getOpcode()) {
1245 default: return false;
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001246 case Hexagon::A2_tfrt:
1247 case Hexagon::A2_tfrf:
1248 case Hexagon::C2_cmoveit:
1249 case Hexagon::C2_cmoveif:
1250 case Hexagon::A2_tfrtnew:
1251 case Hexagon::A2_tfrfnew:
1252 case Hexagon::C2_cmovenewit:
1253 case Hexagon::C2_cmovenewif:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001254 return true;
1255 }
1256}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001257
1258bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001259 switch (MI->getOpcode())
1260 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001261 default: return false;
Colin LeMahieuefa74e02014-11-18 20:28:11 +00001262 case Hexagon::A2_paddf:
1263 case Hexagon::A2_paddfnew:
1264 case Hexagon::A2_paddt:
1265 case Hexagon::A2_paddtnew:
Colin LeMahieu44fd1c82014-11-18 22:45:47 +00001266 case Hexagon::A2_pandf:
1267 case Hexagon::A2_pandfnew:
1268 case Hexagon::A2_pandt:
1269 case Hexagon::A2_pandtnew:
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001270 case Hexagon::A4_paslhf:
1271 case Hexagon::A4_paslhfnew:
1272 case Hexagon::A4_paslht:
1273 case Hexagon::A4_paslhtnew:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001274 case Hexagon::A4_pasrhf:
1275 case Hexagon::A4_pasrhfnew:
1276 case Hexagon::A4_pasrht:
1277 case Hexagon::A4_pasrhtnew:
Colin LeMahieu21866542014-11-19 22:58:04 +00001278 case Hexagon::A2_porf:
1279 case Hexagon::A2_porfnew:
1280 case Hexagon::A2_port:
1281 case Hexagon::A2_portnew:
Colin LeMahieue88447d2014-11-21 21:19:18 +00001282 case Hexagon::A2_psubf:
1283 case Hexagon::A2_psubfnew:
1284 case Hexagon::A2_psubt:
1285 case Hexagon::A2_psubtnew:
Colin LeMahieuac006432014-11-19 23:22:23 +00001286 case Hexagon::A2_pxorf:
1287 case Hexagon::A2_pxorfnew:
1288 case Hexagon::A2_pxort:
1289 case Hexagon::A2_pxortnew:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001290 case Hexagon::A4_psxthf:
1291 case Hexagon::A4_psxthfnew:
1292 case Hexagon::A4_psxtht:
1293 case Hexagon::A4_psxthtnew:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001294 case Hexagon::A4_psxtbf:
1295 case Hexagon::A4_psxtbfnew:
1296 case Hexagon::A4_psxtbt:
1297 case Hexagon::A4_psxtbtnew:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001298 case Hexagon::A4_pzxtbf:
1299 case Hexagon::A4_pzxtbfnew:
1300 case Hexagon::A4_pzxtbt:
1301 case Hexagon::A4_pzxtbtnew:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001302 case Hexagon::A4_pzxthf:
1303 case Hexagon::A4_pzxthfnew:
1304 case Hexagon::A4_pzxtht:
1305 case Hexagon::A4_pzxthtnew:
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001306 case Hexagon::A2_paddit:
1307 case Hexagon::A2_paddif:
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001308 case Hexagon::C2_ccombinewt:
1309 case Hexagon::C2_ccombinewf:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001310 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001311 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001312}
1313
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001314bool HexagonInstrInfo::
1315isConditionalLoad (const MachineInstr* MI) const {
1316 const HexagonRegisterInfo& QRI = getRegisterInfo();
1317 switch (MI->getOpcode())
1318 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001319 default: return false;
Colin LeMahieu947cd702014-12-23 20:44:59 +00001320 case Hexagon::L2_ploadrdt_io :
1321 case Hexagon::L2_ploadrdf_io:
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001322 case Hexagon::L2_ploadrit_io:
1323 case Hexagon::L2_ploadrif_io:
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001324 case Hexagon::L2_ploadrht_io:
1325 case Hexagon::L2_ploadrhf_io:
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001326 case Hexagon::L2_ploadrbt_io:
1327 case Hexagon::L2_ploadrbf_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001328 case Hexagon::L2_ploadruht_io:
1329 case Hexagon::L2_ploadruhf_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001330 case Hexagon::L2_ploadrubt_io:
1331 case Hexagon::L2_ploadrubf_io:
Colin LeMahieu9161d472014-12-30 18:58:47 +00001332 case Hexagon::L2_ploadrdt_pi:
1333 case Hexagon::L2_ploadrdf_pi:
1334 case Hexagon::L2_ploadrit_pi:
1335 case Hexagon::L2_ploadrif_pi:
1336 case Hexagon::L2_ploadrht_pi:
1337 case Hexagon::L2_ploadrhf_pi:
1338 case Hexagon::L2_ploadrbt_pi:
1339 case Hexagon::L2_ploadrbf_pi:
1340 case Hexagon::L2_ploadruht_pi:
1341 case Hexagon::L2_ploadruhf_pi:
1342 case Hexagon::L2_ploadrubt_pi:
1343 case Hexagon::L2_ploadrubf_pi:
Colin LeMahieu9161d472014-12-30 18:58:47 +00001344 case Hexagon::L4_ploadrdt_rr:
1345 case Hexagon::L4_ploadrdf_rr:
1346 case Hexagon::L4_ploadrbt_rr:
1347 case Hexagon::L4_ploadrbf_rr:
1348 case Hexagon::L4_ploadrubt_rr:
1349 case Hexagon::L4_ploadrubf_rr:
1350 case Hexagon::L4_ploadrht_rr:
1351 case Hexagon::L4_ploadrhf_rr:
1352 case Hexagon::L4_ploadruht_rr:
1353 case Hexagon::L4_ploadruhf_rr:
1354 case Hexagon::L4_ploadrit_rr:
1355 case Hexagon::L4_ploadrif_rr:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001356 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001357 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001358}
Andrew Trickd06df962012-02-01 22:13:57 +00001359
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001360// Returns true if an instruction is a conditional store.
1361//
1362// Note: It doesn't include conditional new-value stores as they can't be
1363// converted to .new predicate.
1364//
1365// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1366// ^ ^
1367// / \ (not OK. it will cause new-value store to be
1368// / X conditional on p0.new while R2 producer is
1369// / \ on p0)
1370// / \.
1371// p.new store p.old NV store
1372// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1373// ^ ^
1374// \ /
1375// \ /
1376// \ /
1377// p.old store
1378// [if (p0)memw(R0+#0)=R2]
1379//
1380// The above diagram shows the steps involoved in the conversion of a predicated
1381// store instruction to its .new predicated new-value form.
1382//
1383// The following set of instructions further explains the scenario where
1384// conditional new-value store becomes invalid when promoted to .new predicate
1385// form.
1386//
1387// { 1) if (p0) r0 = add(r1, r2)
1388// 2) p0 = cmp.eq(r3, #0) }
1389//
1390// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1391// the first two instructions because in instr 1, r0 is conditional on old value
1392// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1393// is not valid for new-value stores.
1394bool HexagonInstrInfo::
1395isConditionalStore (const MachineInstr* MI) const {
1396 const HexagonRegisterInfo& QRI = getRegisterInfo();
1397 switch (MI->getOpcode())
1398 {
1399 default: return false;
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001400 case Hexagon::S4_storeirbt_io:
1401 case Hexagon::S4_storeirbf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001402 case Hexagon::S4_pstorerbt_rr:
1403 case Hexagon::S4_pstorerbf_rr:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001404 case Hexagon::S2_pstorerbt_io:
1405 case Hexagon::S2_pstorerbf_io:
Colin LeMahieu3d34afb2014-12-29 19:42:14 +00001406 case Hexagon::S2_pstorerbt_pi:
1407 case Hexagon::S2_pstorerbf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001408 case Hexagon::S2_pstorerdt_io:
1409 case Hexagon::S2_pstorerdf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001410 case Hexagon::S4_pstorerdt_rr:
1411 case Hexagon::S4_pstorerdf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001412 case Hexagon::S2_pstorerdt_pi:
1413 case Hexagon::S2_pstorerdf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001414 case Hexagon::S2_pstorerht_io:
1415 case Hexagon::S2_pstorerhf_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001416 case Hexagon::S4_storeirht_io:
1417 case Hexagon::S4_storeirhf_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001418 case Hexagon::S4_pstorerht_rr:
1419 case Hexagon::S4_pstorerhf_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001420 case Hexagon::S2_pstorerht_pi:
1421 case Hexagon::S2_pstorerhf_pi:
Colin LeMahieubda31b42014-12-29 20:44:51 +00001422 case Hexagon::S2_pstorerit_io:
1423 case Hexagon::S2_pstorerif_io:
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001424 case Hexagon::S4_storeirit_io:
1425 case Hexagon::S4_storeirif_io:
Colin LeMahieu94a498b2014-12-30 20:42:23 +00001426 case Hexagon::S4_pstorerit_rr:
1427 case Hexagon::S4_pstorerif_rr:
Colin LeMahieu9a3cd3f2014-12-29 20:00:43 +00001428 case Hexagon::S2_pstorerit_pi:
1429 case Hexagon::S2_pstorerif_pi:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001430
1431 // V4 global address store before promoting to dot new.
Colin LeMahieu14455532015-01-06 16:15:15 +00001432 case Hexagon::S4_pstorerdt_abs:
1433 case Hexagon::S4_pstorerdf_abs:
1434 case Hexagon::S4_pstorerbt_abs:
1435 case Hexagon::S4_pstorerbf_abs:
1436 case Hexagon::S4_pstorerht_abs:
1437 case Hexagon::S4_pstorerhf_abs:
1438 case Hexagon::S4_pstorerit_abs:
1439 case Hexagon::S4_pstorerif_abs:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001440 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001441
1442 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1443 // from the "Conditional Store" list. Because a predicated new value store
1444 // would NOT be promoted to a double dot new store. See diagram below:
1445 // This function returns yes for those stores that are predicated but not
1446 // yet promoted to predicate dot new instructions.
1447 //
1448 // +---------------------+
1449 // /-----| if (p0) memw(..)=r0 |---------\~
1450 // || +---------------------+ ||
1451 // promote || /\ /\ || promote
1452 // || /||\ /||\ ||
1453 // \||/ demote || \||/
1454 // \/ || || \/
1455 // +-------------------------+ || +-------------------------+
1456 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1457 // +-------------------------+ || +-------------------------+
1458 // || || ||
1459 // || demote \||/
1460 // promote || \/ NOT possible
1461 // || || /\~
1462 // \||/ || /||\~
1463 // \/ || ||
1464 // +-----------------------------+
1465 // | if (p0.new) memw(..)=r0.new |
1466 // +-----------------------------+
1467 // Double Dot New Store
1468 //
1469 }
1470}
1471
Jyotsna Verma84c47102013-05-06 18:49:23 +00001472
1473bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1474 if (isNewValue(MI) && isBranch(MI))
1475 return true;
1476 return false;
1477}
1478
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001479bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1480 return (getAddrMode(MI) == HexagonII::PostInc);
1481}
1482
Jyotsna Verma84c47102013-05-06 18:49:23 +00001483bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1484 const uint64_t F = MI->getDesc().TSFlags;
1485 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1486}
1487
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001488// Returns true, if any one of the operands is a dot new
1489// insn, whether it is predicated dot new or register dot new.
1490bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1491 return (isNewValueInst(MI) ||
1492 (isPredicated(MI) && isPredicatedNew(MI)));
1493}
1494
Jyotsna Verma438cec52013-05-10 20:58:11 +00001495// Returns the most basic instruction for the .new predicated instructions and
1496// new-value stores.
1497// For example, all of the following instructions will be converted back to the
1498// same instruction:
1499// 1) if (p0.new) memw(R0+#0) = R1.new --->
1500// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1501// 3) if (p0.new) memw(R0+#0) = R1 --->
1502//
1503
1504int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1505 int NewOp = opc;
1506 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1507 NewOp = Hexagon::getPredOldOpcode(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001508 assert(NewOp >= 0 &&
1509 "Couldn't change predicate new instruction to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001510 }
1511
Alp Tokerf907b892013-12-05 05:44:44 +00001512 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
Jyotsna Verma438cec52013-05-10 20:58:11 +00001513 NewOp = Hexagon::getNonNVStore(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001514 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001515 }
1516 return NewOp;
1517}
1518
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001519// Return the new value instruction for a given store.
1520int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1521 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1522 if (NVOpcode >= 0) // Valid new-value store instruction.
1523 return NVOpcode;
1524
1525 switch (MI->getOpcode()) {
1526 default: llvm_unreachable("Unknown .new type");
1527 // store new value byte
Colin LeMahieuc0434462015-02-04 17:52:06 +00001528 case Hexagon::S4_storerb_ur:
1529 return Hexagon::S4_storerbnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001530
Colin LeMahieuc0434462015-02-04 17:52:06 +00001531 case Hexagon::S4_storerh_ur:
1532 return Hexagon::S4_storerhnew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001533
Colin LeMahieuc0434462015-02-04 17:52:06 +00001534 case Hexagon::S4_storeri_ur:
1535 return Hexagon::S4_storerinew_ur;
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001536
1537 }
1538 return 0;
1539}
1540
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001541// Return .new predicate version for an instruction.
1542int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1543 const MachineBranchProbabilityInfo
1544 *MBPI) const {
1545
1546 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1547 if (NewOpcode >= 0) // Valid predicate new instruction
1548 return NewOpcode;
1549
1550 switch (MI->getOpcode()) {
1551 default: llvm_unreachable("Unknown .new type");
1552 // Condtional Jumps
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001553 case Hexagon::J2_jumpt:
1554 case Hexagon::J2_jumpf:
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001555 return getDotNewPredJumpOp(MI, MBPI);
1556
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001557 case Hexagon::J2_jumprt:
1558 return Hexagon::J2_jumptnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001559
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001560 case Hexagon::J2_jumprf:
1561 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001562
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001563 case Hexagon::JMPrett:
1564 return Hexagon::J2_jumprtnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001565
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001566 case Hexagon::JMPretf:
1567 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001568
1569
1570 // Conditional combine
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001571 case Hexagon::C2_ccombinewt:
1572 return Hexagon::C2_ccombinewnewt;
1573 case Hexagon::C2_ccombinewf:
1574 return Hexagon::C2_ccombinewnewf;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001575 }
1576}
1577
1578
Jyotsna Verma84256432013-03-01 17:37:13 +00001579unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1580 const uint64_t F = MI->getDesc().TSFlags;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001581
Jyotsna Verma84256432013-03-01 17:37:13 +00001582 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1583}
1584
1585/// immediateExtend - Changes the instruction in place to one using an immediate
1586/// extender.
1587void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1588 assert((isExtendable(MI)||isConstExtended(MI)) &&
1589 "Instruction must be extendable");
1590 // Find which operand is extendable.
1591 short ExtOpNum = getCExtOpNum(MI);
1592 MachineOperand &MO = MI->getOperand(ExtOpNum);
1593 // This needs to be something we understand.
1594 assert((MO.isMBB() || MO.isImm()) &&
1595 "Branch with unknown extendable field type");
1596 // Mark given operand as extended.
1597 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1598}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001599
Eric Christopher143f02c2014-10-09 01:59:35 +00001600DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1601 const TargetSubtargetInfo &STI) const {
1602 const InstrItineraryData *II = STI.getInstrItineraryData();
1603 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
Andrew Trickd06df962012-02-01 22:13:57 +00001604}
1605
1606bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1607 const MachineBasicBlock *MBB,
1608 const MachineFunction &MF) const {
1609 // Debug info is never a scheduling boundary. It's necessary to be explicit
1610 // due to the special treatment of IT instructions below, otherwise a
1611 // dbg_value followed by an IT will result in the IT instruction being
1612 // considered a scheduling hazard, which is wrong. It should be the actual
1613 // instruction preceding the dbg_value instruction(s), just like it is
1614 // when debug info is not present.
1615 if (MI->isDebugValue())
1616 return false;
1617
1618 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001619 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
Andrew Trickd06df962012-02-01 22:13:57 +00001620 return true;
1621
1622 return false;
1623}
Jyotsna Verma84256432013-03-01 17:37:13 +00001624
1625bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001626 const uint64_t F = MI->getDesc().TSFlags;
1627 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1628 if (isExtended) // Instruction must be extended.
1629 return true;
1630
1631 unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1632 & HexagonII::ExtendableMask;
1633 if (!isExtendable)
1634 return false;
1635
1636 short ExtOpNum = getCExtOpNum(MI);
1637 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1638 // Use MO operand flags to determine if MO
1639 // has the HMOTF_ConstExtended flag set.
1640 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1641 return true;
1642 // If this is a Machine BB address we are talking about, and it is
1643 // not marked as extended, say so.
1644 if (MO.isMBB())
1645 return false;
1646
1647 // We could be using an instruction with an extendable immediate and shoehorn
1648 // a global address into it. If it is a global address it will be constant
1649 // extended. We do this for COMBINE.
1650 // We currently only handle isGlobal() because it is the only kind of
1651 // object we are going to end up with here for now.
1652 // In the future we probably should add isSymbol(), etc.
1653 if (MO.isGlobal() || MO.isSymbol())
1654 return true;
1655
1656 // If the extendable operand is not 'Immediate' type, the instruction should
1657 // have 'isExtended' flag set.
1658 assert(MO.isImm() && "Extendable operand must be Immediate type");
1659
1660 int MinValue = getMinValue(MI);
1661 int MaxValue = getMaxValue(MI);
1662 int ImmValue = MO.getImm();
1663
1664 return (ImmValue < MinValue || ImmValue > MaxValue);
1665}
1666
Jyotsna Verma1d297502013-05-02 15:39:30 +00001667// Returns the opcode to use when converting MI, which is a conditional jump,
1668// into a conditional instruction which uses the .new value of the predicate.
1669// We also use branch probabilities to add a hint to the jump.
1670int
1671HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1672 const
1673 MachineBranchProbabilityInfo *MBPI) const {
1674
1675 // We assume that block can have at most two successors.
1676 bool taken = false;
1677 MachineBasicBlock *Src = MI->getParent();
1678 MachineOperand *BrTarget = &MI->getOperand(1);
1679 MachineBasicBlock *Dst = BrTarget->getMBB();
1680
1681 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1682 if (Prediction >= BranchProbability(1,2))
1683 taken = true;
1684
1685 switch (MI->getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001686 case Hexagon::J2_jumpt:
1687 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1688 case Hexagon::J2_jumpf:
1689 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Jyotsna Verma1d297502013-05-02 15:39:30 +00001690
1691 default:
1692 llvm_unreachable("Unexpected jump instruction.");
1693 }
1694}
Jyotsna Verma84256432013-03-01 17:37:13 +00001695// Returns true if a particular operand is extendable for an instruction.
1696bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1697 unsigned short OperandNum) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00001698 const uint64_t F = MI->getDesc().TSFlags;
1699
1700 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1701 == OperandNum;
1702}
1703
1704// Returns Operand Index for the constant extended instruction.
1705unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1706 const uint64_t F = MI->getDesc().TSFlags;
1707 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1708}
1709
1710// Returns the min value that doesn't need to be extended.
1711int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1712 const uint64_t F = MI->getDesc().TSFlags;
1713 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1714 & HexagonII::ExtentSignedMask;
1715 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1716 & HexagonII::ExtentBitsMask;
1717
1718 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001719 return -1U << (bits - 1);
Jyotsna Verma84256432013-03-01 17:37:13 +00001720 else
1721 return 0;
1722}
1723
1724// Returns the max value that doesn't need to be extended.
1725int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1726 const uint64_t F = MI->getDesc().TSFlags;
1727 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1728 & HexagonII::ExtentSignedMask;
1729 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1730 & HexagonII::ExtentBitsMask;
1731
1732 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001733 return ~(-1U << (bits - 1));
Jyotsna Verma84256432013-03-01 17:37:13 +00001734 else
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001735 return ~(-1U << bits);
Jyotsna Verma84256432013-03-01 17:37:13 +00001736}
1737
1738// Returns true if an instruction can be converted into a non-extended
1739// equivalent instruction.
1740bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1741
1742 short NonExtOpcode;
1743 // Check if the instruction has a register form that uses register in place
1744 // of the extended operand, if so return that as the non-extended form.
1745 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1746 return true;
1747
1748 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001749 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001750
1751 switch (getAddrMode(MI)) {
1752 case HexagonII::Absolute :
1753 // Load/store with absolute addressing mode can be converted into
1754 // base+offset mode.
1755 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1756 break;
1757 case HexagonII::BaseImmOffset :
1758 // Load/store with base+offset addressing mode can be converted into
1759 // base+register offset addressing mode. However left shift operand should
1760 // be set to 0.
1761 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1762 break;
1763 default:
1764 return false;
1765 }
1766 if (NonExtOpcode < 0)
1767 return false;
1768 return true;
1769 }
1770 return false;
1771}
1772
1773// Returns opcode of the non-extended equivalent instruction.
1774short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1775
1776 // Check if the instruction has a register form that uses register in place
1777 // of the extended operand, if so return that as the non-extended form.
1778 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1779 if (NonExtOpcode >= 0)
1780 return NonExtOpcode;
1781
1782 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001783 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001784 switch (getAddrMode(MI)) {
1785 case HexagonII::Absolute :
1786 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1787 case HexagonII::BaseImmOffset :
1788 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1789 default:
1790 return -1;
1791 }
1792 }
1793 return -1;
1794}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001795
1796bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001797 return (Opcode == Hexagon::J2_jumpt) ||
1798 (Opcode == Hexagon::J2_jumpf) ||
1799 (Opcode == Hexagon::J2_jumptnewpt) ||
1800 (Opcode == Hexagon::J2_jumpfnewpt) ||
1801 (Opcode == Hexagon::J2_jumpt) ||
1802 (Opcode == Hexagon::J2_jumpf);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001803}
1804
1805bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001806 return (Opcode == Hexagon::J2_jumpf) ||
1807 (Opcode == Hexagon::J2_jumpfnewpt) ||
1808 (Opcode == Hexagon::J2_jumpfnew);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001809}