Rewrite stack callee saved spills and restores to use push/pop instructions.
[oota-llvm.git] / lib / Target / ARM / ARMBaseInstrInfo.h
1 //===- ARMBaseInstrInfo.h - ARM Base Instruction Information ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the Base ARM implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef ARMBASEINSTRUCTIONINFO_H
15 #define ARMBASEINSTRUCTIONINFO_H
16
17 #include "ARM.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20
21 namespace llvm {
22   class ARMSubtarget;
23   class ARMBaseRegisterInfo;
24
25 /// ARMII - This namespace holds all of the target specific flags that
26 /// instruction info tracks.
27 ///
28 namespace ARMII {
29   enum {
30     //===------------------------------------------------------------------===//
31     // Instruction Flags.
32
33     //===------------------------------------------------------------------===//
34     // This four-bit field describes the addressing mode used.
35
36     AddrModeMask  = 0x1f,
37     AddrModeNone    = 0,
38     AddrMode1       = 1,
39     AddrMode2       = 2,
40     AddrMode3       = 3,
41     AddrMode4       = 4,
42     AddrMode5       = 5,
43     AddrMode6       = 6,
44     AddrModeT1_1    = 7,
45     AddrModeT1_2    = 8,
46     AddrModeT1_4    = 9,
47     AddrModeT1_s    = 10, // i8 * 4 for pc and sp relative data
48     AddrModeT2_i12  = 11,
49     AddrModeT2_i8   = 12,
50     AddrModeT2_so   = 13,
51     AddrModeT2_pc   = 14, // +/- i12 for pc relative data
52     AddrModeT2_i8s4 = 15, // i8 * 4
53     AddrMode_i12    = 16,
54
55     // Size* - Flags to keep track of the size of an instruction.
56     SizeShift     = 5,
57     SizeMask      = 7 << SizeShift,
58     SizeSpecial   = 1,   // 0 byte pseudo or special case.
59     Size8Bytes    = 2,
60     Size4Bytes    = 3,
61     Size2Bytes    = 4,
62
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.
65     IndexModeShift = 8,
66     IndexModeMask  = 3 << IndexModeShift,
67     IndexModePre   = 1,
68     IndexModePost  = 2,
69     IndexModeUpd   = 3,
70
71     //===------------------------------------------------------------------===//
72     // Instruction encoding formats.
73     //
74     FormShift     = 10,
75     FormMask      = 0x3f << FormShift,
76
77     // Pseudo instructions
78     Pseudo        = 0  << FormShift,
79
80     // Multiply instructions
81     MulFrm        = 1  << FormShift,
82
83     // Branch instructions
84     BrFrm         = 2  << FormShift,
85     BrMiscFrm     = 3  << FormShift,
86
87     // Data Processing instructions
88     DPFrm         = 4  << FormShift,
89     DPSoRegFrm    = 5  << FormShift,
90
91     // Load and Store
92     LdFrm         = 6  << FormShift,
93     StFrm         = 7  << FormShift,
94     LdMiscFrm     = 8  << FormShift,
95     StMiscFrm     = 9  << FormShift,
96     LdStMulFrm    = 10 << FormShift,
97
98     LdStExFrm     = 11 << FormShift,
99
100     // Miscellaneous arithmetic instructions
101     ArithMiscFrm  = 12 << FormShift,
102     SatFrm        = 13 << FormShift,
103
104     // Extend instructions
105     ExtFrm        = 14 << FormShift,
106
107     // VFP formats
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,
118
119     // Thumb format
120     ThumbFrm      = 25 << FormShift,
121
122     // Miscelleaneous format
123     MiscFrm       = 26 << FormShift,
124
125     // NEON formats
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,
141
142     //===------------------------------------------------------------------===//
143     // Misc flags.
144
145     // UnaryDP - Indicates this is a unary data processing instruction, i.e.
146     // it doesn't have a Rn operand.
147     UnaryDP       = 1 << 16,
148
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,
152
153     //===------------------------------------------------------------------===//
154     // Code domain.
155     DomainShift   = 18,
156     DomainMask    = 3 << DomainShift,
157     DomainGeneral = 0 << DomainShift,
158     DomainVFP     = 1 << DomainShift,
159     DomainNEON    = 2 << DomainShift,
160
161     //===------------------------------------------------------------------===//
162     // Field shifts - such shifts are used to set field while generating
163     // machine instructions.
164     //
165     // FIXME: This list will need adjusting/fixing as the MC code emitter
166     // takes shape and the ARMCodeEmitter.cpp bits go away.
167     ShiftTypeShift = 4,
168
169     M_BitShift     = 5,
170     ShiftImmShift  = 5,
171     ShiftShift     = 7,
172     N_BitShift     = 7,
173     ImmHiShift     = 8,
174     SoRotImmShift  = 8,
175     RegRsShift     = 8,
176     ExtRotImmShift = 10,
177     RegRdLoShift   = 12,
178     RegRdShift     = 12,
179     RegRdHiShift   = 16,
180     RegRnShift     = 16,
181     S_BitShift     = 20,
182     W_BitShift     = 21,
183     AM3_I_BitShift = 22,
184     D_BitShift     = 22,
185     U_BitShift     = 23,
186     P_BitShift     = 24,
187     I_BitShift     = 25,
188     CondShift      = 28
189   };
190 }
191
192 class ARMBaseInstrInfo : public TargetInstrInfoImpl {
193   const ARMSubtarget &Subtarget;
194 protected:
195   // Can be only subclassed.
196   explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
197 public:
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;
201
202   virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
203                                               MachineBasicBlock::iterator &MBBI,
204                                               LiveVariables *LV) const;
205
206   virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
207   const ARMSubtarget &getSubtarget() const { return Subtarget; }
208
209   bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
210                                  MachineBasicBlock::iterator MI,
211                                  const std::vector<CalleeSavedInfo> &CSI,
212                                  const TargetRegisterInfo *TRI) const;
213
214   bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
215                                    MachineBasicBlock::iterator MI,
216                                    const std::vector<CalleeSavedInfo> &CSI,
217                                    const TargetRegisterInfo *TRI) const;
218
219 private:
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;
226   
227   
228 public:
229   // Branch analysis.
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,
238                                 DebugLoc DL) const;
239
240   virtual
241   bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
242
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;
247   }
248
249   ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
250     int PIdx = MI->findFirstPredOperandIdx();
251     return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
252                       : ARMCC::AL;
253   }
254
255   virtual
256   bool PredicateInstruction(MachineInstr *MI,
257                             const SmallVectorImpl<MachineOperand> &Pred) const;
258
259   virtual
260   bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
261                          const SmallVectorImpl<MachineOperand> &Pred2) const;
262
263   virtual bool DefinesPredicate(MachineInstr *MI,
264                                 std::vector<MachineOperand> &Pred) const;
265
266   virtual bool isPredicable(MachineInstr *MI) const;
267
268   /// GetInstSize - Returns the size of the specified MachineInstr.
269   ///
270   virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
271
272   virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
273                                        int &FrameIndex) const;
274   virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
275                                       int &FrameIndex) const;
276
277   virtual void copyPhysReg(MachineBasicBlock &MBB,
278                            MachineBasicBlock::iterator I, DebugLoc DL,
279                            unsigned DestReg, unsigned SrcReg,
280                            bool KillSrc) const;
281
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;
287
288   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
289                                     MachineBasicBlock::iterator MBBI,
290                                     unsigned DestReg, int FrameIndex,
291                                     const TargetRegisterClass *RC,
292                                     const TargetRegisterInfo *TRI) const;
293
294   virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
295                                                  int FrameIx,
296                                                  uint64_t Offset,
297                                                  const MDNode *MDPtr,
298                                                  DebugLoc DL) const;
299
300   virtual void reMaterialize(MachineBasicBlock &MBB,
301                              MachineBasicBlock::iterator MI,
302                              unsigned DestReg, unsigned SubIdx,
303                              const MachineInstr *Orig,
304                              const TargetRegisterInfo &TRI) const;
305
306   MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
307
308   virtual bool produceSameValue(const MachineInstr *MI0,
309                                 const MachineInstr *MI1) const;
310
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;
318
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;
330
331   virtual bool isSchedulingBoundary(const MachineInstr *MI,
332                                     const MachineBasicBlock *MBB,
333                                     const MachineFunction &MF) const;
334
335   virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
336                                    unsigned NumCyles, unsigned ExtraPredCycles,
337                                    float Prob, float Confidence) const;
338
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;
344
345   virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
346                                          unsigned NumCyles,
347                                          float Probability,
348                                          float Confidence) const {
349     return NumCyles == 1;
350   }
351
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;
357
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;
363
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;
368
369   virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
370                                   const MachineInstr *MI) const;
371
372   virtual
373   int getOperandLatency(const InstrItineraryData *ItinData,
374                         const MachineInstr *DefMI, unsigned DefIdx,
375                         const MachineInstr *UseMI, unsigned UseIdx) const;
376   virtual
377   int getOperandLatency(const InstrItineraryData *ItinData,
378                         SDNode *DefNode, unsigned DefIdx,
379                         SDNode *UseNode, unsigned UseIdx) const;
380 private:
381   int getVLDMDefCycle(const InstrItineraryData *ItinData,
382                       const TargetInstrDesc &DefTID,
383                       unsigned DefClass,
384                       unsigned DefIdx, unsigned DefAlign) const;
385   int getLDMDefCycle(const InstrItineraryData *ItinData,
386                      const TargetInstrDesc &DefTID,
387                      unsigned DefClass,
388                      unsigned DefIdx, unsigned DefAlign) const;
389   int getVSTMUseCycle(const InstrItineraryData *ItinData,
390                       const TargetInstrDesc &UseTID,
391                       unsigned UseClass,
392                       unsigned UseIdx, unsigned UseAlign) const;
393   int getSTMUseCycle(const InstrItineraryData *ItinData,
394                      const TargetInstrDesc &UseTID,
395                      unsigned UseClass,
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;
402
403   int getInstrLatency(const InstrItineraryData *ItinData,
404                       const MachineInstr *MI, unsigned *PredCost = 0) const;
405
406   int getInstrLatency(const InstrItineraryData *ItinData,
407                       SDNode *Node) const;
408
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;
415 };
416
417 static inline
418 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
419   return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
420 }
421
422 static inline
423 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
424   return MIB.addReg(0);
425 }
426
427 static inline
428 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
429                                           bool isDead = false) {
430   return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
431 }
432
433 static inline
434 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
435   return MIB.addReg(0);
436 }
437
438 static inline
439 bool isUncondBranchOpcode(int Opc) {
440   return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
441 }
442
443 static inline
444 bool isCondBranchOpcode(int Opc) {
445   return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
446 }
447
448 static inline
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;
452 }
453
454 static inline
455 bool isIndirectBranchOpcode(int Opc) {
456   return Opc == ARM::BRIND || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
457 }
458
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);
463
464 int getMatchingCondBranchOpcode(int Opc);
465
466 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
467 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
468 /// code.
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);
474
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,
485                                DebugLoc dl);
486
487
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);
495
496 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
497                          unsigned FrameReg, int &Offset,
498                          const ARMBaseInstrInfo &TII);
499
500 } // End llvm namespace
501
502 #endif