blob: 6f7e503982c778c2be719c3651f2174c1d068a9f [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman89401822014-05-06 15:04:28 -040014
Ben Claytonac07ed82019-03-26 14:17:41 +000015#include "LLVMReactor.hpp"
Nicolas Capens41a73022020-01-30 00:30:14 -050016
17#include "CPUID.hpp"
Ben Clayton713b8d32019-12-17 20:37:56 +000018#include "Debug.hpp"
Ben Claytonac07ed82019-03-26 14:17:41 +000019#include "LLVMReactorDebugInfo.hpp"
Nicolas Capensd0703092022-05-26 12:58:20 -040020#include "PragmaInternals.hpp"
Antonio Maiorano415d1812020-02-11 16:22:55 -050021#include "Print.hpp"
Ben Clayton713b8d32019-12-17 20:37:56 +000022#include "Reactor.hpp"
Nicolas Capens3b0ad202022-06-02 15:02:31 -040023#include "SIMD.hpp"
Ben Clayton713b8d32019-12-17 20:37:56 +000024#include "x86.hpp"
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040025
Ben Clayton713b8d32019-12-17 20:37:56 +000026#include "llvm/IR/Intrinsics.h"
Antonio Maiorano6a6ae442020-07-20 14:11:48 -040027#include "llvm/IR/IntrinsicsX86.h"
Antonio Maiorano6a6ae442020-07-20 14:11:48 -040028#include "llvm/Support/Alignment.h"
Nicolas Capens88fe9ce2022-04-19 18:32:26 -040029#include "llvm/Support/Error.h"
Antonio Maiorano8b4cf1c2021-01-26 14:40:03 -050030#include "llvm/Support/ManagedStatic.h"
Nicolas Capensea5f37f2022-04-19 16:08:20 -040031
John Bauman89401822014-05-06 15:04:28 -040032#include <fstream>
Ben Claytoncee3dff2019-05-22 12:01:22 +010033#include <iostream>
34#include <mutex>
Ben Clayton1bc7ee92019-02-14 18:43:22 +000035#include <numeric>
36#include <thread>
Nicolas Capens41a73022020-01-30 00:30:14 -050037#include <unordered_map>
John Bauman89401822014-05-06 15:04:28 -040038
Nicolas Capens47dc8672017-04-25 12:54:39 -040039#if defined(__i386__) || defined(__x86_64__)
Ben Clayton713b8d32019-12-17 20:37:56 +000040# include <xmmintrin.h>
Nicolas Capens47dc8672017-04-25 12:54:39 -040041#endif
42
Logan Chien40a60052018-09-26 19:03:53 +080043#include <math.h>
44
Nicolas Capenscb122582014-05-06 23:34:44 -040045#if defined(__x86_64__) && defined(_WIN32)
Nicolas Capens41a73022020-01-30 00:30:14 -050046extern "C" void X86CompilationCallback()
Ben Clayton713b8d32019-12-17 20:37:56 +000047{
Ben Claytonce54c592020-02-07 11:30:51 +000048 UNIMPLEMENTED_NO_BUG("X86CompilationCallback");
Ben Clayton713b8d32019-12-17 20:37:56 +000049}
Ben Clayton2f58df32019-06-23 21:29:25 +010050#endif
51
Ben Clayton20cf5c52019-07-01 11:13:27 +010052#if !LLVM_ENABLE_THREADS
53# error "LLVM_ENABLE_THREADS needs to be enabled"
54#endif
55
Googler4d22b2c2021-02-01 12:10:01 +000056#if LLVM_VERSION_MAJOR < 11
57namespace llvm {
58using FixedVectorType = VectorType;
59} // namespace llvm
60#endif
61
Nicolas Capens157ba262019-12-10 17:49:14 -050062namespace {
63
Antonio Maiorano8b4cf1c2021-01-26 14:40:03 -050064// Used to automatically invoke llvm_shutdown() when driver is unloaded
65llvm::llvm_shutdown_obj llvmShutdownObj;
66
Nicolas Capens7d6b5912020-04-28 15:57:57 -040067// This has to be a raw pointer because glibc 2.17 doesn't support __cxa_thread_atexit_impl
68// for destructing objects at exit. See crbug.com/1074222
69thread_local rr::JITBuilder *jit = nullptr;
Nicolas Capens41a73022020-01-30 00:30:14 -050070
Nicolas Capens157ba262019-12-10 17:49:14 -050071llvm::Value *lowerPAVG(llvm::Value *x, llvm::Value *y)
72{
73 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
Logan Chien0eedc8c2018-08-21 09:34:28 +080074
Nicolas Capens157ba262019-12-10 17:49:14 -050075 llvm::VectorType *extTy =
Ben Clayton713b8d32019-12-17 20:37:56 +000076 llvm::VectorType::getExtendedElementVectorType(ty);
Nicolas Capens157ba262019-12-10 17:49:14 -050077 x = jit->builder->CreateZExt(x, extTy);
78 y = jit->builder->CreateZExt(y, extTy);
Logan Chien0eedc8c2018-08-21 09:34:28 +080079
Nicolas Capens157ba262019-12-10 17:49:14 -050080 // (x + y + 1) >> 1
81 llvm::Constant *one = llvm::ConstantInt::get(extTy, 1);
82 llvm::Value *res = jit->builder->CreateAdd(x, y);
83 res = jit->builder->CreateAdd(res, one);
84 res = jit->builder->CreateLShr(res, one);
85 return jit->builder->CreateTrunc(res, ty);
86}
Logan Chien0eedc8c2018-08-21 09:34:28 +080087
Nicolas Capens157ba262019-12-10 17:49:14 -050088llvm::Value *lowerPMINMAX(llvm::Value *x, llvm::Value *y,
89 llvm::ICmpInst::Predicate pred)
90{
91 return jit->builder->CreateSelect(jit->builder->CreateICmp(pred, x, y), x, y);
92}
Logan Chien0eedc8c2018-08-21 09:34:28 +080093
Nicolas Capens157ba262019-12-10 17:49:14 -050094llvm::Value *lowerPCMP(llvm::ICmpInst::Predicate pred, llvm::Value *x,
95 llvm::Value *y, llvm::Type *dstTy)
96{
97 return jit->builder->CreateSExt(jit->builder->CreateICmp(pred, x, y), dstTy, "");
98}
Logan Chien0eedc8c2018-08-21 09:34:28 +080099
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500100[[maybe_unused]] llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y,
101 llvm::FCmpInst::Predicate pred)
Nicolas Capens157ba262019-12-10 17:49:14 -0500102{
103 return jit->builder->CreateSelect(jit->builder->CreateFCmp(pred, x, y), x, y);
104}
105
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500106[[maybe_unused]] llvm::Value *lowerRound(llvm::Value *x)
Nicolas Capens157ba262019-12-10 17:49:14 -0500107{
108 llvm::Function *nearbyint = llvm::Intrinsic::getDeclaration(
Ben Clayton713b8d32019-12-17 20:37:56 +0000109 jit->module.get(), llvm::Intrinsic::nearbyint, { x->getType() });
Nicolas Capens0ffac8f2020-07-22 21:51:39 -0400110 return jit->builder->CreateCall(nearbyint, { x });
Nicolas Capens157ba262019-12-10 17:49:14 -0500111}
112
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500113[[maybe_unused]] llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty)
Nicolas Capens157ba262019-12-10 17:49:14 -0500114{
115 return jit->builder->CreateFPToSI(lowerRound(x), ty);
116}
117
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500118[[maybe_unused]] llvm::Value *lowerFloor(llvm::Value *x)
Nicolas Capens157ba262019-12-10 17:49:14 -0500119{
120 llvm::Function *floor = llvm::Intrinsic::getDeclaration(
Ben Clayton713b8d32019-12-17 20:37:56 +0000121 jit->module.get(), llvm::Intrinsic::floor, { x->getType() });
Nicolas Capens0ffac8f2020-07-22 21:51:39 -0400122 return jit->builder->CreateCall(floor, { x });
Nicolas Capens157ba262019-12-10 17:49:14 -0500123}
124
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500125[[maybe_unused]] llvm::Value *lowerTrunc(llvm::Value *x)
Nicolas Capens157ba262019-12-10 17:49:14 -0500126{
127 llvm::Function *trunc = llvm::Intrinsic::getDeclaration(
Ben Clayton713b8d32019-12-17 20:37:56 +0000128 jit->module.get(), llvm::Intrinsic::trunc, { x->getType() });
Nicolas Capens0ffac8f2020-07-22 21:51:39 -0400129 return jit->builder->CreateCall(trunc, { x });
Nicolas Capens157ba262019-12-10 17:49:14 -0500130}
131
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500132[[maybe_unused]] llvm::Value *lowerSQRT(llvm::Value *x)
Nicolas Capens157ba262019-12-10 17:49:14 -0500133{
134 llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(
Ben Clayton713b8d32019-12-17 20:37:56 +0000135 jit->module.get(), llvm::Intrinsic::sqrt, { x->getType() });
Nicolas Capens0ffac8f2020-07-22 21:51:39 -0400136 return jit->builder->CreateCall(sqrt, { x });
Nicolas Capens157ba262019-12-10 17:49:14 -0500137}
138
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500139[[maybe_unused]] llvm::Value *lowerRCP(llvm::Value *x)
Nicolas Capens157ba262019-12-10 17:49:14 -0500140{
141 llvm::Type *ty = x->getType();
142 llvm::Constant *one;
Googler4d22b2c2021-02-01 12:10:01 +0000143 if(llvm::FixedVectorType *vectorTy = llvm::dyn_cast<llvm::FixedVectorType>(ty))
Logan Chien83fc07a2018-09-26 22:14:00 +0800144 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500145 one = llvm::ConstantVector::getSplat(
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500146#if LLVM_VERSION_MAJOR >= 11
Nicolas Capens85ea7332021-01-29 16:54:36 -0500147 vectorTy->getElementCount(),
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500148#else
Ben Clayton713b8d32019-12-17 20:37:56 +0000149 vectorTy->getNumElements(),
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500150#endif
Ben Clayton713b8d32019-12-17 20:37:56 +0000151 llvm::ConstantFP::get(vectorTy->getElementType(), 1));
Nicolas Capens157ba262019-12-10 17:49:14 -0500152 }
153 else
154 {
155 one = llvm::ConstantFP::get(ty, 1);
156 }
157 return jit->builder->CreateFDiv(one, x);
158}
159
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500160[[maybe_unused]] llvm::Value *lowerRSQRT(llvm::Value *x)
Nicolas Capens157ba262019-12-10 17:49:14 -0500161{
162 return lowerRCP(lowerSQRT(x));
163}
164
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500165[[maybe_unused]] llvm::Value *lowerVectorShl(llvm::Value *x, uint64_t scalarY)
Nicolas Capens157ba262019-12-10 17:49:14 -0500166{
Googler4d22b2c2021-02-01 12:10:01 +0000167 llvm::FixedVectorType *ty = llvm::cast<llvm::FixedVectorType>(x->getType());
Nicolas Capens157ba262019-12-10 17:49:14 -0500168 llvm::Value *y = llvm::ConstantVector::getSplat(
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500169#if LLVM_VERSION_MAJOR >= 11
Nicolas Capens85ea7332021-01-29 16:54:36 -0500170 ty->getElementCount(),
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500171#else
Ben Clayton713b8d32019-12-17 20:37:56 +0000172 ty->getNumElements(),
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500173#endif
Ben Clayton713b8d32019-12-17 20:37:56 +0000174 llvm::ConstantInt::get(ty->getElementType(), scalarY));
Nicolas Capens157ba262019-12-10 17:49:14 -0500175 return jit->builder->CreateShl(x, y);
176}
177
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500178[[maybe_unused]] llvm::Value *lowerVectorAShr(llvm::Value *x, uint64_t scalarY)
Nicolas Capens157ba262019-12-10 17:49:14 -0500179{
Googler4d22b2c2021-02-01 12:10:01 +0000180 llvm::FixedVectorType *ty = llvm::cast<llvm::FixedVectorType>(x->getType());
Nicolas Capens157ba262019-12-10 17:49:14 -0500181 llvm::Value *y = llvm::ConstantVector::getSplat(
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500182#if LLVM_VERSION_MAJOR >= 11
Nicolas Capens85ea7332021-01-29 16:54:36 -0500183 ty->getElementCount(),
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500184#else
Ben Clayton713b8d32019-12-17 20:37:56 +0000185 ty->getNumElements(),
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500186#endif
Ben Clayton713b8d32019-12-17 20:37:56 +0000187 llvm::ConstantInt::get(ty->getElementType(), scalarY));
Nicolas Capens157ba262019-12-10 17:49:14 -0500188 return jit->builder->CreateAShr(x, y);
189}
190
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500191[[maybe_unused]] llvm::Value *lowerVectorLShr(llvm::Value *x, uint64_t scalarY)
Nicolas Capens157ba262019-12-10 17:49:14 -0500192{
Googler4d22b2c2021-02-01 12:10:01 +0000193 llvm::FixedVectorType *ty = llvm::cast<llvm::FixedVectorType>(x->getType());
Nicolas Capens157ba262019-12-10 17:49:14 -0500194 llvm::Value *y = llvm::ConstantVector::getSplat(
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500195#if LLVM_VERSION_MAJOR >= 11
Nicolas Capens85ea7332021-01-29 16:54:36 -0500196 ty->getElementCount(),
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500197#else
Ben Clayton713b8d32019-12-17 20:37:56 +0000198 ty->getNumElements(),
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500199#endif
Ben Clayton713b8d32019-12-17 20:37:56 +0000200 llvm::ConstantInt::get(ty->getElementType(), scalarY));
Nicolas Capens157ba262019-12-10 17:49:14 -0500201 return jit->builder->CreateLShr(x, y);
202}
203
Nicolas Capensbc0f6632022-02-10 11:20:37 -0500204llvm::Value *lowerShuffleVector(llvm::Value *v1, llvm::Value *v2, llvm::ArrayRef<int> select)
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500205{
Nicolas Capensbc0f6632022-02-10 11:20:37 -0500206 int size = select.size();
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500207 const int maxSize = 16;
208 llvm::Constant *swizzle[maxSize];
209 ASSERT(size <= maxSize);
210
211 for(int i = 0; i < size; i++)
212 {
213 swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*jit->context), select[i]);
214 }
215
216 llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant *>(swizzle, size));
217
218 return jit->builder->CreateShuffleVector(v1, v2, shuffle);
219}
220
221[[maybe_unused]] llvm::Value *lowerMulAdd(llvm::Value *x, llvm::Value *y)
Nicolas Capens157ba262019-12-10 17:49:14 -0500222{
Googler4d22b2c2021-02-01 12:10:01 +0000223 llvm::FixedVectorType *ty = llvm::cast<llvm::FixedVectorType>(x->getType());
Nicolas Capens157ba262019-12-10 17:49:14 -0500224 llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty);
225
226 llvm::Value *extX = jit->builder->CreateSExt(x, extTy);
227 llvm::Value *extY = jit->builder->CreateSExt(y, extTy);
228 llvm::Value *mult = jit->builder->CreateMul(extX, extY);
229
230 llvm::Value *undef = llvm::UndefValue::get(extTy);
231
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +0100232 llvm::SmallVector<int, 16> evenIdx;
233 llvm::SmallVector<int, 16> oddIdx;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500234 for(uint64_t i = 0, n = ty->getNumElements(); i < n; i += 2)
Nicolas Capens157ba262019-12-10 17:49:14 -0500235 {
236 evenIdx.push_back(i);
237 oddIdx.push_back(i + 1);
Logan Chien83fc07a2018-09-26 22:14:00 +0800238 }
239
Nicolas Capensbc0f6632022-02-10 11:20:37 -0500240 llvm::Value *lhs = lowerShuffleVector(mult, undef, evenIdx);
241 llvm::Value *rhs = lowerShuffleVector(mult, undef, oddIdx);
Nicolas Capens157ba262019-12-10 17:49:14 -0500242 return jit->builder->CreateAdd(lhs, rhs);
243}
244
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500245[[maybe_unused]] llvm::Value *lowerPack(llvm::Value *x, llvm::Value *y, bool isSigned)
Nicolas Capens157ba262019-12-10 17:49:14 -0500246{
Googler4d22b2c2021-02-01 12:10:01 +0000247 llvm::FixedVectorType *srcTy = llvm::cast<llvm::FixedVectorType>(x->getType());
Nicolas Capens157ba262019-12-10 17:49:14 -0500248 llvm::VectorType *dstTy = llvm::VectorType::getTruncatedElementVectorType(srcTy);
249
250 llvm::IntegerType *dstElemTy =
Ben Clayton713b8d32019-12-17 20:37:56 +0000251 llvm::cast<llvm::IntegerType>(dstTy->getElementType());
Nicolas Capens157ba262019-12-10 17:49:14 -0500252
253 uint64_t truncNumBits = dstElemTy->getIntegerBitWidth();
254 ASSERT_MSG(truncNumBits < 64, "shift 64 must be handled separately. truncNumBits: %d", int(truncNumBits));
255 llvm::Constant *max, *min;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500256 if(isSigned)
Logan Chien2faa24a2018-09-26 19:59:32 +0800257 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500258 max = llvm::ConstantInt::get(srcTy, (1LL << (truncNumBits - 1)) - 1, true);
259 min = llvm::ConstantInt::get(srcTy, (-1LL << (truncNumBits - 1)), true);
260 }
261 else
262 {
263 max = llvm::ConstantInt::get(srcTy, (1ULL << truncNumBits) - 1, false);
264 min = llvm::ConstantInt::get(srcTy, 0, false);
Logan Chien2faa24a2018-09-26 19:59:32 +0800265 }
266
Nicolas Capens157ba262019-12-10 17:49:14 -0500267 x = lowerPMINMAX(x, min, llvm::ICmpInst::ICMP_SGT);
268 x = lowerPMINMAX(x, max, llvm::ICmpInst::ICMP_SLT);
269 y = lowerPMINMAX(y, min, llvm::ICmpInst::ICMP_SGT);
270 y = lowerPMINMAX(y, max, llvm::ICmpInst::ICMP_SLT);
271
272 x = jit->builder->CreateTrunc(x, dstTy);
273 y = jit->builder->CreateTrunc(y, dstTy);
274
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +0100275 llvm::SmallVector<int, 16> index(srcTy->getNumElements() * 2);
Nicolas Capens157ba262019-12-10 17:49:14 -0500276 std::iota(index.begin(), index.end(), 0);
277
Nicolas Capensbc0f6632022-02-10 11:20:37 -0500278 return lowerShuffleVector(x, y, index);
Nicolas Capens157ba262019-12-10 17:49:14 -0500279}
280
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500281[[maybe_unused]] llvm::Value *lowerSignMask(llvm::Value *x, llvm::Type *retTy)
Nicolas Capens157ba262019-12-10 17:49:14 -0500282{
Googler4d22b2c2021-02-01 12:10:01 +0000283 llvm::FixedVectorType *ty = llvm::cast<llvm::FixedVectorType>(x->getType());
Nicolas Capens157ba262019-12-10 17:49:14 -0500284 llvm::Constant *zero = llvm::ConstantInt::get(ty, 0);
285 llvm::Value *cmp = jit->builder->CreateICmpSLT(x, zero);
286
287 llvm::Value *ret = jit->builder->CreateZExt(
Ben Clayton713b8d32019-12-17 20:37:56 +0000288 jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500289 for(uint64_t i = 1, n = ty->getNumElements(); i < n; ++i)
Logan Chien40a60052018-09-26 19:03:53 +0800290 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500291 llvm::Value *elem = jit->builder->CreateZExt(
Ben Clayton713b8d32019-12-17 20:37:56 +0000292 jit->builder->CreateExtractElement(cmp, i), retTy);
Nicolas Capens157ba262019-12-10 17:49:14 -0500293 ret = jit->builder->CreateOr(ret, jit->builder->CreateShl(elem, i));
Logan Chien40a60052018-09-26 19:03:53 +0800294 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500295 return ret;
296}
Logan Chien40a60052018-09-26 19:03:53 +0800297
Nicolas Capens2176cdf2022-02-08 16:17:25 -0500298[[maybe_unused]] llvm::Value *lowerFPSignMask(llvm::Value *x, llvm::Type *retTy)
Nicolas Capens157ba262019-12-10 17:49:14 -0500299{
Googler4d22b2c2021-02-01 12:10:01 +0000300 llvm::FixedVectorType *ty = llvm::cast<llvm::FixedVectorType>(x->getType());
Nicolas Capens157ba262019-12-10 17:49:14 -0500301 llvm::Constant *zero = llvm::ConstantFP::get(ty, 0);
302 llvm::Value *cmp = jit->builder->CreateFCmpULT(x, zero);
303
304 llvm::Value *ret = jit->builder->CreateZExt(
Ben Clayton713b8d32019-12-17 20:37:56 +0000305 jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500306 for(uint64_t i = 1, n = ty->getNumElements(); i < n; ++i)
Logan Chien8c5ca8d2018-09-27 21:05:53 +0800307 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500308 llvm::Value *elem = jit->builder->CreateZExt(
Ben Clayton713b8d32019-12-17 20:37:56 +0000309 jit->builder->CreateExtractElement(cmp, i), retTy);
Nicolas Capens157ba262019-12-10 17:49:14 -0500310 ret = jit->builder->CreateOr(ret, jit->builder->CreateShl(elem, i));
Logan Chien8c5ca8d2018-09-27 21:05:53 +0800311 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500312 return ret;
313}
Chris Forbese86b6dc2019-03-01 09:08:47 -0800314
Nicolas Capens157ba262019-12-10 17:49:14 -0500315llvm::Value *lowerPUADDSAT(llvm::Value *x, llvm::Value *y)
316{
Ben Clayton713b8d32019-12-17 20:37:56 +0000317 return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::uadd_sat, x, y);
Nicolas Capens157ba262019-12-10 17:49:14 -0500318}
Preston Jacksonee1af662019-09-27 15:26:26 -0600319
Nicolas Capens157ba262019-12-10 17:49:14 -0500320llvm::Value *lowerPSADDSAT(llvm::Value *x, llvm::Value *y)
321{
Ben Clayton713b8d32019-12-17 20:37:56 +0000322 return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::sadd_sat, x, y);
Nicolas Capens157ba262019-12-10 17:49:14 -0500323}
Preston Jacksonee1af662019-09-27 15:26:26 -0600324
Nicolas Capens157ba262019-12-10 17:49:14 -0500325llvm::Value *lowerPUSUBSAT(llvm::Value *x, llvm::Value *y)
326{
Ben Clayton713b8d32019-12-17 20:37:56 +0000327 return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::usub_sat, x, y);
Nicolas Capens157ba262019-12-10 17:49:14 -0500328}
Preston Jacksonee1af662019-09-27 15:26:26 -0600329
Nicolas Capens157ba262019-12-10 17:49:14 -0500330llvm::Value *lowerPSSUBSAT(llvm::Value *x, llvm::Value *y)
331{
Ben Clayton713b8d32019-12-17 20:37:56 +0000332 return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::ssub_sat, x, y);
Nicolas Capens157ba262019-12-10 17:49:14 -0500333}
Preston Jacksonee1af662019-09-27 15:26:26 -0600334
Nicolas Capens157ba262019-12-10 17:49:14 -0500335llvm::Value *lowerMulHigh(llvm::Value *x, llvm::Value *y, bool sext)
336{
337 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
338 llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty);
339
340 llvm::Value *extX, *extY;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500341 if(sext)
Chris Forbese86b6dc2019-03-01 09:08:47 -0800342 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500343 extX = jit->builder->CreateSExt(x, extTy);
344 extY = jit->builder->CreateSExt(y, extTy);
345 }
346 else
347 {
348 extX = jit->builder->CreateZExt(x, extTy);
349 extY = jit->builder->CreateZExt(y, extTy);
Chris Forbese86b6dc2019-03-01 09:08:47 -0800350 }
Antonio Maioranoe6ab4702019-11-29 11:26:30 -0500351
Nicolas Capens157ba262019-12-10 17:49:14 -0500352 llvm::Value *mult = jit->builder->CreateMul(extX, extY);
353
354 llvm::IntegerType *intTy = llvm::cast<llvm::IntegerType>(ty->getElementType());
355 llvm::Value *mulh = jit->builder->CreateAShr(mult, intTy->getBitWidth());
356 return jit->builder->CreateTrunc(mulh, ty);
357}
358
Ben Clayton713b8d32019-12-17 20:37:56 +0000359} // namespace
Nicolas Capens157ba262019-12-10 17:49:14 -0500360
361namespace rr {
362
Nicolas Capensd1116fa2022-06-29 10:39:18 -0400363const int SIMD::Width = 4;
Nicolas Capens3b0ad202022-06-02 15:02:31 -0400364
Nicolas Capens70505b42022-01-31 22:29:48 -0500365std::string Caps::backendName()
Antonio Maioranoab210f92019-12-13 16:26:24 -0500366{
367 return std::string("LLVM ") + LLVM_VERSION_STRING;
368}
369
Nicolas Capens70505b42022-01-31 22:29:48 -0500370bool Caps::coroutinesSupported()
371{
372 return true;
373}
374
375bool Caps::fmaIsFast()
376{
377 static bool AVX2 = CPUID::supportsAVX2(); // Also checks for FMA support
378
379 // If x86 FMA instructions are supported, assume LLVM will emit them instead of making calls to std::fma().
380 return AVX2;
381}
Nicolas Capens157ba262019-12-10 17:49:14 -0500382
Nicolas Capens157ba262019-12-10 17:49:14 -0500383// The abstract Type* types are implemented as LLVM types, except that
384// 64-bit vectors are emulated using 128-bit ones to avoid use of MMX in x86
385// and VFP in ARM, and eliminate the overhead of converting them to explicit
386// 128-bit ones. LLVM types are pointers, so we can represent emulated types
387// as abstract pointers with small enum values.
388enum InternalType : uintptr_t
389{
390 // Emulated types:
391 Type_v2i32,
392 Type_v4i16,
393 Type_v2i16,
394 Type_v8i8,
395 Type_v4i8,
396 Type_v2f32,
397 EmulatedTypeCount,
398 // Returned by asInternalType() to indicate that the abstract Type*
399 // should be interpreted as LLVM type pointer:
400 Type_LLVM
401};
402
403inline InternalType asInternalType(Type *type)
404{
405 InternalType t = static_cast<InternalType>(reinterpret_cast<uintptr_t>(type));
406 return (t < EmulatedTypeCount) ? t : Type_LLVM;
407}
408
409llvm::Type *T(Type *t)
410{
411 // Use 128-bit vectors to implement logically shorter ones.
412 switch(asInternalType(t))
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400413 {
Nicolas Capens112faf42019-12-13 17:32:26 -0500414 case Type_v2i32: return T(Int4::type());
415 case Type_v4i16: return T(Short8::type());
416 case Type_v2i16: return T(Short8::type());
417 case Type_v8i8: return T(Byte16::type());
418 case Type_v4i8: return T(Byte16::type());
419 case Type_v2f32: return T(Float4::type());
420 case Type_LLVM: return reinterpret_cast<llvm::Type *>(t);
421 default:
422 UNREACHABLE("asInternalType(t): %d", int(asInternalType(t)));
423 return nullptr;
Nicolas Capens157ba262019-12-10 17:49:14 -0500424 }
425}
426
427Type *T(InternalType t)
428{
Ben Clayton713b8d32019-12-17 20:37:56 +0000429 return reinterpret_cast<Type *>(t);
Nicolas Capens157ba262019-12-10 17:49:14 -0500430}
431
Antonio Maiorano5ba2a5b2020-01-17 15:29:37 -0500432inline const std::vector<llvm::Type *> &T(const std::vector<Type *> &t)
Nicolas Capens157ba262019-12-10 17:49:14 -0500433{
Antonio Maiorano5ba2a5b2020-01-17 15:29:37 -0500434 return reinterpret_cast<const std::vector<llvm::Type *> &>(t);
Nicolas Capens157ba262019-12-10 17:49:14 -0500435}
436
437inline llvm::BasicBlock *B(BasicBlock *t)
438{
Ben Clayton713b8d32019-12-17 20:37:56 +0000439 return reinterpret_cast<llvm::BasicBlock *>(t);
Nicolas Capens157ba262019-12-10 17:49:14 -0500440}
441
442inline BasicBlock *B(llvm::BasicBlock *t)
443{
Ben Clayton713b8d32019-12-17 20:37:56 +0000444 return reinterpret_cast<BasicBlock *>(t);
Nicolas Capens157ba262019-12-10 17:49:14 -0500445}
446
447static size_t typeSize(Type *type)
448{
449 switch(asInternalType(type))
450 {
Nicolas Capens112faf42019-12-13 17:32:26 -0500451 case Type_v2i32: return 8;
452 case Type_v4i16: return 8;
453 case Type_v2i16: return 4;
454 case Type_v8i8: return 8;
455 case Type_v4i8: return 4;
456 case Type_v2f32: return 8;
457 case Type_LLVM:
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400458 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500459 llvm::Type *t = T(type);
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400460
Nicolas Capens157ba262019-12-10 17:49:14 -0500461 if(t->isPointerTy())
Nicolas Capens01a97962017-07-28 17:30:51 -0400462 {
Ben Clayton713b8d32019-12-17 20:37:56 +0000463 return sizeof(void *);
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500464 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500465
466 // At this point we should only have LLVM 'primitive' types.
467 unsigned int bits = t->getPrimitiveSizeInBits();
468 ASSERT_MSG(bits != 0, "bits: %d", int(bits));
469
470 // TODO(capn): Booleans are 1 bit integers in LLVM's SSA type system,
471 // but are typically stored as one byte. The DataLayout structure should
472 // be used here and many other places if this assumption fails.
473 return (bits + 7) / 8;
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500474 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500475 break;
Nicolas Capens112faf42019-12-13 17:32:26 -0500476 default:
477 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
478 return 0;
Nicolas Capens01a97962017-07-28 17:30:51 -0400479 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500480}
Nicolas Capens01a97962017-07-28 17:30:51 -0400481
Nicolas Capens00ecdf92020-10-30 21:13:40 -0400482static llvm::Function *createFunction(const char *name, llvm::Type *retTy, const std::vector<llvm::Type *> &params)
Nicolas Capens157ba262019-12-10 17:49:14 -0500483{
484 llvm::FunctionType *functionType = llvm::FunctionType::get(retTy, params, false);
485 auto func = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, name, jit->module.get());
Nicolas Capens58d987a2021-01-07 23:24:25 -0500486
487 func->setLinkage(llvm::GlobalValue::ExternalLinkage);
Nicolas Capens157ba262019-12-10 17:49:14 -0500488 func->setDoesNotThrow();
489 func->setCallingConv(llvm::CallingConv::C);
Nicolas Capens58d987a2021-01-07 23:24:25 -0500490
Nicolas Capens4804ac82020-11-02 22:06:55 -0500491 if(__has_feature(memory_sanitizer))
492 {
493 func->addFnAttr(llvm::Attribute::SanitizeMemory);
Nicolas Capensf13461d2022-04-20 15:33:24 -0400494
495 // Assume that when using recent versions of LLVM, MemorySanitizer enabled builds
496 // use -fsanitize-memory-param-retval, which makes the caller not update the shadow
497 // of function parameters. NoUndef skips generating checks for uninitialized values.
498#if LLVM_VERSION_MAJOR >= 13
499 for(unsigned int i = 0; i < params.size(); i++)
500 {
501 func->addParamAttr(i, llvm::Attribute::NoUndef);
502 }
503#endif
Nicolas Capens4804ac82020-11-02 22:06:55 -0500504 }
505
Nicolas Capensa07b3fb2022-07-28 04:18:58 -0400506 if(__has_feature(address_sanitizer))
507 {
508 func->addFnAttr(llvm::Attribute::SanitizeAddress);
509 }
510
Nicolas Capensc13f4b12022-02-25 11:08:07 -0500511 func->addFnAttr("warn-stack-size", "524288"); // Warn when a function uses more than 512 KiB of stack memory
512
Nicolas Capens157ba262019-12-10 17:49:14 -0500513 return func;
514}
515
516Nucleus::Nucleus()
517{
Nicolas Capens00c30ce2020-10-29 09:17:25 -0400518#if !__has_feature(memory_sanitizer)
519 // thread_local variables in shared libraries are initialized at load-time,
520 // but this is not observed by MemorySanitizer if the loader itself was not
Nicolas Capensaf907702021-05-14 11:10:49 -0400521 // instrumented, leading to false-positive uninitialized variable errors.
Nicolas Capens157ba262019-12-10 17:49:14 -0500522 ASSERT(jit == nullptr);
Nicolas Capens7d6b5912020-04-28 15:57:57 -0400523 ASSERT(Variable::unmaterializedVariables == nullptr);
Nicolas Capens00c30ce2020-10-29 09:17:25 -0400524#endif
525
Nicolas Capens79d4c6c2022-04-22 17:20:26 -0400526 jit = new JITBuilder();
Nicolas Capensaf907702021-05-14 11:10:49 -0400527 Variable::unmaterializedVariables = new Variable::UnmaterializedVariables();
Nicolas Capens157ba262019-12-10 17:49:14 -0500528}
529
530Nucleus::~Nucleus()
531{
Nicolas Capens7d6b5912020-04-28 15:57:57 -0400532 delete Variable::unmaterializedVariables;
533 Variable::unmaterializedVariables = nullptr;
534
535 delete jit;
536 jit = nullptr;
Nicolas Capens157ba262019-12-10 17:49:14 -0500537}
538
Nicolas Capens79d4c6c2022-04-22 17:20:26 -0400539std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name)
Nicolas Capens157ba262019-12-10 17:49:14 -0500540{
Nicolas Capens98d2cab2020-04-29 13:45:55 -0400541 if(jit->builder->GetInsertBlock()->empty() || !jit->builder->GetInsertBlock()->back().isTerminator())
542 {
543 llvm::Type *type = jit->function->getReturnType();
544
545 if(type->isVoidTy())
546 {
547 createRetVoid();
548 }
549 else
550 {
551 createRet(V(llvm::UndefValue::get(type)));
552 }
553 }
554
Nicolas Capens1217ab92020-02-20 01:58:11 -0500555 std::shared_ptr<Routine> routine;
Nicolas Capens157ba262019-12-10 17:49:14 -0500556
Nicolas Capens98d2cab2020-04-29 13:45:55 -0400557 auto acquire = [&](rr::JITBuilder *jit) {
Nicolas Capens79d4c6c2022-04-22 17:20:26 -0400558 // ::jit is thread-local, so when this is executed on a separate thread (see JIT_IN_SEPARATE_THREAD)
559 // it needs to only use the jit variable passed in as an argument.
Nicolas Capens157ba262019-12-10 17:49:14 -0500560
Ben Claytonac07ed82019-03-26 14:17:41 +0000561#ifdef ENABLE_RR_DEBUG_INFO
Nicolas Capens1217ab92020-02-20 01:58:11 -0500562 if(jit->debugInfo != nullptr)
563 {
564 jit->debugInfo->Finalize();
565 }
Ben Clayton713b8d32019-12-17 20:37:56 +0000566#endif // ENABLE_RR_DEBUG_INFO
Ben Claytonac07ed82019-03-26 14:17:41 +0000567
Nicolas Capens1217ab92020-02-20 01:58:11 -0500568 if(false)
569 {
570 std::error_code error;
571 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error);
572 jit->module->print(file, 0);
573 }
John Bauman89401822014-05-06 15:04:28 -0400574
Nicolas Capens79d4c6c2022-04-22 17:20:26 -0400575 jit->runPasses();
John Bauman89401822014-05-06 15:04:28 -0400576
Nicolas Capens1217ab92020-02-20 01:58:11 -0500577 if(false)
578 {
579 std::error_code error;
580 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error);
581 jit->module->print(file, 0);
582 }
John Bauman89401822014-05-06 15:04:28 -0400583
Nicolas Capens79d4c6c2022-04-22 17:20:26 -0400584 routine = jit->acquireRoutine(name, &jit->function, 1);
Nicolas Capens1217ab92020-02-20 01:58:11 -0500585 };
586
587#ifdef JIT_IN_SEPARATE_THREAD
588 // Perform optimizations and codegen in a separate thread to avoid stack overflow.
589 // FIXME(b/149829034): This is not a long-term solution. Reactor has no control
590 // over the threading and stack sizes of its users, so this should be addressed
591 // at a higher level instead.
Nicolas Capens98d2cab2020-04-29 13:45:55 -0400592 std::thread thread(acquire, jit);
Nicolas Capens1217ab92020-02-20 01:58:11 -0500593 thread.join();
594#else
Nicolas Capens98d2cab2020-04-29 13:45:55 -0400595 acquire(jit);
Nicolas Capens1217ab92020-02-20 01:58:11 -0500596#endif
Nicolas Capens157ba262019-12-10 17:49:14 -0500597
598 return routine;
599}
600
601Value *Nucleus::allocateStackVariable(Type *type, int arraySize)
602{
603 // Need to allocate it in the entry block for mem2reg to work
604 llvm::BasicBlock &entryBlock = jit->function->getEntryBlock();
605
606 llvm::Instruction *declaration;
607
Antonio Maioranocc5cda02020-05-19 11:08:16 -0400608#if LLVM_VERSION_MAJOR >= 11
609 auto align = jit->module->getDataLayout().getPrefTypeAlign(T(type));
Antonio Maioranocc5cda02020-05-19 11:08:16 -0400610#else
Antonio Maiorano6a6ae442020-07-20 14:11:48 -0400611 auto align = llvm::MaybeAlign(jit->module->getDataLayout().getPrefTypeAlignment(T(type)));
Antonio Maioranocc5cda02020-05-19 11:08:16 -0400612#endif
613
Nicolas Capens157ba262019-12-10 17:49:14 -0500614 if(arraySize)
John Bauman89401822014-05-06 15:04:28 -0400615 {
Nicolas Capens19f01492020-11-02 22:04:54 -0500616 Value *size = (sizeof(size_t) == 8) ? Nucleus::createConstantLong(arraySize) : Nucleus::createConstantInt(arraySize);
617 declaration = new llvm::AllocaInst(T(type), 0, V(size), align);
Nicolas Capens157ba262019-12-10 17:49:14 -0500618 }
619 else
620 {
Antonio Maioranocc5cda02020-05-19 11:08:16 -0400621 declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value *)nullptr, align);
John Bauman89401822014-05-06 15:04:28 -0400622 }
623
Nicolas Capensd0aa9ad2022-12-22 01:46:20 -0500624#if LLVM_VERSION_MAJOR >= 16
625 declaration->insertInto(&entryBlock, entryBlock.begin());
626#else
Nicolas Capens157ba262019-12-10 17:49:14 -0500627 entryBlock.getInstList().push_front(declaration);
Nicolas Capensd0aa9ad2022-12-22 01:46:20 -0500628#endif
Nicolas Capens157ba262019-12-10 17:49:14 -0500629
Nicolas Capensd0703092022-05-26 12:58:20 -0400630 if(getPragmaState(InitializeLocalVariables))
631 {
632 llvm::Type *i8PtrTy = llvm::Type::getInt8Ty(*jit->context)->getPointerTo();
633 llvm::Type *i32Ty = llvm::Type::getInt32Ty(*jit->context);
634 llvm::Function *memset = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::memset, { i8PtrTy, i32Ty });
635
636 jit->builder->CreateCall(memset, { jit->builder->CreatePointerCast(declaration, i8PtrTy),
637 V(Nucleus::createConstantByte((unsigned char)0)),
638 V(Nucleus::createConstantInt((int)typeSize(type) * (arraySize ? arraySize : 1))),
639 V(Nucleus::createConstantBool(false)) });
640 }
641
Nicolas Capens157ba262019-12-10 17:49:14 -0500642 return V(declaration);
643}
644
645BasicBlock *Nucleus::createBasicBlock()
646{
Nicolas Capens567e5602021-01-07 23:18:56 -0500647 return B(llvm::BasicBlock::Create(*jit->context, "", jit->function));
Nicolas Capens157ba262019-12-10 17:49:14 -0500648}
649
650BasicBlock *Nucleus::getInsertBlock()
651{
652 return B(jit->builder->GetInsertBlock());
653}
654
655void Nucleus::setInsertBlock(BasicBlock *basicBlock)
656{
Nicolas Capens7c296ec2021-02-18 14:10:26 -0500657 // assert(jit->builder->GetInsertBlock()->back().isTerminator());
Nicolas Capens157ba262019-12-10 17:49:14 -0500658
659 jit->builder->SetInsertPoint(B(basicBlock));
660}
661
Antonio Maiorano5ba2a5b2020-01-17 15:29:37 -0500662void Nucleus::createFunction(Type *ReturnType, const std::vector<Type *> &Params)
Nicolas Capens157ba262019-12-10 17:49:14 -0500663{
664 jit->function = rr::createFunction("", T(ReturnType), T(Params));
665
666#ifdef ENABLE_RR_DEBUG_INFO
Nicolas Capens567e5602021-01-07 23:18:56 -0500667 jit->debugInfo = std::make_unique<DebugInfo>(jit->builder.get(), jit->context.get(), jit->module.get(), jit->function);
Ben Clayton713b8d32019-12-17 20:37:56 +0000668#endif // ENABLE_RR_DEBUG_INFO
Nicolas Capens157ba262019-12-10 17:49:14 -0500669
Nicolas Capens567e5602021-01-07 23:18:56 -0500670 jit->builder->SetInsertPoint(llvm::BasicBlock::Create(*jit->context, "", jit->function));
Nicolas Capens157ba262019-12-10 17:49:14 -0500671}
672
673Value *Nucleus::getArgument(unsigned int index)
674{
675 llvm::Function::arg_iterator args = jit->function->arg_begin();
676
677 while(index)
John Bauman89401822014-05-06 15:04:28 -0400678 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500679 args++;
680 index--;
John Bauman89401822014-05-06 15:04:28 -0400681 }
682
Nicolas Capens157ba262019-12-10 17:49:14 -0500683 return V(&*args);
684}
685
686void Nucleus::createRetVoid()
687{
688 RR_DEBUG_INFO_UPDATE_LOC();
689
Nicolas Capens519cf222020-05-08 15:27:19 -0400690 ASSERT_MSG(jit->function->getReturnType() == T(Void::type()), "Return type mismatch");
Nicolas Capens157ba262019-12-10 17:49:14 -0500691
692 // Code generated after this point is unreachable, so any variables
693 // being read can safely return an undefined value. We have to avoid
694 // materializing variables after the terminator ret instruction.
695 Variable::killUnmaterialized();
696
697 jit->builder->CreateRetVoid();
698}
699
700void Nucleus::createRet(Value *v)
701{
702 RR_DEBUG_INFO_UPDATE_LOC();
703
704 ASSERT_MSG(jit->function->getReturnType() == V(v)->getType(), "Return type mismatch");
705
706 // Code generated after this point is unreachable, so any variables
707 // being read can safely return an undefined value. We have to avoid
708 // materializing variables after the terminator ret instruction.
709 Variable::killUnmaterialized();
710
711 jit->builder->CreateRet(V(v));
712}
713
714void Nucleus::createBr(BasicBlock *dest)
715{
716 RR_DEBUG_INFO_UPDATE_LOC();
717 Variable::materializeAll();
718
719 jit->builder->CreateBr(B(dest));
720}
721
722void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse)
723{
724 RR_DEBUG_INFO_UPDATE_LOC();
725 Variable::materializeAll();
726 jit->builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse));
727}
728
729Value *Nucleus::createAdd(Value *lhs, Value *rhs)
730{
731 RR_DEBUG_INFO_UPDATE_LOC();
732 return V(jit->builder->CreateAdd(V(lhs), V(rhs)));
733}
734
735Value *Nucleus::createSub(Value *lhs, Value *rhs)
736{
737 RR_DEBUG_INFO_UPDATE_LOC();
738 return V(jit->builder->CreateSub(V(lhs), V(rhs)));
739}
740
741Value *Nucleus::createMul(Value *lhs, Value *rhs)
742{
743 RR_DEBUG_INFO_UPDATE_LOC();
744 return V(jit->builder->CreateMul(V(lhs), V(rhs)));
745}
746
747Value *Nucleus::createUDiv(Value *lhs, Value *rhs)
748{
749 RR_DEBUG_INFO_UPDATE_LOC();
750 return V(jit->builder->CreateUDiv(V(lhs), V(rhs)));
751}
752
753Value *Nucleus::createSDiv(Value *lhs, Value *rhs)
754{
755 RR_DEBUG_INFO_UPDATE_LOC();
756 return V(jit->builder->CreateSDiv(V(lhs), V(rhs)));
757}
758
759Value *Nucleus::createFAdd(Value *lhs, Value *rhs)
760{
761 RR_DEBUG_INFO_UPDATE_LOC();
762 return V(jit->builder->CreateFAdd(V(lhs), V(rhs)));
763}
764
765Value *Nucleus::createFSub(Value *lhs, Value *rhs)
766{
767 RR_DEBUG_INFO_UPDATE_LOC();
768 return V(jit->builder->CreateFSub(V(lhs), V(rhs)));
769}
770
771Value *Nucleus::createFMul(Value *lhs, Value *rhs)
772{
773 RR_DEBUG_INFO_UPDATE_LOC();
774 return V(jit->builder->CreateFMul(V(lhs), V(rhs)));
775}
776
777Value *Nucleus::createFDiv(Value *lhs, Value *rhs)
778{
779 RR_DEBUG_INFO_UPDATE_LOC();
780 return V(jit->builder->CreateFDiv(V(lhs), V(rhs)));
781}
782
783Value *Nucleus::createURem(Value *lhs, Value *rhs)
784{
785 RR_DEBUG_INFO_UPDATE_LOC();
786 return V(jit->builder->CreateURem(V(lhs), V(rhs)));
787}
788
789Value *Nucleus::createSRem(Value *lhs, Value *rhs)
790{
791 RR_DEBUG_INFO_UPDATE_LOC();
792 return V(jit->builder->CreateSRem(V(lhs), V(rhs)));
793}
794
795Value *Nucleus::createFRem(Value *lhs, Value *rhs)
796{
797 RR_DEBUG_INFO_UPDATE_LOC();
798 return V(jit->builder->CreateFRem(V(lhs), V(rhs)));
799}
800
Antonio Maiorano5ef91b82020-01-21 15:10:22 -0500801RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs)
802{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -0400803 return RValue<Float4>(Nucleus::createFRem(lhs.value(), rhs.value()));
Antonio Maiorano5ef91b82020-01-21 15:10:22 -0500804}
805
Nicolas Capens157ba262019-12-10 17:49:14 -0500806Value *Nucleus::createShl(Value *lhs, Value *rhs)
807{
808 RR_DEBUG_INFO_UPDATE_LOC();
809 return V(jit->builder->CreateShl(V(lhs), V(rhs)));
810}
811
812Value *Nucleus::createLShr(Value *lhs, Value *rhs)
813{
814 RR_DEBUG_INFO_UPDATE_LOC();
815 return V(jit->builder->CreateLShr(V(lhs), V(rhs)));
816}
817
818Value *Nucleus::createAShr(Value *lhs, Value *rhs)
819{
820 RR_DEBUG_INFO_UPDATE_LOC();
821 return V(jit->builder->CreateAShr(V(lhs), V(rhs)));
822}
823
824Value *Nucleus::createAnd(Value *lhs, Value *rhs)
825{
826 RR_DEBUG_INFO_UPDATE_LOC();
827 return V(jit->builder->CreateAnd(V(lhs), V(rhs)));
828}
829
830Value *Nucleus::createOr(Value *lhs, Value *rhs)
831{
832 RR_DEBUG_INFO_UPDATE_LOC();
833 return V(jit->builder->CreateOr(V(lhs), V(rhs)));
834}
835
836Value *Nucleus::createXor(Value *lhs, Value *rhs)
837{
838 RR_DEBUG_INFO_UPDATE_LOC();
839 return V(jit->builder->CreateXor(V(lhs), V(rhs)));
840}
841
842Value *Nucleus::createNeg(Value *v)
843{
844 RR_DEBUG_INFO_UPDATE_LOC();
845 return V(jit->builder->CreateNeg(V(v)));
846}
847
848Value *Nucleus::createFNeg(Value *v)
849{
850 RR_DEBUG_INFO_UPDATE_LOC();
851 return V(jit->builder->CreateFNeg(V(v)));
852}
853
854Value *Nucleus::createNot(Value *v)
855{
856 RR_DEBUG_INFO_UPDATE_LOC();
857 return V(jit->builder->CreateNot(V(v)));
858}
859
860Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder)
861{
862 RR_DEBUG_INFO_UPDATE_LOC();
863 switch(asInternalType(type))
John Bauman89401822014-05-06 15:04:28 -0400864 {
Nicolas Capens112faf42019-12-13 17:32:26 -0500865 case Type_v2i32:
866 case Type_v4i16:
867 case Type_v8i8:
868 case Type_v2f32:
869 return createBitCast(
870 createInsertElement(
871 V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::type()), 2, false))),
872 createLoad(createBitCast(ptr, Pointer<Long>::type()), Long::type(), isVolatile, alignment, atomic, memoryOrder),
873 0),
874 type);
875 case Type_v2i16:
876 case Type_v4i8:
877 if(alignment != 0) // Not a local variable (all vectors are 128-bit).
878 {
879 Value *u = V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::type()), 2, false)));
880 Value *i = createLoad(createBitCast(ptr, Pointer<Int>::type()), Int::type(), isVolatile, alignment, atomic, memoryOrder);
881 i = createZExt(i, Long::type());
882 Value *v = createInsertElement(u, i, 0);
883 return createBitCast(v, type);
884 }
885 // Fallthrough to non-emulated case.
886 case Type_LLVM:
Nicolas Capens01a97962017-07-28 17:30:51 -0400887 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500888 auto elTy = T(type);
Nicolas Capens157ba262019-12-10 17:49:14 -0500889
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500890 if(!atomic)
Nicolas Capens157ba262019-12-10 17:49:14 -0500891 {
Benjamin Kramered855982021-12-03 13:44:34 +0100892 return V(jit->builder->CreateAlignedLoad(elTy, V(ptr), llvm::MaybeAlign(alignment), isVolatile));
Nicolas Capens157ba262019-12-10 17:49:14 -0500893 }
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500894 else if(elTy->isIntegerTy() || elTy->isPointerTy())
Nicolas Capens157ba262019-12-10 17:49:14 -0500895 {
896 // Integers and pointers can be atomically loaded by setting
897 // the ordering constraint on the load instruction.
Benjamin Kramered855982021-12-03 13:44:34 +0100898 auto load = jit->builder->CreateAlignedLoad(elTy, V(ptr), llvm::MaybeAlign(alignment), isVolatile);
Nicolas Capens157ba262019-12-10 17:49:14 -0500899 load->setAtomic(atomicOrdering(atomic, memoryOrder));
900 return V(load);
901 }
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500902 else if(elTy->isFloatTy() || elTy->isDoubleTy())
Nicolas Capens157ba262019-12-10 17:49:14 -0500903 {
904 // LLVM claims to support atomic loads of float types as
905 // above, but certain backends cannot deal with this.
906 // Load as an integer and bitcast. See b/136037244.
907 auto size = jit->module->getDataLayout().getTypeStoreSize(elTy);
Nicolas Capens567e5602021-01-07 23:18:56 -0500908 auto elAsIntTy = llvm::IntegerType::get(*jit->context, size * 8);
Nicolas Capens157ba262019-12-10 17:49:14 -0500909 auto ptrCast = jit->builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo());
Benjamin Kramered855982021-12-03 13:44:34 +0100910 auto load = jit->builder->CreateAlignedLoad(elAsIntTy, ptrCast, llvm::MaybeAlign(alignment), isVolatile);
Nicolas Capens157ba262019-12-10 17:49:14 -0500911 load->setAtomic(atomicOrdering(atomic, memoryOrder));
912 auto loadCast = jit->builder->CreateBitCast(load, elTy);
913 return V(loadCast);
914 }
915 else
916 {
917 // More exotic types require falling back to the extern:
918 // void __atomic_load(size_t size, void *ptr, void *ret, int ordering)
Nicolas Capens567e5602021-01-07 23:18:56 -0500919 auto sizetTy = llvm::IntegerType::get(*jit->context, sizeof(size_t) * 8);
920 auto intTy = llvm::IntegerType::get(*jit->context, sizeof(int) * 8);
921 auto i8Ty = llvm::Type::getInt8Ty(*jit->context);
Nicolas Capens157ba262019-12-10 17:49:14 -0500922 auto i8PtrTy = i8Ty->getPointerTo();
Nicolas Capens567e5602021-01-07 23:18:56 -0500923 auto voidTy = llvm::Type::getVoidTy(*jit->context);
Nicolas Capens00ecdf92020-10-30 21:13:40 -0400924 auto funcTy = llvm::FunctionType::get(voidTy, { sizetTy, i8PtrTy, i8PtrTy, intTy }, false);
Nicolas Capens157ba262019-12-10 17:49:14 -0500925 auto func = jit->module->getOrInsertFunction("__atomic_load", funcTy);
926 auto size = jit->module->getDataLayout().getTypeStoreSize(elTy);
927 auto out = allocateStackVariable(type);
928 jit->builder->CreateCall(func, {
Nicolas Capens00ecdf92020-10-30 21:13:40 -0400929 llvm::ConstantInt::get(sizetTy, size),
Ben Clayton713b8d32019-12-17 20:37:56 +0000930 jit->builder->CreatePointerCast(V(ptr), i8PtrTy),
931 jit->builder->CreatePointerCast(V(out), i8PtrTy),
Nicolas Capens00ecdf92020-10-30 21:13:40 -0400932 llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))),
Ben Clayton713b8d32019-12-17 20:37:56 +0000933 });
Benjamin Kramered855982021-12-03 13:44:34 +0100934 return V(jit->builder->CreateLoad(T(type), V(out)));
Nicolas Capens157ba262019-12-10 17:49:14 -0500935 }
Nicolas Capens01a97962017-07-28 17:30:51 -0400936 }
Nicolas Capens112faf42019-12-13 17:32:26 -0500937 default:
938 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
939 return nullptr;
John Bauman89401822014-05-06 15:04:28 -0400940 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500941}
John Bauman89401822014-05-06 15:04:28 -0400942
Nicolas Capens157ba262019-12-10 17:49:14 -0500943Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder)
944{
945 RR_DEBUG_INFO_UPDATE_LOC();
946 switch(asInternalType(type))
Ben Clayton204a4102019-07-31 13:17:47 +0100947 {
Nicolas Capens112faf42019-12-13 17:32:26 -0500948 case Type_v2i32:
949 case Type_v4i16:
950 case Type_v8i8:
951 case Type_v2f32:
952 createStore(
953 createExtractElement(
954 createBitCast(value, T(llvm::VectorType::get(T(Long::type()), 2, false))), Long::type(), 0),
955 createBitCast(ptr, Pointer<Long>::type()),
956 Long::type(), isVolatile, alignment, atomic, memoryOrder);
957 return value;
958 case Type_v2i16:
959 case Type_v4i8:
960 if(alignment != 0) // Not a local variable (all vectors are 128-bit).
961 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500962 createStore(
Nicolas Capens112faf42019-12-13 17:32:26 -0500963 createExtractElement(createBitCast(value, Int4::type()), Int::type(), 0),
964 createBitCast(ptr, Pointer<Int>::type()),
965 Int::type(), isVolatile, alignment, atomic, memoryOrder);
Nicolas Capens157ba262019-12-10 17:49:14 -0500966 return value;
Nicolas Capens112faf42019-12-13 17:32:26 -0500967 }
968 // Fallthrough to non-emulated case.
969 case Type_LLVM:
Nicolas Capens13ac2322016-10-13 14:52:12 -0400970 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500971 auto elTy = T(type);
Nicolas Capens157ba262019-12-10 17:49:14 -0500972
Nicolas Capensdc8cbfa2021-06-10 17:09:50 -0400973 if(__has_feature(memory_sanitizer) && !jit->msanInstrumentation)
Antonio Maioranodd48b7e2020-02-05 13:17:07 -0500974 {
Nicolas Capensb34275e2020-10-30 14:33:50 -0400975 // Mark all memory writes as initialized by calling __msan_unpoison
Antonio Maioranodd48b7e2020-02-05 13:17:07 -0500976 // void __msan_unpoison(const volatile void *a, size_t size)
Nicolas Capens567e5602021-01-07 23:18:56 -0500977 auto voidTy = llvm::Type::getVoidTy(*jit->context);
978 auto i8Ty = llvm::Type::getInt8Ty(*jit->context);
Antonio Maioranodd48b7e2020-02-05 13:17:07 -0500979 auto voidPtrTy = i8Ty->getPointerTo();
Nicolas Capens567e5602021-01-07 23:18:56 -0500980 auto sizetTy = llvm::IntegerType::get(*jit->context, sizeof(size_t) * 8);
Nicolas Capens00ecdf92020-10-30 21:13:40 -0400981 auto funcTy = llvm::FunctionType::get(voidTy, { voidPtrTy, sizetTy }, false);
Antonio Maioranodd48b7e2020-02-05 13:17:07 -0500982 auto func = jit->module->getOrInsertFunction("__msan_unpoison", funcTy);
983 auto size = jit->module->getDataLayout().getTypeStoreSize(elTy);
Nicolas Capensb34275e2020-10-30 14:33:50 -0400984
Antonio Maioranodd48b7e2020-02-05 13:17:07 -0500985 jit->builder->CreateCall(func, { jit->builder->CreatePointerCast(V(ptr), voidPtrTy),
Nicolas Capens00ecdf92020-10-30 21:13:40 -0400986 llvm::ConstantInt::get(sizetTy, size) });
Antonio Maioranodd48b7e2020-02-05 13:17:07 -0500987 }
Antonio Maioranodd48b7e2020-02-05 13:17:07 -0500988
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500989 if(!atomic)
Nicolas Capens157ba262019-12-10 17:49:14 -0500990 {
Antonio Maiorano69661d82021-02-16 10:56:48 -0500991 jit->builder->CreateAlignedStore(V(value), V(ptr), llvm::MaybeAlign(alignment), isVolatile);
Nicolas Capens157ba262019-12-10 17:49:14 -0500992 }
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500993 else if(elTy->isIntegerTy() || elTy->isPointerTy())
Nicolas Capens157ba262019-12-10 17:49:14 -0500994 {
995 // Integers and pointers can be atomically stored by setting
996 // the ordering constraint on the store instruction.
Antonio Maiorano69661d82021-02-16 10:56:48 -0500997 auto store = jit->builder->CreateAlignedStore(V(value), V(ptr), llvm::MaybeAlign(alignment), isVolatile);
Nicolas Capens157ba262019-12-10 17:49:14 -0500998 store->setAtomic(atomicOrdering(atomic, memoryOrder));
999 }
Nicolas Capens81bc9d92019-12-16 15:05:57 -05001000 else if(elTy->isFloatTy() || elTy->isDoubleTy())
Nicolas Capens157ba262019-12-10 17:49:14 -05001001 {
1002 // LLVM claims to support atomic stores of float types as
1003 // above, but certain backends cannot deal with this.
1004 // Store as an bitcast integer. See b/136037244.
1005 auto size = jit->module->getDataLayout().getTypeStoreSize(elTy);
Nicolas Capens567e5602021-01-07 23:18:56 -05001006 auto elAsIntTy = llvm::IntegerType::get(*jit->context, size * 8);
Nicolas Capens157ba262019-12-10 17:49:14 -05001007 auto valCast = jit->builder->CreateBitCast(V(value), elAsIntTy);
1008 auto ptrCast = jit->builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo());
Antonio Maiorano69661d82021-02-16 10:56:48 -05001009 auto store = jit->builder->CreateAlignedStore(valCast, ptrCast, llvm::MaybeAlign(alignment), isVolatile);
Nicolas Capens157ba262019-12-10 17:49:14 -05001010 store->setAtomic(atomicOrdering(atomic, memoryOrder));
1011 }
1012 else
1013 {
1014 // More exotic types require falling back to the extern:
1015 // void __atomic_store(size_t size, void *ptr, void *val, int ordering)
Nicolas Capens567e5602021-01-07 23:18:56 -05001016 auto sizetTy = llvm::IntegerType::get(*jit->context, sizeof(size_t) * 8);
1017 auto intTy = llvm::IntegerType::get(*jit->context, sizeof(int) * 8);
1018 auto i8Ty = llvm::Type::getInt8Ty(*jit->context);
Nicolas Capens157ba262019-12-10 17:49:14 -05001019 auto i8PtrTy = i8Ty->getPointerTo();
Nicolas Capens567e5602021-01-07 23:18:56 -05001020 auto voidTy = llvm::Type::getVoidTy(*jit->context);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001021 auto funcTy = llvm::FunctionType::get(voidTy, { sizetTy, i8PtrTy, i8PtrTy, intTy }, false);
Nicolas Capens157ba262019-12-10 17:49:14 -05001022 auto func = jit->module->getOrInsertFunction("__atomic_store", funcTy);
1023 auto size = jit->module->getDataLayout().getTypeStoreSize(elTy);
1024 auto copy = allocateStackVariable(type);
1025 jit->builder->CreateStore(V(value), V(copy));
1026 jit->builder->CreateCall(func, {
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001027 llvm::ConstantInt::get(sizetTy, size),
Ben Clayton713b8d32019-12-17 20:37:56 +00001028 jit->builder->CreatePointerCast(V(ptr), i8PtrTy),
1029 jit->builder->CreatePointerCast(V(copy), i8PtrTy),
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001030 llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))),
Ben Clayton713b8d32019-12-17 20:37:56 +00001031 });
Nicolas Capens157ba262019-12-10 17:49:14 -05001032 }
1033
1034 return value;
Nicolas Capens13ac2322016-10-13 14:52:12 -04001035 }
Nicolas Capens112faf42019-12-13 17:32:26 -05001036 default:
1037 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
1038 return nullptr;
Nicolas Capens157ba262019-12-10 17:49:14 -05001039 }
1040}
Nicolas Capens13ac2322016-10-13 14:52:12 -04001041
Nicolas Capens157ba262019-12-10 17:49:14 -05001042Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes)
1043{
Antonio Maioranoaae33732020-02-14 14:52:34 -05001044 RR_DEBUG_INFO_UPDATE_LOC();
1045
Nicolas Capens157ba262019-12-10 17:49:14 -05001046 ASSERT(V(ptr)->getType()->isPointerTy());
1047 ASSERT(V(mask)->getType()->isVectorTy());
1048
Googler4d22b2c2021-02-01 12:10:01 +00001049 auto numEls = llvm::cast<llvm::FixedVectorType>(V(mask)->getType())->getNumElements();
Nicolas Capens567e5602021-01-07 23:18:56 -05001050 auto i1Ty = llvm::Type::getInt1Ty(*jit->context);
1051 auto i32Ty = llvm::Type::getInt32Ty(*jit->context);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001052 auto elVecTy = llvm::VectorType::get(T(elTy), numEls, false);
Nicolas Capens157ba262019-12-10 17:49:14 -05001053 auto elVecPtrTy = elVecTy->getPointerTo();
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001054 auto i8Mask = jit->builder->CreateIntCast(V(mask), llvm::VectorType::get(i1Ty, numEls, false), false); // vec<int, int, ...> -> vec<bool, bool, ...>
1055 auto passthrough = zeroMaskedLanes ? llvm::Constant::getNullValue(elVecTy) : llvm::UndefValue::get(elVecTy);
1056 auto align = llvm::ConstantInt::get(i32Ty, alignment);
1057 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_load, { elVecTy, elVecPtrTy });
Nicolas Capens157ba262019-12-10 17:49:14 -05001058 return V(jit->builder->CreateCall(func, { V(ptr), align, i8Mask, passthrough }));
1059}
1060
1061void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment)
1062{
Antonio Maioranoaae33732020-02-14 14:52:34 -05001063 RR_DEBUG_INFO_UPDATE_LOC();
1064
Nicolas Capens157ba262019-12-10 17:49:14 -05001065 ASSERT(V(ptr)->getType()->isPointerTy());
1066 ASSERT(V(val)->getType()->isVectorTy());
1067 ASSERT(V(mask)->getType()->isVectorTy());
1068
Googler4d22b2c2021-02-01 12:10:01 +00001069 auto numEls = llvm::cast<llvm::FixedVectorType>(V(mask)->getType())->getNumElements();
Nicolas Capens567e5602021-01-07 23:18:56 -05001070 auto i1Ty = llvm::Type::getInt1Ty(*jit->context);
1071 auto i32Ty = llvm::Type::getInt32Ty(*jit->context);
Nicolas Capens157ba262019-12-10 17:49:14 -05001072 auto elVecTy = V(val)->getType();
1073 auto elVecPtrTy = elVecTy->getPointerTo();
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001074 auto i1Mask = jit->builder->CreateIntCast(V(mask), llvm::VectorType::get(i1Ty, numEls, false), false); // vec<int, int, ...> -> vec<bool, bool, ...>
1075 auto align = llvm::ConstantInt::get(i32Ty, alignment);
1076 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_store, { elVecTy, elVecPtrTy });
James Price1d9d6452020-05-04 14:41:17 -04001077 jit->builder->CreateCall(func, { V(val), V(ptr), align, i1Mask });
1078
Nicolas Capensdc8cbfa2021-06-10 17:09:50 -04001079 if(__has_feature(memory_sanitizer) && !jit->msanInstrumentation)
James Price1d9d6452020-05-04 14:41:17 -04001080 {
Nicolas Capensb34275e2020-10-30 14:33:50 -04001081 // Mark memory writes as initialized by calling __msan_unpoison
James Price1d9d6452020-05-04 14:41:17 -04001082 // void __msan_unpoison(const volatile void *a, size_t size)
Nicolas Capens567e5602021-01-07 23:18:56 -05001083 auto voidTy = llvm::Type::getVoidTy(*jit->context);
James Price1d9d6452020-05-04 14:41:17 -04001084 auto voidPtrTy = voidTy->getPointerTo();
Nicolas Capens567e5602021-01-07 23:18:56 -05001085 auto sizetTy = llvm::IntegerType::get(*jit->context, sizeof(size_t) * 8);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001086 auto funcTy = llvm::FunctionType::get(voidTy, { voidPtrTy, sizetTy }, false);
James Price1d9d6452020-05-04 14:41:17 -04001087 auto func = jit->module->getOrInsertFunction("__msan_unpoison", funcTy);
1088 auto size = jit->module->getDataLayout().getTypeStoreSize(llvm::cast<llvm::VectorType>(elVecTy)->getElementType());
Nicolas Capensb34275e2020-10-30 14:33:50 -04001089
James Price1d9d6452020-05-04 14:41:17 -04001090 for(unsigned i = 0; i < numEls; i++)
1091 {
1092 // Check mask for this element
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001093 auto idx = llvm::ConstantInt::get(i32Ty, i);
Nicolas Capens567e5602021-01-07 23:18:56 -05001094 auto thenBlock = llvm::BasicBlock::Create(*jit->context, "", jit->function);
1095 auto mergeBlock = llvm::BasicBlock::Create(*jit->context, "", jit->function);
James Price1d9d6452020-05-04 14:41:17 -04001096 jit->builder->CreateCondBr(jit->builder->CreateExtractElement(i1Mask, idx), thenBlock, mergeBlock);
1097 jit->builder->SetInsertPoint(thenBlock);
1098
1099 // Insert __msan_unpoison call in conditional block
Benjamin Kramered855982021-12-03 13:44:34 +01001100 auto elPtr = jit->builder->CreateGEP(elVecTy, V(ptr), idx);
James Price1d9d6452020-05-04 14:41:17 -04001101 jit->builder->CreateCall(func, { jit->builder->CreatePointerCast(elPtr, voidPtrTy),
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001102 llvm::ConstantInt::get(sizetTy, size) });
Nicolas Capensb34275e2020-10-30 14:33:50 -04001103
James Price1d9d6452020-05-04 14:41:17 -04001104 jit->builder->CreateBr(mergeBlock);
1105 jit->builder->SetInsertPoint(mergeBlock);
1106 }
1107 }
Nicolas Capens19f01492020-11-02 22:04:54 -05001108}
Nicolas Capensb34275e2020-10-30 14:33:50 -04001109
1110static llvm::Value *createGather(llvm::Value *base, llvm::Type *elTy, llvm::Value *offsets, llvm::Value *mask, unsigned int alignment, bool zeroMaskedLanes)
1111{
1112 ASSERT(base->getType()->isPointerTy());
1113 ASSERT(offsets->getType()->isVectorTy());
1114 ASSERT(mask->getType()->isVectorTy());
1115
Googler4d22b2c2021-02-01 12:10:01 +00001116 auto numEls = llvm::cast<llvm::FixedVectorType>(mask->getType())->getNumElements();
Nicolas Capens567e5602021-01-07 23:18:56 -05001117 auto i1Ty = llvm::Type::getInt1Ty(*jit->context);
1118 auto i32Ty = llvm::Type::getInt32Ty(*jit->context);
1119 auto i8Ty = llvm::Type::getInt8Ty(*jit->context);
Nicolas Capensb34275e2020-10-30 14:33:50 -04001120 auto i8PtrTy = i8Ty->getPointerTo();
1121 auto elPtrTy = elTy->getPointerTo();
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001122 auto elVecTy = llvm::VectorType::get(elTy, numEls, false);
1123 auto elPtrVecTy = llvm::VectorType::get(elPtrTy, numEls, false);
Nicolas Capensb34275e2020-10-30 14:33:50 -04001124 auto i8Base = jit->builder->CreatePointerCast(base, i8PtrTy);
Benjamin Kramered855982021-12-03 13:44:34 +01001125 auto i8Ptrs = jit->builder->CreateGEP(i8Ty, i8Base, offsets);
Nicolas Capensb34275e2020-10-30 14:33:50 -04001126 auto elPtrs = jit->builder->CreatePointerCast(i8Ptrs, elPtrVecTy);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001127 auto i1Mask = jit->builder->CreateIntCast(mask, llvm::VectorType::get(i1Ty, numEls, false), false); // vec<int, int, ...> -> vec<bool, bool, ...>
1128 auto passthrough = zeroMaskedLanes ? llvm::Constant::getNullValue(elVecTy) : llvm::UndefValue::get(elVecTy);
Nicolas Capensb34275e2020-10-30 14:33:50 -04001129
1130 if(!__has_feature(memory_sanitizer))
1131 {
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001132 auto align = llvm::ConstantInt::get(i32Ty, alignment);
1133 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_gather, { elVecTy, elPtrVecTy });
Nicolas Capensb34275e2020-10-30 14:33:50 -04001134 return jit->builder->CreateCall(func, { elPtrs, align, i1Mask, passthrough });
1135 }
1136 else // __has_feature(memory_sanitizer)
1137 {
1138 // MemorySanitizer currently does not support instrumenting llvm::Intrinsic::masked_gather
1139 // Work around it by emulating gather with element-wise loads.
1140 // TODO(b/172238865): Remove when supported by MemorySanitizer.
1141
1142 Value *result = Nucleus::allocateStackVariable(T(elVecTy));
1143 Nucleus::createStore(V(passthrough), result, T(elVecTy));
1144
1145 for(unsigned i = 0; i < numEls; i++)
1146 {
1147 // Check mask for this element
1148 Value *elementMask = Nucleus::createExtractElement(V(i1Mask), T(i1Ty), i);
1149
1150 If(RValue<Bool>(elementMask))
1151 {
1152 Value *elPtr = Nucleus::createExtractElement(V(elPtrs), T(elPtrTy), i);
1153 Value *el = Nucleus::createLoad(elPtr, T(elTy), /*isVolatile */ false, alignment, /* atomic */ false, std::memory_order_relaxed);
1154
1155 Value *v = Nucleus::createLoad(result, T(elVecTy));
1156 v = Nucleus::createInsertElement(v, el, i);
1157 Nucleus::createStore(v, result, T(elVecTy));
1158 }
1159 }
1160
1161 return V(Nucleus::createLoad(result, T(elVecTy)));
1162 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001163}
1164
Nicolas Capensd1116fa2022-06-29 10:39:18 -04001165RValue<SIMD::Float> Gather(RValue<Pointer<Float>> base, RValue<SIMD::Int> offsets, RValue<SIMD::Int> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */)
Nicolas Capens157ba262019-12-10 17:49:14 -05001166{
Nicolas Capensd1116fa2022-06-29 10:39:18 -04001167 return As<SIMD::Float>(V(createGather(V(base.value()), T(Float::type()), V(offsets.value()), V(mask.value()), alignment, zeroMaskedLanes)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001168}
1169
Nicolas Capensd1116fa2022-06-29 10:39:18 -04001170RValue<SIMD::Int> Gather(RValue<Pointer<Int>> base, RValue<SIMD::Int> offsets, RValue<SIMD::Int> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */)
Nicolas Capens157ba262019-12-10 17:49:14 -05001171{
Nicolas Capensd1116fa2022-06-29 10:39:18 -04001172 return As<SIMD::Int>(V(createGather(V(base.value()), T(Int::type()), V(offsets.value()), V(mask.value()), alignment, zeroMaskedLanes)));
Nicolas Capensb34275e2020-10-30 14:33:50 -04001173}
1174
1175static void createScatter(llvm::Value *base, llvm::Value *val, llvm::Value *offsets, llvm::Value *mask, unsigned int alignment)
1176{
1177 ASSERT(base->getType()->isPointerTy());
1178 ASSERT(val->getType()->isVectorTy());
1179 ASSERT(offsets->getType()->isVectorTy());
1180 ASSERT(mask->getType()->isVectorTy());
1181
Googler4d22b2c2021-02-01 12:10:01 +00001182 auto numEls = llvm::cast<llvm::FixedVectorType>(mask->getType())->getNumElements();
Nicolas Capens567e5602021-01-07 23:18:56 -05001183 auto i1Ty = llvm::Type::getInt1Ty(*jit->context);
1184 auto i32Ty = llvm::Type::getInt32Ty(*jit->context);
1185 auto i8Ty = llvm::Type::getInt8Ty(*jit->context);
Nicolas Capensb34275e2020-10-30 14:33:50 -04001186 auto i8PtrTy = i8Ty->getPointerTo();
1187 auto elVecTy = val->getType();
1188 auto elTy = llvm::cast<llvm::VectorType>(elVecTy)->getElementType();
1189 auto elPtrTy = elTy->getPointerTo();
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001190 auto elPtrVecTy = llvm::VectorType::get(elPtrTy, numEls, false);
Nicolas Capensb34275e2020-10-30 14:33:50 -04001191
1192 auto i8Base = jit->builder->CreatePointerCast(base, i8PtrTy);
Benjamin Kramered855982021-12-03 13:44:34 +01001193 auto i8Ptrs = jit->builder->CreateGEP(i8Ty, i8Base, offsets);
Nicolas Capensb34275e2020-10-30 14:33:50 -04001194 auto elPtrs = jit->builder->CreatePointerCast(i8Ptrs, elPtrVecTy);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001195 auto i1Mask = jit->builder->CreateIntCast(mask, llvm::VectorType::get(i1Ty, numEls, false), false); // vec<int, int, ...> -> vec<bool, bool, ...>
Nicolas Capensb34275e2020-10-30 14:33:50 -04001196
1197 if(!__has_feature(memory_sanitizer))
1198 {
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001199 auto align = llvm::ConstantInt::get(i32Ty, alignment);
1200 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_scatter, { elVecTy, elPtrVecTy });
Nicolas Capensb34275e2020-10-30 14:33:50 -04001201 jit->builder->CreateCall(func, { val, elPtrs, align, i1Mask });
1202 }
1203 else // __has_feature(memory_sanitizer)
1204 {
1205 // MemorySanitizer currently does not support instrumenting llvm::Intrinsic::masked_scatter
1206 // Work around it by emulating scatter with element-wise stores.
1207 // TODO(b/172238865): Remove when supported by MemorySanitizer.
1208
1209 for(unsigned i = 0; i < numEls; i++)
1210 {
1211 // Check mask for this element
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001212 auto idx = llvm::ConstantInt::get(i32Ty, i);
Nicolas Capens567e5602021-01-07 23:18:56 -05001213 auto thenBlock = llvm::BasicBlock::Create(*jit->context, "", jit->function);
1214 auto mergeBlock = llvm::BasicBlock::Create(*jit->context, "", jit->function);
Nicolas Capensb34275e2020-10-30 14:33:50 -04001215 jit->builder->CreateCondBr(jit->builder->CreateExtractElement(i1Mask, idx), thenBlock, mergeBlock);
1216 jit->builder->SetInsertPoint(thenBlock);
1217
1218 auto el = jit->builder->CreateExtractElement(val, idx);
1219 auto elPtr = jit->builder->CreateExtractElement(elPtrs, idx);
1220 Nucleus::createStore(V(el), V(elPtr), T(elTy), /*isVolatile */ false, alignment, /* atomic */ false, std::memory_order_relaxed);
1221
1222 jit->builder->CreateBr(mergeBlock);
1223 jit->builder->SetInsertPoint(mergeBlock);
1224 }
1225 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001226}
1227
Nicolas Capensd1116fa2022-06-29 10:39:18 -04001228void Scatter(RValue<Pointer<Float>> base, RValue<SIMD::Float> val, RValue<SIMD::Int> offsets, RValue<SIMD::Int> mask, unsigned int alignment)
Nicolas Capens157ba262019-12-10 17:49:14 -05001229{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001230 return createScatter(V(base.value()), V(val.value()), V(offsets.value()), V(mask.value()), alignment);
Nicolas Capens157ba262019-12-10 17:49:14 -05001231}
1232
Nicolas Capensd1116fa2022-06-29 10:39:18 -04001233void Scatter(RValue<Pointer<Int>> base, RValue<SIMD::Int> val, RValue<SIMD::Int> offsets, RValue<SIMD::Int> mask, unsigned int alignment)
Nicolas Capens157ba262019-12-10 17:49:14 -05001234{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001235 return createScatter(V(base.value()), V(val.value()), V(offsets.value()), V(mask.value()), alignment);
Nicolas Capens157ba262019-12-10 17:49:14 -05001236}
1237
1238void Nucleus::createFence(std::memory_order memoryOrder)
1239{
Antonio Maioranoaae33732020-02-14 14:52:34 -05001240 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens157ba262019-12-10 17:49:14 -05001241 jit->builder->CreateFence(atomicOrdering(true, memoryOrder));
1242}
1243
1244Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
1245{
1246 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens4011ab32022-05-13 12:06:51 -04001247
Ben Clayton713b8d32019-12-17 20:37:56 +00001248 if(sizeof(void *) == 8)
Nicolas Capens157ba262019-12-10 17:49:14 -05001249 {
1250 // LLVM manual: "When indexing into an array, pointer or vector,
1251 // integers of any width are allowed, and they are not required to
1252 // be constant. These integers are treated as signed values where
1253 // relevant."
1254 //
1255 // Thus if we want indexes to be treated as unsigned we have to
1256 // zero-extend them ourselves.
1257 //
1258 // Note that this is not because we want to address anywhere near
1259 // 4 GB of data. Instead this is important for performance because
1260 // x86 supports automatic zero-extending of 32-bit registers to
1261 // 64-bit. Thus when indexing into an array using a uint32 is
1262 // actually faster than an int32.
Nicolas Capens519cf222020-05-08 15:27:19 -04001263 index = unsignedIndex ? createZExt(index, Long::type()) : createSExt(index, Long::type());
Nicolas Capens13ac2322016-10-13 14:52:12 -04001264 }
1265
Nicolas Capens157ba262019-12-10 17:49:14 -05001266 // For non-emulated types we can rely on LLVM's GEP to calculate the
1267 // effective address correctly.
1268 if(asInternalType(type) == Type_LLVM)
Nicolas Capens13ac2322016-10-13 14:52:12 -04001269 {
Benjamin Kramered855982021-12-03 13:44:34 +01001270 return V(jit->builder->CreateGEP(T(type), V(ptr), V(index)));
John Bauman89401822014-05-06 15:04:28 -04001271 }
1272
Nicolas Capens157ba262019-12-10 17:49:14 -05001273 // For emulated types we have to multiply the index by the intended
1274 // type size ourselves to obain the byte offset.
Ben Clayton713b8d32019-12-17 20:37:56 +00001275 index = (sizeof(void *) == 8) ? createMul(index, createConstantLong((int64_t)typeSize(type))) : createMul(index, createConstantInt((int)typeSize(type)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001276
1277 // Cast to a byte pointer, apply the byte offset, and cast back to the
1278 // original pointer type.
1279 return createBitCast(
Benjamin Kramered855982021-12-03 13:44:34 +01001280 V(jit->builder->CreateGEP(T(Byte::type()), V(createBitCast(ptr, T(llvm::PointerType::get(T(Byte::type()), 0)))), V(index))),
Ben Clayton713b8d32019-12-17 20:37:56 +00001281 T(llvm::PointerType::get(T(type), 0)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001282}
1283
1284Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder)
1285{
1286 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001287 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Add, V(ptr), V(value),
1288#if LLVM_VERSION_MAJOR >= 11
1289 llvm::MaybeAlign(),
1290#endif
1291 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001292}
1293
1294Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder)
1295{
1296 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001297 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Sub, V(ptr), V(value),
1298#if LLVM_VERSION_MAJOR >= 11
1299 llvm::MaybeAlign(),
1300#endif
1301 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001302}
1303
1304Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder)
1305{
1306 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001307 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::And, V(ptr), V(value),
1308#if LLVM_VERSION_MAJOR >= 11
1309 llvm::MaybeAlign(),
1310#endif
1311 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001312}
1313
1314Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder)
1315{
1316 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001317 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Or, V(ptr), V(value),
1318#if LLVM_VERSION_MAJOR >= 11
1319 llvm::MaybeAlign(),
1320#endif
1321 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001322}
1323
1324Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder)
1325{
1326 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001327 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xor, V(ptr), V(value),
1328#if LLVM_VERSION_MAJOR >= 11
1329 llvm::MaybeAlign(),
1330#endif
1331 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001332}
1333
1334Value *Nucleus::createAtomicMin(Value *ptr, Value *value, std::memory_order memoryOrder)
1335{
1336 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001337 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Min, V(ptr), V(value),
1338#if LLVM_VERSION_MAJOR >= 11
1339 llvm::MaybeAlign(),
1340#endif
1341 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001342}
1343
1344Value *Nucleus::createAtomicMax(Value *ptr, Value *value, std::memory_order memoryOrder)
1345{
1346 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001347 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Max, V(ptr), V(value),
1348#if LLVM_VERSION_MAJOR >= 11
1349 llvm::MaybeAlign(),
1350#endif
1351 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001352}
1353
1354Value *Nucleus::createAtomicUMin(Value *ptr, Value *value, std::memory_order memoryOrder)
1355{
1356 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001357 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMin, V(ptr), V(value),
1358#if LLVM_VERSION_MAJOR >= 11
1359 llvm::MaybeAlign(),
1360#endif
1361 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001362}
1363
1364Value *Nucleus::createAtomicUMax(Value *ptr, Value *value, std::memory_order memoryOrder)
1365{
1366 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001367 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMax, V(ptr), V(value),
1368#if LLVM_VERSION_MAJOR >= 11
1369 llvm::MaybeAlign(),
1370#endif
1371 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001372}
1373
Nicolas Capens157ba262019-12-10 17:49:14 -05001374Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder)
1375{
1376 RR_DEBUG_INFO_UPDATE_LOC();
Googler106e0152021-02-26 07:03:59 -05001377 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, V(ptr), V(value),
1378#if LLVM_VERSION_MAJOR >= 11
1379 llvm::MaybeAlign(),
1380#endif
1381 atomicOrdering(true, memoryOrder)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001382}
1383
1384Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal)
1385{
1386 RR_DEBUG_INFO_UPDATE_LOC();
1387 // Note: AtomicCmpXchgInstruction returns a 2-member struct containing {result, success-flag}, not the result directly.
1388 return V(jit->builder->CreateExtractValue(
Googler106e0152021-02-26 07:03:59 -05001389 jit->builder->CreateAtomicCmpXchg(V(ptr), V(compare), V(value),
1390#if LLVM_VERSION_MAJOR >= 11
1391 llvm::MaybeAlign(),
1392#endif
1393 atomicOrdering(true, memoryOrderEqual),
1394 atomicOrdering(true, memoryOrderUnequal)),
Ben Clayton713b8d32019-12-17 20:37:56 +00001395 llvm::ArrayRef<unsigned>(0u)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001396}
1397
1398Value *Nucleus::createTrunc(Value *v, Type *destType)
1399{
1400 RR_DEBUG_INFO_UPDATE_LOC();
1401 return V(jit->builder->CreateTrunc(V(v), T(destType)));
1402}
1403
1404Value *Nucleus::createZExt(Value *v, Type *destType)
1405{
1406 RR_DEBUG_INFO_UPDATE_LOC();
1407 return V(jit->builder->CreateZExt(V(v), T(destType)));
1408}
1409
1410Value *Nucleus::createSExt(Value *v, Type *destType)
1411{
1412 RR_DEBUG_INFO_UPDATE_LOC();
1413 return V(jit->builder->CreateSExt(V(v), T(destType)));
1414}
1415
1416Value *Nucleus::createFPToUI(Value *v, Type *destType)
1417{
1418 RR_DEBUG_INFO_UPDATE_LOC();
1419 return V(jit->builder->CreateFPToUI(V(v), T(destType)));
1420}
1421
1422Value *Nucleus::createFPToSI(Value *v, Type *destType)
1423{
1424 RR_DEBUG_INFO_UPDATE_LOC();
1425 return V(jit->builder->CreateFPToSI(V(v), T(destType)));
1426}
1427
1428Value *Nucleus::createSIToFP(Value *v, Type *destType)
1429{
1430 RR_DEBUG_INFO_UPDATE_LOC();
1431 return V(jit->builder->CreateSIToFP(V(v), T(destType)));
1432}
1433
1434Value *Nucleus::createFPTrunc(Value *v, Type *destType)
1435{
1436 RR_DEBUG_INFO_UPDATE_LOC();
1437 return V(jit->builder->CreateFPTrunc(V(v), T(destType)));
1438}
1439
1440Value *Nucleus::createFPExt(Value *v, Type *destType)
1441{
1442 RR_DEBUG_INFO_UPDATE_LOC();
1443 return V(jit->builder->CreateFPExt(V(v), T(destType)));
1444}
1445
1446Value *Nucleus::createBitCast(Value *v, Type *destType)
1447{
1448 RR_DEBUG_INFO_UPDATE_LOC();
1449 // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need
1450 // support for casting between scalars and wide vectors. Emulate them by writing to the stack and
1451 // reading back as the destination type.
1452 if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy())
John Bauman89401822014-05-06 15:04:28 -04001453 {
Nicolas Capens157ba262019-12-10 17:49:14 -05001454 Value *readAddress = allocateStackVariable(destType);
1455 Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0)));
1456 createStore(v, writeAddress, T(V(v)->getType()));
1457 return createLoad(readAddress, destType);
1458 }
1459 else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy())
1460 {
1461 Value *writeAddress = allocateStackVariable(T(V(v)->getType()));
1462 createStore(v, writeAddress, T(V(v)->getType()));
1463 Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0)));
1464 return createLoad(readAddress, destType);
John Bauman89401822014-05-06 15:04:28 -04001465 }
1466
Nicolas Capens157ba262019-12-10 17:49:14 -05001467 return V(jit->builder->CreateBitCast(V(v), T(destType)));
1468}
1469
Nicolas Capens157ba262019-12-10 17:49:14 -05001470Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs)
1471{
1472 RR_DEBUG_INFO_UPDATE_LOC();
1473 return V(jit->builder->CreateICmpEQ(V(lhs), V(rhs)));
1474}
1475
1476Value *Nucleus::createICmpNE(Value *lhs, Value *rhs)
1477{
1478 RR_DEBUG_INFO_UPDATE_LOC();
1479 return V(jit->builder->CreateICmpNE(V(lhs), V(rhs)));
1480}
1481
1482Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs)
1483{
1484 RR_DEBUG_INFO_UPDATE_LOC();
1485 return V(jit->builder->CreateICmpUGT(V(lhs), V(rhs)));
1486}
1487
1488Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs)
1489{
1490 RR_DEBUG_INFO_UPDATE_LOC();
1491 return V(jit->builder->CreateICmpUGE(V(lhs), V(rhs)));
1492}
1493
1494Value *Nucleus::createICmpULT(Value *lhs, Value *rhs)
1495{
1496 RR_DEBUG_INFO_UPDATE_LOC();
1497 return V(jit->builder->CreateICmpULT(V(lhs), V(rhs)));
1498}
1499
1500Value *Nucleus::createICmpULE(Value *lhs, Value *rhs)
1501{
1502 RR_DEBUG_INFO_UPDATE_LOC();
1503 return V(jit->builder->CreateICmpULE(V(lhs), V(rhs)));
1504}
1505
1506Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs)
1507{
1508 RR_DEBUG_INFO_UPDATE_LOC();
1509 return V(jit->builder->CreateICmpSGT(V(lhs), V(rhs)));
1510}
1511
1512Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs)
1513{
1514 RR_DEBUG_INFO_UPDATE_LOC();
1515 return V(jit->builder->CreateICmpSGE(V(lhs), V(rhs)));
1516}
1517
1518Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs)
1519{
1520 RR_DEBUG_INFO_UPDATE_LOC();
1521 return V(jit->builder->CreateICmpSLT(V(lhs), V(rhs)));
1522}
1523
1524Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs)
1525{
1526 RR_DEBUG_INFO_UPDATE_LOC();
1527 return V(jit->builder->CreateICmpSLE(V(lhs), V(rhs)));
1528}
1529
1530Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs)
1531{
1532 RR_DEBUG_INFO_UPDATE_LOC();
1533 return V(jit->builder->CreateFCmpOEQ(V(lhs), V(rhs)));
1534}
1535
1536Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs)
1537{
1538 RR_DEBUG_INFO_UPDATE_LOC();
1539 return V(jit->builder->CreateFCmpOGT(V(lhs), V(rhs)));
1540}
1541
1542Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs)
1543{
1544 RR_DEBUG_INFO_UPDATE_LOC();
1545 return V(jit->builder->CreateFCmpOGE(V(lhs), V(rhs)));
1546}
1547
1548Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs)
1549{
1550 RR_DEBUG_INFO_UPDATE_LOC();
1551 return V(jit->builder->CreateFCmpOLT(V(lhs), V(rhs)));
1552}
1553
1554Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs)
1555{
1556 RR_DEBUG_INFO_UPDATE_LOC();
1557 return V(jit->builder->CreateFCmpOLE(V(lhs), V(rhs)));
1558}
1559
1560Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs)
1561{
1562 RR_DEBUG_INFO_UPDATE_LOC();
1563 return V(jit->builder->CreateFCmpONE(V(lhs), V(rhs)));
1564}
1565
1566Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs)
1567{
1568 RR_DEBUG_INFO_UPDATE_LOC();
1569 return V(jit->builder->CreateFCmpORD(V(lhs), V(rhs)));
1570}
1571
1572Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs)
1573{
1574 RR_DEBUG_INFO_UPDATE_LOC();
1575 return V(jit->builder->CreateFCmpUNO(V(lhs), V(rhs)));
1576}
1577
1578Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs)
1579{
1580 RR_DEBUG_INFO_UPDATE_LOC();
1581 return V(jit->builder->CreateFCmpUEQ(V(lhs), V(rhs)));
1582}
1583
1584Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs)
1585{
1586 RR_DEBUG_INFO_UPDATE_LOC();
1587 return V(jit->builder->CreateFCmpUGT(V(lhs), V(rhs)));
1588}
1589
1590Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs)
1591{
1592 RR_DEBUG_INFO_UPDATE_LOC();
1593 return V(jit->builder->CreateFCmpUGE(V(lhs), V(rhs)));
1594}
1595
1596Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs)
1597{
1598 RR_DEBUG_INFO_UPDATE_LOC();
1599 return V(jit->builder->CreateFCmpULT(V(lhs), V(rhs)));
1600}
1601
1602Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs)
1603{
1604 RR_DEBUG_INFO_UPDATE_LOC();
1605 return V(jit->builder->CreateFCmpULE(V(lhs), V(rhs)));
1606}
1607
1608Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs)
1609{
1610 RR_DEBUG_INFO_UPDATE_LOC();
1611 return V(jit->builder->CreateFCmpUNE(V(lhs), V(rhs)));
1612}
1613
1614Value *Nucleus::createExtractElement(Value *vector, Type *type, int index)
1615{
1616 RR_DEBUG_INFO_UPDATE_LOC();
1617 ASSERT(V(vector)->getType()->getContainedType(0) == T(type));
1618 return V(jit->builder->CreateExtractElement(V(vector), V(createConstantInt(index))));
1619}
1620
1621Value *Nucleus::createInsertElement(Value *vector, Value *element, int index)
1622{
1623 RR_DEBUG_INFO_UPDATE_LOC();
1624 return V(jit->builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index))));
1625}
1626
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001627Value *Nucleus::createShuffleVector(Value *v1, Value *v2, std::vector<int> select)
Nicolas Capens157ba262019-12-10 17:49:14 -05001628{
1629 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensbc0f6632022-02-10 11:20:37 -05001630
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001631 size_t size = llvm::cast<llvm::FixedVectorType>(V(v1)->getType())->getNumElements();
1632 ASSERT(size == llvm::cast<llvm::FixedVectorType>(V(v2)->getType())->getNumElements());
1633
Nicolas Capensbc0f6632022-02-10 11:20:37 -05001634 llvm::SmallVector<int, 16> mask;
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001635 const size_t selectSize = select.size();
1636 for(size_t i = 0; i < size; i++)
Nicolas Capensbc0f6632022-02-10 11:20:37 -05001637 {
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001638 mask.push_back(select[i % selectSize]);
Nicolas Capensbc0f6632022-02-10 11:20:37 -05001639 }
1640
1641 return V(lowerShuffleVector(V(v1), V(v2), mask));
Nicolas Capens157ba262019-12-10 17:49:14 -05001642}
1643
1644Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse)
1645{
1646 RR_DEBUG_INFO_UPDATE_LOC();
1647 return V(jit->builder->CreateSelect(V(c), V(ifTrue), V(ifFalse)));
1648}
1649
1650SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
1651{
1652 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton713b8d32019-12-17 20:37:56 +00001653 return reinterpret_cast<SwitchCases *>(jit->builder->CreateSwitch(V(control), B(defaultBranch), numCases));
Nicolas Capens157ba262019-12-10 17:49:14 -05001654}
1655
1656void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
1657{
1658 RR_DEBUG_INFO_UPDATE_LOC();
1659 llvm::SwitchInst *sw = reinterpret_cast<llvm::SwitchInst *>(switchCases);
Nicolas Capens567e5602021-01-07 23:18:56 -05001660 sw->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*jit->context), label, true), B(branch));
Nicolas Capens157ba262019-12-10 17:49:14 -05001661}
1662
1663void Nucleus::createUnreachable()
1664{
1665 RR_DEBUG_INFO_UPDATE_LOC();
1666 jit->builder->CreateUnreachable();
1667}
1668
Antonio Maiorano62427e02020-02-13 09:18:05 -05001669Type *Nucleus::getType(Value *value)
1670{
1671 return T(V(value)->getType());
1672}
1673
1674Type *Nucleus::getContainedType(Type *vectorType)
1675{
1676 return T(T(vectorType)->getContainedType(0));
1677}
1678
Nicolas Capens157ba262019-12-10 17:49:14 -05001679Type *Nucleus::getPointerType(Type *ElementType)
1680{
1681 return T(llvm::PointerType::get(T(ElementType), 0));
1682}
1683
Nicolas Capens00ecdf92020-10-30 21:13:40 -04001684static llvm::Type *getNaturalIntType()
Antonio Maiorano62427e02020-02-13 09:18:05 -05001685{
Nicolas Capens567e5602021-01-07 23:18:56 -05001686 return llvm::Type::getIntNTy(*jit->context, sizeof(int) * 8);
Antonio Maiorano62427e02020-02-13 09:18:05 -05001687}
1688
1689Type *Nucleus::getPrintfStorageType(Type *valueType)
1690{
1691 llvm::Type *valueTy = T(valueType);
1692 if(valueTy->isIntegerTy())
1693 {
1694 return T(getNaturalIntType());
1695 }
1696 if(valueTy->isFloatTy())
1697 {
Nicolas Capens567e5602021-01-07 23:18:56 -05001698 return T(llvm::Type::getDoubleTy(*jit->context));
Antonio Maiorano62427e02020-02-13 09:18:05 -05001699 }
1700
1701 UNIMPLEMENTED_NO_BUG("getPrintfStorageType: add more cases as needed");
1702 return {};
1703}
1704
Nicolas Capens157ba262019-12-10 17:49:14 -05001705Value *Nucleus::createNullValue(Type *Ty)
1706{
1707 RR_DEBUG_INFO_UPDATE_LOC();
1708 return V(llvm::Constant::getNullValue(T(Ty)));
1709}
1710
1711Value *Nucleus::createConstantLong(int64_t i)
1712{
1713 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens567e5602021-01-07 23:18:56 -05001714 return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(*jit->context), i, true));
Nicolas Capens157ba262019-12-10 17:49:14 -05001715}
1716
1717Value *Nucleus::createConstantInt(int i)
1718{
1719 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens567e5602021-01-07 23:18:56 -05001720 return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*jit->context), i, true));
Nicolas Capens157ba262019-12-10 17:49:14 -05001721}
1722
1723Value *Nucleus::createConstantInt(unsigned int i)
1724{
1725 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens567e5602021-01-07 23:18:56 -05001726 return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*jit->context), i, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05001727}
1728
1729Value *Nucleus::createConstantBool(bool b)
1730{
1731 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens567e5602021-01-07 23:18:56 -05001732 return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(*jit->context), b));
Nicolas Capens157ba262019-12-10 17:49:14 -05001733}
1734
1735Value *Nucleus::createConstantByte(signed char i)
1736{
1737 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens567e5602021-01-07 23:18:56 -05001738 return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*jit->context), i, true));
Nicolas Capens157ba262019-12-10 17:49:14 -05001739}
1740
1741Value *Nucleus::createConstantByte(unsigned char i)
1742{
1743 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens567e5602021-01-07 23:18:56 -05001744 return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*jit->context), i, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05001745}
1746
1747Value *Nucleus::createConstantShort(short i)
1748{
1749 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens567e5602021-01-07 23:18:56 -05001750 return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*jit->context), i, true));
Nicolas Capens157ba262019-12-10 17:49:14 -05001751}
1752
1753Value *Nucleus::createConstantShort(unsigned short i)
1754{
1755 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens567e5602021-01-07 23:18:56 -05001756 return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*jit->context), i, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05001757}
1758
1759Value *Nucleus::createConstantFloat(float x)
1760{
1761 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens519cf222020-05-08 15:27:19 -04001762 return V(llvm::ConstantFP::get(T(Float::type()), x));
Nicolas Capens157ba262019-12-10 17:49:14 -05001763}
1764
1765Value *Nucleus::createNullPointer(Type *Ty)
1766{
1767 RR_DEBUG_INFO_UPDATE_LOC();
1768 return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0)));
1769}
1770
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001771Value *Nucleus::createConstantVector(std::vector<int64_t> constants, Type *type)
Nicolas Capens157ba262019-12-10 17:49:14 -05001772{
Antonio Maioranoaae33732020-02-14 14:52:34 -05001773 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens157ba262019-12-10 17:49:14 -05001774 ASSERT(llvm::isa<llvm::VectorType>(T(type)));
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001775 const size_t numConstants = constants.size(); // Number of provided constants for the (emulated) type.
1776 const size_t numElements = llvm::cast<llvm::FixedVectorType>(T(type))->getNumElements(); // Number of elements of the underlying vector type.
1777 llvm::SmallVector<llvm::Constant *, 16> constantVector;
Nicolas Capens157ba262019-12-10 17:49:14 -05001778
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001779 for(size_t i = 0; i < numElements; i++)
John Bauman89401822014-05-06 15:04:28 -04001780 {
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001781 constantVector.push_back(llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]));
John Bauman89401822014-05-06 15:04:28 -04001782 }
1783
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001784 return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant *>(constantVector)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001785}
1786
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001787Value *Nucleus::createConstantVector(std::vector<double> constants, Type *type)
Nicolas Capens157ba262019-12-10 17:49:14 -05001788{
Antonio Maioranoaae33732020-02-14 14:52:34 -05001789 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens157ba262019-12-10 17:49:14 -05001790 ASSERT(llvm::isa<llvm::VectorType>(T(type)));
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001791 const size_t numConstants = constants.size(); // Number of provided constants for the (emulated) type.
1792 const size_t numElements = llvm::cast<llvm::FixedVectorType>(T(type))->getNumElements(); // Number of elements of the underlying vector type.
1793 llvm::SmallVector<llvm::Constant *, 16> constantVector;
Nicolas Capens157ba262019-12-10 17:49:14 -05001794
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001795 for(size_t i = 0; i < numElements; i++)
John Bauman89401822014-05-06 15:04:28 -04001796 {
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001797 constantVector.push_back(llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]));
John Bauman89401822014-05-06 15:04:28 -04001798 }
1799
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001800 return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant *>(constantVector)));
Nicolas Capens157ba262019-12-10 17:49:14 -05001801}
John Bauman89401822014-05-06 15:04:28 -04001802
Antonio Maiorano62427e02020-02-13 09:18:05 -05001803Value *Nucleus::createConstantString(const char *v)
1804{
Antonio Maioranoaae33732020-02-14 14:52:34 -05001805 // NOTE: Do not call RR_DEBUG_INFO_UPDATE_LOC() here to avoid recursion when called from rr::Printv
Antonio Maiorano62427e02020-02-13 09:18:05 -05001806 auto ptr = jit->builder->CreateGlobalStringPtr(v);
1807 return V(ptr);
1808}
1809
Nicolas Capens54313fb2021-02-19 14:26:27 -05001810void Nucleus::setOptimizerCallback(OptimizerCallback *callback)
1811{
1812 // The LLVM backend does not produce optimizer reports.
1813 (void)callback;
1814}
1815
Nicolas Capens519cf222020-05-08 15:27:19 -04001816Type *Void::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001817{
Nicolas Capens567e5602021-01-07 23:18:56 -05001818 return T(llvm::Type::getVoidTy(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05001819}
John Bauman89401822014-05-06 15:04:28 -04001820
Nicolas Capens519cf222020-05-08 15:27:19 -04001821Type *Bool::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001822{
Nicolas Capens567e5602021-01-07 23:18:56 -05001823 return T(llvm::Type::getInt1Ty(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05001824}
John Bauman89401822014-05-06 15:04:28 -04001825
Nicolas Capens519cf222020-05-08 15:27:19 -04001826Type *Byte::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001827{
Nicolas Capens567e5602021-01-07 23:18:56 -05001828 return T(llvm::Type::getInt8Ty(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05001829}
John Bauman89401822014-05-06 15:04:28 -04001830
Nicolas Capens519cf222020-05-08 15:27:19 -04001831Type *SByte::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001832{
Nicolas Capens567e5602021-01-07 23:18:56 -05001833 return T(llvm::Type::getInt8Ty(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05001834}
1835
Nicolas Capens519cf222020-05-08 15:27:19 -04001836Type *Short::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001837{
Nicolas Capens567e5602021-01-07 23:18:56 -05001838 return T(llvm::Type::getInt16Ty(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05001839}
1840
Nicolas Capens519cf222020-05-08 15:27:19 -04001841Type *UShort::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001842{
Nicolas Capens567e5602021-01-07 23:18:56 -05001843 return T(llvm::Type::getInt16Ty(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05001844}
1845
Nicolas Capens519cf222020-05-08 15:27:19 -04001846Type *Byte4::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001847{
1848 return T(Type_v4i8);
1849}
1850
Nicolas Capens519cf222020-05-08 15:27:19 -04001851Type *SByte4::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001852{
1853 return T(Type_v4i8);
1854}
1855
1856RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y)
1857{
1858 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08001859#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05001860 return x86::paddusb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08001861#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001862 return As<Byte8>(V(lowerPUADDSAT(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08001863#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05001864}
John Bauman66b8ab22014-05-06 15:57:45 -04001865
Nicolas Capens157ba262019-12-10 17:49:14 -05001866RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y)
1867{
1868 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08001869#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05001870 return x86::psubusb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08001871#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001872 return As<Byte8>(V(lowerPUSUBSAT(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08001873#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05001874}
John Bauman89401822014-05-06 15:04:28 -04001875
Nicolas Capens157ba262019-12-10 17:49:14 -05001876RValue<Int> SignMask(RValue<Byte8> x)
1877{
1878 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08001879#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05001880 return x86::pmovmskb(x);
Logan Chiene3191012018-08-24 22:01:50 +08001881#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001882 return As<Int>(V(lowerSignMask(V(x.value()), T(Int::type()))));
Logan Chiene3191012018-08-24 22:01:50 +08001883#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05001884}
John Bauman89401822014-05-06 15:04:28 -04001885
John Bauman19bac1e2014-05-06 15:23:49 -04001886// RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04001887// {
Logan Chiene3191012018-08-24 22:01:50 +08001888//#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04001889// return x86::pcmpgtb(x, y); // FIXME: Signedness
Logan Chiene3191012018-08-24 22:01:50 +08001890//#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001891// return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value()), V(y.value()), T(Byte8::type()))));
Logan Chiene3191012018-08-24 22:01:50 +08001892//#endif
John Bauman89401822014-05-06 15:04:28 -04001893// }
John Bauman66b8ab22014-05-06 15:57:45 -04001894
Nicolas Capens157ba262019-12-10 17:49:14 -05001895RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y)
1896{
1897 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08001898#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05001899 return x86::pcmpeqb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08001900#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001901 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value()), V(y.value()), T(Byte8::type()))));
Logan Chiene3191012018-08-24 22:01:50 +08001902#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05001903}
John Bauman89401822014-05-06 15:04:28 -04001904
Nicolas Capens519cf222020-05-08 15:27:19 -04001905Type *Byte8::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001906{
1907 return T(Type_v8i8);
1908}
John Bauman89401822014-05-06 15:04:28 -04001909
Nicolas Capens157ba262019-12-10 17:49:14 -05001910RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y)
1911{
1912 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08001913#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05001914 return x86::paddsb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08001915#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001916 return As<SByte8>(V(lowerPSADDSAT(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08001917#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05001918}
John Bauman66b8ab22014-05-06 15:57:45 -04001919
Nicolas Capens157ba262019-12-10 17:49:14 -05001920RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y)
1921{
1922 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08001923#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05001924 return x86::psubsb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08001925#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001926 return As<SByte8>(V(lowerPSSUBSAT(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08001927#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05001928}
John Bauman89401822014-05-06 15:04:28 -04001929
Nicolas Capens157ba262019-12-10 17:49:14 -05001930RValue<Int> SignMask(RValue<SByte8> x)
1931{
1932 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08001933#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05001934 return x86::pmovmskb(As<Byte8>(x));
Logan Chiene3191012018-08-24 22:01:50 +08001935#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001936 return As<Int>(V(lowerSignMask(V(x.value()), T(Int::type()))));
Logan Chiene3191012018-08-24 22:01:50 +08001937#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05001938}
John Bauman89401822014-05-06 15:04:28 -04001939
Nicolas Capens157ba262019-12-10 17:49:14 -05001940RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y)
1941{
1942 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08001943#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05001944 return x86::pcmpgtb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08001945#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001946 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value()), V(y.value()), T(Byte8::type()))));
Logan Chiene3191012018-08-24 22:01:50 +08001947#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05001948}
John Bauman66b8ab22014-05-06 15:57:45 -04001949
Nicolas Capens157ba262019-12-10 17:49:14 -05001950RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y)
1951{
1952 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08001953#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05001954 return x86::pcmpeqb(As<Byte8>(x), As<Byte8>(y));
Logan Chiene3191012018-08-24 22:01:50 +08001955#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001956 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value()), V(y.value()), T(Byte8::type()))));
Logan Chiene3191012018-08-24 22:01:50 +08001957#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05001958}
John Bauman89401822014-05-06 15:04:28 -04001959
Nicolas Capens519cf222020-05-08 15:27:19 -04001960Type *SByte8::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001961{
1962 return T(Type_v8i8);
1963}
John Bauman89401822014-05-06 15:04:28 -04001964
Nicolas Capens519cf222020-05-08 15:27:19 -04001965Type *Byte16::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001966{
Nicolas Capense7cd2242020-08-17 10:49:56 -04001967 return T(llvm::VectorType::get(T(Byte::type()), 16, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05001968}
John Bauman89401822014-05-06 15:04:28 -04001969
Nicolas Capens519cf222020-05-08 15:27:19 -04001970Type *SByte16::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001971{
Nicolas Capense7cd2242020-08-17 10:49:56 -04001972 return T(llvm::VectorType::get(T(SByte::type()), 16, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05001973}
John Bauman89401822014-05-06 15:04:28 -04001974
Nicolas Capens519cf222020-05-08 15:27:19 -04001975Type *Short2::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001976{
1977 return T(Type_v2i16);
1978}
Nicolas Capens16b5f152016-10-13 13:39:01 -04001979
Nicolas Capens519cf222020-05-08 15:27:19 -04001980Type *UShort2::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05001981{
1982 return T(Type_v2i16);
1983}
Nicolas Capens16b5f152016-10-13 13:39:01 -04001984
Nicolas Capens157ba262019-12-10 17:49:14 -05001985Short4::Short4(RValue<Int4> cast)
1986{
1987 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens4e7d3102022-06-21 01:42:18 -04001988 std::vector<int> select = { 0, 2, 4, 6, 0, 2, 4, 6 };
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001989 Value *short8 = Nucleus::createBitCast(cast.value(), Short8::type());
John Bauman89401822014-05-06 15:04:28 -04001990
Nicolas Capens157ba262019-12-10 17:49:14 -05001991 Value *packed = Nucleus::createShuffleVector(short8, short8, select);
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04001992 Value *short4 = As<Short4>(Int2(As<Int4>(packed))).value();
John Bauman89401822014-05-06 15:04:28 -04001993
Nicolas Capens157ba262019-12-10 17:49:14 -05001994 storeValue(short4);
1995}
John Bauman89401822014-05-06 15:04:28 -04001996
John Bauman19bac1e2014-05-06 15:23:49 -04001997// Short4::Short4(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04001998// {
1999// }
2000
Nicolas Capens157ba262019-12-10 17:49:14 -05002001Short4::Short4(RValue<Float4> cast)
2002{
2003 RR_DEBUG_INFO_UPDATE_LOC();
2004 Int4 v4i32 = Int4(cast);
Logan Chiene3191012018-08-24 22:01:50 +08002005#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002006 v4i32 = As<Int4>(x86::packssdw(v4i32, v4i32));
Logan Chiene3191012018-08-24 22:01:50 +08002007#else
Nicolas Capens157ba262019-12-10 17:49:14 -05002008 Value *v = v4i32.loadValue();
2009 v4i32 = As<Int4>(V(lowerPack(V(v), V(v), true)));
Logan Chiene3191012018-08-24 22:01:50 +08002010#endif
John Bauman66b8ab22014-05-06 15:57:45 -04002011
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002012 storeValue(As<Short4>(Int2(v4i32)).value());
Nicolas Capens157ba262019-12-10 17:49:14 -05002013}
John Bauman89401822014-05-06 15:04:28 -04002014
Nicolas Capens157ba262019-12-10 17:49:14 -05002015RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs)
2016{
2017 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002018#if defined(__i386__) || defined(__x86_64__)
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002019 // return RValue<Short4>(Nucleus::createShl(lhs.value(), rhs.value()));
John Bauman89401822014-05-06 15:04:28 -04002020
Nicolas Capens157ba262019-12-10 17:49:14 -05002021 return x86::psllw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002022#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002023 return As<Short4>(V(lowerVectorShl(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002024#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002025}
John Bauman89401822014-05-06 15:04:28 -04002026
Nicolas Capens157ba262019-12-10 17:49:14 -05002027RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs)
2028{
2029 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002030#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002031 return x86::psraw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002032#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002033 return As<Short4>(V(lowerVectorAShr(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002034#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002035}
John Bauman89401822014-05-06 15:04:28 -04002036
Nicolas Capens157ba262019-12-10 17:49:14 -05002037RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y)
2038{
2039 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002040#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002041 return x86::pmaxsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002042#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002043 return RValue<Short4>(V(lowerPMINMAX(V(x.value()), V(y.value()), llvm::ICmpInst::ICMP_SGT)));
Logan Chiene3191012018-08-24 22:01:50 +08002044#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002045}
John Bauman89401822014-05-06 15:04:28 -04002046
Nicolas Capens157ba262019-12-10 17:49:14 -05002047RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y)
2048{
2049 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002050#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002051 return x86::pminsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002052#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002053 return RValue<Short4>(V(lowerPMINMAX(V(x.value()), V(y.value()), llvm::ICmpInst::ICMP_SLT)));
Logan Chiene3191012018-08-24 22:01:50 +08002054#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002055}
John Bauman89401822014-05-06 15:04:28 -04002056
Nicolas Capens157ba262019-12-10 17:49:14 -05002057RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y)
2058{
2059 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002060#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002061 return x86::paddsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002062#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002063 return As<Short4>(V(lowerPSADDSAT(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08002064#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002065}
John Bauman89401822014-05-06 15:04:28 -04002066
Nicolas Capens157ba262019-12-10 17:49:14 -05002067RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y)
2068{
2069 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002070#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002071 return x86::psubsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002072#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002073 return As<Short4>(V(lowerPSSUBSAT(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08002074#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002075}
John Bauman89401822014-05-06 15:04:28 -04002076
Nicolas Capens157ba262019-12-10 17:49:14 -05002077RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y)
2078{
2079 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002080#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002081 return x86::pmulhw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002082#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002083 return As<Short4>(V(lowerMulHigh(V(x.value()), V(y.value()), true)));
Logan Chiene3191012018-08-24 22:01:50 +08002084#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002085}
John Bauman89401822014-05-06 15:04:28 -04002086
Nicolas Capens157ba262019-12-10 17:49:14 -05002087RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y)
2088{
2089 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002090#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002091 return x86::pmaddwd(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002092#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002093 return As<Int2>(V(lowerMulAdd(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08002094#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002095}
John Bauman89401822014-05-06 15:04:28 -04002096
Nicolas Capens157ba262019-12-10 17:49:14 -05002097RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y)
2098{
2099 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002100#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002101 auto result = x86::packsswb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002102#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002103 auto result = V(lowerPack(V(x.value()), V(y.value()), true));
Logan Chiene3191012018-08-24 22:01:50 +08002104#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002105 return As<SByte8>(Swizzle(As<Int4>(result), 0x0202));
2106}
John Bauman89401822014-05-06 15:04:28 -04002107
Nicolas Capens157ba262019-12-10 17:49:14 -05002108RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y)
2109{
2110 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002111#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002112 auto result = x86::packuswb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002113#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002114 auto result = V(lowerPack(V(x.value()), V(y.value()), false));
Logan Chiene3191012018-08-24 22:01:50 +08002115#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002116 return As<Byte8>(Swizzle(As<Int4>(result), 0x0202));
2117}
Nicolas Capens33438a62017-09-27 11:47:35 -04002118
Nicolas Capens157ba262019-12-10 17:49:14 -05002119RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y)
2120{
2121 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002122#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002123 return x86::pcmpgtw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002124#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002125 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value()), V(y.value()), T(Short4::type()))));
Logan Chiene3191012018-08-24 22:01:50 +08002126#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002127}
John Bauman89401822014-05-06 15:04:28 -04002128
Nicolas Capens157ba262019-12-10 17:49:14 -05002129RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y)
2130{
2131 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002132#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002133 return x86::pcmpeqw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002134#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002135 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value()), V(y.value()), T(Short4::type()))));
Logan Chiene3191012018-08-24 22:01:50 +08002136#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002137}
John Bauman89401822014-05-06 15:04:28 -04002138
Nicolas Capens519cf222020-05-08 15:27:19 -04002139Type *Short4::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002140{
2141 return T(Type_v4i16);
2142}
John Bauman89401822014-05-06 15:04:28 -04002143
Nicolas Capens157ba262019-12-10 17:49:14 -05002144UShort4::UShort4(RValue<Float4> cast, bool saturate)
2145{
2146 RR_DEBUG_INFO_UPDATE_LOC();
2147 if(saturate)
John Bauman89401822014-05-06 15:04:28 -04002148 {
Nicolas Capens157ba262019-12-10 17:49:14 -05002149#if defined(__i386__) || defined(__x86_64__)
2150 if(CPUID::supportsSSE4_1())
John Bauman89401822014-05-06 15:04:28 -04002151 {
Ben Clayton713b8d32019-12-17 20:37:56 +00002152 Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation
Nicolas Capens157ba262019-12-10 17:49:14 -05002153 *this = As<Short4>(PackUnsigned(int4, int4));
John Bauman89401822014-05-06 15:04:28 -04002154 }
2155 else
Nicolas Capens157ba262019-12-10 17:49:14 -05002156#endif
John Bauman89401822014-05-06 15:04:28 -04002157 {
Nicolas Capens157ba262019-12-10 17:49:14 -05002158 *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000))));
John Bauman89401822014-05-06 15:04:28 -04002159 }
2160 }
Nicolas Capens157ba262019-12-10 17:49:14 -05002161 else
John Bauman89401822014-05-06 15:04:28 -04002162 {
Nicolas Capens157ba262019-12-10 17:49:14 -05002163 *this = Short4(Int4(cast));
2164 }
2165}
2166
2167RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs)
2168{
2169 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002170#if defined(__i386__) || defined(__x86_64__)
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002171 // return RValue<Short4>(Nucleus::createShl(lhs.value(), rhs.value()));
John Bauman89401822014-05-06 15:04:28 -04002172
Nicolas Capens157ba262019-12-10 17:49:14 -05002173 return As<UShort4>(x86::psllw(As<Short4>(lhs), rhs));
Logan Chiene3191012018-08-24 22:01:50 +08002174#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002175 return As<UShort4>(V(lowerVectorShl(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002176#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002177}
John Bauman89401822014-05-06 15:04:28 -04002178
Nicolas Capens157ba262019-12-10 17:49:14 -05002179RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs)
2180{
2181 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002182#if defined(__i386__) || defined(__x86_64__)
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002183 // return RValue<Short4>(Nucleus::createLShr(lhs.value(), rhs.value()));
John Bauman89401822014-05-06 15:04:28 -04002184
Nicolas Capens157ba262019-12-10 17:49:14 -05002185 return x86::psrlw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002186#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002187 return As<UShort4>(V(lowerVectorLShr(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002188#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002189}
John Bauman89401822014-05-06 15:04:28 -04002190
Nicolas Capens157ba262019-12-10 17:49:14 -05002191RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y)
2192{
2193 RR_DEBUG_INFO_UPDATE_LOC();
2194 return RValue<UShort4>(Max(As<Short4>(x) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u), As<Short4>(y) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)) + Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u));
2195}
John Bauman89401822014-05-06 15:04:28 -04002196
Nicolas Capens157ba262019-12-10 17:49:14 -05002197RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y)
2198{
2199 RR_DEBUG_INFO_UPDATE_LOC();
2200 return RValue<UShort4>(Min(As<Short4>(x) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u), As<Short4>(y) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)) + Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u));
2201}
John Bauman89401822014-05-06 15:04:28 -04002202
Nicolas Capens157ba262019-12-10 17:49:14 -05002203RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y)
2204{
2205 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002206#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002207 return x86::paddusw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002208#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002209 return As<UShort4>(V(lowerPUADDSAT(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08002210#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002211}
John Bauman89401822014-05-06 15:04:28 -04002212
Nicolas Capens157ba262019-12-10 17:49:14 -05002213RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y)
2214{
2215 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002216#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002217 return x86::psubusw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002218#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002219 return As<UShort4>(V(lowerPUSUBSAT(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08002220#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002221}
John Bauman89401822014-05-06 15:04:28 -04002222
Nicolas Capens157ba262019-12-10 17:49:14 -05002223RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y)
2224{
2225 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002226#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002227 return x86::pmulhuw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002228#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002229 return As<UShort4>(V(lowerMulHigh(V(x.value()), V(y.value()), false)));
Logan Chiene3191012018-08-24 22:01:50 +08002230#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002231}
John Bauman89401822014-05-06 15:04:28 -04002232
Nicolas Capens157ba262019-12-10 17:49:14 -05002233RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)
2234{
2235 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002236#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002237 return x86::pavgw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002238#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002239 return As<UShort4>(V(lowerPAVG(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08002240#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002241}
John Bauman89401822014-05-06 15:04:28 -04002242
Nicolas Capens519cf222020-05-08 15:27:19 -04002243Type *UShort4::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002244{
2245 return T(Type_v4i16);
2246}
John Bauman89401822014-05-06 15:04:28 -04002247
Nicolas Capens157ba262019-12-10 17:49:14 -05002248RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs)
2249{
2250 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002251#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002252 return x86::psllw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002253#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002254 return As<Short8>(V(lowerVectorShl(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002255#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002256}
John Bauman89401822014-05-06 15:04:28 -04002257
Nicolas Capens157ba262019-12-10 17:49:14 -05002258RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs)
2259{
2260 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002261#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002262 return x86::psraw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002263#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002264 return As<Short8>(V(lowerVectorAShr(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002265#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002266}
John Bauman89401822014-05-06 15:04:28 -04002267
Nicolas Capens157ba262019-12-10 17:49:14 -05002268RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)
2269{
2270 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002271#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002272 return x86::pmaddwd(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002273#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002274 return As<Int4>(V(lowerMulAdd(V(x.value()), V(y.value()))));
Logan Chiene3191012018-08-24 22:01:50 +08002275#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002276}
John Bauman89401822014-05-06 15:04:28 -04002277
Nicolas Capens157ba262019-12-10 17:49:14 -05002278RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)
2279{
2280 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002281#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002282 return x86::pmulhw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002283#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002284 return As<Short8>(V(lowerMulHigh(V(x.value()), V(y.value()), true)));
Logan Chiene3191012018-08-24 22:01:50 +08002285#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002286}
John Bauman89401822014-05-06 15:04:28 -04002287
Nicolas Capens519cf222020-05-08 15:27:19 -04002288Type *Short8::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002289{
Nicolas Capense7cd2242020-08-17 10:49:56 -04002290 return T(llvm::VectorType::get(T(Short::type()), 8, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05002291}
John Bauman89401822014-05-06 15:04:28 -04002292
Nicolas Capens157ba262019-12-10 17:49:14 -05002293RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs)
2294{
2295 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002296#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002297 return As<UShort8>(x86::psllw(As<Short8>(lhs), rhs));
Logan Chiene3191012018-08-24 22:01:50 +08002298#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002299 return As<UShort8>(V(lowerVectorShl(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002300#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002301}
John Bauman89401822014-05-06 15:04:28 -04002302
Nicolas Capens157ba262019-12-10 17:49:14 -05002303RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs)
2304{
2305 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002306#if defined(__i386__) || defined(__x86_64__)
Ben Clayton713b8d32019-12-17 20:37:56 +00002307 return x86::psrlw(lhs, rhs); // FIXME: Fallback required
Logan Chiene3191012018-08-24 22:01:50 +08002308#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002309 return As<UShort8>(V(lowerVectorLShr(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002310#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002311}
John Bauman89401822014-05-06 15:04:28 -04002312
Nicolas Capens157ba262019-12-10 17:49:14 -05002313RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)
2314{
2315 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002316#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002317 return x86::pmulhuw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002318#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002319 return As<UShort8>(V(lowerMulHigh(V(x.value()), V(y.value()), false)));
Logan Chiene3191012018-08-24 22:01:50 +08002320#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002321}
John Bauman89401822014-05-06 15:04:28 -04002322
Nicolas Capens519cf222020-05-08 15:27:19 -04002323Type *UShort8::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002324{
Nicolas Capense7cd2242020-08-17 10:49:56 -04002325 return T(llvm::VectorType::get(T(UShort::type()), 8, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05002326}
John Bauman89401822014-05-06 15:04:28 -04002327
Ben Clayton713b8d32019-12-17 20:37:56 +00002328RValue<Int> operator++(Int &val, int) // Post-increment
Nicolas Capens157ba262019-12-10 17:49:14 -05002329{
2330 RR_DEBUG_INFO_UPDATE_LOC();
2331 RValue<Int> res = val;
John Bauman89401822014-05-06 15:04:28 -04002332
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002333 Value *inc = Nucleus::createAdd(res.value(), Nucleus::createConstantInt(1));
Nicolas Capens157ba262019-12-10 17:49:14 -05002334 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002335
Nicolas Capens157ba262019-12-10 17:49:14 -05002336 return res;
2337}
John Bauman89401822014-05-06 15:04:28 -04002338
Ben Clayton713b8d32019-12-17 20:37:56 +00002339const Int &operator++(Int &val) // Pre-increment
Nicolas Capens157ba262019-12-10 17:49:14 -05002340{
2341 RR_DEBUG_INFO_UPDATE_LOC();
2342 Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1));
2343 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002344
Nicolas Capens157ba262019-12-10 17:49:14 -05002345 return val;
2346}
John Bauman89401822014-05-06 15:04:28 -04002347
Ben Clayton713b8d32019-12-17 20:37:56 +00002348RValue<Int> operator--(Int &val, int) // Post-decrement
Nicolas Capens157ba262019-12-10 17:49:14 -05002349{
2350 RR_DEBUG_INFO_UPDATE_LOC();
2351 RValue<Int> res = val;
John Bauman89401822014-05-06 15:04:28 -04002352
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002353 Value *inc = Nucleus::createSub(res.value(), Nucleus::createConstantInt(1));
Nicolas Capens157ba262019-12-10 17:49:14 -05002354 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002355
Nicolas Capens157ba262019-12-10 17:49:14 -05002356 return res;
2357}
John Bauman89401822014-05-06 15:04:28 -04002358
Ben Clayton713b8d32019-12-17 20:37:56 +00002359const Int &operator--(Int &val) // Pre-decrement
Nicolas Capens157ba262019-12-10 17:49:14 -05002360{
2361 RR_DEBUG_INFO_UPDATE_LOC();
2362 Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1));
2363 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002364
Nicolas Capens157ba262019-12-10 17:49:14 -05002365 return val;
2366}
John Bauman89401822014-05-06 15:04:28 -04002367
Nicolas Capens157ba262019-12-10 17:49:14 -05002368RValue<Int> RoundInt(RValue<Float> cast)
2369{
2370 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002371#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens157ba262019-12-10 17:49:14 -05002372 return x86::cvtss2si(cast);
Logan Chiene3191012018-08-24 22:01:50 +08002373#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002374 return RValue<Int>(V(lowerRoundInt(V(cast.value()), T(Int::type()))));
Logan Chiene3191012018-08-24 22:01:50 +08002375#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002376}
John Bauman89401822014-05-06 15:04:28 -04002377
Nicolas Capens519cf222020-05-08 15:27:19 -04002378Type *Int::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002379{
Nicolas Capens567e5602021-01-07 23:18:56 -05002380 return T(llvm::Type::getInt32Ty(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05002381}
John Bauman89401822014-05-06 15:04:28 -04002382
Nicolas Capens519cf222020-05-08 15:27:19 -04002383Type *Long::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002384{
Nicolas Capens567e5602021-01-07 23:18:56 -05002385 return T(llvm::Type::getInt64Ty(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05002386}
John Bauman89401822014-05-06 15:04:28 -04002387
Nicolas Capens157ba262019-12-10 17:49:14 -05002388UInt::UInt(RValue<Float> cast)
2389{
2390 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002391 Value *integer = Nucleus::createFPToUI(cast.value(), UInt::type());
Nicolas Capens157ba262019-12-10 17:49:14 -05002392 storeValue(integer);
2393}
John Bauman89401822014-05-06 15:04:28 -04002394
Ben Clayton713b8d32019-12-17 20:37:56 +00002395RValue<UInt> operator++(UInt &val, int) // Post-increment
Nicolas Capens157ba262019-12-10 17:49:14 -05002396{
2397 RR_DEBUG_INFO_UPDATE_LOC();
2398 RValue<UInt> res = val;
John Bauman89401822014-05-06 15:04:28 -04002399
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002400 Value *inc = Nucleus::createAdd(res.value(), Nucleus::createConstantInt(1));
Nicolas Capens157ba262019-12-10 17:49:14 -05002401 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002402
Nicolas Capens157ba262019-12-10 17:49:14 -05002403 return res;
2404}
John Bauman89401822014-05-06 15:04:28 -04002405
Ben Clayton713b8d32019-12-17 20:37:56 +00002406const UInt &operator++(UInt &val) // Pre-increment
Nicolas Capens157ba262019-12-10 17:49:14 -05002407{
2408 RR_DEBUG_INFO_UPDATE_LOC();
2409 Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1));
2410 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002411
Nicolas Capens157ba262019-12-10 17:49:14 -05002412 return val;
2413}
John Bauman89401822014-05-06 15:04:28 -04002414
Ben Clayton713b8d32019-12-17 20:37:56 +00002415RValue<UInt> operator--(UInt &val, int) // Post-decrement
Nicolas Capens157ba262019-12-10 17:49:14 -05002416{
2417 RR_DEBUG_INFO_UPDATE_LOC();
2418 RValue<UInt> res = val;
John Bauman89401822014-05-06 15:04:28 -04002419
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002420 Value *inc = Nucleus::createSub(res.value(), Nucleus::createConstantInt(1));
Nicolas Capens157ba262019-12-10 17:49:14 -05002421 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002422
Nicolas Capens157ba262019-12-10 17:49:14 -05002423 return res;
2424}
John Bauman89401822014-05-06 15:04:28 -04002425
Ben Clayton713b8d32019-12-17 20:37:56 +00002426const UInt &operator--(UInt &val) // Pre-decrement
Nicolas Capens157ba262019-12-10 17:49:14 -05002427{
2428 RR_DEBUG_INFO_UPDATE_LOC();
2429 Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1));
2430 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002431
Nicolas Capens157ba262019-12-10 17:49:14 -05002432 return val;
2433}
John Bauman89401822014-05-06 15:04:28 -04002434
John Bauman19bac1e2014-05-06 15:23:49 -04002435// RValue<UInt> RoundUInt(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04002436// {
Logan Chiene3191012018-08-24 22:01:50 +08002437//#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002438// return x86::cvtss2si(val); // FIXME: Unsigned
Logan Chiene3191012018-08-24 22:01:50 +08002439//#else
2440// return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f));
2441//#endif
John Bauman89401822014-05-06 15:04:28 -04002442// }
2443
Nicolas Capens519cf222020-05-08 15:27:19 -04002444Type *UInt::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002445{
Nicolas Capens567e5602021-01-07 23:18:56 -05002446 return T(llvm::Type::getInt32Ty(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05002447}
John Bauman89401822014-05-06 15:04:28 -04002448
John Bauman19bac1e2014-05-06 15:23:49 -04002449// Int2::Int2(RValue<Int> cast)
2450// {
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002451// Value *extend = Nucleus::createZExt(cast.value(), Long::type());
Nicolas Capens519cf222020-05-08 15:27:19 -04002452// Value *vector = Nucleus::createBitCast(extend, Int2::type());
John Bauman66b8ab22014-05-06 15:57:45 -04002453//
Nicolas Capense89cd582016-09-30 14:23:47 -04002454// int shuffle[2] = {0, 0};
2455// Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle);
John Bauman19bac1e2014-05-06 15:23:49 -04002456//
John Bauman66b8ab22014-05-06 15:57:45 -04002457// storeValue(replicate);
John Bauman19bac1e2014-05-06 15:23:49 -04002458// }
John Bauman89401822014-05-06 15:04:28 -04002459
Nicolas Capens157ba262019-12-10 17:49:14 -05002460RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs)
2461{
2462 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002463#if defined(__i386__) || defined(__x86_64__)
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002464 // return RValue<Int2>(Nucleus::createShl(lhs.value(), rhs.value()));
John Bauman89401822014-05-06 15:04:28 -04002465
Nicolas Capens157ba262019-12-10 17:49:14 -05002466 return x86::pslld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002467#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002468 return As<Int2>(V(lowerVectorShl(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002469#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002470}
John Bauman89401822014-05-06 15:04:28 -04002471
Nicolas Capens157ba262019-12-10 17:49:14 -05002472RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs)
2473{
2474 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002475#if defined(__i386__) || defined(__x86_64__)
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002476 // return RValue<Int2>(Nucleus::createAShr(lhs.value(), rhs.value()));
John Bauman89401822014-05-06 15:04:28 -04002477
Nicolas Capens157ba262019-12-10 17:49:14 -05002478 return x86::psrad(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002479#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002480 return As<Int2>(V(lowerVectorAShr(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002481#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002482}
John Bauman89401822014-05-06 15:04:28 -04002483
Nicolas Capens519cf222020-05-08 15:27:19 -04002484Type *Int2::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002485{
2486 return T(Type_v2i32);
2487}
John Bauman89401822014-05-06 15:04:28 -04002488
Nicolas Capens157ba262019-12-10 17:49:14 -05002489RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs)
2490{
2491 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002492#if defined(__i386__) || defined(__x86_64__)
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002493 // return RValue<UInt2>(Nucleus::createShl(lhs.value(), rhs.value()));
John Bauman89401822014-05-06 15:04:28 -04002494
Nicolas Capens157ba262019-12-10 17:49:14 -05002495 return As<UInt2>(x86::pslld(As<Int2>(lhs), rhs));
Logan Chiene3191012018-08-24 22:01:50 +08002496#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002497 return As<UInt2>(V(lowerVectorShl(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002498#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002499}
John Bauman89401822014-05-06 15:04:28 -04002500
Nicolas Capens157ba262019-12-10 17:49:14 -05002501RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs)
2502{
2503 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002504#if defined(__i386__) || defined(__x86_64__)
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002505 // return RValue<UInt2>(Nucleus::createLShr(lhs.value(), rhs.value()));
John Bauman89401822014-05-06 15:04:28 -04002506
Nicolas Capens157ba262019-12-10 17:49:14 -05002507 return x86::psrld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002508#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002509 return As<UInt2>(V(lowerVectorLShr(V(lhs.value()), rhs)));
Logan Chiene3191012018-08-24 22:01:50 +08002510#endif
Nicolas Capens157ba262019-12-10 17:49:14 -05002511}
John Bauman89401822014-05-06 15:04:28 -04002512
Nicolas Capens519cf222020-05-08 15:27:19 -04002513Type *UInt2::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002514{
2515 return T(Type_v2i32);
2516}
John Bauman89401822014-05-06 15:04:28 -04002517
Ben Clayton713b8d32019-12-17 20:37:56 +00002518Int4::Int4(RValue<Byte4> cast)
2519 : XYZW(this)
Nicolas Capens157ba262019-12-10 17:49:14 -05002520{
2521 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens4e7d3102022-06-21 01:42:18 -04002522 std::vector<int> swizzle = { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 };
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +01002523 Value *a = Nucleus::createBitCast(cast.value(), Byte16::type());
2524 Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::type()), swizzle);
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002525
Nicolas Capens4e7d3102022-06-21 01:42:18 -04002526 std::vector<int> swizzle2 = { 0, 8, 1, 9, 2, 10, 3, 11 };
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +01002527 Value *c = Nucleus::createBitCast(b, Short8::type());
2528 Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::type()), swizzle2);
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002529
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +01002530 *this = As<Int4>(d);
John Bauman89401822014-05-06 15:04:28 -04002531}
2532
Ben Clayton713b8d32019-12-17 20:37:56 +00002533Int4::Int4(RValue<SByte4> cast)
2534 : XYZW(this)
John Bauman89401822014-05-06 15:04:28 -04002535{
Nicolas Capens157ba262019-12-10 17:49:14 -05002536 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens4e7d3102022-06-21 01:42:18 -04002537 std::vector<int> swizzle = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +01002538 Value *a = Nucleus::createBitCast(cast.value(), Byte16::type());
2539 Value *b = Nucleus::createShuffleVector(a, a, swizzle);
Nicolas Capens157ba262019-12-10 17:49:14 -05002540
Nicolas Capens4e7d3102022-06-21 01:42:18 -04002541 std::vector<int> swizzle2 = { 0, 0, 1, 1, 2, 2, 3, 3 };
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +01002542 Value *c = Nucleus::createBitCast(b, Short8::type());
2543 Value *d = Nucleus::createShuffleVector(c, c, swizzle2);
Nicolas Capens157ba262019-12-10 17:49:14 -05002544
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +01002545 *this = As<Int4>(d) >> 24;
Nicolas Capens157ba262019-12-10 17:49:14 -05002546}
2547
Ben Clayton713b8d32019-12-17 20:37:56 +00002548Int4::Int4(RValue<Short4> cast)
2549 : XYZW(this)
Nicolas Capens157ba262019-12-10 17:49:14 -05002550{
2551 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens4e7d3102022-06-21 01:42:18 -04002552 std::vector<int> swizzle = { 0, 0, 1, 1, 2, 2, 3, 3 };
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +01002553 Value *c = Nucleus::createShuffleVector(cast.value(), cast.value(), swizzle);
2554 *this = As<Int4>(c) >> 16;
Nicolas Capens157ba262019-12-10 17:49:14 -05002555}
2556
Ben Clayton713b8d32019-12-17 20:37:56 +00002557Int4::Int4(RValue<UShort4> cast)
2558 : XYZW(this)
Nicolas Capens157ba262019-12-10 17:49:14 -05002559{
2560 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens4e7d3102022-06-21 01:42:18 -04002561 std::vector<int> swizzle = { 0, 8, 1, 9, 2, 10, 3, 11 };
Benjamin Kramer8ccc63f2022-02-08 12:54:51 +01002562 Value *c = Nucleus::createShuffleVector(cast.value(), Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle);
2563 *this = As<Int4>(c);
Nicolas Capens157ba262019-12-10 17:49:14 -05002564}
2565
Ben Clayton713b8d32019-12-17 20:37:56 +00002566Int4::Int4(RValue<Int> rhs)
2567 : XYZW(this)
Nicolas Capens157ba262019-12-10 17:49:14 -05002568{
2569 RR_DEBUG_INFO_UPDATE_LOC();
2570 Value *vector = loadValue();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002571 Value *insert = Nucleus::createInsertElement(vector, rhs.value(), 0);
Nicolas Capens157ba262019-12-10 17:49:14 -05002572
Nicolas Capens4e7d3102022-06-21 01:42:18 -04002573 std::vector<int> swizzle = { 0, 0, 0, 0 };
Nicolas Capens157ba262019-12-10 17:49:14 -05002574 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
2575
2576 storeValue(replicate);
2577}
2578
2579RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs)
2580{
2581 RR_DEBUG_INFO_UPDATE_LOC();
2582#if defined(__i386__) || defined(__x86_64__)
2583 return x86::pslld(lhs, rhs);
2584#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002585 return As<Int4>(V(lowerVectorShl(V(lhs.value()), rhs)));
Nicolas Capens157ba262019-12-10 17:49:14 -05002586#endif
2587}
2588
2589RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs)
2590{
2591 RR_DEBUG_INFO_UPDATE_LOC();
2592#if defined(__i386__) || defined(__x86_64__)
2593 return x86::psrad(lhs, rhs);
2594#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002595 return As<Int4>(V(lowerVectorAShr(V(lhs.value()), rhs)));
Nicolas Capens157ba262019-12-10 17:49:14 -05002596#endif
2597}
2598
2599RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y)
2600{
2601 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002602 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002603}
2604
2605RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y)
2606{
2607 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002608 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002609}
2610
2611RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y)
2612{
2613 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002614 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002615}
2616
2617RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y)
2618{
2619 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002620 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002621}
2622
2623RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y)
2624{
2625 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002626 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002627}
2628
2629RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y)
2630{
2631 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002632 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002633}
2634
Nicolas Capens629bf952022-01-18 15:08:14 -05002635RValue<Int4> Abs(RValue<Int4> x)
2636{
2637#if LLVM_VERSION_MAJOR >= 12
2638 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::abs, { V(x.value())->getType() });
2639 return RValue<Int4>(V(jit->builder->CreateCall(func, { V(x.value()), llvm::ConstantInt::getFalse(*jit->context) })));
2640#else
2641 auto negative = x >> 31;
2642 return (x ^ negative) - negative;
2643#endif
2644}
2645
Nicolas Capens157ba262019-12-10 17:49:14 -05002646RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y)
2647{
2648 RR_DEBUG_INFO_UPDATE_LOC();
2649#if defined(__i386__) || defined(__x86_64__)
2650 if(CPUID::supportsSSE4_1())
2651 {
2652 return x86::pmaxsd(x, y);
2653 }
2654 else
2655#endif
2656 {
2657 RValue<Int4> greater = CmpNLE(x, y);
2658 return (x & greater) | (y & ~greater);
2659 }
2660}
2661
2662RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y)
2663{
2664 RR_DEBUG_INFO_UPDATE_LOC();
2665#if defined(__i386__) || defined(__x86_64__)
2666 if(CPUID::supportsSSE4_1())
2667 {
2668 return x86::pminsd(x, y);
2669 }
2670 else
2671#endif
2672 {
2673 RValue<Int4> less = CmpLT(x, y);
2674 return (x & less) | (y & ~less);
2675 }
2676}
2677
2678RValue<Int4> RoundInt(RValue<Float4> cast)
2679{
2680 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens1ca66982022-05-04 17:19:46 -04002681#if(defined(__i386__) || defined(__x86_64__)) && !__has_feature(memory_sanitizer)
Nicolas Capens157ba262019-12-10 17:49:14 -05002682 return x86::cvtps2dq(cast);
2683#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002684 return As<Int4>(V(lowerRoundInt(V(cast.value()), T(Int4::type()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05002685#endif
2686}
2687
Nicolas Capenseeb81842021-01-12 17:44:40 -05002688RValue<Int4> RoundIntClamped(RValue<Float4> cast)
2689{
2690 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens4f504b02022-02-10 14:13:50 -05002691
2692// TODO(b/165000222): Check if fptosi_sat produces optimal code for x86 and ARM.
Nicolas Capens1ca66982022-05-04 17:19:46 -04002693#if(defined(__i386__) || defined(__x86_64__)) && !__has_feature(memory_sanitizer)
Nicolas Capenseeb81842021-01-12 17:44:40 -05002694 // cvtps2dq produces 0x80000000, a negative value, for input larger than
2695 // 2147483520.0, so clamp to 2147483520. Values less than -2147483520.0
2696 // saturate to 0x80000000.
2697 return x86::cvtps2dq(Min(cast, Float4(0x7FFFFF80)));
Nicolas Capens4f504b02022-02-10 14:13:50 -05002698#elif defined(__arm__) || defined(__aarch64__)
Nicolas Capenseeb81842021-01-12 17:44:40 -05002699 // ARM saturates to the largest positive or negative integer. Unit tests
2700 // verify that lowerRoundInt() behaves as desired.
2701 return As<Int4>(V(lowerRoundInt(V(cast.value()), T(Int4::type()))));
Nicolas Capens4f504b02022-02-10 14:13:50 -05002702#elif LLVM_VERSION_MAJOR >= 14
2703 llvm::Value *rounded = lowerRound(V(cast.value()));
2704 llvm::Function *fptosi_sat = llvm::Intrinsic::getDeclaration(
2705 jit->module.get(), llvm::Intrinsic::fptosi_sat, { T(Int4::type()), T(Float4::type()) });
2706 return RValue<Int4>(V(jit->builder->CreateCall(fptosi_sat, { rounded })));
2707#else
Nicolas Capens1ca66982022-05-04 17:19:46 -04002708 RValue<Float4> clamped = Max(Min(cast, Float4(0x7FFFFF80)), Float4(static_cast<int>(0x80000000)));
Nicolas Capens4f504b02022-02-10 14:13:50 -05002709 return As<Int4>(V(lowerRoundInt(V(clamped.value()), T(Int4::type()))));
Nicolas Capenseeb81842021-01-12 17:44:40 -05002710#endif
2711}
2712
Nicolas Capens157ba262019-12-10 17:49:14 -05002713RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y)
2714{
2715 RR_DEBUG_INFO_UPDATE_LOC();
2716 // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq.
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002717 return As<Int4>(V(lowerMulHigh(V(x.value()), V(y.value()), true)));
Nicolas Capens157ba262019-12-10 17:49:14 -05002718}
2719
2720RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y)
2721{
2722 RR_DEBUG_INFO_UPDATE_LOC();
2723 // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq.
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002724 return As<UInt4>(V(lowerMulHigh(V(x.value()), V(y.value()), false)));
Nicolas Capens157ba262019-12-10 17:49:14 -05002725}
2726
2727RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y)
2728{
2729 RR_DEBUG_INFO_UPDATE_LOC();
2730#if defined(__i386__) || defined(__x86_64__)
2731 return x86::packssdw(x, y);
2732#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002733 return As<Short8>(V(lowerPack(V(x.value()), V(y.value()), true)));
Nicolas Capens157ba262019-12-10 17:49:14 -05002734#endif
2735}
2736
2737RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y)
2738{
2739 RR_DEBUG_INFO_UPDATE_LOC();
2740#if defined(__i386__) || defined(__x86_64__)
2741 return x86::packusdw(x, y);
2742#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002743 return As<UShort8>(V(lowerPack(V(x.value()), V(y.value()), false)));
Nicolas Capens157ba262019-12-10 17:49:14 -05002744#endif
2745}
2746
2747RValue<Int> SignMask(RValue<Int4> x)
2748{
2749 RR_DEBUG_INFO_UPDATE_LOC();
2750#if defined(__i386__) || defined(__x86_64__)
2751 return x86::movmskps(As<Float4>(x));
2752#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002753 return As<Int>(V(lowerSignMask(V(x.value()), T(Int::type()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05002754#endif
2755}
2756
Nicolas Capens519cf222020-05-08 15:27:19 -04002757Type *Int4::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002758{
Nicolas Capense7cd2242020-08-17 10:49:56 -04002759 return T(llvm::VectorType::get(T(Int::type()), 4, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05002760}
2761
Ben Clayton713b8d32019-12-17 20:37:56 +00002762UInt4::UInt4(RValue<Float4> cast)
2763 : XYZW(this)
Nicolas Capens157ba262019-12-10 17:49:14 -05002764{
2765 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002766 Value *xyzw = Nucleus::createFPToUI(cast.value(), UInt4::type());
Nicolas Capens157ba262019-12-10 17:49:14 -05002767 storeValue(xyzw);
2768}
2769
Ben Clayton713b8d32019-12-17 20:37:56 +00002770UInt4::UInt4(RValue<UInt> rhs)
2771 : XYZW(this)
Nicolas Capens157ba262019-12-10 17:49:14 -05002772{
2773 RR_DEBUG_INFO_UPDATE_LOC();
2774 Value *vector = loadValue();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002775 Value *insert = Nucleus::createInsertElement(vector, rhs.value(), 0);
Nicolas Capens157ba262019-12-10 17:49:14 -05002776
Nicolas Capens4e7d3102022-06-21 01:42:18 -04002777 std::vector<int> swizzle = { 0, 0, 0, 0 };
Nicolas Capens157ba262019-12-10 17:49:14 -05002778 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
2779
2780 storeValue(replicate);
2781}
2782
2783RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs)
2784{
2785 RR_DEBUG_INFO_UPDATE_LOC();
2786#if defined(__i386__) || defined(__x86_64__)
2787 return As<UInt4>(x86::pslld(As<Int4>(lhs), rhs));
2788#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002789 return As<UInt4>(V(lowerVectorShl(V(lhs.value()), rhs)));
Nicolas Capens157ba262019-12-10 17:49:14 -05002790#endif
2791}
2792
2793RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs)
2794{
2795 RR_DEBUG_INFO_UPDATE_LOC();
2796#if defined(__i386__) || defined(__x86_64__)
2797 return x86::psrld(lhs, rhs);
2798#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002799 return As<UInt4>(V(lowerVectorLShr(V(lhs.value()), rhs)));
Nicolas Capens157ba262019-12-10 17:49:14 -05002800#endif
2801}
2802
2803RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y)
2804{
2805 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002806 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002807}
2808
2809RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y)
2810{
2811 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002812 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULT(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002813}
2814
2815RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y)
2816{
2817 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002818 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002819}
2820
2821RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y)
2822{
2823 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002824 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002825}
2826
2827RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y)
2828{
2829 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002830 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002831}
2832
2833RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y)
2834{
2835 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002836 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05002837}
2838
2839RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y)
2840{
2841 RR_DEBUG_INFO_UPDATE_LOC();
2842#if defined(__i386__) || defined(__x86_64__)
2843 if(CPUID::supportsSSE4_1())
2844 {
2845 return x86::pmaxud(x, y);
2846 }
2847 else
2848#endif
2849 {
2850 RValue<UInt4> greater = CmpNLE(x, y);
2851 return (x & greater) | (y & ~greater);
2852 }
2853}
2854
2855RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y)
2856{
2857 RR_DEBUG_INFO_UPDATE_LOC();
2858#if defined(__i386__) || defined(__x86_64__)
2859 if(CPUID::supportsSSE4_1())
2860 {
2861 return x86::pminud(x, y);
2862 }
2863 else
2864#endif
2865 {
2866 RValue<UInt4> less = CmpLT(x, y);
2867 return (x & less) | (y & ~less);
2868 }
2869}
2870
Nicolas Capens519cf222020-05-08 15:27:19 -04002871Type *UInt4::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002872{
Nicolas Capense7cd2242020-08-17 10:49:56 -04002873 return T(llvm::VectorType::get(T(UInt::type()), 4, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05002874}
2875
Nicolas Capens519cf222020-05-08 15:27:19 -04002876Type *Half::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05002877{
Nicolas Capens567e5602021-01-07 23:18:56 -05002878 return T(llvm::Type::getInt16Ty(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05002879}
2880
Antonio Maioranod1561872020-12-14 14:03:53 -05002881bool HasRcpApprox()
2882{
2883#if defined(__i386__) || defined(__x86_64__)
2884 return true;
2885#else
2886 return false;
2887#endif
2888}
2889
2890RValue<Float4> RcpApprox(RValue<Float4> x, bool exactAtPow2)
2891{
2892#if defined(__i386__) || defined(__x86_64__)
2893 if(exactAtPow2)
2894 {
2895 // rcpps uses a piecewise-linear approximation which minimizes the relative error
2896 // but is not exact at power-of-two values. Rectify by multiplying by the inverse.
2897 return x86::rcpps(x) * Float4(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f))));
2898 }
2899 return x86::rcpps(x);
2900#else
2901 UNREACHABLE("RValue<Float4> RcpApprox() not available on this platform");
2902 return { 0.0f };
2903#endif
2904}
2905
2906RValue<Float> RcpApprox(RValue<Float> x, bool exactAtPow2)
2907{
2908#if defined(__i386__) || defined(__x86_64__)
2909 if(exactAtPow2)
2910 {
2911 // rcpss uses a piecewise-linear approximation which minimizes the relative error
2912 // but is not exact at power-of-two values. Rectify by multiplying by the inverse.
2913 return x86::rcpss(x) * Float(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f))));
2914 }
2915 return x86::rcpss(x);
2916#else
2917 UNREACHABLE("RValue<Float4> RcpApprox() not available on this platform");
2918 return { 0.0f };
2919#endif
2920}
2921
Antonio Maiorano1cc5b332020-12-14 16:57:28 -05002922bool HasRcpSqrtApprox()
2923{
2924#if defined(__i386__) || defined(__x86_64__)
2925 return true;
2926#else
2927 return false;
2928#endif
2929}
2930
2931RValue<Float4> RcpSqrtApprox(RValue<Float4> x)
2932{
2933#if defined(__i386__) || defined(__x86_64__)
2934 return x86::rsqrtps(x);
2935#else
2936 UNREACHABLE("RValue<Float4> RcpSqrtApprox() not available on this platform");
2937 return { 0.0f };
2938#endif
2939}
2940
2941RValue<Float> RcpSqrtApprox(RValue<Float> x)
2942{
2943#if defined(__i386__) || defined(__x86_64__)
2944 return x86::rsqrtss(x);
2945#else
2946 UNREACHABLE("RValue<Float4> RcpSqrtApprox() not available on this platform");
2947 return { 0.0f };
2948#endif
2949}
2950
Nicolas Capens157ba262019-12-10 17:49:14 -05002951RValue<Float> Sqrt(RValue<Float> x)
2952{
2953 RR_DEBUG_INFO_UPDATE_LOC();
2954#if defined(__i386__) || defined(__x86_64__)
2955 return x86::sqrtss(x);
2956#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002957 return As<Float>(V(lowerSQRT(V(x.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05002958#endif
2959}
2960
2961RValue<Float> Round(RValue<Float> x)
2962{
2963 RR_DEBUG_INFO_UPDATE_LOC();
2964#if defined(__i386__) || defined(__x86_64__)
2965 if(CPUID::supportsSSE4_1())
2966 {
2967 return x86::roundss(x, 0);
2968 }
2969 else
2970 {
2971 return Float4(Round(Float4(x))).x;
2972 }
2973#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002974 return RValue<Float>(V(lowerRound(V(x.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05002975#endif
2976}
2977
2978RValue<Float> Trunc(RValue<Float> x)
2979{
2980 RR_DEBUG_INFO_UPDATE_LOC();
2981#if defined(__i386__) || defined(__x86_64__)
2982 if(CPUID::supportsSSE4_1())
2983 {
2984 return x86::roundss(x, 3);
2985 }
2986 else
2987 {
Ben Clayton713b8d32019-12-17 20:37:56 +00002988 return Float(Int(x)); // Rounded toward zero
Nicolas Capens157ba262019-12-10 17:49:14 -05002989 }
2990#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04002991 return RValue<Float>(V(lowerTrunc(V(x.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05002992#endif
2993}
2994
2995RValue<Float> Frac(RValue<Float> x)
2996{
2997 RR_DEBUG_INFO_UPDATE_LOC();
2998#if defined(__i386__) || defined(__x86_64__)
2999 if(CPUID::supportsSSE4_1())
3000 {
3001 return x - x86::floorss(x);
3002 }
3003 else
3004 {
3005 return Float4(Frac(Float4(x))).x;
3006 }
3007#else
3008 // x - floor(x) can be 1.0 for very small negative x.
3009 // Clamp against the value just below 1.0.
3010 return Min(x - Floor(x), As<Float>(Int(0x3F7FFFFF)));
3011#endif
3012}
3013
3014RValue<Float> Floor(RValue<Float> x)
3015{
3016 RR_DEBUG_INFO_UPDATE_LOC();
3017#if defined(__i386__) || defined(__x86_64__)
3018 if(CPUID::supportsSSE4_1())
3019 {
3020 return x86::floorss(x);
3021 }
3022 else
3023 {
3024 return Float4(Floor(Float4(x))).x;
3025 }
3026#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003027 return RValue<Float>(V(lowerFloor(V(x.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003028#endif
3029}
3030
3031RValue<Float> Ceil(RValue<Float> x)
3032{
3033 RR_DEBUG_INFO_UPDATE_LOC();
3034#if defined(__i386__) || defined(__x86_64__)
3035 if(CPUID::supportsSSE4_1())
3036 {
3037 return x86::ceilss(x);
3038 }
3039 else
3040#endif
3041 {
3042 return Float4(Ceil(Float4(x))).x;
3043 }
3044}
3045
Nicolas Capens519cf222020-05-08 15:27:19 -04003046Type *Float::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05003047{
Nicolas Capens567e5602021-01-07 23:18:56 -05003048 return T(llvm::Type::getFloatTy(*jit->context));
Nicolas Capens157ba262019-12-10 17:49:14 -05003049}
3050
Nicolas Capens519cf222020-05-08 15:27:19 -04003051Type *Float2::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05003052{
3053 return T(Type_v2f32);
3054}
3055
Ben Clayton713b8d32019-12-17 20:37:56 +00003056Float4::Float4(RValue<Float> rhs)
3057 : XYZW(this)
Nicolas Capens157ba262019-12-10 17:49:14 -05003058{
3059 RR_DEBUG_INFO_UPDATE_LOC();
3060 Value *vector = loadValue();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003061 Value *insert = Nucleus::createInsertElement(vector, rhs.value(), 0);
Nicolas Capens157ba262019-12-10 17:49:14 -05003062
Nicolas Capens4e7d3102022-06-21 01:42:18 -04003063 std::vector<int> swizzle = { 0, 0, 0, 0 };
Nicolas Capens157ba262019-12-10 17:49:14 -05003064 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
3065
3066 storeValue(replicate);
3067}
3068
Nicolas Capensbc74bc22022-01-26 10:47:00 -05003069RValue<Float4> MulAdd(RValue<Float4> x, RValue<Float4> y, RValue<Float4> z)
3070{
3071 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::fmuladd, { T(Float4::type()) });
3072 return RValue<Float4>(V(jit->builder->CreateCall(func, { V(x.value()), V(y.value()), V(z.value()) })));
3073}
3074
Nicolas Capens75d79f22022-01-31 17:46:26 -05003075RValue<Float4> FMA(RValue<Float4> x, RValue<Float4> y, RValue<Float4> z)
3076{
3077 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::fma, { T(Float4::type()) });
3078 return RValue<Float4>(V(jit->builder->CreateCall(func, { V(x.value()), V(y.value()), V(z.value()) })));
3079}
3080
Nicolas Capens629bf952022-01-18 15:08:14 -05003081RValue<Float4> Abs(RValue<Float4> x)
3082{
3083 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::fabs, { V(x.value())->getType() });
3084 return RValue<Float4>(V(jit->builder->CreateCall(func, V(x.value()))));
3085}
3086
Nicolas Capens157ba262019-12-10 17:49:14 -05003087RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y)
3088{
3089 RR_DEBUG_INFO_UPDATE_LOC();
3090#if defined(__i386__) || defined(__x86_64__)
3091 return x86::maxps(x, y);
3092#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003093 return As<Float4>(V(lowerPFMINMAX(V(x.value()), V(y.value()), llvm::FCmpInst::FCMP_OGT)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003094#endif
3095}
3096
3097RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y)
3098{
3099 RR_DEBUG_INFO_UPDATE_LOC();
3100#if defined(__i386__) || defined(__x86_64__)
3101 return x86::minps(x, y);
3102#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003103 return As<Float4>(V(lowerPFMINMAX(V(x.value()), V(y.value()), llvm::FCmpInst::FCMP_OLT)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003104#endif
3105}
3106
Nicolas Capens157ba262019-12-10 17:49:14 -05003107RValue<Float4> Sqrt(RValue<Float4> x)
3108{
3109 RR_DEBUG_INFO_UPDATE_LOC();
3110#if defined(__i386__) || defined(__x86_64__)
3111 return x86::sqrtps(x);
3112#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003113 return As<Float4>(V(lowerSQRT(V(x.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003114#endif
3115}
3116
3117RValue<Int> SignMask(RValue<Float4> x)
3118{
3119 RR_DEBUG_INFO_UPDATE_LOC();
3120#if defined(__i386__) || defined(__x86_64__)
3121 return x86::movmskps(x);
3122#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003123 return As<Int>(V(lowerFPSignMask(V(x.value()), T(Int::type()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003124#endif
3125}
3126
3127RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y)
3128{
3129 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton713b8d32019-12-17 20:37:56 +00003130 // return As<Int4>(x86::cmpeqps(x, y));
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003131 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003132}
3133
3134RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y)
3135{
3136 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton713b8d32019-12-17 20:37:56 +00003137 // return As<Int4>(x86::cmpltps(x, y));
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003138 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003139}
3140
3141RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y)
3142{
3143 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton713b8d32019-12-17 20:37:56 +00003144 // return As<Int4>(x86::cmpleps(x, y));
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003145 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003146}
3147
3148RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y)
3149{
3150 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton713b8d32019-12-17 20:37:56 +00003151 // return As<Int4>(x86::cmpneqps(x, y));
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003152 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003153}
3154
3155RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y)
3156{
3157 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton713b8d32019-12-17 20:37:56 +00003158 // return As<Int4>(x86::cmpnltps(x, y));
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003159 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003160}
3161
3162RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y)
3163{
3164 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton713b8d32019-12-17 20:37:56 +00003165 // return As<Int4>(x86::cmpnleps(x, y));
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003166 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003167}
3168
3169RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y)
3170{
3171 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003172 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUEQ(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003173}
3174
3175RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y)
3176{
3177 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003178 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULT(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003179}
3180
3181RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y)
3182{
3183 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003184 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003185}
3186
3187RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y)
3188{
3189 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003190 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUNE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003191}
3192
3193RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y)
3194{
3195 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003196 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGE(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003197}
3198
3199RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y)
3200{
3201 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003202 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value(), y.value()), Int4::type()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003203}
3204
3205RValue<Float4> Round(RValue<Float4> x)
3206{
3207 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens00efa192022-05-05 08:58:28 -04003208#if(defined(__i386__) || defined(__x86_64__)) && !__has_feature(memory_sanitizer)
Nicolas Capens157ba262019-12-10 17:49:14 -05003209 if(CPUID::supportsSSE4_1())
3210 {
3211 return x86::roundps(x, 0);
3212 }
3213 else
3214 {
3215 return Float4(RoundInt(x));
3216 }
3217#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003218 return RValue<Float4>(V(lowerRound(V(x.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003219#endif
3220}
3221
3222RValue<Float4> Trunc(RValue<Float4> x)
3223{
3224 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens00efa192022-05-05 08:58:28 -04003225#if(defined(__i386__) || defined(__x86_64__)) && !__has_feature(memory_sanitizer)
Nicolas Capens157ba262019-12-10 17:49:14 -05003226 if(CPUID::supportsSSE4_1())
3227 {
3228 return x86::roundps(x, 3);
3229 }
3230 else
3231 {
3232 return Float4(Int4(x));
3233 }
3234#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003235 return RValue<Float4>(V(lowerTrunc(V(x.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003236#endif
3237}
3238
3239RValue<Float4> Frac(RValue<Float4> x)
3240{
3241 RR_DEBUG_INFO_UPDATE_LOC();
3242 Float4 frc;
3243
Nicolas Capens00efa192022-05-05 08:58:28 -04003244#if(defined(__i386__) || defined(__x86_64__)) && !__has_feature(memory_sanitizer)
Nicolas Capens157ba262019-12-10 17:49:14 -05003245 if(CPUID::supportsSSE4_1())
3246 {
Nicolas Capensc4d054c2021-05-12 14:19:45 -04003247 frc = x - x86::floorps(x);
Nicolas Capens157ba262019-12-10 17:49:14 -05003248 }
3249 else
3250 {
Ben Clayton713b8d32019-12-17 20:37:56 +00003251 frc = x - Float4(Int4(x)); // Signed fractional part.
Nicolas Capens157ba262019-12-10 17:49:14 -05003252
Ben Clayton713b8d32019-12-17 20:37:56 +00003253 frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1.0f))); // Add 1.0 if negative.
Nicolas Capens157ba262019-12-10 17:49:14 -05003254 }
3255#else
3256 frc = x - Floor(x);
3257#endif
3258
3259 // x - floor(x) can be 1.0 for very small negative x.
3260 // Clamp against the value just below 1.0.
3261 return Min(frc, As<Float4>(Int4(0x3F7FFFFF)));
3262}
3263
3264RValue<Float4> Floor(RValue<Float4> x)
3265{
3266 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens00efa192022-05-05 08:58:28 -04003267#if(defined(__i386__) || defined(__x86_64__)) && !__has_feature(memory_sanitizer)
Nicolas Capens157ba262019-12-10 17:49:14 -05003268 if(CPUID::supportsSSE4_1())
3269 {
3270 return x86::floorps(x);
3271 }
3272 else
3273 {
3274 return x - Frac(x);
3275 }
3276#else
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003277 return RValue<Float4>(V(lowerFloor(V(x.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003278#endif
3279}
3280
3281RValue<Float4> Ceil(RValue<Float4> x)
3282{
3283 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens00efa192022-05-05 08:58:28 -04003284#if(defined(__i386__) || defined(__x86_64__)) && !__has_feature(memory_sanitizer)
Nicolas Capens157ba262019-12-10 17:49:14 -05003285 if(CPUID::supportsSSE4_1())
3286 {
3287 return x86::ceilps(x);
3288 }
3289 else
3290#endif
3291 {
3292 return -Floor(-x);
3293 }
3294}
3295
Nicolas Capens157ba262019-12-10 17:49:14 -05003296RValue<UInt> Ctlz(RValue<UInt> v, bool isZeroUndef)
3297{
Antonio Maioranoaae33732020-02-14 14:52:34 -05003298 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens519cf222020-05-08 15:27:19 -04003299 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::ctlz, { T(UInt::type()) });
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003300 return RValue<UInt>(V(jit->builder->CreateCall(func, { V(v.value()),
Nicolas Capens567e5602021-01-07 23:18:56 -05003301 isZeroUndef ? llvm::ConstantInt::getTrue(*jit->context) : llvm::ConstantInt::getFalse(*jit->context) })));
Nicolas Capens157ba262019-12-10 17:49:14 -05003302}
3303
3304RValue<UInt4> Ctlz(RValue<UInt4> v, bool isZeroUndef)
3305{
Antonio Maioranoaae33732020-02-14 14:52:34 -05003306 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens519cf222020-05-08 15:27:19 -04003307 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::ctlz, { T(UInt4::type()) });
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003308 return RValue<UInt4>(V(jit->builder->CreateCall(func, { V(v.value()),
Nicolas Capens567e5602021-01-07 23:18:56 -05003309 isZeroUndef ? llvm::ConstantInt::getTrue(*jit->context) : llvm::ConstantInt::getFalse(*jit->context) })));
Nicolas Capens157ba262019-12-10 17:49:14 -05003310}
3311
3312RValue<UInt> Cttz(RValue<UInt> v, bool isZeroUndef)
3313{
Antonio Maioranoaae33732020-02-14 14:52:34 -05003314 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens519cf222020-05-08 15:27:19 -04003315 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cttz, { T(UInt::type()) });
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003316 return RValue<UInt>(V(jit->builder->CreateCall(func, { V(v.value()),
Nicolas Capens567e5602021-01-07 23:18:56 -05003317 isZeroUndef ? llvm::ConstantInt::getTrue(*jit->context) : llvm::ConstantInt::getFalse(*jit->context) })));
Nicolas Capens157ba262019-12-10 17:49:14 -05003318}
3319
3320RValue<UInt4> Cttz(RValue<UInt4> v, bool isZeroUndef)
3321{
Antonio Maioranoaae33732020-02-14 14:52:34 -05003322 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens519cf222020-05-08 15:27:19 -04003323 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cttz, { T(UInt4::type()) });
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003324 return RValue<UInt4>(V(jit->builder->CreateCall(func, { V(v.value()),
Nicolas Capens567e5602021-01-07 23:18:56 -05003325 isZeroUndef ? llvm::ConstantInt::getTrue(*jit->context) : llvm::ConstantInt::getFalse(*jit->context) })));
Nicolas Capens157ba262019-12-10 17:49:14 -05003326}
3327
Antonio Maiorano370cba52019-12-31 11:36:07 -05003328RValue<Int> MinAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder)
3329{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003330 return RValue<Int>(Nucleus::createAtomicMin(x.value(), y.value(), memoryOrder));
Antonio Maiorano370cba52019-12-31 11:36:07 -05003331}
3332
3333RValue<UInt> MinAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder)
3334{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003335 return RValue<UInt>(Nucleus::createAtomicUMin(x.value(), y.value(), memoryOrder));
Antonio Maiorano370cba52019-12-31 11:36:07 -05003336}
3337
3338RValue<Int> MaxAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder)
3339{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003340 return RValue<Int>(Nucleus::createAtomicMax(x.value(), y.value(), memoryOrder));
Antonio Maiorano370cba52019-12-31 11:36:07 -05003341}
3342
3343RValue<UInt> MaxAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder)
3344{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003345 return RValue<UInt>(Nucleus::createAtomicUMax(x.value(), y.value(), memoryOrder));
Antonio Maiorano370cba52019-12-31 11:36:07 -05003346}
3347
Nicolas Capens519cf222020-05-08 15:27:19 -04003348Type *Float4::type()
Nicolas Capens157ba262019-12-10 17:49:14 -05003349{
Nicolas Capense7cd2242020-08-17 10:49:56 -04003350 return T(llvm::VectorType::get(T(Float::type()), 4, false));
Nicolas Capens157ba262019-12-10 17:49:14 -05003351}
3352
3353RValue<Long> Ticks()
3354{
3355 RR_DEBUG_INFO_UPDATE_LOC();
3356 llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::readcyclecounter);
3357
3358 return RValue<Long>(V(jit->builder->CreateCall(rdtsc)));
3359}
3360
Nicolas Capens3d7faaa2022-10-04 14:48:57 -04003361RValue<Pointer<Byte>> ConstantPointer(const void *ptr)
Nicolas Capens157ba262019-12-10 17:49:14 -05003362{
Antonio Maioranoaae33732020-02-14 14:52:34 -05003363 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens157ba262019-12-10 17:49:14 -05003364 // Note: this should work for 32-bit pointers as well because 'inttoptr'
3365 // is defined to truncate (and zero extend) if necessary.
Nicolas Capens567e5602021-01-07 23:18:56 -05003366 auto ptrAsInt = llvm::ConstantInt::get(llvm::Type::getInt64Ty(*jit->context), reinterpret_cast<uintptr_t>(ptr));
Nicolas Capens519cf222020-05-08 15:27:19 -04003367 return RValue<Pointer<Byte>>(V(jit->builder->CreateIntToPtr(ptrAsInt, T(Pointer<Byte>::type()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003368}
3369
Nicolas Capens3d7faaa2022-10-04 14:48:57 -04003370RValue<Pointer<Byte>> ConstantData(const void *data, size_t size)
Nicolas Capens157ba262019-12-10 17:49:14 -05003371{
Antonio Maioranoaae33732020-02-14 14:52:34 -05003372 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens79b36b62020-01-30 11:23:30 -05003373 auto str = ::std::string(reinterpret_cast<const char *>(data), size);
Nicolas Capens157ba262019-12-10 17:49:14 -05003374 auto ptr = jit->builder->CreateGlobalStringPtr(str);
3375 return RValue<Pointer<Byte>>(V(ptr));
3376}
3377
Ben Clayton713b8d32019-12-17 20:37:56 +00003378Value *Call(RValue<Pointer<Byte>> fptr, Type *retTy, std::initializer_list<Value *> args, std::initializer_list<Type *> argTys)
Nicolas Capens157ba262019-12-10 17:49:14 -05003379{
Nicolas Capensaf907702021-05-14 11:10:49 -04003380 // If this is a MemorySanitizer build, but Reactor routine instrumentation is not enabled,
3381 // mark all call arguments as initialized by calling __msan_unpoison_param().
Nicolas Capensdc8cbfa2021-06-10 17:09:50 -04003382 if(__has_feature(memory_sanitizer) && !jit->msanInstrumentation)
Nicolas Capensaf907702021-05-14 11:10:49 -04003383 {
3384 // void __msan_unpoison_param(size_t n)
3385 auto voidTy = llvm::Type::getVoidTy(*jit->context);
3386 auto sizetTy = llvm::IntegerType::get(*jit->context, sizeof(size_t) * 8);
3387 auto funcTy = llvm::FunctionType::get(voidTy, { sizetTy }, false);
3388 auto func = jit->module->getOrInsertFunction("__msan_unpoison_param", funcTy);
3389
3390 jit->builder->CreateCall(func, { llvm::ConstantInt::get(sizetTy, args.size()) });
3391 }
3392
Antonio Maioranoaae33732020-02-14 14:52:34 -05003393 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003394 llvm::SmallVector<llvm::Type *, 8> paramTys;
Nicolas Capens81bc9d92019-12-16 15:05:57 -05003395 for(auto ty : argTys) { paramTys.push_back(T(ty)); }
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003396 auto funcTy = llvm::FunctionType::get(T(retTy), paramTys, false);
Nicolas Capens157ba262019-12-10 17:49:14 -05003397
3398 auto funcPtrTy = funcTy->getPointerTo();
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003399 auto funcPtr = jit->builder->CreatePointerCast(V(fptr.value()), funcPtrTy);
Nicolas Capens157ba262019-12-10 17:49:14 -05003400
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003401 llvm::SmallVector<llvm::Value *, 8> arguments;
Nicolas Capens81bc9d92019-12-16 15:05:57 -05003402 for(auto arg : args) { arguments.push_back(V(arg)); }
Ben Clayton9d2fd9c2020-04-11 15:17:05 +01003403 return V(jit->builder->CreateCall(funcTy, funcPtr, arguments));
Nicolas Capens157ba262019-12-10 17:49:14 -05003404}
3405
3406void Breakpoint()
3407{
Antonio Maioranoaae33732020-02-14 14:52:34 -05003408 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens157ba262019-12-10 17:49:14 -05003409 llvm::Function *debugtrap = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::debugtrap);
3410
3411 jit->builder->CreateCall(debugtrap);
3412}
3413
3414} // namespace rr
3415
3416namespace rr {
3417
3418#if defined(__i386__) || defined(__x86_64__)
3419namespace x86 {
3420
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003421// Differs from IRBuilder<>::CreateUnaryIntrinsic() in that it only accepts native instruction intrinsics which have
3422// implicit types, such as 'x86_sse_rcp_ps' operating on v4f32, while 'sqrt' requires explicitly specifying the operand type.
3423static Value *createInstruction(llvm::Intrinsic::ID id, Value *x)
3424{
3425 llvm::Function *intrinsic = llvm::Intrinsic::getDeclaration(jit->module.get(), id);
3426
3427 return V(jit->builder->CreateCall(intrinsic, V(x)));
3428}
3429
3430// Differs from IRBuilder<>::CreateBinaryIntrinsic() in that it only accepts native instruction intrinsics which have
3431// implicit types, such as 'x86_sse_max_ps' operating on v4f32, while 'sadd_sat' requires explicitly specifying the operand types.
3432static Value *createInstruction(llvm::Intrinsic::ID id, Value *x, Value *y)
3433{
3434 llvm::Function *intrinsic = llvm::Intrinsic::getDeclaration(jit->module.get(), id);
3435
3436 return V(jit->builder->CreateCall(intrinsic, { V(x), V(y) }));
3437}
3438
Nicolas Capens157ba262019-12-10 17:49:14 -05003439RValue<Int> cvtss2si(RValue<Float> val)
3440{
Nicolas Capens157ba262019-12-10 17:49:14 -05003441 Float4 vector;
3442 vector.x = val;
3443
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003444 return RValue<Int>(createInstruction(llvm::Intrinsic::x86_sse_cvtss2si, RValue<Float4>(vector).value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003445}
3446
3447RValue<Int4> cvtps2dq(RValue<Float4> val)
3448{
Nicolas Capens1ca66982022-05-04 17:19:46 -04003449 ASSERT(!__has_feature(memory_sanitizer)); // TODO(b/172238865): Not correctly instrumented by MemorySanitizer.
3450
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003451 return RValue<Int4>(createInstruction(llvm::Intrinsic::x86_sse2_cvtps2dq, val.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003452}
3453
3454RValue<Float> rcpss(RValue<Float> val)
3455{
Nicolas Capens82bbd6f2022-05-03 14:41:55 -04003456 Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::type()))), val.value(), 0);
Nicolas Capens157ba262019-12-10 17:49:14 -05003457
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003458 return RValue<Float>(Nucleus::createExtractElement(createInstruction(llvm::Intrinsic::x86_sse_rcp_ss, vector), Float::type(), 0));
Nicolas Capens157ba262019-12-10 17:49:14 -05003459}
3460
3461RValue<Float> sqrtss(RValue<Float> val)
3462{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003463 return RValue<Float>(V(jit->builder->CreateUnaryIntrinsic(llvm::Intrinsic::sqrt, V(val.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003464}
3465
3466RValue<Float> rsqrtss(RValue<Float> val)
3467{
Nicolas Capens82bbd6f2022-05-03 14:41:55 -04003468 Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::type()))), val.value(), 0);
Nicolas Capens157ba262019-12-10 17:49:14 -05003469
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003470 return RValue<Float>(Nucleus::createExtractElement(createInstruction(llvm::Intrinsic::x86_sse_rsqrt_ss, vector), Float::type(), 0));
Nicolas Capens157ba262019-12-10 17:49:14 -05003471}
3472
3473RValue<Float4> rcpps(RValue<Float4> val)
3474{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003475 return RValue<Float4>(createInstruction(llvm::Intrinsic::x86_sse_rcp_ps, val.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003476}
3477
3478RValue<Float4> sqrtps(RValue<Float4> val)
3479{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003480 return RValue<Float4>(V(jit->builder->CreateUnaryIntrinsic(llvm::Intrinsic::sqrt, V(val.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003481}
3482
3483RValue<Float4> rsqrtps(RValue<Float4> val)
3484{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003485 return RValue<Float4>(createInstruction(llvm::Intrinsic::x86_sse_rsqrt_ps, val.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003486}
3487
3488RValue<Float4> maxps(RValue<Float4> x, RValue<Float4> y)
3489{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003490 return RValue<Float4>(createInstruction(llvm::Intrinsic::x86_sse_max_ps, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003491}
3492
3493RValue<Float4> minps(RValue<Float4> x, RValue<Float4> y)
3494{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003495 return RValue<Float4>(createInstruction(llvm::Intrinsic::x86_sse_min_ps, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003496}
3497
3498RValue<Float> roundss(RValue<Float> val, unsigned char imm)
3499{
3500 llvm::Function *roundss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_round_ss);
3501
Nicolas Capens519cf222020-05-08 15:27:19 -04003502 Value *undef = V(llvm::UndefValue::get(T(Float4::type())));
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003503 Value *vector = Nucleus::createInsertElement(undef, val.value(), 0);
Nicolas Capens157ba262019-12-10 17:49:14 -05003504
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003505 return RValue<Float>(Nucleus::createExtractElement(V(jit->builder->CreateCall(roundss, { V(undef), V(vector), V(Nucleus::createConstantInt(imm)) })), Float::type(), 0));
Nicolas Capens157ba262019-12-10 17:49:14 -05003506}
3507
3508RValue<Float> floorss(RValue<Float> val)
3509{
3510 return roundss(val, 1);
3511}
3512
3513RValue<Float> ceilss(RValue<Float> val)
3514{
3515 return roundss(val, 2);
3516}
3517
3518RValue<Float4> roundps(RValue<Float4> val, unsigned char imm)
3519{
Nicolas Capens00efa192022-05-05 08:58:28 -04003520 ASSERT(!__has_feature(memory_sanitizer)); // TODO(b/172238865): Not correctly instrumented by MemorySanitizer.
3521
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003522 return RValue<Float4>(createInstruction(llvm::Intrinsic::x86_sse41_round_ps, val.value(), Nucleus::createConstantInt(imm)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003523}
3524
3525RValue<Float4> floorps(RValue<Float4> val)
3526{
3527 return roundps(val, 1);
3528}
3529
3530RValue<Float4> ceilps(RValue<Float4> val)
3531{
3532 return roundps(val, 2);
3533}
3534
Nicolas Capens157ba262019-12-10 17:49:14 -05003535RValue<Short4> paddsw(RValue<Short4> x, RValue<Short4> y)
3536{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003537 return As<Short4>(V(lowerPSADDSAT(V(x.value()), V(y.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003538}
3539
3540RValue<Short4> psubsw(RValue<Short4> x, RValue<Short4> y)
3541{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003542 return As<Short4>(V(lowerPSSUBSAT(V(x.value()), V(y.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003543}
3544
3545RValue<UShort4> paddusw(RValue<UShort4> x, RValue<UShort4> y)
3546{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003547 return As<UShort4>(V(lowerPUADDSAT(V(x.value()), V(y.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003548}
3549
3550RValue<UShort4> psubusw(RValue<UShort4> x, RValue<UShort4> y)
3551{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003552 return As<UShort4>(V(lowerPUSUBSAT(V(x.value()), V(y.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003553}
3554
3555RValue<SByte8> paddsb(RValue<SByte8> x, RValue<SByte8> y)
3556{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003557 return As<SByte8>(V(lowerPSADDSAT(V(x.value()), V(y.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003558}
3559
3560RValue<SByte8> psubsb(RValue<SByte8> x, RValue<SByte8> y)
3561{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003562 return As<SByte8>(V(lowerPSSUBSAT(V(x.value()), V(y.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003563}
3564
3565RValue<Byte8> paddusb(RValue<Byte8> x, RValue<Byte8> y)
3566{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003567 return As<Byte8>(V(lowerPUADDSAT(V(x.value()), V(y.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003568}
3569
3570RValue<Byte8> psubusb(RValue<Byte8> x, RValue<Byte8> y)
3571{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003572 return As<Byte8>(V(lowerPUSUBSAT(V(x.value()), V(y.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003573}
3574
3575RValue<UShort4> pavgw(RValue<UShort4> x, RValue<UShort4> y)
3576{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003577 return As<UShort4>(V(lowerPAVG(V(x.value()), V(y.value()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003578}
3579
3580RValue<Short4> pmaxsw(RValue<Short4> x, RValue<Short4> y)
3581{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003582 return As<Short4>(V(lowerPMINMAX(V(x.value()), V(y.value()), llvm::ICmpInst::ICMP_SGT)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003583}
3584
3585RValue<Short4> pminsw(RValue<Short4> x, RValue<Short4> y)
3586{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003587 return As<Short4>(V(lowerPMINMAX(V(x.value()), V(y.value()), llvm::ICmpInst::ICMP_SLT)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003588}
3589
3590RValue<Short4> pcmpgtw(RValue<Short4> x, RValue<Short4> y)
3591{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003592 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value()), V(y.value()), T(Short4::type()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003593}
3594
3595RValue<Short4> pcmpeqw(RValue<Short4> x, RValue<Short4> y)
3596{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003597 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value()), V(y.value()), T(Short4::type()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003598}
3599
3600RValue<Byte8> pcmpgtb(RValue<SByte8> x, RValue<SByte8> y)
3601{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003602 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value()), V(y.value()), T(Byte8::type()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003603}
3604
3605RValue<Byte8> pcmpeqb(RValue<Byte8> x, RValue<Byte8> y)
3606{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003607 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value()), V(y.value()), T(Byte8::type()))));
Nicolas Capens157ba262019-12-10 17:49:14 -05003608}
3609
3610RValue<Short4> packssdw(RValue<Int2> x, RValue<Int2> y)
3611{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003612 return As<Short4>(createInstruction(llvm::Intrinsic::x86_sse2_packssdw_128, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003613}
3614
3615RValue<Short8> packssdw(RValue<Int4> x, RValue<Int4> y)
3616{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003617 return RValue<Short8>(createInstruction(llvm::Intrinsic::x86_sse2_packssdw_128, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003618}
3619
3620RValue<SByte8> packsswb(RValue<Short4> x, RValue<Short4> y)
3621{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003622 return As<SByte8>(createInstruction(llvm::Intrinsic::x86_sse2_packsswb_128, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003623}
3624
3625RValue<Byte8> packuswb(RValue<Short4> x, RValue<Short4> y)
3626{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003627 return As<Byte8>(createInstruction(llvm::Intrinsic::x86_sse2_packuswb_128, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003628}
3629
3630RValue<UShort8> packusdw(RValue<Int4> x, RValue<Int4> y)
3631{
3632 if(CPUID::supportsSSE4_1())
3633 {
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003634 return RValue<UShort8>(createInstruction(llvm::Intrinsic::x86_sse41_packusdw, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003635 }
3636 else
3637 {
3638 RValue<Int4> bx = (x & ~(x >> 31)) - Int4(0x8000);
3639 RValue<Int4> by = (y & ~(y >> 31)) - Int4(0x8000);
3640
3641 return As<UShort8>(packssdw(bx, by) + Short8(0x8000u));
3642 }
3643}
3644
3645RValue<UShort4> psrlw(RValue<UShort4> x, unsigned char y)
3646{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003647 return As<UShort4>(createInstruction(llvm::Intrinsic::x86_sse2_psrli_w, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003648}
3649
3650RValue<UShort8> psrlw(RValue<UShort8> x, unsigned char y)
3651{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003652 return RValue<UShort8>(createInstruction(llvm::Intrinsic::x86_sse2_psrli_w, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003653}
3654
3655RValue<Short4> psraw(RValue<Short4> x, unsigned char y)
3656{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003657 return As<Short4>(createInstruction(llvm::Intrinsic::x86_sse2_psrai_w, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003658}
3659
3660RValue<Short8> psraw(RValue<Short8> x, unsigned char y)
3661{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003662 return RValue<Short8>(createInstruction(llvm::Intrinsic::x86_sse2_psrai_w, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003663}
3664
3665RValue<Short4> psllw(RValue<Short4> x, unsigned char y)
3666{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003667 return As<Short4>(createInstruction(llvm::Intrinsic::x86_sse2_pslli_w, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003668}
3669
3670RValue<Short8> psllw(RValue<Short8> x, unsigned char y)
3671{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003672 return RValue<Short8>(createInstruction(llvm::Intrinsic::x86_sse2_pslli_w, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003673}
3674
3675RValue<Int2> pslld(RValue<Int2> x, unsigned char y)
3676{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003677 return As<Int2>(createInstruction(llvm::Intrinsic::x86_sse2_pslli_d, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003678}
3679
3680RValue<Int4> pslld(RValue<Int4> x, unsigned char y)
3681{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003682 return RValue<Int4>(createInstruction(llvm::Intrinsic::x86_sse2_pslli_d, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003683}
3684
3685RValue<Int2> psrad(RValue<Int2> x, unsigned char y)
3686{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003687 return As<Int2>(createInstruction(llvm::Intrinsic::x86_sse2_psrai_d, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003688}
3689
3690RValue<Int4> psrad(RValue<Int4> x, unsigned char y)
3691{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003692 return RValue<Int4>(createInstruction(llvm::Intrinsic::x86_sse2_psrai_d, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003693}
3694
3695RValue<UInt2> psrld(RValue<UInt2> x, unsigned char y)
3696{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003697 return As<UInt2>(createInstruction(llvm::Intrinsic::x86_sse2_psrli_d, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003698}
3699
3700RValue<UInt4> psrld(RValue<UInt4> x, unsigned char y)
3701{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003702 return RValue<UInt4>(createInstruction(llvm::Intrinsic::x86_sse2_psrli_d, x.value(), Nucleus::createConstantInt(y)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003703}
3704
3705RValue<Int4> pmaxsd(RValue<Int4> x, RValue<Int4> y)
3706{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003707 return RValue<Int4>(V(lowerPMINMAX(V(x.value()), V(y.value()), llvm::ICmpInst::ICMP_SGT)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003708}
3709
3710RValue<Int4> pminsd(RValue<Int4> x, RValue<Int4> y)
3711{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003712 return RValue<Int4>(V(lowerPMINMAX(V(x.value()), V(y.value()), llvm::ICmpInst::ICMP_SLT)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003713}
3714
3715RValue<UInt4> pmaxud(RValue<UInt4> x, RValue<UInt4> y)
3716{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003717 return RValue<UInt4>(V(lowerPMINMAX(V(x.value()), V(y.value()), llvm::ICmpInst::ICMP_UGT)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003718}
3719
3720RValue<UInt4> pminud(RValue<UInt4> x, RValue<UInt4> y)
3721{
Nicolas Capensb6e8c3f2020-05-01 23:28:37 -04003722 return RValue<UInt4>(V(lowerPMINMAX(V(x.value()), V(y.value()), llvm::ICmpInst::ICMP_ULT)));
Nicolas Capens157ba262019-12-10 17:49:14 -05003723}
3724
3725RValue<Short4> pmulhw(RValue<Short4> x, RValue<Short4> y)
3726{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003727 return As<Short4>(createInstruction(llvm::Intrinsic::x86_sse2_pmulh_w, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003728}
3729
3730RValue<UShort4> pmulhuw(RValue<UShort4> x, RValue<UShort4> y)
3731{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003732 return As<UShort4>(createInstruction(llvm::Intrinsic::x86_sse2_pmulhu_w, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003733}
3734
3735RValue<Int2> pmaddwd(RValue<Short4> x, RValue<Short4> y)
3736{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003737 return As<Int2>(createInstruction(llvm::Intrinsic::x86_sse2_pmadd_wd, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003738}
3739
3740RValue<Short8> pmulhw(RValue<Short8> x, RValue<Short8> y)
3741{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003742 return RValue<Short8>(createInstruction(llvm::Intrinsic::x86_sse2_pmulh_w, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003743}
3744
3745RValue<UShort8> pmulhuw(RValue<UShort8> x, RValue<UShort8> y)
3746{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003747 return RValue<UShort8>(createInstruction(llvm::Intrinsic::x86_sse2_pmulhu_w, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003748}
3749
3750RValue<Int4> pmaddwd(RValue<Short8> x, RValue<Short8> y)
3751{
Nicolas Capens0ffac8f2020-07-22 21:51:39 -04003752 return RValue<Int4>(createInstruction(llvm::Intrinsic::x86_sse2_pmadd_wd, x.value(), y.value()));
Nicolas Capens157ba262019-12-10 17:49:14 -05003753}
3754
3755RValue<Int> movmskps(RValue<Float4> x)
3756{
Nicolas Capensb162fcf2021-05-12 13:47:25 -04003757 Value *v = x.value();
3758
3759 // TODO(b/172238865): MemorySanitizer does not support movmsk instructions,
3760 // which makes it look at the entire 128-bit input for undefined bits. Mask off
3761 // just the sign bits to avoid false positives.
3762 if(__has_feature(memory_sanitizer))
3763 {
3764 v = As<Float4>(As<Int4>(v) & Int4(0x80000000u)).value();
3765 }
3766
3767 return RValue<Int>(createInstruction(llvm::Intrinsic::x86_sse_movmsk_ps, v));
Nicolas Capens157ba262019-12-10 17:49:14 -05003768}
3769
3770RValue<Int> pmovmskb(RValue<Byte8> x)
3771{
Nicolas Capensb162fcf2021-05-12 13:47:25 -04003772 Value *v = x.value();
3773
3774 // TODO(b/172238865): MemorySanitizer does not support movmsk instructions,
3775 // which makes it look at the entire 128-bit input for undefined bits. Mask off
3776 // just the sign bits in the lower 64-bit vector to avoid false positives.
3777 if(__has_feature(memory_sanitizer))
3778 {
3779 v = As<Byte16>(As<Int4>(v) & Int4(0x80808080u, 0x80808080u, 0, 0)).value();
3780 }
3781
3782 return RValue<Int>(createInstruction(llvm::Intrinsic::x86_sse2_pmovmskb_128, v)) & 0xFF;
Nicolas Capens157ba262019-12-10 17:49:14 -05003783}
3784
Nicolas Capens157ba262019-12-10 17:49:14 -05003785} // namespace x86
Logan Chiene3191012018-08-24 22:01:50 +08003786#endif // defined(__i386__) || defined(__x86_64__)
Ben Clayton1bc7ee92019-02-14 18:43:22 +00003787
Ben Clayton60a3d6f2019-02-26 17:24:46 +00003788#ifdef ENABLE_RR_PRINT
Antonio Maiorano62427e02020-02-13 09:18:05 -05003789void VPrintf(const std::vector<Value *> &vals)
Nicolas Capens157ba262019-12-10 17:49:14 -05003790{
Nicolas Capens567e5602021-01-07 23:18:56 -05003791 auto i32Ty = llvm::Type::getInt32Ty(*jit->context);
3792 auto i8PtrTy = llvm::Type::getInt8PtrTy(*jit->context);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003793 auto funcTy = llvm::FunctionType::get(i32Ty, { i8PtrTy }, true);
Antonio Maiorano8cbee412020-06-10 15:59:20 -04003794 auto func = jit->module->getOrInsertFunction("rr::DebugPrintf", funcTy);
Antonio Maiorano62427e02020-02-13 09:18:05 -05003795 jit->builder->CreateCall(func, V(vals));
Nicolas Capens157ba262019-12-10 17:49:14 -05003796}
Ben Clayton713b8d32019-12-17 20:37:56 +00003797#endif // ENABLE_RR_PRINT
Ben Clayton1bc7ee92019-02-14 18:43:22 +00003798
Nicolas Capens157ba262019-12-10 17:49:14 -05003799void Nop()
3800{
Nicolas Capens567e5602021-01-07 23:18:56 -05003801 auto voidTy = llvm::Type::getVoidTy(*jit->context);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003802 auto funcTy = llvm::FunctionType::get(voidTy, {}, false);
Nicolas Capens157ba262019-12-10 17:49:14 -05003803 auto func = jit->module->getOrInsertFunction("nop", funcTy);
3804 jit->builder->CreateCall(func);
3805}
Ben Claytonac07ed82019-03-26 14:17:41 +00003806
Nicolas Capens157ba262019-12-10 17:49:14 -05003807void EmitDebugLocation()
3808{
Ben Claytonac07ed82019-03-26 14:17:41 +00003809#ifdef ENABLE_RR_DEBUG_INFO
Nicolas Capens81bc9d92019-12-16 15:05:57 -05003810 if(jit->debugInfo != nullptr)
Ben Claytonac07ed82019-03-26 14:17:41 +00003811 {
Nicolas Capens157ba262019-12-10 17:49:14 -05003812 jit->debugInfo->EmitLocation();
Ben Claytonac07ed82019-03-26 14:17:41 +00003813 }
Ben Clayton713b8d32019-12-17 20:37:56 +00003814#endif // ENABLE_RR_DEBUG_INFO
Nicolas Capens157ba262019-12-10 17:49:14 -05003815}
Ben Claytonac07ed82019-03-26 14:17:41 +00003816
Ben Clayton713b8d32019-12-17 20:37:56 +00003817void EmitDebugVariable(Value *value)
Nicolas Capens157ba262019-12-10 17:49:14 -05003818{
3819#ifdef ENABLE_RR_DEBUG_INFO
Nicolas Capens81bc9d92019-12-16 15:05:57 -05003820 if(jit->debugInfo != nullptr)
Ben Claytonac07ed82019-03-26 14:17:41 +00003821 {
Nicolas Capens157ba262019-12-10 17:49:14 -05003822 jit->debugInfo->EmitVariable(value);
Ben Claytonac07ed82019-03-26 14:17:41 +00003823 }
Ben Clayton713b8d32019-12-17 20:37:56 +00003824#endif // ENABLE_RR_DEBUG_INFO
Nicolas Capens157ba262019-12-10 17:49:14 -05003825}
Ben Claytonac07ed82019-03-26 14:17:41 +00003826
Nicolas Capens157ba262019-12-10 17:49:14 -05003827void FlushDebug()
3828{
3829#ifdef ENABLE_RR_DEBUG_INFO
Nicolas Capens81bc9d92019-12-16 15:05:57 -05003830 if(jit->debugInfo != nullptr)
Nicolas Capens157ba262019-12-10 17:49:14 -05003831 {
3832 jit->debugInfo->Flush();
3833 }
Ben Clayton713b8d32019-12-17 20:37:56 +00003834#endif // ENABLE_RR_DEBUG_INFO
Nicolas Capens157ba262019-12-10 17:49:14 -05003835}
3836
3837} // namespace rr
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003838
3839// ------------------------------ Coroutines ------------------------------
3840
3841namespace {
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003842
Nicolas Capens157ba262019-12-10 17:49:14 -05003843// Magic values retuned by llvm.coro.suspend.
3844// See: https://llvm.org/docs/Coroutines.html#llvm-coro-suspend-intrinsic
3845enum SuspendAction
3846{
3847 SuspendActionSuspend = -1,
3848 SuspendActionResume = 0,
3849 SuspendActionDestroy = 1
3850};
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003851
Ben Clayton16da2812019-07-09 23:28:51 +01003852void promoteFunctionToCoroutine()
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003853{
Ben Clayton16da2812019-07-09 23:28:51 +01003854 ASSERT(jit->coroutine.id == nullptr);
3855
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003856 // Types
Nicolas Capens567e5602021-01-07 23:18:56 -05003857 auto voidTy = llvm::Type::getVoidTy(*jit->context);
3858 auto i1Ty = llvm::Type::getInt1Ty(*jit->context);
3859 auto i8Ty = llvm::Type::getInt8Ty(*jit->context);
3860 auto i32Ty = llvm::Type::getInt32Ty(*jit->context);
3861 auto i8PtrTy = llvm::Type::getInt8PtrTy(*jit->context);
Ben Clayton16da2812019-07-09 23:28:51 +01003862 auto promiseTy = jit->coroutine.yieldType;
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003863 auto promisePtrTy = promiseTy->getPointerTo();
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003864
3865 // LLVM intrinsics
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003866 auto coro_id = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_id);
3867 auto coro_size = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_size, { i32Ty });
3868 auto coro_begin = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_begin);
3869 auto coro_resume = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_resume);
3870 auto coro_end = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_end);
3871 auto coro_free = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_free);
3872 auto coro_destroy = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_destroy);
3873 auto coro_promise = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_promise);
3874 auto coro_done = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_done);
3875 auto coro_suspend = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_suspend);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003876
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003877 auto allocFrameTy = llvm::FunctionType::get(i8PtrTy, { i32Ty }, false);
Ben Clayton6f8e5652019-06-29 01:58:02 +01003878 auto allocFrame = jit->module->getOrInsertFunction("coroutine_alloc_frame", allocFrameTy);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003879 auto freeFrameTy = llvm::FunctionType::get(voidTy, { i8PtrTy }, false);
Ben Clayton6f8e5652019-06-29 01:58:02 +01003880 auto freeFrame = jit->module->getOrInsertFunction("coroutine_free_frame", freeFrameTy);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003881
Ben Clayton16da2812019-07-09 23:28:51 +01003882 auto oldInsertionPoint = jit->builder->saveIP();
3883
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003884 // Build the coroutine_await() function:
3885 //
3886 // bool coroutine_await(CoroutineHandle* handle, YieldType* out)
3887 // {
Nicolas Capens81bc9d92019-12-16 15:05:57 -05003888 // if(llvm.coro.done(handle))
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003889 // {
3890 // return false;
3891 // }
3892 // else
3893 // {
3894 // *value = (T*)llvm.coro.promise(handle);
3895 // llvm.coro.resume(handle);
3896 // return true;
3897 // }
3898 // }
3899 //
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003900 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003901 auto args = jit->coroutine.await->arg_begin();
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003902 auto handle = args++;
3903 auto outPtr = args++;
Nicolas Capens567e5602021-01-07 23:18:56 -05003904 jit->builder->SetInsertPoint(llvm::BasicBlock::Create(*jit->context, "co_await", jit->coroutine.await));
3905 auto doneBlock = llvm::BasicBlock::Create(*jit->context, "done", jit->coroutine.await);
3906 auto resumeBlock = llvm::BasicBlock::Create(*jit->context, "resume", jit->coroutine.await);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003907
Ben Clayton713b8d32019-12-17 20:37:56 +00003908 auto done = jit->builder->CreateCall(coro_done, { handle }, "done");
Ben Clayton6f8e5652019-06-29 01:58:02 +01003909 jit->builder->CreateCondBr(done, doneBlock, resumeBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003910
Ben Clayton6f8e5652019-06-29 01:58:02 +01003911 jit->builder->SetInsertPoint(doneBlock);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003912 jit->builder->CreateRet(llvm::ConstantInt::getFalse(i1Ty));
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003913
Ben Clayton6f8e5652019-06-29 01:58:02 +01003914 jit->builder->SetInsertPoint(resumeBlock);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003915 auto promiseAlignment = llvm::ConstantInt::get(i32Ty, 4); // TODO: Get correct alignment.
3916 auto promisePtr = jit->builder->CreateCall(coro_promise, { handle, promiseAlignment, llvm::ConstantInt::get(i1Ty, 0) });
Benjamin Kramered855982021-12-03 13:44:34 +01003917 auto promise = jit->builder->CreateLoad(promiseTy, jit->builder->CreatePointerCast(promisePtr, promisePtrTy));
Ben Clayton6f8e5652019-06-29 01:58:02 +01003918 jit->builder->CreateStore(promise, outPtr);
Ben Clayton713b8d32019-12-17 20:37:56 +00003919 jit->builder->CreateCall(coro_resume, { handle });
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003920 jit->builder->CreateRet(llvm::ConstantInt::getTrue(i1Ty));
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003921 }
3922
3923 // Build the coroutine_destroy() function:
3924 //
3925 // void coroutine_destroy(CoroutineHandle* handle)
3926 // {
3927 // llvm.coro.destroy(handle);
3928 // }
3929 //
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003930 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003931 auto handle = jit->coroutine.destroy->arg_begin();
Nicolas Capens567e5602021-01-07 23:18:56 -05003932 jit->builder->SetInsertPoint(llvm::BasicBlock::Create(*jit->context, "", jit->coroutine.destroy));
Ben Clayton713b8d32019-12-17 20:37:56 +00003933 jit->builder->CreateCall(coro_destroy, { handle });
Ben Clayton6f8e5652019-06-29 01:58:02 +01003934 jit->builder->CreateRetVoid();
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003935 }
3936
3937 // Begin building the main coroutine_begin() function.
3938 //
3939 // CoroutineHandle* coroutine_begin(<Arguments>)
3940 // {
3941 // YieldType promise;
3942 // auto id = llvm.coro.id(0, &promise, nullptr, nullptr);
3943 // void* frame = coroutine_alloc_frame(llvm.coro.size.i32());
3944 // CoroutineHandle *handle = llvm.coro.begin(id, frame);
3945 //
3946 // ... <REACTOR CODE> ...
3947 //
3948 // end:
3949 // SuspendAction action = llvm.coro.suspend(none, true /* final */); // <-- RESUME POINT
Nicolas Capens81bc9d92019-12-16 15:05:57 -05003950 // switch(action)
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003951 // {
3952 // case SuspendActionResume:
3953 // UNREACHABLE(); // Illegal to resume after final suspend.
3954 // case SuspendActionDestroy:
3955 // goto destroy;
3956 // default: // (SuspendActionSuspend)
3957 // goto suspend;
3958 // }
3959 //
3960 // destroy:
3961 // coroutine_free_frame(llvm.coro.free(id, handle));
3962 // goto suspend;
3963 //
3964 // suspend:
3965 // llvm.coro.end(handle, false);
3966 // return handle;
3967 // }
3968 //
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003969
3970#ifdef ENABLE_RR_DEBUG_INFO
Nicolas Capens567e5602021-01-07 23:18:56 -05003971 jit->debugInfo = std::make_unique<rr::DebugInfo>(jit->builder.get(), jit->context.get(), jit->module.get(), jit->function);
Ben Clayton713b8d32019-12-17 20:37:56 +00003972#endif // ENABLE_RR_DEBUG_INFO
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003973
Nicolas Capens567e5602021-01-07 23:18:56 -05003974 jit->coroutine.suspendBlock = llvm::BasicBlock::Create(*jit->context, "suspend", jit->function);
3975 jit->coroutine.endBlock = llvm::BasicBlock::Create(*jit->context, "end", jit->function);
3976 jit->coroutine.destroyBlock = llvm::BasicBlock::Create(*jit->context, "destroy", jit->function);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003977
Ben Clayton16da2812019-07-09 23:28:51 +01003978 jit->builder->SetInsertPoint(jit->coroutine.entryBlock, jit->coroutine.entryBlock->begin());
3979 jit->coroutine.promise = jit->builder->CreateAlloca(promiseTy, nullptr, "promise");
Ben Clayton6f8e5652019-06-29 01:58:02 +01003980 jit->coroutine.id = jit->builder->CreateCall(coro_id, {
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003981 llvm::ConstantInt::get(i32Ty, 0),
Ben Clayton713b8d32019-12-17 20:37:56 +00003982 jit->builder->CreatePointerCast(jit->coroutine.promise, i8PtrTy),
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003983 llvm::ConstantPointerNull::get(i8PtrTy),
3984 llvm::ConstantPointerNull::get(i8PtrTy),
Ben Clayton713b8d32019-12-17 20:37:56 +00003985 });
Ben Clayton6f8e5652019-06-29 01:58:02 +01003986 auto size = jit->builder->CreateCall(coro_size, {});
Ben Clayton713b8d32019-12-17 20:37:56 +00003987 auto frame = jit->builder->CreateCall(allocFrame, { size });
3988 jit->coroutine.handle = jit->builder->CreateCall(coro_begin, { jit->coroutine.id, frame });
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003989
3990 // Build the suspend block
Ben Clayton6f8e5652019-06-29 01:58:02 +01003991 jit->builder->SetInsertPoint(jit->coroutine.suspendBlock);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003992 jit->builder->CreateCall(coro_end, { jit->coroutine.handle, llvm::ConstantInt::get(i1Ty, 0) });
Ben Clayton6f8e5652019-06-29 01:58:02 +01003993 jit->builder->CreateRet(jit->coroutine.handle);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01003994
3995 // Build the end block
Ben Clayton6f8e5652019-06-29 01:58:02 +01003996 jit->builder->SetInsertPoint(jit->coroutine.endBlock);
3997 auto action = jit->builder->CreateCall(coro_suspend, {
Nicolas Capens567e5602021-01-07 23:18:56 -05003998 llvm::ConstantTokenNone::get(*jit->context),
Nicolas Capens00ecdf92020-10-30 21:13:40 -04003999 llvm::ConstantInt::get(i1Ty, 1), // final: true
Ben Clayton713b8d32019-12-17 20:37:56 +00004000 });
Ben Clayton6f8e5652019-06-29 01:58:02 +01004001 auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04004002 // switch_->addCase(llvm::ConstantInt::get(i8Ty, SuspendActionResume), trapBlock); // TODO: Trap attempting to resume after final suspend
4003 switch_->addCase(llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004004
4005 // Build the destroy block
Ben Clayton6f8e5652019-06-29 01:58:02 +01004006 jit->builder->SetInsertPoint(jit->coroutine.destroyBlock);
Ben Clayton713b8d32019-12-17 20:37:56 +00004007 auto memory = jit->builder->CreateCall(coro_free, { jit->coroutine.id, jit->coroutine.handle });
4008 jit->builder->CreateCall(freeFrame, { memory });
Ben Clayton6f8e5652019-06-29 01:58:02 +01004009 jit->builder->CreateBr(jit->coroutine.suspendBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004010
Ben Clayton16da2812019-07-09 23:28:51 +01004011 // Switch back to original insert point to continue building the coroutine.
4012 jit->builder->restoreIP(oldInsertionPoint);
4013}
4014
Ben Clayton713b8d32019-12-17 20:37:56 +00004015} // anonymous namespace
Ben Clayton16da2812019-07-09 23:28:51 +01004016
4017namespace rr {
4018
Antonio Maiorano5ba2a5b2020-01-17 15:29:37 -05004019void Nucleus::createCoroutine(Type *YieldType, const std::vector<Type *> &Params)
Ben Clayton16da2812019-07-09 23:28:51 +01004020{
4021 // Coroutines are initially created as a regular function.
4022 // Upon the first call to Yield(), the function is promoted to a true
4023 // coroutine.
Nicolas Capens567e5602021-01-07 23:18:56 -05004024 auto voidTy = llvm::Type::getVoidTy(*jit->context);
4025 auto i1Ty = llvm::Type::getInt1Ty(*jit->context);
4026 auto i8PtrTy = llvm::Type::getInt8PtrTy(*jit->context);
Ben Clayton16da2812019-07-09 23:28:51 +01004027 auto handleTy = i8PtrTy;
4028 auto boolTy = i1Ty;
4029 auto promiseTy = T(YieldType);
4030 auto promisePtrTy = promiseTy->getPointerTo();
4031
4032 jit->function = rr::createFunction("coroutine_begin", handleTy, T(Params));
Nicolas Capensa07b3fb2022-07-28 04:18:58 -04004033#if LLVM_VERSION_MAJOR >= 16
Nicolas Capens0df01aa2022-06-17 12:13:06 -04004034 jit->function->setPresplitCoroutine();
4035#else
Nicolas Capens16f4b382022-01-05 10:16:33 -05004036 jit->function->addFnAttr("coroutine.presplit", "0");
Nicolas Capens0df01aa2022-06-17 12:13:06 -04004037#endif
Ben Clayton713b8d32019-12-17 20:37:56 +00004038 jit->coroutine.await = rr::createFunction("coroutine_await", boolTy, { handleTy, promisePtrTy });
4039 jit->coroutine.destroy = rr::createFunction("coroutine_destroy", voidTy, { handleTy });
Ben Clayton16da2812019-07-09 23:28:51 +01004040 jit->coroutine.yieldType = promiseTy;
Nicolas Capens567e5602021-01-07 23:18:56 -05004041 jit->coroutine.entryBlock = llvm::BasicBlock::Create(*jit->context, "function", jit->function);
Ben Clayton16da2812019-07-09 23:28:51 +01004042
4043 jit->builder->SetInsertPoint(jit->coroutine.entryBlock);
John Bauman89401822014-05-06 15:04:28 -04004044}
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004045
Ben Clayton713b8d32019-12-17 20:37:56 +00004046void Nucleus::yield(Value *val)
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004047{
Nicolas Capens81bc9d92019-12-16 15:05:57 -05004048 if(jit->coroutine.id == nullptr)
Ben Clayton16da2812019-07-09 23:28:51 +01004049 {
4050 // First call to yield().
4051 // Promote the function to a full coroutine.
4052 promoteFunctionToCoroutine();
4053 ASSERT(jit->coroutine.id != nullptr);
4054 }
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004055
4056 // promise = val;
4057 //
4058 // auto action = llvm.coro.suspend(none, false /* final */); // <-- RESUME POINT
Nicolas Capens81bc9d92019-12-16 15:05:57 -05004059 // switch(action)
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004060 // {
4061 // case SuspendActionResume:
4062 // goto resume;
4063 // case SuspendActionDestroy:
4064 // goto destroy;
4065 // default: // (SuspendActionSuspend)
4066 // goto suspend;
4067 // }
4068 // resume:
4069 //
4070
4071 RR_DEBUG_INFO_UPDATE_LOC();
4072 Variable::materializeAll();
4073
4074 // Types
Nicolas Capens567e5602021-01-07 23:18:56 -05004075 auto i1Ty = llvm::Type::getInt1Ty(*jit->context);
4076 auto i8Ty = llvm::Type::getInt8Ty(*jit->context);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004077
4078 // Intrinsics
Nicolas Capens00ecdf92020-10-30 21:13:40 -04004079 auto coro_suspend = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_suspend);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004080
4081 // Create a block to resume execution.
Nicolas Capens567e5602021-01-07 23:18:56 -05004082 auto resumeBlock = llvm::BasicBlock::Create(*jit->context, "resume", jit->function);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004083
4084 // Store the promise (yield value)
Ben Clayton6f8e5652019-06-29 01:58:02 +01004085 jit->builder->CreateStore(V(val), jit->coroutine.promise);
4086 auto action = jit->builder->CreateCall(coro_suspend, {
Nicolas Capens567e5602021-01-07 23:18:56 -05004087 llvm::ConstantTokenNone::get(*jit->context),
Nicolas Capens00ecdf92020-10-30 21:13:40 -04004088 llvm::ConstantInt::get(i1Ty, 0), // final: true
Ben Clayton713b8d32019-12-17 20:37:56 +00004089 });
Ben Clayton6f8e5652019-06-29 01:58:02 +01004090 auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3);
Nicolas Capens00ecdf92020-10-30 21:13:40 -04004091 switch_->addCase(llvm::ConstantInt::get(i8Ty, SuspendActionResume), resumeBlock);
4092 switch_->addCase(llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004093
4094 // Continue building in the resume block.
Ben Clayton6f8e5652019-06-29 01:58:02 +01004095 jit->builder->SetInsertPoint(resumeBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004096}
4097
Nicolas Capens79d4c6c2022-04-22 17:20:26 -04004098std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name)
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004099{
Nicolas Capens88fe9ce2022-04-19 18:32:26 -04004100 if(jit->coroutine.id)
Ben Clayton16da2812019-07-09 23:28:51 +01004101 {
4102 jit->builder->CreateBr(jit->coroutine.endBlock);
4103 }
4104 else
4105 {
4106 // Coroutine without a Yield acts as a regular function.
4107 // The 'coroutine_begin' function returns a nullptr for the coroutine
4108 // handle.
4109 jit->builder->CreateRet(llvm::Constant::getNullValue(jit->function->getReturnType()));
4110 // The 'coroutine_await' function always returns false (coroutine done).
Nicolas Capens567e5602021-01-07 23:18:56 -05004111 jit->builder->SetInsertPoint(llvm::BasicBlock::Create(*jit->context, "", jit->coroutine.await));
Ben Clayton16da2812019-07-09 23:28:51 +01004112 jit->builder->CreateRet(llvm::Constant::getNullValue(jit->coroutine.await->getReturnType()));
4113 // The 'coroutine_destroy' does nothing, returns void.
Nicolas Capens567e5602021-01-07 23:18:56 -05004114 jit->builder->SetInsertPoint(llvm::BasicBlock::Create(*jit->context, "", jit->coroutine.destroy));
Ben Clayton16da2812019-07-09 23:28:51 +01004115 jit->builder->CreateRetVoid();
4116 }
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004117
4118#ifdef ENABLE_RR_DEBUG_INFO
Nicolas Capens81bc9d92019-12-16 15:05:57 -05004119 if(jit->debugInfo != nullptr)
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004120 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004121 jit->debugInfo->Finalize();
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004122 }
Ben Clayton713b8d32019-12-17 20:37:56 +00004123#endif // ENABLE_RR_DEBUG_INFO
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004124
4125 if(false)
4126 {
4127 std::error_code error;
4128 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error);
Ben Clayton6f8e5652019-06-29 01:58:02 +01004129 jit->module->print(file, 0);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004130 }
4131
Nicolas Capens79d4c6c2022-04-22 17:20:26 -04004132 jit->runPasses();
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004133
4134 if(false)
4135 {
4136 std::error_code error;
4137 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error);
Ben Clayton6f8e5652019-06-29 01:58:02 +01004138 jit->module->print(file, 0);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004139 }
4140
4141 llvm::Function *funcs[Nucleus::CoroutineEntryCount];
Ben Clayton6f8e5652019-06-29 01:58:02 +01004142 funcs[Nucleus::CoroutineEntryBegin] = jit->function;
4143 funcs[Nucleus::CoroutineEntryAwait] = jit->coroutine.await;
4144 funcs[Nucleus::CoroutineEntryDestroy] = jit->coroutine.destroy;
Nicolas Capens98d2cab2020-04-29 13:45:55 -04004145
Nicolas Capens79d4c6c2022-04-22 17:20:26 -04004146 auto routine = jit->acquireRoutine(name, funcs, Nucleus::CoroutineEntryCount);
Nicolas Capens98d2cab2020-04-29 13:45:55 -04004147
Nicolas Capens7d6b5912020-04-28 15:57:57 -04004148 delete jit;
4149 jit = nullptr;
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004150
4151 return routine;
4152}
4153
Antonio Maiorano5ba2a5b2020-01-17 15:29:37 -05004154Nucleus::CoroutineHandle Nucleus::invokeCoroutineBegin(Routine &routine, std::function<Nucleus::CoroutineHandle()> func)
4155{
4156 return func();
4157}
4158
Nicolas Capens44f94692022-06-20 23:15:46 -04004159SIMD::Int::Int(RValue<scalar::Int> rhs)
Nicolas Capens7e960682022-06-30 15:53:27 -04004160 : XYZW(this)
Nicolas Capens44f94692022-06-20 23:15:46 -04004161{
4162 RR_DEBUG_INFO_UPDATE_LOC();
4163 Value *vector = loadValue();
4164 Value *insert = Nucleus::createInsertElement(vector, rhs.value(), 0);
4165
4166 std::vector<int> swizzle = { 0 };
4167 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
4168
4169 storeValue(replicate);
4170}
4171
4172RValue<SIMD::Int> operator<<(RValue<SIMD::Int> lhs, unsigned char rhs)
4173{
4174 RR_DEBUG_INFO_UPDATE_LOC();
4175 return As<SIMD::Int>(V(lowerVectorShl(V(lhs.value()), rhs)));
4176}
4177
4178RValue<SIMD::Int> operator>>(RValue<SIMD::Int> lhs, unsigned char rhs)
4179{
4180 RR_DEBUG_INFO_UPDATE_LOC();
4181 return As<SIMD::Int>(V(lowerVectorAShr(V(lhs.value()), rhs)));
4182}
4183
4184RValue<SIMD::Int> CmpEQ(RValue<SIMD::Int> x, RValue<SIMD::Int> y)
4185{
4186 RR_DEBUG_INFO_UPDATE_LOC();
4187 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value(), y.value()), SIMD::Int::type()));
4188}
4189
4190RValue<SIMD::Int> CmpLT(RValue<SIMD::Int> x, RValue<SIMD::Int> y)
4191{
4192 RR_DEBUG_INFO_UPDATE_LOC();
4193 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value(), y.value()), SIMD::Int::type()));
4194}
4195
4196RValue<SIMD::Int> CmpLE(RValue<SIMD::Int> x, RValue<SIMD::Int> y)
4197{
4198 RR_DEBUG_INFO_UPDATE_LOC();
4199 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value(), y.value()), SIMD::Int::type()));
4200}
4201
4202RValue<SIMD::Int> CmpNEQ(RValue<SIMD::Int> x, RValue<SIMD::Int> y)
4203{
4204 RR_DEBUG_INFO_UPDATE_LOC();
4205 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createICmpNE(x.value(), y.value()), SIMD::Int::type()));
4206}
4207
4208RValue<SIMD::Int> CmpNLT(RValue<SIMD::Int> x, RValue<SIMD::Int> y)
4209{
4210 RR_DEBUG_INFO_UPDATE_LOC();
4211 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value(), y.value()), SIMD::Int::type()));
4212}
4213
4214RValue<SIMD::Int> CmpNLE(RValue<SIMD::Int> x, RValue<SIMD::Int> y)
4215{
4216 RR_DEBUG_INFO_UPDATE_LOC();
4217 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value(), y.value()), SIMD::Int::type()));
4218}
4219
4220RValue<SIMD::Int> Abs(RValue<SIMD::Int> x)
4221{
4222#if LLVM_VERSION_MAJOR >= 12
4223 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::abs, { V(x.value())->getType() });
4224 return RValue<SIMD::Int>(V(jit->builder->CreateCall(func, { V(x.value()), llvm::ConstantInt::getFalse(*jit->context) })));
4225#else
4226 auto negative = x >> 31;
4227 return (x ^ negative) - negative;
4228#endif
4229}
4230
4231RValue<SIMD::Int> Max(RValue<SIMD::Int> x, RValue<SIMD::Int> y)
4232{
4233 RR_DEBUG_INFO_UPDATE_LOC();
4234 RValue<SIMD::Int> greater = CmpNLE(x, y);
4235 return (x & greater) | (y & ~greater);
4236}
4237
4238RValue<SIMD::Int> Min(RValue<SIMD::Int> x, RValue<SIMD::Int> y)
4239{
4240 RR_DEBUG_INFO_UPDATE_LOC();
4241 RValue<SIMD::Int> less = CmpLT(x, y);
4242 return (x & less) | (y & ~less);
4243}
4244
4245RValue<SIMD::Int> RoundInt(RValue<SIMD::Float> cast)
4246{
4247 RR_DEBUG_INFO_UPDATE_LOC();
4248 return As<SIMD::Int>(V(lowerRoundInt(V(cast.value()), T(SIMD::Int::type()))));
4249}
4250
4251RValue<SIMD::Int> RoundIntClamped(RValue<SIMD::Float> cast)
4252{
4253 RR_DEBUG_INFO_UPDATE_LOC();
4254
4255// TODO(b/165000222): Check if fptosi_sat produces optimal code for x86 and ARM.
4256#if defined(__arm__) || defined(__aarch64__)
4257 // ARM saturates to the largest positive or negative integer. Unit tests
4258 // verify that lowerRoundInt() behaves as desired.
4259 return As<SIMD::Int>(V(lowerRoundInt(V(cast.value()), T(SIMD::Int::type()))));
4260#elif LLVM_VERSION_MAJOR >= 14
4261 llvm::Value *rounded = lowerRound(V(cast.value()));
4262 llvm::Function *fptosi_sat = llvm::Intrinsic::getDeclaration(
4263 jit->module.get(), llvm::Intrinsic::fptosi_sat, { T(SIMD::Int::type()), T(SIMD::Float::type()) });
4264 return RValue<SIMD::Int>(V(jit->builder->CreateCall(fptosi_sat, { rounded })));
4265#else
4266 RValue<SIMD::Float> clamped = Max(Min(cast, SIMD::Float(0x7FFFFF80)), SIMD::Float(static_cast<int>(0x80000000)));
4267 return As<SIMD::Int>(V(lowerRoundInt(V(clamped.value()), T(SIMD::Int::type()))));
4268#endif
4269}
4270
Nicolas Capens0ed3fa62022-06-22 16:48:07 -04004271RValue<Int4> Extract128(RValue<SIMD::Int> val, int i)
4272{
4273 llvm::Value *v128 = jit->builder->CreateBitCast(V(val.value()), llvm::FixedVectorType::get(llvm::IntegerType::get(*jit->context, 128), SIMD::Width / 4));
4274
4275 return As<Int4>(V(jit->builder->CreateExtractElement(v128, i)));
4276}
4277
4278RValue<SIMD::Int> Insert128(RValue<SIMD::Int> val, RValue<Int4> element, int i)
4279{
4280 llvm::Value *v128 = jit->builder->CreateBitCast(V(val.value()), llvm::FixedVectorType::get(llvm::IntegerType::get(*jit->context, 128), SIMD::Width / 4));
4281 llvm::Value *a = jit->builder->CreateBitCast(V(element.value()), llvm::IntegerType::get(*jit->context, 128));
4282
4283 return As<SIMD::Int>(V(jit->builder->CreateInsertElement(v128, a, i)));
4284}
4285
Nicolas Capens3b0ad202022-06-02 15:02:31 -04004286Type *SIMD::Int::type()
4287{
Nicolas Capens44f94692022-06-20 23:15:46 -04004288 return T(llvm::VectorType::get(T(scalar::Int::type()), SIMD::Width, false));
4289}
4290
4291SIMD::UInt::UInt(RValue<SIMD::Float> cast)
Nicolas Capens7e960682022-06-30 15:53:27 -04004292 : XYZW(this)
Nicolas Capens44f94692022-06-20 23:15:46 -04004293{
4294 RR_DEBUG_INFO_UPDATE_LOC();
4295 Value *xyzw = Nucleus::createFPToUI(cast.value(), SIMD::UInt::type());
4296 storeValue(xyzw);
4297}
4298
4299SIMD::UInt::UInt(RValue<scalar::UInt> rhs)
Nicolas Capens7e960682022-06-30 15:53:27 -04004300 : XYZW(this)
Nicolas Capens44f94692022-06-20 23:15:46 -04004301{
4302 RR_DEBUG_INFO_UPDATE_LOC();
4303 Value *vector = loadValue();
4304 Value *insert = Nucleus::createInsertElement(vector, rhs.value(), 0);
4305
4306 std::vector<int> swizzle = { 0 };
4307 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
4308
4309 storeValue(replicate);
4310}
4311
4312RValue<SIMD::UInt> operator<<(RValue<SIMD::UInt> lhs, unsigned char rhs)
4313{
4314 RR_DEBUG_INFO_UPDATE_LOC();
4315 return As<SIMD::UInt>(V(lowerVectorShl(V(lhs.value()), rhs)));
4316}
4317
4318RValue<SIMD::UInt> operator>>(RValue<SIMD::UInt> lhs, unsigned char rhs)
4319{
4320 RR_DEBUG_INFO_UPDATE_LOC();
4321 return As<SIMD::UInt>(V(lowerVectorLShr(V(lhs.value()), rhs)));
4322}
4323
4324RValue<SIMD::UInt> CmpEQ(RValue<SIMD::UInt> x, RValue<SIMD::UInt> y)
4325{
4326 RR_DEBUG_INFO_UPDATE_LOC();
4327 return RValue<SIMD::UInt>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value(), y.value()), SIMD::Int::type()));
4328}
4329
4330RValue<SIMD::UInt> CmpLT(RValue<SIMD::UInt> x, RValue<SIMD::UInt> y)
4331{
4332 RR_DEBUG_INFO_UPDATE_LOC();
4333 return RValue<SIMD::UInt>(Nucleus::createSExt(Nucleus::createICmpULT(x.value(), y.value()), SIMD::Int::type()));
4334}
4335
4336RValue<SIMD::UInt> CmpLE(RValue<SIMD::UInt> x, RValue<SIMD::UInt> y)
4337{
4338 RR_DEBUG_INFO_UPDATE_LOC();
4339 return RValue<SIMD::UInt>(Nucleus::createSExt(Nucleus::createICmpULE(x.value(), y.value()), SIMD::Int::type()));
4340}
4341
4342RValue<SIMD::UInt> CmpNEQ(RValue<SIMD::UInt> x, RValue<SIMD::UInt> y)
4343{
4344 RR_DEBUG_INFO_UPDATE_LOC();
4345 return RValue<SIMD::UInt>(Nucleus::createSExt(Nucleus::createICmpNE(x.value(), y.value()), SIMD::Int::type()));
4346}
4347
4348RValue<SIMD::UInt> CmpNLT(RValue<SIMD::UInt> x, RValue<SIMD::UInt> y)
4349{
4350 RR_DEBUG_INFO_UPDATE_LOC();
4351 return RValue<SIMD::UInt>(Nucleus::createSExt(Nucleus::createICmpUGE(x.value(), y.value()), SIMD::Int::type()));
4352}
4353
4354RValue<SIMD::UInt> CmpNLE(RValue<SIMD::UInt> x, RValue<SIMD::UInt> y)
4355{
4356 RR_DEBUG_INFO_UPDATE_LOC();
4357 return RValue<SIMD::UInt>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value(), y.value()), SIMD::Int::type()));
4358}
4359
4360RValue<SIMD::UInt> Max(RValue<SIMD::UInt> x, RValue<SIMD::UInt> y)
4361{
4362 RR_DEBUG_INFO_UPDATE_LOC();
4363 RValue<SIMD::UInt> greater = CmpNLE(x, y);
4364 return (x & greater) | (y & ~greater);
4365}
4366
4367RValue<SIMD::UInt> Min(RValue<SIMD::UInt> x, RValue<SIMD::UInt> y)
4368{
4369 RR_DEBUG_INFO_UPDATE_LOC();
4370 RValue<SIMD::UInt> less = CmpLT(x, y);
4371 return (x & less) | (y & ~less);
4372}
4373
Nicolas Capens0ed3fa62022-06-22 16:48:07 -04004374RValue<UInt4> Extract128(RValue<SIMD::UInt> val, int i)
4375{
4376 llvm::Value *v128 = jit->builder->CreateBitCast(V(val.value()), llvm::FixedVectorType::get(llvm::IntegerType::get(*jit->context, 128), SIMD::Width / 4));
4377
4378 return As<UInt4>(V(jit->builder->CreateExtractElement(v128, i)));
4379}
4380
4381RValue<SIMD::UInt> Insert128(RValue<SIMD::UInt> val, RValue<UInt4> element, int i)
4382{
4383 llvm::Value *v128 = jit->builder->CreateBitCast(V(val.value()), llvm::FixedVectorType::get(llvm::IntegerType::get(*jit->context, 128), SIMD::Width / 4));
4384 llvm::Value *a = jit->builder->CreateBitCast(V(element.value()), llvm::IntegerType::get(*jit->context, 128));
4385
4386 return As<SIMD::UInt>(V(jit->builder->CreateInsertElement(v128, a, i)));
4387}
4388
Nicolas Capens44f94692022-06-20 23:15:46 -04004389Type *SIMD::UInt::type()
4390{
4391 return T(llvm::VectorType::get(T(scalar::UInt::type()), SIMD::Width, false));
4392}
4393
4394SIMD::Float::Float(RValue<scalar::Float> rhs)
Nicolas Capens7e960682022-06-30 15:53:27 -04004395 : XYZW(this)
Nicolas Capens44f94692022-06-20 23:15:46 -04004396{
4397 RR_DEBUG_INFO_UPDATE_LOC();
4398 Value *vector = loadValue();
4399 Value *insert = Nucleus::createInsertElement(vector, rhs.value(), 0);
4400
4401 std::vector<int> swizzle = { 0 };
4402 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
4403
4404 storeValue(replicate);
4405}
4406
4407RValue<SIMD::Float> operator%(RValue<SIMD::Float> lhs, RValue<SIMD::Float> rhs)
4408{
4409 return RValue<SIMD::Float>(Nucleus::createFRem(lhs.value(), rhs.value()));
4410}
4411
4412RValue<SIMD::Float> MulAdd(RValue<SIMD::Float> x, RValue<SIMD::Float> y, RValue<SIMD::Float> z)
4413{
4414 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::fmuladd, { T(SIMD::Float::type()) });
4415 return RValue<SIMD::Float>(V(jit->builder->CreateCall(func, { V(x.value()), V(y.value()), V(z.value()) })));
4416}
4417
4418RValue<SIMD::Float> FMA(RValue<SIMD::Float> x, RValue<SIMD::Float> y, RValue<SIMD::Float> z)
4419{
4420 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::fma, { T(SIMD::Float::type()) });
4421 return RValue<SIMD::Float>(V(jit->builder->CreateCall(func, { V(x.value()), V(y.value()), V(z.value()) })));
4422}
4423
4424RValue<SIMD::Float> Abs(RValue<SIMD::Float> x)
4425{
4426 auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::fabs, { V(x.value())->getType() });
4427 return RValue<SIMD::Float>(V(jit->builder->CreateCall(func, V(x.value()))));
4428}
4429
4430RValue<SIMD::Float> Max(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4431{
4432 RR_DEBUG_INFO_UPDATE_LOC();
4433 return As<SIMD::Float>(V(lowerPFMINMAX(V(x.value()), V(y.value()), llvm::FCmpInst::FCMP_OGT)));
4434}
4435
4436RValue<SIMD::Float> Min(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4437{
4438 RR_DEBUG_INFO_UPDATE_LOC();
4439 return As<SIMD::Float>(V(lowerPFMINMAX(V(x.value()), V(y.value()), llvm::FCmpInst::FCMP_OLT)));
4440}
4441
4442RValue<SIMD::Float> Sqrt(RValue<SIMD::Float> x)
4443{
4444 RR_DEBUG_INFO_UPDATE_LOC();
4445 return As<SIMD::Float>(V(lowerSQRT(V(x.value()))));
4446}
4447
4448RValue<SIMD::Int> CmpEQ(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4449{
4450 RR_DEBUG_INFO_UPDATE_LOC();
4451 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value(), y.value()), SIMD::Int::type()));
4452}
4453
4454RValue<SIMD::Int> CmpLT(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4455{
4456 RR_DEBUG_INFO_UPDATE_LOC();
4457 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value(), y.value()), SIMD::Int::type()));
4458}
4459
4460RValue<SIMD::Int> CmpLE(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4461{
4462 RR_DEBUG_INFO_UPDATE_LOC();
4463 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value(), y.value()), SIMD::Int::type()));
4464}
4465
4466RValue<SIMD::Int> CmpNEQ(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4467{
4468 RR_DEBUG_INFO_UPDATE_LOC();
4469 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value(), y.value()), SIMD::Int::type()));
4470}
4471
4472RValue<SIMD::Int> CmpNLT(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4473{
4474 RR_DEBUG_INFO_UPDATE_LOC();
4475 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value(), y.value()), SIMD::Int::type()));
4476}
4477
4478RValue<SIMD::Int> CmpNLE(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4479{
4480 RR_DEBUG_INFO_UPDATE_LOC();
4481 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value(), y.value()), SIMD::Int::type()));
4482}
4483
4484RValue<SIMD::Int> CmpUEQ(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4485{
4486 RR_DEBUG_INFO_UPDATE_LOC();
4487 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpUEQ(x.value(), y.value()), SIMD::Int::type()));
4488}
4489
4490RValue<SIMD::Int> CmpULT(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4491{
4492 RR_DEBUG_INFO_UPDATE_LOC();
4493 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpULT(x.value(), y.value()), SIMD::Int::type()));
4494}
4495
4496RValue<SIMD::Int> CmpULE(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4497{
4498 RR_DEBUG_INFO_UPDATE_LOC();
4499 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpULE(x.value(), y.value()), SIMD::Int::type()));
4500}
4501
4502RValue<SIMD::Int> CmpUNEQ(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4503{
4504 RR_DEBUG_INFO_UPDATE_LOC();
4505 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpUNE(x.value(), y.value()), SIMD::Int::type()));
4506}
4507
4508RValue<SIMD::Int> CmpUNLT(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4509{
4510 RR_DEBUG_INFO_UPDATE_LOC();
4511 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpUGE(x.value(), y.value()), SIMD::Int::type()));
4512}
4513
4514RValue<SIMD::Int> CmpUNLE(RValue<SIMD::Float> x, RValue<SIMD::Float> y)
4515{
4516 RR_DEBUG_INFO_UPDATE_LOC();
4517 return RValue<SIMD::Int>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value(), y.value()), SIMD::Int::type()));
4518}
4519
4520RValue<SIMD::Float> Round(RValue<SIMD::Float> x)
4521{
4522 RR_DEBUG_INFO_UPDATE_LOC();
4523 return RValue<SIMD::Float>(V(lowerRound(V(x.value()))));
4524}
4525
4526RValue<SIMD::Float> Trunc(RValue<SIMD::Float> x)
4527{
4528 RR_DEBUG_INFO_UPDATE_LOC();
4529 return RValue<SIMD::Float>(V(lowerTrunc(V(x.value()))));
4530}
4531
4532RValue<SIMD::Float> Frac(RValue<SIMD::Float> x)
4533{
4534 RR_DEBUG_INFO_UPDATE_LOC();
4535 SIMD::Float frc = x - Floor(x);
4536
4537 // x - floor(x) can be 1.0 for very small negative x.
4538 // Clamp against the value just below 1.0.
4539 return Min(frc, As<SIMD::Float>(SIMD::Int(0x3F7FFFFF)));
4540}
4541
4542RValue<SIMD::Float> Floor(RValue<SIMD::Float> x)
4543{
4544 RR_DEBUG_INFO_UPDATE_LOC();
4545 return RValue<SIMD::Float>(V(lowerFloor(V(x.value()))));
4546}
4547
4548RValue<SIMD::Float> Ceil(RValue<SIMD::Float> x)
4549{
4550 RR_DEBUG_INFO_UPDATE_LOC();
4551 return -Floor(-x);
4552}
4553
Nicolas Capens0ed3fa62022-06-22 16:48:07 -04004554RValue<Float4> Extract128(RValue<SIMD::Float> val, int i)
4555{
4556 llvm::Value *v128 = jit->builder->CreateBitCast(V(val.value()), llvm::FixedVectorType::get(llvm::IntegerType::get(*jit->context, 128), SIMD::Width / 4));
4557
4558 return As<Float4>(V(jit->builder->CreateExtractElement(v128, i)));
4559}
4560
4561RValue<SIMD::Float> Insert128(RValue<SIMD::Float> val, RValue<Float4> element, int i)
4562{
4563 llvm::Value *v128 = jit->builder->CreateBitCast(V(val.value()), llvm::FixedVectorType::get(llvm::IntegerType::get(*jit->context, 128), SIMD::Width / 4));
4564 llvm::Value *a = jit->builder->CreateBitCast(V(element.value()), llvm::IntegerType::get(*jit->context, 128));
4565
4566 return As<SIMD::Float>(V(jit->builder->CreateInsertElement(v128, a, i)));
4567}
4568
Nicolas Capens44f94692022-06-20 23:15:46 -04004569Type *SIMD::Float::type()
4570{
4571 return T(llvm::VectorType::get(T(scalar::Float::type()), SIMD::Width, false));
Nicolas Capens3b0ad202022-06-02 15:02:31 -04004572}
4573
Nicolas Capens6c3dc352020-01-28 09:22:39 -05004574} // namespace rr