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"
23 #define GET_INSTRINFO_HEADER
24 #include "ARMGenInstrInfo.inc"
28 class ARMBaseRegisterInfo;
30 /// ARMII - This namespace holds all of the target specific flags that
31 /// instruction info tracks.
35 //===------------------------------------------------------------------===//
38 //===------------------------------------------------------------------===//
39 // This four-bit field describes the addressing mode used.
40 AddrModeMask = 0x1f, // The AddrMode enums are declared in ARMBaseInfo.h
42 // IndexMode - Unindex, pre-indexed, or post-indexed are valid for load
43 // and store ops only. Generic "updating" flag is used for ld/st multiple.
44 // The index mode enums are declared in ARMBaseInfo.h
46 IndexModeMask = 3 << IndexModeShift,
48 //===------------------------------------------------------------------===//
49 // Instruction encoding formats.
52 FormMask = 0x3f << FormShift,
54 // Pseudo instructions
55 Pseudo = 0 << FormShift,
57 // Multiply instructions
58 MulFrm = 1 << FormShift,
60 // Branch instructions
61 BrFrm = 2 << FormShift,
62 BrMiscFrm = 3 << FormShift,
64 // Data Processing instructions
65 DPFrm = 4 << FormShift,
66 DPSoRegFrm = 5 << FormShift,
69 LdFrm = 6 << FormShift,
70 StFrm = 7 << FormShift,
71 LdMiscFrm = 8 << FormShift,
72 StMiscFrm = 9 << FormShift,
73 LdStMulFrm = 10 << FormShift,
75 LdStExFrm = 11 << FormShift,
77 // Miscellaneous arithmetic instructions
78 ArithMiscFrm = 12 << FormShift,
79 SatFrm = 13 << FormShift,
81 // Extend instructions
82 ExtFrm = 14 << FormShift,
85 VFPUnaryFrm = 15 << FormShift,
86 VFPBinaryFrm = 16 << FormShift,
87 VFPConv1Frm = 17 << FormShift,
88 VFPConv2Frm = 18 << FormShift,
89 VFPConv3Frm = 19 << FormShift,
90 VFPConv4Frm = 20 << FormShift,
91 VFPConv5Frm = 21 << FormShift,
92 VFPLdStFrm = 22 << FormShift,
93 VFPLdStMulFrm = 23 << FormShift,
94 VFPMiscFrm = 24 << FormShift,
97 ThumbFrm = 25 << FormShift,
99 // Miscelleaneous format
100 MiscFrm = 26 << FormShift,
103 NGetLnFrm = 27 << FormShift,
104 NSetLnFrm = 28 << FormShift,
105 NDupFrm = 29 << FormShift,
106 NLdStFrm = 30 << FormShift,
107 N1RegModImmFrm= 31 << FormShift,
108 N2RegFrm = 32 << FormShift,
109 NVCVTFrm = 33 << FormShift,
110 NVDupLnFrm = 34 << FormShift,
111 N2RegVShLFrm = 35 << FormShift,
112 N2RegVShRFrm = 36 << FormShift,
113 N3RegFrm = 37 << FormShift,
114 N3RegVShFrm = 38 << FormShift,
115 NVExtFrm = 39 << FormShift,
116 NVMulSLFrm = 40 << FormShift,
117 NVTBLFrm = 41 << FormShift,
119 //===------------------------------------------------------------------===//
122 // UnaryDP - Indicates this is a unary data processing instruction, i.e.
123 // it doesn't have a Rn operand.
126 // Xform16Bit - Indicates this Thumb2 instruction may be transformed into
127 // a 16-bit Thumb instruction if certain conditions are met.
128 Xform16Bit = 1 << 14,
130 //===------------------------------------------------------------------===//
133 DomainMask = 7 << DomainShift,
134 DomainGeneral = 0 << DomainShift,
135 DomainVFP = 1 << DomainShift,
136 DomainNEON = 2 << DomainShift,
137 DomainNEONA8 = 4 << DomainShift,
139 //===------------------------------------------------------------------===//
140 // Field shifts - such shifts are used to set field while generating
141 // machine instructions.
143 // FIXME: This list will need adjusting/fixing as the MC code emitter
144 // takes shape and the ARMCodeEmitter.cpp bits go away.
170 class ARMBaseInstrInfo : public ARMGenInstrInfo {
171 const ARMSubtarget &Subtarget;
174 // Can be only subclassed.
175 explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
178 // Return the non-pre/post incrementing version of 'Opc'. Return 0
179 // if there is not such an opcode.
180 virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
182 virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
183 MachineBasicBlock::iterator &MBBI,
184 LiveVariables *LV) const;
186 virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
187 const ARMSubtarget &getSubtarget() const { return Subtarget; }
189 ScheduleHazardRecognizer *
190 CreateTargetHazardRecognizer(const TargetMachine *TM,
191 const ScheduleDAG *DAG) const;
193 ScheduleHazardRecognizer *
194 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
195 const ScheduleDAG *DAG) const;
198 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
199 MachineBasicBlock *&FBB,
200 SmallVectorImpl<MachineOperand> &Cond,
201 bool AllowModify = false) const;
202 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
203 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
204 MachineBasicBlock *FBB,
205 const SmallVectorImpl<MachineOperand> &Cond,
209 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
211 // Predication support.
212 bool isPredicated(const MachineInstr *MI) const {
213 int PIdx = MI->findFirstPredOperandIdx();
214 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
217 ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
218 int PIdx = MI->findFirstPredOperandIdx();
219 return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
224 bool PredicateInstruction(MachineInstr *MI,
225 const SmallVectorImpl<MachineOperand> &Pred) const;
228 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
229 const SmallVectorImpl<MachineOperand> &Pred2) const;
231 virtual bool DefinesPredicate(MachineInstr *MI,
232 std::vector<MachineOperand> &Pred) const;
234 virtual bool isPredicable(MachineInstr *MI) const;
236 /// GetInstSize - Returns the size of the specified MachineInstr.
238 virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
240 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
241 int &FrameIndex) const;
242 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
243 int &FrameIndex) const;
245 virtual void copyPhysReg(MachineBasicBlock &MBB,
246 MachineBasicBlock::iterator I, DebugLoc DL,
247 unsigned DestReg, unsigned SrcReg,
250 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
251 MachineBasicBlock::iterator MBBI,
252 unsigned SrcReg, bool isKill, int FrameIndex,
253 const TargetRegisterClass *RC,
254 const TargetRegisterInfo *TRI) const;
256 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
257 MachineBasicBlock::iterator MBBI,
258 unsigned DestReg, int FrameIndex,
259 const TargetRegisterClass *RC,
260 const TargetRegisterInfo *TRI) const;
262 virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
268 virtual void reMaterialize(MachineBasicBlock &MBB,
269 MachineBasicBlock::iterator MI,
270 unsigned DestReg, unsigned SubIdx,
271 const MachineInstr *Orig,
272 const TargetRegisterInfo &TRI) const;
274 MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
276 virtual bool produceSameValue(const MachineInstr *MI0,
277 const MachineInstr *MI1,
278 const MachineRegisterInfo *MRI) const;
280 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
281 /// determine if two loads are loading from the same base address. It should
282 /// only return true if the base pointers are the same and the only
283 /// differences between the two addresses is the offset. It also returns the
284 /// offsets by reference.
285 virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
286 int64_t &Offset1, int64_t &Offset2)const;
288 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
289 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads
290 /// should be scheduled togther. On some targets if two loads are loading from
291 /// addresses in the same cache line, it's better if they are scheduled
292 /// together. This function takes two integers that represent the load offsets
293 /// from the common base address. It returns true if it decides it's desirable
294 /// to schedule the two loads together. "NumLoads" is the number of loads that
295 /// have already been scheduled after Load1.
296 virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
297 int64_t Offset1, int64_t Offset2,
298 unsigned NumLoads) const;
300 virtual bool isSchedulingBoundary(const MachineInstr *MI,
301 const MachineBasicBlock *MBB,
302 const MachineFunction &MF) const;
304 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
305 unsigned NumCycles, unsigned ExtraPredCycles,
306 const BranchProbability &Probability) const;
308 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
309 unsigned NumT, unsigned ExtraT,
310 MachineBasicBlock &FMBB,
311 unsigned NumF, unsigned ExtraF,
312 const BranchProbability &Probability) const;
314 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
316 const BranchProbability
317 &Probability) const {
318 return NumCycles == 1;
321 /// AnalyzeCompare - For a comparison instruction, return the source register
322 /// in SrcReg and the value it compares against in CmpValue. Return true if
323 /// the comparison instruction can be analyzed.
324 virtual bool AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
325 int &CmpMask, int &CmpValue) const;
327 /// OptimizeCompareInstr - Convert the instruction to set the zero flag so
328 /// that we can remove a "comparison with zero".
329 virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
330 int CmpMask, int CmpValue,
331 const MachineRegisterInfo *MRI) const;
333 /// FoldImmediate - 'Reg' is known to be defined by a move immediate
334 /// instruction, try to fold the immediate into the use instruction.
335 virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
336 unsigned Reg, MachineRegisterInfo *MRI) const;
338 virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
339 const MachineInstr *MI) const;
342 int getOperandLatency(const InstrItineraryData *ItinData,
343 const MachineInstr *DefMI, unsigned DefIdx,
344 const MachineInstr *UseMI, unsigned UseIdx) const;
346 int getOperandLatency(const InstrItineraryData *ItinData,
347 SDNode *DefNode, unsigned DefIdx,
348 SDNode *UseNode, unsigned UseIdx) const;
350 int getVLDMDefCycle(const InstrItineraryData *ItinData,
351 const MCInstrDesc &DefMCID,
353 unsigned DefIdx, unsigned DefAlign) const;
354 int getLDMDefCycle(const InstrItineraryData *ItinData,
355 const MCInstrDesc &DefMCID,
357 unsigned DefIdx, unsigned DefAlign) const;
358 int getVSTMUseCycle(const InstrItineraryData *ItinData,
359 const MCInstrDesc &UseMCID,
361 unsigned UseIdx, unsigned UseAlign) const;
362 int getSTMUseCycle(const InstrItineraryData *ItinData,
363 const MCInstrDesc &UseMCID,
365 unsigned UseIdx, unsigned UseAlign) const;
366 int getOperandLatency(const InstrItineraryData *ItinData,
367 const MCInstrDesc &DefMCID,
368 unsigned DefIdx, unsigned DefAlign,
369 const MCInstrDesc &UseMCID,
370 unsigned UseIdx, unsigned UseAlign) const;
372 int getInstrLatency(const InstrItineraryData *ItinData,
373 const MachineInstr *MI, unsigned *PredCost = 0) const;
375 int getInstrLatency(const InstrItineraryData *ItinData,
378 bool hasHighOperandLatency(const InstrItineraryData *ItinData,
379 const MachineRegisterInfo *MRI,
380 const MachineInstr *DefMI, unsigned DefIdx,
381 const MachineInstr *UseMI, unsigned UseIdx) const;
382 bool hasLowDefLatency(const InstrItineraryData *ItinData,
383 const MachineInstr *DefMI, unsigned DefIdx) const;
386 /// Modeling special VFP / NEON fp MLA / MLS hazards.
388 /// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal
390 DenseMap<unsigned, unsigned> MLxEntryMap;
392 /// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause
393 /// stalls when scheduled together with fp MLA / MLS opcodes.
394 SmallSet<unsigned, 16> MLxHazardOpcodes;
397 /// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS
399 bool isFpMLxInstruction(unsigned Opcode) const {
400 return MLxEntryMap.count(Opcode);
403 /// isFpMLxInstruction - This version also returns the multiply opcode and the
404 /// addition / subtraction opcode to expand to. Return true for 'HasLane' for
405 /// the MLX instructions with an extra lane operand.
406 bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
407 unsigned &AddSubOpc, bool &NegAcc,
408 bool &HasLane) const;
410 /// canCauseFpMLxStall - Return true if an instruction of the specified opcode
411 /// will cause stalls when scheduled after (within 4-cycle window) a fp
412 /// MLA / MLS instruction.
413 bool canCauseFpMLxStall(unsigned Opcode) const {
414 return MLxHazardOpcodes.count(Opcode);
419 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
420 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
424 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
425 return MIB.addReg(0);
429 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
430 bool isDead = false) {
431 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
435 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
436 return MIB.addReg(0);
440 bool isUncondBranchOpcode(int Opc) {
441 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
445 bool isCondBranchOpcode(int Opc) {
446 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
450 bool isJumpTableBranchOpcode(int Opc) {
451 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
452 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
456 bool isIndirectBranchOpcode(int Opc) {
457 return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
460 /// getInstrPredicate - If instruction is predicated, returns its predicate
461 /// condition, otherwise returns AL. It also returns the condition code
462 /// register by reference.
463 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
465 int getMatchingCondBranchOpcode(int Opc);
467 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
468 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
470 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
471 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
472 unsigned DestReg, unsigned BaseReg, int NumBytes,
473 ARMCC::CondCodes Pred, unsigned PredReg,
474 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
476 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
477 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
478 unsigned DestReg, unsigned BaseReg, int NumBytes,
479 ARMCC::CondCodes Pred, unsigned PredReg,
480 const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
481 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
482 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
483 unsigned DestReg, unsigned BaseReg,
484 int NumBytes, const TargetInstrInfo &TII,
485 const ARMBaseRegisterInfo& MRI,
486 unsigned MIFlags = 0);
489 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
490 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
491 /// offset could not be handled directly in MI, and return the left-over
492 /// portion by reference.
493 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
494 unsigned FrameReg, int &Offset,
495 const ARMBaseInstrInfo &TII);
497 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
498 unsigned FrameReg, int &Offset,
499 const ARMBaseInstrInfo &TII);
501 } // End llvm namespace