]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/AArch64/AArch64InstrInfo.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / AArch64 / AArch64InstrInfo.h
1 //===- AArch64InstrInfo.h - AArch64 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 AArch64 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64INSTRINFO_H
15 #define LLVM_LIB_TARGET_AARCH64_AARCH64INSTRINFO_H
16
17 #include "AArch64.h"
18 #include "AArch64RegisterInfo.h"
19 #include "llvm/CodeGen/MachineCombinerPattern.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21
22 #define GET_INSTRINFO_HEADER
23 #include "AArch64GenInstrInfo.inc"
24
25 namespace llvm {
26
27 class AArch64Subtarget;
28 class AArch64TargetMachine;
29
30 class AArch64InstrInfo final : public AArch64GenInstrInfo {
31   const AArch64RegisterInfo RI;
32   const AArch64Subtarget &Subtarget;
33
34 public:
35   explicit AArch64InstrInfo(const AArch64Subtarget &STI);
36
37   /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info.  As
38   /// such, whenever a client has an instance of instruction info, it should
39   /// always be able to get register info as well (through this method).
40   const AArch64RegisterInfo &getRegisterInfo() const { return RI; }
41
42   unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
43
44   bool isAsCheapAsAMove(const MachineInstr &MI) const override;
45
46   bool isCoalescableExtInstr(const MachineInstr &MI, unsigned &SrcReg,
47                              unsigned &DstReg, unsigned &SubIdx) const override;
48
49   bool
50   areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb,
51                                   AliasAnalysis *AA = nullptr) const override;
52
53   unsigned isLoadFromStackSlot(const MachineInstr &MI,
54                                int &FrameIndex) const override;
55   unsigned isStoreToStackSlot(const MachineInstr &MI,
56                               int &FrameIndex) const override;
57
58   /// Returns true if there is a shiftable register and that the shift value
59   /// is non-zero.
60   bool hasShiftedReg(const MachineInstr &MI) const;
61
62   /// Returns true if there is an extendable register and that the extending
63   /// value is non-zero.
64   bool hasExtendedReg(const MachineInstr &MI) const;
65
66   /// \brief Does this instruction set its full destination register to zero?
67   bool isGPRZero(const MachineInstr &MI) const;
68
69   /// \brief Does this instruction rename a GPR without modifying bits?
70   bool isGPRCopy(const MachineInstr &MI) const;
71
72   /// \brief Does this instruction rename an FPR without modifying bits?
73   bool isFPRCopy(const MachineInstr &MI) const;
74
75   /// Return true if this is load/store scales or extends its register offset.
76   /// This refers to scaling a dynamic index as opposed to scaled immediates.
77   /// MI should be a memory op that allows scaled addressing.
78   bool isScaledAddr(const MachineInstr &MI) const;
79
80   /// Return true if pairing the given load or store is hinted to be
81   /// unprofitable.
82   bool isLdStPairSuppressed(const MachineInstr &MI) const;
83
84   /// Return true if this is an unscaled load/store.
85   bool isUnscaledLdSt(unsigned Opc) const;
86
87   /// Return true if this is an unscaled load/store.
88   bool isUnscaledLdSt(MachineInstr &MI) const;
89
90   static bool isPairableLdStInst(const MachineInstr &MI) {
91     switch (MI.getOpcode()) {
92     default:
93       return false;
94     // Scaled instructions.
95     case AArch64::STRSui:
96     case AArch64::STRDui:
97     case AArch64::STRQui:
98     case AArch64::STRXui:
99     case AArch64::STRWui:
100     case AArch64::LDRSui:
101     case AArch64::LDRDui:
102     case AArch64::LDRQui:
103     case AArch64::LDRXui:
104     case AArch64::LDRWui:
105     case AArch64::LDRSWui:
106     // Unscaled instructions.
107     case AArch64::STURSi:
108     case AArch64::STURDi:
109     case AArch64::STURQi:
110     case AArch64::STURWi:
111     case AArch64::STURXi:
112     case AArch64::LDURSi:
113     case AArch64::LDURDi:
114     case AArch64::LDURQi:
115     case AArch64::LDURWi:
116     case AArch64::LDURXi:
117     case AArch64::LDURSWi:
118       return true;
119     }
120   }
121
122   /// \brief Return the opcode that set flags when possible.  The caller is
123   /// responsible for ensuring the opc has a flag setting equivalent.
124   static unsigned convertToFlagSettingOpc(unsigned Opc, bool &Is64Bit) {
125     switch (Opc) {
126     default:
127       llvm_unreachable("Opcode has no flag setting equivalent!");
128     // 32-bit cases:
129     case AArch64::ADDWri: Is64Bit = false; return AArch64::ADDSWri;
130     case AArch64::ADDWrr: Is64Bit = false; return AArch64::ADDSWrr;
131     case AArch64::ADDWrs: Is64Bit = false; return AArch64::ADDSWrs;
132     case AArch64::ADDWrx: Is64Bit = false; return AArch64::ADDSWrx;
133     case AArch64::ANDWri: Is64Bit = false; return AArch64::ANDSWri;
134     case AArch64::ANDWrr: Is64Bit = false; return AArch64::ANDSWrr;
135     case AArch64::ANDWrs: Is64Bit = false; return AArch64::ANDSWrs;
136     case AArch64::BICWrr: Is64Bit = false; return AArch64::BICSWrr;
137     case AArch64::BICWrs: Is64Bit = false; return AArch64::BICSWrs;
138     case AArch64::SUBWri: Is64Bit = false; return AArch64::SUBSWri;
139     case AArch64::SUBWrr: Is64Bit = false; return AArch64::SUBSWrr;
140     case AArch64::SUBWrs: Is64Bit = false; return AArch64::SUBSWrs;
141     case AArch64::SUBWrx: Is64Bit = false; return AArch64::SUBSWrx;
142     // 64-bit cases:
143     case AArch64::ADDXri: Is64Bit = true; return AArch64::ADDSXri;
144     case AArch64::ADDXrr: Is64Bit = true; return AArch64::ADDSXrr;
145     case AArch64::ADDXrs: Is64Bit = true; return AArch64::ADDSXrs;
146     case AArch64::ADDXrx: Is64Bit = true; return AArch64::ADDSXrx;
147     case AArch64::ANDXri: Is64Bit = true; return AArch64::ANDSXri;
148     case AArch64::ANDXrr: Is64Bit = true; return AArch64::ANDSXrr;
149     case AArch64::ANDXrs: Is64Bit = true; return AArch64::ANDSXrs;
150     case AArch64::BICXrr: Is64Bit = true; return AArch64::BICSXrr;
151     case AArch64::BICXrs: Is64Bit = true; return AArch64::BICSXrs;
152     case AArch64::SUBXri: Is64Bit = true; return AArch64::SUBSXri;
153     case AArch64::SUBXrr: Is64Bit = true; return AArch64::SUBSXrr;
154     case AArch64::SUBXrs: Is64Bit = true; return AArch64::SUBSXrs;
155     case AArch64::SUBXrx: Is64Bit = true; return AArch64::SUBSXrx;
156     }
157   }
158
159
160   /// Return true if this is a load/store that can be potentially paired/merged.
161   bool isCandidateToMergeOrPair(MachineInstr &MI) const;
162
163   /// Hint that pairing the given load or store is unprofitable.
164   void suppressLdStPair(MachineInstr &MI) const;
165
166   bool getMemOpBaseRegImmOfs(MachineInstr &LdSt, unsigned &BaseReg,
167                              int64_t &Offset,
168                              const TargetRegisterInfo *TRI) const override;
169
170   bool getMemOpBaseRegImmOfsWidth(MachineInstr &LdSt, unsigned &BaseReg,
171                                   int64_t &Offset, unsigned &Width,
172                                   const TargetRegisterInfo *TRI) const;
173
174   /// Return the immediate offset of the base register in a load/store \p LdSt.
175   MachineOperand &getMemOpBaseRegImmOfsOffsetOperand(MachineInstr &LdSt) const;
176
177   /// \brief Returns true if opcode \p Opc is a memory operation. If it is, set
178   /// \p Scale, \p Width, \p MinOffset, and \p MaxOffset accordingly.
179   ///
180   /// For unscaled instructions, \p Scale is set to 1.
181   bool getMemOpInfo(unsigned Opcode, unsigned &Scale, unsigned &Width,
182                     int64_t &MinOffset, int64_t &MaxOffset) const;
183
184   bool shouldClusterMemOps(MachineInstr &FirstLdSt, MachineInstr &SecondLdSt,
185                            unsigned NumLoads) const override;
186
187   MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
188                                          uint64_t Offset, const MDNode *Var,
189                                          const MDNode *Expr,
190                                          const DebugLoc &DL) const;
191   void copyPhysRegTuple(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
192                         const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
193                         bool KillSrc, unsigned Opcode,
194                         llvm::ArrayRef<unsigned> Indices) const;
195   void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
196                    const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
197                    bool KillSrc) const override;
198
199   void storeRegToStackSlot(MachineBasicBlock &MBB,
200                            MachineBasicBlock::iterator MBBI, unsigned SrcReg,
201                            bool isKill, int FrameIndex,
202                            const TargetRegisterClass *RC,
203                            const TargetRegisterInfo *TRI) const override;
204
205   void loadRegFromStackSlot(MachineBasicBlock &MBB,
206                             MachineBasicBlock::iterator MBBI, unsigned DestReg,
207                             int FrameIndex, const TargetRegisterClass *RC,
208                             const TargetRegisterInfo *TRI) const override;
209
210   // This tells target independent code that it is okay to pass instructions
211   // with subreg operands to foldMemoryOperandImpl.
212   bool isSubregFoldable() const override { return true; }
213
214   using TargetInstrInfo::foldMemoryOperandImpl;
215   MachineInstr *
216   foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
217                         ArrayRef<unsigned> Ops,
218                         MachineBasicBlock::iterator InsertPt, int FrameIndex,
219                         LiveIntervals *LIS = nullptr) const override;
220
221   /// \returns true if a branch from an instruction with opcode \p BranchOpc
222   ///  bytes is capable of jumping to a position \p BrOffset bytes away.
223   bool isBranchOffsetInRange(unsigned BranchOpc,
224                              int64_t BrOffset) const override;
225
226   MachineBasicBlock *getBranchDestBlock(const MachineInstr &MI) const override;
227
228   bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
229                      MachineBasicBlock *&FBB,
230                      SmallVectorImpl<MachineOperand> &Cond,
231                      bool AllowModify = false) const override;
232   unsigned removeBranch(MachineBasicBlock &MBB,
233                         int *BytesRemoved = nullptr) const override;
234   unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
235                         MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
236                         const DebugLoc &DL,
237                         int *BytesAdded = nullptr) const override;
238   bool
239   reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
240   bool canInsertSelect(const MachineBasicBlock &, ArrayRef<MachineOperand> Cond,
241                        unsigned, unsigned, int &, int &, int &) const override;
242   void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
243                     const DebugLoc &DL, unsigned DstReg,
244                     ArrayRef<MachineOperand> Cond, unsigned TrueReg,
245                     unsigned FalseReg) const override;
246   void getNoop(MCInst &NopInst) const override;
247
248   /// analyzeCompare - For a comparison instruction, return the source registers
249   /// in SrcReg and SrcReg2, and the value it compares against in CmpValue.
250   /// Return true if the comparison instruction can be analyzed.
251   bool analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
252                       unsigned &SrcReg2, int &CmpMask,
253                       int &CmpValue) const override;
254   /// optimizeCompareInstr - Convert the instruction supplying the argument to
255   /// the comparison into one that sets the zero bit in the flags register.
256   bool optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
257                             unsigned SrcReg2, int CmpMask, int CmpValue,
258                             const MachineRegisterInfo *MRI) const override;
259   bool optimizeCondBranch(MachineInstr &MI) const override;
260
261   /// Return true when a code sequence can improve throughput. It
262   /// should be called only for instructions in loops.
263   /// \param Pattern - combiner pattern
264   bool isThroughputPattern(MachineCombinerPattern Pattern) const override;
265   /// Return true when there is potentially a faster code sequence
266   /// for an instruction chain ending in <Root>. All potential patterns are
267   /// listed in the <Patterns> array.
268   bool getMachineCombinerPatterns(MachineInstr &Root,
269                   SmallVectorImpl<MachineCombinerPattern> &Patterns)
270       const override;
271   /// Return true when Inst is associative and commutative so that it can be
272   /// reassociated.
273   bool isAssociativeAndCommutative(const MachineInstr &Inst) const override;
274   /// When getMachineCombinerPatterns() finds patterns, this function generates
275   /// the instructions that could replace the original code sequence
276   void genAlternativeCodeSequence(
277       MachineInstr &Root, MachineCombinerPattern Pattern,
278       SmallVectorImpl<MachineInstr *> &InsInstrs,
279       SmallVectorImpl<MachineInstr *> &DelInstrs,
280       DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const override;
281   /// AArch64 supports MachineCombiner.
282   bool useMachineCombiner() const override;
283
284   bool expandPostRAPseudo(MachineInstr &MI) const override;
285
286   std::pair<unsigned, unsigned>
287   decomposeMachineOperandsTargetFlags(unsigned TF) const override;
288   ArrayRef<std::pair<unsigned, const char *>>
289   getSerializableDirectMachineOperandTargetFlags() const override;
290   ArrayRef<std::pair<unsigned, const char *>>
291   getSerializableBitmaskMachineOperandTargetFlags() const override;
292
293   bool isFunctionSafeToOutlineFrom(MachineFunction &MF) const override;
294   unsigned getOutliningBenefit(size_t SequenceSize, size_t Occurrences,
295                                bool CanBeTailCall) const override;
296   AArch64GenInstrInfo::MachineOutlinerInstrType
297   getOutliningType(MachineInstr &MI) const override;
298   void insertOutlinerEpilogue(MachineBasicBlock &MBB,
299                               MachineFunction &MF,
300                               bool IsTailCall) const override;
301   void insertOutlinerPrologue(MachineBasicBlock &MBB,
302                               MachineFunction &MF,
303                               bool isTailCall) const override;
304   MachineBasicBlock::iterator
305   insertOutlinedCall(Module &M, MachineBasicBlock &MBB,
306                      MachineBasicBlock::iterator &It,
307                      MachineFunction &MF,
308                      bool IsTailCall) const override;
309   /// Returns true if the instruction has a shift by immediate that can be
310   /// executed in one cycle less.
311   bool isFalkorShiftExtFast(const MachineInstr &MI) const;
312 private:
313
314   /// \brief Sets the offsets on outlined instructions in \p MBB which use SP
315   /// so that they will be valid post-outlining.
316   ///
317   /// \param MBB A \p MachineBasicBlock in an outlined function.
318   void fixupPostOutline(MachineBasicBlock &MBB) const;
319
320   void instantiateCondBranch(MachineBasicBlock &MBB, const DebugLoc &DL,
321                              MachineBasicBlock *TBB,
322                              ArrayRef<MachineOperand> Cond) const;
323   bool substituteCmpToZero(MachineInstr &CmpInstr, unsigned SrcReg,
324                            const MachineRegisterInfo *MRI) const;
325 };
326
327 /// emitFrameOffset - Emit instructions as needed to set DestReg to SrcReg
328 /// plus Offset.  This is intended to be used from within the prolog/epilog
329 /// insertion (PEI) pass, where a virtual scratch register may be allocated
330 /// if necessary, to be replaced by the scavenger at the end of PEI.
331 void emitFrameOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
332                      const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
333                      int Offset, const TargetInstrInfo *TII,
334                      MachineInstr::MIFlag = MachineInstr::NoFlags,
335                      bool SetNZCV = false);
336
337 /// rewriteAArch64FrameIndex - Rewrite MI to access 'Offset' bytes from the
338 /// FP. Return false if the offset could not be handled directly in MI, and
339 /// return the left-over portion by reference.
340 bool rewriteAArch64FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
341                             unsigned FrameReg, int &Offset,
342                             const AArch64InstrInfo *TII);
343
344 /// \brief Use to report the frame offset status in isAArch64FrameOffsetLegal.
345 enum AArch64FrameOffsetStatus {
346   AArch64FrameOffsetCannotUpdate = 0x0, ///< Offset cannot apply.
347   AArch64FrameOffsetIsLegal = 0x1,      ///< Offset is legal.
348   AArch64FrameOffsetCanUpdate = 0x2     ///< Offset can apply, at least partly.
349 };
350
351 /// \brief Check if the @p Offset is a valid frame offset for @p MI.
352 /// The returned value reports the validity of the frame offset for @p MI.
353 /// It uses the values defined by AArch64FrameOffsetStatus for that.
354 /// If result == AArch64FrameOffsetCannotUpdate, @p MI cannot be updated to
355 /// use an offset.eq
356 /// If result & AArch64FrameOffsetIsLegal, @p Offset can completely be
357 /// rewriten in @p MI.
358 /// If result & AArch64FrameOffsetCanUpdate, @p Offset contains the
359 /// amount that is off the limit of the legal offset.
360 /// If set, @p OutUseUnscaledOp will contain the whether @p MI should be
361 /// turned into an unscaled operator, which opcode is in @p OutUnscaledOp.
362 /// If set, @p EmittableOffset contains the amount that can be set in @p MI
363 /// (possibly with @p OutUnscaledOp if OutUseUnscaledOp is true) and that
364 /// is a legal offset.
365 int isAArch64FrameOffsetLegal(const MachineInstr &MI, int &Offset,
366                             bool *OutUseUnscaledOp = nullptr,
367                             unsigned *OutUnscaledOp = nullptr,
368                             int *EmittableOffset = nullptr);
369
370 static inline bool isUncondBranchOpcode(int Opc) { return Opc == AArch64::B; }
371
372 static inline bool isCondBranchOpcode(int Opc) {
373   switch (Opc) {
374   case AArch64::Bcc:
375   case AArch64::CBZW:
376   case AArch64::CBZX:
377   case AArch64::CBNZW:
378   case AArch64::CBNZX:
379   case AArch64::TBZW:
380   case AArch64::TBZX:
381   case AArch64::TBNZW:
382   case AArch64::TBNZX:
383     return true;
384   default:
385     return false;
386   }
387 }
388
389 static inline bool isIndirectBranchOpcode(int Opc) { return Opc == AArch64::BR; }
390
391 } // end namespace llvm
392
393 #endif