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"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallSet.h"
25 class ARMBaseRegisterInfo;
27 /// ARMII - This namespace holds all of the target specific flags that
28 /// instruction info tracks.
32 //===------------------------------------------------------------------===//
35 //===------------------------------------------------------------------===//
36 // This four-bit field describes the addressing mode used.
49 AddrModeT1_s = 10, // i8 * 4 for pc and sp relative data
53 AddrModeT2_pc = 14, // +/- i12 for pc relative data
54 AddrModeT2_i8s4 = 15, // i8 * 4
57 // Size* - Flags to keep track of the size of an instruction.
59 SizeMask = 7 << SizeShift,
60 SizeSpecial = 1, // 0 byte pseudo or special case.
65 // IndexMode - Unindex, pre-indexed, or post-indexed are valid for load
66 // and store ops only. Generic "updating" flag is used for ld/st multiple.
68 IndexModeMask = 3 << IndexModeShift,
73 //===------------------------------------------------------------------===//
74 // Instruction encoding formats.
77 FormMask = 0x3f << FormShift,
79 // Pseudo instructions
80 Pseudo = 0 << FormShift,
82 // Multiply instructions
83 MulFrm = 1 << FormShift,
85 // Branch instructions
86 BrFrm = 2 << FormShift,
87 BrMiscFrm = 3 << FormShift,
89 // Data Processing instructions
90 DPFrm = 4 << FormShift,
91 DPSoRegFrm = 5 << FormShift,
94 LdFrm = 6 << FormShift,
95 StFrm = 7 << FormShift,
96 LdMiscFrm = 8 << FormShift,
97 StMiscFrm = 9 << FormShift,
98 LdStMulFrm = 10 << FormShift,
100 LdStExFrm = 11 << FormShift,
102 // Miscellaneous arithmetic instructions
103 ArithMiscFrm = 12 << FormShift,
104 SatFrm = 13 << FormShift,
106 // Extend instructions
107 ExtFrm = 14 << FormShift,
110 VFPUnaryFrm = 15 << FormShift,
111 VFPBinaryFrm = 16 << FormShift,
112 VFPConv1Frm = 17 << FormShift,
113 VFPConv2Frm = 18 << FormShift,
114 VFPConv3Frm = 19 << FormShift,
115 VFPConv4Frm = 20 << FormShift,
116 VFPConv5Frm = 21 << FormShift,
117 VFPLdStFrm = 22 << FormShift,
118 VFPLdStMulFrm = 23 << FormShift,
119 VFPMiscFrm = 24 << FormShift,
122 ThumbFrm = 25 << FormShift,
124 // Miscelleaneous format
125 MiscFrm = 26 << FormShift,
128 NGetLnFrm = 27 << FormShift,
129 NSetLnFrm = 28 << FormShift,
130 NDupFrm = 29 << FormShift,
131 NLdStFrm = 30 << FormShift,
132 N1RegModImmFrm= 31 << FormShift,
133 N2RegFrm = 32 << FormShift,
134 NVCVTFrm = 33 << FormShift,
135 NVDupLnFrm = 34 << FormShift,
136 N2RegVShLFrm = 35 << FormShift,
137 N2RegVShRFrm = 36 << FormShift,
138 N3RegFrm = 37 << FormShift,
139 N3RegVShFrm = 38 << FormShift,
140 NVExtFrm = 39 << FormShift,
141 NVMulSLFrm = 40 << FormShift,
142 NVTBLFrm = 41 << FormShift,
144 //===------------------------------------------------------------------===//
147 // UnaryDP - Indicates this is a unary data processing instruction, i.e.
148 // it doesn't have a Rn operand.
151 // Xform16Bit - Indicates this Thumb2 instruction may be transformed into
152 // a 16-bit Thumb instruction if certain conditions are met.
153 Xform16Bit = 1 << 17,
155 //===------------------------------------------------------------------===//
158 DomainMask = 3 << DomainShift,
159 DomainGeneral = 0 << DomainShift,
160 DomainVFP = 1 << DomainShift,
161 DomainNEON = 2 << DomainShift,
163 //===------------------------------------------------------------------===//
164 // Field shifts - such shifts are used to set field while generating
165 // machine instructions.
167 // FIXME: This list will need adjusting/fixing as the MC code emitter
168 // takes shape and the ARMCodeEmitter.cpp bits go away.
194 class ARMBaseInstrInfo : public TargetInstrInfoImpl {
195 const ARMSubtarget &Subtarget;
198 // Can be only subclassed.
199 explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
202 // Return the non-pre/post incrementing version of 'Opc'. Return 0
203 // if there is not such an opcode.
204 virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
206 virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
207 MachineBasicBlock::iterator &MBBI,
208 LiveVariables *LV) const;
210 virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
211 const ARMSubtarget &getSubtarget() const { return Subtarget; }
213 ScheduleHazardRecognizer *
214 CreateTargetHazardRecognizer(const TargetMachine *TM,
215 const ScheduleDAG *DAG) const;
217 ScheduleHazardRecognizer *
218 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
219 const ScheduleDAG *DAG) const;
222 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
223 MachineBasicBlock *&FBB,
224 SmallVectorImpl<MachineOperand> &Cond,
225 bool AllowModify = false) const;
226 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
227 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
228 MachineBasicBlock *FBB,
229 const SmallVectorImpl<MachineOperand> &Cond,
233 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
235 // Predication support.
236 bool isPredicated(const MachineInstr *MI) const {
237 int PIdx = MI->findFirstPredOperandIdx();
238 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
241 ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
242 int PIdx = MI->findFirstPredOperandIdx();
243 return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
248 bool PredicateInstruction(MachineInstr *MI,
249 const SmallVectorImpl<MachineOperand> &Pred) const;
252 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
253 const SmallVectorImpl<MachineOperand> &Pred2) const;
255 virtual bool DefinesPredicate(MachineInstr *MI,
256 std::vector<MachineOperand> &Pred) const;
258 virtual bool isPredicable(MachineInstr *MI) const;
260 /// GetInstSize - Returns the size of the specified MachineInstr.
262 virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
264 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
265 int &FrameIndex) const;
266 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
267 int &FrameIndex) const;
269 virtual void copyPhysReg(MachineBasicBlock &MBB,
270 MachineBasicBlock::iterator I, DebugLoc DL,
271 unsigned DestReg, unsigned SrcReg,
274 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
275 MachineBasicBlock::iterator MBBI,
276 unsigned SrcReg, bool isKill, int FrameIndex,
277 const TargetRegisterClass *RC,
278 const TargetRegisterInfo *TRI) const;
280 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
281 MachineBasicBlock::iterator MBBI,
282 unsigned DestReg, int FrameIndex,
283 const TargetRegisterClass *RC,
284 const TargetRegisterInfo *TRI) const;
286 virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
292 virtual void reMaterialize(MachineBasicBlock &MBB,
293 MachineBasicBlock::iterator MI,
294 unsigned DestReg, unsigned SubIdx,
295 const MachineInstr *Orig,
296 const TargetRegisterInfo &TRI) const;
298 MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
300 virtual bool produceSameValue(const MachineInstr *MI0,
301 const MachineInstr *MI1) const;
303 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
304 /// determine if two loads are loading from the same base address. It should
305 /// only return true if the base pointers are the same and the only
306 /// differences between the two addresses is the offset. It also returns the
307 /// offsets by reference.
308 virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
309 int64_t &Offset1, int64_t &Offset2)const;
311 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
312 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
313 /// be scheduled togther. On some targets if two loads are loading from
314 /// addresses in the same cache line, it's better if they are scheduled
315 /// together. This function takes two integers that represent the load offsets
316 /// from the common base address. It returns true if it decides it's desirable
317 /// to schedule the two loads together. "NumLoads" is the number of loads that
318 /// have already been scheduled after Load1.
319 virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
320 int64_t Offset1, int64_t Offset2,
321 unsigned NumLoads) const;
323 virtual bool isSchedulingBoundary(const MachineInstr *MI,
324 const MachineBasicBlock *MBB,
325 const MachineFunction &MF) const;
327 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
328 unsigned NumCyles, unsigned ExtraPredCycles,
329 float Prob, float Confidence) const;
331 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
332 unsigned NumT, unsigned ExtraT,
333 MachineBasicBlock &FMBB,
334 unsigned NumF, unsigned ExtraF,
335 float Probability, float Confidence) const;
337 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
340 float Confidence) const {
341 return NumCyles == 1;
344 /// AnalyzeCompare - For a comparison instruction, return the source register
345 /// in SrcReg and the value it compares against in CmpValue. Return true if
346 /// the comparison instruction can be analyzed.
347 virtual bool AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
348 int &CmpMask, int &CmpValue) const;
350 /// OptimizeCompareInstr - Convert the instruction to set the zero flag so
351 /// that we can remove a "comparison with zero".
352 virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
353 int CmpMask, int CmpValue,
354 const MachineRegisterInfo *MRI) const;
356 /// FoldImmediate - 'Reg' is known to be defined by a move immediate
357 /// instruction, try to fold the immediate into the use instruction.
358 virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
359 unsigned Reg, MachineRegisterInfo *MRI) const;
361 virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
362 const MachineInstr *MI) const;
365 int getOperandLatency(const InstrItineraryData *ItinData,
366 const MachineInstr *DefMI, unsigned DefIdx,
367 const MachineInstr *UseMI, unsigned UseIdx) const;
369 int getOperandLatency(const InstrItineraryData *ItinData,
370 SDNode *DefNode, unsigned DefIdx,
371 SDNode *UseNode, unsigned UseIdx) const;
373 int getVLDMDefCycle(const InstrItineraryData *ItinData,
374 const TargetInstrDesc &DefTID,
376 unsigned DefIdx, unsigned DefAlign) const;
377 int getLDMDefCycle(const InstrItineraryData *ItinData,
378 const TargetInstrDesc &DefTID,
380 unsigned DefIdx, unsigned DefAlign) const;
381 int getVSTMUseCycle(const InstrItineraryData *ItinData,
382 const TargetInstrDesc &UseTID,
384 unsigned UseIdx, unsigned UseAlign) const;
385 int getSTMUseCycle(const InstrItineraryData *ItinData,
386 const TargetInstrDesc &UseTID,
388 unsigned UseIdx, unsigned UseAlign) const;
389 int getOperandLatency(const InstrItineraryData *ItinData,
390 const TargetInstrDesc &DefTID,
391 unsigned DefIdx, unsigned DefAlign,
392 const TargetInstrDesc &UseTID,
393 unsigned UseIdx, unsigned UseAlign) const;
395 int getInstrLatency(const InstrItineraryData *ItinData,
396 const MachineInstr *MI, unsigned *PredCost = 0) const;
398 int getInstrLatency(const InstrItineraryData *ItinData,
401 bool hasHighOperandLatency(const InstrItineraryData *ItinData,
402 const MachineRegisterInfo *MRI,
403 const MachineInstr *DefMI, unsigned DefIdx,
404 const MachineInstr *UseMI, unsigned UseIdx) const;
405 bool hasLowDefLatency(const InstrItineraryData *ItinData,
406 const MachineInstr *DefMI, unsigned DefIdx) const;
409 /// Modeling special VFP / NEON fp MLA / MLS hazards.
411 /// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal
413 DenseMap<unsigned, unsigned> MLxEntryMap;
415 /// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause
416 /// stalls when scheduled together with fp MLA / MLS opcodes.
417 SmallSet<unsigned, 16> MLxHazardOpcodes;
420 /// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS
422 bool isFpMLxInstruction(unsigned Opcode) const {
423 return MLxEntryMap.count(Opcode);
426 /// isFpMLxInstruction - This version also returns the multiply opcode and the
427 /// addition / subtraction opcode to expand to. Return true for 'HasLane' for
428 /// the MLX instructions with an extra lane operand.
429 bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
430 unsigned &AddSubOpc, bool &NegAcc,
431 bool &HasLane) const;
433 /// canCauseFpMLxStall - Return true if an instruction of the specified opcode
434 /// will cause stalls when scheduled after (within 4-cycle window) a fp
435 /// MLA / MLS instruction.
436 bool canCauseFpMLxStall(unsigned Opcode) const {
437 return MLxHazardOpcodes.count(Opcode);
442 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
443 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
447 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
448 return MIB.addReg(0);
452 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
453 bool isDead = false) {
454 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
458 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
459 return MIB.addReg(0);
463 bool isUncondBranchOpcode(int Opc) {
464 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
468 bool isCondBranchOpcode(int Opc) {
469 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
473 bool isJumpTableBranchOpcode(int Opc) {
474 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
475 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
479 bool isIndirectBranchOpcode(int Opc) {
480 return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
483 /// getInstrPredicate - If instruction is predicated, returns its predicate
484 /// condition, otherwise returns AL. It also returns the condition code
485 /// register by reference.
486 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
488 int getMatchingCondBranchOpcode(int Opc);
490 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
491 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
493 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
494 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
495 unsigned DestReg, unsigned BaseReg, int NumBytes,
496 ARMCC::CondCodes Pred, unsigned PredReg,
497 const ARMBaseInstrInfo &TII);
499 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
500 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
501 unsigned DestReg, unsigned BaseReg, int NumBytes,
502 ARMCC::CondCodes Pred, unsigned PredReg,
503 const ARMBaseInstrInfo &TII);
504 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
505 MachineBasicBlock::iterator &MBBI,
506 unsigned DestReg, unsigned BaseReg,
507 int NumBytes, const TargetInstrInfo &TII,
508 const ARMBaseRegisterInfo& MRI,
512 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
513 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
514 /// offset could not be handled directly in MI, and return the left-over
515 /// portion by reference.
516 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
517 unsigned FrameReg, int &Offset,
518 const ARMBaseInstrInfo &TII);
520 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
521 unsigned FrameReg, int &Offset,
522 const ARMBaseInstrInfo &TII);
524 } // End llvm namespace