Sorry, several patches in one.
[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 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallSet.h"
22
23 namespace llvm {
24   class ARMSubtarget;
25   class ARMBaseRegisterInfo;
26
27 /// ARMII - This namespace holds all of the target specific flags that
28 /// instruction info tracks.
29 ///
30 namespace ARMII {
31   enum {
32     //===------------------------------------------------------------------===//
33     // Instruction Flags.
34
35     //===------------------------------------------------------------------===//
36     // This four-bit field describes the addressing mode used.
37
38     AddrModeMask  = 0x1f,
39     AddrModeNone    = 0,
40     AddrMode1       = 1,
41     AddrMode2       = 2,
42     AddrMode3       = 3,
43     AddrMode4       = 4,
44     AddrMode5       = 5,
45     AddrMode6       = 6,
46     AddrModeT1_1    = 7,
47     AddrModeT1_2    = 8,
48     AddrModeT1_4    = 9,
49     AddrModeT1_s    = 10, // i8 * 4 for pc and sp relative data
50     AddrModeT2_i12  = 11,
51     AddrModeT2_i8   = 12,
52     AddrModeT2_so   = 13,
53     AddrModeT2_pc   = 14, // +/- i12 for pc relative data
54     AddrModeT2_i8s4 = 15, // i8 * 4
55     AddrMode_i12    = 16,
56
57     // Size* - Flags to keep track of the size of an instruction.
58     SizeShift     = 5,
59     SizeMask      = 7 << SizeShift,
60     SizeSpecial   = 1,   // 0 byte pseudo or special case.
61     Size8Bytes    = 2,
62     Size4Bytes    = 3,
63     Size2Bytes    = 4,
64
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.
67     IndexModeShift = 8,
68     IndexModeMask  = 3 << IndexModeShift,
69     IndexModePre   = 1,
70     IndexModePost  = 2,
71     IndexModeUpd   = 3,
72
73     //===------------------------------------------------------------------===//
74     // Instruction encoding formats.
75     //
76     FormShift     = 10,
77     FormMask      = 0x3f << FormShift,
78
79     // Pseudo instructions
80     Pseudo        = 0  << FormShift,
81
82     // Multiply instructions
83     MulFrm        = 1  << FormShift,
84
85     // Branch instructions
86     BrFrm         = 2  << FormShift,
87     BrMiscFrm     = 3  << FormShift,
88
89     // Data Processing instructions
90     DPFrm         = 4  << FormShift,
91     DPSoRegFrm    = 5  << FormShift,
92
93     // Load and Store
94     LdFrm         = 6  << FormShift,
95     StFrm         = 7  << FormShift,
96     LdMiscFrm     = 8  << FormShift,
97     StMiscFrm     = 9  << FormShift,
98     LdStMulFrm    = 10 << FormShift,
99
100     LdStExFrm     = 11 << FormShift,
101
102     // Miscellaneous arithmetic instructions
103     ArithMiscFrm  = 12 << FormShift,
104     SatFrm        = 13 << FormShift,
105
106     // Extend instructions
107     ExtFrm        = 14 << FormShift,
108
109     // VFP formats
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,
120
121     // Thumb format
122     ThumbFrm      = 25 << FormShift,
123
124     // Miscelleaneous format
125     MiscFrm       = 26 << FormShift,
126
127     // NEON formats
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,
143
144     //===------------------------------------------------------------------===//
145     // Misc flags.
146
147     // UnaryDP - Indicates this is a unary data processing instruction, i.e.
148     // it doesn't have a Rn operand.
149     UnaryDP       = 1 << 16,
150
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,
154
155     //===------------------------------------------------------------------===//
156     // Code domain.
157     DomainShift   = 18,
158     DomainMask    = 3 << DomainShift,
159     DomainGeneral = 0 << DomainShift,
160     DomainVFP     = 1 << DomainShift,
161     DomainNEON    = 2 << DomainShift,
162
163     //===------------------------------------------------------------------===//
164     // Field shifts - such shifts are used to set field while generating
165     // machine instructions.
166     //
167     // FIXME: This list will need adjusting/fixing as the MC code emitter
168     // takes shape and the ARMCodeEmitter.cpp bits go away.
169     ShiftTypeShift = 4,
170
171     M_BitShift     = 5,
172     ShiftImmShift  = 5,
173     ShiftShift     = 7,
174     N_BitShift     = 7,
175     ImmHiShift     = 8,
176     SoRotImmShift  = 8,
177     RegRsShift     = 8,
178     ExtRotImmShift = 10,
179     RegRdLoShift   = 12,
180     RegRdShift     = 12,
181     RegRdHiShift   = 16,
182     RegRnShift     = 16,
183     S_BitShift     = 20,
184     W_BitShift     = 21,
185     AM3_I_BitShift = 22,
186     D_BitShift     = 22,
187     U_BitShift     = 23,
188     P_BitShift     = 24,
189     I_BitShift     = 25,
190     CondShift      = 28
191   };
192 }
193
194 class ARMBaseInstrInfo : public TargetInstrInfoImpl {
195   const ARMSubtarget &Subtarget;
196
197 protected:
198   // Can be only subclassed.
199   explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
200
201 public:
202   // Return the non-pre/post incrementing version of 'Opc'. Return 0
203   // if there is not such an opcode.
204   virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
205
206   virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
207                                               MachineBasicBlock::iterator &MBBI,
208                                               LiveVariables *LV) const;
209
210   virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
211   const ARMSubtarget &getSubtarget() const { return Subtarget; }
212
213   ScheduleHazardRecognizer *
214   CreateTargetHazardRecognizer(const TargetMachine *TM,
215                                const ScheduleDAG *DAG) const;
216
217   ScheduleHazardRecognizer *
218   CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
219                                      const ScheduleDAG *DAG) const;
220
221   // Branch analysis.
222   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
223                              MachineBasicBlock *&FBB,
224                              SmallVectorImpl<MachineOperand> &Cond,
225                              bool AllowModify = false) const;
226   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
227   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
228                                 MachineBasicBlock *FBB,
229                                 const SmallVectorImpl<MachineOperand> &Cond,
230                                 DebugLoc DL) const;
231
232   virtual
233   bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
234
235   // Predication support.
236   bool isPredicated(const MachineInstr *MI) const {
237     int PIdx = MI->findFirstPredOperandIdx();
238     return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
239   }
240
241   ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
242     int PIdx = MI->findFirstPredOperandIdx();
243     return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
244                       : ARMCC::AL;
245   }
246
247   virtual
248   bool PredicateInstruction(MachineInstr *MI,
249                             const SmallVectorImpl<MachineOperand> &Pred) const;
250
251   virtual
252   bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
253                          const SmallVectorImpl<MachineOperand> &Pred2) const;
254
255   virtual bool DefinesPredicate(MachineInstr *MI,
256                                 std::vector<MachineOperand> &Pred) const;
257
258   virtual bool isPredicable(MachineInstr *MI) const;
259
260   /// GetInstSize - Returns the size of the specified MachineInstr.
261   ///
262   virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
263
264   virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
265                                        int &FrameIndex) const;
266   virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
267                                       int &FrameIndex) const;
268
269   virtual void copyPhysReg(MachineBasicBlock &MBB,
270                            MachineBasicBlock::iterator I, DebugLoc DL,
271                            unsigned DestReg, unsigned SrcReg,
272                            bool KillSrc) const;
273
274   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
275                                    MachineBasicBlock::iterator MBBI,
276                                    unsigned SrcReg, bool isKill, int FrameIndex,
277                                    const TargetRegisterClass *RC,
278                                    const TargetRegisterInfo *TRI) const;
279
280   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
281                                     MachineBasicBlock::iterator MBBI,
282                                     unsigned DestReg, int FrameIndex,
283                                     const TargetRegisterClass *RC,
284                                     const TargetRegisterInfo *TRI) const;
285
286   virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
287                                                  int FrameIx,
288                                                  uint64_t Offset,
289                                                  const MDNode *MDPtr,
290                                                  DebugLoc DL) const;
291
292   virtual void reMaterialize(MachineBasicBlock &MBB,
293                              MachineBasicBlock::iterator MI,
294                              unsigned DestReg, unsigned SubIdx,
295                              const MachineInstr *Orig,
296                              const TargetRegisterInfo &TRI) const;
297
298   MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
299
300   virtual bool produceSameValue(const MachineInstr *MI0,
301                                 const MachineInstr *MI1,
302                                 const MachineRegisterInfo *MRI) const;
303
304   /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
305   /// determine if two loads are loading from the same base address. It should
306   /// only return true if the base pointers are the same and the only
307   /// differences between the two addresses is the offset. It also returns the
308   /// offsets by reference.
309   virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
310                                        int64_t &Offset1, int64_t &Offset2)const;
311
312   /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
313   /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
314   /// be scheduled togther. On some targets if two loads are loading from
315   /// addresses in the same cache line, it's better if they are scheduled
316   /// together. This function takes two integers that represent the load offsets
317   /// from the common base address. It returns true if it decides it's desirable
318   /// to schedule the two loads together. "NumLoads" is the number of loads that
319   /// have already been scheduled after Load1.
320   virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
321                                        int64_t Offset1, int64_t Offset2,
322                                        unsigned NumLoads) const;
323
324   virtual bool isSchedulingBoundary(const MachineInstr *MI,
325                                     const MachineBasicBlock *MBB,
326                                     const MachineFunction &MF) const;
327
328   virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
329                                    unsigned NumCyles, unsigned ExtraPredCycles,
330                                    float Prob, float Confidence) const;
331
332   virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
333                                    unsigned NumT, unsigned ExtraT,
334                                    MachineBasicBlock &FMBB,
335                                    unsigned NumF, unsigned ExtraF,
336                                    float Probability, float Confidence) const;
337
338   virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
339                                          unsigned NumCyles,
340                                          float Probability,
341                                          float Confidence) const {
342     return NumCyles == 1;
343   }
344
345   /// AnalyzeCompare - For a comparison instruction, return the source register
346   /// in SrcReg and the value it compares against in CmpValue. Return true if
347   /// the comparison instruction can be analyzed.
348   virtual bool AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
349                               int &CmpMask, int &CmpValue) const;
350
351   /// OptimizeCompareInstr - Convert the instruction to set the zero flag so
352   /// that we can remove a "comparison with zero".
353   virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
354                                     int CmpMask, int CmpValue,
355                                     const MachineRegisterInfo *MRI) const;
356
357   /// FoldImmediate - 'Reg' is known to be defined by a move immediate
358   /// instruction, try to fold the immediate into the use instruction.
359   virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
360                              unsigned Reg, MachineRegisterInfo *MRI) const;
361
362   virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
363                                   const MachineInstr *MI) const;
364
365   virtual
366   int getOperandLatency(const InstrItineraryData *ItinData,
367                         const MachineInstr *DefMI, unsigned DefIdx,
368                         const MachineInstr *UseMI, unsigned UseIdx) const;
369   virtual
370   int getOperandLatency(const InstrItineraryData *ItinData,
371                         SDNode *DefNode, unsigned DefIdx,
372                         SDNode *UseNode, unsigned UseIdx) const;
373 private:
374   int getVLDMDefCycle(const InstrItineraryData *ItinData,
375                       const TargetInstrDesc &DefTID,
376                       unsigned DefClass,
377                       unsigned DefIdx, unsigned DefAlign) const;
378   int getLDMDefCycle(const InstrItineraryData *ItinData,
379                      const TargetInstrDesc &DefTID,
380                      unsigned DefClass,
381                      unsigned DefIdx, unsigned DefAlign) const;
382   int getVSTMUseCycle(const InstrItineraryData *ItinData,
383                       const TargetInstrDesc &UseTID,
384                       unsigned UseClass,
385                       unsigned UseIdx, unsigned UseAlign) const;
386   int getSTMUseCycle(const InstrItineraryData *ItinData,
387                      const TargetInstrDesc &UseTID,
388                      unsigned UseClass,
389                      unsigned UseIdx, unsigned UseAlign) const;
390   int getOperandLatency(const InstrItineraryData *ItinData,
391                         const TargetInstrDesc &DefTID,
392                         unsigned DefIdx, unsigned DefAlign,
393                         const TargetInstrDesc &UseTID,
394                         unsigned UseIdx, unsigned UseAlign) const;
395
396   int getInstrLatency(const InstrItineraryData *ItinData,
397                       const MachineInstr *MI, unsigned *PredCost = 0) const;
398
399   int getInstrLatency(const InstrItineraryData *ItinData,
400                       SDNode *Node) const;
401
402   bool hasHighOperandLatency(const InstrItineraryData *ItinData,
403                              const MachineRegisterInfo *MRI,
404                              const MachineInstr *DefMI, unsigned DefIdx,
405                              const MachineInstr *UseMI, unsigned UseIdx) const;
406   bool hasLowDefLatency(const InstrItineraryData *ItinData,
407                         const MachineInstr *DefMI, unsigned DefIdx) const;
408
409 private:
410   /// Modeling special VFP / NEON fp MLA / MLS hazards.
411
412   /// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal
413   /// MLx table.
414   DenseMap<unsigned, unsigned> MLxEntryMap;
415
416   /// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause
417   /// stalls when scheduled together with fp MLA / MLS opcodes.
418   SmallSet<unsigned, 16> MLxHazardOpcodes;
419
420 public:
421   /// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS
422   /// instruction.
423   bool isFpMLxInstruction(unsigned Opcode) const {
424     return MLxEntryMap.count(Opcode);
425   }
426
427   /// isFpMLxInstruction - This version also returns the multiply opcode and the
428   /// addition / subtraction opcode to expand to. Return true for 'HasLane' for
429   /// the MLX instructions with an extra lane operand.
430   bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
431                           unsigned &AddSubOpc, bool &NegAcc,
432                           bool &HasLane) const;
433
434   /// canCauseFpMLxStall - Return true if an instruction of the specified opcode
435   /// will cause stalls when scheduled after (within 4-cycle window) a fp
436   /// MLA / MLS instruction.
437   bool canCauseFpMLxStall(unsigned Opcode) const {
438     return MLxHazardOpcodes.count(Opcode);
439   }
440 };
441
442 static inline
443 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
444   return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
445 }
446
447 static inline
448 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
449   return MIB.addReg(0);
450 }
451
452 static inline
453 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
454                                           bool isDead = false) {
455   return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
456 }
457
458 static inline
459 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
460   return MIB.addReg(0);
461 }
462
463 static inline
464 bool isUncondBranchOpcode(int Opc) {
465   return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
466 }
467
468 static inline
469 bool isCondBranchOpcode(int Opc) {
470   return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
471 }
472
473 static inline
474 bool isJumpTableBranchOpcode(int Opc) {
475   return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
476     Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
477 }
478
479 static inline
480 bool isIndirectBranchOpcode(int Opc) {
481   return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
482 }
483
484 /// getInstrPredicate - If instruction is predicated, returns its predicate
485 /// condition, otherwise returns AL. It also returns the condition code
486 /// register by reference.
487 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
488
489 int getMatchingCondBranchOpcode(int Opc);
490
491 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
492 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
493 /// code.
494 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
495                              MachineBasicBlock::iterator &MBBI, DebugLoc dl,
496                              unsigned DestReg, unsigned BaseReg, int NumBytes,
497                              ARMCC::CondCodes Pred, unsigned PredReg,
498                              const ARMBaseInstrInfo &TII);
499
500 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
501                             MachineBasicBlock::iterator &MBBI, DebugLoc dl,
502                             unsigned DestReg, unsigned BaseReg, int NumBytes,
503                             ARMCC::CondCodes Pred, unsigned PredReg,
504                             const ARMBaseInstrInfo &TII);
505 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
506                                MachineBasicBlock::iterator &MBBI,
507                                unsigned DestReg, unsigned BaseReg,
508                                int NumBytes, const TargetInstrInfo &TII,
509                                const ARMBaseRegisterInfo& MRI,
510                                DebugLoc dl);
511
512
513 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
514 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
515 /// offset could not be handled directly in MI, and return the left-over
516 /// portion by reference.
517 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
518                           unsigned FrameReg, int &Offset,
519                           const ARMBaseInstrInfo &TII);
520
521 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
522                          unsigned FrameReg, int &Offset,
523                          const ARMBaseInstrInfo &TII);
524
525 } // End llvm namespace
526
527 #endif