1 //===- AArch64InstrInfo.h - AArch64 Instruction Information -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the AArch64 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64INSTRINFO_H
15 #define LLVM_LIB_TARGET_AARCH64_AARCH64INSTRINFO_H
18 #include "AArch64RegisterInfo.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/CodeGen/MachineCombinerPattern.h"
22 #define GET_INSTRINFO_HEADER
23 #include "AArch64GenInstrInfo.inc"
27 class AArch64Subtarget;
28 class AArch64TargetMachine;
30 class AArch64InstrInfo : public AArch64GenInstrInfo {
31 // Reserve bits in the MachineMemOperand target hint flags, starting at 1.
32 // They will be shifted into MOTargetHintStart when accessed.
33 enum TargetMemOperandFlags {
37 const AArch64RegisterInfo RI;
38 const AArch64Subtarget &Subtarget;
41 explicit AArch64InstrInfo(const AArch64Subtarget &STI);
43 /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info. As
44 /// such, whenever a client has an instance of instruction info, it should
45 /// always be able to get register info as well (through this method).
46 const AArch64RegisterInfo &getRegisterInfo() const { return RI; }
48 unsigned GetInstSizeInBytes(const MachineInstr *MI) const;
50 bool isAsCheapAsAMove(const MachineInstr *MI) const override;
52 bool isCoalescableExtInstr(const MachineInstr &MI, unsigned &SrcReg,
53 unsigned &DstReg, unsigned &SubIdx) const override;
56 areMemAccessesTriviallyDisjoint(MachineInstr *MIa, MachineInstr *MIb,
57 AliasAnalysis *AA = nullptr) const override;
59 unsigned isLoadFromStackSlot(const MachineInstr *MI,
60 int &FrameIndex) const override;
61 unsigned isStoreToStackSlot(const MachineInstr *MI,
62 int &FrameIndex) const override;
64 /// Returns true if there is a shiftable register and that the shift value
66 bool hasShiftedReg(const MachineInstr *MI) const;
68 /// Returns true if there is an extendable register and that the extending
69 /// value is non-zero.
70 bool hasExtendedReg(const MachineInstr *MI) const;
72 /// \brief Does this instruction set its full destination register to zero?
73 bool isGPRZero(const MachineInstr *MI) const;
75 /// \brief Does this instruction rename a GPR without modifying bits?
76 bool isGPRCopy(const MachineInstr *MI) const;
78 /// \brief Does this instruction rename an FPR without modifying bits?
79 bool isFPRCopy(const MachineInstr *MI) const;
81 /// Return true if this is load/store scales or extends its register offset.
82 /// This refers to scaling a dynamic index as opposed to scaled immediates.
83 /// MI should be a memory op that allows scaled addressing.
84 bool isScaledAddr(const MachineInstr *MI) const;
86 /// Return true if pairing the given load or store is hinted to be
88 bool isLdStPairSuppressed(const MachineInstr *MI) const;
90 /// Hint that pairing the given load or store is unprofitable.
91 void suppressLdStPair(MachineInstr *MI) const;
93 bool getLdStBaseRegImmOfs(MachineInstr *LdSt, unsigned &BaseReg,
95 const TargetRegisterInfo *TRI) const override;
97 bool getLdStBaseRegImmOfsWidth(MachineInstr *LdSt, unsigned &BaseReg,
98 int &Offset, int &Width,
99 const TargetRegisterInfo *TRI) const;
101 bool enableClusterLoads() const override { return true; }
103 bool shouldClusterLoads(MachineInstr *FirstLdSt, MachineInstr *SecondLdSt,
104 unsigned NumLoads) const override;
106 bool shouldScheduleAdjacent(MachineInstr *First,
107 MachineInstr *Second) const override;
109 MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
110 uint64_t Offset, const MDNode *Var,
111 const MDNode *Expr, DebugLoc DL) const;
112 void copyPhysRegTuple(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
113 DebugLoc DL, unsigned DestReg, unsigned SrcReg,
114 bool KillSrc, unsigned Opcode,
115 llvm::ArrayRef<unsigned> Indices) const;
116 void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
117 DebugLoc DL, unsigned DestReg, unsigned SrcReg,
118 bool KillSrc) const override;
120 void storeRegToStackSlot(MachineBasicBlock &MBB,
121 MachineBasicBlock::iterator MBBI, unsigned SrcReg,
122 bool isKill, int FrameIndex,
123 const TargetRegisterClass *RC,
124 const TargetRegisterInfo *TRI) const override;
126 void loadRegFromStackSlot(MachineBasicBlock &MBB,
127 MachineBasicBlock::iterator MBBI, unsigned DestReg,
128 int FrameIndex, const TargetRegisterClass *RC,
129 const TargetRegisterInfo *TRI) const override;
131 using TargetInstrInfo::foldMemoryOperandImpl;
133 foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
134 const SmallVectorImpl<unsigned> &Ops,
135 int FrameIndex) const override;
137 bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
138 MachineBasicBlock *&FBB,
139 SmallVectorImpl<MachineOperand> &Cond,
140 bool AllowModify = false) const override;
141 unsigned RemoveBranch(MachineBasicBlock &MBB) const override;
142 unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
143 MachineBasicBlock *FBB,
144 const SmallVectorImpl<MachineOperand> &Cond,
145 DebugLoc DL) const override;
147 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
148 bool canInsertSelect(const MachineBasicBlock &,
149 const SmallVectorImpl<MachineOperand> &Cond, unsigned,
150 unsigned, int &, int &, int &) const override;
151 void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
152 DebugLoc DL, unsigned DstReg,
153 const SmallVectorImpl<MachineOperand> &Cond,
154 unsigned TrueReg, unsigned FalseReg) const override;
155 void getNoopForMachoTarget(MCInst &NopInst) const override;
157 /// analyzeCompare - For a comparison instruction, return the source registers
158 /// in SrcReg and SrcReg2, and the value it compares against in CmpValue.
159 /// Return true if the comparison instruction can be analyzed.
160 bool analyzeCompare(const MachineInstr *MI, unsigned &SrcReg,
161 unsigned &SrcReg2, int &CmpMask,
162 int &CmpValue) const override;
163 /// optimizeCompareInstr - Convert the instruction supplying the argument to
164 /// the comparison into one that sets the zero bit in the flags register.
165 bool optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg,
166 unsigned SrcReg2, int CmpMask, int CmpValue,
167 const MachineRegisterInfo *MRI) const override;
168 bool optimizeCondBranch(MachineInstr *MI) const;
169 /// hasPattern - return true when there is potentially a faster code sequence
170 /// for an instruction chain ending in <Root>. All potential patterns are
172 /// in the <Pattern> array.
173 bool hasPattern(MachineInstr &Root,
174 SmallVectorImpl<MachineCombinerPattern::MC_PATTERN> &Pattern)
177 /// genAlternativeCodeSequence - when hasPattern() finds a pattern
178 /// this function generates the instructions that could replace the
179 /// original code sequence
180 void genAlternativeCodeSequence(
181 MachineInstr &Root, MachineCombinerPattern::MC_PATTERN P,
182 SmallVectorImpl<MachineInstr *> &InsInstrs,
183 SmallVectorImpl<MachineInstr *> &DelInstrs,
184 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const override;
185 /// useMachineCombiner - AArch64 supports MachineCombiner
186 bool useMachineCombiner() const override;
188 bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
190 void instantiateCondBranch(MachineBasicBlock &MBB, DebugLoc DL,
191 MachineBasicBlock *TBB,
192 const SmallVectorImpl<MachineOperand> &Cond) const;
195 /// emitFrameOffset - Emit instructions as needed to set DestReg to SrcReg
196 /// plus Offset. This is intended to be used from within the prolog/epilog
197 /// insertion (PEI) pass, where a virtual scratch register may be allocated
198 /// if necessary, to be replaced by the scavenger at the end of PEI.
199 void emitFrameOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
200 DebugLoc DL, unsigned DestReg, unsigned SrcReg, int Offset,
201 const TargetInstrInfo *TII,
202 MachineInstr::MIFlag = MachineInstr::NoFlags,
203 bool SetNZCV = false);
205 /// rewriteAArch64FrameIndex - Rewrite MI to access 'Offset' bytes from the
206 /// FP. Return false if the offset could not be handled directly in MI, and
207 /// return the left-over portion by reference.
208 bool rewriteAArch64FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
209 unsigned FrameReg, int &Offset,
210 const AArch64InstrInfo *TII);
212 /// \brief Use to report the frame offset status in isAArch64FrameOffsetLegal.
213 enum AArch64FrameOffsetStatus {
214 AArch64FrameOffsetCannotUpdate = 0x0, ///< Offset cannot apply.
215 AArch64FrameOffsetIsLegal = 0x1, ///< Offset is legal.
216 AArch64FrameOffsetCanUpdate = 0x2 ///< Offset can apply, at least partly.
219 /// \brief Check if the @p Offset is a valid frame offset for @p MI.
220 /// The returned value reports the validity of the frame offset for @p MI.
221 /// It uses the values defined by AArch64FrameOffsetStatus for that.
222 /// If result == AArch64FrameOffsetCannotUpdate, @p MI cannot be updated to
224 /// If result & AArch64FrameOffsetIsLegal, @p Offset can completely be
225 /// rewriten in @p MI.
226 /// If result & AArch64FrameOffsetCanUpdate, @p Offset contains the
227 /// amount that is off the limit of the legal offset.
228 /// If set, @p OutUseUnscaledOp will contain the whether @p MI should be
229 /// turned into an unscaled operator, which opcode is in @p OutUnscaledOp.
230 /// If set, @p EmittableOffset contains the amount that can be set in @p MI
231 /// (possibly with @p OutUnscaledOp if OutUseUnscaledOp is true) and that
232 /// is a legal offset.
233 int isAArch64FrameOffsetLegal(const MachineInstr &MI, int &Offset,
234 bool *OutUseUnscaledOp = nullptr,
235 unsigned *OutUnscaledOp = nullptr,
236 int *EmittableOffset = nullptr);
238 static inline bool isUncondBranchOpcode(int Opc) { return Opc == AArch64::B; }
240 static inline bool isCondBranchOpcode(int Opc) {
257 static inline bool isIndirectBranchOpcode(int Opc) { return Opc == AArch64::BR; }
259 } // end namespace llvm