1 // WebAssemblyMachineFunctionInfo.h-WebAssembly machine function info-*- C++ -*- 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file declares WebAssembly-specific per-machine-function 11 /// information. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H 16 #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H 17 18 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 19 #include "llvm/BinaryFormat/Wasm.h" 20 #include "llvm/CodeGen/MIRYamlMapping.h" 21 #include "llvm/CodeGen/MachineRegisterInfo.h" 22 #include "llvm/MC/MCSymbolWasm.h" 23 24 namespace llvm { 25 26 namespace yaml { 27 struct WebAssemblyFunctionInfo; 28 } 29 30 /// This class is derived from MachineFunctionInfo and contains private 31 /// WebAssembly-specific information for each MachineFunction. 32 class WebAssemblyFunctionInfo final : public MachineFunctionInfo { 33 MachineFunction &MF; 34 35 std::vector<MVT> Params; 36 std::vector<MVT> Results; 37 std::vector<MVT> Locals; 38 39 /// A mapping from CodeGen vreg index to WebAssembly register number. 40 std::vector<unsigned> WARegs; 41 42 /// A mapping from CodeGen vreg index to a boolean value indicating whether 43 /// the given register is considered to be "stackified", meaning it has been 44 /// determined or made to meet the stack requirements: 45 /// - single use (per path) 46 /// - single def (per path) 47 /// - defined and used in LIFO order with other stack registers 48 BitVector VRegStackified; 49 50 // A virtual register holding the pointer to the vararg buffer for vararg 51 // functions. It is created and set in TLI::LowerFormalArguments and read by 52 // TLI::LowerVASTART 53 unsigned VarargVreg = -1U; 54 55 // A virtual register holding the base pointer for functions that have 56 // overaligned values on the user stack. 57 unsigned BasePtrVreg = -1U; 58 59 public: 60 explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF) {} 61 ~WebAssemblyFunctionInfo() override; 62 void initializeBaseYamlFields(const yaml::WebAssemblyFunctionInfo &YamlMFI); 63 64 void addParam(MVT VT) { Params.push_back(VT); } 65 const std::vector<MVT> &getParams() const { return Params; } 66 67 void addResult(MVT VT) { Results.push_back(VT); } 68 const std::vector<MVT> &getResults() const { return Results; } 69 70 void clearParamsAndResults() { 71 Params.clear(); 72 Results.clear(); 73 } 74 75 void setNumLocals(size_t NumLocals) { Locals.resize(NumLocals, MVT::i32); } 76 void setLocal(size_t i, MVT VT) { Locals[i] = VT; } 77 void addLocal(MVT VT) { Locals.push_back(VT); } 78 const std::vector<MVT> &getLocals() const { return Locals; } 79 80 unsigned getVarargBufferVreg() const { 81 assert(VarargVreg != -1U && "Vararg vreg hasn't been set"); 82 return VarargVreg; 83 } 84 void setVarargBufferVreg(unsigned Reg) { VarargVreg = Reg; } 85 86 unsigned getBasePointerVreg() const { 87 assert(BasePtrVreg != -1U && "Base ptr vreg hasn't been set"); 88 return BasePtrVreg; 89 } 90 void setBasePointerVreg(unsigned Reg) { BasePtrVreg = Reg; } 91 92 static const unsigned UnusedReg = -1u; 93 94 void stackifyVReg(unsigned VReg) { 95 assert(MF.getRegInfo().getUniqueVRegDef(VReg)); 96 auto I = TargetRegisterInfo::virtReg2Index(VReg); 97 if (I >= VRegStackified.size()) 98 VRegStackified.resize(I + 1); 99 VRegStackified.set(I); 100 } 101 bool isVRegStackified(unsigned VReg) const { 102 auto I = TargetRegisterInfo::virtReg2Index(VReg); 103 if (I >= VRegStackified.size()) 104 return false; 105 return VRegStackified.test(I); 106 } 107 108 void initWARegs(); 109 void setWAReg(unsigned VReg, unsigned WAReg) { 110 assert(WAReg != UnusedReg); 111 auto I = TargetRegisterInfo::virtReg2Index(VReg); 112 assert(I < WARegs.size()); 113 WARegs[I] = WAReg; 114 } 115 unsigned getWAReg(unsigned VReg) const { 116 auto I = TargetRegisterInfo::virtReg2Index(VReg); 117 assert(I < WARegs.size()); 118 return WARegs[I]; 119 } 120 121 // For a given stackified WAReg, return the id number to print with push/pop. 122 static unsigned getWARegStackId(unsigned Reg) { 123 assert(Reg & INT32_MIN); 124 return Reg & INT32_MAX; 125 } 126 }; 127 128 void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty, 129 SmallVectorImpl<MVT> &ValueVTs); 130 131 // Compute the signature for a given FunctionType (Ty). Note that it's not the 132 // signature for F (F is just used to get varous context) 133 void computeSignatureVTs(const FunctionType *Ty, const Function &F, 134 const TargetMachine &TM, SmallVectorImpl<MVT> &Params, 135 SmallVectorImpl<MVT> &Results); 136 137 void valTypesFromMVTs(const ArrayRef<MVT> &In, 138 SmallVectorImpl<wasm::ValType> &Out); 139 140 std::unique_ptr<wasm::WasmSignature> 141 signatureFromMVTs(const SmallVectorImpl<MVT> &Results, 142 const SmallVectorImpl<MVT> &Params); 143 144 namespace yaml { 145 146 struct WebAssemblyFunctionInfo final : public yaml::MachineFunctionInfo { 147 WebAssemblyFunctionInfo() = default; 148 WebAssemblyFunctionInfo(const llvm::WebAssemblyFunctionInfo &MFI); 149 150 void mappingImpl(yaml::IO &YamlIO) override; 151 ~WebAssemblyFunctionInfo() = default; 152 }; 153 154 template <> struct MappingTraits<WebAssemblyFunctionInfo> { 155 static void mapping(IO &YamlIO, WebAssemblyFunctionInfo &MFI) {} 156 }; 157 158 } // end namespace yaml 159 160 } // end namespace llvm 161 162 #endif 163