blob: b02c14901fb517ab49f108769655d4fc84ad0130 [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"
Dan Gohman10e730a2015-06-29 23:51:55 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Derek Schuff77a7a382018-10-03 22:22:48 +000021#include "llvm/MC/MCSymbolWasm.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000022
23namespace llvm {
24
25/// This class is derived from MachineFunctionInfo and contains private
26/// WebAssembly-specific information for each MachineFunction.
27class WebAssemblyFunctionInfo final : public MachineFunctionInfo {
28 MachineFunction &MF;
29
Dan Gohman754cd112015-11-11 01:33:02 +000030 std::vector<MVT> Params;
Dan Gohman2726b882016-10-06 22:29:32 +000031 std::vector<MVT> Results;
Dan Gohman3acb1872016-10-24 23:27:49 +000032 std::vector<MVT> Locals;
Dan Gohmane51c0582015-10-06 00:27:55 +000033
Dan Gohmancf4748f2015-11-12 17:04:33 +000034 /// A mapping from CodeGen vreg index to WebAssembly register number.
35 std::vector<unsigned> WARegs;
36
Dan Gohman1462faa2015-11-16 16:18:28 +000037 /// 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 Gohman53d13992015-12-02 18:08:49 +000042 /// - defined and used in LIFO order with other stack registers
Dan Gohman1462faa2015-11-16 16:18:28 +000043 BitVector VRegStackified;
44
Derek Schuff27501e22016-02-10 19:51:04 +000045 // 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 Schuff0d41b7b2016-11-07 22:00:48 +000050 // A virtual register holding the base pointer for functions that have
51 // overaligned values on the user stack.
52 unsigned BasePtrVreg = -1U;
53
Heejin Ahnf208f632018-09-05 01:27:38 +000054public:
Dan Gohman0cfb5f82016-05-10 04:24:02 +000055 explicit WebAssemblyFunctionInfo(MachineFunction &MF) : MF(MF) {}
Dan Gohman10e730a2015-06-29 23:51:55 +000056 ~WebAssemblyFunctionInfo() override;
Dan Gohmane51c0582015-10-06 00:27:55 +000057
Dan Gohman754cd112015-11-11 01:33:02 +000058 void addParam(MVT VT) { Params.push_back(VT); }
59 const std::vector<MVT> &getParams() const { return Params; }
60
Dan Gohman2726b882016-10-06 22:29:32 +000061 void addResult(MVT VT) { Results.push_back(VT); }
62 const std::vector<MVT> &getResults() const { return Results; }
63
Heejin Ahnf208f632018-09-05 01:27:38 +000064 void clearParamsAndResults() {
65 Params.clear();
66 Results.clear();
67 }
Dan Gohmanb8184822018-05-22 04:58:36 +000068
Dan Gohmand934cb82017-02-24 23:18:00 +000069 void setNumLocals(size_t NumLocals) { Locals.resize(NumLocals, MVT::i32); }
70 void setLocal(size_t i, MVT VT) { Locals[i] = VT; }
Dan Gohman3acb1872016-10-24 23:27:49 +000071 void addLocal(MVT VT) { Locals.push_back(VT); }
72 const std::vector<MVT> &getLocals() const { return Locals; }
73
Derek Schuff27501e22016-02-10 19:51:04 +000074 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 Schuff0d41b7b2016-11-07 22:00:48 +000080 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 Gohman058fce52015-11-13 00:21:05 +000086 static const unsigned UnusedReg = -1u;
87
Dan Gohman1462faa2015-11-16 16:18:28 +000088 void stackifyVReg(unsigned VReg) {
Dan Gohman4fc4e422016-10-24 19:49:43 +000089 assert(MF.getRegInfo().getUniqueVRegDef(VReg));
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +000090 auto I = TargetRegisterInfo::virtReg2Index(VReg);
91 if (I >= VRegStackified.size())
92 VRegStackified.resize(I + 1);
93 VRegStackified.set(I);
Dan Gohman1462faa2015-11-16 16:18:28 +000094 }
95 bool isVRegStackified(unsigned VReg) const {
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +000096 auto I = TargetRegisterInfo::virtReg2Index(VReg);
97 if (I >= VRegStackified.size())
Dan Gohman1462faa2015-11-16 16:18:28 +000098 return false;
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +000099 return VRegStackified.test(I);
Dan Gohman1462faa2015-11-16 16:18:28 +0000100 }
101
Dan Gohman058fce52015-11-13 00:21:05 +0000102 void initWARegs();
Dan Gohmancf4748f2015-11-12 17:04:33 +0000103 void setWAReg(unsigned VReg, unsigned WAReg) {
Dan Gohman058fce52015-11-13 00:21:05 +0000104 assert(WAReg != UnusedReg);
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +0000105 auto I = TargetRegisterInfo::virtReg2Index(VReg);
106 assert(I < WARegs.size());
107 WARegs[I] = WAReg;
Dan Gohmancf4748f2015-11-12 17:04:33 +0000108 }
Wouter van Oortmerssen78c62962018-06-18 20:45:49 +0000109 unsigned getWAReg(unsigned VReg) const {
110 auto I = TargetRegisterInfo::virtReg2Index(VReg);
111 assert(I < WARegs.size());
112 return WARegs[I];
Dan Gohmancf4748f2015-11-12 17:04:33 +0000113 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000114
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 Gohman10e730a2015-06-29 23:51:55 +0000120};
121
Heejin Ahnf208f632018-09-05 01:27:38 +0000122void ComputeLegalValueVTs(const Function &F, const TargetMachine &TM, Type *Ty,
123 SmallVectorImpl<MVT> &ValueVTs);
Dan Gohman2726b882016-10-06 22:29:32 +0000124
Derek Schuff77a7a382018-10-03 22:22:48 +0000125// 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)
127void ComputeSignatureVTs(const FunctionType *Ty, const Function &F,
128 const TargetMachine &TM, SmallVectorImpl<MVT> &Params,
Dan Gohman2726b882016-10-06 22:29:32 +0000129 SmallVectorImpl<MVT> &Results);
130
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000131void ValTypesFromMVTs(const ArrayRef<MVT> &In,
132 SmallVectorImpl<wasm::ValType> &Out);
133
Derek Schuff77a7a382018-10-03 22:22:48 +0000134std::unique_ptr<wasm::WasmSignature>
135SignatureFromMVTs(const SmallVectorImpl<MVT> &Results,
136 const SmallVectorImpl<MVT> &Params);
137
Dan Gohman10e730a2015-06-29 23:51:55 +0000138} // end namespace llvm
139
140#endif