Coverage Report

Created: 2025-10-10 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/lib/llvm/compiler.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: 2019-2024 Second State INC
3
4
#include "llvm/compiler.h"
5
6
#include "aot/version.h"
7
#include "common/defines.h"
8
#include "common/filesystem.h"
9
#include "common/spdlog.h"
10
#include "data.h"
11
#include "llvm.h"
12
#include "system/allocator.h"
13
14
#include <algorithm>
15
#include <array>
16
#include <cinttypes>
17
#include <cstdint>
18
#include <cstdlib>
19
#include <limits>
20
#include <memory>
21
#include <numeric>
22
#include <string>
23
#include <string_view>
24
#include <system_error>
25
26
namespace LLVM = WasmEdge::LLVM;
27
using namespace std::literals;
28
29
namespace {
30
31
static bool
32
isVoidReturn(WasmEdge::Span<const WasmEdge::ValType> ValTypes) noexcept;
33
static LLVM::Type toLLVMType(LLVM::Context LLContext,
34
                             const WasmEdge::ValType &ValType) noexcept;
35
static std::vector<LLVM::Type>
36
toLLVMArgsType(LLVM::Context LLContext, LLVM::Type ExecCtxPtrTy,
37
               WasmEdge::Span<const WasmEdge::ValType> ValTypes) noexcept;
38
static LLVM::Type
39
toLLVMRetsType(LLVM::Context LLContext,
40
               WasmEdge::Span<const WasmEdge::ValType> ValTypes) noexcept;
41
static LLVM::Type
42
toLLVMType(LLVM::Context LLContext, LLVM::Type ExecCtxPtrTy,
43
           const WasmEdge::AST::FunctionType &FuncType) noexcept;
44
static LLVM::Value
45
toLLVMConstantZero(LLVM::Context LLContext,
46
                   const WasmEdge::ValType &ValType) noexcept;
47
static std::vector<LLVM::Value> unpackStruct(LLVM::Builder &Builder,
48
                                             LLVM::Value Struct) noexcept;
49
class FunctionCompiler;
50
51
// XXX: Misalignment handler not implemented yet, forcing unalignment
52
// force unalignment load/store
53
static inline constexpr const bool kForceUnalignment = true;
54
55
// force checking div/rem on zero
56
static inline constexpr const bool kForceDivCheck = true;
57
58
// Size of a ValVariant
59
static inline constexpr const uint32_t kValSize = sizeof(WasmEdge::ValVariant);
60
61
// Translate Compiler::OptimizationLevel to llvm::PassBuilder version
62
#if LLVM_VERSION_MAJOR >= 13
63
static inline const char *
64
toLLVMLevel(WasmEdge::CompilerConfigure::OptimizationLevel Level) noexcept {
65
  using OL = WasmEdge::CompilerConfigure::OptimizationLevel;
66
  switch (Level) {
67
  case OL::O0:
68
    return "default<O0>,function(tailcallelim)";
69
  case OL::O1:
70
    return "default<O1>,function(tailcallelim)";
71
  case OL::O2:
72
    return "default<O2>";
73
  case OL::O3:
74
    return "default<O3>";
75
  case OL::Os:
76
    return "default<Os>";
77
  case OL::Oz:
78
    return "default<Oz>";
79
  default:
80
    assumingUnreachable();
81
  }
82
}
83
#else
84
static inline std::pair<unsigned int, unsigned int>
85
2.00k
toLLVMLevel(WasmEdge::CompilerConfigure::OptimizationLevel Level) noexcept {
86
2.00k
  using OL = WasmEdge::CompilerConfigure::OptimizationLevel;
87
2.00k
  switch (Level) {
88
0
  case OL::O0:
89
0
    return {0, 0};
90
0
  case OL::O1:
91
0
    return {1, 0};
92
0
  case OL::O2:
93
0
    return {2, 0};
94
2.00k
  case OL::O3:
95
2.00k
    return {3, 0};
96
0
  case OL::Os:
97
0
    return {2, 1};
98
0
  case OL::Oz:
99
0
    return {2, 2};
100
0
  default:
101
0
    assumingUnreachable();
102
2.00k
  }
103
2.00k
}
104
#endif
105
106
static inline LLVMCodeGenOptLevel toLLVMCodeGenLevel(
107
2.00k
    WasmEdge::CompilerConfigure::OptimizationLevel Level) noexcept {
108
2.00k
  using OL = WasmEdge::CompilerConfigure::OptimizationLevel;
109
2.00k
  switch (Level) {
110
0
  case OL::O0:
111
0
    return LLVMCodeGenLevelNone;
112
0
  case OL::O1:
113
0
    return LLVMCodeGenLevelLess;
114
0
  case OL::O2:
115
0
    return LLVMCodeGenLevelDefault;
116
2.00k
  case OL::O3:
117
2.00k
    return LLVMCodeGenLevelAggressive;
118
0
  case OL::Os:
119
0
    return LLVMCodeGenLevelDefault;
120
0
  case OL::Oz:
121
0
    return LLVMCodeGenLevelDefault;
122
0
  default:
123
0
    assumingUnreachable();
124
2.00k
  }
125
2.00k
}
126
} // namespace
127
128
struct LLVM::Compiler::CompileContext {
129
  LLVM::Context LLContext;
130
  LLVM::Module &LLModule;
131
  LLVM::Attribute Cold;
132
  LLVM::Attribute NoAlias;
133
  LLVM::Attribute NoInline;
134
  LLVM::Attribute NoReturn;
135
  LLVM::Attribute ReadOnly;
136
  LLVM::Attribute StrictFP;
137
  LLVM::Attribute UWTable;
138
  LLVM::Attribute NoStackArgProbe;
139
  LLVM::Type VoidTy;
140
  LLVM::Type Int8Ty;
141
  LLVM::Type Int16Ty;
142
  LLVM::Type Int32Ty;
143
  LLVM::Type Int64Ty;
144
  LLVM::Type Int128Ty;
145
  LLVM::Type FloatTy;
146
  LLVM::Type DoubleTy;
147
  LLVM::Type Int8x16Ty;
148
  LLVM::Type Int16x8Ty;
149
  LLVM::Type Int32x4Ty;
150
  LLVM::Type Floatx4Ty;
151
  LLVM::Type Int64x2Ty;
152
  LLVM::Type Doublex2Ty;
153
  LLVM::Type Int128x1Ty;
154
  LLVM::Type Int8PtrTy;
155
  LLVM::Type Int32PtrTy;
156
  LLVM::Type Int64PtrTy;
157
  LLVM::Type Int128PtrTy;
158
  LLVM::Type Int8PtrPtrTy;
159
  LLVM::Type ExecCtxTy;
160
  LLVM::Type ExecCtxPtrTy;
161
  LLVM::Type IntrinsicsTableTy;
162
  LLVM::Type IntrinsicsTablePtrTy;
163
  LLVM::Message SubtargetFeatures;
164
165
#if defined(__x86_64__)
166
#if defined(__XOP__)
167
  bool SupportXOP = true;
168
#else
169
  bool SupportXOP = false;
170
#endif
171
172
#if defined(__SSE4_1__)
173
  bool SupportSSE4_1 = true;
174
#else
175
  bool SupportSSE4_1 = false;
176
#endif
177
178
#if defined(__SSSE3__)
179
  bool SupportSSSE3 = true;
180
#else
181
  bool SupportSSSE3 = false;
182
#endif
183
184
#if defined(__SSE2__)
185
  bool SupportSSE2 = true;
186
#else
187
  bool SupportSSE2 = false;
188
#endif
189
#endif
190
191
#if defined(__aarch64__)
192
#if defined(__ARM_NEON__) || defined(__ARM_NEON) || defined(__ARM_NEON_FP)
193
  bool SupportNEON = true;
194
#else
195
  bool SupportNEON = false;
196
#endif
197
#endif
198
199
  std::vector<const AST::CompositeType *> CompositeTypes;
200
  std::vector<LLVM::Value> FunctionWrappers;
201
  std::vector<std::tuple<uint32_t, LLVM::FunctionCallee,
202
                         const WasmEdge::AST::CodeSegment *>>
203
      Functions;
204
  std::vector<LLVM::Type> Globals;
205
  LLVM::Value IntrinsicsTable;
206
  LLVM::FunctionCallee Trap;
207
  CompileContext(LLVM::Context C, LLVM::Module &M,
208
                 bool IsGenericBinary) noexcept
209
2.00k
      : LLContext(C), LLModule(M),
210
2.00k
        Cold(LLVM::Attribute::createEnum(C, LLVM::Core::Cold, 0)),
211
2.00k
        NoAlias(LLVM::Attribute::createEnum(C, LLVM::Core::NoAlias, 0)),
212
2.00k
        NoInline(LLVM::Attribute::createEnum(C, LLVM::Core::NoInline, 0)),
213
2.00k
        NoReturn(LLVM::Attribute::createEnum(C, LLVM::Core::NoReturn, 0)),
214
2.00k
        ReadOnly(LLVM::Attribute::createEnum(C, LLVM::Core::ReadOnly, 0)),
215
2.00k
        StrictFP(LLVM::Attribute::createEnum(C, LLVM::Core::StrictFP, 0)),
216
2.00k
        UWTable(LLVM::Attribute::createEnum(C, LLVM::Core::UWTable,
217
2.00k
                                            LLVM::Core::UWTableDefault)),
218
        NoStackArgProbe(
219
2.00k
            LLVM::Attribute::createString(C, "no-stack-arg-probe"sv, {})),
220
2.00k
        VoidTy(LLContext.getVoidTy()), Int8Ty(LLContext.getInt8Ty()),
221
2.00k
        Int16Ty(LLContext.getInt16Ty()), Int32Ty(LLContext.getInt32Ty()),
222
2.00k
        Int64Ty(LLContext.getInt64Ty()), Int128Ty(LLContext.getInt128Ty()),
223
2.00k
        FloatTy(LLContext.getFloatTy()), DoubleTy(LLContext.getDoubleTy()),
224
2.00k
        Int8x16Ty(LLVM::Type::getVectorType(Int8Ty, 16)),
225
2.00k
        Int16x8Ty(LLVM::Type::getVectorType(Int16Ty, 8)),
226
2.00k
        Int32x4Ty(LLVM::Type::getVectorType(Int32Ty, 4)),
227
2.00k
        Floatx4Ty(LLVM::Type::getVectorType(FloatTy, 4)),
228
2.00k
        Int64x2Ty(LLVM::Type::getVectorType(Int64Ty, 2)),
229
2.00k
        Doublex2Ty(LLVM::Type::getVectorType(DoubleTy, 2)),
230
2.00k
        Int128x1Ty(LLVM::Type::getVectorType(Int128Ty, 1)),
231
2.00k
        Int8PtrTy(Int8Ty.getPointerTo()), Int32PtrTy(Int32Ty.getPointerTo()),
232
2.00k
        Int64PtrTy(Int64Ty.getPointerTo()),
233
2.00k
        Int128PtrTy(Int128Ty.getPointerTo()),
234
2.00k
        Int8PtrPtrTy(Int8PtrTy.getPointerTo()),
235
2.00k
        ExecCtxTy(LLVM::Type::getStructType(
236
2.00k
            "ExecCtx",
237
2.00k
            std::initializer_list<LLVM::Type>{
238
                // Memory
239
2.00k
                Int8PtrTy.getPointerTo(),
240
                // Globals
241
2.00k
                Int128PtrTy.getPointerTo(),
242
                // InstrCount
243
2.00k
                Int64PtrTy,
244
                // CostTable
245
2.00k
                LLVM::Type::getArrayType(Int64Ty, UINT16_MAX + 1)
246
2.00k
                    .getPointerTo(),
247
                // Gas
248
2.00k
                Int64PtrTy,
249
                // GasLimit
250
2.00k
                Int64Ty,
251
                // StopToken
252
2.00k
                Int32PtrTy,
253
2.00k
            })),
254
2.00k
        ExecCtxPtrTy(ExecCtxTy.getPointerTo()),
255
2.00k
        IntrinsicsTableTy(LLVM::Type::getArrayType(
256
2.00k
            Int8PtrTy,
257
2.00k
            static_cast<uint32_t>(Executable::Intrinsics::kIntrinsicMax))),
258
2.00k
        IntrinsicsTablePtrTy(IntrinsicsTableTy.getPointerTo()),
259
2.00k
        IntrinsicsTable(LLModule.addGlobal(IntrinsicsTablePtrTy, true,
260
2.00k
                                           LLVMExternalLinkage, LLVM::Value(),
261
2.00k
                                           "intrinsics")) {
262
2.00k
    Trap.Ty = LLVM::Type::getFunctionType(VoidTy, {Int32Ty});
263
2.00k
    Trap.Fn = LLModule.addFunction(Trap.Ty, LLVMPrivateLinkage, "trap");
264
2.00k
    Trap.Fn.setDSOLocal(true);
265
2.00k
    Trap.Fn.addFnAttr(NoStackArgProbe);
266
2.00k
    Trap.Fn.addFnAttr(StrictFP);
267
2.00k
    Trap.Fn.addFnAttr(UWTable);
268
2.00k
    Trap.Fn.addFnAttr(NoReturn);
269
2.00k
    Trap.Fn.addFnAttr(Cold);
270
2.00k
    Trap.Fn.addFnAttr(NoInline);
271
272
2.00k
    LLModule.addGlobal(Int32Ty, true, LLVMExternalLinkage,
273
2.00k
                       LLVM::Value::getConstInt(Int32Ty, AOT::kBinaryVersion),
274
2.00k
                       "version");
275
276
2.00k
    if (!IsGenericBinary) {
277
2.00k
      SubtargetFeatures = LLVM::getHostCPUFeatures();
278
2.00k
      auto Features = SubtargetFeatures.string_view();
279
174k
      while (!Features.empty()) {
280
172k
        std::string_view Feature;
281
172k
        if (auto Pos = Features.find(','); Pos != std::string_view::npos) {
282
170k
          Feature = Features.substr(0, Pos);
283
170k
          Features = Features.substr(Pos + 1);
284
170k
        } else {
285
2.00k
          Feature = std::exchange(Features, std::string_view());
286
2.00k
        }
287
172k
        if (Feature[0] != '+') {
288
96.0k
          continue;
289
96.0k
        }
290
76.0k
        Feature = Feature.substr(1);
291
292
76.0k
#if defined(__x86_64__)
293
76.0k
        if (!SupportXOP && Feature == "xop"sv) {
294
0
          SupportXOP = true;
295
0
        }
296
76.0k
        if (!SupportSSE4_1 && Feature == "sse4.1"sv) {
297
2.00k
          SupportSSE4_1 = true;
298
2.00k
        }
299
76.0k
        if (!SupportSSSE3 && Feature == "ssse3"sv) {
300
2.00k
          SupportSSSE3 = true;
301
2.00k
        }
302
76.0k
        if (!SupportSSE2 && Feature == "sse2"sv) {
303
0
          SupportSSE2 = true;
304
0
        }
305
#elif defined(__aarch64__)
306
        if (!SupportNEON && Feature == "neon"sv) {
307
          SupportNEON = true;
308
        }
309
#endif
310
76.0k
      }
311
2.00k
    }
312
313
2.00k
    {
314
      // create trap
315
2.00k
      LLVM::Builder Builder(LLContext);
316
2.00k
      Builder.positionAtEnd(
317
2.00k
          LLVM::BasicBlock::create(LLContext, Trap.Fn, "entry"));
318
2.00k
      auto FnTy = LLVM::Type::getFunctionType(VoidTy, {Int32Ty});
319
2.00k
      auto CallTrap = Builder.createCall(
320
2.00k
          getIntrinsic(Builder, Executable::Intrinsics::kTrap, FnTy),
321
2.00k
          {Trap.Fn.getFirstParam()});
322
2.00k
      CallTrap.addCallSiteAttribute(NoReturn);
323
2.00k
      Builder.createUnreachable();
324
2.00k
    }
325
2.00k
  }
326
  LLVM::Value getMemory(LLVM::Builder &Builder, LLVM::Value ExecCtx,
327
23.7k
                        uint32_t Index) noexcept {
328
23.7k
    auto Array = Builder.createExtractValue(ExecCtx, 0);
329
#if WASMEDGE_ALLOCATOR_IS_STABLE
330
    auto VPtr = Builder.createLoad(
331
        Int8PtrTy, Builder.createInBoundsGEP1(Int8PtrTy, Array,
332
                                              LLContext.getInt64(Index)));
333
    VPtr.setMetadata(LLContext, LLVM::Core::InvariantGroup,
334
                     LLVM::Metadata(LLContext, {}));
335
#else
336
23.7k
    auto VPtrPtr = Builder.createLoad(
337
23.7k
        Int8PtrPtrTy, Builder.createInBoundsGEP1(Int8PtrPtrTy, Array,
338
23.7k
                                                 LLContext.getInt64(Index)));
339
23.7k
    VPtrPtr.setMetadata(LLContext, LLVM::Core::InvariantGroup,
340
23.7k
                        LLVM::Metadata(LLContext, {}));
341
23.7k
    auto VPtr = Builder.createLoad(
342
23.7k
        Int8PtrTy,
343
23.7k
        Builder.createInBoundsGEP1(Int8PtrTy, VPtrPtr, LLContext.getInt64(0)));
344
23.7k
#endif
345
23.7k
    return Builder.createBitCast(VPtr, Int8PtrTy);
346
23.7k
  }
347
  std::pair<LLVM::Type, LLVM::Value> getGlobal(LLVM::Builder &Builder,
348
                                               LLVM::Value ExecCtx,
349
306
                                               uint32_t Index) noexcept {
350
306
    auto Ty = Globals[Index];
351
306
    auto Array = Builder.createExtractValue(ExecCtx, 1);
352
306
    auto VPtr = Builder.createLoad(
353
306
        Int128PtrTy, Builder.createInBoundsGEP1(Int8PtrTy, Array,
354
306
                                                LLContext.getInt64(Index)));
355
306
    VPtr.setMetadata(LLContext, LLVM::Core::InvariantGroup,
356
306
                     LLVM::Metadata(LLContext, {}));
357
306
    auto Ptr = Builder.createBitCast(VPtr, Ty.getPointerTo());
358
306
    return {Ty, Ptr};
359
306
  }
360
  LLVM::Value getInstrCount(LLVM::Builder &Builder,
361
0
                            LLVM::Value ExecCtx) noexcept {
362
0
    return Builder.createExtractValue(ExecCtx, 2);
363
0
  }
364
  LLVM::Value getCostTable(LLVM::Builder &Builder,
365
0
                           LLVM::Value ExecCtx) noexcept {
366
0
    return Builder.createExtractValue(ExecCtx, 3);
367
0
  }
368
0
  LLVM::Value getGas(LLVM::Builder &Builder, LLVM::Value ExecCtx) noexcept {
369
0
    return Builder.createExtractValue(ExecCtx, 4);
370
0
  }
371
  LLVM::Value getGasLimit(LLVM::Builder &Builder,
372
0
                          LLVM::Value ExecCtx) noexcept {
373
0
    return Builder.createExtractValue(ExecCtx, 5);
374
0
  }
375
  LLVM::Value getStopToken(LLVM::Builder &Builder,
376
0
                           LLVM::Value ExecCtx) noexcept {
377
0
    return Builder.createExtractValue(ExecCtx, 6);
378
0
  }
379
  LLVM::FunctionCallee getIntrinsic(LLVM::Builder &Builder,
380
                                    Executable::Intrinsics Index,
381
6.15k
                                    LLVM::Type Ty) noexcept {
382
6.15k
    const auto Value = static_cast<uint32_t>(Index);
383
6.15k
    auto PtrTy = Ty.getPointerTo();
384
6.15k
    auto PtrPtrTy = PtrTy.getPointerTo();
385
6.15k
    auto IT = Builder.createLoad(IntrinsicsTablePtrTy, IntrinsicsTable);
386
6.15k
    IT.setMetadata(LLContext, LLVM::Core::InvariantGroup,
387
6.15k
                   LLVM::Metadata(LLContext, {}));
388
6.15k
    auto VPtr =
389
6.15k
        Builder.createInBoundsGEP2(IntrinsicsTableTy, IT, LLContext.getInt64(0),
390
6.15k
                                   LLContext.getInt64(Value));
391
6.15k
    auto Ptr = Builder.createBitCast(VPtr, PtrPtrTy);
392
6.15k
    return {Ty, Builder.createLoad(PtrTy, Ptr)};
393
6.15k
  }
394
  std::pair<std::vector<ValType>, std::vector<ValType>>
395
17.1k
  resolveBlockType(const BlockType &BType) const noexcept {
396
17.1k
    using VecT = std::vector<ValType>;
397
17.1k
    using RetT = std::pair<VecT, VecT>;
398
17.1k
    if (BType.isEmpty()) {
399
1.99k
      return RetT{};
400
1.99k
    }
401
15.2k
    if (BType.isValType()) {
402
2.30k
      return RetT{{}, {BType.getValType()}};
403
12.8k
    } else {
404
      // Type index case. t2* = type[index].returns
405
12.8k
      const uint32_t TypeIdx = BType.getTypeIndex();
406
12.8k
      const auto &FType = CompositeTypes[TypeIdx]->getFuncType();
407
12.8k
      return RetT{
408
12.8k
          VecT(FType.getParamTypes().begin(), FType.getParamTypes().end()),
409
12.8k
          VecT(FType.getReturnTypes().begin(), FType.getReturnTypes().end())};
410
12.8k
    }
411
15.2k
  }
412
};
413
414
namespace {
415
416
using namespace WasmEdge;
417
418
31.6k
static bool isVoidReturn(Span<const ValType> ValTypes) noexcept {
419
31.6k
  return ValTypes.empty();
420
31.6k
}
421
422
static LLVM::Type toLLVMType(LLVM::Context LLContext,
423
2.05M
                             const ValType &ValType) noexcept {
424
2.05M
  switch (ValType.getCode()) {
425
60.1k
  case TypeCode::I32:
426
60.1k
    return LLContext.getInt32Ty();
427
219k
  case TypeCode::I64:
428
219k
    return LLContext.getInt64Ty();
429
0
  case TypeCode::Ref:
430
22.4k
  case TypeCode::RefNull:
431
1.70M
  case TypeCode::V128:
432
1.70M
    return LLVM::Type::getVectorType(LLContext.getInt64Ty(), 2);
433
50.5k
  case TypeCode::F32:
434
50.5k
    return LLContext.getFloatTy();
435
20.3k
  case TypeCode::F64:
436
20.3k
    return LLContext.getDoubleTy();
437
0
  default:
438
0
    assumingUnreachable();
439
2.05M
  }
440
2.05M
}
441
442
static std::vector<LLVM::Type>
443
toLLVMTypeVector(LLVM::Context LLContext,
444
18.0k
                 Span<const ValType> ValTypes) noexcept {
445
18.0k
  std::vector<LLVM::Type> Result;
446
18.0k
  Result.reserve(ValTypes.size());
447
18.0k
  for (const auto &Type : ValTypes) {
448
17.8k
    Result.push_back(toLLVMType(LLContext, Type));
449
17.8k
  }
450
18.0k
  return Result;
451
18.0k
}
452
453
static std::vector<LLVM::Type>
454
toLLVMArgsType(LLVM::Context LLContext, LLVM::Type ExecCtxPtrTy,
455
14.4k
               Span<const ValType> ValTypes) noexcept {
456
14.4k
  auto Result = toLLVMTypeVector(LLContext, ValTypes);
457
14.4k
  Result.insert(Result.begin(), ExecCtxPtrTy);
458
14.4k
  return Result;
459
14.4k
}
460
461
static LLVM::Type toLLVMRetsType(LLVM::Context LLContext,
462
14.4k
                                 Span<const ValType> ValTypes) noexcept {
463
14.4k
  if (isVoidReturn(ValTypes)) {
464
3.44k
    return LLContext.getVoidTy();
465
3.44k
  }
466
11.0k
  if (ValTypes.size() == 1) {
467
10.4k
    return toLLVMType(LLContext, ValTypes.front());
468
10.4k
  }
469
623
  std::vector<LLVM::Type> Result;
470
623
  Result.reserve(ValTypes.size());
471
1.71k
  for (const auto &Type : ValTypes) {
472
1.71k
    Result.push_back(toLLVMType(LLContext, Type));
473
1.71k
  }
474
623
  return LLVM::Type::getStructType(Result);
475
11.0k
}
476
477
static LLVM::Type toLLVMType(LLVM::Context LLContext, LLVM::Type ExecCtxPtrTy,
478
14.4k
                             const AST::FunctionType &FuncType) noexcept {
479
14.4k
  auto ArgsTy =
480
14.4k
      toLLVMArgsType(LLContext, ExecCtxPtrTy, FuncType.getParamTypes());
481
14.4k
  auto RetTy = toLLVMRetsType(LLContext, FuncType.getReturnTypes());
482
14.4k
  return LLVM::Type::getFunctionType(RetTy, ArgsTy);
483
14.4k
}
484
485
static LLVM::Value toLLVMConstantZero(LLVM::Context LLContext,
486
2.02M
                                      const ValType &ValType) noexcept {
487
2.02M
  switch (ValType.getCode()) {
488
43.0k
  case TypeCode::I32:
489
43.0k
    return LLVM::Value::getConstNull(LLContext.getInt32Ty());
490
216k
  case TypeCode::I64:
491
216k
    return LLVM::Value::getConstNull(LLContext.getInt64Ty());
492
0
  case TypeCode::Ref:
493
21.9k
  case TypeCode::RefNull: {
494
21.9k
    std::array<uint8_t, 16> Data{};
495
21.9k
    const auto Raw = ValType.getRawData();
496
21.9k
    std::copy(Raw.begin(), Raw.end(), Data.begin());
497
21.9k
    return LLVM::Value::getConstVector8(LLContext, Data);
498
0
  }
499
1.67M
  case TypeCode::V128:
500
1.67M
    return LLVM::Value::getConstNull(
501
1.67M
        LLVM::Type::getVectorType(LLContext.getInt64Ty(), 2));
502
48.3k
  case TypeCode::F32:
503
48.3k
    return LLVM::Value::getConstNull(LLContext.getFloatTy());
504
17.7k
  case TypeCode::F64:
505
17.7k
    return LLVM::Value::getConstNull(LLContext.getDoubleTy());
506
0
  default:
507
0
    assumingUnreachable();
508
2.02M
  }
509
2.02M
}
510
511
class FunctionCompiler {
512
  struct Control;
513
514
public:
515
  FunctionCompiler(LLVM::Compiler::CompileContext &Context,
516
                   LLVM::FunctionCallee F, Span<const ValType> Locals,
517
                   bool Interruptible, bool InstructionCounting,
518
                   bool GasMeasuring) noexcept
519
9.82k
      : Context(Context), LLContext(Context.LLContext),
520
9.82k
        Interruptible(Interruptible), F(F), Builder(LLContext) {
521
9.82k
    if (F.Fn) {
522
9.82k
      Builder.positionAtEnd(LLVM::BasicBlock::create(LLContext, F.Fn, "entry"));
523
9.82k
      ExecCtx = Builder.createLoad(Context.ExecCtxTy, F.Fn.getFirstParam());
524
525
9.82k
      if (InstructionCounting) {
526
0
        LocalInstrCount = Builder.createAlloca(Context.Int64Ty);
527
0
        Builder.createStore(LLContext.getInt64(0), LocalInstrCount);
528
0
      }
529
530
9.82k
      if (GasMeasuring) {
531
0
        LocalGas = Builder.createAlloca(Context.Int64Ty);
532
0
        Builder.createStore(LLContext.getInt64(0), LocalGas);
533
0
      }
534
535
18.8k
      for (LLVM::Value Arg = F.Fn.getFirstParam().getNextParam(); Arg;
536
9.82k
           Arg = Arg.getNextParam()) {
537
8.99k
        LLVM::Type Ty = Arg.getType();
538
8.99k
        LLVM::Value ArgPtr = Builder.createAlloca(Ty);
539
8.99k
        Builder.createStore(Arg, ArgPtr);
540
8.99k
        Local.emplace_back(Ty, ArgPtr);
541
8.99k
      }
542
543
2.02M
      for (const auto &Type : Locals) {
544
2.02M
        LLVM::Type Ty = toLLVMType(LLContext, Type);
545
2.02M
        LLVM::Value ArgPtr = Builder.createAlloca(Ty);
546
2.02M
        Builder.createStore(toLLVMConstantZero(LLContext, Type), ArgPtr);
547
2.02M
        Local.emplace_back(Ty, ArgPtr);
548
2.02M
      }
549
9.82k
    }
550
9.82k
  }
551
552
33.6k
  LLVM::BasicBlock getTrapBB(ErrCode::Value Error) noexcept {
553
33.6k
    if (auto Iter = TrapBB.find(Error); Iter != TrapBB.end()) {
554
30.6k
      return Iter->second;
555
30.6k
    }
556
2.93k
    auto BB = LLVM::BasicBlock::create(LLContext, F.Fn, "trap");
557
2.93k
    TrapBB.emplace(Error, BB);
558
2.93k
    return BB;
559
33.6k
  }
560
561
  void
562
  compile(const AST::CodeSegment &Code,
563
9.82k
          std::pair<std::vector<ValType>, std::vector<ValType>> Type) noexcept {
564
9.82k
    auto RetBB = LLVM::BasicBlock::create(LLContext, F.Fn, "ret");
565
9.82k
    Type.first.clear();
566
9.82k
    enterBlock(RetBB, {}, {}, {}, std::move(Type));
567
9.82k
    compile(Code.getExpr().getInstrs());
568
9.82k
    assuming(ControlStack.empty());
569
9.82k
    compileReturn();
570
571
9.82k
    for (auto &[Error, BB] : TrapBB) {
572
2.93k
      Builder.positionAtEnd(BB);
573
2.93k
      updateInstrCount();
574
2.93k
      updateGasAtTrap();
575
2.93k
      auto CallTrap = Builder.createCall(
576
2.93k
          Context.Trap, {LLContext.getInt32(static_cast<uint32_t>(Error))});
577
2.93k
      CallTrap.addCallSiteAttribute(Context.NoReturn);
578
2.93k
      Builder.createUnreachable();
579
2.93k
    }
580
9.82k
  }
581
582
9.82k
  void compile(AST::InstrView Instrs) noexcept {
583
1.63M
    auto Dispatch = [this](const AST::Instruction &Instr) -> void {
584
1.63M
      switch (Instr.getOpCode()) {
585
      // Control instructions (for blocks)
586
3.26k
      case OpCode::Block: {
587
3.26k
        auto Block = LLVM::BasicBlock::create(LLContext, F.Fn, "block");
588
3.26k
        auto EndBlock = LLVM::BasicBlock::create(LLContext, F.Fn, "block.end");
589
3.26k
        Builder.createBr(Block);
590
591
3.26k
        Builder.positionAtEnd(Block);
592
3.26k
        auto Type = Context.resolveBlockType(Instr.getBlockType());
593
3.26k
        const auto Arity = Type.first.size();
594
3.26k
        std::vector<LLVM::Value> Args(Arity);
595
3.26k
        if (isUnreachable()) {
596
1.04k
          for (size_t I = 0; I < Arity; ++I) {
597
341
            auto Ty = toLLVMType(LLContext, Type.first[I]);
598
341
            Args[I] = LLVM::Value::getUndef(Ty);
599
341
          }
600
2.56k
        } else {
601
3.00k
          for (size_t I = 0; I < Arity; ++I) {
602
445
            const size_t J = Arity - 1 - I;
603
445
            Args[J] = stackPop();
604
445
          }
605
2.56k
        }
606
3.26k
        enterBlock(EndBlock, {}, {}, std::move(Args), std::move(Type));
607
3.26k
        checkStop();
608
3.26k
        updateGas();
609
3.26k
        return;
610
0
      }
611
1.54k
      case OpCode::Loop: {
612
1.54k
        auto Curr = Builder.getInsertBlock();
613
1.54k
        auto Loop = LLVM::BasicBlock::create(LLContext, F.Fn, "loop");
614
1.54k
        auto EndLoop = LLVM::BasicBlock::create(LLContext, F.Fn, "loop.end");
615
1.54k
        Builder.createBr(Loop);
616
617
1.54k
        Builder.positionAtEnd(Loop);
618
1.54k
        auto Type = Context.resolveBlockType(Instr.getBlockType());
619
1.54k
        const auto Arity = Type.first.size();
620
1.54k
        std::vector<LLVM::Value> Args(Arity);
621
1.54k
        if (isUnreachable()) {
622
860
          for (size_t I = 0; I < Arity; ++I) {
623
377
            auto Ty = toLLVMType(LLContext, Type.first[I]);
624
377
            auto Value = LLVM::Value::getUndef(Ty);
625
377
            auto PHINode = Builder.createPHI(Ty);
626
377
            PHINode.addIncoming(Value, Curr);
627
377
            Args[I] = PHINode;
628
377
          }
629
1.05k
        } else {
630
1.53k
          for (size_t I = 0; I < Arity; ++I) {
631
477
            const size_t J = Arity - 1 - I;
632
477
            auto Value = stackPop();
633
477
            auto PHINode = Builder.createPHI(Value.getType());
634
477
            PHINode.addIncoming(Value, Curr);
635
477
            Args[J] = PHINode;
636
477
          }
637
1.05k
        }
638
1.54k
        enterBlock(Loop, EndLoop, {}, std::move(Args), std::move(Type));
639
1.54k
        checkStop();
640
1.54k
        updateGas();
641
1.54k
        return;
642
0
      }
643
2.57k
      case OpCode::If: {
644
2.57k
        auto Then = LLVM::BasicBlock::create(LLContext, F.Fn, "then");
645
2.57k
        auto Else = LLVM::BasicBlock::create(LLContext, F.Fn, "else");
646
2.57k
        auto EndIf = LLVM::BasicBlock::create(LLContext, F.Fn, "if.end");
647
2.57k
        LLVM::Value Cond;
648
2.57k
        if (isUnreachable()) {
649
522
          Cond = LLVM::Value::getUndef(LLContext.getInt1Ty());
650
2.04k
        } else {
651
2.04k
          Cond = Builder.createICmpNE(stackPop(), LLContext.getInt32(0));
652
2.04k
        }
653
2.57k
        Builder.createCondBr(Cond, Then, Else);
654
655
2.57k
        Builder.positionAtEnd(Then);
656
2.57k
        auto Type = Context.resolveBlockType(Instr.getBlockType());
657
2.57k
        const auto Arity = Type.first.size();
658
2.57k
        std::vector<LLVM::Value> Args(Arity);
659
2.57k
        if (isUnreachable()) {
660
1.05k
          for (size_t I = 0; I < Arity; ++I) {
661
531
            auto Ty = toLLVMType(LLContext, Type.first[I]);
662
531
            Args[I] = LLVM::Value::getUndef(Ty);
663
531
          }
664
2.04k
        } else {
665
2.84k
          for (size_t I = 0; I < Arity; ++I) {
666
794
            const size_t J = Arity - 1 - I;
667
794
            Args[J] = stackPop();
668
794
          }
669
2.04k
        }
670
2.57k
        enterBlock(EndIf, {}, Else, std::move(Args), std::move(Type));
671
2.57k
        return;
672
0
      }
673
17.1k
      case OpCode::End: {
674
17.1k
        auto Entry = leaveBlock();
675
17.1k
        if (Entry.ElseBlock) {
676
968
          auto Block = Builder.getInsertBlock();
677
968
          Builder.positionAtEnd(Entry.ElseBlock);
678
968
          enterBlock(Block, {}, {}, std::move(Entry.Args),
679
968
                     std::move(Entry.Type), std::move(Entry.ReturnPHI));
680
968
          Entry = leaveBlock();
681
968
        }
682
17.1k
        buildPHI(Entry.Type.second, Entry.ReturnPHI);
683
17.1k
        return;
684
0
      }
685
1.60k
      case OpCode::Else: {
686
1.60k
        auto Entry = leaveBlock();
687
1.60k
        Builder.positionAtEnd(Entry.ElseBlock);
688
1.60k
        enterBlock(Entry.JumpBlock, {}, {}, std::move(Entry.Args),
689
1.60k
                   std::move(Entry.Type), std::move(Entry.ReturnPHI));
690
1.60k
        return;
691
0
      }
692
1.61M
      default:
693
1.61M
        break;
694
1.63M
      }
695
696
1.61M
      if (isUnreachable()) {
697
517k
        return;
698
517k
      }
699
700
1.09M
      switch (Instr.getOpCode()) {
701
      // Control instructions
702
2.98k
      case OpCode::Unreachable:
703
2.98k
        Builder.createBr(getTrapBB(ErrCode::Value::Unreachable));
704
2.98k
        setUnreachable();
705
2.98k
        Builder.positionAtEnd(
706
2.98k
            LLVM::BasicBlock::create(LLContext, F.Fn, "unreachable.end"));
707
2.98k
        break;
708
42.7k
      case OpCode::Nop:
709
42.7k
        break;
710
      // LEGACY-EH: remove the `Try` cases after deprecating legacy EH.
711
      // case OpCode::Try:
712
      // case OpCode::Throw:
713
      // case OpCode::Throw_ref:
714
759
      case OpCode::Br: {
715
759
        const auto Label = Instr.getJump().TargetIndex;
716
759
        setLableJumpPHI(Label);
717
759
        Builder.createBr(getLabel(Label));
718
759
        setUnreachable();
719
759
        Builder.positionAtEnd(
720
759
            LLVM::BasicBlock::create(LLContext, F.Fn, "br.end"));
721
759
        break;
722
0
      }
723
352
      case OpCode::Br_if: {
724
352
        const auto Label = Instr.getJump().TargetIndex;
725
352
        auto Cond = Builder.createICmpNE(stackPop(), LLContext.getInt32(0));
726
352
        setLableJumpPHI(Label);
727
352
        auto Next = LLVM::BasicBlock::create(LLContext, F.Fn, "br_if.end");
728
352
        Builder.createCondBr(Cond, getLabel(Label), Next);
729
352
        Builder.positionAtEnd(Next);
730
352
        break;
731
0
      }
732
872
      case OpCode::Br_table: {
733
872
        auto LabelTable = Instr.getLabelList();
734
872
        assuming(LabelTable.size() <= std::numeric_limits<uint32_t>::max());
735
872
        const auto LabelTableSize =
736
872
            static_cast<uint32_t>(LabelTable.size() - 1);
737
872
        auto Value = stackPop();
738
872
        setLableJumpPHI(LabelTable[LabelTableSize].TargetIndex);
739
872
        auto Switch = Builder.createSwitch(
740
872
            Value, getLabel(LabelTable[LabelTableSize].TargetIndex),
741
872
            LabelTableSize);
742
20.1k
        for (uint32_t I = 0; I < LabelTableSize; ++I) {
743
19.3k
          setLableJumpPHI(LabelTable[I].TargetIndex);
744
19.3k
          Switch.addCase(LLContext.getInt32(I),
745
19.3k
                         getLabel(LabelTable[I].TargetIndex));
746
19.3k
        }
747
872
        setUnreachable();
748
872
        Builder.positionAtEnd(
749
872
            LLVM::BasicBlock::create(LLContext, F.Fn, "br_table.end"));
750
872
        break;
751
872
      }
752
0
      case OpCode::Br_on_null: {
753
0
        const auto Label = Instr.getJump().TargetIndex;
754
0
        auto Value = Builder.createBitCast(stackPop(), Context.Int64x2Ty);
755
0
        auto Cond = Builder.createICmpEQ(
756
0
            Builder.createExtractElement(Value, LLContext.getInt64(1)),
757
0
            LLContext.getInt64(0));
758
0
        setLableJumpPHI(Label);
759
0
        auto Next = LLVM::BasicBlock::create(LLContext, F.Fn, "br_on_null.end");
760
0
        Builder.createCondBr(Cond, getLabel(Label), Next);
761
0
        Builder.positionAtEnd(Next);
762
0
        stackPush(Value);
763
0
        break;
764
872
      }
765
0
      case OpCode::Br_on_non_null: {
766
0
        const auto Label = Instr.getJump().TargetIndex;
767
0
        auto Cond = Builder.createICmpNE(
768
0
            Builder.createExtractElement(
769
0
                Builder.createBitCast(Stack.back(), Context.Int64x2Ty),
770
0
                LLContext.getInt64(1)),
771
0
            LLContext.getInt64(0));
772
0
        setLableJumpPHI(Label);
773
0
        auto Next =
774
0
            LLVM::BasicBlock::create(LLContext, F.Fn, "br_on_non_null.end");
775
0
        Builder.createCondBr(Cond, getLabel(Label), Next);
776
0
        Builder.positionAtEnd(Next);
777
0
        stackPop();
778
0
        break;
779
872
      }
780
0
      case OpCode::Br_on_cast:
781
0
      case OpCode::Br_on_cast_fail: {
782
0
        auto Ref = Builder.createBitCast(Stack.back(), Context.Int64x2Ty);
783
0
        const auto Label = Instr.getBrCast().Jump.TargetIndex;
784
0
        std::array<uint8_t, 16> Buf = {0};
785
0
        std::copy_n(Instr.getBrCast().RType2.getRawData().cbegin(), 8,
786
0
                    Buf.begin());
787
0
        auto VType = Builder.createExtractElement(
788
0
            Builder.createBitCast(LLVM::Value::getConstVector8(LLContext, Buf),
789
0
                                  Context.Int64x2Ty),
790
0
            LLContext.getInt64(0));
791
0
        auto IsRefTest = Builder.createCall(
792
0
            Context.getIntrinsic(Builder, Executable::Intrinsics::kRefTest,
793
0
                                 LLVM::Type::getFunctionType(
794
0
                                     Context.Int32Ty,
795
0
                                     {Context.Int64x2Ty, Context.Int64Ty},
796
0
                                     false)),
797
0
            {Ref, VType});
798
0
        auto Cond =
799
0
            (Instr.getOpCode() == OpCode::Br_on_cast)
800
0
                ? Builder.createICmpNE(IsRefTest, LLContext.getInt32(0))
801
0
                : Builder.createICmpEQ(IsRefTest, LLContext.getInt32(0));
802
0
        setLableJumpPHI(Label);
803
0
        auto Next = LLVM::BasicBlock::create(LLContext, F.Fn, "br_on_cast.end");
804
0
        Builder.createCondBr(Cond, getLabel(Label), Next);
805
0
        Builder.positionAtEnd(Next);
806
0
        break;
807
0
      }
808
706
      case OpCode::Return:
809
706
        compileReturn();
810
706
        setUnreachable();
811
706
        Builder.positionAtEnd(
812
706
            LLVM::BasicBlock::create(LLContext, F.Fn, "ret.end"));
813
706
        break;
814
3.05k
      case OpCode::Call:
815
3.05k
        updateInstrCount();
816
3.05k
        updateGas();
817
3.05k
        compileCallOp(Instr.getTargetIndex());
818
3.05k
        break;
819
583
      case OpCode::Call_indirect:
820
583
        updateInstrCount();
821
583
        updateGas();
822
583
        compileIndirectCallOp(Instr.getSourceIndex(), Instr.getTargetIndex());
823
583
        break;
824
0
      case OpCode::Return_call:
825
0
        updateInstrCount();
826
0
        updateGas();
827
0
        compileReturnCallOp(Instr.getTargetIndex());
828
0
        setUnreachable();
829
0
        Builder.positionAtEnd(
830
0
            LLVM::BasicBlock::create(LLContext, F.Fn, "ret_call.end"));
831
0
        break;
832
0
      case OpCode::Return_call_indirect:
833
0
        updateInstrCount();
834
0
        updateGas();
835
0
        compileReturnIndirectCallOp(Instr.getSourceIndex(),
836
0
                                    Instr.getTargetIndex());
837
0
        setUnreachable();
838
0
        Builder.positionAtEnd(
839
0
            LLVM::BasicBlock::create(LLContext, F.Fn, "ret_call_indir.end"));
840
0
        break;
841
0
      case OpCode::Call_ref:
842
0
        updateInstrCount();
843
0
        updateGas();
844
0
        compileCallRefOp(Instr.getTargetIndex());
845
0
        break;
846
0
      case OpCode::Return_call_ref:
847
0
        updateInstrCount();
848
0
        updateGas();
849
0
        compileReturnCallRefOp(Instr.getTargetIndex());
850
0
        setUnreachable();
851
0
        Builder.positionAtEnd(
852
0
            LLVM::BasicBlock::create(LLContext, F.Fn, "ret_call_ref.end"));
853
0
        break;
854
        // LEGACY-EH: remove the `Catch` cases after deprecating legacy EH.
855
        // case OpCode::Catch:
856
        // case OpCode::Catch_all:
857
        // case OpCode::Try_table:
858
859
      // Reference Instructions
860
982
      case OpCode::Ref__null: {
861
982
        std::array<uint8_t, 16> Buf = {0};
862
        // For null references, the dynamic type down scaling is needed.
863
982
        ValType VType;
864
982
        if (Instr.getValType().isAbsHeapType()) {
865
982
          switch (Instr.getValType().getHeapTypeCode()) {
866
0
          case TypeCode::NullFuncRef:
867
389
          case TypeCode::FuncRef:
868
389
            VType = TypeCode::NullFuncRef;
869
389
            break;
870
0
          case TypeCode::NullExternRef:
871
593
          case TypeCode::ExternRef:
872
593
            VType = TypeCode::NullExternRef;
873
593
            break;
874
0
          case TypeCode::NullRef:
875
0
          case TypeCode::AnyRef:
876
0
          case TypeCode::EqRef:
877
0
          case TypeCode::I31Ref:
878
0
          case TypeCode::StructRef:
879
0
          case TypeCode::ArrayRef:
880
0
            VType = TypeCode::NullRef;
881
0
            break;
882
0
          default:
883
0
            assumingUnreachable();
884
982
          }
885
982
        } else {
886
0
          assuming(Instr.getValType().getTypeIndex() <
887
0
                   Context.CompositeTypes.size());
888
0
          const auto *CompType =
889
0
              Context.CompositeTypes[Instr.getValType().getTypeIndex()];
890
0
          assuming(CompType != nullptr);
891
0
          if (CompType->isFunc()) {
892
0
            VType = TypeCode::NullFuncRef;
893
0
          } else {
894
0
            VType = TypeCode::NullRef;
895
0
          }
896
0
        }
897
982
        std::copy_n(VType.getRawData().cbegin(), 8, Buf.begin());
898
982
        stackPush(Builder.createBitCast(
899
982
            LLVM::Value::getConstVector8(LLContext, Buf), Context.Int64x2Ty));
900
982
        break;
901
982
      }
902
457
      case OpCode::Ref__is_null:
903
457
        stackPush(Builder.createZExt(
904
457
            Builder.createICmpEQ(
905
457
                Builder.createExtractElement(
906
457
                    Builder.createBitCast(stackPop(), Context.Int64x2Ty),
907
457
                    LLContext.getInt64(1)),
908
457
                LLContext.getInt64(0)),
909
457
            Context.Int32Ty));
910
457
        break;
911
27
      case OpCode::Ref__func:
912
27
        stackPush(Builder.createCall(
913
27
            Context.getIntrinsic(Builder, Executable::Intrinsics::kRefFunc,
914
27
                                 LLVM::Type::getFunctionType(Context.Int64x2Ty,
915
27
                                                             {Context.Int32Ty},
916
27
                                                             false)),
917
27
            {LLContext.getInt32(Instr.getTargetIndex())}));
918
27
        break;
919
0
      case OpCode::Ref__eq: {
920
0
        LLVM::Value RHS = stackPop();
921
0
        LLVM::Value LHS = stackPop();
922
0
        stackPush(Builder.createZExt(
923
0
            Builder.createICmpEQ(
924
0
                Builder.createExtractElement(LHS, LLContext.getInt64(1)),
925
0
                Builder.createExtractElement(RHS, LLContext.getInt64(1))),
926
0
            Context.Int32Ty));
927
0
        break;
928
982
      }
929
0
      case OpCode::Ref__as_non_null: {
930
0
        auto Next =
931
0
            LLVM::BasicBlock::create(LLContext, F.Fn, "ref_as_non_null.ok");
932
0
        Stack.back() = Builder.createBitCast(Stack.back(), Context.Int64x2Ty);
933
0
        auto IsNotNull = Builder.createLikely(Builder.createICmpNE(
934
0
            Builder.createExtractElement(Stack.back(), LLContext.getInt64(1)),
935
0
            LLContext.getInt64(0)));
936
0
        Builder.createCondBr(IsNotNull, Next,
937
0
                             getTrapBB(ErrCode::Value::CastNullToNonNull));
938
0
        Builder.positionAtEnd(Next);
939
0
        break;
940
982
      }
941
942
      // Reference Instructions (GC proposal)
943
0
      case OpCode::Struct__new:
944
0
      case OpCode::Struct__new_default: {
945
0
        LLVM::Value Args = LLVM::Value::getConstPointerNull(Context.Int8PtrTy);
946
0
        assuming(Instr.getTargetIndex() < Context.CompositeTypes.size());
947
0
        const auto *CompType = Context.CompositeTypes[Instr.getTargetIndex()];
948
0
        assuming(CompType != nullptr && !CompType->isFunc());
949
0
        auto ArgSize = CompType->getFieldTypes().size();
950
0
        if (Instr.getOpCode() == OpCode::Struct__new) {
951
0
          std::vector<LLVM::Value> ArgsVec(ArgSize, nullptr);
952
0
          for (size_t I = 0; I < ArgSize; ++I) {
953
0
            ArgsVec[ArgSize - I - 1] = stackPop();
954
0
          }
955
0
          Args = Builder.createArray(ArgSize, kValSize);
956
0
          Builder.createArrayPtrStore(ArgsVec, Args, Context.Int8Ty, kValSize);
957
0
        } else {
958
0
          ArgSize = 0;
959
0
        }
960
0
        stackPush(Builder.createCall(
961
0
            Context.getIntrinsic(
962
0
                Builder, Executable::Intrinsics::kStructNew,
963
0
                LLVM::Type::getFunctionType(
964
0
                    Context.Int64x2Ty,
965
0
                    {Context.Int32Ty, Context.Int8PtrTy, Context.Int32Ty},
966
0
                    false)),
967
0
            {LLContext.getInt32(Instr.getTargetIndex()), Args,
968
0
             LLContext.getInt32(static_cast<uint32_t>(ArgSize))}));
969
0
        break;
970
0
      }
971
0
      case OpCode::Struct__get:
972
0
      case OpCode::Struct__get_u:
973
0
      case OpCode::Struct__get_s: {
974
0
        assuming(static_cast<size_t>(Instr.getTargetIndex()) <
975
0
                 Context.CompositeTypes.size());
976
0
        const auto *CompType = Context.CompositeTypes[Instr.getTargetIndex()];
977
0
        assuming(CompType != nullptr && !CompType->isFunc());
978
0
        assuming(static_cast<size_t>(Instr.getSourceIndex()) <
979
0
                 CompType->getFieldTypes().size());
980
0
        const auto &StorageType =
981
0
            CompType->getFieldTypes()[Instr.getSourceIndex()].getStorageType();
982
0
        auto Ref = stackPop();
983
0
        auto IsSigned = (Instr.getOpCode() == OpCode::Struct__get_s)
984
0
                            ? LLContext.getInt8(1)
985
0
                            : LLContext.getInt8(0);
986
0
        LLVM::Value Ret = Builder.createAlloca(Context.Int64x2Ty);
987
0
        Builder.createCall(
988
0
            Context.getIntrinsic(
989
0
                Builder, Executable::Intrinsics::kStructGet,
990
0
                LLVM::Type::getFunctionType(Context.VoidTy,
991
0
                                            {Context.Int64x2Ty, Context.Int32Ty,
992
0
                                             Context.Int32Ty, Context.Int8Ty,
993
0
                                             Context.Int8PtrTy},
994
0
                                            false)),
995
0
            {Ref, LLContext.getInt32(Instr.getTargetIndex()),
996
0
             LLContext.getInt32(Instr.getSourceIndex()), IsSigned, Ret});
997
998
0
        switch (StorageType.getCode()) {
999
0
        case TypeCode::I8:
1000
0
        case TypeCode::I16:
1001
0
        case TypeCode::I32: {
1002
0
          stackPush(Builder.createValuePtrLoad(Context.Int32Ty, Ret,
1003
0
                                               Context.Int64x2Ty));
1004
0
          break;
1005
0
        }
1006
0
        case TypeCode::I64: {
1007
0
          stackPush(Builder.createValuePtrLoad(Context.Int64Ty, Ret,
1008
0
                                               Context.Int64x2Ty));
1009
0
          break;
1010
0
        }
1011
0
        case TypeCode::F32: {
1012
0
          stackPush(Builder.createValuePtrLoad(Context.FloatTy, Ret,
1013
0
                                               Context.Int64x2Ty));
1014
0
          break;
1015
0
        }
1016
0
        case TypeCode::F64: {
1017
0
          stackPush(Builder.createValuePtrLoad(Context.DoubleTy, Ret,
1018
0
                                               Context.Int64x2Ty));
1019
0
          break;
1020
0
        }
1021
0
        case TypeCode::V128:
1022
0
        case TypeCode::Ref:
1023
0
        case TypeCode::RefNull: {
1024
0
          stackPush(Builder.createValuePtrLoad(Context.Int64x2Ty, Ret,
1025
0
                                               Context.Int64x2Ty));
1026
0
          break;
1027
0
        }
1028
0
        default:
1029
0
          assumingUnreachable();
1030
0
        }
1031
0
        break;
1032
0
      }
1033
0
      case OpCode::Struct__set: {
1034
0
        auto Val = stackPop();
1035
0
        auto Ref = stackPop();
1036
0
        LLVM::Value Arg = Builder.createAlloca(Context.Int64x2Ty);
1037
0
        Builder.createValuePtrStore(Val, Arg, Context.Int64x2Ty);
1038
0
        Builder.createCall(
1039
0
            Context.getIntrinsic(Builder, Executable::Intrinsics::kStructSet,
1040
0
                                 LLVM::Type::getFunctionType(
1041
0
                                     Context.VoidTy,
1042
0
                                     {Context.Int64x2Ty, Context.Int32Ty,
1043
0
                                      Context.Int32Ty, Context.Int8PtrTy},
1044
0
                                     false)),
1045
0
            {Ref, LLContext.getInt32(Instr.getTargetIndex()),
1046
0
             LLContext.getInt32(Instr.getSourceIndex()), Arg});
1047
0
        break;
1048
0
      }
1049
0
      case OpCode::Array__new: {
1050
0
        auto Length = stackPop();
1051
0
        auto Val = stackPop();
1052
0
        LLVM::Value Arg = Builder.createAlloca(Context.Int64x2Ty);
1053
0
        Builder.createValuePtrStore(Val, Arg, Context.Int64x2Ty);
1054
0
        stackPush(Builder.createCall(
1055
0
            Context.getIntrinsic(Builder, Executable::Intrinsics::kArrayNew,
1056
0
                                 LLVM::Type::getFunctionType(
1057
0
                                     Context.Int64x2Ty,
1058
0
                                     {Context.Int32Ty, Context.Int32Ty,
1059
0
                                      Context.Int8PtrTy, Context.Int32Ty},
1060
0
                                     false)),
1061
0
            {LLContext.getInt32(Instr.getTargetIndex()), Length, Arg,
1062
0
             LLContext.getInt32(1)}));
1063
0
        break;
1064
0
      }
1065
0
      case OpCode::Array__new_default: {
1066
0
        auto Length = stackPop();
1067
0
        LLVM::Value Arg = LLVM::Value::getConstPointerNull(Context.Int8PtrTy);
1068
0
        stackPush(Builder.createCall(
1069
0
            Context.getIntrinsic(Builder, Executable::Intrinsics::kArrayNew,
1070
0
                                 LLVM::Type::getFunctionType(
1071
0
                                     Context.Int64x2Ty,
1072
0
                                     {Context.Int32Ty, Context.Int32Ty,
1073
0
                                      Context.Int8PtrTy, Context.Int32Ty},
1074
0
                                     false)),
1075
0
            {LLContext.getInt32(Instr.getTargetIndex()), Length, Arg,
1076
0
             LLContext.getInt32(0)}));
1077
0
        break;
1078
0
      }
1079
0
      case OpCode::Array__new_fixed: {
1080
0
        const auto ArgSize = Instr.getSourceIndex();
1081
0
        std::vector<LLVM::Value> ArgsVec(ArgSize, nullptr);
1082
0
        for (size_t I = 0; I < ArgSize; ++I) {
1083
0
          ArgsVec[ArgSize - I - 1] = stackPop();
1084
0
        }
1085
0
        LLVM::Value Args = Builder.createArray(ArgSize, kValSize);
1086
0
        Builder.createArrayPtrStore(ArgsVec, Args, Context.Int8Ty, kValSize);
1087
0
        stackPush(Builder.createCall(
1088
0
            Context.getIntrinsic(Builder, Executable::Intrinsics::kArrayNew,
1089
0
                                 LLVM::Type::getFunctionType(
1090
0
                                     Context.Int64x2Ty,
1091
0
                                     {Context.Int32Ty, Context.Int32Ty,
1092
0
                                      Context.Int8PtrTy, Context.Int32Ty},
1093
0
                                     false)),
1094
0
            {LLContext.getInt32(Instr.getTargetIndex()),
1095
0
             LLContext.getInt32(ArgSize), Args, LLContext.getInt32(ArgSize)}));
1096
0
        break;
1097
0
      }
1098
0
      case OpCode::Array__new_data:
1099
0
      case OpCode::Array__new_elem: {
1100
0
        auto Length = stackPop();
1101
0
        auto Start = stackPop();
1102
0
        stackPush(Builder.createCall(
1103
0
            Context.getIntrinsic(
1104
0
                Builder,
1105
0
                ((Instr.getOpCode() == OpCode::Array__new_data)
1106
0
                     ? Executable::Intrinsics::kArrayNewData
1107
0
                     : Executable::Intrinsics::kArrayNewElem),
1108
0
                LLVM::Type::getFunctionType(Context.Int64x2Ty,
1109
0
                                            {Context.Int32Ty, Context.Int32Ty,
1110
0
                                             Context.Int32Ty, Context.Int32Ty},
1111
0
                                            false)),
1112
0
            {LLContext.getInt32(Instr.getTargetIndex()),
1113
0
             LLContext.getInt32(Instr.getSourceIndex()), Start, Length}));
1114
0
        break;
1115
0
      }
1116
0
      case OpCode::Array__get:
1117
0
      case OpCode::Array__get_u:
1118
0
      case OpCode::Array__get_s: {
1119
0
        assuming(static_cast<size_t>(Instr.getTargetIndex()) <
1120
0
                 Context.CompositeTypes.size());
1121
0
        const auto *CompType = Context.CompositeTypes[Instr.getTargetIndex()];
1122
0
        assuming(CompType != nullptr && !CompType->isFunc());
1123
0
        assuming(static_cast<size_t>(1) == CompType->getFieldTypes().size());
1124
0
        const auto &StorageType = CompType->getFieldTypes()[0].getStorageType();
1125
0
        auto Idx = stackPop();
1126
0
        auto Ref = stackPop();
1127
0
        auto IsSigned = (Instr.getOpCode() == OpCode::Array__get_s)
1128
0
                            ? LLContext.getInt8(1)
1129
0
                            : LLContext.getInt8(0);
1130
0
        LLVM::Value Ret = Builder.createAlloca(Context.Int64x2Ty);
1131
0
        Builder.createCall(
1132
0
            Context.getIntrinsic(
1133
0
                Builder, Executable::Intrinsics::kArrayGet,
1134
0
                LLVM::Type::getFunctionType(Context.VoidTy,
1135
0
                                            {Context.Int64x2Ty, Context.Int32Ty,
1136
0
                                             Context.Int32Ty, Context.Int8Ty,
1137
0
                                             Context.Int8PtrTy},
1138
0
                                            false)),
1139
0
            {Ref, LLContext.getInt32(Instr.getTargetIndex()), Idx, IsSigned,
1140
0
             Ret});
1141
1142
0
        switch (StorageType.getCode()) {
1143
0
        case TypeCode::I8:
1144
0
        case TypeCode::I16:
1145
0
        case TypeCode::I32: {
1146
0
          stackPush(Builder.createValuePtrLoad(Context.Int32Ty, Ret,
1147
0
                                               Context.Int64x2Ty));
1148
0
          break;
1149
0
        }
1150
0
        case TypeCode::I64: {
1151
0
          stackPush(Builder.createValuePtrLoad(Context.Int64Ty, Ret,
1152
0
                                               Context.Int64x2Ty));
1153
0
          break;
1154
0
        }
1155
0
        case TypeCode::F32: {
1156
0
          stackPush(Builder.createValuePtrLoad(Context.FloatTy, Ret,
1157
0
                                               Context.Int64x2Ty));
1158
0
          break;
1159
0
        }
1160
0
        case TypeCode::F64: {
1161
0
          stackPush(Builder.createValuePtrLoad(Context.DoubleTy, Ret,
1162
0
                                               Context.Int64x2Ty));
1163
0
          break;
1164
0
        }
1165
0
        case TypeCode::V128:
1166
0
        case TypeCode::Ref:
1167
0
        case TypeCode::RefNull: {
1168
0
          stackPush(Builder.createValuePtrLoad(Context.Int64x2Ty, Ret,
1169
0
                                               Context.Int64x2Ty));
1170
0
          break;
1171
0
        }
1172
0
        default:
1173
0
          assumingUnreachable();
1174
0
        }
1175
0
        break;
1176
0
      }
1177
0
      case OpCode::Array__set: {
1178
0
        auto Val = stackPop();
1179
0
        auto Idx = stackPop();
1180
0
        auto Ref = stackPop();
1181
0
        LLVM::Value Arg = Builder.createAlloca(Context.Int64x2Ty);
1182
0
        Builder.createValuePtrStore(Val, Arg, Context.Int64x2Ty);
1183
0
        Builder.createCall(
1184
0
            Context.getIntrinsic(Builder, Executable::Intrinsics::kArraySet,
1185
0
                                 LLVM::Type::getFunctionType(
1186
0
                                     Context.VoidTy,
1187
0
                                     {Context.Int64x2Ty, Context.Int32Ty,
1188
0
                                      Context.Int32Ty, Context.Int8PtrTy},
1189
0
                                     false)),
1190
0
            {Ref, LLContext.getInt32(Instr.getTargetIndex()), Idx, Arg});
1191
0
        break;
1192
0
      }
1193
0
      case OpCode::Array__len: {
1194
0
        auto Ref = stackPop();
1195
0
        stackPush(Builder.createCall(
1196
0
            Context.getIntrinsic(
1197
0
                Builder, Executable::Intrinsics::kArrayLen,
1198
0
                LLVM::Type::getFunctionType(Context.Int32Ty,
1199
0
                                            {Context.Int64x2Ty}, false)),
1200
0
            {Ref}));
1201
0
        break;
1202
0
      }
1203
0
      case OpCode::Array__fill: {
1204
0
        auto Cnt = stackPop();
1205
0
        auto Val = stackPop();
1206
0
        auto Off = stackPop();
1207
0
        auto Ref = stackPop();
1208
0
        LLVM::Value Arg = Builder.createAlloca(Context.Int64x2Ty);
1209
0
        Builder.createValuePtrStore(Val, Arg, Context.Int64x2Ty);
1210
0
        Builder.createCall(
1211
0
            Context.getIntrinsic(
1212
0
                Builder, Executable::Intrinsics::kArrayFill,
1213
0
                LLVM::Type::getFunctionType(Context.VoidTy,
1214
0
                                            {Context.Int64x2Ty, Context.Int32Ty,
1215
0
                                             Context.Int32Ty, Context.Int32Ty,
1216
0
                                             Context.Int8PtrTy},
1217
0
                                            false)),
1218
0
            {Ref, LLContext.getInt32(Instr.getTargetIndex()), Off, Cnt, Arg});
1219
0
        break;
1220
0
      }
1221
0
      case OpCode::Array__copy: {
1222
0
        auto Cnt = stackPop();
1223
0
        auto SrcOff = stackPop();
1224
0
        auto SrcRef = stackPop();
1225
0
        auto DstOff = stackPop();
1226
0
        auto DstRef = stackPop();
1227
0
        Builder.createCall(
1228
0
            Context.getIntrinsic(
1229
0
                Builder, Executable::Intrinsics::kArrayCopy,
1230
0
                LLVM::Type::getFunctionType(Context.VoidTy,
1231
0
                                            {Context.Int64x2Ty, Context.Int32Ty,
1232
0
                                             Context.Int32Ty, Context.Int64x2Ty,
1233
0
                                             Context.Int32Ty, Context.Int32Ty,
1234
0
                                             Context.Int32Ty},
1235
0
                                            false)),
1236
0
            {DstRef, LLContext.getInt32(Instr.getTargetIndex()), DstOff, SrcRef,
1237
0
             LLContext.getInt32(Instr.getSourceIndex()), SrcOff, Cnt});
1238
0
        break;
1239
0
      }
1240
0
      case OpCode::Array__init_data:
1241
0
      case OpCode::Array__init_elem: {
1242
0
        auto Cnt = stackPop();
1243
0
        auto SrcOff = stackPop();
1244
0
        auto DstOff = stackPop();
1245
0
        auto Ref = stackPop();
1246
0
        Builder.createCall(
1247
0
            Context.getIntrinsic(
1248
0
                Builder,
1249
0
                ((Instr.getOpCode() == OpCode::Array__init_data)
1250
0
                     ? Executable::Intrinsics::kArrayInitData
1251
0
                     : Executable::Intrinsics::kArrayInitElem),
1252
0
                LLVM::Type::getFunctionType(Context.VoidTy,
1253
0
                                            {Context.Int64x2Ty, Context.Int32Ty,
1254
0
                                             Context.Int32Ty, Context.Int32Ty,
1255
0
                                             Context.Int32Ty, Context.Int32Ty},
1256
0
                                            false)),
1257
0
            {Ref, LLContext.getInt32(Instr.getTargetIndex()),
1258
0
             LLContext.getInt32(Instr.getSourceIndex()), DstOff, SrcOff, Cnt});
1259
0
        break;
1260
0
      }
1261
0
      case OpCode::Ref__test:
1262
0
      case OpCode::Ref__test_null: {
1263
0
        auto Ref = stackPop();
1264
0
        std::array<uint8_t, 16> Buf = {0};
1265
0
        std::copy_n(Instr.getValType().getRawData().cbegin(), 8, Buf.begin());
1266
0
        auto VType = Builder.createExtractElement(
1267
0
            Builder.createBitCast(LLVM::Value::getConstVector8(LLContext, Buf),
1268
0
                                  Context.Int64x2Ty),
1269
0
            LLContext.getInt64(0));
1270
0
        stackPush(Builder.createCall(
1271
0
            Context.getIntrinsic(Builder, Executable::Intrinsics::kRefTest,
1272
0
                                 LLVM::Type::getFunctionType(
1273
0
                                     Context.Int32Ty,
1274
0
                                     {Context.Int64x2Ty, Context.Int64Ty},
1275
0
                                     false)),
1276
0
            {Ref, VType}));
1277
0
        break;
1278
0
      }
1279
0
      case OpCode::Ref__cast:
1280
0
      case OpCode::Ref__cast_null: {
1281
0
        auto Ref = stackPop();
1282
0
        std::array<uint8_t, 16> Buf = {0};
1283
0
        std::copy_n(Instr.getValType().getRawData().cbegin(), 8, Buf.begin());
1284
0
        auto VType = Builder.createExtractElement(
1285
0
            Builder.createBitCast(LLVM::Value::getConstVector8(LLContext, Buf),
1286
0
                                  Context.Int64x2Ty),
1287
0
            LLContext.getInt64(0));
1288
0
        stackPush(Builder.createCall(
1289
0
            Context.getIntrinsic(Builder, Executable::Intrinsics::kRefCast,
1290
0
                                 LLVM::Type::getFunctionType(
1291
0
                                     Context.Int64x2Ty,
1292
0
                                     {Context.Int64x2Ty, Context.Int64Ty},
1293
0
                                     false)),
1294
0
            {Ref, VType}));
1295
0
        break;
1296
0
      }
1297
0
      case OpCode::Any__convert_extern: {
1298
0
        std::array<uint8_t, 16> RawRef = {0};
1299
0
        auto Ref = stackPop();
1300
0
        auto PtrVal = Builder.createExtractElement(Ref, LLContext.getInt64(1));
1301
0
        auto IsNullBB =
1302
0
            LLVM::BasicBlock::create(LLContext, F.Fn, "any_conv_extern.null");
1303
0
        auto NotNullBB = LLVM::BasicBlock::create(LLContext, F.Fn,
1304
0
                                                  "any_conv_extern.not_null");
1305
0
        auto IsExtrefBB = LLVM::BasicBlock::create(LLContext, F.Fn,
1306
0
                                                   "any_conv_extern.is_extref");
1307
0
        auto EndBB =
1308
0
            LLVM::BasicBlock::create(LLContext, F.Fn, "any_conv_extern.end");
1309
0
        auto CondIsNull = Builder.createICmpEQ(PtrVal, LLContext.getInt64(0));
1310
0
        Builder.createCondBr(CondIsNull, IsNullBB, NotNullBB);
1311
1312
0
        Builder.positionAtEnd(IsNullBB);
1313
0
        auto VT = ValType(TypeCode::RefNull, TypeCode::NullRef);
1314
0
        std::copy_n(VT.getRawData().cbegin(), 8, RawRef.begin());
1315
0
        auto Ret1 = Builder.createBitCast(
1316
0
            LLVM::Value::getConstVector8(LLContext, RawRef), Context.Int64x2Ty);
1317
0
        Builder.createBr(EndBB);
1318
1319
0
        Builder.positionAtEnd(NotNullBB);
1320
0
        auto Ret2 = Builder.createBitCast(
1321
0
            Builder.createInsertElement(
1322
0
                Builder.createBitCast(Ref, Context.Int8x16Ty),
1323
0
                LLContext.getInt8(0), LLContext.getInt64(1)),
1324
0
            Context.Int64x2Ty);
1325
0
        auto HType = Builder.createExtractElement(
1326
0
            Builder.createBitCast(Ret2, Context.Int8x16Ty),
1327
0
            LLContext.getInt64(3));
1328
0
        auto CondIsExtref = Builder.createOr(
1329
0
            Builder.createICmpEQ(HType, LLContext.getInt8(static_cast<uint8_t>(
1330
0
                                            TypeCode::ExternRef))),
1331
0
            Builder.createICmpEQ(HType, LLContext.getInt8(static_cast<uint8_t>(
1332
0
                                            TypeCode::NullExternRef))));
1333
0
        Builder.createCondBr(CondIsExtref, IsExtrefBB, EndBB);
1334
1335
0
        Builder.positionAtEnd(IsExtrefBB);
1336
0
        VT = ValType(TypeCode::Ref, TypeCode::AnyRef);
1337
0
        std::copy_n(VT.getRawData().cbegin(), 8, RawRef.begin());
1338
0
        auto Ret3 = Builder.createInsertElement(
1339
0
            Builder.createBitCast(
1340
0
                LLVM::Value::getConstVector8(LLContext, RawRef),
1341
0
                Context.Int64x2Ty),
1342
0
            PtrVal, LLContext.getInt64(1));
1343
0
        Builder.createBr(EndBB);
1344
1345
0
        Builder.positionAtEnd(EndBB);
1346
0
        auto Ret = Builder.createPHI(Context.Int64x2Ty);
1347
0
        Ret.addIncoming(Ret1, IsNullBB);
1348
0
        Ret.addIncoming(Ret2, NotNullBB);
1349
0
        Ret.addIncoming(Ret3, IsExtrefBB);
1350
0
        stackPush(Ret);
1351
0
        break;
1352
0
      }
1353
0
      case OpCode::Extern__convert_any: {
1354
0
        std::array<uint8_t, 16> RawRef = {0};
1355
0
        auto Ref = stackPop();
1356
0
        auto IsNullBB =
1357
0
            LLVM::BasicBlock::create(LLContext, F.Fn, "extern_conv_any.null");
1358
0
        auto NotNullBB = LLVM::BasicBlock::create(LLContext, F.Fn,
1359
0
                                                  "extern_conv_any.not_null");
1360
0
        auto EndBB =
1361
0
            LLVM::BasicBlock::create(LLContext, F.Fn, "extern_conv_any.end");
1362
0
        auto CondIsNull = Builder.createICmpEQ(
1363
0
            Builder.createExtractElement(Ref, LLContext.getInt64(1)),
1364
0
            LLContext.getInt64(0));
1365
0
        Builder.createCondBr(CondIsNull, IsNullBB, NotNullBB);
1366
1367
0
        Builder.positionAtEnd(IsNullBB);
1368
0
        auto VT = ValType(TypeCode::RefNull, TypeCode::NullExternRef);
1369
0
        std::copy_n(VT.getRawData().cbegin(), 8, RawRef.begin());
1370
0
        auto Ret1 = Builder.createBitCast(
1371
0
            LLVM::Value::getConstVector8(LLContext, RawRef), Context.Int64x2Ty);
1372
0
        Builder.createBr(EndBB);
1373
1374
0
        Builder.positionAtEnd(NotNullBB);
1375
0
        auto Ret2 = Builder.createBitCast(
1376
0
            Builder.createInsertElement(
1377
0
                Builder.createBitCast(Ref, Context.Int8x16Ty),
1378
0
                LLContext.getInt8(1), LLContext.getInt64(1)),
1379
0
            Context.Int64x2Ty);
1380
0
        Builder.createBr(EndBB);
1381
1382
0
        Builder.positionAtEnd(EndBB);
1383
0
        auto Ret = Builder.createPHI(Context.Int64x2Ty);
1384
0
        Ret.addIncoming(Ret1, IsNullBB);
1385
0
        Ret.addIncoming(Ret2, NotNullBB);
1386
0
        stackPush(Ret);
1387
0
        break;
1388
0
      }
1389
0
      case OpCode::Ref__i31: {
1390
0
        std::array<uint8_t, 16> RawRef = {0};
1391
0
        auto VT = ValType(TypeCode::Ref, TypeCode::I31Ref);
1392
0
        std::copy_n(VT.getRawData().cbegin(), 8, RawRef.begin());
1393
0
        auto Ref = Builder.createBitCast(
1394
0
            LLVM::Value::getConstVector8(LLContext, RawRef), Context.Int64x2Ty);
1395
0
        auto Val = Builder.createZExt(
1396
0
            Builder.createOr(
1397
0
                Builder.createAnd(stackPop(), LLContext.getInt32(0x7FFFFFFFU)),
1398
0
                LLContext.getInt32(0x80000000U)),
1399
0
            Context.Int64Ty);
1400
0
        stackPush(Builder.createInsertElement(Ref, Val, LLContext.getInt64(1)));
1401
0
        break;
1402
0
      }
1403
0
      case OpCode::I31__get_s: {
1404
0
        auto Next = LLVM::BasicBlock::create(LLContext, F.Fn, "i31.get.ok");
1405
0
        auto Ref = Builder.createBitCast(stackPop(), Context.Int64x2Ty);
1406
0
        auto Val = Builder.createTrunc(
1407
0
            Builder.createExtractElement(Ref, LLContext.getInt64(1)),
1408
0
            Context.Int32Ty);
1409
0
        auto IsNotNull = Builder.createLikely(Builder.createICmpNE(
1410
0
            Builder.createAnd(Val, LLContext.getInt32(0x80000000U)),
1411
0
            LLContext.getInt32(0)));
1412
0
        Builder.createCondBr(IsNotNull, Next,
1413
0
                             getTrapBB(ErrCode::Value::AccessNullI31));
1414
0
        Builder.positionAtEnd(Next);
1415
0
        Val = Builder.createAnd(Val, LLContext.getInt32(0x7FFFFFFFU));
1416
0
        stackPush(Builder.createOr(
1417
0
            Val, Builder.createShl(
1418
0
                     Builder.createAnd(Val, LLContext.getInt32(0x40000000U)),
1419
0
                     LLContext.getInt32(1))));
1420
0
        break;
1421
0
      }
1422
0
      case OpCode::I31__get_u: {
1423
0
        auto Next = LLVM::BasicBlock::create(LLContext, F.Fn, "i31.get.ok");
1424
0
        auto Ref = Builder.createBitCast(stackPop(), Context.Int64x2Ty);
1425
0
        auto Val = Builder.createTrunc(
1426
0
            Builder.createExtractElement(Ref, LLContext.getInt64(1)),
1427
0
            Context.Int32Ty);
1428
0
        auto IsNotNull = Builder.createLikely(Builder.createICmpNE(
1429
0
            Builder.createAnd(Val, LLContext.getInt32(0x80000000U)),
1430
0
            LLContext.getInt32(0)));
1431
0
        Builder.createCondBr(IsNotNull, Next,
1432
0
                             getTrapBB(ErrCode::Value::AccessNullI31));
1433
0
        Builder.positionAtEnd(Next);
1434
0
        stackPush(Builder.createAnd(Val, LLContext.getInt32(0x7FFFFFFFU)));
1435
0
        break;
1436
0
      }
1437
1438
      // Parametric Instructions
1439
3.21k
      case OpCode::Drop:
1440
3.21k
        stackPop();
1441
3.21k
        break;
1442
697
      case OpCode::Select:
1443
1.14k
      case OpCode::Select_t: {
1444
1.14k
        auto Cond = Builder.createICmpNE(stackPop(), LLContext.getInt32(0));
1445
1.14k
        auto False = stackPop();
1446
1.14k
        auto True = stackPop();
1447
1.14k
        stackPush(Builder.createSelect(Cond, True, False));
1448
1.14k
        break;
1449
697
      }
1450
1451
      // Variable Instructions
1452
10.7k
      case OpCode::Local__get: {
1453
10.7k
        const auto &L = Local[Instr.getTargetIndex()];
1454
10.7k
        stackPush(Builder.createLoad(L.first, L.second));
1455
10.7k
        break;
1456
697
      }
1457
4.48k
      case OpCode::Local__set:
1458
4.48k
        Builder.createStore(stackPop(), Local[Instr.getTargetIndex()].second);
1459
4.48k
        break;
1460
771
      case OpCode::Local__tee:
1461
771
        Builder.createStore(Stack.back(), Local[Instr.getTargetIndex()].second);
1462
771
        break;
1463
256
      case OpCode::Global__get: {
1464
256
        const auto G =
1465
256
            Context.getGlobal(Builder, ExecCtx, Instr.getTargetIndex());
1466
256
        stackPush(Builder.createLoad(G.first, G.second));
1467
256
        break;
1468
697
      }
1469
50
      case OpCode::Global__set:
1470
50
        Builder.createStore(
1471
50
            stackPop(),
1472
50
            Context.getGlobal(Builder, ExecCtx, Instr.getTargetIndex()).second);
1473
50
        break;
1474
1475
      // Table Instructions
1476
36
      case OpCode::Table__get: {
1477
36
        auto Idx = stackPop();
1478
36
        stackPush(Builder.createCall(
1479
36
            Context.getIntrinsic(
1480
36
                Builder, Executable::Intrinsics::kTableGet,
1481
36
                LLVM::Type::getFunctionType(Context.Int64x2Ty,
1482
36
                                            {Context.Int32Ty, Context.Int32Ty},
1483
36
                                            false)),
1484
36
            {LLContext.getInt32(Instr.getTargetIndex()), Idx}));
1485
36
        break;
1486
697
      }
1487
29
      case OpCode::Table__set: {
1488
29
        auto Ref = stackPop();
1489
29
        auto Idx = stackPop();
1490
29
        Builder.createCall(
1491
29
            Context.getIntrinsic(
1492
29
                Builder, Executable::Intrinsics::kTableSet,
1493
29
                LLVM::Type::getFunctionType(
1494
29
                    Context.Int64Ty,
1495
29
                    {Context.Int32Ty, Context.Int32Ty, Context.Int64x2Ty},
1496
29
                    false)),
1497
29
            {LLContext.getInt32(Instr.getTargetIndex()), Idx, Ref});
1498
29
        break;
1499
697
      }
1500
27
      case OpCode::Table__init: {
1501
27
        auto Len = stackPop();
1502
27
        auto Src = stackPop();
1503
27
        auto Dst = stackPop();
1504
27
        Builder.createCall(
1505
27
            Context.getIntrinsic(
1506
27
                Builder, Executable::Intrinsics::kTableInit,
1507
27
                LLVM::Type::getFunctionType(Context.VoidTy,
1508
27
                                            {Context.Int32Ty, Context.Int32Ty,
1509
27
                                             Context.Int32Ty, Context.Int32Ty,
1510
27
                                             Context.Int32Ty},
1511
27
                                            false)),
1512
27
            {LLContext.getInt32(Instr.getTargetIndex()),
1513
27
             LLContext.getInt32(Instr.getSourceIndex()), Dst, Src, Len});
1514
27
        break;
1515
697
      }
1516
35
      case OpCode::Elem__drop: {
1517
35
        Builder.createCall(
1518
35
            Context.getIntrinsic(Builder, Executable::Intrinsics::kElemDrop,
1519
35
                                 LLVM::Type::getFunctionType(
1520
35
                                     Context.VoidTy, {Context.Int32Ty}, false)),
1521
35
            {LLContext.getInt32(Instr.getTargetIndex())});
1522
35
        break;
1523
697
      }
1524
21
      case OpCode::Table__copy: {
1525
21
        auto Len = stackPop();
1526
21
        auto Src = stackPop();
1527
21
        auto Dst = stackPop();
1528
21
        Builder.createCall(
1529
21
            Context.getIntrinsic(
1530
21
                Builder, Executable::Intrinsics::kTableCopy,
1531
21
                LLVM::Type::getFunctionType(Context.VoidTy,
1532
21
                                            {Context.Int32Ty, Context.Int32Ty,
1533
21
                                             Context.Int32Ty, Context.Int32Ty,
1534
21
                                             Context.Int32Ty},
1535
21
                                            false)),
1536
21
            {LLContext.getInt32(Instr.getTargetIndex()),
1537
21
             LLContext.getInt32(Instr.getSourceIndex()), Dst, Src, Len});
1538
21
        break;
1539
697
      }
1540
14
      case OpCode::Table__grow: {
1541
14
        auto NewSize = stackPop();
1542
14
        auto Val = stackPop();
1543
14
        stackPush(Builder.createCall(
1544
14
            Context.getIntrinsic(
1545
14
                Builder, Executable::Intrinsics::kTableGrow,
1546
14
                LLVM::Type::getFunctionType(
1547
14
                    Context.Int32Ty,
1548
14
                    {Context.Int32Ty, Context.Int64x2Ty, Context.Int32Ty},
1549
14
                    false)),
1550
14
            {LLContext.getInt32(Instr.getTargetIndex()), Val, NewSize}));
1551
14
        break;
1552
697
      }
1553
17
      case OpCode::Table__size: {
1554
17
        stackPush(Builder.createCall(
1555
17
            Context.getIntrinsic(Builder, Executable::Intrinsics::kTableSize,
1556
17
                                 LLVM::Type::getFunctionType(Context.Int32Ty,
1557
17
                                                             {Context.Int32Ty},
1558
17
                                                             false)),
1559
17
            {LLContext.getInt32(Instr.getTargetIndex())}));
1560
17
        break;
1561
697
      }
1562
3
      case OpCode::Table__fill: {
1563
3
        auto Len = stackPop();
1564
3
        auto Val = stackPop();
1565
3
        auto Off = stackPop();
1566
3
        Builder.createCall(
1567
3
            Context.getIntrinsic(Builder, Executable::Intrinsics::kTableFill,
1568
3
                                 LLVM::Type::getFunctionType(
1569
3
                                     Context.Int32Ty,
1570
3
                                     {Context.Int32Ty, Context.Int32Ty,
1571
3
                                      Context.Int64x2Ty, Context.Int32Ty},
1572
3
                                     false)),
1573
3
            {LLContext.getInt32(Instr.getTargetIndex()), Off, Val, Len});
1574
3
        break;
1575
697
      }
1576
1577
      // Memory Instructions
1578
1.57k
      case OpCode::I32__load:
1579
1.57k
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1580
1.57k
                      Instr.getMemoryAlign(), Context.Int32Ty);
1581
1.57k
        break;
1582
3.85k
      case OpCode::I64__load:
1583
3.85k
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1584
3.85k
                      Instr.getMemoryAlign(), Context.Int64Ty);
1585
3.85k
        break;
1586
110
      case OpCode::F32__load:
1587
110
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1588
110
                      Instr.getMemoryAlign(), Context.FloatTy);
1589
110
        break;
1590
237
      case OpCode::F64__load:
1591
237
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1592
237
                      Instr.getMemoryAlign(), Context.DoubleTy);
1593
237
        break;
1594
642
      case OpCode::I32__load8_s:
1595
642
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1596
642
                      Instr.getMemoryAlign(), Context.Int8Ty, Context.Int32Ty,
1597
642
                      true);
1598
642
        break;
1599
167
      case OpCode::I32__load8_u:
1600
167
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1601
167
                      Instr.getMemoryAlign(), Context.Int8Ty, Context.Int32Ty,
1602
167
                      false);
1603
167
        break;
1604
373
      case OpCode::I32__load16_s:
1605
373
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1606
373
                      Instr.getMemoryAlign(), Context.Int16Ty, Context.Int32Ty,
1607
373
                      true);
1608
373
        break;
1609
1.86k
      case OpCode::I32__load16_u:
1610
1.86k
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1611
1.86k
                      Instr.getMemoryAlign(), Context.Int16Ty, Context.Int32Ty,
1612
1.86k
                      false);
1613
1.86k
        break;
1614
753
      case OpCode::I64__load8_s:
1615
753
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1616
753
                      Instr.getMemoryAlign(), Context.Int8Ty, Context.Int64Ty,
1617
753
                      true);
1618
753
        break;
1619
509
      case OpCode::I64__load8_u:
1620
509
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1621
509
                      Instr.getMemoryAlign(), Context.Int8Ty, Context.Int64Ty,
1622
509
                      false);
1623
509
        break;
1624
522
      case OpCode::I64__load16_s:
1625
522
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1626
522
                      Instr.getMemoryAlign(), Context.Int16Ty, Context.Int64Ty,
1627
522
                      true);
1628
522
        break;
1629
825
      case OpCode::I64__load16_u:
1630
825
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1631
825
                      Instr.getMemoryAlign(), Context.Int16Ty, Context.Int64Ty,
1632
825
                      false);
1633
825
        break;
1634
459
      case OpCode::I64__load32_s:
1635
459
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1636
459
                      Instr.getMemoryAlign(), Context.Int32Ty, Context.Int64Ty,
1637
459
                      true);
1638
459
        break;
1639
489
      case OpCode::I64__load32_u:
1640
489
        compileLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1641
489
                      Instr.getMemoryAlign(), Context.Int32Ty, Context.Int64Ty,
1642
489
                      false);
1643
489
        break;
1644
507
      case OpCode::I32__store:
1645
507
        compileStoreOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1646
507
                       Instr.getMemoryAlign(), Context.Int32Ty);
1647
507
        break;
1648
1.36k
      case OpCode::I64__store:
1649
1.36k
        compileStoreOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1650
1.36k
                       Instr.getMemoryAlign(), Context.Int64Ty);
1651
1.36k
        break;
1652
70
      case OpCode::F32__store:
1653
70
        compileStoreOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1654
70
                       Instr.getMemoryAlign(), Context.FloatTy);
1655
70
        break;
1656
45
      case OpCode::F64__store:
1657
45
        compileStoreOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1658
45
                       Instr.getMemoryAlign(), Context.DoubleTy);
1659
45
        break;
1660
383
      case OpCode::I32__store8:
1661
402
      case OpCode::I64__store8:
1662
402
        compileStoreOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1663
402
                       Instr.getMemoryAlign(), Context.Int8Ty, true);
1664
402
        break;
1665
264
      case OpCode::I32__store16:
1666
309
      case OpCode::I64__store16:
1667
309
        compileStoreOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1668
309
                       Instr.getMemoryAlign(), Context.Int16Ty, true);
1669
309
        break;
1670
36
      case OpCode::I64__store32:
1671
36
        compileStoreOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
1672
36
                       Instr.getMemoryAlign(), Context.Int32Ty, true);
1673
36
        break;
1674
978
      case OpCode::Memory__size:
1675
978
        stackPush(Builder.createCall(
1676
978
            Context.getIntrinsic(Builder, Executable::Intrinsics::kMemSize,
1677
978
                                 LLVM::Type::getFunctionType(Context.Int32Ty,
1678
978
                                                             {Context.Int32Ty},
1679
978
                                                             false)),
1680
978
            {LLContext.getInt32(Instr.getTargetIndex())}));
1681
978
        break;
1682
616
      case OpCode::Memory__grow: {
1683
616
        auto Diff = stackPop();
1684
616
        stackPush(Builder.createCall(
1685
616
            Context.getIntrinsic(
1686
616
                Builder, Executable::Intrinsics::kMemGrow,
1687
616
                LLVM::Type::getFunctionType(Context.Int32Ty,
1688
616
                                            {Context.Int32Ty, Context.Int32Ty},
1689
616
                                            false)),
1690
616
            {LLContext.getInt32(Instr.getTargetIndex()), Diff}));
1691
616
        break;
1692
264
      }
1693
25
      case OpCode::Memory__init: {
1694
25
        auto Len = stackPop();
1695
25
        auto Src = stackPop();
1696
25
        auto Dst = stackPop();
1697
25
        Builder.createCall(
1698
25
            Context.getIntrinsic(
1699
25
                Builder, Executable::Intrinsics::kMemInit,
1700
25
                LLVM::Type::getFunctionType(Context.VoidTy,
1701
25
                                            {Context.Int32Ty, Context.Int32Ty,
1702
25
                                             Context.Int32Ty, Context.Int32Ty,
1703
25
                                             Context.Int32Ty},
1704
25
                                            false)),
1705
25
            {LLContext.getInt32(Instr.getTargetIndex()),
1706
25
             LLContext.getInt32(Instr.getSourceIndex()), Dst, Src, Len});
1707
25
        break;
1708
264
      }
1709
22
      case OpCode::Data__drop: {
1710
22
        Builder.createCall(
1711
22
            Context.getIntrinsic(Builder, Executable::Intrinsics::kDataDrop,
1712
22
                                 LLVM::Type::getFunctionType(
1713
22
                                     Context.VoidTy, {Context.Int32Ty}, false)),
1714
22
            {LLContext.getInt32(Instr.getTargetIndex())});
1715
22
        break;
1716
264
      }
1717
254
      case OpCode::Memory__copy: {
1718
254
        auto Len = stackPop();
1719
254
        auto Src = stackPop();
1720
254
        auto Dst = stackPop();
1721
254
        Builder.createCall(
1722
254
            Context.getIntrinsic(
1723
254
                Builder, Executable::Intrinsics::kMemCopy,
1724
254
                LLVM::Type::getFunctionType(Context.VoidTy,
1725
254
                                            {Context.Int32Ty, Context.Int32Ty,
1726
254
                                             Context.Int32Ty, Context.Int32Ty,
1727
254
                                             Context.Int32Ty},
1728
254
                                            false)),
1729
254
            {LLContext.getInt32(Instr.getTargetIndex()),
1730
254
             LLContext.getInt32(Instr.getSourceIndex()), Dst, Src, Len});
1731
254
        break;
1732
264
      }
1733
580
      case OpCode::Memory__fill: {
1734
580
        auto Len = stackPop();
1735
580
        auto Val = Builder.createTrunc(stackPop(), Context.Int8Ty);
1736
580
        auto Off = stackPop();
1737
580
        Builder.createCall(
1738
580
            Context.getIntrinsic(
1739
580
                Builder, Executable::Intrinsics::kMemFill,
1740
580
                LLVM::Type::getFunctionType(Context.VoidTy,
1741
580
                                            {Context.Int32Ty, Context.Int32Ty,
1742
580
                                             Context.Int8Ty, Context.Int32Ty},
1743
580
                                            false)),
1744
580
            {LLContext.getInt32(Instr.getTargetIndex()), Off, Val, Len});
1745
580
        break;
1746
264
      }
1747
1748
      // Const Numeric Instructions
1749
593k
      case OpCode::I32__const:
1750
593k
        stackPush(LLContext.getInt32(Instr.getNum().get<uint32_t>()));
1751
593k
        break;
1752
94.6k
      case OpCode::I64__const:
1753
94.6k
        stackPush(LLContext.getInt64(Instr.getNum().get<uint64_t>()));
1754
94.6k
        break;
1755
16.1k
      case OpCode::F32__const:
1756
16.1k
        stackPush(LLContext.getFloat(Instr.getNum().get<float>()));
1757
16.1k
        break;
1758
7.04k
      case OpCode::F64__const:
1759
7.04k
        stackPush(LLContext.getDouble(Instr.getNum().get<double>()));
1760
7.04k
        break;
1761
1762
      // Unary Numeric Instructions
1763
7.50k
      case OpCode::I32__eqz:
1764
7.50k
        stackPush(Builder.createZExt(
1765
7.50k
            Builder.createICmpEQ(stackPop(), LLContext.getInt32(0)),
1766
7.50k
            Context.Int32Ty));
1767
7.50k
        break;
1768
1.22k
      case OpCode::I64__eqz:
1769
1.22k
        stackPush(Builder.createZExt(
1770
1.22k
            Builder.createICmpEQ(stackPop(), LLContext.getInt64(0)),
1771
1.22k
            Context.Int32Ty));
1772
1.22k
        break;
1773
2.22k
      case OpCode::I32__clz:
1774
2.22k
        assuming(LLVM::Core::Ctlz != LLVM::Core::NotIntrinsic);
1775
2.22k
        stackPush(Builder.createIntrinsic(LLVM::Core::Ctlz, {Context.Int32Ty},
1776
2.22k
                                          {stackPop(), LLContext.getFalse()}));
1777
2.22k
        break;
1778
862
      case OpCode::I64__clz:
1779
862
        assuming(LLVM::Core::Ctlz != LLVM::Core::NotIntrinsic);
1780
862
        stackPush(Builder.createIntrinsic(LLVM::Core::Ctlz, {Context.Int64Ty},
1781
862
                                          {stackPop(), LLContext.getFalse()}));
1782
862
        break;
1783
1.87k
      case OpCode::I32__ctz:
1784
1.87k
        assuming(LLVM::Core::Cttz != LLVM::Core::NotIntrinsic);
1785
1.87k
        stackPush(Builder.createIntrinsic(LLVM::Core::Cttz, {Context.Int32Ty},
1786
1.87k
                                          {stackPop(), LLContext.getFalse()}));
1787
1.87k
        break;
1788
450
      case OpCode::I64__ctz:
1789
450
        assuming(LLVM::Core::Cttz != LLVM::Core::NotIntrinsic);
1790
450
        stackPush(Builder.createIntrinsic(LLVM::Core::Cttz, {Context.Int64Ty},
1791
450
                                          {stackPop(), LLContext.getFalse()}));
1792
450
        break;
1793
15.5k
      case OpCode::I32__popcnt:
1794
17.5k
      case OpCode::I64__popcnt:
1795
17.5k
        assuming(LLVM::Core::Ctpop != LLVM::Core::NotIntrinsic);
1796
17.5k
        stackPush(Builder.createUnaryIntrinsic(LLVM::Core::Ctpop, stackPop()));
1797
17.5k
        break;
1798
1.35k
      case OpCode::F32__abs:
1799
2.92k
      case OpCode::F64__abs:
1800
2.92k
        assuming(LLVM::Core::Fabs != LLVM::Core::NotIntrinsic);
1801
2.92k
        stackPush(Builder.createUnaryIntrinsic(LLVM::Core::Fabs, stackPop()));
1802
2.92k
        break;
1803
1.17k
      case OpCode::F32__neg:
1804
1.93k
      case OpCode::F64__neg:
1805
1.93k
        stackPush(Builder.createFNeg(stackPop()));
1806
1.93k
        break;
1807
2.12k
      case OpCode::F32__ceil:
1808
4.71k
      case OpCode::F64__ceil:
1809
4.71k
        assuming(LLVM::Core::Ceil != LLVM::Core::NotIntrinsic);
1810
4.71k
        stackPush(Builder.createUnaryIntrinsic(LLVM::Core::Ceil, stackPop()));
1811
4.71k
        break;
1812
1.01k
      case OpCode::F32__floor:
1813
1.39k
      case OpCode::F64__floor:
1814
1.39k
        assuming(LLVM::Core::Floor != LLVM::Core::NotIntrinsic);
1815
1.39k
        stackPush(Builder.createUnaryIntrinsic(LLVM::Core::Floor, stackPop()));
1816
1.39k
        break;
1817
557
      case OpCode::F32__trunc:
1818
877
      case OpCode::F64__trunc:
1819
877
        assuming(LLVM::Core::Trunc != LLVM::Core::NotIntrinsic);
1820
877
        stackPush(Builder.createUnaryIntrinsic(LLVM::Core::Trunc, stackPop()));
1821
877
        break;
1822
930
      case OpCode::F32__nearest:
1823
1.29k
      case OpCode::F64__nearest: {
1824
1.29k
        const bool IsFloat = Instr.getOpCode() == OpCode::F32__nearest;
1825
1.29k
        LLVM::Value Value = stackPop();
1826
1827
1.29k
#if LLVM_VERSION_MAJOR >= 12 && !defined(__s390x__)
1828
1.29k
        assuming(LLVM::Core::Roundeven != LLVM::Core::NotIntrinsic);
1829
1.29k
        if (LLVM::Core::Roundeven != LLVM::Core::NotIntrinsic) {
1830
1.29k
          stackPush(Builder.createUnaryIntrinsic(LLVM::Core::Roundeven, Value));
1831
1.29k
          break;
1832
1.29k
        }
1833
0
#endif
1834
1835
        // The VectorSize is only used when SSE4_1 or NEON is supported.
1836
0
        [[maybe_unused]] const uint32_t VectorSize = IsFloat ? 4 : 2;
1837
0
#if defined(__x86_64__)
1838
0
        if (Context.SupportSSE4_1) {
1839
0
          auto Zero = LLContext.getInt64(0);
1840
0
          auto VectorTy =
1841
0
              LLVM::Type::getVectorType(Value.getType(), VectorSize);
1842
0
          LLVM::Value Ret = LLVM::Value::getUndef(VectorTy);
1843
0
          Ret = Builder.createInsertElement(Ret, Value, Zero);
1844
0
          auto ID = IsFloat ? LLVM::Core::X86SSE41RoundSs
1845
0
                            : LLVM::Core::X86SSE41RoundSd;
1846
0
          assuming(ID != LLVM::Core::NotIntrinsic);
1847
0
          Ret = Builder.createIntrinsic(ID, {},
1848
0
                                        {Ret, Ret, LLContext.getInt32(8)});
1849
0
          Ret = Builder.createExtractElement(Ret, Zero);
1850
0
          stackPush(Ret);
1851
0
          break;
1852
0
        }
1853
0
#endif
1854
1855
#if defined(__aarch64__)
1856
        if (Context.SupportNEON &&
1857
            LLVM::Core::AArch64NeonFRIntN != LLVM::Core::NotIntrinsic) {
1858
          auto Zero = LLContext.getInt64(0);
1859
          auto VectorTy =
1860
              LLVM::Type::getVectorType(Value.getType(), VectorSize);
1861
          LLVM::Value Ret = LLVM::Value::getUndef(VectorTy);
1862
          Ret = Builder.createInsertElement(Ret, Value, Zero);
1863
          Ret =
1864
              Builder.createUnaryIntrinsic(LLVM::Core::AArch64NeonFRIntN, Ret);
1865
          Ret = Builder.createExtractElement(Ret, Zero);
1866
          stackPush(Ret);
1867
          break;
1868
        }
1869
#endif
1870
1871
        // Fallback case.
1872
        // If the SSE4.1 is not supported on the x86_64 platform or
1873
        // the NEON is not supported on the aarch64 platform,
1874
        // then fallback to this.
1875
0
        assuming(LLVM::Core::Nearbyint != LLVM::Core::NotIntrinsic);
1876
0
        stackPush(Builder.createUnaryIntrinsic(LLVM::Core::Nearbyint, Value));
1877
0
        break;
1878
0
      }
1879
398
      case OpCode::F32__sqrt:
1880
2.41k
      case OpCode::F64__sqrt:
1881
2.41k
        assuming(LLVM::Core::Sqrt != LLVM::Core::NotIntrinsic);
1882
2.41k
        stackPush(Builder.createUnaryIntrinsic(LLVM::Core::Sqrt, stackPop()));
1883
2.41k
        break;
1884
450
      case OpCode::I32__wrap_i64:
1885
450
        stackPush(Builder.createTrunc(stackPop(), Context.Int32Ty));
1886
450
        break;
1887
1.47k
      case OpCode::I32__trunc_f32_s:
1888
1.47k
        compileSignedTrunc(Context.Int32Ty);
1889
1.47k
        break;
1890
272
      case OpCode::I32__trunc_f64_s:
1891
272
        compileSignedTrunc(Context.Int32Ty);
1892
272
        break;
1893
181
      case OpCode::I32__trunc_f32_u:
1894
181
        compileUnsignedTrunc(Context.Int32Ty);
1895
181
        break;
1896
1.61k
      case OpCode::I32__trunc_f64_u:
1897
1.61k
        compileUnsignedTrunc(Context.Int32Ty);
1898
1.61k
        break;
1899
2.35k
      case OpCode::I64__extend_i32_s:
1900
2.35k
        stackPush(Builder.createSExt(stackPop(), Context.Int64Ty));
1901
2.35k
        break;
1902
361
      case OpCode::I64__extend_i32_u:
1903
361
        stackPush(Builder.createZExt(stackPop(), Context.Int64Ty));
1904
361
        break;
1905
60
      case OpCode::I64__trunc_f32_s:
1906
60
        compileSignedTrunc(Context.Int64Ty);
1907
60
        break;
1908
424
      case OpCode::I64__trunc_f64_s:
1909
424
        compileSignedTrunc(Context.Int64Ty);
1910
424
        break;
1911
949
      case OpCode::I64__trunc_f32_u:
1912
949
        compileUnsignedTrunc(Context.Int64Ty);
1913
949
        break;
1914
1.26k
      case OpCode::I64__trunc_f64_u:
1915
1.26k
        compileUnsignedTrunc(Context.Int64Ty);
1916
1.26k
        break;
1917
1.78k
      case OpCode::F32__convert_i32_s:
1918
2.32k
      case OpCode::F32__convert_i64_s:
1919
2.32k
        stackPush(Builder.createSIToFP(stackPop(), Context.FloatTy));
1920
2.32k
        break;
1921
768
      case OpCode::F32__convert_i32_u:
1922
1.90k
      case OpCode::F32__convert_i64_u:
1923
1.90k
        stackPush(Builder.createUIToFP(stackPop(), Context.FloatTy));
1924
1.90k
        break;
1925
1.82k
      case OpCode::F64__convert_i32_s:
1926
6.34k
      case OpCode::F64__convert_i64_s:
1927
6.34k
        stackPush(Builder.createSIToFP(stackPop(), Context.DoubleTy));
1928
6.34k
        break;
1929
1.22k
      case OpCode::F64__convert_i32_u:
1930
1.43k
      case OpCode::F64__convert_i64_u:
1931
1.43k
        stackPush(Builder.createUIToFP(stackPop(), Context.DoubleTy));
1932
1.43k
        break;
1933
172
      case OpCode::F32__demote_f64:
1934
172
        stackPush(Builder.createFPTrunc(stackPop(), Context.FloatTy));
1935
172
        break;
1936
89
      case OpCode::F64__promote_f32:
1937
89
        stackPush(Builder.createFPExt(stackPop(), Context.DoubleTy));
1938
89
        break;
1939
776
      case OpCode::I32__reinterpret_f32:
1940
776
        stackPush(Builder.createBitCast(stackPop(), Context.Int32Ty));
1941
776
        break;
1942
782
      case OpCode::I64__reinterpret_f64:
1943
782
        stackPush(Builder.createBitCast(stackPop(), Context.Int64Ty));
1944
782
        break;
1945
4.38k
      case OpCode::F32__reinterpret_i32:
1946
4.38k
        stackPush(Builder.createBitCast(stackPop(), Context.FloatTy));
1947
4.38k
        break;
1948
1.38k
      case OpCode::F64__reinterpret_i64:
1949
1.38k
        stackPush(Builder.createBitCast(stackPop(), Context.DoubleTy));
1950
1.38k
        break;
1951
2.33k
      case OpCode::I32__extend8_s:
1952
2.33k
        stackPush(Builder.createSExt(
1953
2.33k
            Builder.createTrunc(stackPop(), Context.Int8Ty), Context.Int32Ty));
1954
2.33k
        break;
1955
3.62k
      case OpCode::I32__extend16_s:
1956
3.62k
        stackPush(Builder.createSExt(
1957
3.62k
            Builder.createTrunc(stackPop(), Context.Int16Ty), Context.Int32Ty));
1958
3.62k
        break;
1959
386
      case OpCode::I64__extend8_s:
1960
386
        stackPush(Builder.createSExt(
1961
386
            Builder.createTrunc(stackPop(), Context.Int8Ty), Context.Int64Ty));
1962
386
        break;
1963
665
      case OpCode::I64__extend16_s:
1964
665
        stackPush(Builder.createSExt(
1965
665
            Builder.createTrunc(stackPop(), Context.Int16Ty), Context.Int64Ty));
1966
665
        break;
1967
782
      case OpCode::I64__extend32_s:
1968
782
        stackPush(Builder.createSExt(
1969
782
            Builder.createTrunc(stackPop(), Context.Int32Ty), Context.Int64Ty));
1970
782
        break;
1971
1972
      // Binary Numeric Instructions
1973
1.21k
      case OpCode::I32__eq:
1974
1.47k
      case OpCode::I64__eq: {
1975
1.47k
        LLVM::Value RHS = stackPop();
1976
1.47k
        LLVM::Value LHS = stackPop();
1977
1.47k
        stackPush(Builder.createZExt(Builder.createICmpEQ(LHS, RHS),
1978
1.47k
                                     Context.Int32Ty));
1979
1.47k
        break;
1980
1.21k
      }
1981
680
      case OpCode::I32__ne:
1982
701
      case OpCode::I64__ne: {
1983
701
        LLVM::Value RHS = stackPop();
1984
701
        LLVM::Value LHS = stackPop();
1985
701
        stackPush(Builder.createZExt(Builder.createICmpNE(LHS, RHS),
1986
701
                                     Context.Int32Ty));
1987
701
        break;
1988
680
      }
1989
4.33k
      case OpCode::I32__lt_s:
1990
4.95k
      case OpCode::I64__lt_s: {
1991
4.95k
        LLVM::Value RHS = stackPop();
1992
4.95k
        LLVM::Value LHS = stackPop();
1993
4.95k
        stackPush(Builder.createZExt(Builder.createICmpSLT(LHS, RHS),
1994
4.95k
                                     Context.Int32Ty));
1995
4.95k
        break;
1996
4.33k
      }
1997
6.62k
      case OpCode::I32__lt_u:
1998
7.02k
      case OpCode::I64__lt_u: {
1999
7.02k
        LLVM::Value RHS = stackPop();
2000
7.02k
        LLVM::Value LHS = stackPop();
2001
7.02k
        stackPush(Builder.createZExt(Builder.createICmpULT(LHS, RHS),
2002
7.02k
                                     Context.Int32Ty));
2003
7.02k
        break;
2004
6.62k
      }
2005
1.36k
      case OpCode::I32__gt_s:
2006
1.80k
      case OpCode::I64__gt_s: {
2007
1.80k
        LLVM::Value RHS = stackPop();
2008
1.80k
        LLVM::Value LHS = stackPop();
2009
1.80k
        stackPush(Builder.createZExt(Builder.createICmpSGT(LHS, RHS),
2010
1.80k
                                     Context.Int32Ty));
2011
1.80k
        break;
2012
1.36k
      }
2013
8.55k
      case OpCode::I32__gt_u:
2014
8.83k
      case OpCode::I64__gt_u: {
2015
8.83k
        LLVM::Value RHS = stackPop();
2016
8.83k
        LLVM::Value LHS = stackPop();
2017
8.83k
        stackPush(Builder.createZExt(Builder.createICmpUGT(LHS, RHS),
2018
8.83k
                                     Context.Int32Ty));
2019
8.83k
        break;
2020
8.55k
      }
2021
2.44k
      case OpCode::I32__le_s:
2022
3.37k
      case OpCode::I64__le_s: {
2023
3.37k
        LLVM::Value RHS = stackPop();
2024
3.37k
        LLVM::Value LHS = stackPop();
2025
3.37k
        stackPush(Builder.createZExt(Builder.createICmpSLE(LHS, RHS),
2026
3.37k
                                     Context.Int32Ty));
2027
3.37k
        break;
2028
2.44k
      }
2029
749
      case OpCode::I32__le_u:
2030
2.36k
      case OpCode::I64__le_u: {
2031
2.36k
        LLVM::Value RHS = stackPop();
2032
2.36k
        LLVM::Value LHS = stackPop();
2033
2.36k
        stackPush(Builder.createZExt(Builder.createICmpULE(LHS, RHS),
2034
2.36k
                                     Context.Int32Ty));
2035
2.36k
        break;
2036
749
      }
2037
1.26k
      case OpCode::I32__ge_s:
2038
1.29k
      case OpCode::I64__ge_s: {
2039
1.29k
        LLVM::Value RHS = stackPop();
2040
1.29k
        LLVM::Value LHS = stackPop();
2041
1.29k
        stackPush(Builder.createZExt(Builder.createICmpSGE(LHS, RHS),
2042
1.29k
                                     Context.Int32Ty));
2043
1.29k
        break;
2044
1.26k
      }
2045
1.80k
      case OpCode::I32__ge_u:
2046
2.45k
      case OpCode::I64__ge_u: {
2047
2.45k
        LLVM::Value RHS = stackPop();
2048
2.45k
        LLVM::Value LHS = stackPop();
2049
2.45k
        stackPush(Builder.createZExt(Builder.createICmpUGE(LHS, RHS),
2050
2.45k
                                     Context.Int32Ty));
2051
2.45k
        break;
2052
1.80k
      }
2053
162
      case OpCode::F32__eq:
2054
215
      case OpCode::F64__eq: {
2055
215
        LLVM::Value RHS = stackPop();
2056
215
        LLVM::Value LHS = stackPop();
2057
215
        stackPush(Builder.createZExt(Builder.createFCmpOEQ(LHS, RHS),
2058
215
                                     Context.Int32Ty));
2059
215
        break;
2060
162
      }
2061
97
      case OpCode::F32__ne:
2062
124
      case OpCode::F64__ne: {
2063
124
        LLVM::Value RHS = stackPop();
2064
124
        LLVM::Value LHS = stackPop();
2065
124
        stackPush(Builder.createZExt(Builder.createFCmpUNE(LHS, RHS),
2066
124
                                     Context.Int32Ty));
2067
124
        break;
2068
97
      }
2069
204
      case OpCode::F32__lt:
2070
335
      case OpCode::F64__lt: {
2071
335
        LLVM::Value RHS = stackPop();
2072
335
        LLVM::Value LHS = stackPop();
2073
335
        stackPush(Builder.createZExt(Builder.createFCmpOLT(LHS, RHS),
2074
335
                                     Context.Int32Ty));
2075
335
        break;
2076
204
      }
2077
151
      case OpCode::F32__gt:
2078
278
      case OpCode::F64__gt: {
2079
278
        LLVM::Value RHS = stackPop();
2080
278
        LLVM::Value LHS = stackPop();
2081
278
        stackPush(Builder.createZExt(Builder.createFCmpOGT(LHS, RHS),
2082
278
                                     Context.Int32Ty));
2083
278
        break;
2084
151
      }
2085
75
      case OpCode::F32__le:
2086
193
      case OpCode::F64__le: {
2087
193
        LLVM::Value RHS = stackPop();
2088
193
        LLVM::Value LHS = stackPop();
2089
193
        stackPush(Builder.createZExt(Builder.createFCmpOLE(LHS, RHS),
2090
193
                                     Context.Int32Ty));
2091
193
        break;
2092
75
      }
2093
229
      case OpCode::F32__ge:
2094
257
      case OpCode::F64__ge: {
2095
257
        LLVM::Value RHS = stackPop();
2096
257
        LLVM::Value LHS = stackPop();
2097
257
        stackPush(Builder.createZExt(Builder.createFCmpOGE(LHS, RHS),
2098
257
                                     Context.Int32Ty));
2099
257
        break;
2100
229
      }
2101
722
      case OpCode::I32__add:
2102
1.25k
      case OpCode::I64__add: {
2103
1.25k
        LLVM::Value RHS = stackPop();
2104
1.25k
        LLVM::Value LHS = stackPop();
2105
1.25k
        stackPush(Builder.createAdd(LHS, RHS));
2106
1.25k
        break;
2107
722
      }
2108
1.78k
      case OpCode::I32__sub:
2109
2.36k
      case OpCode::I64__sub: {
2110
2.36k
        LLVM::Value RHS = stackPop();
2111
2.36k
        LLVM::Value LHS = stackPop();
2112
2113
2.36k
        stackPush(Builder.createSub(LHS, RHS));
2114
2.36k
        break;
2115
1.78k
      }
2116
611
      case OpCode::I32__mul:
2117
1.53k
      case OpCode::I64__mul: {
2118
1.53k
        LLVM::Value RHS = stackPop();
2119
1.53k
        LLVM::Value LHS = stackPop();
2120
1.53k
        stackPush(Builder.createMul(LHS, RHS));
2121
1.53k
        break;
2122
611
      }
2123
1.56k
      case OpCode::I32__div_s:
2124
2.06k
      case OpCode::I64__div_s: {
2125
2.06k
        LLVM::Value RHS = stackPop();
2126
2.06k
        LLVM::Value LHS = stackPop();
2127
2.06k
        if constexpr (kForceDivCheck) {
2128
2.06k
          const bool Is32 = Instr.getOpCode() == OpCode::I32__div_s;
2129
2.06k
          LLVM::Value IntZero =
2130
2.06k
              Is32 ? LLContext.getInt32(0) : LLContext.getInt64(0);
2131
2.06k
          LLVM::Value IntMinusOne =
2132
2.06k
              Is32 ? LLContext.getInt32(static_cast<uint32_t>(INT32_C(-1)))
2133
2.06k
                   : LLContext.getInt64(static_cast<uint64_t>(INT64_C(-1)));
2134
2.06k
          LLVM::Value IntMin = Is32 ? LLContext.getInt32(static_cast<uint32_t>(
2135
1.56k
                                          std::numeric_limits<int32_t>::min()))
2136
2.06k
                                    : LLContext.getInt64(static_cast<uint64_t>(
2137
504
                                          std::numeric_limits<int64_t>::min()));
2138
2139
2.06k
          auto NoZeroBB =
2140
2.06k
              LLVM::BasicBlock::create(LLContext, F.Fn, "div.nozero");
2141
2.06k
          auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "div.ok");
2142
2143
2.06k
          auto IsNotZero =
2144
2.06k
              Builder.createLikely(Builder.createICmpNE(RHS, IntZero));
2145
2.06k
          Builder.createCondBr(IsNotZero, NoZeroBB,
2146
2.06k
                               getTrapBB(ErrCode::Value::DivideByZero));
2147
2148
2.06k
          Builder.positionAtEnd(NoZeroBB);
2149
2.06k
          auto NotOverflow = Builder.createLikely(
2150
2.06k
              Builder.createOr(Builder.createICmpNE(LHS, IntMin),
2151
2.06k
                               Builder.createICmpNE(RHS, IntMinusOne)));
2152
2.06k
          Builder.createCondBr(NotOverflow, OkBB,
2153
2.06k
                               getTrapBB(ErrCode::Value::IntegerOverflow));
2154
2155
2.06k
          Builder.positionAtEnd(OkBB);
2156
2.06k
        }
2157
2.06k
        stackPush(Builder.createSDiv(LHS, RHS));
2158
2.06k
        break;
2159
1.56k
      }
2160
3.75k
      case OpCode::I32__div_u:
2161
4.07k
      case OpCode::I64__div_u: {
2162
4.07k
        LLVM::Value RHS = stackPop();
2163
4.07k
        LLVM::Value LHS = stackPop();
2164
4.07k
        if constexpr (kForceDivCheck) {
2165
4.07k
          const bool Is32 = Instr.getOpCode() == OpCode::I32__div_u;
2166
4.07k
          LLVM::Value IntZero =
2167
4.07k
              Is32 ? LLContext.getInt32(0) : LLContext.getInt64(0);
2168
4.07k
          auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "div.ok");
2169
2170
4.07k
          auto IsNotZero =
2171
4.07k
              Builder.createLikely(Builder.createICmpNE(RHS, IntZero));
2172
4.07k
          Builder.createCondBr(IsNotZero, OkBB,
2173
4.07k
                               getTrapBB(ErrCode::Value::DivideByZero));
2174
4.07k
          Builder.positionAtEnd(OkBB);
2175
4.07k
        }
2176
4.07k
        stackPush(Builder.createUDiv(LHS, RHS));
2177
4.07k
        break;
2178
3.75k
      }
2179
1.12k
      case OpCode::I32__rem_s:
2180
1.64k
      case OpCode::I64__rem_s: {
2181
1.64k
        LLVM::Value RHS = stackPop();
2182
1.64k
        LLVM::Value LHS = stackPop();
2183
        // handle INT32_MIN % -1
2184
1.64k
        const bool Is32 = Instr.getOpCode() == OpCode::I32__rem_s;
2185
1.64k
        LLVM::Value IntMinusOne =
2186
1.64k
            Is32 ? LLContext.getInt32(static_cast<uint32_t>(INT32_C(-1)))
2187
1.64k
                 : LLContext.getInt64(static_cast<uint64_t>(INT64_C(-1)));
2188
1.64k
        LLVM::Value IntMin = Is32 ? LLContext.getInt32(static_cast<uint32_t>(
2189
1.12k
                                        std::numeric_limits<int32_t>::min()))
2190
1.64k
                                  : LLContext.getInt64(static_cast<uint64_t>(
2191
527
                                        std::numeric_limits<int64_t>::min()));
2192
1.64k
        LLVM::Value IntZero =
2193
1.64k
            Is32 ? LLContext.getInt32(0) : LLContext.getInt64(0);
2194
2195
1.64k
        auto NoOverflowBB =
2196
1.64k
            LLVM::BasicBlock::create(LLContext, F.Fn, "no.overflow");
2197
1.64k
        auto EndBB = LLVM::BasicBlock::create(LLContext, F.Fn, "end.overflow");
2198
2199
1.64k
        if constexpr (kForceDivCheck) {
2200
1.64k
          auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "rem.ok");
2201
2202
1.64k
          auto IsNotZero =
2203
1.64k
              Builder.createLikely(Builder.createICmpNE(RHS, IntZero));
2204
1.64k
          Builder.createCondBr(IsNotZero, OkBB,
2205
1.64k
                               getTrapBB(ErrCode::Value::DivideByZero));
2206
1.64k
          Builder.positionAtEnd(OkBB);
2207
1.64k
        }
2208
2209
1.64k
        auto CurrBB = Builder.getInsertBlock();
2210
2211
1.64k
        auto NotOverflow = Builder.createLikely(
2212
1.64k
            Builder.createOr(Builder.createICmpNE(LHS, IntMin),
2213
1.64k
                             Builder.createICmpNE(RHS, IntMinusOne)));
2214
1.64k
        Builder.createCondBr(NotOverflow, NoOverflowBB, EndBB);
2215
2216
1.64k
        Builder.positionAtEnd(NoOverflowBB);
2217
1.64k
        auto Ret1 = Builder.createSRem(LHS, RHS);
2218
1.64k
        Builder.createBr(EndBB);
2219
2220
1.64k
        Builder.positionAtEnd(EndBB);
2221
1.64k
        auto Ret = Builder.createPHI(Ret1.getType());
2222
1.64k
        Ret.addIncoming(Ret1, NoOverflowBB);
2223
1.64k
        Ret.addIncoming(IntZero, CurrBB);
2224
2225
1.64k
        stackPush(Ret);
2226
1.64k
        break;
2227
1.12k
      }
2228
1.32k
      case OpCode::I32__rem_u:
2229
2.00k
      case OpCode::I64__rem_u: {
2230
2.00k
        LLVM::Value RHS = stackPop();
2231
2.00k
        LLVM::Value LHS = stackPop();
2232
2.00k
        if constexpr (kForceDivCheck) {
2233
2.00k
          LLVM::Value IntZero = Instr.getOpCode() == OpCode::I32__rem_u
2234
2.00k
                                    ? LLContext.getInt32(0)
2235
2.00k
                                    : LLContext.getInt64(0);
2236
2.00k
          auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "rem.ok");
2237
2238
2.00k
          auto IsNotZero =
2239
2.00k
              Builder.createLikely(Builder.createICmpNE(RHS, IntZero));
2240
2.00k
          Builder.createCondBr(IsNotZero, OkBB,
2241
2.00k
                               getTrapBB(ErrCode::Value::DivideByZero));
2242
2.00k
          Builder.positionAtEnd(OkBB);
2243
2.00k
        }
2244
2.00k
        stackPush(Builder.createURem(LHS, RHS));
2245
2.00k
        break;
2246
1.32k
      }
2247
680
      case OpCode::I32__and:
2248
2.03k
      case OpCode::I64__and: {
2249
2.03k
        LLVM::Value RHS = stackPop();
2250
2.03k
        LLVM::Value LHS = stackPop();
2251
2.03k
        stackPush(Builder.createAnd(LHS, RHS));
2252
2.03k
        break;
2253
680
      }
2254
1.47k
      case OpCode::I32__or:
2255
1.84k
      case OpCode::I64__or: {
2256
1.84k
        LLVM::Value RHS = stackPop();
2257
1.84k
        LLVM::Value LHS = stackPop();
2258
1.84k
        stackPush(Builder.createOr(LHS, RHS));
2259
1.84k
        break;
2260
1.47k
      }
2261
1.17k
      case OpCode::I32__xor:
2262
1.93k
      case OpCode::I64__xor: {
2263
1.93k
        LLVM::Value RHS = stackPop();
2264
1.93k
        LLVM::Value LHS = stackPop();
2265
1.93k
        stackPush(Builder.createXor(LHS, RHS));
2266
1.93k
        break;
2267
1.17k
      }
2268
2.11k
      case OpCode::I32__shl:
2269
2.92k
      case OpCode::I64__shl: {
2270
2.92k
        LLVM::Value Mask = Instr.getOpCode() == OpCode::I32__shl
2271
2.92k
                               ? LLContext.getInt32(31)
2272
2.92k
                               : LLContext.getInt64(63);
2273
2.92k
        LLVM::Value RHS = Builder.createAnd(stackPop(), Mask);
2274
2.92k
        LLVM::Value LHS = stackPop();
2275
2.92k
        stackPush(Builder.createShl(LHS, RHS));
2276
2.92k
        break;
2277
2.11k
      }
2278
1.80k
      case OpCode::I32__shr_s:
2279
2.28k
      case OpCode::I64__shr_s: {
2280
2.28k
        LLVM::Value Mask = Instr.getOpCode() == OpCode::I32__shr_s
2281
2.28k
                               ? LLContext.getInt32(31)
2282
2.28k
                               : LLContext.getInt64(63);
2283
2.28k
        LLVM::Value RHS = Builder.createAnd(stackPop(), Mask);
2284
2.28k
        LLVM::Value LHS = stackPop();
2285
2.28k
        stackPush(Builder.createAShr(LHS, RHS));
2286
2.28k
        break;
2287
1.80k
      }
2288
5.24k
      case OpCode::I32__shr_u:
2289
5.55k
      case OpCode::I64__shr_u: {
2290
5.55k
        LLVM::Value Mask = Instr.getOpCode() == OpCode::I32__shr_u
2291
5.55k
                               ? LLContext.getInt32(31)
2292
5.55k
                               : LLContext.getInt64(63);
2293
5.55k
        LLVM::Value RHS = Builder.createAnd(stackPop(), Mask);
2294
5.55k
        LLVM::Value LHS = stackPop();
2295
5.55k
        stackPush(Builder.createLShr(LHS, RHS));
2296
5.55k
        break;
2297
5.24k
      }
2298
2.78k
      case OpCode::I32__rotl: {
2299
2.78k
        LLVM::Value RHS = stackPop();
2300
2.78k
        LLVM::Value LHS = stackPop();
2301
2.78k
        assuming(LLVM::Core::FShl != LLVM::Core::NotIntrinsic);
2302
2.78k
        stackPush(Builder.createIntrinsic(LLVM::Core::FShl, {Context.Int32Ty},
2303
2.78k
                                          {LHS, LHS, RHS}));
2304
2.78k
        break;
2305
2.78k
      }
2306
978
      case OpCode::I32__rotr: {
2307
978
        LLVM::Value RHS = stackPop();
2308
978
        LLVM::Value LHS = stackPop();
2309
978
        assuming(LLVM::Core::FShr != LLVM::Core::NotIntrinsic);
2310
978
        stackPush(Builder.createIntrinsic(LLVM::Core::FShr, {Context.Int32Ty},
2311
978
                                          {LHS, LHS, RHS}));
2312
978
        break;
2313
978
      }
2314
995
      case OpCode::I64__rotl: {
2315
995
        LLVM::Value RHS = stackPop();
2316
995
        LLVM::Value LHS = stackPop();
2317
995
        assuming(LLVM::Core::FShl != LLVM::Core::NotIntrinsic);
2318
995
        stackPush(Builder.createIntrinsic(LLVM::Core::FShl, {Context.Int64Ty},
2319
995
                                          {LHS, LHS, RHS}));
2320
995
        break;
2321
995
      }
2322
1.37k
      case OpCode::I64__rotr: {
2323
1.37k
        LLVM::Value RHS = stackPop();
2324
1.37k
        LLVM::Value LHS = stackPop();
2325
1.37k
        assuming(LLVM::Core::FShr != LLVM::Core::NotIntrinsic);
2326
1.37k
        stackPush(Builder.createIntrinsic(LLVM::Core::FShr, {Context.Int64Ty},
2327
1.37k
                                          {LHS, LHS, RHS}));
2328
1.37k
        break;
2329
1.37k
      }
2330
287
      case OpCode::F32__add:
2331
584
      case OpCode::F64__add: {
2332
584
        LLVM::Value RHS = stackPop();
2333
584
        LLVM::Value LHS = stackPop();
2334
584
        stackPush(Builder.createFAdd(LHS, RHS));
2335
584
        break;
2336
287
      }
2337
141
      case OpCode::F32__sub:
2338
431
      case OpCode::F64__sub: {
2339
431
        LLVM::Value RHS = stackPop();
2340
431
        LLVM::Value LHS = stackPop();
2341
431
        stackPush(Builder.createFSub(LHS, RHS));
2342
431
        break;
2343
141
      }
2344
599
      case OpCode::F32__mul:
2345
738
      case OpCode::F64__mul: {
2346
738
        LLVM::Value RHS = stackPop();
2347
738
        LLVM::Value LHS = stackPop();
2348
738
        stackPush(Builder.createFMul(LHS, RHS));
2349
738
        break;
2350
599
      }
2351
248
      case OpCode::F32__div:
2352
587
      case OpCode::F64__div: {
2353
587
        LLVM::Value RHS = stackPop();
2354
587
        LLVM::Value LHS = stackPop();
2355
587
        stackPush(Builder.createFDiv(LHS, RHS));
2356
587
        break;
2357
248
      }
2358
316
      case OpCode::F32__min:
2359
695
      case OpCode::F64__min: {
2360
695
        LLVM::Value RHS = stackPop();
2361
695
        LLVM::Value LHS = stackPop();
2362
695
        auto FpTy = Instr.getOpCode() == OpCode::F32__min ? Context.FloatTy
2363
695
                                                          : Context.DoubleTy;
2364
695
        auto IntTy = Instr.getOpCode() == OpCode::F32__min ? Context.Int32Ty
2365
695
                                                           : Context.Int64Ty;
2366
2367
695
        auto UEQ = Builder.createFCmpUEQ(LHS, RHS);
2368
695
        auto UNO = Builder.createFCmpUNO(LHS, RHS);
2369
2370
695
        auto LHSInt = Builder.createBitCast(LHS, IntTy);
2371
695
        auto RHSInt = Builder.createBitCast(RHS, IntTy);
2372
695
        auto OrInt = Builder.createOr(LHSInt, RHSInt);
2373
695
        auto OrFp = Builder.createBitCast(OrInt, FpTy);
2374
2375
695
        auto AddFp = Builder.createFAdd(LHS, RHS);
2376
2377
695
        assuming(LLVM::Core::MinNum != LLVM::Core::NotIntrinsic);
2378
695
        auto MinFp = Builder.createIntrinsic(LLVM::Core::MinNum,
2379
695
                                             {LHS.getType()}, {LHS, RHS});
2380
2381
695
        auto Ret = Builder.createSelect(
2382
695
            UEQ, Builder.createSelect(UNO, AddFp, OrFp), MinFp);
2383
695
        stackPush(Ret);
2384
695
        break;
2385
695
      }
2386
351
      case OpCode::F32__max:
2387
945
      case OpCode::F64__max: {
2388
945
        LLVM::Value RHS = stackPop();
2389
945
        LLVM::Value LHS = stackPop();
2390
945
        auto FpTy = Instr.getOpCode() == OpCode::F32__max ? Context.FloatTy
2391
945
                                                          : Context.DoubleTy;
2392
945
        auto IntTy = Instr.getOpCode() == OpCode::F32__max ? Context.Int32Ty
2393
945
                                                           : Context.Int64Ty;
2394
2395
945
        auto UEQ = Builder.createFCmpUEQ(LHS, RHS);
2396
945
        auto UNO = Builder.createFCmpUNO(LHS, RHS);
2397
2398
945
        auto LHSInt = Builder.createBitCast(LHS, IntTy);
2399
945
        auto RHSInt = Builder.createBitCast(RHS, IntTy);
2400
945
        auto AndInt = Builder.createAnd(LHSInt, RHSInt);
2401
945
        auto AndFp = Builder.createBitCast(AndInt, FpTy);
2402
2403
945
        auto AddFp = Builder.createFAdd(LHS, RHS);
2404
2405
945
        assuming(LLVM::Core::MaxNum != LLVM::Core::NotIntrinsic);
2406
945
        auto MaxFp = Builder.createIntrinsic(LLVM::Core::MaxNum,
2407
945
                                             {LHS.getType()}, {LHS, RHS});
2408
2409
945
        auto Ret = Builder.createSelect(
2410
945
            UEQ, Builder.createSelect(UNO, AddFp, AndFp), MaxFp);
2411
945
        stackPush(Ret);
2412
945
        break;
2413
945
      }
2414
562
      case OpCode::F32__copysign:
2415
973
      case OpCode::F64__copysign: {
2416
973
        LLVM::Value RHS = stackPop();
2417
973
        LLVM::Value LHS = stackPop();
2418
973
        assuming(LLVM::Core::CopySign != LLVM::Core::NotIntrinsic);
2419
973
        stackPush(Builder.createIntrinsic(LLVM::Core::CopySign, {LHS.getType()},
2420
973
                                          {LHS, RHS}));
2421
973
        break;
2422
973
      }
2423
2424
      // Saturating Truncation Numeric Instructions
2425
199
      case OpCode::I32__trunc_sat_f32_s:
2426
199
        compileSignedTruncSat(Context.Int32Ty);
2427
199
        break;
2428
93
      case OpCode::I32__trunc_sat_f32_u:
2429
93
        compileUnsignedTruncSat(Context.Int32Ty);
2430
93
        break;
2431
266
      case OpCode::I32__trunc_sat_f64_s:
2432
266
        compileSignedTruncSat(Context.Int32Ty);
2433
266
        break;
2434
161
      case OpCode::I32__trunc_sat_f64_u:
2435
161
        compileUnsignedTruncSat(Context.Int32Ty);
2436
161
        break;
2437
497
      case OpCode::I64__trunc_sat_f32_s:
2438
497
        compileSignedTruncSat(Context.Int64Ty);
2439
497
        break;
2440
350
      case OpCode::I64__trunc_sat_f32_u:
2441
350
        compileUnsignedTruncSat(Context.Int64Ty);
2442
350
        break;
2443
292
      case OpCode::I64__trunc_sat_f64_s:
2444
292
        compileSignedTruncSat(Context.Int64Ty);
2445
292
        break;
2446
323
      case OpCode::I64__trunc_sat_f64_u:
2447
323
        compileUnsignedTruncSat(Context.Int64Ty);
2448
323
        break;
2449
2450
      // SIMD Memory Instructions
2451
5.16k
      case OpCode::V128__load:
2452
5.16k
        compileVectorLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2453
5.16k
                            Instr.getMemoryAlign(), Context.Int128x1Ty);
2454
5.16k
        break;
2455
219
      case OpCode::V128__load8x8_s:
2456
219
        compileVectorLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2457
219
                            Instr.getMemoryAlign(),
2458
219
                            LLVM::Type::getVectorType(Context.Int8Ty, 8),
2459
219
                            Context.Int16x8Ty, true);
2460
219
        break;
2461
45
      case OpCode::V128__load8x8_u:
2462
45
        compileVectorLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2463
45
                            Instr.getMemoryAlign(),
2464
45
                            LLVM::Type::getVectorType(Context.Int8Ty, 8),
2465
45
                            Context.Int16x8Ty, false);
2466
45
        break;
2467
359
      case OpCode::V128__load16x4_s:
2468
359
        compileVectorLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2469
359
                            Instr.getMemoryAlign(),
2470
359
                            LLVM::Type::getVectorType(Context.Int16Ty, 4),
2471
359
                            Context.Int32x4Ty, true);
2472
359
        break;
2473
587
      case OpCode::V128__load16x4_u:
2474
587
        compileVectorLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2475
587
                            Instr.getMemoryAlign(),
2476
587
                            LLVM::Type::getVectorType(Context.Int16Ty, 4),
2477
587
                            Context.Int32x4Ty, false);
2478
587
        break;
2479
215
      case OpCode::V128__load32x2_s:
2480
215
        compileVectorLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2481
215
                            Instr.getMemoryAlign(),
2482
215
                            LLVM::Type::getVectorType(Context.Int32Ty, 2),
2483
215
                            Context.Int64x2Ty, true);
2484
215
        break;
2485
132
      case OpCode::V128__load32x2_u:
2486
132
        compileVectorLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2487
132
                            Instr.getMemoryAlign(),
2488
132
                            LLVM::Type::getVectorType(Context.Int32Ty, 2),
2489
132
                            Context.Int64x2Ty, false);
2490
132
        break;
2491
79
      case OpCode::V128__load8_splat:
2492
79
        compileSplatLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2493
79
                           Instr.getMemoryAlign(), Context.Int8Ty,
2494
79
                           Context.Int8x16Ty);
2495
79
        break;
2496
252
      case OpCode::V128__load16_splat:
2497
252
        compileSplatLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2498
252
                           Instr.getMemoryAlign(), Context.Int16Ty,
2499
252
                           Context.Int16x8Ty);
2500
252
        break;
2501
204
      case OpCode::V128__load32_splat:
2502
204
        compileSplatLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2503
204
                           Instr.getMemoryAlign(), Context.Int32Ty,
2504
204
                           Context.Int32x4Ty);
2505
204
        break;
2506
152
      case OpCode::V128__load64_splat:
2507
152
        compileSplatLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2508
152
                           Instr.getMemoryAlign(), Context.Int64Ty,
2509
152
                           Context.Int64x2Ty);
2510
152
        break;
2511
91
      case OpCode::V128__load32_zero:
2512
91
        compileVectorLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2513
91
                            Instr.getMemoryAlign(), Context.Int32Ty,
2514
91
                            Context.Int128Ty, false);
2515
91
        break;
2516
150
      case OpCode::V128__load64_zero:
2517
150
        compileVectorLoadOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2518
150
                            Instr.getMemoryAlign(), Context.Int64Ty,
2519
150
                            Context.Int128Ty, false);
2520
150
        break;
2521
223
      case OpCode::V128__store:
2522
223
        compileStoreOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2523
223
                       Instr.getMemoryAlign(), Context.Int128x1Ty, false, true);
2524
223
        break;
2525
187
      case OpCode::V128__load8_lane:
2526
187
        compileLoadLaneOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2527
187
                          Instr.getMemoryAlign(), Instr.getMemoryLane(),
2528
187
                          Context.Int8Ty, Context.Int8x16Ty);
2529
187
        break;
2530
148
      case OpCode::V128__load16_lane:
2531
148
        compileLoadLaneOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2532
148
                          Instr.getMemoryAlign(), Instr.getMemoryLane(),
2533
148
                          Context.Int16Ty, Context.Int16x8Ty);
2534
148
        break;
2535
122
      case OpCode::V128__load32_lane:
2536
122
        compileLoadLaneOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2537
122
                          Instr.getMemoryAlign(), Instr.getMemoryLane(),
2538
122
                          Context.Int32Ty, Context.Int32x4Ty);
2539
122
        break;
2540
24
      case OpCode::V128__load64_lane:
2541
24
        compileLoadLaneOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2542
24
                          Instr.getMemoryAlign(), Instr.getMemoryLane(),
2543
24
                          Context.Int64Ty, Context.Int64x2Ty);
2544
24
        break;
2545
130
      case OpCode::V128__store8_lane:
2546
130
        compileStoreLaneOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2547
130
                           Instr.getMemoryAlign(), Instr.getMemoryLane(),
2548
130
                           Context.Int8Ty, Context.Int8x16Ty);
2549
130
        break;
2550
63
      case OpCode::V128__store16_lane:
2551
63
        compileStoreLaneOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2552
63
                           Instr.getMemoryAlign(), Instr.getMemoryLane(),
2553
63
                           Context.Int16Ty, Context.Int16x8Ty);
2554
63
        break;
2555
119
      case OpCode::V128__store32_lane:
2556
119
        compileStoreLaneOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2557
119
                           Instr.getMemoryAlign(), Instr.getMemoryLane(),
2558
119
                           Context.Int32Ty, Context.Int32x4Ty);
2559
119
        break;
2560
26
      case OpCode::V128__store64_lane:
2561
26
        compileStoreLaneOp(Instr.getTargetIndex(), Instr.getMemoryOffset(),
2562
26
                           Instr.getMemoryAlign(), Instr.getMemoryLane(),
2563
26
                           Context.Int64Ty, Context.Int64x2Ty);
2564
26
        break;
2565
2566
      // SIMD Const Instructions
2567
346
      case OpCode::V128__const: {
2568
346
        const auto Value = Instr.getNum().get<uint64x2_t>();
2569
346
        auto Vector =
2570
346
            LLVM::Value::getConstVector64(LLContext, {Value[0], Value[1]});
2571
346
        stackPush(Builder.createBitCast(Vector, Context.Int64x2Ty));
2572
346
        break;
2573
973
      }
2574
2575
      // SIMD Shuffle Instructions
2576
15
      case OpCode::I8x16__shuffle: {
2577
15
        auto V2 = Builder.createBitCast(stackPop(), Context.Int8x16Ty);
2578
15
        auto V1 = Builder.createBitCast(stackPop(), Context.Int8x16Ty);
2579
15
        const auto V3 = Instr.getNum().get<uint128_t>();
2580
15
        std::array<uint8_t, 16> Mask;
2581
255
        for (size_t I = 0; I < 16; ++I) {
2582
240
          auto Num = static_cast<uint8_t>(V3 >> (I * 8));
2583
240
          if constexpr (Endian::native == Endian::little) {
2584
240
            Mask[I] = Num;
2585
          } else {
2586
            Mask[15 - I] = Num < 16 ? 15 - Num : 47 - Num;
2587
          }
2588
240
        }
2589
15
        stackPush(Builder.createBitCast(
2590
15
            Builder.createShuffleVector(
2591
15
                V1, V2, LLVM::Value::getConstVector8(LLContext, Mask)),
2592
15
            Context.Int64x2Ty));
2593
15
        break;
2594
973
      }
2595
2596
      // SIMD Lane Instructions
2597
71
      case OpCode::I8x16__extract_lane_s:
2598
71
        compileExtractLaneOp(Context.Int8x16Ty, Instr.getMemoryLane(),
2599
71
                             Context.Int32Ty, true);
2600
71
        break;
2601
29
      case OpCode::I8x16__extract_lane_u:
2602
29
        compileExtractLaneOp(Context.Int8x16Ty, Instr.getMemoryLane(),
2603
29
                             Context.Int32Ty, false);
2604
29
        break;
2605
150
      case OpCode::I8x16__replace_lane:
2606
150
        compileReplaceLaneOp(Context.Int8x16Ty, Instr.getMemoryLane());
2607
150
        break;
2608
490
      case OpCode::I16x8__extract_lane_s:
2609
490
        compileExtractLaneOp(Context.Int16x8Ty, Instr.getMemoryLane(),
2610
490
                             Context.Int32Ty, true);
2611
490
        break;
2612
459
      case OpCode::I16x8__extract_lane_u:
2613
459
        compileExtractLaneOp(Context.Int16x8Ty, Instr.getMemoryLane(),
2614
459
                             Context.Int32Ty, false);
2615
459
        break;
2616
825
      case OpCode::I16x8__replace_lane:
2617
825
        compileReplaceLaneOp(Context.Int16x8Ty, Instr.getMemoryLane());
2618
825
        break;
2619
68
      case OpCode::I32x4__extract_lane:
2620
68
        compileExtractLaneOp(Context.Int32x4Ty, Instr.getMemoryLane());
2621
68
        break;
2622
221
      case OpCode::I32x4__replace_lane:
2623
221
        compileReplaceLaneOp(Context.Int32x4Ty, Instr.getMemoryLane());
2624
221
        break;
2625
134
      case OpCode::I64x2__extract_lane:
2626
134
        compileExtractLaneOp(Context.Int64x2Ty, Instr.getMemoryLane());
2627
134
        break;
2628
14
      case OpCode::I64x2__replace_lane:
2629
14
        compileReplaceLaneOp(Context.Int64x2Ty, Instr.getMemoryLane());
2630
14
        break;
2631
63
      case OpCode::F32x4__extract_lane:
2632
63
        compileExtractLaneOp(Context.Floatx4Ty, Instr.getMemoryLane());
2633
63
        break;
2634
23
      case OpCode::F32x4__replace_lane:
2635
23
        compileReplaceLaneOp(Context.Floatx4Ty, Instr.getMemoryLane());
2636
23
        break;
2637
72
      case OpCode::F64x2__extract_lane:
2638
72
        compileExtractLaneOp(Context.Doublex2Ty, Instr.getMemoryLane());
2639
72
        break;
2640
7
      case OpCode::F64x2__replace_lane:
2641
7
        compileReplaceLaneOp(Context.Doublex2Ty, Instr.getMemoryLane());
2642
7
        break;
2643
2644
      // SIMD Numeric Instructions
2645
64
      case OpCode::I8x16__swizzle:
2646
64
        compileVectorSwizzle();
2647
64
        break;
2648
40.3k
      case OpCode::I8x16__splat:
2649
40.3k
        compileSplatOp(Context.Int8x16Ty);
2650
40.3k
        break;
2651
10.9k
      case OpCode::I16x8__splat:
2652
10.9k
        compileSplatOp(Context.Int16x8Ty);
2653
10.9k
        break;
2654
1.51k
      case OpCode::I32x4__splat:
2655
1.51k
        compileSplatOp(Context.Int32x4Ty);
2656
1.51k
        break;
2657
454
      case OpCode::I64x2__splat:
2658
454
        compileSplatOp(Context.Int64x2Ty);
2659
454
        break;
2660
366
      case OpCode::F32x4__splat:
2661
366
        compileSplatOp(Context.Floatx4Ty);
2662
366
        break;
2663
60
      case OpCode::F64x2__splat:
2664
60
        compileSplatOp(Context.Doublex2Ty);
2665
60
        break;
2666
95
      case OpCode::I8x16__eq:
2667
95
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntEQ);
2668
95
        break;
2669
398
      case OpCode::I8x16__ne:
2670
398
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntNE);
2671
398
        break;
2672
47
      case OpCode::I8x16__lt_s:
2673
47
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntSLT);
2674
47
        break;
2675
73
      case OpCode::I8x16__lt_u:
2676
73
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntULT);
2677
73
        break;
2678
146
      case OpCode::I8x16__gt_s:
2679
146
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntSGT);
2680
146
        break;
2681
237
      case OpCode::I8x16__gt_u:
2682
237
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntUGT);
2683
237
        break;
2684
109
      case OpCode::I8x16__le_s:
2685
109
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntSLE);
2686
109
        break;
2687
111
      case OpCode::I8x16__le_u:
2688
111
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntULE);
2689
111
        break;
2690
787
      case OpCode::I8x16__ge_s:
2691
787
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntSGE);
2692
787
        break;
2693
114
      case OpCode::I8x16__ge_u:
2694
114
        compileVectorCompareOp(Context.Int8x16Ty, LLVMIntUGE);
2695
114
        break;
2696
71
      case OpCode::I16x8__eq:
2697
71
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntEQ);
2698
71
        break;
2699
228
      case OpCode::I16x8__ne:
2700
228
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntNE);
2701
228
        break;
2702
51
      case OpCode::I16x8__lt_s:
2703
51
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntSLT);
2704
51
        break;
2705
227
      case OpCode::I16x8__lt_u:
2706
227
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntULT);
2707
227
        break;
2708
293
      case OpCode::I16x8__gt_s:
2709
293
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntSGT);
2710
293
        break;
2711
130
      case OpCode::I16x8__gt_u:
2712
130
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntUGT);
2713
130
        break;
2714
142
      case OpCode::I16x8__le_s:
2715
142
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntSLE);
2716
142
        break;
2717
86
      case OpCode::I16x8__le_u:
2718
86
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntULE);
2719
86
        break;
2720
150
      case OpCode::I16x8__ge_s:
2721
150
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntSGE);
2722
150
        break;
2723
78
      case OpCode::I16x8__ge_u:
2724
78
        compileVectorCompareOp(Context.Int16x8Ty, LLVMIntUGE);
2725
78
        break;
2726
61
      case OpCode::I32x4__eq:
2727
61
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntEQ);
2728
61
        break;
2729
163
      case OpCode::I32x4__ne:
2730
163
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntNE);
2731
163
        break;
2732
41
      case OpCode::I32x4__lt_s:
2733
41
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntSLT);
2734
41
        break;
2735
168
      case OpCode::I32x4__lt_u:
2736
168
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntULT);
2737
168
        break;
2738
117
      case OpCode::I32x4__gt_s:
2739
117
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntSGT);
2740
117
        break;
2741
231
      case OpCode::I32x4__gt_u:
2742
231
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntUGT);
2743
231
        break;
2744
277
      case OpCode::I32x4__le_s:
2745
277
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntSLE);
2746
277
        break;
2747
255
      case OpCode::I32x4__le_u:
2748
255
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntULE);
2749
255
        break;
2750
55
      case OpCode::I32x4__ge_s:
2751
55
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntSGE);
2752
55
        break;
2753
103
      case OpCode::I32x4__ge_u:
2754
103
        compileVectorCompareOp(Context.Int32x4Ty, LLVMIntUGE);
2755
103
        break;
2756
124
      case OpCode::I64x2__eq:
2757
124
        compileVectorCompareOp(Context.Int64x2Ty, LLVMIntEQ);
2758
124
        break;
2759
56
      case OpCode::I64x2__ne:
2760
56
        compileVectorCompareOp(Context.Int64x2Ty, LLVMIntNE);
2761
56
        break;
2762
49
      case OpCode::I64x2__lt_s:
2763
49
        compileVectorCompareOp(Context.Int64x2Ty, LLVMIntSLT);
2764
49
        break;
2765
134
      case OpCode::I64x2__gt_s:
2766
134
        compileVectorCompareOp(Context.Int64x2Ty, LLVMIntSGT);
2767
134
        break;
2768
35
      case OpCode::I64x2__le_s:
2769
35
        compileVectorCompareOp(Context.Int64x2Ty, LLVMIntSLE);
2770
35
        break;
2771
42
      case OpCode::I64x2__ge_s:
2772
42
        compileVectorCompareOp(Context.Int64x2Ty, LLVMIntSGE);
2773
42
        break;
2774
1.54k
      case OpCode::F32x4__eq:
2775
1.54k
        compileVectorCompareOp(Context.Floatx4Ty, LLVMRealOEQ,
2776
1.54k
                               Context.Int32x4Ty);
2777
1.54k
        break;
2778
39
      case OpCode::F32x4__ne:
2779
39
        compileVectorCompareOp(Context.Floatx4Ty, LLVMRealUNE,
2780
39
                               Context.Int32x4Ty);
2781
39
        break;
2782
895
      case OpCode::F32x4__lt:
2783
895
        compileVectorCompareOp(Context.Floatx4Ty, LLVMRealOLT,
2784
895
                               Context.Int32x4Ty);
2785
895
        break;
2786
73
      case OpCode::F32x4__gt:
2787
73
        compileVectorCompareOp(Context.Floatx4Ty, LLVMRealOGT,
2788
73
                               Context.Int32x4Ty);
2789
73
        break;
2790
336
      case OpCode::F32x4__le:
2791
336
        compileVectorCompareOp(Context.Floatx4Ty, LLVMRealOLE,
2792
336
                               Context.Int32x4Ty);
2793
336
        break;
2794
75
      case OpCode::F32x4__ge:
2795
75
        compileVectorCompareOp(Context.Floatx4Ty, LLVMRealOGE,
2796
75
                               Context.Int32x4Ty);
2797
75
        break;
2798
58
      case OpCode::F64x2__eq:
2799
58
        compileVectorCompareOp(Context.Doublex2Ty, LLVMRealOEQ,
2800
58
                               Context.Int64x2Ty);
2801
58
        break;
2802
116
      case OpCode::F64x2__ne:
2803
116
        compileVectorCompareOp(Context.Doublex2Ty, LLVMRealUNE,
2804
116
                               Context.Int64x2Ty);
2805
116
        break;
2806
129
      case OpCode::F64x2__lt:
2807
129
        compileVectorCompareOp(Context.Doublex2Ty, LLVMRealOLT,
2808
129
                               Context.Int64x2Ty);
2809
129
        break;
2810
57
      case OpCode::F64x2__gt:
2811
57
        compileVectorCompareOp(Context.Doublex2Ty, LLVMRealOGT,
2812
57
                               Context.Int64x2Ty);
2813
57
        break;
2814
171
      case OpCode::F64x2__le:
2815
171
        compileVectorCompareOp(Context.Doublex2Ty, LLVMRealOLE,
2816
171
                               Context.Int64x2Ty);
2817
171
        break;
2818
87
      case OpCode::F64x2__ge:
2819
87
        compileVectorCompareOp(Context.Doublex2Ty, LLVMRealOGE,
2820
87
                               Context.Int64x2Ty);
2821
87
        break;
2822
132
      case OpCode::V128__not:
2823
132
        Stack.back() = Builder.createNot(Stack.back());
2824
132
        break;
2825
66
      case OpCode::V128__and: {
2826
66
        auto RHS = stackPop();
2827
66
        auto LHS = stackPop();
2828
66
        stackPush(Builder.createAnd(LHS, RHS));
2829
66
        break;
2830
973
      }
2831
83
      case OpCode::V128__andnot: {
2832
83
        auto RHS = stackPop();
2833
83
        auto LHS = stackPop();
2834
83
        stackPush(Builder.createAnd(LHS, Builder.createNot(RHS)));
2835
83
        break;
2836
973
      }
2837
113
      case OpCode::V128__or: {
2838
113
        auto RHS = stackPop();
2839
113
        auto LHS = stackPop();
2840
113
        stackPush(Builder.createOr(LHS, RHS));
2841
113
        break;
2842
973
      }
2843
51
      case OpCode::V128__xor: {
2844
51
        auto RHS = stackPop();
2845
51
        auto LHS = stackPop();
2846
51
        stackPush(Builder.createXor(LHS, RHS));
2847
51
        break;
2848
973
      }
2849
111
      case OpCode::V128__bitselect: {
2850
111
        auto C = stackPop();
2851
111
        auto V2 = stackPop();
2852
111
        auto V1 = stackPop();
2853
111
        stackPush(Builder.createXor(
2854
111
            Builder.createAnd(Builder.createXor(V1, V2), C), V2));
2855
111
        break;
2856
973
      }
2857
107
      case OpCode::V128__any_true:
2858
107
        compileVectorAnyTrue();
2859
107
        break;
2860
874
      case OpCode::I8x16__abs:
2861
874
        compileVectorAbs(Context.Int8x16Ty);
2862
874
        break;
2863
1.59k
      case OpCode::I8x16__neg:
2864
1.59k
        compileVectorNeg(Context.Int8x16Ty);
2865
1.59k
        break;
2866
132
      case OpCode::I8x16__popcnt:
2867
132
        compileVectorPopcnt();
2868
132
        break;
2869
313
      case OpCode::I8x16__all_true:
2870
313
        compileVectorAllTrue(Context.Int8x16Ty);
2871
313
        break;
2872
856
      case OpCode::I8x16__bitmask:
2873
856
        compileVectorBitMask(Context.Int8x16Ty);
2874
856
        break;
2875
85
      case OpCode::I8x16__narrow_i16x8_s:
2876
85
        compileVectorNarrow(Context.Int16x8Ty, true);
2877
85
        break;
2878
201
      case OpCode::I8x16__narrow_i16x8_u:
2879
201
        compileVectorNarrow(Context.Int16x8Ty, false);
2880
201
        break;
2881
192
      case OpCode::I8x16__shl:
2882
192
        compileVectorShl(Context.Int8x16Ty);
2883
192
        break;
2884
1.18k
      case OpCode::I8x16__shr_s:
2885
1.18k
        compileVectorAShr(Context.Int8x16Ty);
2886
1.18k
        break;
2887
62
      case OpCode::I8x16__shr_u:
2888
62
        compileVectorLShr(Context.Int8x16Ty);
2889
62
        break;
2890
55
      case OpCode::I8x16__add:
2891
55
        compileVectorVectorAdd(Context.Int8x16Ty);
2892
55
        break;
2893
1.06k
      case OpCode::I8x16__add_sat_s:
2894
1.06k
        compileVectorVectorAddSat(Context.Int8x16Ty, true);
2895
1.06k
        break;
2896
70
      case OpCode::I8x16__add_sat_u:
2897
70
        compileVectorVectorAddSat(Context.Int8x16Ty, false);
2898
70
        break;
2899
74
      case OpCode::I8x16__sub:
2900
74
        compileVectorVectorSub(Context.Int8x16Ty);
2901
74
        break;
2902
210
      case OpCode::I8x16__sub_sat_s:
2903
210
        compileVectorVectorSubSat(Context.Int8x16Ty, true);
2904
210
        break;
2905
82
      case OpCode::I8x16__sub_sat_u:
2906
82
        compileVectorVectorSubSat(Context.Int8x16Ty, false);
2907
82
        break;
2908
55
      case OpCode::I8x16__min_s:
2909
55
        compileVectorVectorSMin(Context.Int8x16Ty);
2910
55
        break;
2911
92
      case OpCode::I8x16__min_u:
2912
92
        compileVectorVectorUMin(Context.Int8x16Ty);
2913
92
        break;
2914
274
      case OpCode::I8x16__max_s:
2915
274
        compileVectorVectorSMax(Context.Int8x16Ty);
2916
274
        break;
2917
87
      case OpCode::I8x16__max_u:
2918
87
        compileVectorVectorUMax(Context.Int8x16Ty);
2919
87
        break;
2920
121
      case OpCode::I8x16__avgr_u:
2921
121
        compileVectorVectorUAvgr(Context.Int8x16Ty);
2922
121
        break;
2923
404
      case OpCode::I16x8__abs:
2924
404
        compileVectorAbs(Context.Int16x8Ty);
2925
404
        break;
2926
263
      case OpCode::I16x8__neg:
2927
263
        compileVectorNeg(Context.Int16x8Ty);
2928
263
        break;
2929
105
      case OpCode::I16x8__all_true:
2930
105
        compileVectorAllTrue(Context.Int16x8Ty);
2931
105
        break;
2932
102
      case OpCode::I16x8__bitmask:
2933
102
        compileVectorBitMask(Context.Int16x8Ty);
2934
102
        break;
2935
45
      case OpCode::I16x8__narrow_i32x4_s:
2936
45
        compileVectorNarrow(Context.Int32x4Ty, true);
2937
45
        break;
2938
357
      case OpCode::I16x8__narrow_i32x4_u:
2939
357
        compileVectorNarrow(Context.Int32x4Ty, false);
2940
357
        break;
2941
1.02k
      case OpCode::I16x8__extend_low_i8x16_s:
2942
1.02k
        compileVectorExtend(Context.Int8x16Ty, true, true);
2943
1.02k
        break;
2944
65
      case OpCode::I16x8__extend_high_i8x16_s:
2945
65
        compileVectorExtend(Context.Int8x16Ty, true, false);
2946
65
        break;
2947
533
      case OpCode::I16x8__extend_low_i8x16_u:
2948
533
        compileVectorExtend(Context.Int8x16Ty, false, true);
2949
533
        break;
2950
12
      case OpCode::I16x8__extend_high_i8x16_u:
2951
12
        compileVectorExtend(Context.Int8x16Ty, false, false);
2952
12
        break;
2953
80
      case OpCode::I16x8__shl:
2954
80
        compileVectorShl(Context.Int16x8Ty);
2955
80
        break;
2956
561
      case OpCode::I16x8__shr_s:
2957
561
        compileVectorAShr(Context.Int16x8Ty);
2958
561
        break;
2959
54
      case OpCode::I16x8__shr_u:
2960
54
        compileVectorLShr(Context.Int16x8Ty);
2961
54
        break;
2962
148
      case OpCode::I16x8__add:
2963
148
        compileVectorVectorAdd(Context.Int16x8Ty);
2964
148
        break;
2965
23
      case OpCode::I16x8__add_sat_s:
2966
23
        compileVectorVectorAddSat(Context.Int16x8Ty, true);
2967
23
        break;
2968
776
      case OpCode::I16x8__add_sat_u:
2969
776
        compileVectorVectorAddSat(Context.Int16x8Ty, false);
2970
776
        break;
2971
342
      case OpCode::I16x8__sub:
2972
342
        compileVectorVectorSub(Context.Int16x8Ty);
2973
342
        break;
2974
24
      case OpCode::I16x8__sub_sat_s:
2975
24
        compileVectorVectorSubSat(Context.Int16x8Ty, true);
2976
24
        break;
2977
70
      case OpCode::I16x8__sub_sat_u:
2978
70
        compileVectorVectorSubSat(Context.Int16x8Ty, false);
2979
70
        break;
2980
115
      case OpCode::I16x8__mul:
2981
115
        compileVectorVectorMul(Context.Int16x8Ty);
2982
115
        break;
2983
158
      case OpCode::I16x8__min_s:
2984
158
        compileVectorVectorSMin(Context.Int16x8Ty);
2985
158
        break;
2986
123
      case OpCode::I16x8__min_u:
2987
123
        compileVectorVectorUMin(Context.Int16x8Ty);
2988
123
        break;
2989
80
      case OpCode::I16x8__max_s:
2990
80
        compileVectorVectorSMax(Context.Int16x8Ty);
2991
80
        break;
2992
1.08k
      case OpCode::I16x8__max_u:
2993
1.08k
        compileVectorVectorUMax(Context.Int16x8Ty);
2994
1.08k
        break;
2995
217
      case OpCode::I16x8__avgr_u:
2996
217
        compileVectorVectorUAvgr(Context.Int16x8Ty);
2997
217
        break;
2998
66
      case OpCode::I16x8__extmul_low_i8x16_s:
2999
66
        compileVectorExtMul(Context.Int8x16Ty, true, true);
3000
66
        break;
3001
197
      case OpCode::I16x8__extmul_high_i8x16_s:
3002
197
        compileVectorExtMul(Context.Int8x16Ty, true, false);
3003
197
        break;
3004
112
      case OpCode::I16x8__extmul_low_i8x16_u:
3005
112
        compileVectorExtMul(Context.Int8x16Ty, false, true);
3006
112
        break;
3007
433
      case OpCode::I16x8__extmul_high_i8x16_u:
3008
433
        compileVectorExtMul(Context.Int8x16Ty, false, false);
3009
433
        break;
3010
152
      case OpCode::I16x8__q15mulr_sat_s:
3011
152
        compileVectorVectorQ15MulSat();
3012
152
        break;
3013
321
      case OpCode::I16x8__extadd_pairwise_i8x16_s:
3014
321
        compileVectorExtAddPairwise(Context.Int8x16Ty, true);
3015
321
        break;
3016
332
      case OpCode::I16x8__extadd_pairwise_i8x16_u:
3017
332
        compileVectorExtAddPairwise(Context.Int8x16Ty, false);
3018
332
        break;
3019
67
      case OpCode::I32x4__abs:
3020
67
        compileVectorAbs(Context.Int32x4Ty);
3021
67
        break;
3022
196
      case OpCode::I32x4__neg:
3023
196
        compileVectorNeg(Context.Int32x4Ty);
3024
196
        break;
3025
171
      case OpCode::I32x4__all_true:
3026
171
        compileVectorAllTrue(Context.Int32x4Ty);
3027
171
        break;
3028
87
      case OpCode::I32x4__bitmask:
3029
87
        compileVectorBitMask(Context.Int32x4Ty);
3030
87
        break;
3031
109
      case OpCode::I32x4__extend_low_i16x8_s:
3032
109
        compileVectorExtend(Context.Int16x8Ty, true, true);
3033
109
        break;
3034
517
      case OpCode::I32x4__extend_high_i16x8_s:
3035
517
        compileVectorExtend(Context.Int16x8Ty, true, false);
3036
517
        break;
3037
1.90k
      case OpCode::I32x4__extend_low_i16x8_u:
3038
1.90k
        compileVectorExtend(Context.Int16x8Ty, false, true);
3039
1.90k
        break;
3040
142
      case OpCode::I32x4__extend_high_i16x8_u:
3041
142
        compileVectorExtend(Context.Int16x8Ty, false, false);
3042
142
        break;
3043
1.68k
      case OpCode::I32x4__shl:
3044
1.68k
        compileVectorShl(Context.Int32x4Ty);
3045
1.68k
        break;
3046
490
      case OpCode::I32x4__shr_s:
3047
490
        compileVectorAShr(Context.Int32x4Ty);
3048
490
        break;
3049
99
      case OpCode::I32x4__shr_u:
3050
99
        compileVectorLShr(Context.Int32x4Ty);
3051
99
        break;
3052
103
      case OpCode::I32x4__add:
3053
103
        compileVectorVectorAdd(Context.Int32x4Ty);
3054
103
        break;
3055
141
      case OpCode::I32x4__sub:
3056
141
        compileVectorVectorSub(Context.Int32x4Ty);
3057
141
        break;
3058
243
      case OpCode::I32x4__mul:
3059
243
        compileVectorVectorMul(Context.Int32x4Ty);
3060
243
        break;
3061
104
      case OpCode::I32x4__min_s:
3062
104
        compileVectorVectorSMin(Context.Int32x4Ty);
3063
104
        break;
3064
93
      case OpCode::I32x4__min_u:
3065
93
        compileVectorVectorUMin(Context.Int32x4Ty);
3066
93
        break;
3067
64
      case OpCode::I32x4__max_s:
3068
64
        compileVectorVectorSMax(Context.Int32x4Ty);
3069
64
        break;
3070
106
      case OpCode::I32x4__max_u:
3071
106
        compileVectorVectorUMax(Context.Int32x4Ty);
3072
106
        break;
3073
106
      case OpCode::I32x4__extmul_low_i16x8_s:
3074
106
        compileVectorExtMul(Context.Int16x8Ty, true, true);
3075
106
        break;
3076
54
      case OpCode::I32x4__extmul_high_i16x8_s:
3077
54
        compileVectorExtMul(Context.Int16x8Ty, true, false);
3078
54
        break;
3079
248
      case OpCode::I32x4__extmul_low_i16x8_u:
3080
248
        compileVectorExtMul(Context.Int16x8Ty, false, true);
3081
248
        break;
3082
78
      case OpCode::I32x4__extmul_high_i16x8_u:
3083
78
        compileVectorExtMul(Context.Int16x8Ty, false, false);
3084
78
        break;
3085
1.32k
      case OpCode::I32x4__extadd_pairwise_i16x8_s:
3086
1.32k
        compileVectorExtAddPairwise(Context.Int16x8Ty, true);
3087
1.32k
        break;
3088
464
      case OpCode::I32x4__extadd_pairwise_i16x8_u:
3089
464
        compileVectorExtAddPairwise(Context.Int16x8Ty, false);
3090
464
        break;
3091
101
      case OpCode::I32x4__dot_i16x8_s: {
3092
101
        auto ExtendTy = Context.Int16x8Ty.getExtendedElementVectorType();
3093
101
        auto Undef = LLVM::Value::getUndef(ExtendTy);
3094
101
        auto LHS = Builder.createSExt(
3095
101
            Builder.createBitCast(stackPop(), Context.Int16x8Ty), ExtendTy);
3096
101
        auto RHS = Builder.createSExt(
3097
101
            Builder.createBitCast(stackPop(), Context.Int16x8Ty), ExtendTy);
3098
101
        auto M = Builder.createMul(LHS, RHS);
3099
101
        auto L = Builder.createShuffleVector(
3100
101
            M, Undef,
3101
101
            LLVM::Value::getConstVector32(LLContext, {0U, 2U, 4U, 6U}));
3102
101
        auto R = Builder.createShuffleVector(
3103
101
            M, Undef,
3104
101
            LLVM::Value::getConstVector32(LLContext, {1U, 3U, 5U, 7U}));
3105
101
        auto V = Builder.createAdd(L, R);
3106
101
        stackPush(Builder.createBitCast(V, Context.Int64x2Ty));
3107
101
        break;
3108
973
      }
3109
1.00k
      case OpCode::I64x2__abs:
3110
1.00k
        compileVectorAbs(Context.Int64x2Ty);
3111
1.00k
        break;
3112
592
      case OpCode::I64x2__neg:
3113
592
        compileVectorNeg(Context.Int64x2Ty);
3114
592
        break;
3115
286
      case OpCode::I64x2__all_true:
3116
286
        compileVectorAllTrue(Context.Int64x2Ty);
3117
286
        break;
3118
241
      case OpCode::I64x2__bitmask:
3119
241
        compileVectorBitMask(Context.Int64x2Ty);
3120
241
        break;
3121
120
      case OpCode::I64x2__extend_low_i32x4_s:
3122
120
        compileVectorExtend(Context.Int32x4Ty, true, true);
3123
120
        break;
3124
756
      case OpCode::I64x2__extend_high_i32x4_s:
3125
756
        compileVectorExtend(Context.Int32x4Ty, true, false);
3126
756
        break;
3127
256
      case OpCode::I64x2__extend_low_i32x4_u:
3128
256
        compileVectorExtend(Context.Int32x4Ty, false, true);
3129
256
        break;
3130
712
      case OpCode::I64x2__extend_high_i32x4_u:
3131
712
        compileVectorExtend(Context.Int32x4Ty, false, false);
3132
712
        break;
3133
96
      case OpCode::I64x2__shl:
3134
96
        compileVectorShl(Context.Int64x2Ty);
3135
96
        break;
3136
298
      case OpCode::I64x2__shr_s:
3137
298
        compileVectorAShr(Context.Int64x2Ty);
3138
298
        break;
3139
68
      case OpCode::I64x2__shr_u:
3140
68
        compileVectorLShr(Context.Int64x2Ty);
3141
68
        break;
3142
38
      case OpCode::I64x2__add:
3143
38
        compileVectorVectorAdd(Context.Int64x2Ty);
3144
38
        break;
3145
255
      case OpCode::I64x2__sub:
3146
255
        compileVectorVectorSub(Context.Int64x2Ty);
3147
255
        break;
3148
85
      case OpCode::I64x2__mul:
3149
85
        compileVectorVectorMul(Context.Int64x2Ty);
3150
85
        break;
3151
38
      case OpCode::I64x2__extmul_low_i32x4_s:
3152
38
        compileVectorExtMul(Context.Int32x4Ty, true, true);
3153
38
        break;
3154
330
      case OpCode::I64x2__extmul_high_i32x4_s:
3155
330
        compileVectorExtMul(Context.Int32x4Ty, true, false);
3156
330
        break;
3157
30
      case OpCode::I64x2__extmul_low_i32x4_u:
3158
30
        compileVectorExtMul(Context.Int32x4Ty, false, true);
3159
30
        break;
3160
119
      case OpCode::I64x2__extmul_high_i32x4_u:
3161
119
        compileVectorExtMul(Context.Int32x4Ty, false, false);
3162
119
        break;
3163
139
      case OpCode::F32x4__abs:
3164
139
        compileVectorFAbs(Context.Floatx4Ty);
3165
139
        break;
3166
150
      case OpCode::F32x4__neg:
3167
150
        compileVectorFNeg(Context.Floatx4Ty);
3168
150
        break;
3169
195
      case OpCode::F32x4__sqrt:
3170
195
        compileVectorFSqrt(Context.Floatx4Ty);
3171
195
        break;
3172
152
      case OpCode::F32x4__add:
3173
152
        compileVectorVectorFAdd(Context.Floatx4Ty);
3174
152
        break;
3175
291
      case OpCode::F32x4__sub:
3176
291
        compileVectorVectorFSub(Context.Floatx4Ty);
3177
291
        break;
3178
40
      case OpCode::F32x4__mul:
3179
40
        compileVectorVectorFMul(Context.Floatx4Ty);
3180
40
        break;
3181
192
      case OpCode::F32x4__div:
3182
192
        compileVectorVectorFDiv(Context.Floatx4Ty);
3183
192
        break;
3184
125
      case OpCode::F32x4__min:
3185
125
        compileVectorVectorFMin(Context.Floatx4Ty);
3186
125
        break;
3187
36
      case OpCode::F32x4__max:
3188
36
        compileVectorVectorFMax(Context.Floatx4Ty);
3189
36
        break;
3190
54
      case OpCode::F32x4__pmin:
3191
54
        compileVectorVectorFPMin(Context.Floatx4Ty);
3192
54
        break;
3193
243
      case OpCode::F32x4__pmax:
3194
243
        compileVectorVectorFPMax(Context.Floatx4Ty);
3195
243
        break;
3196
806
      case OpCode::F32x4__ceil:
3197
806
        compileVectorFCeil(Context.Floatx4Ty);
3198
806
        break;
3199
1.81k
      case OpCode::F32x4__floor:
3200
1.81k
        compileVectorFFloor(Context.Floatx4Ty);
3201
1.81k
        break;
3202
1.69k
      case OpCode::F32x4__trunc:
3203
1.69k
        compileVectorFTrunc(Context.Floatx4Ty);
3204
1.69k
        break;
3205
224
      case OpCode::F32x4__nearest:
3206
224
        compileVectorFNearest(Context.Floatx4Ty);
3207
224
        break;
3208
440
      case OpCode::F64x2__abs:
3209
440
        compileVectorFAbs(Context.Doublex2Ty);
3210
440
        break;
3211
789
      case OpCode::F64x2__neg:
3212
789
        compileVectorFNeg(Context.Doublex2Ty);
3213
789
        break;
3214
127
      case OpCode::F64x2__sqrt:
3215
127
        compileVectorFSqrt(Context.Doublex2Ty);
3216
127
        break;
3217
61
      case OpCode::F64x2__add:
3218
61
        compileVectorVectorFAdd(Context.Doublex2Ty);
3219
61
        break;
3220
230
      case OpCode::F64x2__sub:
3221
230
        compileVectorVectorFSub(Context.Doublex2Ty);
3222
230
        break;
3223
212
      case OpCode::F64x2__mul:
3224
212
        compileVectorVectorFMul(Context.Doublex2Ty);
3225
212
        break;
3226
37
      case OpCode::F64x2__div:
3227
37
        compileVectorVectorFDiv(Context.Doublex2Ty);
3228
37
        break;
3229
184
      case OpCode::F64x2__min:
3230
184
        compileVectorVectorFMin(Context.Doublex2Ty);
3231
184
        break;
3232
154
      case OpCode::F64x2__max:
3233
154
        compileVectorVectorFMax(Context.Doublex2Ty);
3234
154
        break;
3235
312
      case OpCode::F64x2__pmin:
3236
312
        compileVectorVectorFPMin(Context.Doublex2Ty);
3237
312
        break;
3238
67
      case OpCode::F64x2__pmax:
3239
67
        compileVectorVectorFPMax(Context.Doublex2Ty);
3240
67
        break;
3241
576
      case OpCode::F64x2__ceil:
3242
576
        compileVectorFCeil(Context.Doublex2Ty);
3243
576
        break;
3244
690
      case OpCode::F64x2__floor:
3245
690
        compileVectorFFloor(Context.Doublex2Ty);
3246
690
        break;
3247
117
      case OpCode::F64x2__trunc:
3248
117
        compileVectorFTrunc(Context.Doublex2Ty);
3249
117
        break;
3250
152
      case OpCode::F64x2__nearest:
3251
152
        compileVectorFNearest(Context.Doublex2Ty);
3252
152
        break;
3253
215
      case OpCode::I32x4__trunc_sat_f32x4_s:
3254
215
        compileVectorTruncSatS32(Context.Floatx4Ty, false);
3255
215
        break;
3256
3.71k
      case OpCode::I32x4__trunc_sat_f32x4_u:
3257
3.71k
        compileVectorTruncSatU32(Context.Floatx4Ty, false);
3258
3.71k
        break;
3259
390
      case OpCode::F32x4__convert_i32x4_s:
3260
390
        compileVectorConvertS(Context.Int32x4Ty, Context.Floatx4Ty, false);
3261
390
        break;
3262
726
      case OpCode::F32x4__convert_i32x4_u:
3263
726
        compileVectorConvertU(Context.Int32x4Ty, Context.Floatx4Ty, false);
3264
726
        break;
3265
744
      case OpCode::I32x4__trunc_sat_f64x2_s_zero:
3266
744
        compileVectorTruncSatS32(Context.Doublex2Ty, true);
3267
744
        break;
3268
2.13k
      case OpCode::I32x4__trunc_sat_f64x2_u_zero:
3269
2.13k
        compileVectorTruncSatU32(Context.Doublex2Ty, true);
3270
2.13k
        break;
3271
329
      case OpCode::F64x2__convert_low_i32x4_s:
3272
329
        compileVectorConvertS(Context.Int32x4Ty, Context.Doublex2Ty, true);
3273
329
        break;
3274
1.26k
      case OpCode::F64x2__convert_low_i32x4_u:
3275
1.26k
        compileVectorConvertU(Context.Int32x4Ty, Context.Doublex2Ty, true);
3276
1.26k
        break;
3277
623
      case OpCode::F32x4__demote_f64x2_zero:
3278
623
        compileVectorDemote();
3279
623
        break;
3280
610
      case OpCode::F64x2__promote_low_f32x4:
3281
610
        compileVectorPromote();
3282
610
        break;
3283
3284
      // Relaxed SIMD Instructions
3285
0
      case OpCode::I8x16__relaxed_swizzle:
3286
0
        compileVectorSwizzle();
3287
0
        break;
3288
0
      case OpCode::I32x4__relaxed_trunc_f32x4_s:
3289
0
        compileVectorTruncSatS32(Context.Floatx4Ty, false);
3290
0
        break;
3291
0
      case OpCode::I32x4__relaxed_trunc_f32x4_u:
3292
0
        compileVectorTruncSatU32(Context.Floatx4Ty, false);
3293
0
        break;
3294
0
      case OpCode::I32x4__relaxed_trunc_f64x2_s_zero:
3295
0
        compileVectorTruncSatS32(Context.Doublex2Ty, true);
3296
0
        break;
3297
0
      case OpCode::I32x4__relaxed_trunc_f64x2_u_zero:
3298
0
        compileVectorTruncSatU32(Context.Doublex2Ty, true);
3299
0
        break;
3300
0
      case OpCode::F32x4__relaxed_madd:
3301
0
        compileVectorVectorMAdd(Context.Floatx4Ty);
3302
0
        break;
3303
0
      case OpCode::F32x4__relaxed_nmadd:
3304
0
        compileVectorVectorNMAdd(Context.Floatx4Ty);
3305
0
        break;
3306
0
      case OpCode::F64x2__relaxed_madd:
3307
0
        compileVectorVectorMAdd(Context.Doublex2Ty);
3308
0
        break;
3309
0
      case OpCode::F64x2__relaxed_nmadd:
3310
0
        compileVectorVectorNMAdd(Context.Doublex2Ty);
3311
0
        break;
3312
0
      case OpCode::I8x16__relaxed_laneselect:
3313
0
      case OpCode::I16x8__relaxed_laneselect:
3314
0
      case OpCode::I32x4__relaxed_laneselect:
3315
0
      case OpCode::I64x2__relaxed_laneselect: {
3316
0
        auto C = stackPop();
3317
0
        auto V2 = stackPop();
3318
0
        auto V1 = stackPop();
3319
0
        stackPush(Builder.createXor(
3320
0
            Builder.createAnd(Builder.createXor(V1, V2), C), V2));
3321
0
        break;
3322
0
      }
3323
0
      case OpCode::F32x4__relaxed_min:
3324
0
        compileVectorVectorFMin(Context.Floatx4Ty);
3325
0
        break;
3326
0
      case OpCode::F32x4__relaxed_max:
3327
0
        compileVectorVectorFMax(Context.Floatx4Ty);
3328
0
        break;
3329
0
      case OpCode::F64x2__relaxed_min:
3330
0
        compileVectorVectorFMin(Context.Doublex2Ty);
3331
0
        break;
3332
0
      case OpCode::F64x2__relaxed_max:
3333
0
        compileVectorVectorFMax(Context.Doublex2Ty);
3334
0
        break;
3335
0
      case OpCode::I16x8__relaxed_q15mulr_s:
3336
0
        compileVectorVectorQ15MulSat();
3337
0
        break;
3338
0
      case OpCode::I16x8__relaxed_dot_i8x16_i7x16_s:
3339
0
        compileVectorRelaxedIntegerDotProduct();
3340
0
        break;
3341
0
      case OpCode::I32x4__relaxed_dot_i8x16_i7x16_add_s:
3342
0
        compileVectorRelaxedIntegerDotProductAdd();
3343
0
        break;
3344
3345
      // Atomic Instructions
3346
204
      case OpCode::Atomic__fence:
3347
204
        return compileMemoryFence();
3348
33
      case OpCode::Memory__atomic__notify:
3349
33
        return compileAtomicNotify(Instr.getTargetIndex(),
3350
33
                                   Instr.getMemoryOffset());
3351
5
      case OpCode::Memory__atomic__wait32:
3352
5
        return compileAtomicWait(Instr.getTargetIndex(),
3353
5
                                 Instr.getMemoryOffset(), Context.Int32Ty, 32);
3354
2
      case OpCode::Memory__atomic__wait64:
3355
2
        return compileAtomicWait(Instr.getTargetIndex(),
3356
2
                                 Instr.getMemoryOffset(), Context.Int64Ty, 64);
3357
0
      case OpCode::I32__atomic__load:
3358
0
        return compileAtomicLoad(
3359
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3360
0
            Instr.getMemoryAlign(), Context.Int32Ty, Context.Int32Ty, true);
3361
0
      case OpCode::I64__atomic__load:
3362
0
        return compileAtomicLoad(
3363
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3364
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int64Ty, true);
3365
0
      case OpCode::I32__atomic__load8_u:
3366
0
        return compileAtomicLoad(
3367
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3368
0
            Instr.getMemoryAlign(), Context.Int32Ty, Context.Int8Ty);
3369
0
      case OpCode::I32__atomic__load16_u:
3370
0
        return compileAtomicLoad(
3371
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3372
0
            Instr.getMemoryAlign(), Context.Int32Ty, Context.Int16Ty);
3373
0
      case OpCode::I64__atomic__load8_u:
3374
0
        return compileAtomicLoad(
3375
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3376
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int8Ty);
3377
0
      case OpCode::I64__atomic__load16_u:
3378
0
        return compileAtomicLoad(
3379
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3380
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int16Ty);
3381
0
      case OpCode::I64__atomic__load32_u:
3382
0
        return compileAtomicLoad(
3383
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3384
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int32Ty);
3385
0
      case OpCode::I32__atomic__store:
3386
0
        return compileAtomicStore(
3387
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3388
0
            Instr.getMemoryAlign(), Context.Int32Ty, Context.Int32Ty, true);
3389
0
      case OpCode::I64__atomic__store:
3390
0
        return compileAtomicStore(
3391
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3392
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int64Ty, true);
3393
0
      case OpCode::I32__atomic__store8:
3394
0
        return compileAtomicStore(
3395
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3396
0
            Instr.getMemoryAlign(), Context.Int32Ty, Context.Int8Ty, true);
3397
0
      case OpCode::I32__atomic__store16:
3398
0
        return compileAtomicStore(
3399
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3400
0
            Instr.getMemoryAlign(), Context.Int32Ty, Context.Int16Ty, true);
3401
0
      case OpCode::I64__atomic__store8:
3402
0
        return compileAtomicStore(
3403
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3404
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int8Ty, true);
3405
0
      case OpCode::I64__atomic__store16:
3406
0
        return compileAtomicStore(
3407
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3408
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int16Ty, true);
3409
0
      case OpCode::I64__atomic__store32:
3410
0
        return compileAtomicStore(
3411
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3412
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int32Ty, true);
3413
0
      case OpCode::I32__atomic__rmw__add:
3414
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3415
0
                                  Instr.getMemoryOffset(),
3416
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAdd,
3417
0
                                  Context.Int32Ty, Context.Int32Ty, true);
3418
0
      case OpCode::I64__atomic__rmw__add:
3419
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3420
0
                                  Instr.getMemoryOffset(),
3421
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAdd,
3422
0
                                  Context.Int64Ty, Context.Int64Ty, true);
3423
0
      case OpCode::I32__atomic__rmw8__add_u:
3424
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3425
0
                                  Instr.getMemoryOffset(),
3426
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAdd,
3427
0
                                  Context.Int32Ty, Context.Int8Ty);
3428
0
      case OpCode::I32__atomic__rmw16__add_u:
3429
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3430
0
                                  Instr.getMemoryOffset(),
3431
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAdd,
3432
0
                                  Context.Int32Ty, Context.Int16Ty);
3433
0
      case OpCode::I64__atomic__rmw8__add_u:
3434
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3435
0
                                  Instr.getMemoryOffset(),
3436
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAdd,
3437
0
                                  Context.Int64Ty, Context.Int8Ty);
3438
0
      case OpCode::I64__atomic__rmw16__add_u:
3439
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3440
0
                                  Instr.getMemoryOffset(),
3441
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAdd,
3442
0
                                  Context.Int64Ty, Context.Int16Ty);
3443
0
      case OpCode::I64__atomic__rmw32__add_u:
3444
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3445
0
                                  Instr.getMemoryOffset(),
3446
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAdd,
3447
0
                                  Context.Int64Ty, Context.Int32Ty);
3448
0
      case OpCode::I32__atomic__rmw__sub:
3449
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3450
0
                                  Instr.getMemoryOffset(),
3451
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpSub,
3452
0
                                  Context.Int32Ty, Context.Int32Ty, true);
3453
0
      case OpCode::I64__atomic__rmw__sub:
3454
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3455
0
                                  Instr.getMemoryOffset(),
3456
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpSub,
3457
0
                                  Context.Int64Ty, Context.Int64Ty, true);
3458
0
      case OpCode::I32__atomic__rmw8__sub_u:
3459
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3460
0
                                  Instr.getMemoryOffset(),
3461
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpSub,
3462
0
                                  Context.Int32Ty, Context.Int8Ty);
3463
0
      case OpCode::I32__atomic__rmw16__sub_u:
3464
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3465
0
                                  Instr.getMemoryOffset(),
3466
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpSub,
3467
0
                                  Context.Int32Ty, Context.Int16Ty);
3468
0
      case OpCode::I64__atomic__rmw8__sub_u:
3469
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3470
0
                                  Instr.getMemoryOffset(),
3471
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpSub,
3472
0
                                  Context.Int64Ty, Context.Int8Ty);
3473
0
      case OpCode::I64__atomic__rmw16__sub_u:
3474
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3475
0
                                  Instr.getMemoryOffset(),
3476
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpSub,
3477
0
                                  Context.Int64Ty, Context.Int16Ty);
3478
0
      case OpCode::I64__atomic__rmw32__sub_u:
3479
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3480
0
                                  Instr.getMemoryOffset(),
3481
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpSub,
3482
0
                                  Context.Int64Ty, Context.Int32Ty);
3483
0
      case OpCode::I32__atomic__rmw__and:
3484
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3485
0
                                  Instr.getMemoryOffset(),
3486
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAnd,
3487
0
                                  Context.Int32Ty, Context.Int32Ty, true);
3488
0
      case OpCode::I64__atomic__rmw__and:
3489
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3490
0
                                  Instr.getMemoryOffset(),
3491
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAnd,
3492
0
                                  Context.Int64Ty, Context.Int64Ty, true);
3493
0
      case OpCode::I32__atomic__rmw8__and_u:
3494
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3495
0
                                  Instr.getMemoryOffset(),
3496
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAnd,
3497
0
                                  Context.Int32Ty, Context.Int8Ty);
3498
0
      case OpCode::I32__atomic__rmw16__and_u:
3499
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3500
0
                                  Instr.getMemoryOffset(),
3501
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAnd,
3502
0
                                  Context.Int32Ty, Context.Int16Ty);
3503
0
      case OpCode::I64__atomic__rmw8__and_u:
3504
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3505
0
                                  Instr.getMemoryOffset(),
3506
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAnd,
3507
0
                                  Context.Int64Ty, Context.Int8Ty);
3508
0
      case OpCode::I64__atomic__rmw16__and_u:
3509
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3510
0
                                  Instr.getMemoryOffset(),
3511
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAnd,
3512
0
                                  Context.Int64Ty, Context.Int16Ty);
3513
0
      case OpCode::I64__atomic__rmw32__and_u:
3514
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3515
0
                                  Instr.getMemoryOffset(),
3516
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpAnd,
3517
0
                                  Context.Int64Ty, Context.Int32Ty);
3518
0
      case OpCode::I32__atomic__rmw__or:
3519
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3520
0
                                  Instr.getMemoryOffset(),
3521
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpOr,
3522
0
                                  Context.Int32Ty, Context.Int32Ty, true);
3523
0
      case OpCode::I64__atomic__rmw__or:
3524
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3525
0
                                  Instr.getMemoryOffset(),
3526
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpOr,
3527
0
                                  Context.Int64Ty, Context.Int64Ty, true);
3528
0
      case OpCode::I32__atomic__rmw8__or_u:
3529
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3530
0
                                  Instr.getMemoryOffset(),
3531
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpOr,
3532
0
                                  Context.Int32Ty, Context.Int8Ty);
3533
0
      case OpCode::I32__atomic__rmw16__or_u:
3534
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3535
0
                                  Instr.getMemoryOffset(),
3536
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpOr,
3537
0
                                  Context.Int32Ty, Context.Int16Ty);
3538
0
      case OpCode::I64__atomic__rmw8__or_u:
3539
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3540
0
                                  Instr.getMemoryOffset(),
3541
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpOr,
3542
0
                                  Context.Int64Ty, Context.Int8Ty);
3543
0
      case OpCode::I64__atomic__rmw16__or_u:
3544
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3545
0
                                  Instr.getMemoryOffset(),
3546
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpOr,
3547
0
                                  Context.Int64Ty, Context.Int16Ty);
3548
0
      case OpCode::I64__atomic__rmw32__or_u:
3549
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3550
0
                                  Instr.getMemoryOffset(),
3551
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpOr,
3552
0
                                  Context.Int64Ty, Context.Int32Ty);
3553
0
      case OpCode::I32__atomic__rmw__xor:
3554
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3555
0
                                  Instr.getMemoryOffset(),
3556
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXor,
3557
0
                                  Context.Int32Ty, Context.Int32Ty, true);
3558
0
      case OpCode::I64__atomic__rmw__xor:
3559
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3560
0
                                  Instr.getMemoryOffset(),
3561
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXor,
3562
0
                                  Context.Int64Ty, Context.Int64Ty, true);
3563
0
      case OpCode::I32__atomic__rmw8__xor_u:
3564
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3565
0
                                  Instr.getMemoryOffset(),
3566
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXor,
3567
0
                                  Context.Int32Ty, Context.Int8Ty);
3568
0
      case OpCode::I32__atomic__rmw16__xor_u:
3569
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3570
0
                                  Instr.getMemoryOffset(),
3571
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXor,
3572
0
                                  Context.Int32Ty, Context.Int16Ty);
3573
0
      case OpCode::I64__atomic__rmw8__xor_u:
3574
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3575
0
                                  Instr.getMemoryOffset(),
3576
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXor,
3577
0
                                  Context.Int64Ty, Context.Int8Ty);
3578
0
      case OpCode::I64__atomic__rmw16__xor_u:
3579
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3580
0
                                  Instr.getMemoryOffset(),
3581
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXor,
3582
0
                                  Context.Int64Ty, Context.Int16Ty);
3583
0
      case OpCode::I64__atomic__rmw32__xor_u:
3584
0
        return compileAtomicRMWOp(Instr.getTargetIndex(),
3585
0
                                  Instr.getMemoryOffset(),
3586
0
                                  Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXor,
3587
0
                                  Context.Int64Ty, Context.Int32Ty);
3588
0
      case OpCode::I32__atomic__rmw__xchg:
3589
0
        return compileAtomicRMWOp(
3590
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3591
0
            Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXchg, Context.Int32Ty,
3592
0
            Context.Int32Ty, true);
3593
0
      case OpCode::I64__atomic__rmw__xchg:
3594
0
        return compileAtomicRMWOp(
3595
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3596
0
            Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXchg, Context.Int64Ty,
3597
0
            Context.Int64Ty, true);
3598
0
      case OpCode::I32__atomic__rmw8__xchg_u:
3599
0
        return compileAtomicRMWOp(
3600
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3601
0
            Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXchg, Context.Int32Ty,
3602
0
            Context.Int8Ty);
3603
0
      case OpCode::I32__atomic__rmw16__xchg_u:
3604
0
        return compileAtomicRMWOp(
3605
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3606
0
            Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXchg, Context.Int32Ty,
3607
0
            Context.Int16Ty);
3608
0
      case OpCode::I64__atomic__rmw8__xchg_u:
3609
0
        return compileAtomicRMWOp(
3610
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3611
0
            Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXchg, Context.Int64Ty,
3612
0
            Context.Int8Ty);
3613
0
      case OpCode::I64__atomic__rmw16__xchg_u:
3614
0
        return compileAtomicRMWOp(
3615
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3616
0
            Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXchg, Context.Int64Ty,
3617
0
            Context.Int16Ty);
3618
0
      case OpCode::I64__atomic__rmw32__xchg_u:
3619
0
        return compileAtomicRMWOp(
3620
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3621
0
            Instr.getMemoryAlign(), LLVMAtomicRMWBinOpXchg, Context.Int64Ty,
3622
0
            Context.Int32Ty);
3623
0
      case OpCode::I32__atomic__rmw__cmpxchg:
3624
0
        return compileAtomicCompareExchange(
3625
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3626
0
            Instr.getMemoryAlign(), Context.Int32Ty, Context.Int32Ty, true);
3627
0
      case OpCode::I64__atomic__rmw__cmpxchg:
3628
0
        return compileAtomicCompareExchange(
3629
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3630
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int64Ty, true);
3631
0
      case OpCode::I32__atomic__rmw8__cmpxchg_u:
3632
0
        return compileAtomicCompareExchange(
3633
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3634
0
            Instr.getMemoryAlign(), Context.Int32Ty, Context.Int8Ty);
3635
0
      case OpCode::I32__atomic__rmw16__cmpxchg_u:
3636
0
        return compileAtomicCompareExchange(
3637
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3638
0
            Instr.getMemoryAlign(), Context.Int32Ty, Context.Int16Ty);
3639
0
      case OpCode::I64__atomic__rmw8__cmpxchg_u:
3640
0
        return compileAtomicCompareExchange(
3641
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3642
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int8Ty);
3643
0
      case OpCode::I64__atomic__rmw16__cmpxchg_u:
3644
0
        return compileAtomicCompareExchange(
3645
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3646
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int16Ty);
3647
0
      case OpCode::I64__atomic__rmw32__cmpxchg_u:
3648
0
        return compileAtomicCompareExchange(
3649
0
            Instr.getTargetIndex(), Instr.getMemoryOffset(),
3650
0
            Instr.getMemoryAlign(), Context.Int64Ty, Context.Int32Ty);
3651
3652
0
      default:
3653
0
        assumingUnreachable();
3654
1.09M
      }
3655
1.09M
      return;
3656
1.09M
    };
3657
1.63M
    for (const auto &Instr : Instrs) {
3658
      // Update instruction count
3659
1.63M
      if (LocalInstrCount) {
3660
0
        Builder.createStore(
3661
0
            Builder.createAdd(
3662
0
                Builder.createLoad(Context.Int64Ty, LocalInstrCount),
3663
0
                LLContext.getInt64(1)),
3664
0
            LocalInstrCount);
3665
0
      }
3666
1.63M
      if (LocalGas) {
3667
0
        auto NewGas = Builder.createAdd(
3668
0
            Builder.createLoad(Context.Int64Ty, LocalGas),
3669
0
            Builder.createLoad(
3670
0
                Context.Int64Ty,
3671
0
                Builder.createConstInBoundsGEP2_64(
3672
0
                    LLVM::Type::getArrayType(Context.Int64Ty, UINT16_MAX + 1),
3673
0
                    Context.getCostTable(Builder, ExecCtx), 0,
3674
0
                    uint16_t(Instr.getOpCode()))));
3675
0
        Builder.createStore(NewGas, LocalGas);
3676
0
      }
3677
3678
      // Make the instruction node according to Code.
3679
1.63M
      Dispatch(Instr);
3680
1.63M
    }
3681
9.82k
  }
3682
2.23k
  void compileSignedTrunc(LLVM::Type IntType) noexcept {
3683
2.23k
    auto NormBB = LLVM::BasicBlock::create(LLContext, F.Fn, "strunc.norm");
3684
2.23k
    auto NotMinBB = LLVM::BasicBlock::create(LLContext, F.Fn, "strunc.notmin");
3685
2.23k
    auto NotMaxBB = LLVM::BasicBlock::create(LLContext, F.Fn, "strunc.notmax");
3686
2.23k
    auto Value = stackPop();
3687
2.23k
    const auto [Precise, MinFp, MaxFp] =
3688
2.23k
        [IntType, Value]() -> std::tuple<bool, LLVM::Value, LLVM::Value> {
3689
2.23k
      const auto BitWidth = IntType.getIntegerBitWidth();
3690
2.23k
      const auto [Min, Max] = [BitWidth]() -> std::tuple<int64_t, int64_t> {
3691
2.23k
        switch (BitWidth) {
3692
1.74k
        case 32:
3693
1.74k
          return {std::numeric_limits<int32_t>::min(),
3694
1.74k
                  std::numeric_limits<int32_t>::max()};
3695
484
        case 64:
3696
484
          return {std::numeric_limits<int64_t>::min(),
3697
484
                  std::numeric_limits<int64_t>::max()};
3698
0
        default:
3699
0
          assumingUnreachable();
3700
2.23k
        }
3701
2.23k
      }();
3702
2.23k
      auto FPType = Value.getType();
3703
2.23k
      assuming(FPType.isFloatTy() || FPType.isDoubleTy());
3704
2.23k
      const auto FPWidth = FPType.getFPMantissaWidth();
3705
2.23k
      return {BitWidth <= FPWidth, LLVM::Value::getConstReal(FPType, Min),
3706
2.23k
              LLVM::Value::getConstReal(FPType, Max)};
3707
2.23k
    }();
3708
3709
2.23k
    auto IsNotNan = Builder.createLikely(Builder.createFCmpORD(Value, Value));
3710
2.23k
    Builder.createCondBr(IsNotNan, NormBB,
3711
2.23k
                         getTrapBB(ErrCode::Value::InvalidConvToInt));
3712
3713
2.23k
    Builder.positionAtEnd(NormBB);
3714
2.23k
    assuming(LLVM::Core::Trunc != LLVM::Core::NotIntrinsic);
3715
2.23k
    auto Trunc = Builder.createUnaryIntrinsic(LLVM::Core::Trunc, Value);
3716
2.23k
    auto IsNotUnderflow =
3717
2.23k
        Builder.createLikely(Builder.createFCmpOGE(Trunc, MinFp));
3718
2.23k
    Builder.createCondBr(IsNotUnderflow, NotMinBB,
3719
2.23k
                         getTrapBB(ErrCode::Value::IntegerOverflow));
3720
3721
2.23k
    Builder.positionAtEnd(NotMinBB);
3722
2.23k
    auto IsNotOverflow = Builder.createLikely(
3723
2.23k
        Builder.createFCmp(Precise ? LLVMRealOLE : LLVMRealOLT, Trunc, MaxFp));
3724
2.23k
    Builder.createCondBr(IsNotOverflow, NotMaxBB,
3725
2.23k
                         getTrapBB(ErrCode::Value::IntegerOverflow));
3726
3727
2.23k
    Builder.positionAtEnd(NotMaxBB);
3728
2.23k
    stackPush(Builder.createFPToSI(Trunc, IntType));
3729
2.23k
  }
3730
1.25k
  void compileSignedTruncSat(LLVM::Type IntType) noexcept {
3731
1.25k
    auto CurrBB = Builder.getInsertBlock();
3732
1.25k
    auto NormBB = LLVM::BasicBlock::create(LLContext, F.Fn, "ssat.norm");
3733
1.25k
    auto NotMinBB = LLVM::BasicBlock::create(LLContext, F.Fn, "ssat.notmin");
3734
1.25k
    auto NotMaxBB = LLVM::BasicBlock::create(LLContext, F.Fn, "ssat.notmax");
3735
1.25k
    auto EndBB = LLVM::BasicBlock::create(LLContext, F.Fn, "ssat.end");
3736
1.25k
    auto Value = stackPop();
3737
1.25k
    const auto [Precise, MinInt, MaxInt, MinFp, MaxFp] = [IntType, Value]()
3738
1.25k
        -> std::tuple<bool, uint64_t, uint64_t, LLVM::Value, LLVM::Value> {
3739
1.25k
      const auto BitWidth = IntType.getIntegerBitWidth();
3740
1.25k
      const auto [Min, Max] = [BitWidth]() -> std::tuple<int64_t, int64_t> {
3741
1.25k
        switch (BitWidth) {
3742
465
        case 32:
3743
465
          return {std::numeric_limits<int32_t>::min(),
3744
465
                  std::numeric_limits<int32_t>::max()};
3745
789
        case 64:
3746
789
          return {std::numeric_limits<int64_t>::min(),
3747
789
                  std::numeric_limits<int64_t>::max()};
3748
0
        default:
3749
0
          assumingUnreachable();
3750
1.25k
        }
3751
1.25k
      }();
3752
1.25k
      auto FPType = Value.getType();
3753
1.25k
      assuming(FPType.isFloatTy() || FPType.isDoubleTy());
3754
1.25k
      const auto FPWidth = FPType.getFPMantissaWidth();
3755
1.25k
      return {BitWidth <= FPWidth, static_cast<uint64_t>(Min),
3756
1.25k
              static_cast<uint64_t>(Max),
3757
1.25k
              LLVM::Value::getConstReal(FPType, Min),
3758
1.25k
              LLVM::Value::getConstReal(FPType, Max)};
3759
1.25k
    }();
3760
3761
1.25k
    auto IsNotNan = Builder.createLikely(Builder.createFCmpORD(Value, Value));
3762
1.25k
    Builder.createCondBr(IsNotNan, NormBB, EndBB);
3763
3764
1.25k
    Builder.positionAtEnd(NormBB);
3765
1.25k
    assuming(LLVM::Core::Trunc != LLVM::Core::NotIntrinsic);
3766
1.25k
    auto Trunc = Builder.createUnaryIntrinsic(LLVM::Core::Trunc, Value);
3767
1.25k
    auto IsNotUnderflow =
3768
1.25k
        Builder.createLikely(Builder.createFCmpOGE(Trunc, MinFp));
3769
1.25k
    Builder.createCondBr(IsNotUnderflow, NotMinBB, EndBB);
3770
3771
1.25k
    Builder.positionAtEnd(NotMinBB);
3772
1.25k
    auto IsNotOverflow = Builder.createLikely(
3773
1.25k
        Builder.createFCmp(Precise ? LLVMRealOLE : LLVMRealOLT, Trunc, MaxFp));
3774
1.25k
    Builder.createCondBr(IsNotOverflow, NotMaxBB, EndBB);
3775
3776
1.25k
    Builder.positionAtEnd(NotMaxBB);
3777
1.25k
    auto IntValue = Builder.createFPToSI(Trunc, IntType);
3778
1.25k
    Builder.createBr(EndBB);
3779
3780
1.25k
    Builder.positionAtEnd(EndBB);
3781
1.25k
    auto PHIRet = Builder.createPHI(IntType);
3782
1.25k
    PHIRet.addIncoming(LLVM::Value::getConstInt(IntType, 0, true), CurrBB);
3783
1.25k
    PHIRet.addIncoming(LLVM::Value::getConstInt(IntType, MinInt, true), NormBB);
3784
1.25k
    PHIRet.addIncoming(LLVM::Value::getConstInt(IntType, MaxInt, true),
3785
1.25k
                       NotMinBB);
3786
1.25k
    PHIRet.addIncoming(IntValue, NotMaxBB);
3787
3788
1.25k
    stackPush(PHIRet);
3789
1.25k
  }
3790
4.01k
  void compileUnsignedTrunc(LLVM::Type IntType) noexcept {
3791
4.01k
    auto NormBB = LLVM::BasicBlock::create(LLContext, F.Fn, "utrunc.norm");
3792
4.01k
    auto NotMinBB = LLVM::BasicBlock::create(LLContext, F.Fn, "utrunc.notmin");
3793
4.01k
    auto NotMaxBB = LLVM::BasicBlock::create(LLContext, F.Fn, "utrunc.notmax");
3794
4.01k
    auto Value = stackPop();
3795
4.01k
    const auto [Precise, MinFp, MaxFp] =
3796
4.01k
        [IntType, Value]() -> std::tuple<bool, LLVM::Value, LLVM::Value> {
3797
4.01k
      const auto BitWidth = IntType.getIntegerBitWidth();
3798
4.01k
      const auto [Min, Max] = [BitWidth]() -> std::tuple<uint64_t, uint64_t> {
3799
4.01k
        switch (BitWidth) {
3800
1.79k
        case 32:
3801
1.79k
          return {std::numeric_limits<uint32_t>::min(),
3802
1.79k
                  std::numeric_limits<uint32_t>::max()};
3803
2.21k
        case 64:
3804
2.21k
          return {std::numeric_limits<uint64_t>::min(),
3805
2.21k
                  std::numeric_limits<uint64_t>::max()};
3806
0
        default:
3807
0
          assumingUnreachable();
3808
4.01k
        }
3809
4.01k
      }();
3810
4.01k
      auto FPType = Value.getType();
3811
4.01k
      assuming(FPType.isFloatTy() || FPType.isDoubleTy());
3812
4.01k
      const auto FPWidth = FPType.getFPMantissaWidth();
3813
4.01k
      return {BitWidth <= FPWidth, LLVM::Value::getConstReal(FPType, Min),
3814
4.01k
              LLVM::Value::getConstReal(FPType, Max)};
3815
4.01k
    }();
3816
3817
4.01k
    auto IsNotNan = Builder.createLikely(Builder.createFCmpORD(Value, Value));
3818
4.01k
    Builder.createCondBr(IsNotNan, NormBB,
3819
4.01k
                         getTrapBB(ErrCode::Value::InvalidConvToInt));
3820
3821
4.01k
    Builder.positionAtEnd(NormBB);
3822
4.01k
    assuming(LLVM::Core::Trunc != LLVM::Core::NotIntrinsic);
3823
4.01k
    auto Trunc = Builder.createUnaryIntrinsic(LLVM::Core::Trunc, Value);
3824
4.01k
    auto IsNotUnderflow =
3825
4.01k
        Builder.createLikely(Builder.createFCmpOGE(Trunc, MinFp));
3826
4.01k
    Builder.createCondBr(IsNotUnderflow, NotMinBB,
3827
4.01k
                         getTrapBB(ErrCode::Value::IntegerOverflow));
3828
3829
4.01k
    Builder.positionAtEnd(NotMinBB);
3830
4.01k
    auto IsNotOverflow = Builder.createLikely(
3831
4.01k
        Builder.createFCmp(Precise ? LLVMRealOLE : LLVMRealOLT, Trunc, MaxFp));
3832
4.01k
    Builder.createCondBr(IsNotOverflow, NotMaxBB,
3833
4.01k
                         getTrapBB(ErrCode::Value::IntegerOverflow));
3834
3835
4.01k
    Builder.positionAtEnd(NotMaxBB);
3836
4.01k
    stackPush(Builder.createFPToUI(Trunc, IntType));
3837
4.01k
  }
3838
927
  void compileUnsignedTruncSat(LLVM::Type IntType) noexcept {
3839
927
    auto CurrBB = Builder.getInsertBlock();
3840
927
    auto NormBB = LLVM::BasicBlock::create(LLContext, F.Fn, "usat.norm");
3841
927
    auto NotMaxBB = LLVM::BasicBlock::create(LLContext, F.Fn, "usat.notmax");
3842
927
    auto EndBB = LLVM::BasicBlock::create(LLContext, F.Fn, "usat.end");
3843
927
    auto Value = stackPop();
3844
927
    const auto [Precise, MinInt, MaxInt, MinFp, MaxFp] = [IntType, Value]()
3845
927
        -> std::tuple<bool, uint64_t, uint64_t, LLVM::Value, LLVM::Value> {
3846
927
      const auto BitWidth = IntType.getIntegerBitWidth();
3847
927
      const auto [Min, Max] = [BitWidth]() -> std::tuple<uint64_t, uint64_t> {
3848
927
        switch (BitWidth) {
3849
254
        case 32:
3850
254
          return {std::numeric_limits<uint32_t>::min(),
3851
254
                  std::numeric_limits<uint32_t>::max()};
3852
673
        case 64:
3853
673
          return {std::numeric_limits<uint64_t>::min(),
3854
673
                  std::numeric_limits<uint64_t>::max()};
3855
0
        default:
3856
0
          assumingUnreachable();
3857
927
        }
3858
927
      }();
3859
927
      auto FPType = Value.getType();
3860
927
      assuming(FPType.isFloatTy() || FPType.isDoubleTy());
3861
927
      const auto FPWidth = FPType.getFPMantissaWidth();
3862
927
      return {BitWidth <= FPWidth, Min, Max,
3863
927
              LLVM::Value::getConstReal(FPType, Min),
3864
927
              LLVM::Value::getConstReal(FPType, Max)};
3865
927
    }();
3866
3867
927
    assuming(LLVM::Core::Trunc != LLVM::Core::NotIntrinsic);
3868
927
    auto Trunc = Builder.createUnaryIntrinsic(LLVM::Core::Trunc, Value);
3869
927
    auto IsNotUnderflow =
3870
927
        Builder.createLikely(Builder.createFCmpOGE(Trunc, MinFp));
3871
927
    Builder.createCondBr(IsNotUnderflow, NormBB, EndBB);
3872
3873
927
    Builder.positionAtEnd(NormBB);
3874
927
    auto IsNotOverflow = Builder.createLikely(
3875
927
        Builder.createFCmp(Precise ? LLVMRealOLE : LLVMRealOLT, Trunc, MaxFp));
3876
927
    Builder.createCondBr(IsNotOverflow, NotMaxBB, EndBB);
3877
3878
927
    Builder.positionAtEnd(NotMaxBB);
3879
927
    auto IntValue = Builder.createFPToUI(Trunc, IntType);
3880
927
    Builder.createBr(EndBB);
3881
3882
927
    Builder.positionAtEnd(EndBB);
3883
927
    auto PHIRet = Builder.createPHI(IntType);
3884
927
    PHIRet.addIncoming(LLVM::Value::getConstInt(IntType, MinInt), CurrBB);
3885
927
    PHIRet.addIncoming(LLVM::Value::getConstInt(IntType, MaxInt), NormBB);
3886
927
    PHIRet.addIncoming(IntValue, NotMaxBB);
3887
3888
927
    stackPush(PHIRet);
3889
927
  }
3890
3891
  void compileAtomicCheckOffsetAlignment(LLVM::Value Offset,
3892
40
                                         LLVM::Type IntType) noexcept {
3893
40
    const auto BitWidth = IntType.getIntegerBitWidth();
3894
40
    auto BWMask = LLContext.getInt64((BitWidth >> 3) - 1);
3895
40
    auto Value = Builder.createAnd(Offset, BWMask);
3896
40
    auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "address_align_ok");
3897
40
    auto IsAddressAligned = Builder.createLikely(
3898
40
        Builder.createICmpEQ(Value, LLContext.getInt64(0)));
3899
40
    Builder.createCondBr(IsAddressAligned, OkBB,
3900
40
                         getTrapBB(ErrCode::Value::UnalignedAtomicAccess));
3901
3902
40
    Builder.positionAtEnd(OkBB);
3903
40
  }
3904
3905
204
  void compileMemoryFence() noexcept {
3906
204
    Builder.createFence(LLVMAtomicOrderingSequentiallyConsistent);
3907
204
  }
3908
  void compileAtomicNotify(unsigned MemoryIndex,
3909
33
                           unsigned MemoryOffset) noexcept {
3910
33
    auto Count = stackPop();
3911
33
    auto Addr = Builder.createZExt(Stack.back(), Context.Int64Ty);
3912
33
    if (MemoryOffset != 0) {
3913
26
      Addr = Builder.createAdd(Addr, LLContext.getInt64(MemoryOffset));
3914
26
    }
3915
33
    compileAtomicCheckOffsetAlignment(Addr, Context.Int32Ty);
3916
33
    auto Offset = stackPop();
3917
3918
33
    stackPush(Builder.createCall(
3919
33
        Context.getIntrinsic(
3920
33
            Builder, Executable::Intrinsics::kMemAtomicNotify,
3921
33
            LLVM::Type::getFunctionType(
3922
33
                Context.Int32Ty,
3923
33
                {Context.Int32Ty, Context.Int32Ty, Context.Int32Ty}, false)),
3924
33
        {LLContext.getInt32(MemoryIndex), Offset, Count}));
3925
33
  }
3926
  void compileAtomicWait(unsigned MemoryIndex, unsigned MemoryOffset,
3927
7
                         LLVM::Type TargetType, uint32_t BitWidth) noexcept {
3928
7
    auto Timeout = stackPop();
3929
7
    auto ExpectedValue = Builder.createZExtOrTrunc(stackPop(), Context.Int64Ty);
3930
7
    auto Addr = Builder.createZExt(Stack.back(), Context.Int64Ty);
3931
7
    if (MemoryOffset != 0) {
3932
3
      Addr = Builder.createAdd(Addr, LLContext.getInt64(MemoryOffset));
3933
3
    }
3934
7
    compileAtomicCheckOffsetAlignment(Addr, TargetType);
3935
7
    auto Offset = stackPop();
3936
3937
7
    stackPush(Builder.createCall(
3938
7
        Context.getIntrinsic(
3939
7
            Builder, Executable::Intrinsics::kMemAtomicWait,
3940
7
            LLVM::Type::getFunctionType(Context.Int32Ty,
3941
7
                                        {Context.Int32Ty, Context.Int32Ty,
3942
7
                                         Context.Int64Ty, Context.Int64Ty,
3943
7
                                         Context.Int32Ty},
3944
7
                                        false)),
3945
7
        {LLContext.getInt32(MemoryIndex), Offset, ExpectedValue, Timeout,
3946
7
         LLContext.getInt32(BitWidth)}));
3947
7
  }
3948
  void compileAtomicLoad(unsigned MemoryIndex, unsigned MemoryOffset,
3949
                         unsigned Alignment, LLVM::Type IntType,
3950
0
                         LLVM::Type TargetType, bool Signed = false) noexcept {
3951
3952
0
    auto Offset = Builder.createZExt(Stack.back(), Context.Int64Ty);
3953
0
    if (MemoryOffset != 0) {
3954
0
      Offset = Builder.createAdd(Offset, LLContext.getInt64(MemoryOffset));
3955
0
    }
3956
0
    compileAtomicCheckOffsetAlignment(Offset, TargetType);
3957
0
    auto VPtr = Builder.createInBoundsGEP1(
3958
0
        Context.Int8Ty, Context.getMemory(Builder, ExecCtx, MemoryIndex),
3959
0
        Offset);
3960
3961
0
    auto Ptr = Builder.createBitCast(VPtr, TargetType.getPointerTo());
3962
0
    auto Load = switchEndian(Builder.createLoad(TargetType, Ptr, true));
3963
0
    Load.setAlignment(1 << Alignment);
3964
0
    Load.setOrdering(LLVMAtomicOrderingSequentiallyConsistent);
3965
3966
0
    if (Signed) {
3967
0
      Stack.back() = Builder.createSExt(Load, IntType);
3968
0
    } else {
3969
0
      Stack.back() = Builder.createZExt(Load, IntType);
3970
0
    }
3971
0
  }
3972
  void compileAtomicStore(unsigned MemoryIndex, unsigned MemoryOffset,
3973
                          unsigned Alignment, LLVM::Type, LLVM::Type TargetType,
3974
0
                          bool Signed = false) noexcept {
3975
0
    auto V = stackPop();
3976
3977
0
    if (Signed) {
3978
0
      V = Builder.createSExtOrTrunc(V, TargetType);
3979
0
    } else {
3980
0
      V = Builder.createZExtOrTrunc(V, TargetType);
3981
0
    }
3982
0
    V = switchEndian(V);
3983
0
    auto Offset = Builder.createZExt(Stack.back(), Context.Int64Ty);
3984
0
    if (MemoryOffset != 0) {
3985
0
      Offset = Builder.createAdd(Offset, LLContext.getInt64(MemoryOffset));
3986
0
    }
3987
0
    compileAtomicCheckOffsetAlignment(Offset, TargetType);
3988
0
    auto VPtr = Builder.createInBoundsGEP1(
3989
0
        Context.Int8Ty, Context.getMemory(Builder, ExecCtx, MemoryIndex),
3990
0
        Offset);
3991
0
    auto Ptr = Builder.createBitCast(VPtr, TargetType.getPointerTo());
3992
0
    auto Store = Builder.createStore(V, Ptr, true);
3993
0
    Store.setAlignment(1 << Alignment);
3994
0
    Store.setOrdering(LLVMAtomicOrderingSequentiallyConsistent);
3995
0
  }
3996
3997
  void compileAtomicRMWOp(unsigned MemoryIndex, unsigned MemoryOffset,
3998
                          [[maybe_unused]] unsigned Alignment,
3999
                          LLVMAtomicRMWBinOp BinOp, LLVM::Type IntType,
4000
0
                          LLVM::Type TargetType, bool Signed = false) noexcept {
4001
0
    auto Value = Builder.createSExtOrTrunc(stackPop(), TargetType);
4002
0
    auto Offset = Builder.createZExt(Stack.back(), Context.Int64Ty);
4003
0
    if (MemoryOffset != 0) {
4004
0
      Offset = Builder.createAdd(Offset, LLContext.getInt64(MemoryOffset));
4005
0
    }
4006
0
    compileAtomicCheckOffsetAlignment(Offset, TargetType);
4007
0
    auto VPtr = Builder.createInBoundsGEP1(
4008
0
        Context.Int8Ty, Context.getMemory(Builder, ExecCtx, MemoryIndex),
4009
0
        Offset);
4010
0
    auto Ptr = Builder.createBitCast(VPtr, TargetType.getPointerTo());
4011
4012
0
    LLVM::Value Ret;
4013
    if constexpr (Endian::native == Endian::big) {
4014
      if (BinOp == LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpAdd ||
4015
          BinOp == LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpSub) {
4016
        auto AtomicBB = LLVM::BasicBlock::create(LLContext, F.Fn, "atomic.rmw");
4017
        auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "atomic.rmw.ok");
4018
        Builder.createBr(AtomicBB);
4019
        Builder.positionAtEnd(AtomicBB);
4020
4021
        auto Load = Builder.createLoad(TargetType, Ptr, true);
4022
        Load.setOrdering(LLVMAtomicOrderingMonotonic);
4023
        Load.setAlignment(1 << Alignment);
4024
4025
        LLVM::Value New;
4026
        if (BinOp == LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpAdd)
4027
          New = Builder.createAdd(switchEndian(Load), Value);
4028
        else if (BinOp == LLVMAtomicRMWBinOp::LLVMAtomicRMWBinOpSub) {
4029
          New = Builder.createSub(switchEndian(Load), Value);
4030
        } else {
4031
          assumingUnreachable();
4032
        }
4033
        New = switchEndian(New);
4034
4035
        auto Exchange = Builder.createAtomicCmpXchg(
4036
            Ptr, Load, New, LLVMAtomicOrderingSequentiallyConsistent,
4037
            LLVMAtomicOrderingSequentiallyConsistent);
4038
4039
        Ret = Builder.createExtractValue(Exchange, 0);
4040
        auto Success = Builder.createExtractValue(Exchange, 1);
4041
        Builder.createCondBr(Success, OkBB, AtomicBB);
4042
        Builder.positionAtEnd(OkBB);
4043
      } else {
4044
        Ret = Builder.createAtomicRMW(BinOp, Ptr, switchEndian(Value),
4045
                                      LLVMAtomicOrderingSequentiallyConsistent);
4046
      }
4047
0
    } else {
4048
0
      Ret = Builder.createAtomicRMW(BinOp, Ptr, switchEndian(Value),
4049
0
                                    LLVMAtomicOrderingSequentiallyConsistent);
4050
0
    }
4051
0
    Ret = switchEndian(Ret);
4052
#if LLVM_VERSION_MAJOR >= 13
4053
    Ret.setAlignment(1 << Alignment);
4054
#endif
4055
0
    if (Signed) {
4056
0
      Stack.back() = Builder.createSExt(Ret, IntType);
4057
0
    } else {
4058
0
      Stack.back() = Builder.createZExt(Ret, IntType);
4059
0
    }
4060
0
  }
4061
  void compileAtomicCompareExchange(unsigned MemoryIndex, unsigned MemoryOffset,
4062
                                    [[maybe_unused]] unsigned Alignment,
4063
                                    LLVM::Type IntType, LLVM::Type TargetType,
4064
0
                                    bool Signed = false) noexcept {
4065
4066
0
    auto Replacement = Builder.createSExtOrTrunc(stackPop(), TargetType);
4067
0
    auto Expected = Builder.createSExtOrTrunc(stackPop(), TargetType);
4068
0
    auto Offset = Builder.createZExt(Stack.back(), Context.Int64Ty);
4069
0
    if (MemoryOffset != 0) {
4070
0
      Offset = Builder.createAdd(Offset, LLContext.getInt64(MemoryOffset));
4071
0
    }
4072
0
    compileAtomicCheckOffsetAlignment(Offset, TargetType);
4073
0
    auto VPtr = Builder.createInBoundsGEP1(
4074
0
        Context.Int8Ty, Context.getMemory(Builder, ExecCtx, MemoryIndex),
4075
0
        Offset);
4076
0
    auto Ptr = Builder.createBitCast(VPtr, TargetType.getPointerTo());
4077
4078
0
    auto Ret = Builder.createAtomicCmpXchg(
4079
0
        Ptr, switchEndian(Expected), switchEndian(Replacement),
4080
0
        LLVMAtomicOrderingSequentiallyConsistent,
4081
0
        LLVMAtomicOrderingSequentiallyConsistent);
4082
#if LLVM_VERSION_MAJOR >= 13
4083
    Ret.setAlignment(1 << Alignment);
4084
#endif
4085
0
    auto OldVal = Builder.createExtractValue(Ret, 0);
4086
0
    OldVal = switchEndian(OldVal);
4087
0
    if (Signed) {
4088
0
      Stack.back() = Builder.createSExt(OldVal, IntType);
4089
0
    } else {
4090
0
      Stack.back() = Builder.createZExt(OldVal, IntType);
4091
0
    }
4092
0
  }
4093
4094
10.5k
  void compileReturn() noexcept {
4095
10.5k
    updateInstrCount();
4096
10.5k
    updateGas();
4097
10.5k
    auto Ty = F.Ty.getReturnType();
4098
10.5k
    if (Ty.isVoidTy()) {
4099
1.89k
      Builder.createRetVoid();
4100
8.63k
    } else if (Ty.isStructTy()) {
4101
313
      const auto Count = Ty.getStructNumElements();
4102
313
      std::vector<LLVM::Value> Ret(Count);
4103
1.19k
      for (unsigned I = 0; I < Count; ++I) {
4104
879
        const unsigned J = Count - 1 - I;
4105
879
        Ret[J] = stackPop();
4106
879
      }
4107
313
      Builder.createAggregateRet(Ret);
4108
8.32k
    } else {
4109
8.32k
      Builder.createRet(stackPop());
4110
8.32k
    }
4111
10.5k
  }
4112
4113
17.0k
  void updateInstrCount() noexcept {
4114
17.0k
    if (LocalInstrCount) {
4115
0
      auto Store [[maybe_unused]] = Builder.createAtomicRMW(
4116
0
          LLVMAtomicRMWBinOpAdd, Context.getInstrCount(Builder, ExecCtx),
4117
0
          Builder.createLoad(Context.Int64Ty, LocalInstrCount),
4118
0
          LLVMAtomicOrderingMonotonic);
4119
#if LLVM_VERSION_MAJOR >= 13
4120
      Store.setAlignment(8);
4121
#endif
4122
0
      Builder.createStore(LLContext.getInt64(0), LocalInstrCount);
4123
0
    }
4124
17.0k
  }
4125
4126
18.9k
  void updateGas() noexcept {
4127
18.9k
    if (LocalGas) {
4128
0
      auto CurrBB = Builder.getInsertBlock();
4129
0
      auto CheckBB = LLVM::BasicBlock::create(LLContext, F.Fn, "gas_check");
4130
0
      auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "gas_ok");
4131
0
      auto EndBB = LLVM::BasicBlock::create(LLContext, F.Fn, "gas_end");
4132
4133
0
      auto Cost = Builder.createLoad(Context.Int64Ty, LocalGas);
4134
0
      Cost.setAlignment(64);
4135
0
      auto GasPtr = Context.getGas(Builder, ExecCtx);
4136
0
      auto GasLimit = Context.getGasLimit(Builder, ExecCtx);
4137
0
      auto Gas = Builder.createLoad(Context.Int64Ty, GasPtr);
4138
0
      Gas.setAlignment(64);
4139
0
      Gas.setOrdering(LLVMAtomicOrderingMonotonic);
4140
0
      Builder.createBr(CheckBB);
4141
0
      Builder.positionAtEnd(CheckBB);
4142
4143
0
      auto PHIOldGas = Builder.createPHI(Context.Int64Ty);
4144
0
      auto NewGas = Builder.createAdd(PHIOldGas, Cost);
4145
0
      auto IsGasRemain =
4146
0
          Builder.createLikely(Builder.createICmpULE(NewGas, GasLimit));
4147
0
      Builder.createCondBr(IsGasRemain, OkBB,
4148
0
                           getTrapBB(ErrCode::Value::CostLimitExceeded));
4149
0
      Builder.positionAtEnd(OkBB);
4150
4151
0
      auto RGasAndSucceed = Builder.createAtomicCmpXchg(
4152
0
          GasPtr, PHIOldGas, NewGas, LLVMAtomicOrderingMonotonic,
4153
0
          LLVMAtomicOrderingMonotonic);
4154
#if LLVM_VERSION_MAJOR >= 13
4155
      RGasAndSucceed.setAlignment(8);
4156
#endif
4157
0
      RGasAndSucceed.setWeak(true);
4158
0
      auto RGas = Builder.createExtractValue(RGasAndSucceed, 0);
4159
0
      auto Succeed = Builder.createExtractValue(RGasAndSucceed, 1);
4160
0
      Builder.createCondBr(Builder.createLikely(Succeed), EndBB, CheckBB);
4161
0
      Builder.positionAtEnd(EndBB);
4162
4163
0
      Builder.createStore(LLContext.getInt64(0), LocalGas);
4164
4165
0
      PHIOldGas.addIncoming(Gas, CurrBB);
4166
0
      PHIOldGas.addIncoming(RGas, OkBB);
4167
0
    }
4168
18.9k
  }
4169
4170
2.93k
  void updateGasAtTrap() noexcept {
4171
2.93k
    if (LocalGas) {
4172
0
      auto Update [[maybe_unused]] = Builder.createAtomicRMW(
4173
0
          LLVMAtomicRMWBinOpAdd, Context.getGas(Builder, ExecCtx),
4174
0
          Builder.createLoad(Context.Int64Ty, LocalGas),
4175
0
          LLVMAtomicOrderingMonotonic);
4176
#if LLVM_VERSION_MAJOR >= 13
4177
      Update.setAlignment(8);
4178
#endif
4179
0
    }
4180
2.93k
  }
4181
4182
private:
4183
3.05k
  void compileCallOp(const unsigned int FuncIndex) noexcept {
4184
3.05k
    const auto &FuncType =
4185
3.05k
        Context.CompositeTypes[std::get<0>(Context.Functions[FuncIndex])]
4186
3.05k
            ->getFuncType();
4187
3.05k
    const auto &Function = std::get<1>(Context.Functions[FuncIndex]);
4188
3.05k
    const auto &ParamTypes = FuncType.getParamTypes();
4189
4190
3.05k
    std::vector<LLVM::Value> Args(ParamTypes.size() + 1);
4191
3.05k
    Args[0] = F.Fn.getFirstParam();
4192
3.85k
    for (size_t I = 0; I < ParamTypes.size(); ++I) {
4193
801
      const size_t J = ParamTypes.size() - 1 - I;
4194
801
      Args[J + 1] = stackPop();
4195
801
    }
4196
4197
3.05k
    auto Ret = Builder.createCall(Function, Args);
4198
3.05k
    auto Ty = Ret.getType();
4199
3.05k
    if (Ty.isVoidTy()) {
4200
      // nothing to do
4201
1.78k
    } else if (Ty.isStructTy()) {
4202
153
      for (auto Val : unpackStruct(Builder, Ret)) {
4203
153
        stackPush(Val);
4204
153
      }
4205
1.19k
    } else {
4206
1.19k
      stackPush(Ret);
4207
1.19k
    }
4208
3.05k
  }
4209
4210
  void compileIndirectCallOp(const uint32_t TableIndex,
4211
583
                             const uint32_t FuncTypeIndex) noexcept {
4212
583
    auto NotNullBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_i.not_null");
4213
583
    auto IsNullBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_i.is_null");
4214
583
    auto EndBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_i.end");
4215
4216
583
    LLVM::Value FuncIndex = stackPop();
4217
583
    const auto &FuncType = Context.CompositeTypes[FuncTypeIndex]->getFuncType();
4218
583
    auto FTy = toLLVMType(Context.LLContext, Context.ExecCtxPtrTy, FuncType);
4219
583
    auto RTy = FTy.getReturnType();
4220
4221
583
    const size_t ArgSize = FuncType.getParamTypes().size();
4222
583
    const size_t RetSize =
4223
583
        RTy.isVoidTy() ? 0 : FuncType.getReturnTypes().size();
4224
583
    std::vector<LLVM::Value> ArgsVec(ArgSize + 1, nullptr);
4225
583
    ArgsVec[0] = F.Fn.getFirstParam();
4226
1.15k
    for (size_t I = 0; I < ArgSize; ++I) {
4227
576
      const size_t J = ArgSize - I;
4228
576
      ArgsVec[J] = stackPop();
4229
576
    }
4230
4231
583
    std::vector<LLVM::Value> FPtrRetsVec;
4232
583
    FPtrRetsVec.reserve(RetSize);
4233
583
    {
4234
583
      auto FPtr = Builder.createCall(
4235
583
          Context.getIntrinsic(
4236
583
              Builder, Executable::Intrinsics::kTableGetFuncSymbol,
4237
583
              LLVM::Type::getFunctionType(
4238
583
                  FTy.getPointerTo(),
4239
583
                  {Context.Int32Ty, Context.Int32Ty, Context.Int32Ty}, false)),
4240
583
          {LLContext.getInt32(TableIndex), LLContext.getInt32(FuncTypeIndex),
4241
583
           FuncIndex});
4242
583
      Builder.createCondBr(
4243
583
          Builder.createLikely(Builder.createNot(Builder.createIsNull(FPtr))),
4244
583
          NotNullBB, IsNullBB);
4245
583
      Builder.positionAtEnd(NotNullBB);
4246
4247
583
      auto FPtrRet =
4248
583
          Builder.createCall(LLVM::FunctionCallee{FTy, FPtr}, ArgsVec);
4249
583
      if (RetSize == 0) {
4250
        // nothing to do
4251
448
      } else if (RetSize == 1) {
4252
436
        FPtrRetsVec.push_back(FPtrRet);
4253
436
      } else {
4254
24
        for (auto Val : unpackStruct(Builder, FPtrRet)) {
4255
24
          FPtrRetsVec.push_back(Val);
4256
24
        }
4257
12
      }
4258
583
    }
4259
4260
583
    Builder.createBr(EndBB);
4261
583
    Builder.positionAtEnd(IsNullBB);
4262
4263
583
    std::vector<LLVM::Value> RetsVec;
4264
583
    {
4265
583
      LLVM::Value Args = Builder.createArray(ArgSize, kValSize);
4266
583
      LLVM::Value Rets = Builder.createArray(RetSize, kValSize);
4267
583
      Builder.createArrayPtrStore(
4268
583
          Span<LLVM::Value>(ArgsVec.begin() + 1, ArgSize), Args, Context.Int8Ty,
4269
583
          kValSize);
4270
4271
583
      Builder.createCall(
4272
583
          Context.getIntrinsic(
4273
583
              Builder, Executable::Intrinsics::kCallIndirect,
4274
583
              LLVM::Type::getFunctionType(Context.VoidTy,
4275
583
                                          {Context.Int32Ty, Context.Int32Ty,
4276
583
                                           Context.Int32Ty, Context.Int8PtrTy,
4277
583
                                           Context.Int8PtrTy},
4278
583
                                          false)),
4279
583
          {LLContext.getInt32(TableIndex), LLContext.getInt32(FuncTypeIndex),
4280
583
           FuncIndex, Args, Rets});
4281
4282
583
      if (RetSize == 0) {
4283
        // nothing to do
4284
448
      } else if (RetSize == 1) {
4285
436
        RetsVec.push_back(
4286
436
            Builder.createValuePtrLoad(RTy, Rets, Context.Int8Ty));
4287
436
      } else {
4288
12
        RetsVec = Builder.createArrayPtrLoad(RetSize, RTy, Rets, Context.Int8Ty,
4289
12
                                             kValSize);
4290
12
      }
4291
583
      Builder.createBr(EndBB);
4292
583
      Builder.positionAtEnd(EndBB);
4293
583
    }
4294
4295
1.04k
    for (unsigned I = 0; I < RetSize; ++I) {
4296
460
      auto PHIRet = Builder.createPHI(FPtrRetsVec[I].getType());
4297
460
      PHIRet.addIncoming(FPtrRetsVec[I], NotNullBB);
4298
460
      PHIRet.addIncoming(RetsVec[I], IsNullBB);
4299
460
      stackPush(PHIRet);
4300
460
    }
4301
583
  }
4302
4303
0
  void compileReturnCallOp(const unsigned int FuncIndex) noexcept {
4304
0
    const auto &FuncType =
4305
0
        Context.CompositeTypes[std::get<0>(Context.Functions[FuncIndex])]
4306
0
            ->getFuncType();
4307
0
    const auto &Function = std::get<1>(Context.Functions[FuncIndex]);
4308
0
    const auto &ParamTypes = FuncType.getParamTypes();
4309
4310
0
    std::vector<LLVM::Value> Args(ParamTypes.size() + 1);
4311
0
    Args[0] = F.Fn.getFirstParam();
4312
0
    for (size_t I = 0; I < ParamTypes.size(); ++I) {
4313
0
      const size_t J = ParamTypes.size() - 1 - I;
4314
0
      Args[J + 1] = stackPop();
4315
0
    }
4316
4317
0
    auto Ret = Builder.createCall(Function, Args);
4318
0
    auto Ty = Ret.getType();
4319
0
    if (Ty.isVoidTy()) {
4320
0
      Builder.createRetVoid();
4321
0
    } else {
4322
0
      Builder.createRet(Ret);
4323
0
    }
4324
0
  }
4325
4326
  void compileReturnIndirectCallOp(const uint32_t TableIndex,
4327
0
                                   const uint32_t FuncTypeIndex) noexcept {
4328
0
    auto NotNullBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_i.not_null");
4329
0
    auto IsNullBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_i.is_null");
4330
4331
0
    LLVM::Value FuncIndex = stackPop();
4332
0
    const auto &FuncType = Context.CompositeTypes[FuncTypeIndex]->getFuncType();
4333
0
    auto FTy = toLLVMType(Context.LLContext, Context.ExecCtxPtrTy, FuncType);
4334
0
    auto RTy = FTy.getReturnType();
4335
4336
0
    const size_t ArgSize = FuncType.getParamTypes().size();
4337
0
    const size_t RetSize =
4338
0
        RTy.isVoidTy() ? 0 : FuncType.getReturnTypes().size();
4339
0
    std::vector<LLVM::Value> ArgsVec(ArgSize + 1, nullptr);
4340
0
    ArgsVec[0] = F.Fn.getFirstParam();
4341
0
    for (size_t I = 0; I < ArgSize; ++I) {
4342
0
      const size_t J = ArgSize - I;
4343
0
      ArgsVec[J] = stackPop();
4344
0
    }
4345
4346
0
    {
4347
0
      auto FPtr = Builder.createCall(
4348
0
          Context.getIntrinsic(
4349
0
              Builder, Executable::Intrinsics::kTableGetFuncSymbol,
4350
0
              LLVM::Type::getFunctionType(
4351
0
                  FTy.getPointerTo(),
4352
0
                  {Context.Int32Ty, Context.Int32Ty, Context.Int32Ty}, false)),
4353
0
          {LLContext.getInt32(TableIndex), LLContext.getInt32(FuncTypeIndex),
4354
0
           FuncIndex});
4355
0
      Builder.createCondBr(
4356
0
          Builder.createLikely(Builder.createNot(Builder.createIsNull(FPtr))),
4357
0
          NotNullBB, IsNullBB);
4358
0
      Builder.positionAtEnd(NotNullBB);
4359
4360
0
      auto FPtrRet =
4361
0
          Builder.createCall(LLVM::FunctionCallee(FTy, FPtr), ArgsVec);
4362
0
      if (RetSize == 0) {
4363
0
        Builder.createRetVoid();
4364
0
      } else {
4365
0
        Builder.createRet(FPtrRet);
4366
0
      }
4367
0
    }
4368
4369
0
    Builder.positionAtEnd(IsNullBB);
4370
4371
0
    {
4372
0
      LLVM::Value Args = Builder.createArray(ArgSize, kValSize);
4373
0
      LLVM::Value Rets = Builder.createArray(RetSize, kValSize);
4374
0
      Builder.createArrayPtrStore(
4375
0
          Span<LLVM::Value>(ArgsVec.begin() + 1, ArgSize), Args, Context.Int8Ty,
4376
0
          kValSize);
4377
4378
0
      Builder.createCall(
4379
0
          Context.getIntrinsic(
4380
0
              Builder, Executable::Intrinsics::kCallIndirect,
4381
0
              LLVM::Type::getFunctionType(Context.VoidTy,
4382
0
                                          {Context.Int32Ty, Context.Int32Ty,
4383
0
                                           Context.Int32Ty, Context.Int8PtrTy,
4384
0
                                           Context.Int8PtrTy},
4385
0
                                          false)),
4386
0
          {LLContext.getInt32(TableIndex), LLContext.getInt32(FuncTypeIndex),
4387
0
           FuncIndex, Args, Rets});
4388
4389
0
      if (RetSize == 0) {
4390
0
        Builder.createRetVoid();
4391
0
      } else if (RetSize == 1) {
4392
0
        Builder.createRet(
4393
0
            Builder.createValuePtrLoad(RTy, Rets, Context.Int8Ty));
4394
0
      } else {
4395
0
        Builder.createAggregateRet(Builder.createArrayPtrLoad(
4396
0
            RetSize, RTy, Rets, Context.Int8Ty, kValSize));
4397
0
      }
4398
0
    }
4399
0
  }
4400
4401
0
  void compileCallRefOp(const unsigned int TypeIndex) noexcept {
4402
0
    auto NotNullBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_r.not_null");
4403
0
    auto IsNullBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_r.is_null");
4404
0
    auto EndBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_i.end");
4405
4406
0
    auto Ref = Builder.createBitCast(stackPop(), Context.Int64x2Ty);
4407
0
    auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_r.ref_not_null");
4408
0
    auto IsRefNotNull = Builder.createLikely(Builder.createICmpNE(
4409
0
        Builder.createExtractElement(Ref, LLContext.getInt64(1)),
4410
0
        LLContext.getInt64(0)));
4411
0
    Builder.createCondBr(IsRefNotNull, OkBB,
4412
0
                         getTrapBB(ErrCode::Value::AccessNullFunc));
4413
0
    Builder.positionAtEnd(OkBB);
4414
4415
0
    const auto &FuncType = Context.CompositeTypes[TypeIndex]->getFuncType();
4416
0
    auto FTy = toLLVMType(Context.LLContext, Context.ExecCtxPtrTy, FuncType);
4417
0
    auto RTy = FTy.getReturnType();
4418
4419
0
    const size_t ArgSize = FuncType.getParamTypes().size();
4420
0
    const size_t RetSize =
4421
0
        RTy.isVoidTy() ? 0 : FuncType.getReturnTypes().size();
4422
0
    std::vector<LLVM::Value> ArgsVec(ArgSize + 1, nullptr);
4423
0
    ArgsVec[0] = F.Fn.getFirstParam();
4424
0
    for (size_t I = 0; I < ArgSize; ++I) {
4425
0
      const size_t J = ArgSize - I;
4426
0
      ArgsVec[J] = stackPop();
4427
0
    }
4428
4429
0
    std::vector<LLVM::Value> FPtrRetsVec;
4430
0
    FPtrRetsVec.reserve(RetSize);
4431
0
    {
4432
0
      auto FPtr = Builder.createCall(
4433
0
          Context.getIntrinsic(
4434
0
              Builder, Executable::Intrinsics::kRefGetFuncSymbol,
4435
0
              LLVM::Type::getFunctionType(FTy.getPointerTo(),
4436
0
                                          {Context.Int64x2Ty}, false)),
4437
0
          {Ref});
4438
0
      Builder.createCondBr(
4439
0
          Builder.createLikely(Builder.createNot(Builder.createIsNull(FPtr))),
4440
0
          NotNullBB, IsNullBB);
4441
0
      Builder.positionAtEnd(NotNullBB);
4442
4443
0
      auto FPtrRet =
4444
0
          Builder.createCall(LLVM::FunctionCallee{FTy, FPtr}, ArgsVec);
4445
0
      if (RetSize == 0) {
4446
        // nothing to do
4447
0
      } else if (RetSize == 1) {
4448
0
        FPtrRetsVec.push_back(FPtrRet);
4449
0
      } else {
4450
0
        for (auto Val : unpackStruct(Builder, FPtrRet)) {
4451
0
          FPtrRetsVec.push_back(Val);
4452
0
        }
4453
0
      }
4454
0
    }
4455
4456
0
    Builder.createBr(EndBB);
4457
0
    Builder.positionAtEnd(IsNullBB);
4458
4459
0
    std::vector<LLVM::Value> RetsVec;
4460
0
    {
4461
0
      LLVM::Value Args = Builder.createArray(ArgSize, kValSize);
4462
0
      LLVM::Value Rets = Builder.createArray(RetSize, kValSize);
4463
0
      Builder.createArrayPtrStore(
4464
0
          Span<LLVM::Value>(ArgsVec.begin() + 1, ArgSize), Args, Context.Int8Ty,
4465
0
          kValSize);
4466
4467
0
      Builder.createCall(
4468
0
          Context.getIntrinsic(
4469
0
              Builder, Executable::Intrinsics::kCallRef,
4470
0
              LLVM::Type::getFunctionType(
4471
0
                  Context.VoidTy,
4472
0
                  {Context.Int64x2Ty, Context.Int8PtrTy, Context.Int8PtrTy},
4473
0
                  false)),
4474
0
          {Ref, Args, Rets});
4475
4476
0
      if (RetSize == 0) {
4477
        // nothing to do
4478
0
      } else if (RetSize == 1) {
4479
0
        RetsVec.push_back(
4480
0
            Builder.createValuePtrLoad(RTy, Rets, Context.Int8Ty));
4481
0
      } else {
4482
0
        RetsVec = Builder.createArrayPtrLoad(RetSize, RTy, Rets, Context.Int8Ty,
4483
0
                                             kValSize);
4484
0
      }
4485
0
      Builder.createBr(EndBB);
4486
0
      Builder.positionAtEnd(EndBB);
4487
0
    }
4488
4489
0
    for (unsigned I = 0; I < RetSize; ++I) {
4490
0
      auto PHIRet = Builder.createPHI(FPtrRetsVec[I].getType());
4491
0
      PHIRet.addIncoming(FPtrRetsVec[I], NotNullBB);
4492
0
      PHIRet.addIncoming(RetsVec[I], IsNullBB);
4493
0
      stackPush(PHIRet);
4494
0
    }
4495
0
  }
4496
4497
0
  void compileReturnCallRefOp(const unsigned int TypeIndex) noexcept {
4498
0
    auto NotNullBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_r.not_null");
4499
0
    auto IsNullBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_r.is_null");
4500
4501
0
    auto Ref = Builder.createBitCast(stackPop(), Context.Int64x2Ty);
4502
0
    auto OkBB = LLVM::BasicBlock::create(LLContext, F.Fn, "c_r.ref_not_null");
4503
0
    auto IsRefNotNull = Builder.createLikely(Builder.createICmpNE(
4504
0
        Builder.createExtractElement(Ref, LLContext.getInt64(1)),
4505
0
        LLContext.getInt64(0)));
4506
0
    Builder.createCondBr(IsRefNotNull, OkBB,
4507
0
                         getTrapBB(ErrCode::Value::AccessNullFunc));
4508
0
    Builder.positionAtEnd(OkBB);
4509
4510
0
    const auto &FuncType = Context.CompositeTypes[TypeIndex]->getFuncType();
4511
0
    auto FTy = toLLVMType(Context.LLContext, Context.ExecCtxPtrTy, FuncType);
4512
0
    auto RTy = FTy.getReturnType();
4513
4514
0
    const size_t ArgSize = FuncType.getParamTypes().size();
4515
0
    const size_t RetSize =
4516
0
        RTy.isVoidTy() ? 0 : FuncType.getReturnTypes().size();
4517
0
    std::vector<LLVM::Value> ArgsVec(ArgSize + 1, nullptr);
4518
0
    ArgsVec[0] = F.Fn.getFirstParam();
4519
0
    for (size_t I = 0; I < ArgSize; ++I) {
4520
0
      const size_t J = ArgSize - I;
4521
0
      ArgsVec[J] = stackPop();
4522
0
    }
4523
4524
0
    {
4525
0
      auto FPtr = Builder.createCall(
4526
0
          Context.getIntrinsic(
4527
0
              Builder, Executable::Intrinsics::kRefGetFuncSymbol,
4528
0
              LLVM::Type::getFunctionType(FTy.getPointerTo(),
4529
0
                                          {Context.Int64x2Ty}, false)),
4530
0
          {Ref});
4531
0
      Builder.createCondBr(
4532
0
          Builder.createLikely(Builder.createNot(Builder.createIsNull(FPtr))),
4533
0
          NotNullBB, IsNullBB);
4534
0
      Builder.positionAtEnd(NotNullBB);
4535
4536
0
      auto FPtrRet =
4537
0
          Builder.createCall(LLVM::FunctionCallee(FTy, FPtr), ArgsVec);
4538
0
      if (RetSize == 0) {
4539
0
        Builder.createRetVoid();
4540
0
      } else {
4541
0
        Builder.createRet(FPtrRet);
4542
0
      }
4543
0
    }
4544
4545
0
    Builder.positionAtEnd(IsNullBB);
4546
4547
0
    {
4548
0
      LLVM::Value Args = Builder.createArray(ArgSize, kValSize);
4549
0
      LLVM::Value Rets = Builder.createArray(RetSize, kValSize);
4550
0
      Builder.createArrayPtrStore(
4551
0
          Span<LLVM::Value>(ArgsVec.begin() + 1, ArgSize), Args, Context.Int8Ty,
4552
0
          kValSize);
4553
4554
0
      Builder.createCall(
4555
0
          Context.getIntrinsic(
4556
0
              Builder, Executable::Intrinsics::kCallRef,
4557
0
              LLVM::Type::getFunctionType(
4558
0
                  Context.VoidTy,
4559
0
                  {Context.Int64x2Ty, Context.Int8PtrTy, Context.Int8PtrTy},
4560
0
                  false)),
4561
0
          {Ref, Args, Rets});
4562
4563
0
      if (RetSize == 0) {
4564
0
        Builder.createRetVoid();
4565
0
      } else if (RetSize == 1) {
4566
0
        Builder.createRet(
4567
0
            Builder.createValuePtrLoad(RTy, Rets, Context.Int8Ty));
4568
0
      } else {
4569
0
        Builder.createAggregateRet(Builder.createArrayPtrLoad(
4570
0
            RetSize, RTy, Rets, Context.Int8Ty, kValSize));
4571
0
      }
4572
0
    }
4573
0
  }
4574
4575
  void compileLoadOp(unsigned MemoryIndex, unsigned Offset, unsigned Alignment,
4576
20.5k
                     LLVM::Type LoadTy) noexcept {
4577
20.5k
    if constexpr (kForceUnalignment) {
4578
20.5k
      Alignment = 0;
4579
20.5k
    }
4580
20.5k
    auto Off = Builder.createZExt(stackPop(), Context.Int64Ty);
4581
20.5k
    if (Offset != 0) {
4582
13.6k
      Off = Builder.createAdd(Off, LLContext.getInt64(Offset));
4583
13.6k
    }
4584
4585
20.5k
    auto VPtr = Builder.createInBoundsGEP1(
4586
20.5k
        Context.Int8Ty, Context.getMemory(Builder, ExecCtx, MemoryIndex), Off);
4587
20.5k
    auto Ptr = Builder.createBitCast(VPtr, LoadTy.getPointerTo());
4588
20.5k
    auto LoadInst = Builder.createLoad(LoadTy, Ptr, true);
4589
20.5k
    LoadInst.setAlignment(1 << Alignment);
4590
20.5k
    stackPush(switchEndian(LoadInst));
4591
20.5k
  }
4592
  void compileLoadOp(unsigned MemoryIndex, unsigned Offset, unsigned Alignment,
4593
                     LLVM::Type LoadTy, LLVM::Type ExtendTy,
4594
8.40k
                     bool Signed) noexcept {
4595
8.40k
    compileLoadOp(MemoryIndex, Offset, Alignment, LoadTy);
4596
8.40k
    if (Signed) {
4597
3.54k
      Stack.back() = Builder.createSExt(Stack.back(), ExtendTy);
4598
4.86k
    } else {
4599
4.86k
      Stack.back() = Builder.createZExt(Stack.back(), ExtendTy);
4600
4.86k
    }
4601
8.40k
  }
4602
  void compileVectorLoadOp(unsigned MemoryIndex, unsigned Offset,
4603
5.16k
                           unsigned Alignment, LLVM::Type LoadTy) noexcept {
4604
5.16k
    compileLoadOp(MemoryIndex, Offset, Alignment, LoadTy);
4605
5.16k
    Stack.back() = Builder.createBitCast(Stack.back(), Context.Int64x2Ty);
4606
5.16k
  }
4607
  void compileVectorLoadOp(unsigned MemoryIndex, unsigned Offset,
4608
                           unsigned Alignment, LLVM::Type LoadTy,
4609
1.79k
                           LLVM::Type ExtendTy, bool Signed) noexcept {
4610
1.79k
    compileLoadOp(MemoryIndex, Offset, Alignment, LoadTy, ExtendTy, Signed);
4611
1.79k
    Stack.back() = Builder.createBitCast(Stack.back(), Context.Int64x2Ty);
4612
1.79k
  }
4613
  void compileSplatLoadOp(unsigned MemoryIndex, unsigned Offset,
4614
                          unsigned Alignment, LLVM::Type LoadTy,
4615
687
                          LLVM::Type VectorTy) noexcept {
4616
687
    compileLoadOp(MemoryIndex, Offset, Alignment, LoadTy);
4617
687
    compileSplatOp(VectorTy);
4618
687
  }
4619
  void compileLoadLaneOp(unsigned MemoryIndex, unsigned Offset,
4620
                         unsigned Alignment, unsigned Index, LLVM::Type LoadTy,
4621
481
                         LLVM::Type VectorTy) noexcept {
4622
481
    auto Vector = stackPop();
4623
481
    compileLoadOp(MemoryIndex, Offset, Alignment, LoadTy);
4624
    if constexpr (Endian::native == Endian::big) {
4625
      Index = VectorTy.getVectorSize() - 1 - Index;
4626
    }
4627
481
    auto Value = Stack.back();
4628
481
    Stack.back() = Builder.createBitCast(
4629
481
        Builder.createInsertElement(Builder.createBitCast(Vector, VectorTy),
4630
481
                                    Value, LLContext.getInt64(Index)),
4631
481
        Context.Int64x2Ty);
4632
481
  }
4633
  void compileStoreOp(unsigned MemoryIndex, unsigned Offset, unsigned Alignment,
4634
                      LLVM::Type LoadTy, bool Trunc = false,
4635
3.29k
                      bool BitCast = false) noexcept {
4636
3.29k
    if constexpr (kForceUnalignment) {
4637
3.29k
      Alignment = 0;
4638
3.29k
    }
4639
3.29k
    auto V = stackPop();
4640
3.29k
    auto Off = Builder.createZExt(stackPop(), Context.Int64Ty);
4641
3.29k
    if (Offset != 0) {
4642
2.52k
      Off = Builder.createAdd(Off, LLContext.getInt64(Offset));
4643
2.52k
    }
4644
4645
3.29k
    if (Trunc) {
4646
747
      V = Builder.createTrunc(V, LoadTy);
4647
747
    }
4648
3.29k
    if (BitCast) {
4649
223
      V = Builder.createBitCast(V, LoadTy);
4650
223
    }
4651
3.29k
    V = switchEndian(V);
4652
3.29k
    auto VPtr = Builder.createInBoundsGEP1(
4653
3.29k
        Context.Int8Ty, Context.getMemory(Builder, ExecCtx, MemoryIndex), Off);
4654
3.29k
    auto Ptr = Builder.createBitCast(VPtr, LoadTy.getPointerTo());
4655
3.29k
    auto StoreInst = Builder.createStore(V, Ptr, true);
4656
3.29k
    StoreInst.setAlignment(1 << Alignment);
4657
3.29k
  }
4658
  void compileStoreLaneOp(unsigned MemoryIndex, unsigned Offset,
4659
                          unsigned Alignment, unsigned Index, LLVM::Type LoadTy,
4660
338
                          LLVM::Type VectorTy) noexcept {
4661
338
    auto Vector = Stack.back();
4662
    if constexpr (Endian::native == Endian::big) {
4663
      Index = VectorTy.getVectorSize() - Index - 1;
4664
    }
4665
338
    Stack.back() = Builder.createExtractElement(
4666
338
        Builder.createBitCast(Vector, VectorTy), LLContext.getInt64(Index));
4667
338
    compileStoreOp(MemoryIndex, Offset, Alignment, LoadTy);
4668
338
  }
4669
54.3k
  void compileSplatOp(LLVM::Type VectorTy) noexcept {
4670
54.3k
    auto Undef = LLVM::Value::getUndef(VectorTy);
4671
54.3k
    auto Zeros = LLVM::Value::getConstNull(
4672
54.3k
        LLVM::Type::getVectorType(Context.Int32Ty, VectorTy.getVectorSize()));
4673
54.3k
    auto Value = Builder.createTrunc(Stack.back(), VectorTy.getElementType());
4674
54.3k
    auto Vector =
4675
54.3k
        Builder.createInsertElement(Undef, Value, LLContext.getInt64(0));
4676
54.3k
    Vector = Builder.createShuffleVector(Vector, Undef, Zeros);
4677
4678
54.3k
    Stack.back() = Builder.createBitCast(Vector, Context.Int64x2Ty);
4679
54.3k
  }
4680
1.38k
  void compileExtractLaneOp(LLVM::Type VectorTy, unsigned Index) noexcept {
4681
1.38k
    auto Vector = Builder.createBitCast(Stack.back(), VectorTy);
4682
    if constexpr (Endian::native == Endian::big) {
4683
      Index = VectorTy.getVectorSize() - Index - 1;
4684
    }
4685
1.38k
    Stack.back() =
4686
1.38k
        Builder.createExtractElement(Vector, LLContext.getInt64(Index));
4687
1.38k
  }
4688
  void compileExtractLaneOp(LLVM::Type VectorTy, unsigned Index,
4689
1.04k
                            LLVM::Type ExtendTy, bool Signed) noexcept {
4690
1.04k
    compileExtractLaneOp(VectorTy, Index);
4691
1.04k
    if (Signed) {
4692
561
      Stack.back() = Builder.createSExt(Stack.back(), ExtendTy);
4693
561
    } else {
4694
488
      Stack.back() = Builder.createZExt(Stack.back(), ExtendTy);
4695
488
    }
4696
1.04k
  }
4697
1.24k
  void compileReplaceLaneOp(LLVM::Type VectorTy, unsigned Index) noexcept {
4698
1.24k
    auto Value = Builder.createTrunc(stackPop(), VectorTy.getElementType());
4699
1.24k
    auto Vector = Stack.back();
4700
    if constexpr (Endian::native == Endian::big) {
4701
      Index = VectorTy.getVectorSize() - Index - 1;
4702
    }
4703
1.24k
    Stack.back() = Builder.createBitCast(
4704
1.24k
        Builder.createInsertElement(Builder.createBitCast(Vector, VectorTy),
4705
1.24k
                                    Value, LLContext.getInt64(Index)),
4706
1.24k
        Context.Int64x2Ty);
4707
1.24k
  }
4708
  void compileVectorCompareOp(LLVM::Type VectorTy,
4709
5.48k
                              LLVMIntPredicate Predicate) noexcept {
4710
5.48k
    auto RHS = stackPop();
4711
5.48k
    auto LHS = stackPop();
4712
5.48k
    auto Result = Builder.createSExt(
4713
5.48k
        Builder.createICmp(Predicate, Builder.createBitCast(LHS, VectorTy),
4714
5.48k
                           Builder.createBitCast(RHS, VectorTy)),
4715
5.48k
        VectorTy);
4716
5.48k
    stackPush(Builder.createBitCast(Result, Context.Int64x2Ty));
4717
5.48k
  }
4718
  void compileVectorCompareOp(LLVM::Type VectorTy, LLVMRealPredicate Predicate,
4719
3.58k
                              LLVM::Type ResultTy) noexcept {
4720
3.58k
    auto RHS = stackPop();
4721
3.58k
    auto LHS = stackPop();
4722
3.58k
    auto Result = Builder.createSExt(
4723
3.58k
        Builder.createFCmp(Predicate, Builder.createBitCast(LHS, VectorTy),
4724
3.58k
                           Builder.createBitCast(RHS, VectorTy)),
4725
3.58k
        ResultTy);
4726
3.58k
    stackPush(Builder.createBitCast(Result, Context.Int64x2Ty));
4727
3.58k
  }
4728
  template <typename Func>
4729
26.2k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
26.2k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
26.2k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
26.2k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorAbs(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorAbs(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
2.35k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
2.35k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
2.35k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
2.35k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorNeg(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorNeg(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
2.64k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
2.64k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
2.64k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
2.64k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorPopcnt()::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorPopcnt()::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
132
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
132
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
132
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
132
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorExtAddPairwise(WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorExtAddPairwise(WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
2.44k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
2.44k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
2.44k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
2.44k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorFAbs(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorFAbs(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
579
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
579
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
579
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
579
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorFNeg(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorFNeg(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
939
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
939
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
939
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
939
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorFSqrt(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorFSqrt(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
322
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
322
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
322
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
322
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorFCeil(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorFCeil(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
1.38k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
1.38k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
1.38k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
1.38k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorFFloor(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorFFloor(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
2.50k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
2.50k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
2.50k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
2.50k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorFTrunc(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorFTrunc(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
1.80k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
1.80k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
1.80k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
1.80k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorFNearest(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorFNearest(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
376
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
376
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
376
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
376
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorTruncSatS32(WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorTruncSatS32(WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
959
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
959
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
959
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
959
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorTruncSatU32(WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorTruncSatU32(WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
5.84k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
5.84k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
5.84k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
5.84k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorConvertS(WasmEdge::LLVM::Type, WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorConvertS(WasmEdge::LLVM::Type, WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
719
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
719
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
719
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
719
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorConvertU(WasmEdge::LLVM::Type, WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorConvertU(WasmEdge::LLVM::Type, WasmEdge::LLVM::Type, bool)::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
1.99k
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
1.99k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
1.99k
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
1.99k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorDemote()::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorDemote()::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
623
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
623
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
623
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
623
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorPromote()::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorPromote()::{lambda(auto:1)#1}&&)
Line
Count
Source
4729
610
  void compileVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4730
610
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4731
610
    Stack.back() = Builder.createBitCast(Op(V), Context.Int64x2Ty);
4732
610
  }
4733
2.35k
  void compileVectorAbs(LLVM::Type VectorTy) noexcept {
4734
2.35k
    compileVectorOp(VectorTy, [this, VectorTy](auto V) noexcept {
4735
2.35k
      auto Zero = LLVM::Value::getConstNull(VectorTy);
4736
2.35k
      auto C = Builder.createICmpSLT(V, Zero);
4737
2.35k
      return Builder.createSelect(C, Builder.createNeg(V), V);
4738
2.35k
    });
4739
2.35k
  }
4740
2.64k
  void compileVectorNeg(LLVM::Type VectorTy) noexcept {
4741
2.64k
    compileVectorOp(VectorTy,
4742
2.64k
                    [this](auto V) noexcept { return Builder.createNeg(V); });
4743
2.64k
  }
4744
132
  void compileVectorPopcnt() noexcept {
4745
132
    compileVectorOp(Context.Int8x16Ty, [this](auto V) noexcept {
4746
132
      assuming(LLVM::Core::Ctpop != LLVM::Core::NotIntrinsic);
4747
132
      return Builder.createUnaryIntrinsic(LLVM::Core::Ctpop, V);
4748
132
    });
4749
132
  }
4750
  template <typename Func>
4751
2.26k
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4752
2.26k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4753
2.26k
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
4754
2.26k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorReduceIOp<(anonymous namespace)::FunctionCompiler::compileVectorAnyTrue()::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorAnyTrue()::{lambda(auto:1)#1}&&)
Line
Count
Source
4751
107
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4752
107
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4753
107
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
4754
107
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorReduceIOp<(anonymous namespace)::FunctionCompiler::compileVectorAllTrue(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorAllTrue(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4751
875
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4752
875
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4753
875
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
4754
875
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorReduceIOp<(anonymous namespace)::FunctionCompiler::compileVectorBitMask(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorBitMask(WasmEdge::LLVM::Type)::{lambda(auto:1)#1}&&)
Line
Count
Source
4751
1.28k
  void compileVectorReduceIOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4752
1.28k
    auto V = Builder.createBitCast(Stack.back(), VectorTy);
4753
1.28k
    Stack.back() = Builder.createZExt(Op(V), Context.Int32Ty);
4754
1.28k
  }
4755
107
  void compileVectorAnyTrue() noexcept {
4756
107
    compileVectorReduceIOp(Context.Int128x1Ty, [this](auto V) noexcept {
4757
107
      auto Zero = LLVM::Value::getConstNull(Context.Int128x1Ty);
4758
107
      return Builder.createBitCast(Builder.createICmpNE(V, Zero),
4759
107
                                   LLContext.getInt1Ty());
4760
107
    });
4761
107
  }
4762
875
  void compileVectorAllTrue(LLVM::Type VectorTy) noexcept {
4763
875
    compileVectorReduceIOp(VectorTy, [this, VectorTy](auto V) noexcept {
4764
875
      const auto Size = VectorTy.getVectorSize();
4765
875
      auto IntType = LLContext.getIntNTy(Size);
4766
875
      auto Zero = LLVM::Value::getConstNull(VectorTy);
4767
875
      auto Cmp = Builder.createBitCast(Builder.createICmpEQ(V, Zero), IntType);
4768
875
      auto CmpZero = LLVM::Value::getConstInt(IntType, 0);
4769
875
      return Builder.createICmpEQ(Cmp, CmpZero);
4770
875
    });
4771
875
  }
4772
1.28k
  void compileVectorBitMask(LLVM::Type VectorTy) noexcept {
4773
1.28k
    compileVectorReduceIOp(VectorTy, [this, VectorTy](auto V) noexcept {
4774
1.28k
      const auto Size = VectorTy.getVectorSize();
4775
1.28k
      auto IntType = LLContext.getIntNTy(Size);
4776
1.28k
      auto Zero = LLVM::Value::getConstNull(VectorTy);
4777
1.28k
      return Builder.createBitCast(Builder.createICmpSLT(V, Zero), IntType);
4778
1.28k
    });
4779
1.28k
  }
4780
  template <typename Func>
4781
4.86k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4782
4.86k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
4783
4.86k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
4784
4.86k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
4785
4.86k
    auto RHS = Builder.createVectorSplat(
4786
4.86k
        VectorTy.getVectorSize(),
4787
4.86k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
4788
4.86k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
4789
4.86k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4790
4.86k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4791
4.86k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorShiftOp<(anonymous namespace)::FunctionCompiler::compileVectorShl(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorShl(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4781
2.05k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4782
2.05k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
4783
2.05k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
4784
2.05k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
4785
2.05k
    auto RHS = Builder.createVectorSplat(
4786
2.05k
        VectorTy.getVectorSize(),
4787
2.05k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
4788
2.05k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
4789
2.05k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4790
2.05k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4791
2.05k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorShiftOp<(anonymous namespace)::FunctionCompiler::compileVectorAShr(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorAShr(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4781
2.53k
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4782
2.53k
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
4783
2.53k
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
4784
2.53k
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
4785
2.53k
    auto RHS = Builder.createVectorSplat(
4786
2.53k
        VectorTy.getVectorSize(),
4787
2.53k
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
4788
2.53k
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
4789
2.53k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4790
2.53k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4791
2.53k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorShiftOp<(anonymous namespace)::FunctionCompiler::compileVectorLShr(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorLShr(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4781
283
  void compileVectorShiftOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4782
283
    const bool Trunc = VectorTy.getElementType().getIntegerBitWidth() < 32;
4783
283
    const uint32_t Mask = VectorTy.getElementType().getIntegerBitWidth() - 1;
4784
283
    auto N = Builder.createAnd(stackPop(), LLContext.getInt32(Mask));
4785
283
    auto RHS = Builder.createVectorSplat(
4786
283
        VectorTy.getVectorSize(),
4787
283
        Trunc ? Builder.createTrunc(N, VectorTy.getElementType())
4788
283
              : Builder.createZExtOrTrunc(N, VectorTy.getElementType()));
4789
283
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4790
283
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4791
283
  }
4792
2.05k
  void compileVectorShl(LLVM::Type VectorTy) noexcept {
4793
2.05k
    compileVectorShiftOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4794
2.05k
      return Builder.createShl(LHS, RHS);
4795
2.05k
    });
4796
2.05k
  }
4797
283
  void compileVectorLShr(LLVM::Type VectorTy) noexcept {
4798
283
    compileVectorShiftOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4799
283
      return Builder.createLShr(LHS, RHS);
4800
283
    });
4801
283
  }
4802
2.53k
  void compileVectorAShr(LLVM::Type VectorTy) noexcept {
4803
2.53k
    compileVectorShiftOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4804
2.53k
      return Builder.createAShr(LHS, RHS);
4805
2.53k
    });
4806
2.53k
  }
4807
  template <typename Func>
4808
9.11k
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
9.11k
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
9.11k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
9.11k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
9.11k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorAdd(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorAdd(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
344
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
344
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
344
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
344
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
344
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorAddSat(WasmEdge::LLVM::Type, bool)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorAddSat(WasmEdge::LLVM::Type, bool)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
1.93k
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
1.93k
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
1.93k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
1.93k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
1.93k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorSub(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorSub(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
812
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
812
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
812
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
812
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
812
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorSubSat(WasmEdge::LLVM::Type, bool)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorSubSat(WasmEdge::LLVM::Type, bool)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
386
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
386
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
386
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
386
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
386
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorSMin(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorSMin(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
317
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
317
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
317
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
317
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
317
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorUMin(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorUMin(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
308
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
308
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
308
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
308
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
308
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorSMax(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorSMax(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
418
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
418
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
418
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
418
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
418
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorUMax(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorUMax(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
1.27k
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
1.27k
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
1.27k
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
1.27k
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
1.27k
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorUAvgr(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorUAvgr(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
338
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
338
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
338
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
338
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
338
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorMul(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorMul(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
443
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
443
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
443
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
443
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
443
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorQ15MulSat()::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorQ15MulSat()::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
152
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
152
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
152
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
152
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
152
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorFAdd(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorFAdd(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
213
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
213
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
213
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
213
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
213
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorFSub(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorFSub(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
521
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
521
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
521
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
521
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
521
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorFMul(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorFMul(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
252
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
252
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
252
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
252
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
252
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorFDiv(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorFDiv(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
229
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
229
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
229
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
229
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
229
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorFMin(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorFMin(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
309
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
309
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
309
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
309
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
309
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorFMax(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorFMax(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
190
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
190
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
190
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
190
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
190
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorFPMin(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorFPMin(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
366
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
366
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
366
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
366
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
366
  }
compiler.cpp:void (anonymous namespace)::FunctionCompiler::compileVectorVectorOp<(anonymous namespace)::FunctionCompiler::compileVectorVectorFPMax(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}>(WasmEdge::LLVM::Type, (anonymous namespace)::FunctionCompiler::compileVectorVectorFPMax(WasmEdge::LLVM::Type)::{lambda(auto:1, auto:2)#1}&&)
Line
Count
Source
4808
310
  void compileVectorVectorOp(LLVM::Type VectorTy, Func &&Op) noexcept {
4809
310
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
4810
310
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
4811
310
    stackPush(Builder.createBitCast(Op(LHS, RHS), Context.Int64x2Ty));
4812
310
  }
4813
344
  void compileVectorVectorAdd(LLVM::Type VectorTy) noexcept {
4814
344
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4815
344
      return Builder.createAdd(LHS, RHS);
4816
344
    });
4817
344
  }
4818
1.93k
  void compileVectorVectorAddSat(LLVM::Type VectorTy, bool Signed) noexcept {
4819
1.93k
    auto ID = Signed ? LLVM::Core::SAddSat : LLVM::Core::UAddSat;
4820
1.93k
    assuming(ID != LLVM::Core::NotIntrinsic);
4821
1.93k
    compileVectorVectorOp(
4822
1.93k
        VectorTy, [this, VectorTy, ID](auto LHS, auto RHS) noexcept {
4823
1.93k
          return Builder.createIntrinsic(ID, {VectorTy}, {LHS, RHS});
4824
1.93k
        });
4825
1.93k
  }
4826
812
  void compileVectorVectorSub(LLVM::Type VectorTy) noexcept {
4827
812
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4828
812
      return Builder.createSub(LHS, RHS);
4829
812
    });
4830
812
  }
4831
386
  void compileVectorVectorSubSat(LLVM::Type VectorTy, bool Signed) noexcept {
4832
386
    auto ID = Signed ? LLVM::Core::SSubSat : LLVM::Core::USubSat;
4833
386
    assuming(ID != LLVM::Core::NotIntrinsic);
4834
386
    compileVectorVectorOp(
4835
386
        VectorTy, [this, VectorTy, ID](auto LHS, auto RHS) noexcept {
4836
386
          return Builder.createIntrinsic(ID, {VectorTy}, {LHS, RHS});
4837
386
        });
4838
386
  }
4839
443
  void compileVectorVectorMul(LLVM::Type VectorTy) noexcept {
4840
443
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4841
443
      return Builder.createMul(LHS, RHS);
4842
443
    });
4843
443
  }
4844
64
  void compileVectorSwizzle() noexcept {
4845
64
    auto Index = Builder.createBitCast(stackPop(), Context.Int8x16Ty);
4846
64
    auto Vector = Builder.createBitCast(stackPop(), Context.Int8x16Ty);
4847
4848
64
#if defined(__x86_64__)
4849
64
    if (Context.SupportSSSE3) {
4850
64
      auto Magic = Builder.createVectorSplat(16, LLContext.getInt8(112));
4851
64
      auto Added = Builder.createAdd(Index, Magic);
4852
64
      auto NewIndex = Builder.createSelect(
4853
64
          Builder.createICmpUGT(Index, Added),
4854
64
          LLVM::Value::getConstAllOnes(Context.Int8x16Ty), Added);
4855
64
      assuming(LLVM::Core::X86SSSE3PShufB128 != LLVM::Core::NotIntrinsic);
4856
64
      stackPush(Builder.createBitCast(
4857
64
          Builder.createIntrinsic(LLVM::Core::X86SSSE3PShufB128, {},
4858
64
                                  {Vector, NewIndex}),
4859
64
          Context.Int64x2Ty));
4860
64
      return;
4861
64
    }
4862
0
#endif
4863
4864
#if defined(__aarch64__)
4865
    if (Context.SupportNEON) {
4866
      assuming(LLVM::Core::AArch64NeonTbl1 != LLVM::Core::NotIntrinsic);
4867
      stackPush(Builder.createBitCast(
4868
          Builder.createIntrinsic(LLVM::Core::AArch64NeonTbl1,
4869
                                  {Context.Int8x16Ty}, {Vector, Index}),
4870
          Context.Int64x2Ty));
4871
      return;
4872
    }
4873
#endif
4874
4875
0
    auto Mask = Builder.createVectorSplat(16, LLContext.getInt8(15));
4876
0
    auto Zero = Builder.createVectorSplat(16, LLContext.getInt8(0));
4877
4878
#if defined(__s390x__)
4879
    assuming(LLVM::Core::S390VPerm != LLVM::Core::NotIntrinsic);
4880
    auto Exceed = Builder.createICmpULE(Index, Mask);
4881
    Index = Builder.createSub(Mask, Index);
4882
    auto Result = Builder.createIntrinsic(LLVM::Core::S390VPerm, {},
4883
                                          {Vector, Zero, Index});
4884
    Result = Builder.createSelect(Exceed, Result, Zero);
4885
    stackPush(Builder.createBitCast(Result, Context.Int64x2Ty));
4886
    return;
4887
#endif
4888
4889
    // Fallback case.
4890
    // If the SSSE3 is not supported on the x86_64 platform or
4891
    // the NEON is not supported on the aarch64 platform,
4892
    // then fallback to this.
4893
0
    auto IsOver = Builder.createICmpUGT(Index, Mask);
4894
0
    auto InboundIndex = Builder.createAnd(Index, Mask);
4895
0
    auto Array = Builder.createArray(16, 1);
4896
0
    for (size_t I = 0; I < 16; ++I) {
4897
0
      Builder.createStore(
4898
0
          Builder.createExtractElement(Vector, LLContext.getInt64(I)),
4899
0
          Builder.createInBoundsGEP1(Context.Int8Ty, Array,
4900
0
                                     LLContext.getInt64(I)));
4901
0
    }
4902
0
    LLVM::Value Ret = LLVM::Value::getUndef(Context.Int8x16Ty);
4903
0
    for (size_t I = 0; I < 16; ++I) {
4904
0
      auto Idx =
4905
0
          Builder.createExtractElement(InboundIndex, LLContext.getInt64(I));
4906
0
      auto Value = Builder.createLoad(
4907
0
          Context.Int8Ty,
4908
0
          Builder.createInBoundsGEP1(Context.Int8Ty, Array, Idx));
4909
0
      Ret = Builder.createInsertElement(Ret, Value, LLContext.getInt64(I));
4910
0
    }
4911
0
    Ret = Builder.createSelect(IsOver, Zero, Ret);
4912
0
    stackPush(Builder.createBitCast(Ret, Context.Int64x2Ty));
4913
0
  }
4914
4915
152
  void compileVectorVectorQ15MulSat() noexcept {
4916
152
    compileVectorVectorOp(
4917
152
        Context.Int16x8Ty, [this](auto LHS, auto RHS) noexcept -> LLVM::Value {
4918
152
#if defined(__x86_64__)
4919
152
          if (Context.SupportSSSE3) {
4920
152
            assuming(LLVM::Core::X86SSSE3PMulHrSw128 !=
4921
152
                     LLVM::Core::NotIntrinsic);
4922
152
            auto Result = Builder.createIntrinsic(
4923
152
                LLVM::Core::X86SSSE3PMulHrSw128, {}, {LHS, RHS});
4924
152
            auto IntMaxV = Builder.createVectorSplat(
4925
152
                8, LLContext.getInt16(UINT16_C(0x8000)));
4926
152
            auto NotOver = Builder.createSExt(
4927
152
                Builder.createICmpEQ(Result, IntMaxV), Context.Int16x8Ty);
4928
152
            return Builder.createXor(Result, NotOver);
4929
152
          }
4930
0
#endif
4931
4932
#if defined(__aarch64__)
4933
          if (Context.SupportNEON) {
4934
            assuming(LLVM::Core::AArch64NeonSQRDMulH !=
4935
                     LLVM::Core::NotIntrinsic);
4936
            return Builder.createBinaryIntrinsic(
4937
                LLVM::Core::AArch64NeonSQRDMulH, LHS, RHS);
4938
          }
4939
#endif
4940
4941
          // Fallback case.
4942
          // If the SSSE3 is not supported on the x86_64 platform or
4943
          // the NEON is not supported on the aarch64 platform,
4944
          // then fallback to this.
4945
0
          auto ExtTy = Context.Int16x8Ty.getExtendedElementVectorType();
4946
0
          auto Offset = Builder.createVectorSplat(
4947
0
              8, LLContext.getInt32(UINT32_C(0x4000)));
4948
0
          auto Shift =
4949
0
              Builder.createVectorSplat(8, LLContext.getInt32(UINT32_C(15)));
4950
0
          auto ExtLHS = Builder.createSExt(LHS, ExtTy);
4951
0
          auto ExtRHS = Builder.createSExt(RHS, ExtTy);
4952
0
          auto Result = Builder.createTrunc(
4953
0
              Builder.createAShr(
4954
0
                  Builder.createAdd(Builder.createMul(ExtLHS, ExtRHS), Offset),
4955
0
                  Shift),
4956
0
              Context.Int16x8Ty);
4957
0
          auto IntMaxV = Builder.createVectorSplat(
4958
0
              8, LLContext.getInt16(UINT16_C(0x8000)));
4959
0
          auto NotOver = Builder.createSExt(
4960
0
              Builder.createICmpEQ(Result, IntMaxV), Context.Int16x8Ty);
4961
0
          return Builder.createXor(Result, NotOver);
4962
152
        });
4963
152
  }
4964
317
  void compileVectorVectorSMin(LLVM::Type VectorTy) noexcept {
4965
317
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4966
317
      auto C = Builder.createICmpSLE(LHS, RHS);
4967
317
      return Builder.createSelect(C, LHS, RHS);
4968
317
    });
4969
317
  }
4970
308
  void compileVectorVectorUMin(LLVM::Type VectorTy) noexcept {
4971
308
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4972
308
      auto C = Builder.createICmpULE(LHS, RHS);
4973
308
      return Builder.createSelect(C, LHS, RHS);
4974
308
    });
4975
308
  }
4976
418
  void compileVectorVectorSMax(LLVM::Type VectorTy) noexcept {
4977
418
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4978
418
      auto C = Builder.createICmpSGE(LHS, RHS);
4979
418
      return Builder.createSelect(C, LHS, RHS);
4980
418
    });
4981
418
  }
4982
1.27k
  void compileVectorVectorUMax(LLVM::Type VectorTy) noexcept {
4983
1.27k
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
4984
1.27k
      auto C = Builder.createICmpUGE(LHS, RHS);
4985
1.27k
      return Builder.createSelect(C, LHS, RHS);
4986
1.27k
    });
4987
1.27k
  }
4988
338
  void compileVectorVectorUAvgr(LLVM::Type VectorTy) noexcept {
4989
338
    auto ExtendTy = VectorTy.getExtendedElementVectorType();
4990
338
    compileVectorVectorOp(
4991
338
        VectorTy,
4992
338
        [this, VectorTy, ExtendTy](auto LHS, auto RHS) noexcept -> LLVM::Value {
4993
338
#if defined(__x86_64__)
4994
338
          if (Context.SupportSSE2) {
4995
338
            const auto ID = [VectorTy]() noexcept {
4996
338
              switch (VectorTy.getElementType().getIntegerBitWidth()) {
4997
121
              case 8:
4998
121
                return LLVM::Core::X86SSE2PAvgB;
4999
217
              case 16:
5000
217
                return LLVM::Core::X86SSE2PAvgW;
5001
0
              default:
5002
0
                assumingUnreachable();
5003
338
              }
5004
338
            }();
5005
338
            assuming(ID != LLVM::Core::NotIntrinsic);
5006
338
            return Builder.createIntrinsic(ID, {}, {LHS, RHS});
5007
338
          }
5008
0
#endif
5009
5010
#if defined(__aarch64__)
5011
          if (Context.SupportNEON) {
5012
            assuming(LLVM::Core::AArch64NeonURHAdd != LLVM::Core::NotIntrinsic);
5013
            return Builder.createBinaryIntrinsic(LLVM::Core::AArch64NeonURHAdd,
5014
                                                 LHS, RHS);
5015
          }
5016
#endif
5017
5018
          // Fallback case.
5019
          // If the SSE2 is not supported on the x86_64 platform or
5020
          // the NEON is not supported on the aarch64 platform,
5021
          // then fallback to this.
5022
0
          auto EL = Builder.createZExt(LHS, ExtendTy);
5023
0
          auto ER = Builder.createZExt(RHS, ExtendTy);
5024
0
          auto One = Builder.createZExt(
5025
0
              Builder.createVectorSplat(ExtendTy.getVectorSize(),
5026
0
                                        LLContext.getTrue()),
5027
0
              ExtendTy);
5028
0
          return Builder.createTrunc(
5029
0
              Builder.createLShr(
5030
0
                  Builder.createAdd(Builder.createAdd(EL, ER), One), One),
5031
0
              VectorTy);
5032
338
        });
5033
338
  }
5034
688
  void compileVectorNarrow(LLVM::Type FromTy, bool Signed) noexcept {
5035
688
    auto [MinInt,
5036
688
          MaxInt] = [&]() noexcept -> std::tuple<LLVM::Value, LLVM::Value> {
5037
688
      switch (FromTy.getElementType().getIntegerBitWidth()) {
5038
286
      case 16: {
5039
286
        const auto Min =
5040
286
            static_cast<int16_t>(Signed ? std::numeric_limits<int8_t>::min()
5041
286
                                        : std::numeric_limits<uint8_t>::min());
5042
286
        const auto Max =
5043
286
            static_cast<int16_t>(Signed ? std::numeric_limits<int8_t>::max()
5044
286
                                        : std::numeric_limits<uint8_t>::max());
5045
286
        return {LLContext.getInt16(static_cast<uint16_t>(Min)),
5046
286
                LLContext.getInt16(static_cast<uint16_t>(Max))};
5047
0
      }
5048
402
      case 32: {
5049
402
        const auto Min =
5050
402
            static_cast<int32_t>(Signed ? std::numeric_limits<int16_t>::min()
5051
402
                                        : std::numeric_limits<uint16_t>::min());
5052
402
        const auto Max =
5053
402
            static_cast<int32_t>(Signed ? std::numeric_limits<int16_t>::max()
5054
402
                                        : std::numeric_limits<uint16_t>::max());
5055
402
        return {LLContext.getInt32(static_cast<uint32_t>(Min)),
5056
402
                LLContext.getInt32(static_cast<uint32_t>(Max))};
5057
0
      }
5058
0
      default:
5059
0
        assumingUnreachable();
5060
688
      }
5061
688
    }();
5062
688
    const auto Count = FromTy.getVectorSize();
5063
688
    auto VMin = Builder.createVectorSplat(Count, MinInt);
5064
688
    auto VMax = Builder.createVectorSplat(Count, MaxInt);
5065
5066
688
    auto TruncTy = FromTy.getTruncatedElementVectorType();
5067
5068
688
    auto F2 = Builder.createBitCast(stackPop(), FromTy);
5069
688
    F2 = Builder.createSelect(Builder.createICmpSLT(F2, VMin), VMin, F2);
5070
688
    F2 = Builder.createSelect(Builder.createICmpSGT(F2, VMax), VMax, F2);
5071
688
    F2 = Builder.createTrunc(F2, TruncTy);
5072
5073
688
    auto F1 = Builder.createBitCast(stackPop(), FromTy);
5074
688
    F1 = Builder.createSelect(Builder.createICmpSLT(F1, VMin), VMin, F1);
5075
688
    F1 = Builder.createSelect(Builder.createICmpSGT(F1, VMax), VMax, F1);
5076
688
    F1 = Builder.createTrunc(F1, TruncTy);
5077
5078
688
    std::vector<uint32_t> Mask(Count * 2);
5079
688
    std::iota(Mask.begin(), Mask.end(), 0);
5080
688
    auto V = Endian::native == Endian::little
5081
688
                 ? Builder.createShuffleVector(
5082
688
                       F1, F2, LLVM::Value::getConstVector32(LLContext, Mask))
5083
688
                 : Builder.createShuffleVector(
5084
0
                       F2, F1, LLVM::Value::getConstVector32(LLContext, Mask));
5085
688
    stackPush(Builder.createBitCast(V, Context.Int64x2Ty));
5086
688
  }
5087
6.15k
  void compileVectorExtend(LLVM::Type FromTy, bool Signed, bool Low) noexcept {
5088
6.15k
    auto ExtTy = FromTy.getExtendedElementVectorType();
5089
6.15k
    const auto Count = FromTy.getVectorSize();
5090
6.15k
    std::vector<uint32_t> Mask(Count / 2);
5091
    if constexpr (Endian::native == Endian::big) {
5092
      Low = !Low;
5093
    }
5094
6.15k
    std::iota(Mask.begin(), Mask.end(), Low ? 0 : Count / 2);
5095
6.15k
    auto R = Builder.createBitCast(Stack.back(), FromTy);
5096
6.15k
    if (Signed) {
5097
2.58k
      R = Builder.createSExt(R, ExtTy);
5098
3.56k
    } else {
5099
3.56k
      R = Builder.createZExt(R, ExtTy);
5100
3.56k
    }
5101
6.15k
    R = Builder.createShuffleVector(
5102
6.15k
        R, LLVM::Value::getUndef(ExtTy),
5103
6.15k
        LLVM::Value::getConstVector32(LLContext, Mask));
5104
6.15k
    Stack.back() = Builder.createBitCast(R, Context.Int64x2Ty);
5105
6.15k
  }
5106
1.81k
  void compileVectorExtMul(LLVM::Type FromTy, bool Signed, bool Low) noexcept {
5107
1.81k
    auto ExtTy = FromTy.getExtendedElementVectorType();
5108
1.81k
    const auto Count = FromTy.getVectorSize();
5109
1.81k
    std::vector<uint32_t> Mask(Count / 2);
5110
1.81k
    std::iota(Mask.begin(), Mask.end(), Low ? 0 : Count / 2);
5111
3.62k
    auto Extend = [this, FromTy, Signed, ExtTy, &Mask](LLVM::Value R) noexcept {
5112
3.62k
      R = Builder.createBitCast(R, FromTy);
5113
3.62k
      if (Signed) {
5114
1.58k
        R = Builder.createSExt(R, ExtTy);
5115
2.04k
      } else {
5116
2.04k
        R = Builder.createZExt(R, ExtTy);
5117
2.04k
      }
5118
3.62k
      return Builder.createShuffleVector(
5119
3.62k
          R, LLVM::Value::getUndef(ExtTy),
5120
3.62k
          LLVM::Value::getConstVector32(LLContext, Mask));
5121
3.62k
    };
5122
1.81k
    auto RHS = Extend(stackPop());
5123
1.81k
    auto LHS = Extend(stackPop());
5124
1.81k
    stackPush(
5125
1.81k
        Builder.createBitCast(Builder.createMul(RHS, LHS), Context.Int64x2Ty));
5126
1.81k
  }
5127
2.44k
  void compileVectorExtAddPairwise(LLVM::Type VectorTy, bool Signed) noexcept {
5128
2.44k
    compileVectorOp(
5129
2.44k
        VectorTy, [this, VectorTy, Signed](auto V) noexcept -> LLVM::Value {
5130
2.44k
          auto ExtTy = VectorTy.getExtendedElementVectorType()
5131
2.44k
                           .getHalfElementsVectorType();
5132
2.44k
#if defined(__x86_64__)
5133
2.44k
          const auto Count = VectorTy.getVectorSize();
5134
2.44k
          if (Context.SupportXOP) {
5135
0
            const auto ID = [Count, Signed]() noexcept {
5136
0
              switch (Count) {
5137
0
              case 8:
5138
0
                return Signed ? LLVM::Core::X86XOpVPHAddWD
5139
0
                              : LLVM::Core::X86XOpVPHAddUWD;
5140
0
              case 16:
5141
0
                return Signed ? LLVM::Core::X86XOpVPHAddBW
5142
0
                              : LLVM::Core::X86XOpVPHAddUBW;
5143
0
              default:
5144
0
                assumingUnreachable();
5145
0
              }
5146
0
            }();
5147
0
            assuming(ID != LLVM::Core::NotIntrinsic);
5148
0
            return Builder.createUnaryIntrinsic(ID, V);
5149
0
          }
5150
2.44k
          if (Context.SupportSSSE3 && Count == 16) {
5151
653
            assuming(LLVM::Core::X86SSSE3PMAddUbSw128 !=
5152
653
                     LLVM::Core::NotIntrinsic);
5153
653
            if (Signed) {
5154
321
              return Builder.createIntrinsic(
5155
321
                  LLVM::Core::X86SSSE3PMAddUbSw128, {},
5156
321
                  {Builder.createVectorSplat(16, LLContext.getInt8(1)), V});
5157
332
            } else {
5158
332
              return Builder.createIntrinsic(
5159
332
                  LLVM::Core::X86SSSE3PMAddUbSw128, {},
5160
332
                  {V, Builder.createVectorSplat(16, LLContext.getInt8(1))});
5161
332
            }
5162
653
          }
5163
1.78k
          if (Context.SupportSSE2 && Count == 8) {
5164
1.78k
            assuming(LLVM::Core::X86SSE2PMAddWd != LLVM::Core::NotIntrinsic);
5165
1.78k
            if (Signed) {
5166
1.32k
              return Builder.createIntrinsic(
5167
1.32k
                  LLVM::Core::X86SSE2PMAddWd, {},
5168
1.32k
                  {V, Builder.createVectorSplat(8, LLContext.getInt16(1))});
5169
1.32k
            } else {
5170
464
              V = Builder.createXor(
5171
464
                  V, Builder.createVectorSplat(8, LLContext.getInt16(0x8000)));
5172
464
              V = Builder.createIntrinsic(
5173
464
                  LLVM::Core::X86SSE2PMAddWd, {},
5174
464
                  {V, Builder.createVectorSplat(8, LLContext.getInt16(1))});
5175
464
              return Builder.createAdd(
5176
464
                  V, Builder.createVectorSplat(4, LLContext.getInt32(0x10000)));
5177
464
            }
5178
1.78k
          }
5179
0
#endif
5180
5181
#if defined(__aarch64__)
5182
          if (Context.SupportNEON) {
5183
            const auto ID = Signed ? LLVM::Core::AArch64NeonSAddLP
5184
                                   : LLVM::Core::AArch64NeonUAddLP;
5185
            assuming(ID != LLVM::Core::NotIntrinsic);
5186
            return Builder.createIntrinsic(ID, {ExtTy, VectorTy}, {V});
5187
          }
5188
#endif
5189
5190
          // Fallback case.
5191
          // If the XOP, SSSE3, or SSE2 is not supported on the x86_64 platform
5192
          // or the NEON is not supported on the aarch64 platform,
5193
          // then fallback to this.
5194
0
          auto Width = LLVM::Value::getConstInt(
5195
0
              ExtTy.getElementType(),
5196
0
              VectorTy.getElementType().getIntegerBitWidth());
5197
0
          Width = Builder.createVectorSplat(ExtTy.getVectorSize(), Width);
5198
0
          auto EV = Builder.createBitCast(V, ExtTy);
5199
0
          LLVM::Value L, R;
5200
0
          if (Signed) {
5201
0
            L = Builder.createAShr(EV, Width);
5202
0
            R = Builder.createAShr(Builder.createShl(EV, Width), Width);
5203
0
          } else {
5204
0
            L = Builder.createLShr(EV, Width);
5205
0
            R = Builder.createLShr(Builder.createShl(EV, Width), Width);
5206
0
          }
5207
0
          return Builder.createAdd(L, R);
5208
1.78k
        });
5209
2.44k
  }
5210
579
  void compileVectorFAbs(LLVM::Type VectorTy) noexcept {
5211
579
    compileVectorOp(VectorTy, [this](auto V) noexcept {
5212
579
      assuming(LLVM::Core::Fabs != LLVM::Core::NotIntrinsic);
5213
579
      return Builder.createUnaryIntrinsic(LLVM::Core::Fabs, V);
5214
579
    });
5215
579
  }
5216
939
  void compileVectorFNeg(LLVM::Type VectorTy) noexcept {
5217
939
    compileVectorOp(VectorTy,
5218
939
                    [this](auto V) noexcept { return Builder.createFNeg(V); });
5219
939
  }
5220
322
  void compileVectorFSqrt(LLVM::Type VectorTy) noexcept {
5221
322
    compileVectorOp(VectorTy, [this](auto V) noexcept {
5222
322
      assuming(LLVM::Core::Sqrt != LLVM::Core::NotIntrinsic);
5223
322
      return Builder.createUnaryIntrinsic(LLVM::Core::Sqrt, V);
5224
322
    });
5225
322
  }
5226
1.38k
  void compileVectorFCeil(LLVM::Type VectorTy) noexcept {
5227
1.38k
    compileVectorOp(VectorTy, [this](auto V) noexcept {
5228
1.38k
      assuming(LLVM::Core::Ceil != LLVM::Core::NotIntrinsic);
5229
1.38k
      return Builder.createUnaryIntrinsic(LLVM::Core::Ceil, V);
5230
1.38k
    });
5231
1.38k
  }
5232
2.50k
  void compileVectorFFloor(LLVM::Type VectorTy) noexcept {
5233
2.50k
    compileVectorOp(VectorTy, [this](auto V) noexcept {
5234
2.50k
      assuming(LLVM::Core::Floor != LLVM::Core::NotIntrinsic);
5235
2.50k
      return Builder.createUnaryIntrinsic(LLVM::Core::Floor, V);
5236
2.50k
    });
5237
2.50k
  }
5238
1.80k
  void compileVectorFTrunc(LLVM::Type VectorTy) noexcept {
5239
1.80k
    compileVectorOp(VectorTy, [this](auto V) noexcept {
5240
1.80k
      assuming(LLVM::Core::Trunc != LLVM::Core::NotIntrinsic);
5241
1.80k
      return Builder.createUnaryIntrinsic(LLVM::Core::Trunc, V);
5242
1.80k
    });
5243
1.80k
  }
5244
376
  void compileVectorFNearest(LLVM::Type VectorTy) noexcept {
5245
376
    compileVectorOp(VectorTy, [&](auto V) noexcept {
5246
376
#if LLVM_VERSION_MAJOR >= 12 && !defined(__s390x__)
5247
376
      assuming(LLVM::Core::Roundeven != LLVM::Core::NotIntrinsic);
5248
376
      if (LLVM::Core::Roundeven != LLVM::Core::NotIntrinsic) {
5249
376
        return Builder.createUnaryIntrinsic(LLVM::Core::Roundeven, V);
5250
376
      }
5251
0
#endif
5252
5253
0
#if defined(__x86_64__)
5254
0
      if (Context.SupportSSE4_1) {
5255
0
        const bool IsFloat = VectorTy.getElementType().isFloatTy();
5256
0
        auto ID =
5257
0
            IsFloat ? LLVM::Core::X86SSE41RoundPs : LLVM::Core::X86SSE41RoundPd;
5258
0
        assuming(ID != LLVM::Core::NotIntrinsic);
5259
0
        return Builder.createIntrinsic(ID, {}, {V, LLContext.getInt32(8)});
5260
0
      }
5261
0
#endif
5262
5263
#if defined(__aarch64__)
5264
      if (Context.SupportNEON &&
5265
          LLVM::Core::AArch64NeonFRIntN != LLVM::Core::NotIntrinsic) {
5266
        return Builder.createUnaryIntrinsic(LLVM::Core::AArch64NeonFRIntN, V);
5267
      }
5268
#endif
5269
5270
      // Fallback case.
5271
      // If the SSE4.1 is not supported on the x86_64 platform or
5272
      // the NEON is not supported on the aarch64 platform,
5273
      // then fallback to this.
5274
0
      assuming(LLVM::Core::Nearbyint != LLVM::Core::NotIntrinsic);
5275
0
      return Builder.createUnaryIntrinsic(LLVM::Core::Nearbyint, V);
5276
0
    });
5277
376
  }
5278
213
  void compileVectorVectorFAdd(LLVM::Type VectorTy) noexcept {
5279
213
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
5280
213
      return Builder.createFAdd(LHS, RHS);
5281
213
    });
5282
213
  }
5283
521
  void compileVectorVectorFSub(LLVM::Type VectorTy) noexcept {
5284
521
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
5285
521
      return Builder.createFSub(LHS, RHS);
5286
521
    });
5287
521
  }
5288
252
  void compileVectorVectorFMul(LLVM::Type VectorTy) noexcept {
5289
252
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
5290
252
      return Builder.createFMul(LHS, RHS);
5291
252
    });
5292
252
  }
5293
229
  void compileVectorVectorFDiv(LLVM::Type VectorTy) noexcept {
5294
229
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
5295
229
      return Builder.createFDiv(LHS, RHS);
5296
229
    });
5297
229
  }
5298
309
  void compileVectorVectorFMin(LLVM::Type VectorTy) noexcept {
5299
309
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
5300
309
      auto LNaN = Builder.createFCmpUNO(LHS, LHS);
5301
309
      auto RNaN = Builder.createFCmpUNO(RHS, RHS);
5302
309
      auto OLT = Builder.createFCmpOLT(LHS, RHS);
5303
309
      auto OGT = Builder.createFCmpOGT(LHS, RHS);
5304
309
      auto Ret = Builder.createBitCast(
5305
309
          Builder.createOr(Builder.createBitCast(LHS, Context.Int64x2Ty),
5306
309
                           Builder.createBitCast(RHS, Context.Int64x2Ty)),
5307
309
          LHS.getType());
5308
309
      Ret = Builder.createSelect(OGT, RHS, Ret);
5309
309
      Ret = Builder.createSelect(OLT, LHS, Ret);
5310
309
      Ret = Builder.createSelect(RNaN, RHS, Ret);
5311
309
      Ret = Builder.createSelect(LNaN, LHS, Ret);
5312
309
      return Ret;
5313
309
    });
5314
309
  }
5315
190
  void compileVectorVectorFMax(LLVM::Type VectorTy) noexcept {
5316
190
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
5317
190
      auto LNaN = Builder.createFCmpUNO(LHS, LHS);
5318
190
      auto RNaN = Builder.createFCmpUNO(RHS, RHS);
5319
190
      auto OLT = Builder.createFCmpOLT(LHS, RHS);
5320
190
      auto OGT = Builder.createFCmpOGT(LHS, RHS);
5321
190
      auto Ret = Builder.createBitCast(
5322
190
          Builder.createAnd(Builder.createBitCast(LHS, Context.Int64x2Ty),
5323
190
                            Builder.createBitCast(RHS, Context.Int64x2Ty)),
5324
190
          LHS.getType());
5325
190
      Ret = Builder.createSelect(OLT, RHS, Ret);
5326
190
      Ret = Builder.createSelect(OGT, LHS, Ret);
5327
190
      Ret = Builder.createSelect(RNaN, RHS, Ret);
5328
190
      Ret = Builder.createSelect(LNaN, LHS, Ret);
5329
190
      return Ret;
5330
190
    });
5331
190
  }
5332
366
  void compileVectorVectorFPMin(LLVM::Type VectorTy) noexcept {
5333
366
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
5334
366
      auto Cmp = Builder.createFCmpOLT(RHS, LHS);
5335
366
      return Builder.createSelect(Cmp, RHS, LHS);
5336
366
    });
5337
366
  }
5338
310
  void compileVectorVectorFPMax(LLVM::Type VectorTy) noexcept {
5339
310
    compileVectorVectorOp(VectorTy, [this](auto LHS, auto RHS) noexcept {
5340
310
      auto Cmp = Builder.createFCmpOGT(RHS, LHS);
5341
310
      return Builder.createSelect(Cmp, RHS, LHS);
5342
310
    });
5343
310
  }
5344
959
  void compileVectorTruncSatS32(LLVM::Type VectorTy, bool PadZero) noexcept {
5345
959
    compileVectorOp(VectorTy, [this, VectorTy, PadZero](auto V) noexcept {
5346
959
      const auto Size = VectorTy.getVectorSize();
5347
959
      auto FPTy = VectorTy.getElementType();
5348
959
      auto IntMin = LLContext.getInt32(
5349
959
          static_cast<uint32_t>(std::numeric_limits<int32_t>::min()));
5350
959
      auto IntMax = LLContext.getInt32(
5351
959
          static_cast<uint32_t>(std::numeric_limits<int32_t>::max()));
5352
959
      auto IntMinV = Builder.createVectorSplat(Size, IntMin);
5353
959
      auto IntMaxV = Builder.createVectorSplat(Size, IntMax);
5354
959
      auto IntZeroV = LLVM::Value::getConstNull(IntMinV.getType());
5355
959
      auto FPMin = Builder.createSIToFP(IntMin, FPTy);
5356
959
      auto FPMax = Builder.createSIToFP(IntMax, FPTy);
5357
959
      auto FPMinV = Builder.createVectorSplat(Size, FPMin);
5358
959
      auto FPMaxV = Builder.createVectorSplat(Size, FPMax);
5359
5360
959
      auto Normal = Builder.createFCmpORD(V, V);
5361
959
      auto NotUnder = Builder.createFCmpUGE(V, FPMinV);
5362
959
      auto NotOver = Builder.createFCmpULT(V, FPMaxV);
5363
959
      V = Builder.createFPToSI(
5364
959
          V, LLVM::Type::getVectorType(LLContext.getInt32Ty(), Size));
5365
959
      V = Builder.createSelect(Normal, V, IntZeroV);
5366
959
      V = Builder.createSelect(NotUnder, V, IntMinV);
5367
959
      V = Builder.createSelect(NotOver, V, IntMaxV);
5368
959
      if (PadZero) {
5369
744
        std::vector<uint32_t> Mask(Size * 2);
5370
744
        std::iota(Mask.begin(), Mask.end(), 0);
5371
744
        if constexpr (Endian::native == Endian::little) {
5372
744
          V = Builder.createShuffleVector(
5373
744
              V, IntZeroV, LLVM::Value::getConstVector32(LLContext, Mask));
5374
        } else {
5375
          V = Builder.createShuffleVector(
5376
              IntZeroV, V, LLVM::Value::getConstVector32(LLContext, Mask));
5377
        }
5378
744
      }
5379
959
      return V;
5380
959
    });
5381
959
  }
5382
5.84k
  void compileVectorTruncSatU32(LLVM::Type VectorTy, bool PadZero) noexcept {
5383
5.84k
    compileVectorOp(VectorTy, [this, VectorTy, PadZero](auto V) noexcept {
5384
5.84k
      const auto Size = VectorTy.getVectorSize();
5385
5.84k
      auto FPTy = VectorTy.getElementType();
5386
5.84k
      auto IntMin = LLContext.getInt32(std::numeric_limits<uint32_t>::min());
5387
5.84k
      auto IntMax = LLContext.getInt32(std::numeric_limits<uint32_t>::max());
5388
5.84k
      auto IntMinV = Builder.createVectorSplat(Size, IntMin);
5389
5.84k
      auto IntMaxV = Builder.createVectorSplat(Size, IntMax);
5390
5.84k
      auto FPMin = Builder.createUIToFP(IntMin, FPTy);
5391
5.84k
      auto FPMax = Builder.createUIToFP(IntMax, FPTy);
5392
5.84k
      auto FPMinV = Builder.createVectorSplat(Size, FPMin);
5393
5.84k
      auto FPMaxV = Builder.createVectorSplat(Size, FPMax);
5394
5395
5.84k
      auto NotUnder = Builder.createFCmpOGE(V, FPMinV);
5396
5.84k
      auto NotOver = Builder.createFCmpULT(V, FPMaxV);
5397
5.84k
      V = Builder.createFPToUI(
5398
5.84k
          V, LLVM::Type::getVectorType(LLContext.getInt32Ty(), Size));
5399
5.84k
      V = Builder.createSelect(NotUnder, V, IntMinV);
5400
5.84k
      V = Builder.createSelect(NotOver, V, IntMaxV);
5401
5.84k
      if (PadZero) {
5402
2.13k
        auto IntZeroV = LLVM::Value::getConstNull(IntMinV.getType());
5403
2.13k
        std::vector<uint32_t> Mask(Size * 2);
5404
2.13k
        std::iota(Mask.begin(), Mask.end(), 0);
5405
2.13k
        if constexpr (Endian::native == Endian::little) {
5406
2.13k
          V = Builder.createShuffleVector(
5407
2.13k
              V, IntZeroV, LLVM::Value::getConstVector32(LLContext, Mask));
5408
        } else {
5409
          V = Builder.createShuffleVector(
5410
              IntZeroV, V, LLVM::Value::getConstVector32(LLContext, Mask));
5411
        }
5412
2.13k
      }
5413
5.84k
      return V;
5414
5.84k
    });
5415
5.84k
  }
5416
  void compileVectorConvertS(LLVM::Type VectorTy, LLVM::Type FPVectorTy,
5417
719
                             bool Low) noexcept {
5418
719
    compileVectorOp(VectorTy,
5419
719
                    [this, VectorTy, FPVectorTy, Low](auto V) noexcept {
5420
719
                      if (Low) {
5421
329
                        const auto Size = VectorTy.getVectorSize() / 2;
5422
329
                        std::vector<uint32_t> Mask(Size);
5423
329
                        if constexpr (Endian::native == Endian::little) {
5424
329
                          std::iota(Mask.begin(), Mask.end(), 0);
5425
                        } else {
5426
                          std::iota(Mask.begin(), Mask.end(), Size);
5427
                        }
5428
329
                        V = Builder.createShuffleVector(
5429
329
                            V, LLVM::Value::getUndef(VectorTy),
5430
329
                            LLVM::Value::getConstVector32(LLContext, Mask));
5431
329
                      }
5432
719
                      return Builder.createSIToFP(V, FPVectorTy);
5433
719
                    });
5434
719
  }
5435
  void compileVectorConvertU(LLVM::Type VectorTy, LLVM::Type FPVectorTy,
5436
1.99k
                             bool Low) noexcept {
5437
1.99k
    compileVectorOp(VectorTy,
5438
1.99k
                    [this, VectorTy, FPVectorTy, Low](auto V) noexcept {
5439
1.99k
                      if (Low) {
5440
1.26k
                        const auto Size = VectorTy.getVectorSize() / 2;
5441
1.26k
                        std::vector<uint32_t> Mask(Size);
5442
1.26k
                        if constexpr (Endian::native == Endian::little) {
5443
1.26k
                          std::iota(Mask.begin(), Mask.end(), 0);
5444
                        } else {
5445
                          std::iota(Mask.begin(), Mask.end(), Size);
5446
                        }
5447
1.26k
                        V = Builder.createShuffleVector(
5448
1.26k
                            V, LLVM::Value::getUndef(VectorTy),
5449
1.26k
                            LLVM::Value::getConstVector32(LLContext, Mask));
5450
1.26k
                      }
5451
1.99k
                      return Builder.createUIToFP(V, FPVectorTy);
5452
1.99k
                    });
5453
1.99k
  }
5454
623
  void compileVectorDemote() noexcept {
5455
623
    compileVectorOp(Context.Doublex2Ty, [this](auto V) noexcept {
5456
623
      auto Demoted = Builder.createFPTrunc(
5457
623
          V, LLVM::Type::getVectorType(Context.FloatTy, 2));
5458
623
      auto ZeroV = LLVM::Value::getConstNull(Demoted.getType());
5459
623
      if constexpr (Endian::native == Endian::little) {
5460
623
        return Builder.createShuffleVector(
5461
623
            Demoted, ZeroV,
5462
623
            LLVM::Value::getConstVector32(LLContext, {0u, 1u, 2u, 3u}));
5463
      } else {
5464
        return Builder.createShuffleVector(
5465
            Demoted, ZeroV,
5466
            LLVM::Value::getConstVector32(LLContext, {3u, 2u, 1u, 0u}));
5467
      }
5468
623
    });
5469
623
  }
5470
610
  void compileVectorPromote() noexcept {
5471
610
    compileVectorOp(Context.Floatx4Ty, [this](auto V) noexcept {
5472
610
      auto UndefV = LLVM::Value::getUndef(V.getType());
5473
610
      auto Low = Builder.createShuffleVector(
5474
610
          V, UndefV, LLVM::Value::getConstVector32(LLContext, {0u, 1u}));
5475
610
      return Builder.createFPExt(
5476
610
          Low, LLVM::Type::getVectorType(Context.DoubleTy, 2));
5477
610
    });
5478
610
  }
5479
5480
0
  void compileVectorVectorMAdd(LLVM::Type VectorTy) noexcept {
5481
0
    auto C = Builder.createBitCast(stackPop(), VectorTy);
5482
0
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
5483
0
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
5484
0
    stackPush(Builder.createBitCast(
5485
0
        Builder.createFAdd(Builder.createFMul(LHS, RHS), C),
5486
0
        Context.Int64x2Ty));
5487
0
  }
5488
5489
0
  void compileVectorVectorNMAdd(LLVM::Type VectorTy) noexcept {
5490
0
    auto C = Builder.createBitCast(stackPop(), VectorTy);
5491
0
    auto RHS = Builder.createBitCast(stackPop(), VectorTy);
5492
0
    auto LHS = Builder.createBitCast(stackPop(), VectorTy);
5493
0
    stackPush(Builder.createBitCast(
5494
0
        Builder.createFAdd(Builder.createFMul(Builder.createFNeg(LHS), RHS), C),
5495
0
        Context.Int64x2Ty));
5496
0
  }
5497
5498
0
  void compileVectorRelaxedIntegerDotProduct() noexcept {
5499
0
    auto OriTy = Context.Int8x16Ty;
5500
0
    auto ExtTy = Context.Int16x8Ty;
5501
0
    auto RHS = Builder.createBitCast(stackPop(), OriTy);
5502
0
    auto LHS = Builder.createBitCast(stackPop(), OriTy);
5503
0
#if defined(__x86_64__)
5504
0
    if (Context.SupportSSSE3) {
5505
0
      assuming(LLVM::Core::X86SSSE3PMAddUbSw128 != LLVM::Core::NotIntrinsic);
5506
      // WebAssembly Relaxed SIMD spec: signed(LHS) * unsigned/signed(RHS)
5507
      // But PMAddUbSw128 is unsigned(LHS) * signed(RHS). Therefore swap both
5508
      // side to match the WebAssembly spec
5509
0
      return stackPush(Builder.createBitCast(
5510
0
          Builder.createIntrinsic(LLVM::Core::X86SSSE3PMAddUbSw128, {},
5511
0
                                  {RHS, LHS}),
5512
0
          Context.Int64x2Ty));
5513
0
    }
5514
0
#endif
5515
0
    auto Width = LLVM::Value::getConstInt(
5516
0
        ExtTy.getElementType(), OriTy.getElementType().getIntegerBitWidth());
5517
0
    Width = Builder.createVectorSplat(ExtTy.getVectorSize(), Width);
5518
0
    auto EA = Builder.createBitCast(LHS, ExtTy);
5519
0
    auto EB = Builder.createBitCast(RHS, ExtTy);
5520
5521
0
    LLVM::Value AL, AR, BL, BR;
5522
0
    AL = Builder.createAShr(EA, Width);
5523
0
    AR = Builder.createAShr(Builder.createShl(EA, Width), Width);
5524
0
    BL = Builder.createAShr(EB, Width);
5525
0
    BR = Builder.createAShr(Builder.createShl(EB, Width), Width);
5526
5527
0
    return stackPush(Builder.createBitCast(
5528
0
        Builder.createAdd(Builder.createMul(AL, BL), Builder.createMul(AR, BR)),
5529
0
        Context.Int64x2Ty));
5530
0
  }
5531
5532
0
  void compileVectorRelaxedIntegerDotProductAdd() noexcept {
5533
0
    auto OriTy = Context.Int8x16Ty;
5534
0
    auto ExtTy = Context.Int16x8Ty;
5535
0
    auto FinTy = Context.Int32x4Ty;
5536
0
    auto VC = Builder.createBitCast(stackPop(), FinTy);
5537
0
    auto RHS = Builder.createBitCast(stackPop(), OriTy);
5538
0
    auto LHS = Builder.createBitCast(stackPop(), OriTy);
5539
0
    LLVM::Value IM;
5540
0
#if defined(__x86_64__)
5541
0
    if (Context.SupportSSSE3) {
5542
0
      assuming(LLVM::Core::X86SSSE3PMAddUbSw128 != LLVM::Core::NotIntrinsic);
5543
      // WebAssembly Relaxed SIMD spec: signed(LHS) * unsigned/signed(RHS)
5544
      // But PMAddUbSw128 is unsigned(LHS) * signed(RHS). Therefore swap both
5545
      // side to match the WebAssembly spec
5546
0
      IM = Builder.createIntrinsic(LLVM::Core::X86SSSE3PMAddUbSw128, {},
5547
0
                                   {RHS, LHS});
5548
0
    } else
5549
0
#endif
5550
0
    {
5551
0
      auto Width = LLVM::Value::getConstInt(
5552
0
          ExtTy.getElementType(), OriTy.getElementType().getIntegerBitWidth());
5553
0
      Width = Builder.createVectorSplat(ExtTy.getVectorSize(), Width);
5554
0
      auto EA = Builder.createBitCast(LHS, ExtTy);
5555
0
      auto EB = Builder.createBitCast(RHS, ExtTy);
5556
5557
0
      LLVM::Value AL, AR, BL, BR;
5558
0
      AL = Builder.createAShr(EA, Width);
5559
0
      AR = Builder.createAShr(Builder.createShl(EA, Width), Width);
5560
0
      BL = Builder.createAShr(EB, Width);
5561
0
      BR = Builder.createAShr(Builder.createShl(EB, Width), Width);
5562
0
      IM = Builder.createAdd(Builder.createMul(AL, BL),
5563
0
                             Builder.createMul(AR, BR));
5564
0
    }
5565
5566
0
    auto Width = LLVM::Value::getConstInt(
5567
0
        FinTy.getElementType(), ExtTy.getElementType().getIntegerBitWidth());
5568
0
    Width = Builder.createVectorSplat(FinTy.getVectorSize(), Width);
5569
0
    auto IME = Builder.createBitCast(IM, FinTy);
5570
0
    auto L = Builder.createAShr(IME, Width);
5571
0
    auto R = Builder.createAShr(Builder.createShl(IME, Width), Width);
5572
5573
0
    return stackPush(Builder.createBitCast(
5574
0
        Builder.createAdd(Builder.createAdd(L, R), VC), Context.Int64x2Ty));
5575
0
  }
5576
5577
  void
5578
  enterBlock(LLVM::BasicBlock JumpBlock, LLVM::BasicBlock NextBlock,
5579
             LLVM::BasicBlock ElseBlock, std::vector<LLVM::Value> Args,
5580
             std::pair<std::vector<ValType>, std::vector<ValType>> Type,
5581
             std::vector<std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
5582
19.7k
                 ReturnPHI = {}) noexcept {
5583
19.7k
    assuming(Type.first.size() == Args.size());
5584
19.7k
    for (auto &Value : Args) {
5585
4.29k
      stackPush(Value);
5586
4.29k
    }
5587
19.7k
    const auto Unreachable = isUnreachable();
5588
19.7k
    ControlStack.emplace_back(Stack.size() - Args.size(), Unreachable,
5589
19.7k
                              JumpBlock, NextBlock, ElseBlock, std::move(Args),
5590
19.7k
                              std::move(Type), std::move(ReturnPHI));
5591
19.7k
  }
5592
5593
19.7k
  Control leaveBlock() noexcept {
5594
19.7k
    Control Entry = std::move(ControlStack.back());
5595
19.7k
    ControlStack.pop_back();
5596
5597
19.7k
    auto NextBlock = Entry.NextBlock ? Entry.NextBlock : Entry.JumpBlock;
5598
19.7k
    if (!Entry.Unreachable) {
5599
12.2k
      const auto &ReturnType = Entry.Type.second;
5600
12.2k
      if (!ReturnType.empty()) {
5601
9.19k
        std::vector<LLVM::Value> Rets(ReturnType.size());
5602
18.8k
        for (size_t I = 0; I < Rets.size(); ++I) {
5603
9.61k
          const size_t J = Rets.size() - 1 - I;
5604
9.61k
          Rets[J] = stackPop();
5605
9.61k
        }
5606
9.19k
        Entry.ReturnPHI.emplace_back(std::move(Rets), Builder.getInsertBlock());
5607
9.19k
      }
5608
12.2k
      Builder.createBr(NextBlock);
5609
12.2k
    } else {
5610
7.55k
      Builder.createUnreachable();
5611
7.55k
    }
5612
19.7k
    Builder.positionAtEnd(NextBlock);
5613
19.7k
    Stack.erase(Stack.begin() + static_cast<int64_t>(Entry.StackSize),
5614
19.7k
                Stack.end());
5615
19.7k
    return Entry;
5616
19.7k
  }
5617
5618
4.80k
  void checkStop() noexcept {
5619
4.80k
    if (!Interruptible) {
5620
4.80k
      return;
5621
4.80k
    }
5622
0
    auto NotStopBB = LLVM::BasicBlock::create(LLContext, F.Fn, "NotStop");
5623
0
    auto StopToken = Builder.createAtomicRMW(
5624
0
        LLVMAtomicRMWBinOpXchg, Context.getStopToken(Builder, ExecCtx),
5625
0
        LLContext.getInt32(0), LLVMAtomicOrderingMonotonic);
5626
#if LLVM_VERSION_MAJOR >= 13
5627
    StopToken.setAlignment(32);
5628
#endif
5629
0
    auto NotStop = Builder.createLikely(
5630
0
        Builder.createICmpEQ(StopToken, LLContext.getInt32(0)));
5631
0
    Builder.createCondBr(NotStop, NotStopBB,
5632
0
                         getTrapBB(ErrCode::Value::Interrupted));
5633
5634
0
    Builder.positionAtEnd(NotStopBB);
5635
0
  }
5636
5637
5.31k
  void setUnreachable() noexcept {
5638
5.31k
    if (ControlStack.empty()) {
5639
0
      IsUnreachable = true;
5640
5.31k
    } else {
5641
5.31k
      ControlStack.back().Unreachable = true;
5642
5.31k
    }
5643
5.31k
  }
5644
5645
1.64M
  bool isUnreachable() const noexcept {
5646
1.64M
    if (ControlStack.empty()) {
5647
9.82k
      return IsUnreachable;
5648
1.63M
    } else {
5649
1.63M
      return ControlStack.back().Unreachable;
5650
1.63M
    }
5651
1.64M
  }
5652
5653
  void
5654
  buildPHI(Span<const ValType> RetType,
5655
           Span<const std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
5656
17.1k
               Incomings) noexcept {
5657
17.1k
    if (isVoidReturn(RetType)) {
5658
5.48k
      return;
5659
5.48k
    }
5660
11.7k
    std::vector<LLVM::Value> Nodes;
5661
11.7k
    if (Incomings.size() == 0) {
5662
2.42k
      const auto &Types = toLLVMTypeVector(LLContext, RetType);
5663
2.42k
      Nodes.reserve(Types.size());
5664
2.75k
      for (LLVM::Type Type : Types) {
5665
2.75k
        Nodes.push_back(LLVM::Value::getUndef(Type));
5666
2.75k
      }
5667
9.28k
    } else if (Incomings.size() == 1) {
5668
8.20k
      Nodes = std::move(std::get<0>(Incomings.front()));
5669
8.20k
    } else {
5670
1.07k
      const auto &Types = toLLVMTypeVector(LLContext, RetType);
5671
1.07k
      Nodes.reserve(Types.size());
5672
2.26k
      for (size_t I = 0; I < Types.size(); ++I) {
5673
1.18k
        auto PHIRet = Builder.createPHI(Types[I]);
5674
3.14k
        for (auto &[Value, BB] : Incomings) {
5675
3.14k
          assuming(Value.size() == Types.size());
5676
3.14k
          PHIRet.addIncoming(Value[I], BB);
5677
3.14k
        }
5678
1.18k
        Nodes.push_back(PHIRet);
5679
1.18k
      }
5680
1.07k
    }
5681
12.4k
    for (auto &Val : Nodes) {
5682
12.4k
      stackPush(Val);
5683
12.4k
    }
5684
11.7k
  }
5685
5686
21.3k
  void setLableJumpPHI(unsigned int Index) noexcept {
5687
21.3k
    assuming(Index < ControlStack.size());
5688
21.3k
    auto &Entry = *(ControlStack.rbegin() + Index);
5689
21.3k
    if (Entry.NextBlock) { // is loop
5690
2.18k
      std::vector<LLVM::Value> Args(Entry.Type.first.size());
5691
4.03k
      for (size_t I = 0; I < Args.size(); ++I) {
5692
1.84k
        const size_t J = Args.size() - 1 - I;
5693
1.84k
        Args[J] = stackPop();
5694
1.84k
      }
5695
4.03k
      for (size_t I = 0; I < Args.size(); ++I) {
5696
1.84k
        Entry.Args[I].addIncoming(Args[I], Builder.getInsertBlock());
5697
1.84k
        stackPush(Args[I]);
5698
1.84k
      }
5699
19.1k
    } else if (!Entry.Type.second.empty()) { // has return value
5700
1.92k
      std::vector<LLVM::Value> Rets(Entry.Type.second.size());
5701
3.98k
      for (size_t I = 0; I < Rets.size(); ++I) {
5702
2.06k
        const size_t J = Rets.size() - 1 - I;
5703
2.06k
        Rets[J] = stackPop();
5704
2.06k
      }
5705
3.98k
      for (size_t I = 0; I < Rets.size(); ++I) {
5706
2.06k
        stackPush(Rets[I]);
5707
2.06k
      }
5708
1.92k
      Entry.ReturnPHI.emplace_back(std::move(Rets), Builder.getInsertBlock());
5709
1.92k
    }
5710
21.3k
  }
5711
5712
21.3k
  LLVM::BasicBlock getLabel(unsigned int Index) const noexcept {
5713
21.3k
    return (ControlStack.rbegin() + Index)->JumpBlock;
5714
21.3k
  }
5715
5716
961k
  void stackPush(LLVM::Value Value) noexcept { Stack.push_back(Value); }
5717
368k
  LLVM::Value stackPop() noexcept {
5718
368k
    assuming(!ControlStack.empty() || !Stack.empty());
5719
368k
    assuming(ControlStack.empty() ||
5720
368k
             Stack.size() > ControlStack.back().StackSize);
5721
368k
    auto Value = Stack.back();
5722
368k
    Stack.pop_back();
5723
368k
    return Value;
5724
368k
  }
5725
5726
23.7k
  LLVM::Value switchEndian(LLVM::Value Value) {
5727
    if constexpr (Endian::native == Endian::big) {
5728
      auto Type = Value.getType();
5729
      if ((Type.isIntegerTy() && Type.getIntegerBitWidth() > 8) ||
5730
          (Type.isVectorTy() && Type.getVectorSize() == 1)) {
5731
        return Builder.createUnaryIntrinsic(LLVM::Core::Bswap, Value);
5732
      }
5733
      if (Type.isVectorTy()) {
5734
        LLVM::Type VecType = Type.getElementType().getIntegerBitWidth() == 128
5735
                                 ? Context.Int128Ty
5736
                                 : Context.Int64Ty;
5737
        Value = Builder.createBitCast(Value, VecType);
5738
        Value = Builder.createUnaryIntrinsic(LLVM::Core::Bswap, Value);
5739
        return Builder.createBitCast(Value, Type);
5740
      }
5741
      if (Type.isFloatTy() || Type.isDoubleTy()) {
5742
        LLVM::Type IntType =
5743
            Type.isFloatTy() ? Context.Int32Ty : Context.Int64Ty;
5744
        Value = Builder.createBitCast(Value, IntType);
5745
        Value = Builder.createUnaryIntrinsic(LLVM::Core::Bswap, Value);
5746
        return Builder.createBitCast(Value, Type);
5747
      }
5748
    }
5749
23.7k
    return Value;
5750
23.7k
  }
5751
5752
  LLVM::Compiler::CompileContext &Context;
5753
  LLVM::Context LLContext;
5754
  std::vector<std::pair<LLVM::Type, LLVM::Value>> Local;
5755
  std::vector<LLVM::Value> Stack;
5756
  LLVM::Value LocalInstrCount = nullptr;
5757
  LLVM::Value LocalGas = nullptr;
5758
  std::unordered_map<ErrCode::Value, LLVM::BasicBlock> TrapBB;
5759
  bool IsUnreachable = false;
5760
  bool Interruptible = false;
5761
  struct Control {
5762
    size_t StackSize;
5763
    bool Unreachable;
5764
    LLVM::BasicBlock JumpBlock;
5765
    LLVM::BasicBlock NextBlock;
5766
    LLVM::BasicBlock ElseBlock;
5767
    std::vector<LLVM::Value> Args;
5768
    std::pair<std::vector<ValType>, std::vector<ValType>> Type;
5769
    std::vector<std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
5770
        ReturnPHI;
5771
    Control(size_t S, bool U, LLVM::BasicBlock J, LLVM::BasicBlock N,
5772
            LLVM::BasicBlock E, std::vector<LLVM::Value> A,
5773
            std::pair<std::vector<ValType>, std::vector<ValType>> T,
5774
            std::vector<std::tuple<std::vector<LLVM::Value>, LLVM::BasicBlock>>
5775
                R) noexcept
5776
19.7k
        : StackSize(S), Unreachable(U), JumpBlock(J), NextBlock(N),
5777
19.7k
          ElseBlock(E), Args(std::move(A)), Type(std::move(T)),
5778
19.7k
          ReturnPHI(std::move(R)) {}
5779
    Control(const Control &) = default;
5780
24.5k
    Control(Control &&) = default;
5781
    Control &operator=(const Control &) = default;
5782
968
    Control &operator=(Control &&) = default;
5783
  };
5784
  std::vector<Control> ControlStack;
5785
  LLVM::FunctionCallee F;
5786
  LLVM::Value ExecCtx;
5787
  LLVM::Builder Builder;
5788
};
5789
5790
std::vector<LLVM::Value> unpackStruct(LLVM::Builder &Builder,
5791
384
                                      LLVM::Value Struct) noexcept {
5792
384
  const auto N = Struct.getType().getStructNumElements();
5793
384
  std::vector<LLVM::Value> Ret;
5794
384
  Ret.reserve(N);
5795
1.39k
  for (unsigned I = 0; I < N; ++I) {
5796
1.00k
    Ret.push_back(Builder.createExtractValue(Struct, I));
5797
1.00k
  }
5798
384
  return Ret;
5799
384
}
5800
5801
} // namespace
5802
5803
namespace WasmEdge {
5804
namespace LLVM {
5805
5806
2.00k
Expect<void> Compiler::checkConfigure() noexcept {
5807
2.00k
  if (Conf.hasProposal(Proposal::ExceptionHandling)) {
5808
0
    spdlog::error(ErrCode::Value::InvalidConfigure);
5809
0
    spdlog::error(
5810
0
        "    Proposal ExceptionHandling is not yet supported in LLVM backend");
5811
0
    return Unexpect(ErrCode::Value::InvalidConfigure);
5812
0
  }
5813
2.00k
  return {};
5814
2.00k
}
5815
5816
2.00k
Expect<Data> Compiler::compile(const AST::Module &Module) noexcept {
5817
  // Check the module is validated.
5818
2.00k
  if (unlikely(!Module.getIsValidated())) {
5819
0
    spdlog::error(ErrCode::Value::NotValidated);
5820
0
    return Unexpect(ErrCode::Value::NotValidated);
5821
0
  }
5822
5823
2.00k
  std::unique_lock Lock(Mutex);
5824
2.00k
  spdlog::info("compile start"sv);
5825
5826
2.00k
  LLVM::Core::init();
5827
5828
2.00k
  LLVM::Data D;
5829
2.00k
  auto LLContext = D.extract().getLLContext();
5830
2.00k
  auto &LLModule = D.extract().LLModule;
5831
2.00k
  LLModule.setTarget(LLVM::getDefaultTargetTriple().unwrap());
5832
2.00k
  LLModule.addFlag(LLVMModuleFlagBehaviorError, "PIC Level"sv, 2);
5833
5834
2.00k
  CompileContext NewContext(LLContext, LLModule,
5835
2.00k
                            Conf.getCompilerConfigure().isGenericBinary());
5836
2.00k
  struct RAIICleanup {
5837
2.00k
    RAIICleanup(CompileContext *&Context, CompileContext &NewContext)
5838
2.00k
        : Context(Context) {
5839
2.00k
      Context = &NewContext;
5840
2.00k
    }
5841
2.00k
    ~RAIICleanup() { Context = nullptr; }
5842
2.00k
    CompileContext *&Context;
5843
2.00k
  };
5844
2.00k
  RAIICleanup Cleanup(Context, NewContext);
5845
5846
  // Compile Function Types
5847
2.00k
  compile(Module.getTypeSection());
5848
  // Compile ImportSection
5849
2.00k
  compile(Module.getImportSection());
5850
  // Compile GlobalSection
5851
2.00k
  compile(Module.getGlobalSection());
5852
  // Compile MemorySection (MemorySec, DataSec)
5853
2.00k
  compile(Module.getMemorySection(), Module.getDataSection());
5854
  // Compile TableSection (TableSec, ElemSec)
5855
2.00k
  compile(Module.getTableSection(), Module.getElementSection());
5856
  // compile Functions in module. (FunctionSec, CodeSec)
5857
2.00k
  compile(Module.getFunctionSection(), Module.getCodeSection());
5858
  // Compile ExportSection
5859
2.00k
  compile(Module.getExportSection());
5860
  // StartSection is not required to compile
5861
5862
2.00k
  spdlog::info("verify start"sv);
5863
2.00k
  LLModule.verify(LLVMPrintMessageAction);
5864
5865
2.00k
  spdlog::info("optimize start"sv);
5866
2.00k
  auto &TM = D.extract().TM;
5867
2.00k
  {
5868
2.00k
    auto Triple = LLModule.getTarget();
5869
2.00k
    auto [TheTarget, ErrorMessage] = LLVM::Target::getFromTriple(Triple);
5870
2.00k
    if (ErrorMessage) {
5871
0
      spdlog::error("getFromTriple failed:{}"sv, ErrorMessage.string_view());
5872
0
      return Unexpect(ErrCode::Value::IllegalPath);
5873
2.00k
    } else {
5874
2.00k
      std::string CPUName;
5875
#if defined(__riscv) && __riscv_xlen == 64
5876
      CPUName = "generic-rv64"s;
5877
#else
5878
2.00k
      if (!Conf.getCompilerConfigure().isGenericBinary()) {
5879
2.00k
        CPUName = LLVM::getHostCPUName().string_view();
5880
2.00k
      } else {
5881
0
        CPUName = "generic"s;
5882
0
      }
5883
2.00k
#endif
5884
5885
2.00k
      TM = LLVM::TargetMachine::create(
5886
2.00k
          TheTarget, Triple, CPUName.c_str(),
5887
2.00k
          LLVM::getHostCPUFeatures().unwrap(),
5888
2.00k
          toLLVMCodeGenLevel(
5889
2.00k
              Conf.getCompilerConfigure().getOptimizationLevel()),
5890
2.00k
          LLVMRelocPIC, LLVMCodeModelDefault);
5891
2.00k
    }
5892
5893
#if LLVM_VERSION_MAJOR >= 13
5894
    auto PBO = LLVM::PassBuilderOptions::create();
5895
    if (auto Error = PBO.runPasses(
5896
            LLModule,
5897
            toLLVMLevel(Conf.getCompilerConfigure().getOptimizationLevel()),
5898
            TM)) {
5899
      spdlog::error("{}"sv, Error.message().string_view());
5900
    }
5901
#else
5902
2.00k
    auto FP = LLVM::PassManager::createForModule(LLModule);
5903
2.00k
    auto MP = LLVM::PassManager::create();
5904
5905
2.00k
    TM.addAnalysisPasses(MP);
5906
2.00k
    TM.addAnalysisPasses(FP);
5907
2.00k
    {
5908
2.00k
      auto PMB = LLVM::PassManagerBuilder::create();
5909
2.00k
      auto [OptLevel, SizeLevel] =
5910
2.00k
          toLLVMLevel(Conf.getCompilerConfigure().getOptimizationLevel());
5911
2.00k
      PMB.setOptLevel(OptLevel);
5912
2.00k
      PMB.setSizeLevel(SizeLevel);
5913
2.00k
      PMB.populateFunctionPassManager(FP);
5914
2.00k
      PMB.populateModulePassManager(MP);
5915
2.00k
    }
5916
2.00k
    switch (Conf.getCompilerConfigure().getOptimizationLevel()) {
5917
0
    case CompilerConfigure::OptimizationLevel::O0:
5918
0
    case CompilerConfigure::OptimizationLevel::O1:
5919
0
      FP.addTailCallEliminationPass();
5920
0
      break;
5921
2.00k
    default:
5922
2.00k
      break;
5923
2.00k
    }
5924
5925
2.00k
    FP.initializeFunctionPassManager();
5926
22.2k
    for (auto Fn = LLModule.getFirstFunction(); Fn; Fn = Fn.getNextFunction()) {
5927
20.2k
      FP.runFunctionPassManager(Fn);
5928
20.2k
    }
5929
2.00k
    FP.finalizeFunctionPassManager();
5930
2.00k
    MP.runPassManager(LLModule);
5931
2.00k
#endif
5932
2.00k
  }
5933
5934
  // Set initializer for constant value
5935
2.00k
  if (auto IntrinsicsTable = LLModule.getNamedGlobal("intrinsics")) {
5936
1.14k
    IntrinsicsTable.setInitializer(
5937
1.14k
        LLVM::Value::getConstNull(IntrinsicsTable.getType()));
5938
1.14k
    IntrinsicsTable.setGlobalConstant(false);
5939
1.14k
  } else {
5940
856
    auto IntrinsicsTableTy = LLVM::Type::getArrayType(
5941
856
        LLContext.getInt8Ty().getPointerTo(),
5942
856
        static_cast<uint32_t>(Executable::Intrinsics::kIntrinsicMax));
5943
856
    LLModule.addGlobal(
5944
856
        IntrinsicsTableTy.getPointerTo(), false, LLVMExternalLinkage,
5945
856
        LLVM::Value::getConstNull(IntrinsicsTableTy), "intrinsics");
5946
856
  }
5947
5948
2.00k
  spdlog::info("optimize done"sv);
5949
2.00k
  return Expect<Data>{std::move(D)};
5950
2.00k
}
5951
5952
2.00k
void Compiler::compile(const AST::TypeSection &TypeSec) noexcept {
5953
2.00k
  auto WrapperTy =
5954
2.00k
      LLVM::Type::getFunctionType(Context->VoidTy,
5955
2.00k
                                  {Context->ExecCtxPtrTy, Context->Int8PtrTy,
5956
2.00k
                                   Context->Int8PtrTy, Context->Int8PtrTy},
5957
2.00k
                                  false);
5958
2.00k
  auto SubTypes = TypeSec.getContent();
5959
2.00k
  const auto Size = SubTypes.size();
5960
2.00k
  if (Size == 0) {
5961
107
    return;
5962
107
  }
5963
1.89k
  Context->CompositeTypes.reserve(Size);
5964
1.89k
  Context->FunctionWrappers.reserve(Size);
5965
5966
  // Iterate and compile types.
5967
5.84k
  for (size_t I = 0; I < Size; ++I) {
5968
3.95k
    const auto &CompType = SubTypes[I].getCompositeType();
5969
3.95k
    const auto Name = fmt::format("t{}"sv, Context->CompositeTypes.size());
5970
3.95k
    if (CompType.isFunc()) {
5971
      // Check function type is unique
5972
3.95k
      {
5973
3.95k
        bool Unique = true;
5974
14.3k
        for (size_t J = 0; J < I; ++J) {
5975
10.5k
          if (Context->CompositeTypes[J] &&
5976
10.5k
              Context->CompositeTypes[J]->isFunc()) {
5977
10.5k
            const auto &OldFuncType = Context->CompositeTypes[J]->getFuncType();
5978
10.5k
            if (OldFuncType == CompType.getFuncType()) {
5979
123
              Unique = false;
5980
123
              Context->CompositeTypes.push_back(Context->CompositeTypes[J]);
5981
123
              auto F = Context->FunctionWrappers[J];
5982
123
              Context->FunctionWrappers.push_back(F);
5983
123
              auto A = Context->LLModule.addAlias(WrapperTy, F, Name.c_str());
5984
123
              A.setLinkage(LLVMExternalLinkage);
5985
123
              A.setVisibility(LLVMProtectedVisibility);
5986
123
              A.setDSOLocal(true);
5987
123
              A.setDLLStorageClass(LLVMDLLExportStorageClass);
5988
123
              break;
5989
123
            }
5990
10.5k
          }
5991
10.5k
        }
5992
3.95k
        if (!Unique) {
5993
123
          continue;
5994
123
        }
5995
3.95k
      }
5996
5997
      // Create Wrapper
5998
3.82k
      auto F = Context->LLModule.addFunction(WrapperTy, LLVMExternalLinkage,
5999
3.82k
                                             Name.c_str());
6000
3.82k
      {
6001
3.82k
        F.setVisibility(LLVMProtectedVisibility);
6002
3.82k
        F.setDSOLocal(true);
6003
3.82k
        F.setDLLStorageClass(LLVMDLLExportStorageClass);
6004
3.82k
        F.addFnAttr(Context->NoStackArgProbe);
6005
3.82k
        F.addFnAttr(Context->StrictFP);
6006
3.82k
        F.addFnAttr(Context->UWTable);
6007
3.82k
        F.addParamAttr(0, Context->ReadOnly);
6008
3.82k
        F.addParamAttr(0, Context->NoAlias);
6009
3.82k
        F.addParamAttr(1, Context->NoAlias);
6010
3.82k
        F.addParamAttr(2, Context->NoAlias);
6011
3.82k
        F.addParamAttr(3, Context->NoAlias);
6012
6013
3.82k
        LLVM::Builder Builder(Context->LLContext);
6014
3.82k
        Builder.positionAtEnd(
6015
3.82k
            LLVM::BasicBlock::create(Context->LLContext, F, "entry"));
6016
6017
3.82k
        auto FTy = toLLVMType(Context->LLContext, Context->ExecCtxPtrTy,
6018
3.82k
                              CompType.getFuncType());
6019
3.82k
        auto RTy = FTy.getReturnType();
6020
3.82k
        std::vector<LLVM::Type> FPTy(FTy.getNumParams());
6021
3.82k
        FTy.getParamTypes(FPTy);
6022
6023
3.82k
        const size_t ArgCount = FPTy.size() - 1;
6024
3.82k
        auto ExecCtxPtr = F.getFirstParam();
6025
3.82k
        auto RawFunc = LLVM::FunctionCallee{
6026
3.82k
            FTy, Builder.createBitCast(ExecCtxPtr.getNextParam(),
6027
3.82k
                                       FTy.getPointerTo())};
6028
3.82k
        auto RawArgs = ExecCtxPtr.getNextParam().getNextParam();
6029
3.82k
        auto RawRets = RawArgs.getNextParam();
6030
6031
3.82k
        std::vector<LLVM::Value> Args;
6032
3.82k
        Args.reserve(FTy.getNumParams());
6033
3.82k
        Args.push_back(ExecCtxPtr);
6034
8.06k
        for (size_t J = 0; J < ArgCount; ++J) {
6035
4.23k
          Args.push_back(Builder.createValuePtrLoad(
6036
4.23k
              FPTy[J + 1], RawArgs, Context->Int8Ty, J * kValSize));
6037
4.23k
        }
6038
6039
3.82k
        auto Ret = Builder.createCall(RawFunc, Args);
6040
3.82k
        if (RTy.isVoidTy()) {
6041
          // nothing to do
6042
2.52k
        } else if (RTy.isStructTy()) {
6043
304
          auto Rets = unpackStruct(Builder, Ret);
6044
304
          Builder.createArrayPtrStore(Rets, RawRets, Context->Int8Ty, kValSize);
6045
2.21k
        } else {
6046
2.21k
          Builder.createValuePtrStore(Ret, RawRets, Context->Int8Ty);
6047
2.21k
        }
6048
3.82k
        Builder.createRetVoid();
6049
3.82k
      }
6050
      // Copy wrapper, param and return lists to module instance.
6051
3.82k
      Context->FunctionWrappers.push_back(F);
6052
3.82k
    } else {
6053
      // Non function type case. Create empty wrapper.
6054
0
      auto F = Context->LLModule.addFunction(WrapperTy, LLVMExternalLinkage,
6055
0
                                             Name.c_str());
6056
0
      {
6057
0
        F.setVisibility(LLVMProtectedVisibility);
6058
0
        F.setDSOLocal(true);
6059
0
        F.setDLLStorageClass(LLVMDLLExportStorageClass);
6060
0
        F.addFnAttr(Context->NoStackArgProbe);
6061
0
        F.addFnAttr(Context->StrictFP);
6062
0
        F.addFnAttr(Context->UWTable);
6063
0
        F.addParamAttr(0, Context->ReadOnly);
6064
0
        F.addParamAttr(0, Context->NoAlias);
6065
0
        F.addParamAttr(1, Context->NoAlias);
6066
0
        F.addParamAttr(2, Context->NoAlias);
6067
0
        F.addParamAttr(3, Context->NoAlias);
6068
6069
0
        LLVM::Builder Builder(Context->LLContext);
6070
0
        Builder.positionAtEnd(
6071
0
            LLVM::BasicBlock::create(Context->LLContext, F, "entry"));
6072
0
        Builder.createRetVoid();
6073
0
      }
6074
0
      Context->FunctionWrappers.push_back(F);
6075
0
    }
6076
3.82k
    Context->CompositeTypes.push_back(&CompType);
6077
3.82k
  }
6078
1.89k
}
6079
6080
2.00k
void Compiler::compile(const AST::ImportSection &ImportSec) noexcept {
6081
  // Iterate and compile import descriptions.
6082
2.00k
  for (const auto &ImpDesc : ImportSec.getContent()) {
6083
    // Get data from import description.
6084
351
    const auto &ExtType = ImpDesc.getExternalType();
6085
6086
    // Add the imports into module instance.
6087
351
    switch (ExtType) {
6088
263
    case ExternalType::Function: // Function type index
6089
263
    {
6090
263
      const auto FuncID = static_cast<uint32_t>(Context->Functions.size());
6091
      // Get the function type index in module.
6092
263
      uint32_t TypeIdx = ImpDesc.getExternalFuncTypeIdx();
6093
263
      assuming(TypeIdx < Context->CompositeTypes.size());
6094
263
      assuming(Context->CompositeTypes[TypeIdx]->isFunc());
6095
263
      const auto &FuncType = Context->CompositeTypes[TypeIdx]->getFuncType();
6096
263
      auto FTy =
6097
263
          toLLVMType(Context->LLContext, Context->ExecCtxPtrTy, FuncType);
6098
263
      auto RTy = FTy.getReturnType();
6099
263
      auto F = LLVM::FunctionCallee{
6100
263
          FTy,
6101
263
          Context->LLModule.addFunction(FTy, LLVMInternalLinkage,
6102
263
                                        fmt::format("f{}"sv, FuncID).c_str())};
6103
263
      F.Fn.setDSOLocal(true);
6104
263
      F.Fn.addFnAttr(Context->NoStackArgProbe);
6105
263
      F.Fn.addFnAttr(Context->StrictFP);
6106
263
      F.Fn.addFnAttr(Context->UWTable);
6107
263
      F.Fn.addParamAttr(0, Context->ReadOnly);
6108
263
      F.Fn.addParamAttr(0, Context->NoAlias);
6109
6110
263
      LLVM::Builder Builder(Context->LLContext);
6111
263
      Builder.positionAtEnd(
6112
263
          LLVM::BasicBlock::create(Context->LLContext, F.Fn, "entry"));
6113
6114
263
      const auto ArgSize = FuncType.getParamTypes().size();
6115
263
      const auto RetSize =
6116
263
          RTy.isVoidTy() ? 0 : FuncType.getReturnTypes().size();
6117
6118
263
      LLVM::Value Args = Builder.createArray(ArgSize, kValSize);
6119
263
      LLVM::Value Rets = Builder.createArray(RetSize, kValSize);
6120
6121
263
      auto Arg = F.Fn.getFirstParam();
6122
384
      for (unsigned I = 0; I < ArgSize; ++I) {
6123
121
        Arg = Arg.getNextParam();
6124
121
        Builder.createValuePtrStore(Arg, Args, Context->Int8Ty, I * kValSize);
6125
121
      }
6126
6127
263
      Builder.createCall(
6128
263
          Context->getIntrinsic(
6129
263
              Builder, Executable::Intrinsics::kCall,
6130
263
              LLVM::Type::getFunctionType(
6131
263
                  Context->VoidTy,
6132
263
                  {Context->Int32Ty, Context->Int8PtrTy, Context->Int8PtrTy},
6133
263
                  false)),
6134
263
          {Context->LLContext.getInt32(FuncID), Args, Rets});
6135
6136
263
      if (RetSize == 0) {
6137
144
        Builder.createRetVoid();
6138
144
      } else if (RetSize == 1) {
6139
92
        Builder.createRet(
6140
92
            Builder.createValuePtrLoad(RTy, Rets, Context->Int8Ty));
6141
92
      } else {
6142
27
        Builder.createAggregateRet(Builder.createArrayPtrLoad(
6143
27
            RetSize, RTy, Rets, Context->Int8Ty, kValSize));
6144
27
      }
6145
6146
263
      Context->Functions.emplace_back(TypeIdx, F, nullptr);
6147
263
      break;
6148
263
    }
6149
41
    case ExternalType::Table: // Table type
6150
41
    {
6151
      // Nothing to do.
6152
41
      break;
6153
263
    }
6154
8
    case ExternalType::Memory: // Memory type
6155
8
    {
6156
      // Nothing to do.
6157
8
      break;
6158
263
    }
6159
39
    case ExternalType::Global: // Global type
6160
39
    {
6161
      // Get global type. External type checked in validation.
6162
39
      const auto &GlobType = ImpDesc.getExternalGlobalType();
6163
39
      const auto &ValType = GlobType.getValType();
6164
39
      auto Type = toLLVMType(Context->LLContext, ValType);
6165
39
      Context->Globals.push_back(Type);
6166
39
      break;
6167
263
    }
6168
0
    default:
6169
0
      break;
6170
351
    }
6171
351
  }
6172
2.00k
}
6173
6174
2.00k
void Compiler::compile(const AST::ExportSection &) noexcept {}
6175
6176
2.00k
void Compiler::compile(const AST::GlobalSection &GlobalSec) noexcept {
6177
2.00k
  for (const auto &GlobalSeg : GlobalSec.getContent()) {
6178
104
    const auto &ValType = GlobalSeg.getGlobalType().getValType();
6179
104
    auto Type = toLLVMType(Context->LLContext, ValType);
6180
104
    Context->Globals.push_back(Type);
6181
104
  }
6182
2.00k
}
6183
6184
void Compiler::compile(const AST::MemorySection &,
6185
2.00k
                       const AST::DataSection &) noexcept {}
6186
6187
void Compiler::compile(const AST::TableSection &,
6188
2.00k
                       const AST::ElementSection &) noexcept {}
6189
6190
void Compiler::compile(const AST::FunctionSection &FuncSec,
6191
2.00k
                       const AST::CodeSection &CodeSec) noexcept {
6192
2.00k
  const auto &TypeIdxs = FuncSec.getContent();
6193
2.00k
  const auto &CodeSegs = CodeSec.getContent();
6194
2.00k
  if (TypeIdxs.size() == 0 || CodeSegs.size() == 0) {
6195
185
    return;
6196
185
  }
6197
6198
11.6k
  for (size_t I = 0; I < TypeIdxs.size() && I < CodeSegs.size(); ++I) {
6199
9.82k
    const auto &TypeIdx = TypeIdxs[I];
6200
9.82k
    const auto &Code = CodeSegs[I];
6201
9.82k
    assuming(TypeIdx < Context->CompositeTypes.size());
6202
9.82k
    assuming(Context->CompositeTypes[TypeIdx]->isFunc());
6203
9.82k
    const auto &FuncType = Context->CompositeTypes[TypeIdx]->getFuncType();
6204
9.82k
    const auto FuncID = Context->Functions.size();
6205
9.82k
    auto FTy = toLLVMType(Context->LLContext, Context->ExecCtxPtrTy, FuncType);
6206
9.82k
    LLVM::FunctionCallee F = {FTy, Context->LLModule.addFunction(
6207
9.82k
                                       FTy, LLVMExternalLinkage,
6208
9.82k
                                       fmt::format("f{}"sv, FuncID).c_str())};
6209
9.82k
    F.Fn.setVisibility(LLVMProtectedVisibility);
6210
9.82k
    F.Fn.setDSOLocal(true);
6211
9.82k
    F.Fn.setDLLStorageClass(LLVMDLLExportStorageClass);
6212
9.82k
    F.Fn.addFnAttr(Context->NoStackArgProbe);
6213
9.82k
    F.Fn.addFnAttr(Context->StrictFP);
6214
9.82k
    F.Fn.addFnAttr(Context->UWTable);
6215
9.82k
    F.Fn.addParamAttr(0, Context->ReadOnly);
6216
9.82k
    F.Fn.addParamAttr(0, Context->NoAlias);
6217
6218
9.82k
    Context->Functions.emplace_back(TypeIdx, F, &Code);
6219
9.82k
  }
6220
6221
9.93k
  for (auto [T, F, Code] : Context->Functions) {
6222
9.93k
    if (!Code) {
6223
110
      continue;
6224
110
    }
6225
6226
9.82k
    std::vector<ValType> Locals;
6227
9.82k
    for (const auto &Local : Code->getLocals()) {
6228
2.02M
      for (unsigned I = 0; I < Local.first; ++I) {
6229
2.02M
        Locals.push_back(Local.second);
6230
2.02M
      }
6231
1.43k
    }
6232
9.82k
    FunctionCompiler FC(*Context, F, Locals,
6233
9.82k
                        Conf.getCompilerConfigure().isInterruptible(),
6234
9.82k
                        Conf.getStatisticsConfigure().isInstructionCounting(),
6235
9.82k
                        Conf.getStatisticsConfigure().isCostMeasuring());
6236
9.82k
    auto Type = Context->resolveBlockType(T);
6237
9.82k
    FC.compile(*Code, std::move(Type));
6238
9.82k
    F.Fn.eliminateUnreachableBlocks();
6239
9.82k
  }
6240
1.81k
}
6241
6242
} // namespace LLVM
6243
} // namespace WasmEdge