blob: bfa442b24a769905eff321df92ddfa5841c039d5 [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) {
95 // WebAssembly does not produce floating-point exceptions on normal floating
96 // point operations.
97 setHasFloatingPointExceptions(false);
Dan Gohman489abd72015-07-07 22:38:06 +000098 // We don't know the microarchitecture here, so just reduce register pressure.
99 setSchedulingPreference(Sched::RegPressure);
JF Bastienb9073fb2015-07-22 21:28:15 +0000100 // 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());
110
JF Bastien4a642252015-08-10 22:36:48 +0000111 // FIXME: many setOperationAction are missing...
112
JF Bastienda06bce2015-08-11 21:02:46 +0000113 for (auto T : {MVT::f32, MVT::f64}) {
114 // Don't expand the floating-point types to constant pools.
115 setOperationAction(ISD::ConstantFP, T, Legal);
116 // Expand floating-point comparisons.
117 for (auto CC : {ISD::SETO, ISD::SETUO, ISD::SETUEQ, ISD::SETONE,
118 ISD::SETULT, ISD::SETULE, ISD::SETUGT, ISD::SETUGE})
119 setCondCodeAction(CC, T, Expand);
120 }
Dan Gohmanbfaf7e12015-07-02 21:36:25 +0000121}
Dan Gohman10e730a2015-06-29 23:51:55 +0000122
JF Bastienfda53372015-08-03 00:00:11 +0000123MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout &DL,
124 EVT VT) const {
125 return VT.getSimpleVT();
126}
127
JF Bastien480c8402015-08-11 20:13:18 +0000128const char *
129WebAssemblyTargetLowering::getTargetNodeName(unsigned Opcode) const {
130 switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) {
131 case WebAssemblyISD::FIRST_NUMBER: break;
132 case WebAssemblyISD::RETURN: return "WebAssemblyISD::RETURN";
133 case WebAssemblyISD::ARGUMENT: return "WebAssemblyISD::ARGUMENT";
134 }
135 return nullptr;
136}
137
Dan Gohman10e730a2015-06-29 23:51:55 +0000138//===----------------------------------------------------------------------===//
139// WebAssembly Lowering private implementation.
140//===----------------------------------------------------------------------===//
141
142//===----------------------------------------------------------------------===//
143// Lowering Code
144//===----------------------------------------------------------------------===//
145
JF Bastienb9073fb2015-07-22 21:28:15 +0000146static void fail(SDLoc DL, SelectionDAG &DAG, const char *msg) {
147 MachineFunction &MF = DAG.getMachineFunction();
148 DAG.getContext()->diagnose(
149 DiagnosticInfoUnsupported(DL, *MF.getFunction(), msg, SDValue()));
150}
151
152bool WebAssemblyTargetLowering::CanLowerReturn(
153 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
154 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
155 // WebAssembly can't currently handle returning tuples.
156 return Outs.size() <= 1;
157}
158
159SDValue WebAssemblyTargetLowering::LowerReturn(
160 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
161 const SmallVectorImpl<ISD::OutputArg> &Outs,
162 const SmallVectorImpl<SDValue> &OutVals, SDLoc DL,
163 SelectionDAG &DAG) const {
164
165 assert(Outs.size() <= 1 && "WebAssembly can only return up to one value");
166 if (CallConv != CallingConv::C)
167 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
JF Bastien600aee92015-07-31 17:53:38 +0000168 if (IsVarArg)
169 fail(DL, DAG, "WebAssembly doesn't support varargs yet");
JF Bastienb9073fb2015-07-22 21:28:15 +0000170
JF Bastien600aee92015-07-31 17:53:38 +0000171 SmallVector<SDValue, 4> RetOps(1, Chain);
172 RetOps.append(OutVals.begin(), OutVals.end());
JF Bastien4a2d5602015-07-31 21:04:18 +0000173 Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps);
JF Bastienb9073fb2015-07-22 21:28:15 +0000174
175 return Chain;
176}
177
178SDValue WebAssemblyTargetLowering::LowerFormalArguments(
179 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
180 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
181 SmallVectorImpl<SDValue> &InVals) const {
182 MachineFunction &MF = DAG.getMachineFunction();
183
184 if (CallConv != CallingConv::C)
185 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions");
186 if (IsVarArg)
187 fail(DL, DAG, "WebAssembly doesn't support varargs yet");
188 if (MF.getFunction()->hasStructRetAttr())
189 fail(DL, DAG, "WebAssembly doesn't support struct return yet");
190
JF Bastien600aee92015-07-31 17:53:38 +0000191 unsigned ArgNo = 0;
192 for (const ISD::InputArg &In : Ins) {
193 if (In.Flags.isZExt())
194 fail(DL, DAG, "WebAssembly hasn't implemented zext arguments");
195 if (In.Flags.isSExt())
196 fail(DL, DAG, "WebAssembly hasn't implemented sext arguments");
197 if (In.Flags.isInReg())
198 fail(DL, DAG, "WebAssembly hasn't implemented inreg arguments");
199 if (In.Flags.isSRet())
200 fail(DL, DAG, "WebAssembly hasn't implemented sret arguments");
201 if (In.Flags.isByVal())
202 fail(DL, DAG, "WebAssembly hasn't implemented byval arguments");
203 if (In.Flags.isInAlloca())
204 fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments");
205 if (In.Flags.isNest())
206 fail(DL, DAG, "WebAssembly hasn't implemented nest arguments");
207 if (In.Flags.isReturned())
208 fail(DL, DAG, "WebAssembly hasn't implemented returned arguments");
209 if (In.Flags.isInConsecutiveRegs())
210 fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments");
211 if (In.Flags.isInConsecutiveRegsLast())
212 fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments");
213 if (In.Flags.isSplit())
214 fail(DL, DAG, "WebAssembly hasn't implemented split arguments");
JF Bastien600aee92015-07-31 17:53:38 +0000215 // FIXME Do something with In.getOrigAlign()?
JF Bastiend7fcc6f2015-07-31 18:13:27 +0000216 InVals.push_back(
217 In.Used
218 ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT,
219 DAG.getTargetConstant(ArgNo, DL, MVT::i32))
220 : DAG.getNode(ISD::UNDEF, DL, In.VT));
221 ++ArgNo;
JF Bastien600aee92015-07-31 17:53:38 +0000222 }
JF Bastienb9073fb2015-07-22 21:28:15 +0000223
224 return Chain;
225}
226
Dan Gohman10e730a2015-06-29 23:51:55 +0000227//===----------------------------------------------------------------------===//
228// Other Lowering Code
229//===----------------------------------------------------------------------===//
230
231//===----------------------------------------------------------------------===//
232// WebAssembly Optimization Hooks
233//===----------------------------------------------------------------------===//
234
235MCSection *WebAssemblyTargetObjectFile::SelectSectionForGlobal(
236 const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
237 const TargetMachine &TM) const {
238 return getDataSection();
239}