1 //- WebAssemblyISelLowering.h - WebAssembly DAG Lowering Interface -*- 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 /// \file
10 /// This file defines the interfaces that WebAssembly uses to lower LLVM
11 /// code into a selection DAG.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYISELLOWERING_H
16 #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYISELLOWERING_H
17 
18 #include "llvm/CodeGen/TargetLowering.h"
19 
20 namespace llvm {
21 
22 namespace WebAssemblyISD {
23 
24 enum NodeType : unsigned {
25   FIRST_NUMBER = ISD::BUILTIN_OP_END,
26 #define HANDLE_NODETYPE(NODE) NODE,
27 #define HANDLE_MEM_NODETYPE(NODE)
28 #include "WebAssemblyISD.def"
29   FIRST_MEM_OPCODE = ISD::FIRST_TARGET_MEMORY_OPCODE,
30 #undef HANDLE_NODETYPE
31 #undef HANDLE_MEM_NODETYPE
32 #define HANDLE_NODETYPE(NODE)
33 #define HANDLE_MEM_NODETYPE(NODE) NODE,
34 #include "WebAssemblyISD.def"
35 #undef HANDLE_NODETYPE
36 #undef HANDLE_MEM_NODETYPE
37 };
38 
39 } // end namespace WebAssemblyISD
40 
41 class WebAssemblySubtarget;
42 
43 class WebAssemblyTargetLowering final : public TargetLowering {
44 public:
45   WebAssemblyTargetLowering(const TargetMachine &TM,
46                             const WebAssemblySubtarget &STI);
47 
48   enum WasmAddressSpace : unsigned {
49     // WebAssembly uses the following address spaces:
50     // AS 0 : is the default address space for values in linear memory
51     DEFAULT = 0,
52     // AS 1 : is a non-integral address space for global variables
53     GLOBAL = 1,
54     // AS 10 : is a non-integral address space for externref values
55     EXTERNREF = 10,
56     // AS 20 : is a non-integral address space for funcref values
57     FUNCREF = 20,
58   };
59 
60   MVT getPointerTy(const DataLayout &DL, uint32_t AS = 0) const override {
61     if (AS == WasmAddressSpace::EXTERNREF)
62       return MVT::externref;
63     if (AS == WasmAddressSpace::FUNCREF)
64       return MVT::funcref;
65     return TargetLowering::getPointerTy(DL, AS);
66   }
67   MVT getPointerMemTy(const DataLayout &DL, uint32_t AS = 0) const override {
68     if (AS == WasmAddressSpace::EXTERNREF)
69       return MVT::externref;
70     if (AS == WasmAddressSpace::FUNCREF)
71       return MVT::funcref;
72     return TargetLowering::getPointerMemTy(DL, AS);
73   }
74 
75   static bool isFuncrefType(const Type *Ty);
76   static bool isExternrefType(const Type *Ty);
77 
78 private:
79   /// Keep a pointer to the WebAssemblySubtarget around so that we can make the
80   /// right decision when generating code for different targets.
81   const WebAssemblySubtarget *Subtarget;
82 
83   AtomicExpansionKind shouldExpandAtomicRMWInIR(AtomicRMWInst *) const override;
84   bool shouldScalarizeBinop(SDValue VecOp) const override;
85   FastISel *createFastISel(FunctionLoweringInfo &FuncInfo,
86                            const TargetLibraryInfo *LibInfo) const override;
87   MVT getScalarShiftAmountTy(const DataLayout &DL, EVT) const override;
88   MachineBasicBlock *
89   EmitInstrWithCustomInserter(MachineInstr &MI,
90                               MachineBasicBlock *MBB) const override;
91   const char *getTargetNodeName(unsigned Opcode) const override;
92   std::pair<unsigned, const TargetRegisterClass *>
93   getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
94                                StringRef Constraint, MVT VT) const override;
95   bool isCheapToSpeculateCttz() const override;
96   bool isCheapToSpeculateCtlz() const override;
97   bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty,
98                              unsigned AS,
99                              Instruction *I = nullptr) const override;
100   bool allowsMisalignedMemoryAccesses(EVT, unsigned AddrSpace, Align Alignment,
101                                       MachineMemOperand::Flags Flags,
102                                       bool *Fast) const override;
103   bool isIntDivCheap(EVT VT, AttributeList Attr) const override;
104   bool isVectorLoadExtDesirable(SDValue ExtVal) const override;
105   EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
106                          EVT VT) const override;
107   bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I,
108                           MachineFunction &MF,
109                           unsigned Intrinsic) const override;
110 
111   SDValue LowerCall(CallLoweringInfo &CLI,
112                     SmallVectorImpl<SDValue> &InVals) const override;
113   bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
114                       bool isVarArg,
115                       const SmallVectorImpl<ISD::OutputArg> &Outs,
116                       LLVMContext &Context) const override;
117   SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
118                       const SmallVectorImpl<ISD::OutputArg> &Outs,
119                       const SmallVectorImpl<SDValue> &OutVals, const SDLoc &dl,
120                       SelectionDAG &DAG) const override;
121   SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
122                                bool IsVarArg,
123                                const SmallVectorImpl<ISD::InputArg> &Ins,
124                                const SDLoc &DL, SelectionDAG &DAG,
125                                SmallVectorImpl<SDValue> &InVals) const override;
126 
127   void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
128                           SelectionDAG &DAG) const override;
129 
130   const char *getClearCacheBuiltinName() const override {
131     report_fatal_error("llvm.clear_cache is not supported on wasm");
132   }
133 
134   // Custom lowering hooks.
135   SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
136   SDValue LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const;
137   SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
138   SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
139   SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
140   SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
141   SDValue LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const;
142   SDValue LowerBR_JT(SDValue Op, SelectionDAG &DAG) const;
143   SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
144   SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
145   SDValue LowerCopyToReg(SDValue Op, SelectionDAG &DAG) const;
146   SDValue LowerIntrinsic(SDValue Op, SelectionDAG &DAG) const;
147   SDValue LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const;
148   SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const;
149   SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const;
150   SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const;
151   SDValue LowerAccessVectorElement(SDValue Op, SelectionDAG &DAG) const;
152   SDValue LowerShift(SDValue Op, SelectionDAG &DAG) const;
153   SDValue LowerFP_TO_INT_SAT(SDValue Op, SelectionDAG &DAG) const;
154   SDValue LowerLoad(SDValue Op, SelectionDAG &DAG) const;
155   SDValue LowerStore(SDValue Op, SelectionDAG &DAG) const;
156 
157   // Custom DAG combine hooks
158   SDValue
159   PerformDAGCombine(SDNode *N,
160                     TargetLowering::DAGCombinerInfo &DCI) const override;
161 };
162 
163 namespace WebAssembly {
164 FastISel *createFastISel(FunctionLoweringInfo &funcInfo,
165                          const TargetLibraryInfo *libInfo);
166 } // end namespace WebAssembly
167 
168 } // end namespace llvm
169 
170 #endif
171