7f02e94cb28d49ef0b12e304d31551a33f63b975
[oota-llvm.git] / lib / Target / ARM64 / ARM64InstrInfo.h
1 //===- ARM64InstrInfo.h - ARM64 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 ARM64 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TARGET_ARM64INSTRINFO_H
15 #define LLVM_TARGET_ARM64INSTRINFO_H
16
17 #include "ARM64.h"
18 #include "ARM64RegisterInfo.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20
21 #define GET_INSTRINFO_HEADER
22 #include "ARM64GenInstrInfo.inc"
23
24 namespace llvm {
25
26 class ARM64Subtarget;
27 class ARM64TargetMachine;
28
29 class ARM64InstrInfo : public ARM64GenInstrInfo {
30   // Reserve bits in the MachineMemOperand target hint flags, starting at 1.
31   // They will be shifted into MOTargetHintStart when accessed.
32   enum TargetMemOperandFlags {
33     MOSuppressPair = 1
34   };
35
36   const ARM64RegisterInfo RI;
37   const ARM64Subtarget &Subtarget;
38
39 public:
40   explicit ARM64InstrInfo(const ARM64Subtarget &STI);
41
42   /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info.  As
43   /// such, whenever a client has an instance of instruction info, it should
44   /// always be able to get register info as well (through this method).
45   const ARM64RegisterInfo &getRegisterInfo() const { return RI; }
46
47   const ARM64Subtarget &getSubTarget() const { return Subtarget; }
48
49   unsigned GetInstSizeInBytes(const MachineInstr *MI) const;
50
51   bool isCoalescableExtInstr(const MachineInstr &MI, unsigned &SrcReg,
52                              unsigned &DstReg, unsigned &SubIdx) const override;
53
54   unsigned isLoadFromStackSlot(const MachineInstr *MI,
55                                int &FrameIndex) const override;
56   unsigned isStoreToStackSlot(const MachineInstr *MI,
57                               int &FrameIndex) const override;
58
59   /// \brief Does this instruction set its full destination register to zero?
60   bool isGPRZero(const MachineInstr *MI) const;
61
62   /// \brief Does this instruction rename a GPR without modifying bits?
63   bool isGPRCopy(const MachineInstr *MI) const;
64
65   /// \brief Does this instruction rename an FPR without modifying bits?
66   bool isFPRCopy(const MachineInstr *MI) const;
67
68   /// Return true if this is load/store scales or extends its register offset.
69   /// This refers to scaling a dynamic index as opposed to scaled immediates.
70   /// MI should be a memory op that allows scaled addressing.
71   bool isScaledAddr(const MachineInstr *MI) const;
72
73   /// Return true if pairing the given load or store is hinted to be
74   /// unprofitable.
75   bool isLdStPairSuppressed(const MachineInstr *MI) const;
76
77   /// Hint that pairing the given load or store is unprofitable.
78   void suppressLdStPair(MachineInstr *MI) const;
79
80   bool getLdStBaseRegImmOfs(MachineInstr *LdSt, unsigned &BaseReg,
81                             unsigned &Offset,
82                             const TargetRegisterInfo *TRI) const override;
83
84   bool enableClusterLoads() const override { return true; }
85
86   bool shouldClusterLoads(MachineInstr *FirstLdSt, MachineInstr *SecondLdSt,
87                           unsigned NumLoads) const override;
88
89   bool shouldScheduleAdjacent(MachineInstr *First,
90                               MachineInstr *Second) const override;
91
92   MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
93                                          uint64_t Offset, const MDNode *MDPtr,
94                                          DebugLoc DL) const;
95   void copyPhysRegTuple(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
96                         DebugLoc DL, unsigned DestReg, unsigned SrcReg,
97                         bool KillSrc, unsigned Opcode,
98                         llvm::ArrayRef<unsigned> Indices) const;
99   void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
100                    DebugLoc DL, unsigned DestReg, unsigned SrcReg,
101                    bool KillSrc) const override;
102
103   void storeRegToStackSlot(MachineBasicBlock &MBB,
104                            MachineBasicBlock::iterator MBBI, unsigned SrcReg,
105                            bool isKill, int FrameIndex,
106                            const TargetRegisterClass *RC,
107                            const TargetRegisterInfo *TRI) const override;
108
109   void loadRegFromStackSlot(MachineBasicBlock &MBB,
110                             MachineBasicBlock::iterator MBBI, unsigned DestReg,
111                             int FrameIndex, const TargetRegisterClass *RC,
112                             const TargetRegisterInfo *TRI) const override;
113
114   MachineInstr *
115   foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
116                         const SmallVectorImpl<unsigned> &Ops,
117                         int FrameIndex) const override;
118
119   bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
120                      MachineBasicBlock *&FBB,
121                      SmallVectorImpl<MachineOperand> &Cond,
122                      bool AllowModify = false) const override;
123   unsigned RemoveBranch(MachineBasicBlock &MBB) const override;
124   unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
125                         MachineBasicBlock *FBB,
126                         const SmallVectorImpl<MachineOperand> &Cond,
127                         DebugLoc DL) const override;
128   bool
129   ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
130   bool canInsertSelect(const MachineBasicBlock &,
131                        const SmallVectorImpl<MachineOperand> &Cond, unsigned,
132                        unsigned, int &, int &, int &) const override;
133   void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
134                     DebugLoc DL, unsigned DstReg,
135                     const SmallVectorImpl<MachineOperand> &Cond,
136                     unsigned TrueReg, unsigned FalseReg) const override;
137   void getNoopForMachoTarget(MCInst &NopInst) const override;
138
139   /// analyzeCompare - For a comparison instruction, return the source registers
140   /// in SrcReg and SrcReg2, and the value it compares against in CmpValue.
141   /// Return true if the comparison instruction can be analyzed.
142   bool analyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
143                       unsigned &SrcReg2, int &CmpMask,
144                       int &CmpValue) const override;
145   /// optimizeCompareInstr - Convert the instruction supplying the argument to
146   /// the comparison into one that sets the zero bit in the flags register.
147   bool optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
148                             unsigned SrcReg2, int CmpMask, int CmpValue,
149                             const MachineRegisterInfo *MRI) const override;
150
151 private:
152   void instantiateCondBranch(MachineBasicBlock &MBB, DebugLoc DL,
153                              MachineBasicBlock *TBB,
154                              const SmallVectorImpl<MachineOperand> &Cond) const;
155 };
156
157 /// emitFrameOffset - Emit instructions as needed to set DestReg to SrcReg
158 /// plus Offset.  This is intended to be used from within the prolog/epilog
159 /// insertion (PEI) pass, where a virtual scratch register may be allocated
160 /// if necessary, to be replaced by the scavenger at the end of PEI.
161 void emitFrameOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
162                      DebugLoc DL, unsigned DestReg, unsigned SrcReg, int Offset,
163                      const ARM64InstrInfo *TII,
164                      MachineInstr::MIFlag = MachineInstr::NoFlags,
165                      bool SetCPSR = false);
166
167 /// rewriteARM64FrameIndex - Rewrite MI to access 'Offset' bytes from the
168 /// FP. Return false if the offset could not be handled directly in MI, and
169 /// return the left-over portion by reference.
170 bool rewriteARM64FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
171                             unsigned FrameReg, int &Offset,
172                             const ARM64InstrInfo *TII);
173
174 /// \brief Use to report the frame offset status in isARM64FrameOffsetLegal.
175 enum ARM64FrameOffsetStatus {
176   ARM64FrameOffsetCannotUpdate = 0x0, ///< Offset cannot apply.
177   ARM64FrameOffsetIsLegal = 0x1,      ///< Offset is legal.
178   ARM64FrameOffsetCanUpdate = 0x2     ///< Offset can apply, at least partly.
179 };
180
181 /// \brief Check if the @p Offset is a valid frame offset for @p MI.
182 /// The returned value reports the validity of the frame offset for @p MI.
183 /// It uses the values defined by ARM64FrameOffsetStatus for that.
184 /// If result == ARM64FrameOffsetCannotUpdate, @p MI cannot be updated to
185 /// use an offset.eq
186 /// If result & ARM64FrameOffsetIsLegal, @p Offset can completely be
187 /// rewriten in @p MI.
188 /// If result & ARM64FrameOffsetCanUpdate, @p Offset contains the
189 /// amount that is off the limit of the legal offset.
190 /// If set, @p OutUseUnscaledOp will contain the whether @p MI should be
191 /// turned into an unscaled operator, which opcode is in @p OutUnscaledOp.
192 /// If set, @p EmittableOffset contains the amount that can be set in @p MI
193 /// (possibly with @p OutUnscaledOp if OutUseUnscaledOp is true) and that
194 /// is a legal offset.
195 int isARM64FrameOffsetLegal(const MachineInstr &MI, int &Offset,
196                             bool *OutUseUnscaledOp = NULL,
197                             unsigned *OutUnscaledOp = NULL,
198                             int *EmittableOffset = NULL);
199
200 static inline bool isUncondBranchOpcode(int Opc) { return Opc == ARM64::B; }
201
202 static inline bool isCondBranchOpcode(int Opc) {
203   switch (Opc) {
204   case ARM64::Bcc:
205   case ARM64::CBZW:
206   case ARM64::CBZX:
207   case ARM64::CBNZW:
208   case ARM64::CBNZX:
209   case ARM64::TBZ:
210   case ARM64::TBNZ:
211     return true;
212   default:
213     return false;
214   }
215 }
216
217 static inline bool isIndirectBranchOpcode(int Opc) { return Opc == ARM64::BR; }
218
219 } // end namespace llvm
220
221 #endif