blob: d78315924a5a74b1c435f6f370216f7bf9ab2213 [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;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000081 case Hexagon::LDriw:
82 case Hexagon::LDrid:
83 case Hexagon::LDrih:
84 case Hexagon::LDrib:
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;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106 case Hexagon::STriw:
107 case Hexagon::STrid:
108 case Hexagon::STrih:
109 case Hexagon::STrib:
110 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;
361 case Hexagon::CMPbEQri_V4:
362 case Hexagon::CMPbEQrr_sbsb_V4:
363 case Hexagon::CMPbEQrr_ubub_V4:
364 case Hexagon::CMPbGTUri_V4:
365 case Hexagon::CMPbGTUrr_V4:
366 case Hexagon::CMPbGTrr_V4:
367 SrcReg = MI->getOperand(1).getReg();
368 Mask = 0xFF;
369 break;
370 case Hexagon::CMPhEQri_V4:
371 case Hexagon::CMPhEQrr_shl_V4:
372 case Hexagon::CMPhEQrr_xor_V4:
373 case Hexagon::CMPhGTUri_V4:
374 case Hexagon::CMPhGTUrr_V4:
375 case Hexagon::CMPhGTrr_shl_V4:
376 SrcReg = MI->getOperand(1).getReg();
377 Mask = 0xFFFF;
378 break;
379 }
380
381 // Set the value/second source register.
382 switch (Opc) {
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000383 case Hexagon::C2_cmpeqp:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000384 case Hexagon::C2_cmpeq:
Colin LeMahieu9bfe5472014-12-08 21:56:47 +0000385 case Hexagon::C2_cmpgtp:
386 case Hexagon::C2_cmpgtup:
Colin LeMahieu902157c2014-11-25 18:20:52 +0000387 case Hexagon::C2_cmpgtu:
388 case Hexagon::C2_cmpgt:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000389 case Hexagon::CMPbEQrr_sbsb_V4:
390 case Hexagon::CMPbEQrr_ubub_V4:
391 case Hexagon::CMPbGTUrr_V4:
392 case Hexagon::CMPbGTrr_V4:
393 case Hexagon::CMPhEQrr_shl_V4:
394 case Hexagon::CMPhEQrr_xor_V4:
395 case Hexagon::CMPhGTUrr_V4:
396 case Hexagon::CMPhGTrr_shl_V4:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000397 SrcReg2 = MI->getOperand(2).getReg();
398 return true;
399
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +0000400 case Hexagon::C2_cmpeqi:
401 case Hexagon::C2_cmpgtui:
402 case Hexagon::C2_cmpgti:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000403 case Hexagon::CMPbEQri_V4:
404 case Hexagon::CMPbGTUri_V4:
405 case Hexagon::CMPhEQri_V4:
406 case Hexagon::CMPhGTUri_V4:
407 SrcReg2 = 0;
408 Value = MI->getOperand(2).getImm();
409 return true;
410 }
411
412 return false;
413}
414
415
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000416void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
417 MachineBasicBlock::iterator I, DebugLoc DL,
418 unsigned DestReg, unsigned SrcReg,
419 bool KillSrc) const {
420 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000421 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000422 return;
423 }
424 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000425 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000426 return;
427 }
428 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
429 // Map Pd = Ps to Pd = or(Ps, Ps).
Colin LeMahieu5cf56322014-12-08 23:55:43 +0000430 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000431 DestReg).addReg(SrcReg).addReg(SrcReg);
432 return;
433 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000434 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
435 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000436 // We can have an overlap between single and double reg: r1:0 = r0.
437 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
438 // r1:0 = r0
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000439 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000440 Hexagon::subreg_hireg))).addImm(0);
441 } else {
442 // r1:0 = r1 or no overlap.
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000443 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000444 Hexagon::subreg_loreg))).addReg(SrcReg);
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000445 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000446 Hexagon::subreg_hireg))).addImm(0);
447 }
448 return;
449 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000450 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000451 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Colin LeMahieu0f850bd2014-12-19 20:29:29 +0000452 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000453 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000454 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000455 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
456 Hexagon::IntRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000457 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000458 addReg(SrcReg, getKillRegState(KillSrc));
459 return;
460 }
461 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
462 Hexagon::PredRegsRegClass.contains(DestReg)) {
Colin LeMahieu30dcb232014-12-09 18:16:49 +0000463 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000464 addReg(SrcReg, getKillRegState(KillSrc));
465 return;
466 }
Sirish Pande30804c22012-02-15 18:52:27 +0000467
468 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000469}
470
471
472void HexagonInstrInfo::
473storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
474 unsigned SrcReg, bool isKill, int FI,
475 const TargetRegisterClass *RC,
476 const TargetRegisterInfo *TRI) const {
477
478 DebugLoc DL = MBB.findDebugLoc(I);
479 MachineFunction &MF = *MBB.getParent();
480 MachineFrameInfo &MFI = *MF.getFrameInfo();
481 unsigned Align = MFI.getObjectAlignment(FI);
482
483 MachineMemOperand *MMO =
484 MF.getMachineMemOperand(
485 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
486 MachineMemOperand::MOStore,
487 MFI.getObjectSize(FI),
488 Align);
489
Craig Topperc7242e02012-04-20 07:30:17 +0000490 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000491 BuildMI(MBB, I, DL, get(Hexagon::STriw))
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::DoubleRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 BuildMI(MBB, I, DL, get(Hexagon::STrid))
496 .addFrameIndex(FI).addImm(0)
497 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000498 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000499 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
500 .addFrameIndex(FI).addImm(0)
501 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
502 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000503 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000504 }
505}
506
507
508void HexagonInstrInfo::storeRegToAddr(
509 MachineFunction &MF, unsigned SrcReg,
510 bool isKill,
511 SmallVectorImpl<MachineOperand> &Addr,
512 const TargetRegisterClass *RC,
513 SmallVectorImpl<MachineInstr*> &NewMIs) const
514{
Craig Toppere55c5562012-02-07 02:50:20 +0000515 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000516}
517
518
519void HexagonInstrInfo::
520loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
521 unsigned DestReg, int FI,
522 const TargetRegisterClass *RC,
523 const TargetRegisterInfo *TRI) const {
524 DebugLoc DL = MBB.findDebugLoc(I);
525 MachineFunction &MF = *MBB.getParent();
526 MachineFrameInfo &MFI = *MF.getFrameInfo();
527 unsigned Align = MFI.getObjectAlignment(FI);
528
529 MachineMemOperand *MMO =
530 MF.getMachineMemOperand(
531 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
532 MachineMemOperand::MOLoad,
533 MFI.getObjectSize(FI),
534 Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000535 if (RC == &Hexagon::IntRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000536 BuildMI(MBB, I, DL, get(Hexagon::LDriw), DestReg)
537 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000538 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000539 BuildMI(MBB, I, DL, get(Hexagon::LDrid), DestReg)
540 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000541 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000542 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
543 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
544 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000545 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000546 }
547}
548
549
550void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
551 SmallVectorImpl<MachineOperand> &Addr,
552 const TargetRegisterClass *RC,
553 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Craig Toppere55c5562012-02-07 02:50:20 +0000554 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000555}
556
557
558MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
559 MachineInstr* MI,
560 const SmallVectorImpl<unsigned> &Ops,
561 int FI) const {
562 // Hexagon_TODO: Implement.
Craig Topper062a2ba2014-04-25 05:30:21 +0000563 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000564}
565
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000566unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
567
568 MachineRegisterInfo &RegInfo = MF->getRegInfo();
569 const TargetRegisterClass *TRC;
Sirish Pande69295b82012-05-10 20:20:25 +0000570 if (VT == MVT::i1) {
Craig Topperc7242e02012-04-20 07:30:17 +0000571 TRC = &Hexagon::PredRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000572 } else if (VT == MVT::i32 || VT == MVT::f32) {
Craig Topperc7242e02012-04-20 07:30:17 +0000573 TRC = &Hexagon::IntRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000574 } else if (VT == MVT::i64 || VT == MVT::f64) {
Craig Topperc7242e02012-04-20 07:30:17 +0000575 TRC = &Hexagon::DoubleRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000576 } else {
Benjamin Kramerb6684012011-12-27 11:41:05 +0000577 llvm_unreachable("Cannot handle this register class");
Sirish Pande69295b82012-05-10 20:20:25 +0000578 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000579
580 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
581 return NewReg;
582}
583
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000584bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000585 // Constant extenders are allowed only for V4 and above.
586 if (!Subtarget.hasV4TOps())
587 return false;
588
589 const MCInstrDesc &MID = MI->getDesc();
590 const uint64_t F = MID.TSFlags;
591 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
592 return true;
593
594 // TODO: This is largely obsolete now. Will need to be removed
595 // in consecutive patches.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000596 switch(MI->getOpcode()) {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000597 // TFR_FI Remains a special case.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000598 case Hexagon::TFR_FI:
599 return true;
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000600 default:
601 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000602 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000603 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000604}
605
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000606// This returns true in two cases:
607// - The OP code itself indicates that this is an extended instruction.
608// - One of MOs has been marked with HMOTF_ConstExtended flag.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000609bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000610 // First check if this is permanently extended op code.
611 const uint64_t F = MI->getDesc().TSFlags;
612 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
613 return true;
614 // Use MO operand flags to determine if one of MI's operands
615 // has HMOTF_ConstExtended flag set.
616 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
617 E = MI->operands_end(); I != E; ++I) {
618 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Sirish Pande69295b82012-05-10 20:20:25 +0000619 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000620 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000621 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000622}
623
Jyotsna Verma84c47102013-05-06 18:49:23 +0000624bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
625 return MI->getDesc().isBranch();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000626}
627
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000628bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
629 if (isNewValueJump(MI))
630 return true;
631
632 if (isNewValueStore(MI))
633 return true;
634
635 return false;
636}
637
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000638bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
639 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
640}
Andrew Trickd06df962012-02-01 22:13:57 +0000641
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000642bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
643 bool isPred = MI->getDesc().isPredicable();
644
645 if (!isPred)
646 return false;
647
648 const int Opc = MI->getOpcode();
649
650 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +0000651 case Hexagon::A2_tfrsi:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000652 return isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000653
654 case Hexagon::STrid:
655 case Hexagon::STrid_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000656 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000657
658 case Hexagon::STriw:
659 case Hexagon::STriw_indexed:
660 case Hexagon::STriw_nv_V4:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000661 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000662
663 case Hexagon::STrih:
664 case Hexagon::STrih_indexed:
665 case Hexagon::STrih_nv_V4:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000666 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000667
668 case Hexagon::STrib:
669 case Hexagon::STrib_indexed:
670 case Hexagon::STrib_nv_V4:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000671 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000672
673 case Hexagon::LDrid:
674 case Hexagon::LDrid_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000675 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000676
677 case Hexagon::LDriw:
678 case Hexagon::LDriw_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000679 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000680
681 case Hexagon::LDrih:
682 case Hexagon::LDriuh:
683 case Hexagon::LDrih_indexed:
684 case Hexagon::LDriuh_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000685 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000686
687 case Hexagon::LDrib:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +0000688 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000689 case Hexagon::LDrib_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000690 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000691
692 case Hexagon::POST_LDrid:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000693 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000694
695 case Hexagon::POST_LDriw:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000696 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000697
698 case Hexagon::POST_LDrih:
699 case Hexagon::POST_LDriuh:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000700 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000701
702 case Hexagon::POST_LDrib:
703 case Hexagon::POST_LDriub:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000704 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000705
706 case Hexagon::STrib_imm_V4:
707 case Hexagon::STrih_imm_V4:
708 case Hexagon::STriw_imm_V4:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000709 return (isUInt<6>(MI->getOperand(1).getImm()) &&
710 isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000711
712 case Hexagon::ADD_ri:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000713 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000714
Colin LeMahieu3b3197e2014-11-24 17:44:19 +0000715 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +0000716 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +0000717 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +0000718 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +0000719 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +0000720 case Hexagon::A2_zxth:
Sirish Pande8bb97452012-05-12 05:54:15 +0000721 return Subtarget.hasV4TOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000722 }
723
724 return true;
725}
726
Sirish Pande8bb97452012-05-12 05:54:15 +0000727// This function performs the following inversiones:
728//
729// cPt ---> cNotPt
730// cNotPt ---> cPt
731//
Sirish Pande30804c22012-02-15 18:52:27 +0000732unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
Jyotsna Verma84c47102013-05-06 18:49:23 +0000733 int InvPredOpcode;
734 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
735 : Hexagon::getTruePredOpcode(Opc);
736 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
737 return InvPredOpcode;
738
Sirish Pande30804c22012-02-15 18:52:27 +0000739 switch(Opc) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000740 default: llvm_unreachable("Unexpected predicated instruction");
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000741 case Hexagon::C2_ccombinewt:
742 return Hexagon::C2_ccombinewf;
743 case Hexagon::C2_ccombinewf:
744 return Hexagon::C2_ccombinewt;
Sirish Pande30804c22012-02-15 18:52:27 +0000745
Jyotsna Verma978e9722013-05-09 18:25:44 +0000746 // Dealloc_return.
Sirish Pande30804c22012-02-15 18:52:27 +0000747 case Hexagon::DEALLOC_RET_cPt_V4:
748 return Hexagon::DEALLOC_RET_cNotPt_V4;
749 case Hexagon::DEALLOC_RET_cNotPt_V4:
750 return Hexagon::DEALLOC_RET_cPt_V4;
Sirish Pande30804c22012-02-15 18:52:27 +0000751 }
752}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000753
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000754// New Value Store instructions.
755bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
756 const uint64_t F = MI->getDesc().TSFlags;
757
758 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
759}
760
761bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
762 const uint64_t F = get(Opcode).TSFlags;
763
764 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
765}
Andrew Trickd06df962012-02-01 22:13:57 +0000766
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000767int HexagonInstrInfo::
768getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
Pranav Bhandarkar34b60182012-11-01 19:13:23 +0000769 enum Hexagon::PredSense inPredSense;
770 inPredSense = invertPredicate ? Hexagon::PredSense_false :
771 Hexagon::PredSense_true;
772 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
773 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
774 return CondOpcode;
775
776 // This switch case will be removed once all the instructions have been
777 // modified to use relation maps.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000778 switch(Opc) {
Sirish Pande69295b82012-05-10 20:20:25 +0000779 case Hexagon::TFRI_f:
780 return !invertPredicate ? Hexagon::TFRI_cPt_f :
781 Hexagon::TFRI_cNotPt_f;
Colin LeMahieub580d7d2014-12-09 19:23:45 +0000782 case Hexagon::A2_combinew:
783 return !invertPredicate ? Hexagon::C2_ccombinewt :
784 Hexagon::C2_ccombinewf;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000785
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000786 // Word.
Jyotsna Verma978e9722013-05-09 18:25:44 +0000787 case Hexagon::STriw_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788 return !invertPredicate ? Hexagon::STriw_cPt :
789 Hexagon::STriw_cNotPt;
Jyotsna Verma978e9722013-05-09 18:25:44 +0000790 case Hexagon::STriw_indexed_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000791 return !invertPredicate ? Hexagon::STriw_indexed_cPt :
792 Hexagon::STriw_indexed_cNotPt;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000793
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000794 // DEALLOC_RETURN.
795 case Hexagon::DEALLOC_RET_V4:
796 return !invertPredicate ? Hexagon::DEALLOC_RET_cPt_V4 :
797 Hexagon::DEALLOC_RET_cNotPt_V4;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000798 }
Benjamin Kramerb6684012011-12-27 11:41:05 +0000799 llvm_unreachable("Unexpected predicable instruction");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000800}
801
802
803bool HexagonInstrInfo::
804PredicateInstruction(MachineInstr *MI,
805 const SmallVectorImpl<MachineOperand> &Cond) const {
806 int Opc = MI->getOpcode();
807 assert (isPredicable(MI) && "Expected predicable instruction");
808 bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
809 (Cond[0].getImm() == 0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000810
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000811 // This will change MI's opcode to its predicate version.
812 // However, its operand list is still the old one, i.e. the
813 // non-predicate one.
814 MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
815
816 int oper = -1;
817 unsigned int GAIdx = 0;
818
819 // Indicates whether the current MI has a GlobalAddress operand
820 bool hasGAOpnd = false;
821 std::vector<MachineOperand> tmpOpnds;
822
823 // Indicates whether we need to shift operands to right.
824 bool needShift = true;
825
826 // The predicate is ALWAYS the FIRST input operand !!!
827 if (MI->getNumOperands() == 0) {
828 // The non-predicate version of MI does not take any operands,
829 // i.e. no outs and no ins. In this condition, the predicate
830 // operand will be directly placed at Operands[0]. No operand
831 // shift is needed.
832 // Example: BARRIER
833 needShift = false;
834 oper = -1;
835 }
836 else if ( MI->getOperand(MI->getNumOperands()-1).isReg()
837 && MI->getOperand(MI->getNumOperands()-1).isDef()
838 && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
839 // The non-predicate version of MI does not have any input operands.
840 // In this condition, we extend the length of Operands[] by one and
841 // copy the original last operand to the newly allocated slot.
842 // At this moment, it is just a place holder. Later, we will put
843 // predicate operand directly into it. No operand shift is needed.
844 // Example: r0=BARRIER (this is a faked insn used here for illustration)
845 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
846 needShift = false;
847 oper = MI->getNumOperands() - 2;
848 }
849 else {
850 // We need to right shift all input operands by one. Duplicate the
851 // last operand into the newly allocated slot.
852 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
853 }
854
855 if (needShift)
856 {
857 // Operands[ MI->getNumOperands() - 2 ] has been copied into
858 // Operands[ MI->getNumOperands() - 1 ], so we start from
859 // Operands[ MI->getNumOperands() - 3 ].
860 // oper is a signed int.
861 // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
862 for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
863 {
864 MachineOperand &MO = MI->getOperand(oper);
865
866 // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4] Opnd[5] Opnd[6] Opnd[7]
867 // <Def0> <Def1> <Use0> <Use1> <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
868 // /\~
869 // /||\~
870 // ||
871 // Predicate Operand here
872 if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
873 break;
874 }
875 if (MO.isReg()) {
876 MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
877 MO.isImplicit(), MO.isKill(),
878 MO.isDead(), MO.isUndef(),
879 MO.isDebug());
880 }
881 else if (MO.isImm()) {
882 MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
883 }
884 else if (MO.isGlobal()) {
885 // MI can not have more than one GlobalAddress operand.
886 assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
887
888 // There is no member function called "ChangeToGlobalAddress" in the
889 // MachineOperand class (not like "ChangeToRegister" and
890 // "ChangeToImmediate"). So we have to remove them from Operands[] list
891 // first, and then add them back after we have inserted the predicate
892 // operand. tmpOpnds[] is to remember these operands before we remove
893 // them.
894 tmpOpnds.push_back(MO);
895
896 // Operands[oper] is a GlobalAddress operand;
897 // Operands[oper+1] has been copied into Operands[oper+2];
898 hasGAOpnd = true;
899 GAIdx = oper;
900 continue;
901 }
902 else {
903 assert(false && "Unexpected operand type");
904 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000905 }
906 }
907
908 int regPos = invertJump ? 1 : 0;
909 MachineOperand PredMO = Cond[regPos];
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000910
911 // [oper] now points to the last explicit Def. Predicate operand must be
912 // located at [oper+1]. See diagram above.
913 // This assumes that the predicate is always the first operand,
914 // i.e. Operands[0+numResults], in the set of inputs
915 // It is better to have an assert here to check this. But I don't know how
916 // to write this assert because findFirstPredOperandIdx() would return -1
917 if (oper < -1) oper = -1;
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000918
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000919 MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000920 PredMO.isImplicit(), false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000921 PredMO.isDead(), PredMO.isUndef(),
922 PredMO.isDebug());
923
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +0000924 MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
925 RegInfo.clearKillFlags(PredMO.getReg());
926
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000927 if (hasGAOpnd)
928 {
929 unsigned int i;
930
931 // Operands[GAIdx] is the original GlobalAddress operand, which is
932 // already copied into tmpOpnds[0].
933 // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
934 // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
935 // so we start from [GAIdx+2]
936 for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
937 tmpOpnds.push_back(MI->getOperand(i));
938
939 // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
940 // It is very important that we always remove from the end of Operands[]
941 // MI->getNumOperands() is at least 2 if program goes to here.
942 for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
943 MI->RemoveOperand(i);
944
945 for (i = 0; i < tmpOpnds.size(); ++i)
946 MI->addOperand(tmpOpnds[i]);
947 }
948
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000949 return true;
950}
951
952
953bool
954HexagonInstrInfo::
955isProfitableToIfCvt(MachineBasicBlock &MBB,
Kay Tiong Khoof2949212012-06-13 15:53:04 +0000956 unsigned NumCycles,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000957 unsigned ExtraPredCycles,
958 const BranchProbability &Probability) const {
959 return true;
960}
961
962
963bool
964HexagonInstrInfo::
965isProfitableToIfCvt(MachineBasicBlock &TMBB,
966 unsigned NumTCycles,
967 unsigned ExtraTCycles,
968 MachineBasicBlock &FMBB,
969 unsigned NumFCycles,
970 unsigned ExtraFCycles,
971 const BranchProbability &Probability) const {
972 return true;
973}
974
Jyotsna Verma84c47102013-05-06 18:49:23 +0000975// Returns true if an instruction is predicated irrespective of the predicate
976// sense. For example, all of the following will return true.
977// if (p0) R1 = add(R2, R3)
978// if (!p0) R1 = add(R2, R3)
979// if (p0.new) R1 = add(R2, R3)
980// if (!p0.new) R1 = add(R2, R3)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000981bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoon6f358372012-02-08 18:25:47 +0000982 const uint64_t F = MI->getDesc().TSFlags;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000983
Brendon Cahoon6f358372012-02-08 18:25:47 +0000984 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000985}
986
Jyotsna Verma84c47102013-05-06 18:49:23 +0000987bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
988 const uint64_t F = get(Opcode).TSFlags;
989
990 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
991}
992
993bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
994 const uint64_t F = MI->getDesc().TSFlags;
995
996 assert(isPredicated(MI));
997 return (!((F >> HexagonII::PredicatedFalsePos) &
998 HexagonII::PredicatedFalseMask));
999}
1000
1001bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
1002 const uint64_t F = get(Opcode).TSFlags;
1003
1004 // Make sure that the instruction is predicated.
1005 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1006 return (!((F >> HexagonII::PredicatedFalsePos) &
1007 HexagonII::PredicatedFalseMask));
1008}
1009
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001010bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1011 const uint64_t F = MI->getDesc().TSFlags;
1012
1013 assert(isPredicated(MI));
1014 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1015}
1016
Jyotsna Verma84c47102013-05-06 18:49:23 +00001017bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1018 const uint64_t F = get(Opcode).TSFlags;
1019
1020 assert(isPredicated(Opcode));
1021 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1022}
1023
Jyotsna Verma438cec52013-05-10 20:58:11 +00001024// Returns true, if a ST insn can be promoted to a new-value store.
1025bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
1026 const HexagonRegisterInfo& QRI = getRegisterInfo();
1027 const uint64_t F = MI->getDesc().TSFlags;
1028
1029 return ((F >> HexagonII::mayNVStorePos) &
1030 HexagonII::mayNVStoreMask &
1031 QRI.Subtarget.hasV4TOps());
1032}
1033
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001034bool
1035HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1036 std::vector<MachineOperand> &Pred) const {
1037 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1038 MachineOperand MO = MI->getOperand(oper);
1039 if (MO.isReg() && MO.isDef()) {
1040 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
Craig Topperc7242e02012-04-20 07:30:17 +00001041 if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001042 Pred.push_back(MO);
1043 return true;
1044 }
1045 }
1046 }
1047 return false;
1048}
1049
1050
1051bool
1052HexagonInstrInfo::
1053SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1054 const SmallVectorImpl<MachineOperand> &Pred2) const {
1055 // TODO: Fix this
1056 return false;
1057}
1058
1059
1060//
1061// We indicate that we want to reverse the branch by
1062// inserting a 0 at the beginning of the Cond vector.
1063//
1064bool HexagonInstrInfo::
1065ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1066 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1067 Cond.erase(Cond.begin());
1068 } else {
1069 Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1070 }
1071 return false;
1072}
1073
1074
1075bool HexagonInstrInfo::
1076isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1077 const BranchProbability &Probability) const {
1078 return (NumInstrs <= 4);
1079}
1080
1081bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1082 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001083 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001084 case Hexagon::DEALLOC_RET_V4 :
1085 case Hexagon::DEALLOC_RET_cPt_V4 :
1086 case Hexagon::DEALLOC_RET_cNotPt_V4 :
1087 case Hexagon::DEALLOC_RET_cdnPnt_V4 :
1088 case Hexagon::DEALLOC_RET_cNotdnPnt_V4 :
1089 case Hexagon::DEALLOC_RET_cdnPt_V4 :
1090 case Hexagon::DEALLOC_RET_cNotdnPt_V4 :
1091 return true;
1092 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001093}
1094
1095
1096bool HexagonInstrInfo::
1097isValidOffset(const int Opcode, const int Offset) const {
1098 // This function is to check whether the "Offset" is in the correct range of
1099 // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1100 // inserted to calculate the final address. Due to this reason, the function
1101 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00001102 // We used to assert if the offset was not properly aligned, however,
1103 // there are cases where a misaligned pointer recast can cause this
1104 // problem, and we need to allow for it. The front end warns of such
1105 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001106
1107 switch(Opcode) {
1108
1109 case Hexagon::LDriw:
Jyotsna Verma9b60c1d2013-01-17 18:42:37 +00001110 case Hexagon::LDriw_indexed:
Sirish Pande69295b82012-05-10 20:20:25 +00001111 case Hexagon::LDriw_f:
Jyotsna Verma9b60c1d2013-01-17 18:42:37 +00001112 case Hexagon::STriw_indexed:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001113 case Hexagon::STriw:
Sirish Pande69295b82012-05-10 20:20:25 +00001114 case Hexagon::STriw_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001115 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1116 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1117
1118 case Hexagon::LDrid:
Jyotsna Verma9b60c1d2013-01-17 18:42:37 +00001119 case Hexagon::LDrid_indexed:
Sirish Pande69295b82012-05-10 20:20:25 +00001120 case Hexagon::LDrid_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001121 case Hexagon::STrid:
Jyotsna Verma9b60c1d2013-01-17 18:42:37 +00001122 case Hexagon::STrid_indexed:
Sirish Pande69295b82012-05-10 20:20:25 +00001123 case Hexagon::STrid_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001124 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1125 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1126
1127 case Hexagon::LDrih:
1128 case Hexagon::LDriuh:
1129 case Hexagon::STrih:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001130 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1131 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1132
1133 case Hexagon::LDrib:
1134 case Hexagon::STrib:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001135 case Hexagon::L2_loadrub_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001136 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1137 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1138
1139 case Hexagon::ADD_ri:
1140 case Hexagon::TFR_FI:
1141 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1142 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1143
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001144 case Hexagon::MemOPw_ADDi_V4 :
1145 case Hexagon::MemOPw_SUBi_V4 :
1146 case Hexagon::MemOPw_ADDr_V4 :
1147 case Hexagon::MemOPw_SUBr_V4 :
1148 case Hexagon::MemOPw_ANDr_V4 :
1149 case Hexagon::MemOPw_ORr_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001150 return (0 <= Offset && Offset <= 255);
1151
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001152 case Hexagon::MemOPh_ADDi_V4 :
1153 case Hexagon::MemOPh_SUBi_V4 :
1154 case Hexagon::MemOPh_ADDr_V4 :
1155 case Hexagon::MemOPh_SUBr_V4 :
1156 case Hexagon::MemOPh_ANDr_V4 :
1157 case Hexagon::MemOPh_ORr_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001158 return (0 <= Offset && Offset <= 127);
1159
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001160 case Hexagon::MemOPb_ADDi_V4 :
1161 case Hexagon::MemOPb_SUBi_V4 :
1162 case Hexagon::MemOPb_ADDr_V4 :
1163 case Hexagon::MemOPb_SUBr_V4 :
1164 case Hexagon::MemOPb_ANDr_V4 :
1165 case Hexagon::MemOPb_ORr_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001166 return (0 <= Offset && Offset <= 63);
1167
1168 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1169 // any size. Later pass knows how to handle it.
1170 case Hexagon::STriw_pred:
1171 case Hexagon::LDriw_pred:
1172 return true;
1173
Colin LeMahieu5ccbb122014-12-19 00:06:53 +00001174 case Hexagon::J2_loop0i:
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +00001175 return isUInt<10>(Offset);
1176
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001177 // INLINEASM is very special.
1178 case Hexagon::INLINEASM:
1179 return true;
1180 }
1181
Benjamin Kramerb6684012011-12-27 11:41:05 +00001182 llvm_unreachable("No offset range is defined for this opcode. "
1183 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001184}
1185
1186
1187//
1188// Check if the Offset is a valid auto-inc imm by Load/Store Type.
1189//
1190bool HexagonInstrInfo::
1191isValidAutoIncImm(const EVT VT, const int Offset) const {
1192
1193 if (VT == MVT::i64) {
1194 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1195 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1196 (Offset & 0x7) == 0);
1197 }
1198 if (VT == MVT::i32) {
1199 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1200 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1201 (Offset & 0x3) == 0);
1202 }
1203 if (VT == MVT::i16) {
1204 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1205 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1206 (Offset & 0x1) == 0);
1207 }
1208 if (VT == MVT::i8) {
1209 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1210 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1211 }
Craig Toppere55c5562012-02-07 02:50:20 +00001212 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001213}
1214
1215
1216bool HexagonInstrInfo::
1217isMemOp(const MachineInstr *MI) const {
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001218// return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1219
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001220 switch (MI->getOpcode())
1221 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001222 default: return false;
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001223 case Hexagon::MemOPw_ADDi_V4 :
1224 case Hexagon::MemOPw_SUBi_V4 :
1225 case Hexagon::MemOPw_ADDr_V4 :
1226 case Hexagon::MemOPw_SUBr_V4 :
1227 case Hexagon::MemOPw_ANDr_V4 :
1228 case Hexagon::MemOPw_ORr_V4 :
1229 case Hexagon::MemOPh_ADDi_V4 :
1230 case Hexagon::MemOPh_SUBi_V4 :
1231 case Hexagon::MemOPh_ADDr_V4 :
1232 case Hexagon::MemOPh_SUBr_V4 :
1233 case Hexagon::MemOPh_ANDr_V4 :
1234 case Hexagon::MemOPh_ORr_V4 :
1235 case Hexagon::MemOPb_ADDi_V4 :
1236 case Hexagon::MemOPb_SUBi_V4 :
1237 case Hexagon::MemOPb_ADDr_V4 :
1238 case Hexagon::MemOPb_SUBr_V4 :
1239 case Hexagon::MemOPb_ANDr_V4 :
1240 case Hexagon::MemOPb_ORr_V4 :
1241 case Hexagon::MemOPb_SETBITi_V4:
1242 case Hexagon::MemOPh_SETBITi_V4:
1243 case Hexagon::MemOPw_SETBITi_V4:
1244 case Hexagon::MemOPb_CLRBITi_V4:
1245 case Hexagon::MemOPh_CLRBITi_V4:
1246 case Hexagon::MemOPw_CLRBITi_V4:
1247 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001248 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001249 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001250}
1251
1252
1253bool HexagonInstrInfo::
1254isSpillPredRegOp(const MachineInstr *MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001255 switch (MI->getOpcode()) {
1256 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001257 case Hexagon::STriw_pred :
1258 case Hexagon::LDriw_pred :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001259 return true;
Sirish Pande2c7bf002012-04-23 17:49:28 +00001260 }
Sirish Pande4bd20c52012-05-12 05:10:30 +00001261}
1262
1263bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1264 switch (MI->getOpcode()) {
Sirish Pande8bb97452012-05-12 05:54:15 +00001265 default: return false;
Colin LeMahieu902157c2014-11-25 18:20:52 +00001266 case Hexagon::C2_cmpeq:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001267 case Hexagon::C2_cmpeqi:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001268 case Hexagon::C2_cmpgt:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001269 case Hexagon::C2_cmpgti:
Colin LeMahieu902157c2014-11-25 18:20:52 +00001270 case Hexagon::C2_cmpgtu:
Colin LeMahieu6e0f9f82014-11-26 19:43:12 +00001271 case Hexagon::C2_cmpgtui:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001272 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +00001273 }
Sirish Pande2c7bf002012-04-23 17:49:28 +00001274}
1275
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001276bool HexagonInstrInfo::
1277isConditionalTransfer (const MachineInstr *MI) const {
1278 switch (MI->getOpcode()) {
1279 default: return false;
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001280 case Hexagon::A2_tfrt:
1281 case Hexagon::A2_tfrf:
1282 case Hexagon::C2_cmoveit:
1283 case Hexagon::C2_cmoveif:
1284 case Hexagon::A2_tfrtnew:
1285 case Hexagon::A2_tfrfnew:
1286 case Hexagon::C2_cmovenewit:
1287 case Hexagon::C2_cmovenewif:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001288 return true;
1289 }
1290}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001291
1292bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001293 switch (MI->getOpcode())
1294 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001295 default: return false;
Colin LeMahieuefa74e02014-11-18 20:28:11 +00001296 case Hexagon::A2_paddf:
1297 case Hexagon::A2_paddfnew:
1298 case Hexagon::A2_paddt:
1299 case Hexagon::A2_paddtnew:
Colin LeMahieu44fd1c82014-11-18 22:45:47 +00001300 case Hexagon::A2_pandf:
1301 case Hexagon::A2_pandfnew:
1302 case Hexagon::A2_pandt:
1303 case Hexagon::A2_pandtnew:
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001304 case Hexagon::A4_paslhf:
1305 case Hexagon::A4_paslhfnew:
1306 case Hexagon::A4_paslht:
1307 case Hexagon::A4_paslhtnew:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001308 case Hexagon::A4_pasrhf:
1309 case Hexagon::A4_pasrhfnew:
1310 case Hexagon::A4_pasrht:
1311 case Hexagon::A4_pasrhtnew:
Colin LeMahieu21866542014-11-19 22:58:04 +00001312 case Hexagon::A2_porf:
1313 case Hexagon::A2_porfnew:
1314 case Hexagon::A2_port:
1315 case Hexagon::A2_portnew:
Colin LeMahieue88447d2014-11-21 21:19:18 +00001316 case Hexagon::A2_psubf:
1317 case Hexagon::A2_psubfnew:
1318 case Hexagon::A2_psubt:
1319 case Hexagon::A2_psubtnew:
Colin LeMahieuac006432014-11-19 23:22:23 +00001320 case Hexagon::A2_pxorf:
1321 case Hexagon::A2_pxorfnew:
1322 case Hexagon::A2_pxort:
1323 case Hexagon::A2_pxortnew:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001324 case Hexagon::A4_psxthf:
1325 case Hexagon::A4_psxthfnew:
1326 case Hexagon::A4_psxtht:
1327 case Hexagon::A4_psxthtnew:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001328 case Hexagon::A4_psxtbf:
1329 case Hexagon::A4_psxtbfnew:
1330 case Hexagon::A4_psxtbt:
1331 case Hexagon::A4_psxtbtnew:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001332 case Hexagon::A4_pzxtbf:
1333 case Hexagon::A4_pzxtbfnew:
1334 case Hexagon::A4_pzxtbt:
1335 case Hexagon::A4_pzxtbtnew:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001336 case Hexagon::A4_pzxthf:
1337 case Hexagon::A4_pzxthfnew:
1338 case Hexagon::A4_pzxtht:
1339 case Hexagon::A4_pzxthtnew:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001340 case Hexagon::ADD_ri_cPt:
1341 case Hexagon::ADD_ri_cNotPt:
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001342 case Hexagon::C2_ccombinewt:
1343 case Hexagon::C2_ccombinewf:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001344 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001345 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001346}
1347
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001348bool HexagonInstrInfo::
1349isConditionalLoad (const MachineInstr* MI) const {
1350 const HexagonRegisterInfo& QRI = getRegisterInfo();
1351 switch (MI->getOpcode())
1352 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001353 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001354 case Hexagon::LDrid_cPt :
1355 case Hexagon::LDrid_cNotPt :
1356 case Hexagon::LDrid_indexed_cPt :
1357 case Hexagon::LDrid_indexed_cNotPt :
1358 case Hexagon::LDriw_cPt :
1359 case Hexagon::LDriw_cNotPt :
1360 case Hexagon::LDriw_indexed_cPt :
1361 case Hexagon::LDriw_indexed_cNotPt :
1362 case Hexagon::LDrih_cPt :
1363 case Hexagon::LDrih_cNotPt :
1364 case Hexagon::LDrih_indexed_cPt :
1365 case Hexagon::LDrih_indexed_cNotPt :
1366 case Hexagon::LDrib_cPt :
1367 case Hexagon::LDrib_cNotPt :
1368 case Hexagon::LDrib_indexed_cPt :
1369 case Hexagon::LDrib_indexed_cNotPt :
1370 case Hexagon::LDriuh_cPt :
1371 case Hexagon::LDriuh_cNotPt :
1372 case Hexagon::LDriuh_indexed_cPt :
1373 case Hexagon::LDriuh_indexed_cNotPt :
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001374 case Hexagon::L2_ploadrubt_io:
1375 case Hexagon::L2_ploadrubf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001376 return true;
1377 case Hexagon::POST_LDrid_cPt :
1378 case Hexagon::POST_LDrid_cNotPt :
1379 case Hexagon::POST_LDriw_cPt :
1380 case Hexagon::POST_LDriw_cNotPt :
1381 case Hexagon::POST_LDrih_cPt :
1382 case Hexagon::POST_LDrih_cNotPt :
1383 case Hexagon::POST_LDrib_cPt :
1384 case Hexagon::POST_LDrib_cNotPt :
1385 case Hexagon::POST_LDriuh_cPt :
1386 case Hexagon::POST_LDriuh_cNotPt :
1387 case Hexagon::POST_LDriub_cPt :
1388 case Hexagon::POST_LDriub_cNotPt :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001389 return QRI.Subtarget.hasV4TOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001390 case Hexagon::LDrid_indexed_shl_cPt_V4 :
1391 case Hexagon::LDrid_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001392 case Hexagon::LDrib_indexed_shl_cPt_V4 :
1393 case Hexagon::LDrib_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001394 case Hexagon::LDriub_indexed_shl_cPt_V4 :
1395 case Hexagon::LDriub_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001396 case Hexagon::LDrih_indexed_shl_cPt_V4 :
1397 case Hexagon::LDrih_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001398 case Hexagon::LDriuh_indexed_shl_cPt_V4 :
1399 case Hexagon::LDriuh_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001400 case Hexagon::LDriw_indexed_shl_cPt_V4 :
1401 case Hexagon::LDriw_indexed_shl_cNotPt_V4 :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001402 return QRI.Subtarget.hasV4TOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001403 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001404}
Andrew Trickd06df962012-02-01 22:13:57 +00001405
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001406// Returns true if an instruction is a conditional store.
1407//
1408// Note: It doesn't include conditional new-value stores as they can't be
1409// converted to .new predicate.
1410//
1411// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1412// ^ ^
1413// / \ (not OK. it will cause new-value store to be
1414// / X conditional on p0.new while R2 producer is
1415// / \ on p0)
1416// / \.
1417// p.new store p.old NV store
1418// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1419// ^ ^
1420// \ /
1421// \ /
1422// \ /
1423// p.old store
1424// [if (p0)memw(R0+#0)=R2]
1425//
1426// The above diagram shows the steps involoved in the conversion of a predicated
1427// store instruction to its .new predicated new-value form.
1428//
1429// The following set of instructions further explains the scenario where
1430// conditional new-value store becomes invalid when promoted to .new predicate
1431// form.
1432//
1433// { 1) if (p0) r0 = add(r1, r2)
1434// 2) p0 = cmp.eq(r3, #0) }
1435//
1436// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1437// the first two instructions because in instr 1, r0 is conditional on old value
1438// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1439// is not valid for new-value stores.
1440bool HexagonInstrInfo::
1441isConditionalStore (const MachineInstr* MI) const {
1442 const HexagonRegisterInfo& QRI = getRegisterInfo();
1443 switch (MI->getOpcode())
1444 {
1445 default: return false;
1446 case Hexagon::STrib_imm_cPt_V4 :
1447 case Hexagon::STrib_imm_cNotPt_V4 :
1448 case Hexagon::STrib_indexed_shl_cPt_V4 :
1449 case Hexagon::STrib_indexed_shl_cNotPt_V4 :
1450 case Hexagon::STrib_cPt :
1451 case Hexagon::STrib_cNotPt :
1452 case Hexagon::POST_STbri_cPt :
1453 case Hexagon::POST_STbri_cNotPt :
1454 case Hexagon::STrid_indexed_cPt :
1455 case Hexagon::STrid_indexed_cNotPt :
1456 case Hexagon::STrid_indexed_shl_cPt_V4 :
1457 case Hexagon::POST_STdri_cPt :
1458 case Hexagon::POST_STdri_cNotPt :
1459 case Hexagon::STrih_cPt :
1460 case Hexagon::STrih_cNotPt :
1461 case Hexagon::STrih_indexed_cPt :
1462 case Hexagon::STrih_indexed_cNotPt :
1463 case Hexagon::STrih_imm_cPt_V4 :
1464 case Hexagon::STrih_imm_cNotPt_V4 :
1465 case Hexagon::STrih_indexed_shl_cPt_V4 :
1466 case Hexagon::STrih_indexed_shl_cNotPt_V4 :
1467 case Hexagon::POST_SThri_cPt :
1468 case Hexagon::POST_SThri_cNotPt :
1469 case Hexagon::STriw_cPt :
1470 case Hexagon::STriw_cNotPt :
1471 case Hexagon::STriw_indexed_cPt :
1472 case Hexagon::STriw_indexed_cNotPt :
1473 case Hexagon::STriw_imm_cPt_V4 :
1474 case Hexagon::STriw_imm_cNotPt_V4 :
1475 case Hexagon::STriw_indexed_shl_cPt_V4 :
1476 case Hexagon::STriw_indexed_shl_cNotPt_V4 :
1477 case Hexagon::POST_STwri_cPt :
1478 case Hexagon::POST_STwri_cNotPt :
1479 return QRI.Subtarget.hasV4TOps();
1480
1481 // V4 global address store before promoting to dot new.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001482 case Hexagon::STd_GP_cPt_V4 :
1483 case Hexagon::STd_GP_cNotPt_V4 :
1484 case Hexagon::STb_GP_cPt_V4 :
1485 case Hexagon::STb_GP_cNotPt_V4 :
1486 case Hexagon::STh_GP_cPt_V4 :
1487 case Hexagon::STh_GP_cNotPt_V4 :
1488 case Hexagon::STw_GP_cPt_V4 :
1489 case Hexagon::STw_GP_cNotPt_V4 :
1490 return QRI.Subtarget.hasV4TOps();
1491
1492 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1493 // from the "Conditional Store" list. Because a predicated new value store
1494 // would NOT be promoted to a double dot new store. See diagram below:
1495 // This function returns yes for those stores that are predicated but not
1496 // yet promoted to predicate dot new instructions.
1497 //
1498 // +---------------------+
1499 // /-----| if (p0) memw(..)=r0 |---------\~
1500 // || +---------------------+ ||
1501 // promote || /\ /\ || promote
1502 // || /||\ /||\ ||
1503 // \||/ demote || \||/
1504 // \/ || || \/
1505 // +-------------------------+ || +-------------------------+
1506 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1507 // +-------------------------+ || +-------------------------+
1508 // || || ||
1509 // || demote \||/
1510 // promote || \/ NOT possible
1511 // || || /\~
1512 // \||/ || /||\~
1513 // \/ || ||
1514 // +-----------------------------+
1515 // | if (p0.new) memw(..)=r0.new |
1516 // +-----------------------------+
1517 // Double Dot New Store
1518 //
1519 }
1520}
1521
Jyotsna Verma84c47102013-05-06 18:49:23 +00001522
1523bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1524 if (isNewValue(MI) && isBranch(MI))
1525 return true;
1526 return false;
1527}
1528
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001529bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1530 return (getAddrMode(MI) == HexagonII::PostInc);
1531}
1532
Jyotsna Verma84c47102013-05-06 18:49:23 +00001533bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1534 const uint64_t F = MI->getDesc().TSFlags;
1535 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1536}
1537
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001538// Returns true, if any one of the operands is a dot new
1539// insn, whether it is predicated dot new or register dot new.
1540bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1541 return (isNewValueInst(MI) ||
1542 (isPredicated(MI) && isPredicatedNew(MI)));
1543}
1544
Jyotsna Verma438cec52013-05-10 20:58:11 +00001545// Returns the most basic instruction for the .new predicated instructions and
1546// new-value stores.
1547// For example, all of the following instructions will be converted back to the
1548// same instruction:
1549// 1) if (p0.new) memw(R0+#0) = R1.new --->
1550// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1551// 3) if (p0.new) memw(R0+#0) = R1 --->
1552//
1553
1554int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1555 int NewOp = opc;
1556 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1557 NewOp = Hexagon::getPredOldOpcode(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001558 assert(NewOp >= 0 &&
1559 "Couldn't change predicate new instruction to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001560 }
1561
Alp Tokerf907b892013-12-05 05:44:44 +00001562 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
Jyotsna Verma438cec52013-05-10 20:58:11 +00001563 NewOp = Hexagon::getNonNVStore(NewOp);
Craig Topper35b2f752014-06-19 06:10:58 +00001564 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
Jyotsna Verma438cec52013-05-10 20:58:11 +00001565 }
1566 return NewOp;
1567}
1568
Jyotsna Verma300f0b92013-05-10 20:27:34 +00001569// Return the new value instruction for a given store.
1570int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1571 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1572 if (NVOpcode >= 0) // Valid new-value store instruction.
1573 return NVOpcode;
1574
1575 switch (MI->getOpcode()) {
1576 default: llvm_unreachable("Unknown .new type");
1577 // store new value byte
1578 case Hexagon::STrib_shl_V4:
1579 return Hexagon::STrib_shl_nv_V4;
1580
1581 case Hexagon::STrih_shl_V4:
1582 return Hexagon::STrih_shl_nv_V4;
1583
1584 case Hexagon::STriw_f:
1585 return Hexagon::STriw_nv_V4;
1586
1587 case Hexagon::STriw_indexed_f:
1588 return Hexagon::STriw_indexed_nv_V4;
1589
1590 case Hexagon::STriw_shl_V4:
1591 return Hexagon::STriw_shl_nv_V4;
1592
1593 }
1594 return 0;
1595}
1596
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001597// Return .new predicate version for an instruction.
1598int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1599 const MachineBranchProbabilityInfo
1600 *MBPI) const {
1601
1602 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1603 if (NewOpcode >= 0) // Valid predicate new instruction
1604 return NewOpcode;
1605
1606 switch (MI->getOpcode()) {
1607 default: llvm_unreachable("Unknown .new type");
1608 // Condtional Jumps
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001609 case Hexagon::J2_jumpt:
1610 case Hexagon::J2_jumpf:
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001611 return getDotNewPredJumpOp(MI, MBPI);
1612
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001613 case Hexagon::J2_jumprt:
1614 return Hexagon::J2_jumptnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001615
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001616 case Hexagon::J2_jumprf:
1617 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001618
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001619 case Hexagon::JMPrett:
1620 return Hexagon::J2_jumprtnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001621
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001622 case Hexagon::JMPretf:
1623 return Hexagon::J2_jumprfnewpt;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001624
1625
1626 // Conditional combine
Colin LeMahieub580d7d2014-12-09 19:23:45 +00001627 case Hexagon::C2_ccombinewt:
1628 return Hexagon::C2_ccombinewnewt;
1629 case Hexagon::C2_ccombinewf:
1630 return Hexagon::C2_ccombinewnewf;
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001631 }
1632}
1633
1634
Jyotsna Verma84256432013-03-01 17:37:13 +00001635unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1636 const uint64_t F = MI->getDesc().TSFlags;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001637
Jyotsna Verma84256432013-03-01 17:37:13 +00001638 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1639}
1640
1641/// immediateExtend - Changes the instruction in place to one using an immediate
1642/// extender.
1643void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1644 assert((isExtendable(MI)||isConstExtended(MI)) &&
1645 "Instruction must be extendable");
1646 // Find which operand is extendable.
1647 short ExtOpNum = getCExtOpNum(MI);
1648 MachineOperand &MO = MI->getOperand(ExtOpNum);
1649 // This needs to be something we understand.
1650 assert((MO.isMBB() || MO.isImm()) &&
1651 "Branch with unknown extendable field type");
1652 // Mark given operand as extended.
1653 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1654}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001655
Eric Christopher143f02c2014-10-09 01:59:35 +00001656DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1657 const TargetSubtargetInfo &STI) const {
1658 const InstrItineraryData *II = STI.getInstrItineraryData();
1659 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
Andrew Trickd06df962012-02-01 22:13:57 +00001660}
1661
1662bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1663 const MachineBasicBlock *MBB,
1664 const MachineFunction &MF) const {
1665 // Debug info is never a scheduling boundary. It's necessary to be explicit
1666 // due to the special treatment of IT instructions below, otherwise a
1667 // dbg_value followed by an IT will result in the IT instruction being
1668 // considered a scheduling hazard, which is wrong. It should be the actual
1669 // instruction preceding the dbg_value instruction(s), just like it is
1670 // when debug info is not present.
1671 if (MI->isDebugValue())
1672 return false;
1673
1674 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001675 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
Andrew Trickd06df962012-02-01 22:13:57 +00001676 return true;
1677
1678 return false;
1679}
Jyotsna Verma84256432013-03-01 17:37:13 +00001680
1681bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
1682
1683 // Constant extenders are allowed only for V4 and above.
1684 if (!Subtarget.hasV4TOps())
1685 return false;
1686
1687 const uint64_t F = MI->getDesc().TSFlags;
1688 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1689 if (isExtended) // Instruction must be extended.
1690 return true;
1691
1692 unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1693 & HexagonII::ExtendableMask;
1694 if (!isExtendable)
1695 return false;
1696
1697 short ExtOpNum = getCExtOpNum(MI);
1698 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1699 // Use MO operand flags to determine if MO
1700 // has the HMOTF_ConstExtended flag set.
1701 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1702 return true;
1703 // If this is a Machine BB address we are talking about, and it is
1704 // not marked as extended, say so.
1705 if (MO.isMBB())
1706 return false;
1707
1708 // We could be using an instruction with an extendable immediate and shoehorn
1709 // a global address into it. If it is a global address it will be constant
1710 // extended. We do this for COMBINE.
1711 // We currently only handle isGlobal() because it is the only kind of
1712 // object we are going to end up with here for now.
1713 // In the future we probably should add isSymbol(), etc.
1714 if (MO.isGlobal() || MO.isSymbol())
1715 return true;
1716
1717 // If the extendable operand is not 'Immediate' type, the instruction should
1718 // have 'isExtended' flag set.
1719 assert(MO.isImm() && "Extendable operand must be Immediate type");
1720
1721 int MinValue = getMinValue(MI);
1722 int MaxValue = getMaxValue(MI);
1723 int ImmValue = MO.getImm();
1724
1725 return (ImmValue < MinValue || ImmValue > MaxValue);
1726}
1727
Jyotsna Verma1d297502013-05-02 15:39:30 +00001728// Returns the opcode to use when converting MI, which is a conditional jump,
1729// into a conditional instruction which uses the .new value of the predicate.
1730// We also use branch probabilities to add a hint to the jump.
1731int
1732HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1733 const
1734 MachineBranchProbabilityInfo *MBPI) const {
1735
1736 // We assume that block can have at most two successors.
1737 bool taken = false;
1738 MachineBasicBlock *Src = MI->getParent();
1739 MachineOperand *BrTarget = &MI->getOperand(1);
1740 MachineBasicBlock *Dst = BrTarget->getMBB();
1741
1742 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1743 if (Prediction >= BranchProbability(1,2))
1744 taken = true;
1745
1746 switch (MI->getOpcode()) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001747 case Hexagon::J2_jumpt:
1748 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1749 case Hexagon::J2_jumpf:
1750 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Jyotsna Verma1d297502013-05-02 15:39:30 +00001751
1752 default:
1753 llvm_unreachable("Unexpected jump instruction.");
1754 }
1755}
Jyotsna Verma84256432013-03-01 17:37:13 +00001756// Returns true if a particular operand is extendable for an instruction.
1757bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1758 unsigned short OperandNum) const {
1759 // Constant extenders are allowed only for V4 and above.
1760 if (!Subtarget.hasV4TOps())
1761 return false;
1762
1763 const uint64_t F = MI->getDesc().TSFlags;
1764
1765 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1766 == OperandNum;
1767}
1768
1769// Returns Operand Index for the constant extended instruction.
1770unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1771 const uint64_t F = MI->getDesc().TSFlags;
1772 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1773}
1774
1775// Returns the min value that doesn't need to be extended.
1776int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1777 const uint64_t F = MI->getDesc().TSFlags;
1778 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1779 & HexagonII::ExtentSignedMask;
1780 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1781 & HexagonII::ExtentBitsMask;
1782
1783 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001784 return -1U << (bits - 1);
Jyotsna Verma84256432013-03-01 17:37:13 +00001785 else
1786 return 0;
1787}
1788
1789// Returns the max value that doesn't need to be extended.
1790int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1791 const uint64_t F = MI->getDesc().TSFlags;
1792 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1793 & HexagonII::ExtentSignedMask;
1794 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1795 & HexagonII::ExtentBitsMask;
1796
1797 if (isSigned) // if value is signed
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001798 return ~(-1U << (bits - 1));
Jyotsna Verma84256432013-03-01 17:37:13 +00001799 else
Alexey Samsonov2651ae62014-08-20 21:22:03 +00001800 return ~(-1U << bits);
Jyotsna Verma84256432013-03-01 17:37:13 +00001801}
1802
1803// Returns true if an instruction can be converted into a non-extended
1804// equivalent instruction.
1805bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1806
1807 short NonExtOpcode;
1808 // Check if the instruction has a register form that uses register in place
1809 // of the extended operand, if so return that as the non-extended form.
1810 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1811 return true;
1812
1813 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001814 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001815
1816 switch (getAddrMode(MI)) {
1817 case HexagonII::Absolute :
1818 // Load/store with absolute addressing mode can be converted into
1819 // base+offset mode.
1820 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1821 break;
1822 case HexagonII::BaseImmOffset :
1823 // Load/store with base+offset addressing mode can be converted into
1824 // base+register offset addressing mode. However left shift operand should
1825 // be set to 0.
1826 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1827 break;
1828 default:
1829 return false;
1830 }
1831 if (NonExtOpcode < 0)
1832 return false;
1833 return true;
1834 }
1835 return false;
1836}
1837
1838// Returns opcode of the non-extended equivalent instruction.
1839short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1840
1841 // Check if the instruction has a register form that uses register in place
1842 // of the extended operand, if so return that as the non-extended form.
1843 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1844 if (NonExtOpcode >= 0)
1845 return NonExtOpcode;
1846
1847 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00001848 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00001849 switch (getAddrMode(MI)) {
1850 case HexagonII::Absolute :
1851 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1852 case HexagonII::BaseImmOffset :
1853 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1854 default:
1855 return -1;
1856 }
1857 }
1858 return -1;
1859}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001860
1861bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001862 return (Opcode == Hexagon::J2_jumpt) ||
1863 (Opcode == Hexagon::J2_jumpf) ||
1864 (Opcode == Hexagon::J2_jumptnewpt) ||
1865 (Opcode == Hexagon::J2_jumpfnewpt) ||
1866 (Opcode == Hexagon::J2_jumpt) ||
1867 (Opcode == Hexagon::J2_jumpf);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001868}
1869
1870bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +00001871 return (Opcode == Hexagon::J2_jumpf) ||
1872 (Opcode == Hexagon::J2_jumpfnewpt) ||
1873 (Opcode == Hexagon::J2_jumpfnew);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001874}