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