1 //=- WebAssemblyISelLowering.cpp - WebAssembly DAG Lowering Implementation -==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements the WebAssemblyTargetLowering class.
13 //===----------------------------------------------------------------------===//
15 #include "WebAssemblyISelLowering.h"
16 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17 #include "WebAssemblyMachineFunctionInfo.h"
18 #include "WebAssemblySubtarget.h"
19 #include "WebAssemblyTargetMachine.h"
20 #include "WebAssemblyTargetObjectFile.h"
21 #include "llvm/CodeGen/Analysis.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/IR/DiagnosticInfo.h"
25 #include "llvm/IR/DiagnosticPrinter.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/Intrinsics.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Target/TargetOptions.h"
36 #define DEBUG_TYPE "wasm-lower"
39 // Diagnostic information for unimplemented or unsupported feature reporting.
40 // FIXME copied from BPF and AMDGPU.
41 class DiagnosticInfoUnsupported : public DiagnosticInfo {
43 // Debug location where this diagnostic is triggered.
45 const Twine &Description;
51 static int getKindID() {
53 KindID = llvm::getNextAvailablePluginDiagnosticKind();
58 DiagnosticInfoUnsupported(SDLoc DLoc, const Function &Fn, const Twine &Desc,
60 : DiagnosticInfo(getKindID(), DS_Error), DLoc(DLoc.getDebugLoc()),
61 Description(Desc), Fn(Fn), Value(Value) {}
63 void print(DiagnosticPrinter &DP) const override {
65 raw_string_ostream OS(Str);
68 auto DIL = DLoc.get();
69 StringRef Filename = DIL->getFilename();
70 unsigned Line = DIL->getLine();
71 unsigned Column = DIL->getColumn();
72 OS << Filename << ':' << Line << ':' << Column << ' ';
75 OS << "in function " << Fn.getName() << ' ' << *Fn.getFunctionType() << '\n'
84 static bool classof(const DiagnosticInfo *DI) {
85 return DI->getKind() == getKindID();
89 int DiagnosticInfoUnsupported::KindID = 0;
90 } // end anonymous namespace
92 WebAssemblyTargetLowering::WebAssemblyTargetLowering(
93 const TargetMachine &TM, const WebAssemblySubtarget &STI)
94 : TargetLowering(TM), Subtarget(&STI) {
95 // WebAssembly does not produce floating-point exceptions on normal floating
97 setHasFloatingPointExceptions(false);
98 // We don't know the microarchitecture here, so just reduce register pressure.
99 setSchedulingPreference(Sched::RegPressure);
100 // Tell ISel that we have a stack pointer.
101 setStackPointerRegisterToSaveRestore(
102 Subtarget->hasAddr64() ? WebAssembly::SP64 : WebAssembly::SP32);
103 // Set up the register classes.
104 addRegisterClass(MVT::i32, &WebAssembly::Int32RegClass);
105 addRegisterClass(MVT::i64, &WebAssembly::Int64RegClass);
106 addRegisterClass(MVT::f32, &WebAssembly::Float32RegClass);
107 addRegisterClass(MVT::f64, &WebAssembly::Float64RegClass);
108 // Compute derived properties from the register classes.
109 computeRegisterProperties(Subtarget->getRegisterInfo());
111 // FIXME: setOperationAction...
114 //===----------------------------------------------------------------------===//
115 // WebAssembly Lowering private implementation.
116 //===----------------------------------------------------------------------===//
118 //===----------------------------------------------------------------------===//
120 //===----------------------------------------------------------------------===//
122 static void fail(SDLoc DL, SelectionDAG &DAG, const char *msg) {
123 MachineFunction &MF = DAG.getMachineFunction();
124 DAG.getContext()->diagnose(
125 DiagnosticInfoUnsupported(DL, *MF.getFunction(), msg, SDValue()));
128 bool WebAssemblyTargetLowering::CanLowerReturn(
129 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
130 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
131 // WebAssembly can't currently handle returning tuples.
132 return Outs.size() <= 1;
135 SDValue WebAssemblyTargetLowering::LowerReturn(
136 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
137 const SmallVectorImpl<ISD::OutputArg> &Outs,
138 const SmallVectorImpl<SDValue> &OutVals, SDLoc DL,
139 SelectionDAG &DAG) const {
141 assert(Outs.size() <= 1 && "WebAssembly can only return up to one value");
142 if (CallConv != CallingConv::C)
143 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
145 fail(DL, DAG, "WebAssembly doesn't support varargs yet");
147 SmallVector<SDValue, 4> RetOps(1, Chain);
148 RetOps.append(OutVals.begin(), OutVals.end());
149 Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps);
154 SDValue WebAssemblyTargetLowering::LowerFormalArguments(
155 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
156 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
157 SmallVectorImpl<SDValue> &InVals) const {
158 MachineFunction &MF = DAG.getMachineFunction();
160 if (CallConv != CallingConv::C)
161 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
163 fail(DL, DAG, "WebAssembly doesn't support varargs yet");
164 if (MF.getFunction()->hasStructRetAttr())
165 fail(DL, DAG, "WebAssembly doesn't support struct return yet");
168 for (const ISD::InputArg &In : Ins) {
169 if (In.Flags.isZExt())
170 fail(DL, DAG, "WebAssembly hasn't implemented zext arguments");
171 if (In.Flags.isSExt())
172 fail(DL, DAG, "WebAssembly hasn't implemented sext arguments");
173 if (In.Flags.isInReg())
174 fail(DL, DAG, "WebAssembly hasn't implemented inreg arguments");
175 if (In.Flags.isSRet())
176 fail(DL, DAG, "WebAssembly hasn't implemented sret arguments");
177 if (In.Flags.isByVal())
178 fail(DL, DAG, "WebAssembly hasn't implemented byval arguments");
179 if (In.Flags.isInAlloca())
180 fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments");
181 if (In.Flags.isNest())
182 fail(DL, DAG, "WebAssembly hasn't implemented nest arguments");
183 if (In.Flags.isReturned())
184 fail(DL, DAG, "WebAssembly hasn't implemented returned arguments");
185 if (In.Flags.isInConsecutiveRegs())
186 fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments");
187 if (In.Flags.isInConsecutiveRegsLast())
188 fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments");
189 if (In.Flags.isSplit())
190 fail(DL, DAG, "WebAssembly hasn't implemented split arguments");
191 // FIXME Do something with In.getOrigAlign()?
194 ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT,
195 DAG.getTargetConstant(ArgNo, DL, MVT::i32))
196 : DAG.getNode(ISD::UNDEF, DL, In.VT));
203 //===----------------------------------------------------------------------===//
204 // Other Lowering Code
205 //===----------------------------------------------------------------------===//
207 //===----------------------------------------------------------------------===//
208 // WebAssembly Optimization Hooks
209 //===----------------------------------------------------------------------===//
211 MCSection *WebAssemblyTargetObjectFile::SelectSectionForGlobal(
212 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
213 const TargetMachine &TM) const {
214 return getDataSection();