blob: 4b9ba491dee6326b8a0613c84c63b19835db9810 [file] [log] [blame]
Nick Lewyckyc3890d22015-07-29 22:32:47 +00001// WebAssemblyMachineFunctionInfo.h-WebAssembly machine function info-*- C++ -*-
Dan Gohman10e730a2015-06-29 23:51:55 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Gohman10e730a2015-06-29 23:51:55 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file declares WebAssembly-specific per-machine-function
Dan Gohman10e730a2015-06-29 23:51:55 +000011/// information.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H
16#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYMACHINEFUNCTIONINFO_H
17
Derek Schuff9769deb2015-12-11 23:49:46 +000018#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Derek Schuff77a7a382018-10-03 22:22:48 +000019#include "llvm/BinaryFormat/Wasm.h"
Heejin Ahn52221d52019-03-26 17:35:35 +000020#include "llvm/CodeGen/MIRYamlMapping.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Derek Schuff77a7a382018-10-03 22:22:48 +000022#include "llvm/MC/MCSymbolWasm.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000023
24namespace llvm {
25
Heejin Ahn52221d52019-03-26 17:35:35 +000026namespace yaml {
27struct WebAssemblyFunctionInfo;
28}
29
Dan Gohman10e730a2015-06-29 23:51:55 +000030/// This class is derived from MachineFunctionInfo and contains private
31/// WebAssembly-specific information for each MachineFunction.
32class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
33 MachineFunction &MF;
34
Dan Gohman754cd112015-11-11 01:33:02 +000035 std::vector<MVT> Params;
Dan Gohman2726b882016-10-06 22:29:32 +000036 std::vector<MVT> Results;
Dan Gohman3acb1872016-10-24 23:27:49 +000037 std::vector<MVT> Locals;
Dan Gohmane51c0582015-10-06 00:27:55 +000038
Dan Gohmancf4748f2015-11-12 17:04:33 +000039 /// A mapping from CodeGen vreg index to WebAssembly register number.
40 std::vector<unsigned> WARegs;
41
Dan Gohman1462faa2015-11-16 16:18:28 +000042 /// 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)
Dan Gohman53d13992015-12-02 18:08:49 +000047 /// - defined and used in LIFO order with other stack registers
Dan Gohman1462faa2015-11-16 16:18:28 +000048 BitVector VRegStackified;
49
Derek Schuff27501e22016-02-10 19:51:04 +000050 // 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
Derek Schuff0d41b7b2016-11-07 22:00:48 +000055 // A virtual register holding the base pointer for functions that have
56 // overaligned values on the user stack.
57 unsigned BasePtrVreg = -1U;
58
Heejin Ahn1aaa4812019-03-26 17:46:14 +000059 // Function properties.
60 bool CFGStackified = false;
61
Heejin Ahnf208f632018-09-05 01:27:38 +000062public:
Dan Gohman0cfb5f82016-05-10 04:24:02 +000063 explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF) {}
Dan Gohman10e730a2015-06-29 23:51:55 +000064 ~WebAssemblyFunctionInfo() override;
Heejin Ahn52221d52019-03-26 17:35:35 +000065 void initializeBaseYamlFields(const yaml::WebAssemblyFunctionInfo &YamlMFI);
Dan Gohmane51c0582015-10-06 00:27:55 +000066
Dan Gohman754cd112015-11-11 01:33:02 +000067 void addParam(MVT VT) { Params.push_back(VT); }
68 const std::vector<MVT> &getParams() const { return Params; }
69
Dan Gohman2726b882016-10-06 22:29:32 +000070 void addResult(MVT VT) { Results.push_back(VT); }
71 const std::vector<MVT> &getResults() const { return Results; }
72
Heejin Ahnf208f632018-09-05 01:27:38 +000073 void clearParamsAndResults() {
74 Params.clear();
75 Results.clear();
76 }
Dan Gohmanb8184822018-05-22 04:58:36 +000077
Dan Gohmand934cb82017-02-24 23:18:00 +000078 void setNumLocals(size_t NumLocals) { Locals.resize(NumLocals, MVT::i32); }
79 void setLocal(size_t i, MVT VT) { Locals[i] = VT; }
Dan Gohman3acb1872016-10-24 23:27:49 +000080 void addLocal(MVT VT) { Locals.push_back(VT); }
81 const std::vector<MVT> &getLocals() const { return Locals; }
82
Derek Schuff27501e22016-02-10 19:51:04 +000083 unsigned getVarargBufferVreg() const {
84 assert(VarargVreg != -1U && "Vararg vreg hasn't been set");
85 return VarargVreg;
86 }
87 void setVarargBufferVreg(unsigned Reg) { VarargVreg = Reg; }
88
Derek Schuff0d41b7b2016-11-07 22:00:48 +000089 unsigned getBasePointerVreg() const {
90 assert(BasePtrVreg != -1U && "Base ptr vreg hasn't been set");
91 return BasePtrVreg;
92 }
93 void setBasePointerVreg(unsigned Reg) { BasePtrVreg = Reg; }
94
Dan Gohman058fce52015-11-13 00:21:05 +000095 static const unsigned UnusedReg = -1u;
96
Dan Gohman1462faa2015-11-16 16:18:28 +000097 void stackifyVReg(unsigned VReg) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000098 assert(MF.getRegInfo().getUniqueVRegDef(VReg));
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +000099 auto I = TargetRegisterInfo::virtReg2Index(VReg);
100 if (I >= VRegStackified.size())
101 VRegStackified.resize(I + 1);
102 VRegStackified.set(I);
Dan Gohman1462faa2015-11-16 16:18:28 +0000103 }
104 bool isVRegStackified(unsigned VReg) const {
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +0000105 auto I = TargetRegisterInfo::virtReg2Index(VReg);
106 if (I >= VRegStackified.size())
Dan Gohman1462faa2015-11-16 16:18:28 +0000107 return false;
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +0000108 return VRegStackified.test(I);
Dan Gohman1462faa2015-11-16 16:18:28 +0000109 }
110
Dan Gohman058fce52015-11-13 00:21:05 +0000111 void initWARegs();
Dan Gohmancf4748f2015-11-12 17:04:33 +0000112 void setWAReg(unsigned VReg, unsigned WAReg) {
Dan Gohman058fce52015-11-13 00:21:05 +0000113 assert(WAReg != UnusedReg);
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +0000114 auto I = TargetRegisterInfo::virtReg2Index(VReg);
115 assert(I < WARegs.size());
116 WARegs[I] = WAReg;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000117 }
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +0000118 unsigned getWAReg(unsigned VReg) const {
119 auto I = TargetRegisterInfo::virtReg2Index(VReg);
120 assert(I < WARegs.size());
121 return WARegs[I];
Dan Gohmancf4748f2015-11-12 17:04:33 +0000122 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000123
124 // For a given stackified WAReg, return the id number to print with push/pop.
125 static unsigned getWARegStackId(unsigned Reg) {
126 assert(Reg & INT32_MIN);
127 return Reg & INT32_MAX;
128 }
Heejin Ahn1aaa4812019-03-26 17:46:14 +0000129
130 bool isCFGStackified() const { return CFGStackified; }
131 void setCFGStackified(bool Value = true) { CFGStackified = Value; }
Dan Gohman10e730a2015-06-29 23:51:55 +0000132};
133
Heejin Ahn18c56a02019-02-04 19:13:39 +0000134void computeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty,
Heejin Ahnf208f632018-09-05 01:27:38 +0000135 SmallVectorImpl<MVT> &ValueVTs);
Dan Gohman2726b882016-10-06 22:29:32 +0000136
Derek Schuff77a7a382018-10-03 22:22:48 +0000137// Compute the signature for a given FunctionType (Ty). Note that it's not the
138// signature for F (F is just used to get varous context)
Heejin Ahn18c56a02019-02-04 19:13:39 +0000139void computeSignatureVTs(const FunctionType *Ty, const Function &F,
Derek Schuff77a7a382018-10-03 22:22:48 +0000140 const TargetMachine &TM, SmallVectorImpl<MVT> &Params,
Dan Gohman2726b882016-10-06 22:29:32 +0000141 SmallVectorImpl<MVT> &Results);
142
Heejin Ahn18c56a02019-02-04 19:13:39 +0000143void valTypesFromMVTs(const ArrayRef<MVT> &In,
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000144 SmallVectorImpl<wasm::ValType> &Out);
145
Derek Schuff77a7a382018-10-03 22:22:48 +0000146std::unique_ptr<wasm::WasmSignature>
Heejin Ahn18c56a02019-02-04 19:13:39 +0000147signatureFromMVTs(const SmallVectorImpl<MVT> &Results,
Derek Schuff77a7a382018-10-03 22:22:48 +0000148 const SmallVectorImpl<MVT> &Params);
149
Heejin Ahn52221d52019-03-26 17:35:35 +0000150namespace yaml {
151
152struct WebAssemblyFunctionInfo final : public yaml::MachineFunctionInfo {
Heejin Ahn1aaa4812019-03-26 17:46:14 +0000153 bool CFGStackified = false;
154
Heejin Ahn52221d52019-03-26 17:35:35 +0000155 WebAssemblyFunctionInfo() = default;
156 WebAssemblyFunctionInfo(const llvm::WebAssemblyFunctionInfo &MFI);
157
158 void mappingImpl(yaml::IO &YamlIO) override;
159 ~WebAssemblyFunctionInfo() = default;
160};
161
162template <> struct MappingTraits<WebAssemblyFunctionInfo> {
Heejin Ahn1aaa4812019-03-26 17:46:14 +0000163 static void mapping(IO &YamlIO, WebAssemblyFunctionInfo &MFI) {
164 YamlIO.mapOptional("isCFGStackified", MFI.CFGStackified, false);
165 }
Heejin Ahn52221d52019-03-26 17:35:35 +0000166};
167
168} // end namespace yaml
169
Dan Gohman10e730a2015-06-29 23:51:55 +0000170} // end namespace llvm
171
172#endif