blob: 4cb3a3138e8ed34b1fd5bdb81d46f014368a2b44 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//=- WebAssemblyISelLowering.cpp - WebAssembly DAG Lowering Implementation -==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements the WebAssemblyTargetLowering class.
12///
13//===----------------------------------------------------------------------===//
14
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"
JF Bastienb9073fb2015-07-22 21:28:15 +000024#include "llvm/IR/DiagnosticInfo.h"
25#include "llvm/IR/DiagnosticPrinter.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000026#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"
JF Bastienb9073fb2015-07-22 21:28:15 +000033
Dan Gohman10e730a2015-06-29 23:51:55 +000034using namespace llvm;
35
36#define DEBUG_TYPE "wasm-lower"
37
JF Bastienb9073fb2015-07-22 21:28:15 +000038namespace {
39// Diagnostic information for unimplemented or unsupported feature reporting.
40// FIXME copied from BPF and AMDGPU.
41class DiagnosticInfoUnsupported : public DiagnosticInfo {
42private:
43 // Debug location where this diagnostic is triggered.
44 DebugLoc DLoc;
45 const Twine &Description;
46 const Function &Fn;
47 SDValue Value;
48
49 static int KindID;
50
51 static int getKindID() {
52 if (KindID == 0)
53 KindID = llvm::getNextAvailablePluginDiagnosticKind();
54 return KindID;
55 }
56
57public:
58 DiagnosticInfoUnsupported(SDLoc DLoc, const Function &Fn, const Twine &Desc,
59 SDValue Value)
60 : DiagnosticInfo(getKindID(), DS_Error), DLoc(DLoc.getDebugLoc()),
61 Description(Desc), Fn(Fn), Value(Value) {}
62
63 void print(DiagnosticPrinter &DP) const override {
64 std::string Str;
65 raw_string_ostream OS(Str);
66
67 if (DLoc) {
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 << ' ';
73 }
74
75 OS << "in function " << Fn.getName() << ' ' << *Fn.getFunctionType() << '\n'
76 << Description;
77 if (Value)
78 Value->print(OS);
79 OS << '\n';
80 OS.flush();
81 DP << Str;
82 }
83
84 static bool classof(const DiagnosticInfo *DI) {
85 return DI->getKind() == getKindID();
86 }
87};
88
89int DiagnosticInfoUnsupported::KindID = 0;
90} // end anonymous namespace
91
Dan Gohman10e730a2015-06-29 23:51:55 +000092WebAssemblyTargetLowering::WebAssemblyTargetLowering(
93 const TargetMachine &TM, const WebAssemblySubtarget &STI)
Dan Gohmanbfaf7e12015-07-02 21:36:25 +000094 : TargetLowering(TM), Subtarget(&STI) {
JF Bastien71d29ac2015-08-12 17:53:29 +000095 // Booleans always contain 0 or 1.
96 setBooleanContents(ZeroOrOneBooleanContent);
Dan Gohmanbfaf7e12015-07-02 21:36:25 +000097 // WebAssembly does not produce floating-point exceptions on normal floating
98 // point operations.
99 setHasFloatingPointExceptions(false);
Dan Gohman489abd72015-07-07 22:38:06 +0000100 // We don't know the microarchitecture here, so just reduce register pressure.
101 setSchedulingPreference(Sched::RegPressure);
JF Bastienb9073fb2015-07-22 21:28:15 +0000102 // Tell ISel that we have a stack pointer.
103 setStackPointerRegisterToSaveRestore(
104 Subtarget->hasAddr64() ? WebAssembly::SP64 : WebAssembly::SP32);
105 // Set up the register classes.
106 addRegisterClass(MVT::i32, &WebAssembly::Int32RegClass);
107 addRegisterClass(MVT::i64, &WebAssembly::Int64RegClass);
108 addRegisterClass(MVT::f32, &WebAssembly::Float32RegClass);
109 addRegisterClass(MVT::f64, &WebAssembly::Float64RegClass);
110 // Compute derived properties from the register classes.
111 computeRegisterProperties(Subtarget->getRegisterInfo());
112
JF Bastien4a642252015-08-10 22:36:48 +0000113 // FIXME: many setOperationAction are missing...
114
JF Bastienda06bce2015-08-11 21:02:46 +0000115 for (auto T : {MVT::f32, MVT::f64}) {
116 // Don't expand the floating-point types to constant pools.
117 setOperationAction(ISD::ConstantFP, T, Legal);
118 // Expand floating-point comparisons.
119 for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE,
120 ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE})
121 setCondCodeAction(CC, T, Expand);
Dan Gohman32907a62015-08-20 22:57:13 +0000122 // Expand floating-point library function operators.
Dan Gohman896e53f2015-08-24 18:23:13 +0000123 for (auto Op : {ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOWI, ISD::FPOW})
Dan Gohman32907a62015-08-20 22:57:13 +0000124 setOperationAction(Op, T, Expand);
Dan Gohman896e53f2015-08-24 18:23:13 +0000125 // Note supported floating-point library function operators that otherwise
126 // default to expand.
127 for (auto Op : {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT,
128 ISD::FRINT})
129 setOperationAction(Op, T, Legal);
JF Bastienda06bce2015-08-11 21:02:46 +0000130 }
Dan Gohman32907a62015-08-20 22:57:13 +0000131
132 for (auto T : {MVT::i32, MVT::i64}) {
133 // Expand unavailable integer operations.
134 for (auto Op : {ISD::BSWAP, ISD::ROTL, ISD::ROTR,
135 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
136 ISD::MULHS, ISD::MULHU, ISD::SDIVREM, ISD::UDIVREM,
137 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
138 ISD::ADDC, ISD::ADDE, ISD::SUBC, ISD::SUBE}) {
139 setOperationAction(Op, T, Expand);
140 }
141 }
142
143 // As a special case, these operators use the type to mean the type to
144 // sign-extend from.
145 for (auto T : {MVT::i1, MVT::i8, MVT::i16})
146 setOperationAction(ISD::SIGN_EXTEND_INREG, T, Expand);
147
148 // Dynamic stack allocation: use the default expansion.
149 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
150 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
151 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Dan Gohmanbfaf7e12015-07-02 21:36:25 +0000152}
Dan Gohman10e730a2015-06-29 23:51:55 +0000153
JF Bastienfda53372015-08-03 00:00:11 +0000154MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout &DL,
155 EVT VT) const {
156 return VT.getSimpleVT();
157}
158
JF Bastien480c8402015-08-11 20:13:18 +0000159const char *
160WebAssemblyTargetLowering::getTargetNodeName(unsigned Opcode) const {
161 switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) {
162 case WebAssemblyISD::FIRST_NUMBER: break;
163 case WebAssemblyISD::RETURN: return "WebAssemblyISD::RETURN";
164 case WebAssemblyISD::ARGUMENT: return "WebAssemblyISD::ARGUMENT";
165 }
166 return nullptr;
167}
168
Dan Gohman10e730a2015-06-29 23:51:55 +0000169//===----------------------------------------------------------------------===//
170// WebAssembly Lowering private implementation.
171//===----------------------------------------------------------------------===//
172
173//===----------------------------------------------------------------------===//
174// Lowering Code
175//===----------------------------------------------------------------------===//
176
JF Bastienb9073fb2015-07-22 21:28:15 +0000177static void fail(SDLoc DL, SelectionDAG &DAG, const char *msg) {
178 MachineFunction &MF = DAG.getMachineFunction();
179 DAG.getContext()->diagnose(
180 DiagnosticInfoUnsupported(DL, *MF.getFunction(), msg, SDValue()));
181}
182
183bool WebAssemblyTargetLowering::CanLowerReturn(
184 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
185 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
186 // WebAssembly can't currently handle returning tuples.
187 return Outs.size() <= 1;
188}
189
190SDValue WebAssemblyTargetLowering::LowerReturn(
191 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
192 const SmallVectorImpl<ISD::OutputArg> &Outs,
193 const SmallVectorImpl<SDValue> &OutVals, SDLoc DL,
194 SelectionDAG &DAG) const {
195
196 assert(Outs.size() <= 1 && "WebAssembly can only return up to one value");
197 if (CallConv != CallingConv::C)
198 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
JF Bastien600aee92015-07-31 17:53:38 +0000199 if (IsVarArg)
200 fail(DL, DAG, "WebAssembly doesn't support varargs yet");
JF Bastienb9073fb2015-07-22 21:28:15 +0000201
JF Bastien600aee92015-07-31 17:53:38 +0000202 SmallVector<SDValue, 4> RetOps(1, Chain);
203 RetOps.append(OutVals.begin(), OutVals.end());
JF Bastien4a2d5602015-07-31 21:04:18 +0000204 Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps);
JF Bastienb9073fb2015-07-22 21:28:15 +0000205
206 return Chain;
207}
208
209SDValue WebAssemblyTargetLowering::LowerFormalArguments(
210 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
211 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
212 SmallVectorImpl<SDValue> &InVals) const {
213 MachineFunction &MF = DAG.getMachineFunction();
214
215 if (CallConv != CallingConv::C)
216 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
217 if (IsVarArg)
218 fail(DL, DAG, "WebAssembly doesn't support varargs yet");
219 if (MF.getFunction()->hasStructRetAttr())
220 fail(DL, DAG, "WebAssembly doesn't support struct return yet");
221
JF Bastien600aee92015-07-31 17:53:38 +0000222 unsigned ArgNo = 0;
223 for (const ISD::InputArg &In : Ins) {
224 if (In.Flags.isZExt())
225 fail(DL, DAG, "WebAssembly hasn't implemented zext arguments");
226 if (In.Flags.isSExt())
227 fail(DL, DAG, "WebAssembly hasn't implemented sext arguments");
228 if (In.Flags.isInReg())
229 fail(DL, DAG, "WebAssembly hasn't implemented inreg arguments");
230 if (In.Flags.isSRet())
231 fail(DL, DAG, "WebAssembly hasn't implemented sret arguments");
232 if (In.Flags.isByVal())
233 fail(DL, DAG, "WebAssembly hasn't implemented byval arguments");
234 if (In.Flags.isInAlloca())
235 fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments");
236 if (In.Flags.isNest())
237 fail(DL, DAG, "WebAssembly hasn't implemented nest arguments");
238 if (In.Flags.isReturned())
239 fail(DL, DAG, "WebAssembly hasn't implemented returned arguments");
240 if (In.Flags.isInConsecutiveRegs())
241 fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments");
242 if (In.Flags.isInConsecutiveRegsLast())
243 fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments");
244 if (In.Flags.isSplit())
245 fail(DL, DAG, "WebAssembly hasn't implemented split arguments");
JF Bastien600aee92015-07-31 17:53:38 +0000246 // FIXME Do something with In.getOrigAlign()?
JF Bastiend7fcc6f2015-07-31 18:13:27 +0000247 InVals.push_back(
248 In.Used
249 ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT,
250 DAG.getTargetConstant(ArgNo, DL, MVT::i32))
251 : DAG.getNode(ISD::UNDEF, DL, In.VT));
252 ++ArgNo;
JF Bastien600aee92015-07-31 17:53:38 +0000253 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000254
255 return Chain;
256}
257
Dan Gohman10e730a2015-06-29 23:51:55 +0000258//===----------------------------------------------------------------------===//
259// Other Lowering Code
260//===----------------------------------------------------------------------===//
261
262//===----------------------------------------------------------------------===//
263// WebAssembly Optimization Hooks
264//===----------------------------------------------------------------------===//
265
266MCSection *WebAssemblyTargetObjectFile::SelectSectionForGlobal(
267 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
268 const TargetMachine &TM) const {
269 return getDataSection();
270}