- Add MachineInstrBundle.h and MachineInstrBundle.cpp. This includes a function
[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 #define GET_INSTRINFO_HEADER
24 #include "ARMGenInstrInfo.inc"
25
26 namespace llvm {
27   class ARMSubtarget;
28   class ARMBaseRegisterInfo;
29
30 class ARMBaseInstrInfo : public ARMGenInstrInfo {
31   const ARMSubtarget &Subtarget;
32
33 protected:
34   // Can be only subclassed.
35   explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
36
37 public:
38   // Return the non-pre/post incrementing version of 'Opc'. Return 0
39   // if there is not such an opcode.
40   virtual unsigned getUnindexedOpcode(unsigned Opc) const =0;
41
42   virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
43                                               MachineBasicBlock::iterator &MBBI,
44                                               LiveVariables *LV) const;
45
46   virtual const ARMBaseRegisterInfo &getRegisterInfo() const =0;
47   const ARMSubtarget &getSubtarget() const { return Subtarget; }
48
49   ScheduleHazardRecognizer *
50   CreateTargetHazardRecognizer(const TargetMachine *TM,
51                                const ScheduleDAG *DAG) const;
52
53   ScheduleHazardRecognizer *
54   CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
55                                      const ScheduleDAG *DAG) const;
56
57   // Branch analysis.
58   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
59                              MachineBasicBlock *&FBB,
60                              SmallVectorImpl<MachineOperand> &Cond,
61                              bool AllowModify = false) const;
62   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const;
63   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
64                                 MachineBasicBlock *FBB,
65                                 const SmallVectorImpl<MachineOperand> &Cond,
66                                 DebugLoc DL) const;
67
68   virtual
69   bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const;
70
71   // Predication support.
72   bool isPredicated(const MachineInstr *MI) const;
73
74   ARMCC::CondCodes getPredicate(const MachineInstr *MI) const {
75     int PIdx = MI->findFirstPredOperandIdx();
76     return PIdx != -1 ? (ARMCC::CondCodes)MI->getOperand(PIdx).getImm()
77                       : ARMCC::AL;
78   }
79
80   virtual
81   bool PredicateInstruction(MachineInstr *MI,
82                             const SmallVectorImpl<MachineOperand> &Pred) const;
83
84   virtual
85   bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
86                          const SmallVectorImpl<MachineOperand> &Pred2) const;
87
88   virtual bool DefinesPredicate(MachineInstr *MI,
89                                 std::vector<MachineOperand> &Pred) const;
90
91   virtual bool isPredicable(MachineInstr *MI) const;
92
93   /// GetInstSize - Returns the size of the specified MachineInstr.
94   ///
95   virtual unsigned GetInstSizeInBytes(const MachineInstr* MI) const;
96
97   virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
98                                        int &FrameIndex) const;
99   virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
100                                       int &FrameIndex) const;
101   virtual unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
102                                              int &FrameIndex) const;
103   virtual unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
104                                             int &FrameIndex) const;
105
106   virtual void copyPhysReg(MachineBasicBlock &MBB,
107                            MachineBasicBlock::iterator I, DebugLoc DL,
108                            unsigned DestReg, unsigned SrcReg,
109                            bool KillSrc) const;
110
111   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
112                                    MachineBasicBlock::iterator MBBI,
113                                    unsigned SrcReg, bool isKill, int FrameIndex,
114                                    const TargetRegisterClass *RC,
115                                    const TargetRegisterInfo *TRI) const;
116
117   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
118                                     MachineBasicBlock::iterator MBBI,
119                                     unsigned DestReg, int FrameIndex,
120                                     const TargetRegisterClass *RC,
121                                     const TargetRegisterInfo *TRI) const;
122
123   virtual bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const;
124
125   virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
126                                                  int FrameIx,
127                                                  uint64_t Offset,
128                                                  const MDNode *MDPtr,
129                                                  DebugLoc DL) const;
130
131   virtual void reMaterialize(MachineBasicBlock &MBB,
132                              MachineBasicBlock::iterator MI,
133                              unsigned DestReg, unsigned SubIdx,
134                              const MachineInstr *Orig,
135                              const TargetRegisterInfo &TRI) const;
136
137   MachineInstr *duplicate(MachineInstr *Orig, MachineFunction &MF) const;
138
139   virtual bool produceSameValue(const MachineInstr *MI0,
140                                 const MachineInstr *MI1,
141                                 const MachineRegisterInfo *MRI) const;
142
143   /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
144   /// determine if two loads are loading from the same base address. It should
145   /// only return true if the base pointers are the same and the only
146   /// differences between the two addresses is the offset. It also returns the
147   /// offsets by reference.
148   virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
149                                        int64_t &Offset1, int64_t &Offset2)const;
150
151   /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
152   /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads
153   /// should be scheduled togther. On some targets if two loads are loading from
154   /// addresses in the same cache line, it's better if they are scheduled
155   /// together. This function takes two integers that represent the load offsets
156   /// from the common base address. It returns true if it decides it's desirable
157   /// to schedule the two loads together. "NumLoads" is the number of loads that
158   /// have already been scheduled after Load1.
159   virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
160                                        int64_t Offset1, int64_t Offset2,
161                                        unsigned NumLoads) const;
162
163   virtual bool isSchedulingBoundary(const MachineInstr *MI,
164                                     const MachineBasicBlock *MBB,
165                                     const MachineFunction &MF) const;
166
167   virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB,
168                                    unsigned NumCycles, unsigned ExtraPredCycles,
169                                    const BranchProbability &Probability) const;
170
171   virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
172                                    unsigned NumT, unsigned ExtraT,
173                                    MachineBasicBlock &FMBB,
174                                    unsigned NumF, unsigned ExtraF,
175                                    const BranchProbability &Probability) const;
176
177   virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
178                                          unsigned NumCycles,
179                                          const BranchProbability
180                                            &Probability) const {
181     return NumCycles == 1;
182   }
183
184   /// AnalyzeCompare - For a comparison instruction, return the source register
185   /// in SrcReg and the value it compares against in CmpValue. Return true if
186   /// the comparison instruction can be analyzed.
187   virtual bool AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
188                               int &CmpMask, int &CmpValue) const;
189
190   /// OptimizeCompareInstr - Convert the instruction to set the zero flag so
191   /// that we can remove a "comparison with zero".
192   virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
193                                     int CmpMask, int CmpValue,
194                                     const MachineRegisterInfo *MRI) const;
195
196   /// FoldImmediate - 'Reg' is known to be defined by a move immediate
197   /// instruction, try to fold the immediate into the use instruction.
198   virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
199                              unsigned Reg, MachineRegisterInfo *MRI) const;
200
201   virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
202                                   const MachineInstr *MI) const;
203
204   virtual
205   int getOperandLatency(const InstrItineraryData *ItinData,
206                         const MachineInstr *DefMI, unsigned DefIdx,
207                         const MachineInstr *UseMI, unsigned UseIdx) const;
208   virtual
209   int getOperandLatency(const InstrItineraryData *ItinData,
210                         SDNode *DefNode, unsigned DefIdx,
211                         SDNode *UseNode, unsigned UseIdx) const;
212
213   /// VFP/NEON execution domains.
214   std::pair<uint16_t, uint16_t>
215   getExecutionDomain(const MachineInstr *MI) const;
216   void setExecutionDomain(MachineInstr *MI, unsigned Domain) const;
217
218 private:
219   unsigned getInstBundleLength(const MachineInstr *MI) const;
220
221   int getVLDMDefCycle(const InstrItineraryData *ItinData,
222                       const MCInstrDesc &DefMCID,
223                       unsigned DefClass,
224                       unsigned DefIdx, unsigned DefAlign) const;
225   int getLDMDefCycle(const InstrItineraryData *ItinData,
226                      const MCInstrDesc &DefMCID,
227                      unsigned DefClass,
228                      unsigned DefIdx, unsigned DefAlign) const;
229   int getVSTMUseCycle(const InstrItineraryData *ItinData,
230                       const MCInstrDesc &UseMCID,
231                       unsigned UseClass,
232                       unsigned UseIdx, unsigned UseAlign) const;
233   int getSTMUseCycle(const InstrItineraryData *ItinData,
234                      const MCInstrDesc &UseMCID,
235                      unsigned UseClass,
236                      unsigned UseIdx, unsigned UseAlign) const;
237   int getOperandLatency(const InstrItineraryData *ItinData,
238                         const MCInstrDesc &DefMCID,
239                         unsigned DefIdx, unsigned DefAlign,
240                         const MCInstrDesc &UseMCID,
241                         unsigned UseIdx, unsigned UseAlign) const;
242
243   int getInstrLatency(const InstrItineraryData *ItinData,
244                       const MachineInstr *MI, unsigned *PredCost = 0) const;
245
246   int getInstrLatency(const InstrItineraryData *ItinData,
247                       SDNode *Node) const;
248
249   bool hasHighOperandLatency(const InstrItineraryData *ItinData,
250                              const MachineRegisterInfo *MRI,
251                              const MachineInstr *DefMI, unsigned DefIdx,
252                              const MachineInstr *UseMI, unsigned UseIdx) const;
253   bool hasLowDefLatency(const InstrItineraryData *ItinData,
254                         const MachineInstr *DefMI, unsigned DefIdx) const;
255
256   /// verifyInstruction - Perform target specific instruction verification.
257   bool verifyInstruction(const MachineInstr *MI, StringRef &ErrInfo) const;
258
259 private:
260   /// Modeling special VFP / NEON fp MLA / MLS hazards.
261
262   /// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal
263   /// MLx table.
264   DenseMap<unsigned, unsigned> MLxEntryMap;
265
266   /// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause
267   /// stalls when scheduled together with fp MLA / MLS opcodes.
268   SmallSet<unsigned, 16> MLxHazardOpcodes;
269
270 public:
271   /// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS
272   /// instruction.
273   bool isFpMLxInstruction(unsigned Opcode) const {
274     return MLxEntryMap.count(Opcode);
275   }
276
277   /// isFpMLxInstruction - This version also returns the multiply opcode and the
278   /// addition / subtraction opcode to expand to. Return true for 'HasLane' for
279   /// the MLX instructions with an extra lane operand.
280   bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
281                           unsigned &AddSubOpc, bool &NegAcc,
282                           bool &HasLane) const;
283
284   /// canCauseFpMLxStall - Return true if an instruction of the specified opcode
285   /// will cause stalls when scheduled after (within 4-cycle window) a fp
286   /// MLA / MLS instruction.
287   bool canCauseFpMLxStall(unsigned Opcode) const {
288     return MLxHazardOpcodes.count(Opcode);
289   }
290 };
291
292 static inline
293 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
294   return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
295 }
296
297 static inline
298 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
299   return MIB.addReg(0);
300 }
301
302 static inline
303 const MachineInstrBuilder &AddDefaultT1CC(const MachineInstrBuilder &MIB,
304                                           bool isDead = false) {
305   return MIB.addReg(ARM::CPSR, getDefRegState(true) | getDeadRegState(isDead));
306 }
307
308 static inline
309 const MachineInstrBuilder &AddNoT1CC(const MachineInstrBuilder &MIB) {
310   return MIB.addReg(0);
311 }
312
313 static inline
314 bool isUncondBranchOpcode(int Opc) {
315   return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
316 }
317
318 static inline
319 bool isCondBranchOpcode(int Opc) {
320   return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
321 }
322
323 static inline
324 bool isJumpTableBranchOpcode(int Opc) {
325   return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm || Opc == ARM::BR_JTadd ||
326     Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT;
327 }
328
329 static inline
330 bool isIndirectBranchOpcode(int Opc) {
331   return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
332 }
333
334 /// getInstrPredicate - If instruction is predicated, returns its predicate
335 /// condition, otherwise returns AL. It also returns the condition code
336 /// register by reference.
337 ARMCC::CondCodes getInstrPredicate(const MachineInstr *MI, unsigned &PredReg);
338
339 int getMatchingCondBranchOpcode(int Opc);
340
341
342 /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether
343 /// the instruction is encoded with an 'S' bit is determined by the optional
344 /// CPSR def operand.
345 unsigned convertAddSubFlagsOpcode(unsigned OldOpc);
346
347 /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
348 /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
349 /// code.
350 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
351                              MachineBasicBlock::iterator &MBBI, DebugLoc dl,
352                              unsigned DestReg, unsigned BaseReg, int NumBytes,
353                              ARMCC::CondCodes Pred, unsigned PredReg,
354                              const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
355
356 void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
357                             MachineBasicBlock::iterator &MBBI, DebugLoc dl,
358                             unsigned DestReg, unsigned BaseReg, int NumBytes,
359                             ARMCC::CondCodes Pred, unsigned PredReg,
360                             const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
361 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
362                                MachineBasicBlock::iterator &MBBI, DebugLoc dl,
363                                unsigned DestReg, unsigned BaseReg,
364                                int NumBytes, const TargetInstrInfo &TII,
365                                const ARMBaseRegisterInfo& MRI,
366                                unsigned MIFlags = 0);
367
368
369 /// rewriteARMFrameIndex / rewriteT2FrameIndex -
370 /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
371 /// offset could not be handled directly in MI, and return the left-over
372 /// portion by reference.
373 bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
374                           unsigned FrameReg, int &Offset,
375                           const ARMBaseInstrInfo &TII);
376
377 bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
378                          unsigned FrameReg, int &Offset,
379                          const ARMBaseInstrInfo &TII);
380
381 } // End llvm namespace
382
383 #endif