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
55 // Size* - Flags to keep track of the size of an instruction.
57 SizeMask = 7 << SizeShift,
58 SizeSpecial = 1, // 0 byte pseudo or special case.
63 // IndexMode - Unindex, pre-indexed, or post-indexed are valid for load
64 // and store ops only. Generic "updating" flag is used for ld/st multiple.
66 IndexModeMask = 3 << IndexModeShift,
71 //===------------------------------------------------------------------===//
72 // Instruction encoding formats.
75 FormMask = 0x3f << FormShift,
77 // Pseudo instructions
78 Pseudo = 0 << FormShift,
80 // Multiply instructions
81 MulFrm = 1 << FormShift,
83 // Branch instructions
84 BrFrm = 2 << FormShift,
85 BrMiscFrm = 3 << FormShift,
87 // Data Processing instructions
88 DPFrm = 4 << FormShift,
89 DPSoRegFrm = 5 << FormShift,
92 LdFrm = 6 << FormShift,
93 StFrm = 7 << FormShift,
94 LdMiscFrm = 8 << FormShift,
95 StMiscFrm = 9 << FormShift,
96 LdStMulFrm = 10 << FormShift,
98 LdStExFrm = 11 << FormShift,
100 // Miscellaneous arithmetic instructions
101 ArithMiscFrm = 12 << FormShift,
102 SatFrm = 13 << FormShift,
104 // Extend instructions
105 ExtFrm = 14 << FormShift,
108 VFPUnaryFrm = 15 << FormShift,
109 VFPBinaryFrm = 16 << FormShift,
110 VFPConv1Frm = 17 << FormShift,
111 VFPConv2Frm = 18 << FormShift,
112 VFPConv3Frm = 19 << FormShift,
113 VFPConv4Frm = 20 << FormShift,
114 VFPConv5Frm = 21 << FormShift,
115 VFPLdStFrm = 22 << FormShift,
116 VFPLdStMulFrm = 23 << FormShift,
117 VFPMiscFrm = 24 << FormShift,
120 ThumbFrm = 25 << FormShift,
122 // Miscelleaneous format
123 MiscFrm = 26 << FormShift,
126 NGetLnFrm = 27 << FormShift,
127 NSetLnFrm = 28 << FormShift,
128 NDupFrm = 29 << FormShift,
129 NLdStFrm = 30 << FormShift,
130 N1RegModImmFrm= 31 << FormShift,
131 N2RegFrm = 32 << FormShift,
132 NVCVTFrm = 33 << FormShift,
133 NVDupLnFrm = 34 << FormShift,
134 N2RegVShLFrm = 35 << FormShift,
135 N2RegVShRFrm = 36 << FormShift,
136 N3RegFrm = 37 << FormShift,
137 N3RegVShFrm = 38 << FormShift,
138 NVExtFrm = 39 << FormShift,
139 NVMulSLFrm = 40 << FormShift,
140 NVTBLFrm = 41 << FormShift,
142 //===------------------------------------------------------------------===//
145 // UnaryDP - Indicates this is a unary data processing instruction, i.e.
146 // it doesn't have a Rn operand.
149 // Xform16Bit - Indicates this Thumb2 instruction may be transformed into
150 // a 16-bit Thumb instruction if certain conditions are met.
151 Xform16Bit = 1 << 17,
153 //===------------------------------------------------------------------===//
156 DomainMask = 3 << DomainShift,
157 DomainGeneral = 0 << DomainShift,
158 DomainVFP = 1 << DomainShift,
159 DomainNEON = 2 << DomainShift,
161 //===------------------------------------------------------------------===//
162 // Field shifts - such shifts are used to set field while generating
163 // machine instructions.
165 // FIXME: This list will need adjusting/fixing as the MC code emitter
166 // takes shape and the ARMCodeEmitter.cpp bits go away.
192 class ARMBaseInstrInfo : public TargetInstrInfoImpl {
193 const ARMSubtarget &Subtarget;
195 // Can be only subclassed.
196 explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
198 // Return the non-pre/post incrementing version of 'Opc'. Return 0
199 // if there is not such an opcode.
200 virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
202 virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
203 MachineBasicBlock::iterator &MBBI,
204 LiveVariables *LV) const;
206 virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
207 const ARMSubtarget &getSubtarget() const { return Subtarget; }
209 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
210 MachineBasicBlock::iterator MI,
211 const std::vector<CalleeSavedInfo> &CSI,
212 const TargetRegisterInfo *TRI) const;
214 bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
215 MachineBasicBlock::iterator MI,
216 const std::vector<CalleeSavedInfo> &CSI,
217 const TargetRegisterInfo *TRI) const;
220 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
221 MachineBasicBlock *&FBB,
222 SmallVectorImpl<MachineOperand> &Cond,
223 bool AllowModify = false) const;
224 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
225 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
226 MachineBasicBlock *FBB,
227 const SmallVectorImpl<MachineOperand> &Cond,
231 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
233 // Predication support.
234 bool isPredicated(const MachineInstr *MI) const {
235 int PIdx = MI->findFirstPredOperandIdx();
236 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
239 ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
240 int PIdx = MI->findFirstPredOperandIdx();
241 return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
246 bool PredicateInstruction(MachineInstr *MI,
247 const SmallVectorImpl<MachineOperand> &Pred) const;
250 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
251 const SmallVectorImpl<MachineOperand> &Pred2) const;
253 virtual bool DefinesPredicate(MachineInstr *MI,
254 std::vector<MachineOperand> &Pred) const;
256 virtual bool isPredicable(MachineInstr *MI) const;
258 /// GetInstSize - Returns the size of the specified MachineInstr.
260 virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
262 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
263 int &FrameIndex) const;
264 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
265 int &FrameIndex) const;
267 virtual void copyPhysReg(MachineBasicBlock &MBB,
268 MachineBasicBlock::iterator I, DebugLoc DL,
269 unsigned DestReg, unsigned SrcReg,
272 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
273 MachineBasicBlock::iterator MBBI,
274 unsigned SrcReg, bool isKill, int FrameIndex,
275 const TargetRegisterClass *RC,
276 const TargetRegisterInfo *TRI) const;
278 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
279 MachineBasicBlock::iterator MBBI,
280 unsigned DestReg, int FrameIndex,
281 const TargetRegisterClass *RC,
282 const TargetRegisterInfo *TRI) const;
284 virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
290 virtual void reMaterialize(MachineBasicBlock &MBB,
291 MachineBasicBlock::iterator MI,
292 unsigned DestReg, unsigned SubIdx,
293 const MachineInstr *Orig,
294 const TargetRegisterInfo &TRI) const;
296 MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
298 virtual bool produceSameValue(const MachineInstr *MI0,
299 const MachineInstr *MI1) const;
301 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
302 /// determine if two loads are loading from the same base address. It should
303 /// only return true if the base pointers are the same and the only
304 /// differences between the two addresses is the offset. It also returns the
305 /// offsets by reference.
306 virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
307 int64_t &Offset1, int64_t &Offset2)const;
309 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
310 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
311 /// be scheduled togther. On some targets if two loads are loading from
312 /// addresses in the same cache line, it's better if they are scheduled
313 /// together. This function takes two integers that represent the load offsets
314 /// from the common base address. It returns true if it decides it's desirable
315 /// to schedule the two loads together. "NumLoads" is the number of loads that
316 /// have already been scheduled after Load1.
317 virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
318 int64_t Offset1, int64_t Offset2,
319 unsigned NumLoads) const;
321 virtual bool isSchedulingBoundary(const MachineInstr *MI,
322 const MachineBasicBlock *MBB,
323 const MachineFunction &MF) const;
325 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
326 unsigned NumCyles, unsigned ExtraPredCycles,
327 float Prob, float Confidence) const;
329 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
330 unsigned NumT, unsigned ExtraT,
331 MachineBasicBlock &FMBB,
332 unsigned NumF, unsigned ExtraF,
333 float Probability, float Confidence) const;
335 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
338 float Confidence) const {
339 return NumCyles == 1;
342 /// AnalyzeCompare - For a comparison instruction, return the source register
343 /// in SrcReg and the value it compares against in CmpValue. Return true if
344 /// the comparison instruction can be analyzed.
345 virtual bool AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
346 int &CmpMask, int &CmpValue) const;
348 /// OptimizeCompareInstr - Convert the instruction to set the zero flag so
349 /// that we can remove a "comparison with zero".
350 virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
351 int CmpMask, int CmpValue,
352 const MachineRegisterInfo *MRI,
353 MachineBasicBlock::iterator &MII) const;
355 virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
356 const MachineInstr *MI) const;
359 int getOperandLatency(const InstrItineraryData *ItinData,
360 const MachineInstr *DefMI, unsigned DefIdx,
361 const MachineInstr *UseMI, unsigned UseIdx) const;
363 int getOperandLatency(const InstrItineraryData *ItinData,
364 SDNode *DefNode, unsigned DefIdx,
365 SDNode *UseNode, unsigned UseIdx) const;
367 int getVLDMDefCycle(const InstrItineraryData *ItinData,
368 const TargetInstrDesc &DefTID,
370 unsigned DefIdx, unsigned DefAlign) const;
371 int getLDMDefCycle(const InstrItineraryData *ItinData,
372 const TargetInstrDesc &DefTID,
374 unsigned DefIdx, unsigned DefAlign) const;
375 int getVSTMUseCycle(const InstrItineraryData *ItinData,
376 const TargetInstrDesc &UseTID,
378 unsigned UseIdx, unsigned UseAlign) const;
379 int getSTMUseCycle(const InstrItineraryData *ItinData,
380 const TargetInstrDesc &UseTID,
382 unsigned UseIdx, unsigned UseAlign) const;
383 int getOperandLatency(const InstrItineraryData *ItinData,
384 const TargetInstrDesc &DefTID,
385 unsigned DefIdx, unsigned DefAlign,
386 const TargetInstrDesc &UseTID,
387 unsigned UseIdx, unsigned UseAlign) const;
389 int getInstrLatency(const InstrItineraryData *ItinData,
390 const MachineInstr *MI, unsigned *PredCost = 0) const;
392 int getInstrLatency(const InstrItineraryData *ItinData,
395 bool hasHighOperandLatency(const InstrItineraryData *ItinData,
396 const MachineRegisterInfo *MRI,
397 const MachineInstr *DefMI, unsigned DefIdx,
398 const MachineInstr *UseMI, unsigned UseIdx) const;
399 bool hasLowDefLatency(const InstrItineraryData *ItinData,
400 const MachineInstr *DefMI, unsigned DefIdx) const;
404 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
405 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
409 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
410 return MIB.addReg(0);
414 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
415 bool isDead = false) {
416 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
420 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
421 return MIB.addReg(0);
425 bool isUncondBranchOpcode(int Opc) {
426 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
430 bool isCondBranchOpcode(int Opc) {
431 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
435 bool isJumpTableBranchOpcode(int Opc) {
436 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
437 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
441 bool isIndirectBranchOpcode(int Opc) {
442 return Opc == ARM::BRIND || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
445 /// getInstrPredicate - If instruction is predicated, returns its predicate
446 /// condition, otherwise returns AL. It also returns the condition code
447 /// register by reference.
448 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
450 int getMatchingCondBranchOpcode(int Opc);
452 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
453 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
455 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
456 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
457 unsigned DestReg, unsigned BaseReg, int NumBytes,
458 ARMCC::CondCodes Pred, unsigned PredReg,
459 const ARMBaseInstrInfo &TII);
461 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
462 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
463 unsigned DestReg, unsigned BaseReg, int NumBytes,
464 ARMCC::CondCodes Pred, unsigned PredReg,
465 const ARMBaseInstrInfo &TII);
466 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
467 MachineBasicBlock::iterator &MBBI,
468 unsigned DestReg, unsigned BaseReg,
469 int NumBytes, const TargetInstrInfo &TII,
470 const ARMBaseRegisterInfo& MRI,
474 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
475 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
476 /// offset could not be handled directly in MI, and return the left-over
477 /// portion by reference.
478 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
479 unsigned FrameReg, int &Offset,
480 const ARMBaseInstrInfo &TII);
482 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
483 unsigned FrameReg, int &Offset,
484 const ARMBaseInstrInfo &TII);
486 } // End llvm namespace