Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 3 | // 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 8 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 9 | // 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 14 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 15 | #include "Reactor.hpp" |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 16 | #include "Debug.hpp" |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 17 | #include "LLVMReactor.hpp" |
| 18 | #include "LLVMReactorDebugInfo.hpp" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 19 | |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 20 | #include "x86.hpp" |
| 21 | #include "CPUID.hpp" |
| 22 | #include "Thread.hpp" |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 23 | #include "ExecutableMemory.hpp" |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 24 | #include "MutexLock.hpp" |
| 25 | |
| 26 | #undef min |
| 27 | #undef max |
| 28 | |
Ben Clayton | 09a7f45 | 2019-04-25 15:22:43 +0100 | [diff] [blame] | 29 | #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 Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 36 | #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 Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 49 | #include "llvm/IR/Intrinsics.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 50 | #include "llvm/IR/IRBuilder.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 51 | #include "llvm/IR/LegacyPassManager.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 52 | #include "llvm/IR/LLVMContext.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 53 | #include "llvm/IR/Mangler.h" |
| 54 | #include "llvm/IR/Module.h" |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 55 | #include "llvm/IR/Verifier.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 56 | #include "llvm/Support/Error.h" |
| 57 | #include "llvm/Support/TargetSelect.h" |
| 58 | #include "llvm/Target/TargetOptions.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 59 | #include "llvm/Transforms/Coroutines.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 60 | #include "llvm/Transforms/InstCombine/InstCombine.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 61 | #include "llvm/Transforms/IPO.h" |
| 62 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 63 | #include "llvm/Transforms/Scalar.h" |
| 64 | #include "llvm/Transforms/Scalar/GVN.h" |
Ben Clayton | 20507fa | 2019-04-20 01:40:15 -0400 | [diff] [blame] | 65 | |
Ben Clayton | 09a7f45 | 2019-04-25 15:22:43 +0100 | [diff] [blame] | 66 | #if defined(__clang__) |
| 67 | #pragma clang diagnostic pop |
| 68 | #endif // defined(__clang__) |
| 69 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 70 | #include "LLVMRoutine.hpp" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 71 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 72 | #define ARGS(...) {__VA_ARGS__} |
| 73 | #define CreateCall2 CreateCall |
| 74 | #define CreateCall3 CreateCall |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 75 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 76 | #include <unordered_map> |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 77 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 78 | #include <fstream> |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 79 | #include <iostream> |
| 80 | #include <mutex> |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 81 | #include <numeric> |
| 82 | #include <thread> |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 83 | |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 84 | #if defined(__i386__) || defined(__x86_64__) |
| 85 | #include <xmmintrin.h> |
| 86 | #endif |
| 87 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 88 | #include <math.h> |
| 89 | |
Nicolas Capens | cb12258 | 2014-05-06 23:34:44 -0400 | [diff] [blame] | 90 | #if defined(__x86_64__) && defined(_WIN32) |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 91 | 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 Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 101 | #endif |
| 102 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 103 | namespace rr |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 104 | { |
| 105 | class LLVMReactorJIT; |
| 106 | } |
| 107 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 108 | namespace |
| 109 | { |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 110 | rr::LLVMReactorJIT *reactorJIT = nullptr; |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 111 | llvm::IRBuilder<> *builder = nullptr; |
| 112 | llvm::LLVMContext *context = nullptr; |
| 113 | llvm::Module *module = nullptr; |
| 114 | llvm::Function *function = nullptr; |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 115 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 116 | #ifdef ENABLE_RR_DEBUG_INFO |
| 117 | std::unique_ptr<rr::DebugInfo> debugInfo; |
| 118 | #endif |
| 119 | |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 120 | rr::MutexLock codegenMutex; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 121 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 122 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 123 | 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 Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 132 | #endif // ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 133 | |
Ben Clayton | 40a885e | 2019-06-23 19:12:48 +0100 | [diff] [blame] | 134 | 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 Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 160 | 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 Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 178 | llvm::ICmpInst::Predicate pred) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 179 | { |
| 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 Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 184 | llvm::Value *y, llvm::Type *dstTy) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 185 | { |
| 186 | return ::builder->CreateSExt(::builder->CreateICmp(pred, x, y), dstTy, ""); |
| 187 | } |
| 188 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 189 | #if defined(__i386__) || defined(__x86_64__) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 190 | 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 Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 201 | : ::builder->CreateZExt(v, dstTy); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 202 | } |
| 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 Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 212 | |
| 213 | #if !defined(__i386__) && !defined(__x86_64__) |
| 214 | llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 215 | llvm::FCmpInst::Predicate pred) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 216 | { |
| 217 | return ::builder->CreateSelect(::builder->CreateFCmp(pred, x, y), x, y); |
| 218 | } |
| 219 | |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 220 | 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 Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 227 | llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty) |
| 228 | { |
| 229 | return ::builder->CreateFPToSI(lowerRound(x), ty); |
| 230 | } |
| 231 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 232 | 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 Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 239 | 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 Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 246 | // Packed add/sub saturatation |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 247 | llvm::Value *lowerPSAT(llvm::Value *x, llvm::Value *y, bool isAdd, bool isSigned) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 248 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 249 | 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 264 | ASSERT_MSG(numBits <= 64, "numBits: %d", int(numBits)); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 265 | 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 Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | llvm::Value *lowerPUADDSAT(llvm::Value *x, llvm::Value *y) |
| 282 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 283 | return lowerPSAT(x, y, true, false); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | llvm::Value *lowerPSADDSAT(llvm::Value *x, llvm::Value *y) |
| 287 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 288 | return lowerPSAT(x, y, true, true); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | llvm::Value *lowerPUSUBSAT(llvm::Value *x, llvm::Value *y) |
| 292 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 293 | return lowerPSAT(x, y, false, false); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | llvm::Value *lowerPSSUBSAT(llvm::Value *x, llvm::Value *y) |
| 297 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 298 | return lowerPSAT(x, y, false, true); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 299 | } |
| 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 Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 381 | 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 390 | ASSERT_MSG(truncNumBits < 64, "shift 64 must be handled separately. truncNumBits: %d", int(truncNumBits)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 391 | 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 Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 451 | |
| 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 Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 475 | } |
| 476 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 477 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 478 | { |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 479 | const Capabilities Caps = |
| 480 | { |
| 481 | true, // CallSupported |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 482 | true, // CoroutinesSupported |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 483 | }; |
| 484 | |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 485 | 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 Clayton | fb28067 | 2019-04-25 11:16:15 +0100 | [diff] [blame] | 495 | UNREACHABLE("memoryOrder: %d", int(memoryOrder)); |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 496 | 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 Forbes | fd4c96d | 2019-06-20 11:20:42 -0700 | [diff] [blame] | 533 | #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 Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 549 | class ExternalFunctionSymbolResolver |
| 550 | { |
| 551 | private: |
| 552 | using FunctionMap = std::unordered_map<std::string, void *>; |
| 553 | FunctionMap func_; |
| 554 | |
| 555 | public: |
| 556 | ExternalFunctionSymbolResolver() |
| 557 | { |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 558 | 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 Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 585 | struct F |
| 586 | { |
| 587 | static void nop() {} |
| 588 | static void neverCalled() { UNREACHABLE("Should never be called"); } |
| 589 | |
Ben Clayton | 40a885e | 2019-06-23 19:12:48 +0100 | [diff] [blame] | 590 | static void* coroutine_alloc_frame(size_t size) { return alignedAlloc(size, 16); } |
| 591 | static void coroutine_free_frame(void* ptr) { alignedFree(ptr); } |
| 592 | |
Chris Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 593 | #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 Forbes | fd4c96d | 2019-06-20 11:20:42 -0700 | [diff] [blame] | 597 | 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 Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 608 | #endif |
| 609 | }; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 610 | |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 611 | func_.emplace("nop", reinterpret_cast<void*>(F::nop)); |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 612 | func_.emplace("floorf", reinterpret_cast<void*>(floorf)); |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 613 | func_.emplace("nearbyintf", reinterpret_cast<void*>(nearbyintf)); |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 614 | func_.emplace("truncf", reinterpret_cast<void*>(truncf)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 615 | func_.emplace("printf", reinterpret_cast<void*>(printf)); |
| 616 | func_.emplace("puts", reinterpret_cast<void*>(puts)); |
Chris Forbes | 1a4c712 | 2019-03-15 14:50:47 -0700 | [diff] [blame] | 617 | func_.emplace("fmodf", reinterpret_cast<void*>(fmodf)); |
Ben Clayton | b5f1a13 | 2019-06-24 11:00:53 +0100 | [diff] [blame] | 618 | |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 619 | func_.emplace("sinf", reinterpret_cast<void*>(sinf)); |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 620 | func_.emplace("cosf", reinterpret_cast<void*>(cosf)); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 621 | func_.emplace("asinf", reinterpret_cast<void*>(asinf)); |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 622 | func_.emplace("acosf", reinterpret_cast<void*>(acosf)); |
Ben Clayton | 749b4e0 | 2019-04-09 14:27:43 -0400 | [diff] [blame] | 623 | func_.emplace("atanf", reinterpret_cast<void*>(atanf)); |
Ben Clayton | d963697 | 2019-04-09 15:09:54 -0400 | [diff] [blame] | 624 | func_.emplace("sinhf", reinterpret_cast<void*>(sinhf)); |
Ben Clayton | 900ea2c | 2019-04-09 15:25:36 -0400 | [diff] [blame] | 625 | func_.emplace("coshf", reinterpret_cast<void*>(coshf)); |
Ben Clayton | 3928bd9 | 2019-04-09 15:27:41 -0400 | [diff] [blame] | 626 | func_.emplace("tanhf", reinterpret_cast<void*>(tanhf)); |
Ben Clayton | f6d77ab | 2019-04-09 15:30:04 -0400 | [diff] [blame] | 627 | func_.emplace("asinhf", reinterpret_cast<void*>(asinhf)); |
Ben Clayton | 28ebcb0 | 2019-04-09 15:33:38 -0400 | [diff] [blame] | 628 | func_.emplace("acoshf", reinterpret_cast<void*>(acoshf)); |
Ben Clayton | fa6a539 | 2019-04-09 15:35:24 -0400 | [diff] [blame] | 629 | func_.emplace("atanhf", reinterpret_cast<void*>(atanhf)); |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 630 | func_.emplace("atan2f", reinterpret_cast<void*>(atan2f)); |
Ben Clayton | bfe94f0 | 2019-04-09 15:52:12 -0400 | [diff] [blame] | 631 | func_.emplace("powf", reinterpret_cast<void*>(powf)); |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 632 | func_.emplace("expf", reinterpret_cast<void*>(expf)); |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 633 | func_.emplace("logf", reinterpret_cast<void*>(logf)); |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 634 | func_.emplace("exp2f", reinterpret_cast<void*>(exp2f)); |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 635 | func_.emplace("log2f", reinterpret_cast<void*>(log2f)); |
Ben Clayton | b5f1a13 | 2019-06-24 11:00:53 +0100 | [diff] [blame] | 636 | |
| 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 Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 655 | func_.emplace("atomic_load", reinterpret_cast<void*>(Atomic::load)); |
| 656 | func_.emplace("atomic_store", reinterpret_cast<void*>(Atomic::store)); |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 657 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 658 | // FIXME (b/119409619): use an allocator here so we can control all memory allocations |
Ben Clayton | 40a885e | 2019-06-23 19:12:48 +0100 | [diff] [blame] | 659 | 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 Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 661 | |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 662 | #ifdef __APPLE__ |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 663 | func_.emplace("sincosf_stret", reinterpret_cast<void*>(__sincosf_stret)); |
| 664 | #elif defined(__linux__) |
| 665 | func_.emplace("sincosf", reinterpret_cast<void*>(sincosf)); |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 666 | #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 Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 671 | |
| 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 Forbes | fd4c96d | 2019-06-20 11:20:42 -0700 | [diff] [blame] | 676 | 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 Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 686 | #endif |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | void *findSymbol(const std::string &name) const |
| 690 | { |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 691 | // 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 697 | // 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 Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 699 | return it->second; |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 700 | } |
| 701 | }; |
| 702 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 703 | 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 Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 710 | ExternalFunctionSymbolResolver externalSymbolResolver; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 711 | std::shared_ptr<llvm::orc::SymbolResolver> resolver; |
| 712 | std::unique_ptr<llvm::TargetMachine> targetMachine; |
| 713 | const llvm::DataLayout dataLayout; |
| 714 | ObjLayer objLayer; |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 715 | CompileLayer compileLayer; // guarded by mutex |
| 716 | std::mutex mutex; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 717 | 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 Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 725 | 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 Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 732 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 742 | #ifdef ENABLE_RR_DEBUG_INFO |
| 743 | .setOptLevel(llvm::CodeGenOpt::None) |
| 744 | #endif // ENABLE_RR_DEBUG_INFO |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 745 | .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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 756 | }, |
| 757 | ObjLayer::NotifyLoadedFtor(), |
| 758 | [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj, const llvm::RuntimeDyld::LoadedObjectInfo &L) { |
| 759 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 760 | DebugInfo::NotifyObjectEmitted(Obj, L); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 761 | #endif // ENABLE_RR_DEBUG_INFO |
| 762 | }, |
| 763 | [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj) { |
| 764 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 765 | DebugInfo::NotifyFreeingObject(Obj); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 766 | #endif // ENABLE_RR_DEBUG_INFO |
| 767 | } |
| 768 | ), |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 769 | compileLayer(objLayer, llvm::orc::SimpleCompiler(*targetMachine)), |
| 770 | emittedFunctionsNum(0) |
| 771 | { |
| 772 | } |
| 773 | |
| 774 | void startSession() |
| 775 | { |
| 776 | ::module = new llvm::Module("", *::context); |
Nicolas Capens | 900e1da | 2019-06-23 00:14:55 -0400 | [diff] [blame] | 777 | ::module->setDataLayout(dataLayout); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | void endSession() |
| 781 | { |
| 782 | ::function = nullptr; |
| 783 | ::module = nullptr; |
| 784 | } |
| 785 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 786 | LLVMRoutine *acquireRoutine(llvm::Function **funcs, size_t count) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 787 | { |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 788 | 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 Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 796 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 797 | 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 Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 803 | std::unique_ptr<llvm::Module> mod(::module); |
| 804 | ::module = nullptr; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 805 | |
| 806 | auto moduleKey = session.allocateVModule(); |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 807 | |
| 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 Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 819 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 820 | // Resolve the function addresses. |
| 821 | std::vector<void*> addresses(count); |
| 822 | for (size_t i = 0; i < count; i++) |
Nicolas Capens | adfbbcb | 2018-10-31 14:38:53 -0400 | [diff] [blame] | 823 | { |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 824 | if(auto expectAddr = symbols[i].getAddress()) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 825 | { |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 826 | addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(expectAddr.get())); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 827 | } |
Nicolas Capens | adfbbcb | 2018-10-31 14:38:53 -0400 | [diff] [blame] | 828 | } |
| 829 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 830 | return new LLVMRoutine(addresses.data(), count, releaseRoutineCallback, this, moduleKey); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | void optimize(llvm::Module *module) |
| 834 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 835 | #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 Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 842 | 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 862 | UNREACHABLE("optimization[pass]: %d, pass: %d", int(optimization[pass]), int(pass)); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
| 866 | passManager->run(*::module); |
| 867 | } |
| 868 | |
| 869 | private: |
| 870 | void releaseRoutineModule(llvm::orc::VModuleKey moduleKey) |
| 871 | { |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 872 | std::unique_lock<std::mutex> lock(mutex); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 873 | llvm::cantFail(compileLayer.removeModule(moduleKey)); |
| 874 | } |
| 875 | |
| 876 | static void releaseRoutineCallback(LLVMReactorJIT *jit, uint64_t moduleKey) |
| 877 | { |
| 878 | jit->releaseRoutineModule(moduleKey); |
| 879 | } |
| 880 | }; |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 881 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 882 | Optimization optimization[10] = {InstructionCombining, Disabled}; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 883 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 884 | // 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 Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 890 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 891 | // Emulated types: |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 892 | Type_v2i32, |
| 893 | Type_v4i16, |
| 894 | Type_v2i16, |
| 895 | Type_v8i8, |
| 896 | Type_v4i8, |
| 897 | Type_v2f32, |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 898 | EmulatedTypeCount, |
| 899 | // Returned by asInternalType() to indicate that the abstract Type* |
| 900 | // should be interpreted as LLVM type pointer: |
| 901 | Type_LLVM |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 902 | }; |
| 903 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 904 | 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 Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 910 | llvm::Type *T(Type *t) |
| 911 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 912 | // Use 128-bit vectors to implement logically shorter ones. |
| 913 | switch(asInternalType(t)) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 914 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 915 | 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 922 | default: |
| 923 | UNREACHABLE("asInternalType(t): %d", int(asInternalType(t))); |
| 924 | return nullptr; |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 925 | } |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 926 | } |
| 927 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 928 | Type *T(InternalType t) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 929 | { |
| 930 | return reinterpret_cast<Type*>(t); |
| 931 | } |
| 932 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 933 | inline std::vector<llvm::Type*> &T(std::vector<Type*> &t) |
| 934 | { |
| 935 | return reinterpret_cast<std::vector<llvm::Type*>&>(t); |
| 936 | } |
| 937 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 938 | inline llvm::BasicBlock *B(BasicBlock *t) |
| 939 | { |
| 940 | return reinterpret_cast<llvm::BasicBlock*>(t); |
| 941 | } |
| 942 | |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 943 | inline BasicBlock *B(llvm::BasicBlock *t) |
| 944 | { |
| 945 | return reinterpret_cast<BasicBlock*>(t); |
| 946 | } |
| 947 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 948 | static size_t typeSize(Type *type) |
| 949 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 950 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 951 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 952 | 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 Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 959 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 960 | llvm::Type *t = T(type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 961 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 962 | 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 969 | ASSERT_MSG(bits != 0, "bits: %d", int(bits)); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 970 | |
| 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 978 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 979 | return 0; |
| 980 | } |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 981 | } |
| 982 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 983 | static unsigned int elementCount(Type *type) |
| 984 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 985 | switch(asInternalType(type)) |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 986 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 987 | 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 994 | default: |
| 995 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 996 | return 0; |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 997 | } |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 998 | } |
| 999 | |
Ben Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 1000 | static ::llvm::Function* createFunction(const char *name, ::llvm::Type *retTy, const std::vector<::llvm::Type*> ¶ms) |
| 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 Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 1006 | return func; |
| 1007 | } |
| 1008 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1009 | Nucleus::Nucleus() |
| 1010 | { |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 1011 | ::codegenMutex.lock(); // Reactor and LLVM are currently not thread safe |
Nicolas Capens | b7ea984 | 2015-04-01 10:54:59 -0400 | [diff] [blame] | 1012 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1013 | llvm::InitializeNativeTarget(); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1014 | llvm::InitializeNativeTargetAsmPrinter(); |
| 1015 | llvm::InitializeNativeTargetAsmParser(); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1016 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1017 | if(!::context) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1018 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1019 | ::context = new llvm::LLVMContext(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1020 | } |
| 1021 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1022 | #if defined(__x86_64__) |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1023 | static const char arch[] = "x86-64"; |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1024 | #elif defined(__i386__) |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1025 | static const char arch[] = "x86"; |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1026 | #elif defined(__aarch64__) |
| 1027 | static const char arch[] = "arm64"; |
| 1028 | #elif defined(__arm__) |
| 1029 | static const char arch[] = "arm"; |
Gordana Cmiljanovic | 082dfec | 2018-10-19 11:36:15 +0200 | [diff] [blame] | 1030 | #elif defined(__mips__) |
Gordana Cmiljanovic | 20622c0 | 2018-11-05 15:00:11 +0100 | [diff] [blame] | 1031 | #if defined(__mips64) |
| 1032 | static const char arch[] = "mips64el"; |
| 1033 | #else |
| 1034 | static const char arch[] = "mipsel"; |
| 1035 | #endif |
Colin Samples | 8fd5330 | 2019-06-13 09:57:44 -0400 | [diff] [blame] | 1036 | #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 1037 | static const char arch[] = "ppc64le"; |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1038 | #else |
| 1039 | #error "unknown architecture" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1040 | #endif |
| 1041 | |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1042 | llvm::SmallVector<std::string, 8> mattrs; |
| 1043 | |
| 1044 | llvm::StringMap<bool> features; |
Colin Samples | 8fd5330 | 2019-06-13 09:57:44 -0400 | [diff] [blame] | 1045 | |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1046 | bool ok = llvm::sys::getHostCPUFeatures(features); |
Colin Samples | 8fd5330 | 2019-06-13 09:57:44 -0400 | [diff] [blame] | 1047 | |
| 1048 | #if defined(__i386__) || defined(__x86_64__) || \ |
| 1049 | (defined(__linux__) && (defined(__arm__) || defined(__aarch64__))) |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1050 | ASSERT_MSG(ok, "llvm::sys::getHostCPUFeatures returned false"); |
Colin Samples | 8fd5330 | 2019-06-13 09:57:44 -0400 | [diff] [blame] | 1051 | #else |
| 1052 | (void) ok; // getHostCPUFeatures always returns false on other platforms |
| 1053 | #endif |
| 1054 | |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1055 | for (auto &feature : features) |
| 1056 | { |
| 1057 | if (feature.second) { mattrs.push_back(feature.first()); } |
| 1058 | } |
| 1059 | |
| 1060 | #if 0 |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1061 | #if defined(__i386__) || defined(__x86_64__) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1062 | 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 Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1068 | mattrs.push_back(CPUID::supportsSSE4_1() ? "+sse4.1" : "-sse4.1"); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1069 | #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 Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1077 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1078 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1079 | llvm::TargetOptions targetOpts; |
Nicolas Capens | a764381 | 2018-09-13 14:20:06 -0400 | [diff] [blame] | 1080 | targetOpts.UnsafeFPMath = false; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1081 | // targetOpts.NoInfsFPMath = true; |
| 1082 | // targetOpts.NoNaNsFPMath = true; |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1083 | |
| 1084 | if(!::reactorJIT) |
| 1085 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1086 | ::reactorJIT = new LLVMReactorJIT(arch, mattrs, targetOpts); |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | ::reactorJIT->startSession(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1090 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1091 | if(!::builder) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1092 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1093 | ::builder = new llvm::IRBuilder<>(*::context); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | Nucleus::~Nucleus() |
| 1098 | { |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 1099 | #ifdef ENABLE_RR_DEBUG_INFO |
| 1100 | debugInfo.reset(nullptr); |
| 1101 | #endif // ENABLE_RR_DEBUG_INFO |
| 1102 | |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1103 | ::reactorJIT->endSession(); |
Nicolas Capens | b7ea984 | 2015-04-01 10:54:59 -0400 | [diff] [blame] | 1104 | |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 1105 | ::codegenMutex.unlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1106 | } |
| 1107 | |
Chris Forbes | 878d4b0 | 2019-01-21 10:48:35 -0800 | [diff] [blame] | 1108 | Routine *Nucleus::acquireRoutine(const char *name, bool runOptimizations) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1109 | { |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1110 | if(::builder->GetInsertBlock()->empty() || !::builder->GetInsertBlock()->back().isTerminator()) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1111 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1112 | llvm::Type *type = ::function->getReturnType(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1113 | |
| 1114 | if(type->isVoidTy()) |
| 1115 | { |
| 1116 | createRetVoid(); |
| 1117 | } |
| 1118 | else |
| 1119 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1120 | createRet(V(llvm::UndefValue::get(type))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1121 | } |
| 1122 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1123 | |
Ben Clayton | 97c13ad | 2019-05-02 11:59:30 +0100 | [diff] [blame] | 1124 | #ifdef ENABLE_RR_DEBUG_INFO |
| 1125 | if (debugInfo != nullptr) |
| 1126 | { |
| 1127 | debugInfo->Finalize(); |
| 1128 | } |
| 1129 | #endif // ENABLE_RR_DEBUG_INFO |
| 1130 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1131 | if(false) |
| 1132 | { |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 1133 | std::error_code error; |
| 1134 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error); |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1135 | ::module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1136 | } |
| 1137 | |
Ben Clayton | 5375f47 | 2019-06-24 13:33:11 +0100 | [diff] [blame] | 1138 | #if defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 1139 | { |
| 1140 | llvm::legacy::PassManager pm; |
| 1141 | pm.add(llvm::createVerifierPass()); |
| 1142 | pm.run(*::module); |
| 1143 | } |
Ben Clayton | 5375f47 | 2019-06-24 13:33:11 +0100 | [diff] [blame] | 1144 | #endif // defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 1145 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1146 | if(runOptimizations) |
| 1147 | { |
| 1148 | optimize(); |
| 1149 | } |
| 1150 | |
| 1151 | if(false) |
| 1152 | { |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 1153 | std::error_code error; |
| 1154 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error); |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1155 | ::module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1156 | } |
| 1157 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 1158 | LLVMRoutine *routine = ::reactorJIT->acquireRoutine(&::function, 1); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1159 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1160 | return routine; |
| 1161 | } |
| 1162 | |
| 1163 | void Nucleus::optimize() |
| 1164 | { |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1165 | ::reactorJIT->optimize(::module); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1166 | } |
| 1167 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1168 | Value *Nucleus::allocateStackVariable(Type *type, int arraySize) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1169 | { |
| 1170 | // Need to allocate it in the entry block for mem2reg to work |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 1171 | llvm::BasicBlock &entryBlock = ::function->getEntryBlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1172 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1173 | llvm::Instruction *declaration; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1174 | |
| 1175 | if(arraySize) |
| 1176 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1177 | declaration = new llvm::AllocaInst(T(type), 0, V(Nucleus::createConstantInt(arraySize))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1178 | } |
| 1179 | else |
| 1180 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1181 | declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value*)nullptr); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | entryBlock.getInstList().push_front(declaration); |
| 1185 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1186 | return V(declaration); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | BasicBlock *Nucleus::createBasicBlock() |
| 1190 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1191 | return B(llvm::BasicBlock::Create(*::context, "", ::function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | BasicBlock *Nucleus::getInsertBlock() |
| 1195 | { |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 1196 | return B(::builder->GetInsertBlock()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1200 | { |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1201 | // assert(::builder->GetInsertBlock()->back().isTerminator()); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1202 | |
| 1203 | Variable::materializeAll(); |
| 1204 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1205 | ::builder->SetInsertPoint(B(basicBlock)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1208 | void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1209 | { |
Ben Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 1210 | ::function = rr::createFunction("", T(ReturnType), T(Params)); |
Nicolas Capens | 52551d1 | 2018-09-13 14:30:56 -0400 | [diff] [blame] | 1211 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1212 | #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 Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1216 | ::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "", ::function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1217 | } |
| 1218 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1219 | Value *Nucleus::getArgument(unsigned int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1220 | { |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1221 | llvm::Function::arg_iterator args = ::function->arg_begin(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1222 | |
| 1223 | while(index) |
| 1224 | { |
| 1225 | args++; |
| 1226 | index--; |
| 1227 | } |
| 1228 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1229 | return V(&*args); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1230 | } |
| 1231 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1232 | void Nucleus::createRetVoid() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1233 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1234 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1235 | |
Ben Clayton | c958b17 | 2019-05-02 12:20:59 +0100 | [diff] [blame] | 1236 | ASSERT_MSG(::function->getReturnType() == T(Void::getType()), "Return type mismatch"); |
| 1237 | |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1238 | // 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 Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1243 | ::builder->CreateRetVoid(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1244 | } |
| 1245 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1246 | void Nucleus::createRet(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1247 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1248 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1249 | |
Ben Clayton | c958b17 | 2019-05-02 12:20:59 +0100 | [diff] [blame] | 1250 | ASSERT_MSG(::function->getReturnType() == V(v)->getType(), "Return type mismatch"); |
| 1251 | |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1252 | // 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 Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1257 | ::builder->CreateRet(V(v)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1258 | } |
| 1259 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1260 | void Nucleus::createBr(BasicBlock *dest) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1261 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1262 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1263 | Variable::materializeAll(); |
| 1264 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1265 | ::builder->CreateBr(B(dest)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1266 | } |
| 1267 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1268 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1269 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1270 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1271 | Variable::materializeAll(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1272 | ::builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1276 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1277 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1278 | return V(::builder->CreateAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1282 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1283 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1284 | return V(::builder->CreateSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1288 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1289 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1290 | return V(::builder->CreateMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1294 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1295 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1296 | return V(::builder->CreateUDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1300 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1301 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1302 | return V(::builder->CreateSDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1306 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1307 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1308 | return V(::builder->CreateFAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1312 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1313 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1314 | return V(::builder->CreateFSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1318 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1319 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1320 | return V(::builder->CreateFMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1324 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1325 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1326 | return V(::builder->CreateFDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1330 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1331 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1332 | return V(::builder->CreateURem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1336 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1337 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1338 | return V(::builder->CreateSRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1342 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1343 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1344 | return V(::builder->CreateFRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1348 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1349 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1350 | return V(::builder->CreateShl(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1354 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1355 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1356 | return V(::builder->CreateLShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1360 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1361 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1362 | return V(::builder->CreateAShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1366 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1367 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1368 | return V(::builder->CreateAnd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1372 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1373 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1374 | return V(::builder->CreateOr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1378 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1379 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1380 | return V(::builder->CreateXor(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1381 | } |
| 1382 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1383 | Value *Nucleus::createNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1384 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1385 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1386 | return V(::builder->CreateNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1387 | } |
| 1388 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1389 | Value *Nucleus::createFNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1390 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1391 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1392 | return V(::builder->CreateFNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1393 | } |
| 1394 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1395 | Value *Nucleus::createNot(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1396 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1397 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1398 | return V(::builder->CreateNot(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1399 | } |
| 1400 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1401 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1402 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1403 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1404 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1405 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1406 | 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 Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1413 | createLoad(createBitCast(ptr, Pointer<Long>::getType()), Long::getType(), isVolatile, alignment, atomic, memoryOrder), |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1414 | 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 Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1419 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1420 | Value *u = V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))); |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1421 | Value *i = createLoad(createBitCast(ptr, Pointer<Int>::getType()), Int::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1422 | i = createZExt(i, Long::getType()); |
| 1423 | Value *v = createInsertElement(u, i, 0); |
| 1424 | return createBitCast(v, type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1425 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1426 | // Fallthrough to non-emulated case. |
| 1427 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1428 | { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1429 | auto elTy = T(type); |
| 1430 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1431 | |
| 1432 | if (!atomic) |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1433 | { |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1434 | 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 Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1461 | 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 Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1478 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1479 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1480 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1481 | return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1482 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1483 | } |
| 1484 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1485 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1486 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1487 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1488 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1489 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1490 | 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 Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1498 | Long::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1499 | return value; |
| 1500 | case Type_v2i16: |
| 1501 | case Type_v4i8: |
| 1502 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1503 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1504 | createStore( |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1505 | createExtractElement(createBitCast(value, Int4::getType()), Int::getType(), 0), |
| 1506 | createBitCast(ptr, Pointer<Int>::getType()), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1507 | Int::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1508 | return value; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1509 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1510 | // Fallthrough to non-emulated case. |
| 1511 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1512 | { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1513 | auto elTy = T(type); |
| 1514 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1515 | |
| 1516 | if (!atomic) |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1517 | { |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1518 | ::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 Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1543 | 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 Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1560 | |
| 1561 | return value; |
| 1562 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1563 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1564 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1565 | return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1566 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1567 | } |
| 1568 | |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1569 | 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 Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1603 | 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 Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 1652 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 1653 | { |
| 1654 | ::builder->CreateFence(atomicOrdering(true, memoryOrder)); |
| 1655 | } |
| 1656 | |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1657 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1658 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1659 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1660 | ASSERT(V(ptr)->getType()->getContainedType(0) == T(type)); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1661 | if(sizeof(void*) == 8) |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1662 | { |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1663 | // 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 Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1679 | } |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1680 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1681 | // 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 Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1684 | { |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1685 | return V(::builder->CreateGEP(V(ptr), V(index))); |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1686 | } |
| 1687 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1688 | // For emulated types we have to multiply the index by the intended |
| 1689 | // type size ourselves to obain the byte offset. |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1690 | index = (sizeof(void*) == 8) ? |
| 1691 | createMul(index, createConstantLong((int64_t)typeSize(type))) : |
| 1692 | createMul(index, createConstantInt((int)typeSize(type))); |
| 1693 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1694 | // Cast to a byte pointer, apply the byte offset, and cast back to the |
| 1695 | // original pointer type. |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1696 | 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1699 | } |
| 1700 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1701 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1702 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1703 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1704 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Add, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1705 | } |
| 1706 | |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 1707 | 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 Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1713 | 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 Forbes | f31bdad | 2019-05-23 14:58:08 -0700 | [diff] [blame] | 1743 | 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 Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1756 | 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 Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1760 | } |
| 1761 | |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 1762 | 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 Forbes | c9ca99e | 2019-04-19 07:53:34 -0700 | [diff] [blame] | 1765 | // Note: AtomicCmpXchgInstruction returns a 2-member struct containing {result, success-flag}, not the result directly. |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 1766 | 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 Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1771 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1772 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1773 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1774 | return V(::builder->CreateTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1775 | } |
| 1776 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1777 | Value *Nucleus::createZExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1778 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1779 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1780 | return V(::builder->CreateZExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1781 | } |
| 1782 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1783 | Value *Nucleus::createSExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1784 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1785 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1786 | return V(::builder->CreateSExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1787 | } |
| 1788 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1789 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1790 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1791 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1792 | return V(::builder->CreateFPToSI(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1793 | } |
| 1794 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1795 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1796 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1797 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1798 | return V(::builder->CreateSIToFP(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1799 | } |
| 1800 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1801 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1802 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1803 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1804 | return V(::builder->CreateFPTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1805 | } |
| 1806 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1807 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1808 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1809 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1810 | return V(::builder->CreateFPExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1811 | } |
| 1812 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1813 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1814 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1815 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1816 | // 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 Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1819 | if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1820 | { |
| 1821 | Value *readAddress = allocateStackVariable(destType); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1822 | Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0))); |
| 1823 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1824 | return createLoad(readAddress, destType); |
| 1825 | } |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1826 | else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1827 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1828 | Value *writeAddress = allocateStackVariable(T(V(v)->getType())); |
| 1829 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1830 | Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0))); |
| 1831 | return createLoad(readAddress, destType); |
| 1832 | } |
| 1833 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1834 | return V(::builder->CreateBitCast(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1835 | } |
| 1836 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1837 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1838 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1839 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1840 | return V(::builder->CreateICmpEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1844 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1845 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1846 | return V(::builder->CreateICmpNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1850 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1851 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1852 | return V(::builder->CreateICmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1856 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1857 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1858 | return V(::builder->CreateICmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1862 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1863 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1864 | return V(::builder->CreateICmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1868 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1869 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1870 | return V(::builder->CreateICmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1874 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1875 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1876 | return V(::builder->CreateICmpSGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1880 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1881 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1882 | return V(::builder->CreateICmpSGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1886 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1887 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1888 | return V(::builder->CreateICmpSLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1892 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1893 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1894 | return V(::builder->CreateICmpSLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1898 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1899 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1900 | return V(::builder->CreateFCmpOEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1904 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1905 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1906 | return V(::builder->CreateFCmpOGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1910 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1911 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1912 | return V(::builder->CreateFCmpOGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1916 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1917 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1918 | return V(::builder->CreateFCmpOLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1922 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1923 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1924 | return V(::builder->CreateFCmpOLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1928 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1929 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1930 | return V(::builder->CreateFCmpONE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1934 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1935 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1936 | return V(::builder->CreateFCmpORD(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1940 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1941 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1942 | return V(::builder->CreateFCmpUNO(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1946 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1947 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1948 | return V(::builder->CreateFCmpUEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1952 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1953 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1954 | return V(::builder->CreateFCmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1958 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1959 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1960 | return V(::builder->CreateFCmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1964 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1965 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1966 | return V(::builder->CreateFCmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1970 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1971 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1972 | return V(::builder->CreateFCmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1976 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1977 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 71008d8 | 2019-03-05 17:17:59 +0000 | [diff] [blame] | 1978 | return V(::builder->CreateFCmpUNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1979 | } |
| 1980 | |
Nicolas Capens | e95d534 | 2016-09-30 11:37:28 -0400 | [diff] [blame] | 1981 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1982 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1983 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1984 | ASSERT(V(vector)->getType()->getContainedType(0) == T(type)); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1985 | return V(::builder->CreateExtractElement(V(vector), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1989 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1990 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1991 | return V(::builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1992 | } |
| 1993 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1994 | Value *Nucleus::createShuffleVector(Value *v1, Value *v2, const int *select) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1995 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1996 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1997 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1998 | int size = llvm::cast<llvm::VectorType>(V(v1)->getType())->getNumElements(); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 1999 | const int maxSize = 16; |
| 2000 | llvm::Constant *swizzle[maxSize]; |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2001 | ASSERT(size <= maxSize); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2002 | |
| 2003 | for(int i = 0; i < size; i++) |
| 2004 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2005 | swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), select[i]); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(swizzle, size)); |
| 2009 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2010 | return V(::builder->CreateShuffleVector(V(v1), V(v2), shuffle)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2011 | } |
| 2012 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2013 | Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2014 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2015 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2016 | return V(::builder->CreateSelect(V(c), V(ifTrue), V(ifFalse))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2017 | } |
| 2018 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 2019 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2020 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2021 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2022 | return reinterpret_cast<SwitchCases*>(::builder->CreateSwitch(V(control), B(defaultBranch), numCases)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2023 | } |
| 2024 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 2025 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2026 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2027 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2028 | llvm::SwitchInst *sw = reinterpret_cast<llvm::SwitchInst *>(switchCases); |
| 2029 | sw->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), label, true), B(branch)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2030 | } |
| 2031 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 2032 | void Nucleus::createUnreachable() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2033 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2034 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 2035 | ::builder->CreateUnreachable(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2036 | } |
| 2037 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2038 | Type *Nucleus::getPointerType(Type *ElementType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2039 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2040 | return T(llvm::PointerType::get(T(ElementType), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2041 | } |
| 2042 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2043 | Value *Nucleus::createNullValue(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2044 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2045 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2046 | return V(llvm::Constant::getNullValue(T(Ty))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2047 | } |
| 2048 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2049 | Value *Nucleus::createConstantLong(int64_t i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2050 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2051 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2052 | return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2053 | } |
| 2054 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2055 | Value *Nucleus::createConstantInt(int i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2056 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2057 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2058 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2059 | } |
| 2060 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2061 | Value *Nucleus::createConstantInt(unsigned int i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2062 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2063 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2064 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2065 | } |
| 2066 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2067 | Value *Nucleus::createConstantBool(bool b) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2068 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2069 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2070 | return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(*::context), b)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2071 | } |
| 2072 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2073 | Value *Nucleus::createConstantByte(signed char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2074 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2075 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2076 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2077 | } |
| 2078 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2079 | Value *Nucleus::createConstantByte(unsigned char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2080 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2081 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2082 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2083 | } |
| 2084 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2085 | Value *Nucleus::createConstantShort(short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2086 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2087 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2088 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2089 | } |
| 2090 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2091 | Value *Nucleus::createConstantShort(unsigned short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2092 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2093 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2094 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2095 | } |
| 2096 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2097 | Value *Nucleus::createConstantFloat(float x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2098 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2099 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2100 | return V(llvm::ConstantFP::get(T(Float::getType()), x)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2101 | } |
| 2102 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2103 | Value *Nucleus::createNullPointer(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2104 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2105 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2106 | return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2107 | } |
| 2108 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2109 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2110 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2111 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2112 | 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2114 | ASSERT(numElements <= 16 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2115 | llvm::Constant *constantVector[16]; |
| 2116 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2117 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2118 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2119 | constantVector[i] = llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2120 | } |
| 2121 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2122 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2126 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2127 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2128 | 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 Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2130 | ASSERT(numElements <= 8 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2131 | llvm::Constant *constantVector[8]; |
| 2132 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2133 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2134 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2135 | constantVector[i] = llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2136 | } |
| 2137 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2138 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2139 | } |
| 2140 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2141 | Type *Void::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2142 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2143 | return T(llvm::Type::getVoidTy(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2144 | } |
| 2145 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2146 | Type *Bool::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2147 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2148 | return T(llvm::Type::getInt1Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2149 | } |
| 2150 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2151 | Type *Byte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2152 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2153 | return T(llvm::Type::getInt8Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2154 | } |
| 2155 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2156 | Type *SByte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2157 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2158 | return T(llvm::Type::getInt8Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2159 | } |
| 2160 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2161 | Type *Short::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2162 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2163 | return T(llvm::Type::getInt16Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2164 | } |
| 2165 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2166 | Type *UShort::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2167 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2168 | return T(llvm::Type::getInt16Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2169 | } |
| 2170 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2171 | Type *Byte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2172 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2173 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2174 | } |
| 2175 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2176 | Type *SByte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2177 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2178 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2179 | } |
| 2180 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2181 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2182 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2183 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2184 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2185 | return x86::paddusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2186 | #else |
| 2187 | return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 2188 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2189 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2190 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2191 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2192 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2193 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2194 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2195 | return x86::psubusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2196 | #else |
| 2197 | return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 2198 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2199 | } |
| 2200 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2201 | RValue<Int> SignMask(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2202 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2203 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2204 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2205 | return x86::pmovmskb(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2206 | #else |
| 2207 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2208 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2209 | } |
| 2210 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2211 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2212 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2213 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2214 | // return x86::pcmpgtb(x, y); // FIXME: Signedness |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2215 | //#else |
| 2216 | // return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2217 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2218 | // } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2219 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2220 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2221 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2222 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2223 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2224 | return x86::pcmpeqb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2225 | #else |
| 2226 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2227 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2228 | } |
| 2229 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2230 | Type *Byte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2231 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2232 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2233 | } |
| 2234 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2235 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2236 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2237 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2238 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2239 | return x86::paddsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2240 | #else |
| 2241 | return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 2242 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2243 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2244 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2245 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2246 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2247 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2248 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2249 | return x86::psubsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2250 | #else |
| 2251 | return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 2252 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2253 | } |
| 2254 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2255 | RValue<Int> SignMask(RValue<SByte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2256 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2257 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2258 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2259 | return x86::pmovmskb(As<Byte8>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2260 | #else |
| 2261 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2262 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2263 | } |
| 2264 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2265 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2266 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2267 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2268 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2269 | return x86::pcmpgtb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2270 | #else |
| 2271 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2272 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2273 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2274 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2275 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2276 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2277 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2278 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2279 | return x86::pcmpeqb(As<Byte8>(x), As<Byte8>(y)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2280 | #else |
| 2281 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2282 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2283 | } |
| 2284 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2285 | Type *SByte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2286 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2287 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2288 | } |
| 2289 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2290 | Type *Byte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2291 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2292 | return T(llvm::VectorType::get(T(Byte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2293 | } |
| 2294 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2295 | Type *SByte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2296 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2297 | return T(llvm::VectorType::get(T(SByte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2298 | } |
| 2299 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2300 | Type *Short2::getType() |
| 2301 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2302 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2303 | } |
| 2304 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2305 | Type *UShort2::getType() |
| 2306 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2307 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2308 | } |
| 2309 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2310 | Short4::Short4(RValue<Int4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2311 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2312 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2313 | int select[8] = {0, 2, 4, 6, 0, 2, 4, 6}; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2314 | Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType()); |
| 2315 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2316 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2317 | Value *short4 = As<Short4>(Int2(As<Int4>(packed))).value; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2318 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2319 | storeValue(short4); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2320 | } |
| 2321 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2322 | // Short4::Short4(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2323 | // { |
| 2324 | // } |
| 2325 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2326 | Short4::Short4(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2327 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2328 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2329 | Int4 v4i32 = Int4(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2330 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2331 | v4i32 = As<Int4>(x86::packssdw(v4i32, v4i32)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2332 | #else |
| 2333 | Value *v = v4i32.loadValue(); |
| 2334 | v4i32 = As<Int4>(V(lowerPack(V(v), V(v), true))); |
| 2335 | #endif |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2336 | |
| 2337 | storeValue(As<Short4>(Int2(v4i32)).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2338 | } |
| 2339 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2340 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2341 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2342 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2343 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2344 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2345 | |
| 2346 | return x86::psllw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2347 | #else |
| 2348 | return As<Short4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2349 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2350 | } |
| 2351 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2352 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2353 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2354 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2355 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2356 | return x86::psraw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2357 | #else |
| 2358 | return As<Short4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2359 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2360 | } |
| 2361 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2362 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2363 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2364 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2365 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2366 | return x86::pmaxsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2367 | #else |
| 2368 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
| 2369 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2370 | } |
| 2371 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2372 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2373 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2374 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2375 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2376 | return x86::pminsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2377 | #else |
| 2378 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
| 2379 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2380 | } |
| 2381 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2382 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2383 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2384 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2385 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2386 | return x86::paddsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2387 | #else |
| 2388 | return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 2389 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2390 | } |
| 2391 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2392 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2393 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2394 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2395 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2396 | return x86::psubsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2397 | #else |
| 2398 | return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 2399 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2400 | } |
| 2401 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2402 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2403 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2404 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2405 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2406 | return x86::pmulhw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2407 | #else |
| 2408 | return As<Short4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2409 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2410 | } |
| 2411 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2412 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2413 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2414 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2415 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2416 | return x86::pmaddwd(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2417 | #else |
| 2418 | return As<Int2>(V(lowerMulAdd(V(x.value), V(y.value)))); |
| 2419 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2420 | } |
| 2421 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2422 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2423 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2424 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2425 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2426 | auto result = x86::packsswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2427 | #else |
| 2428 | auto result = V(lowerPack(V(x.value), V(y.value), true)); |
| 2429 | #endif |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2430 | return As<SByte8>(Swizzle(As<Int4>(result), 0x88)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2431 | } |
| 2432 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2433 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2434 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2435 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2436 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2437 | auto result = x86::packuswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2438 | #else |
| 2439 | auto result = V(lowerPack(V(x.value), V(y.value), false)); |
| 2440 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2441 | return As<Byte8>(Swizzle(As<Int4>(result), 0x88)); |
| 2442 | } |
| 2443 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2444 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2445 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2446 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2447 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2448 | return x86::pcmpgtw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2449 | #else |
| 2450 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); |
| 2451 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2452 | } |
| 2453 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2454 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2455 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2456 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2457 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2458 | return x86::pcmpeqw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2459 | #else |
| 2460 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); |
| 2461 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2462 | } |
| 2463 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2464 | Type *Short4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2465 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2466 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2467 | } |
| 2468 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2469 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2470 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2471 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2472 | if(saturate) |
| 2473 | { |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 2474 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2475 | if(CPUID::supportsSSE4_1()) |
| 2476 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2477 | Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2478 | *this = As<Short4>(PackUnsigned(int4, int4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2479 | } |
| 2480 | else |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 2481 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2482 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2483 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | else |
| 2487 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2488 | *this = Short4(Int4(cast)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2489 | } |
| 2490 | } |
| 2491 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2492 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2493 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2494 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2495 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2496 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2497 | |
| 2498 | return As<UShort4>(x86::psllw(As<Short4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2499 | #else |
| 2500 | return As<UShort4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2501 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2502 | } |
| 2503 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2504 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2505 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2506 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2507 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2508 | // return RValue<Short4>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 2509 | |
| 2510 | return x86::psrlw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2511 | #else |
| 2512 | return As<UShort4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2513 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2514 | } |
| 2515 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2516 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2517 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2518 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2519 | 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2520 | } |
| 2521 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2522 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2523 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2524 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2525 | 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2526 | } |
| 2527 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2528 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2529 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2530 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2531 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2532 | return x86::paddusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2533 | #else |
| 2534 | return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 2535 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2536 | } |
| 2537 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2538 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2539 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2540 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2541 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2542 | return x86::psubusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2543 | #else |
| 2544 | return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 2545 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2546 | } |
| 2547 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2548 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2549 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2550 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2551 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2552 | return x86::pmulhuw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2553 | #else |
| 2554 | return As<UShort4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2555 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2556 | } |
| 2557 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2558 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2559 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2560 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2561 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2562 | return x86::pavgw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2563 | #else |
| 2564 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
| 2565 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2566 | } |
| 2567 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2568 | Type *UShort4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2569 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2570 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2571 | } |
| 2572 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2573 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2574 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2575 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2576 | #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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2581 | } |
| 2582 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2583 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2584 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2585 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2586 | #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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2591 | } |
| 2592 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2593 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2594 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2595 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2596 | #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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2601 | } |
| 2602 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2603 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2604 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2605 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2606 | #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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2611 | } |
| 2612 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2613 | Type *Short8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2614 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2615 | return T(llvm::VectorType::get(T(Short::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2616 | } |
| 2617 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2618 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2619 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2620 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2621 | #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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2626 | } |
| 2627 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2628 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2629 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2630 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2631 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2632 | return x86::psrlw(lhs, rhs); // FIXME: Fallback required |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2633 | #else |
| 2634 | return As<UShort8>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2635 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2636 | } |
| 2637 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2638 | RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2639 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2640 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2641 | 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2660 | |
| 2661 | Value *byte16 = Nucleus::createBitCast(x.value, Byte16::getType()); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2662 | Value *shuffle = Nucleus::createShuffleVector(byte16, byte16, pshufb); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2663 | Value *short8 = Nucleus::createBitCast(shuffle, UShort8::getType()); |
| 2664 | |
| 2665 | return RValue<UShort8>(short8); |
| 2666 | } |
| 2667 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2668 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2669 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2670 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2671 | #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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2676 | } |
| 2677 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2678 | Type *UShort8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2679 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2680 | return T(llvm::VectorType::get(T(UShort::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2681 | } |
| 2682 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2683 | RValue<Int> operator++(Int &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2684 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2685 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2686 | RValue<Int> res = val; |
| 2687 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2688 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2689 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2690 | |
| 2691 | return res; |
| 2692 | } |
| 2693 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2694 | const Int &operator++(Int &val) // Pre-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2695 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2696 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2697 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2698 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2699 | |
| 2700 | return val; |
| 2701 | } |
| 2702 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2703 | RValue<Int> operator--(Int &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2704 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2705 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2706 | RValue<Int> res = val; |
| 2707 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2708 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2709 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2710 | |
| 2711 | return res; |
| 2712 | } |
| 2713 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2714 | const Int &operator--(Int &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2715 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2716 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2717 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2718 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2719 | |
| 2720 | return val; |
| 2721 | } |
| 2722 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2723 | RValue<Int> RoundInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2724 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2725 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2726 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2727 | return x86::cvtss2si(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2728 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 2729 | return RValue<Int>(V(lowerRoundInt(V(cast.value), T(Int::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2730 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2731 | } |
| 2732 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2733 | Type *Int::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2734 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2735 | return T(llvm::Type::getInt32Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2736 | } |
| 2737 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2738 | Type *Long::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2739 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2740 | return T(llvm::Type::getInt64Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2741 | } |
| 2742 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2743 | UInt::UInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2744 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2745 | RR_DEBUG_INFO_UPDATE_LOC(); |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2746 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 2747 | // Value *integer = Nucleus::createFPToUI(cast.value, UInt::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2748 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2749 | // 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2761 | } |
| 2762 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2763 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2764 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2765 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2766 | RValue<UInt> res = val; |
| 2767 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2768 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2769 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2770 | |
| 2771 | return res; |
| 2772 | } |
| 2773 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2774 | const UInt &operator++(UInt &val) // Pre-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2775 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2776 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2777 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2778 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2779 | |
| 2780 | return val; |
| 2781 | } |
| 2782 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2783 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2784 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2785 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2786 | RValue<UInt> res = val; |
| 2787 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2788 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2789 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2790 | |
| 2791 | return res; |
| 2792 | } |
| 2793 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2794 | const UInt &operator--(UInt &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2795 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2796 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2797 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2798 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2799 | |
| 2800 | return val; |
| 2801 | } |
| 2802 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2803 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2804 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2805 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2806 | // return x86::cvtss2si(val); // FIXME: Unsigned |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2807 | //#else |
| 2808 | // return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f)); |
| 2809 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2810 | // } |
| 2811 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2812 | Type *UInt::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2813 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2814 | return T(llvm::Type::getInt32Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2815 | } |
| 2816 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2817 | // Int2::Int2(RValue<Int> cast) |
| 2818 | // { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2819 | // Value *extend = Nucleus::createZExt(cast.value, Long::getType()); |
| 2820 | // Value *vector = Nucleus::createBitCast(extend, Int2::getType()); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2821 | // |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2822 | // int shuffle[2] = {0, 0}; |
| 2823 | // Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2824 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2825 | // storeValue(replicate); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2826 | // } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2827 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2828 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2829 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2830 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2831 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2832 | // return RValue<Int2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2833 | |
| 2834 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2835 | #else |
| 2836 | return As<Int2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2837 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2838 | } |
| 2839 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2840 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2841 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2842 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2843 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2844 | // return RValue<Int2>(Nucleus::createAShr(lhs.value, rhs.value)); |
| 2845 | |
| 2846 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2847 | #else |
| 2848 | return As<Int2>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2849 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2850 | } |
| 2851 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2852 | Type *Int2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2853 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2854 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2855 | } |
| 2856 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2857 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2858 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2859 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2860 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2861 | // return RValue<UInt2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2862 | |
| 2863 | return As<UInt2>(x86::pslld(As<Int2>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2864 | #else |
| 2865 | return As<UInt2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2866 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2867 | } |
| 2868 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2869 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2870 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2871 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2872 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2873 | // return RValue<UInt2>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 2874 | |
| 2875 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2876 | #else |
| 2877 | return As<UInt2>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2878 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2879 | } |
| 2880 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2881 | Type *UInt2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2882 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2883 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2884 | } |
| 2885 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2886 | Int4::Int4(RValue<Byte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2887 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2888 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2889 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2890 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2891 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2892 | *this = x86::pmovzxbd(As<Byte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2893 | } |
| 2894 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2895 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2896 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2897 | int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2898 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2899 | Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::getType()), swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2900 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2901 | int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2902 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2903 | Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::getType()), swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2904 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2905 | *this = As<Int4>(d); |
| 2906 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2907 | } |
| 2908 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2909 | Int4::Int4(RValue<SByte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2910 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2911 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2912 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2913 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2914 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2915 | *this = x86::pmovsxbd(As<SByte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2916 | } |
| 2917 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2918 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2919 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2920 | 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 Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2923 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2924 | int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2925 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
| 2926 | Value *d = Nucleus::createShuffleVector(c, c, swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2927 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2928 | *this = As<Int4>(d) >> 24; |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2929 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2930 | } |
| 2931 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2932 | Int4::Int4(RValue<Short4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2933 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2934 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2935 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2936 | if(CPUID::supportsSSE4_1()) |
| 2937 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2938 | *this = x86::pmovsxwd(As<Short8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2939 | } |
| 2940 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2941 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2942 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2943 | int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2944 | Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle); |
| 2945 | *this = As<Int4>(c) >> 16; |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2946 | } |
| 2947 | } |
| 2948 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2949 | Int4::Int4(RValue<UShort4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2950 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2951 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2952 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2953 | if(CPUID::supportsSSE4_1()) |
| 2954 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2955 | *this = x86::pmovzxwd(As<UShort8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2956 | } |
| 2957 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2958 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2959 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2960 | int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2961 | Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
| 2962 | *this = As<Int4>(c); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2966 | Int4::Int4(RValue<Int> rhs) : XYZW(this) |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 2967 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2968 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 2969 | Value *vector = loadValue(); |
| 2970 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 2971 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2972 | int swizzle[4] = {0, 0, 0, 0}; |
| 2973 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 2974 | |
| 2975 | storeValue(replicate); |
| 2976 | } |
| 2977 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2978 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2979 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2980 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2981 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2982 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2983 | #else |
| 2984 | return As<Int4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2985 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2986 | } |
| 2987 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2988 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2989 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2990 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2991 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2992 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2993 | #else |
| 2994 | return As<Int4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2995 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2996 | } |
| 2997 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2998 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 2999 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3000 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3001 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 3005 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3006 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3007 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 3011 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3012 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3013 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 3017 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3018 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3019 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 3023 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3024 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3025 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3029 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3030 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3031 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3035 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3036 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3037 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3038 | if(CPUID::supportsSSE4_1()) |
| 3039 | { |
| 3040 | return x86::pmaxsd(x, y); |
| 3041 | } |
| 3042 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3043 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3044 | { |
| 3045 | RValue<Int4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3046 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | |
| 3050 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3051 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3052 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3053 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3054 | if(CPUID::supportsSSE4_1()) |
| 3055 | { |
| 3056 | return x86::pminsd(x, y); |
| 3057 | } |
| 3058 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3059 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3060 | { |
| 3061 | RValue<Int4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3062 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | RValue<Int4> RoundInt(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3067 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3068 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3069 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3070 | return x86::cvtps2dq(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3071 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 3072 | return As<Int4>(V(lowerRoundInt(V(cast.value), T(Int4::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3073 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3074 | } |
| 3075 | |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3076 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 3077 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3078 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3079 | // 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3085 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3086 | // 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 Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3090 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3091 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3092 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3093 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3094 | return x86::packssdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3095 | #else |
| 3096 | return As<Short8>(V(lowerPack(V(x.value), V(y.value), true))); |
| 3097 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3098 | } |
| 3099 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3100 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3101 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3102 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3103 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3104 | return x86::packusdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3105 | #else |
| 3106 | return As<UShort8>(V(lowerPack(V(x.value), V(y.value), false))); |
| 3107 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3108 | } |
| 3109 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3110 | RValue<Int> SignMask(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3111 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3112 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3113 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3114 | return x86::movmskps(As<Float4>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3115 | #else |
| 3116 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 3117 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3118 | } |
| 3119 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3120 | Type *Int4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3121 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3122 | return T(llvm::VectorType::get(T(Int::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3123 | } |
| 3124 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3125 | UInt4::UInt4(RValue<Float4> cast) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3126 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3127 | RR_DEBUG_INFO_UPDATE_LOC(); |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 3128 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 3129 | // Value *xyzw = Nucleus::createFPToUI(cast.value, UInt4::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3130 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 3131 | // 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3143 | } |
| 3144 | |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3145 | 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 Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3157 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3158 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3159 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3160 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3161 | return As<UInt4>(x86::pslld(As<Int4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3162 | #else |
| 3163 | return As<UInt4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 3164 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3165 | } |
| 3166 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3167 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3168 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3169 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3170 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3171 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3172 | #else |
| 3173 | return As<UInt4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 3174 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3175 | } |
| 3176 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3177 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3178 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3179 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3180 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3184 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3185 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3186 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3191 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3192 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3196 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3197 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3198 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3203 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3204 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3208 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3209 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3210 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3215 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3216 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3217 | if(CPUID::supportsSSE4_1()) |
| 3218 | { |
| 3219 | return x86::pmaxud(x, y); |
| 3220 | } |
| 3221 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3222 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3223 | { |
| 3224 | RValue<UInt4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3225 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3230 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3231 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3232 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3233 | if(CPUID::supportsSSE4_1()) |
| 3234 | { |
| 3235 | return x86::pminud(x, y); |
| 3236 | } |
| 3237 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3238 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3239 | { |
| 3240 | RValue<UInt4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3241 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3242 | } |
| 3243 | } |
| 3244 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3245 | Type *UInt4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3246 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3247 | return T(llvm::VectorType::get(T(UInt::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3248 | } |
| 3249 | |
Alexis Hetu | 734e257 | 2018-12-20 14:00:49 -0500 | [diff] [blame] | 3250 | Type *Half::getType() |
| 3251 | { |
| 3252 | return T(llvm::Type::getInt16Ty(*::context)); |
| 3253 | } |
| 3254 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 3255 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3256 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3257 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3258 | #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 Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 3265 | return x86::rcpss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3266 | #else |
| 3267 | return As<Float>(V(lowerRCP(V(x.value)))); |
| 3268 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3269 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3270 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3271 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3272 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3273 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3274 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3275 | return x86::rsqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3276 | #else |
| 3277 | return As<Float>(V(lowerRSQRT(V(x.value)))); |
| 3278 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3279 | } |
| 3280 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3281 | RValue<Float> Sqrt(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3282 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3283 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3284 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3285 | return x86::sqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3286 | #else |
| 3287 | return As<Float>(V(lowerSQRT(V(x.value)))); |
| 3288 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3289 | } |
| 3290 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3291 | RValue<Float> Round(RValue<Float> x) |
| 3292 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3293 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3294 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3295 | if(CPUID::supportsSSE4_1()) |
| 3296 | { |
| 3297 | return x86::roundss(x, 0); |
| 3298 | } |
| 3299 | else |
| 3300 | { |
| 3301 | return Float4(Round(Float4(x))).x; |
| 3302 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 3303 | #else |
| 3304 | return RValue<Float>(V(lowerRound(V(x.value)))); |
| 3305 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | RValue<Float> Trunc(RValue<Float> x) |
| 3309 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3310 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3311 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3312 | 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 Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 3320 | #else |
| 3321 | return RValue<Float>(V(lowerTrunc(V(x.value)))); |
| 3322 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | RValue<Float> Frac(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3326 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3327 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3328 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3329 | if(CPUID::supportsSSE4_1()) |
| 3330 | { |
| 3331 | return x - x86::floorss(x); |
| 3332 | } |
| 3333 | else |
| 3334 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3335 | return Float4(Frac(Float4(x))).x; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3336 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 3337 | #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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3342 | } |
| 3343 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3344 | RValue<Float> Floor(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3345 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3346 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3347 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3348 | if(CPUID::supportsSSE4_1()) |
| 3349 | { |
| 3350 | return x86::floorss(x); |
| 3351 | } |
| 3352 | else |
| 3353 | { |
| 3354 | return Float4(Floor(Float4(x))).x; |
| 3355 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3356 | #else |
| 3357 | return RValue<Float>(V(lowerFloor(V(x.value)))); |
| 3358 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3359 | } |
| 3360 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3361 | RValue<Float> Ceil(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3362 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3363 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3364 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3365 | if(CPUID::supportsSSE4_1()) |
| 3366 | { |
| 3367 | return x86::ceilss(x); |
| 3368 | } |
| 3369 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3370 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3371 | { |
| 3372 | return Float4(Ceil(Float4(x))).x; |
| 3373 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3374 | } |
| 3375 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3376 | Type *Float::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3377 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 3378 | return T(llvm::Type::getFloatTy(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3379 | } |
| 3380 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3381 | Type *Float2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3382 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3383 | return T(Type_v2f32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3384 | } |
| 3385 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3386 | Float4::Float4(RValue<Float> rhs) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3387 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3388 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3389 | Value *vector = loadValue(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3390 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 3391 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3392 | int swizzle[4] = {0, 0, 0, 0}; |
| 3393 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3394 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3395 | storeValue(replicate); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3396 | } |
| 3397 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3398 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3399 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3400 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3401 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3402 | return x86::maxps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3403 | #else |
| 3404 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OGT))); |
| 3405 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3406 | } |
| 3407 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3408 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3409 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3410 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3411 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3412 | return x86::minps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3413 | #else |
| 3414 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OLT))); |
| 3415 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3416 | } |
| 3417 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 3418 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3419 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3420 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3421 | #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 Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 3428 | return x86::rcpps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3429 | #else |
| 3430 | return As<Float4>(V(lowerRCP(V(x.value)))); |
| 3431 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3432 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3433 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3434 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3435 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3436 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3437 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3438 | return x86::rsqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3439 | #else |
| 3440 | return As<Float4>(V(lowerRSQRT(V(x.value)))); |
| 3441 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3442 | } |
| 3443 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3444 | RValue<Float4> Sqrt(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3445 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3446 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3447 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3448 | return x86::sqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3449 | #else |
| 3450 | return As<Float4>(V(lowerSQRT(V(x.value)))); |
| 3451 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3452 | } |
| 3453 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3454 | RValue<Int> SignMask(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3455 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3456 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3457 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3458 | return x86::movmskps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3459 | #else |
| 3460 | return As<Int>(V(lowerFPSignMask(V(x.value), T(Int::getType())))); |
| 3461 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3462 | } |
| 3463 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3464 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3465 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3466 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3467 | // return As<Int4>(x86::cmpeqps(x, y)); |
| 3468 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value, y.value), Int4::getType())); |
| 3469 | } |
| 3470 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3471 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3472 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3473 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3474 | // return As<Int4>(x86::cmpltps(x, y)); |
| 3475 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value, y.value), Int4::getType())); |
| 3476 | } |
| 3477 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3478 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3479 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3480 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3481 | // return As<Int4>(x86::cmpleps(x, y)); |
| 3482 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value, y.value), Int4::getType())); |
| 3483 | } |
| 3484 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3485 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3486 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3487 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3488 | // return As<Int4>(x86::cmpneqps(x, y)); |
| 3489 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value, y.value), Int4::getType())); |
| 3490 | } |
| 3491 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3492 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3493 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3494 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3495 | // return As<Int4>(x86::cmpnltps(x, y)); |
| 3496 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value, y.value), Int4::getType())); |
| 3497 | } |
| 3498 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3499 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3500 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3501 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3502 | // return As<Int4>(x86::cmpnleps(x, y)); |
| 3503 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value, y.value), Int4::getType())); |
| 3504 | } |
| 3505 | |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3506 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 3507 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3508 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3509 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3514 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3515 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3520 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3521 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3526 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3527 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3532 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3533 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3538 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3539 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value, y.value), Int4::getType())); |
| 3540 | } |
| 3541 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3542 | RValue<Float4> Round(RValue<Float4> x) |
| 3543 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3544 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3545 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3546 | if(CPUID::supportsSSE4_1()) |
| 3547 | { |
| 3548 | return x86::roundps(x, 0); |
| 3549 | } |
| 3550 | else |
| 3551 | { |
| 3552 | return Float4(RoundInt(x)); |
| 3553 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 3554 | #else |
| 3555 | return RValue<Float4>(V(lowerRound(V(x.value)))); |
| 3556 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | RValue<Float4> Trunc(RValue<Float4> x) |
| 3560 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3561 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3562 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3563 | if(CPUID::supportsSSE4_1()) |
| 3564 | { |
| 3565 | return x86::roundps(x, 3); |
| 3566 | } |
| 3567 | else |
| 3568 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3569 | return Float4(Int4(x)); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3570 | } |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 3571 | #else |
| 3572 | return RValue<Float4>(V(lowerTrunc(V(x.value)))); |
| 3573 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | RValue<Float4> Frac(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3577 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3578 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3579 | Float4 frc; |
| 3580 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3581 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3582 | if(CPUID::supportsSSE4_1()) |
| 3583 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3584 | frc = x - Floor(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3585 | } |
| 3586 | else |
| 3587 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3588 | frc = x - Float4(Int4(x)); // Signed fractional part. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3589 | |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3590 | frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1.0f))); // Add 1.0 if negative. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3591 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 3592 | #else |
| 3593 | frc = x - Floor(x); |
| 3594 | #endif |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3595 | |
| 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3599 | } |
| 3600 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3601 | RValue<Float4> Floor(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3602 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3603 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3604 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3605 | if(CPUID::supportsSSE4_1()) |
| 3606 | { |
| 3607 | return x86::floorps(x); |
| 3608 | } |
| 3609 | else |
| 3610 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3611 | return x - Frac(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3612 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3613 | #else |
| 3614 | return RValue<Float4>(V(lowerFloor(V(x.value)))); |
| 3615 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3616 | } |
| 3617 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3618 | RValue<Float4> Ceil(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3619 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3620 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3621 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3622 | if(CPUID::supportsSSE4_1()) |
| 3623 | { |
| 3624 | return x86::ceilps(x); |
| 3625 | } |
| 3626 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3627 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3628 | { |
| 3629 | return -Floor(-x); |
| 3630 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3631 | } |
| 3632 | |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 3633 | 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 Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 3639 | 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 Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 3645 | RValue<Float4> Tan(RValue<Float4> v) |
| 3646 | { |
| 3647 | return Sin(v) / Cos(v); |
| 3648 | } |
| 3649 | |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 3650 | static RValue<Float4> TransformFloat4PerElement(RValue<Float4> v, const char* name) |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3651 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3652 | auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), ::llvm::ArrayRef<llvm::Type*>(T(Float::getType())), false); |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 3653 | auto func = ::module->getOrInsertFunction(name, funcTy); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3654 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
| 3655 | for (uint64_t i = 0; i < 4; i++) |
| 3656 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3657 | auto el = ::builder->CreateCall(func, V(Nucleus::createExtractElement(v.value, Float::getType(), i))); |
| 3658 | out = V(Nucleus::createInsertElement(V(out), V(el), i)); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3659 | } |
| 3660 | return RValue<Float4>(V(out)); |
| 3661 | } |
| 3662 | |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 3663 | 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 Clayton | 749b4e0 | 2019-04-09 14:27:43 -0400 | [diff] [blame] | 3673 | RValue<Float4> Atan(RValue<Float4> v) |
| 3674 | { |
| 3675 | return TransformFloat4PerElement(v, "atanf"); |
| 3676 | } |
| 3677 | |
Ben Clayton | d963697 | 2019-04-09 15:09:54 -0400 | [diff] [blame] | 3678 | RValue<Float4> Sinh(RValue<Float4> v) |
| 3679 | { |
| 3680 | return TransformFloat4PerElement(v, "sinhf"); |
| 3681 | } |
| 3682 | |
Ben Clayton | 900ea2c | 2019-04-09 15:25:36 -0400 | [diff] [blame] | 3683 | RValue<Float4> Cosh(RValue<Float4> v) |
| 3684 | { |
| 3685 | return TransformFloat4PerElement(v, "coshf"); |
| 3686 | } |
| 3687 | |
Ben Clayton | 3928bd9 | 2019-04-09 15:27:41 -0400 | [diff] [blame] | 3688 | RValue<Float4> Tanh(RValue<Float4> v) |
| 3689 | { |
| 3690 | return TransformFloat4PerElement(v, "tanhf"); |
| 3691 | } |
| 3692 | |
Ben Clayton | f6d77ab | 2019-04-09 15:30:04 -0400 | [diff] [blame] | 3693 | RValue<Float4> Asinh(RValue<Float4> v) |
| 3694 | { |
| 3695 | return TransformFloat4PerElement(v, "asinhf"); |
| 3696 | } |
| 3697 | |
Ben Clayton | 28ebcb0 | 2019-04-09 15:33:38 -0400 | [diff] [blame] | 3698 | RValue<Float4> Acosh(RValue<Float4> v) |
| 3699 | { |
| 3700 | return TransformFloat4PerElement(v, "acoshf"); |
| 3701 | } |
| 3702 | |
Ben Clayton | fa6a539 | 2019-04-09 15:35:24 -0400 | [diff] [blame] | 3703 | RValue<Float4> Atanh(RValue<Float4> v) |
| 3704 | { |
| 3705 | return TransformFloat4PerElement(v, "atanhf"); |
| 3706 | } |
| 3707 | |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3708 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 3709 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3710 | ::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 Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3714 | 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 Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3718 | 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 Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3723 | } |
| 3724 | return RValue<Float4>(V(out)); |
| 3725 | } |
| 3726 | |
Ben Clayton | bfe94f0 | 2019-04-09 15:52:12 -0400 | [diff] [blame] | 3727 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 3728 | { |
Ben Clayton | 7579db1 | 2019-05-02 08:37:12 +0100 | [diff] [blame] | 3729 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::pow, { T(Float4::getType()) }); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3730 | return RValue<Float4>(V(::builder->CreateCall2(func, ARGS(V(x.value), V(y.value))))); |
Ben Clayton | bfe94f0 | 2019-04-09 15:52:12 -0400 | [diff] [blame] | 3731 | } |
| 3732 | |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 3733 | RValue<Float4> Exp(RValue<Float4> v) |
| 3734 | { |
| 3735 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::exp, { T(Float4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3736 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 3737 | } |
| 3738 | |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 3739 | RValue<Float4> Log(RValue<Float4> v) |
| 3740 | { |
| 3741 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::log, { T(Float4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3742 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 3743 | } |
| 3744 | |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 3745 | RValue<Float4> Exp2(RValue<Float4> v) |
| 3746 | { |
| 3747 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::exp2, { T(Float4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3748 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 3749 | } |
| 3750 | |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 3751 | RValue<Float4> Log2(RValue<Float4> v) |
| 3752 | { |
| 3753 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::log2, { T(Float4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3754 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 3755 | } |
| 3756 | |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3757 | RValue<UInt4> Ctlz(RValue<UInt4> v, bool isZeroUndef) |
| 3758 | { |
Ben Clayton | 7579db1 | 2019-05-02 08:37:12 +0100 | [diff] [blame] | 3759 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::ctlz, { T(UInt4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3760 | return RValue<UInt4>(V(::builder->CreateCall2(func, ARGS( |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3761 | V(v.value), |
| 3762 | isZeroUndef ? ::llvm::ConstantInt::getTrue(*::context) : ::llvm::ConstantInt::getFalse(*::context) |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3763 | )))); |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3764 | } |
| 3765 | |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3766 | RValue<UInt4> Cttz(RValue<UInt4> v, bool isZeroUndef) |
| 3767 | { |
Ben Clayton | 7579db1 | 2019-05-02 08:37:12 +0100 | [diff] [blame] | 3768 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::cttz, { T(UInt4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3769 | return RValue<UInt4>(V(::builder->CreateCall2(func, ARGS( |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3770 | V(v.value), |
| 3771 | isZeroUndef ? ::llvm::ConstantInt::getTrue(*::context) : ::llvm::ConstantInt::getFalse(*::context) |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3772 | )))); |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3773 | } |
| 3774 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3775 | Type *Float4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3776 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3777 | return T(llvm::VectorType::get(T(Float::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3778 | } |
| 3779 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3780 | RValue<Long> Ticks() |
| 3781 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3782 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3783 | llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::readcyclecounter); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3784 | |
Nicolas Capens | 2ab69ee | 2016-09-26 11:45:17 -0400 | [diff] [blame] | 3785 | return RValue<Long>(V(::builder->CreateCall(rdtsc))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3786 | } |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 3787 | |
| 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 Capens | 9770a46 | 2019-06-25 10:47:10 -0400 | [diff] [blame] | 3809 | |
| 3810 | void Breakpoint() |
| 3811 | { |
| 3812 | llvm::Function *debugtrap = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::debugtrap); |
| 3813 | |
| 3814 | ::builder->CreateCall(debugtrap); |
| 3815 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3816 | } |
| 3817 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 3818 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3819 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3820 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3821 | namespace x86 |
| 3822 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3823 | RValue<Int> cvtss2si(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3824 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3825 | llvm::Function *cvtss2si = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_cvtss2si); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3826 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3827 | Float4 vector; |
| 3828 | vector.x = val; |
| 3829 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3830 | return RValue<Int>(V(::builder->CreateCall(cvtss2si, ARGS(V(RValue<Float4>(vector).value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3831 | } |
| 3832 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3833 | RValue<Int4> cvtps2dq(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3834 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3835 | llvm::Function *cvtps2dq = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_cvtps2dq); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3836 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3837 | return RValue<Int4>(V(::builder->CreateCall(cvtps2dq, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3838 | } |
| 3839 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3840 | RValue<Float> rcpss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3841 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3842 | llvm::Function *rcpss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rcp_ss); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3843 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3844 | Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3845 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3846 | return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall(rcpss, ARGS(V(vector)))), Float::getType(), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3847 | } |
| 3848 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3849 | RValue<Float> sqrtss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3850 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3851 | 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3853 | } |
| 3854 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3855 | RValue<Float> rsqrtss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3856 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3857 | llvm::Function *rsqrtss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rsqrt_ss); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3858 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3859 | Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3860 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3861 | return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall(rsqrtss, ARGS(V(vector)))), Float::getType(), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3862 | } |
| 3863 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3864 | RValue<Float4> rcpps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3865 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3866 | llvm::Function *rcpps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rcp_ps); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3867 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3868 | return RValue<Float4>(V(::builder->CreateCall(rcpps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3869 | } |
| 3870 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3871 | RValue<Float4> sqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3872 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3873 | llvm::Function *sqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sqrt, {V(val.value)->getType()}); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3874 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3875 | return RValue<Float4>(V(::builder->CreateCall(sqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3876 | } |
| 3877 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3878 | RValue<Float4> rsqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3879 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3880 | llvm::Function *rsqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rsqrt_ps); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3881 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3882 | return RValue<Float4>(V(::builder->CreateCall(rsqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3883 | } |
| 3884 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3885 | RValue<Float4> maxps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3886 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3887 | llvm::Function *maxps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_max_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3888 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3889 | return RValue<Float4>(V(::builder->CreateCall2(maxps, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3890 | } |
| 3891 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3892 | RValue<Float4> minps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3893 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3894 | llvm::Function *minps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_min_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3895 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3896 | return RValue<Float4>(V(::builder->CreateCall2(minps, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3897 | } |
| 3898 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3899 | RValue<Float> roundss(RValue<Float> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3900 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3901 | llvm::Function *roundss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_round_ss); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3902 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3903 | Value *undef = V(llvm::UndefValue::get(T(Float4::getType()))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3904 | Value *vector = Nucleus::createInsertElement(undef, val.value, 0); |
| 3905 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3906 | return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall3(roundss, ARGS(V(undef), V(vector), V(Nucleus::createConstantInt(imm))))), Float::getType(), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3907 | } |
| 3908 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3909 | RValue<Float> floorss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3910 | { |
| 3911 | return roundss(val, 1); |
| 3912 | } |
| 3913 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3914 | RValue<Float> ceilss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3915 | { |
| 3916 | return roundss(val, 2); |
| 3917 | } |
| 3918 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3919 | RValue<Float4> roundps(RValue<Float4> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3920 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3921 | llvm::Function *roundps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_round_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3922 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3923 | return RValue<Float4>(V(::builder->CreateCall2(roundps, ARGS(V(val.value), V(Nucleus::createConstantInt(imm)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3924 | } |
| 3925 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3926 | RValue<Float4> floorps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3927 | { |
| 3928 | return roundps(val, 1); |
| 3929 | } |
| 3930 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3931 | RValue<Float4> ceilps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3932 | { |
| 3933 | return roundps(val, 2); |
| 3934 | } |
| 3935 | |
Alexis Hetu | 0f44807 | 2016-03-18 10:56:08 -0400 | [diff] [blame] | 3936 | RValue<Int4> pabsd(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3937 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3938 | return RValue<Int4>(V(lowerPABS(V(x.value)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3939 | } |
| 3940 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3941 | RValue<Short4> paddsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3942 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3943 | llvm::Function *paddsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_padds_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3944 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3945 | return As<Short4>(V(::builder->CreateCall2(paddsw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3946 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3947 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3948 | RValue<Short4> psubsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3949 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3950 | llvm::Function *psubsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubs_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3951 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3952 | return As<Short4>(V(::builder->CreateCall2(psubsw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3953 | } |
| 3954 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3955 | RValue<UShort4> paddusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3956 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3957 | llvm::Function *paddusw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_paddus_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3958 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3959 | return As<UShort4>(V(::builder->CreateCall2(paddusw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3960 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3961 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3962 | RValue<UShort4> psubusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3963 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3964 | llvm::Function *psubusw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubus_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3965 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3966 | return As<UShort4>(V(::builder->CreateCall2(psubusw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3967 | } |
| 3968 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3969 | RValue<SByte8> paddsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3970 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3971 | llvm::Function *paddsb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_padds_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3972 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3973 | return As<SByte8>(V(::builder->CreateCall2(paddsb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3974 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3975 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3976 | RValue<SByte8> psubsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3977 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3978 | llvm::Function *psubsb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubs_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3979 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3980 | return As<SByte8>(V(::builder->CreateCall2(psubsb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3981 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3982 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3983 | RValue<Byte8> paddusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3984 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3985 | llvm::Function *paddusb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_paddus_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3986 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3987 | return As<Byte8>(V(::builder->CreateCall2(paddusb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3988 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3989 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3990 | RValue<Byte8> psubusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3991 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3992 | llvm::Function *psubusb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubus_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3993 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3994 | return As<Byte8>(V(::builder->CreateCall2(psubusb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | RValue<UShort4> pavgw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3998 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3999 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4000 | } |
| 4001 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4002 | RValue<Short4> pmaxsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4003 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4004 | return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4005 | } |
| 4006 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4007 | RValue<Short4> pminsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4008 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4009 | return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4010 | } |
| 4011 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4012 | RValue<Short4> pcmpgtw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4013 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4014 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4015 | } |
| 4016 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4017 | RValue<Short4> pcmpeqw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4018 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4019 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4020 | } |
| 4021 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4022 | RValue<Byte8> pcmpgtb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4023 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4024 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4025 | } |
| 4026 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4027 | RValue<Byte8> pcmpeqb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4028 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4029 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4030 | } |
| 4031 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4032 | RValue<Short4> packssdw(RValue<Int2> x, RValue<Int2> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4033 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4034 | llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packssdw_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4035 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4036 | return As<Short4>(V(::builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4037 | } |
| 4038 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4039 | RValue<Short8> packssdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4040 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 4041 | llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packssdw_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4042 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4043 | return RValue<Short8>(V(::builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4044 | } |
| 4045 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4046 | RValue<SByte8> packsswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4047 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4048 | llvm::Function *packsswb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packsswb_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4049 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4050 | return As<SByte8>(V(::builder->CreateCall2(packsswb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4051 | } |
| 4052 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 4053 | RValue<Byte8> packuswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4054 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4055 | llvm::Function *packuswb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packuswb_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4056 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4057 | return As<Byte8>(V(::builder->CreateCall2(packuswb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4058 | } |
| 4059 | |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 4060 | RValue<UShort8> packusdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4061 | { |
| 4062 | if(CPUID::supportsSSE4_1()) |
| 4063 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4064 | llvm::Function *packusdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_packusdw); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4065 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4066 | return RValue<UShort8>(V(::builder->CreateCall2(packusdw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4067 | } |
| 4068 | else |
| 4069 | { |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 4070 | 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4074 | } |
| 4075 | } |
| 4076 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4077 | RValue<UShort4> psrlw(RValue<UShort4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4078 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4079 | llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4080 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4081 | return As<UShort4>(V(::builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4082 | } |
| 4083 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4084 | RValue<UShort8> psrlw(RValue<UShort8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4085 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4086 | llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4087 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4088 | return RValue<UShort8>(V(::builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4089 | } |
| 4090 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4091 | RValue<Short4> psraw(RValue<Short4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4092 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4093 | llvm::Function *psraw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4094 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4095 | return As<Short4>(V(::builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4096 | } |
| 4097 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4098 | RValue<Short8> psraw(RValue<Short8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4099 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4100 | llvm::Function *psraw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4101 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4102 | return RValue<Short8>(V(::builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4103 | } |
| 4104 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4105 | RValue<Short4> psllw(RValue<Short4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4106 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4107 | llvm::Function *psllw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4108 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4109 | return As<Short4>(V(::builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4110 | } |
| 4111 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4112 | RValue<Short8> psllw(RValue<Short8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4113 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4114 | llvm::Function *psllw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4115 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4116 | return RValue<Short8>(V(::builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4117 | } |
| 4118 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4119 | RValue<Int2> pslld(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4120 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4121 | llvm::Function *pslld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4122 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4123 | return As<Int2>(V(::builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4124 | } |
| 4125 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4126 | RValue<Int4> pslld(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4127 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 4128 | llvm::Function *pslld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4129 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4130 | return RValue<Int4>(V(::builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4131 | } |
| 4132 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4133 | RValue<Int2> psrad(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4134 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4135 | llvm::Function *psrad = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4136 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4137 | return As<Int2>(V(::builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4138 | } |
| 4139 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4140 | RValue<Int4> psrad(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4141 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 4142 | llvm::Function *psrad = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4143 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4144 | return RValue<Int4>(V(::builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4145 | } |
| 4146 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4147 | RValue<UInt2> psrld(RValue<UInt2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4148 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4149 | llvm::Function *psrld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4150 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4151 | return As<UInt2>(V(::builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4152 | } |
| 4153 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4154 | RValue<UInt4> psrld(RValue<UInt4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4155 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 4156 | llvm::Function *psrld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4157 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4158 | return RValue<UInt4>(V(::builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4159 | } |
| 4160 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4161 | RValue<Int4> pmaxsd(RValue<Int4> x, RValue<Int4> y) |
| 4162 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4163 | return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | RValue<Int4> pminsd(RValue<Int4> x, RValue<Int4> y) |
| 4167 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4168 | return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4169 | } |
| 4170 | |
| 4171 | RValue<UInt4> pmaxud(RValue<UInt4> x, RValue<UInt4> y) |
| 4172 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4173 | return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_UGT))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4174 | } |
| 4175 | |
| 4176 | RValue<UInt4> pminud(RValue<UInt4> x, RValue<UInt4> y) |
| 4177 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4178 | return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_ULT))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | RValue<Short4> pmulhw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4182 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4183 | llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulh_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4184 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4185 | return As<Short4>(V(::builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4186 | } |
| 4187 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4188 | RValue<UShort4> pmulhuw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4189 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4190 | llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulhu_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4191 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4192 | return As<UShort4>(V(::builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4193 | } |
| 4194 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4195 | RValue<Int2> pmaddwd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4196 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4197 | llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmadd_wd); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4198 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4199 | return As<Int2>(V(::builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4200 | } |
| 4201 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4202 | RValue<Short8> pmulhw(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4203 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4204 | llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulh_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4205 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4206 | return RValue<Short8>(V(::builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4207 | } |
| 4208 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4209 | RValue<UShort8> pmulhuw(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4210 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4211 | llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulhu_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4212 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4213 | return RValue<UShort8>(V(::builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4214 | } |
| 4215 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4216 | RValue<Int4> pmaddwd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4217 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4218 | llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmadd_wd); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4219 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4220 | return RValue<Int4>(V(::builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4221 | } |
| 4222 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4223 | RValue<Int> movmskps(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4224 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4225 | llvm::Function *movmskps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_movmsk_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4226 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4227 | return RValue<Int>(V(::builder->CreateCall(movmskps, ARGS(V(x.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4228 | } |
| 4229 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4230 | RValue<Int> pmovmskb(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4231 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4232 | llvm::Function *pmovmskb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmovmskb_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4233 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4234 | return RValue<Int>(V(::builder->CreateCall(pmovmskb, ARGS(V(x.value))))) & 0xFF; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4235 | } |
| 4236 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4237 | RValue<Int4> pmovzxbd(RValue<Byte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4238 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4239 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4240 | } |
| 4241 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4242 | RValue<Int4> pmovsxbd(RValue<SByte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4243 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4244 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4245 | } |
| 4246 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4247 | RValue<Int4> pmovzxwd(RValue<UShort8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4248 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4249 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4250 | } |
| 4251 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4252 | RValue<Int4> pmovsxwd(RValue<Short8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4253 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4254 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4255 | } |
| 4256 | } |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 4257 | #endif // defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4258 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 4259 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4260 | // 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 Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4274 | // 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 Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4295 | // 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 Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4308 | 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 Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4317 | 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 Clayton | bc0cbb9 | 2019-05-15 17:12:57 +0100 | [diff] [blame] | 4319 | std::vector<Value*> PrintValue::Ty<const char*>::val(const char* v) { return {V(::builder->CreateGlobalStringPtr(v))}; } |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4320 | |
| 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 Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4325 | auto intTy = ::llvm::Type::getIntNTy(*::context, sizeof(int) * 8); // Natural integer width. |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4326 | 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 Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4377 | 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 Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4415 | } // namespace rr |
| 4416 | |
| 4417 | // ------------------------------ Coroutines ------------------------------ |
| 4418 | |
| 4419 | namespace { |
| 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 | |
| 4445 | namespace rr { |
| 4446 | |
| 4447 | void 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 Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 4493 | ::coroutine.await = rr::createFunction("coroutine_await", boolTy, {handleTy, promisePtrTy}); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4494 | { |
| 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 Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 4524 | ::coroutine.destroy = rr::createFunction("coroutine_destroy", voidTy, {handleTy}); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4525 | { |
| 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 Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 4564 | ::function = rr::createFunction("coroutine_begin", handleTy, T(Params)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4565 | |
| 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 Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4611 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4612 | |
| 4613 | void 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 | |
| 4659 | Routine* 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 |