blob: 8abb2c6e5e1e1afef6186e29691bce123d62ffaa [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
Nicolas Capenscb986762017-01-20 11:34:37 -050015#include "Reactor.hpp"
Ben Claytoneb50d252019-04-15 13:50:01 -040016#include "Debug.hpp"
Ben Claytonac07ed82019-03-26 14:17:41 +000017#include "LLVMReactor.hpp"
18#include "LLVMReactorDebugInfo.hpp"
John Bauman89401822014-05-06 15:04:28 -040019
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040020#include "x86.hpp"
21#include "CPUID.hpp"
22#include "Thread.hpp"
Nicolas Capens1a3ce872018-10-10 10:42:36 -040023#include "ExecutableMemory.hpp"
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040024#include "MutexLock.hpp"
25
26#undef min
27#undef max
28
Ben Clayton09a7f452019-04-25 15:22:43 +010029#if defined(__clang__)
30// LLVM has occurances of the extra-semi warning in its headers, which will be
31// treated as an error in SwiftShader targets.
32#pragma clang diagnostic push
33#pragma clang diagnostic ignored "-Wextra-semi"
34#endif // defined(__clang__)
35
Ben Clayton5875be52019-04-11 14:57:40 -040036#include "llvm/Analysis/LoopPass.h"
37#include "llvm/ExecutionEngine/ExecutionEngine.h"
38#include "llvm/ExecutionEngine/JITSymbol.h"
39#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
40#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
41#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
42#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
43#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
44#include "llvm/ExecutionEngine/SectionMemoryManager.h"
45#include "llvm/IR/Constants.h"
46#include "llvm/IR/DataLayout.h"
47#include "llvm/IR/Function.h"
48#include "llvm/IR/GlobalVariable.h"
Ben Clayton5875be52019-04-11 14:57:40 -040049#include "llvm/IR/Intrinsics.h"
Ben Clayton1c82c7b2019-04-30 12:49:27 +010050#include "llvm/IR/IRBuilder.h"
Ben Clayton5875be52019-04-11 14:57:40 -040051#include "llvm/IR/LegacyPassManager.h"
Ben Clayton1c82c7b2019-04-30 12:49:27 +010052#include "llvm/IR/LLVMContext.h"
Ben Clayton5875be52019-04-11 14:57:40 -040053#include "llvm/IR/Mangler.h"
54#include "llvm/IR/Module.h"
Ben Clayton4b944652019-05-02 10:56:19 +010055#include "llvm/IR/Verifier.h"
Ben Clayton5875be52019-04-11 14:57:40 -040056#include "llvm/Support/Error.h"
57#include "llvm/Support/TargetSelect.h"
58#include "llvm/Target/TargetOptions.h"
Ben Clayton1c82c7b2019-04-30 12:49:27 +010059#include "llvm/Transforms/Coroutines.h"
Ben Clayton5875be52019-04-11 14:57:40 -040060#include "llvm/Transforms/InstCombine/InstCombine.h"
Ben Clayton1c82c7b2019-04-30 12:49:27 +010061#include "llvm/Transforms/IPO.h"
62#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Ben Clayton5875be52019-04-11 14:57:40 -040063#include "llvm/Transforms/Scalar.h"
64#include "llvm/Transforms/Scalar/GVN.h"
Ben Clayton20507fa2019-04-20 01:40:15 -040065
Ben Clayton09a7f452019-04-25 15:22:43 +010066#if defined(__clang__)
67#pragma clang diagnostic pop
68#endif // defined(__clang__)
69
Ben Clayton5875be52019-04-11 14:57:40 -040070#include "LLVMRoutine.hpp"
John Bauman89401822014-05-06 15:04:28 -040071
Ben Clayton5875be52019-04-11 14:57:40 -040072#define ARGS(...) {__VA_ARGS__}
73#define CreateCall2 CreateCall
74#define CreateCall3 CreateCall
Logan Chien0eedc8c2018-08-21 09:34:28 +080075
Ben Clayton5875be52019-04-11 14:57:40 -040076#include <unordered_map>
Logan Chien0eedc8c2018-08-21 09:34:28 +080077
John Bauman89401822014-05-06 15:04:28 -040078#include <fstream>
Ben Claytoncee3dff2019-05-22 12:01:22 +010079#include <iostream>
80#include <mutex>
Ben Clayton1bc7ee92019-02-14 18:43:22 +000081#include <numeric>
82#include <thread>
John Bauman89401822014-05-06 15:04:28 -040083
Nicolas Capens47dc8672017-04-25 12:54:39 -040084#if defined(__i386__) || defined(__x86_64__)
85#include <xmmintrin.h>
86#endif
87
Logan Chien40a60052018-09-26 19:03:53 +080088#include <math.h>
89
Nicolas Capenscb122582014-05-06 23:34:44 -040090#if defined(__x86_64__) && defined(_WIN32)
Ben Clayton2f58df32019-06-23 21:29:25 +010091 extern "C" void X86CompilationCallback()
92 {
93 UNIMPLEMENTED("X86CompilationCallback");
94 }
95#endif
96
97#if defined(_WIN64)
98 extern "C" void __chkstk();
99#elif defined(_WIN32)
100 extern "C" void _chkstk();
John Bauman66b8ab22014-05-06 15:57:45 -0400101#endif
102
Nicolas Capens48461502018-08-06 14:20:45 -0400103namespace rr
Logan Chien52cde602018-09-03 19:37:57 +0800104{
105 class LLVMReactorJIT;
106}
107
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -0400108namespace
109{
Nicolas Capens48461502018-08-06 14:20:45 -0400110 rr::LLVMReactorJIT *reactorJIT = nullptr;
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -0400111 llvm::IRBuilder<> *builder = nullptr;
112 llvm::LLVMContext *context = nullptr;
113 llvm::Module *module = nullptr;
114 llvm::Function *function = nullptr;
Nicolas Capens3bbc5e12016-09-27 10:49:52 -0400115
Ben Claytonac07ed82019-03-26 14:17:41 +0000116#ifdef ENABLE_RR_DEBUG_INFO
117 std::unique_ptr<rr::DebugInfo> debugInfo;
118#endif
119
Nicolas Capensc07dc4b2018-08-06 14:20:45 -0400120 rr::MutexLock codegenMutex;
Logan Chien0eedc8c2018-08-21 09:34:28 +0800121
Ben Clayton60a3d6f2019-02-26 17:24:46 +0000122#ifdef ENABLE_RR_PRINT
Ben Clayton1bc7ee92019-02-14 18:43:22 +0000123 std::string replace(std::string str, const std::string& substr, const std::string& replacement)
124 {
125 size_t pos = 0;
126 while((pos = str.find(substr, pos)) != std::string::npos) {
127 str.replace(pos, substr.length(), replacement);
128 pos += replacement.length();
129 }
130 return str;
131 }
Ben Clayton60a3d6f2019-02-26 17:24:46 +0000132#endif // ENABLE_RR_PRINT
Ben Clayton1bc7ee92019-02-14 18:43:22 +0000133
Ben Clayton40a885e2019-06-23 19:12:48 +0100134 template <typename T>
135 T alignUp(T val, T alignment)
136 {
137 return alignment * ((val + alignment - 1) / alignment);
138 }
139
140 void* alignedAlloc(size_t size, size_t alignment)
141 {
142 ASSERT(alignment < 256);
143 auto allocation = new uint8_t[size + sizeof(uint8_t) + alignment];
144 auto aligned = allocation;
145 aligned += sizeof(uint8_t); // Make space for the base-address offset.
146 aligned = reinterpret_cast<uint8_t*>(alignUp(reinterpret_cast<uintptr_t>(aligned), alignment)); // align
147 auto offset = static_cast<uint8_t>(aligned - allocation);
148 aligned[-1] = offset;
149 return aligned;
150 }
151
152 void alignedFree(void* ptr)
153 {
154 auto aligned = reinterpret_cast<uint8_t*>(ptr);
155 auto offset = aligned[-1];
156 auto allocation = aligned - offset;
157 delete[] allocation;
158 }
159
Logan Chien0eedc8c2018-08-21 09:34:28 +0800160 llvm::Value *lowerPAVG(llvm::Value *x, llvm::Value *y)
161 {
162 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
163
164 llvm::VectorType *extTy =
165 llvm::VectorType::getExtendedElementVectorType(ty);
166 x = ::builder->CreateZExt(x, extTy);
167 y = ::builder->CreateZExt(y, extTy);
168
169 // (x + y + 1) >> 1
170 llvm::Constant *one = llvm::ConstantInt::get(extTy, 1);
171 llvm::Value *res = ::builder->CreateAdd(x, y);
172 res = ::builder->CreateAdd(res, one);
173 res = ::builder->CreateLShr(res, one);
174 return ::builder->CreateTrunc(res, ty);
175 }
176
177 llvm::Value *lowerPMINMAX(llvm::Value *x, llvm::Value *y,
Logan Chienb5ce5092018-09-27 18:45:58 +0800178 llvm::ICmpInst::Predicate pred)
Logan Chien0eedc8c2018-08-21 09:34:28 +0800179 {
180 return ::builder->CreateSelect(::builder->CreateICmp(pred, x, y), x, y);
181 }
182
183 llvm::Value *lowerPCMP(llvm::ICmpInst::Predicate pred, llvm::Value *x,
Logan Chienb5ce5092018-09-27 18:45:58 +0800184 llvm::Value *y, llvm::Type *dstTy)
Logan Chien0eedc8c2018-08-21 09:34:28 +0800185 {
186 return ::builder->CreateSExt(::builder->CreateICmp(pred, x, y), dstTy, "");
187 }
188
Logan Chiene3191012018-08-24 22:01:50 +0800189#if defined(__i386__) || defined(__x86_64__)
Logan Chien0eedc8c2018-08-21 09:34:28 +0800190 llvm::Value *lowerPMOV(llvm::Value *op, llvm::Type *dstType, bool sext)
191 {
192 llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(op->getType());
193 llvm::VectorType *dstTy = llvm::cast<llvm::VectorType>(dstType);
194
195 llvm::Value *undef = llvm::UndefValue::get(srcTy);
196 llvm::SmallVector<uint32_t, 16> mask(dstTy->getNumElements());
197 std::iota(mask.begin(), mask.end(), 0);
198 llvm::Value *v = ::builder->CreateShuffleVector(op, undef, mask);
199
200 return sext ? ::builder->CreateSExt(v, dstTy)
Logan Chienb5ce5092018-09-27 18:45:58 +0800201 : ::builder->CreateZExt(v, dstTy);
Logan Chien0eedc8c2018-08-21 09:34:28 +0800202 }
203
204 llvm::Value *lowerPABS(llvm::Value *v)
205 {
206 llvm::Value *zero = llvm::Constant::getNullValue(v->getType());
207 llvm::Value *cmp = ::builder->CreateICmp(llvm::ICmpInst::ICMP_SGT, v, zero);
208 llvm::Value *neg = ::builder->CreateNeg(v);
209 return ::builder->CreateSelect(cmp, v, neg);
210 }
211#endif // defined(__i386__) || defined(__x86_64__)
Logan Chiene3191012018-08-24 22:01:50 +0800212
213#if !defined(__i386__) && !defined(__x86_64__)
214 llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y,
Logan Chienb5ce5092018-09-27 18:45:58 +0800215 llvm::FCmpInst::Predicate pred)
Logan Chiene3191012018-08-24 22:01:50 +0800216 {
217 return ::builder->CreateSelect(::builder->CreateFCmp(pred, x, y), x, y);
218 }
219
Logan Chien83fc07a2018-09-26 22:14:00 +0800220 llvm::Value *lowerRound(llvm::Value *x)
221 {
222 llvm::Function *nearbyint = llvm::Intrinsic::getDeclaration(
223 ::module, llvm::Intrinsic::nearbyint, {x->getType()});
224 return ::builder->CreateCall(nearbyint, ARGS(x));
225 }
226
Logan Chien2faa24a2018-09-26 19:59:32 +0800227 llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty)
228 {
229 return ::builder->CreateFPToSI(lowerRound(x), ty);
230 }
231
Logan Chien40a60052018-09-26 19:03:53 +0800232 llvm::Value *lowerFloor(llvm::Value *x)
233 {
234 llvm::Function *floor = llvm::Intrinsic::getDeclaration(
235 ::module, llvm::Intrinsic::floor, {x->getType()});
236 return ::builder->CreateCall(floor, ARGS(x));
237 }
238
Logan Chien8c5ca8d2018-09-27 21:05:53 +0800239 llvm::Value *lowerTrunc(llvm::Value *x)
240 {
241 llvm::Function *trunc = llvm::Intrinsic::getDeclaration(
242 ::module, llvm::Intrinsic::trunc, {x->getType()});
243 return ::builder->CreateCall(trunc, ARGS(x));
244 }
245
Logan Chiene3191012018-08-24 22:01:50 +0800246 // Packed add/sub saturatation
Logan Chien28794cf2018-09-26 18:58:03 +0800247 llvm::Value *lowerPSAT(llvm::Value *x, llvm::Value *y, bool isAdd, bool isSigned)
Logan Chiene3191012018-08-24 22:01:50 +0800248 {
Logan Chien28794cf2018-09-26 18:58:03 +0800249 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
250 llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty);
251
252 unsigned numBits = ty->getScalarSizeInBits();
253
254 llvm::Value *max, *min, *extX, *extY;
255 if (isSigned)
256 {
257 max = llvm::ConstantInt::get(extTy, (1LL << (numBits - 1)) - 1, true);
258 min = llvm::ConstantInt::get(extTy, (-1LL << (numBits - 1)), true);
259 extX = ::builder->CreateSExt(x, extTy);
260 extY = ::builder->CreateSExt(y, extTy);
261 }
262 else
263 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400264 ASSERT_MSG(numBits <= 64, "numBits: %d", int(numBits));
Logan Chien28794cf2018-09-26 18:58:03 +0800265 uint64_t maxVal = (numBits == 64) ? ~0ULL : (1ULL << numBits) - 1;
266 max = llvm::ConstantInt::get(extTy, maxVal, false);
267 min = llvm::ConstantInt::get(extTy, 0, false);
268 extX = ::builder->CreateZExt(x, extTy);
269 extY = ::builder->CreateZExt(y, extTy);
270 }
271
272 llvm::Value *res = isAdd ? ::builder->CreateAdd(extX, extY)
273 : ::builder->CreateSub(extX, extY);
274
275 res = lowerPMINMAX(res, min, llvm::ICmpInst::ICMP_SGT);
276 res = lowerPMINMAX(res, max, llvm::ICmpInst::ICMP_SLT);
277
278 return ::builder->CreateTrunc(res, ty);
Logan Chiene3191012018-08-24 22:01:50 +0800279 }
280
281 llvm::Value *lowerPUADDSAT(llvm::Value *x, llvm::Value *y)
282 {
Logan Chien28794cf2018-09-26 18:58:03 +0800283 return lowerPSAT(x, y, true, false);
Logan Chiene3191012018-08-24 22:01:50 +0800284 }
285
286 llvm::Value *lowerPSADDSAT(llvm::Value *x, llvm::Value *y)
287 {
Logan Chien28794cf2018-09-26 18:58:03 +0800288 return lowerPSAT(x, y, true, true);
Logan Chiene3191012018-08-24 22:01:50 +0800289 }
290
291 llvm::Value *lowerPUSUBSAT(llvm::Value *x, llvm::Value *y)
292 {
Logan Chien28794cf2018-09-26 18:58:03 +0800293 return lowerPSAT(x, y, false, false);
Logan Chiene3191012018-08-24 22:01:50 +0800294 }
295
296 llvm::Value *lowerPSSUBSAT(llvm::Value *x, llvm::Value *y)
297 {
Logan Chien28794cf2018-09-26 18:58:03 +0800298 return lowerPSAT(x, y, false, true);
Logan Chiene3191012018-08-24 22:01:50 +0800299 }
300
301 llvm::Value *lowerSQRT(llvm::Value *x)
302 {
303 llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(
304 ::module, llvm::Intrinsic::sqrt, {x->getType()});
305 return ::builder->CreateCall(sqrt, ARGS(x));
306 }
307
308 llvm::Value *lowerRCP(llvm::Value *x)
309 {
310 llvm::Type *ty = x->getType();
311 llvm::Constant *one;
312 if (llvm::VectorType *vectorTy = llvm::dyn_cast<llvm::VectorType>(ty))
313 {
314 one = llvm::ConstantVector::getSplat(
315 vectorTy->getNumElements(),
316 llvm::ConstantFP::get(vectorTy->getElementType(), 1));
317 }
318 else
319 {
320 one = llvm::ConstantFP::get(ty, 1);
321 }
322 return ::builder->CreateFDiv(one, x);
323 }
324
325 llvm::Value *lowerRSQRT(llvm::Value *x)
326 {
327 return lowerRCP(lowerSQRT(x));
328 }
329
330 llvm::Value *lowerVectorShl(llvm::Value *x, uint64_t scalarY)
331 {
332 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
333 llvm::Value *y = llvm::ConstantVector::getSplat(
334 ty->getNumElements(),
335 llvm::ConstantInt::get(ty->getElementType(), scalarY));
336 return ::builder->CreateShl(x, y);
337 }
338
339 llvm::Value *lowerVectorAShr(llvm::Value *x, uint64_t scalarY)
340 {
341 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
342 llvm::Value *y = llvm::ConstantVector::getSplat(
343 ty->getNumElements(),
344 llvm::ConstantInt::get(ty->getElementType(), scalarY));
345 return ::builder->CreateAShr(x, y);
346 }
347
348 llvm::Value *lowerVectorLShr(llvm::Value *x, uint64_t scalarY)
349 {
350 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
351 llvm::Value *y = llvm::ConstantVector::getSplat(
352 ty->getNumElements(),
353 llvm::ConstantInt::get(ty->getElementType(), scalarY));
354 return ::builder->CreateLShr(x, y);
355 }
356
357 llvm::Value *lowerMulAdd(llvm::Value *x, llvm::Value *y)
358 {
359 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
360 llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty);
361
362 llvm::Value *extX = ::builder->CreateSExt(x, extTy);
363 llvm::Value *extY = ::builder->CreateSExt(y, extTy);
364 llvm::Value *mult = ::builder->CreateMul(extX, extY);
365
366 llvm::Value *undef = llvm::UndefValue::get(extTy);
367
368 llvm::SmallVector<uint32_t, 16> evenIdx;
369 llvm::SmallVector<uint32_t, 16> oddIdx;
370 for (uint64_t i = 0, n = ty->getNumElements(); i < n; i += 2)
371 {
372 evenIdx.push_back(i);
373 oddIdx.push_back(i + 1);
374 }
375
376 llvm::Value *lhs = ::builder->CreateShuffleVector(mult, undef, evenIdx);
377 llvm::Value *rhs = ::builder->CreateShuffleVector(mult, undef, oddIdx);
378 return ::builder->CreateAdd(lhs, rhs);
379 }
380
Logan Chiene3191012018-08-24 22:01:50 +0800381 llvm::Value *lowerPack(llvm::Value *x, llvm::Value *y, bool isSigned)
382 {
383 llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(x->getType());
384 llvm::VectorType *dstTy = llvm::VectorType::getTruncatedElementVectorType(srcTy);
385
386 llvm::IntegerType *dstElemTy =
387 llvm::cast<llvm::IntegerType>(dstTy->getElementType());
388
389 uint64_t truncNumBits = dstElemTy->getIntegerBitWidth();
Ben Claytoneb50d252019-04-15 13:50:01 -0400390 ASSERT_MSG(truncNumBits < 64, "shift 64 must be handled separately. truncNumBits: %d", int(truncNumBits));
Logan Chiene3191012018-08-24 22:01:50 +0800391 llvm::Constant *max, *min;
392 if (isSigned)
393 {
394 max = llvm::ConstantInt::get(srcTy, (1LL << (truncNumBits - 1)) - 1, true);
395 min = llvm::ConstantInt::get(srcTy, (-1LL << (truncNumBits - 1)), true);
396 }
397 else
398 {
399 max = llvm::ConstantInt::get(srcTy, (1ULL << truncNumBits) - 1, false);
400 min = llvm::ConstantInt::get(srcTy, 0, false);
401 }
402
403 x = lowerPMINMAX(x, min, llvm::ICmpInst::ICMP_SGT);
404 x = lowerPMINMAX(x, max, llvm::ICmpInst::ICMP_SLT);
405 y = lowerPMINMAX(y, min, llvm::ICmpInst::ICMP_SGT);
406 y = lowerPMINMAX(y, max, llvm::ICmpInst::ICMP_SLT);
407
408 x = ::builder->CreateTrunc(x, dstTy);
409 y = ::builder->CreateTrunc(y, dstTy);
410
411 llvm::SmallVector<uint32_t, 16> index(srcTy->getNumElements() * 2);
412 std::iota(index.begin(), index.end(), 0);
413
414 return ::builder->CreateShuffleVector(x, y, index);
415 }
416
417 llvm::Value *lowerSignMask(llvm::Value *x, llvm::Type *retTy)
418 {
419 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
420 llvm::Constant *zero = llvm::ConstantInt::get(ty, 0);
421 llvm::Value *cmp = ::builder->CreateICmpSLT(x, zero);
422
423 llvm::Value *ret = ::builder->CreateZExt(
424 ::builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy);
425 for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i)
426 {
427 llvm::Value *elem = ::builder->CreateZExt(
428 ::builder->CreateExtractElement(cmp, i), retTy);
429 ret = ::builder->CreateOr(ret, ::builder->CreateShl(elem, i));
430 }
431 return ret;
432 }
433
434 llvm::Value *lowerFPSignMask(llvm::Value *x, llvm::Type *retTy)
435 {
436 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
437 llvm::Constant *zero = llvm::ConstantFP::get(ty, 0);
438 llvm::Value *cmp = ::builder->CreateFCmpULT(x, zero);
439
440 llvm::Value *ret = ::builder->CreateZExt(
441 ::builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy);
442 for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i)
443 {
444 llvm::Value *elem = ::builder->CreateZExt(
445 ::builder->CreateExtractElement(cmp, i), retTy);
446 ret = ::builder->CreateOr(ret, ::builder->CreateShl(elem, i));
447 }
448 return ret;
449 }
450#endif // !defined(__i386__) && !defined(__x86_64__)
Chris Forbese86b6dc2019-03-01 09:08:47 -0800451
452 llvm::Value *lowerMulHigh(llvm::Value *x, llvm::Value *y, bool sext)
453 {
454 llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType());
455 llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty);
456
457 llvm::Value *extX, *extY;
458 if (sext)
459 {
460 extX = ::builder->CreateSExt(x, extTy);
461 extY = ::builder->CreateSExt(y, extTy);
462 }
463 else
464 {
465 extX = ::builder->CreateZExt(x, extTy);
466 extY = ::builder->CreateZExt(y, extTy);
467 }
468
469 llvm::Value *mult = ::builder->CreateMul(extX, extY);
470
471 llvm::IntegerType *intTy = llvm::cast<llvm::IntegerType>(ty->getElementType());
472 llvm::Value *mulh = ::builder->CreateAShr(mult, intTy->getBitWidth());
473 return ::builder->CreateTrunc(mulh, ty);
474 }
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -0400475}
476
Nicolas Capens48461502018-08-06 14:20:45 -0400477namespace rr
John Bauman89401822014-05-06 15:04:28 -0400478{
Ben Claytonc7904162019-04-17 17:35:48 -0400479 const Capabilities Caps =
480 {
481 true, // CallSupported
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100482 true, // CoroutinesSupported
Ben Claytonc7904162019-04-17 17:35:48 -0400483 };
484
Ben Clayton4d1f8d02019-04-17 23:47:35 -0400485 static std::memory_order atomicOrdering(llvm::AtomicOrdering memoryOrder)
486 {
487 switch(memoryOrder)
488 {
489 case llvm::AtomicOrdering::Monotonic: return std::memory_order_relaxed; // https://llvm.org/docs/Atomics.html#monotonic
490 case llvm::AtomicOrdering::Acquire: return std::memory_order_acquire;
491 case llvm::AtomicOrdering::Release: return std::memory_order_release;
492 case llvm::AtomicOrdering::AcquireRelease: return std::memory_order_acq_rel;
493 case llvm::AtomicOrdering::SequentiallyConsistent: return std::memory_order_seq_cst;
494 default:
Ben Claytonfb280672019-04-25 11:16:15 +0100495 UNREACHABLE("memoryOrder: %d", int(memoryOrder));
Ben Clayton4d1f8d02019-04-17 23:47:35 -0400496 return std::memory_order_acq_rel;
497 }
498 }
499
500 static llvm::AtomicOrdering atomicOrdering(bool atomic, std::memory_order memoryOrder)
501 {
502 if(!atomic)
503 {
504 return llvm::AtomicOrdering::NotAtomic;
505 }
506
507 switch(memoryOrder)
508 {
509 case std::memory_order_relaxed: return llvm::AtomicOrdering::Monotonic; // https://llvm.org/docs/Atomics.html#monotonic
510 case std::memory_order_consume: return llvm::AtomicOrdering::Acquire; // https://llvm.org/docs/Atomics.html#acquire: "It should also be used for C++11/C11 memory_order_consume."
511 case std::memory_order_acquire: return llvm::AtomicOrdering::Acquire;
512 case std::memory_order_release: return llvm::AtomicOrdering::Release;
513 case std::memory_order_acq_rel: return llvm::AtomicOrdering::AcquireRelease;
514 case std::memory_order_seq_cst: return llvm::AtomicOrdering::SequentiallyConsistent;
515 default:
516 UNREACHABLE("memoryOrder: %d", int(memoryOrder));
517 return llvm::AtomicOrdering::AcquireRelease;
518 }
519 }
520
521 template <typename T>
522 static void atomicLoad(void *ptr, void *ret, llvm::AtomicOrdering ordering)
523 {
524 *reinterpret_cast<T*>(ret) = std::atomic_load_explicit<T>(reinterpret_cast<std::atomic<T>*>(ptr), atomicOrdering(ordering));
525 }
526
527 template <typename T>
528 static void atomicStore(void *ptr, void *val, llvm::AtomicOrdering ordering)
529 {
530 std::atomic_store_explicit<T>(reinterpret_cast<std::atomic<T>*>(ptr), *reinterpret_cast<T*>(val), atomicOrdering(ordering));
531 }
532
Chris Forbesfd4c96d2019-06-20 11:20:42 -0700533#ifdef __ANDROID__
534 template<typename F>
535 static uint32_t sync_fetch_and_op(uint32_t volatile *ptr, uint32_t val, F f)
536 {
537 // Build an arbitrary op out of looped CAS
538 for (;;)
539 {
540 uint32_t expected = *ptr;
541 uint32_t desired = f(expected, val);
542
543 if (expected == __sync_val_compare_and_swap_4(ptr, expected, desired))
544 return expected;
545 }
546 }
547#endif
548
Logan Chien40a60052018-09-26 19:03:53 +0800549 class ExternalFunctionSymbolResolver
550 {
551 private:
552 using FunctionMap = std::unordered_map<std::string, void *>;
553 FunctionMap func_;
554
555 public:
556 ExternalFunctionSymbolResolver()
557 {
Ben Clayton4d1f8d02019-04-17 23:47:35 -0400558 struct Atomic
559 {
560 static void load(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering)
561 {
562 switch (size)
563 {
564 case 1: atomicLoad<uint8_t>(ptr, ret, ordering); break;
565 case 2: atomicLoad<uint16_t>(ptr, ret, ordering); break;
566 case 4: atomicLoad<uint32_t>(ptr, ret, ordering); break;
567 case 8: atomicLoad<uint64_t>(ptr, ret, ordering); break;
568 default:
569 UNIMPLEMENTED("Atomic::load(size: %d)", int(size));
570 }
571 }
572 static void store(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering)
573 {
574 switch (size)
575 {
576 case 1: atomicStore<uint8_t>(ptr, ret, ordering); break;
577 case 2: atomicStore<uint16_t>(ptr, ret, ordering); break;
578 case 4: atomicStore<uint32_t>(ptr, ret, ordering); break;
579 case 8: atomicStore<uint64_t>(ptr, ret, ordering); break;
580 default:
581 UNIMPLEMENTED("Atomic::store(size: %d)", int(size));
582 }
583 }
584 };
Chris Forbes9283b252019-06-17 09:44:28 -0700585 struct F
586 {
587 static void nop() {}
588 static void neverCalled() { UNREACHABLE("Should never be called"); }
589
Ben Clayton40a885e2019-06-23 19:12:48 +0100590 static void* coroutine_alloc_frame(size_t size) { return alignedAlloc(size, 16); }
591 static void coroutine_free_frame(void* ptr) { alignedFree(ptr); }
592
Chris Forbes9283b252019-06-17 09:44:28 -0700593#ifdef __ANDROID__
594 // forwarders since we can't take address of builtins
595 static void sync_synchronize() { __sync_synchronize(); }
596 static uint32_t sync_fetch_and_add_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_add_4(ptr, val); }
Chris Forbesfd4c96d2019-06-20 11:20:42 -0700597 static uint32_t sync_fetch_and_and_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_and_4(ptr, val); }
598 static uint32_t sync_fetch_and_or_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_or_4(ptr, val); }
599 static uint32_t sync_fetch_and_xor_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_xor_4(ptr, val); }
600 static uint32_t sync_fetch_and_sub_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_sub_4(ptr, val); }
601 static uint32_t sync_lock_test_and_set_4(uint32_t *ptr, uint32_t val) { return __sync_lock_test_and_set_4(ptr, val); }
602 static uint32_t sync_val_compare_and_swap_4(uint32_t *ptr, uint32_t expected, uint32_t desired) { return __sync_val_compare_and_swap_4(ptr, expected, desired); }
603
604 static uint32_t sync_fetch_and_max_4(uint32_t *ptr, uint32_t val) { return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::max(a,b);}); }
605 static uint32_t sync_fetch_and_min_4(uint32_t *ptr, uint32_t val) { return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::min(a,b);}); }
606 static uint32_t sync_fetch_and_umax_4(uint32_t *ptr, uint32_t val) { return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::max(a,b);}); }
607 static uint32_t sync_fetch_and_umin_4(uint32_t *ptr, uint32_t val) { return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::min(a,b);}); }
Chris Forbes9283b252019-06-17 09:44:28 -0700608#endif
609 };
Ben Claytonac07ed82019-03-26 14:17:41 +0000610
Ben Clayton4d1f8d02019-04-17 23:47:35 -0400611 func_.emplace("nop", reinterpret_cast<void*>(F::nop));
Logan Chien40a60052018-09-26 19:03:53 +0800612 func_.emplace("floorf", reinterpret_cast<void*>(floorf));
Logan Chien83fc07a2018-09-26 22:14:00 +0800613 func_.emplace("nearbyintf", reinterpret_cast<void*>(nearbyintf));
Logan Chien8c5ca8d2018-09-27 21:05:53 +0800614 func_.emplace("truncf", reinterpret_cast<void*>(truncf));
Ben Clayton1bc7ee92019-02-14 18:43:22 +0000615 func_.emplace("printf", reinterpret_cast<void*>(printf));
616 func_.emplace("puts", reinterpret_cast<void*>(puts));
Chris Forbes1a4c7122019-03-15 14:50:47 -0700617 func_.emplace("fmodf", reinterpret_cast<void*>(fmodf));
Ben Claytonb5f1a132019-06-24 11:00:53 +0100618
Ben Claytona2c8b772019-04-09 13:42:36 -0400619 func_.emplace("sinf", reinterpret_cast<void*>(sinf));
Ben Clayton1b6f8c72019-04-09 13:47:43 -0400620 func_.emplace("cosf", reinterpret_cast<void*>(cosf));
Ben Claytonf9350d72019-04-09 14:19:02 -0400621 func_.emplace("asinf", reinterpret_cast<void*>(asinf));
Ben Claytoneafae472019-04-09 14:22:38 -0400622 func_.emplace("acosf", reinterpret_cast<void*>(acosf));
Ben Clayton749b4e02019-04-09 14:27:43 -0400623 func_.emplace("atanf", reinterpret_cast<void*>(atanf));
Ben Claytond9636972019-04-09 15:09:54 -0400624 func_.emplace("sinhf", reinterpret_cast<void*>(sinhf));
Ben Clayton900ea2c2019-04-09 15:25:36 -0400625 func_.emplace("coshf", reinterpret_cast<void*>(coshf));
Ben Clayton3928bd92019-04-09 15:27:41 -0400626 func_.emplace("tanhf", reinterpret_cast<void*>(tanhf));
Ben Claytonf6d77ab2019-04-09 15:30:04 -0400627 func_.emplace("asinhf", reinterpret_cast<void*>(asinhf));
Ben Clayton28ebcb02019-04-09 15:33:38 -0400628 func_.emplace("acoshf", reinterpret_cast<void*>(acoshf));
Ben Claytonfa6a5392019-04-09 15:35:24 -0400629 func_.emplace("atanhf", reinterpret_cast<void*>(atanhf));
Ben Claytona520c3e2019-04-09 15:43:45 -0400630 func_.emplace("atan2f", reinterpret_cast<void*>(atan2f));
Ben Claytonbfe94f02019-04-09 15:52:12 -0400631 func_.emplace("powf", reinterpret_cast<void*>(powf));
Ben Clayton242f0022019-04-09 16:00:53 -0400632 func_.emplace("expf", reinterpret_cast<void*>(expf));
Ben Clayton2c1da722019-04-09 16:03:03 -0400633 func_.emplace("logf", reinterpret_cast<void*>(logf));
Ben Claytonf40b56c2019-04-09 16:06:55 -0400634 func_.emplace("exp2f", reinterpret_cast<void*>(exp2f));
Ben Claytone17acfe2019-04-09 16:09:13 -0400635 func_.emplace("log2f", reinterpret_cast<void*>(log2f));
Ben Claytonb5f1a132019-06-24 11:00:53 +0100636
637 func_.emplace("sin", reinterpret_cast<void*>(static_cast<double(*)(double)>(sin)));
638 func_.emplace("cos", reinterpret_cast<void*>(static_cast<double(*)(double)>(cos)));
639 func_.emplace("asin", reinterpret_cast<void*>(static_cast<double(*)(double)>(asin)));
640 func_.emplace("acos", reinterpret_cast<void*>(static_cast<double(*)(double)>(acos)));
641 func_.emplace("atan", reinterpret_cast<void*>(static_cast<double(*)(double)>(atan)));
642 func_.emplace("sinh", reinterpret_cast<void*>(static_cast<double(*)(double)>(sinh)));
643 func_.emplace("cosh", reinterpret_cast<void*>(static_cast<double(*)(double)>(cosh)));
644 func_.emplace("tanh", reinterpret_cast<void*>(static_cast<double(*)(double)>(tanh)));
645 func_.emplace("asinh", reinterpret_cast<void*>(static_cast<double(*)(double)>(asinh)));
646 func_.emplace("acosh", reinterpret_cast<void*>(static_cast<double(*)(double)>(acosh)));
647 func_.emplace("atanh", reinterpret_cast<void*>(static_cast<double(*)(double)>(atanh)));
648 func_.emplace("atan2", reinterpret_cast<void*>(static_cast<double(*)(double,double)>(atan2)));
649 func_.emplace("pow", reinterpret_cast<void*>(static_cast<double(*)(double,double)>(pow)));
650 func_.emplace("exp", reinterpret_cast<void*>(static_cast<double(*)(double)>(exp)));
651 func_.emplace("log", reinterpret_cast<void*>(static_cast<double(*)(double)>(log)));
652 func_.emplace("exp2", reinterpret_cast<void*>(static_cast<double(*)(double)>(exp2)));
653 func_.emplace("log2", reinterpret_cast<void*>(static_cast<double(*)(double)>(log2)));
654
Ben Clayton4d1f8d02019-04-17 23:47:35 -0400655 func_.emplace("atomic_load", reinterpret_cast<void*>(Atomic::load));
656 func_.emplace("atomic_store", reinterpret_cast<void*>(Atomic::store));
Ben Clayton14740062019-04-09 13:48:41 -0400657
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100658 // FIXME (b/119409619): use an allocator here so we can control all memory allocations
Ben Clayton40a885e2019-06-23 19:12:48 +0100659 func_.emplace("coroutine_alloc_frame", reinterpret_cast<void*>(F::coroutine_alloc_frame));
660 func_.emplace("coroutine_free_frame", reinterpret_cast<void*>(F::coroutine_free_frame));
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100661
Ben Clayton14740062019-04-09 13:48:41 -0400662#ifdef __APPLE__
Ben Clayton14740062019-04-09 13:48:41 -0400663 func_.emplace("sincosf_stret", reinterpret_cast<void*>(__sincosf_stret));
664#elif defined(__linux__)
665 func_.emplace("sincosf", reinterpret_cast<void*>(sincosf));
Ben Clayton2f58df32019-06-23 21:29:25 +0100666#elif defined(_WIN64)
667 func_.emplace("chkstk", reinterpret_cast<void*>(__chkstk));
668#elif defined(_WIN32)
669 func_.emplace("chkstk", reinterpret_cast<void*>(_chkstk));
670#endif
Chris Forbes9283b252019-06-17 09:44:28 -0700671
672#ifdef __ANDROID__
673 func_.emplace("aeabi_unwind_cpp_pr0", reinterpret_cast<void*>(F::neverCalled));
674 func_.emplace("sync_synchronize", reinterpret_cast<void*>(F::sync_synchronize));
675 func_.emplace("sync_fetch_and_add_4", reinterpret_cast<void*>(F::sync_fetch_and_add_4));
Chris Forbesfd4c96d2019-06-20 11:20:42 -0700676 func_.emplace("sync_fetch_and_and_4", reinterpret_cast<void*>(F::sync_fetch_and_and_4));
677 func_.emplace("sync_fetch_and_or_4", reinterpret_cast<void*>(F::sync_fetch_and_or_4));
678 func_.emplace("sync_fetch_and_xor_4", reinterpret_cast<void*>(F::sync_fetch_and_xor_4));
679 func_.emplace("sync_fetch_and_sub_4", reinterpret_cast<void*>(F::sync_fetch_and_sub_4));
680 func_.emplace("sync_lock_test_and_set_4", reinterpret_cast<void*>(F::sync_lock_test_and_set_4));
681 func_.emplace("sync_val_compare_and_swap_4", reinterpret_cast<void*>(F::sync_val_compare_and_swap_4));
682 func_.emplace("sync_fetch_and_max_4", reinterpret_cast<void*>(F::sync_fetch_and_max_4));
683 func_.emplace("sync_fetch_and_min_4", reinterpret_cast<void*>(F::sync_fetch_and_min_4));
684 func_.emplace("sync_fetch_and_umax_4", reinterpret_cast<void*>(F::sync_fetch_and_umax_4));
685 func_.emplace("sync_fetch_and_umin_4", reinterpret_cast<void*>(F::sync_fetch_and_umin_4));
Chris Forbes9283b252019-06-17 09:44:28 -0700686#endif
Logan Chien40a60052018-09-26 19:03:53 +0800687 }
688
689 void *findSymbol(const std::string &name) const
690 {
Ben Clayton1bc7ee92019-02-14 18:43:22 +0000691 // Trim off any underscores from the start of the symbol. LLVM likes
692 // to append these on macOS.
693 const char* trimmed = name.c_str();
694 while (trimmed[0] == '_') { trimmed++; }
695
696 FunctionMap::const_iterator it = func_.find(trimmed);
Ben Claytoneb50d252019-04-15 13:50:01 -0400697 // Missing functions will likely make the module fail in exciting non-obvious ways.
698 ASSERT_MSG(it != func_.end(), "Missing external function: '%s'", name.c_str());
Ben Clayton1bc7ee92019-02-14 18:43:22 +0000699 return it->second;
Logan Chien40a60052018-09-26 19:03:53 +0800700 }
701 };
702
Logan Chien0eedc8c2018-08-21 09:34:28 +0800703 class LLVMReactorJIT
704 {
705 private:
706 using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer;
707 using CompileLayer = llvm::orc::IRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>;
708
709 llvm::orc::ExecutionSession session;
Logan Chien40a60052018-09-26 19:03:53 +0800710 ExternalFunctionSymbolResolver externalSymbolResolver;
Logan Chien0eedc8c2018-08-21 09:34:28 +0800711 std::shared_ptr<llvm::orc::SymbolResolver> resolver;
712 std::unique_ptr<llvm::TargetMachine> targetMachine;
713 const llvm::DataLayout dataLayout;
714 ObjLayer objLayer;
Ben Claytoncee3dff2019-05-22 12:01:22 +0100715 CompileLayer compileLayer; // guarded by mutex
716 std::mutex mutex;
Logan Chien0eedc8c2018-08-21 09:34:28 +0800717 size_t emittedFunctionsNum;
718
719 public:
720 LLVMReactorJIT(const char *arch, const llvm::SmallVectorImpl<std::string>& mattrs,
721 const llvm::TargetOptions &targetOpts):
722 resolver(createLegacyLookupResolver(
723 session,
724 [this](const std::string &name) {
Logan Chien40a60052018-09-26 19:03:53 +0800725 void *func = externalSymbolResolver.findSymbol(name);
726 if (func != nullptr)
727 {
728 return llvm::JITSymbol(
729 reinterpret_cast<uintptr_t>(func), llvm::JITSymbolFlags::Absolute);
730 }
731
Logan Chien0eedc8c2018-08-21 09:34:28 +0800732 return objLayer.findSymbol(name, true);
733 },
734 [](llvm::Error err) {
735 if (err)
736 {
737 // TODO: Log the symbol resolution errors.
738 return;
739 }
740 })),
741 targetMachine(llvm::EngineBuilder()
Ben Claytonac07ed82019-03-26 14:17:41 +0000742#ifdef ENABLE_RR_DEBUG_INFO
743 .setOptLevel(llvm::CodeGenOpt::None)
744#endif // ENABLE_RR_DEBUG_INFO
Logan Chien0eedc8c2018-08-21 09:34:28 +0800745 .setMArch(arch)
746 .setMAttrs(mattrs)
747 .setTargetOptions(targetOpts)
748 .selectTarget()),
749 dataLayout(targetMachine->createDataLayout()),
750 objLayer(
751 session,
752 [this](llvm::orc::VModuleKey) {
753 return ObjLayer::Resources{
754 std::make_shared<llvm::SectionMemoryManager>(),
755 resolver};
Ben Claytonac07ed82019-03-26 14:17:41 +0000756 },
757 ObjLayer::NotifyLoadedFtor(),
758 [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj, const llvm::RuntimeDyld::LoadedObjectInfo &L) {
759#ifdef ENABLE_RR_DEBUG_INFO
Ben Clayton90cb2602019-05-23 14:42:32 +0100760 DebugInfo::NotifyObjectEmitted(Obj, L);
Ben Claytonac07ed82019-03-26 14:17:41 +0000761#endif // ENABLE_RR_DEBUG_INFO
762 },
763 [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj) {
764#ifdef ENABLE_RR_DEBUG_INFO
Ben Clayton90cb2602019-05-23 14:42:32 +0100765 DebugInfo::NotifyFreeingObject(Obj);
Ben Claytonac07ed82019-03-26 14:17:41 +0000766#endif // ENABLE_RR_DEBUG_INFO
767 }
768 ),
Logan Chien0eedc8c2018-08-21 09:34:28 +0800769 compileLayer(objLayer, llvm::orc::SimpleCompiler(*targetMachine)),
770 emittedFunctionsNum(0)
771 {
772 }
773
774 void startSession()
775 {
776 ::module = new llvm::Module("", *::context);
Nicolas Capens900e1da2019-06-23 00:14:55 -0400777 ::module->setDataLayout(dataLayout);
Logan Chien0eedc8c2018-08-21 09:34:28 +0800778 }
779
780 void endSession()
781 {
782 ::function = nullptr;
783 ::module = nullptr;
784 }
785
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100786 LLVMRoutine *acquireRoutine(llvm::Function **funcs, size_t count)
Logan Chien0eedc8c2018-08-21 09:34:28 +0800787 {
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100788 std::vector<std::string> mangledNames(count);
789 for (size_t i = 0; i < count; i++)
790 {
791 auto func = funcs[i];
792 std::string name = "f" + llvm::Twine(emittedFunctionsNum++).str();
793 func->setName(name);
794 func->setLinkage(llvm::GlobalValue::ExternalLinkage);
795 func->setDoesNotThrow();
Logan Chien0eedc8c2018-08-21 09:34:28 +0800796
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100797 llvm::raw_string_ostream mangledNameStream(mangledNames[i]);
798 llvm::Mangler::getNameWithPrefix(mangledNameStream, name, dataLayout);
799 }
800
801 // Compile the module - after this the llvm::Functions will have
802 // been freed.
Logan Chien0eedc8c2018-08-21 09:34:28 +0800803 std::unique_ptr<llvm::Module> mod(::module);
804 ::module = nullptr;
Logan Chien0eedc8c2018-08-21 09:34:28 +0800805
806 auto moduleKey = session.allocateVModule();
Ben Claytoncee3dff2019-05-22 12:01:22 +0100807
808 // Resolve the function symbols - needs to be performed under mutex lock.
809 std::vector<llvm::JITSymbol> symbols;
810 {
811 std::unique_lock<std::mutex> lock(mutex);
812 llvm::cantFail(compileLayer.addModule(moduleKey, std::move(mod)));
813 funcs = nullptr; // Now points to released memory.
814 for (size_t i = 0; i < count; i++)
815 {
816 symbols.push_back(compileLayer.findSymbolIn(moduleKey, mangledNames[i], false));
817 }
818 }
Logan Chien0eedc8c2018-08-21 09:34:28 +0800819
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100820 // Resolve the function addresses.
821 std::vector<void*> addresses(count);
822 for (size_t i = 0; i < count; i++)
Nicolas Capensadfbbcb2018-10-31 14:38:53 -0400823 {
Ben Claytoncee3dff2019-05-22 12:01:22 +0100824 if(auto expectAddr = symbols[i].getAddress())
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100825 {
Ben Claytoncee3dff2019-05-22 12:01:22 +0100826 addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(expectAddr.get()));
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100827 }
Nicolas Capensadfbbcb2018-10-31 14:38:53 -0400828 }
829
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100830 return new LLVMRoutine(addresses.data(), count, releaseRoutineCallback, this, moduleKey);
Logan Chien0eedc8c2018-08-21 09:34:28 +0800831 }
832
833 void optimize(llvm::Module *module)
834 {
Ben Claytonac07ed82019-03-26 14:17:41 +0000835#ifdef ENABLE_RR_DEBUG_INFO
836 if (debugInfo != nullptr)
837 {
838 return; // Don't optimize if we're generating debug info.
839 }
840#endif // ENABLE_RR_DEBUG_INFO
841
Logan Chien0eedc8c2018-08-21 09:34:28 +0800842 std::unique_ptr<llvm::legacy::PassManager> passManager(
843 new llvm::legacy::PassManager());
844
845 passManager->add(llvm::createSROAPass());
846
847 for(int pass = 0; pass < 10 && optimization[pass] != Disabled; pass++)
848 {
849 switch(optimization[pass])
850 {
851 case Disabled: break;
852 case CFGSimplification: passManager->add(llvm::createCFGSimplificationPass()); break;
853 case LICM: passManager->add(llvm::createLICMPass()); break;
854 case AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); break;
855 case GVN: passManager->add(llvm::createGVNPass()); break;
856 case InstructionCombining: passManager->add(llvm::createInstructionCombiningPass()); break;
857 case Reassociate: passManager->add(llvm::createReassociatePass()); break;
858 case DeadStoreElimination: passManager->add(llvm::createDeadStoreEliminationPass()); break;
859 case SCCP: passManager->add(llvm::createSCCPPass()); break;
860 case ScalarReplAggregates: passManager->add(llvm::createSROAPass()); break;
861 default:
Ben Claytoneb50d252019-04-15 13:50:01 -0400862 UNREACHABLE("optimization[pass]: %d, pass: %d", int(optimization[pass]), int(pass));
Logan Chien0eedc8c2018-08-21 09:34:28 +0800863 }
864 }
865
866 passManager->run(*::module);
867 }
868
869 private:
870 void releaseRoutineModule(llvm::orc::VModuleKey moduleKey)
871 {
Ben Claytoncee3dff2019-05-22 12:01:22 +0100872 std::unique_lock<std::mutex> lock(mutex);
Logan Chien0eedc8c2018-08-21 09:34:28 +0800873 llvm::cantFail(compileLayer.removeModule(moduleKey));
874 }
875
876 static void releaseRoutineCallback(LLVMReactorJIT *jit, uint64_t moduleKey)
877 {
878 jit->releaseRoutineModule(moduleKey);
879 }
880 };
Logan Chien52cde602018-09-03 19:37:57 +0800881
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -0400882 Optimization optimization[10] = {InstructionCombining, Disabled};
John Bauman89401822014-05-06 15:04:28 -0400883
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500884 // The abstract Type* types are implemented as LLVM types, except that
885 // 64-bit vectors are emulated using 128-bit ones to avoid use of MMX in x86
886 // and VFP in ARM, and eliminate the overhead of converting them to explicit
887 // 128-bit ones. LLVM types are pointers, so we can represent emulated types
888 // as abstract pointers with small enum values.
889 enum InternalType : uintptr_t
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400890 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500891 // Emulated types:
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400892 Type_v2i32,
893 Type_v4i16,
894 Type_v2i16,
895 Type_v8i8,
896 Type_v4i8,
897 Type_v2f32,
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500898 EmulatedTypeCount,
899 // Returned by asInternalType() to indicate that the abstract Type*
900 // should be interpreted as LLVM type pointer:
901 Type_LLVM
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400902 };
903
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500904 inline InternalType asInternalType(Type *type)
905 {
906 InternalType t = static_cast<InternalType>(reinterpret_cast<uintptr_t>(type));
907 return (t < EmulatedTypeCount) ? t : Type_LLVM;
908 }
909
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400910 llvm::Type *T(Type *t)
911 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500912 // Use 128-bit vectors to implement logically shorter ones.
913 switch(asInternalType(t))
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400914 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500915 case Type_v2i32: return T(Int4::getType());
916 case Type_v4i16: return T(Short8::getType());
917 case Type_v2i16: return T(Short8::getType());
918 case Type_v8i8: return T(Byte16::getType());
919 case Type_v4i8: return T(Byte16::getType());
920 case Type_v2f32: return T(Float4::getType());
921 case Type_LLVM: return reinterpret_cast<llvm::Type*>(t);
Ben Claytoneb50d252019-04-15 13:50:01 -0400922 default:
923 UNREACHABLE("asInternalType(t): %d", int(asInternalType(t)));
924 return nullptr;
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400925 }
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400926 }
927
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500928 Type *T(InternalType t)
Nicolas Capensfbf2bc52017-07-26 17:26:17 -0400929 {
930 return reinterpret_cast<Type*>(t);
931 }
932
Nicolas Capensac230122016-09-20 14:30:06 -0400933 inline std::vector<llvm::Type*> &T(std::vector<Type*> &t)
934 {
935 return reinterpret_cast<std::vector<llvm::Type*>&>(t);
936 }
937
Logan Chien191b3052018-08-31 16:57:15 +0800938 inline llvm::BasicBlock *B(BasicBlock *t)
939 {
940 return reinterpret_cast<llvm::BasicBlock*>(t);
941 }
942
Nicolas Capensc8b67a42016-09-25 15:02:52 -0400943 inline BasicBlock *B(llvm::BasicBlock *t)
944 {
945 return reinterpret_cast<BasicBlock*>(t);
946 }
947
Nicolas Capens01a97962017-07-28 17:30:51 -0400948 static size_t typeSize(Type *type)
949 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500950 switch(asInternalType(type))
Nicolas Capens01a97962017-07-28 17:30:51 -0400951 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500952 case Type_v2i32: return 8;
953 case Type_v4i16: return 8;
954 case Type_v2i16: return 4;
955 case Type_v8i8: return 8;
956 case Type_v4i8: return 4;
957 case Type_v2f32: return 8;
958 case Type_LLVM:
Nicolas Capens01a97962017-07-28 17:30:51 -0400959 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500960 llvm::Type *t = T(type);
Nicolas Capens01a97962017-07-28 17:30:51 -0400961
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500962 if(t->isPointerTy())
963 {
964 return sizeof(void*);
965 }
966
967 // At this point we should only have LLVM 'primitive' types.
968 unsigned int bits = t->getPrimitiveSizeInBits();
Ben Claytoneb50d252019-04-15 13:50:01 -0400969 ASSERT_MSG(bits != 0, "bits: %d", int(bits));
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500970
971 // TODO(capn): Booleans are 1 bit integers in LLVM's SSA type system,
972 // but are typically stored as one byte. The DataLayout structure should
973 // be used here and many other places if this assumption fails.
974 return (bits + 7) / 8;
975 }
976 break;
977 default:
Ben Claytoneb50d252019-04-15 13:50:01 -0400978 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500979 return 0;
980 }
Nicolas Capens01a97962017-07-28 17:30:51 -0400981 }
982
Nicolas Capens69674fb2017-09-01 11:08:44 -0400983 static unsigned int elementCount(Type *type)
984 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500985 switch(asInternalType(type))
Nicolas Capens69674fb2017-09-01 11:08:44 -0400986 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -0500987 case Type_v2i32: return 2;
988 case Type_v4i16: return 4;
989 case Type_v2i16: return 2;
990 case Type_v8i8: return 8;
991 case Type_v4i8: return 4;
992 case Type_v2f32: return 2;
993 case Type_LLVM: return llvm::cast<llvm::VectorType>(T(type))->getNumElements();
Ben Claytoneb50d252019-04-15 13:50:01 -0400994 default:
995 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
996 return 0;
Nicolas Capens69674fb2017-09-01 11:08:44 -0400997 }
Nicolas Capens69674fb2017-09-01 11:08:44 -0400998 }
999
Ben Claytonea38f952019-06-17 13:56:56 +01001000 static ::llvm::Function* createFunction(const char *name, ::llvm::Type *retTy, const std::vector<::llvm::Type*> &params)
1001 {
1002 llvm::FunctionType *functionType = llvm::FunctionType::get(retTy, params, false);
1003 auto func = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, name, ::module);
1004 func->setDoesNotThrow();
1005 func->setCallingConv(llvm::CallingConv::C);
Ben Claytonea38f952019-06-17 13:56:56 +01001006 return func;
1007 }
1008
John Bauman89401822014-05-06 15:04:28 -04001009 Nucleus::Nucleus()
1010 {
Nicolas Capens3bbc5e12016-09-27 10:49:52 -04001011 ::codegenMutex.lock(); // Reactor and LLVM are currently not thread safe
Nicolas Capensb7ea9842015-04-01 10:54:59 -04001012
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001013 llvm::InitializeNativeTarget();
Logan Chien0eedc8c2018-08-21 09:34:28 +08001014 llvm::InitializeNativeTargetAsmPrinter();
1015 llvm::InitializeNativeTargetAsmParser();
Logan Chien0eedc8c2018-08-21 09:34:28 +08001016
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -04001017 if(!::context)
John Bauman89401822014-05-06 15:04:28 -04001018 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001019 ::context = new llvm::LLVMContext();
John Bauman89401822014-05-06 15:04:28 -04001020 }
1021
John Bauman89401822014-05-06 15:04:28 -04001022 #if defined(__x86_64__)
Logan Chien52cde602018-09-03 19:37:57 +08001023 static const char arch[] = "x86-64";
Logan Chiene3191012018-08-24 22:01:50 +08001024 #elif defined(__i386__)
Logan Chien52cde602018-09-03 19:37:57 +08001025 static const char arch[] = "x86";
Logan Chiene3191012018-08-24 22:01:50 +08001026 #elif defined(__aarch64__)
1027 static const char arch[] = "arm64";
1028 #elif defined(__arm__)
1029 static const char arch[] = "arm";
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +02001030 #elif defined(__mips__)
Gordana Cmiljanovic20622c02018-11-05 15:00:11 +01001031 #if defined(__mips64)
1032 static const char arch[] = "mips64el";
1033 #else
1034 static const char arch[] = "mipsel";
1035 #endif
Colin Samples8fd53302019-06-13 09:57:44 -04001036 #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1037 static const char arch[] = "ppc64le";
Logan Chiene3191012018-08-24 22:01:50 +08001038 #else
1039 #error "unknown architecture"
John Bauman89401822014-05-06 15:04:28 -04001040 #endif
1041
Ben Clayton0fc611f2019-04-18 11:23:27 -04001042 llvm::SmallVector<std::string, 8> mattrs;
1043
1044 llvm::StringMap<bool> features;
Colin Samples8fd53302019-06-13 09:57:44 -04001045
Ben Clayton0fc611f2019-04-18 11:23:27 -04001046 bool ok = llvm::sys::getHostCPUFeatures(features);
Colin Samples8fd53302019-06-13 09:57:44 -04001047
1048 #if defined(__i386__) || defined(__x86_64__) || \
1049 (defined(__linux__) && (defined(__arm__) || defined(__aarch64__)))
Ben Clayton0fc611f2019-04-18 11:23:27 -04001050 ASSERT_MSG(ok, "llvm::sys::getHostCPUFeatures returned false");
Colin Samples8fd53302019-06-13 09:57:44 -04001051 #else
1052 (void) ok; // getHostCPUFeatures always returns false on other platforms
1053 #endif
1054
Ben Clayton0fc611f2019-04-18 11:23:27 -04001055 for (auto &feature : features)
1056 {
1057 if (feature.second) { mattrs.push_back(feature.first()); }
1058 }
1059
1060#if 0
Logan Chiene3191012018-08-24 22:01:50 +08001061#if defined(__i386__) || defined(__x86_64__)
Logan Chien0eedc8c2018-08-21 09:34:28 +08001062 mattrs.push_back(CPUID::supportsMMX() ? "+mmx" : "-mmx");
1063 mattrs.push_back(CPUID::supportsCMOV() ? "+cmov" : "-cmov");
1064 mattrs.push_back(CPUID::supportsSSE() ? "+sse" : "-sse");
1065 mattrs.push_back(CPUID::supportsSSE2() ? "+sse2" : "-sse2");
1066 mattrs.push_back(CPUID::supportsSSE3() ? "+sse3" : "-sse3");
1067 mattrs.push_back(CPUID::supportsSSSE3() ? "+ssse3" : "-ssse3");
Logan Chien0eedc8c2018-08-21 09:34:28 +08001068 mattrs.push_back(CPUID::supportsSSE4_1() ? "+sse4.1" : "-sse4.1");
Logan Chiene3191012018-08-24 22:01:50 +08001069#elif defined(__arm__)
1070#if __ARM_ARCH >= 8
1071 mattrs.push_back("+armv8-a");
1072#else
1073 // armv7-a requires compiler-rt routines; otherwise, compiled kernel
1074 // might fail to link.
1075#endif
1076#endif
Ben Clayton0fc611f2019-04-18 11:23:27 -04001077#endif
John Bauman89401822014-05-06 15:04:28 -04001078
Logan Chien0eedc8c2018-08-21 09:34:28 +08001079 llvm::TargetOptions targetOpts;
Nicolas Capensa7643812018-09-13 14:20:06 -04001080 targetOpts.UnsafeFPMath = false;
Logan Chien0eedc8c2018-08-21 09:34:28 +08001081 // targetOpts.NoInfsFPMath = true;
1082 // targetOpts.NoNaNsFPMath = true;
Logan Chien52cde602018-09-03 19:37:57 +08001083
1084 if(!::reactorJIT)
1085 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08001086 ::reactorJIT = new LLVMReactorJIT(arch, mattrs, targetOpts);
Logan Chien52cde602018-09-03 19:37:57 +08001087 }
1088
1089 ::reactorJIT->startSession();
John Bauman89401822014-05-06 15:04:28 -04001090
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -04001091 if(!::builder)
John Bauman89401822014-05-06 15:04:28 -04001092 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001093 ::builder = new llvm::IRBuilder<>(*::context);
John Bauman89401822014-05-06 15:04:28 -04001094 }
1095 }
1096
1097 Nucleus::~Nucleus()
1098 {
Ben Clayton90cb2602019-05-23 14:42:32 +01001099#ifdef ENABLE_RR_DEBUG_INFO
1100 debugInfo.reset(nullptr);
1101#endif // ENABLE_RR_DEBUG_INFO
1102
Logan Chien52cde602018-09-03 19:37:57 +08001103 ::reactorJIT->endSession();
Nicolas Capensb7ea9842015-04-01 10:54:59 -04001104
Nicolas Capens3bbc5e12016-09-27 10:49:52 -04001105 ::codegenMutex.unlock();
John Bauman89401822014-05-06 15:04:28 -04001106 }
1107
Chris Forbes878d4b02019-01-21 10:48:35 -08001108 Routine *Nucleus::acquireRoutine(const char *name, bool runOptimizations)
John Bauman89401822014-05-06 15:04:28 -04001109 {
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -04001110 if(::builder->GetInsertBlock()->empty() || !::builder->GetInsertBlock()->back().isTerminator())
John Bauman19bac1e2014-05-06 15:23:49 -04001111 {
Nicolas Capensac230122016-09-20 14:30:06 -04001112 llvm::Type *type = ::function->getReturnType();
John Bauman19bac1e2014-05-06 15:23:49 -04001113
1114 if(type->isVoidTy())
1115 {
1116 createRetVoid();
1117 }
1118 else
1119 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001120 createRet(V(llvm::UndefValue::get(type)));
John Bauman19bac1e2014-05-06 15:23:49 -04001121 }
1122 }
John Bauman89401822014-05-06 15:04:28 -04001123
Ben Clayton97c13ad2019-05-02 11:59:30 +01001124#ifdef ENABLE_RR_DEBUG_INFO
1125 if (debugInfo != nullptr)
1126 {
1127 debugInfo->Finalize();
1128 }
1129#endif // ENABLE_RR_DEBUG_INFO
1130
John Bauman89401822014-05-06 15:04:28 -04001131 if(false)
1132 {
Ben Clayton5875be52019-04-11 14:57:40 -04001133 std::error_code error;
1134 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error);
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -04001135 ::module->print(file, 0);
John Bauman89401822014-05-06 15:04:28 -04001136 }
1137
Ben Clayton5375f472019-06-24 13:33:11 +01001138#if defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG)
Ben Clayton4b944652019-05-02 10:56:19 +01001139 {
1140 llvm::legacy::PassManager pm;
1141 pm.add(llvm::createVerifierPass());
1142 pm.run(*::module);
1143 }
Ben Clayton5375f472019-06-24 13:33:11 +01001144#endif // defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG)
Ben Clayton4b944652019-05-02 10:56:19 +01001145
John Bauman89401822014-05-06 15:04:28 -04001146 if(runOptimizations)
1147 {
1148 optimize();
1149 }
1150
1151 if(false)
1152 {
Ben Clayton5875be52019-04-11 14:57:40 -04001153 std::error_code error;
1154 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error);
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -04001155 ::module->print(file, 0);
John Bauman89401822014-05-06 15:04:28 -04001156 }
1157
Ben Clayton1c82c7b2019-04-30 12:49:27 +01001158 LLVMRoutine *routine = ::reactorJIT->acquireRoutine(&::function, 1);
John Bauman89401822014-05-06 15:04:28 -04001159
John Bauman89401822014-05-06 15:04:28 -04001160 return routine;
1161 }
1162
1163 void Nucleus::optimize()
1164 {
Logan Chien52cde602018-09-03 19:37:57 +08001165 ::reactorJIT->optimize(::module);
John Bauman89401822014-05-06 15:04:28 -04001166 }
1167
John Bauman19bac1e2014-05-06 15:23:49 -04001168 Value *Nucleus::allocateStackVariable(Type *type, int arraySize)
John Bauman89401822014-05-06 15:04:28 -04001169 {
1170 // Need to allocate it in the entry block for mem2reg to work
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001171 llvm::BasicBlock &entryBlock = ::function->getEntryBlock();
John Bauman89401822014-05-06 15:04:28 -04001172
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001173 llvm::Instruction *declaration;
John Bauman89401822014-05-06 15:04:28 -04001174
1175 if(arraySize)
1176 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08001177 declaration = new llvm::AllocaInst(T(type), 0, V(Nucleus::createConstantInt(arraySize)));
John Bauman89401822014-05-06 15:04:28 -04001178 }
1179 else
1180 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08001181 declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value*)nullptr);
John Bauman89401822014-05-06 15:04:28 -04001182 }
1183
1184 entryBlock.getInstList().push_front(declaration);
1185
Nicolas Capens19336542016-09-26 10:32:29 -04001186 return V(declaration);
John Bauman89401822014-05-06 15:04:28 -04001187 }
1188
1189 BasicBlock *Nucleus::createBasicBlock()
1190 {
Logan Chien191b3052018-08-31 16:57:15 +08001191 return B(llvm::BasicBlock::Create(*::context, "", ::function));
John Bauman89401822014-05-06 15:04:28 -04001192 }
1193
1194 BasicBlock *Nucleus::getInsertBlock()
1195 {
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001196 return B(::builder->GetInsertBlock());
John Bauman89401822014-05-06 15:04:28 -04001197 }
1198
1199 void Nucleus::setInsertBlock(BasicBlock *basicBlock)
1200 {
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -04001201 // assert(::builder->GetInsertBlock()->back().isTerminator());
Nicolas Capens0192d152019-03-27 14:46:07 -04001202
1203 Variable::materializeAll();
1204
Logan Chien191b3052018-08-31 16:57:15 +08001205 ::builder->SetInsertPoint(B(basicBlock));
John Bauman89401822014-05-06 15:04:28 -04001206 }
1207
Nicolas Capensac230122016-09-20 14:30:06 -04001208 void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params)
John Bauman89401822014-05-06 15:04:28 -04001209 {
Ben Claytonea38f952019-06-17 13:56:56 +01001210 ::function = rr::createFunction("", T(ReturnType), T(Params));
Nicolas Capens52551d12018-09-13 14:30:56 -04001211
Ben Claytonac07ed82019-03-26 14:17:41 +00001212#ifdef ENABLE_RR_DEBUG_INFO
1213 ::debugInfo = std::unique_ptr<DebugInfo>(new DebugInfo(::builder, ::context, ::module, ::function));
1214#endif // ENABLE_RR_DEBUG_INFO
1215
Logan Chien191b3052018-08-31 16:57:15 +08001216 ::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "", ::function));
John Bauman89401822014-05-06 15:04:28 -04001217 }
1218
Nicolas Capens19336542016-09-26 10:32:29 -04001219 Value *Nucleus::getArgument(unsigned int index)
John Bauman89401822014-05-06 15:04:28 -04001220 {
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -04001221 llvm::Function::arg_iterator args = ::function->arg_begin();
John Bauman89401822014-05-06 15:04:28 -04001222
1223 while(index)
1224 {
1225 args++;
1226 index--;
1227 }
1228
Nicolas Capens19336542016-09-26 10:32:29 -04001229 return V(&*args);
John Bauman89401822014-05-06 15:04:28 -04001230 }
1231
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04001232 void Nucleus::createRetVoid()
John Bauman89401822014-05-06 15:04:28 -04001233 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001234 RR_DEBUG_INFO_UPDATE_LOC();
1235
Ben Claytonc958b172019-05-02 12:20:59 +01001236 ASSERT_MSG(::function->getReturnType() == T(Void::getType()), "Return type mismatch");
1237
Nicolas Capens0192d152019-03-27 14:46:07 -04001238 // Code generated after this point is unreachable, so any variables
1239 // being read can safely return an undefined value. We have to avoid
1240 // materializing variables after the terminator ret instruction.
1241 Variable::killUnmaterialized();
1242
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04001243 ::builder->CreateRetVoid();
John Bauman89401822014-05-06 15:04:28 -04001244 }
1245
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04001246 void Nucleus::createRet(Value *v)
John Bauman89401822014-05-06 15:04:28 -04001247 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001248 RR_DEBUG_INFO_UPDATE_LOC();
1249
Ben Claytonc958b172019-05-02 12:20:59 +01001250 ASSERT_MSG(::function->getReturnType() == V(v)->getType(), "Return type mismatch");
1251
Nicolas Capens0192d152019-03-27 14:46:07 -04001252 // Code generated after this point is unreachable, so any variables
1253 // being read can safely return an undefined value. We have to avoid
1254 // materializing variables after the terminator ret instruction.
1255 Variable::killUnmaterialized();
1256
Logan Chien191b3052018-08-31 16:57:15 +08001257 ::builder->CreateRet(V(v));
John Bauman89401822014-05-06 15:04:28 -04001258 }
1259
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04001260 void Nucleus::createBr(BasicBlock *dest)
John Bauman89401822014-05-06 15:04:28 -04001261 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001262 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens0192d152019-03-27 14:46:07 -04001263 Variable::materializeAll();
1264
Logan Chien191b3052018-08-31 16:57:15 +08001265 ::builder->CreateBr(B(dest));
John Bauman89401822014-05-06 15:04:28 -04001266 }
1267
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04001268 void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse)
John Bauman89401822014-05-06 15:04:28 -04001269 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001270 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens0192d152019-03-27 14:46:07 -04001271 Variable::materializeAll();
Logan Chien191b3052018-08-31 16:57:15 +08001272 ::builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse));
John Bauman89401822014-05-06 15:04:28 -04001273 }
1274
1275 Value *Nucleus::createAdd(Value *lhs, Value *rhs)
1276 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001277 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001278 return V(::builder->CreateAdd(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001279 }
1280
1281 Value *Nucleus::createSub(Value *lhs, Value *rhs)
1282 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001283 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001284 return V(::builder->CreateSub(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001285 }
1286
1287 Value *Nucleus::createMul(Value *lhs, Value *rhs)
1288 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001289 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001290 return V(::builder->CreateMul(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001291 }
1292
1293 Value *Nucleus::createUDiv(Value *lhs, Value *rhs)
1294 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001295 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001296 return V(::builder->CreateUDiv(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001297 }
1298
1299 Value *Nucleus::createSDiv(Value *lhs, Value *rhs)
1300 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001301 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001302 return V(::builder->CreateSDiv(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001303 }
1304
1305 Value *Nucleus::createFAdd(Value *lhs, Value *rhs)
1306 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001307 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001308 return V(::builder->CreateFAdd(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001309 }
1310
1311 Value *Nucleus::createFSub(Value *lhs, Value *rhs)
1312 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001313 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001314 return V(::builder->CreateFSub(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001315 }
1316
1317 Value *Nucleus::createFMul(Value *lhs, Value *rhs)
1318 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001319 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001320 return V(::builder->CreateFMul(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001321 }
1322
1323 Value *Nucleus::createFDiv(Value *lhs, Value *rhs)
1324 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001325 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001326 return V(::builder->CreateFDiv(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001327 }
1328
1329 Value *Nucleus::createURem(Value *lhs, Value *rhs)
1330 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001331 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001332 return V(::builder->CreateURem(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001333 }
1334
1335 Value *Nucleus::createSRem(Value *lhs, Value *rhs)
1336 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001337 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001338 return V(::builder->CreateSRem(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001339 }
1340
1341 Value *Nucleus::createFRem(Value *lhs, Value *rhs)
1342 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001343 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001344 return V(::builder->CreateFRem(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001345 }
1346
1347 Value *Nucleus::createShl(Value *lhs, Value *rhs)
1348 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001349 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001350 return V(::builder->CreateShl(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001351 }
1352
1353 Value *Nucleus::createLShr(Value *lhs, Value *rhs)
1354 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001355 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001356 return V(::builder->CreateLShr(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001357 }
1358
1359 Value *Nucleus::createAShr(Value *lhs, Value *rhs)
1360 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001361 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001362 return V(::builder->CreateAShr(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001363 }
1364
1365 Value *Nucleus::createAnd(Value *lhs, Value *rhs)
1366 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001367 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001368 return V(::builder->CreateAnd(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001369 }
1370
1371 Value *Nucleus::createOr(Value *lhs, Value *rhs)
1372 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001373 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001374 return V(::builder->CreateOr(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001375 }
1376
1377 Value *Nucleus::createXor(Value *lhs, Value *rhs)
1378 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001379 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001380 return V(::builder->CreateXor(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001381 }
1382
Nicolas Capens19336542016-09-26 10:32:29 -04001383 Value *Nucleus::createNeg(Value *v)
John Bauman89401822014-05-06 15:04:28 -04001384 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001385 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001386 return V(::builder->CreateNeg(V(v)));
John Bauman89401822014-05-06 15:04:28 -04001387 }
1388
Nicolas Capens19336542016-09-26 10:32:29 -04001389 Value *Nucleus::createFNeg(Value *v)
John Bauman89401822014-05-06 15:04:28 -04001390 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001391 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001392 return V(::builder->CreateFNeg(V(v)));
John Bauman89401822014-05-06 15:04:28 -04001393 }
1394
Nicolas Capens19336542016-09-26 10:32:29 -04001395 Value *Nucleus::createNot(Value *v)
John Bauman89401822014-05-06 15:04:28 -04001396 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001397 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001398 return V(::builder->CreateNot(V(v)));
John Bauman89401822014-05-06 15:04:28 -04001399 }
1400
Nicolas Capens86509d92019-03-21 13:23:50 -04001401 Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder)
John Bauman89401822014-05-06 15:04:28 -04001402 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001403 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001404 switch(asInternalType(type))
Nicolas Capens01a97962017-07-28 17:30:51 -04001405 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001406 case Type_v2i32:
1407 case Type_v4i16:
1408 case Type_v8i8:
1409 case Type_v2f32:
1410 return createBitCast(
1411 createInsertElement(
1412 V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))),
Nicolas Capens86509d92019-03-21 13:23:50 -04001413 createLoad(createBitCast(ptr, Pointer<Long>::getType()), Long::getType(), isVolatile, alignment, atomic, memoryOrder),
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001414 0),
1415 type);
1416 case Type_v2i16:
1417 case Type_v4i8:
1418 if(alignment != 0) // Not a local variable (all vectors are 128-bit).
Nicolas Capens01a97962017-07-28 17:30:51 -04001419 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001420 Value *u = V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2)));
Nicolas Capens86509d92019-03-21 13:23:50 -04001421 Value *i = createLoad(createBitCast(ptr, Pointer<Int>::getType()), Int::getType(), isVolatile, alignment, atomic, memoryOrder);
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001422 i = createZExt(i, Long::getType());
1423 Value *v = createInsertElement(u, i, 0);
1424 return createBitCast(v, type);
Nicolas Capens01a97962017-07-28 17:30:51 -04001425 }
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001426 // Fallthrough to non-emulated case.
1427 case Type_LLVM:
Nicolas Capens86509d92019-03-21 13:23:50 -04001428 {
Ben Clayton99e57192019-05-03 13:25:08 +01001429 auto elTy = T(type);
1430 ASSERT(V(ptr)->getType()->getContainedType(0) == elTy);
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001431
1432 if (!atomic)
Ben Clayton99e57192019-05-03 13:25:08 +01001433 {
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001434 return V(::builder->CreateAlignedLoad(V(ptr), alignment, isVolatile));
1435 }
1436 else if (elTy->isIntegerTy() || elTy->isPointerTy())
1437 {
1438 // Integers and pointers can be atomically loaded by setting
1439 // the ordering constraint on the load instruction.
1440 auto load = ::builder->CreateAlignedLoad(V(ptr), alignment, isVolatile);
1441 load->setAtomic(atomicOrdering(atomic, memoryOrder));
1442 return V(load);
1443 }
1444 else if (elTy->isFloatTy() || elTy->isDoubleTy())
1445 {
1446 // LLVM claims to support atomic loads of float types as
1447 // above, but certain backends cannot deal with this.
1448 // Load as an integer and bitcast. See b/136037244.
1449 auto size = ::module->getDataLayout().getTypeStoreSize(elTy);
1450 auto elAsIntTy = ::llvm::IntegerType::get(*::context, size * 8);
1451 auto ptrCast = ::builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo());
1452 auto load = ::builder->CreateAlignedLoad(ptrCast, alignment, isVolatile);
1453 load->setAtomic(atomicOrdering(atomic, memoryOrder));
1454 auto loadCast = ::builder->CreateBitCast(load, elTy);
1455 return V(loadCast);
1456 }
1457 else
1458 {
1459 // More exotic types require falling back to the extern:
1460 // void __atomic_load(size_t size, void *ptr, void *ret, int ordering)
Ben Clayton99e57192019-05-03 13:25:08 +01001461 auto sizetTy = ::llvm::IntegerType::get(*::context, sizeof(size_t) * 8);
1462 auto intTy = ::llvm::IntegerType::get(*::context, sizeof(int) * 8);
1463 auto i8Ty = ::llvm::Type::getInt8Ty(*::context);
1464 auto i8PtrTy = i8Ty->getPointerTo();
1465 auto voidTy = ::llvm::Type::getVoidTy(*::context);
1466 auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false);
1467 auto func = ::module->getOrInsertFunction("__atomic_load", funcTy);
1468 auto size = ::module->getDataLayout().getTypeStoreSize(elTy);
1469 auto out = allocateStackVariable(type);
1470 ::builder->CreateCall(func, {
1471 ::llvm::ConstantInt::get(sizetTy, size),
1472 ::builder->CreatePointerCast(V(ptr), i8PtrTy),
1473 ::builder->CreatePointerCast(V(out), i8PtrTy),
1474 ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))),
1475 });
1476 return V(::builder->CreateLoad(V(out)));
1477 }
Nicolas Capens86509d92019-03-21 13:23:50 -04001478 }
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001479 default:
Ben Claytoneb50d252019-04-15 13:50:01 -04001480 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
1481 return nullptr;
Nicolas Capens01a97962017-07-28 17:30:51 -04001482 }
John Bauman89401822014-05-06 15:04:28 -04001483 }
1484
Nicolas Capens86509d92019-03-21 13:23:50 -04001485 Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder)
John Bauman89401822014-05-06 15:04:28 -04001486 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001487 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001488 switch(asInternalType(type))
Nicolas Capens01a97962017-07-28 17:30:51 -04001489 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001490 case Type_v2i32:
1491 case Type_v4i16:
1492 case Type_v8i8:
1493 case Type_v2f32:
1494 createStore(
1495 createExtractElement(
1496 createBitCast(value, T(llvm::VectorType::get(T(Long::getType()), 2))), Long::getType(), 0),
1497 createBitCast(ptr, Pointer<Long>::getType()),
Nicolas Capens86509d92019-03-21 13:23:50 -04001498 Long::getType(), isVolatile, alignment, atomic, memoryOrder);
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001499 return value;
1500 case Type_v2i16:
1501 case Type_v4i8:
1502 if(alignment != 0) // Not a local variable (all vectors are 128-bit).
Nicolas Capens01a97962017-07-28 17:30:51 -04001503 {
Logan Chien191b3052018-08-31 16:57:15 +08001504 createStore(
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001505 createExtractElement(createBitCast(value, Int4::getType()), Int::getType(), 0),
1506 createBitCast(ptr, Pointer<Int>::getType()),
Nicolas Capens86509d92019-03-21 13:23:50 -04001507 Int::getType(), isVolatile, alignment, atomic, memoryOrder);
Nicolas Capens01a97962017-07-28 17:30:51 -04001508 return value;
Nicolas Capens01a97962017-07-28 17:30:51 -04001509 }
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001510 // Fallthrough to non-emulated case.
1511 case Type_LLVM:
Nicolas Capens86509d92019-03-21 13:23:50 -04001512 {
Ben Clayton99e57192019-05-03 13:25:08 +01001513 auto elTy = T(type);
1514 ASSERT(V(ptr)->getType()->getContainedType(0) == elTy);
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001515
1516 if (!atomic)
Ben Clayton99e57192019-05-03 13:25:08 +01001517 {
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001518 ::builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile);
1519 }
1520 else if (elTy->isIntegerTy() || elTy->isPointerTy())
1521 {
1522 // Integers and pointers can be atomically stored by setting
1523 // the ordering constraint on the store instruction.
1524 auto store = ::builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile);
1525 store->setAtomic(atomicOrdering(atomic, memoryOrder));
1526 }
1527 else if (elTy->isFloatTy() || elTy->isDoubleTy())
1528 {
1529 // LLVM claims to support atomic stores of float types as
1530 // above, but certain backends cannot deal with this.
1531 // Store as an bitcast integer. See b/136037244.
1532 auto size = ::module->getDataLayout().getTypeStoreSize(elTy);
1533 auto elAsIntTy = ::llvm::IntegerType::get(*::context, size * 8);
1534 auto valCast = ::builder->CreateBitCast(V(value), elAsIntTy);
1535 auto ptrCast = ::builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo());
1536 auto store = ::builder->CreateAlignedStore(valCast, ptrCast, alignment, isVolatile);
1537 store->setAtomic(atomicOrdering(atomic, memoryOrder));
1538 }
1539 else
1540 {
1541 // More exotic types require falling back to the extern:
1542 // void __atomic_store(size_t size, void *ptr, void *val, int ordering)
Ben Clayton99e57192019-05-03 13:25:08 +01001543 auto sizetTy = ::llvm::IntegerType::get(*::context, sizeof(size_t) * 8);
1544 auto intTy = ::llvm::IntegerType::get(*::context, sizeof(int) * 8);
1545 auto i8Ty = ::llvm::Type::getInt8Ty(*::context);
1546 auto i8PtrTy = i8Ty->getPointerTo();
1547 auto voidTy = ::llvm::Type::getVoidTy(*::context);
1548 auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false);
1549 auto func = ::module->getOrInsertFunction("__atomic_store", funcTy);
1550 auto size = ::module->getDataLayout().getTypeStoreSize(elTy);
1551 auto copy = allocateStackVariable(type);
1552 ::builder->CreateStore(V(value), V(copy));
1553 ::builder->CreateCall(func, {
1554 ::llvm::ConstantInt::get(sizetTy, size),
1555 ::builder->CreatePointerCast(V(ptr), i8PtrTy),
1556 ::builder->CreatePointerCast(V(copy), i8PtrTy),
1557 ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))),
1558 });
1559 }
Nicolas Capens86509d92019-03-21 13:23:50 -04001560
1561 return value;
1562 }
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001563 default:
Ben Claytoneb50d252019-04-15 13:50:01 -04001564 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
1565 return nullptr;
Nicolas Capens01a97962017-07-28 17:30:51 -04001566 }
John Bauman89401822014-05-06 15:04:28 -04001567 }
1568
Ben Claytoncb2ebc92019-06-20 00:18:03 +01001569 Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment)
1570 {
1571 ASSERT(V(ptr)->getType()->isPointerTy());
1572 ASSERT(V(mask)->getType()->isVectorTy());
1573
1574 auto numEls = V(mask)->getType()->getVectorNumElements();
1575 auto i1Ty = ::llvm::Type::getInt1Ty(*::context);
1576 auto i32Ty = ::llvm::Type::getInt32Ty(*::context);
1577 auto elVecTy = ::llvm::VectorType::get(T(elTy), numEls);
1578 auto elVecPtrTy = elVecTy->getPointerTo();
1579 auto i8Mask = ::builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...>
1580 auto passthrough = ::llvm::Constant::getNullValue(elVecTy);
1581 auto align = ::llvm::ConstantInt::get(i32Ty, alignment);
1582 auto func = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::masked_load, { elVecTy, elVecPtrTy } );
1583 return V(::builder->CreateCall(func, { V(ptr), align, i8Mask, passthrough }));
1584 }
1585
1586 void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment)
1587 {
1588 ASSERT(V(ptr)->getType()->isPointerTy());
1589 ASSERT(V(val)->getType()->isVectorTy());
1590 ASSERT(V(mask)->getType()->isVectorTy());
1591
1592 auto numEls = V(mask)->getType()->getVectorNumElements();
1593 auto i1Ty = ::llvm::Type::getInt1Ty(*::context);
1594 auto i32Ty = ::llvm::Type::getInt32Ty(*::context);
1595 auto elVecTy = V(val)->getType();
1596 auto elVecPtrTy = elVecTy->getPointerTo();
1597 auto i8Mask = ::builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...>
1598 auto align = ::llvm::ConstantInt::get(i32Ty, alignment);
1599 auto func = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::masked_store, { elVecTy, elVecPtrTy } );
1600 ::builder->CreateCall(func, { V(val), V(ptr), align, i8Mask });
1601 }
1602
Ben Clayton0fc611f2019-04-18 11:23:27 -04001603 Value *Nucleus::createGather(Value *base, Type *elTy, Value *offsets, Value *mask, unsigned int alignment)
1604 {
1605 ASSERT(V(base)->getType()->isPointerTy());
1606 ASSERT(V(offsets)->getType()->isVectorTy());
1607 ASSERT(V(mask)->getType()->isVectorTy());
1608
1609 auto numEls = V(mask)->getType()->getVectorNumElements();
1610 auto i1Ty = ::llvm::Type::getInt1Ty(*::context);
1611 auto i32Ty = ::llvm::Type::getInt32Ty(*::context);
1612 auto i8Ty = ::llvm::Type::getInt8Ty(*::context);
1613 auto i8PtrTy = i8Ty->getPointerTo();
1614 auto elPtrTy = T(elTy)->getPointerTo();
1615 auto elVecTy = ::llvm::VectorType::get(T(elTy), numEls);
1616 auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls);
1617 auto i8Base = ::builder->CreatePointerCast(V(base), i8PtrTy);
1618 auto i8Ptrs = ::builder->CreateGEP(i8Base, V(offsets));
1619 auto elPtrs = ::builder->CreatePointerCast(i8Ptrs, elPtrVecTy);
1620 auto i8Mask = ::builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...>
1621 auto passthrough = ::llvm::Constant::getNullValue(elVecTy);
1622 auto align = ::llvm::ConstantInt::get(i32Ty, alignment);
1623 auto func = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::masked_gather, { elVecTy, elPtrVecTy } );
1624 return V(::builder->CreateCall(func, { elPtrs, align, i8Mask, passthrough }));
1625 }
1626
1627 void Nucleus::createScatter(Value *base, Value *val, Value *offsets, Value *mask, unsigned int alignment)
1628 {
1629 ASSERT(V(base)->getType()->isPointerTy());
1630 ASSERT(V(val)->getType()->isVectorTy());
1631 ASSERT(V(offsets)->getType()->isVectorTy());
1632 ASSERT(V(mask)->getType()->isVectorTy());
1633
1634 auto numEls = V(mask)->getType()->getVectorNumElements();
1635 auto i1Ty = ::llvm::Type::getInt1Ty(*::context);
1636 auto i32Ty = ::llvm::Type::getInt32Ty(*::context);
1637 auto i8Ty = ::llvm::Type::getInt8Ty(*::context);
1638 auto i8PtrTy = i8Ty->getPointerTo();
1639 auto elVecTy = V(val)->getType();
1640 auto elTy = elVecTy->getVectorElementType();
1641 auto elPtrTy = elTy->getPointerTo();
1642 auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls);
1643 auto i8Base = ::builder->CreatePointerCast(V(base), i8PtrTy);
1644 auto i8Ptrs = ::builder->CreateGEP(i8Base, V(offsets));
1645 auto elPtrs = ::builder->CreatePointerCast(i8Ptrs, elPtrVecTy);
1646 auto i8Mask = ::builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...>
1647 auto align = ::llvm::ConstantInt::get(i32Ty, alignment);
1648 auto func = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::masked_scatter, { elVecTy, elPtrVecTy } );
1649 ::builder->CreateCall(func, { V(val), elPtrs, align, i8Mask });
1650 }
1651
Ben Claytonb16c5862019-05-08 14:01:38 +01001652 void Nucleus::createFence(std::memory_order memoryOrder)
1653 {
1654 ::builder->CreateFence(atomicOrdering(true, memoryOrder));
1655 }
1656
Nicolas Capensd294def2017-01-26 17:44:37 -08001657 Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
John Bauman89401822014-05-06 15:04:28 -04001658 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001659 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytoneb50d252019-04-15 13:50:01 -04001660 ASSERT(V(ptr)->getType()->getContainedType(0) == T(type));
Nicolas Capens01a97962017-07-28 17:30:51 -04001661 if(sizeof(void*) == 8)
Nicolas Capensd294def2017-01-26 17:44:37 -08001662 {
Ben Claytonb1243732019-02-27 23:56:18 +00001663 // LLVM manual: "When indexing into an array, pointer or vector,
1664 // integers of any width are allowed, and they are not required to
1665 // be constant. These integers are treated as signed values where
1666 // relevant."
1667 //
1668 // Thus if we want indexes to be treated as unsigned we have to
1669 // zero-extend them ourselves.
1670 //
1671 // Note that this is not because we want to address anywhere near
1672 // 4 GB of data. Instead this is important for performance because
1673 // x86 supports automatic zero-extending of 32-bit registers to
1674 // 64-bit. Thus when indexing into an array using a uint32 is
1675 // actually faster than an int32.
1676 index = unsignedIndex ?
1677 createZExt(index, Long::getType()) :
1678 createSExt(index, Long::getType());
Nicolas Capens01a97962017-07-28 17:30:51 -04001679 }
Ben Claytonb1243732019-02-27 23:56:18 +00001680
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001681 // For non-emulated types we can rely on LLVM's GEP to calculate the
1682 // effective address correctly.
1683 if(asInternalType(type) == Type_LLVM)
Nicolas Capens01a97962017-07-28 17:30:51 -04001684 {
Ben Claytonb1243732019-02-27 23:56:18 +00001685 return V(::builder->CreateGEP(V(ptr), V(index)));
Nicolas Capensd294def2017-01-26 17:44:37 -08001686 }
1687
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001688 // For emulated types we have to multiply the index by the intended
1689 // type size ourselves to obain the byte offset.
Ben Claytonb1243732019-02-27 23:56:18 +00001690 index = (sizeof(void*) == 8) ?
1691 createMul(index, createConstantLong((int64_t)typeSize(type))) :
1692 createMul(index, createConstantInt((int)typeSize(type)));
1693
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001694 // Cast to a byte pointer, apply the byte offset, and cast back to the
1695 // original pointer type.
Logan Chien191b3052018-08-31 16:57:15 +08001696 return createBitCast(
1697 V(::builder->CreateGEP(V(createBitCast(ptr, T(llvm::PointerType::get(T(Byte::getType()), 0)))), V(index))),
1698 T(llvm::PointerType::get(T(type), 0)));
John Bauman89401822014-05-06 15:04:28 -04001699 }
1700
Chris Forbes17813932019-04-18 11:45:54 -07001701 Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder)
John Bauman19bac1e2014-05-06 15:23:49 -04001702 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001703 RR_DEBUG_INFO_UPDATE_LOC();
Chris Forbes17813932019-04-18 11:45:54 -07001704 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Add, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
1705 }
1706
Chris Forbes707ed992019-04-18 18:17:35 -07001707 Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder)
1708 {
1709 RR_DEBUG_INFO_UPDATE_LOC();
1710 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Sub, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
1711 }
1712
Chris Forbes17813932019-04-18 11:45:54 -07001713 Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder)
1714 {
1715 RR_DEBUG_INFO_UPDATE_LOC();
1716 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::And, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
1717 }
1718
1719 Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder)
1720 {
1721 RR_DEBUG_INFO_UPDATE_LOC();
1722 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Or, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
1723 }
1724
1725 Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder)
1726 {
1727 RR_DEBUG_INFO_UPDATE_LOC();
1728 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xor, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
1729 }
1730
1731 Value *Nucleus::createAtomicMin(Value *ptr, Value *value, std::memory_order memoryOrder)
1732 {
1733 RR_DEBUG_INFO_UPDATE_LOC();
1734 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Min, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
1735 }
1736
1737 Value *Nucleus::createAtomicMax(Value *ptr, Value *value, std::memory_order memoryOrder)
1738 {
1739 RR_DEBUG_INFO_UPDATE_LOC();
1740 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Max, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
1741 }
1742
Chris Forbesf31bdad2019-05-23 14:58:08 -07001743 Value *Nucleus::createAtomicUMin(Value *ptr, Value *value, std::memory_order memoryOrder)
1744 {
1745 RR_DEBUG_INFO_UPDATE_LOC();
1746 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMin, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
1747 }
1748
1749 Value *Nucleus::createAtomicUMax(Value *ptr, Value *value, std::memory_order memoryOrder)
1750 {
1751 RR_DEBUG_INFO_UPDATE_LOC();
1752 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMax, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
1753 }
1754
1755
Chris Forbes17813932019-04-18 11:45:54 -07001756 Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder)
1757 {
1758 RR_DEBUG_INFO_UPDATE_LOC();
1759 return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
John Bauman19bac1e2014-05-06 15:23:49 -04001760 }
1761
Chris Forbesa16238d2019-04-18 16:31:54 -07001762 Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal)
1763 {
1764 RR_DEBUG_INFO_UPDATE_LOC();
Chris Forbesc9ca99e2019-04-19 07:53:34 -07001765 // Note: AtomicCmpXchgInstruction returns a 2-member struct containing {result, success-flag}, not the result directly.
Chris Forbesa16238d2019-04-18 16:31:54 -07001766 return V(::builder->CreateExtractValue(
1767 ::builder->CreateAtomicCmpXchg(V(ptr), V(compare), V(value), atomicOrdering(true, memoryOrderEqual), atomicOrdering(true, memoryOrderUnequal)),
1768 llvm::ArrayRef<unsigned>(0u)));
1769 }
1770
Nicolas Capens19336542016-09-26 10:32:29 -04001771 Value *Nucleus::createTrunc(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001772 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001773 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001774 return V(::builder->CreateTrunc(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001775 }
1776
Nicolas Capens19336542016-09-26 10:32:29 -04001777 Value *Nucleus::createZExt(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001778 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001779 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001780 return V(::builder->CreateZExt(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001781 }
1782
Nicolas Capens19336542016-09-26 10:32:29 -04001783 Value *Nucleus::createSExt(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001784 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001785 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001786 return V(::builder->CreateSExt(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001787 }
1788
Nicolas Capens19336542016-09-26 10:32:29 -04001789 Value *Nucleus::createFPToSI(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001790 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001791 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001792 return V(::builder->CreateFPToSI(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001793 }
1794
Nicolas Capens19336542016-09-26 10:32:29 -04001795 Value *Nucleus::createSIToFP(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001796 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001797 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001798 return V(::builder->CreateSIToFP(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001799 }
1800
Nicolas Capens19336542016-09-26 10:32:29 -04001801 Value *Nucleus::createFPTrunc(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001802 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001803 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001804 return V(::builder->CreateFPTrunc(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001805 }
1806
Nicolas Capens19336542016-09-26 10:32:29 -04001807 Value *Nucleus::createFPExt(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001808 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001809 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001810 return V(::builder->CreateFPExt(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001811 }
1812
Nicolas Capens19336542016-09-26 10:32:29 -04001813 Value *Nucleus::createBitCast(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001814 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001815 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens01a97962017-07-28 17:30:51 -04001816 // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need
1817 // support for casting between scalars and wide vectors. Emulate them by writing to the stack and
1818 // reading back as the destination type.
Logan Chien191b3052018-08-31 16:57:15 +08001819 if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy())
Nicolas Capens01a97962017-07-28 17:30:51 -04001820 {
1821 Value *readAddress = allocateStackVariable(destType);
Logan Chien191b3052018-08-31 16:57:15 +08001822 Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0)));
1823 createStore(v, writeAddress, T(V(v)->getType()));
Nicolas Capens01a97962017-07-28 17:30:51 -04001824 return createLoad(readAddress, destType);
1825 }
Logan Chien191b3052018-08-31 16:57:15 +08001826 else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy())
Nicolas Capens01a97962017-07-28 17:30:51 -04001827 {
Logan Chien191b3052018-08-31 16:57:15 +08001828 Value *writeAddress = allocateStackVariable(T(V(v)->getType()));
1829 createStore(v, writeAddress, T(V(v)->getType()));
Nicolas Capens01a97962017-07-28 17:30:51 -04001830 Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0)));
1831 return createLoad(readAddress, destType);
1832 }
1833
Logan Chien191b3052018-08-31 16:57:15 +08001834 return V(::builder->CreateBitCast(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001835 }
1836
John Bauman89401822014-05-06 15:04:28 -04001837 Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs)
1838 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001839 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001840 return V(::builder->CreateICmpEQ(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001841 }
1842
1843 Value *Nucleus::createICmpNE(Value *lhs, Value *rhs)
1844 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001845 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001846 return V(::builder->CreateICmpNE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001847 }
1848
1849 Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs)
1850 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001851 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001852 return V(::builder->CreateICmpUGT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001853 }
1854
1855 Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs)
1856 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001857 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001858 return V(::builder->CreateICmpUGE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001859 }
1860
1861 Value *Nucleus::createICmpULT(Value *lhs, Value *rhs)
1862 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001863 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001864 return V(::builder->CreateICmpULT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001865 }
1866
1867 Value *Nucleus::createICmpULE(Value *lhs, Value *rhs)
1868 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001869 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001870 return V(::builder->CreateICmpULE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001871 }
1872
1873 Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs)
1874 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001875 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001876 return V(::builder->CreateICmpSGT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001877 }
1878
1879 Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs)
1880 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001881 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001882 return V(::builder->CreateICmpSGE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001883 }
1884
1885 Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs)
1886 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001887 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001888 return V(::builder->CreateICmpSLT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001889 }
1890
1891 Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs)
1892 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001893 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001894 return V(::builder->CreateICmpSLE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001895 }
1896
1897 Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs)
1898 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001899 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001900 return V(::builder->CreateFCmpOEQ(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001901 }
1902
1903 Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs)
1904 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001905 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001906 return V(::builder->CreateFCmpOGT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001907 }
1908
1909 Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs)
1910 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001911 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001912 return V(::builder->CreateFCmpOGE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001913 }
1914
1915 Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs)
1916 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001917 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001918 return V(::builder->CreateFCmpOLT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001919 }
1920
1921 Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs)
1922 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001923 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001924 return V(::builder->CreateFCmpOLE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001925 }
1926
1927 Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs)
1928 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001929 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001930 return V(::builder->CreateFCmpONE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001931 }
1932
1933 Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs)
1934 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001935 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001936 return V(::builder->CreateFCmpORD(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001937 }
1938
1939 Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs)
1940 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001941 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001942 return V(::builder->CreateFCmpUNO(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001943 }
1944
1945 Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs)
1946 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001947 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001948 return V(::builder->CreateFCmpUEQ(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001949 }
1950
1951 Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs)
1952 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001953 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001954 return V(::builder->CreateFCmpUGT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001955 }
1956
1957 Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs)
1958 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001959 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001960 return V(::builder->CreateFCmpUGE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001961 }
1962
1963 Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs)
1964 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001965 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001966 return V(::builder->CreateFCmpULT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001967 }
1968
1969 Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs)
1970 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001971 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001972 return V(::builder->CreateFCmpULE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001973 }
1974
1975 Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs)
1976 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001977 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton71008d82019-03-05 17:17:59 +00001978 return V(::builder->CreateFCmpUNE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001979 }
1980
Nicolas Capense95d5342016-09-30 11:37:28 -04001981 Value *Nucleus::createExtractElement(Value *vector, Type *type, int index)
John Bauman89401822014-05-06 15:04:28 -04001982 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001983 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytoneb50d252019-04-15 13:50:01 -04001984 ASSERT(V(vector)->getType()->getContainedType(0) == T(type));
Logan Chien191b3052018-08-31 16:57:15 +08001985 return V(::builder->CreateExtractElement(V(vector), V(createConstantInt(index))));
John Bauman89401822014-05-06 15:04:28 -04001986 }
1987
1988 Value *Nucleus::createInsertElement(Value *vector, Value *element, int index)
1989 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001990 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08001991 return V(::builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index))));
John Bauman89401822014-05-06 15:04:28 -04001992 }
1993
Logan Chien191b3052018-08-31 16:57:15 +08001994 Value *Nucleus::createShuffleVector(Value *v1, Value *v2, const int *select)
John Bauman89401822014-05-06 15:04:28 -04001995 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001996 RR_DEBUG_INFO_UPDATE_LOC();
1997
Logan Chien191b3052018-08-31 16:57:15 +08001998 int size = llvm::cast<llvm::VectorType>(V(v1)->getType())->getNumElements();
Nicolas Capense89cd582016-09-30 14:23:47 -04001999 const int maxSize = 16;
2000 llvm::Constant *swizzle[maxSize];
Ben Claytoneb50d252019-04-15 13:50:01 -04002001 ASSERT(size <= maxSize);
Nicolas Capense89cd582016-09-30 14:23:47 -04002002
2003 for(int i = 0; i < size; i++)
2004 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002005 swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), select[i]);
Nicolas Capense89cd582016-09-30 14:23:47 -04002006 }
2007
2008 llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(swizzle, size));
2009
Logan Chien191b3052018-08-31 16:57:15 +08002010 return V(::builder->CreateShuffleVector(V(v1), V(v2), shuffle));
John Bauman89401822014-05-06 15:04:28 -04002011 }
2012
Logan Chien191b3052018-08-31 16:57:15 +08002013 Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse)
John Bauman89401822014-05-06 15:04:28 -04002014 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002015 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002016 return V(::builder->CreateSelect(V(c), V(ifTrue), V(ifFalse)));
John Bauman89401822014-05-06 15:04:28 -04002017 }
2018
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05002019 SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
John Bauman89401822014-05-06 15:04:28 -04002020 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002021 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002022 return reinterpret_cast<SwitchCases*>(::builder->CreateSwitch(V(control), B(defaultBranch), numCases));
John Bauman89401822014-05-06 15:04:28 -04002023 }
2024
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05002025 void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
John Bauman89401822014-05-06 15:04:28 -04002026 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002027 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002028 llvm::SwitchInst *sw = reinterpret_cast<llvm::SwitchInst *>(switchCases);
2029 sw->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), label, true), B(branch));
John Bauman89401822014-05-06 15:04:28 -04002030 }
2031
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04002032 void Nucleus::createUnreachable()
John Bauman89401822014-05-06 15:04:28 -04002033 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002034 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04002035 ::builder->CreateUnreachable();
John Bauman89401822014-05-06 15:04:28 -04002036 }
2037
Nicolas Capensac230122016-09-20 14:30:06 -04002038 Type *Nucleus::getPointerType(Type *ElementType)
John Bauman89401822014-05-06 15:04:28 -04002039 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002040 return T(llvm::PointerType::get(T(ElementType), 0));
John Bauman89401822014-05-06 15:04:28 -04002041 }
2042
Nicolas Capens13ac2322016-10-13 14:52:12 -04002043 Value *Nucleus::createNullValue(Type *Ty)
John Bauman89401822014-05-06 15:04:28 -04002044 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002045 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002046 return V(llvm::Constant::getNullValue(T(Ty)));
John Bauman89401822014-05-06 15:04:28 -04002047 }
2048
Nicolas Capens13ac2322016-10-13 14:52:12 -04002049 Value *Nucleus::createConstantLong(int64_t i)
John Bauman89401822014-05-06 15:04:28 -04002050 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002051 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002052 return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(*::context), i, true));
John Bauman89401822014-05-06 15:04:28 -04002053 }
2054
Nicolas Capens13ac2322016-10-13 14:52:12 -04002055 Value *Nucleus::createConstantInt(int i)
John Bauman89401822014-05-06 15:04:28 -04002056 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002057 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002058 return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, true));
John Bauman89401822014-05-06 15:04:28 -04002059 }
2060
Nicolas Capens13ac2322016-10-13 14:52:12 -04002061 Value *Nucleus::createConstantInt(unsigned int i)
John Bauman89401822014-05-06 15:04:28 -04002062 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002063 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002064 return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, false));
John Bauman89401822014-05-06 15:04:28 -04002065 }
2066
Nicolas Capens13ac2322016-10-13 14:52:12 -04002067 Value *Nucleus::createConstantBool(bool b)
John Bauman89401822014-05-06 15:04:28 -04002068 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002069 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002070 return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(*::context), b));
John Bauman89401822014-05-06 15:04:28 -04002071 }
2072
Nicolas Capens13ac2322016-10-13 14:52:12 -04002073 Value *Nucleus::createConstantByte(signed char i)
John Bauman89401822014-05-06 15:04:28 -04002074 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002075 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002076 return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, true));
John Bauman89401822014-05-06 15:04:28 -04002077 }
2078
Nicolas Capens13ac2322016-10-13 14:52:12 -04002079 Value *Nucleus::createConstantByte(unsigned char i)
John Bauman89401822014-05-06 15:04:28 -04002080 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002081 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002082 return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, false));
John Bauman89401822014-05-06 15:04:28 -04002083 }
2084
Nicolas Capens13ac2322016-10-13 14:52:12 -04002085 Value *Nucleus::createConstantShort(short i)
John Bauman89401822014-05-06 15:04:28 -04002086 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002087 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002088 return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, true));
John Bauman89401822014-05-06 15:04:28 -04002089 }
2090
Nicolas Capens13ac2322016-10-13 14:52:12 -04002091 Value *Nucleus::createConstantShort(unsigned short i)
John Bauman89401822014-05-06 15:04:28 -04002092 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002093 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002094 return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, false));
John Bauman89401822014-05-06 15:04:28 -04002095 }
2096
Nicolas Capens13ac2322016-10-13 14:52:12 -04002097 Value *Nucleus::createConstantFloat(float x)
John Bauman89401822014-05-06 15:04:28 -04002098 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002099 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002100 return V(llvm::ConstantFP::get(T(Float::getType()), x));
John Bauman89401822014-05-06 15:04:28 -04002101 }
2102
Nicolas Capens13ac2322016-10-13 14:52:12 -04002103 Value *Nucleus::createNullPointer(Type *Ty)
John Bauman89401822014-05-06 15:04:28 -04002104 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002105 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002106 return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0)));
John Bauman89401822014-05-06 15:04:28 -04002107 }
2108
Nicolas Capens13ac2322016-10-13 14:52:12 -04002109 Value *Nucleus::createConstantVector(const int64_t *constants, Type *type)
John Bauman89401822014-05-06 15:04:28 -04002110 {
Ben Claytoneb50d252019-04-15 13:50:01 -04002111 ASSERT(llvm::isa<llvm::VectorType>(T(type)));
Nicolas Capens69674fb2017-09-01 11:08:44 -04002112 const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type.
2113 const int numElements = llvm::cast<llvm::VectorType>(T(type))->getNumElements(); // Number of elements of the underlying vector type.
Ben Claytoneb50d252019-04-15 13:50:01 -04002114 ASSERT(numElements <= 16 && numConstants <= numElements);
Nicolas Capens13ac2322016-10-13 14:52:12 -04002115 llvm::Constant *constantVector[16];
2116
Nicolas Capens69674fb2017-09-01 11:08:44 -04002117 for(int i = 0; i < numElements; i++)
Nicolas Capens13ac2322016-10-13 14:52:12 -04002118 {
Nicolas Capens69674fb2017-09-01 11:08:44 -04002119 constantVector[i] = llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]);
Nicolas Capens13ac2322016-10-13 14:52:12 -04002120 }
2121
Nicolas Capens69674fb2017-09-01 11:08:44 -04002122 return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements)));
Nicolas Capens13ac2322016-10-13 14:52:12 -04002123 }
2124
2125 Value *Nucleus::createConstantVector(const double *constants, Type *type)
2126 {
Ben Claytoneb50d252019-04-15 13:50:01 -04002127 ASSERT(llvm::isa<llvm::VectorType>(T(type)));
Nicolas Capens69674fb2017-09-01 11:08:44 -04002128 const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type.
2129 const int numElements = llvm::cast<llvm::VectorType>(T(type))->getNumElements(); // Number of elements of the underlying vector type.
Ben Claytoneb50d252019-04-15 13:50:01 -04002130 ASSERT(numElements <= 8 && numConstants <= numElements);
Nicolas Capens13ac2322016-10-13 14:52:12 -04002131 llvm::Constant *constantVector[8];
2132
Nicolas Capens69674fb2017-09-01 11:08:44 -04002133 for(int i = 0; i < numElements; i++)
Nicolas Capens13ac2322016-10-13 14:52:12 -04002134 {
Nicolas Capens69674fb2017-09-01 11:08:44 -04002135 constantVector[i] = llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]);
Nicolas Capens13ac2322016-10-13 14:52:12 -04002136 }
2137
Nicolas Capens69674fb2017-09-01 11:08:44 -04002138 return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements)));
John Bauman89401822014-05-06 15:04:28 -04002139 }
2140
John Bauman19bac1e2014-05-06 15:23:49 -04002141 Type *Void::getType()
John Bauman89401822014-05-06 15:04:28 -04002142 {
Nicolas Capensac230122016-09-20 14:30:06 -04002143 return T(llvm::Type::getVoidTy(*::context));
John Bauman89401822014-05-06 15:04:28 -04002144 }
2145
John Bauman19bac1e2014-05-06 15:23:49 -04002146 Type *Bool::getType()
John Bauman89401822014-05-06 15:04:28 -04002147 {
Nicolas Capensac230122016-09-20 14:30:06 -04002148 return T(llvm::Type::getInt1Ty(*::context));
John Bauman89401822014-05-06 15:04:28 -04002149 }
2150
John Bauman19bac1e2014-05-06 15:23:49 -04002151 Type *Byte::getType()
John Bauman89401822014-05-06 15:04:28 -04002152 {
Nicolas Capensac230122016-09-20 14:30:06 -04002153 return T(llvm::Type::getInt8Ty(*::context));
John Bauman89401822014-05-06 15:04:28 -04002154 }
2155
John Bauman19bac1e2014-05-06 15:23:49 -04002156 Type *SByte::getType()
John Bauman89401822014-05-06 15:04:28 -04002157 {
Nicolas Capensac230122016-09-20 14:30:06 -04002158 return T(llvm::Type::getInt8Ty(*::context));
John Bauman89401822014-05-06 15:04:28 -04002159 }
2160
John Bauman19bac1e2014-05-06 15:23:49 -04002161 Type *Short::getType()
John Bauman89401822014-05-06 15:04:28 -04002162 {
Nicolas Capensac230122016-09-20 14:30:06 -04002163 return T(llvm::Type::getInt16Ty(*::context));
John Bauman89401822014-05-06 15:04:28 -04002164 }
2165
John Bauman19bac1e2014-05-06 15:23:49 -04002166 Type *UShort::getType()
John Bauman89401822014-05-06 15:04:28 -04002167 {
Nicolas Capensac230122016-09-20 14:30:06 -04002168 return T(llvm::Type::getInt16Ty(*::context));
John Bauman89401822014-05-06 15:04:28 -04002169 }
2170
John Bauman19bac1e2014-05-06 15:23:49 -04002171 Type *Byte4::getType()
John Bauman89401822014-05-06 15:04:28 -04002172 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002173 return T(Type_v4i8);
John Bauman89401822014-05-06 15:04:28 -04002174 }
2175
John Bauman19bac1e2014-05-06 15:23:49 -04002176 Type *SByte4::getType()
John Bauman89401822014-05-06 15:04:28 -04002177 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002178 return T(Type_v4i8);
John Bauman89401822014-05-06 15:04:28 -04002179 }
2180
John Bauman19bac1e2014-05-06 15:23:49 -04002181 RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04002182 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002183 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002184#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002185 return x86::paddusb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002186#else
2187 return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value))));
2188#endif
John Bauman89401822014-05-06 15:04:28 -04002189 }
John Bauman66b8ab22014-05-06 15:57:45 -04002190
John Bauman19bac1e2014-05-06 15:23:49 -04002191 RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04002192 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002193 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002194#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002195 return x86::psubusb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002196#else
2197 return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value))));
2198#endif
John Bauman89401822014-05-06 15:04:28 -04002199 }
2200
John Bauman19bac1e2014-05-06 15:23:49 -04002201 RValue<Int> SignMask(RValue<Byte8> x)
John Bauman89401822014-05-06 15:04:28 -04002202 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002203 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002204#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002205 return x86::pmovmskb(x);
Logan Chiene3191012018-08-24 22:01:50 +08002206#else
2207 return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType()))));
2208#endif
John Bauman89401822014-05-06 15:04:28 -04002209 }
2210
John Bauman19bac1e2014-05-06 15:23:49 -04002211// RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04002212// {
Logan Chiene3191012018-08-24 22:01:50 +08002213//#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002214// return x86::pcmpgtb(x, y); // FIXME: Signedness
Logan Chiene3191012018-08-24 22:01:50 +08002215//#else
2216// return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType()))));
2217//#endif
John Bauman89401822014-05-06 15:04:28 -04002218// }
John Bauman66b8ab22014-05-06 15:57:45 -04002219
John Bauman19bac1e2014-05-06 15:23:49 -04002220 RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04002221 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002222 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002223#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002224 return x86::pcmpeqb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002225#else
2226 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType()))));
2227#endif
John Bauman89401822014-05-06 15:04:28 -04002228 }
2229
John Bauman19bac1e2014-05-06 15:23:49 -04002230 Type *Byte8::getType()
John Bauman89401822014-05-06 15:04:28 -04002231 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002232 return T(Type_v8i8);
John Bauman89401822014-05-06 15:04:28 -04002233 }
2234
John Bauman19bac1e2014-05-06 15:23:49 -04002235 RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04002236 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002237 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002238#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002239 return x86::paddsb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002240#else
2241 return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value))));
2242#endif
John Bauman89401822014-05-06 15:04:28 -04002243 }
John Bauman66b8ab22014-05-06 15:57:45 -04002244
John Bauman19bac1e2014-05-06 15:23:49 -04002245 RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04002246 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002247 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002248#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002249 return x86::psubsb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002250#else
2251 return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value))));
2252#endif
John Bauman89401822014-05-06 15:04:28 -04002253 }
2254
John Bauman19bac1e2014-05-06 15:23:49 -04002255 RValue<Int> SignMask(RValue<SByte8> x)
John Bauman89401822014-05-06 15:04:28 -04002256 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002257 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002258#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002259 return x86::pmovmskb(As<Byte8>(x));
Logan Chiene3191012018-08-24 22:01:50 +08002260#else
2261 return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType()))));
2262#endif
John Bauman89401822014-05-06 15:04:28 -04002263 }
2264
John Bauman19bac1e2014-05-06 15:23:49 -04002265 RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04002266 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002267 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002268#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002269 return x86::pcmpgtb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002270#else
2271 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType()))));
2272#endif
John Bauman89401822014-05-06 15:04:28 -04002273 }
John Bauman66b8ab22014-05-06 15:57:45 -04002274
John Bauman19bac1e2014-05-06 15:23:49 -04002275 RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04002276 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002277 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002278#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002279 return x86::pcmpeqb(As<Byte8>(x), As<Byte8>(y));
Logan Chiene3191012018-08-24 22:01:50 +08002280#else
2281 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType()))));
2282#endif
John Bauman89401822014-05-06 15:04:28 -04002283 }
2284
John Bauman19bac1e2014-05-06 15:23:49 -04002285 Type *SByte8::getType()
John Bauman89401822014-05-06 15:04:28 -04002286 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002287 return T(Type_v8i8);
John Bauman89401822014-05-06 15:04:28 -04002288 }
2289
John Bauman19bac1e2014-05-06 15:23:49 -04002290 Type *Byte16::getType()
John Bauman89401822014-05-06 15:04:28 -04002291 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002292 return T(llvm::VectorType::get(T(Byte::getType()), 16));
John Bauman89401822014-05-06 15:04:28 -04002293 }
2294
John Bauman19bac1e2014-05-06 15:23:49 -04002295 Type *SByte16::getType()
John Bauman89401822014-05-06 15:04:28 -04002296 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002297 return T(llvm::VectorType::get(T(SByte::getType()), 16));
John Bauman89401822014-05-06 15:04:28 -04002298 }
2299
Nicolas Capens16b5f152016-10-13 13:39:01 -04002300 Type *Short2::getType()
2301 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002302 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002303 }
2304
Nicolas Capens16b5f152016-10-13 13:39:01 -04002305 Type *UShort2::getType()
2306 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002307 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002308 }
2309
John Bauman19bac1e2014-05-06 15:23:49 -04002310 Short4::Short4(RValue<Int4> cast)
John Bauman89401822014-05-06 15:04:28 -04002311 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002312 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens01a97962017-07-28 17:30:51 -04002313 int select[8] = {0, 2, 4, 6, 0, 2, 4, 6};
John Bauman89401822014-05-06 15:04:28 -04002314 Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType());
2315
Nicolas Capens01a97962017-07-28 17:30:51 -04002316 Value *packed = Nucleus::createShuffleVector(short8, short8, select);
2317 Value *short4 = As<Short4>(Int2(As<Int4>(packed))).value;
John Bauman89401822014-05-06 15:04:28 -04002318
John Bauman66b8ab22014-05-06 15:57:45 -04002319 storeValue(short4);
John Bauman89401822014-05-06 15:04:28 -04002320 }
2321
John Bauman19bac1e2014-05-06 15:23:49 -04002322// Short4::Short4(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04002323// {
2324// }
2325
John Bauman19bac1e2014-05-06 15:23:49 -04002326 Short4::Short4(RValue<Float4> cast)
John Bauman89401822014-05-06 15:04:28 -04002327 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002328 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002329 Int4 v4i32 = Int4(cast);
Logan Chiene3191012018-08-24 22:01:50 +08002330#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002331 v4i32 = As<Int4>(x86::packssdw(v4i32, v4i32));
Logan Chiene3191012018-08-24 22:01:50 +08002332#else
2333 Value *v = v4i32.loadValue();
2334 v4i32 = As<Int4>(V(lowerPack(V(v), V(v), true)));
2335#endif
John Bauman66b8ab22014-05-06 15:57:45 -04002336
2337 storeValue(As<Short4>(Int2(v4i32)).value);
John Bauman89401822014-05-06 15:04:28 -04002338 }
2339
John Bauman19bac1e2014-05-06 15:23:49 -04002340 RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002341 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002342 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002343#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002344 // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value));
2345
2346 return x86::psllw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002347#else
2348 return As<Short4>(V(lowerVectorShl(V(lhs.value), rhs)));
2349#endif
John Bauman89401822014-05-06 15:04:28 -04002350 }
2351
John Bauman19bac1e2014-05-06 15:23:49 -04002352 RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002353 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002354 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002355#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002356 return x86::psraw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002357#else
2358 return As<Short4>(V(lowerVectorAShr(V(lhs.value), rhs)));
2359#endif
John Bauman89401822014-05-06 15:04:28 -04002360 }
2361
John Bauman19bac1e2014-05-06 15:23:49 -04002362 RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002363 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002364 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002365#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002366 return x86::pmaxsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002367#else
2368 return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT)));
2369#endif
John Bauman89401822014-05-06 15:04:28 -04002370 }
2371
John Bauman19bac1e2014-05-06 15:23:49 -04002372 RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002373 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002374 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002375#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002376 return x86::pminsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002377#else
2378 return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT)));
2379#endif
John Bauman89401822014-05-06 15:04:28 -04002380 }
2381
John Bauman19bac1e2014-05-06 15:23:49 -04002382 RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002383 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002384 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002385#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002386 return x86::paddsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002387#else
2388 return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value))));
2389#endif
John Bauman89401822014-05-06 15:04:28 -04002390 }
2391
John Bauman19bac1e2014-05-06 15:23:49 -04002392 RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002393 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002394 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002395#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002396 return x86::psubsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002397#else
2398 return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value))));
2399#endif
John Bauman89401822014-05-06 15:04:28 -04002400 }
2401
John Bauman19bac1e2014-05-06 15:23:49 -04002402 RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002403 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002404 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002405#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002406 return x86::pmulhw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002407#else
2408 return As<Short4>(V(lowerMulHigh(V(x.value), V(y.value), true)));
2409#endif
John Bauman89401822014-05-06 15:04:28 -04002410 }
2411
John Bauman19bac1e2014-05-06 15:23:49 -04002412 RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002413 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002414 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002415#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002416 return x86::pmaddwd(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002417#else
2418 return As<Int2>(V(lowerMulAdd(V(x.value), V(y.value))));
2419#endif
John Bauman89401822014-05-06 15:04:28 -04002420 }
2421
Nicolas Capens33438a62017-09-27 11:47:35 -04002422 RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002423 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002424 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002425#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens01a97962017-07-28 17:30:51 -04002426 auto result = x86::packsswb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002427#else
2428 auto result = V(lowerPack(V(x.value), V(y.value), true));
2429#endif
Nicolas Capens01a97962017-07-28 17:30:51 -04002430 return As<SByte8>(Swizzle(As<Int4>(result), 0x88));
John Bauman89401822014-05-06 15:04:28 -04002431 }
2432
Nicolas Capens33438a62017-09-27 11:47:35 -04002433 RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y)
2434 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002435 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002436#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens33438a62017-09-27 11:47:35 -04002437 auto result = x86::packuswb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002438#else
2439 auto result = V(lowerPack(V(x.value), V(y.value), false));
2440#endif
Nicolas Capens33438a62017-09-27 11:47:35 -04002441 return As<Byte8>(Swizzle(As<Int4>(result), 0x88));
2442 }
2443
John Bauman19bac1e2014-05-06 15:23:49 -04002444 RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002445 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002446 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002447#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002448 return x86::pcmpgtw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002449#else
2450 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType()))));
2451#endif
John Bauman89401822014-05-06 15:04:28 -04002452 }
2453
John Bauman19bac1e2014-05-06 15:23:49 -04002454 RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002455 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002456 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002457#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002458 return x86::pcmpeqw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002459#else
2460 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType()))));
2461#endif
John Bauman89401822014-05-06 15:04:28 -04002462 }
2463
John Bauman19bac1e2014-05-06 15:23:49 -04002464 Type *Short4::getType()
John Bauman89401822014-05-06 15:04:28 -04002465 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002466 return T(Type_v4i16);
John Bauman89401822014-05-06 15:04:28 -04002467 }
2468
John Bauman19bac1e2014-05-06 15:23:49 -04002469 UShort4::UShort4(RValue<Float4> cast, bool saturate)
John Bauman89401822014-05-06 15:04:28 -04002470 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002471 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002472 if(saturate)
2473 {
Logan Chiena8385ed2018-09-26 19:22:54 +08002474#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002475 if(CPUID::supportsSSE4_1())
2476 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002477 Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation
Nicolas Capens33438a62017-09-27 11:47:35 -04002478 *this = As<Short4>(PackUnsigned(int4, int4));
John Bauman89401822014-05-06 15:04:28 -04002479 }
2480 else
Logan Chiena8385ed2018-09-26 19:22:54 +08002481#endif
John Bauman89401822014-05-06 15:04:28 -04002482 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002483 *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000))));
John Bauman89401822014-05-06 15:04:28 -04002484 }
2485 }
2486 else
2487 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002488 *this = Short4(Int4(cast));
John Bauman89401822014-05-06 15:04:28 -04002489 }
2490 }
2491
John Bauman19bac1e2014-05-06 15:23:49 -04002492 RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002493 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002494 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002495#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002496 // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value));
2497
2498 return As<UShort4>(x86::psllw(As<Short4>(lhs), rhs));
Logan Chiene3191012018-08-24 22:01:50 +08002499#else
2500 return As<UShort4>(V(lowerVectorShl(V(lhs.value), rhs)));
2501#endif
John Bauman89401822014-05-06 15:04:28 -04002502 }
2503
John Bauman19bac1e2014-05-06 15:23:49 -04002504 RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002505 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002506 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002507#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002508 // return RValue<Short4>(Nucleus::createLShr(lhs.value, rhs.value));
2509
2510 return x86::psrlw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002511#else
2512 return As<UShort4>(V(lowerVectorLShr(V(lhs.value), rhs)));
2513#endif
John Bauman89401822014-05-06 15:04:28 -04002514 }
2515
John Bauman19bac1e2014-05-06 15:23:49 -04002516 RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002517 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002518 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman66b8ab22014-05-06 15:57:45 -04002519 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));
John Bauman89401822014-05-06 15:04:28 -04002520 }
2521
John Bauman19bac1e2014-05-06 15:23:49 -04002522 RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002523 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002524 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman66b8ab22014-05-06 15:57:45 -04002525 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));
John Bauman89401822014-05-06 15:04:28 -04002526 }
2527
John Bauman19bac1e2014-05-06 15:23:49 -04002528 RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002529 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002530 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002531#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002532 return x86::paddusw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002533#else
2534 return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value))));
2535#endif
John Bauman89401822014-05-06 15:04:28 -04002536 }
2537
John Bauman19bac1e2014-05-06 15:23:49 -04002538 RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002539 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002540 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002541#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002542 return x86::psubusw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002543#else
2544 return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value))));
2545#endif
John Bauman89401822014-05-06 15:04:28 -04002546 }
2547
John Bauman19bac1e2014-05-06 15:23:49 -04002548 RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002549 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002550 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002551#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002552 return x86::pmulhuw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002553#else
2554 return As<UShort4>(V(lowerMulHigh(V(x.value), V(y.value), false)));
2555#endif
John Bauman89401822014-05-06 15:04:28 -04002556 }
2557
John Bauman19bac1e2014-05-06 15:23:49 -04002558 RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002559 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002560 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002561#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002562 return x86::pavgw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002563#else
2564 return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value))));
2565#endif
John Bauman89401822014-05-06 15:04:28 -04002566 }
2567
John Bauman19bac1e2014-05-06 15:23:49 -04002568 Type *UShort4::getType()
John Bauman89401822014-05-06 15:04:28 -04002569 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002570 return T(Type_v4i16);
John Bauman89401822014-05-06 15:04:28 -04002571 }
2572
John Bauman19bac1e2014-05-06 15:23:49 -04002573 RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002574 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002575 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002576#if defined(__i386__) || defined(__x86_64__)
2577 return x86::psllw(lhs, rhs);
2578#else
2579 return As<Short8>(V(lowerVectorShl(V(lhs.value), rhs)));
2580#endif
John Bauman89401822014-05-06 15:04:28 -04002581 }
2582
John Bauman19bac1e2014-05-06 15:23:49 -04002583 RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002584 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002585 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002586#if defined(__i386__) || defined(__x86_64__)
2587 return x86::psraw(lhs, rhs);
2588#else
2589 return As<Short8>(V(lowerVectorAShr(V(lhs.value), rhs)));
2590#endif
John Bauman89401822014-05-06 15:04:28 -04002591 }
2592
John Bauman19bac1e2014-05-06 15:23:49 -04002593 RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)
John Bauman89401822014-05-06 15:04:28 -04002594 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002595 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002596#if defined(__i386__) || defined(__x86_64__)
2597 return x86::pmaddwd(x, y);
2598#else
2599 return As<Int4>(V(lowerMulAdd(V(x.value), V(y.value))));
2600#endif
John Bauman89401822014-05-06 15:04:28 -04002601 }
2602
John Bauman19bac1e2014-05-06 15:23:49 -04002603 RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)
John Bauman89401822014-05-06 15:04:28 -04002604 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002605 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002606#if defined(__i386__) || defined(__x86_64__)
2607 return x86::pmulhw(x, y);
2608#else
2609 return As<Short8>(V(lowerMulHigh(V(x.value), V(y.value), true)));
2610#endif
John Bauman89401822014-05-06 15:04:28 -04002611 }
2612
John Bauman19bac1e2014-05-06 15:23:49 -04002613 Type *Short8::getType()
John Bauman89401822014-05-06 15:04:28 -04002614 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002615 return T(llvm::VectorType::get(T(Short::getType()), 8));
John Bauman89401822014-05-06 15:04:28 -04002616 }
2617
John Bauman19bac1e2014-05-06 15:23:49 -04002618 RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002619 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002620 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002621#if defined(__i386__) || defined(__x86_64__)
2622 return As<UShort8>(x86::psllw(As<Short8>(lhs), rhs));
2623#else
2624 return As<UShort8>(V(lowerVectorShl(V(lhs.value), rhs)));
2625#endif
John Bauman89401822014-05-06 15:04:28 -04002626 }
2627
John Bauman19bac1e2014-05-06 15:23:49 -04002628 RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002629 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002630 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002631#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002632 return x86::psrlw(lhs, rhs); // FIXME: Fallback required
Logan Chiene3191012018-08-24 22:01:50 +08002633#else
2634 return As<UShort8>(V(lowerVectorLShr(V(lhs.value), rhs)));
2635#endif
John Bauman89401822014-05-06 15:04:28 -04002636 }
2637
John Bauman19bac1e2014-05-06 15:23:49 -04002638 RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7)
John Bauman89401822014-05-06 15:04:28 -04002639 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002640 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capense89cd582016-09-30 14:23:47 -04002641 int pshufb[16] =
2642 {
2643 select0 + 0,
2644 select0 + 1,
2645 select1 + 0,
2646 select1 + 1,
2647 select2 + 0,
2648 select2 + 1,
2649 select3 + 0,
2650 select3 + 1,
2651 select4 + 0,
2652 select4 + 1,
2653 select5 + 0,
2654 select5 + 1,
2655 select6 + 0,
2656 select6 + 1,
2657 select7 + 0,
2658 select7 + 1,
2659 };
John Bauman89401822014-05-06 15:04:28 -04002660
2661 Value *byte16 = Nucleus::createBitCast(x.value, Byte16::getType());
Nicolas Capense89cd582016-09-30 14:23:47 -04002662 Value *shuffle = Nucleus::createShuffleVector(byte16, byte16, pshufb);
John Bauman89401822014-05-06 15:04:28 -04002663 Value *short8 = Nucleus::createBitCast(shuffle, UShort8::getType());
2664
2665 return RValue<UShort8>(short8);
2666 }
2667
John Bauman19bac1e2014-05-06 15:23:49 -04002668 RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)
John Bauman89401822014-05-06 15:04:28 -04002669 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002670 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002671#if defined(__i386__) || defined(__x86_64__)
2672 return x86::pmulhuw(x, y);
2673#else
2674 return As<UShort8>(V(lowerMulHigh(V(x.value), V(y.value), false)));
2675#endif
John Bauman89401822014-05-06 15:04:28 -04002676 }
2677
John Bauman19bac1e2014-05-06 15:23:49 -04002678 Type *UShort8::getType()
John Bauman89401822014-05-06 15:04:28 -04002679 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002680 return T(llvm::VectorType::get(T(UShort::getType()), 8));
John Bauman89401822014-05-06 15:04:28 -04002681 }
2682
Nicolas Capens96d4e092016-11-18 14:22:38 -05002683 RValue<Int> operator++(Int &val, int) // Post-increment
John Bauman89401822014-05-06 15:04:28 -04002684 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002685 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002686 RValue<Int> res = val;
2687
Logan Chien191b3052018-08-31 16:57:15 +08002688 Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002689 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002690
2691 return res;
2692 }
2693
Nicolas Capens96d4e092016-11-18 14:22:38 -05002694 const Int &operator++(Int &val) // Pre-increment
John Bauman89401822014-05-06 15:04:28 -04002695 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002696 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002697 Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002698 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002699
2700 return val;
2701 }
2702
Nicolas Capens96d4e092016-11-18 14:22:38 -05002703 RValue<Int> operator--(Int &val, int) // Post-decrement
John Bauman89401822014-05-06 15:04:28 -04002704 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002705 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002706 RValue<Int> res = val;
2707
Logan Chien191b3052018-08-31 16:57:15 +08002708 Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002709 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002710
2711 return res;
2712 }
2713
Nicolas Capens96d4e092016-11-18 14:22:38 -05002714 const Int &operator--(Int &val) // Pre-decrement
John Bauman89401822014-05-06 15:04:28 -04002715 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002716 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002717 Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002718 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002719
2720 return val;
2721 }
2722
John Bauman19bac1e2014-05-06 15:23:49 -04002723 RValue<Int> RoundInt(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04002724 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002725 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002726#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002727 return x86::cvtss2si(cast);
Logan Chiene3191012018-08-24 22:01:50 +08002728#else
Logan Chien2faa24a2018-09-26 19:59:32 +08002729 return RValue<Int>(V(lowerRoundInt(V(cast.value), T(Int::getType()))));
Logan Chiene3191012018-08-24 22:01:50 +08002730#endif
John Bauman89401822014-05-06 15:04:28 -04002731 }
2732
John Bauman19bac1e2014-05-06 15:23:49 -04002733 Type *Int::getType()
John Bauman89401822014-05-06 15:04:28 -04002734 {
Nicolas Capensac230122016-09-20 14:30:06 -04002735 return T(llvm::Type::getInt32Ty(*::context));
John Bauman89401822014-05-06 15:04:28 -04002736 }
2737
John Bauman19bac1e2014-05-06 15:23:49 -04002738 Type *Long::getType()
John Bauman89401822014-05-06 15:04:28 -04002739 {
Nicolas Capensac230122016-09-20 14:30:06 -04002740 return T(llvm::Type::getInt64Ty(*::context));
John Bauman89401822014-05-06 15:04:28 -04002741 }
2742
John Bauman19bac1e2014-05-06 15:23:49 -04002743 UInt::UInt(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04002744 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002745 RR_DEBUG_INFO_UPDATE_LOC();
Alexis Hetu764d1422016-09-28 08:44:22 -04002746 // Note: createFPToUI is broken, must perform conversion using createFPtoSI
2747 // Value *integer = Nucleus::createFPToUI(cast.value, UInt::getType());
John Bauman89401822014-05-06 15:04:28 -04002748
Alexis Hetu764d1422016-09-28 08:44:22 -04002749 // Smallest positive value representable in UInt, but not in Int
2750 const unsigned int ustart = 0x80000000u;
2751 const float ustartf = float(ustart);
2752
2753 // If the value is negative, store 0, otherwise store the result of the conversion
2754 storeValue((~(As<Int>(cast) >> 31) &
2755 // Check if the value can be represented as an Int
2756 IfThenElse(cast >= ustartf,
2757 // If the value is too large, subtract ustart and re-add it after conversion.
2758 As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)),
2759 // Otherwise, just convert normally
2760 Int(cast))).value);
John Bauman89401822014-05-06 15:04:28 -04002761 }
2762
Nicolas Capens96d4e092016-11-18 14:22:38 -05002763 RValue<UInt> operator++(UInt &val, int) // Post-increment
John Bauman89401822014-05-06 15:04:28 -04002764 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002765 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002766 RValue<UInt> res = val;
2767
Logan Chien191b3052018-08-31 16:57:15 +08002768 Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002769 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002770
2771 return res;
2772 }
2773
Nicolas Capens96d4e092016-11-18 14:22:38 -05002774 const UInt &operator++(UInt &val) // Pre-increment
John Bauman89401822014-05-06 15:04:28 -04002775 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002776 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002777 Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002778 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002779
2780 return val;
2781 }
2782
Nicolas Capens96d4e092016-11-18 14:22:38 -05002783 RValue<UInt> operator--(UInt &val, int) // Post-decrement
John Bauman89401822014-05-06 15:04:28 -04002784 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002785 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002786 RValue<UInt> res = val;
2787
Logan Chien191b3052018-08-31 16:57:15 +08002788 Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002789 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002790
2791 return res;
2792 }
2793
Nicolas Capens96d4e092016-11-18 14:22:38 -05002794 const UInt &operator--(UInt &val) // Pre-decrement
John Bauman89401822014-05-06 15:04:28 -04002795 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002796 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002797 Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002798 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002799
2800 return val;
2801 }
2802
John Bauman19bac1e2014-05-06 15:23:49 -04002803// RValue<UInt> RoundUInt(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04002804// {
Logan Chiene3191012018-08-24 22:01:50 +08002805//#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002806// return x86::cvtss2si(val); // FIXME: Unsigned
Logan Chiene3191012018-08-24 22:01:50 +08002807//#else
2808// return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f));
2809//#endif
John Bauman89401822014-05-06 15:04:28 -04002810// }
2811
John Bauman19bac1e2014-05-06 15:23:49 -04002812 Type *UInt::getType()
John Bauman89401822014-05-06 15:04:28 -04002813 {
Nicolas Capensac230122016-09-20 14:30:06 -04002814 return T(llvm::Type::getInt32Ty(*::context));
John Bauman89401822014-05-06 15:04:28 -04002815 }
2816
John Bauman19bac1e2014-05-06 15:23:49 -04002817// Int2::Int2(RValue<Int> cast)
2818// {
John Bauman19bac1e2014-05-06 15:23:49 -04002819// Value *extend = Nucleus::createZExt(cast.value, Long::getType());
2820// Value *vector = Nucleus::createBitCast(extend, Int2::getType());
John Bauman66b8ab22014-05-06 15:57:45 -04002821//
Nicolas Capense89cd582016-09-30 14:23:47 -04002822// int shuffle[2] = {0, 0};
2823// Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle);
John Bauman19bac1e2014-05-06 15:23:49 -04002824//
John Bauman66b8ab22014-05-06 15:57:45 -04002825// storeValue(replicate);
John Bauman19bac1e2014-05-06 15:23:49 -04002826// }
John Bauman89401822014-05-06 15:04:28 -04002827
John Bauman19bac1e2014-05-06 15:23:49 -04002828 RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002829 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002830 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002831#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002832 // return RValue<Int2>(Nucleus::createShl(lhs.value, rhs.value));
2833
2834 return x86::pslld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002835#else
2836 return As<Int2>(V(lowerVectorShl(V(lhs.value), rhs)));
2837#endif
John Bauman89401822014-05-06 15:04:28 -04002838 }
2839
John Bauman19bac1e2014-05-06 15:23:49 -04002840 RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002841 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002842 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002843#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002844 // return RValue<Int2>(Nucleus::createAShr(lhs.value, rhs.value));
2845
2846 return x86::psrad(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002847#else
2848 return As<Int2>(V(lowerVectorAShr(V(lhs.value), rhs)));
2849#endif
John Bauman89401822014-05-06 15:04:28 -04002850 }
2851
John Bauman19bac1e2014-05-06 15:23:49 -04002852 Type *Int2::getType()
John Bauman89401822014-05-06 15:04:28 -04002853 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002854 return T(Type_v2i32);
John Bauman89401822014-05-06 15:04:28 -04002855 }
2856
John Bauman19bac1e2014-05-06 15:23:49 -04002857 RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002858 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002859 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002860#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002861 // return RValue<UInt2>(Nucleus::createShl(lhs.value, rhs.value));
2862
2863 return As<UInt2>(x86::pslld(As<Int2>(lhs), rhs));
Logan Chiene3191012018-08-24 22:01:50 +08002864#else
2865 return As<UInt2>(V(lowerVectorShl(V(lhs.value), rhs)));
2866#endif
John Bauman89401822014-05-06 15:04:28 -04002867 }
2868
John Bauman19bac1e2014-05-06 15:23:49 -04002869 RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002870 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002871 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002872#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002873 // return RValue<UInt2>(Nucleus::createLShr(lhs.value, rhs.value));
2874
2875 return x86::psrld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002876#else
2877 return As<UInt2>(V(lowerVectorLShr(V(lhs.value), rhs)));
2878#endif
John Bauman89401822014-05-06 15:04:28 -04002879 }
2880
John Bauman19bac1e2014-05-06 15:23:49 -04002881 Type *UInt2::getType()
John Bauman89401822014-05-06 15:04:28 -04002882 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002883 return T(Type_v2i32);
John Bauman89401822014-05-06 15:04:28 -04002884 }
2885
Nicolas Capenscb986762017-01-20 11:34:37 -05002886 Int4::Int4(RValue<Byte4> cast) : XYZW(this)
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002887 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002888 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002889#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens01a97962017-07-28 17:30:51 -04002890 if(CPUID::supportsSSE4_1())
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002891 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002892 *this = x86::pmovzxbd(As<Byte16>(cast));
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002893 }
2894 else
Logan Chiene3191012018-08-24 22:01:50 +08002895#endif
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002896 {
Nicolas Capense89cd582016-09-30 14:23:47 -04002897 int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};
Nicolas Capens01a97962017-07-28 17:30:51 -04002898 Value *a = Nucleus::createBitCast(cast.value, Byte16::getType());
Logan Chien191b3052018-08-31 16:57:15 +08002899 Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::getType()), swizzle);
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002900
Nicolas Capense89cd582016-09-30 14:23:47 -04002901 int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11};
Nicolas Capens01a97962017-07-28 17:30:51 -04002902 Value *c = Nucleus::createBitCast(b, Short8::getType());
Logan Chien191b3052018-08-31 16:57:15 +08002903 Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::getType()), swizzle2);
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002904
Nicolas Capens01a97962017-07-28 17:30:51 -04002905 *this = As<Int4>(d);
2906 }
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002907 }
2908
Nicolas Capenscb986762017-01-20 11:34:37 -05002909 Int4::Int4(RValue<SByte4> cast) : XYZW(this)
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002910 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002911 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002912#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens01a97962017-07-28 17:30:51 -04002913 if(CPUID::supportsSSE4_1())
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002914 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002915 *this = x86::pmovsxbd(As<SByte16>(cast));
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002916 }
2917 else
Logan Chiene3191012018-08-24 22:01:50 +08002918#endif
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002919 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002920 int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7};
2921 Value *a = Nucleus::createBitCast(cast.value, Byte16::getType());
2922 Value *b = Nucleus::createShuffleVector(a, a, swizzle);
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002923
Nicolas Capense89cd582016-09-30 14:23:47 -04002924 int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3};
Nicolas Capens01a97962017-07-28 17:30:51 -04002925 Value *c = Nucleus::createBitCast(b, Short8::getType());
2926 Value *d = Nucleus::createShuffleVector(c, c, swizzle2);
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002927
Nicolas Capens01a97962017-07-28 17:30:51 -04002928 *this = As<Int4>(d) >> 24;
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002929 }
Meng-Lin Wu601d0052016-06-10 14:18:41 -04002930 }
2931
Nicolas Capenscb986762017-01-20 11:34:37 -05002932 Int4::Int4(RValue<Short4> cast) : XYZW(this)
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002933 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002934 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002935#if defined(__i386__) || defined(__x86_64__)
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002936 if(CPUID::supportsSSE4_1())
2937 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002938 *this = x86::pmovsxwd(As<Short8>(cast));
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002939 }
2940 else
Logan Chiene3191012018-08-24 22:01:50 +08002941#endif
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002942 {
Nicolas Capense89cd582016-09-30 14:23:47 -04002943 int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3};
Nicolas Capens01a97962017-07-28 17:30:51 -04002944 Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle);
2945 *this = As<Int4>(c) >> 16;
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002946 }
2947 }
2948
Nicolas Capenscb986762017-01-20 11:34:37 -05002949 Int4::Int4(RValue<UShort4> cast) : XYZW(this)
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002950 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002951 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002952#if defined(__i386__) || defined(__x86_64__)
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002953 if(CPUID::supportsSSE4_1())
2954 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002955 *this = x86::pmovzxwd(As<UShort8>(cast));
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002956 }
2957 else
Logan Chiene3191012018-08-24 22:01:50 +08002958#endif
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002959 {
Nicolas Capense89cd582016-09-30 14:23:47 -04002960 int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11};
Nicolas Capens01a97962017-07-28 17:30:51 -04002961 Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle);
2962 *this = As<Int4>(c);
Alexis Hetu2aa852f2015-10-14 16:32:39 -04002963 }
2964 }
2965
Nicolas Capenscb986762017-01-20 11:34:37 -05002966 Int4::Int4(RValue<Int> rhs) : XYZW(this)
Nicolas Capens24c8cf02016-08-15 15:33:14 -04002967 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002968 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens24c8cf02016-08-15 15:33:14 -04002969 Value *vector = loadValue();
2970 Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0);
2971
Nicolas Capense89cd582016-09-30 14:23:47 -04002972 int swizzle[4] = {0, 0, 0, 0};
2973 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
Nicolas Capens24c8cf02016-08-15 15:33:14 -04002974
2975 storeValue(replicate);
2976 }
2977
John Bauman19bac1e2014-05-06 15:23:49 -04002978 RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002979 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002980 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002981#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002982 return x86::pslld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002983#else
2984 return As<Int4>(V(lowerVectorShl(V(lhs.value), rhs)));
2985#endif
John Bauman89401822014-05-06 15:04:28 -04002986 }
2987
John Bauman19bac1e2014-05-06 15:23:49 -04002988 RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002989 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002990 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002991#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002992 return x86::psrad(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002993#else
2994 return As<Int4>(V(lowerVectorAShr(V(lhs.value), rhs)));
2995#endif
John Bauman89401822014-05-06 15:04:28 -04002996 }
2997
John Bauman19bac1e2014-05-06 15:23:49 -04002998 RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y)
2999 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003000 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003001 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003002 }
3003
3004 RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y)
3005 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003006 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003007 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003008 }
3009
3010 RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y)
3011 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003012 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003013 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003014 }
3015
3016 RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y)
3017 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003018 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003019 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003020 }
3021
3022 RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y)
3023 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003024 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003025 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003026 }
3027
3028 RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y)
3029 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003030 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003031 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003032 }
3033
3034 RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y)
3035 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003036 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003037#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003038 if(CPUID::supportsSSE4_1())
3039 {
3040 return x86::pmaxsd(x, y);
3041 }
3042 else
Logan Chiene3191012018-08-24 22:01:50 +08003043#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003044 {
3045 RValue<Int4> greater = CmpNLE(x, y);
Tom Anderson69bc6e82017-03-20 11:54:29 -07003046 return (x & greater) | (y & ~greater);
John Bauman19bac1e2014-05-06 15:23:49 -04003047 }
3048 }
3049
3050 RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y)
3051 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003052 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003053#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003054 if(CPUID::supportsSSE4_1())
3055 {
3056 return x86::pminsd(x, y);
3057 }
3058 else
Logan Chiene3191012018-08-24 22:01:50 +08003059#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003060 {
3061 RValue<Int4> less = CmpLT(x, y);
Tom Anderson69bc6e82017-03-20 11:54:29 -07003062 return (x & less) | (y & ~less);
John Bauman19bac1e2014-05-06 15:23:49 -04003063 }
3064 }
3065
3066 RValue<Int4> RoundInt(RValue<Float4> cast)
John Bauman89401822014-05-06 15:04:28 -04003067 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003068 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003069#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003070 return x86::cvtps2dq(cast);
Logan Chiene3191012018-08-24 22:01:50 +08003071#else
Logan Chien2faa24a2018-09-26 19:59:32 +08003072 return As<Int4>(V(lowerRoundInt(V(cast.value), T(Int4::getType()))));
Logan Chiene3191012018-08-24 22:01:50 +08003073#endif
John Bauman89401822014-05-06 15:04:28 -04003074 }
3075
Chris Forbese86b6dc2019-03-01 09:08:47 -08003076 RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y)
3077 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003078 RR_DEBUG_INFO_UPDATE_LOC();
Chris Forbese86b6dc2019-03-01 09:08:47 -08003079 // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq.
3080 return As<Int4>(V(lowerMulHigh(V(x.value), V(y.value), true)));
3081 }
3082
3083 RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y)
3084 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003085 RR_DEBUG_INFO_UPDATE_LOC();
Chris Forbese86b6dc2019-03-01 09:08:47 -08003086 // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq.
3087 return As<UInt4>(V(lowerMulHigh(V(x.value), V(y.value), false)));
3088 }
3089
Nicolas Capens33438a62017-09-27 11:47:35 -04003090 RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y)
John Bauman89401822014-05-06 15:04:28 -04003091 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003092 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003093#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003094 return x86::packssdw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08003095#else
3096 return As<Short8>(V(lowerPack(V(x.value), V(y.value), true)));
3097#endif
John Bauman89401822014-05-06 15:04:28 -04003098 }
3099
Nicolas Capens33438a62017-09-27 11:47:35 -04003100 RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y)
3101 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003102 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003103#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens33438a62017-09-27 11:47:35 -04003104 return x86::packusdw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08003105#else
3106 return As<UShort8>(V(lowerPack(V(x.value), V(y.value), false)));
3107#endif
Nicolas Capens33438a62017-09-27 11:47:35 -04003108 }
3109
John Bauman19bac1e2014-05-06 15:23:49 -04003110 RValue<Int> SignMask(RValue<Int4> x)
John Bauman89401822014-05-06 15:04:28 -04003111 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003112 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003113#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003114 return x86::movmskps(As<Float4>(x));
Logan Chiene3191012018-08-24 22:01:50 +08003115#else
3116 return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType()))));
3117#endif
John Bauman89401822014-05-06 15:04:28 -04003118 }
3119
John Bauman19bac1e2014-05-06 15:23:49 -04003120 Type *Int4::getType()
John Bauman89401822014-05-06 15:04:28 -04003121 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003122 return T(llvm::VectorType::get(T(Int::getType()), 4));
John Bauman89401822014-05-06 15:04:28 -04003123 }
3124
Nicolas Capenscb986762017-01-20 11:34:37 -05003125 UInt4::UInt4(RValue<Float4> cast) : XYZW(this)
John Bauman89401822014-05-06 15:04:28 -04003126 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003127 RR_DEBUG_INFO_UPDATE_LOC();
Alexis Hetu764d1422016-09-28 08:44:22 -04003128 // Note: createFPToUI is broken, must perform conversion using createFPtoSI
3129 // Value *xyzw = Nucleus::createFPToUI(cast.value, UInt4::getType());
John Bauman89401822014-05-06 15:04:28 -04003130
Alexis Hetu764d1422016-09-28 08:44:22 -04003131 // Smallest positive value representable in UInt, but not in Int
3132 const unsigned int ustart = 0x80000000u;
3133 const float ustartf = float(ustart);
3134
3135 // Check if the value can be represented as an Int
3136 Int4 uiValue = CmpNLT(cast, Float4(ustartf));
3137 // If the value is too large, subtract ustart and re-add it after conversion.
3138 uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) |
3139 // Otherwise, just convert normally
3140 (~uiValue & Int4(cast));
3141 // If the value is negative, store 0, otherwise store the result of the conversion
3142 storeValue((~(As<Int4>(cast) >> 31) & uiValue).value);
John Bauman89401822014-05-06 15:04:28 -04003143 }
3144
Ben Clayton88816fa2019-05-15 17:08:14 +01003145 UInt4::UInt4(RValue<UInt> rhs) : XYZW(this)
3146 {
3147 RR_DEBUG_INFO_UPDATE_LOC();
3148 Value *vector = loadValue();
3149 Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0);
3150
3151 int swizzle[4] = {0, 0, 0, 0};
3152 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
3153
3154 storeValue(replicate);
3155 }
3156
John Bauman19bac1e2014-05-06 15:23:49 -04003157 RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003158 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003159 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003160#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003161 return As<UInt4>(x86::pslld(As<Int4>(lhs), rhs));
Logan Chiene3191012018-08-24 22:01:50 +08003162#else
3163 return As<UInt4>(V(lowerVectorShl(V(lhs.value), rhs)));
3164#endif
John Bauman89401822014-05-06 15:04:28 -04003165 }
3166
John Bauman19bac1e2014-05-06 15:23:49 -04003167 RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003168 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003169 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003170#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003171 return x86::psrld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08003172#else
3173 return As<UInt4>(V(lowerVectorLShr(V(lhs.value), rhs)));
3174#endif
John Bauman89401822014-05-06 15:04:28 -04003175 }
3176
John Bauman19bac1e2014-05-06 15:23:49 -04003177 RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y)
3178 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003179 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003180 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003181 }
3182
3183 RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y)
3184 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003185 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman19bac1e2014-05-06 15:23:49 -04003186 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType()));
3187 }
3188
3189 RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y)
3190 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003191 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003192 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003193 }
3194
3195 RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y)
3196 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003197 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman19bac1e2014-05-06 15:23:49 -04003198 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType()));
3199 }
3200
3201 RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y)
3202 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003203 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003204 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003205 }
3206
3207 RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y)
3208 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003209 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman19bac1e2014-05-06 15:23:49 -04003210 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType()));
3211 }
3212
3213 RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y)
3214 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003215 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003216#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003217 if(CPUID::supportsSSE4_1())
3218 {
3219 return x86::pmaxud(x, y);
3220 }
3221 else
Logan Chiene3191012018-08-24 22:01:50 +08003222#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003223 {
3224 RValue<UInt4> greater = CmpNLE(x, y);
Tom Anderson69bc6e82017-03-20 11:54:29 -07003225 return (x & greater) | (y & ~greater);
John Bauman19bac1e2014-05-06 15:23:49 -04003226 }
3227 }
3228
3229 RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y)
3230 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003231 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003232#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003233 if(CPUID::supportsSSE4_1())
3234 {
3235 return x86::pminud(x, y);
3236 }
3237 else
Logan Chiene3191012018-08-24 22:01:50 +08003238#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003239 {
3240 RValue<UInt4> less = CmpLT(x, y);
Tom Anderson69bc6e82017-03-20 11:54:29 -07003241 return (x & less) | (y & ~less);
John Bauman19bac1e2014-05-06 15:23:49 -04003242 }
3243 }
3244
John Bauman19bac1e2014-05-06 15:23:49 -04003245 Type *UInt4::getType()
John Bauman89401822014-05-06 15:04:28 -04003246 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003247 return T(llvm::VectorType::get(T(UInt::getType()), 4));
John Bauman89401822014-05-06 15:04:28 -04003248 }
3249
Alexis Hetu734e2572018-12-20 14:00:49 -05003250 Type *Half::getType()
3251 {
3252 return T(llvm::Type::getInt16Ty(*::context));
3253 }
3254
Nicolas Capens05b3d662016-02-25 23:58:33 -05003255 RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2)
John Bauman89401822014-05-06 15:04:28 -04003256 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003257 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003258#if defined(__i386__) || defined(__x86_64__)
3259 if(exactAtPow2)
3260 {
3261 // rcpss uses a piecewise-linear approximation which minimizes the relative error
3262 // but is not exact at power-of-two values. Rectify by multiplying by the inverse.
3263 return x86::rcpss(x) * Float(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f))));
3264 }
Nicolas Capens47dc8672017-04-25 12:54:39 -04003265 return x86::rcpss(x);
Logan Chiene3191012018-08-24 22:01:50 +08003266#else
3267 return As<Float>(V(lowerRCP(V(x.value))));
3268#endif
John Bauman89401822014-05-06 15:04:28 -04003269 }
John Bauman66b8ab22014-05-06 15:57:45 -04003270
John Bauman19bac1e2014-05-06 15:23:49 -04003271 RValue<Float> RcpSqrt_pp(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003272 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003273 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003274#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003275 return x86::rsqrtss(x);
Logan Chiene3191012018-08-24 22:01:50 +08003276#else
3277 return As<Float>(V(lowerRSQRT(V(x.value))));
3278#endif
John Bauman89401822014-05-06 15:04:28 -04003279 }
3280
John Bauman19bac1e2014-05-06 15:23:49 -04003281 RValue<Float> Sqrt(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003282 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003283 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003284#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003285 return x86::sqrtss(x);
Logan Chiene3191012018-08-24 22:01:50 +08003286#else
3287 return As<Float>(V(lowerSQRT(V(x.value))));
3288#endif
John Bauman89401822014-05-06 15:04:28 -04003289 }
3290
John Bauman19bac1e2014-05-06 15:23:49 -04003291 RValue<Float> Round(RValue<Float> x)
3292 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003293 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003294#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003295 if(CPUID::supportsSSE4_1())
3296 {
3297 return x86::roundss(x, 0);
3298 }
3299 else
3300 {
3301 return Float4(Round(Float4(x))).x;
3302 }
Logan Chien83fc07a2018-09-26 22:14:00 +08003303#else
3304 return RValue<Float>(V(lowerRound(V(x.value))));
3305#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003306 }
3307
3308 RValue<Float> Trunc(RValue<Float> x)
3309 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003310 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003311#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003312 if(CPUID::supportsSSE4_1())
3313 {
3314 return x86::roundss(x, 3);
3315 }
3316 else
3317 {
3318 return Float(Int(x)); // Rounded toward zero
3319 }
Logan Chien8c5ca8d2018-09-27 21:05:53 +08003320#else
3321 return RValue<Float>(V(lowerTrunc(V(x.value))));
3322#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003323 }
3324
3325 RValue<Float> Frac(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003326 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003327 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003328#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003329 if(CPUID::supportsSSE4_1())
3330 {
3331 return x - x86::floorss(x);
3332 }
3333 else
3334 {
John Bauman19bac1e2014-05-06 15:23:49 -04003335 return Float4(Frac(Float4(x))).x;
John Bauman89401822014-05-06 15:04:28 -04003336 }
Logan Chien3c6a1ae2018-09-26 22:18:16 +08003337#else
3338 // x - floor(x) can be 1.0 for very small negative x.
3339 // Clamp against the value just below 1.0.
3340 return Min(x - Floor(x), As<Float>(Int(0x3F7FFFFF)));
3341#endif
John Bauman89401822014-05-06 15:04:28 -04003342 }
3343
John Bauman19bac1e2014-05-06 15:23:49 -04003344 RValue<Float> Floor(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003345 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003346 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003347#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003348 if(CPUID::supportsSSE4_1())
3349 {
3350 return x86::floorss(x);
3351 }
3352 else
3353 {
3354 return Float4(Floor(Float4(x))).x;
3355 }
Logan Chien40a60052018-09-26 19:03:53 +08003356#else
3357 return RValue<Float>(V(lowerFloor(V(x.value))));
3358#endif
John Bauman89401822014-05-06 15:04:28 -04003359 }
3360
John Bauman19bac1e2014-05-06 15:23:49 -04003361 RValue<Float> Ceil(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003362 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003363 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003364#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003365 if(CPUID::supportsSSE4_1())
3366 {
3367 return x86::ceilss(x);
3368 }
3369 else
Logan Chiene3191012018-08-24 22:01:50 +08003370#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003371 {
3372 return Float4(Ceil(Float4(x))).x;
3373 }
John Bauman89401822014-05-06 15:04:28 -04003374 }
3375
John Bauman19bac1e2014-05-06 15:23:49 -04003376 Type *Float::getType()
John Bauman89401822014-05-06 15:04:28 -04003377 {
Nicolas Capensac230122016-09-20 14:30:06 -04003378 return T(llvm::Type::getFloatTy(*::context));
John Bauman89401822014-05-06 15:04:28 -04003379 }
3380
John Bauman19bac1e2014-05-06 15:23:49 -04003381 Type *Float2::getType()
John Bauman89401822014-05-06 15:04:28 -04003382 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003383 return T(Type_v2f32);
John Bauman89401822014-05-06 15:04:28 -04003384 }
3385
Nicolas Capenscb986762017-01-20 11:34:37 -05003386 Float4::Float4(RValue<Float> rhs) : XYZW(this)
John Bauman89401822014-05-06 15:04:28 -04003387 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003388 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman66b8ab22014-05-06 15:57:45 -04003389 Value *vector = loadValue();
John Bauman89401822014-05-06 15:04:28 -04003390 Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0);
3391
Nicolas Capense89cd582016-09-30 14:23:47 -04003392 int swizzle[4] = {0, 0, 0, 0};
3393 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
John Bauman89401822014-05-06 15:04:28 -04003394
John Bauman66b8ab22014-05-06 15:57:45 -04003395 storeValue(replicate);
John Bauman89401822014-05-06 15:04:28 -04003396 }
3397
John Bauman19bac1e2014-05-06 15:23:49 -04003398 RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003399 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003400 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003401#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003402 return x86::maxps(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08003403#else
3404 return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OGT)));
3405#endif
John Bauman89401822014-05-06 15:04:28 -04003406 }
3407
John Bauman19bac1e2014-05-06 15:23:49 -04003408 RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003409 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003410 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003411#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003412 return x86::minps(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08003413#else
3414 return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OLT)));
3415#endif
John Bauman89401822014-05-06 15:04:28 -04003416 }
3417
Nicolas Capens05b3d662016-02-25 23:58:33 -05003418 RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2)
John Bauman89401822014-05-06 15:04:28 -04003419 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003420 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003421#if defined(__i386__) || defined(__x86_64__)
3422 if(exactAtPow2)
3423 {
3424 // rcpps uses a piecewise-linear approximation which minimizes the relative error
3425 // but is not exact at power-of-two values. Rectify by multiplying by the inverse.
3426 return x86::rcpps(x) * Float4(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f))));
3427 }
Nicolas Capens47dc8672017-04-25 12:54:39 -04003428 return x86::rcpps(x);
Logan Chiene3191012018-08-24 22:01:50 +08003429#else
3430 return As<Float4>(V(lowerRCP(V(x.value))));
3431#endif
John Bauman89401822014-05-06 15:04:28 -04003432 }
John Bauman66b8ab22014-05-06 15:57:45 -04003433
John Bauman19bac1e2014-05-06 15:23:49 -04003434 RValue<Float4> RcpSqrt_pp(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003435 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003436 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003437#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003438 return x86::rsqrtps(x);
Logan Chiene3191012018-08-24 22:01:50 +08003439#else
3440 return As<Float4>(V(lowerRSQRT(V(x.value))));
3441#endif
John Bauman89401822014-05-06 15:04:28 -04003442 }
3443
John Bauman19bac1e2014-05-06 15:23:49 -04003444 RValue<Float4> Sqrt(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003445 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003446 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003447#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003448 return x86::sqrtps(x);
Logan Chiene3191012018-08-24 22:01:50 +08003449#else
3450 return As<Float4>(V(lowerSQRT(V(x.value))));
3451#endif
John Bauman89401822014-05-06 15:04:28 -04003452 }
3453
John Bauman19bac1e2014-05-06 15:23:49 -04003454 RValue<Int> SignMask(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003455 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003456 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003457#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003458 return x86::movmskps(x);
Logan Chiene3191012018-08-24 22:01:50 +08003459#else
3460 return As<Int>(V(lowerFPSignMask(V(x.value), T(Int::getType()))));
3461#endif
John Bauman89401822014-05-06 15:04:28 -04003462 }
3463
John Bauman19bac1e2014-05-06 15:23:49 -04003464 RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003465 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003466 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003467 // return As<Int4>(x86::cmpeqps(x, y));
3468 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value, y.value), Int4::getType()));
3469 }
3470
John Bauman19bac1e2014-05-06 15:23:49 -04003471 RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003472 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003473 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003474 // return As<Int4>(x86::cmpltps(x, y));
3475 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value, y.value), Int4::getType()));
3476 }
3477
John Bauman19bac1e2014-05-06 15:23:49 -04003478 RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003479 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003480 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003481 // return As<Int4>(x86::cmpleps(x, y));
3482 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value, y.value), Int4::getType()));
3483 }
3484
John Bauman19bac1e2014-05-06 15:23:49 -04003485 RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003486 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003487 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003488 // return As<Int4>(x86::cmpneqps(x, y));
3489 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value, y.value), Int4::getType()));
3490 }
3491
John Bauman19bac1e2014-05-06 15:23:49 -04003492 RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003493 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003494 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003495 // return As<Int4>(x86::cmpnltps(x, y));
3496 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value, y.value), Int4::getType()));
3497 }
3498
John Bauman19bac1e2014-05-06 15:23:49 -04003499 RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003500 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003501 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003502 // return As<Int4>(x86::cmpnleps(x, y));
3503 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value, y.value), Int4::getType()));
3504 }
3505
Ben Claytonec1aeb82019-03-04 19:33:27 +00003506 RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y)
3507 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003508 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003509 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUEQ(x.value, y.value), Int4::getType()));
3510 }
3511
3512 RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y)
3513 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003514 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003515 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULT(x.value, y.value), Int4::getType()));
3516 }
3517
3518 RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y)
3519 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003520 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003521 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULE(x.value, y.value), Int4::getType()));
3522 }
3523
3524 RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y)
3525 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003526 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003527 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUNE(x.value, y.value), Int4::getType()));
3528 }
3529
3530 RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y)
3531 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003532 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003533 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGE(x.value, y.value), Int4::getType()));
3534 }
3535
3536 RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y)
3537 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003538 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003539 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value, y.value), Int4::getType()));
3540 }
3541
John Bauman19bac1e2014-05-06 15:23:49 -04003542 RValue<Float4> Round(RValue<Float4> x)
3543 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003544 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003545#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003546 if(CPUID::supportsSSE4_1())
3547 {
3548 return x86::roundps(x, 0);
3549 }
3550 else
3551 {
3552 return Float4(RoundInt(x));
3553 }
Logan Chien83fc07a2018-09-26 22:14:00 +08003554#else
3555 return RValue<Float4>(V(lowerRound(V(x.value))));
3556#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003557 }
3558
3559 RValue<Float4> Trunc(RValue<Float4> x)
3560 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003561 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003562#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003563 if(CPUID::supportsSSE4_1())
3564 {
3565 return x86::roundps(x, 3);
3566 }
3567 else
3568 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003569 return Float4(Int4(x));
John Bauman19bac1e2014-05-06 15:23:49 -04003570 }
Logan Chien8c5ca8d2018-09-27 21:05:53 +08003571#else
3572 return RValue<Float4>(V(lowerTrunc(V(x.value))));
3573#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003574 }
3575
3576 RValue<Float4> Frac(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003577 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003578 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb9230422017-07-17 10:27:33 -04003579 Float4 frc;
3580
Logan Chien40a60052018-09-26 19:03:53 +08003581#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003582 if(CPUID::supportsSSE4_1())
3583 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003584 frc = x - Floor(x);
John Bauman89401822014-05-06 15:04:28 -04003585 }
3586 else
3587 {
Nicolas Capensb9230422017-07-17 10:27:33 -04003588 frc = x - Float4(Int4(x)); // Signed fractional part.
John Bauman89401822014-05-06 15:04:28 -04003589
Nicolas Capensb9230422017-07-17 10:27:33 -04003590 frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1.0f))); // Add 1.0 if negative.
John Bauman89401822014-05-06 15:04:28 -04003591 }
Logan Chien3c6a1ae2018-09-26 22:18:16 +08003592#else
3593 frc = x - Floor(x);
3594#endif
Nicolas Capensb9230422017-07-17 10:27:33 -04003595
3596 // x - floor(x) can be 1.0 for very small negative x.
3597 // Clamp against the value just below 1.0.
3598 return Min(frc, As<Float4>(Int4(0x3F7FFFFF)));
John Bauman89401822014-05-06 15:04:28 -04003599 }
3600
John Bauman19bac1e2014-05-06 15:23:49 -04003601 RValue<Float4> Floor(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003602 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003603 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003604#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003605 if(CPUID::supportsSSE4_1())
3606 {
3607 return x86::floorps(x);
3608 }
3609 else
3610 {
John Bauman19bac1e2014-05-06 15:23:49 -04003611 return x - Frac(x);
John Bauman89401822014-05-06 15:04:28 -04003612 }
Logan Chien40a60052018-09-26 19:03:53 +08003613#else
3614 return RValue<Float4>(V(lowerFloor(V(x.value))));
3615#endif
John Bauman89401822014-05-06 15:04:28 -04003616 }
3617
John Bauman19bac1e2014-05-06 15:23:49 -04003618 RValue<Float4> Ceil(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003619 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003620 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003621#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003622 if(CPUID::supportsSSE4_1())
3623 {
3624 return x86::ceilps(x);
3625 }
3626 else
Logan Chiene3191012018-08-24 22:01:50 +08003627#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003628 {
3629 return -Floor(-x);
3630 }
John Bauman89401822014-05-06 15:04:28 -04003631 }
3632
Ben Claytona2c8b772019-04-09 13:42:36 -04003633 RValue<Float4> Sin(RValue<Float4> v)
3634 {
3635 auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sin, { V(v.value)->getType() } );
3636 return RValue<Float4>(V(::builder->CreateCall(func, V(v.value))));
3637 }
3638
Ben Clayton1b6f8c72019-04-09 13:47:43 -04003639 RValue<Float4> Cos(RValue<Float4> v)
3640 {
3641 auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::cos, { V(v.value)->getType() } );
3642 return RValue<Float4>(V(::builder->CreateCall(func, V(v.value))));
3643 }
3644
Ben Clayton14740062019-04-09 13:48:41 -04003645 RValue<Float4> Tan(RValue<Float4> v)
3646 {
3647 return Sin(v) / Cos(v);
3648 }
3649
Ben Claytoneafae472019-04-09 14:22:38 -04003650 static RValue<Float4> TransformFloat4PerElement(RValue<Float4> v, const char* name)
Ben Claytonf9350d72019-04-09 14:19:02 -04003651 {
Ben Claytonc38fc122019-04-11 08:58:49 -04003652 auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), ::llvm::ArrayRef<llvm::Type*>(T(Float::getType())), false);
Ben Claytoneafae472019-04-09 14:22:38 -04003653 auto func = ::module->getOrInsertFunction(name, funcTy);
Ben Claytonf9350d72019-04-09 14:19:02 -04003654 llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType()));
3655 for (uint64_t i = 0; i < 4; i++)
3656 {
Ben Claytonc38fc122019-04-11 08:58:49 -04003657 auto el = ::builder->CreateCall(func, V(Nucleus::createExtractElement(v.value, Float::getType(), i)));
3658 out = V(Nucleus::createInsertElement(V(out), V(el), i));
Ben Claytonf9350d72019-04-09 14:19:02 -04003659 }
3660 return RValue<Float4>(V(out));
3661 }
3662
Ben Claytoneafae472019-04-09 14:22:38 -04003663 RValue<Float4> Asin(RValue<Float4> v)
3664 {
3665 return TransformFloat4PerElement(v, "asinf");
3666 }
3667
3668 RValue<Float4> Acos(RValue<Float4> v)
3669 {
3670 return TransformFloat4PerElement(v, "acosf");
3671 }
3672
Ben Clayton749b4e02019-04-09 14:27:43 -04003673 RValue<Float4> Atan(RValue<Float4> v)
3674 {
3675 return TransformFloat4PerElement(v, "atanf");
3676 }
3677
Ben Claytond9636972019-04-09 15:09:54 -04003678 RValue<Float4> Sinh(RValue<Float4> v)
3679 {
3680 return TransformFloat4PerElement(v, "sinhf");
3681 }
3682
Ben Clayton900ea2c2019-04-09 15:25:36 -04003683 RValue<Float4> Cosh(RValue<Float4> v)
3684 {
3685 return TransformFloat4PerElement(v, "coshf");
3686 }
3687
Ben Clayton3928bd92019-04-09 15:27:41 -04003688 RValue<Float4> Tanh(RValue<Float4> v)
3689 {
3690 return TransformFloat4PerElement(v, "tanhf");
3691 }
3692
Ben Claytonf6d77ab2019-04-09 15:30:04 -04003693 RValue<Float4> Asinh(RValue<Float4> v)
3694 {
3695 return TransformFloat4PerElement(v, "asinhf");
3696 }
3697
Ben Clayton28ebcb02019-04-09 15:33:38 -04003698 RValue<Float4> Acosh(RValue<Float4> v)
3699 {
3700 return TransformFloat4PerElement(v, "acoshf");
3701 }
3702
Ben Claytonfa6a5392019-04-09 15:35:24 -04003703 RValue<Float4> Atanh(RValue<Float4> v)
3704 {
3705 return TransformFloat4PerElement(v, "atanhf");
3706 }
3707
Ben Claytona520c3e2019-04-09 15:43:45 -04003708 RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y)
3709 {
Ben Claytonc38fc122019-04-11 08:58:49 -04003710 ::llvm::SmallVector<::llvm::Type*, 2> paramTys;
3711 paramTys.push_back(T(Float::getType()));
3712 paramTys.push_back(T(Float::getType()));
3713 auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), paramTys, false);
Ben Claytona520c3e2019-04-09 15:43:45 -04003714 auto func = ::module->getOrInsertFunction("atan2f", funcTy);
3715 llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType()));
3716 for (uint64_t i = 0; i < 4; i++)
3717 {
Ben Claytonc38fc122019-04-11 08:58:49 -04003718 auto el = ::builder->CreateCall2(func, ARGS(
3719 V(Nucleus::createExtractElement(x.value, Float::getType(), i)),
3720 V(Nucleus::createExtractElement(y.value, Float::getType(), i))
3721 ));
3722 out = V(Nucleus::createInsertElement(V(out), V(el), i));
Ben Claytona520c3e2019-04-09 15:43:45 -04003723 }
3724 return RValue<Float4>(V(out));
3725 }
3726
Ben Claytonbfe94f02019-04-09 15:52:12 -04003727 RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y)
3728 {
Ben Clayton7579db12019-05-02 08:37:12 +01003729 auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::pow, { T(Float4::getType()) });
Ben Claytonc38fc122019-04-11 08:58:49 -04003730 return RValue<Float4>(V(::builder->CreateCall2(func, ARGS(V(x.value), V(y.value)))));
Ben Claytonbfe94f02019-04-09 15:52:12 -04003731 }
3732
Ben Clayton242f0022019-04-09 16:00:53 -04003733 RValue<Float4> Exp(RValue<Float4> v)
3734 {
3735 auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::exp, { T(Float4::getType()) } );
Ben Claytonc38fc122019-04-11 08:58:49 -04003736 return RValue<Float4>(V(::builder->CreateCall(func, V(v.value))));
Ben Clayton242f0022019-04-09 16:00:53 -04003737 }
3738
Ben Clayton2c1da722019-04-09 16:03:03 -04003739 RValue<Float4> Log(RValue<Float4> v)
3740 {
3741 auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::log, { T(Float4::getType()) } );
Ben Claytonc38fc122019-04-11 08:58:49 -04003742 return RValue<Float4>(V(::builder->CreateCall(func, V(v.value))));
Ben Clayton2c1da722019-04-09 16:03:03 -04003743 }
3744
Ben Claytonf40b56c2019-04-09 16:06:55 -04003745 RValue<Float4> Exp2(RValue<Float4> v)
3746 {
3747 auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::exp2, { T(Float4::getType()) } );
Ben Claytonc38fc122019-04-11 08:58:49 -04003748 return RValue<Float4>(V(::builder->CreateCall(func, V(v.value))));
Ben Claytonf40b56c2019-04-09 16:06:55 -04003749 }
3750
Ben Claytone17acfe2019-04-09 16:09:13 -04003751 RValue<Float4> Log2(RValue<Float4> v)
3752 {
3753 auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::log2, { T(Float4::getType()) } );
Ben Claytonc38fc122019-04-11 08:58:49 -04003754 return RValue<Float4>(V(::builder->CreateCall(func, V(v.value))));
Ben Claytone17acfe2019-04-09 16:09:13 -04003755 }
3756
Ben Clayton60958262019-04-10 14:53:30 -04003757 RValue<UInt4> Ctlz(RValue<UInt4> v, bool isZeroUndef)
3758 {
Ben Clayton7579db12019-05-02 08:37:12 +01003759 auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::ctlz, { T(UInt4::getType()) } );
Ben Claytonc38fc122019-04-11 08:58:49 -04003760 return RValue<UInt4>(V(::builder->CreateCall2(func, ARGS(
Ben Clayton60958262019-04-10 14:53:30 -04003761 V(v.value),
3762 isZeroUndef ? ::llvm::ConstantInt::getTrue(*::context) : ::llvm::ConstantInt::getFalse(*::context)
Ben Claytonc38fc122019-04-11 08:58:49 -04003763 ))));
Ben Clayton60958262019-04-10 14:53:30 -04003764 }
3765
Ben Clayton3f007c42019-04-10 14:54:23 -04003766 RValue<UInt4> Cttz(RValue<UInt4> v, bool isZeroUndef)
3767 {
Ben Clayton7579db12019-05-02 08:37:12 +01003768 auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::cttz, { T(UInt4::getType()) } );
Ben Claytonc38fc122019-04-11 08:58:49 -04003769 return RValue<UInt4>(V(::builder->CreateCall2(func, ARGS(
Ben Clayton3f007c42019-04-10 14:54:23 -04003770 V(v.value),
3771 isZeroUndef ? ::llvm::ConstantInt::getTrue(*::context) : ::llvm::ConstantInt::getFalse(*::context)
Ben Claytonc38fc122019-04-11 08:58:49 -04003772 ))));
Ben Clayton3f007c42019-04-10 14:54:23 -04003773 }
3774
John Bauman19bac1e2014-05-06 15:23:49 -04003775 Type *Float4::getType()
John Bauman89401822014-05-06 15:04:28 -04003776 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003777 return T(llvm::VectorType::get(T(Float::getType()), 4));
John Bauman89401822014-05-06 15:04:28 -04003778 }
3779
John Bauman89401822014-05-06 15:04:28 -04003780 RValue<Long> Ticks()
3781 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003782 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003783 llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::readcyclecounter);
John Bauman89401822014-05-06 15:04:28 -04003784
Nicolas Capens2ab69ee2016-09-26 11:45:17 -04003785 return RValue<Long>(V(::builder->CreateCall(rdtsc)));
John Bauman89401822014-05-06 15:04:28 -04003786 }
Ben Claytond853c122019-04-16 17:51:49 -04003787
3788 RValue<Pointer<Byte>> ConstantPointer(void const * ptr)
3789 {
3790 // Note: this should work for 32-bit pointers as well because 'inttoptr'
3791 // is defined to truncate (and zero extend) if necessary.
3792 auto ptrAsInt = ::llvm::ConstantInt::get(::llvm::Type::getInt64Ty(*::context), reinterpret_cast<uintptr_t>(ptr));
3793 return RValue<Pointer<Byte>>(V(::builder->CreateIntToPtr(ptrAsInt, T(Pointer<Byte>::getType()))));
3794 }
3795
3796 Value* Call(RValue<Pointer<Byte>> fptr, Type* retTy, std::initializer_list<Value*> args, std::initializer_list<Type*> argTys)
3797 {
3798 ::llvm::SmallVector<::llvm::Type*, 8> paramTys;
3799 for (auto ty : argTys) { paramTys.push_back(T(ty)); }
3800 auto funcTy = ::llvm::FunctionType::get(T(retTy), paramTys, false);
3801
3802 auto funcPtrTy = funcTy->getPointerTo();
3803 auto funcPtr = ::builder->CreatePointerCast(V(fptr.value), funcPtrTy);
3804
3805 ::llvm::SmallVector<::llvm::Value*, 8> arguments;
3806 for (auto arg : args) { arguments.push_back(V(arg)); }
3807 return V(::builder->CreateCall(funcPtr, arguments));
3808 }
Nicolas Capens9770a462019-06-25 10:47:10 -04003809
3810 void Breakpoint()
3811 {
3812 llvm::Function *debugtrap = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::debugtrap);
3813
3814 ::builder->CreateCall(debugtrap);
3815 }
John Bauman89401822014-05-06 15:04:28 -04003816}
3817
Nicolas Capens48461502018-08-06 14:20:45 -04003818namespace rr
John Bauman89401822014-05-06 15:04:28 -04003819{
Logan Chiene3191012018-08-24 22:01:50 +08003820#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003821 namespace x86
3822 {
John Bauman19bac1e2014-05-06 15:23:49 -04003823 RValue<Int> cvtss2si(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04003824 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003825 llvm::Function *cvtss2si = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_cvtss2si);
John Bauman66b8ab22014-05-06 15:57:45 -04003826
John Bauman89401822014-05-06 15:04:28 -04003827 Float4 vector;
3828 vector.x = val;
3829
Logan Chien813d5032018-08-31 17:19:45 +08003830 return RValue<Int>(V(::builder->CreateCall(cvtss2si, ARGS(V(RValue<Float4>(vector).value)))));
John Bauman89401822014-05-06 15:04:28 -04003831 }
3832
John Bauman19bac1e2014-05-06 15:23:49 -04003833 RValue<Int4> cvtps2dq(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04003834 {
Nicolas Capens9e013d42017-07-28 17:26:14 -04003835 llvm::Function *cvtps2dq = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_cvtps2dq);
John Bauman89401822014-05-06 15:04:28 -04003836
Logan Chien813d5032018-08-31 17:19:45 +08003837 return RValue<Int4>(V(::builder->CreateCall(cvtps2dq, ARGS(V(val.value)))));
John Bauman89401822014-05-06 15:04:28 -04003838 }
3839
John Bauman19bac1e2014-05-06 15:23:49 -04003840 RValue<Float> rcpss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04003841 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003842 llvm::Function *rcpss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rcp_ss);
John Bauman89401822014-05-06 15:04:28 -04003843
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003844 Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0);
John Bauman66b8ab22014-05-06 15:57:45 -04003845
Logan Chien813d5032018-08-31 17:19:45 +08003846 return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall(rcpss, ARGS(V(vector)))), Float::getType(), 0));
John Bauman89401822014-05-06 15:04:28 -04003847 }
3848
John Bauman19bac1e2014-05-06 15:23:49 -04003849 RValue<Float> sqrtss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04003850 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08003851 llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sqrt, {V(val.value)->getType()});
3852 return RValue<Float>(V(::builder->CreateCall(sqrt, ARGS(V(val.value)))));
John Bauman89401822014-05-06 15:04:28 -04003853 }
3854
John Bauman19bac1e2014-05-06 15:23:49 -04003855 RValue<Float> rsqrtss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04003856 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003857 llvm::Function *rsqrtss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rsqrt_ss);
John Bauman66b8ab22014-05-06 15:57:45 -04003858
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003859 Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0);
John Bauman89401822014-05-06 15:04:28 -04003860
Logan Chien813d5032018-08-31 17:19:45 +08003861 return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall(rsqrtss, ARGS(V(vector)))), Float::getType(), 0));
John Bauman89401822014-05-06 15:04:28 -04003862 }
3863
John Bauman19bac1e2014-05-06 15:23:49 -04003864 RValue<Float4> rcpps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04003865 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003866 llvm::Function *rcpps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rcp_ps);
John Bauman66b8ab22014-05-06 15:57:45 -04003867
Logan Chien813d5032018-08-31 17:19:45 +08003868 return RValue<Float4>(V(::builder->CreateCall(rcpps, ARGS(V(val.value)))));
John Bauman89401822014-05-06 15:04:28 -04003869 }
3870
John Bauman19bac1e2014-05-06 15:23:49 -04003871 RValue<Float4> sqrtps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04003872 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08003873 llvm::Function *sqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sqrt, {V(val.value)->getType()});
John Bauman66b8ab22014-05-06 15:57:45 -04003874
Logan Chien813d5032018-08-31 17:19:45 +08003875 return RValue<Float4>(V(::builder->CreateCall(sqrtps, ARGS(V(val.value)))));
John Bauman89401822014-05-06 15:04:28 -04003876 }
3877
John Bauman19bac1e2014-05-06 15:23:49 -04003878 RValue<Float4> rsqrtps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04003879 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003880 llvm::Function *rsqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rsqrt_ps);
John Bauman66b8ab22014-05-06 15:57:45 -04003881
Logan Chien813d5032018-08-31 17:19:45 +08003882 return RValue<Float4>(V(::builder->CreateCall(rsqrtps, ARGS(V(val.value)))));
John Bauman89401822014-05-06 15:04:28 -04003883 }
3884
John Bauman19bac1e2014-05-06 15:23:49 -04003885 RValue<Float4> maxps(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003886 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003887 llvm::Function *maxps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_max_ps);
John Bauman89401822014-05-06 15:04:28 -04003888
Logan Chien813d5032018-08-31 17:19:45 +08003889 return RValue<Float4>(V(::builder->CreateCall2(maxps, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04003890 }
3891
John Bauman19bac1e2014-05-06 15:23:49 -04003892 RValue<Float4> minps(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003893 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003894 llvm::Function *minps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_min_ps);
John Bauman89401822014-05-06 15:04:28 -04003895
Logan Chien813d5032018-08-31 17:19:45 +08003896 return RValue<Float4>(V(::builder->CreateCall2(minps, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04003897 }
3898
John Bauman19bac1e2014-05-06 15:23:49 -04003899 RValue<Float> roundss(RValue<Float> val, unsigned char imm)
John Bauman89401822014-05-06 15:04:28 -04003900 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003901 llvm::Function *roundss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_round_ss);
John Bauman89401822014-05-06 15:04:28 -04003902
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003903 Value *undef = V(llvm::UndefValue::get(T(Float4::getType())));
John Bauman89401822014-05-06 15:04:28 -04003904 Value *vector = Nucleus::createInsertElement(undef, val.value, 0);
3905
Logan Chien813d5032018-08-31 17:19:45 +08003906 return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall3(roundss, ARGS(V(undef), V(vector), V(Nucleus::createConstantInt(imm))))), Float::getType(), 0));
John Bauman89401822014-05-06 15:04:28 -04003907 }
3908
John Bauman19bac1e2014-05-06 15:23:49 -04003909 RValue<Float> floorss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04003910 {
3911 return roundss(val, 1);
3912 }
3913
John Bauman19bac1e2014-05-06 15:23:49 -04003914 RValue<Float> ceilss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04003915 {
3916 return roundss(val, 2);
3917 }
3918
John Bauman19bac1e2014-05-06 15:23:49 -04003919 RValue<Float4> roundps(RValue<Float4> val, unsigned char imm)
John Bauman89401822014-05-06 15:04:28 -04003920 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003921 llvm::Function *roundps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_round_ps);
John Bauman89401822014-05-06 15:04:28 -04003922
Logan Chien813d5032018-08-31 17:19:45 +08003923 return RValue<Float4>(V(::builder->CreateCall2(roundps, ARGS(V(val.value), V(Nucleus::createConstantInt(imm))))));
John Bauman89401822014-05-06 15:04:28 -04003924 }
3925
John Bauman19bac1e2014-05-06 15:23:49 -04003926 RValue<Float4> floorps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04003927 {
3928 return roundps(val, 1);
3929 }
3930
John Bauman19bac1e2014-05-06 15:23:49 -04003931 RValue<Float4> ceilps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04003932 {
3933 return roundps(val, 2);
3934 }
3935
Alexis Hetu0f448072016-03-18 10:56:08 -04003936 RValue<Int4> pabsd(RValue<Int4> x)
John Bauman89401822014-05-06 15:04:28 -04003937 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08003938 return RValue<Int4>(V(lowerPABS(V(x.value))));
John Bauman89401822014-05-06 15:04:28 -04003939 }
3940
John Bauman19bac1e2014-05-06 15:23:49 -04003941 RValue<Short4> paddsw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04003942 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003943 llvm::Function *paddsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_padds_w);
John Bauman89401822014-05-06 15:04:28 -04003944
Logan Chien813d5032018-08-31 17:19:45 +08003945 return As<Short4>(V(::builder->CreateCall2(paddsw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04003946 }
John Bauman66b8ab22014-05-06 15:57:45 -04003947
John Bauman19bac1e2014-05-06 15:23:49 -04003948 RValue<Short4> psubsw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04003949 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003950 llvm::Function *psubsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubs_w);
John Bauman89401822014-05-06 15:04:28 -04003951
Logan Chien813d5032018-08-31 17:19:45 +08003952 return As<Short4>(V(::builder->CreateCall2(psubsw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04003953 }
3954
John Bauman19bac1e2014-05-06 15:23:49 -04003955 RValue<UShort4> paddusw(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04003956 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003957 llvm::Function *paddusw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_paddus_w);
John Bauman89401822014-05-06 15:04:28 -04003958
Logan Chien813d5032018-08-31 17:19:45 +08003959 return As<UShort4>(V(::builder->CreateCall2(paddusw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04003960 }
John Bauman66b8ab22014-05-06 15:57:45 -04003961
John Bauman19bac1e2014-05-06 15:23:49 -04003962 RValue<UShort4> psubusw(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04003963 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003964 llvm::Function *psubusw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubus_w);
John Bauman89401822014-05-06 15:04:28 -04003965
Logan Chien813d5032018-08-31 17:19:45 +08003966 return As<UShort4>(V(::builder->CreateCall2(psubusw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04003967 }
3968
John Bauman19bac1e2014-05-06 15:23:49 -04003969 RValue<SByte8> paddsb(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04003970 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003971 llvm::Function *paddsb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_padds_b);
John Bauman89401822014-05-06 15:04:28 -04003972
Logan Chien813d5032018-08-31 17:19:45 +08003973 return As<SByte8>(V(::builder->CreateCall2(paddsb, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04003974 }
John Bauman66b8ab22014-05-06 15:57:45 -04003975
John Bauman19bac1e2014-05-06 15:23:49 -04003976 RValue<SByte8> psubsb(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04003977 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003978 llvm::Function *psubsb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubs_b);
John Bauman89401822014-05-06 15:04:28 -04003979
Logan Chien813d5032018-08-31 17:19:45 +08003980 return As<SByte8>(V(::builder->CreateCall2(psubsb, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04003981 }
John Bauman66b8ab22014-05-06 15:57:45 -04003982
John Bauman19bac1e2014-05-06 15:23:49 -04003983 RValue<Byte8> paddusb(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04003984 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003985 llvm::Function *paddusb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_paddus_b);
John Bauman89401822014-05-06 15:04:28 -04003986
Logan Chien813d5032018-08-31 17:19:45 +08003987 return As<Byte8>(V(::builder->CreateCall2(paddusb, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04003988 }
John Bauman66b8ab22014-05-06 15:57:45 -04003989
John Bauman19bac1e2014-05-06 15:23:49 -04003990 RValue<Byte8> psubusb(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04003991 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003992 llvm::Function *psubusb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubus_b);
John Bauman89401822014-05-06 15:04:28 -04003993
Logan Chien813d5032018-08-31 17:19:45 +08003994 return As<Byte8>(V(::builder->CreateCall2(psubusb, ARGS(V(x.value), V(y.value)))));
John Bauman19bac1e2014-05-06 15:23:49 -04003995 }
3996
3997 RValue<UShort4> pavgw(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04003998 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08003999 return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value))));
John Bauman89401822014-05-06 15:04:28 -04004000 }
4001
John Bauman19bac1e2014-05-06 15:23:49 -04004002 RValue<Short4> pmaxsw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004003 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004004 return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT)));
John Bauman89401822014-05-06 15:04:28 -04004005 }
4006
John Bauman19bac1e2014-05-06 15:23:49 -04004007 RValue<Short4> pminsw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004008 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004009 return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT)));
John Bauman89401822014-05-06 15:04:28 -04004010 }
4011
John Bauman19bac1e2014-05-06 15:23:49 -04004012 RValue<Short4> pcmpgtw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004013 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004014 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType()))));
John Bauman89401822014-05-06 15:04:28 -04004015 }
4016
John Bauman19bac1e2014-05-06 15:23:49 -04004017 RValue<Short4> pcmpeqw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004018 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004019 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType()))));
John Bauman89401822014-05-06 15:04:28 -04004020 }
4021
John Bauman19bac1e2014-05-06 15:23:49 -04004022 RValue<Byte8> pcmpgtb(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04004023 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004024 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType()))));
John Bauman89401822014-05-06 15:04:28 -04004025 }
4026
John Bauman19bac1e2014-05-06 15:23:49 -04004027 RValue<Byte8> pcmpeqb(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04004028 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004029 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType()))));
John Bauman89401822014-05-06 15:04:28 -04004030 }
4031
John Bauman19bac1e2014-05-06 15:23:49 -04004032 RValue<Short4> packssdw(RValue<Int2> x, RValue<Int2> y)
John Bauman89401822014-05-06 15:04:28 -04004033 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004034 llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packssdw_128);
John Bauman89401822014-05-06 15:04:28 -04004035
Logan Chien813d5032018-08-31 17:19:45 +08004036 return As<Short4>(V(::builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004037 }
4038
John Bauman19bac1e2014-05-06 15:23:49 -04004039 RValue<Short8> packssdw(RValue<Int4> x, RValue<Int4> y)
John Bauman89401822014-05-06 15:04:28 -04004040 {
Nicolas Capens9e013d42017-07-28 17:26:14 -04004041 llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packssdw_128);
John Bauman89401822014-05-06 15:04:28 -04004042
Logan Chien813d5032018-08-31 17:19:45 +08004043 return RValue<Short8>(V(::builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004044 }
4045
John Bauman19bac1e2014-05-06 15:23:49 -04004046 RValue<SByte8> packsswb(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004047 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004048 llvm::Function *packsswb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packsswb_128);
John Bauman89401822014-05-06 15:04:28 -04004049
Logan Chien813d5032018-08-31 17:19:45 +08004050 return As<SByte8>(V(::builder->CreateCall2(packsswb, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004051 }
4052
Nicolas Capens33438a62017-09-27 11:47:35 -04004053 RValue<Byte8> packuswb(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004054 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004055 llvm::Function *packuswb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packuswb_128);
John Bauman89401822014-05-06 15:04:28 -04004056
Logan Chien813d5032018-08-31 17:19:45 +08004057 return As<Byte8>(V(::builder->CreateCall2(packuswb, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004058 }
4059
Nicolas Capens3e7062b2017-01-17 14:01:33 -05004060 RValue<UShort8> packusdw(RValue<Int4> x, RValue<Int4> y)
John Bauman89401822014-05-06 15:04:28 -04004061 {
4062 if(CPUID::supportsSSE4_1())
4063 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004064 llvm::Function *packusdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_packusdw);
John Bauman66b8ab22014-05-06 15:57:45 -04004065
Logan Chien813d5032018-08-31 17:19:45 +08004066 return RValue<UShort8>(V(::builder->CreateCall2(packusdw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004067 }
4068 else
4069 {
Nicolas Capens3e7062b2017-01-17 14:01:33 -05004070 RValue<Int4> bx = (x & ~(x >> 31)) - Int4(0x8000);
4071 RValue<Int4> by = (y & ~(y >> 31)) - Int4(0x8000);
4072
4073 return As<UShort8>(packssdw(bx, by) + Short8(0x8000u));
John Bauman89401822014-05-06 15:04:28 -04004074 }
4075 }
4076
John Bauman19bac1e2014-05-06 15:23:49 -04004077 RValue<UShort4> psrlw(RValue<UShort4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004078 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004079 llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_w);
John Bauman89401822014-05-06 15:04:28 -04004080
Logan Chien813d5032018-08-31 17:19:45 +08004081 return As<UShort4>(V(::builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004082 }
4083
John Bauman19bac1e2014-05-06 15:23:49 -04004084 RValue<UShort8> psrlw(RValue<UShort8> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004085 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004086 llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_w);
John Bauman89401822014-05-06 15:04:28 -04004087
Logan Chien813d5032018-08-31 17:19:45 +08004088 return RValue<UShort8>(V(::builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004089 }
4090
John Bauman19bac1e2014-05-06 15:23:49 -04004091 RValue<Short4> psraw(RValue<Short4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004092 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004093 llvm::Function *psraw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_w);
John Bauman89401822014-05-06 15:04:28 -04004094
Logan Chien813d5032018-08-31 17:19:45 +08004095 return As<Short4>(V(::builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004096 }
4097
John Bauman19bac1e2014-05-06 15:23:49 -04004098 RValue<Short8> psraw(RValue<Short8> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004099 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004100 llvm::Function *psraw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_w);
John Bauman89401822014-05-06 15:04:28 -04004101
Logan Chien813d5032018-08-31 17:19:45 +08004102 return RValue<Short8>(V(::builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004103 }
4104
John Bauman19bac1e2014-05-06 15:23:49 -04004105 RValue<Short4> psllw(RValue<Short4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004106 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004107 llvm::Function *psllw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_w);
John Bauman89401822014-05-06 15:04:28 -04004108
Logan Chien813d5032018-08-31 17:19:45 +08004109 return As<Short4>(V(::builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004110 }
4111
John Bauman19bac1e2014-05-06 15:23:49 -04004112 RValue<Short8> psllw(RValue<Short8> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004113 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004114 llvm::Function *psllw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_w);
John Bauman89401822014-05-06 15:04:28 -04004115
Logan Chien813d5032018-08-31 17:19:45 +08004116 return RValue<Short8>(V(::builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004117 }
4118
John Bauman19bac1e2014-05-06 15:23:49 -04004119 RValue<Int2> pslld(RValue<Int2> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004120 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004121 llvm::Function *pslld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_d);
John Bauman89401822014-05-06 15:04:28 -04004122
Logan Chien813d5032018-08-31 17:19:45 +08004123 return As<Int2>(V(::builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004124 }
4125
John Bauman19bac1e2014-05-06 15:23:49 -04004126 RValue<Int4> pslld(RValue<Int4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004127 {
Nicolas Capens9e013d42017-07-28 17:26:14 -04004128 llvm::Function *pslld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_d);
John Bauman89401822014-05-06 15:04:28 -04004129
Logan Chien813d5032018-08-31 17:19:45 +08004130 return RValue<Int4>(V(::builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004131 }
4132
John Bauman19bac1e2014-05-06 15:23:49 -04004133 RValue<Int2> psrad(RValue<Int2> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004134 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004135 llvm::Function *psrad = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_d);
John Bauman89401822014-05-06 15:04:28 -04004136
Logan Chien813d5032018-08-31 17:19:45 +08004137 return As<Int2>(V(::builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004138 }
4139
John Bauman19bac1e2014-05-06 15:23:49 -04004140 RValue<Int4> psrad(RValue<Int4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004141 {
Nicolas Capens9e013d42017-07-28 17:26:14 -04004142 llvm::Function *psrad = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_d);
John Bauman89401822014-05-06 15:04:28 -04004143
Logan Chien813d5032018-08-31 17:19:45 +08004144 return RValue<Int4>(V(::builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004145 }
4146
John Bauman19bac1e2014-05-06 15:23:49 -04004147 RValue<UInt2> psrld(RValue<UInt2> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004148 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004149 llvm::Function *psrld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_d);
John Bauman89401822014-05-06 15:04:28 -04004150
Logan Chien813d5032018-08-31 17:19:45 +08004151 return As<UInt2>(V(::builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004152 }
4153
John Bauman19bac1e2014-05-06 15:23:49 -04004154 RValue<UInt4> psrld(RValue<UInt4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004155 {
Nicolas Capens9e013d42017-07-28 17:26:14 -04004156 llvm::Function *psrld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_d);
John Bauman89401822014-05-06 15:04:28 -04004157
Logan Chien813d5032018-08-31 17:19:45 +08004158 return RValue<UInt4>(V(::builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004159 }
4160
John Bauman19bac1e2014-05-06 15:23:49 -04004161 RValue<Int4> pmaxsd(RValue<Int4> x, RValue<Int4> y)
4162 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004163 return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT)));
John Bauman19bac1e2014-05-06 15:23:49 -04004164 }
4165
4166 RValue<Int4> pminsd(RValue<Int4> x, RValue<Int4> y)
4167 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004168 return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT)));
John Bauman19bac1e2014-05-06 15:23:49 -04004169 }
4170
4171 RValue<UInt4> pmaxud(RValue<UInt4> x, RValue<UInt4> y)
4172 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004173 return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_UGT)));
John Bauman19bac1e2014-05-06 15:23:49 -04004174 }
4175
4176 RValue<UInt4> pminud(RValue<UInt4> x, RValue<UInt4> y)
4177 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004178 return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_ULT)));
John Bauman19bac1e2014-05-06 15:23:49 -04004179 }
4180
4181 RValue<Short4> pmulhw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004182 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004183 llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulh_w);
John Bauman89401822014-05-06 15:04:28 -04004184
Logan Chien813d5032018-08-31 17:19:45 +08004185 return As<Short4>(V(::builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004186 }
4187
John Bauman19bac1e2014-05-06 15:23:49 -04004188 RValue<UShort4> pmulhuw(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04004189 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004190 llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulhu_w);
John Bauman89401822014-05-06 15:04:28 -04004191
Logan Chien813d5032018-08-31 17:19:45 +08004192 return As<UShort4>(V(::builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004193 }
4194
John Bauman19bac1e2014-05-06 15:23:49 -04004195 RValue<Int2> pmaddwd(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004196 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004197 llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmadd_wd);
John Bauman89401822014-05-06 15:04:28 -04004198
Logan Chien813d5032018-08-31 17:19:45 +08004199 return As<Int2>(V(::builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004200 }
4201
John Bauman19bac1e2014-05-06 15:23:49 -04004202 RValue<Short8> pmulhw(RValue<Short8> x, RValue<Short8> y)
John Bauman89401822014-05-06 15:04:28 -04004203 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004204 llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulh_w);
John Bauman89401822014-05-06 15:04:28 -04004205
Logan Chien813d5032018-08-31 17:19:45 +08004206 return RValue<Short8>(V(::builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004207 }
4208
John Bauman19bac1e2014-05-06 15:23:49 -04004209 RValue<UShort8> pmulhuw(RValue<UShort8> x, RValue<UShort8> y)
John Bauman89401822014-05-06 15:04:28 -04004210 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004211 llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulhu_w);
John Bauman89401822014-05-06 15:04:28 -04004212
Logan Chien813d5032018-08-31 17:19:45 +08004213 return RValue<UShort8>(V(::builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004214 }
4215
John Bauman19bac1e2014-05-06 15:23:49 -04004216 RValue<Int4> pmaddwd(RValue<Short8> x, RValue<Short8> y)
John Bauman89401822014-05-06 15:04:28 -04004217 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004218 llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmadd_wd);
John Bauman89401822014-05-06 15:04:28 -04004219
Logan Chien813d5032018-08-31 17:19:45 +08004220 return RValue<Int4>(V(::builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004221 }
4222
John Bauman19bac1e2014-05-06 15:23:49 -04004223 RValue<Int> movmskps(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04004224 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004225 llvm::Function *movmskps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_movmsk_ps);
John Bauman89401822014-05-06 15:04:28 -04004226
Logan Chien813d5032018-08-31 17:19:45 +08004227 return RValue<Int>(V(::builder->CreateCall(movmskps, ARGS(V(x.value)))));
John Bauman89401822014-05-06 15:04:28 -04004228 }
4229
John Bauman19bac1e2014-05-06 15:23:49 -04004230 RValue<Int> pmovmskb(RValue<Byte8> x)
John Bauman89401822014-05-06 15:04:28 -04004231 {
Nicolas Capens01a97962017-07-28 17:30:51 -04004232 llvm::Function *pmovmskb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmovmskb_128);
John Bauman89401822014-05-06 15:04:28 -04004233
Logan Chien813d5032018-08-31 17:19:45 +08004234 return RValue<Int>(V(::builder->CreateCall(pmovmskb, ARGS(V(x.value))))) & 0xFF;
John Bauman89401822014-05-06 15:04:28 -04004235 }
4236
Nicolas Capens01a97962017-07-28 17:30:51 -04004237 RValue<Int4> pmovzxbd(RValue<Byte16> x)
John Bauman89401822014-05-06 15:04:28 -04004238 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004239 return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false)));
John Bauman89401822014-05-06 15:04:28 -04004240 }
4241
Nicolas Capens01a97962017-07-28 17:30:51 -04004242 RValue<Int4> pmovsxbd(RValue<SByte16> x)
John Bauman89401822014-05-06 15:04:28 -04004243 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004244 return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true)));
John Bauman89401822014-05-06 15:04:28 -04004245 }
4246
Nicolas Capens01a97962017-07-28 17:30:51 -04004247 RValue<Int4> pmovzxwd(RValue<UShort8> x)
John Bauman89401822014-05-06 15:04:28 -04004248 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004249 return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false)));
John Bauman89401822014-05-06 15:04:28 -04004250 }
4251
Nicolas Capens01a97962017-07-28 17:30:51 -04004252 RValue<Int4> pmovsxwd(RValue<Short8> x)
John Bauman89401822014-05-06 15:04:28 -04004253 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004254 return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true)));
John Bauman89401822014-05-06 15:04:28 -04004255 }
4256 }
Logan Chiene3191012018-08-24 22:01:50 +08004257#endif // defined(__i386__) || defined(__x86_64__)
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004258
Ben Clayton60a3d6f2019-02-26 17:24:46 +00004259#ifdef ENABLE_RR_PRINT
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004260 // extractAll returns a vector containing the extracted n scalar value of
4261 // the vector vec.
4262 static std::vector<Value*> extractAll(Value* vec, int n)
4263 {
4264 std::vector<Value*> elements;
4265 elements.reserve(n);
4266 for (int i = 0; i < n; i++)
4267 {
4268 auto el = V(::builder->CreateExtractElement(V(vec), i));
4269 elements.push_back(el);
4270 }
4271 return elements;
4272 }
4273
Ben Claytonca8e3d72019-05-14 16:51:05 +01004274 // toInt returns all the integer values in vals extended to a native width
4275 // integer.
4276 static std::vector<Value*> toInt(const std::vector<Value*>& vals, bool isSigned)
4277 {
4278 auto intTy = ::llvm::Type::getIntNTy(*::context, sizeof(int) * 8); // Natural integer width.
4279 std::vector<Value*> elements;
4280 elements.reserve(vals.size());
4281 for (auto v : vals)
4282 {
4283 if (isSigned)
4284 {
4285 elements.push_back(V(::builder->CreateSExt(V(v), intTy)));
4286 }
4287 else
4288 {
4289 elements.push_back(V(::builder->CreateZExt(V(v), intTy)));
4290 }
4291 }
4292 return elements;
4293 }
4294
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004295 // toDouble returns all the float values in vals extended to doubles.
4296 static std::vector<Value*> toDouble(const std::vector<Value*>& vals)
4297 {
4298 auto doubleTy = ::llvm::Type::getDoubleTy(*::context);
4299 std::vector<Value*> elements;
4300 elements.reserve(vals.size());
4301 for (auto v : vals)
4302 {
4303 elements.push_back(V(::builder->CreateFPExt(V(v), doubleTy)));
4304 }
4305 return elements;
4306 }
4307
Ben Claytonca8e3d72019-05-14 16:51:05 +01004308 std::vector<Value*> PrintValue::Ty<Byte4>::val(const RValue<Byte4>& v) { return toInt(extractAll(v.value, 4), false); }
4309 std::vector<Value*> PrintValue::Ty<Int>::val(const RValue<Int>& v) { return toInt({v.value}, true); }
4310 std::vector<Value*> PrintValue::Ty<Int2>::val(const RValue<Int2>& v) { return toInt(extractAll(v.value, 2), true); }
4311 std::vector<Value*> PrintValue::Ty<Int4>::val(const RValue<Int4>& v) { return toInt(extractAll(v.value, 4), true); }
4312 std::vector<Value*> PrintValue::Ty<UInt>::val(const RValue<UInt>& v) { return toInt({v.value}, false); }
4313 std::vector<Value*> PrintValue::Ty<UInt2>::val(const RValue<UInt2>& v) { return toInt(extractAll(v.value, 2), false); }
4314 std::vector<Value*> PrintValue::Ty<UInt4>::val(const RValue<UInt4>& v) { return toInt(extractAll(v.value, 4), false); }
4315 std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return toInt(extractAll(v.value, 4), true); }
4316 std::vector<Value*> PrintValue::Ty<UShort4>::val(const RValue<UShort4>& v) { return toInt(extractAll(v.value, 4), false); }
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004317 std::vector<Value*> PrintValue::Ty<Float>::val(const RValue<Float>& v) { return toDouble({v.value}); }
4318 std::vector<Value*> PrintValue::Ty<Float4>::val(const RValue<Float4>& v) { return toDouble(extractAll(v.value, 4)); }
Ben Claytonbc0cbb92019-05-15 17:12:57 +01004319 std::vector<Value*> PrintValue::Ty<const char*>::val(const char* v) { return {V(::builder->CreateGlobalStringPtr(v))}; }
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004320
4321 void Printv(const char* function, const char* file, int line, const char* fmt, std::initializer_list<PrintValue> args)
4322 {
4323 // LLVM types used below.
4324 auto i32Ty = ::llvm::Type::getInt32Ty(*::context);
Ben Claytonca8e3d72019-05-14 16:51:05 +01004325 auto intTy = ::llvm::Type::getIntNTy(*::context, sizeof(int) * 8); // Natural integer width.
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004326 auto i8PtrTy = ::llvm::Type::getInt8PtrTy(*::context);
4327 auto funcTy = ::llvm::FunctionType::get(i32Ty, {i8PtrTy}, true);
4328
4329 auto func = ::module->getOrInsertFunction("printf", funcTy);
4330
4331 // Build the printf format message string.
4332 std::string str;
4333 if (file != nullptr) { str += (line > 0) ? "%s:%d " : "%s "; }
4334 if (function != nullptr) { str += "%s "; }
4335 str += fmt;
4336
4337 // Perform subsitution on all '{n}' bracketed indices in the format
4338 // message.
4339 int i = 0;
4340 for (const PrintValue& arg : args)
4341 {
4342 str = replace(str, "{" + std::to_string(i++) + "}", arg.format);
4343 }
4344
4345 ::llvm::SmallVector<::llvm::Value*, 8> vals;
4346
4347 // The format message is always the first argument.
4348 vals.push_back(::builder->CreateGlobalStringPtr(str));
4349
4350 // Add optional file, line and function info if provided.
4351 if (file != nullptr)
4352 {
4353 vals.push_back(::builder->CreateGlobalStringPtr(file));
4354 if (line > 0)
4355 {
4356 vals.push_back(::llvm::ConstantInt::get(intTy, line));
4357 }
4358 }
4359 if (function != nullptr)
4360 {
4361 vals.push_back(::builder->CreateGlobalStringPtr(function));
4362 }
4363
4364 // Add all format arguments.
4365 for (const PrintValue& arg : args)
4366 {
4367 for (auto val : arg.values)
4368 {
4369 vals.push_back(V(val));
4370 }
4371 }
4372
4373 ::builder->CreateCall(func, vals);
4374 }
4375#endif // ENABLE_RR_PRINT
4376
Ben Claytonac07ed82019-03-26 14:17:41 +00004377 void Nop()
4378 {
4379 auto voidTy = ::llvm::Type::getVoidTy(*context);
4380 auto funcTy = ::llvm::FunctionType::get(voidTy, {}, false);
4381 auto func = ::module->getOrInsertFunction("nop", funcTy);
4382 builder->CreateCall(func);
4383 }
4384
4385 void EmitDebugLocation()
4386 {
4387#ifdef ENABLE_RR_DEBUG_INFO
4388 if (debugInfo != nullptr)
4389 {
4390 debugInfo->EmitLocation();
4391 }
4392#endif // ENABLE_RR_DEBUG_INFO
4393 }
4394
4395 void EmitDebugVariable(Value* value)
4396 {
4397#ifdef ENABLE_RR_DEBUG_INFO
4398 if (debugInfo != nullptr)
4399 {
4400 debugInfo->EmitVariable(value);
4401 }
4402#endif // ENABLE_RR_DEBUG_INFO
4403 }
4404
4405 void FlushDebug()
4406 {
4407#ifdef ENABLE_RR_DEBUG_INFO
4408 if (debugInfo != nullptr)
4409 {
4410 debugInfo->Flush();
4411 }
4412#endif // ENABLE_RR_DEBUG_INFO
4413 }
4414
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004415} // namespace rr
4416
4417// ------------------------------ Coroutines ------------------------------
4418
4419namespace {
4420
4421 struct CoroutineState
4422 {
4423 llvm::Function *await = nullptr;
4424 llvm::Function *destroy = nullptr;
4425 llvm::Value *handle = nullptr;
4426 llvm::Value *id = nullptr;
4427 llvm::Value *promise = nullptr;
4428 llvm::BasicBlock *suspendBlock = nullptr;
4429 llvm::BasicBlock *endBlock = nullptr;
4430 llvm::BasicBlock *destroyBlock = nullptr;
4431 };
4432 CoroutineState coroutine;
4433
4434 // Magic values retuned by llvm.coro.suspend.
4435 // See: https://llvm.org/docs/Coroutines.html#llvm-coro-suspend-intrinsic
4436 enum SuspendAction
4437 {
4438 SuspendActionSuspend = -1,
4439 SuspendActionResume = 0,
4440 SuspendActionDestroy = 1
4441 };
4442
4443} // anonymous namespace
4444
4445namespace rr {
4446
4447void Nucleus::createCoroutine(Type *YieldType, std::vector<Type*> &Params)
4448{
4449 // Types
4450 auto voidTy = ::llvm::Type::getVoidTy(*::context);
4451 auto i1Ty = ::llvm::Type::getInt1Ty(*::context);
4452 auto i8Ty = ::llvm::Type::getInt8Ty(*::context);
4453 auto i32Ty = ::llvm::Type::getInt32Ty(*::context);
4454 auto i8PtrTy = ::llvm::Type::getInt8PtrTy(*::context);
4455 auto promiseTy = T(YieldType);
4456 auto promisePtrTy = promiseTy->getPointerTo();
4457 auto handleTy = i8PtrTy;
4458 auto boolTy = i1Ty;
4459
4460 // LLVM intrinsics
4461 auto coro_id = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::coro_id);
4462 auto coro_size = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::coro_size, {i32Ty});
4463 auto coro_begin = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::coro_begin);
4464 auto coro_resume = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_resume);
4465 auto coro_end = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_end);
4466 auto coro_free = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_free);
4467 auto coro_destroy = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_destroy);
4468 auto coro_promise = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_promise);
4469 auto coro_done = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_done);
4470 auto coro_suspend = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_suspend);
4471
4472 auto allocFrameTy = ::llvm::FunctionType::get(i8PtrTy, {i32Ty}, false);
4473 auto allocFrame = ::module->getOrInsertFunction("coroutine_alloc_frame", allocFrameTy);
4474 auto freeFrameTy = ::llvm::FunctionType::get(voidTy, {i8PtrTy}, false);
4475 auto freeFrame = ::module->getOrInsertFunction("coroutine_free_frame", freeFrameTy);
4476
4477 // Build the coroutine_await() function:
4478 //
4479 // bool coroutine_await(CoroutineHandle* handle, YieldType* out)
4480 // {
4481 // if (llvm.coro.done(handle))
4482 // {
4483 // return false;
4484 // }
4485 // else
4486 // {
4487 // *value = (T*)llvm.coro.promise(handle);
4488 // llvm.coro.resume(handle);
4489 // return true;
4490 // }
4491 // }
4492 //
Ben Claytonea38f952019-06-17 13:56:56 +01004493 ::coroutine.await = rr::createFunction("coroutine_await", boolTy, {handleTy, promisePtrTy});
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004494 {
4495 auto args = ::coroutine.await->arg_begin();
4496 auto handle = args++;
4497 auto outPtr = args++;
4498 ::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "co_await", ::coroutine.await));
4499 auto doneBlock = llvm::BasicBlock::Create(*::context, "done", ::coroutine.await);
4500 auto resumeBlock = llvm::BasicBlock::Create(*::context, "resume", ::coroutine.await);
4501
4502 auto done = ::builder->CreateCall(coro_done, {handle}, "done");
4503 ::builder->CreateCondBr(done, doneBlock, resumeBlock);
4504
4505 ::builder->SetInsertPoint(doneBlock);
4506 ::builder->CreateRet(::llvm::ConstantInt::getFalse(i1Ty));
4507
4508 ::builder->SetInsertPoint(resumeBlock);
4509 auto promiseAlignment = ::llvm::ConstantInt::get(i32Ty, 4); // TODO: Get correct alignment.
4510 auto promisePtr = ::builder->CreateCall(coro_promise, {handle, promiseAlignment, ::llvm::ConstantInt::get(i1Ty, 0)});
4511 auto promise = ::builder->CreateLoad(::builder->CreatePointerCast(promisePtr, promisePtrTy));
4512 ::builder->CreateStore(promise, outPtr);
4513 ::builder->CreateCall(coro_resume, {handle});
4514 ::builder->CreateRet(::llvm::ConstantInt::getTrue(i1Ty));
4515 }
4516
4517 // Build the coroutine_destroy() function:
4518 //
4519 // void coroutine_destroy(CoroutineHandle* handle)
4520 // {
4521 // llvm.coro.destroy(handle);
4522 // }
4523 //
Ben Claytonea38f952019-06-17 13:56:56 +01004524 ::coroutine.destroy = rr::createFunction("coroutine_destroy", voidTy, {handleTy});
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004525 {
4526 auto handle = ::coroutine.destroy->arg_begin();
4527 ::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "", ::coroutine.destroy));
4528 ::builder->CreateCall(coro_destroy, {handle});
4529 ::builder->CreateRetVoid();
4530 }
4531
4532 // Begin building the main coroutine_begin() function.
4533 //
4534 // CoroutineHandle* coroutine_begin(<Arguments>)
4535 // {
4536 // YieldType promise;
4537 // auto id = llvm.coro.id(0, &promise, nullptr, nullptr);
4538 // void* frame = coroutine_alloc_frame(llvm.coro.size.i32());
4539 // CoroutineHandle *handle = llvm.coro.begin(id, frame);
4540 //
4541 // ... <REACTOR CODE> ...
4542 //
4543 // end:
4544 // SuspendAction action = llvm.coro.suspend(none, true /* final */); // <-- RESUME POINT
4545 // switch (action)
4546 // {
4547 // case SuspendActionResume:
4548 // UNREACHABLE(); // Illegal to resume after final suspend.
4549 // case SuspendActionDestroy:
4550 // goto destroy;
4551 // default: // (SuspendActionSuspend)
4552 // goto suspend;
4553 // }
4554 //
4555 // destroy:
4556 // coroutine_free_frame(llvm.coro.free(id, handle));
4557 // goto suspend;
4558 //
4559 // suspend:
4560 // llvm.coro.end(handle, false);
4561 // return handle;
4562 // }
4563 //
Ben Claytonea38f952019-06-17 13:56:56 +01004564 ::function = rr::createFunction("coroutine_begin", handleTy, T(Params));
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004565
4566#ifdef ENABLE_RR_DEBUG_INFO
4567 ::debugInfo = std::unique_ptr<DebugInfo>(new DebugInfo(::builder, ::context, ::module, ::function));
4568#endif // ENABLE_RR_DEBUG_INFO
4569
4570 auto entryBlock = llvm::BasicBlock::Create(*::context, "coroutine", ::function);
4571 ::coroutine.suspendBlock = llvm::BasicBlock::Create(*::context, "suspend", ::function);
4572 ::coroutine.endBlock = llvm::BasicBlock::Create(*::context, "end", ::function);
4573 ::coroutine.destroyBlock = llvm::BasicBlock::Create(*::context, "destroy", ::function);
4574
4575 ::builder->SetInsertPoint(entryBlock);
4576 Variable::materializeAll();
4577 ::coroutine.promise = ::builder->CreateAlloca(T(YieldType), nullptr, "promise");
4578 ::coroutine.id = ::builder->CreateCall(coro_id, {
4579 ::llvm::ConstantInt::get(i32Ty, 0),
4580 ::builder->CreatePointerCast(::coroutine.promise, i8PtrTy),
4581 ::llvm::ConstantPointerNull::get(i8PtrTy),
4582 ::llvm::ConstantPointerNull::get(i8PtrTy),
4583 });
4584 auto size = ::builder->CreateCall(coro_size, {});
4585 auto frame = ::builder->CreateCall(allocFrame, {size});
4586 ::coroutine.handle = ::builder->CreateCall(coro_begin, {::coroutine.id, frame});
4587
4588 // Build the suspend block
4589 ::builder->SetInsertPoint(::coroutine.suspendBlock);
4590 ::builder->CreateCall(coro_end, {::coroutine.handle, ::llvm::ConstantInt::get(i1Ty, 0)});
4591 ::builder->CreateRet(::coroutine.handle);
4592
4593 // Build the end block
4594 ::builder->SetInsertPoint(::coroutine.endBlock);
4595 auto action = ::builder->CreateCall(coro_suspend, {
4596 ::llvm::ConstantTokenNone::get(*::context),
4597 ::llvm::ConstantInt::get(i1Ty, 1), // final: true
4598 });
4599 auto switch_ = ::builder->CreateSwitch(action, ::coroutine.suspendBlock, 3);
4600 // switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), trapBlock); // TODO: Trap attempting to resume after final suspend
4601 switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), ::coroutine.destroyBlock);
4602
4603 // Build the destroy block
4604 ::builder->SetInsertPoint(::coroutine.destroyBlock);
4605 auto memory = ::builder->CreateCall(coro_free, {::coroutine.id, ::coroutine.handle});
4606 ::builder->CreateCall(freeFrame, {memory});
4607 ::builder->CreateBr(::coroutine.suspendBlock);
4608
4609 // Switch back to the entry block for reactor codegen.
4610 ::builder->SetInsertPoint(entryBlock);
John Bauman89401822014-05-06 15:04:28 -04004611}
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004612
4613void Nucleus::yield(Value* val)
4614{
4615 ASSERT_MSG(::coroutine.id != nullptr, "yield() can only be called when building a Coroutine");
4616
4617 // promise = val;
4618 //
4619 // auto action = llvm.coro.suspend(none, false /* final */); // <-- RESUME POINT
4620 // switch (action)
4621 // {
4622 // case SuspendActionResume:
4623 // goto resume;
4624 // case SuspendActionDestroy:
4625 // goto destroy;
4626 // default: // (SuspendActionSuspend)
4627 // goto suspend;
4628 // }
4629 // resume:
4630 //
4631
4632 RR_DEBUG_INFO_UPDATE_LOC();
4633 Variable::materializeAll();
4634
4635 // Types
4636 auto i1Ty = ::llvm::Type::getInt1Ty(*::context);
4637 auto i8Ty = ::llvm::Type::getInt8Ty(*::context);
4638
4639 // Intrinsics
4640 auto coro_suspend = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_suspend);
4641
4642 // Create a block to resume execution.
4643 auto resumeBlock = llvm::BasicBlock::Create(*::context, "resume", ::function);
4644
4645 // Store the promise (yield value)
4646 ::builder->CreateStore(V(val), ::coroutine.promise);
4647 auto action = ::builder->CreateCall(coro_suspend, {
4648 ::llvm::ConstantTokenNone::get(*::context),
4649 ::llvm::ConstantInt::get(i1Ty, 0), // final: true
4650 });
4651 auto switch_ = ::builder->CreateSwitch(action, ::coroutine.suspendBlock, 3);
4652 switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), resumeBlock);
4653 switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), ::coroutine.destroyBlock);
4654
4655 // Continue building in the resume block.
4656 ::builder->SetInsertPoint(resumeBlock);
4657}
4658
4659Routine* Nucleus::acquireCoroutine(const char *name, bool runOptimizations)
4660{
4661 ASSERT_MSG(::coroutine.id != nullptr, "acquireCoroutine() called without a call to createCoroutine()");
4662
4663 ::builder->CreateBr(::coroutine.endBlock);
4664
4665#ifdef ENABLE_RR_DEBUG_INFO
4666 if (debugInfo != nullptr)
4667 {
4668 debugInfo->Finalize();
4669 }
4670#endif // ENABLE_RR_DEBUG_INFO
4671
4672 if(false)
4673 {
4674 std::error_code error;
4675 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error);
4676 ::module->print(file, 0);
4677 }
4678
4679 // Run manadory coroutine transforms.
4680 llvm::legacy::PassManager pm;
4681 pm.add(llvm::createCoroEarlyPass());
4682 pm.add(llvm::createCoroSplitPass());
4683 pm.add(llvm::createCoroElidePass());
4684 pm.add(llvm::createBarrierNoopPass());
4685 pm.add(llvm::createCoroCleanupPass());
4686 pm.run(*::module);
4687
4688 if(runOptimizations)
4689 {
4690 optimize();
4691 }
4692
4693 if(false)
4694 {
4695 std::error_code error;
4696 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error);
4697 ::module->print(file, 0);
4698 }
4699
4700 llvm::Function *funcs[Nucleus::CoroutineEntryCount];
4701 funcs[Nucleus::CoroutineEntryBegin] = ::function;
4702 funcs[Nucleus::CoroutineEntryAwait] = ::coroutine.await;
4703 funcs[Nucleus::CoroutineEntryDestroy] = ::coroutine.destroy;
4704 Routine *routine = ::reactorJIT->acquireRoutine(funcs, Nucleus::CoroutineEntryCount);
4705
4706 ::coroutine = CoroutineState{};
4707
4708 return routine;
4709}
4710
4711} // namespace rr