Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1 | //===-- RISCVISelLowering.h - RISCV DAG Lowering Interface ------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the interfaces that RISCV uses to lower LLVM code into a |
| 10 | // selection DAG. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_LIB_TARGET_RISCV_RISCVISELLOWERING_H |
| 15 | #define LLVM_LIB_TARGET_RISCV_RISCVISELLOWERING_H |
| 16 | |
| 17 | #include "RISCV.h" |
| 18 | #include "llvm/CodeGen/SelectionDAG.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/TargetLowering.h" |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 20 | |
| 21 | namespace llvm { |
| 22 | class RISCVSubtarget; |
| 23 | namespace RISCVISD { |
| 24 | enum NodeType : unsigned { |
| 25 | FIRST_NUMBER = ISD::BUILTIN_OP_END, |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 26 | RET_FLAG, |
Ana Pazos | 2e4106b | 2018-07-26 17:49:43 +0000 | [diff] [blame] | 27 | URET_FLAG, |
| 28 | SRET_FLAG, |
| 29 | MRET_FLAG, |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 30 | CALL, |
Alex Bradbury | 0b4175f | 2018-04-12 05:34:25 +0000 | [diff] [blame] | 31 | SELECT_CC, |
| 32 | BuildPairF64, |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 33 | SplitF64, |
Alex Bradbury | 299d690 | 2019-01-25 05:04:00 +0000 | [diff] [blame] | 34 | TAIL, |
| 35 | // RV64I shifts, directly matching the semantics of the named RISC-V |
| 36 | // instructions. |
| 37 | SLLW, |
| 38 | SRAW, |
Alex Bradbury | 456d379 | 2019-01-25 05:11:34 +0000 | [diff] [blame] | 39 | SRLW, |
| 40 | // 32-bit operations from RV64M that can't be simply matched with a pattern |
| 41 | // at instruction selection time. |
| 42 | DIVW, |
| 43 | DIVUW, |
Alex Bradbury | d834d83 | 2019-01-31 22:48:38 +0000 | [diff] [blame] | 44 | REMUW, |
| 45 | // FPR32<->GPR transfer operations for RV64. Needed as an i32<->f32 bitcast |
| 46 | // is not legal on RV64. FMV_W_X_RV64 matches the semantics of the FMV.W.X. |
| 47 | // FMV_X_ANYEXTW_RV64 is similar to FMV.X.W but has an any-extended result. |
| 48 | // This is a more convenient semantic for producing dagcombines that remove |
| 49 | // unnecessary GPR->FPR->GPR moves. |
| 50 | FMV_W_X_RV64, |
Sam Elliott | b2c9eed | 2019-07-05 12:35:21 +0000 | [diff] [blame] | 51 | FMV_X_ANYEXTW_RV64, |
| 52 | // READ_CYCLE_WIDE - A read of the 64-bit cycle CSR on a 32-bit target |
| 53 | // (returns (Lo, Hi)). It takes a chain operand. |
| 54 | READ_CYCLE_WIDE |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 55 | }; |
| 56 | } |
| 57 | |
| 58 | class RISCVTargetLowering : public TargetLowering { |
| 59 | const RISCVSubtarget &Subtarget; |
| 60 | |
| 61 | public: |
| 62 | explicit RISCVTargetLowering(const TargetMachine &TM, |
| 63 | const RISCVSubtarget &STI); |
| 64 | |
Alex Bradbury | 21aea51 | 2018-09-19 10:54:22 +0000 | [diff] [blame] | 65 | bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I, |
| 66 | MachineFunction &MF, |
| 67 | unsigned Intrinsic) const override; |
Alex Bradbury | 0992629 | 2018-04-26 12:13:48 +0000 | [diff] [blame] | 68 | bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty, |
| 69 | unsigned AS, |
| 70 | Instruction *I = nullptr) const override; |
Alex Bradbury | dcbff63 | 2018-04-26 13:15:17 +0000 | [diff] [blame] | 71 | bool isLegalICmpImmediate(int64_t Imm) const override; |
Alex Bradbury | 5c41ece | 2018-04-26 13:00:37 +0000 | [diff] [blame] | 72 | bool isLegalAddImmediate(int64_t Imm) const override; |
Alex Bradbury | 130b8b3 | 2018-04-26 13:37:00 +0000 | [diff] [blame] | 73 | bool isTruncateFree(Type *SrcTy, Type *DstTy) const override; |
| 74 | bool isTruncateFree(EVT SrcVT, EVT DstVT) const override; |
Alex Bradbury | 15e894b | 2018-04-26 14:04:18 +0000 | [diff] [blame] | 75 | bool isZExtFree(SDValue Val, EVT VT2) const override; |
Alex Bradbury | e0e62e9 | 2018-11-30 09:56:54 +0000 | [diff] [blame] | 76 | bool isSExtCheaperThanZExt(EVT SrcVT, EVT DstVT) const override; |
Alex Bradbury | 0992629 | 2018-04-26 12:13:48 +0000 | [diff] [blame] | 77 | |
Sam Elliott | f720647 | 2019-06-07 12:20:14 +0000 | [diff] [blame] | 78 | bool hasBitPreservingFPLogic(EVT VT) const override; |
| 79 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 80 | // Provide custom lowering hooks for some operations. |
| 81 | SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override; |
Alex Bradbury | 299d690 | 2019-01-25 05:04:00 +0000 | [diff] [blame] | 82 | void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results, |
| 83 | SelectionDAG &DAG) const override; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 84 | |
Alex Bradbury | 5ac0a2f | 2018-10-03 23:30:16 +0000 | [diff] [blame] | 85 | SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override; |
| 86 | |
Alex Bradbury | 299d690 | 2019-01-25 05:04:00 +0000 | [diff] [blame] | 87 | unsigned ComputeNumSignBitsForTargetNode(SDValue Op, |
| 88 | const APInt &DemandedElts, |
| 89 | const SelectionDAG &DAG, |
| 90 | unsigned Depth) const override; |
| 91 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 92 | // This method returns the name of a target specific DAG node. |
| 93 | const char *getTargetNodeName(unsigned Opcode) const override; |
| 94 | |
Sam Elliott | 9e6b2e1 | 2019-07-31 09:45:55 +0000 | [diff] [blame] | 95 | ConstraintType getConstraintType(StringRef Constraint) const override; |
Lewis Revill | 7abf863 | 2019-08-16 10:28:34 +0000 | [diff] [blame] | 96 | |
| 97 | unsigned getInlineAsmMemConstraint(StringRef ConstraintCode) const override; |
| 98 | |
Alex Bradbury | 9330e64 | 2018-01-10 20:05:09 +0000 | [diff] [blame] | 99 | std::pair<unsigned, const TargetRegisterClass *> |
| 100 | getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, |
| 101 | StringRef Constraint, MVT VT) const override; |
| 102 | |
Lewis Revill | 28a5cad | 2019-06-11 12:42:13 +0000 | [diff] [blame] | 103 | void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint, |
| 104 | std::vector<SDValue> &Ops, |
| 105 | SelectionDAG &DAG) const override; |
| 106 | |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 107 | MachineBasicBlock * |
| 108 | EmitInstrWithCustomInserter(MachineInstr &MI, |
| 109 | MachineBasicBlock *BB) const override; |
| 110 | |
Shiva Chen | bbf4c5c | 2018-02-02 02:43:18 +0000 | [diff] [blame] | 111 | EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, |
| 112 | EVT VT) const override; |
| 113 | |
Alex Bradbury | 3369101 | 2019-03-22 10:39:22 +0000 | [diff] [blame] | 114 | bool convertSetCCLogicToBitwiseLogic(EVT VT) const override { |
| 115 | return VT.isScalarInteger(); |
| 116 | } |
| 117 | |
Alex Bradbury | 96f492d | 2018-06-13 12:04:51 +0000 | [diff] [blame] | 118 | bool shouldInsertFencesForAtomic(const Instruction *I) const override { |
| 119 | return isa<LoadInst>(I) || isa<StoreInst>(I); |
| 120 | } |
| 121 | Instruction *emitLeadingFence(IRBuilder<> &Builder, Instruction *Inst, |
| 122 | AtomicOrdering Ord) const override; |
| 123 | Instruction *emitTrailingFence(IRBuilder<> &Builder, Instruction *Inst, |
| 124 | AtomicOrdering Ord) const override; |
| 125 | |
Alex Bradbury | 4d20cc2 | 2019-03-11 21:41:22 +0000 | [diff] [blame] | 126 | ISD::NodeType getExtendForAtomicOps() const override { |
| 127 | return ISD::SIGN_EXTEND; |
| 128 | } |
| 129 | |
Luis Marques | 20d2424 | 2019-04-16 14:38:32 +0000 | [diff] [blame] | 130 | bool shouldExpandShift(SelectionDAG &DAG, SDNode *N) const override { |
| 131 | if (DAG.getMachineFunction().getFunction().hasMinSize()) |
| 132 | return false; |
| 133 | return true; |
| 134 | } |
Sam Elliott | 9f155bc | 2019-06-18 20:38:08 +0000 | [diff] [blame] | 135 | bool isDesirableToCommuteWithShift(const SDNode *N, |
| 136 | CombineLevel Level) const override; |
Luis Marques | 20d2424 | 2019-04-16 14:38:32 +0000 | [diff] [blame] | 137 | |
Alex Bradbury | 0b9addb | 2019-07-08 09:16:47 +0000 | [diff] [blame] | 138 | /// If a physical register, this returns the register that receives the |
| 139 | /// exception address on entry to an EH pad. |
| 140 | unsigned |
| 141 | getExceptionPointerRegister(const Constant *PersonalityFn) const override; |
| 142 | |
| 143 | /// If a physical register, this returns the register that receives the |
| 144 | /// exception typeid on entry to a landing pad. |
| 145 | unsigned |
| 146 | getExceptionSelectorRegister(const Constant *PersonalityFn) const override; |
| 147 | |
Shiva Chen | b39876d | 2019-08-28 23:40:37 +0000 | [diff] [blame] | 148 | bool shouldExtendTypeInLibCall(EVT Type) const override; |
| 149 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 150 | private: |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 151 | void analyzeInputArgs(MachineFunction &MF, CCState &CCInfo, |
| 152 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 153 | bool IsRet) const; |
| 154 | void analyzeOutputArgs(MachineFunction &MF, CCState &CCInfo, |
| 155 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 156 | bool IsRet, CallLoweringInfo *CLI) const; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 157 | // Lower incoming arguments, copy physregs into vregs |
| 158 | SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, |
| 159 | bool IsVarArg, |
| 160 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| 161 | const SDLoc &DL, SelectionDAG &DAG, |
| 162 | SmallVectorImpl<SDValue> &InVals) const override; |
Alex Bradbury | dc31c61 | 2017-12-11 12:49:02 +0000 | [diff] [blame] | 163 | bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF, |
| 164 | bool IsVarArg, |
| 165 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 166 | LLVMContext &Context) const override; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 167 | SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, |
| 168 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 169 | const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL, |
| 170 | SelectionDAG &DAG) const override; |
Alex Bradbury | a337675 | 2017-11-08 13:41:21 +0000 | [diff] [blame] | 171 | SDValue LowerCall(TargetLowering::CallLoweringInfo &CLI, |
| 172 | SmallVectorImpl<SDValue> &InVals) const override; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 173 | bool shouldConvertConstantLoadToIntImm(const APInt &Imm, |
| 174 | Type *Ty) const override { |
| 175 | return true; |
| 176 | } |
Alex Bradbury | da20f5c | 2019-04-01 14:42:56 +0000 | [diff] [blame] | 177 | |
| 178 | template <class NodeTy> |
Lewis Revill | a524036 | 2019-06-11 12:57:47 +0000 | [diff] [blame] | 179 | SDValue getAddr(NodeTy *N, SelectionDAG &DAG, bool IsLocal = true) const; |
Alex Bradbury | da20f5c | 2019-04-01 14:42:56 +0000 | [diff] [blame] | 180 | |
Lewis Revill | 39263ac | 2019-06-19 08:40:59 +0000 | [diff] [blame] | 181 | SDValue getStaticTLSAddr(GlobalAddressSDNode *N, SelectionDAG &DAG, |
| 182 | bool UseGOT) const; |
| 183 | SDValue getDynamicTLSAddr(GlobalAddressSDNode *N, SelectionDAG &DAG) const; |
| 184 | |
Luis Marques | 2e46312 | 2019-06-17 10:54:12 +0000 | [diff] [blame] | 185 | bool shouldConsiderGEPOffsetSplit() const override { return true; } |
Alex Bradbury | ec8aa91 | 2017-11-08 13:24:21 +0000 | [diff] [blame] | 186 | SDValue lowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const; |
Alex Bradbury | ffc435e | 2017-11-21 08:11:03 +0000 | [diff] [blame] | 187 | SDValue lowerBlockAddress(SDValue Op, SelectionDAG &DAG) const; |
Alex Bradbury | 80c8eb7 | 2018-03-20 13:26:12 +0000 | [diff] [blame] | 188 | SDValue lowerConstantPool(SDValue Op, SelectionDAG &DAG) const; |
Lewis Revill | 39263ac | 2019-06-19 08:40:59 +0000 | [diff] [blame] | 189 | SDValue lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const; |
Alex Bradbury | 6538516 | 2017-11-21 07:51:32 +0000 | [diff] [blame] | 190 | SDValue lowerSELECT(SDValue Op, SelectionDAG &DAG) const; |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 191 | SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const; |
Alex Bradbury | 0e16766 | 2018-10-04 05:27:50 +0000 | [diff] [blame] | 192 | SDValue lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const; |
| 193 | SDValue lowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const; |
Luis Marques | 20d2424 | 2019-04-16 14:38:32 +0000 | [diff] [blame] | 194 | SDValue lowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const; |
| 195 | SDValue lowerShiftRightParts(SDValue Op, SelectionDAG &DAG, bool IsSRA) const; |
Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 196 | |
Alex Bradbury | db67be8 | 2019-02-21 14:31:41 +0000 | [diff] [blame] | 197 | bool isEligibleForTailCallOptimization( |
| 198 | CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF, |
| 199 | const SmallVector<CCValAssign, 16> &ArgLocs) const; |
Alex Bradbury | 21aea51 | 2018-09-19 10:54:22 +0000 | [diff] [blame] | 200 | |
| 201 | TargetLowering::AtomicExpansionKind |
| 202 | shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const override; |
| 203 | virtual Value *emitMaskedAtomicRMWIntrinsic( |
| 204 | IRBuilder<> &Builder, AtomicRMWInst *AI, Value *AlignedAddr, Value *Incr, |
| 205 | Value *Mask, Value *ShiftAmt, AtomicOrdering Ord) const override; |
Alex Bradbury | 66d9a75 | 2018-11-29 20:43:42 +0000 | [diff] [blame] | 206 | TargetLowering::AtomicExpansionKind |
| 207 | shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *CI) const override; |
| 208 | virtual Value * |
| 209 | emitMaskedAtomicCmpXchgIntrinsic(IRBuilder<> &Builder, AtomicCmpXchgInst *CI, |
| 210 | Value *AlignedAddr, Value *CmpVal, |
| 211 | Value *NewVal, Value *Mask, |
| 212 | AtomicOrdering Ord) const override; |
Simon Cook | aed9d6d | 2019-10-22 21:25:01 +0100 | [diff] [blame] | 213 | |
| 214 | /// Generate error diagnostics if any register used by CC has been marked |
| 215 | /// reserved. |
| 216 | void validateCCReservedRegs( |
| 217 | const SmallVectorImpl<std::pair<llvm::Register, llvm::SDValue>> &Regs, |
| 218 | MachineFunction &MF) const; |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 219 | }; |
| 220 | } |
| 221 | |
| 222 | #endif |