blob: fa18f7c8e401b6cefabe8b37cee30ebfef2dc85c [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,
44 REMUW
Alex Bradbury89718422017-10-19 21:37:38 +000045};
46}
47
48class RISCVTargetLowering : public TargetLowering {
49 const RISCVSubtarget &Subtarget;
50
51public:
52 explicit RISCVTargetLowering(const TargetMachine &TM,
53 const RISCVSubtarget &STI);
54
Alex Bradbury21aea512018-09-19 10:54:22 +000055 bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I,
56 MachineFunction &MF,
57 unsigned Intrinsic) const override;
Alex Bradbury09926292018-04-26 12:13:48 +000058 bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty,
59 unsigned AS,
60 Instruction *I = nullptr) const override;
Alex Bradburydcbff632018-04-26 13:15:17 +000061 bool isLegalICmpImmediate(int64_t Imm) const override;
Alex Bradbury5c41ece2018-04-26 13:00:37 +000062 bool isLegalAddImmediate(int64_t Imm) const override;
Alex Bradbury130b8b32018-04-26 13:37:00 +000063 bool isTruncateFree(Type *SrcTy, Type *DstTy) const override;
64 bool isTruncateFree(EVT SrcVT, EVT DstVT) const override;
Alex Bradbury15e894b2018-04-26 14:04:18 +000065 bool isZExtFree(SDValue Val, EVT VT2) const override;
Alex Bradburye0e62e92018-11-30 09:56:54 +000066 bool isSExtCheaperThanZExt(EVT SrcVT, EVT DstVT) const override;
Alex Bradbury09926292018-04-26 12:13:48 +000067
Alex Bradbury89718422017-10-19 21:37:38 +000068 // Provide custom lowering hooks for some operations.
69 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
Alex Bradbury299d6902019-01-25 05:04:00 +000070 void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
71 SelectionDAG &DAG) const override;
Alex Bradbury89718422017-10-19 21:37:38 +000072
Alex Bradbury5ac0a2f2018-10-03 23:30:16 +000073 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
74
Alex Bradbury299d6902019-01-25 05:04:00 +000075 unsigned ComputeNumSignBitsForTargetNode(SDValue Op,
76 const APInt &DemandedElts,
77 const SelectionDAG &DAG,
78 unsigned Depth) const override;
79
Alex Bradbury89718422017-10-19 21:37:38 +000080 // This method returns the name of a target specific DAG node.
81 const char *getTargetNodeName(unsigned Opcode) const override;
82
Alex Bradbury9330e642018-01-10 20:05:09 +000083 std::pair<unsigned, const TargetRegisterClass *>
84 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
85 StringRef Constraint, MVT VT) const override;
86
Alex Bradbury65385162017-11-21 07:51:32 +000087 MachineBasicBlock *
88 EmitInstrWithCustomInserter(MachineInstr &MI,
89 MachineBasicBlock *BB) const override;
90
Shiva Chenbbf4c5c2018-02-02 02:43:18 +000091 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
92 EVT VT) const override;
93
Alex Bradbury96f492d2018-06-13 12:04:51 +000094 bool shouldInsertFencesForAtomic(const Instruction *I) const override {
95 return isa<LoadInst>(I) || isa<StoreInst>(I);
96 }
97 Instruction *emitLeadingFence(IRBuilder<> &Builder, Instruction *Inst,
98 AtomicOrdering Ord) const override;
99 Instruction *emitTrailingFence(IRBuilder<> &Builder, Instruction *Inst,
100 AtomicOrdering Ord) const override;
101
Alex Bradbury89718422017-10-19 21:37:38 +0000102private:
Alex Bradburydc31c612017-12-11 12:49:02 +0000103 void analyzeInputArgs(MachineFunction &MF, CCState &CCInfo,
104 const SmallVectorImpl<ISD::InputArg> &Ins,
105 bool IsRet) const;
106 void analyzeOutputArgs(MachineFunction &MF, CCState &CCInfo,
107 const SmallVectorImpl<ISD::OutputArg> &Outs,
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000108 bool IsRet, CallLoweringInfo *CLI) const;
Alex Bradbury89718422017-10-19 21:37:38 +0000109 // Lower incoming arguments, copy physregs into vregs
110 SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
111 bool IsVarArg,
112 const SmallVectorImpl<ISD::InputArg> &Ins,
113 const SDLoc &DL, SelectionDAG &DAG,
114 SmallVectorImpl<SDValue> &InVals) const override;
Alex Bradburydc31c612017-12-11 12:49:02 +0000115 bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
116 bool IsVarArg,
117 const SmallVectorImpl<ISD::OutputArg> &Outs,
118 LLVMContext &Context) const override;
Alex Bradbury89718422017-10-19 21:37:38 +0000119 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
120 const SmallVectorImpl<ISD::OutputArg> &Outs,
121 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
122 SelectionDAG &DAG) const override;
Alex Bradburya3376752017-11-08 13:41:21 +0000123 SDValue LowerCall(TargetLowering::CallLoweringInfo &CLI,
124 SmallVectorImpl<SDValue> &InVals) const override;
Alex Bradbury89718422017-10-19 21:37:38 +0000125 bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
126 Type *Ty) const override {
127 return true;
128 }
Alex Bradburyec8aa912017-11-08 13:24:21 +0000129 SDValue lowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
Alex Bradburyffc435e2017-11-21 08:11:03 +0000130 SDValue lowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
Alex Bradbury80c8eb72018-03-20 13:26:12 +0000131 SDValue lowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
Alex Bradbury65385162017-11-21 07:51:32 +0000132 SDValue lowerSELECT(SDValue Op, SelectionDAG &DAG) const;
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000133 SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const;
Alex Bradbury0e167662018-10-04 05:27:50 +0000134 SDValue lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
135 SDValue lowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +0000136
137 bool IsEligibleForTailCallOptimization(CCState &CCInfo,
138 CallLoweringInfo &CLI, MachineFunction &MF,
139 const SmallVector<CCValAssign, 16> &ArgLocs) const;
Alex Bradbury21aea512018-09-19 10:54:22 +0000140
141 TargetLowering::AtomicExpansionKind
142 shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const override;
143 virtual Value *emitMaskedAtomicRMWIntrinsic(
144 IRBuilder<> &Builder, AtomicRMWInst *AI, Value *AlignedAddr, Value *Incr,
145 Value *Mask, Value *ShiftAmt, AtomicOrdering Ord) const override;
Alex Bradbury66d9a752018-11-29 20:43:42 +0000146 TargetLowering::AtomicExpansionKind
147 shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *CI) const override;
148 virtual Value *
149 emitMaskedAtomicCmpXchgIntrinsic(IRBuilder<> &Builder, AtomicCmpXchgInst *CI,
150 Value *AlignedAddr, Value *CmpVal,
151 Value *NewVal, Value *Mask,
152 AtomicOrdering Ord) const override;
Alex Bradbury89718422017-10-19 21:37:38 +0000153};
154}
155
156#endif