xref: /freebsd/contrib/llvm-project/llvm/lib/Target/RISCV/RISCVInstrInfo.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1*06c3fb27SDimitry Andric //===-- RISCVInstrInfo.h - RISC-V Instruction Information -------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
9*06c3fb27SDimitry Andric // This file contains the RISC-V implementation of the TargetInstrInfo class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_LIB_TARGET_RISCV_RISCVINSTRINFO_H
140b57cec5SDimitry Andric #define LLVM_LIB_TARGET_RISCV_RISCVINSTRINFO_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "RISCVRegisterInfo.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
18fe6060f1SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #define GET_INSTRINFO_HEADER
210eae32dcSDimitry Andric #define GET_INSTRINFO_OPERAND_ENUM
220b57cec5SDimitry Andric #include "RISCVGenInstrInfo.inc"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric namespace llvm {
250b57cec5SDimitry Andric 
268bcb0991SDimitry Andric class RISCVSubtarget;
278bcb0991SDimitry Andric 
28*06c3fb27SDimitry Andric static const MachineMemOperand::Flags MONontemporalBit0 =
29*06c3fb27SDimitry Andric     MachineMemOperand::MOTargetFlag1;
30*06c3fb27SDimitry Andric static const MachineMemOperand::Flags MONontemporalBit1 =
31*06c3fb27SDimitry Andric     MachineMemOperand::MOTargetFlag2;
32*06c3fb27SDimitry Andric 
33349cc55cSDimitry Andric namespace RISCVCC {
34349cc55cSDimitry Andric 
35349cc55cSDimitry Andric enum CondCode {
36349cc55cSDimitry Andric   COND_EQ,
37349cc55cSDimitry Andric   COND_NE,
38349cc55cSDimitry Andric   COND_LT,
39349cc55cSDimitry Andric   COND_GE,
40349cc55cSDimitry Andric   COND_LTU,
41349cc55cSDimitry Andric   COND_GEU,
42349cc55cSDimitry Andric   COND_INVALID
43349cc55cSDimitry Andric };
44349cc55cSDimitry Andric 
45349cc55cSDimitry Andric CondCode getOppositeBranchCondition(CondCode);
46349cc55cSDimitry Andric 
47349cc55cSDimitry Andric } // end of namespace RISCVCC
48349cc55cSDimitry Andric 
490b57cec5SDimitry Andric class RISCVInstrInfo : public RISCVGenInstrInfo {
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric public:
528bcb0991SDimitry Andric   explicit RISCVInstrInfo(RISCVSubtarget &STI);
530b57cec5SDimitry Andric 
54fe6060f1SDimitry Andric   MCInst getNop() const override;
55349cc55cSDimitry Andric   const MCInstrDesc &getBrCond(RISCVCC::CondCode CC) const;
56fe6060f1SDimitry Andric 
570b57cec5SDimitry Andric   unsigned isLoadFromStackSlot(const MachineInstr &MI,
580b57cec5SDimitry Andric                                int &FrameIndex) const override;
59*06c3fb27SDimitry Andric   unsigned isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex,
60*06c3fb27SDimitry Andric                                unsigned &MemBytes) const override;
610b57cec5SDimitry Andric   unsigned isStoreToStackSlot(const MachineInstr &MI,
620b57cec5SDimitry Andric                               int &FrameIndex) const override;
63*06c3fb27SDimitry Andric   unsigned isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex,
64*06c3fb27SDimitry Andric                               unsigned &MemBytes) const override;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
67480093f4SDimitry Andric                    const DebugLoc &DL, MCRegister DstReg, MCRegister SrcReg,
680b57cec5SDimitry Andric                    bool KillSrc) const override;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   void storeRegToStackSlot(MachineBasicBlock &MBB,
715ffd83dbSDimitry Andric                            MachineBasicBlock::iterator MBBI, Register SrcReg,
720b57cec5SDimitry Andric                            bool IsKill, int FrameIndex,
730b57cec5SDimitry Andric                            const TargetRegisterClass *RC,
74bdd1243dSDimitry Andric                            const TargetRegisterInfo *TRI,
75bdd1243dSDimitry Andric                            Register VReg) const override;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   void loadRegFromStackSlot(MachineBasicBlock &MBB,
785ffd83dbSDimitry Andric                             MachineBasicBlock::iterator MBBI, Register DstReg,
790b57cec5SDimitry Andric                             int FrameIndex, const TargetRegisterClass *RC,
80bdd1243dSDimitry Andric                             const TargetRegisterInfo *TRI,
81bdd1243dSDimitry Andric                             Register VReg) const override;
820b57cec5SDimitry Andric 
83fcaf7f86SDimitry Andric   using TargetInstrInfo::foldMemoryOperandImpl;
84fcaf7f86SDimitry Andric   MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
85fcaf7f86SDimitry Andric                                       ArrayRef<unsigned> Ops,
86fcaf7f86SDimitry Andric                                       MachineBasicBlock::iterator InsertPt,
87fcaf7f86SDimitry Andric                                       int FrameIndex,
88fcaf7f86SDimitry Andric                                       LiveIntervals *LIS = nullptr,
89fcaf7f86SDimitry Andric                                       VirtRegMap *VRM = nullptr) const override;
90fcaf7f86SDimitry Andric 
918bcb0991SDimitry Andric   // Materializes the given integer Val into DstReg.
928bcb0991SDimitry Andric   void movImm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
938bcb0991SDimitry Andric               const DebugLoc &DL, Register DstReg, uint64_t Val,
940b57cec5SDimitry Andric               MachineInstr::MIFlag Flag = MachineInstr::NoFlags) const;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
990b57cec5SDimitry Andric                      MachineBasicBlock *&FBB,
1000b57cec5SDimitry Andric                      SmallVectorImpl<MachineOperand> &Cond,
1010b57cec5SDimitry Andric                      bool AllowModify) const override;
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
1040b57cec5SDimitry Andric                         MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
1050b57cec5SDimitry Andric                         const DebugLoc &dl,
1060b57cec5SDimitry Andric                         int *BytesAdded = nullptr) const override;
1070b57cec5SDimitry Andric 
108349cc55cSDimitry Andric   void insertIndirectBranch(MachineBasicBlock &MBB,
1090b57cec5SDimitry Andric                             MachineBasicBlock &NewDestBB,
110349cc55cSDimitry Andric                             MachineBasicBlock &RestoreBB, const DebugLoc &DL,
111349cc55cSDimitry Andric                             int64_t BrOffset, RegScavenger *RS) const override;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   unsigned removeBranch(MachineBasicBlock &MBB,
1140b57cec5SDimitry Andric                         int *BytesRemoved = nullptr) const override;
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   bool
1170b57cec5SDimitry Andric   reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   MachineBasicBlock *getBranchDestBlock(const MachineInstr &MI) const override;
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   bool isBranchOffsetInRange(unsigned BranchOpc,
1220b57cec5SDimitry Andric                              int64_t BrOffset) const override;
1230b57cec5SDimitry Andric 
124bdd1243dSDimitry Andric   bool analyzeSelect(const MachineInstr &MI,
125bdd1243dSDimitry Andric                      SmallVectorImpl<MachineOperand> &Cond, unsigned &TrueOp,
126bdd1243dSDimitry Andric                      unsigned &FalseOp, bool &Optimizable) const override;
127bdd1243dSDimitry Andric 
128bdd1243dSDimitry Andric   MachineInstr *optimizeSelect(MachineInstr &MI,
129bdd1243dSDimitry Andric                                SmallPtrSetImpl<MachineInstr *> &SeenMIs,
130bdd1243dSDimitry Andric                                bool) const override;
131bdd1243dSDimitry Andric 
1320b57cec5SDimitry Andric   bool isAsCheapAsAMove(const MachineInstr &MI) const override;
1338bcb0991SDimitry Andric 
134bdd1243dSDimitry Andric   std::optional<DestSourcePair>
135e8d8bef9SDimitry Andric   isCopyInstrImpl(const MachineInstr &MI) const override;
136e8d8bef9SDimitry Andric 
1378bcb0991SDimitry Andric   bool verifyInstruction(const MachineInstr &MI,
1388bcb0991SDimitry Andric                          StringRef &ErrInfo) const override;
1398bcb0991SDimitry Andric 
140480093f4SDimitry Andric   bool getMemOperandWithOffsetWidth(const MachineInstr &LdSt,
141480093f4SDimitry Andric                                     const MachineOperand *&BaseOp,
142480093f4SDimitry Andric                                     int64_t &Offset, unsigned &Width,
143480093f4SDimitry Andric                                     const TargetRegisterInfo *TRI) const;
144480093f4SDimitry Andric 
145480093f4SDimitry Andric   bool areMemAccessesTriviallyDisjoint(const MachineInstr &MIa,
146480093f4SDimitry Andric                                        const MachineInstr &MIb) const override;
147480093f4SDimitry Andric 
148480093f4SDimitry Andric 
149480093f4SDimitry Andric   std::pair<unsigned, unsigned>
150480093f4SDimitry Andric   decomposeMachineOperandsTargetFlags(unsigned TF) const override;
151480093f4SDimitry Andric 
152480093f4SDimitry Andric   ArrayRef<std::pair<unsigned, const char *>>
153480093f4SDimitry Andric   getSerializableDirectMachineOperandTargetFlags() const override;
154480093f4SDimitry Andric 
155480093f4SDimitry Andric   // Return true if the function can safely be outlined from.
156972a253aSDimitry Andric   bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
157480093f4SDimitry Andric                                    bool OutlineFromLinkOnceODRs) const override;
158480093f4SDimitry Andric 
159480093f4SDimitry Andric   // Return true if MBB is safe to outline from, and return any target-specific
160480093f4SDimitry Andric   // information in Flags.
161972a253aSDimitry Andric   bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
162480093f4SDimitry Andric                               unsigned &Flags) const override;
163480093f4SDimitry Andric 
16481ad6265SDimitry Andric   bool shouldOutlineFromFunctionByDefault(MachineFunction &MF) const override;
16581ad6265SDimitry Andric 
166480093f4SDimitry Andric   // Calculate target-specific information for a set of outlining candidates.
167*06c3fb27SDimitry Andric   std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(
168480093f4SDimitry Andric       std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
169480093f4SDimitry Andric 
170480093f4SDimitry Andric   // Return if/how a given MachineInstr should be outlined.
171*06c3fb27SDimitry Andric   virtual outliner::InstrType
172*06c3fb27SDimitry Andric   getOutliningTypeImpl(MachineBasicBlock::iterator &MBBI,
173480093f4SDimitry Andric                        unsigned Flags) const override;
174480093f4SDimitry Andric 
175480093f4SDimitry Andric   // Insert a custom frame for outlined functions.
176972a253aSDimitry Andric   void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,
177480093f4SDimitry Andric                           const outliner::OutlinedFunction &OF) const override;
178480093f4SDimitry Andric 
179480093f4SDimitry Andric   // Insert a call to an outlined function into a given basic block.
180972a253aSDimitry Andric   MachineBasicBlock::iterator
181480093f4SDimitry Andric   insertOutlinedCall(Module &M, MachineBasicBlock &MBB,
182480093f4SDimitry Andric                      MachineBasicBlock::iterator &It, MachineFunction &MF,
18381ad6265SDimitry Andric                      outliner::Candidate &C) const override;
184fe6060f1SDimitry Andric 
185fe6060f1SDimitry Andric   bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1,
186fe6060f1SDimitry Andric                              unsigned &SrcOpIdx2) const override;
187fe6060f1SDimitry Andric   MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
188fe6060f1SDimitry Andric                                        unsigned OpIdx1,
189fe6060f1SDimitry Andric                                        unsigned OpIdx2) const override;
190fe6060f1SDimitry Andric 
191349cc55cSDimitry Andric   MachineInstr *convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
192349cc55cSDimitry Andric                                       LiveIntervals *LIS) const override;
193fe6060f1SDimitry Andric 
19481ad6265SDimitry Andric   // MIR printer helper function to annotate Operands with a comment.
19581ad6265SDimitry Andric   std::string
19681ad6265SDimitry Andric   createMIROperandComment(const MachineInstr &MI, const MachineOperand &Op,
19781ad6265SDimitry Andric                           unsigned OpIdx,
19881ad6265SDimitry Andric                           const TargetRegisterInfo *TRI) const override;
19981ad6265SDimitry Andric 
200bdd1243dSDimitry Andric   void getVLENFactoredAmount(
201fe6060f1SDimitry Andric       MachineFunction &MF, MachineBasicBlock &MBB,
202bdd1243dSDimitry Andric       MachineBasicBlock::iterator II, const DebugLoc &DL, Register DestReg,
203bdd1243dSDimitry Andric       int64_t Amount, MachineInstr::MIFlag Flag = MachineInstr::NoFlags) const;
204bdd1243dSDimitry Andric 
205bdd1243dSDimitry Andric   bool useMachineCombiner() const override { return true; }
206bdd1243dSDimitry Andric 
207*06c3fb27SDimitry Andric   MachineTraceStrategy getMachineCombinerTraceStrategy() const override;
208*06c3fb27SDimitry Andric 
209bdd1243dSDimitry Andric   void setSpecialOperandAttr(MachineInstr &OldMI1, MachineInstr &OldMI2,
210bdd1243dSDimitry Andric                              MachineInstr &NewMI1,
211bdd1243dSDimitry Andric                              MachineInstr &NewMI2) const override;
212bdd1243dSDimitry Andric   bool
213bdd1243dSDimitry Andric   getMachineCombinerPatterns(MachineInstr &Root,
214bdd1243dSDimitry Andric                              SmallVectorImpl<MachineCombinerPattern> &Patterns,
215bdd1243dSDimitry Andric                              bool DoRegPressureReduce) const override;
216bdd1243dSDimitry Andric 
217bdd1243dSDimitry Andric   void
218bdd1243dSDimitry Andric   finalizeInsInstrs(MachineInstr &Root, MachineCombinerPattern &P,
219bdd1243dSDimitry Andric                     SmallVectorImpl<MachineInstr *> &InsInstrs) const override;
220bdd1243dSDimitry Andric 
221bdd1243dSDimitry Andric   void genAlternativeCodeSequence(
222bdd1243dSDimitry Andric       MachineInstr &Root, MachineCombinerPattern Pattern,
223bdd1243dSDimitry Andric       SmallVectorImpl<MachineInstr *> &InsInstrs,
224bdd1243dSDimitry Andric       SmallVectorImpl<MachineInstr *> &DelInstrs,
225bdd1243dSDimitry Andric       DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const override;
226bdd1243dSDimitry Andric 
227bdd1243dSDimitry Andric   bool hasReassociableSibling(const MachineInstr &Inst,
228bdd1243dSDimitry Andric                               bool &Commuted) const override;
229bdd1243dSDimitry Andric 
230bdd1243dSDimitry Andric   bool isAssociativeAndCommutative(const MachineInstr &Inst,
231bdd1243dSDimitry Andric                                    bool Invert) const override;
232bdd1243dSDimitry Andric 
233bdd1243dSDimitry Andric   std::optional<unsigned> getInverseOpcode(unsigned Opcode) const override;
234bdd1243dSDimitry Andric 
235*06c3fb27SDimitry Andric   ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
236*06c3fb27SDimitry Andric   getSerializableMachineMemOperandTargetFlags() const override;
237fe6060f1SDimitry Andric 
2388bcb0991SDimitry Andric protected:
2398bcb0991SDimitry Andric   const RISCVSubtarget &STI;
240*06c3fb27SDimitry Andric 
241*06c3fb27SDimitry Andric private:
242*06c3fb27SDimitry Andric   unsigned getInstBundleLength(const MachineInstr &MI) const;
2430b57cec5SDimitry Andric };
2445ffd83dbSDimitry Andric 
245349cc55cSDimitry Andric namespace RISCV {
2460eae32dcSDimitry Andric 
247fcaf7f86SDimitry Andric // Returns true if this is the sext.w pattern, addiw rd, rs1, 0.
248fcaf7f86SDimitry Andric bool isSEXT_W(const MachineInstr &MI);
249fcaf7f86SDimitry Andric bool isZEXT_W(const MachineInstr &MI);
250fcaf7f86SDimitry Andric bool isZEXT_B(const MachineInstr &MI);
251fcaf7f86SDimitry Andric 
25281ad6265SDimitry Andric // Returns true if the given MI is an RVV instruction opcode for which we may
25381ad6265SDimitry Andric // expect to see a FrameIndex operand.
25481ad6265SDimitry Andric bool isRVVSpill(const MachineInstr &MI);
25581ad6265SDimitry Andric 
256bdd1243dSDimitry Andric std::optional<std::pair<unsigned, unsigned>>
257bdd1243dSDimitry Andric isRVVSpillForZvlsseg(unsigned Opcode);
25881ad6265SDimitry Andric 
25981ad6265SDimitry Andric bool isFaultFirstLoad(const MachineInstr &MI);
26081ad6265SDimitry Andric 
2610eae32dcSDimitry Andric // Implemented in RISCVGenInstrInfo.inc
2620eae32dcSDimitry Andric int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIndex);
2630eae32dcSDimitry Andric 
264bdd1243dSDimitry Andric // Return true if both input instructions have equal rounding mode. If at least
265bdd1243dSDimitry Andric // one of the instructions does not have rounding mode, false will be returned.
266bdd1243dSDimitry Andric bool hasEqualFRM(const MachineInstr &MI1, const MachineInstr &MI2);
267bdd1243dSDimitry Andric 
268349cc55cSDimitry Andric // Special immediate for AVL operand of V pseudo instructions to indicate VLMax.
269349cc55cSDimitry Andric static constexpr int64_t VLMaxSentinel = -1LL;
270bdd1243dSDimitry Andric 
271*06c3fb27SDimitry Andric // Mask assignments for floating-point
272*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Negative_Infinity = 0x001;
273*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Negative_Normal = 0x002;
274*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Negative_Subnormal = 0x004;
275*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Negative_Zero = 0x008;
276*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Positive_Zero = 0x010;
277*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Positive_Subnormal = 0x020;
278*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Positive_Normal = 0x040;
279*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Positive_Infinity = 0x080;
280*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Signaling_NaN = 0x100;
281*06c3fb27SDimitry Andric static constexpr unsigned FPMASK_Quiet_NaN = 0x200;
282349cc55cSDimitry Andric } // namespace RISCV
283349cc55cSDimitry Andric 
284fe6060f1SDimitry Andric namespace RISCVVPseudosTable {
285fe6060f1SDimitry Andric 
286fe6060f1SDimitry Andric struct PseudoInfo {
287fe6060f1SDimitry Andric   uint16_t Pseudo;
288fe6060f1SDimitry Andric   uint16_t BaseInstr;
289fe6060f1SDimitry Andric };
290fe6060f1SDimitry Andric 
291fe6060f1SDimitry Andric #define GET_RISCVVPseudosTable_DECL
292fe6060f1SDimitry Andric #include "RISCVGenSearchableTables.inc"
293fe6060f1SDimitry Andric 
294fe6060f1SDimitry Andric } // end namespace RISCVVPseudosTable
295fe6060f1SDimitry Andric 
2965ffd83dbSDimitry Andric } // end namespace llvm
2970b57cec5SDimitry Andric #endif
298