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/CodeGen/MachineCombinerPattern.h"
20 #include "llvm/CodeGen/TargetInstrInfo.h"
22 #define GET_INSTRINFO_HEADER
23 #include "AArch64GenInstrInfo.inc"
27 class AArch64Subtarget;
28 class AArch64TargetMachine;
30 static const MachineMemOperand::Flags MOSuppressPair =
31 MachineMemOperand::MOTargetFlag1;
32 static const MachineMemOperand::Flags MOStridedAccess =
33 MachineMemOperand::MOTargetFlag2;
35 #define FALKOR_STRIDED_ACCESS_MD "falkor.strided.access"
37 class AArch64InstrInfo final : public AArch64GenInstrInfo {
38 const AArch64RegisterInfo RI;
39 const AArch64Subtarget &Subtarget;
42 explicit AArch64InstrInfo(const AArch64Subtarget &STI);
44 /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info. As
45 /// such, whenever a client has an instance of instruction info, it should
46 /// always be able to get register info as well (through this method).
47 const AArch64RegisterInfo &getRegisterInfo() const { return RI; }
49 unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
51 bool isAsCheapAsAMove(const MachineInstr &MI) const override;
53 bool isCoalescableExtInstr(const MachineInstr &MI, unsigned &SrcReg,
54 unsigned &DstReg, unsigned &SubIdx) const override;
57 areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb,
58 AliasAnalysis *AA = nullptr) const override;
60 unsigned isLoadFromStackSlot(const MachineInstr &MI,
61 int &FrameIndex) const override;
62 unsigned isStoreToStackSlot(const MachineInstr &MI,
63 int &FrameIndex) const override;
65 /// Does this instruction set its full destination register to zero?
66 static bool isGPRZero(const MachineInstr &MI);
68 /// Does this instruction rename a GPR without modifying bits?
69 static bool isGPRCopy(const MachineInstr &MI);
71 /// Does this instruction rename an FPR without modifying bits?
72 static bool isFPRCopy(const MachineInstr &MI);
74 /// Return true if pairing the given load or store is hinted to be
76 static bool isLdStPairSuppressed(const MachineInstr &MI);
78 /// Return true if the given load or store is a strided memory access.
79 static bool isStridedAccess(const MachineInstr &MI);
81 /// Return true if this is an unscaled load/store.
82 static bool isUnscaledLdSt(unsigned Opc);
83 static bool isUnscaledLdSt(MachineInstr &MI) {
84 return isUnscaledLdSt(MI.getOpcode());
87 /// Return true if pairing the given load or store may be paired with another.
88 static bool isPairableLdStInst(const MachineInstr &MI);
90 /// Return the opcode that set flags when possible. The caller is
91 /// responsible for ensuring the opc has a flag setting equivalent.
92 static unsigned convertToFlagSettingOpc(unsigned Opc, bool &Is64Bit);
94 /// Return true if this is a load/store that can be potentially paired/merged.
95 bool isCandidateToMergeOrPair(MachineInstr &MI) const;
97 /// Hint that pairing the given load or store is unprofitable.
98 static void suppressLdStPair(MachineInstr &MI);
100 bool getMemOperandWithOffset(MachineInstr &MI, MachineOperand *&BaseOp,
102 const TargetRegisterInfo *TRI) const override;
104 bool getMemOperandWithOffsetWidth(MachineInstr &MI, MachineOperand *&BaseOp,
105 int64_t &Offset, unsigned &Width,
106 const TargetRegisterInfo *TRI) const;
108 /// Return the immediate offset of the base register in a load/store \p LdSt.
109 MachineOperand &getMemOpBaseRegImmOfsOffsetOperand(MachineInstr &LdSt) const;
111 /// Returns true if opcode \p Opc is a memory operation. If it is, set
112 /// \p Scale, \p Width, \p MinOffset, and \p MaxOffset accordingly.
114 /// For unscaled instructions, \p Scale is set to 1.
115 bool getMemOpInfo(unsigned Opcode, unsigned &Scale, unsigned &Width,
116 int64_t &MinOffset, int64_t &MaxOffset) const;
118 bool shouldClusterMemOps(MachineOperand &BaseOp1, MachineOperand &BaseOp2,
119 unsigned NumLoads) const override;
121 void copyPhysRegTuple(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
122 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
123 bool KillSrc, unsigned Opcode,
124 llvm::ArrayRef<unsigned> Indices) const;
125 void copyGPRRegTuple(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
126 DebugLoc DL, unsigned DestReg, unsigned SrcReg,
127 bool KillSrc, unsigned Opcode, unsigned ZeroReg,
128 llvm::ArrayRef<unsigned> Indices) const;
129 void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
130 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
131 bool KillSrc) const override;
133 void storeRegToStackSlot(MachineBasicBlock &MBB,
134 MachineBasicBlock::iterator MBBI, unsigned SrcReg,
135 bool isKill, int FrameIndex,
136 const TargetRegisterClass *RC,
137 const TargetRegisterInfo *TRI) const override;
139 void loadRegFromStackSlot(MachineBasicBlock &MBB,
140 MachineBasicBlock::iterator MBBI, unsigned DestReg,
141 int FrameIndex, const TargetRegisterClass *RC,
142 const TargetRegisterInfo *TRI) const override;
144 // This tells target independent code that it is okay to pass instructions
145 // with subreg operands to foldMemoryOperandImpl.
146 bool isSubregFoldable() const override { return true; }
148 using TargetInstrInfo::foldMemoryOperandImpl;
150 foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
151 ArrayRef<unsigned> Ops,
152 MachineBasicBlock::iterator InsertPt, int FrameIndex,
153 LiveIntervals *LIS = nullptr) const override;
155 /// \returns true if a branch from an instruction with opcode \p BranchOpc
156 /// bytes is capable of jumping to a position \p BrOffset bytes away.
157 bool isBranchOffsetInRange(unsigned BranchOpc,
158 int64_t BrOffset) const override;
160 MachineBasicBlock *getBranchDestBlock(const MachineInstr &MI) const override;
162 bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
163 MachineBasicBlock *&FBB,
164 SmallVectorImpl<MachineOperand> &Cond,
165 bool AllowModify = false) const override;
166 unsigned removeBranch(MachineBasicBlock &MBB,
167 int *BytesRemoved = nullptr) const override;
168 unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
169 MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
171 int *BytesAdded = nullptr) const override;
173 reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
174 bool canInsertSelect(const MachineBasicBlock &, ArrayRef<MachineOperand> Cond,
175 unsigned, unsigned, int &, int &, int &) const override;
176 void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
177 const DebugLoc &DL, unsigned DstReg,
178 ArrayRef<MachineOperand> Cond, unsigned TrueReg,
179 unsigned FalseReg) const override;
180 void getNoop(MCInst &NopInst) const override;
182 bool isSchedulingBoundary(const MachineInstr &MI,
183 const MachineBasicBlock *MBB,
184 const MachineFunction &MF) const override;
186 /// analyzeCompare - For a comparison instruction, return the source registers
187 /// in SrcReg and SrcReg2, and the value it compares against in CmpValue.
188 /// Return true if the comparison instruction can be analyzed.
189 bool analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
190 unsigned &SrcReg2, int &CmpMask,
191 int &CmpValue) const override;
192 /// optimizeCompareInstr - Convert the instruction supplying the argument to
193 /// the comparison into one that sets the zero bit in the flags register.
194 bool optimizeCompareInstr(MachineInstr &CmpInstr, unsigned SrcReg,
195 unsigned SrcReg2, int CmpMask, int CmpValue,
196 const MachineRegisterInfo *MRI) const override;
197 bool optimizeCondBranch(MachineInstr &MI) const override;
199 /// Return true when a code sequence can improve throughput. It
200 /// should be called only for instructions in loops.
201 /// \param Pattern - combiner pattern
202 bool isThroughputPattern(MachineCombinerPattern Pattern) const override;
203 /// Return true when there is potentially a faster code sequence
204 /// for an instruction chain ending in ``Root``. All potential patterns are
205 /// listed in the ``Patterns`` array.
206 bool getMachineCombinerPatterns(
208 SmallVectorImpl<MachineCombinerPattern> &Patterns) const override;
209 /// Return true when Inst is associative and commutative so that it can be
211 bool isAssociativeAndCommutative(const MachineInstr &Inst) const override;
212 /// When getMachineCombinerPatterns() finds patterns, this function generates
213 /// the instructions that could replace the original code sequence
214 void genAlternativeCodeSequence(
215 MachineInstr &Root, MachineCombinerPattern Pattern,
216 SmallVectorImpl<MachineInstr *> &InsInstrs,
217 SmallVectorImpl<MachineInstr *> &DelInstrs,
218 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const override;
219 /// AArch64 supports MachineCombiner.
220 bool useMachineCombiner() const override;
222 bool expandPostRAPseudo(MachineInstr &MI) const override;
224 std::pair<unsigned, unsigned>
225 decomposeMachineOperandsTargetFlags(unsigned TF) const override;
226 ArrayRef<std::pair<unsigned, const char *>>
227 getSerializableDirectMachineOperandTargetFlags() const override;
228 ArrayRef<std::pair<unsigned, const char *>>
229 getSerializableBitmaskMachineOperandTargetFlags() const override;
230 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
231 getSerializableMachineMemOperandTargetFlags() const override;
233 bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
234 bool OutlineFromLinkOnceODRs) const override;
235 outliner::OutlinedFunction getOutliningCandidateInfo(
236 std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
238 getOutliningType(MachineBasicBlock::iterator &MIT, unsigned Flags) const override;
239 bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
240 unsigned &Flags) const override;
241 void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,
242 const outliner::OutlinedFunction &OF) const override;
243 MachineBasicBlock::iterator
244 insertOutlinedCall(Module &M, MachineBasicBlock &MBB,
245 MachineBasicBlock::iterator &It, MachineFunction &MF,
246 const outliner::Candidate &C) const override;
247 bool shouldOutlineFromFunctionByDefault(MachineFunction &MF) const override;
248 /// Returns true if the instruction has a shift by immediate that can be
249 /// executed in one cycle less.
250 static bool isFalkorShiftExtFast(const MachineInstr &MI);
251 /// Return true if the instructions is a SEH instruciton used for unwinding
253 static bool isSEHInstruction(const MachineInstr &MI);
255 #define GET_INSTRINFO_HELPER_DECLS
256 #include "AArch64GenInstrInfo.inc"
259 /// Sets the offsets on outlined instructions in \p MBB which use SP
260 /// so that they will be valid post-outlining.
262 /// \param MBB A \p MachineBasicBlock in an outlined function.
263 void fixupPostOutline(MachineBasicBlock &MBB) const;
265 void instantiateCondBranch(MachineBasicBlock &MBB, const DebugLoc &DL,
266 MachineBasicBlock *TBB,
267 ArrayRef<MachineOperand> Cond) const;
268 bool substituteCmpToZero(MachineInstr &CmpInstr, unsigned SrcReg,
269 const MachineRegisterInfo *MRI) const;
271 /// Returns an unused general-purpose register which can be used for
272 /// constructing an outlined call if one exists. Returns 0 otherwise.
273 unsigned findRegisterToSaveLRTo(const outliner::Candidate &C) const;
276 /// emitFrameOffset - Emit instructions as needed to set DestReg to SrcReg
277 /// plus Offset. This is intended to be used from within the prolog/epilog
278 /// insertion (PEI) pass, where a virtual scratch register may be allocated
279 /// if necessary, to be replaced by the scavenger at the end of PEI.
280 void emitFrameOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
281 const DebugLoc &DL, unsigned DestReg, unsigned SrcReg,
282 int Offset, const TargetInstrInfo *TII,
283 MachineInstr::MIFlag = MachineInstr::NoFlags,
284 bool SetNZCV = false, bool NeedsWinCFI = false);
286 /// rewriteAArch64FrameIndex - Rewrite MI to access 'Offset' bytes from the
287 /// FP. Return false if the offset could not be handled directly in MI, and
288 /// return the left-over portion by reference.
289 bool rewriteAArch64FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
290 unsigned FrameReg, int &Offset,
291 const AArch64InstrInfo *TII);
293 /// Use to report the frame offset status in isAArch64FrameOffsetLegal.
294 enum AArch64FrameOffsetStatus {
295 AArch64FrameOffsetCannotUpdate = 0x0, ///< Offset cannot apply.
296 AArch64FrameOffsetIsLegal = 0x1, ///< Offset is legal.
297 AArch64FrameOffsetCanUpdate = 0x2 ///< Offset can apply, at least partly.
300 /// Check if the @p Offset is a valid frame offset for @p MI.
301 /// The returned value reports the validity of the frame offset for @p MI.
302 /// It uses the values defined by AArch64FrameOffsetStatus for that.
303 /// If result == AArch64FrameOffsetCannotUpdate, @p MI cannot be updated to
305 /// If result & AArch64FrameOffsetIsLegal, @p Offset can completely be
306 /// rewritten in @p MI.
307 /// If result & AArch64FrameOffsetCanUpdate, @p Offset contains the
308 /// amount that is off the limit of the legal offset.
309 /// If set, @p OutUseUnscaledOp will contain the whether @p MI should be
310 /// turned into an unscaled operator, which opcode is in @p OutUnscaledOp.
311 /// If set, @p EmittableOffset contains the amount that can be set in @p MI
312 /// (possibly with @p OutUnscaledOp if OutUseUnscaledOp is true) and that
313 /// is a legal offset.
314 int isAArch64FrameOffsetLegal(const MachineInstr &MI, int &Offset,
315 bool *OutUseUnscaledOp = nullptr,
316 unsigned *OutUnscaledOp = nullptr,
317 int *EmittableOffset = nullptr);
319 static inline bool isUncondBranchOpcode(int Opc) { return Opc == AArch64::B; }
321 static inline bool isCondBranchOpcode(int Opc) {
338 static inline bool isIndirectBranchOpcode(int Opc) {
339 return Opc == AArch64::BR;
343 #define TSFLAG_ELEMENT_SIZE_TYPE(X) (X) // 3-bits
344 #define TSFLAG_DESTRUCTIVE_INST_TYPE(X) ((X) << 3) // 1-bit
349 enum ElementSizeType {
350 ElementSizeMask = TSFLAG_ELEMENT_SIZE_TYPE(0x7),
351 ElementSizeNone = TSFLAG_ELEMENT_SIZE_TYPE(0x0),
352 ElementSizeB = TSFLAG_ELEMENT_SIZE_TYPE(0x1),
353 ElementSizeH = TSFLAG_ELEMENT_SIZE_TYPE(0x2),
354 ElementSizeS = TSFLAG_ELEMENT_SIZE_TYPE(0x3),
355 ElementSizeD = TSFLAG_ELEMENT_SIZE_TYPE(0x4),
358 enum DestructiveInstType {
359 DestructiveInstTypeMask = TSFLAG_DESTRUCTIVE_INST_TYPE(0x1),
360 NotDestructive = TSFLAG_DESTRUCTIVE_INST_TYPE(0x0),
361 Destructive = TSFLAG_DESTRUCTIVE_INST_TYPE(0x1),
364 #undef TSFLAG_ELEMENT_SIZE_TYPE
365 #undef TSFLAG_DESTRUCTIVE_INST_TYPE
368 } // end namespace llvm