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 = 7 << DomainShift,
159 DomainGeneral = 0 << DomainShift,
160 DomainVFP = 1 << DomainShift,
161 DomainNEON = 2 << DomainShift,
162 DomainNEONA8 = 4 << DomainShift,
164 //===------------------------------------------------------------------===//
165 // Field shifts - such shifts are used to set field while generating
166 // machine instructions.
168 // FIXME: This list will need adjusting/fixing as the MC code emitter
169 // takes shape and the ARMCodeEmitter.cpp bits go away.
195 class ARMBaseInstrInfo : public TargetInstrInfoImpl {
196 const ARMSubtarget &Subtarget;
199 // Can be only subclassed.
200 explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
203 // Return the non-pre/post incrementing version of 'Opc'. Return 0
204 // if there is not such an opcode.
205 virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
207 virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
208 MachineBasicBlock::iterator &MBBI,
209 LiveVariables *LV) const;
211 virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
212 const ARMSubtarget &getSubtarget() const { return Subtarget; }
214 ScheduleHazardRecognizer *
215 CreateTargetHazardRecognizer(const TargetMachine *TM,
216 const ScheduleDAG *DAG) const;
218 ScheduleHazardRecognizer *
219 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
220 const ScheduleDAG *DAG) const;
223 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
224 MachineBasicBlock *&FBB,
225 SmallVectorImpl<MachineOperand> &Cond,
226 bool AllowModify = false) const;
227 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
228 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
229 MachineBasicBlock *FBB,
230 const SmallVectorImpl<MachineOperand> &Cond,
234 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
236 // Predication support.
237 bool isPredicated(const MachineInstr *MI) const {
238 int PIdx = MI->findFirstPredOperandIdx();
239 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
242 ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
243 int PIdx = MI->findFirstPredOperandIdx();
244 return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
249 bool PredicateInstruction(MachineInstr *MI,
250 const SmallVectorImpl<MachineOperand> &Pred) const;
253 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
254 const SmallVectorImpl<MachineOperand> &Pred2) const;
256 virtual bool DefinesPredicate(MachineInstr *MI,
257 std::vector<MachineOperand> &Pred) const;
259 virtual bool isPredicable(MachineInstr *MI) const;
261 /// GetInstSize - Returns the size of the specified MachineInstr.
263 virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
265 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
266 int &FrameIndex) const;
267 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
268 int &FrameIndex) const;
270 virtual void copyPhysReg(MachineBasicBlock &MBB,
271 MachineBasicBlock::iterator I, DebugLoc DL,
272 unsigned DestReg, unsigned SrcReg,
275 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
276 MachineBasicBlock::iterator MBBI,
277 unsigned SrcReg, bool isKill, int FrameIndex,
278 const TargetRegisterClass *RC,
279 const TargetRegisterInfo *TRI) const;
281 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
282 MachineBasicBlock::iterator MBBI,
283 unsigned DestReg, int FrameIndex,
284 const TargetRegisterClass *RC,
285 const TargetRegisterInfo *TRI) const;
287 virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
293 virtual void reMaterialize(MachineBasicBlock &MBB,
294 MachineBasicBlock::iterator MI,
295 unsigned DestReg, unsigned SubIdx,
296 const MachineInstr *Orig,
297 const TargetRegisterInfo &TRI) const;
299 MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
301 virtual bool produceSameValue(const MachineInstr *MI0,
302 const MachineInstr *MI1,
303 const MachineRegisterInfo *MRI) const;
305 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
306 /// determine if two loads are loading from the same base address. It should
307 /// only return true if the base pointers are the same and the only
308 /// differences between the two addresses is the offset. It also returns the
309 /// offsets by reference.
310 virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
311 int64_t &Offset1, int64_t &Offset2)const;
313 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
314 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
315 /// be scheduled togther. On some targets if two loads are loading from
316 /// addresses in the same cache line, it's better if they are scheduled
317 /// together. This function takes two integers that represent the load offsets
318 /// from the common base address. It returns true if it decides it's desirable
319 /// to schedule the two loads together. "NumLoads" is the number of loads that
320 /// have already been scheduled after Load1.
321 virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
322 int64_t Offset1, int64_t Offset2,
323 unsigned NumLoads) const;
325 virtual bool isSchedulingBoundary(const MachineInstr *MI,
326 const MachineBasicBlock *MBB,
327 const MachineFunction &MF) const;
329 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
330 unsigned NumCyles, unsigned ExtraPredCycles,
331 float Prob, float Confidence) const;
333 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
334 unsigned NumT, unsigned ExtraT,
335 MachineBasicBlock &FMBB,
336 unsigned NumF, unsigned ExtraF,
337 float Probability, float Confidence) const;
339 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
342 float Confidence) const {
343 return NumCyles == 1;
346 /// AnalyzeCompare - For a comparison instruction, return the source register
347 /// in SrcReg and the value it compares against in CmpValue. Return true if
348 /// the comparison instruction can be analyzed.
349 virtual bool AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
350 int &CmpMask, int &CmpValue) const;
352 /// OptimizeCompareInstr - Convert the instruction to set the zero flag so
353 /// that we can remove a "comparison with zero".
354 virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
355 int CmpMask, int CmpValue,
356 const MachineRegisterInfo *MRI) const;
358 /// FoldImmediate - 'Reg' is known to be defined by a move immediate
359 /// instruction, try to fold the immediate into the use instruction.
360 virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
361 unsigned Reg, MachineRegisterInfo *MRI) const;
363 virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
364 const MachineInstr *MI) const;
367 int getOperandLatency(const InstrItineraryData *ItinData,
368 const MachineInstr *DefMI, unsigned DefIdx,
369 const MachineInstr *UseMI, unsigned UseIdx) const;
371 int getOperandLatency(const InstrItineraryData *ItinData,
372 SDNode *DefNode, unsigned DefIdx,
373 SDNode *UseNode, unsigned UseIdx) const;
375 int getVLDMDefCycle(const InstrItineraryData *ItinData,
376 const TargetInstrDesc &DefTID,
378 unsigned DefIdx, unsigned DefAlign) const;
379 int getLDMDefCycle(const InstrItineraryData *ItinData,
380 const TargetInstrDesc &DefTID,
382 unsigned DefIdx, unsigned DefAlign) const;
383 int getVSTMUseCycle(const InstrItineraryData *ItinData,
384 const TargetInstrDesc &UseTID,
386 unsigned UseIdx, unsigned UseAlign) const;
387 int getSTMUseCycle(const InstrItineraryData *ItinData,
388 const TargetInstrDesc &UseTID,
390 unsigned UseIdx, unsigned UseAlign) const;
391 int getOperandLatency(const InstrItineraryData *ItinData,
392 const TargetInstrDesc &DefTID,
393 unsigned DefIdx, unsigned DefAlign,
394 const TargetInstrDesc &UseTID,
395 unsigned UseIdx, unsigned UseAlign) const;
397 int getInstrLatency(const InstrItineraryData *ItinData,
398 const MachineInstr *MI, unsigned *PredCost = 0) const;
400 int getInstrLatency(const InstrItineraryData *ItinData,
403 bool hasHighOperandLatency(const InstrItineraryData *ItinData,
404 const MachineRegisterInfo *MRI,
405 const MachineInstr *DefMI, unsigned DefIdx,
406 const MachineInstr *UseMI, unsigned UseIdx) const;
407 bool hasLowDefLatency(const InstrItineraryData *ItinData,
408 const MachineInstr *DefMI, unsigned DefIdx) const;
411 /// Modeling special VFP / NEON fp MLA / MLS hazards.
413 /// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal
415 DenseMap<unsigned, unsigned> MLxEntryMap;
417 /// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause
418 /// stalls when scheduled together with fp MLA / MLS opcodes.
419 SmallSet<unsigned, 16> MLxHazardOpcodes;
422 /// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS
424 bool isFpMLxInstruction(unsigned Opcode) const {
425 return MLxEntryMap.count(Opcode);
428 /// isFpMLxInstruction - This version also returns the multiply opcode and the
429 /// addition / subtraction opcode to expand to. Return true for 'HasLane' for
430 /// the MLX instructions with an extra lane operand.
431 bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
432 unsigned &AddSubOpc, bool &NegAcc,
433 bool &HasLane) const;
435 /// canCauseFpMLxStall - Return true if an instruction of the specified opcode
436 /// will cause stalls when scheduled after (within 4-cycle window) a fp
437 /// MLA / MLS instruction.
438 bool canCauseFpMLxStall(unsigned Opcode) const {
439 return MLxHazardOpcodes.count(Opcode);
444 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
445 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
449 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
450 return MIB.addReg(0);
454 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
455 bool isDead = false) {
456 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
460 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
461 return MIB.addReg(0);
465 bool isUncondBranchOpcode(int Opc) {
466 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
470 bool isCondBranchOpcode(int Opc) {
471 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
475 bool isJumpTableBranchOpcode(int Opc) {
476 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
477 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
481 bool isIndirectBranchOpcode(int Opc) {
482 return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
485 /// getInstrPredicate - If instruction is predicated, returns its predicate
486 /// condition, otherwise returns AL. It also returns the condition code
487 /// register by reference.
488 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
490 int getMatchingCondBranchOpcode(int Opc);
492 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
493 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
495 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
496 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
497 unsigned DestReg, unsigned BaseReg, int NumBytes,
498 ARMCC::CondCodes Pred, unsigned PredReg,
499 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
501 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
502 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
503 unsigned DestReg, unsigned BaseReg, int NumBytes,
504 ARMCC::CondCodes Pred, unsigned PredReg,
505 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
506 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
507 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
508 unsigned DestReg, unsigned BaseReg,
509 int NumBytes, const TargetInstrInfo &TII,
510 const ARMBaseRegisterInfo& MRI,
511 unsigned MIFlags = 0);
514 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
515 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
516 /// offset could not be handled directly in MI, and return the left-over
517 /// portion by reference.
518 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
519 unsigned FrameReg, int &Offset,
520 const ARMBaseInstrInfo &TII);
522 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
523 unsigned FrameReg, int &Offset,
524 const ARMBaseInstrInfo &TII);
526 } // End llvm namespace