Nick Lewycky | c3890d2 | 2015-07-29 22:32:47 +0000 | [diff] [blame] | 1 | // WebAssemblyMachineFunctionInfo.h-WebAssembly machine function info-*- C++ -*- |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 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 |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file declares WebAssembly-specific per-machine-function |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 12 | /// information. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H |
| 17 | #define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H |
| 18 | |
Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 20 | #include "llvm/BinaryFormat/Wasm.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSymbolWasm.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | /// This class is derived from MachineFunctionInfo and contains private |
| 27 | /// WebAssembly-specific information for each MachineFunction. |
| 28 | class WebAssemblyFunctionInfo final : public MachineFunctionInfo { |
| 29 | MachineFunction &MF; |
| 30 | |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 31 | std::vector<MVT> Params; |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 32 | std::vector<MVT> Results; |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 33 | std::vector<MVT> Locals; |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 34 | |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 35 | /// A mapping from CodeGen vreg index to WebAssembly register number. |
| 36 | std::vector<unsigned> WARegs; |
| 37 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 38 | /// A mapping from CodeGen vreg index to a boolean value indicating whether |
| 39 | /// the given register is considered to be "stackified", meaning it has been |
| 40 | /// determined or made to meet the stack requirements: |
| 41 | /// - single use (per path) |
| 42 | /// - single def (per path) |
Dan Gohman | 53d1399 | 2015-12-02 18:08:49 +0000 | [diff] [blame] | 43 | /// - defined and used in LIFO order with other stack registers |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 44 | BitVector VRegStackified; |
| 45 | |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 46 | // A virtual register holding the pointer to the vararg buffer for vararg |
| 47 | // functions. It is created and set in TLI::LowerFormalArguments and read by |
| 48 | // TLI::LowerVASTART |
| 49 | unsigned VarargVreg = -1U; |
| 50 | |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 51 | // A virtual register holding the base pointer for functions that have |
| 52 | // overaligned values on the user stack. |
| 53 | unsigned BasePtrVreg = -1U; |
| 54 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 55 | public: |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 56 | explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF) {} |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 57 | ~WebAssemblyFunctionInfo() override; |
Dan Gohman | e51c058 | 2015-10-06 00:27:55 +0000 | [diff] [blame] | 58 | |
Dan Gohman | 754cd11 | 2015-11-11 01:33:02 +0000 | [diff] [blame] | 59 | void addParam(MVT VT) { Params.push_back(VT); } |
| 60 | const std::vector<MVT> &getParams() const { return Params; } |
| 61 | |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 62 | void addResult(MVT VT) { Results.push_back(VT); } |
| 63 | const std::vector<MVT> &getResults() const { return Results; } |
| 64 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 65 | void clearParamsAndResults() { |
| 66 | Params.clear(); |
| 67 | Results.clear(); |
| 68 | } |
Dan Gohman | b818482 | 2018-05-22 04:58:36 +0000 | [diff] [blame] | 69 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 70 | void setNumLocals(size_t NumLocals) { Locals.resize(NumLocals, MVT::i32); } |
| 71 | void setLocal(size_t i, MVT VT) { Locals[i] = VT; } |
Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 72 | void addLocal(MVT VT) { Locals.push_back(VT); } |
| 73 | const std::vector<MVT> &getLocals() const { return Locals; } |
| 74 | |
Derek Schuff | 27501e2 | 2016-02-10 19:51:04 +0000 | [diff] [blame] | 75 | unsigned getVarargBufferVreg() const { |
| 76 | assert(VarargVreg != -1U && "Vararg vreg hasn't been set"); |
| 77 | return VarargVreg; |
| 78 | } |
| 79 | void setVarargBufferVreg(unsigned Reg) { VarargVreg = Reg; } |
| 80 | |
Derek Schuff | 0d41b7b | 2016-11-07 22:00:48 +0000 | [diff] [blame] | 81 | unsigned getBasePointerVreg() const { |
| 82 | assert(BasePtrVreg != -1U && "Base ptr vreg hasn't been set"); |
| 83 | return BasePtrVreg; |
| 84 | } |
| 85 | void setBasePointerVreg(unsigned Reg) { BasePtrVreg = Reg; } |
| 86 | |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 87 | static const unsigned UnusedReg = -1u; |
| 88 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 89 | void stackifyVReg(unsigned VReg) { |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 90 | assert(MF.getRegInfo().getUniqueVRegDef(VReg)); |
Wouter van Oortmerssen | 78c6296 | 2018-06-18 20:45:49 +0000 | [diff] [blame] | 91 | auto I = TargetRegisterInfo::virtReg2Index(VReg); |
| 92 | if (I >= VRegStackified.size()) |
| 93 | VRegStackified.resize(I + 1); |
| 94 | VRegStackified.set(I); |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 95 | } |
| 96 | bool isVRegStackified(unsigned VReg) const { |
Wouter van Oortmerssen | 78c6296 | 2018-06-18 20:45:49 +0000 | [diff] [blame] | 97 | auto I = TargetRegisterInfo::virtReg2Index(VReg); |
| 98 | if (I >= VRegStackified.size()) |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 99 | return false; |
Wouter van Oortmerssen | 78c6296 | 2018-06-18 20:45:49 +0000 | [diff] [blame] | 100 | return VRegStackified.test(I); |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 103 | void initWARegs(); |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 104 | void setWAReg(unsigned VReg, unsigned WAReg) { |
Dan Gohman | 058fce5 | 2015-11-13 00:21:05 +0000 | [diff] [blame] | 105 | assert(WAReg != UnusedReg); |
Wouter van Oortmerssen | 78c6296 | 2018-06-18 20:45:49 +0000 | [diff] [blame] | 106 | auto I = TargetRegisterInfo::virtReg2Index(VReg); |
| 107 | assert(I < WARegs.size()); |
| 108 | WARegs[I] = WAReg; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 109 | } |
Wouter van Oortmerssen | 78c6296 | 2018-06-18 20:45:49 +0000 | [diff] [blame] | 110 | unsigned getWAReg(unsigned VReg) const { |
| 111 | auto I = TargetRegisterInfo::virtReg2Index(VReg); |
| 112 | assert(I < WARegs.size()); |
| 113 | return WARegs[I]; |
Dan Gohman | cf4748f | 2015-11-12 17:04:33 +0000 | [diff] [blame] | 114 | } |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 115 | |
| 116 | // For a given stackified WAReg, return the id number to print with push/pop. |
| 117 | static unsigned getWARegStackId(unsigned Reg) { |
| 118 | assert(Reg & INT32_MIN); |
| 119 | return Reg & INT32_MAX; |
| 120 | } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 123 | void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty, |
| 124 | SmallVectorImpl<MVT> &ValueVTs); |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 125 | |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 126 | // Compute the signature for a given FunctionType (Ty). Note that it's not the |
| 127 | // signature for F (F is just used to get varous context) |
| 128 | void ComputeSignatureVTs(const FunctionType *Ty, const Function &F, |
| 129 | const TargetMachine &TM, SmallVectorImpl<MVT> &Params, |
Dan Gohman | 2726b88 | 2016-10-06 22:29:32 +0000 | [diff] [blame] | 130 | SmallVectorImpl<MVT> &Results); |
| 131 | |
Wouter van Oortmerssen | 49482f8 | 2018-11-19 17:10:36 +0000 | [diff] [blame] | 132 | void ValTypesFromMVTs(const ArrayRef<MVT> &In, |
| 133 | SmallVectorImpl<wasm::ValType> &Out); |
| 134 | |
Derek Schuff | 77a7a38 | 2018-10-03 22:22:48 +0000 | [diff] [blame] | 135 | std::unique_ptr<wasm::WasmSignature> |
| 136 | SignatureFromMVTs(const SmallVectorImpl<MVT> &Results, |
| 137 | const SmallVectorImpl<MVT> &Params); |
| 138 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 139 | } // end namespace llvm |
| 140 | |
| 141 | #endif |