blob: 469e8021fae0d0800e460d280ed187145d7b8103 [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#define GET_INSTRINFO_CTOR
Pranav Bhandarkar34b60182012-11-01 19:13:23 +000030#define GET_INSTRMAP_INFO
Tony Linthicum1213a7a2011-12-12 21:14:40 +000031#include "HexagonGenInstrInfo.inc"
Andrew Trickd06df962012-02-01 22:13:57 +000032#include "HexagonGenDFAPacketizer.inc"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034using namespace llvm;
35
36///
37/// Constants for Hexagon instructions.
38///
39const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000040const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000041const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000042const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000043const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000044const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000045const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000046const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000047const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000048const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000049const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000050const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000051const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000052const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000054const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000055const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000056const int Hexagon_MEMB_AUTOINC_MIN = -8;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000057
58
59HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
60 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
61 RI(ST, *this), Subtarget(ST) {
62}
63
64
65/// isLoadFromStackSlot - If the specified machine instruction is a direct
66/// load from a stack slot, return the virtual or physical register number of
67/// the destination along with the FrameIndex of the loaded stack slot. If
68/// not, return 0. This predicate must return 0 if the instruction has
69/// any side effects other than loading from the stack slot.
70unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
71 int &FrameIndex) const {
72
73
74 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000075 default: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000076 case Hexagon::LDriw:
77 case Hexagon::LDrid:
78 case Hexagon::LDrih:
79 case Hexagon::LDrib:
80 case Hexagon::LDriub:
81 if (MI->getOperand(2).isFI() &&
82 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
83 FrameIndex = MI->getOperand(2).getIndex();
84 return MI->getOperand(0).getReg();
85 }
86 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000087 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088 return 0;
89}
90
91
92/// isStoreToStackSlot - If the specified machine instruction is a direct
93/// store to a stack slot, return the virtual or physical register number of
94/// the source reg along with the FrameIndex of the loaded stack slot. If
95/// not, return 0. This predicate must return 0 if the instruction has
96/// any side effects other than storing to the stack slot.
97unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
98 int &FrameIndex) const {
99 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000100 default: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000101 case Hexagon::STriw:
102 case Hexagon::STrid:
103 case Hexagon::STrih:
104 case Hexagon::STrib:
105 if (MI->getOperand(2).isFI() &&
106 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
Sirish Pande8bb97452012-05-12 05:54:15 +0000107 FrameIndex = MI->getOperand(0).getIndex();
108 return MI->getOperand(2).getReg();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109 }
110 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000112 return 0;
113}
114
115
116unsigned
117HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
118 MachineBasicBlock *FBB,
119 const SmallVectorImpl<MachineOperand> &Cond,
120 DebugLoc DL) const{
121
122 int BOpc = Hexagon::JMP;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000123 int BccOpc = Hexagon::JMP_t;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000124
125 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
126
127 int regPos = 0;
128 // Check if ReverseBranchCondition has asked to reverse this branch
129 // If we want to reverse the branch an odd number of times, we want
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000130 // JMP_f.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000131 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000132 BccOpc = Hexagon::JMP_f;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000133 regPos = 1;
134 }
135
136 if (FBB == 0) {
137 if (Cond.empty()) {
138 // Due to a bug in TailMerging/CFG Optimization, we need to add a
139 // special case handling of a predicated jump followed by an
140 // unconditional jump. If not, Tail Merging and CFG Optimization go
141 // into an infinite loop.
142 MachineBasicBlock *NewTBB, *NewFBB;
143 SmallVector<MachineOperand, 4> Cond;
144 MachineInstr *Term = MBB.getFirstTerminator();
145 if (isPredicated(Term) && !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond,
146 false)) {
147 MachineBasicBlock *NextBB =
148 llvm::next(MachineFunction::iterator(&MBB));
149 if (NewTBB == NextBB) {
150 ReverseBranchCondition(Cond);
151 RemoveBranch(MBB);
152 return InsertBranch(MBB, TBB, 0, Cond, DL);
153 }
154 }
155 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
156 } else {
157 BuildMI(&MBB, DL,
158 get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
159 }
160 return 1;
161 }
162
163 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
164 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
165
166 return 2;
167}
168
169
170bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
171 MachineBasicBlock *&TBB,
172 MachineBasicBlock *&FBB,
173 SmallVectorImpl<MachineOperand> &Cond,
174 bool AllowModify) const {
Benjamin Kramer0b03cbd2012-05-13 15:13:22 +0000175 TBB = NULL;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000176 FBB = NULL;
177
178 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000179 MachineBasicBlock::instr_iterator I = MBB.instr_end();
180 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000181 return false;
182
183 // A basic block may looks like this:
184 //
185 // [ insn
186 // EH_LABEL
187 // insn
188 // insn
189 // insn
190 // EH_LABEL
191 // insn ]
192 //
193 // It has two succs but does not have a terminator
194 // Don't know how to handle it.
195 do {
196 --I;
197 if (I->isEHLabel())
198 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000199 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000200
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000201 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000202 --I;
203
204 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000205 if (I == MBB.instr_begin())
206 return false;
207 --I;
208 }
209
210 // Delete the JMP if it's equivalent to a fall-through.
211 if (AllowModify && I->getOpcode() == Hexagon::JMP &&
212 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
213 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
214 I->eraseFromParent();
215 I = MBB.instr_end();
216 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000217 return false;
218 --I;
219 }
220 if (!isUnpredicatedTerminator(I))
221 return false;
222
223 // Get the last instruction in the block.
224 MachineInstr *LastInst = I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000225 MachineInstr *SecondLastInst = NULL;
226 // Find one more terminator if present.
227 do {
228 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
229 if (!SecondLastInst)
230 SecondLastInst = I;
231 else
232 // This is a third branch.
233 return true;
234 }
235 if (I == MBB.instr_begin())
236 break;
237 --I;
238 } while(I);
239
240 int LastOpcode = LastInst->getOpcode();
241
242 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
243 bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000244
245 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000246 if (LastInst && !SecondLastInst) {
247 if (LastOpcode == Hexagon::JMP) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000248 TBB = LastInst->getOperand(0).getMBB();
249 return false;
250 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000251 if (LastOpcode == Hexagon::ENDLOOP0) {
252 TBB = LastInst->getOperand(0).getMBB();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000253 Cond.push_back(LastInst->getOperand(0));
254 return false;
255 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000256 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000257 TBB = LastInst->getOperand(1).getMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000258 if (LastOpcodeHasNot) {
259 Cond.push_back(MachineOperand::CreateImm(0));
260 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000261 Cond.push_back(LastInst->getOperand(0));
262 return false;
263 }
264 // Otherwise, don't know what this is.
265 return true;
266 }
267
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000268 int SecLastOpcode = SecondLastInst->getOpcode();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000269
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000270 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
271 bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
272 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::JMP)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000273 TBB = SecondLastInst->getOperand(1).getMBB();
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000274 if (SecLastOpcodeHasNot)
275 Cond.push_back(MachineOperand::CreateImm(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000276 Cond.push_back(SecondLastInst->getOperand(0));
277 FBB = LastInst->getOperand(0).getMBB();
278 return false;
279 }
280
281 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
282 // executed, so remove it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000283 if (SecLastOpcode == Hexagon::JMP && LastOpcode == Hexagon::JMP) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000284 TBB = SecondLastInst->getOperand(0).getMBB();
285 I = LastInst;
286 if (AllowModify)
287 I->eraseFromParent();
288 return false;
289 }
290
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000291 // If the block ends with an ENDLOOP, and JMP, handle it.
292 if (SecLastOpcode == Hexagon::ENDLOOP0 &&
293 LastOpcode == Hexagon::JMP) {
294 TBB = SecondLastInst->getOperand(0).getMBB();
295 Cond.push_back(SecondLastInst->getOperand(0));
296 FBB = LastInst->getOperand(0).getMBB();
297 return false;
298 }
299
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000300 // Otherwise, can't handle this.
301 return true;
302}
303
304
305unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
306 int BOpc = Hexagon::JMP;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000307 int BccOpc = Hexagon::JMP_t;
308 int BccOpcNot = Hexagon::JMP_f;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000309
310 MachineBasicBlock::iterator I = MBB.end();
311 if (I == MBB.begin()) return 0;
312 --I;
313 if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc &&
314 I->getOpcode() != BccOpcNot)
315 return 0;
316
317 // Remove the branch.
318 I->eraseFromParent();
319
320 I = MBB.end();
321
322 if (I == MBB.begin()) return 1;
323 --I;
324 if (I->getOpcode() != BccOpc && I->getOpcode() != BccOpcNot)
325 return 1;
326
327 // Remove the branch.
328 I->eraseFromParent();
329 return 2;
330}
331
332
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000333/// \brief For a comparison instruction, return the source registers in
334/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
335/// compares against in CmpValue. Return true if the comparison instruction
336/// can be analyzed.
337bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
338 unsigned &SrcReg, unsigned &SrcReg2,
339 int &Mask, int &Value) const {
340 unsigned Opc = MI->getOpcode();
341
342 // Set mask and the first source register.
343 switch (Opc) {
344 case Hexagon::CMPEHexagon4rr:
345 case Hexagon::CMPEQri:
346 case Hexagon::CMPEQrr:
347 case Hexagon::CMPGT64rr:
348 case Hexagon::CMPGTU64rr:
349 case Hexagon::CMPGTUri:
350 case Hexagon::CMPGTUrr:
351 case Hexagon::CMPGTri:
352 case Hexagon::CMPGTrr:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000353 SrcReg = MI->getOperand(1).getReg();
354 Mask = ~0;
355 break;
356 case Hexagon::CMPbEQri_V4:
357 case Hexagon::CMPbEQrr_sbsb_V4:
358 case Hexagon::CMPbEQrr_ubub_V4:
359 case Hexagon::CMPbGTUri_V4:
360 case Hexagon::CMPbGTUrr_V4:
361 case Hexagon::CMPbGTrr_V4:
362 SrcReg = MI->getOperand(1).getReg();
363 Mask = 0xFF;
364 break;
365 case Hexagon::CMPhEQri_V4:
366 case Hexagon::CMPhEQrr_shl_V4:
367 case Hexagon::CMPhEQrr_xor_V4:
368 case Hexagon::CMPhGTUri_V4:
369 case Hexagon::CMPhGTUrr_V4:
370 case Hexagon::CMPhGTrr_shl_V4:
371 SrcReg = MI->getOperand(1).getReg();
372 Mask = 0xFFFF;
373 break;
374 }
375
376 // Set the value/second source register.
377 switch (Opc) {
378 case Hexagon::CMPEHexagon4rr:
379 case Hexagon::CMPEQrr:
380 case Hexagon::CMPGT64rr:
381 case Hexagon::CMPGTU64rr:
382 case Hexagon::CMPGTUrr:
383 case Hexagon::CMPGTrr:
384 case Hexagon::CMPbEQrr_sbsb_V4:
385 case Hexagon::CMPbEQrr_ubub_V4:
386 case Hexagon::CMPbGTUrr_V4:
387 case Hexagon::CMPbGTrr_V4:
388 case Hexagon::CMPhEQrr_shl_V4:
389 case Hexagon::CMPhEQrr_xor_V4:
390 case Hexagon::CMPhGTUrr_V4:
391 case Hexagon::CMPhGTrr_shl_V4:
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000392 SrcReg2 = MI->getOperand(2).getReg();
393 return true;
394
395 case Hexagon::CMPEQri:
396 case Hexagon::CMPGTUri:
397 case Hexagon::CMPGTri:
398 case Hexagon::CMPbEQri_V4:
399 case Hexagon::CMPbGTUri_V4:
400 case Hexagon::CMPhEQri_V4:
401 case Hexagon::CMPhGTUri_V4:
402 SrcReg2 = 0;
403 Value = MI->getOperand(2).getImm();
404 return true;
405 }
406
407 return false;
408}
409
410
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000411void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
412 MachineBasicBlock::iterator I, DebugLoc DL,
413 unsigned DestReg, unsigned SrcReg,
414 bool KillSrc) const {
415 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
416 BuildMI(MBB, I, DL, get(Hexagon::TFR), DestReg).addReg(SrcReg);
417 return;
418 }
419 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Jyotsna Vermae95559f2012-11-29 19:35:44 +0000420 BuildMI(MBB, I, DL, get(Hexagon::TFR64), DestReg).addReg(SrcReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000421 return;
422 }
423 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
424 // Map Pd = Ps to Pd = or(Ps, Ps).
425 BuildMI(MBB, I, DL, get(Hexagon::OR_pp),
426 DestReg).addReg(SrcReg).addReg(SrcReg);
427 return;
428 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000429 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
430 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000431 // We can have an overlap between single and double reg: r1:0 = r0.
432 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
433 // r1:0 = r0
434 BuildMI(MBB, I, DL, get(Hexagon::TFRI), (RI.getSubReg(DestReg,
435 Hexagon::subreg_hireg))).addImm(0);
436 } else {
437 // r1:0 = r1 or no overlap.
438 BuildMI(MBB, I, DL, get(Hexagon::TFR), (RI.getSubReg(DestReg,
439 Hexagon::subreg_loreg))).addReg(SrcReg);
440 BuildMI(MBB, I, DL, get(Hexagon::TFRI), (RI.getSubReg(DestReg,
441 Hexagon::subreg_hireg))).addImm(0);
442 }
443 return;
444 }
Sirish Pande8bb97452012-05-12 05:54:15 +0000445 if (Hexagon::CRRegsRegClass.contains(DestReg) &&
446 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000447 BuildMI(MBB, I, DL, get(Hexagon::TFCR), DestReg).addReg(SrcReg);
448 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000449 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000450 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
451 Hexagon::IntRegsRegClass.contains(DestReg)) {
452 BuildMI(MBB, I, DL, get(Hexagon::TFR_RsPd), DestReg).
453 addReg(SrcReg, getKillRegState(KillSrc));
454 return;
455 }
456 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
457 Hexagon::PredRegsRegClass.contains(DestReg)) {
458 BuildMI(MBB, I, DL, get(Hexagon::TFR_PdRs), DestReg).
459 addReg(SrcReg, getKillRegState(KillSrc));
460 return;
461 }
Sirish Pande30804c22012-02-15 18:52:27 +0000462
463 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000464}
465
466
467void HexagonInstrInfo::
468storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
469 unsigned SrcReg, bool isKill, int FI,
470 const TargetRegisterClass *RC,
471 const TargetRegisterInfo *TRI) const {
472
473 DebugLoc DL = MBB.findDebugLoc(I);
474 MachineFunction &MF = *MBB.getParent();
475 MachineFrameInfo &MFI = *MF.getFrameInfo();
476 unsigned Align = MFI.getObjectAlignment(FI);
477
478 MachineMemOperand *MMO =
479 MF.getMachineMemOperand(
480 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
481 MachineMemOperand::MOStore,
482 MFI.getObjectSize(FI),
483 Align);
484
Craig Topperc7242e02012-04-20 07:30:17 +0000485 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000486 BuildMI(MBB, I, DL, get(Hexagon::STriw))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000487 .addFrameIndex(FI).addImm(0)
488 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000489 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000490 BuildMI(MBB, I, DL, get(Hexagon::STrid))
491 .addFrameIndex(FI).addImm(0)
492 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000493 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000494 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
495 .addFrameIndex(FI).addImm(0)
496 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
497 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000498 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000499 }
500}
501
502
503void HexagonInstrInfo::storeRegToAddr(
504 MachineFunction &MF, unsigned SrcReg,
505 bool isKill,
506 SmallVectorImpl<MachineOperand> &Addr,
507 const TargetRegisterClass *RC,
508 SmallVectorImpl<MachineInstr*> &NewMIs) const
509{
Craig Toppere55c5562012-02-07 02:50:20 +0000510 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000511}
512
513
514void HexagonInstrInfo::
515loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
516 unsigned DestReg, int FI,
517 const TargetRegisterClass *RC,
518 const TargetRegisterInfo *TRI) const {
519 DebugLoc DL = MBB.findDebugLoc(I);
520 MachineFunction &MF = *MBB.getParent();
521 MachineFrameInfo &MFI = *MF.getFrameInfo();
522 unsigned Align = MFI.getObjectAlignment(FI);
523
524 MachineMemOperand *MMO =
525 MF.getMachineMemOperand(
526 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
527 MachineMemOperand::MOLoad,
528 MFI.getObjectSize(FI),
529 Align);
Craig Topperc7242e02012-04-20 07:30:17 +0000530 if (RC == &Hexagon::IntRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000531 BuildMI(MBB, I, DL, get(Hexagon::LDriw), DestReg)
532 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000533 } else if (RC == &Hexagon::DoubleRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000534 BuildMI(MBB, I, DL, get(Hexagon::LDrid), DestReg)
535 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000536 } else if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
538 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
539 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000540 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000541 }
542}
543
544
545void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
546 SmallVectorImpl<MachineOperand> &Addr,
547 const TargetRegisterClass *RC,
548 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Craig Toppere55c5562012-02-07 02:50:20 +0000549 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000550}
551
552
553MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
554 MachineInstr* MI,
555 const SmallVectorImpl<unsigned> &Ops,
556 int FI) const {
557 // Hexagon_TODO: Implement.
558 return(0);
559}
560
Jyotsna Vermaadd82b32013-03-29 21:09:53 +0000561MachineInstr*
562HexagonInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
563 int FrameIx, uint64_t Offset,
564 const MDNode *MDPtr,
565 DebugLoc DL) const {
566 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Hexagon::DBG_VALUE))
567 .addImm(0).addImm(Offset).addMetadata(MDPtr);
568 return &*MIB;
569}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000570
571unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
572
573 MachineRegisterInfo &RegInfo = MF->getRegInfo();
574 const TargetRegisterClass *TRC;
Sirish Pande69295b82012-05-10 20:20:25 +0000575 if (VT == MVT::i1) {
Craig Topperc7242e02012-04-20 07:30:17 +0000576 TRC = &Hexagon::PredRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000577 } else if (VT == MVT::i32 || VT == MVT::f32) {
Craig Topperc7242e02012-04-20 07:30:17 +0000578 TRC = &Hexagon::IntRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000579 } else if (VT == MVT::i64 || VT == MVT::f64) {
Craig Topperc7242e02012-04-20 07:30:17 +0000580 TRC = &Hexagon::DoubleRegsRegClass;
Sirish Pande69295b82012-05-10 20:20:25 +0000581 } else {
Benjamin Kramerb6684012011-12-27 11:41:05 +0000582 llvm_unreachable("Cannot handle this register class");
Sirish Pande69295b82012-05-10 20:20:25 +0000583 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000584
585 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
586 return NewReg;
587}
588
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000589bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000590 // Constant extenders are allowed only for V4 and above.
591 if (!Subtarget.hasV4TOps())
592 return false;
593
594 const MCInstrDesc &MID = MI->getDesc();
595 const uint64_t F = MID.TSFlags;
596 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
597 return true;
598
599 // TODO: This is largely obsolete now. Will need to be removed
600 // in consecutive patches.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000601 switch(MI->getOpcode()) {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000602 // TFR_FI Remains a special case.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000603 case Hexagon::TFR_FI:
604 return true;
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000605 default:
606 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000607 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000608 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000609}
610
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000611// This returns true in two cases:
612// - The OP code itself indicates that this is an extended instruction.
613// - One of MOs has been marked with HMOTF_ConstExtended flag.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000614bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000615 // First check if this is permanently extended op code.
616 const uint64_t F = MI->getDesc().TSFlags;
617 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
618 return true;
619 // Use MO operand flags to determine if one of MI's operands
620 // has HMOTF_ConstExtended flag set.
621 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
622 E = MI->operands_end(); I != E; ++I) {
623 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Sirish Pande69295b82012-05-10 20:20:25 +0000624 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000625 }
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000626 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000627}
628
Jyotsna Verma84c47102013-05-06 18:49:23 +0000629bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
630 return MI->getDesc().isBranch();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000631}
632
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000633bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
634 switch (MI->getOpcode()) {
635 default: return false;
636 // Store Byte
637 case Hexagon::STrib_nv_V4:
638 case Hexagon::STrib_indexed_nv_V4:
639 case Hexagon::STrib_indexed_shl_nv_V4:
640 case Hexagon::STrib_shl_nv_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000641 case Hexagon::STb_GP_nv_V4:
642 case Hexagon::POST_STbri_nv_V4:
643 case Hexagon::STrib_cPt_nv_V4:
644 case Hexagon::STrib_cdnPt_nv_V4:
645 case Hexagon::STrib_cNotPt_nv_V4:
646 case Hexagon::STrib_cdnNotPt_nv_V4:
647 case Hexagon::STrib_indexed_cPt_nv_V4:
648 case Hexagon::STrib_indexed_cdnPt_nv_V4:
649 case Hexagon::STrib_indexed_cNotPt_nv_V4:
650 case Hexagon::STrib_indexed_cdnNotPt_nv_V4:
651 case Hexagon::STrib_indexed_shl_cPt_nv_V4:
652 case Hexagon::STrib_indexed_shl_cdnPt_nv_V4:
653 case Hexagon::STrib_indexed_shl_cNotPt_nv_V4:
654 case Hexagon::STrib_indexed_shl_cdnNotPt_nv_V4:
655 case Hexagon::POST_STbri_cPt_nv_V4:
656 case Hexagon::POST_STbri_cdnPt_nv_V4:
657 case Hexagon::POST_STbri_cNotPt_nv_V4:
658 case Hexagon::POST_STbri_cdnNotPt_nv_V4:
659 case Hexagon::STb_GP_cPt_nv_V4:
660 case Hexagon::STb_GP_cNotPt_nv_V4:
661 case Hexagon::STb_GP_cdnPt_nv_V4:
662 case Hexagon::STb_GP_cdnNotPt_nv_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000663 case Hexagon::STrib_abs_nv_V4:
664 case Hexagon::STrib_abs_cPt_nv_V4:
665 case Hexagon::STrib_abs_cdnPt_nv_V4:
666 case Hexagon::STrib_abs_cNotPt_nv_V4:
667 case Hexagon::STrib_abs_cdnNotPt_nv_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000668
669 // Store Halfword
670 case Hexagon::STrih_nv_V4:
671 case Hexagon::STrih_indexed_nv_V4:
672 case Hexagon::STrih_indexed_shl_nv_V4:
673 case Hexagon::STrih_shl_nv_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000674 case Hexagon::STh_GP_nv_V4:
675 case Hexagon::POST_SThri_nv_V4:
676 case Hexagon::STrih_cPt_nv_V4:
677 case Hexagon::STrih_cdnPt_nv_V4:
678 case Hexagon::STrih_cNotPt_nv_V4:
679 case Hexagon::STrih_cdnNotPt_nv_V4:
680 case Hexagon::STrih_indexed_cPt_nv_V4:
681 case Hexagon::STrih_indexed_cdnPt_nv_V4:
682 case Hexagon::STrih_indexed_cNotPt_nv_V4:
683 case Hexagon::STrih_indexed_cdnNotPt_nv_V4:
684 case Hexagon::STrih_indexed_shl_cPt_nv_V4:
685 case Hexagon::STrih_indexed_shl_cdnPt_nv_V4:
686 case Hexagon::STrih_indexed_shl_cNotPt_nv_V4:
687 case Hexagon::STrih_indexed_shl_cdnNotPt_nv_V4:
688 case Hexagon::POST_SThri_cPt_nv_V4:
689 case Hexagon::POST_SThri_cdnPt_nv_V4:
690 case Hexagon::POST_SThri_cNotPt_nv_V4:
691 case Hexagon::POST_SThri_cdnNotPt_nv_V4:
692 case Hexagon::STh_GP_cPt_nv_V4:
693 case Hexagon::STh_GP_cNotPt_nv_V4:
694 case Hexagon::STh_GP_cdnPt_nv_V4:
695 case Hexagon::STh_GP_cdnNotPt_nv_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000696 case Hexagon::STrih_abs_nv_V4:
697 case Hexagon::STrih_abs_cPt_nv_V4:
698 case Hexagon::STrih_abs_cdnPt_nv_V4:
699 case Hexagon::STrih_abs_cNotPt_nv_V4:
700 case Hexagon::STrih_abs_cdnNotPt_nv_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000701
702 // Store Word
703 case Hexagon::STriw_nv_V4:
704 case Hexagon::STriw_indexed_nv_V4:
705 case Hexagon::STriw_indexed_shl_nv_V4:
706 case Hexagon::STriw_shl_nv_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000707 case Hexagon::STw_GP_nv_V4:
708 case Hexagon::POST_STwri_nv_V4:
709 case Hexagon::STriw_cPt_nv_V4:
710 case Hexagon::STriw_cdnPt_nv_V4:
711 case Hexagon::STriw_cNotPt_nv_V4:
712 case Hexagon::STriw_cdnNotPt_nv_V4:
713 case Hexagon::STriw_indexed_cPt_nv_V4:
714 case Hexagon::STriw_indexed_cdnPt_nv_V4:
715 case Hexagon::STriw_indexed_cNotPt_nv_V4:
716 case Hexagon::STriw_indexed_cdnNotPt_nv_V4:
717 case Hexagon::STriw_indexed_shl_cPt_nv_V4:
718 case Hexagon::STriw_indexed_shl_cdnPt_nv_V4:
719 case Hexagon::STriw_indexed_shl_cNotPt_nv_V4:
720 case Hexagon::STriw_indexed_shl_cdnNotPt_nv_V4:
721 case Hexagon::POST_STwri_cPt_nv_V4:
722 case Hexagon::POST_STwri_cdnPt_nv_V4:
723 case Hexagon::POST_STwri_cNotPt_nv_V4:
724 case Hexagon::POST_STwri_cdnNotPt_nv_V4:
725 case Hexagon::STw_GP_cPt_nv_V4:
726 case Hexagon::STw_GP_cNotPt_nv_V4:
727 case Hexagon::STw_GP_cdnPt_nv_V4:
728 case Hexagon::STw_GP_cdnNotPt_nv_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000729 case Hexagon::STriw_abs_nv_V4:
730 case Hexagon::STriw_abs_cPt_nv_V4:
731 case Hexagon::STriw_abs_cdnPt_nv_V4:
732 case Hexagon::STriw_abs_cNotPt_nv_V4:
733 case Hexagon::STriw_abs_cdnNotPt_nv_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000734 return true;
735 }
736}
737
Jyotsna Vermaf1214a82013-03-05 18:51:42 +0000738bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
739 if (isNewValueJump(MI))
740 return true;
741
742 if (isNewValueStore(MI))
743 return true;
744
745 return false;
746}
747
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000748bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
749 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
750}
Andrew Trickd06df962012-02-01 22:13:57 +0000751
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
753 bool isPred = MI->getDesc().isPredicable();
754
755 if (!isPred)
756 return false;
757
758 const int Opc = MI->getOpcode();
759
760 switch(Opc) {
761 case Hexagon::TFRI:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000762 return isInt<12>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000763
764 case Hexagon::STrid:
765 case Hexagon::STrid_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000766 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000767
768 case Hexagon::STriw:
769 case Hexagon::STriw_indexed:
770 case Hexagon::STriw_nv_V4:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000771 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000772
773 case Hexagon::STrih:
774 case Hexagon::STrih_indexed:
775 case Hexagon::STrih_nv_V4:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000776 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000777
778 case Hexagon::STrib:
779 case Hexagon::STrib_indexed:
780 case Hexagon::STrib_nv_V4:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000781 return isUInt<6>(MI->getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000782
783 case Hexagon::LDrid:
784 case Hexagon::LDrid_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000785 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000786
787 case Hexagon::LDriw:
788 case Hexagon::LDriw_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000789 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000790
791 case Hexagon::LDrih:
792 case Hexagon::LDriuh:
793 case Hexagon::LDrih_indexed:
794 case Hexagon::LDriuh_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000795 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000796
797 case Hexagon::LDrib:
798 case Hexagon::LDriub:
799 case Hexagon::LDrib_indexed:
800 case Hexagon::LDriub_indexed:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000801 return isUInt<6>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000802
803 case Hexagon::POST_LDrid:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000804 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000805
806 case Hexagon::POST_LDriw:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000807 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000808
809 case Hexagon::POST_LDrih:
810 case Hexagon::POST_LDriuh:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000811 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000812
813 case Hexagon::POST_LDrib:
814 case Hexagon::POST_LDriub:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000815 return isInt<4>(MI->getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000816
817 case Hexagon::STrib_imm_V4:
818 case Hexagon::STrih_imm_V4:
819 case Hexagon::STriw_imm_V4:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000820 return (isUInt<6>(MI->getOperand(1).getImm()) &&
821 isInt<6>(MI->getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000822
823 case Hexagon::ADD_ri:
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000824 return isInt<8>(MI->getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000825
826 case Hexagon::ASLH:
827 case Hexagon::ASRH:
828 case Hexagon::SXTB:
829 case Hexagon::SXTH:
830 case Hexagon::ZXTB:
831 case Hexagon::ZXTH:
Sirish Pande8bb97452012-05-12 05:54:15 +0000832 return Subtarget.hasV4TOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000833 }
834
835 return true;
836}
837
Sirish Pande8bb97452012-05-12 05:54:15 +0000838// This function performs the following inversiones:
839//
840// cPt ---> cNotPt
841// cNotPt ---> cPt
842//
Sirish Pande30804c22012-02-15 18:52:27 +0000843unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
Jyotsna Verma84c47102013-05-06 18:49:23 +0000844 int InvPredOpcode;
845 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
846 : Hexagon::getTruePredOpcode(Opc);
847 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
848 return InvPredOpcode;
849
Sirish Pande30804c22012-02-15 18:52:27 +0000850 switch(Opc) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000851 default: llvm_unreachable("Unexpected predicated instruction");
Sirish Pande30804c22012-02-15 18:52:27 +0000852 case Hexagon::COMBINE_rr_cPt:
853 return Hexagon::COMBINE_rr_cNotPt;
854 case Hexagon::COMBINE_rr_cNotPt:
855 return Hexagon::COMBINE_rr_cPt;
856
Jyotsna Verma978e9722013-05-09 18:25:44 +0000857 // Dealloc_return.
Sirish Pande30804c22012-02-15 18:52:27 +0000858 case Hexagon::DEALLOC_RET_cPt_V4:
859 return Hexagon::DEALLOC_RET_cNotPt_V4;
860 case Hexagon::DEALLOC_RET_cNotPt_V4:
861 return Hexagon::DEALLOC_RET_cPt_V4;
Sirish Pande30804c22012-02-15 18:52:27 +0000862 }
863}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000864
Andrew Trickd06df962012-02-01 22:13:57 +0000865
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000866int HexagonInstrInfo::
867getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
Pranav Bhandarkar34b60182012-11-01 19:13:23 +0000868 enum Hexagon::PredSense inPredSense;
869 inPredSense = invertPredicate ? Hexagon::PredSense_false :
870 Hexagon::PredSense_true;
871 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
872 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
873 return CondOpcode;
874
875 // This switch case will be removed once all the instructions have been
876 // modified to use relation maps.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000877 switch(Opc) {
Sirish Pande69295b82012-05-10 20:20:25 +0000878 case Hexagon::TFRI_f:
879 return !invertPredicate ? Hexagon::TFRI_cPt_f :
880 Hexagon::TFRI_cNotPt_f;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000881 case Hexagon::COMBINE_rr:
882 return !invertPredicate ? Hexagon::COMBINE_rr_cPt :
883 Hexagon::COMBINE_rr_cNotPt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000884
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000885 // Word.
Jyotsna Verma978e9722013-05-09 18:25:44 +0000886 case Hexagon::STriw_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000887 return !invertPredicate ? Hexagon::STriw_cPt :
888 Hexagon::STriw_cNotPt;
Jyotsna Verma978e9722013-05-09 18:25:44 +0000889 case Hexagon::STriw_indexed_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000890 return !invertPredicate ? Hexagon::STriw_indexed_cPt :
891 Hexagon::STriw_indexed_cNotPt;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000892
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000893 // DEALLOC_RETURN.
894 case Hexagon::DEALLOC_RET_V4:
895 return !invertPredicate ? Hexagon::DEALLOC_RET_cPt_V4 :
896 Hexagon::DEALLOC_RET_cNotPt_V4;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000897 }
Benjamin Kramerb6684012011-12-27 11:41:05 +0000898 llvm_unreachable("Unexpected predicable instruction");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000899}
900
901
902bool HexagonInstrInfo::
903PredicateInstruction(MachineInstr *MI,
904 const SmallVectorImpl<MachineOperand> &Cond) const {
905 int Opc = MI->getOpcode();
906 assert (isPredicable(MI) && "Expected predicable instruction");
907 bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
908 (Cond[0].getImm() == 0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000909
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +0000910 // This will change MI's opcode to its predicate version.
911 // However, its operand list is still the old one, i.e. the
912 // non-predicate one.
913 MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
914
915 int oper = -1;
916 unsigned int GAIdx = 0;
917
918 // Indicates whether the current MI has a GlobalAddress operand
919 bool hasGAOpnd = false;
920 std::vector<MachineOperand> tmpOpnds;
921
922 // Indicates whether we need to shift operands to right.
923 bool needShift = true;
924
925 // The predicate is ALWAYS the FIRST input operand !!!
926 if (MI->getNumOperands() == 0) {
927 // The non-predicate version of MI does not take any operands,
928 // i.e. no outs and no ins. In this condition, the predicate
929 // operand will be directly placed at Operands[0]. No operand
930 // shift is needed.
931 // Example: BARRIER
932 needShift = false;
933 oper = -1;
934 }
935 else if ( MI->getOperand(MI->getNumOperands()-1).isReg()
936 && MI->getOperand(MI->getNumOperands()-1).isDef()
937 && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
938 // The non-predicate version of MI does not have any input operands.
939 // In this condition, we extend the length of Operands[] by one and
940 // copy the original last operand to the newly allocated slot.
941 // At this moment, it is just a place holder. Later, we will put
942 // predicate operand directly into it. No operand shift is needed.
943 // Example: r0=BARRIER (this is a faked insn used here for illustration)
944 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
945 needShift = false;
946 oper = MI->getNumOperands() - 2;
947 }
948 else {
949 // We need to right shift all input operands by one. Duplicate the
950 // last operand into the newly allocated slot.
951 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
952 }
953
954 if (needShift)
955 {
956 // Operands[ MI->getNumOperands() - 2 ] has been copied into
957 // Operands[ MI->getNumOperands() - 1 ], so we start from
958 // Operands[ MI->getNumOperands() - 3 ].
959 // oper is a signed int.
960 // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
961 for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
962 {
963 MachineOperand &MO = MI->getOperand(oper);
964
965 // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4] Opnd[5] Opnd[6] Opnd[7]
966 // <Def0> <Def1> <Use0> <Use1> <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
967 // /\~
968 // /||\~
969 // ||
970 // Predicate Operand here
971 if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
972 break;
973 }
974 if (MO.isReg()) {
975 MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
976 MO.isImplicit(), MO.isKill(),
977 MO.isDead(), MO.isUndef(),
978 MO.isDebug());
979 }
980 else if (MO.isImm()) {
981 MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
982 }
983 else if (MO.isGlobal()) {
984 // MI can not have more than one GlobalAddress operand.
985 assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
986
987 // There is no member function called "ChangeToGlobalAddress" in the
988 // MachineOperand class (not like "ChangeToRegister" and
989 // "ChangeToImmediate"). So we have to remove them from Operands[] list
990 // first, and then add them back after we have inserted the predicate
991 // operand. tmpOpnds[] is to remember these operands before we remove
992 // them.
993 tmpOpnds.push_back(MO);
994
995 // Operands[oper] is a GlobalAddress operand;
996 // Operands[oper+1] has been copied into Operands[oper+2];
997 hasGAOpnd = true;
998 GAIdx = oper;
999 continue;
1000 }
1001 else {
1002 assert(false && "Unexpected operand type");
1003 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001004 }
1005 }
1006
1007 int regPos = invertJump ? 1 : 0;
1008 MachineOperand PredMO = Cond[regPos];
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001009
1010 // [oper] now points to the last explicit Def. Predicate operand must be
1011 // located at [oper+1]. See diagram above.
1012 // This assumes that the predicate is always the first operand,
1013 // i.e. Operands[0+numResults], in the set of inputs
1014 // It is better to have an assert here to check this. But I don't know how
1015 // to write this assert because findFirstPredOperandIdx() would return -1
1016 if (oper < -1) oper = -1;
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +00001017
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001018 MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +00001019 PredMO.isImplicit(), false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001020 PredMO.isDead(), PredMO.isUndef(),
1021 PredMO.isDebug());
1022
Jyotsna Vermacd66c0a2013-05-01 21:27:30 +00001023 MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
1024 RegInfo.clearKillFlags(PredMO.getReg());
1025
Jyotsna Verma39f7a2b2013-02-12 16:06:23 +00001026 if (hasGAOpnd)
1027 {
1028 unsigned int i;
1029
1030 // Operands[GAIdx] is the original GlobalAddress operand, which is
1031 // already copied into tmpOpnds[0].
1032 // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
1033 // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
1034 // so we start from [GAIdx+2]
1035 for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
1036 tmpOpnds.push_back(MI->getOperand(i));
1037
1038 // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
1039 // It is very important that we always remove from the end of Operands[]
1040 // MI->getNumOperands() is at least 2 if program goes to here.
1041 for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
1042 MI->RemoveOperand(i);
1043
1044 for (i = 0; i < tmpOpnds.size(); ++i)
1045 MI->addOperand(tmpOpnds[i]);
1046 }
1047
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001048 return true;
1049}
1050
1051
1052bool
1053HexagonInstrInfo::
1054isProfitableToIfCvt(MachineBasicBlock &MBB,
Kay Tiong Khoof2949212012-06-13 15:53:04 +00001055 unsigned NumCycles,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001056 unsigned ExtraPredCycles,
1057 const BranchProbability &Probability) const {
1058 return true;
1059}
1060
1061
1062bool
1063HexagonInstrInfo::
1064isProfitableToIfCvt(MachineBasicBlock &TMBB,
1065 unsigned NumTCycles,
1066 unsigned ExtraTCycles,
1067 MachineBasicBlock &FMBB,
1068 unsigned NumFCycles,
1069 unsigned ExtraFCycles,
1070 const BranchProbability &Probability) const {
1071 return true;
1072}
1073
Jyotsna Verma84c47102013-05-06 18:49:23 +00001074// Returns true if an instruction is predicated irrespective of the predicate
1075// sense. For example, all of the following will return true.
1076// if (p0) R1 = add(R2, R3)
1077// if (!p0) R1 = add(R2, R3)
1078// if (p0.new) R1 = add(R2, R3)
1079// if (!p0.new) R1 = add(R2, R3)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001080bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
Brendon Cahoon6f358372012-02-08 18:25:47 +00001081 const uint64_t F = MI->getDesc().TSFlags;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001082
Brendon Cahoon6f358372012-02-08 18:25:47 +00001083 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001084}
1085
Jyotsna Verma84c47102013-05-06 18:49:23 +00001086bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
1087 const uint64_t F = get(Opcode).TSFlags;
1088
1089 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1090}
1091
1092bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
1093 const uint64_t F = MI->getDesc().TSFlags;
1094
1095 assert(isPredicated(MI));
1096 return (!((F >> HexagonII::PredicatedFalsePos) &
1097 HexagonII::PredicatedFalseMask));
1098}
1099
1100bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
1101 const uint64_t F = get(Opcode).TSFlags;
1102
1103 // Make sure that the instruction is predicated.
1104 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1105 return (!((F >> HexagonII::PredicatedFalsePos) &
1106 HexagonII::PredicatedFalseMask));
1107}
1108
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001109bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1110 const uint64_t F = MI->getDesc().TSFlags;
1111
1112 assert(isPredicated(MI));
1113 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1114}
1115
Jyotsna Verma84c47102013-05-06 18:49:23 +00001116bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1117 const uint64_t F = get(Opcode).TSFlags;
1118
1119 assert(isPredicated(Opcode));
1120 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1121}
1122
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001123bool
1124HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1125 std::vector<MachineOperand> &Pred) const {
1126 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1127 MachineOperand MO = MI->getOperand(oper);
1128 if (MO.isReg() && MO.isDef()) {
1129 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
Craig Topperc7242e02012-04-20 07:30:17 +00001130 if (RC == &Hexagon::PredRegsRegClass) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001131 Pred.push_back(MO);
1132 return true;
1133 }
1134 }
1135 }
1136 return false;
1137}
1138
1139
1140bool
1141HexagonInstrInfo::
1142SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1143 const SmallVectorImpl<MachineOperand> &Pred2) const {
1144 // TODO: Fix this
1145 return false;
1146}
1147
1148
1149//
1150// We indicate that we want to reverse the branch by
1151// inserting a 0 at the beginning of the Cond vector.
1152//
1153bool HexagonInstrInfo::
1154ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1155 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1156 Cond.erase(Cond.begin());
1157 } else {
1158 Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1159 }
1160 return false;
1161}
1162
1163
1164bool HexagonInstrInfo::
1165isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1166 const BranchProbability &Probability) const {
1167 return (NumInstrs <= 4);
1168}
1169
1170bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1171 switch (MI->getOpcode()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001172 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001173 case Hexagon::DEALLOC_RET_V4 :
1174 case Hexagon::DEALLOC_RET_cPt_V4 :
1175 case Hexagon::DEALLOC_RET_cNotPt_V4 :
1176 case Hexagon::DEALLOC_RET_cdnPnt_V4 :
1177 case Hexagon::DEALLOC_RET_cNotdnPnt_V4 :
1178 case Hexagon::DEALLOC_RET_cdnPt_V4 :
1179 case Hexagon::DEALLOC_RET_cNotdnPt_V4 :
1180 return true;
1181 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001182}
1183
1184
1185bool HexagonInstrInfo::
1186isValidOffset(const int Opcode, const int Offset) const {
1187 // This function is to check whether the "Offset" is in the correct range of
1188 // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1189 // inserted to calculate the final address. Due to this reason, the function
1190 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00001191 // We used to assert if the offset was not properly aligned, however,
1192 // there are cases where a misaligned pointer recast can cause this
1193 // problem, and we need to allow for it. The front end warns of such
1194 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001195
1196 switch(Opcode) {
1197
1198 case Hexagon::LDriw:
Jyotsna Verma9b60c1d2013-01-17 18:42:37 +00001199 case Hexagon::LDriw_indexed:
Sirish Pande69295b82012-05-10 20:20:25 +00001200 case Hexagon::LDriw_f:
Jyotsna Verma9b60c1d2013-01-17 18:42:37 +00001201 case Hexagon::STriw_indexed:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001202 case Hexagon::STriw:
Sirish Pande69295b82012-05-10 20:20:25 +00001203 case Hexagon::STriw_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001204 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1205 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1206
1207 case Hexagon::LDrid:
Jyotsna Verma9b60c1d2013-01-17 18:42:37 +00001208 case Hexagon::LDrid_indexed:
Sirish Pande69295b82012-05-10 20:20:25 +00001209 case Hexagon::LDrid_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001210 case Hexagon::STrid:
Jyotsna Verma9b60c1d2013-01-17 18:42:37 +00001211 case Hexagon::STrid_indexed:
Sirish Pande69295b82012-05-10 20:20:25 +00001212 case Hexagon::STrid_f:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001213 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1214 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1215
1216 case Hexagon::LDrih:
1217 case Hexagon::LDriuh:
1218 case Hexagon::STrih:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001219 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1220 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1221
1222 case Hexagon::LDrib:
1223 case Hexagon::STrib:
1224 case Hexagon::LDriub:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001225 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1226 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1227
1228 case Hexagon::ADD_ri:
1229 case Hexagon::TFR_FI:
1230 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1231 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1232
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001233 case Hexagon::MemOPw_ADDi_V4 :
1234 case Hexagon::MemOPw_SUBi_V4 :
1235 case Hexagon::MemOPw_ADDr_V4 :
1236 case Hexagon::MemOPw_SUBr_V4 :
1237 case Hexagon::MemOPw_ANDr_V4 :
1238 case Hexagon::MemOPw_ORr_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001239 return (0 <= Offset && Offset <= 255);
1240
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001241 case Hexagon::MemOPh_ADDi_V4 :
1242 case Hexagon::MemOPh_SUBi_V4 :
1243 case Hexagon::MemOPh_ADDr_V4 :
1244 case Hexagon::MemOPh_SUBr_V4 :
1245 case Hexagon::MemOPh_ANDr_V4 :
1246 case Hexagon::MemOPh_ORr_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001247 return (0 <= Offset && Offset <= 127);
1248
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001249 case Hexagon::MemOPb_ADDi_V4 :
1250 case Hexagon::MemOPb_SUBi_V4 :
1251 case Hexagon::MemOPb_ADDr_V4 :
1252 case Hexagon::MemOPb_SUBr_V4 :
1253 case Hexagon::MemOPb_ANDr_V4 :
1254 case Hexagon::MemOPb_ORr_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001255 return (0 <= Offset && Offset <= 63);
1256
1257 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1258 // any size. Later pass knows how to handle it.
1259 case Hexagon::STriw_pred:
1260 case Hexagon::LDriw_pred:
1261 return true;
1262
Krzysztof Parzyszek9a278f12013-02-11 21:37:55 +00001263 case Hexagon::LOOP0_i:
1264 return isUInt<10>(Offset);
1265
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001266 // INLINEASM is very special.
1267 case Hexagon::INLINEASM:
1268 return true;
1269 }
1270
Benjamin Kramerb6684012011-12-27 11:41:05 +00001271 llvm_unreachable("No offset range is defined for this opcode. "
1272 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001273}
1274
1275
1276//
1277// Check if the Offset is a valid auto-inc imm by Load/Store Type.
1278//
1279bool HexagonInstrInfo::
1280isValidAutoIncImm(const EVT VT, const int Offset) const {
1281
1282 if (VT == MVT::i64) {
1283 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1284 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1285 (Offset & 0x7) == 0);
1286 }
1287 if (VT == MVT::i32) {
1288 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1289 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1290 (Offset & 0x3) == 0);
1291 }
1292 if (VT == MVT::i16) {
1293 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1294 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1295 (Offset & 0x1) == 0);
1296 }
1297 if (VT == MVT::i8) {
1298 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1299 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1300 }
Craig Toppere55c5562012-02-07 02:50:20 +00001301 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001302}
1303
1304
1305bool HexagonInstrInfo::
1306isMemOp(const MachineInstr *MI) const {
1307 switch (MI->getOpcode())
1308 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001309 default: return false;
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001310 case Hexagon::MemOPw_ADDi_V4 :
1311 case Hexagon::MemOPw_SUBi_V4 :
1312 case Hexagon::MemOPw_ADDr_V4 :
1313 case Hexagon::MemOPw_SUBr_V4 :
1314 case Hexagon::MemOPw_ANDr_V4 :
1315 case Hexagon::MemOPw_ORr_V4 :
1316 case Hexagon::MemOPh_ADDi_V4 :
1317 case Hexagon::MemOPh_SUBi_V4 :
1318 case Hexagon::MemOPh_ADDr_V4 :
1319 case Hexagon::MemOPh_SUBr_V4 :
1320 case Hexagon::MemOPh_ANDr_V4 :
1321 case Hexagon::MemOPh_ORr_V4 :
1322 case Hexagon::MemOPb_ADDi_V4 :
1323 case Hexagon::MemOPb_SUBi_V4 :
1324 case Hexagon::MemOPb_ADDr_V4 :
1325 case Hexagon::MemOPb_SUBr_V4 :
1326 case Hexagon::MemOPb_ANDr_V4 :
1327 case Hexagon::MemOPb_ORr_V4 :
1328 case Hexagon::MemOPb_SETBITi_V4:
1329 case Hexagon::MemOPh_SETBITi_V4:
1330 case Hexagon::MemOPw_SETBITi_V4:
1331 case Hexagon::MemOPb_CLRBITi_V4:
1332 case Hexagon::MemOPh_CLRBITi_V4:
1333 case Hexagon::MemOPw_CLRBITi_V4:
1334 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001335 }
Jyotsna Vermafdc660b2013-03-22 18:41:34 +00001336 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001337}
1338
1339
1340bool HexagonInstrInfo::
1341isSpillPredRegOp(const MachineInstr *MI) const {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001342 switch (MI->getOpcode()) {
1343 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001344 case Hexagon::STriw_pred :
1345 case Hexagon::LDriw_pred :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001346 return true;
Sirish Pande2c7bf002012-04-23 17:49:28 +00001347 }
Sirish Pande4bd20c52012-05-12 05:10:30 +00001348}
1349
1350bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1351 switch (MI->getOpcode()) {
Sirish Pande8bb97452012-05-12 05:54:15 +00001352 default: return false;
Sirish Pande4bd20c52012-05-12 05:10:30 +00001353 case Hexagon::CMPEQrr:
1354 case Hexagon::CMPEQri:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001355 case Hexagon::CMPGTrr:
1356 case Hexagon::CMPGTri:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001357 case Hexagon::CMPGTUrr:
1358 case Hexagon::CMPGTUri:
Sirish Pande4bd20c52012-05-12 05:10:30 +00001359 return true;
Sirish Pande4bd20c52012-05-12 05:10:30 +00001360 }
Sirish Pande2c7bf002012-04-23 17:49:28 +00001361}
1362
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001363bool HexagonInstrInfo::
1364isConditionalTransfer (const MachineInstr *MI) const {
1365 switch (MI->getOpcode()) {
1366 default: return false;
1367 case Hexagon::TFR_cPt:
1368 case Hexagon::TFR_cNotPt:
1369 case Hexagon::TFRI_cPt:
1370 case Hexagon::TFRI_cNotPt:
1371 case Hexagon::TFR_cdnPt:
1372 case Hexagon::TFR_cdnNotPt:
1373 case Hexagon::TFRI_cdnPt:
1374 case Hexagon::TFRI_cdnNotPt:
1375 return true;
1376 }
1377}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001378
1379bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
1380 const HexagonRegisterInfo& QRI = getRegisterInfo();
1381 switch (MI->getOpcode())
1382 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001383 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001384 case Hexagon::ADD_ri_cPt:
1385 case Hexagon::ADD_ri_cNotPt:
1386 case Hexagon::ADD_rr_cPt:
1387 case Hexagon::ADD_rr_cNotPt:
1388 case Hexagon::XOR_rr_cPt:
1389 case Hexagon::XOR_rr_cNotPt:
1390 case Hexagon::AND_rr_cPt:
1391 case Hexagon::AND_rr_cNotPt:
1392 case Hexagon::OR_rr_cPt:
1393 case Hexagon::OR_rr_cNotPt:
1394 case Hexagon::SUB_rr_cPt:
1395 case Hexagon::SUB_rr_cNotPt:
1396 case Hexagon::COMBINE_rr_cPt:
1397 case Hexagon::COMBINE_rr_cNotPt:
1398 return true;
1399 case Hexagon::ASLH_cPt_V4:
1400 case Hexagon::ASLH_cNotPt_V4:
1401 case Hexagon::ASRH_cPt_V4:
1402 case Hexagon::ASRH_cNotPt_V4:
1403 case Hexagon::SXTB_cPt_V4:
1404 case Hexagon::SXTB_cNotPt_V4:
1405 case Hexagon::SXTH_cPt_V4:
1406 case Hexagon::SXTH_cNotPt_V4:
1407 case Hexagon::ZXTB_cPt_V4:
1408 case Hexagon::ZXTB_cNotPt_V4:
1409 case Hexagon::ZXTH_cPt_V4:
1410 case Hexagon::ZXTH_cNotPt_V4:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001411 return QRI.Subtarget.hasV4TOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001412 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001413}
1414
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001415bool HexagonInstrInfo::
1416isConditionalLoad (const MachineInstr* MI) const {
1417 const HexagonRegisterInfo& QRI = getRegisterInfo();
1418 switch (MI->getOpcode())
1419 {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001420 default: return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001421 case Hexagon::LDrid_cPt :
1422 case Hexagon::LDrid_cNotPt :
1423 case Hexagon::LDrid_indexed_cPt :
1424 case Hexagon::LDrid_indexed_cNotPt :
1425 case Hexagon::LDriw_cPt :
1426 case Hexagon::LDriw_cNotPt :
1427 case Hexagon::LDriw_indexed_cPt :
1428 case Hexagon::LDriw_indexed_cNotPt :
1429 case Hexagon::LDrih_cPt :
1430 case Hexagon::LDrih_cNotPt :
1431 case Hexagon::LDrih_indexed_cPt :
1432 case Hexagon::LDrih_indexed_cNotPt :
1433 case Hexagon::LDrib_cPt :
1434 case Hexagon::LDrib_cNotPt :
1435 case Hexagon::LDrib_indexed_cPt :
1436 case Hexagon::LDrib_indexed_cNotPt :
1437 case Hexagon::LDriuh_cPt :
1438 case Hexagon::LDriuh_cNotPt :
1439 case Hexagon::LDriuh_indexed_cPt :
1440 case Hexagon::LDriuh_indexed_cNotPt :
1441 case Hexagon::LDriub_cPt :
1442 case Hexagon::LDriub_cNotPt :
1443 case Hexagon::LDriub_indexed_cPt :
1444 case Hexagon::LDriub_indexed_cNotPt :
1445 return true;
1446 case Hexagon::POST_LDrid_cPt :
1447 case Hexagon::POST_LDrid_cNotPt :
1448 case Hexagon::POST_LDriw_cPt :
1449 case Hexagon::POST_LDriw_cNotPt :
1450 case Hexagon::POST_LDrih_cPt :
1451 case Hexagon::POST_LDrih_cNotPt :
1452 case Hexagon::POST_LDrib_cPt :
1453 case Hexagon::POST_LDrib_cNotPt :
1454 case Hexagon::POST_LDriuh_cPt :
1455 case Hexagon::POST_LDriuh_cNotPt :
1456 case Hexagon::POST_LDriub_cPt :
1457 case Hexagon::POST_LDriub_cNotPt :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001458 return QRI.Subtarget.hasV4TOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001459 case Hexagon::LDrid_indexed_shl_cPt_V4 :
1460 case Hexagon::LDrid_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001461 case Hexagon::LDrib_indexed_shl_cPt_V4 :
1462 case Hexagon::LDrib_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001463 case Hexagon::LDriub_indexed_shl_cPt_V4 :
1464 case Hexagon::LDriub_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001465 case Hexagon::LDrih_indexed_shl_cPt_V4 :
1466 case Hexagon::LDrih_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001467 case Hexagon::LDriuh_indexed_shl_cPt_V4 :
1468 case Hexagon::LDriuh_indexed_shl_cNotPt_V4 :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001469 case Hexagon::LDriw_indexed_shl_cPt_V4 :
1470 case Hexagon::LDriw_indexed_shl_cNotPt_V4 :
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001471 return QRI.Subtarget.hasV4TOps();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001472 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001473}
Andrew Trickd06df962012-02-01 22:13:57 +00001474
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001475// Returns true if an instruction is a conditional store.
1476//
1477// Note: It doesn't include conditional new-value stores as they can't be
1478// converted to .new predicate.
1479//
1480// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1481// ^ ^
1482// / \ (not OK. it will cause new-value store to be
1483// / X conditional on p0.new while R2 producer is
1484// / \ on p0)
1485// / \.
1486// p.new store p.old NV store
1487// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1488// ^ ^
1489// \ /
1490// \ /
1491// \ /
1492// p.old store
1493// [if (p0)memw(R0+#0)=R2]
1494//
1495// The above diagram shows the steps involoved in the conversion of a predicated
1496// store instruction to its .new predicated new-value form.
1497//
1498// The following set of instructions further explains the scenario where
1499// conditional new-value store becomes invalid when promoted to .new predicate
1500// form.
1501//
1502// { 1) if (p0) r0 = add(r1, r2)
1503// 2) p0 = cmp.eq(r3, #0) }
1504//
1505// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1506// the first two instructions because in instr 1, r0 is conditional on old value
1507// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1508// is not valid for new-value stores.
1509bool HexagonInstrInfo::
1510isConditionalStore (const MachineInstr* MI) const {
1511 const HexagonRegisterInfo& QRI = getRegisterInfo();
1512 switch (MI->getOpcode())
1513 {
1514 default: return false;
1515 case Hexagon::STrib_imm_cPt_V4 :
1516 case Hexagon::STrib_imm_cNotPt_V4 :
1517 case Hexagon::STrib_indexed_shl_cPt_V4 :
1518 case Hexagon::STrib_indexed_shl_cNotPt_V4 :
1519 case Hexagon::STrib_cPt :
1520 case Hexagon::STrib_cNotPt :
1521 case Hexagon::POST_STbri_cPt :
1522 case Hexagon::POST_STbri_cNotPt :
1523 case Hexagon::STrid_indexed_cPt :
1524 case Hexagon::STrid_indexed_cNotPt :
1525 case Hexagon::STrid_indexed_shl_cPt_V4 :
1526 case Hexagon::POST_STdri_cPt :
1527 case Hexagon::POST_STdri_cNotPt :
1528 case Hexagon::STrih_cPt :
1529 case Hexagon::STrih_cNotPt :
1530 case Hexagon::STrih_indexed_cPt :
1531 case Hexagon::STrih_indexed_cNotPt :
1532 case Hexagon::STrih_imm_cPt_V4 :
1533 case Hexagon::STrih_imm_cNotPt_V4 :
1534 case Hexagon::STrih_indexed_shl_cPt_V4 :
1535 case Hexagon::STrih_indexed_shl_cNotPt_V4 :
1536 case Hexagon::POST_SThri_cPt :
1537 case Hexagon::POST_SThri_cNotPt :
1538 case Hexagon::STriw_cPt :
1539 case Hexagon::STriw_cNotPt :
1540 case Hexagon::STriw_indexed_cPt :
1541 case Hexagon::STriw_indexed_cNotPt :
1542 case Hexagon::STriw_imm_cPt_V4 :
1543 case Hexagon::STriw_imm_cNotPt_V4 :
1544 case Hexagon::STriw_indexed_shl_cPt_V4 :
1545 case Hexagon::STriw_indexed_shl_cNotPt_V4 :
1546 case Hexagon::POST_STwri_cPt :
1547 case Hexagon::POST_STwri_cNotPt :
1548 return QRI.Subtarget.hasV4TOps();
1549
1550 // V4 global address store before promoting to dot new.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001551 case Hexagon::STd_GP_cPt_V4 :
1552 case Hexagon::STd_GP_cNotPt_V4 :
1553 case Hexagon::STb_GP_cPt_V4 :
1554 case Hexagon::STb_GP_cNotPt_V4 :
1555 case Hexagon::STh_GP_cPt_V4 :
1556 case Hexagon::STh_GP_cNotPt_V4 :
1557 case Hexagon::STw_GP_cPt_V4 :
1558 case Hexagon::STw_GP_cNotPt_V4 :
1559 return QRI.Subtarget.hasV4TOps();
1560
1561 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1562 // from the "Conditional Store" list. Because a predicated new value store
1563 // would NOT be promoted to a double dot new store. See diagram below:
1564 // This function returns yes for those stores that are predicated but not
1565 // yet promoted to predicate dot new instructions.
1566 //
1567 // +---------------------+
1568 // /-----| if (p0) memw(..)=r0 |---------\~
1569 // || +---------------------+ ||
1570 // promote || /\ /\ || promote
1571 // || /||\ /||\ ||
1572 // \||/ demote || \||/
1573 // \/ || || \/
1574 // +-------------------------+ || +-------------------------+
1575 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1576 // +-------------------------+ || +-------------------------+
1577 // || || ||
1578 // || demote \||/
1579 // promote || \/ NOT possible
1580 // || || /\~
1581 // \||/ || /||\~
1582 // \/ || ||
1583 // +-----------------------------+
1584 // | if (p0.new) memw(..)=r0.new |
1585 // +-----------------------------+
1586 // Double Dot New Store
1587 //
1588 }
1589}
1590
Jyotsna Verma84c47102013-05-06 18:49:23 +00001591
1592bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1593 if (isNewValue(MI) && isBranch(MI))
1594 return true;
1595 return false;
1596}
1597
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001598bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1599 return (getAddrMode(MI) == HexagonII::PostInc);
1600}
1601
Jyotsna Verma84c47102013-05-06 18:49:23 +00001602bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1603 const uint64_t F = MI->getDesc().TSFlags;
1604 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1605}
1606
Jyotsna Vermaa46059b2013-03-28 19:44:04 +00001607// Returns true, if any one of the operands is a dot new
1608// insn, whether it is predicated dot new or register dot new.
1609bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1610 return (isNewValueInst(MI) ||
1611 (isPredicated(MI) && isPredicatedNew(MI)));
1612}
1613
Jyotsna Verma00681dc2013-05-09 19:16:07 +00001614// Return .new predicate version for an instruction.
1615int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1616 const MachineBranchProbabilityInfo
1617 *MBPI) const {
1618
1619 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1620 if (NewOpcode >= 0) // Valid predicate new instruction
1621 return NewOpcode;
1622
1623 switch (MI->getOpcode()) {
1624 default: llvm_unreachable("Unknown .new type");
1625 // Condtional Jumps
1626 case Hexagon::JMP_t:
1627 case Hexagon::JMP_f:
1628 return getDotNewPredJumpOp(MI, MBPI);
1629
1630 case Hexagon::JMPR_t:
1631 return Hexagon::JMPR_tnew_tV3;
1632
1633 case Hexagon::JMPR_f:
1634 return Hexagon::JMPR_fnew_tV3;
1635
1636 case Hexagon::JMPret_t:
1637 return Hexagon::JMPret_tnew_tV3;
1638
1639 case Hexagon::JMPret_f:
1640 return Hexagon::JMPret_fnew_tV3;
1641
1642
1643 // Conditional combine
1644 case Hexagon::COMBINE_rr_cPt :
1645 return Hexagon::COMBINE_rr_cdnPt;
1646 case Hexagon::COMBINE_rr_cNotPt :
1647 return Hexagon::COMBINE_rr_cdnNotPt;
1648 }
1649}
1650
1651
Jyotsna Verma84256432013-03-01 17:37:13 +00001652unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1653 const uint64_t F = MI->getDesc().TSFlags;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001654
Jyotsna Verma84256432013-03-01 17:37:13 +00001655 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1656}
1657
1658/// immediateExtend - Changes the instruction in place to one using an immediate
1659/// extender.
1660void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1661 assert((isExtendable(MI)||isConstExtended(MI)) &&
1662 "Instruction must be extendable");
1663 // Find which operand is extendable.
1664 short ExtOpNum = getCExtOpNum(MI);
1665 MachineOperand &MO = MI->getOperand(ExtOpNum);
1666 // This needs to be something we understand.
1667 assert((MO.isMBB() || MO.isImm()) &&
1668 "Branch with unknown extendable field type");
1669 // Mark given operand as extended.
1670 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1671}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001672
Andrew Trickd06df962012-02-01 22:13:57 +00001673DFAPacketizer *HexagonInstrInfo::
1674CreateTargetScheduleState(const TargetMachine *TM,
1675 const ScheduleDAG *DAG) const {
1676 const InstrItineraryData *II = TM->getInstrItineraryData();
1677 return TM->getSubtarget<HexagonGenSubtargetInfo>().createDFAPacketizer(II);
1678}
1679
1680bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1681 const MachineBasicBlock *MBB,
1682 const MachineFunction &MF) const {
1683 // Debug info is never a scheduling boundary. It's necessary to be explicit
1684 // due to the special treatment of IT instructions below, otherwise a
1685 // dbg_value followed by an IT will result in the IT instruction being
1686 // considered a scheduling hazard, which is wrong. It should be the actual
1687 // instruction preceding the dbg_value instruction(s), just like it is
1688 // when debug info is not present.
1689 if (MI->isDebugValue())
1690 return false;
1691
1692 // Terminators and labels can't be scheduled around.
1693 if (MI->getDesc().isTerminator() || MI->isLabel() || MI->isInlineAsm())
1694 return true;
1695
1696 return false;
1697}
Jyotsna Verma84256432013-03-01 17:37:13 +00001698
1699bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
1700
1701 // Constant extenders are allowed only for V4 and above.
1702 if (!Subtarget.hasV4TOps())
1703 return false;
1704
1705 const uint64_t F = MI->getDesc().TSFlags;
1706 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1707 if (isExtended) // Instruction must be extended.
1708 return true;
1709
1710 unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1711 & HexagonII::ExtendableMask;
1712 if (!isExtendable)
1713 return false;
1714
1715 short ExtOpNum = getCExtOpNum(MI);
1716 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1717 // Use MO operand flags to determine if MO
1718 // has the HMOTF_ConstExtended flag set.
1719 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1720 return true;
1721 // If this is a Machine BB address we are talking about, and it is
1722 // not marked as extended, say so.
1723 if (MO.isMBB())
1724 return false;
1725
1726 // We could be using an instruction with an extendable immediate and shoehorn
1727 // a global address into it. If it is a global address it will be constant
1728 // extended. We do this for COMBINE.
1729 // We currently only handle isGlobal() because it is the only kind of
1730 // object we are going to end up with here for now.
1731 // In the future we probably should add isSymbol(), etc.
1732 if (MO.isGlobal() || MO.isSymbol())
1733 return true;
1734
1735 // If the extendable operand is not 'Immediate' type, the instruction should
1736 // have 'isExtended' flag set.
1737 assert(MO.isImm() && "Extendable operand must be Immediate type");
1738
1739 int MinValue = getMinValue(MI);
1740 int MaxValue = getMaxValue(MI);
1741 int ImmValue = MO.getImm();
1742
1743 return (ImmValue < MinValue || ImmValue > MaxValue);
1744}
1745
Jyotsna Verma1d297502013-05-02 15:39:30 +00001746// Returns the opcode to use when converting MI, which is a conditional jump,
1747// into a conditional instruction which uses the .new value of the predicate.
1748// We also use branch probabilities to add a hint to the jump.
1749int
1750HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1751 const
1752 MachineBranchProbabilityInfo *MBPI) const {
1753
1754 // We assume that block can have at most two successors.
1755 bool taken = false;
1756 MachineBasicBlock *Src = MI->getParent();
1757 MachineOperand *BrTarget = &MI->getOperand(1);
1758 MachineBasicBlock *Dst = BrTarget->getMBB();
1759
1760 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1761 if (Prediction >= BranchProbability(1,2))
1762 taken = true;
1763
1764 switch (MI->getOpcode()) {
1765 case Hexagon::JMP_t:
1766 return taken ? Hexagon::JMP_tnew_t : Hexagon::JMP_tnew_nt;
1767 case Hexagon::JMP_f:
1768 return taken ? Hexagon::JMP_fnew_t : Hexagon::JMP_fnew_nt;
1769
1770 default:
1771 llvm_unreachable("Unexpected jump instruction.");
1772 }
1773}
Jyotsna Verma84256432013-03-01 17:37:13 +00001774// Returns true if a particular operand is extendable for an instruction.
1775bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1776 unsigned short OperandNum) const {
1777 // Constant extenders are allowed only for V4 and above.
1778 if (!Subtarget.hasV4TOps())
1779 return false;
1780
1781 const uint64_t F = MI->getDesc().TSFlags;
1782
1783 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1784 == OperandNum;
1785}
1786
1787// Returns Operand Index for the constant extended instruction.
1788unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1789 const uint64_t F = MI->getDesc().TSFlags;
1790 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1791}
1792
1793// Returns the min value that doesn't need to be extended.
1794int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1795 const uint64_t F = MI->getDesc().TSFlags;
1796 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1797 & HexagonII::ExtentSignedMask;
1798 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1799 & HexagonII::ExtentBitsMask;
1800
1801 if (isSigned) // if value is signed
1802 return -1 << (bits - 1);
1803 else
1804 return 0;
1805}
1806
1807// Returns the max value that doesn't need to be extended.
1808int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1809 const uint64_t F = MI->getDesc().TSFlags;
1810 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1811 & HexagonII::ExtentSignedMask;
1812 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1813 & HexagonII::ExtentBitsMask;
1814
1815 if (isSigned) // if value is signed
1816 return ~(-1 << (bits - 1));
1817 else
1818 return ~(-1 << bits);
1819}
1820
1821// Returns true if an instruction can be converted into a non-extended
1822// equivalent instruction.
1823bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1824
1825 short NonExtOpcode;
1826 // Check if the instruction has a register form that uses register in place
1827 // of the extended operand, if so return that as the non-extended form.
1828 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1829 return true;
1830
1831 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1832 // Check addressing mode and retreive non-ext equivalent instruction.
1833
1834 switch (getAddrMode(MI)) {
1835 case HexagonII::Absolute :
1836 // Load/store with absolute addressing mode can be converted into
1837 // base+offset mode.
1838 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1839 break;
1840 case HexagonII::BaseImmOffset :
1841 // Load/store with base+offset addressing mode can be converted into
1842 // base+register offset addressing mode. However left shift operand should
1843 // be set to 0.
1844 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1845 break;
1846 default:
1847 return false;
1848 }
1849 if (NonExtOpcode < 0)
1850 return false;
1851 return true;
1852 }
1853 return false;
1854}
1855
1856// Returns opcode of the non-extended equivalent instruction.
1857short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1858
1859 // Check if the instruction has a register form that uses register in place
1860 // of the extended operand, if so return that as the non-extended form.
1861 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1862 if (NonExtOpcode >= 0)
1863 return NonExtOpcode;
1864
1865 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1866 // Check addressing mode and retreive non-ext equivalent instruction.
1867 switch (getAddrMode(MI)) {
1868 case HexagonII::Absolute :
1869 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1870 case HexagonII::BaseImmOffset :
1871 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1872 default:
1873 return -1;
1874 }
1875 }
1876 return -1;
1877}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001878
1879bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
1880 return (Opcode == Hexagon::JMP_t) ||
1881 (Opcode == Hexagon::JMP_f) ||
1882 (Opcode == Hexagon::JMP_tnew_t) ||
1883 (Opcode == Hexagon::JMP_fnew_t) ||
1884 (Opcode == Hexagon::JMP_tnew_nt) ||
1885 (Opcode == Hexagon::JMP_fnew_nt);
1886}
1887
1888bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
1889 return (Opcode == Hexagon::JMP_f) ||
1890 (Opcode == Hexagon::JMP_fnew_t) ||
1891 (Opcode == Hexagon::JMP_fnew_nt);
1892}