1 //===- ARMBaseInstrInfo.h - ARM Base Instruction Information ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Base ARM implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #ifndef ARMBASEINSTRUCTIONINFO_H
15 #define ARMBASEINSTRUCTIONINFO_H
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/Target/TargetInstrInfo.h"
23 class ARMBaseRegisterInfo;
25 /// ARMII - This namespace holds all of the target specific flags that
26 /// instruction info tracks.
30 //===------------------------------------------------------------------===//
33 //===------------------------------------------------------------------===//
34 // This four-bit field describes the addressing mode used.
47 AddrModeT1_s = 10, // i8 * 4 for pc and sp relative data
51 AddrModeT2_pc = 14, // +/- i12 for pc relative data
52 AddrModeT2_i8s4 = 15, // i8 * 4
54 // Size* - Flags to keep track of the size of an instruction.
56 SizeMask = 7 << SizeShift,
57 SizeSpecial = 1, // 0 byte pseudo or special case.
62 // 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.
65 IndexModeMask = 3 << IndexModeShift,
70 //===------------------------------------------------------------------===//
71 // Instruction encoding formats.
74 FormMask = 0x3f << FormShift,
76 // Pseudo instructions
77 Pseudo = 0 << FormShift,
79 // Multiply instructions
80 MulFrm = 1 << FormShift,
82 // Branch instructions
83 BrFrm = 2 << FormShift,
84 BrMiscFrm = 3 << FormShift,
86 // Data Processing instructions
87 DPFrm = 4 << FormShift,
88 DPSoRegFrm = 5 << FormShift,
91 LdFrm = 6 << FormShift,
92 StFrm = 7 << FormShift,
93 LdMiscFrm = 8 << FormShift,
94 StMiscFrm = 9 << FormShift,
95 LdStMulFrm = 10 << FormShift,
97 LdStExFrm = 11 << FormShift,
99 // Miscellaneous arithmetic instructions
100 ArithMiscFrm = 12 << FormShift,
101 SatFrm = 13 << FormShift,
103 // Extend instructions
104 ExtFrm = 14 << FormShift,
107 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,
119 ThumbFrm = 25 << FormShift,
121 // Miscelleaneous format
122 MiscFrm = 26 << FormShift,
125 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,
141 //===------------------------------------------------------------------===//
144 // UnaryDP - Indicates this is a unary data processing instruction, i.e.
145 // it doesn't have a Rn operand.
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,
152 //===------------------------------------------------------------------===//
155 DomainMask = 3 << DomainShift,
156 DomainGeneral = 0 << DomainShift,
157 DomainVFP = 1 << DomainShift,
158 DomainNEON = 2 << DomainShift,
160 //===------------------------------------------------------------------===//
161 // Field shifts - such shifts are used to set field while generating
162 // machine instructions.
185 /// Target Operand Flag enum.
187 //===------------------------------------------------------------------===//
188 // ARM Specific MachineOperand flags.
192 /// MO_LO16 - On a symbol operand, this represents a relocation containing
193 /// lower 16 bit of the address. Used only via movw instruction.
196 /// MO_HI16 - On a symbol operand, this represents a relocation containing
197 /// higher 16 bit of the address. Used only via movt instruction.
202 class ARMBaseInstrInfo : public TargetInstrInfoImpl {
203 const ARMSubtarget &Subtarget;
205 // Can be only subclassed.
206 explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
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;
212 virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
213 MachineBasicBlock::iterator &MBBI,
214 LiveVariables *LV) const;
216 virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
217 const ARMSubtarget &getSubtarget() const { return Subtarget; }
219 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
220 MachineBasicBlock::iterator MI,
221 const std::vector<CalleeSavedInfo> &CSI,
222 const TargetRegisterInfo *TRI) const;
225 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
226 MachineBasicBlock *&FBB,
227 SmallVectorImpl<MachineOperand> &Cond,
228 bool AllowModify = false) const;
229 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
230 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
231 MachineBasicBlock *FBB,
232 const SmallVectorImpl<MachineOperand> &Cond,
236 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
238 // Predication support.
239 bool isPredicated(const MachineInstr *MI) const {
240 int PIdx = MI->findFirstPredOperandIdx();
241 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
244 ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
245 int PIdx = MI->findFirstPredOperandIdx();
246 return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
251 bool PredicateInstruction(MachineInstr *MI,
252 const SmallVectorImpl<MachineOperand> &Pred) const;
255 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
256 const SmallVectorImpl<MachineOperand> &Pred2) const;
258 virtual bool DefinesPredicate(MachineInstr *MI,
259 std::vector<MachineOperand> &Pred) const;
261 virtual bool isPredicable(MachineInstr *MI) const;
263 /// GetInstSize - Returns the size of the specified MachineInstr.
265 virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
267 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
268 int &FrameIndex) const;
269 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
270 int &FrameIndex) const;
272 virtual void copyPhysReg(MachineBasicBlock &MBB,
273 MachineBasicBlock::iterator I, DebugLoc DL,
274 unsigned DestReg, unsigned SrcReg,
277 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
278 MachineBasicBlock::iterator MBBI,
279 unsigned SrcReg, bool isKill, int FrameIndex,
280 const TargetRegisterClass *RC,
281 const TargetRegisterInfo *TRI) const;
283 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
284 MachineBasicBlock::iterator MBBI,
285 unsigned DestReg, int FrameIndex,
286 const TargetRegisterClass *RC,
287 const TargetRegisterInfo *TRI) const;
289 virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
295 virtual void reMaterialize(MachineBasicBlock &MBB,
296 MachineBasicBlock::iterator MI,
297 unsigned DestReg, unsigned SubIdx,
298 const MachineInstr *Orig,
299 const TargetRegisterInfo &TRI) const;
301 MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
303 virtual bool produceSameValue(const MachineInstr *MI0,
304 const MachineInstr *MI1) const;
306 /// 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;
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;
326 virtual bool isSchedulingBoundary(const MachineInstr *MI,
327 const MachineBasicBlock *MBB,
328 const MachineFunction &MF) const;
330 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
331 unsigned NumInstrs) const;
333 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,unsigned NumT,
334 MachineBasicBlock &FMBB,unsigned NumF) const;
336 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
337 unsigned NumInstrs) const {
338 return NumInstrs && NumInstrs == 1;
341 /// 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,
345 int &CmpValue) const;
347 /// ConvertToSetZeroFlag - Convert the instruction to set the zero flag so
348 /// that we can remove a "comparison with zero".
349 virtual bool ConvertToSetZeroFlag(MachineInstr *Instr,
350 MachineInstr *CmpInstr) const;
354 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
355 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
359 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
360 return MIB.addReg(0);
364 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
365 bool isDead = false) {
366 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
370 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
371 return MIB.addReg(0);
375 bool isUncondBranchOpcode(int Opc) {
376 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
380 bool isCondBranchOpcode(int Opc) {
381 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
385 bool isJumpTableBranchOpcode(int Opc) {
386 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
387 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
391 bool isIndirectBranchOpcode(int Opc) {
392 return Opc == ARM::BRIND || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
395 /// getInstrPredicate - If instruction is predicated, returns its predicate
396 /// condition, otherwise returns AL. It also returns the condition code
397 /// register by reference.
398 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
400 int getMatchingCondBranchOpcode(int Opc);
402 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
403 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
405 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
406 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
407 unsigned DestReg, unsigned BaseReg, int NumBytes,
408 ARMCC::CondCodes Pred, unsigned PredReg,
409 const ARMBaseInstrInfo &TII);
411 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
412 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
413 unsigned DestReg, unsigned BaseReg, int NumBytes,
414 ARMCC::CondCodes Pred, unsigned PredReg,
415 const ARMBaseInstrInfo &TII);
418 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
419 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
420 /// offset could not be handled directly in MI, and return the left-over
421 /// portion by reference.
422 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
423 unsigned FrameReg, int &Offset,
424 const ARMBaseInstrInfo &TII);
426 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
427 unsigned FrameReg, int &Offset,
428 const ARMBaseInstrInfo &TII);
430 } // End llvm namespace