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.
37 AddrModeMask = 0x1f, // The AddrMode enums are declared in ARMBaseInfo.h
39 // Size* - Flags to keep track of the size of an instruction.
41 SizeMask = 7 << SizeShift,
42 SizeSpecial = 1, // 0 byte pseudo or special case.
47 // IndexMode - Unindex, pre-indexed, or post-indexed are valid for load
48 // and store ops only. Generic "updating" flag is used for ld/st multiple.
49 // The index mode enums are declared in ARMBaseInfo.h
51 IndexModeMask = 3 << IndexModeShift,
53 //===------------------------------------------------------------------===//
54 // Instruction encoding formats.
57 FormMask = 0x3f << FormShift,
59 // Pseudo instructions
60 Pseudo = 0 << FormShift,
62 // Multiply instructions
63 MulFrm = 1 << FormShift,
65 // Branch instructions
66 BrFrm = 2 << FormShift,
67 BrMiscFrm = 3 << FormShift,
69 // Data Processing instructions
70 DPFrm = 4 << FormShift,
71 DPSoRegFrm = 5 << FormShift,
74 LdFrm = 6 << FormShift,
75 StFrm = 7 << FormShift,
76 LdMiscFrm = 8 << FormShift,
77 StMiscFrm = 9 << FormShift,
78 LdStMulFrm = 10 << FormShift,
80 LdStExFrm = 11 << FormShift,
82 // Miscellaneous arithmetic instructions
83 ArithMiscFrm = 12 << FormShift,
84 SatFrm = 13 << FormShift,
86 // Extend instructions
87 ExtFrm = 14 << FormShift,
90 VFPUnaryFrm = 15 << FormShift,
91 VFPBinaryFrm = 16 << FormShift,
92 VFPConv1Frm = 17 << FormShift,
93 VFPConv2Frm = 18 << FormShift,
94 VFPConv3Frm = 19 << FormShift,
95 VFPConv4Frm = 20 << FormShift,
96 VFPConv5Frm = 21 << FormShift,
97 VFPLdStFrm = 22 << FormShift,
98 VFPLdStMulFrm = 23 << FormShift,
99 VFPMiscFrm = 24 << FormShift,
102 ThumbFrm = 25 << FormShift,
104 // Miscelleaneous format
105 MiscFrm = 26 << FormShift,
108 NGetLnFrm = 27 << FormShift,
109 NSetLnFrm = 28 << FormShift,
110 NDupFrm = 29 << FormShift,
111 NLdStFrm = 30 << FormShift,
112 N1RegModImmFrm= 31 << FormShift,
113 N2RegFrm = 32 << FormShift,
114 NVCVTFrm = 33 << FormShift,
115 NVDupLnFrm = 34 << FormShift,
116 N2RegVShLFrm = 35 << FormShift,
117 N2RegVShRFrm = 36 << FormShift,
118 N3RegFrm = 37 << FormShift,
119 N3RegVShFrm = 38 << FormShift,
120 NVExtFrm = 39 << FormShift,
121 NVMulSLFrm = 40 << FormShift,
122 NVTBLFrm = 41 << FormShift,
124 //===------------------------------------------------------------------===//
127 // UnaryDP - Indicates this is a unary data processing instruction, i.e.
128 // it doesn't have a Rn operand.
131 // Xform16Bit - Indicates this Thumb2 instruction may be transformed into
132 // a 16-bit Thumb instruction if certain conditions are met.
133 Xform16Bit = 1 << 17,
135 //===------------------------------------------------------------------===//
138 DomainMask = 7 << DomainShift,
139 DomainGeneral = 0 << DomainShift,
140 DomainVFP = 1 << DomainShift,
141 DomainNEON = 2 << DomainShift,
142 DomainNEONA8 = 4 << DomainShift,
144 //===------------------------------------------------------------------===//
145 // Field shifts - such shifts are used to set field while generating
146 // machine instructions.
148 // FIXME: This list will need adjusting/fixing as the MC code emitter
149 // takes shape and the ARMCodeEmitter.cpp bits go away.
175 class ARMBaseInstrInfo : public TargetInstrInfoImpl {
176 const ARMSubtarget &Subtarget;
179 // Can be only subclassed.
180 explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
183 // Return the non-pre/post incrementing version of 'Opc'. Return 0
184 // if there is not such an opcode.
185 virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
187 virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
188 MachineBasicBlock::iterator &MBBI,
189 LiveVariables *LV) const;
191 virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
192 const ARMSubtarget &getSubtarget() const { return Subtarget; }
194 ScheduleHazardRecognizer *
195 CreateTargetHazardRecognizer(const TargetMachine *TM,
196 const ScheduleDAG *DAG) const;
198 ScheduleHazardRecognizer *
199 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
200 const ScheduleDAG *DAG) const;
203 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
204 MachineBasicBlock *&FBB,
205 SmallVectorImpl<MachineOperand> &Cond,
206 bool AllowModify = false) const;
207 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
208 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
209 MachineBasicBlock *FBB,
210 const SmallVectorImpl<MachineOperand> &Cond,
214 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
216 // Predication support.
217 bool isPredicated(const MachineInstr *MI) const {
218 int PIdx = MI->findFirstPredOperandIdx();
219 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
222 ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
223 int PIdx = MI->findFirstPredOperandIdx();
224 return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
229 bool PredicateInstruction(MachineInstr *MI,
230 const SmallVectorImpl<MachineOperand> &Pred) const;
233 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
234 const SmallVectorImpl<MachineOperand> &Pred2) const;
236 virtual bool DefinesPredicate(MachineInstr *MI,
237 std::vector<MachineOperand> &Pred) const;
239 virtual bool isPredicable(MachineInstr *MI) const;
241 /// GetInstSize - Returns the size of the specified MachineInstr.
243 virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
245 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
246 int &FrameIndex) const;
247 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
248 int &FrameIndex) const;
250 virtual void copyPhysReg(MachineBasicBlock &MBB,
251 MachineBasicBlock::iterator I, DebugLoc DL,
252 unsigned DestReg, unsigned SrcReg,
255 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
256 MachineBasicBlock::iterator MBBI,
257 unsigned SrcReg, bool isKill, int FrameIndex,
258 const TargetRegisterClass *RC,
259 const TargetRegisterInfo *TRI) const;
261 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
262 MachineBasicBlock::iterator MBBI,
263 unsigned DestReg, int FrameIndex,
264 const TargetRegisterClass *RC,
265 const TargetRegisterInfo *TRI) const;
267 virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
273 virtual void reMaterialize(MachineBasicBlock &MBB,
274 MachineBasicBlock::iterator MI,
275 unsigned DestReg, unsigned SubIdx,
276 const MachineInstr *Orig,
277 const TargetRegisterInfo &TRI) const;
279 MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
281 virtual bool produceSameValue(const MachineInstr *MI0,
282 const MachineInstr *MI1,
283 const MachineRegisterInfo *MRI) const;
285 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
286 /// determine if two loads are loading from the same base address. It should
287 /// only return true if the base pointers are the same and the only
288 /// differences between the two addresses is the offset. It also returns the
289 /// offsets by reference.
290 virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
291 int64_t &Offset1, int64_t &Offset2)const;
293 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
294 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
295 /// be scheduled togther. On some targets if two loads are loading from
296 /// addresses in the same cache line, it's better if they are scheduled
297 /// together. This function takes two integers that represent the load offsets
298 /// from the common base address. It returns true if it decides it's desirable
299 /// to schedule the two loads together. "NumLoads" is the number of loads that
300 /// have already been scheduled after Load1.
301 virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
302 int64_t Offset1, int64_t Offset2,
303 unsigned NumLoads) const;
305 virtual bool isSchedulingBoundary(const MachineInstr *MI,
306 const MachineBasicBlock *MBB,
307 const MachineFunction &MF) const;
309 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
310 unsigned NumCycles, unsigned ExtraPredCycles,
311 float Prob, float Confidence) const;
313 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
314 unsigned NumT, unsigned ExtraT,
315 MachineBasicBlock &FMBB,
316 unsigned NumF, unsigned ExtraF,
317 float Probability, float Confidence) const;
319 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
322 float Confidence) const {
323 return NumCycles == 1;
326 /// AnalyzeCompare - For a comparison instruction, return the source register
327 /// in SrcReg and the value it compares against in CmpValue. Return true if
328 /// the comparison instruction can be analyzed.
329 virtual bool AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
330 int &CmpMask, int &CmpValue) const;
332 /// OptimizeCompareInstr - Convert the instruction to set the zero flag so
333 /// that we can remove a "comparison with zero".
334 virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
335 int CmpMask, int CmpValue,
336 const MachineRegisterInfo *MRI) const;
338 /// FoldImmediate - 'Reg' is known to be defined by a move immediate
339 /// instruction, try to fold the immediate into the use instruction.
340 virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
341 unsigned Reg, MachineRegisterInfo *MRI) const;
343 virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
344 const MachineInstr *MI) const;
347 int getOperandLatency(const InstrItineraryData *ItinData,
348 const MachineInstr *DefMI, unsigned DefIdx,
349 const MachineInstr *UseMI, unsigned UseIdx) const;
351 int getOperandLatency(const InstrItineraryData *ItinData,
352 SDNode *DefNode, unsigned DefIdx,
353 SDNode *UseNode, unsigned UseIdx) const;
355 int getVLDMDefCycle(const InstrItineraryData *ItinData,
356 const TargetInstrDesc &DefTID,
358 unsigned DefIdx, unsigned DefAlign) const;
359 int getLDMDefCycle(const InstrItineraryData *ItinData,
360 const TargetInstrDesc &DefTID,
362 unsigned DefIdx, unsigned DefAlign) const;
363 int getVSTMUseCycle(const InstrItineraryData *ItinData,
364 const TargetInstrDesc &UseTID,
366 unsigned UseIdx, unsigned UseAlign) const;
367 int getSTMUseCycle(const InstrItineraryData *ItinData,
368 const TargetInstrDesc &UseTID,
370 unsigned UseIdx, unsigned UseAlign) const;
371 int getOperandLatency(const InstrItineraryData *ItinData,
372 const TargetInstrDesc &DefTID,
373 unsigned DefIdx, unsigned DefAlign,
374 const TargetInstrDesc &UseTID,
375 unsigned UseIdx, unsigned UseAlign) const;
377 int getInstrLatency(const InstrItineraryData *ItinData,
378 const MachineInstr *MI, unsigned *PredCost = 0) const;
380 int getInstrLatency(const InstrItineraryData *ItinData,
383 bool hasHighOperandLatency(const InstrItineraryData *ItinData,
384 const MachineRegisterInfo *MRI,
385 const MachineInstr *DefMI, unsigned DefIdx,
386 const MachineInstr *UseMI, unsigned UseIdx) const;
387 bool hasLowDefLatency(const InstrItineraryData *ItinData,
388 const MachineInstr *DefMI, unsigned DefIdx) const;
391 /// Modeling special VFP / NEON fp MLA / MLS hazards.
393 /// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal
395 DenseMap<unsigned, unsigned> MLxEntryMap;
397 /// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause
398 /// stalls when scheduled together with fp MLA / MLS opcodes.
399 SmallSet<unsigned, 16> MLxHazardOpcodes;
402 /// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS
404 bool isFpMLxInstruction(unsigned Opcode) const {
405 return MLxEntryMap.count(Opcode);
408 /// isFpMLxInstruction - This version also returns the multiply opcode and the
409 /// addition / subtraction opcode to expand to. Return true for 'HasLane' for
410 /// the MLX instructions with an extra lane operand.
411 bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
412 unsigned &AddSubOpc, bool &NegAcc,
413 bool &HasLane) const;
415 /// canCauseFpMLxStall - Return true if an instruction of the specified opcode
416 /// will cause stalls when scheduled after (within 4-cycle window) a fp
417 /// MLA / MLS instruction.
418 bool canCauseFpMLxStall(unsigned Opcode) const {
419 return MLxHazardOpcodes.count(Opcode);
424 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
425 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
429 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
430 return MIB.addReg(0);
434 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
435 bool isDead = false) {
436 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
440 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
441 return MIB.addReg(0);
445 bool isUncondBranchOpcode(int Opc) {
446 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
450 bool isCondBranchOpcode(int Opc) {
451 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
455 bool isJumpTableBranchOpcode(int Opc) {
456 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
457 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
461 bool isIndirectBranchOpcode(int Opc) {
462 return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
465 /// getInstrPredicate - If instruction is predicated, returns its predicate
466 /// condition, otherwise returns AL. It also returns the condition code
467 /// register by reference.
468 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
470 int getMatchingCondBranchOpcode(int Opc);
472 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
473 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
475 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
476 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
477 unsigned DestReg, unsigned BaseReg, int NumBytes,
478 ARMCC::CondCodes Pred, unsigned PredReg,
479 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
481 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
482 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
483 unsigned DestReg, unsigned BaseReg, int NumBytes,
484 ARMCC::CondCodes Pred, unsigned PredReg,
485 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
486 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
487 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
488 unsigned DestReg, unsigned BaseReg,
489 int NumBytes, const TargetInstrInfo &TII,
490 const ARMBaseRegisterInfo& MRI,
491 unsigned MIFlags = 0);
494 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
495 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
496 /// offset could not be handled directly in MI, and return the left-over
497 /// portion by reference.
498 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
499 unsigned FrameReg, int &Offset,
500 const ARMBaseInstrInfo &TII);
502 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
503 unsigned FrameReg, int &Offset,
504 const ARMBaseInstrInfo &TII);
506 } // End llvm namespace