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 void emitPopInst(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
221 const std::vector<CalleeSavedInfo> &CSI, unsigned Opc,
222 bool isVarArg, bool(*Func)(unsigned, bool)) const;
223 void emitPushInst(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
224 const std::vector<CalleeSavedInfo> &CSI, unsigned Opc,
225 bool(*Func)(unsigned, bool)) const;
230 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
231 MachineBasicBlock *&FBB,
232 SmallVectorImpl<MachineOperand> &Cond,
233 bool AllowModify = false) const;
234 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
235 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
236 MachineBasicBlock *FBB,
237 const SmallVectorImpl<MachineOperand> &Cond,
241 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
243 // Predication support.
244 bool isPredicated(const MachineInstr *MI) const {
245 int PIdx = MI->findFirstPredOperandIdx();
246 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
249 ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
250 int PIdx = MI->findFirstPredOperandIdx();
251 return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
256 bool PredicateInstruction(MachineInstr *MI,
257 const SmallVectorImpl<MachineOperand> &Pred) const;
260 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
261 const SmallVectorImpl<MachineOperand> &Pred2) const;
263 virtual bool DefinesPredicate(MachineInstr *MI,
264 std::vector<MachineOperand> &Pred) const;
266 virtual bool isPredicable(MachineInstr *MI) const;
268 /// GetInstSize - Returns the size of the specified MachineInstr.
270 virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
272 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
273 int &FrameIndex) const;
274 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
275 int &FrameIndex) const;
277 virtual void copyPhysReg(MachineBasicBlock &MBB,
278 MachineBasicBlock::iterator I, DebugLoc DL,
279 unsigned DestReg, unsigned SrcReg,
282 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
283 MachineBasicBlock::iterator MBBI,
284 unsigned SrcReg, bool isKill, int FrameIndex,
285 const TargetRegisterClass *RC,
286 const TargetRegisterInfo *TRI) const;
288 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
289 MachineBasicBlock::iterator MBBI,
290 unsigned DestReg, int FrameIndex,
291 const TargetRegisterClass *RC,
292 const TargetRegisterInfo *TRI) const;
294 virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
300 virtual void reMaterialize(MachineBasicBlock &MBB,
301 MachineBasicBlock::iterator MI,
302 unsigned DestReg, unsigned SubIdx,
303 const MachineInstr *Orig,
304 const TargetRegisterInfo &TRI) const;
306 MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
308 virtual bool produceSameValue(const MachineInstr *MI0,
309 const MachineInstr *MI1) const;
311 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
312 /// determine if two loads are loading from the same base address. It should
313 /// only return true if the base pointers are the same and the only
314 /// differences between the two addresses is the offset. It also returns the
315 /// offsets by reference.
316 virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
317 int64_t &Offset1, int64_t &Offset2)const;
319 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
320 /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
321 /// be scheduled togther. On some targets if two loads are loading from
322 /// addresses in the same cache line, it's better if they are scheduled
323 /// together. This function takes two integers that represent the load offsets
324 /// from the common base address. It returns true if it decides it's desirable
325 /// to schedule the two loads together. "NumLoads" is the number of loads that
326 /// have already been scheduled after Load1.
327 virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
328 int64_t Offset1, int64_t Offset2,
329 unsigned NumLoads) const;
331 virtual bool isSchedulingBoundary(const MachineInstr *MI,
332 const MachineBasicBlock *MBB,
333 const MachineFunction &MF) const;
335 virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
336 unsigned NumCyles, unsigned ExtraPredCycles,
337 float Prob, float Confidence) const;
339 virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
340 unsigned NumT, unsigned ExtraT,
341 MachineBasicBlock &FMBB,
342 unsigned NumF, unsigned ExtraF,
343 float Probability, float Confidence) const;
345 virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
348 float Confidence) const {
349 return NumCyles == 1;
352 /// AnalyzeCompare - For a comparison instruction, return the source register
353 /// in SrcReg and the value it compares against in CmpValue. Return true if
354 /// the comparison instruction can be analyzed.
355 virtual bool AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
356 int &CmpMask, int &CmpValue) const;
358 /// OptimizeCompareInstr - Convert the instruction to set the zero flag so
359 /// that we can remove a "comparison with zero".
360 virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
361 int CmpMask, int CmpValue,
362 const MachineRegisterInfo *MRI) const;
364 /// FoldImmediate - 'Reg' is known to be defined by a move immediate
365 /// instruction, try to fold the immediate into the use instruction.
366 virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
367 unsigned Reg, MachineRegisterInfo *MRI) const;
369 virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
370 const MachineInstr *MI) const;
373 int getOperandLatency(const InstrItineraryData *ItinData,
374 const MachineInstr *DefMI, unsigned DefIdx,
375 const MachineInstr *UseMI, unsigned UseIdx) const;
377 int getOperandLatency(const InstrItineraryData *ItinData,
378 SDNode *DefNode, unsigned DefIdx,
379 SDNode *UseNode, unsigned UseIdx) const;
381 int getVLDMDefCycle(const InstrItineraryData *ItinData,
382 const TargetInstrDesc &DefTID,
384 unsigned DefIdx, unsigned DefAlign) const;
385 int getLDMDefCycle(const InstrItineraryData *ItinData,
386 const TargetInstrDesc &DefTID,
388 unsigned DefIdx, unsigned DefAlign) const;
389 int getVSTMUseCycle(const InstrItineraryData *ItinData,
390 const TargetInstrDesc &UseTID,
392 unsigned UseIdx, unsigned UseAlign) const;
393 int getSTMUseCycle(const InstrItineraryData *ItinData,
394 const TargetInstrDesc &UseTID,
396 unsigned UseIdx, unsigned UseAlign) const;
397 int getOperandLatency(const InstrItineraryData *ItinData,
398 const TargetInstrDesc &DefTID,
399 unsigned DefIdx, unsigned DefAlign,
400 const TargetInstrDesc &UseTID,
401 unsigned UseIdx, unsigned UseAlign) const;
403 int getInstrLatency(const InstrItineraryData *ItinData,
404 const MachineInstr *MI, unsigned *PredCost = 0) const;
406 int getInstrLatency(const InstrItineraryData *ItinData,
409 bool hasHighOperandLatency(const InstrItineraryData *ItinData,
410 const MachineRegisterInfo *MRI,
411 const MachineInstr *DefMI, unsigned DefIdx,
412 const MachineInstr *UseMI, unsigned UseIdx) const;
413 bool hasLowDefLatency(const InstrItineraryData *ItinData,
414 const MachineInstr *DefMI, unsigned DefIdx) const;
418 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
419 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
423 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
424 return MIB.addReg(0);
428 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
429 bool isDead = false) {
430 return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
434 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
435 return MIB.addReg(0);
439 bool isUncondBranchOpcode(int Opc) {
440 return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
444 bool isCondBranchOpcode(int Opc) {
445 return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
449 bool isJumpTableBranchOpcode(int Opc) {
450 return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
451 Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
455 bool isIndirectBranchOpcode(int Opc) {
456 return Opc == ARM::BRIND || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
459 /// getInstrPredicate - If instruction is predicated, returns its predicate
460 /// condition, otherwise returns AL. It also returns the condition code
461 /// register by reference.
462 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
464 int getMatchingCondBranchOpcode(int Opc);
466 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
467 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
469 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
470 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
471 unsigned DestReg, unsigned BaseReg, int NumBytes,
472 ARMCC::CondCodes Pred, unsigned PredReg,
473 const ARMBaseInstrInfo &TII);
475 void emitT2RegPlusImmediate(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);
480 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
481 MachineBasicBlock::iterator &MBBI,
482 unsigned DestReg, unsigned BaseReg,
483 int NumBytes, const TargetInstrInfo &TII,
484 const ARMBaseRegisterInfo& MRI,
488 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
489 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
490 /// offset could not be handled directly in MI, and return the left-over
491 /// portion by reference.
492 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
493 unsigned FrameReg, int &Offset,
494 const ARMBaseInstrInfo &TII);
496 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
497 unsigned FrameReg, int &Offset,
498 const ARMBaseInstrInfo &TII);
500 } // End llvm namespace