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" 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" 33 34 using namespace llvm; 35 36 #define DEBUG_TYPE "wasm-lower" 37 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 92 WebAssemblyTargetLowering::WebAssemblyTargetLowering( 93 const TargetMachine &TM, const WebAssemblySubtarget &STI) 94 : TargetLowering(TM), Subtarget(&STI) { 95 // Booleans always contain 0 or 1. 96 setBooleanContents(ZeroOrOneBooleanContent); 97 // WebAssembly does not produce floating-point exceptions on normal floating 98 // point operations. 99 setHasFloatingPointExceptions(false); 100 // We don't know the microarchitecture here, so just reduce register pressure. 101 setSchedulingPreference(Sched::RegPressure); 102 // 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 113 // FIXME: many setOperationAction are missing... 114 115 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); 122 } 123 } 124 125 MVT WebAssemblyTargetLowering::getScalarShiftAmountTy(const DataLayout &DL, 126 EVT VT) const { 127 return VT.getSimpleVT(); 128 } 129 130 const char * 131 WebAssemblyTargetLowering::getTargetNodeName(unsigned Opcode) const { 132 switch (static_cast<WebAssemblyISD::NodeType>(Opcode)) { 133 case WebAssemblyISD::FIRST_NUMBER: break; 134 case WebAssemblyISD::RETURN: return "WebAssemblyISD::RETURN"; 135 case WebAssemblyISD::ARGUMENT: return "WebAssemblyISD::ARGUMENT"; 136 } 137 return nullptr; 138 } 139 140 //===----------------------------------------------------------------------===// 141 // WebAssembly Lowering private implementation. 142 //===----------------------------------------------------------------------===// 143 144 //===----------------------------------------------------------------------===// 145 // Lowering Code 146 //===----------------------------------------------------------------------===// 147 148 static void fail(SDLoc DL, SelectionDAG &DAG, const char *msg) { 149 MachineFunction &MF = DAG.getMachineFunction(); 150 DAG.getContext()->diagnose( 151 DiagnosticInfoUnsupported(DL, *MF.getFunction(), msg, SDValue())); 152 } 153 154 bool WebAssemblyTargetLowering::CanLowerReturn( 155 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg, 156 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const { 157 // WebAssembly can't currently handle returning tuples. 158 return Outs.size() <= 1; 159 } 160 161 SDValue WebAssemblyTargetLowering::LowerReturn( 162 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 163 const SmallVectorImpl<ISD::OutputArg> &Outs, 164 const SmallVectorImpl<SDValue> &OutVals, SDLoc DL, 165 SelectionDAG &DAG) const { 166 167 assert(Outs.size() <= 1 && "WebAssembly can only return up to one value"); 168 if (CallConv != CallingConv::C) 169 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); 170 if (IsVarArg) 171 fail(DL, DAG, "WebAssembly doesn't support varargs yet"); 172 173 SmallVector<SDValue, 4> RetOps(1, Chain); 174 RetOps.append(OutVals.begin(), OutVals.end()); 175 Chain = DAG.getNode(WebAssemblyISD::RETURN, DL, MVT::Other, RetOps); 176 177 return Chain; 178 } 179 180 SDValue WebAssemblyTargetLowering::LowerFormalArguments( 181 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 182 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG, 183 SmallVectorImpl<SDValue> &InVals) const { 184 MachineFunction &MF = DAG.getMachineFunction(); 185 186 if (CallConv != CallingConv::C) 187 fail(DL, DAG, "WebAssembly doesn't support non-C calling conventions"); 188 if (IsVarArg) 189 fail(DL, DAG, "WebAssembly doesn't support varargs yet"); 190 if (MF.getFunction()->hasStructRetAttr()) 191 fail(DL, DAG, "WebAssembly doesn't support struct return yet"); 192 193 unsigned ArgNo = 0; 194 for (const ISD::InputArg &In : Ins) { 195 if (In.Flags.isZExt()) 196 fail(DL, DAG, "WebAssembly hasn't implemented zext arguments"); 197 if (In.Flags.isSExt()) 198 fail(DL, DAG, "WebAssembly hasn't implemented sext arguments"); 199 if (In.Flags.isInReg()) 200 fail(DL, DAG, "WebAssembly hasn't implemented inreg arguments"); 201 if (In.Flags.isSRet()) 202 fail(DL, DAG, "WebAssembly hasn't implemented sret arguments"); 203 if (In.Flags.isByVal()) 204 fail(DL, DAG, "WebAssembly hasn't implemented byval arguments"); 205 if (In.Flags.isInAlloca()) 206 fail(DL, DAG, "WebAssembly hasn't implemented inalloca arguments"); 207 if (In.Flags.isNest()) 208 fail(DL, DAG, "WebAssembly hasn't implemented nest arguments"); 209 if (In.Flags.isReturned()) 210 fail(DL, DAG, "WebAssembly hasn't implemented returned arguments"); 211 if (In.Flags.isInConsecutiveRegs()) 212 fail(DL, DAG, "WebAssembly hasn't implemented cons regs arguments"); 213 if (In.Flags.isInConsecutiveRegsLast()) 214 fail(DL, DAG, "WebAssembly hasn't implemented cons regs last arguments"); 215 if (In.Flags.isSplit()) 216 fail(DL, DAG, "WebAssembly hasn't implemented split arguments"); 217 // FIXME Do something with In.getOrigAlign()? 218 InVals.push_back( 219 In.Used 220 ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT, 221 DAG.getTargetConstant(ArgNo, DL, MVT::i32)) 222 : DAG.getNode(ISD::UNDEF, DL, In.VT)); 223 ++ArgNo; 224 } 225 226 return Chain; 227 } 228 229 //===----------------------------------------------------------------------===// 230 // Other Lowering Code 231 //===----------------------------------------------------------------------===// 232 233 //===----------------------------------------------------------------------===// 234 // WebAssembly Optimization Hooks 235 //===----------------------------------------------------------------------===// 236 237 MCSection *WebAssemblyTargetObjectFile::SelectSectionForGlobal( 238 const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 239 const TargetMachine &TM) const { 240 return getDataSection(); 241 } 242