17d523365SDimitry Andric // WebAssemblyMachineFunctionInfo.h-WebAssembly machine function info-*- C++ -*-
23dac3a9bSDimitry Andric //
33dac3a9bSDimitry Andric //                     The LLVM Compiler Infrastructure
43dac3a9bSDimitry Andric //
53dac3a9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
63dac3a9bSDimitry Andric // License. See LICENSE.TXT for details.
73dac3a9bSDimitry Andric //
83dac3a9bSDimitry Andric //===----------------------------------------------------------------------===//
93dac3a9bSDimitry Andric ///
103dac3a9bSDimitry Andric /// \file
114ba319b5SDimitry Andric /// This file declares WebAssembly-specific per-machine-function
123dac3a9bSDimitry Andric /// information.
133dac3a9bSDimitry Andric ///
143dac3a9bSDimitry Andric //===----------------------------------------------------------------------===//
153dac3a9bSDimitry Andric 
163dac3a9bSDimitry Andric #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H
173dac3a9bSDimitry Andric #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H
183dac3a9bSDimitry Andric 
197d523365SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
20*b5893f02SDimitry Andric #include "llvm/BinaryFormat/Wasm.h"
213dac3a9bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
22*b5893f02SDimitry Andric #include "llvm/MC/MCSymbolWasm.h"
233dac3a9bSDimitry Andric 
243dac3a9bSDimitry Andric namespace llvm {
253dac3a9bSDimitry Andric 
263dac3a9bSDimitry Andric /// This class is derived from MachineFunctionInfo and contains private
273dac3a9bSDimitry Andric /// WebAssembly-specific information for each MachineFunction.
283dac3a9bSDimitry Andric class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
293dac3a9bSDimitry Andric   MachineFunction &MF;
303dac3a9bSDimitry Andric 
317d523365SDimitry Andric   std::vector<MVT> Params;
32d88c1a5aSDimitry Andric   std::vector<MVT> Results;
33d88c1a5aSDimitry Andric   std::vector<MVT> Locals;
347d523365SDimitry Andric 
357d523365SDimitry Andric   /// A mapping from CodeGen vreg index to WebAssembly register number.
367d523365SDimitry Andric   std::vector<unsigned> WARegs;
377d523365SDimitry Andric 
387d523365SDimitry Andric   /// A mapping from CodeGen vreg index to a boolean value indicating whether
397d523365SDimitry Andric   /// the given register is considered to be "stackified", meaning it has been
407d523365SDimitry Andric   /// determined or made to meet the stack requirements:
417d523365SDimitry Andric   ///   - single use (per path)
427d523365SDimitry Andric   ///   - single def (per path)
437d523365SDimitry Andric   ///   - defined and used in LIFO order with other stack registers
447d523365SDimitry Andric   BitVector VRegStackified;
457d523365SDimitry Andric 
463ca95b02SDimitry Andric   // A virtual register holding the pointer to the vararg buffer for vararg
473ca95b02SDimitry Andric   // functions. It is created and set in TLI::LowerFormalArguments and read by
483ca95b02SDimitry Andric   // TLI::LowerVASTART
493ca95b02SDimitry Andric   unsigned VarargVreg = -1U;
507d523365SDimitry Andric 
51d88c1a5aSDimitry Andric   // A virtual register holding the base pointer for functions that have
52d88c1a5aSDimitry Andric   // overaligned values on the user stack.
53d88c1a5aSDimitry Andric   unsigned BasePtrVreg = -1U;
54d88c1a5aSDimitry Andric 
553dac3a9bSDimitry Andric public:
WebAssemblyFunctionInfo(MachineFunction & MF)563ca95b02SDimitry Andric   explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF) {}
573dac3a9bSDimitry Andric   ~WebAssemblyFunctionInfo() override;
587d523365SDimitry Andric 
addParam(MVT VT)597d523365SDimitry Andric   void addParam(MVT VT) { Params.push_back(VT); }
getParams()607d523365SDimitry Andric   const std::vector<MVT> &getParams() const { return Params; }
617d523365SDimitry Andric 
addResult(MVT VT)62d88c1a5aSDimitry Andric   void addResult(MVT VT) { Results.push_back(VT); }
getResults()63d88c1a5aSDimitry Andric   const std::vector<MVT> &getResults() const { return Results; }
64d88c1a5aSDimitry Andric 
clearParamsAndResults()65*b5893f02SDimitry Andric   void clearParamsAndResults() {
66*b5893f02SDimitry Andric     Params.clear();
67*b5893f02SDimitry Andric     Results.clear();
68*b5893f02SDimitry Andric   }
694ba319b5SDimitry Andric 
setNumLocals(size_t NumLocals)707a7e6055SDimitry Andric   void setNumLocals(size_t NumLocals) { Locals.resize(NumLocals, MVT::i32); }
setLocal(size_t i,MVT VT)717a7e6055SDimitry Andric   void setLocal(size_t i, MVT VT) { Locals[i] = VT; }
addLocal(MVT VT)72d88c1a5aSDimitry Andric   void addLocal(MVT VT) { Locals.push_back(VT); }
getLocals()73d88c1a5aSDimitry Andric   const std::vector<MVT> &getLocals() const { return Locals; }
74d88c1a5aSDimitry Andric 
getVarargBufferVreg()753ca95b02SDimitry Andric   unsigned getVarargBufferVreg() const {
763ca95b02SDimitry Andric     assert(VarargVreg != -1U && "Vararg vreg hasn't been set");
773ca95b02SDimitry Andric     return VarargVreg;
783ca95b02SDimitry Andric   }
setVarargBufferVreg(unsigned Reg)793ca95b02SDimitry Andric   void setVarargBufferVreg(unsigned Reg) { VarargVreg = Reg; }
803ca95b02SDimitry Andric 
getBasePointerVreg()81d88c1a5aSDimitry Andric   unsigned getBasePointerVreg() const {
82d88c1a5aSDimitry Andric     assert(BasePtrVreg != -1U && "Base ptr vreg hasn't been set");
83d88c1a5aSDimitry Andric     return BasePtrVreg;
84d88c1a5aSDimitry Andric   }
setBasePointerVreg(unsigned Reg)85d88c1a5aSDimitry Andric   void setBasePointerVreg(unsigned Reg) { BasePtrVreg = Reg; }
86d88c1a5aSDimitry Andric 
877d523365SDimitry Andric   static const unsigned UnusedReg = -1u;
887d523365SDimitry Andric 
stackifyVReg(unsigned VReg)897d523365SDimitry Andric   void stackifyVReg(unsigned VReg) {
90d88c1a5aSDimitry Andric     assert(MF.getRegInfo().getUniqueVRegDef(VReg));
914ba319b5SDimitry Andric     auto I = TargetRegisterInfo::virtReg2Index(VReg);
924ba319b5SDimitry Andric     if (I >= VRegStackified.size())
934ba319b5SDimitry Andric       VRegStackified.resize(I + 1);
944ba319b5SDimitry Andric     VRegStackified.set(I);
957d523365SDimitry Andric   }
isVRegStackified(unsigned VReg)967d523365SDimitry Andric   bool isVRegStackified(unsigned VReg) const {
974ba319b5SDimitry Andric     auto I = TargetRegisterInfo::virtReg2Index(VReg);
984ba319b5SDimitry Andric     if (I >= VRegStackified.size())
997d523365SDimitry Andric       return false;
1004ba319b5SDimitry Andric     return VRegStackified.test(I);
1017d523365SDimitry Andric   }
1027d523365SDimitry Andric 
1037d523365SDimitry Andric   void initWARegs();
setWAReg(unsigned VReg,unsigned WAReg)1047d523365SDimitry Andric   void setWAReg(unsigned VReg, unsigned WAReg) {
1057d523365SDimitry Andric     assert(WAReg != UnusedReg);
1064ba319b5SDimitry Andric     auto I = TargetRegisterInfo::virtReg2Index(VReg);
1074ba319b5SDimitry Andric     assert(I < WARegs.size());
1084ba319b5SDimitry Andric     WARegs[I] = WAReg;
1097d523365SDimitry Andric   }
getWAReg(unsigned VReg)1104ba319b5SDimitry Andric   unsigned getWAReg(unsigned VReg) const {
1114ba319b5SDimitry Andric     auto I = TargetRegisterInfo::virtReg2Index(VReg);
1124ba319b5SDimitry Andric     assert(I < WARegs.size());
1134ba319b5SDimitry Andric     return WARegs[I];
1147d523365SDimitry Andric   }
1157d523365SDimitry Andric 
1163ca95b02SDimitry Andric   // For a given stackified WAReg, return the id number to print with push/pop.
getWARegStackId(unsigned Reg)1173ca95b02SDimitry Andric   static unsigned getWARegStackId(unsigned Reg) {
1183ca95b02SDimitry Andric     assert(Reg & INT32_MIN);
1193ca95b02SDimitry Andric     return Reg & INT32_MAX;
1207d523365SDimitry Andric   }
1213dac3a9bSDimitry Andric };
1223dac3a9bSDimitry Andric 
123*b5893f02SDimitry Andric void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty,
124*b5893f02SDimitry Andric                           SmallVectorImpl<MVT> &ValueVTs);
125d88c1a5aSDimitry Andric 
126*b5893f02SDimitry Andric // Compute the signature for a given FunctionType (Ty). Note that it's not the
127*b5893f02SDimitry Andric // signature for F (F is just used to get varous context)
128*b5893f02SDimitry Andric void ComputeSignatureVTs(const FunctionType *Ty, const Function &F,
129*b5893f02SDimitry Andric                          const TargetMachine &TM, SmallVectorImpl<MVT> &Params,
130d88c1a5aSDimitry Andric                          SmallVectorImpl<MVT> &Results);
131d88c1a5aSDimitry Andric 
132*b5893f02SDimitry Andric void ValTypesFromMVTs(const ArrayRef<MVT> &In,
133*b5893f02SDimitry Andric                       SmallVectorImpl<wasm::ValType> &Out);
134*b5893f02SDimitry Andric 
135*b5893f02SDimitry Andric std::unique_ptr<wasm::WasmSignature>
136*b5893f02SDimitry Andric SignatureFromMVTs(const SmallVectorImpl<MVT> &Results,
137*b5893f02SDimitry Andric                   const SmallVectorImpl<MVT> &Params);
138*b5893f02SDimitry Andric 
1393dac3a9bSDimitry Andric } // end namespace llvm
1403dac3a9bSDimitry Andric 
1413dac3a9bSDimitry Andric #endif
142