blob: 207125e2443952cc09d54fa05bc42b9a840ffaef [file] [log] [blame]
Jim Grosbach31c24bf2009-11-07 22:00:39 +00001//===- ARMBaseInstrInfo.h - ARM Base Instruction Information ----*- C++ -*-===//
David Goodwin334c2642009-07-08 16:09:28 +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 Base ARM implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef ARMBASEINSTRUCTIONINFO_H
15#define ARMBASEINSTRUCTIONINFO_H
16
David Goodwin334c2642009-07-08 16:09:28 +000017#include "ARM.h"
Anton Korobeynikovb8e9ac82009-07-16 23:26:06 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/Target/TargetInstrInfo.h"
David Goodwin334c2642009-07-08 16:09:28 +000020
21namespace llvm {
Chris Lattner4dbbe342010-07-20 21:17:29 +000022 class ARMSubtarget;
23 class ARMBaseRegisterInfo;
David Goodwin334c2642009-07-08 16:09:28 +000024
25/// ARMII - This namespace holds all of the target specific flags that
26/// instruction info tracks.
27///
28namespace ARMII {
29 enum {
30 //===------------------------------------------------------------------===//
31 // Instruction Flags.
32
33 //===------------------------------------------------------------------===//
34 // This four-bit field describes the addressing mode used.
35
36 AddrModeMask = 0xf,
37 AddrModeNone = 0,
38 AddrMode1 = 1,
39 AddrMode2 = 2,
40 AddrMode3 = 3,
41 AddrMode4 = 4,
42 AddrMode5 = 5,
43 AddrMode6 = 6,
44 AddrModeT1_1 = 7,
45 AddrModeT1_2 = 8,
46 AddrModeT1_4 = 9,
47 AddrModeT1_s = 10, // i8 * 4 for pc and sp relative data
48 AddrModeT2_i12 = 11,
49 AddrModeT2_i8 = 12,
50 AddrModeT2_so = 13,
51 AddrModeT2_pc = 14, // +/- i12 for pc relative data
52 AddrModeT2_i8s4 = 15, // i8 * 4
53
54 // Size* - Flags to keep track of the size of an instruction.
55 SizeShift = 4,
56 SizeMask = 7 << SizeShift,
57 SizeSpecial = 1, // 0 byte pseudo or special case.
58 Size8Bytes = 2,
59 Size4Bytes = 3,
60 Size2Bytes = 4,
61
Bob Wilsonbffb5b32010-03-13 07:34:35 +000062 // IndexMode - Unindex, pre-indexed, or post-indexed are valid for load
63 // and store ops only. Generic "updating" flag is used for ld/st multiple.
David Goodwin334c2642009-07-08 16:09:28 +000064 IndexModeShift = 7,
65 IndexModeMask = 3 << IndexModeShift,
66 IndexModePre = 1,
67 IndexModePost = 2,
Bob Wilsonbffb5b32010-03-13 07:34:35 +000068 IndexModeUpd = 3,
David Goodwin334c2642009-07-08 16:09:28 +000069
70 //===------------------------------------------------------------------===//
71 // Instruction encoding formats.
72 //
73 FormShift = 9,
74 FormMask = 0x3f << FormShift,
75
76 // Pseudo instructions
77 Pseudo = 0 << FormShift,
78
79 // Multiply instructions
80 MulFrm = 1 << FormShift,
81
82 // Branch instructions
83 BrFrm = 2 << FormShift,
84 BrMiscFrm = 3 << FormShift,
85
86 // Data Processing instructions
87 DPFrm = 4 << FormShift,
88 DPSoRegFrm = 5 << FormShift,
89
90 // Load and Store
91 LdFrm = 6 << FormShift,
92 StFrm = 7 << FormShift,
93 LdMiscFrm = 8 << FormShift,
94 StMiscFrm = 9 << FormShift,
95 LdStMulFrm = 10 << FormShift,
96
Johnny Chen81f04d52010-03-19 17:39:00 +000097 LdStExFrm = 11 << FormShift,
Jim Grosbach5278eb82009-12-11 01:42:04 +000098
David Goodwin334c2642009-07-08 16:09:28 +000099 // Miscellaneous arithmetic instructions
Johnny Chen81f04d52010-03-19 17:39:00 +0000100 ArithMiscFrm = 12 << FormShift,
Bob Wilson9a1c1892010-08-11 00:01:18 +0000101 SatFrm = 13 << FormShift,
David Goodwin334c2642009-07-08 16:09:28 +0000102
103 // Extend instructions
Bob Wilson9a1c1892010-08-11 00:01:18 +0000104 ExtFrm = 14 << FormShift,
David Goodwin334c2642009-07-08 16:09:28 +0000105
106 // VFP formats
Bob Wilson9a1c1892010-08-11 00:01:18 +0000107 VFPUnaryFrm = 15 << FormShift,
108 VFPBinaryFrm = 16 << FormShift,
109 VFPConv1Frm = 17 << FormShift,
110 VFPConv2Frm = 18 << FormShift,
111 VFPConv3Frm = 19 << FormShift,
112 VFPConv4Frm = 20 << FormShift,
113 VFPConv5Frm = 21 << FormShift,
114 VFPLdStFrm = 22 << FormShift,
115 VFPLdStMulFrm = 23 << FormShift,
116 VFPMiscFrm = 24 << FormShift,
David Goodwin334c2642009-07-08 16:09:28 +0000117
118 // Thumb format
Bob Wilson9a1c1892010-08-11 00:01:18 +0000119 ThumbFrm = 25 << FormShift,
David Goodwin334c2642009-07-08 16:09:28 +0000120
Bob Wilson26532632010-06-25 23:45:37 +0000121 // Miscelleaneous format
Bob Wilson9a1c1892010-08-11 00:01:18 +0000122 MiscFrm = 26 << FormShift,
Bob Wilson26532632010-06-25 23:45:37 +0000123
Bob Wilson1a913ed2010-06-11 21:34:50 +0000124 // NEON formats
Bob Wilson9a1c1892010-08-11 00:01:18 +0000125 NGetLnFrm = 27 << FormShift,
126 NSetLnFrm = 28 << FormShift,
127 NDupFrm = 29 << FormShift,
128 NLdStFrm = 30 << FormShift,
129 N1RegModImmFrm= 31 << FormShift,
130 N2RegFrm = 32 << FormShift,
131 NVCVTFrm = 33 << FormShift,
132 NVDupLnFrm = 34 << FormShift,
133 N2RegVShLFrm = 35 << FormShift,
134 N2RegVShRFrm = 36 << FormShift,
135 N3RegFrm = 37 << FormShift,
136 N3RegVShFrm = 38 << FormShift,
137 NVExtFrm = 39 << FormShift,
138 NVMulSLFrm = 40 << FormShift,
139 NVTBLFrm = 41 << FormShift,
David Goodwin334c2642009-07-08 16:09:28 +0000140
141 //===------------------------------------------------------------------===//
142 // Misc flags.
143
144 // UnaryDP - Indicates this is a unary data processing instruction, i.e.
145 // it doesn't have a Rn operand.
146 UnaryDP = 1 << 15,
147
148 // Xform16Bit - Indicates this Thumb2 instruction may be transformed into
149 // a 16-bit Thumb instruction if certain conditions are met.
150 Xform16Bit = 1 << 16,
151
152 //===------------------------------------------------------------------===//
Anton Korobeynikovf95215f2009-11-02 00:10:38 +0000153 // Code domain.
154 DomainShift = 17,
155 DomainMask = 3 << DomainShift,
156 DomainGeneral = 0 << DomainShift,
157 DomainVFP = 1 << DomainShift,
158 DomainNEON = 2 << DomainShift,
159
160 //===------------------------------------------------------------------===//
David Goodwin334c2642009-07-08 16:09:28 +0000161 // Field shifts - such shifts are used to set field while generating
162 // machine instructions.
163 M_BitShift = 5,
164 ShiftImmShift = 5,
165 ShiftShift = 7,
166 N_BitShift = 7,
167 ImmHiShift = 8,
168 SoRotImmShift = 8,
169 RegRsShift = 8,
170 ExtRotImmShift = 10,
171 RegRdLoShift = 12,
172 RegRdShift = 12,
173 RegRdHiShift = 16,
174 RegRnShift = 16,
175 S_BitShift = 20,
176 W_BitShift = 21,
177 AM3_I_BitShift = 22,
178 D_BitShift = 22,
179 U_BitShift = 23,
180 P_BitShift = 24,
181 I_BitShift = 25,
182 CondShift = 28
183 };
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000184
185 /// Target Operand Flag enum.
186 enum TOF {
187 //===------------------------------------------------------------------===//
188 // ARM Specific MachineOperand flags.
189
190 MO_NO_FLAG,
191
192 /// MO_LO16 - On a symbol operand, this represents a relocation containing
193 /// lower 16 bit of the address. Used only via movw instruction.
194 MO_LO16,
195
196 /// MO_HI16 - On a symbol operand, this represents a relocation containing
197 /// higher 16 bit of the address. Used only via movt instruction.
198 MO_HI16
199 };
Evan Chengb46aaa32009-07-19 19:16:46 +0000200}
201
David Goodwin334c2642009-07-08 16:09:28 +0000202class ARMBaseInstrInfo : public TargetInstrInfoImpl {
Chris Lattner4dbbe342010-07-20 21:17:29 +0000203 const ARMSubtarget &Subtarget;
David Goodwin334c2642009-07-08 16:09:28 +0000204protected:
205 // Can be only subclassed.
Anton Korobeynikovf95215f2009-11-02 00:10:38 +0000206 explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
David Goodwin334c2642009-07-08 16:09:28 +0000207public:
208 // Return the non-pre/post incrementing version of 'Opc'. Return 0
209 // if there is not such an opcode.
210 virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
211
David Goodwin334c2642009-07-08 16:09:28 +0000212 virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
213 MachineBasicBlock::iterator &MBBI,
214 LiveVariables *LV) const;
215
216 virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
Anton Korobeynikovf95215f2009-11-02 00:10:38 +0000217 const ARMSubtarget &getSubtarget() const { return Subtarget; }
David Goodwin334c2642009-07-08 16:09:28 +0000218
Evan Cheng2457f2c2010-05-22 01:47:14 +0000219 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
220 MachineBasicBlock::iterator MI,
221 const std::vector<CalleeSavedInfo> &CSI,
222 const TargetRegisterInfo *TRI) const;
223
David Goodwin334c2642009-07-08 16:09:28 +0000224 // Branch analysis.
225 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
226 MachineBasicBlock *&FBB,
227 SmallVectorImpl<MachineOperand> &Cond,
Chris Lattner20628752010-07-22 21:27:00 +0000228 bool AllowModify = false) const;
David Goodwin334c2642009-07-08 16:09:28 +0000229 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
230 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
231 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000232 const SmallVectorImpl<MachineOperand> &Cond,
233 DebugLoc DL) const;
David Goodwin334c2642009-07-08 16:09:28 +0000234
235 virtual
236 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
237
238 // Predication support.
Evan Chengab331502009-07-10 01:38:27 +0000239 bool isPredicated(const MachineInstr *MI) const {
240 int PIdx = MI->findFirstPredOperandIdx();
241 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
242 }
David Goodwin334c2642009-07-08 16:09:28 +0000243
244 ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
245 int PIdx = MI->findFirstPredOperandIdx();
246 return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
247 : ARMCC::AL;
248 }
249
250 virtual
251 bool PredicateInstruction(MachineInstr *MI,
252 const SmallVectorImpl<MachineOperand> &Pred) const;
253
254 virtual
255 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
256 const SmallVectorImpl<MachineOperand> &Pred2) const;
257
258 virtual bool DefinesPredicate(MachineInstr *MI,
259 std::vector<MachineOperand> &Pred) const;
260
Evan Chengac0869d2009-11-21 06:21:52 +0000261 virtual bool isPredicable(MachineInstr *MI) const;
262
David Goodwin334c2642009-07-08 16:09:28 +0000263 /// GetInstSize - Returns the size of the specified MachineInstr.
264 ///
265 virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
266
David Goodwin334c2642009-07-08 16:09:28 +0000267 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
268 int &FrameIndex) const;
269 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
270 int &FrameIndex) const;
271
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000272 virtual void copyPhysReg(MachineBasicBlock &MBB,
273 MachineBasicBlock::iterator I, DebugLoc DL,
274 unsigned DestReg, unsigned SrcReg,
275 bool KillSrc) const;
Evan Cheng5732ca02009-07-27 03:14:20 +0000276
David Goodwin334c2642009-07-08 16:09:28 +0000277 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
278 MachineBasicBlock::iterator MBBI,
279 unsigned SrcReg, bool isKill, int FrameIndex,
Evan Cheng746ad692010-05-06 19:06:44 +0000280 const TargetRegisterClass *RC,
281 const TargetRegisterInfo *TRI) const;
David Goodwin334c2642009-07-08 16:09:28 +0000282
David Goodwin334c2642009-07-08 16:09:28 +0000283 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
284 MachineBasicBlock::iterator MBBI,
285 unsigned DestReg, int FrameIndex,
Evan Cheng746ad692010-05-06 19:06:44 +0000286 const TargetRegisterClass *RC,
287 const TargetRegisterInfo *TRI) const;
David Goodwin334c2642009-07-08 16:09:28 +0000288
Evan Cheng62b50652010-04-26 07:39:25 +0000289 virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000290 int FrameIx,
Evan Cheng62b50652010-04-26 07:39:25 +0000291 uint64_t Offset,
292 const MDNode *MDPtr,
293 DebugLoc DL) const;
294
Evan Chengfdc83402009-11-08 00:15:23 +0000295 virtual void reMaterialize(MachineBasicBlock &MBB,
296 MachineBasicBlock::iterator MI,
297 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +0000298 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000299 const TargetRegisterInfo &TRI) const;
Evan Chengfdc83402009-11-08 00:15:23 +0000300
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000301 MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
302
Evan Cheng506049f2010-03-03 01:44:33 +0000303 virtual bool produceSameValue(const MachineInstr *MI0,
304 const MachineInstr *MI1) const;
Evan Cheng86050dc2010-06-18 23:09:54 +0000305
Bill Wendling4b722102010-06-23 23:00:16 +0000306 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
307 /// determine if two loads are loading from the same base address. It should
308 /// only return true if the base pointers are the same and the only
309 /// differences between the two addresses is the offset. It also returns the
310 /// offsets by reference.
311 virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
312 int64_t &Offset1, int64_t &Offset2)const;
313
314 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
315 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
316 /// be scheduled togther. On some targets if two loads are loading from
317 /// addresses in the same cache line, it's better if they are scheduled
318 /// together. This function takes two integers that represent the load offsets
319 /// from the common base address. It returns true if it decides it's desirable
320 /// to schedule the two loads together. "NumLoads" is the number of loads that
321 /// have already been scheduled after Load1.
322 virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
323 int64_t Offset1, int64_t Offset2,
324 unsigned NumLoads) const;
325
Evan Cheng86050dc2010-06-18 23:09:54 +0000326 virtual bool isSchedulingBoundary(const MachineInstr *MI,
327 const MachineBasicBlock *MBB,
328 const MachineFunction &MF) const;
Evan Cheng13151432010-06-25 22:42:03 +0000329
330 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
331 unsigned NumInstrs) const;
332
333 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,unsigned NumT,
334 MachineBasicBlock &FMBB,unsigned NumF) const;
335
336 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
337 unsigned NumInstrs) const {
338 return NumInstrs && NumInstrs == 1;
339 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +0000340
Bill Wendlingc98af332010-08-08 05:04:59 +0000341 /// AnalyzeCompare - For a comparison instruction, return the source register
342 /// in SrcReg and the value it compares against in CmpValue. Return true if
343 /// the comparison instruction can be analyzed.
344 virtual bool AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +0000345 int &CmpValue) const;
346
Bill Wendlinga6556862010-09-11 00:13:50 +0000347 /// OptimizeCompareInstr - Convert the instruction to set the zero flag so
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +0000348 /// that we can remove a "comparison with zero".
Bill Wendlinga6556862010-09-11 00:13:50 +0000349 virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
Bill Wendling36656612010-09-10 23:46:12 +0000350 int CmpValue,
Bill Wendling220e2402010-09-10 21:55:43 +0000351 MachineBasicBlock::iterator &MII) const;
Evan Cheng5f54ce32010-09-09 18:18:55 +0000352
353 virtual unsigned getNumMicroOps(const MachineInstr *MI,
Evan Cheng3ef1c872010-09-10 01:29:16 +0000354 const InstrItineraryData *ItinData) const;
David Goodwin334c2642009-07-08 16:09:28 +0000355};
Evan Cheng6495f632009-07-28 05:48:47 +0000356
357static inline
358const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
359 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
David Goodwin334c2642009-07-08 16:09:28 +0000360}
361
Evan Cheng6495f632009-07-28 05:48:47 +0000362static inline
363const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
364 return MIB.addReg(0);
365}
366
367static inline
Evan Chenge8af1f92009-08-10 02:37:24 +0000368const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
369 bool isDead = false) {
370 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
Evan Cheng6495f632009-07-28 05:48:47 +0000371}
372
373static inline
Evan Chengbc9b7542009-08-15 07:59:10 +0000374const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
375 return MIB.addReg(0);
376}
377
378static inline
Evan Cheng6495f632009-07-28 05:48:47 +0000379bool isUncondBranchOpcode(int Opc) {
380 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
381}
382
383static inline
384bool isCondBranchOpcode(int Opc) {
385 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
386}
387
388static inline
389bool isJumpTableBranchOpcode(int Opc) {
390 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
391 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
392}
393
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000394static inline
395bool isIndirectBranchOpcode(int Opc) {
Anton Korobeynikovce7bf1c2010-03-06 19:39:36 +0000396 return Opc == ARM::BRIND || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000397}
398
Evan Cheng8fb90362009-08-08 03:20:32 +0000399/// getInstrPredicate - If instruction is predicated, returns its predicate
400/// condition, otherwise returns AL. It also returns the condition code
401/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +0000402ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
Evan Cheng8fb90362009-08-08 03:20:32 +0000403
Evan Cheng6495f632009-07-28 05:48:47 +0000404int getMatchingCondBranchOpcode(int Opc);
405
406/// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
407/// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
408/// code.
409void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
410 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
411 unsigned DestReg, unsigned BaseReg, int NumBytes,
412 ARMCC::CondCodes Pred, unsigned PredReg,
413 const ARMBaseInstrInfo &TII);
414
415void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
416 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
417 unsigned DestReg, unsigned BaseReg, int NumBytes,
418 ARMCC::CondCodes Pred, unsigned PredReg,
419 const ARMBaseInstrInfo &TII);
420
421
Jim Grosbach764ab522009-08-11 15:33:49 +0000422/// rewriteARMFrameIndex / rewriteT2FrameIndex -
Evan Chengcdbb3f52009-08-27 01:23:50 +0000423/// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
424/// offset could not be handled directly in MI, and return the left-over
425/// portion by reference.
426bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
427 unsigned FrameReg, int &Offset,
428 const ARMBaseInstrInfo &TII);
Evan Cheng6495f632009-07-28 05:48:47 +0000429
Evan Chengcdbb3f52009-08-27 01:23:50 +0000430bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
431 unsigned FrameReg, int &Offset,
432 const ARMBaseInstrInfo &TII);
Evan Cheng6495f632009-07-28 05:48:47 +0000433
434} // End llvm namespace
435
David Goodwin334c2642009-07-08 16:09:28 +0000436#endif