LLVM 18.0.0git
RISCVInstrInfo.h
Go to the documentation of this file.
1//===-- RISCVInstrInfo.h - RISC-V Instruction Information -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the RISC-V implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_RISCV_RISCVINSTRINFO_H
14#define LLVM_LIB_TARGET_RISCV_RISCVINSTRINFO_H
15
16#include "RISCVRegisterInfo.h"
19
20#define GET_INSTRINFO_HEADER
21#define GET_INSTRINFO_OPERAND_ENUM
22#include "RISCVGenInstrInfo.inc"
23
24namespace llvm {
25
26class RISCVSubtarget;
27
32
33namespace RISCVCC {
34
43};
44
46unsigned getBrCond(CondCode CC);
47
48} // end of namespace RISCVCC
49
51
52public:
54
55 MCInst getNop() const override;
57
58 unsigned isLoadFromStackSlot(const MachineInstr &MI,
59 int &FrameIndex) const override;
60 unsigned isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex,
61 unsigned &MemBytes) const override;
62 unsigned isStoreToStackSlot(const MachineInstr &MI,
63 int &FrameIndex) const override;
64 unsigned isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex,
65 unsigned &MemBytes) const override;
66
69 MCRegister DstReg, MCRegister SrcReg, bool KillSrc,
70 unsigned Opc, unsigned NF = 1) const;
72 const DebugLoc &DL, MCRegister DstReg, MCRegister SrcReg,
73 bool KillSrc) const override;
74
77 bool IsKill, int FrameIndex,
78 const TargetRegisterClass *RC,
80 Register VReg) const override;
81
84 int FrameIndex, const TargetRegisterClass *RC,
86 Register VReg) const override;
87
92 int FrameIndex,
93 LiveIntervals *LIS = nullptr,
94 VirtRegMap *VRM = nullptr) const override;
95
96 // Materializes the given integer Val into DstReg.
98 const DebugLoc &DL, Register DstReg, uint64_t Val,
100 bool DstRenamable = false, bool DstIsDead = false) const;
101
102 unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
103
105 MachineBasicBlock *&FBB,
107 bool AllowModify) const override;
108
111 const DebugLoc &dl,
112 int *BytesAdded = nullptr) const override;
113
115 MachineBasicBlock &NewDestBB,
116 MachineBasicBlock &RestoreBB, const DebugLoc &DL,
117 int64_t BrOffset, RegScavenger *RS) const override;
118
120 int *BytesRemoved = nullptr) const override;
121
122 bool
124
125 bool optimizeCondBranch(MachineInstr &MI) const override;
126
127 MachineBasicBlock *getBranchDestBlock(const MachineInstr &MI) const override;
128
129 bool isBranchOffsetInRange(unsigned BranchOpc,
130 int64_t BrOffset) const override;
131
132 bool analyzeSelect(const MachineInstr &MI,
133 SmallVectorImpl<MachineOperand> &Cond, unsigned &TrueOp,
134 unsigned &FalseOp, bool &Optimizable) const override;
135
138 bool) const override;
139
140 bool isAsCheapAsAMove(const MachineInstr &MI) const override;
141
142 std::optional<DestSourcePair>
143 isCopyInstrImpl(const MachineInstr &MI) const override;
144
146 StringRef &ErrInfo) const override;
147
149 const MachineInstr &AddrI,
150 ExtAddrMode &AM) const override;
151
153 const ExtAddrMode &AM) const override;
154
157 int64_t &Offset, bool &OffsetIsScalable, unsigned &Width,
158 const TargetRegisterInfo *TRI) const override;
159
162 unsigned ClusterSize,
163 unsigned NumBytes) const override;
164
166 const MachineOperand *&BaseOp,
167 int64_t &Offset, unsigned &Width,
168 const TargetRegisterInfo *TRI) const;
169
171 const MachineInstr &MIb) const override;
172
173
174 std::pair<unsigned, unsigned>
175 decomposeMachineOperandsTargetFlags(unsigned TF) const override;
176
179
180 // Return true if the function can safely be outlined from.
182 bool OutlineFromLinkOnceODRs) const override;
183
184 // Return true if MBB is safe to outline from, and return any target-specific
185 // information in Flags.
187 unsigned &Flags) const override;
188
190
191 // Calculate target-specific information for a set of outlining candidates.
192 std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
193 std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
194
195 // Return if/how a given MachineInstr should be outlined.
196 virtual outliner::InstrType
198 unsigned Flags) const override;
199
200 // Insert a custom frame for outlined functions.
202 const outliner::OutlinedFunction &OF) const override;
203
204 // Insert a call to an outlined function into a given basic block.
208 outliner::Candidate &C) const override;
209
210 std::optional<RegImmPair> isAddImmediate(const MachineInstr &MI,
211 Register Reg) const override;
212
213 bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1,
214 unsigned &SrcOpIdx2) const override;
216 unsigned OpIdx1,
217 unsigned OpIdx2) const override;
218
220 LiveIntervals *LIS) const override;
221
222 // MIR printer helper function to annotate Operands with a comment.
223 std::string
225 unsigned OpIdx,
226 const TargetRegisterInfo *TRI) const override;
227
231 int64_t Amount, MachineInstr::MIFlag Flag = MachineInstr::NoFlags) const;
232
233 bool useMachineCombiner() const override { return true; }
234
236
237 bool
240 bool DoRegPressureReduce) const override;
241
242 void
244 SmallVectorImpl<MachineInstr *> &InsInstrs) const override;
245
250 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const override;
251
252 bool hasReassociableSibling(const MachineInstr &Inst,
253 bool &Commuted) const override;
254
256 bool Invert) const override;
257
258 std::optional<unsigned> getInverseOpcode(unsigned Opcode) const override;
259
262
263protected:
265
266private:
267 unsigned getInstBundleLength(const MachineInstr &MI) const;
268};
269
270namespace RISCV {
271
272// Returns true if this is the sext.w pattern, addiw rd, rs1, 0.
273bool isSEXT_W(const MachineInstr &MI);
274bool isZEXT_W(const MachineInstr &MI);
275bool isZEXT_B(const MachineInstr &MI);
276
277// Returns true if the given MI is an RVV instruction opcode for which we may
278// expect to see a FrameIndex operand.
279bool isRVVSpill(const MachineInstr &MI);
280
281std::optional<std::pair<unsigned, unsigned>>
283
284bool isFaultFirstLoad(const MachineInstr &MI);
285
286// Implemented in RISCVGenInstrInfo.inc
288
289// Return true if both input instructions have equal rounding mode. If at least
290// one of the instructions does not have rounding mode, false will be returned.
291bool hasEqualFRM(const MachineInstr &MI1, const MachineInstr &MI2);
292
293// If \p Opcode is a .vx vector instruction, returns the lower number of bits
294// that are used from the scalar .x operand for a given \p Log2SEW. Otherwise
295// returns null.
296std::optional<unsigned> getVectorLowDemandedScalarBits(uint16_t Opcode,
297 unsigned Log2SEW);
298
299// Returns the MC opcode of RVV pseudo instruction.
300unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode);
301
302// Special immediate for AVL operand of V pseudo instructions to indicate VLMax.
303static constexpr int64_t VLMaxSentinel = -1LL;
304
305// Mask assignments for floating-point
306static constexpr unsigned FPMASK_Negative_Infinity = 0x001;
307static constexpr unsigned FPMASK_Negative_Normal = 0x002;
308static constexpr unsigned FPMASK_Negative_Subnormal = 0x004;
309static constexpr unsigned FPMASK_Negative_Zero = 0x008;
310static constexpr unsigned FPMASK_Positive_Zero = 0x010;
311static constexpr unsigned FPMASK_Positive_Subnormal = 0x020;
312static constexpr unsigned FPMASK_Positive_Normal = 0x040;
313static constexpr unsigned FPMASK_Positive_Infinity = 0x080;
314static constexpr unsigned FPMASK_Signaling_NaN = 0x100;
315static constexpr unsigned FPMASK_Quiet_NaN = 0x200;
316} // namespace RISCV
317
318namespace RISCVVPseudosTable {
319
323};
324
325#define GET_RISCVVPseudosTable_DECL
326#include "RISCVGenSearchableTables.inc"
327
328} // end namespace RISCVVPseudosTable
329
330} // end namespace llvm
331#endif
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
IRTranslator LLVM IR MI
unsigned const TargetRegisterInfo * TRI
unsigned Reg
#define P(N)
unsigned Log2SEW
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static constexpr uint32_t Opcode
Definition: aarch32.h:200
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:33
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Representation of each machine instruction.
Definition: MachineInstr.h:68
Flags
Flags values. These may be or'd together.
MachineOperand class - Representation of each machine instruction operand.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
MachineInstr * convertToThreeAddress(MachineInstr &MI, LiveVariables *LV, LiveIntervals *LIS) const override
unsigned isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex) const override
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
std::optional< outliner::OutlinedFunction > getOutliningCandidateInfo(std::vector< outliner::Candidate > &RepeatedSequenceLocs) const override
void movImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, Register DstReg, uint64_t Val, MachineInstr::MIFlag Flag=MachineInstr::NoFlags, bool DstRenamable=false, bool DstIsDead=false) const
unsigned isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex) const override
MachineInstr * emitLdStWithAddr(MachineInstr &MemI, const ExtAddrMode &AM) const override
const MCInstrDesc & getBrCond(RISCVCC::CondCode CC) const
bool isFunctionSafeToOutlineFrom(MachineFunction &MF, bool OutlineFromLinkOnceODRs) const override
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &dl, int *BytesAdded=nullptr) const override
bool hasReassociableSibling(const MachineInstr &Inst, bool &Commuted) const override
void genAlternativeCodeSequence(MachineInstr &Root, MachineCombinerPattern Pattern, SmallVectorImpl< MachineInstr * > &InsInstrs, SmallVectorImpl< MachineInstr * > &DelInstrs, DenseMap< unsigned, unsigned > &InstrIdxForVirtReg) const override
MachineInstr * optimizeSelect(MachineInstr &MI, SmallPtrSetImpl< MachineInstr * > &SeenMIs, bool) const override
bool canFoldIntoAddrMode(const MachineInstr &MemI, Register Reg, const MachineInstr &AddrI, ExtAddrMode &AM) const override
void insertIndirectBranch(MachineBasicBlock &MBB, MachineBasicBlock &NewDestBB, MachineBasicBlock &RestoreBB, const DebugLoc &DL, int64_t BrOffset, RegScavenger *RS) const override
bool isAsCheapAsAMove(const MachineInstr &MI) const override
bool verifyInstruction(const MachineInstr &MI, StringRef &ErrInfo) const override
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, MCRegister DstReg, MCRegister SrcReg, bool KillSrc) const override
const RISCVSubtarget & STI
bool getMachineCombinerPatterns(MachineInstr &Root, SmallVectorImpl< MachineCombinerPattern > &Patterns, bool DoRegPressureReduce) const override
bool useMachineCombiner() const override
std::optional< unsigned > getInverseOpcode(unsigned Opcode) const override
ArrayRef< std::pair< unsigned, const char * > > getSerializableDirectMachineOperandTargetFlags() const override
MachineTraceStrategy getMachineCombinerTraceStrategy() const override
unsigned getInstSizeInBytes(const MachineInstr &MI) const override
virtual outliner::InstrType getOutliningTypeImpl(MachineBasicBlock::iterator &MBBI, unsigned Flags) const override
std::optional< RegImmPair > isAddImmediate(const MachineInstr &MI, Register Reg) const override
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
ArrayRef< std::pair< MachineMemOperand::Flags, const char * > > getSerializableMachineMemOperandTargetFlags() const override
MCInst getNop() const override
void finalizeInsInstrs(MachineInstr &Root, MachineCombinerPattern &P, SmallVectorImpl< MachineInstr * > &InsInstrs) const override
MachineInstr * foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI, ArrayRef< unsigned > Ops, MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS=nullptr, VirtRegMap *VRM=nullptr) const override
bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB, unsigned &Flags) const override
void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF, const outliner::OutlinedFunction &OF) const override
std::pair< unsigned, unsigned > decomposeMachineOperandsTargetFlags(unsigned TF) const override
bool getMemOperandsWithOffsetWidth(const MachineInstr &MI, SmallVectorImpl< const MachineOperand * > &BaseOps, int64_t &Offset, bool &OffsetIsScalable, unsigned &Width, const TargetRegisterInfo *TRI) const override
MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const override
void getVLENFactoredAmount(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator II, const DebugLoc &DL, Register DestReg, int64_t Amount, MachineInstr::MIFlag Flag=MachineInstr::NoFlags) const
MachineBasicBlock * getBranchDestBlock(const MachineInstr &MI) const override
std::string createMIROperandComment(const MachineInstr &MI, const MachineOperand &Op, unsigned OpIdx, const TargetRegisterInfo *TRI) const override
bool getMemOperandWithOffsetWidth(const MachineInstr &LdSt, const MachineOperand *&BaseOp, int64_t &Offset, unsigned &Width, const TargetRegisterInfo *TRI) const
bool shouldOutlineFromFunctionByDefault(MachineFunction &MF) const override
bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1, unsigned &SrcOpIdx2) const override
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
MachineBasicBlock::iterator insertOutlinedCall(Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It, MachineFunction &MF, outliner::Candidate &C) const override
bool isBranchOffsetInRange(unsigned BranchOpc, int64_t BrOffset) const override
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DstReg, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg) const override
bool shouldClusterMemOps(ArrayRef< const MachineOperand * > BaseOps1, ArrayRef< const MachineOperand * > BaseOps2, unsigned ClusterSize, unsigned NumBytes) const override
bool isAssociativeAndCommutative(const MachineInstr &Inst, bool Invert) const override
void copyPhysRegVector(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, MCRegister DstReg, MCRegister SrcReg, bool KillSrc, unsigned Opc, unsigned NF=1) const
bool optimizeCondBranch(MachineInstr &MI) const override
std::optional< DestSourcePair > isCopyInstrImpl(const MachineInstr &MI) const override
bool analyzeSelect(const MachineInstr &MI, SmallVectorImpl< MachineOperand > &Cond, unsigned &TrueOp, unsigned &FalseOp, bool &Optimizable) const override
bool areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, const MachineInstr &MIb) const override
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool IsKill, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg) const override
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:345
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
virtual MachineInstr * foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI, ArrayRef< unsigned > Ops, MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS=nullptr, VirtRegMap *VRM=nullptr) const
Target-dependent implementation for foldMemoryOperand.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
CondCode getOppositeBranchCondition(CondCode)
unsigned getBrCond(CondCode CC)
static constexpr unsigned FPMASK_Negative_Zero
static constexpr unsigned FPMASK_Positive_Subnormal
bool hasEqualFRM(const MachineInstr &MI1, const MachineInstr &MI2)
static constexpr unsigned FPMASK_Positive_Normal
static constexpr unsigned FPMASK_Negative_Subnormal
static constexpr unsigned FPMASK_Negative_Normal
std::optional< unsigned > getVectorLowDemandedScalarBits(uint16_t Opcode, unsigned Log2SEW)
static constexpr unsigned FPMASK_Positive_Infinity
int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIndex)
static constexpr unsigned FPMASK_Negative_Infinity
static constexpr unsigned FPMASK_Quiet_NaN
unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode)
bool isSEXT_W(const MachineInstr &MI)
bool isFaultFirstLoad(const MachineInstr &MI)
std::optional< std::pair< unsigned, unsigned > > isRVVSpillForZvlsseg(unsigned Opcode)
static constexpr unsigned FPMASK_Signaling_NaN
bool isZEXT_B(const MachineInstr &MI)
static constexpr unsigned FPMASK_Positive_Zero
bool isRVVSpill(const MachineInstr &MI)
static constexpr int64_t VLMaxSentinel
bool isZEXT_W(const MachineInstr &MI)
InstrType
Represents how an instruction should be mapped by the outliner.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:440
MachineTraceStrategy
Strategies for selecting traces.
static const MachineMemOperand::Flags MONontemporalBit1
static const MachineMemOperand::Flags MONontemporalBit0
MachineCombinerPattern
These are instruction patterns matched by the machine combiner pass.
Used to describe addressing mode similar to ExtAddrMode in CodeGenPrepare.
An individual sequence of instructions to be replaced with a call to an outlined function.
The information necessary to create an outlined function for some class of candidate.