Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //=- 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 Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DiagnosticInfo.h" |
| 25 | #include "llvm/IR/DiagnosticPrinter.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 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" |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 33 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | #define DEBUG_TYPE "wasm-lower" |
| 37 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | // Diagnostic information for unimplemented or unsupported feature reporting. |
| 40 | // FIXME copied from BPF and AMDGPU. |
| 41 | class DiagnosticInfoUnsupported : public DiagnosticInfo { |
| 42 | private: |
| 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 | |
| 57 | public: |
| 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 | |
| 89 | int DiagnosticInfoUnsupported::KindID = 0; |
| 90 | } // end anonymous namespace |
| 91 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 92 | WebAssemblyTargetLowering::WebAssemblyTargetLowering( |
| 93 | const TargetMachine &TM, const WebAssemblySubtarget &STI) |
Dan Gohman | bfaf7e1 | 2015-07-02 21:36:25 +0000 | [diff] [blame] | 94 | : TargetLowering(TM), Subtarget(&STI) { |
| 95 | // WebAssembly does not produce floating-point exceptions on normal floating |
| 96 | // point operations. |
| 97 | setHasFloatingPointExceptions(false); |
Dan Gohman | 489abd7 | 2015-07-07 22:38:06 +0000 | [diff] [blame] | 98 | // We don't know the microarchitecture here, so just reduce register pressure. |
| 99 | setSchedulingPreference(Sched::RegPressure); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 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()); |
| 110 | |
| 111 | // FIXME: setOperationAction... |
Dan Gohman | bfaf7e1 | 2015-07-02 21:36:25 +0000 | [diff] [blame] | 112 | } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 113 | |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | // WebAssembly Lowering private implementation. |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | // Lowering Code |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 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())); |
| 126 | } |
| 127 | |
| 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; |
| 133 | } |
| 134 | |
| 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 { |
| 140 | |
| 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"); |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 144 | if (IsVarArg) |
| 145 | fail(DL, DAG, "WebAssembly doesn't support varargs yet"); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 146 | |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 147 | SmallVector<SDValue, 4> RetOps(1, Chain); |
| 148 | RetOps.append(OutVals.begin(), OutVals.end()); |
| 149 | const SDValue Ops[] = {Chain, OutVals.front()}; |
| 150 | Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, Ops); |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 151 | |
| 152 | return Chain; |
| 153 | } |
| 154 | |
| 155 | SDValue WebAssemblyTargetLowering::LowerFormalArguments( |
| 156 | SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, |
| 157 | const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG, |
| 158 | SmallVectorImpl<SDValue> &InVals) const { |
| 159 | MachineFunction &MF = DAG.getMachineFunction(); |
| 160 | |
| 161 | if (CallConv != CallingConv::C) |
| 162 | fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); |
| 163 | if (IsVarArg) |
| 164 | fail(DL, DAG, "WebAssembly doesn't support varargs yet"); |
| 165 | if (MF.getFunction()->hasStructRetAttr()) |
| 166 | fail(DL, DAG, "WebAssembly doesn't support struct return yet"); |
| 167 | |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 168 | unsigned ArgNo = 0; |
| 169 | for (const ISD::InputArg &In : Ins) { |
| 170 | if (In.Flags.isZExt()) |
| 171 | fail(DL, DAG, "WebAssembly hasn't implemented zext arguments"); |
| 172 | if (In.Flags.isSExt()) |
| 173 | fail(DL, DAG, "WebAssembly hasn't implemented sext arguments"); |
| 174 | if (In.Flags.isInReg()) |
| 175 | fail(DL, DAG, "WebAssembly hasn't implemented inreg arguments"); |
| 176 | if (In.Flags.isSRet()) |
| 177 | fail(DL, DAG, "WebAssembly hasn't implemented sret arguments"); |
| 178 | if (In.Flags.isByVal()) |
| 179 | fail(DL, DAG, "WebAssembly hasn't implemented byval arguments"); |
| 180 | if (In.Flags.isInAlloca()) |
| 181 | fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); |
| 182 | if (In.Flags.isNest()) |
| 183 | fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); |
| 184 | if (In.Flags.isReturned()) |
| 185 | fail(DL, DAG, "WebAssembly hasn't implemented returned arguments"); |
| 186 | if (In.Flags.isInConsecutiveRegs()) |
| 187 | fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); |
| 188 | if (In.Flags.isInConsecutiveRegsLast()) |
| 189 | fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); |
| 190 | if (In.Flags.isSplit()) |
| 191 | fail(DL, DAG, "WebAssembly hasn't implemented split arguments"); |
| 192 | if (In.VT != MVT::i32) |
| 193 | fail(DL, DAG, "WebAssembly hasn't implemented non-i32 arguments"); |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 194 | // FIXME Do something with In.getOrigAlign()? |
JF Bastien | d7fcc6f | 2015-07-31 18:13:27 +0000 | [diff] [blame^] | 195 | InVals.push_back( |
| 196 | In.Used |
| 197 | ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT, |
| 198 | DAG.getTargetConstant(ArgNo, DL, MVT::i32)) |
| 199 | : DAG.getNode(ISD::UNDEF, DL, In.VT)); |
| 200 | ++ArgNo; |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 201 | } |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 202 | |
| 203 | return Chain; |
| 204 | } |
| 205 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
| 207 | // Other Lowering Code |
| 208 | //===----------------------------------------------------------------------===// |
| 209 | |
| 210 | //===----------------------------------------------------------------------===// |
| 211 | // WebAssembly Optimization Hooks |
| 212 | //===----------------------------------------------------------------------===// |
| 213 | |
| 214 | MCSection *WebAssemblyTargetObjectFile::SelectSectionForGlobal( |
| 215 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 216 | const TargetMachine &TM) const { |
| 217 | return getDataSection(); |
| 218 | } |