blob: f28c4753c1d90ca30e234c56424fe0f6b561ce5c [file] [log] [blame]
Alex Bradbury89718422017-10-19 21:37:38 +00001//===-- RISCVISelLowering.h - RISCV DAG Lowering Interface ------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Bradbury89718422017-10-19 21:37:38 +00006//
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 Blaikieb3bde2e2017-11-17 01:07:10 +000019#include "llvm/CodeGen/TargetLowering.h"
Alex Bradbury89718422017-10-19 21:37:38 +000020
21namespace llvm {
22class RISCVSubtarget;
23namespace RISCVISD {
24enum NodeType : unsigned {
25 FIRST_NUMBER = ISD::BUILTIN_OP_END,
Alex Bradburya3376752017-11-08 13:41:21 +000026 RET_FLAG,
Ana Pazos2e4106b2018-07-26 17:49:43 +000027 URET_FLAG,
28 SRET_FLAG,
29 MRET_FLAG,
Alex Bradbury65385162017-11-21 07:51:32 +000030 CALL,
Alex Bradbury0b4175f2018-04-12 05:34:25 +000031 SELECT_CC,
32 BuildPairF64,
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +000033 SplitF64,
Alex Bradbury299d6902019-01-25 05:04:00 +000034 TAIL,
35 // RV64I shifts, directly matching the semantics of the named RISC-V
36 // instructions.
37 SLLW,
38 SRAW,
Alex Bradbury456d3792019-01-25 05:11:34 +000039 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 Bradburyd834d832019-01-31 22:48:38 +000044 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 Elliottb2c9eed2019-07-05 12:35:21 +000051 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 Bradbury89718422017-10-19 21:37:38 +000055};
56}
57
58class RISCVTargetLowering : public TargetLowering {
59 const RISCVSubtarget &Subtarget;
60
61public:
62 explicit RISCVTargetLowering(const TargetMachine &TM,
63 const RISCVSubtarget &STI);
64
Alex Bradbury21aea512018-09-19 10:54:22 +000065 bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I,
66 MachineFunction &MF,
67 unsigned Intrinsic) const override;
Alex Bradbury09926292018-04-26 12:13:48 +000068 bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty,
69 unsigned AS,
70 Instruction *I = nullptr) const override;
Alex Bradburydcbff632018-04-26 13:15:17 +000071 bool isLegalICmpImmediate(int64_t Imm) const override;
Alex Bradbury5c41ece2018-04-26 13:00:37 +000072 bool isLegalAddImmediate(int64_t Imm) const override;
Alex Bradbury130b8b32018-04-26 13:37:00 +000073 bool isTruncateFree(Type *SrcTy, Type *DstTy) const override;
74 bool isTruncateFree(EVT SrcVT, EVT DstVT) const override;
Alex Bradbury15e894b2018-04-26 14:04:18 +000075 bool isZExtFree(SDValue Val, EVT VT2) const override;
Alex Bradburye0e62e92018-11-30 09:56:54 +000076 bool isSExtCheaperThanZExt(EVT SrcVT, EVT DstVT) const override;
Alex Bradbury09926292018-04-26 12:13:48 +000077
Sam Elliottf7206472019-06-07 12:20:14 +000078 bool hasBitPreservingFPLogic(EVT VT) const override;
79
Alex Bradbury89718422017-10-19 21:37:38 +000080 // Provide custom lowering hooks for some operations.
81 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
Alex Bradbury299d6902019-01-25 05:04:00 +000082 void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
83 SelectionDAG &DAG) const override;
Alex Bradbury89718422017-10-19 21:37:38 +000084
Alex Bradbury5ac0a2f2018-10-03 23:30:16 +000085 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
86
Alex Bradbury299d6902019-01-25 05:04:00 +000087 unsigned ComputeNumSignBitsForTargetNode(SDValue Op,
88 const APInt &DemandedElts,
89 const SelectionDAG &DAG,
90 unsigned Depth) const override;
91
Alex Bradbury89718422017-10-19 21:37:38 +000092 // This method returns the name of a target specific DAG node.
93 const char *getTargetNodeName(unsigned Opcode) const override;
94
Sam Elliott9e6b2e12019-07-31 09:45:55 +000095 ConstraintType getConstraintType(StringRef Constraint) const override;
Alex Bradbury9330e642018-01-10 20:05:09 +000096 std::pair<unsigned, const TargetRegisterClass *>
97 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
98 StringRef Constraint, MVT VT) const override;
99
Lewis Revill28a5cad2019-06-11 12:42:13 +0000100 void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
101 std::vector<SDValue> &Ops,
102 SelectionDAG &DAG) const override;
103
Alex Bradbury65385162017-11-21 07:51:32 +0000104 MachineBasicBlock *
105 EmitInstrWithCustomInserter(MachineInstr &MI,
106 MachineBasicBlock *BB) const override;
107
Shiva Chenbbf4c5c2018-02-02 02:43:18 +0000108 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
109 EVT VT) const override;
110
Alex Bradbury33691012019-03-22 10:39:22 +0000111 bool convertSetCCLogicToBitwiseLogic(EVT VT) const override {
112 return VT.isScalarInteger();
113 }
114
Alex Bradbury96f492d2018-06-13 12:04:51 +0000115 bool shouldInsertFencesForAtomic(const Instruction *I) const override {
116 return isa<LoadInst>(I) || isa<StoreInst>(I);
117 }
118 Instruction *emitLeadingFence(IRBuilder<> &Builder, Instruction *Inst,
119 AtomicOrdering Ord) const override;
120 Instruction *emitTrailingFence(IRBuilder<> &Builder, Instruction *Inst,
121 AtomicOrdering Ord) const override;
122
Alex Bradbury4d20cc22019-03-11 21:41:22 +0000123 ISD::NodeType getExtendForAtomicOps() const override {
124 return ISD::SIGN_EXTEND;
125 }
126
Luis Marques20d24242019-04-16 14:38:32 +0000127 bool shouldExpandShift(SelectionDAG &DAG, SDNode *N) const override {
128 if (DAG.getMachineFunction().getFunction().hasMinSize())
129 return false;
130 return true;
131 }
Sam Elliott9f155bc2019-06-18 20:38:08 +0000132 bool isDesirableToCommuteWithShift(const SDNode *N,
133 CombineLevel Level) const override;
Luis Marques20d24242019-04-16 14:38:32 +0000134
Alex Bradbury0b9addb2019-07-08 09:16:47 +0000135 /// If a physical register, this returns the register that receives the
136 /// exception address on entry to an EH pad.
137 unsigned
138 getExceptionPointerRegister(const Constant *PersonalityFn) const override;
139
140 /// If a physical register, this returns the register that receives the
141 /// exception typeid on entry to a landing pad.
142 unsigned
143 getExceptionSelectorRegister(const Constant *PersonalityFn) const override;
144
Alex Bradbury89718422017-10-19 21:37:38 +0000145private:
Alex Bradburydc31c612017-12-11 12:49:02 +0000146 void analyzeInputArgs(MachineFunction &MF, CCState &CCInfo,
147 const SmallVectorImpl<ISD::InputArg> &Ins,
148 bool IsRet) const;
149 void analyzeOutputArgs(MachineFunction &MF, CCState &CCInfo,
150 const SmallVectorImpl<ISD::OutputArg> &Outs,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000151 bool IsRet, CallLoweringInfo *CLI) const;
Alex Bradbury89718422017-10-19 21:37:38 +0000152 // Lower incoming arguments, copy physregs into vregs
153 SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
154 bool IsVarArg,
155 const SmallVectorImpl<ISD::InputArg> &Ins,
156 const SDLoc &DL, SelectionDAG &DAG,
157 SmallVectorImpl<SDValue> &InVals) const override;
Alex Bradburydc31c612017-12-11 12:49:02 +0000158 bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
159 bool IsVarArg,
160 const SmallVectorImpl<ISD::OutputArg> &Outs,
161 LLVMContext &Context) const override;
Alex Bradbury89718422017-10-19 21:37:38 +0000162 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
163 const SmallVectorImpl<ISD::OutputArg> &Outs,
164 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
165 SelectionDAG &DAG) const override;
Alex Bradburya3376752017-11-08 13:41:21 +0000166 SDValue LowerCall(TargetLowering::CallLoweringInfo &CLI,
167 SmallVectorImpl<SDValue> &InVals) const override;
Alex Bradbury89718422017-10-19 21:37:38 +0000168 bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
169 Type *Ty) const override {
170 return true;
171 }
Alex Bradburyda20f5c2019-04-01 14:42:56 +0000172
173 template <class NodeTy>
Lewis Revilla5240362019-06-11 12:57:47 +0000174 SDValue getAddr(NodeTy *N, SelectionDAG &DAG, bool IsLocal = true) const;
Alex Bradburyda20f5c2019-04-01 14:42:56 +0000175
Lewis Revill39263ac2019-06-19 08:40:59 +0000176 SDValue getStaticTLSAddr(GlobalAddressSDNode *N, SelectionDAG &DAG,
177 bool UseGOT) const;
178 SDValue getDynamicTLSAddr(GlobalAddressSDNode *N, SelectionDAG &DAG) const;
179
Luis Marques2e463122019-06-17 10:54:12 +0000180 bool shouldConsiderGEPOffsetSplit() const override { return true; }
Alex Bradburyec8aa912017-11-08 13:24:21 +0000181 SDValue lowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
Alex Bradburyffc435e2017-11-21 08:11:03 +0000182 SDValue lowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000183 SDValue lowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
Lewis Revill39263ac2019-06-19 08:40:59 +0000184 SDValue lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
Alex Bradbury65385162017-11-21 07:51:32 +0000185 SDValue lowerSELECT(SDValue Op, SelectionDAG &DAG) const;
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000186 SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const;
Alex Bradbury0e167662018-10-04 05:27:50 +0000187 SDValue lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
188 SDValue lowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
Luis Marques20d24242019-04-16 14:38:32 +0000189 SDValue lowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const;
190 SDValue lowerShiftRightParts(SDValue Op, SelectionDAG &DAG, bool IsSRA) const;
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +0000191
Alex Bradburydb67be82019-02-21 14:31:41 +0000192 bool isEligibleForTailCallOptimization(
193 CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
194 const SmallVector<CCValAssign, 16> &ArgLocs) const;
Alex Bradbury21aea512018-09-19 10:54:22 +0000195
196 TargetLowering::AtomicExpansionKind
197 shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const override;
198 virtual Value *emitMaskedAtomicRMWIntrinsic(
199 IRBuilder<> &Builder, AtomicRMWInst *AI, Value *AlignedAddr, Value *Incr,
200 Value *Mask, Value *ShiftAmt, AtomicOrdering Ord) const override;
Alex Bradbury66d9a752018-11-29 20:43:42 +0000201 TargetLowering::AtomicExpansionKind
202 shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *CI) const override;
203 virtual Value *
204 emitMaskedAtomicCmpXchgIntrinsic(IRBuilder<> &Builder, AtomicCmpXchgInst *CI,
205 Value *AlignedAddr, Value *CmpVal,
206 Value *NewVal, Value *Mask,
207 AtomicOrdering Ord) const override;
Alex Bradbury89718422017-10-19 21:37:38 +0000208};
209}
210
211#endif