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) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 91 | extern "C" void X86CompilationCallback() |
| 92 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 93 | UNIMPLEMENTED("X86CompilationCallback"); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 94 | } |
| 95 | #endif |
| 96 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 97 | namespace rr |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 98 | { |
| 99 | class LLVMReactorJIT; |
| 100 | } |
| 101 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 102 | namespace |
| 103 | { |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 104 | rr::LLVMReactorJIT *reactorJIT = nullptr; |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 105 | llvm::IRBuilder<> *builder = nullptr; |
| 106 | llvm::LLVMContext *context = nullptr; |
| 107 | llvm::Module *module = nullptr; |
| 108 | llvm::Function *function = nullptr; |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 109 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 110 | #ifdef ENABLE_RR_DEBUG_INFO |
| 111 | std::unique_ptr<rr::DebugInfo> debugInfo; |
| 112 | #endif |
| 113 | |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 114 | rr::MutexLock codegenMutex; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 115 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 116 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 117 | std::string replace(std::string str, const std::string& substr, const std::string& replacement) |
| 118 | { |
| 119 | size_t pos = 0; |
| 120 | while((pos = str.find(substr, pos)) != std::string::npos) { |
| 121 | str.replace(pos, substr.length(), replacement); |
| 122 | pos += replacement.length(); |
| 123 | } |
| 124 | return str; |
| 125 | } |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 126 | #endif // ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 127 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 128 | llvm::Value *lowerPAVG(llvm::Value *x, llvm::Value *y) |
| 129 | { |
| 130 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 131 | |
| 132 | llvm::VectorType *extTy = |
| 133 | llvm::VectorType::getExtendedElementVectorType(ty); |
| 134 | x = ::builder->CreateZExt(x, extTy); |
| 135 | y = ::builder->CreateZExt(y, extTy); |
| 136 | |
| 137 | // (x + y + 1) >> 1 |
| 138 | llvm::Constant *one = llvm::ConstantInt::get(extTy, 1); |
| 139 | llvm::Value *res = ::builder->CreateAdd(x, y); |
| 140 | res = ::builder->CreateAdd(res, one); |
| 141 | res = ::builder->CreateLShr(res, one); |
| 142 | return ::builder->CreateTrunc(res, ty); |
| 143 | } |
| 144 | |
| 145 | llvm::Value *lowerPMINMAX(llvm::Value *x, llvm::Value *y, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 146 | llvm::ICmpInst::Predicate pred) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 147 | { |
| 148 | return ::builder->CreateSelect(::builder->CreateICmp(pred, x, y), x, y); |
| 149 | } |
| 150 | |
| 151 | llvm::Value *lowerPCMP(llvm::ICmpInst::Predicate pred, llvm::Value *x, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 152 | llvm::Value *y, llvm::Type *dstTy) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 153 | { |
| 154 | return ::builder->CreateSExt(::builder->CreateICmp(pred, x, y), dstTy, ""); |
| 155 | } |
| 156 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 157 | #if defined(__i386__) || defined(__x86_64__) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 158 | llvm::Value *lowerPMOV(llvm::Value *op, llvm::Type *dstType, bool sext) |
| 159 | { |
| 160 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(op->getType()); |
| 161 | llvm::VectorType *dstTy = llvm::cast<llvm::VectorType>(dstType); |
| 162 | |
| 163 | llvm::Value *undef = llvm::UndefValue::get(srcTy); |
| 164 | llvm::SmallVector<uint32_t, 16> mask(dstTy->getNumElements()); |
| 165 | std::iota(mask.begin(), mask.end(), 0); |
| 166 | llvm::Value *v = ::builder->CreateShuffleVector(op, undef, mask); |
| 167 | |
| 168 | return sext ? ::builder->CreateSExt(v, dstTy) |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 169 | : ::builder->CreateZExt(v, dstTy); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | llvm::Value *lowerPABS(llvm::Value *v) |
| 173 | { |
| 174 | llvm::Value *zero = llvm::Constant::getNullValue(v->getType()); |
| 175 | llvm::Value *cmp = ::builder->CreateICmp(llvm::ICmpInst::ICMP_SGT, v, zero); |
| 176 | llvm::Value *neg = ::builder->CreateNeg(v); |
| 177 | return ::builder->CreateSelect(cmp, v, neg); |
| 178 | } |
| 179 | #endif // defined(__i386__) || defined(__x86_64__) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 180 | |
| 181 | #if !defined(__i386__) && !defined(__x86_64__) |
| 182 | llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 183 | llvm::FCmpInst::Predicate pred) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 184 | { |
| 185 | return ::builder->CreateSelect(::builder->CreateFCmp(pred, x, y), x, y); |
| 186 | } |
| 187 | |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 188 | llvm::Value *lowerRound(llvm::Value *x) |
| 189 | { |
| 190 | llvm::Function *nearbyint = llvm::Intrinsic::getDeclaration( |
| 191 | ::module, llvm::Intrinsic::nearbyint, {x->getType()}); |
| 192 | return ::builder->CreateCall(nearbyint, ARGS(x)); |
| 193 | } |
| 194 | |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 195 | llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty) |
| 196 | { |
| 197 | return ::builder->CreateFPToSI(lowerRound(x), ty); |
| 198 | } |
| 199 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 200 | llvm::Value *lowerFloor(llvm::Value *x) |
| 201 | { |
| 202 | llvm::Function *floor = llvm::Intrinsic::getDeclaration( |
| 203 | ::module, llvm::Intrinsic::floor, {x->getType()}); |
| 204 | return ::builder->CreateCall(floor, ARGS(x)); |
| 205 | } |
| 206 | |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 207 | llvm::Value *lowerTrunc(llvm::Value *x) |
| 208 | { |
| 209 | llvm::Function *trunc = llvm::Intrinsic::getDeclaration( |
| 210 | ::module, llvm::Intrinsic::trunc, {x->getType()}); |
| 211 | return ::builder->CreateCall(trunc, ARGS(x)); |
| 212 | } |
| 213 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 214 | // Packed add/sub saturatation |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 215 | 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] | 216 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 217 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 218 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 219 | |
| 220 | unsigned numBits = ty->getScalarSizeInBits(); |
| 221 | |
| 222 | llvm::Value *max, *min, *extX, *extY; |
| 223 | if (isSigned) |
| 224 | { |
| 225 | max = llvm::ConstantInt::get(extTy, (1LL << (numBits - 1)) - 1, true); |
| 226 | min = llvm::ConstantInt::get(extTy, (-1LL << (numBits - 1)), true); |
| 227 | extX = ::builder->CreateSExt(x, extTy); |
| 228 | extY = ::builder->CreateSExt(y, extTy); |
| 229 | } |
| 230 | else |
| 231 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 232 | ASSERT_MSG(numBits <= 64, "numBits: %d", int(numBits)); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 233 | uint64_t maxVal = (numBits == 64) ? ~0ULL : (1ULL << numBits) - 1; |
| 234 | max = llvm::ConstantInt::get(extTy, maxVal, false); |
| 235 | min = llvm::ConstantInt::get(extTy, 0, false); |
| 236 | extX = ::builder->CreateZExt(x, extTy); |
| 237 | extY = ::builder->CreateZExt(y, extTy); |
| 238 | } |
| 239 | |
| 240 | llvm::Value *res = isAdd ? ::builder->CreateAdd(extX, extY) |
| 241 | : ::builder->CreateSub(extX, extY); |
| 242 | |
| 243 | res = lowerPMINMAX(res, min, llvm::ICmpInst::ICMP_SGT); |
| 244 | res = lowerPMINMAX(res, max, llvm::ICmpInst::ICMP_SLT); |
| 245 | |
| 246 | return ::builder->CreateTrunc(res, ty); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | llvm::Value *lowerPUADDSAT(llvm::Value *x, llvm::Value *y) |
| 250 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 251 | return lowerPSAT(x, y, true, false); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | llvm::Value *lowerPSADDSAT(llvm::Value *x, llvm::Value *y) |
| 255 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 256 | return lowerPSAT(x, y, true, true); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | llvm::Value *lowerPUSUBSAT(llvm::Value *x, llvm::Value *y) |
| 260 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 261 | return lowerPSAT(x, y, false, false); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | llvm::Value *lowerPSSUBSAT(llvm::Value *x, llvm::Value *y) |
| 265 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 266 | return lowerPSAT(x, y, false, true); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | llvm::Value *lowerSQRT(llvm::Value *x) |
| 270 | { |
| 271 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration( |
| 272 | ::module, llvm::Intrinsic::sqrt, {x->getType()}); |
| 273 | return ::builder->CreateCall(sqrt, ARGS(x)); |
| 274 | } |
| 275 | |
| 276 | llvm::Value *lowerRCP(llvm::Value *x) |
| 277 | { |
| 278 | llvm::Type *ty = x->getType(); |
| 279 | llvm::Constant *one; |
| 280 | if (llvm::VectorType *vectorTy = llvm::dyn_cast<llvm::VectorType>(ty)) |
| 281 | { |
| 282 | one = llvm::ConstantVector::getSplat( |
| 283 | vectorTy->getNumElements(), |
| 284 | llvm::ConstantFP::get(vectorTy->getElementType(), 1)); |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | one = llvm::ConstantFP::get(ty, 1); |
| 289 | } |
| 290 | return ::builder->CreateFDiv(one, x); |
| 291 | } |
| 292 | |
| 293 | llvm::Value *lowerRSQRT(llvm::Value *x) |
| 294 | { |
| 295 | return lowerRCP(lowerSQRT(x)); |
| 296 | } |
| 297 | |
| 298 | llvm::Value *lowerVectorShl(llvm::Value *x, uint64_t scalarY) |
| 299 | { |
| 300 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 301 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 302 | ty->getNumElements(), |
| 303 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
| 304 | return ::builder->CreateShl(x, y); |
| 305 | } |
| 306 | |
| 307 | llvm::Value *lowerVectorAShr(llvm::Value *x, uint64_t scalarY) |
| 308 | { |
| 309 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 310 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 311 | ty->getNumElements(), |
| 312 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
| 313 | return ::builder->CreateAShr(x, y); |
| 314 | } |
| 315 | |
| 316 | llvm::Value *lowerVectorLShr(llvm::Value *x, uint64_t scalarY) |
| 317 | { |
| 318 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 319 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 320 | ty->getNumElements(), |
| 321 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
| 322 | return ::builder->CreateLShr(x, y); |
| 323 | } |
| 324 | |
| 325 | llvm::Value *lowerMulAdd(llvm::Value *x, llvm::Value *y) |
| 326 | { |
| 327 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 328 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 329 | |
| 330 | llvm::Value *extX = ::builder->CreateSExt(x, extTy); |
| 331 | llvm::Value *extY = ::builder->CreateSExt(y, extTy); |
| 332 | llvm::Value *mult = ::builder->CreateMul(extX, extY); |
| 333 | |
| 334 | llvm::Value *undef = llvm::UndefValue::get(extTy); |
| 335 | |
| 336 | llvm::SmallVector<uint32_t, 16> evenIdx; |
| 337 | llvm::SmallVector<uint32_t, 16> oddIdx; |
| 338 | for (uint64_t i = 0, n = ty->getNumElements(); i < n; i += 2) |
| 339 | { |
| 340 | evenIdx.push_back(i); |
| 341 | oddIdx.push_back(i + 1); |
| 342 | } |
| 343 | |
| 344 | llvm::Value *lhs = ::builder->CreateShuffleVector(mult, undef, evenIdx); |
| 345 | llvm::Value *rhs = ::builder->CreateShuffleVector(mult, undef, oddIdx); |
| 346 | return ::builder->CreateAdd(lhs, rhs); |
| 347 | } |
| 348 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 349 | llvm::Value *lowerPack(llvm::Value *x, llvm::Value *y, bool isSigned) |
| 350 | { |
| 351 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(x->getType()); |
| 352 | llvm::VectorType *dstTy = llvm::VectorType::getTruncatedElementVectorType(srcTy); |
| 353 | |
| 354 | llvm::IntegerType *dstElemTy = |
| 355 | llvm::cast<llvm::IntegerType>(dstTy->getElementType()); |
| 356 | |
| 357 | uint64_t truncNumBits = dstElemTy->getIntegerBitWidth(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 358 | 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] | 359 | llvm::Constant *max, *min; |
| 360 | if (isSigned) |
| 361 | { |
| 362 | max = llvm::ConstantInt::get(srcTy, (1LL << (truncNumBits - 1)) - 1, true); |
| 363 | min = llvm::ConstantInt::get(srcTy, (-1LL << (truncNumBits - 1)), true); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | max = llvm::ConstantInt::get(srcTy, (1ULL << truncNumBits) - 1, false); |
| 368 | min = llvm::ConstantInt::get(srcTy, 0, false); |
| 369 | } |
| 370 | |
| 371 | x = lowerPMINMAX(x, min, llvm::ICmpInst::ICMP_SGT); |
| 372 | x = lowerPMINMAX(x, max, llvm::ICmpInst::ICMP_SLT); |
| 373 | y = lowerPMINMAX(y, min, llvm::ICmpInst::ICMP_SGT); |
| 374 | y = lowerPMINMAX(y, max, llvm::ICmpInst::ICMP_SLT); |
| 375 | |
| 376 | x = ::builder->CreateTrunc(x, dstTy); |
| 377 | y = ::builder->CreateTrunc(y, dstTy); |
| 378 | |
| 379 | llvm::SmallVector<uint32_t, 16> index(srcTy->getNumElements() * 2); |
| 380 | std::iota(index.begin(), index.end(), 0); |
| 381 | |
| 382 | return ::builder->CreateShuffleVector(x, y, index); |
| 383 | } |
| 384 | |
| 385 | llvm::Value *lowerSignMask(llvm::Value *x, llvm::Type *retTy) |
| 386 | { |
| 387 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 388 | llvm::Constant *zero = llvm::ConstantInt::get(ty, 0); |
| 389 | llvm::Value *cmp = ::builder->CreateICmpSLT(x, zero); |
| 390 | |
| 391 | llvm::Value *ret = ::builder->CreateZExt( |
| 392 | ::builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
| 393 | for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
| 394 | { |
| 395 | llvm::Value *elem = ::builder->CreateZExt( |
| 396 | ::builder->CreateExtractElement(cmp, i), retTy); |
| 397 | ret = ::builder->CreateOr(ret, ::builder->CreateShl(elem, i)); |
| 398 | } |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | llvm::Value *lowerFPSignMask(llvm::Value *x, llvm::Type *retTy) |
| 403 | { |
| 404 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 405 | llvm::Constant *zero = llvm::ConstantFP::get(ty, 0); |
| 406 | llvm::Value *cmp = ::builder->CreateFCmpULT(x, zero); |
| 407 | |
| 408 | llvm::Value *ret = ::builder->CreateZExt( |
| 409 | ::builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
| 410 | for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
| 411 | { |
| 412 | llvm::Value *elem = ::builder->CreateZExt( |
| 413 | ::builder->CreateExtractElement(cmp, i), retTy); |
| 414 | ret = ::builder->CreateOr(ret, ::builder->CreateShl(elem, i)); |
| 415 | } |
| 416 | return ret; |
| 417 | } |
| 418 | #endif // !defined(__i386__) && !defined(__x86_64__) |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 419 | |
| 420 | llvm::Value *lowerMulHigh(llvm::Value *x, llvm::Value *y, bool sext) |
| 421 | { |
| 422 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 423 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 424 | |
| 425 | llvm::Value *extX, *extY; |
| 426 | if (sext) |
| 427 | { |
| 428 | extX = ::builder->CreateSExt(x, extTy); |
| 429 | extY = ::builder->CreateSExt(y, extTy); |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | extX = ::builder->CreateZExt(x, extTy); |
| 434 | extY = ::builder->CreateZExt(y, extTy); |
| 435 | } |
| 436 | |
| 437 | llvm::Value *mult = ::builder->CreateMul(extX, extY); |
| 438 | |
| 439 | llvm::IntegerType *intTy = llvm::cast<llvm::IntegerType>(ty->getElementType()); |
| 440 | llvm::Value *mulh = ::builder->CreateAShr(mult, intTy->getBitWidth()); |
| 441 | return ::builder->CreateTrunc(mulh, ty); |
| 442 | } |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 443 | } |
| 444 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 445 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 446 | { |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 447 | const Capabilities Caps = |
| 448 | { |
| 449 | true, // CallSupported |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 450 | true, // CoroutinesSupported |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 451 | }; |
| 452 | |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 453 | static std::memory_order atomicOrdering(llvm::AtomicOrdering memoryOrder) |
| 454 | { |
| 455 | switch(memoryOrder) |
| 456 | { |
| 457 | case llvm::AtomicOrdering::Monotonic: return std::memory_order_relaxed; // https://llvm.org/docs/Atomics.html#monotonic |
| 458 | case llvm::AtomicOrdering::Acquire: return std::memory_order_acquire; |
| 459 | case llvm::AtomicOrdering::Release: return std::memory_order_release; |
| 460 | case llvm::AtomicOrdering::AcquireRelease: return std::memory_order_acq_rel; |
| 461 | case llvm::AtomicOrdering::SequentiallyConsistent: return std::memory_order_seq_cst; |
| 462 | default: |
Ben Clayton | fb28067 | 2019-04-25 11:16:15 +0100 | [diff] [blame] | 463 | UNREACHABLE("memoryOrder: %d", int(memoryOrder)); |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 464 | return std::memory_order_acq_rel; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | static llvm::AtomicOrdering atomicOrdering(bool atomic, std::memory_order memoryOrder) |
| 469 | { |
| 470 | if(!atomic) |
| 471 | { |
| 472 | return llvm::AtomicOrdering::NotAtomic; |
| 473 | } |
| 474 | |
| 475 | switch(memoryOrder) |
| 476 | { |
| 477 | case std::memory_order_relaxed: return llvm::AtomicOrdering::Monotonic; // https://llvm.org/docs/Atomics.html#monotonic |
| 478 | 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." |
| 479 | case std::memory_order_acquire: return llvm::AtomicOrdering::Acquire; |
| 480 | case std::memory_order_release: return llvm::AtomicOrdering::Release; |
| 481 | case std::memory_order_acq_rel: return llvm::AtomicOrdering::AcquireRelease; |
| 482 | case std::memory_order_seq_cst: return llvm::AtomicOrdering::SequentiallyConsistent; |
| 483 | default: |
| 484 | UNREACHABLE("memoryOrder: %d", int(memoryOrder)); |
| 485 | return llvm::AtomicOrdering::AcquireRelease; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | template <typename T> |
| 490 | static void atomicLoad(void *ptr, void *ret, llvm::AtomicOrdering ordering) |
| 491 | { |
| 492 | *reinterpret_cast<T*>(ret) = std::atomic_load_explicit<T>(reinterpret_cast<std::atomic<T>*>(ptr), atomicOrdering(ordering)); |
| 493 | } |
| 494 | |
| 495 | template <typename T> |
| 496 | static void atomicStore(void *ptr, void *val, llvm::AtomicOrdering ordering) |
| 497 | { |
| 498 | std::atomic_store_explicit<T>(reinterpret_cast<std::atomic<T>*>(ptr), *reinterpret_cast<T*>(val), atomicOrdering(ordering)); |
| 499 | } |
| 500 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 501 | class ExternalFunctionSymbolResolver |
| 502 | { |
| 503 | private: |
| 504 | using FunctionMap = std::unordered_map<std::string, void *>; |
| 505 | FunctionMap func_; |
| 506 | |
| 507 | public: |
| 508 | ExternalFunctionSymbolResolver() |
| 509 | { |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 510 | struct Atomic |
| 511 | { |
| 512 | static void load(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering) |
| 513 | { |
| 514 | switch (size) |
| 515 | { |
| 516 | case 1: atomicLoad<uint8_t>(ptr, ret, ordering); break; |
| 517 | case 2: atomicLoad<uint16_t>(ptr, ret, ordering); break; |
| 518 | case 4: atomicLoad<uint32_t>(ptr, ret, ordering); break; |
| 519 | case 8: atomicLoad<uint64_t>(ptr, ret, ordering); break; |
| 520 | default: |
| 521 | UNIMPLEMENTED("Atomic::load(size: %d)", int(size)); |
| 522 | } |
| 523 | } |
| 524 | static void store(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering) |
| 525 | { |
| 526 | switch (size) |
| 527 | { |
| 528 | case 1: atomicStore<uint8_t>(ptr, ret, ordering); break; |
| 529 | case 2: atomicStore<uint16_t>(ptr, ret, ordering); break; |
| 530 | case 4: atomicStore<uint32_t>(ptr, ret, ordering); break; |
| 531 | case 8: atomicStore<uint64_t>(ptr, ret, ordering); break; |
| 532 | default: |
| 533 | UNIMPLEMENTED("Atomic::store(size: %d)", int(size)); |
| 534 | } |
| 535 | } |
| 536 | }; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 537 | struct F { static void nop() {} }; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 538 | |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 539 | func_.emplace("nop", reinterpret_cast<void*>(F::nop)); |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 540 | func_.emplace("floorf", reinterpret_cast<void*>(floorf)); |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 541 | func_.emplace("nearbyintf", reinterpret_cast<void*>(nearbyintf)); |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 542 | func_.emplace("truncf", reinterpret_cast<void*>(truncf)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 543 | func_.emplace("printf", reinterpret_cast<void*>(printf)); |
| 544 | func_.emplace("puts", reinterpret_cast<void*>(puts)); |
Chris Forbes | 1a4c712 | 2019-03-15 14:50:47 -0700 | [diff] [blame] | 545 | func_.emplace("fmodf", reinterpret_cast<void*>(fmodf)); |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 546 | func_.emplace("sinf", reinterpret_cast<void*>(sinf)); |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 547 | func_.emplace("cosf", reinterpret_cast<void*>(cosf)); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 548 | func_.emplace("asinf", reinterpret_cast<void*>(asinf)); |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 549 | func_.emplace("acosf", reinterpret_cast<void*>(acosf)); |
Ben Clayton | 749b4e0 | 2019-04-09 14:27:43 -0400 | [diff] [blame] | 550 | func_.emplace("atanf", reinterpret_cast<void*>(atanf)); |
Ben Clayton | d963697 | 2019-04-09 15:09:54 -0400 | [diff] [blame] | 551 | func_.emplace("sinhf", reinterpret_cast<void*>(sinhf)); |
Ben Clayton | 900ea2c | 2019-04-09 15:25:36 -0400 | [diff] [blame] | 552 | func_.emplace("coshf", reinterpret_cast<void*>(coshf)); |
Ben Clayton | 3928bd9 | 2019-04-09 15:27:41 -0400 | [diff] [blame] | 553 | func_.emplace("tanhf", reinterpret_cast<void*>(tanhf)); |
Ben Clayton | f6d77ab | 2019-04-09 15:30:04 -0400 | [diff] [blame] | 554 | func_.emplace("asinhf", reinterpret_cast<void*>(asinhf)); |
Ben Clayton | 28ebcb0 | 2019-04-09 15:33:38 -0400 | [diff] [blame] | 555 | func_.emplace("acoshf", reinterpret_cast<void*>(acoshf)); |
Ben Clayton | fa6a539 | 2019-04-09 15:35:24 -0400 | [diff] [blame] | 556 | func_.emplace("atanhf", reinterpret_cast<void*>(atanhf)); |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 557 | func_.emplace("atan2f", reinterpret_cast<void*>(atan2f)); |
Ben Clayton | bfe94f0 | 2019-04-09 15:52:12 -0400 | [diff] [blame] | 558 | func_.emplace("powf", reinterpret_cast<void*>(powf)); |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 559 | func_.emplace("expf", reinterpret_cast<void*>(expf)); |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 560 | func_.emplace("logf", reinterpret_cast<void*>(logf)); |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 561 | func_.emplace("exp2f", reinterpret_cast<void*>(exp2f)); |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 562 | func_.emplace("log2f", reinterpret_cast<void*>(log2f)); |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 563 | func_.emplace("atomic_load", reinterpret_cast<void*>(Atomic::load)); |
| 564 | func_.emplace("atomic_store", reinterpret_cast<void*>(Atomic::store)); |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 565 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 566 | // FIXME (b/119409619): use an allocator here so we can control all memory allocations |
| 567 | func_.emplace("coroutine_alloc_frame", reinterpret_cast<void*>(malloc)); |
| 568 | func_.emplace("coroutine_free_frame", reinterpret_cast<void*>(free)); |
| 569 | |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 570 | #ifdef __APPLE__ |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 571 | func_.emplace("sincosf_stret", reinterpret_cast<void*>(__sincosf_stret)); |
| 572 | #elif defined(__linux__) |
| 573 | func_.emplace("sincosf", reinterpret_cast<void*>(sincosf)); |
| 574 | #endif // __APPLE__ |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | void *findSymbol(const std::string &name) const |
| 578 | { |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 579 | // Trim off any underscores from the start of the symbol. LLVM likes |
| 580 | // to append these on macOS. |
| 581 | const char* trimmed = name.c_str(); |
| 582 | while (trimmed[0] == '_') { trimmed++; } |
| 583 | |
| 584 | FunctionMap::const_iterator it = func_.find(trimmed); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 585 | // Missing functions will likely make the module fail in exciting non-obvious ways. |
| 586 | ASSERT_MSG(it != func_.end(), "Missing external function: '%s'", name.c_str()); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 587 | return it->second; |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 588 | } |
| 589 | }; |
| 590 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 591 | class LLVMReactorJIT |
| 592 | { |
| 593 | private: |
| 594 | using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer; |
| 595 | using CompileLayer = llvm::orc::IRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>; |
| 596 | |
| 597 | llvm::orc::ExecutionSession session; |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 598 | ExternalFunctionSymbolResolver externalSymbolResolver; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 599 | std::shared_ptr<llvm::orc::SymbolResolver> resolver; |
| 600 | std::unique_ptr<llvm::TargetMachine> targetMachine; |
| 601 | const llvm::DataLayout dataLayout; |
| 602 | ObjLayer objLayer; |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 603 | CompileLayer compileLayer; // guarded by mutex |
| 604 | std::mutex mutex; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 605 | size_t emittedFunctionsNum; |
| 606 | |
| 607 | public: |
| 608 | LLVMReactorJIT(const char *arch, const llvm::SmallVectorImpl<std::string>& mattrs, |
| 609 | const llvm::TargetOptions &targetOpts): |
| 610 | resolver(createLegacyLookupResolver( |
| 611 | session, |
| 612 | [this](const std::string &name) { |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 613 | void *func = externalSymbolResolver.findSymbol(name); |
| 614 | if (func != nullptr) |
| 615 | { |
| 616 | return llvm::JITSymbol( |
| 617 | reinterpret_cast<uintptr_t>(func), llvm::JITSymbolFlags::Absolute); |
| 618 | } |
| 619 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 620 | return objLayer.findSymbol(name, true); |
| 621 | }, |
| 622 | [](llvm::Error err) { |
| 623 | if (err) |
| 624 | { |
| 625 | // TODO: Log the symbol resolution errors. |
| 626 | return; |
| 627 | } |
| 628 | })), |
| 629 | targetMachine(llvm::EngineBuilder() |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 630 | #ifdef ENABLE_RR_DEBUG_INFO |
| 631 | .setOptLevel(llvm::CodeGenOpt::None) |
| 632 | #endif // ENABLE_RR_DEBUG_INFO |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 633 | .setMArch(arch) |
| 634 | .setMAttrs(mattrs) |
| 635 | .setTargetOptions(targetOpts) |
| 636 | .selectTarget()), |
| 637 | dataLayout(targetMachine->createDataLayout()), |
| 638 | objLayer( |
| 639 | session, |
| 640 | [this](llvm::orc::VModuleKey) { |
| 641 | return ObjLayer::Resources{ |
| 642 | std::make_shared<llvm::SectionMemoryManager>(), |
| 643 | resolver}; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 644 | }, |
| 645 | ObjLayer::NotifyLoadedFtor(), |
| 646 | [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj, const llvm::RuntimeDyld::LoadedObjectInfo &L) { |
| 647 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 648 | DebugInfo::NotifyObjectEmitted(Obj, L); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 649 | #endif // ENABLE_RR_DEBUG_INFO |
| 650 | }, |
| 651 | [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj) { |
| 652 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 653 | DebugInfo::NotifyFreeingObject(Obj); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 654 | #endif // ENABLE_RR_DEBUG_INFO |
| 655 | } |
| 656 | ), |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 657 | compileLayer(objLayer, llvm::orc::SimpleCompiler(*targetMachine)), |
| 658 | emittedFunctionsNum(0) |
| 659 | { |
| 660 | } |
| 661 | |
| 662 | void startSession() |
| 663 | { |
| 664 | ::module = new llvm::Module("", *::context); |
| 665 | } |
| 666 | |
| 667 | void endSession() |
| 668 | { |
| 669 | ::function = nullptr; |
| 670 | ::module = nullptr; |
| 671 | } |
| 672 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 673 | LLVMRoutine *acquireRoutine(llvm::Function **funcs, size_t count) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 674 | { |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 675 | std::vector<std::string> mangledNames(count); |
| 676 | for (size_t i = 0; i < count; i++) |
| 677 | { |
| 678 | auto func = funcs[i]; |
| 679 | std::string name = "f" + llvm::Twine(emittedFunctionsNum++).str(); |
| 680 | func->setName(name); |
| 681 | func->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 682 | func->setDoesNotThrow(); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 683 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 684 | llvm::raw_string_ostream mangledNameStream(mangledNames[i]); |
| 685 | llvm::Mangler::getNameWithPrefix(mangledNameStream, name, dataLayout); |
| 686 | } |
| 687 | |
| 688 | // Compile the module - after this the llvm::Functions will have |
| 689 | // been freed. |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 690 | std::unique_ptr<llvm::Module> mod(::module); |
| 691 | ::module = nullptr; |
| 692 | mod->setDataLayout(dataLayout); |
| 693 | |
| 694 | auto moduleKey = session.allocateVModule(); |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 695 | |
| 696 | // Resolve the function symbols - needs to be performed under mutex lock. |
| 697 | std::vector<llvm::JITSymbol> symbols; |
| 698 | { |
| 699 | std::unique_lock<std::mutex> lock(mutex); |
| 700 | llvm::cantFail(compileLayer.addModule(moduleKey, std::move(mod))); |
| 701 | funcs = nullptr; // Now points to released memory. |
| 702 | for (size_t i = 0; i < count; i++) |
| 703 | { |
| 704 | symbols.push_back(compileLayer.findSymbolIn(moduleKey, mangledNames[i], false)); |
| 705 | } |
| 706 | } |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 707 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 708 | // Resolve the function addresses. |
| 709 | std::vector<void*> addresses(count); |
| 710 | for (size_t i = 0; i < count; i++) |
Nicolas Capens | adfbbcb | 2018-10-31 14:38:53 -0400 | [diff] [blame] | 711 | { |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 712 | if(auto expectAddr = symbols[i].getAddress()) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 713 | { |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 714 | addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(expectAddr.get())); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 715 | } |
Nicolas Capens | adfbbcb | 2018-10-31 14:38:53 -0400 | [diff] [blame] | 716 | } |
| 717 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 718 | return new LLVMRoutine(addresses.data(), count, releaseRoutineCallback, this, moduleKey); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void optimize(llvm::Module *module) |
| 722 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 723 | #ifdef ENABLE_RR_DEBUG_INFO |
| 724 | if (debugInfo != nullptr) |
| 725 | { |
| 726 | return; // Don't optimize if we're generating debug info. |
| 727 | } |
| 728 | #endif // ENABLE_RR_DEBUG_INFO |
| 729 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 730 | std::unique_ptr<llvm::legacy::PassManager> passManager( |
| 731 | new llvm::legacy::PassManager()); |
| 732 | |
| 733 | passManager->add(llvm::createSROAPass()); |
| 734 | |
| 735 | for(int pass = 0; pass < 10 && optimization[pass] != Disabled; pass++) |
| 736 | { |
| 737 | switch(optimization[pass]) |
| 738 | { |
| 739 | case Disabled: break; |
| 740 | case CFGSimplification: passManager->add(llvm::createCFGSimplificationPass()); break; |
| 741 | case LICM: passManager->add(llvm::createLICMPass()); break; |
| 742 | case AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); break; |
| 743 | case GVN: passManager->add(llvm::createGVNPass()); break; |
| 744 | case InstructionCombining: passManager->add(llvm::createInstructionCombiningPass()); break; |
| 745 | case Reassociate: passManager->add(llvm::createReassociatePass()); break; |
| 746 | case DeadStoreElimination: passManager->add(llvm::createDeadStoreEliminationPass()); break; |
| 747 | case SCCP: passManager->add(llvm::createSCCPPass()); break; |
| 748 | case ScalarReplAggregates: passManager->add(llvm::createSROAPass()); break; |
| 749 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 750 | UNREACHABLE("optimization[pass]: %d, pass: %d", int(optimization[pass]), int(pass)); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
| 754 | passManager->run(*::module); |
| 755 | } |
| 756 | |
| 757 | private: |
| 758 | void releaseRoutineModule(llvm::orc::VModuleKey moduleKey) |
| 759 | { |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 760 | std::unique_lock<std::mutex> lock(mutex); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 761 | llvm::cantFail(compileLayer.removeModule(moduleKey)); |
| 762 | } |
| 763 | |
| 764 | static void releaseRoutineCallback(LLVMReactorJIT *jit, uint64_t moduleKey) |
| 765 | { |
| 766 | jit->releaseRoutineModule(moduleKey); |
| 767 | } |
| 768 | }; |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 769 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 770 | Optimization optimization[10] = {InstructionCombining, Disabled}; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 771 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 772 | // The abstract Type* types are implemented as LLVM types, except that |
| 773 | // 64-bit vectors are emulated using 128-bit ones to avoid use of MMX in x86 |
| 774 | // and VFP in ARM, and eliminate the overhead of converting them to explicit |
| 775 | // 128-bit ones. LLVM types are pointers, so we can represent emulated types |
| 776 | // as abstract pointers with small enum values. |
| 777 | enum InternalType : uintptr_t |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 778 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 779 | // Emulated types: |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 780 | Type_v2i32, |
| 781 | Type_v4i16, |
| 782 | Type_v2i16, |
| 783 | Type_v8i8, |
| 784 | Type_v4i8, |
| 785 | Type_v2f32, |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 786 | EmulatedTypeCount, |
| 787 | // Returned by asInternalType() to indicate that the abstract Type* |
| 788 | // should be interpreted as LLVM type pointer: |
| 789 | Type_LLVM |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 790 | }; |
| 791 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 792 | inline InternalType asInternalType(Type *type) |
| 793 | { |
| 794 | InternalType t = static_cast<InternalType>(reinterpret_cast<uintptr_t>(type)); |
| 795 | return (t < EmulatedTypeCount) ? t : Type_LLVM; |
| 796 | } |
| 797 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 798 | llvm::Type *T(Type *t) |
| 799 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 800 | // Use 128-bit vectors to implement logically shorter ones. |
| 801 | switch(asInternalType(t)) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 802 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 803 | case Type_v2i32: return T(Int4::getType()); |
| 804 | case Type_v4i16: return T(Short8::getType()); |
| 805 | case Type_v2i16: return T(Short8::getType()); |
| 806 | case Type_v8i8: return T(Byte16::getType()); |
| 807 | case Type_v4i8: return T(Byte16::getType()); |
| 808 | case Type_v2f32: return T(Float4::getType()); |
| 809 | case Type_LLVM: return reinterpret_cast<llvm::Type*>(t); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 810 | default: |
| 811 | UNREACHABLE("asInternalType(t): %d", int(asInternalType(t))); |
| 812 | return nullptr; |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 813 | } |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 814 | } |
| 815 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 816 | Type *T(InternalType t) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 817 | { |
| 818 | return reinterpret_cast<Type*>(t); |
| 819 | } |
| 820 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 821 | inline std::vector<llvm::Type*> &T(std::vector<Type*> &t) |
| 822 | { |
| 823 | return reinterpret_cast<std::vector<llvm::Type*>&>(t); |
| 824 | } |
| 825 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 826 | inline llvm::BasicBlock *B(BasicBlock *t) |
| 827 | { |
| 828 | return reinterpret_cast<llvm::BasicBlock*>(t); |
| 829 | } |
| 830 | |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 831 | inline BasicBlock *B(llvm::BasicBlock *t) |
| 832 | { |
| 833 | return reinterpret_cast<BasicBlock*>(t); |
| 834 | } |
| 835 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 836 | static size_t typeSize(Type *type) |
| 837 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 838 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 839 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 840 | case Type_v2i32: return 8; |
| 841 | case Type_v4i16: return 8; |
| 842 | case Type_v2i16: return 4; |
| 843 | case Type_v8i8: return 8; |
| 844 | case Type_v4i8: return 4; |
| 845 | case Type_v2f32: return 8; |
| 846 | case Type_LLVM: |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 847 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 848 | llvm::Type *t = T(type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 849 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 850 | if(t->isPointerTy()) |
| 851 | { |
| 852 | return sizeof(void*); |
| 853 | } |
| 854 | |
| 855 | // At this point we should only have LLVM 'primitive' types. |
| 856 | unsigned int bits = t->getPrimitiveSizeInBits(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 857 | ASSERT_MSG(bits != 0, "bits: %d", int(bits)); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 858 | |
| 859 | // TODO(capn): Booleans are 1 bit integers in LLVM's SSA type system, |
| 860 | // but are typically stored as one byte. The DataLayout structure should |
| 861 | // be used here and many other places if this assumption fails. |
| 862 | return (bits + 7) / 8; |
| 863 | } |
| 864 | break; |
| 865 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 866 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 867 | return 0; |
| 868 | } |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 869 | } |
| 870 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 871 | static unsigned int elementCount(Type *type) |
| 872 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 873 | switch(asInternalType(type)) |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 874 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 875 | case Type_v2i32: return 2; |
| 876 | case Type_v4i16: return 4; |
| 877 | case Type_v2i16: return 2; |
| 878 | case Type_v8i8: return 8; |
| 879 | case Type_v4i8: return 4; |
| 880 | case Type_v2f32: return 2; |
| 881 | case Type_LLVM: return llvm::cast<llvm::VectorType>(T(type))->getNumElements(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 882 | default: |
| 883 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 884 | return 0; |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 885 | } |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 886 | } |
| 887 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 888 | Nucleus::Nucleus() |
| 889 | { |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 890 | ::codegenMutex.lock(); // Reactor and LLVM are currently not thread safe |
Nicolas Capens | b7ea984 | 2015-04-01 10:54:59 -0400 | [diff] [blame] | 891 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 892 | llvm::InitializeNativeTarget(); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 893 | llvm::InitializeNativeTargetAsmPrinter(); |
| 894 | llvm::InitializeNativeTargetAsmParser(); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 895 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 896 | if(!::context) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 897 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 898 | ::context = new llvm::LLVMContext(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 899 | } |
| 900 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 901 | #if defined(__x86_64__) |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 902 | static const char arch[] = "x86-64"; |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 903 | #elif defined(__i386__) |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 904 | static const char arch[] = "x86"; |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 905 | #elif defined(__aarch64__) |
| 906 | static const char arch[] = "arm64"; |
| 907 | #elif defined(__arm__) |
| 908 | static const char arch[] = "arm"; |
Gordana Cmiljanovic | 082dfec | 2018-10-19 11:36:15 +0200 | [diff] [blame] | 909 | #elif defined(__mips__) |
Gordana Cmiljanovic | 20622c0 | 2018-11-05 15:00:11 +0100 | [diff] [blame] | 910 | #if defined(__mips64) |
| 911 | static const char arch[] = "mips64el"; |
| 912 | #else |
| 913 | static const char arch[] = "mipsel"; |
| 914 | #endif |
Colin Samples | 8fd5330 | 2019-06-13 09:57:44 -0400 | [diff] [blame^] | 915 | #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 916 | static const char arch[] = "ppc64le"; |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 917 | #else |
| 918 | #error "unknown architecture" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 919 | #endif |
| 920 | |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 921 | llvm::SmallVector<std::string, 8> mattrs; |
| 922 | |
| 923 | llvm::StringMap<bool> features; |
Colin Samples | 8fd5330 | 2019-06-13 09:57:44 -0400 | [diff] [blame^] | 924 | |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 925 | bool ok = llvm::sys::getHostCPUFeatures(features); |
Colin Samples | 8fd5330 | 2019-06-13 09:57:44 -0400 | [diff] [blame^] | 926 | |
| 927 | #if defined(__i386__) || defined(__x86_64__) || \ |
| 928 | (defined(__linux__) && (defined(__arm__) || defined(__aarch64__))) |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 929 | ASSERT_MSG(ok, "llvm::sys::getHostCPUFeatures returned false"); |
Colin Samples | 8fd5330 | 2019-06-13 09:57:44 -0400 | [diff] [blame^] | 930 | #else |
| 931 | (void) ok; // getHostCPUFeatures always returns false on other platforms |
| 932 | #endif |
| 933 | |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 934 | for (auto &feature : features) |
| 935 | { |
| 936 | if (feature.second) { mattrs.push_back(feature.first()); } |
| 937 | } |
| 938 | |
| 939 | #if 0 |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 940 | #if defined(__i386__) || defined(__x86_64__) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 941 | mattrs.push_back(CPUID::supportsMMX() ? "+mmx" : "-mmx"); |
| 942 | mattrs.push_back(CPUID::supportsCMOV() ? "+cmov" : "-cmov"); |
| 943 | mattrs.push_back(CPUID::supportsSSE() ? "+sse" : "-sse"); |
| 944 | mattrs.push_back(CPUID::supportsSSE2() ? "+sse2" : "-sse2"); |
| 945 | mattrs.push_back(CPUID::supportsSSE3() ? "+sse3" : "-sse3"); |
| 946 | mattrs.push_back(CPUID::supportsSSSE3() ? "+ssse3" : "-ssse3"); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 947 | mattrs.push_back(CPUID::supportsSSE4_1() ? "+sse4.1" : "-sse4.1"); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 948 | #elif defined(__arm__) |
| 949 | #if __ARM_ARCH >= 8 |
| 950 | mattrs.push_back("+armv8-a"); |
| 951 | #else |
| 952 | // armv7-a requires compiler-rt routines; otherwise, compiled kernel |
| 953 | // might fail to link. |
| 954 | #endif |
| 955 | #endif |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 956 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 957 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 958 | llvm::TargetOptions targetOpts; |
Nicolas Capens | a764381 | 2018-09-13 14:20:06 -0400 | [diff] [blame] | 959 | targetOpts.UnsafeFPMath = false; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 960 | // targetOpts.NoInfsFPMath = true; |
| 961 | // targetOpts.NoNaNsFPMath = true; |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 962 | |
| 963 | if(!::reactorJIT) |
| 964 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 965 | ::reactorJIT = new LLVMReactorJIT(arch, mattrs, targetOpts); |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | ::reactorJIT->startSession(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 969 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 970 | if(!::builder) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 971 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 972 | ::builder = new llvm::IRBuilder<>(*::context); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
| 976 | Nucleus::~Nucleus() |
| 977 | { |
Ben Clayton | 90cb260 | 2019-05-23 14:42:32 +0100 | [diff] [blame] | 978 | #ifdef ENABLE_RR_DEBUG_INFO |
| 979 | debugInfo.reset(nullptr); |
| 980 | #endif // ENABLE_RR_DEBUG_INFO |
| 981 | |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 982 | ::reactorJIT->endSession(); |
Nicolas Capens | b7ea984 | 2015-04-01 10:54:59 -0400 | [diff] [blame] | 983 | |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 984 | ::codegenMutex.unlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 985 | } |
| 986 | |
Chris Forbes | 878d4b0 | 2019-01-21 10:48:35 -0800 | [diff] [blame] | 987 | Routine *Nucleus::acquireRoutine(const char *name, bool runOptimizations) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 988 | { |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 989 | if(::builder->GetInsertBlock()->empty() || !::builder->GetInsertBlock()->back().isTerminator()) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 990 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 991 | llvm::Type *type = ::function->getReturnType(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 992 | |
| 993 | if(type->isVoidTy()) |
| 994 | { |
| 995 | createRetVoid(); |
| 996 | } |
| 997 | else |
| 998 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 999 | createRet(V(llvm::UndefValue::get(type))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1000 | } |
| 1001 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1002 | |
Ben Clayton | 97c13ad | 2019-05-02 11:59:30 +0100 | [diff] [blame] | 1003 | #ifdef ENABLE_RR_DEBUG_INFO |
| 1004 | if (debugInfo != nullptr) |
| 1005 | { |
| 1006 | debugInfo->Finalize(); |
| 1007 | } |
| 1008 | #endif // ENABLE_RR_DEBUG_INFO |
| 1009 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1010 | if(false) |
| 1011 | { |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 1012 | std::error_code error; |
| 1013 | 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] | 1014 | ::module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1015 | } |
| 1016 | |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 1017 | // FIXME: Disable for release builds once heavy development is over. |
| 1018 | bool verifyIR = true; |
| 1019 | if(verifyIR) |
| 1020 | { |
| 1021 | llvm::legacy::PassManager pm; |
| 1022 | pm.add(llvm::createVerifierPass()); |
| 1023 | pm.run(*::module); |
| 1024 | } |
| 1025 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1026 | if(runOptimizations) |
| 1027 | { |
| 1028 | optimize(); |
| 1029 | } |
| 1030 | |
| 1031 | if(false) |
| 1032 | { |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 1033 | std::error_code error; |
| 1034 | 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] | 1035 | ::module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1036 | } |
| 1037 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 1038 | LLVMRoutine *routine = ::reactorJIT->acquireRoutine(&::function, 1); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1039 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1040 | return routine; |
| 1041 | } |
| 1042 | |
| 1043 | void Nucleus::optimize() |
| 1044 | { |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1045 | ::reactorJIT->optimize(::module); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1046 | } |
| 1047 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1048 | Value *Nucleus::allocateStackVariable(Type *type, int arraySize) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1049 | { |
| 1050 | // Need to allocate it in the entry block for mem2reg to work |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 1051 | llvm::BasicBlock &entryBlock = ::function->getEntryBlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1052 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1053 | llvm::Instruction *declaration; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1054 | |
| 1055 | if(arraySize) |
| 1056 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1057 | declaration = new llvm::AllocaInst(T(type), 0, V(Nucleus::createConstantInt(arraySize))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1058 | } |
| 1059 | else |
| 1060 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1061 | declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value*)nullptr); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | entryBlock.getInstList().push_front(declaration); |
| 1065 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1066 | return V(declaration); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | BasicBlock *Nucleus::createBasicBlock() |
| 1070 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1071 | return B(llvm::BasicBlock::Create(*::context, "", ::function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | BasicBlock *Nucleus::getInsertBlock() |
| 1075 | { |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 1076 | return B(::builder->GetInsertBlock()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1080 | { |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1081 | // assert(::builder->GetInsertBlock()->back().isTerminator()); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1082 | |
| 1083 | Variable::materializeAll(); |
| 1084 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1085 | ::builder->SetInsertPoint(B(basicBlock)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1086 | } |
| 1087 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1088 | void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1089 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1090 | llvm::FunctionType *functionType = llvm::FunctionType::get(T(ReturnType), T(Params), false); |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1091 | ::function = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, "", ::module); |
| 1092 | ::function->setCallingConv(llvm::CallingConv::C); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1093 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 1094 | #if defined(_WIN32) |
Nicolas Capens | 52551d1 | 2018-09-13 14:30:56 -0400 | [diff] [blame] | 1095 | // FIXME(capn): |
| 1096 | // On Windows, stack memory is committed in increments of 4 kB pages, with the last page |
| 1097 | // having a trap which allows the OS to grow the stack. For functions with a stack frame |
| 1098 | // larger than 4 kB this can cause an issue when a variable is accessed beyond the guard |
| 1099 | // page. Therefore the compiler emits a call to __chkstk in the function prolog to probe |
| 1100 | // the stack and ensure all pages have been committed. This is currently broken in LLVM |
| 1101 | // JIT, but we can prevent emitting the stack probe call: |
| 1102 | ::function->addFnAttr("stack-probe-size", "1048576"); |
| 1103 | #endif |
| 1104 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1105 | #ifdef ENABLE_RR_DEBUG_INFO |
| 1106 | ::debugInfo = std::unique_ptr<DebugInfo>(new DebugInfo(::builder, ::context, ::module, ::function)); |
| 1107 | #endif // ENABLE_RR_DEBUG_INFO |
| 1108 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1109 | ::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "", ::function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1110 | } |
| 1111 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1112 | Value *Nucleus::getArgument(unsigned int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1113 | { |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1114 | llvm::Function::arg_iterator args = ::function->arg_begin(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1115 | |
| 1116 | while(index) |
| 1117 | { |
| 1118 | args++; |
| 1119 | index--; |
| 1120 | } |
| 1121 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1122 | return V(&*args); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1123 | } |
| 1124 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1125 | void Nucleus::createRetVoid() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1126 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1127 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1128 | |
Ben Clayton | c958b17 | 2019-05-02 12:20:59 +0100 | [diff] [blame] | 1129 | ASSERT_MSG(::function->getReturnType() == T(Void::getType()), "Return type mismatch"); |
| 1130 | |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1131 | // Code generated after this point is unreachable, so any variables |
| 1132 | // being read can safely return an undefined value. We have to avoid |
| 1133 | // materializing variables after the terminator ret instruction. |
| 1134 | Variable::killUnmaterialized(); |
| 1135 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1136 | ::builder->CreateRetVoid(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1137 | } |
| 1138 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1139 | void Nucleus::createRet(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1140 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1141 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1142 | |
Ben Clayton | c958b17 | 2019-05-02 12:20:59 +0100 | [diff] [blame] | 1143 | ASSERT_MSG(::function->getReturnType() == V(v)->getType(), "Return type mismatch"); |
| 1144 | |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1145 | // Code generated after this point is unreachable, so any variables |
| 1146 | // being read can safely return an undefined value. We have to avoid |
| 1147 | // materializing variables after the terminator ret instruction. |
| 1148 | Variable::killUnmaterialized(); |
| 1149 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1150 | ::builder->CreateRet(V(v)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1151 | } |
| 1152 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1153 | void Nucleus::createBr(BasicBlock *dest) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1154 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1155 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1156 | Variable::materializeAll(); |
| 1157 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1158 | ::builder->CreateBr(B(dest)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1159 | } |
| 1160 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1161 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1162 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1163 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1164 | Variable::materializeAll(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1165 | ::builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1169 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1170 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1171 | return V(::builder->CreateAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1175 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1176 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1177 | return V(::builder->CreateSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1181 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1182 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1183 | return V(::builder->CreateMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1187 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1188 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1189 | return V(::builder->CreateUDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1193 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1194 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1195 | return V(::builder->CreateSDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1199 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1200 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1201 | return V(::builder->CreateFAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1205 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1206 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1207 | return V(::builder->CreateFSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1211 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1212 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1213 | return V(::builder->CreateFMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1217 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1218 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1219 | return V(::builder->CreateFDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1223 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1224 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1225 | return V(::builder->CreateURem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1229 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1230 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1231 | return V(::builder->CreateSRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1235 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1236 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1237 | return V(::builder->CreateFRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1241 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1242 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1243 | return V(::builder->CreateShl(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1247 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1248 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1249 | return V(::builder->CreateLShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1253 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1254 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1255 | return V(::builder->CreateAShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1259 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1260 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1261 | return V(::builder->CreateAnd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1265 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1266 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1267 | return V(::builder->CreateOr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1271 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1272 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1273 | return V(::builder->CreateXor(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1274 | } |
| 1275 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1276 | Value *Nucleus::createNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1277 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1278 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1279 | return V(::builder->CreateNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1280 | } |
| 1281 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1282 | Value *Nucleus::createFNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1283 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1284 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1285 | return V(::builder->CreateFNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1286 | } |
| 1287 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1288 | Value *Nucleus::createNot(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1289 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1290 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1291 | return V(::builder->CreateNot(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1292 | } |
| 1293 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1294 | 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] | 1295 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1296 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1297 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1298 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1299 | case Type_v2i32: |
| 1300 | case Type_v4i16: |
| 1301 | case Type_v8i8: |
| 1302 | case Type_v2f32: |
| 1303 | return createBitCast( |
| 1304 | createInsertElement( |
| 1305 | V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1306 | createLoad(createBitCast(ptr, Pointer<Long>::getType()), Long::getType(), isVolatile, alignment, atomic, memoryOrder), |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1307 | 0), |
| 1308 | type); |
| 1309 | case Type_v2i16: |
| 1310 | case Type_v4i8: |
| 1311 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1312 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1313 | 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] | 1314 | 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] | 1315 | i = createZExt(i, Long::getType()); |
| 1316 | Value *v = createInsertElement(u, i, 0); |
| 1317 | return createBitCast(v, type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1318 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1319 | // Fallthrough to non-emulated case. |
| 1320 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1321 | { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1322 | auto elTy = T(type); |
| 1323 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
| 1324 | if (atomic && !(elTy->isIntegerTy() || elTy->isPointerTy() || elTy->isFloatTy())) |
| 1325 | { |
| 1326 | // atomic load operand must have integer, pointer, or floating point type |
| 1327 | // Fall back to using: |
| 1328 | // void __atomic_load(size_t size, void *ptr, void *ret, int ordering) |
| 1329 | auto sizetTy = ::llvm::IntegerType::get(*::context, sizeof(size_t) * 8); |
| 1330 | auto intTy = ::llvm::IntegerType::get(*::context, sizeof(int) * 8); |
| 1331 | auto i8Ty = ::llvm::Type::getInt8Ty(*::context); |
| 1332 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 1333 | auto voidTy = ::llvm::Type::getVoidTy(*::context); |
| 1334 | auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false); |
| 1335 | auto func = ::module->getOrInsertFunction("__atomic_load", funcTy); |
| 1336 | auto size = ::module->getDataLayout().getTypeStoreSize(elTy); |
| 1337 | auto out = allocateStackVariable(type); |
| 1338 | ::builder->CreateCall(func, { |
| 1339 | ::llvm::ConstantInt::get(sizetTy, size), |
| 1340 | ::builder->CreatePointerCast(V(ptr), i8PtrTy), |
| 1341 | ::builder->CreatePointerCast(V(out), i8PtrTy), |
| 1342 | ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))), |
| 1343 | }); |
| 1344 | return V(::builder->CreateLoad(V(out))); |
| 1345 | } |
| 1346 | else |
| 1347 | { |
| 1348 | auto load = new llvm::LoadInst(V(ptr), "", isVolatile, alignment); |
| 1349 | load->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1350 | return V(::builder->Insert(load)); |
| 1351 | } |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1352 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1353 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1354 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1355 | return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1356 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1357 | } |
| 1358 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1359 | 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] | 1360 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1361 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1362 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1363 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1364 | case Type_v2i32: |
| 1365 | case Type_v4i16: |
| 1366 | case Type_v8i8: |
| 1367 | case Type_v2f32: |
| 1368 | createStore( |
| 1369 | createExtractElement( |
| 1370 | createBitCast(value, T(llvm::VectorType::get(T(Long::getType()), 2))), Long::getType(), 0), |
| 1371 | createBitCast(ptr, Pointer<Long>::getType()), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1372 | Long::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1373 | return value; |
| 1374 | case Type_v2i16: |
| 1375 | case Type_v4i8: |
| 1376 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1377 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1378 | createStore( |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1379 | createExtractElement(createBitCast(value, Int4::getType()), Int::getType(), 0), |
| 1380 | createBitCast(ptr, Pointer<Int>::getType()), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1381 | Int::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1382 | return value; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1383 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1384 | // Fallthrough to non-emulated case. |
| 1385 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1386 | { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1387 | auto elTy = T(type); |
| 1388 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
| 1389 | if (atomic && !(elTy->isIntegerTy() || elTy->isPointerTy() || elTy->isFloatTy())) |
| 1390 | { |
| 1391 | // atomic store operand must have integer, pointer, or floating point type |
| 1392 | // Fall back to using: |
| 1393 | // void __atomic_store(size_t size, void *ptr, void *val, int ordering) |
| 1394 | auto sizetTy = ::llvm::IntegerType::get(*::context, sizeof(size_t) * 8); |
| 1395 | auto intTy = ::llvm::IntegerType::get(*::context, sizeof(int) * 8); |
| 1396 | auto i8Ty = ::llvm::Type::getInt8Ty(*::context); |
| 1397 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 1398 | auto voidTy = ::llvm::Type::getVoidTy(*::context); |
| 1399 | auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false); |
| 1400 | auto func = ::module->getOrInsertFunction("__atomic_store", funcTy); |
| 1401 | auto size = ::module->getDataLayout().getTypeStoreSize(elTy); |
| 1402 | auto copy = allocateStackVariable(type); |
| 1403 | ::builder->CreateStore(V(value), V(copy)); |
| 1404 | ::builder->CreateCall(func, { |
| 1405 | ::llvm::ConstantInt::get(sizetTy, size), |
| 1406 | ::builder->CreatePointerCast(V(ptr), i8PtrTy), |
| 1407 | ::builder->CreatePointerCast(V(copy), i8PtrTy), |
| 1408 | ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))), |
| 1409 | }); |
| 1410 | } |
| 1411 | else |
| 1412 | { |
| 1413 | auto store = ::builder->Insert(new llvm::StoreInst(V(value), V(ptr), isVolatile, alignment)); |
| 1414 | store->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1415 | } |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1416 | |
| 1417 | return value; |
| 1418 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1419 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1420 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1421 | return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1422 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1423 | } |
| 1424 | |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1425 | Value *Nucleus::createGather(Value *base, Type *elTy, Value *offsets, Value *mask, unsigned int alignment) |
| 1426 | { |
| 1427 | ASSERT(V(base)->getType()->isPointerTy()); |
| 1428 | ASSERT(V(offsets)->getType()->isVectorTy()); |
| 1429 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1430 | |
| 1431 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
| 1432 | auto i1Ty = ::llvm::Type::getInt1Ty(*::context); |
| 1433 | auto i32Ty = ::llvm::Type::getInt32Ty(*::context); |
| 1434 | auto i8Ty = ::llvm::Type::getInt8Ty(*::context); |
| 1435 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 1436 | auto elPtrTy = T(elTy)->getPointerTo(); |
| 1437 | auto elVecTy = ::llvm::VectorType::get(T(elTy), numEls); |
| 1438 | auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls); |
| 1439 | auto i8Base = ::builder->CreatePointerCast(V(base), i8PtrTy); |
| 1440 | auto i8Ptrs = ::builder->CreateGEP(i8Base, V(offsets)); |
| 1441 | auto elPtrs = ::builder->CreatePointerCast(i8Ptrs, elPtrVecTy); |
| 1442 | auto i8Mask = ::builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
| 1443 | auto passthrough = ::llvm::Constant::getNullValue(elVecTy); |
| 1444 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
| 1445 | auto func = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::masked_gather, { elVecTy, elPtrVecTy } ); |
| 1446 | return V(::builder->CreateCall(func, { elPtrs, align, i8Mask, passthrough })); |
| 1447 | } |
| 1448 | |
| 1449 | void Nucleus::createScatter(Value *base, Value *val, Value *offsets, Value *mask, unsigned int alignment) |
| 1450 | { |
| 1451 | ASSERT(V(base)->getType()->isPointerTy()); |
| 1452 | ASSERT(V(val)->getType()->isVectorTy()); |
| 1453 | ASSERT(V(offsets)->getType()->isVectorTy()); |
| 1454 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1455 | |
| 1456 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
| 1457 | auto i1Ty = ::llvm::Type::getInt1Ty(*::context); |
| 1458 | auto i32Ty = ::llvm::Type::getInt32Ty(*::context); |
| 1459 | auto i8Ty = ::llvm::Type::getInt8Ty(*::context); |
| 1460 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 1461 | auto elVecTy = V(val)->getType(); |
| 1462 | auto elTy = elVecTy->getVectorElementType(); |
| 1463 | auto elPtrTy = elTy->getPointerTo(); |
| 1464 | auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls); |
| 1465 | auto i8Base = ::builder->CreatePointerCast(V(base), i8PtrTy); |
| 1466 | auto i8Ptrs = ::builder->CreateGEP(i8Base, V(offsets)); |
| 1467 | auto elPtrs = ::builder->CreatePointerCast(i8Ptrs, elPtrVecTy); |
| 1468 | auto i8Mask = ::builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
| 1469 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
| 1470 | auto func = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::masked_scatter, { elVecTy, elPtrVecTy } ); |
| 1471 | ::builder->CreateCall(func, { V(val), elPtrs, align, i8Mask }); |
| 1472 | } |
| 1473 | |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 1474 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 1475 | { |
| 1476 | ::builder->CreateFence(atomicOrdering(true, memoryOrder)); |
| 1477 | } |
| 1478 | |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1479 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1480 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1481 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1482 | ASSERT(V(ptr)->getType()->getContainedType(0) == T(type)); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1483 | if(sizeof(void*) == 8) |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1484 | { |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1485 | // LLVM manual: "When indexing into an array, pointer or vector, |
| 1486 | // integers of any width are allowed, and they are not required to |
| 1487 | // be constant. These integers are treated as signed values where |
| 1488 | // relevant." |
| 1489 | // |
| 1490 | // Thus if we want indexes to be treated as unsigned we have to |
| 1491 | // zero-extend them ourselves. |
| 1492 | // |
| 1493 | // Note that this is not because we want to address anywhere near |
| 1494 | // 4 GB of data. Instead this is important for performance because |
| 1495 | // x86 supports automatic zero-extending of 32-bit registers to |
| 1496 | // 64-bit. Thus when indexing into an array using a uint32 is |
| 1497 | // actually faster than an int32. |
| 1498 | index = unsignedIndex ? |
| 1499 | createZExt(index, Long::getType()) : |
| 1500 | createSExt(index, Long::getType()); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1501 | } |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1502 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1503 | // For non-emulated types we can rely on LLVM's GEP to calculate the |
| 1504 | // effective address correctly. |
| 1505 | if(asInternalType(type) == Type_LLVM) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1506 | { |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1507 | return V(::builder->CreateGEP(V(ptr), V(index))); |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1508 | } |
| 1509 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1510 | // For emulated types we have to multiply the index by the intended |
| 1511 | // type size ourselves to obain the byte offset. |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1512 | index = (sizeof(void*) == 8) ? |
| 1513 | createMul(index, createConstantLong((int64_t)typeSize(type))) : |
| 1514 | createMul(index, createConstantInt((int)typeSize(type))); |
| 1515 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1516 | // Cast to a byte pointer, apply the byte offset, and cast back to the |
| 1517 | // original pointer type. |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1518 | return createBitCast( |
| 1519 | V(::builder->CreateGEP(V(createBitCast(ptr, T(llvm::PointerType::get(T(Byte::getType()), 0)))), V(index))), |
| 1520 | T(llvm::PointerType::get(T(type), 0))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1521 | } |
| 1522 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1523 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1524 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1525 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1526 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Add, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1527 | } |
| 1528 | |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 1529 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1530 | { |
| 1531 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1532 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Sub, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1533 | } |
| 1534 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1535 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1536 | { |
| 1537 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1538 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::And, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1539 | } |
| 1540 | |
| 1541 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1542 | { |
| 1543 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1544 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Or, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1545 | } |
| 1546 | |
| 1547 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1548 | { |
| 1549 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1550 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xor, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1551 | } |
| 1552 | |
| 1553 | Value *Nucleus::createAtomicMin(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1554 | { |
| 1555 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1556 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Min, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1557 | } |
| 1558 | |
| 1559 | Value *Nucleus::createAtomicMax(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1560 | { |
| 1561 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1562 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Max, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1563 | } |
| 1564 | |
Chris Forbes | f31bdad | 2019-05-23 14:58:08 -0700 | [diff] [blame] | 1565 | Value *Nucleus::createAtomicUMin(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1566 | { |
| 1567 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1568 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMin, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1569 | } |
| 1570 | |
| 1571 | Value *Nucleus::createAtomicUMax(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1572 | { |
| 1573 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1574 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMax, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1575 | } |
| 1576 | |
| 1577 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1578 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1579 | { |
| 1580 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1581 | 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] | 1582 | } |
| 1583 | |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 1584 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1585 | { |
| 1586 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | c9ca99e | 2019-04-19 07:53:34 -0700 | [diff] [blame] | 1587 | // 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] | 1588 | return V(::builder->CreateExtractValue( |
| 1589 | ::builder->CreateAtomicCmpXchg(V(ptr), V(compare), V(value), atomicOrdering(true, memoryOrderEqual), atomicOrdering(true, memoryOrderUnequal)), |
| 1590 | llvm::ArrayRef<unsigned>(0u))); |
| 1591 | } |
| 1592 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1593 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1594 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1595 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1596 | return V(::builder->CreateTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1597 | } |
| 1598 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1599 | Value *Nucleus::createZExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1600 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1601 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1602 | return V(::builder->CreateZExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1603 | } |
| 1604 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1605 | Value *Nucleus::createSExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1606 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1607 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1608 | return V(::builder->CreateSExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1609 | } |
| 1610 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1611 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1612 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1613 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1614 | return V(::builder->CreateFPToSI(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1615 | } |
| 1616 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1617 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1618 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1619 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1620 | return V(::builder->CreateSIToFP(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1621 | } |
| 1622 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1623 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1624 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1625 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1626 | return V(::builder->CreateFPTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1627 | } |
| 1628 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1629 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1630 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1631 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1632 | return V(::builder->CreateFPExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1633 | } |
| 1634 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1635 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1636 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1637 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1638 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1639 | // support for casting between scalars and wide vectors. Emulate them by writing to the stack and |
| 1640 | // reading back as the destination type. |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1641 | if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1642 | { |
| 1643 | Value *readAddress = allocateStackVariable(destType); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1644 | Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0))); |
| 1645 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1646 | return createLoad(readAddress, destType); |
| 1647 | } |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1648 | else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1649 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1650 | Value *writeAddress = allocateStackVariable(T(V(v)->getType())); |
| 1651 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1652 | Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0))); |
| 1653 | return createLoad(readAddress, destType); |
| 1654 | } |
| 1655 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1656 | return V(::builder->CreateBitCast(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1657 | } |
| 1658 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1659 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1660 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1661 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1662 | return V(::builder->CreateICmpEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1666 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1667 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1668 | return V(::builder->CreateICmpNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1672 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1673 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1674 | return V(::builder->CreateICmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1678 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1679 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1680 | return V(::builder->CreateICmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1684 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1685 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1686 | return V(::builder->CreateICmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1690 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1691 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1692 | return V(::builder->CreateICmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1696 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1697 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1698 | return V(::builder->CreateICmpSGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1702 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1703 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1704 | return V(::builder->CreateICmpSGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1708 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1709 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1710 | return V(::builder->CreateICmpSLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1714 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1715 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1716 | return V(::builder->CreateICmpSLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1720 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1721 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1722 | return V(::builder->CreateFCmpOEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1726 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1727 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1728 | return V(::builder->CreateFCmpOGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1732 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1733 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1734 | return V(::builder->CreateFCmpOGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1738 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1739 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1740 | return V(::builder->CreateFCmpOLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1744 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1745 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1746 | return V(::builder->CreateFCmpOLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1750 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1751 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1752 | return V(::builder->CreateFCmpONE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1756 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1757 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1758 | return V(::builder->CreateFCmpORD(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1762 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1763 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1764 | return V(::builder->CreateFCmpUNO(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1768 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1769 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1770 | return V(::builder->CreateFCmpUEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1774 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1775 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1776 | return V(::builder->CreateFCmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1780 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1781 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1782 | return V(::builder->CreateFCmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1786 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1787 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1788 | return V(::builder->CreateFCmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1792 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1793 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1794 | return V(::builder->CreateFCmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1798 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1799 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 71008d8 | 2019-03-05 17:17:59 +0000 | [diff] [blame] | 1800 | return V(::builder->CreateFCmpUNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1801 | } |
| 1802 | |
Nicolas Capens | e95d534 | 2016-09-30 11:37:28 -0400 | [diff] [blame] | 1803 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1804 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1805 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1806 | ASSERT(V(vector)->getType()->getContainedType(0) == T(type)); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1807 | return V(::builder->CreateExtractElement(V(vector), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1811 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1812 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1813 | return V(::builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1814 | } |
| 1815 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1816 | Value *Nucleus::createShuffleVector(Value *v1, Value *v2, const int *select) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1817 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1818 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1819 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1820 | int size = llvm::cast<llvm::VectorType>(V(v1)->getType())->getNumElements(); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 1821 | const int maxSize = 16; |
| 1822 | llvm::Constant *swizzle[maxSize]; |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1823 | ASSERT(size <= maxSize); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 1824 | |
| 1825 | for(int i = 0; i < size; i++) |
| 1826 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1827 | swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), select[i]); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(swizzle, size)); |
| 1831 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1832 | return V(::builder->CreateShuffleVector(V(v1), V(v2), shuffle)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1833 | } |
| 1834 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1835 | Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1836 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1837 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1838 | return V(::builder->CreateSelect(V(c), V(ifTrue), V(ifFalse))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1839 | } |
| 1840 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1841 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1842 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1843 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1844 | return reinterpret_cast<SwitchCases*>(::builder->CreateSwitch(V(control), B(defaultBranch), numCases)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1845 | } |
| 1846 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1847 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1848 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1849 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1850 | llvm::SwitchInst *sw = reinterpret_cast<llvm::SwitchInst *>(switchCases); |
| 1851 | 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] | 1852 | } |
| 1853 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1854 | void Nucleus::createUnreachable() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1855 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1856 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1857 | ::builder->CreateUnreachable(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1858 | } |
| 1859 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1860 | Type *Nucleus::getPointerType(Type *ElementType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1861 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1862 | return T(llvm::PointerType::get(T(ElementType), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1863 | } |
| 1864 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1865 | Value *Nucleus::createNullValue(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1866 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1867 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1868 | return V(llvm::Constant::getNullValue(T(Ty))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1869 | } |
| 1870 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1871 | Value *Nucleus::createConstantLong(int64_t i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1872 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1873 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1874 | return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1875 | } |
| 1876 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1877 | Value *Nucleus::createConstantInt(int i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1878 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1879 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1880 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1881 | } |
| 1882 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1883 | Value *Nucleus::createConstantInt(unsigned int i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1884 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1885 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1886 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1887 | } |
| 1888 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1889 | Value *Nucleus::createConstantBool(bool b) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1890 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1891 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1892 | return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(*::context), b)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1893 | } |
| 1894 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1895 | Value *Nucleus::createConstantByte(signed char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1896 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1897 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1898 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1899 | } |
| 1900 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1901 | Value *Nucleus::createConstantByte(unsigned char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1902 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1903 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1904 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1905 | } |
| 1906 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1907 | Value *Nucleus::createConstantShort(short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1908 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1909 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1910 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1911 | } |
| 1912 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1913 | Value *Nucleus::createConstantShort(unsigned short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1914 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1915 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1916 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1917 | } |
| 1918 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1919 | Value *Nucleus::createConstantFloat(float x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1920 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1921 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1922 | return V(llvm::ConstantFP::get(T(Float::getType()), x)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1923 | } |
| 1924 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1925 | Value *Nucleus::createNullPointer(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1926 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1927 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1928 | return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1929 | } |
| 1930 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1931 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1932 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1933 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1934 | const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. |
| 1935 | 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] | 1936 | ASSERT(numElements <= 16 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1937 | llvm::Constant *constantVector[16]; |
| 1938 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1939 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1940 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1941 | constantVector[i] = llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1942 | } |
| 1943 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1944 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 1948 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1949 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1950 | const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. |
| 1951 | 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] | 1952 | ASSERT(numElements <= 8 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1953 | llvm::Constant *constantVector[8]; |
| 1954 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1955 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1956 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1957 | constantVector[i] = llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1958 | } |
| 1959 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1960 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1961 | } |
| 1962 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1963 | Type *Void::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1964 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1965 | return T(llvm::Type::getVoidTy(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1966 | } |
| 1967 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1968 | Type *Bool::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1969 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1970 | return T(llvm::Type::getInt1Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1971 | } |
| 1972 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1973 | Type *Byte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1974 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1975 | return T(llvm::Type::getInt8Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1976 | } |
| 1977 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1978 | Type *SByte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1979 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1980 | return T(llvm::Type::getInt8Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1981 | } |
| 1982 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1983 | Type *Short::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1984 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1985 | return T(llvm::Type::getInt16Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1986 | } |
| 1987 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1988 | Type *UShort::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1989 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1990 | return T(llvm::Type::getInt16Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1991 | } |
| 1992 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1993 | Type *Byte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1994 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1995 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1996 | } |
| 1997 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1998 | Type *SByte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1999 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2000 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2001 | } |
| 2002 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2003 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2004 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2005 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2006 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2007 | return x86::paddusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2008 | #else |
| 2009 | return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 2010 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2011 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2012 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2013 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
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 | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2016 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2017 | return x86::psubusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2018 | #else |
| 2019 | return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 2020 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2021 | } |
| 2022 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2023 | RValue<Int> SignMask(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2024 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2025 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2026 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2027 | return x86::pmovmskb(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2028 | #else |
| 2029 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2030 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2031 | } |
| 2032 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2033 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2034 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2035 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2036 | // return x86::pcmpgtb(x, y); // FIXME: Signedness |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2037 | //#else |
| 2038 | // return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2039 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2040 | // } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2041 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2042 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2043 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2044 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2045 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2046 | return x86::pcmpeqb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2047 | #else |
| 2048 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2049 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2050 | } |
| 2051 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2052 | Type *Byte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2053 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2054 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2055 | } |
| 2056 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2057 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2058 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2059 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2060 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2061 | return x86::paddsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2062 | #else |
| 2063 | return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 2064 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2065 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2066 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2067 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
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(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2070 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2071 | return x86::psubsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2072 | #else |
| 2073 | return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 2074 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2075 | } |
| 2076 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2077 | RValue<Int> SignMask(RValue<SByte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2078 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2079 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2080 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2081 | return x86::pmovmskb(As<Byte8>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2082 | #else |
| 2083 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2084 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2085 | } |
| 2086 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2087 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2088 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2089 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2090 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2091 | return x86::pcmpgtb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2092 | #else |
| 2093 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2094 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2095 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2096 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2097 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
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(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2100 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2101 | return x86::pcmpeqb(As<Byte8>(x), As<Byte8>(y)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2102 | #else |
| 2103 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2104 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2105 | } |
| 2106 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2107 | Type *SByte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2108 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2109 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2110 | } |
| 2111 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2112 | Type *Byte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2113 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2114 | return T(llvm::VectorType::get(T(Byte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2115 | } |
| 2116 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2117 | Type *SByte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2118 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2119 | return T(llvm::VectorType::get(T(SByte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2120 | } |
| 2121 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2122 | Type *Short2::getType() |
| 2123 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2124 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2125 | } |
| 2126 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2127 | Type *UShort2::getType() |
| 2128 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2129 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2130 | } |
| 2131 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2132 | Short4::Short4(RValue<Int4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2133 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2134 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2135 | int select[8] = {0, 2, 4, 6, 0, 2, 4, 6}; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2136 | Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType()); |
| 2137 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2138 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2139 | Value *short4 = As<Short4>(Int2(As<Int4>(packed))).value; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2140 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2141 | storeValue(short4); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2142 | } |
| 2143 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2144 | // Short4::Short4(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2145 | // { |
| 2146 | // } |
| 2147 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2148 | Short4::Short4(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2149 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2150 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2151 | Int4 v4i32 = Int4(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2152 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2153 | v4i32 = As<Int4>(x86::packssdw(v4i32, v4i32)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2154 | #else |
| 2155 | Value *v = v4i32.loadValue(); |
| 2156 | v4i32 = As<Int4>(V(lowerPack(V(v), V(v), true))); |
| 2157 | #endif |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2158 | |
| 2159 | storeValue(As<Short4>(Int2(v4i32)).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2160 | } |
| 2161 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2162 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2163 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2164 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2165 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2166 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2167 | |
| 2168 | return x86::psllw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2169 | #else |
| 2170 | return As<Short4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2171 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2172 | } |
| 2173 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2174 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2175 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2176 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2177 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2178 | return x86::psraw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2179 | #else |
| 2180 | return As<Short4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2181 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2182 | } |
| 2183 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2184 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2185 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2186 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2187 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2188 | return x86::pmaxsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2189 | #else |
| 2190 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
| 2191 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2192 | } |
| 2193 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2194 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2195 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2196 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2197 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2198 | return x86::pminsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2199 | #else |
| 2200 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
| 2201 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2202 | } |
| 2203 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2204 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2205 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2206 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2207 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2208 | return x86::paddsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2209 | #else |
| 2210 | return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 2211 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2212 | } |
| 2213 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2214 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2215 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2216 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2217 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2218 | return x86::psubsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2219 | #else |
| 2220 | return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 2221 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2222 | } |
| 2223 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2224 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2225 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2226 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2227 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2228 | return x86::pmulhw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2229 | #else |
| 2230 | return As<Short4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2231 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2232 | } |
| 2233 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2234 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2235 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2236 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2237 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2238 | return x86::pmaddwd(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2239 | #else |
| 2240 | return As<Int2>(V(lowerMulAdd(V(x.value), V(y.value)))); |
| 2241 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2242 | } |
| 2243 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2244 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2245 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2246 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2247 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2248 | auto result = x86::packsswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2249 | #else |
| 2250 | auto result = V(lowerPack(V(x.value), V(y.value), true)); |
| 2251 | #endif |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2252 | return As<SByte8>(Swizzle(As<Int4>(result), 0x88)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2253 | } |
| 2254 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2255 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 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__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2259 | auto result = x86::packuswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2260 | #else |
| 2261 | auto result = V(lowerPack(V(x.value), V(y.value), false)); |
| 2262 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2263 | return As<Byte8>(Swizzle(As<Int4>(result), 0x88)); |
| 2264 | } |
| 2265 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2266 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2267 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2268 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2269 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2270 | return x86::pcmpgtw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2271 | #else |
| 2272 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); |
| 2273 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2274 | } |
| 2275 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2276 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2277 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2278 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2279 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2280 | return x86::pcmpeqw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2281 | #else |
| 2282 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); |
| 2283 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2284 | } |
| 2285 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2286 | Type *Short4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2287 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2288 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2289 | } |
| 2290 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2291 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2292 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2293 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2294 | if(saturate) |
| 2295 | { |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 2296 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2297 | if(CPUID::supportsSSE4_1()) |
| 2298 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2299 | Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2300 | *this = As<Short4>(PackUnsigned(int4, int4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2301 | } |
| 2302 | else |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 2303 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2304 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2305 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2306 | } |
| 2307 | } |
| 2308 | else |
| 2309 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2310 | *this = Short4(Int4(cast)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2311 | } |
| 2312 | } |
| 2313 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2314 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2315 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2316 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2317 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2318 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2319 | |
| 2320 | return As<UShort4>(x86::psllw(As<Short4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2321 | #else |
| 2322 | return As<UShort4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2323 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2324 | } |
| 2325 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2326 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
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(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2329 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2330 | // return RValue<Short4>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 2331 | |
| 2332 | return x86::psrlw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2333 | #else |
| 2334 | return As<UShort4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2335 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2336 | } |
| 2337 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2338 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2339 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2340 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2341 | 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] | 2342 | } |
| 2343 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2344 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2345 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2346 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2347 | 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] | 2348 | } |
| 2349 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2350 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2351 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2352 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2353 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2354 | return x86::paddusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2355 | #else |
| 2356 | return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 2357 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2358 | } |
| 2359 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2360 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2361 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2362 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2363 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2364 | return x86::psubusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2365 | #else |
| 2366 | return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 2367 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2368 | } |
| 2369 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2370 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2371 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2372 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2373 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2374 | return x86::pmulhuw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2375 | #else |
| 2376 | return As<UShort4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2377 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2378 | } |
| 2379 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2380 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2381 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2382 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2383 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2384 | return x86::pavgw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2385 | #else |
| 2386 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
| 2387 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2388 | } |
| 2389 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2390 | Type *UShort4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2391 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2392 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2393 | } |
| 2394 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2395 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2396 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2397 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2398 | #if defined(__i386__) || defined(__x86_64__) |
| 2399 | return x86::psllw(lhs, rhs); |
| 2400 | #else |
| 2401 | return As<Short8>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2402 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2403 | } |
| 2404 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2405 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2406 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2407 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2408 | #if defined(__i386__) || defined(__x86_64__) |
| 2409 | return x86::psraw(lhs, rhs); |
| 2410 | #else |
| 2411 | return As<Short8>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2412 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2413 | } |
| 2414 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2415 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2416 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2417 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2418 | #if defined(__i386__) || defined(__x86_64__) |
| 2419 | return x86::pmaddwd(x, y); |
| 2420 | #else |
| 2421 | return As<Int4>(V(lowerMulAdd(V(x.value), V(y.value)))); |
| 2422 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2423 | } |
| 2424 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2425 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2426 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2427 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2428 | #if defined(__i386__) || defined(__x86_64__) |
| 2429 | return x86::pmulhw(x, y); |
| 2430 | #else |
| 2431 | return As<Short8>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2432 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2433 | } |
| 2434 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2435 | Type *Short8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2436 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2437 | return T(llvm::VectorType::get(T(Short::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2438 | } |
| 2439 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2440 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2441 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2442 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2443 | #if defined(__i386__) || defined(__x86_64__) |
| 2444 | return As<UShort8>(x86::psllw(As<Short8>(lhs), rhs)); |
| 2445 | #else |
| 2446 | return As<UShort8>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2447 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2448 | } |
| 2449 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2450 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2451 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2452 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2453 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2454 | return x86::psrlw(lhs, rhs); // FIXME: Fallback required |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2455 | #else |
| 2456 | return As<UShort8>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2457 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2458 | } |
| 2459 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2460 | 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] | 2461 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2462 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2463 | int pshufb[16] = |
| 2464 | { |
| 2465 | select0 + 0, |
| 2466 | select0 + 1, |
| 2467 | select1 + 0, |
| 2468 | select1 + 1, |
| 2469 | select2 + 0, |
| 2470 | select2 + 1, |
| 2471 | select3 + 0, |
| 2472 | select3 + 1, |
| 2473 | select4 + 0, |
| 2474 | select4 + 1, |
| 2475 | select5 + 0, |
| 2476 | select5 + 1, |
| 2477 | select6 + 0, |
| 2478 | select6 + 1, |
| 2479 | select7 + 0, |
| 2480 | select7 + 1, |
| 2481 | }; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2482 | |
| 2483 | Value *byte16 = Nucleus::createBitCast(x.value, Byte16::getType()); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2484 | Value *shuffle = Nucleus::createShuffleVector(byte16, byte16, pshufb); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2485 | Value *short8 = Nucleus::createBitCast(shuffle, UShort8::getType()); |
| 2486 | |
| 2487 | return RValue<UShort8>(short8); |
| 2488 | } |
| 2489 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2490 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2491 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2492 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2493 | #if defined(__i386__) || defined(__x86_64__) |
| 2494 | return x86::pmulhuw(x, y); |
| 2495 | #else |
| 2496 | return As<UShort8>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2497 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2498 | } |
| 2499 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2500 | Type *UShort8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2501 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2502 | return T(llvm::VectorType::get(T(UShort::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2503 | } |
| 2504 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2505 | RValue<Int> operator++(Int &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2506 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2507 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2508 | RValue<Int> res = val; |
| 2509 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2510 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2511 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2512 | |
| 2513 | return res; |
| 2514 | } |
| 2515 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2516 | const Int &operator++(Int &val) // Pre-increment |
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(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2519 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2520 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2521 | |
| 2522 | return val; |
| 2523 | } |
| 2524 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2525 | RValue<Int> operator--(Int &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2526 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2527 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2528 | RValue<Int> res = val; |
| 2529 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2530 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2531 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2532 | |
| 2533 | return res; |
| 2534 | } |
| 2535 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2536 | const Int &operator--(Int &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2537 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2538 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2539 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2540 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2541 | |
| 2542 | return val; |
| 2543 | } |
| 2544 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2545 | RValue<Int> RoundInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2546 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2547 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2548 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2549 | return x86::cvtss2si(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2550 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 2551 | return RValue<Int>(V(lowerRoundInt(V(cast.value), T(Int::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2552 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2553 | } |
| 2554 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2555 | Type *Int::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2556 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2557 | return T(llvm::Type::getInt32Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2558 | } |
| 2559 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2560 | Type *Long::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2561 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2562 | return T(llvm::Type::getInt64Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2563 | } |
| 2564 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2565 | UInt::UInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2566 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2567 | RR_DEBUG_INFO_UPDATE_LOC(); |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2568 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 2569 | // Value *integer = Nucleus::createFPToUI(cast.value, UInt::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2570 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2571 | // Smallest positive value representable in UInt, but not in Int |
| 2572 | const unsigned int ustart = 0x80000000u; |
| 2573 | const float ustartf = float(ustart); |
| 2574 | |
| 2575 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2576 | storeValue((~(As<Int>(cast) >> 31) & |
| 2577 | // Check if the value can be represented as an Int |
| 2578 | IfThenElse(cast >= ustartf, |
| 2579 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2580 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 2581 | // Otherwise, just convert normally |
| 2582 | Int(cast))).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2583 | } |
| 2584 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2585 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2586 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2587 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2588 | RValue<UInt> res = val; |
| 2589 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2590 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2591 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2592 | |
| 2593 | return res; |
| 2594 | } |
| 2595 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2596 | const UInt &operator++(UInt &val) // Pre-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2597 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2598 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2599 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2600 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2601 | |
| 2602 | return val; |
| 2603 | } |
| 2604 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2605 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2606 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2607 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2608 | RValue<UInt> res = val; |
| 2609 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2610 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2611 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2612 | |
| 2613 | return res; |
| 2614 | } |
| 2615 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2616 | const UInt &operator--(UInt &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2617 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2618 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2619 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2620 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2621 | |
| 2622 | return val; |
| 2623 | } |
| 2624 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2625 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2626 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2627 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2628 | // return x86::cvtss2si(val); // FIXME: Unsigned |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2629 | //#else |
| 2630 | // return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f)); |
| 2631 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2632 | // } |
| 2633 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2634 | Type *UInt::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2635 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2636 | return T(llvm::Type::getInt32Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2637 | } |
| 2638 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2639 | // Int2::Int2(RValue<Int> cast) |
| 2640 | // { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2641 | // Value *extend = Nucleus::createZExt(cast.value, Long::getType()); |
| 2642 | // Value *vector = Nucleus::createBitCast(extend, Int2::getType()); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2643 | // |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2644 | // int shuffle[2] = {0, 0}; |
| 2645 | // Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2646 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2647 | // storeValue(replicate); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2648 | // } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2649 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2650 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2651 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2652 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2653 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2654 | // return RValue<Int2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2655 | |
| 2656 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2657 | #else |
| 2658 | return As<Int2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2659 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2660 | } |
| 2661 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2662 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2663 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2664 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2665 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2666 | // return RValue<Int2>(Nucleus::createAShr(lhs.value, rhs.value)); |
| 2667 | |
| 2668 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2669 | #else |
| 2670 | return As<Int2>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2671 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2672 | } |
| 2673 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2674 | Type *Int2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2675 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2676 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2677 | } |
| 2678 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2679 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2680 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2681 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2682 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2683 | // return RValue<UInt2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2684 | |
| 2685 | return As<UInt2>(x86::pslld(As<Int2>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2686 | #else |
| 2687 | return As<UInt2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2688 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2689 | } |
| 2690 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2691 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2692 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2693 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2694 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2695 | // return RValue<UInt2>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 2696 | |
| 2697 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2698 | #else |
| 2699 | return As<UInt2>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2700 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2701 | } |
| 2702 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2703 | Type *UInt2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2704 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2705 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2706 | } |
| 2707 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2708 | Int4::Int4(RValue<Byte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2709 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2710 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2711 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2712 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2713 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2714 | *this = x86::pmovzxbd(As<Byte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2715 | } |
| 2716 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2717 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2718 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2719 | 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] | 2720 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2721 | Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::getType()), swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2722 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2723 | int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2724 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2725 | Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::getType()), swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2726 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2727 | *this = As<Int4>(d); |
| 2728 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2729 | } |
| 2730 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2731 | Int4::Int4(RValue<SByte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2732 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2733 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2734 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2735 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2736 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2737 | *this = x86::pmovsxbd(As<SByte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2738 | } |
| 2739 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2740 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2741 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2742 | int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; |
| 2743 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
| 2744 | Value *b = Nucleus::createShuffleVector(a, a, swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2745 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2746 | int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2747 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
| 2748 | Value *d = Nucleus::createShuffleVector(c, c, swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2749 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2750 | *this = As<Int4>(d) >> 24; |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2751 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2752 | } |
| 2753 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2754 | Int4::Int4(RValue<Short4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2755 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2756 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2757 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2758 | if(CPUID::supportsSSE4_1()) |
| 2759 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2760 | *this = x86::pmovsxwd(As<Short8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2761 | } |
| 2762 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2763 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2764 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2765 | int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2766 | Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle); |
| 2767 | *this = As<Int4>(c) >> 16; |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2768 | } |
| 2769 | } |
| 2770 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2771 | Int4::Int4(RValue<UShort4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2772 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2773 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2774 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2775 | if(CPUID::supportsSSE4_1()) |
| 2776 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2777 | *this = x86::pmovzxwd(As<UShort8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2778 | } |
| 2779 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2780 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2781 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2782 | int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2783 | Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
| 2784 | *this = As<Int4>(c); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2788 | Int4::Int4(RValue<Int> rhs) : XYZW(this) |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 2789 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2790 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 2791 | Value *vector = loadValue(); |
| 2792 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 2793 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2794 | int swizzle[4] = {0, 0, 0, 0}; |
| 2795 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 2796 | |
| 2797 | storeValue(replicate); |
| 2798 | } |
| 2799 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2800 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2801 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2802 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2803 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2804 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2805 | #else |
| 2806 | return As<Int4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2807 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2808 | } |
| 2809 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2810 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2811 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2812 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2813 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2814 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2815 | #else |
| 2816 | return As<Int4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2817 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2818 | } |
| 2819 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2820 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 2821 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2822 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 2823 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
Alexis Hetu | fb60399 | 2016-04-26 11:50:40 -0400 | [diff] [blame] | 2824 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2825 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
| 2826 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 2830 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2831 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 9ae6cfd | 2017-11-27 14:58:53 -0500 | [diff] [blame] | 2832 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2833 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2834 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType())); |
| 2835 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 2839 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2840 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 2841 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2842 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2843 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType())); |
| 2844 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 2848 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2849 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 9ae6cfd | 2017-11-27 14:58:53 -0500 | [diff] [blame] | 2850 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2851 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2852 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
| 2853 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 2857 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2858 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 2859 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2860 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2861 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType())); |
| 2862 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 2866 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2867 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 9ae6cfd | 2017-11-27 14:58:53 -0500 | [diff] [blame] | 2868 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2869 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2870 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType())); |
| 2871 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 2875 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2876 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2877 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2878 | if(CPUID::supportsSSE4_1()) |
| 2879 | { |
| 2880 | return x86::pmaxsd(x, y); |
| 2881 | } |
| 2882 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2883 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2884 | { |
| 2885 | RValue<Int4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 2886 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 2891 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2892 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2893 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2894 | if(CPUID::supportsSSE4_1()) |
| 2895 | { |
| 2896 | return x86::pminsd(x, y); |
| 2897 | } |
| 2898 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2899 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2900 | { |
| 2901 | RValue<Int4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 2902 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | RValue<Int4> RoundInt(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2907 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2908 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2909 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2910 | return x86::cvtps2dq(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2911 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 2912 | return As<Int4>(V(lowerRoundInt(V(cast.value), T(Int4::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2913 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2914 | } |
| 2915 | |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 2916 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 2917 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2918 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 2919 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2920 | return As<Int4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2921 | } |
| 2922 | |
| 2923 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 2924 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2925 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 2926 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2927 | return As<UInt4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2928 | } |
| 2929 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2930 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2931 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2932 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2933 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2934 | return x86::packssdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2935 | #else |
| 2936 | return As<Short8>(V(lowerPack(V(x.value), V(y.value), true))); |
| 2937 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2938 | } |
| 2939 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2940 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 2941 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2942 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2943 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2944 | return x86::packusdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2945 | #else |
| 2946 | return As<UShort8>(V(lowerPack(V(x.value), V(y.value), false))); |
| 2947 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2948 | } |
| 2949 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2950 | RValue<Int> SignMask(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2951 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2952 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2953 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2954 | return x86::movmskps(As<Float4>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2955 | #else |
| 2956 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2957 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2958 | } |
| 2959 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2960 | Type *Int4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2961 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2962 | return T(llvm::VectorType::get(T(Int::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2963 | } |
| 2964 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2965 | UInt4::UInt4(RValue<Float4> cast) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2966 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2967 | RR_DEBUG_INFO_UPDATE_LOC(); |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2968 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 2969 | // Value *xyzw = Nucleus::createFPToUI(cast.value, UInt4::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2970 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2971 | // Smallest positive value representable in UInt, but not in Int |
| 2972 | const unsigned int ustart = 0x80000000u; |
| 2973 | const float ustartf = float(ustart); |
| 2974 | |
| 2975 | // Check if the value can be represented as an Int |
| 2976 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 2977 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2978 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
| 2979 | // Otherwise, just convert normally |
| 2980 | (~uiValue & Int4(cast)); |
| 2981 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2982 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2983 | } |
| 2984 | |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 2985 | UInt4::UInt4(RValue<UInt> rhs) : XYZW(this) |
| 2986 | { |
| 2987 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2988 | Value *vector = loadValue(); |
| 2989 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 2990 | |
| 2991 | int swizzle[4] = {0, 0, 0, 0}; |
| 2992 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
| 2993 | |
| 2994 | storeValue(replicate); |
| 2995 | } |
| 2996 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2997 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2998 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2999 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3000 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3001 | return As<UInt4>(x86::pslld(As<Int4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3002 | #else |
| 3003 | return As<UInt4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 3004 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3005 | } |
| 3006 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3007 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3008 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3009 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3010 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3011 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3012 | #else |
| 3013 | return As<UInt4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 3014 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3015 | } |
| 3016 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3017 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3018 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3019 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 3020 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
Alexis Hetu | fb60399 | 2016-04-26 11:50:40 -0400 | [diff] [blame] | 3021 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 3022 | // return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
| 3023 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())) ^ UInt4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3027 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3028 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3029 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType())); |
| 3030 | } |
| 3031 | |
| 3032 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3033 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3034 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 3035 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 3036 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 3037 | // return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULE(x.value, y.value), Int4::getType())); |
| 3038 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType())) ^ UInt4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3042 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3043 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3044 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
| 3045 | } |
| 3046 | |
| 3047 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3048 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3049 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 3050 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 3051 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 3052 | // return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGE(x.value, y.value), Int4::getType())); |
| 3053 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType())) ^ UInt4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3057 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3058 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3059 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType())); |
| 3060 | } |
| 3061 | |
| 3062 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3063 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3064 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3065 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3066 | if(CPUID::supportsSSE4_1()) |
| 3067 | { |
| 3068 | return x86::pmaxud(x, y); |
| 3069 | } |
| 3070 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3071 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3072 | { |
| 3073 | RValue<UInt4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3074 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3079 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3080 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3081 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3082 | if(CPUID::supportsSSE4_1()) |
| 3083 | { |
| 3084 | return x86::pminud(x, y); |
| 3085 | } |
| 3086 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3087 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3088 | { |
| 3089 | RValue<UInt4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3090 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3094 | Type *UInt4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3095 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3096 | return T(llvm::VectorType::get(T(UInt::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3097 | } |
| 3098 | |
Alexis Hetu | 734e257 | 2018-12-20 14:00:49 -0500 | [diff] [blame] | 3099 | Type *Half::getType() |
| 3100 | { |
| 3101 | return T(llvm::Type::getInt16Ty(*::context)); |
| 3102 | } |
| 3103 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 3104 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3105 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3106 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3107 | #if defined(__i386__) || defined(__x86_64__) |
| 3108 | if(exactAtPow2) |
| 3109 | { |
| 3110 | // rcpss uses a piecewise-linear approximation which minimizes the relative error |
| 3111 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 3112 | return x86::rcpss(x) * Float(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 3113 | } |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 3114 | return x86::rcpss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3115 | #else |
| 3116 | return As<Float>(V(lowerRCP(V(x.value)))); |
| 3117 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3118 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3119 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3120 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3121 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3122 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3123 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3124 | return x86::rsqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3125 | #else |
| 3126 | return As<Float>(V(lowerRSQRT(V(x.value)))); |
| 3127 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3128 | } |
| 3129 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3130 | RValue<Float> Sqrt(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3131 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3132 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3133 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3134 | return x86::sqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3135 | #else |
| 3136 | return As<Float>(V(lowerSQRT(V(x.value)))); |
| 3137 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3138 | } |
| 3139 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3140 | RValue<Float> Round(RValue<Float> x) |
| 3141 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3142 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3143 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3144 | if(CPUID::supportsSSE4_1()) |
| 3145 | { |
| 3146 | return x86::roundss(x, 0); |
| 3147 | } |
| 3148 | else |
| 3149 | { |
| 3150 | return Float4(Round(Float4(x))).x; |
| 3151 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 3152 | #else |
| 3153 | return RValue<Float>(V(lowerRound(V(x.value)))); |
| 3154 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | RValue<Float> Trunc(RValue<Float> x) |
| 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 | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3161 | if(CPUID::supportsSSE4_1()) |
| 3162 | { |
| 3163 | return x86::roundss(x, 3); |
| 3164 | } |
| 3165 | else |
| 3166 | { |
| 3167 | return Float(Int(x)); // Rounded toward zero |
| 3168 | } |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 3169 | #else |
| 3170 | return RValue<Float>(V(lowerTrunc(V(x.value)))); |
| 3171 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | RValue<Float> Frac(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3175 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3176 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3177 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3178 | if(CPUID::supportsSSE4_1()) |
| 3179 | { |
| 3180 | return x - x86::floorss(x); |
| 3181 | } |
| 3182 | else |
| 3183 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3184 | return Float4(Frac(Float4(x))).x; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3185 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 3186 | #else |
| 3187 | // x - floor(x) can be 1.0 for very small negative x. |
| 3188 | // Clamp against the value just below 1.0. |
| 3189 | return Min(x - Floor(x), As<Float>(Int(0x3F7FFFFF))); |
| 3190 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3191 | } |
| 3192 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3193 | RValue<Float> Floor(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3194 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3195 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3196 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3197 | if(CPUID::supportsSSE4_1()) |
| 3198 | { |
| 3199 | return x86::floorss(x); |
| 3200 | } |
| 3201 | else |
| 3202 | { |
| 3203 | return Float4(Floor(Float4(x))).x; |
| 3204 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3205 | #else |
| 3206 | return RValue<Float>(V(lowerFloor(V(x.value)))); |
| 3207 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3208 | } |
| 3209 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3210 | RValue<Float> Ceil(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3211 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3212 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3213 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3214 | if(CPUID::supportsSSE4_1()) |
| 3215 | { |
| 3216 | return x86::ceilss(x); |
| 3217 | } |
| 3218 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3219 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3220 | { |
| 3221 | return Float4(Ceil(Float4(x))).x; |
| 3222 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3223 | } |
| 3224 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3225 | Type *Float::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3226 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 3227 | return T(llvm::Type::getFloatTy(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3228 | } |
| 3229 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3230 | Type *Float2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3231 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3232 | return T(Type_v2f32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3233 | } |
| 3234 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3235 | Float4::Float4(RValue<Float> rhs) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3236 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3237 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3238 | Value *vector = loadValue(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3239 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 3240 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3241 | int swizzle[4] = {0, 0, 0, 0}; |
| 3242 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3243 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3244 | storeValue(replicate); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3245 | } |
| 3246 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3247 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3248 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3249 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3250 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3251 | return x86::maxps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3252 | #else |
| 3253 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OGT))); |
| 3254 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3255 | } |
| 3256 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3257 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3258 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3259 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3260 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3261 | return x86::minps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3262 | #else |
| 3263 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OLT))); |
| 3264 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3265 | } |
| 3266 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 3267 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3268 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3269 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3270 | #if defined(__i386__) || defined(__x86_64__) |
| 3271 | if(exactAtPow2) |
| 3272 | { |
| 3273 | // rcpps uses a piecewise-linear approximation which minimizes the relative error |
| 3274 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 3275 | return x86::rcpps(x) * Float4(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 3276 | } |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 3277 | return x86::rcpps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3278 | #else |
| 3279 | return As<Float4>(V(lowerRCP(V(x.value)))); |
| 3280 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3281 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3282 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3283 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3284 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3285 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3286 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3287 | return x86::rsqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3288 | #else |
| 3289 | return As<Float4>(V(lowerRSQRT(V(x.value)))); |
| 3290 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3291 | } |
| 3292 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3293 | RValue<Float4> Sqrt(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3294 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3295 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3296 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3297 | return x86::sqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3298 | #else |
| 3299 | return As<Float4>(V(lowerSQRT(V(x.value)))); |
| 3300 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3301 | } |
| 3302 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3303 | RValue<Int> SignMask(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3304 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3305 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3306 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3307 | return x86::movmskps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3308 | #else |
| 3309 | return As<Int>(V(lowerFPSignMask(V(x.value), T(Int::getType())))); |
| 3310 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3311 | } |
| 3312 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3313 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3314 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3315 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3316 | // return As<Int4>(x86::cmpeqps(x, y)); |
| 3317 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value, y.value), Int4::getType())); |
| 3318 | } |
| 3319 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3320 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3321 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3322 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3323 | // return As<Int4>(x86::cmpltps(x, y)); |
| 3324 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value, y.value), Int4::getType())); |
| 3325 | } |
| 3326 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3327 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3328 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3329 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3330 | // return As<Int4>(x86::cmpleps(x, y)); |
| 3331 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value, y.value), Int4::getType())); |
| 3332 | } |
| 3333 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3334 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3335 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3336 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3337 | // return As<Int4>(x86::cmpneqps(x, y)); |
| 3338 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value, y.value), Int4::getType())); |
| 3339 | } |
| 3340 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3341 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3342 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3343 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3344 | // return As<Int4>(x86::cmpnltps(x, y)); |
| 3345 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value, y.value), Int4::getType())); |
| 3346 | } |
| 3347 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3348 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3349 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3350 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3351 | // return As<Int4>(x86::cmpnleps(x, y)); |
| 3352 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value, y.value), Int4::getType())); |
| 3353 | } |
| 3354 | |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3355 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 3356 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3357 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3358 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUEQ(x.value, y.value), Int4::getType())); |
| 3359 | } |
| 3360 | |
| 3361 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 3362 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3363 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3364 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULT(x.value, y.value), Int4::getType())); |
| 3365 | } |
| 3366 | |
| 3367 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 3368 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3369 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3370 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULE(x.value, y.value), Int4::getType())); |
| 3371 | } |
| 3372 | |
| 3373 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 3374 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3375 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3376 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUNE(x.value, y.value), Int4::getType())); |
| 3377 | } |
| 3378 | |
| 3379 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 3380 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3381 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3382 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGE(x.value, y.value), Int4::getType())); |
| 3383 | } |
| 3384 | |
| 3385 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 3386 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3387 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3388 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value, y.value), Int4::getType())); |
| 3389 | } |
| 3390 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3391 | RValue<Float4> Round(RValue<Float4> x) |
| 3392 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3393 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3394 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3395 | if(CPUID::supportsSSE4_1()) |
| 3396 | { |
| 3397 | return x86::roundps(x, 0); |
| 3398 | } |
| 3399 | else |
| 3400 | { |
| 3401 | return Float4(RoundInt(x)); |
| 3402 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 3403 | #else |
| 3404 | return RValue<Float4>(V(lowerRound(V(x.value)))); |
| 3405 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | RValue<Float4> Trunc(RValue<Float4> x) |
| 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 | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3412 | if(CPUID::supportsSSE4_1()) |
| 3413 | { |
| 3414 | return x86::roundps(x, 3); |
| 3415 | } |
| 3416 | else |
| 3417 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3418 | return Float4(Int4(x)); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3419 | } |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 3420 | #else |
| 3421 | return RValue<Float4>(V(lowerTrunc(V(x.value)))); |
| 3422 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | RValue<Float4> Frac(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3426 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3427 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3428 | Float4 frc; |
| 3429 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3430 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3431 | if(CPUID::supportsSSE4_1()) |
| 3432 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3433 | frc = x - Floor(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3434 | } |
| 3435 | else |
| 3436 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3437 | frc = x - Float4(Int4(x)); // Signed fractional part. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3438 | |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3439 | 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] | 3440 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 3441 | #else |
| 3442 | frc = x - Floor(x); |
| 3443 | #endif |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3444 | |
| 3445 | // x - floor(x) can be 1.0 for very small negative x. |
| 3446 | // Clamp against the value just below 1.0. |
| 3447 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3448 | } |
| 3449 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3450 | RValue<Float4> Floor(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3451 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3452 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3453 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3454 | if(CPUID::supportsSSE4_1()) |
| 3455 | { |
| 3456 | return x86::floorps(x); |
| 3457 | } |
| 3458 | else |
| 3459 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3460 | return x - Frac(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3461 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3462 | #else |
| 3463 | return RValue<Float4>(V(lowerFloor(V(x.value)))); |
| 3464 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3465 | } |
| 3466 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3467 | RValue<Float4> Ceil(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3468 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3469 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3470 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3471 | if(CPUID::supportsSSE4_1()) |
| 3472 | { |
| 3473 | return x86::ceilps(x); |
| 3474 | } |
| 3475 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3476 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3477 | { |
| 3478 | return -Floor(-x); |
| 3479 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3480 | } |
| 3481 | |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 3482 | RValue<Float4> Sin(RValue<Float4> v) |
| 3483 | { |
| 3484 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sin, { V(v.value)->getType() } ); |
| 3485 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
| 3486 | } |
| 3487 | |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 3488 | RValue<Float4> Cos(RValue<Float4> v) |
| 3489 | { |
| 3490 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::cos, { V(v.value)->getType() } ); |
| 3491 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
| 3492 | } |
| 3493 | |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 3494 | RValue<Float4> Tan(RValue<Float4> v) |
| 3495 | { |
| 3496 | return Sin(v) / Cos(v); |
| 3497 | } |
| 3498 | |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 3499 | static RValue<Float4> TransformFloat4PerElement(RValue<Float4> v, const char* name) |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3500 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3501 | 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] | 3502 | auto func = ::module->getOrInsertFunction(name, funcTy); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3503 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
| 3504 | for (uint64_t i = 0; i < 4; i++) |
| 3505 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3506 | auto el = ::builder->CreateCall(func, V(Nucleus::createExtractElement(v.value, Float::getType(), i))); |
| 3507 | out = V(Nucleus::createInsertElement(V(out), V(el), i)); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3508 | } |
| 3509 | return RValue<Float4>(V(out)); |
| 3510 | } |
| 3511 | |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 3512 | RValue<Float4> Asin(RValue<Float4> v) |
| 3513 | { |
| 3514 | return TransformFloat4PerElement(v, "asinf"); |
| 3515 | } |
| 3516 | |
| 3517 | RValue<Float4> Acos(RValue<Float4> v) |
| 3518 | { |
| 3519 | return TransformFloat4PerElement(v, "acosf"); |
| 3520 | } |
| 3521 | |
Ben Clayton | 749b4e0 | 2019-04-09 14:27:43 -0400 | [diff] [blame] | 3522 | RValue<Float4> Atan(RValue<Float4> v) |
| 3523 | { |
| 3524 | return TransformFloat4PerElement(v, "atanf"); |
| 3525 | } |
| 3526 | |
Ben Clayton | d963697 | 2019-04-09 15:09:54 -0400 | [diff] [blame] | 3527 | RValue<Float4> Sinh(RValue<Float4> v) |
| 3528 | { |
| 3529 | return TransformFloat4PerElement(v, "sinhf"); |
| 3530 | } |
| 3531 | |
Ben Clayton | 900ea2c | 2019-04-09 15:25:36 -0400 | [diff] [blame] | 3532 | RValue<Float4> Cosh(RValue<Float4> v) |
| 3533 | { |
| 3534 | return TransformFloat4PerElement(v, "coshf"); |
| 3535 | } |
| 3536 | |
Ben Clayton | 3928bd9 | 2019-04-09 15:27:41 -0400 | [diff] [blame] | 3537 | RValue<Float4> Tanh(RValue<Float4> v) |
| 3538 | { |
| 3539 | return TransformFloat4PerElement(v, "tanhf"); |
| 3540 | } |
| 3541 | |
Ben Clayton | f6d77ab | 2019-04-09 15:30:04 -0400 | [diff] [blame] | 3542 | RValue<Float4> Asinh(RValue<Float4> v) |
| 3543 | { |
| 3544 | return TransformFloat4PerElement(v, "asinhf"); |
| 3545 | } |
| 3546 | |
Ben Clayton | 28ebcb0 | 2019-04-09 15:33:38 -0400 | [diff] [blame] | 3547 | RValue<Float4> Acosh(RValue<Float4> v) |
| 3548 | { |
| 3549 | return TransformFloat4PerElement(v, "acoshf"); |
| 3550 | } |
| 3551 | |
Ben Clayton | fa6a539 | 2019-04-09 15:35:24 -0400 | [diff] [blame] | 3552 | RValue<Float4> Atanh(RValue<Float4> v) |
| 3553 | { |
| 3554 | return TransformFloat4PerElement(v, "atanhf"); |
| 3555 | } |
| 3556 | |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3557 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 3558 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3559 | ::llvm::SmallVector<::llvm::Type*, 2> paramTys; |
| 3560 | paramTys.push_back(T(Float::getType())); |
| 3561 | paramTys.push_back(T(Float::getType())); |
| 3562 | auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), paramTys, false); |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3563 | auto func = ::module->getOrInsertFunction("atan2f", funcTy); |
| 3564 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
| 3565 | for (uint64_t i = 0; i < 4; i++) |
| 3566 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3567 | auto el = ::builder->CreateCall2(func, ARGS( |
| 3568 | V(Nucleus::createExtractElement(x.value, Float::getType(), i)), |
| 3569 | V(Nucleus::createExtractElement(y.value, Float::getType(), i)) |
| 3570 | )); |
| 3571 | out = V(Nucleus::createInsertElement(V(out), V(el), i)); |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3572 | } |
| 3573 | return RValue<Float4>(V(out)); |
| 3574 | } |
| 3575 | |
Ben Clayton | bfe94f0 | 2019-04-09 15:52:12 -0400 | [diff] [blame] | 3576 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 3577 | { |
Ben Clayton | 7579db1 | 2019-05-02 08:37:12 +0100 | [diff] [blame] | 3578 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::pow, { T(Float4::getType()) }); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3579 | 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] | 3580 | } |
| 3581 | |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 3582 | RValue<Float4> Exp(RValue<Float4> v) |
| 3583 | { |
| 3584 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::exp, { T(Float4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3585 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 3586 | } |
| 3587 | |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 3588 | RValue<Float4> Log(RValue<Float4> v) |
| 3589 | { |
| 3590 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::log, { T(Float4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3591 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 3592 | } |
| 3593 | |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 3594 | RValue<Float4> Exp2(RValue<Float4> v) |
| 3595 | { |
| 3596 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::exp2, { T(Float4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3597 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 3598 | } |
| 3599 | |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 3600 | RValue<Float4> Log2(RValue<Float4> v) |
| 3601 | { |
| 3602 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::log2, { T(Float4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3603 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 3604 | } |
| 3605 | |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3606 | RValue<UInt4> Ctlz(RValue<UInt4> v, bool isZeroUndef) |
| 3607 | { |
Ben Clayton | 7579db1 | 2019-05-02 08:37:12 +0100 | [diff] [blame] | 3608 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::ctlz, { T(UInt4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3609 | return RValue<UInt4>(V(::builder->CreateCall2(func, ARGS( |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3610 | V(v.value), |
| 3611 | isZeroUndef ? ::llvm::ConstantInt::getTrue(*::context) : ::llvm::ConstantInt::getFalse(*::context) |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3612 | )))); |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3613 | } |
| 3614 | |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3615 | RValue<UInt4> Cttz(RValue<UInt4> v, bool isZeroUndef) |
| 3616 | { |
Ben Clayton | 7579db1 | 2019-05-02 08:37:12 +0100 | [diff] [blame] | 3617 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::cttz, { T(UInt4::getType()) } ); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3618 | return RValue<UInt4>(V(::builder->CreateCall2(func, ARGS( |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3619 | V(v.value), |
| 3620 | isZeroUndef ? ::llvm::ConstantInt::getTrue(*::context) : ::llvm::ConstantInt::getFalse(*::context) |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3621 | )))); |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3622 | } |
| 3623 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3624 | Type *Float4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3625 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3626 | return T(llvm::VectorType::get(T(Float::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3627 | } |
| 3628 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3629 | RValue<Long> Ticks() |
| 3630 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3631 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3632 | llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::readcyclecounter); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3633 | |
Nicolas Capens | 2ab69ee | 2016-09-26 11:45:17 -0400 | [diff] [blame] | 3634 | return RValue<Long>(V(::builder->CreateCall(rdtsc))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3635 | } |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 3636 | |
| 3637 | RValue<Pointer<Byte>> ConstantPointer(void const * ptr) |
| 3638 | { |
| 3639 | // Note: this should work for 32-bit pointers as well because 'inttoptr' |
| 3640 | // is defined to truncate (and zero extend) if necessary. |
| 3641 | auto ptrAsInt = ::llvm::ConstantInt::get(::llvm::Type::getInt64Ty(*::context), reinterpret_cast<uintptr_t>(ptr)); |
| 3642 | return RValue<Pointer<Byte>>(V(::builder->CreateIntToPtr(ptrAsInt, T(Pointer<Byte>::getType())))); |
| 3643 | } |
| 3644 | |
| 3645 | Value* Call(RValue<Pointer<Byte>> fptr, Type* retTy, std::initializer_list<Value*> args, std::initializer_list<Type*> argTys) |
| 3646 | { |
| 3647 | ::llvm::SmallVector<::llvm::Type*, 8> paramTys; |
| 3648 | for (auto ty : argTys) { paramTys.push_back(T(ty)); } |
| 3649 | auto funcTy = ::llvm::FunctionType::get(T(retTy), paramTys, false); |
| 3650 | |
| 3651 | auto funcPtrTy = funcTy->getPointerTo(); |
| 3652 | auto funcPtr = ::builder->CreatePointerCast(V(fptr.value), funcPtrTy); |
| 3653 | |
| 3654 | ::llvm::SmallVector<::llvm::Value*, 8> arguments; |
| 3655 | for (auto arg : args) { arguments.push_back(V(arg)); } |
| 3656 | return V(::builder->CreateCall(funcPtr, arguments)); |
| 3657 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3658 | } |
| 3659 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 3660 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3661 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3662 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3663 | namespace x86 |
| 3664 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3665 | RValue<Int> cvtss2si(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3666 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3667 | llvm::Function *cvtss2si = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_cvtss2si); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3668 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3669 | Float4 vector; |
| 3670 | vector.x = val; |
| 3671 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3672 | 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] | 3673 | } |
| 3674 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3675 | RValue<Int4> cvtps2dq(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3676 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3677 | llvm::Function *cvtps2dq = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_cvtps2dq); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3678 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3679 | return RValue<Int4>(V(::builder->CreateCall(cvtps2dq, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3680 | } |
| 3681 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3682 | RValue<Float> rcpss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3683 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3684 | 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] | 3685 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3686 | 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] | 3687 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3688 | 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] | 3689 | } |
| 3690 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3691 | RValue<Float> sqrtss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3692 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3693 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sqrt, {V(val.value)->getType()}); |
| 3694 | return RValue<Float>(V(::builder->CreateCall(sqrt, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3695 | } |
| 3696 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3697 | RValue<Float> rsqrtss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3698 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3699 | 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] | 3700 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3701 | 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] | 3702 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3703 | 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] | 3704 | } |
| 3705 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3706 | RValue<Float4> rcpps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3707 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3708 | 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] | 3709 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3710 | return RValue<Float4>(V(::builder->CreateCall(rcpps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3711 | } |
| 3712 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3713 | RValue<Float4> sqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3714 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3715 | 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] | 3716 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3717 | return RValue<Float4>(V(::builder->CreateCall(sqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3718 | } |
| 3719 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3720 | RValue<Float4> rsqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3721 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3722 | 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] | 3723 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3724 | return RValue<Float4>(V(::builder->CreateCall(rsqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3725 | } |
| 3726 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3727 | RValue<Float4> maxps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3728 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3729 | 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] | 3730 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3731 | 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] | 3732 | } |
| 3733 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3734 | RValue<Float4> minps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3735 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3736 | 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] | 3737 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3738 | 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] | 3739 | } |
| 3740 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3741 | RValue<Float> roundss(RValue<Float> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3742 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3743 | 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] | 3744 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3745 | Value *undef = V(llvm::UndefValue::get(T(Float4::getType()))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3746 | Value *vector = Nucleus::createInsertElement(undef, val.value, 0); |
| 3747 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3748 | 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] | 3749 | } |
| 3750 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3751 | RValue<Float> floorss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3752 | { |
| 3753 | return roundss(val, 1); |
| 3754 | } |
| 3755 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3756 | RValue<Float> ceilss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3757 | { |
| 3758 | return roundss(val, 2); |
| 3759 | } |
| 3760 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3761 | RValue<Float4> roundps(RValue<Float4> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3762 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3763 | 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] | 3764 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3765 | 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] | 3766 | } |
| 3767 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3768 | RValue<Float4> floorps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3769 | { |
| 3770 | return roundps(val, 1); |
| 3771 | } |
| 3772 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3773 | RValue<Float4> ceilps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3774 | { |
| 3775 | return roundps(val, 2); |
| 3776 | } |
| 3777 | |
Alexis Hetu | 0f44807 | 2016-03-18 10:56:08 -0400 | [diff] [blame] | 3778 | RValue<Int4> pabsd(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3779 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3780 | return RValue<Int4>(V(lowerPABS(V(x.value)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3781 | } |
| 3782 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3783 | RValue<Short4> paddsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3784 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3785 | 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] | 3786 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3787 | 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] | 3788 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3789 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3790 | RValue<Short4> psubsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3791 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3792 | 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] | 3793 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3794 | 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] | 3795 | } |
| 3796 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3797 | RValue<UShort4> paddusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3798 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3799 | 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] | 3800 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3801 | 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] | 3802 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3803 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3804 | RValue<UShort4> psubusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3805 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3806 | 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] | 3807 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3808 | 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] | 3809 | } |
| 3810 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3811 | RValue<SByte8> paddsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3812 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3813 | 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] | 3814 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3815 | 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] | 3816 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3817 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3818 | RValue<SByte8> psubsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3819 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3820 | 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] | 3821 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3822 | 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] | 3823 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3824 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3825 | RValue<Byte8> paddusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3826 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3827 | 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] | 3828 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3829 | 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] | 3830 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3831 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3832 | RValue<Byte8> psubusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3833 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3834 | 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] | 3835 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3836 | 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] | 3837 | } |
| 3838 | |
| 3839 | RValue<UShort4> pavgw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3840 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3841 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3842 | } |
| 3843 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3844 | RValue<Short4> pmaxsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3845 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3846 | 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] | 3847 | } |
| 3848 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3849 | RValue<Short4> pminsw(RValue<Short4> x, RValue<Short4> y) |
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 | 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] | 3852 | } |
| 3853 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3854 | RValue<Short4> pcmpgtw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3855 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3856 | 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] | 3857 | } |
| 3858 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3859 | RValue<Short4> pcmpeqw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3860 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3861 | 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] | 3862 | } |
| 3863 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3864 | RValue<Byte8> pcmpgtb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3865 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3866 | 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] | 3867 | } |
| 3868 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3869 | RValue<Byte8> pcmpeqb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3870 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3871 | 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] | 3872 | } |
| 3873 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3874 | RValue<Short4> packssdw(RValue<Int2> x, RValue<Int2> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3875 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3876 | 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] | 3877 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3878 | 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] | 3879 | } |
| 3880 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3881 | RValue<Short8> packssdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3882 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3883 | 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] | 3884 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3885 | 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] | 3886 | } |
| 3887 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3888 | RValue<SByte8> packsswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3889 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3890 | 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] | 3891 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3892 | 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] | 3893 | } |
| 3894 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3895 | RValue<Byte8> packuswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3896 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3897 | 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] | 3898 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3899 | 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] | 3900 | } |
| 3901 | |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 3902 | RValue<UShort8> packusdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3903 | { |
| 3904 | if(CPUID::supportsSSE4_1()) |
| 3905 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3906 | llvm::Function *packusdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_packusdw); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3907 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3908 | 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] | 3909 | } |
| 3910 | else |
| 3911 | { |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 3912 | RValue<Int4> bx = (x & ~(x >> 31)) - Int4(0x8000); |
| 3913 | RValue<Int4> by = (y & ~(y >> 31)) - Int4(0x8000); |
| 3914 | |
| 3915 | return As<UShort8>(packssdw(bx, by) + Short8(0x8000u)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3916 | } |
| 3917 | } |
| 3918 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3919 | RValue<UShort4> psrlw(RValue<UShort4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3920 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3921 | 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] | 3922 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3923 | 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] | 3924 | } |
| 3925 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3926 | RValue<UShort8> psrlw(RValue<UShort8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3927 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3928 | 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] | 3929 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3930 | 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] | 3931 | } |
| 3932 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3933 | RValue<Short4> psraw(RValue<Short4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3934 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3935 | 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] | 3936 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3937 | 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] | 3938 | } |
| 3939 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3940 | RValue<Short8> psraw(RValue<Short8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3941 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3942 | 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] | 3943 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3944 | 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] | 3945 | } |
| 3946 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3947 | RValue<Short4> psllw(RValue<Short4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3948 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3949 | 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] | 3950 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3951 | 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] | 3952 | } |
| 3953 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3954 | RValue<Short8> psllw(RValue<Short8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3955 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3956 | 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] | 3957 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3958 | 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] | 3959 | } |
| 3960 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3961 | RValue<Int2> pslld(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3962 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3963 | 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] | 3964 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3965 | 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] | 3966 | } |
| 3967 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3968 | RValue<Int4> pslld(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3969 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3970 | 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] | 3971 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3972 | 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] | 3973 | } |
| 3974 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3975 | RValue<Int2> psrad(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3976 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3977 | 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] | 3978 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3979 | 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] | 3980 | } |
| 3981 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3982 | RValue<Int4> psrad(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3983 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3984 | 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] | 3985 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3986 | 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] | 3987 | } |
| 3988 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3989 | RValue<UInt2> psrld(RValue<UInt2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3990 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3991 | 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] | 3992 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3993 | 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] | 3994 | } |
| 3995 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3996 | RValue<UInt4> psrld(RValue<UInt4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3997 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3998 | 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] | 3999 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4000 | 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] | 4001 | } |
| 4002 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4003 | RValue<Int4> pmaxsd(RValue<Int4> x, RValue<Int4> y) |
| 4004 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4005 | 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] | 4006 | } |
| 4007 | |
| 4008 | RValue<Int4> pminsd(RValue<Int4> x, RValue<Int4> y) |
| 4009 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4010 | 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] | 4011 | } |
| 4012 | |
| 4013 | RValue<UInt4> pmaxud(RValue<UInt4> x, RValue<UInt4> y) |
| 4014 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4015 | 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] | 4016 | } |
| 4017 | |
| 4018 | RValue<UInt4> pminud(RValue<UInt4> x, RValue<UInt4> y) |
| 4019 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4020 | 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] | 4021 | } |
| 4022 | |
| 4023 | RValue<Short4> pmulhw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4024 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4025 | 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] | 4026 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4027 | 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] | 4028 | } |
| 4029 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4030 | RValue<UShort4> pmulhuw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4031 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4032 | 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] | 4033 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4034 | 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] | 4035 | } |
| 4036 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4037 | RValue<Int2> pmaddwd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4038 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4039 | 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] | 4040 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4041 | 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] | 4042 | } |
| 4043 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4044 | RValue<Short8> pmulhw(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4045 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4046 | 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] | 4047 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4048 | 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] | 4049 | } |
| 4050 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4051 | RValue<UShort8> pmulhuw(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4052 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4053 | 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] | 4054 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4055 | 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] | 4056 | } |
| 4057 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4058 | RValue<Int4> pmaddwd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4059 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4060 | 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] | 4061 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4062 | 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] | 4063 | } |
| 4064 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4065 | RValue<Int> movmskps(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4066 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4067 | 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] | 4068 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4069 | return RValue<Int>(V(::builder->CreateCall(movmskps, ARGS(V(x.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4070 | } |
| 4071 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4072 | RValue<Int> pmovmskb(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4073 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4074 | 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] | 4075 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 4076 | return RValue<Int>(V(::builder->CreateCall(pmovmskb, ARGS(V(x.value))))) & 0xFF; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4077 | } |
| 4078 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4079 | RValue<Int4> pmovzxbd(RValue<Byte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4080 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4081 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4082 | } |
| 4083 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4084 | RValue<Int4> pmovsxbd(RValue<SByte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4085 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4086 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4087 | } |
| 4088 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4089 | RValue<Int4> pmovzxwd(RValue<UShort8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4090 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4091 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4092 | } |
| 4093 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4094 | RValue<Int4> pmovsxwd(RValue<Short8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4095 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4096 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4097 | } |
| 4098 | } |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 4099 | #endif // defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4100 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 4101 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4102 | // extractAll returns a vector containing the extracted n scalar value of |
| 4103 | // the vector vec. |
| 4104 | static std::vector<Value*> extractAll(Value* vec, int n) |
| 4105 | { |
| 4106 | std::vector<Value*> elements; |
| 4107 | elements.reserve(n); |
| 4108 | for (int i = 0; i < n; i++) |
| 4109 | { |
| 4110 | auto el = V(::builder->CreateExtractElement(V(vec), i)); |
| 4111 | elements.push_back(el); |
| 4112 | } |
| 4113 | return elements; |
| 4114 | } |
| 4115 | |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4116 | // toInt returns all the integer values in vals extended to a native width |
| 4117 | // integer. |
| 4118 | static std::vector<Value*> toInt(const std::vector<Value*>& vals, bool isSigned) |
| 4119 | { |
| 4120 | auto intTy = ::llvm::Type::getIntNTy(*::context, sizeof(int) * 8); // Natural integer width. |
| 4121 | std::vector<Value*> elements; |
| 4122 | elements.reserve(vals.size()); |
| 4123 | for (auto v : vals) |
| 4124 | { |
| 4125 | if (isSigned) |
| 4126 | { |
| 4127 | elements.push_back(V(::builder->CreateSExt(V(v), intTy))); |
| 4128 | } |
| 4129 | else |
| 4130 | { |
| 4131 | elements.push_back(V(::builder->CreateZExt(V(v), intTy))); |
| 4132 | } |
| 4133 | } |
| 4134 | return elements; |
| 4135 | } |
| 4136 | |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4137 | // toDouble returns all the float values in vals extended to doubles. |
| 4138 | static std::vector<Value*> toDouble(const std::vector<Value*>& vals) |
| 4139 | { |
| 4140 | auto doubleTy = ::llvm::Type::getDoubleTy(*::context); |
| 4141 | std::vector<Value*> elements; |
| 4142 | elements.reserve(vals.size()); |
| 4143 | for (auto v : vals) |
| 4144 | { |
| 4145 | elements.push_back(V(::builder->CreateFPExt(V(v), doubleTy))); |
| 4146 | } |
| 4147 | return elements; |
| 4148 | } |
| 4149 | |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4150 | std::vector<Value*> PrintValue::Ty<Byte4>::val(const RValue<Byte4>& v) { return toInt(extractAll(v.value, 4), false); } |
| 4151 | std::vector<Value*> PrintValue::Ty<Int>::val(const RValue<Int>& v) { return toInt({v.value}, true); } |
| 4152 | std::vector<Value*> PrintValue::Ty<Int2>::val(const RValue<Int2>& v) { return toInt(extractAll(v.value, 2), true); } |
| 4153 | std::vector<Value*> PrintValue::Ty<Int4>::val(const RValue<Int4>& v) { return toInt(extractAll(v.value, 4), true); } |
| 4154 | std::vector<Value*> PrintValue::Ty<UInt>::val(const RValue<UInt>& v) { return toInt({v.value}, false); } |
| 4155 | std::vector<Value*> PrintValue::Ty<UInt2>::val(const RValue<UInt2>& v) { return toInt(extractAll(v.value, 2), false); } |
| 4156 | std::vector<Value*> PrintValue::Ty<UInt4>::val(const RValue<UInt4>& v) { return toInt(extractAll(v.value, 4), false); } |
| 4157 | std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return toInt(extractAll(v.value, 4), true); } |
| 4158 | 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] | 4159 | std::vector<Value*> PrintValue::Ty<Float>::val(const RValue<Float>& v) { return toDouble({v.value}); } |
| 4160 | 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] | 4161 | 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] | 4162 | |
| 4163 | void Printv(const char* function, const char* file, int line, const char* fmt, std::initializer_list<PrintValue> args) |
| 4164 | { |
| 4165 | // LLVM types used below. |
| 4166 | auto i32Ty = ::llvm::Type::getInt32Ty(*::context); |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4167 | auto intTy = ::llvm::Type::getIntNTy(*::context, sizeof(int) * 8); // Natural integer width. |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4168 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(*::context); |
| 4169 | auto funcTy = ::llvm::FunctionType::get(i32Ty, {i8PtrTy}, true); |
| 4170 | |
| 4171 | auto func = ::module->getOrInsertFunction("printf", funcTy); |
| 4172 | |
| 4173 | // Build the printf format message string. |
| 4174 | std::string str; |
| 4175 | if (file != nullptr) { str += (line > 0) ? "%s:%d " : "%s "; } |
| 4176 | if (function != nullptr) { str += "%s "; } |
| 4177 | str += fmt; |
| 4178 | |
| 4179 | // Perform subsitution on all '{n}' bracketed indices in the format |
| 4180 | // message. |
| 4181 | int i = 0; |
| 4182 | for (const PrintValue& arg : args) |
| 4183 | { |
| 4184 | str = replace(str, "{" + std::to_string(i++) + "}", arg.format); |
| 4185 | } |
| 4186 | |
| 4187 | ::llvm::SmallVector<::llvm::Value*, 8> vals; |
| 4188 | |
| 4189 | // The format message is always the first argument. |
| 4190 | vals.push_back(::builder->CreateGlobalStringPtr(str)); |
| 4191 | |
| 4192 | // Add optional file, line and function info if provided. |
| 4193 | if (file != nullptr) |
| 4194 | { |
| 4195 | vals.push_back(::builder->CreateGlobalStringPtr(file)); |
| 4196 | if (line > 0) |
| 4197 | { |
| 4198 | vals.push_back(::llvm::ConstantInt::get(intTy, line)); |
| 4199 | } |
| 4200 | } |
| 4201 | if (function != nullptr) |
| 4202 | { |
| 4203 | vals.push_back(::builder->CreateGlobalStringPtr(function)); |
| 4204 | } |
| 4205 | |
| 4206 | // Add all format arguments. |
| 4207 | for (const PrintValue& arg : args) |
| 4208 | { |
| 4209 | for (auto val : arg.values) |
| 4210 | { |
| 4211 | vals.push_back(V(val)); |
| 4212 | } |
| 4213 | } |
| 4214 | |
| 4215 | ::builder->CreateCall(func, vals); |
| 4216 | } |
| 4217 | #endif // ENABLE_RR_PRINT |
| 4218 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4219 | void Break() |
| 4220 | { |
| 4221 | auto trap = ::llvm::Intrinsic::getDeclaration(module, llvm::Intrinsic::trap); |
| 4222 | builder->CreateCall(trap); |
| 4223 | } |
| 4224 | |
| 4225 | void Nop() |
| 4226 | { |
| 4227 | auto voidTy = ::llvm::Type::getVoidTy(*context); |
| 4228 | auto funcTy = ::llvm::FunctionType::get(voidTy, {}, false); |
| 4229 | auto func = ::module->getOrInsertFunction("nop", funcTy); |
| 4230 | builder->CreateCall(func); |
| 4231 | } |
| 4232 | |
| 4233 | void EmitDebugLocation() |
| 4234 | { |
| 4235 | #ifdef ENABLE_RR_DEBUG_INFO |
| 4236 | if (debugInfo != nullptr) |
| 4237 | { |
| 4238 | debugInfo->EmitLocation(); |
| 4239 | } |
| 4240 | #endif // ENABLE_RR_DEBUG_INFO |
| 4241 | } |
| 4242 | |
| 4243 | void EmitDebugVariable(Value* value) |
| 4244 | { |
| 4245 | #ifdef ENABLE_RR_DEBUG_INFO |
| 4246 | if (debugInfo != nullptr) |
| 4247 | { |
| 4248 | debugInfo->EmitVariable(value); |
| 4249 | } |
| 4250 | #endif // ENABLE_RR_DEBUG_INFO |
| 4251 | } |
| 4252 | |
| 4253 | void FlushDebug() |
| 4254 | { |
| 4255 | #ifdef ENABLE_RR_DEBUG_INFO |
| 4256 | if (debugInfo != nullptr) |
| 4257 | { |
| 4258 | debugInfo->Flush(); |
| 4259 | } |
| 4260 | #endif // ENABLE_RR_DEBUG_INFO |
| 4261 | } |
| 4262 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4263 | } // namespace rr |
| 4264 | |
| 4265 | // ------------------------------ Coroutines ------------------------------ |
| 4266 | |
| 4267 | namespace { |
| 4268 | |
| 4269 | struct CoroutineState |
| 4270 | { |
| 4271 | llvm::Function *await = nullptr; |
| 4272 | llvm::Function *destroy = nullptr; |
| 4273 | llvm::Value *handle = nullptr; |
| 4274 | llvm::Value *id = nullptr; |
| 4275 | llvm::Value *promise = nullptr; |
| 4276 | llvm::BasicBlock *suspendBlock = nullptr; |
| 4277 | llvm::BasicBlock *endBlock = nullptr; |
| 4278 | llvm::BasicBlock *destroyBlock = nullptr; |
| 4279 | }; |
| 4280 | CoroutineState coroutine; |
| 4281 | |
| 4282 | // Magic values retuned by llvm.coro.suspend. |
| 4283 | // See: https://llvm.org/docs/Coroutines.html#llvm-coro-suspend-intrinsic |
| 4284 | enum SuspendAction |
| 4285 | { |
| 4286 | SuspendActionSuspend = -1, |
| 4287 | SuspendActionResume = 0, |
| 4288 | SuspendActionDestroy = 1 |
| 4289 | }; |
| 4290 | |
| 4291 | } // anonymous namespace |
| 4292 | |
| 4293 | namespace rr { |
| 4294 | |
| 4295 | void Nucleus::createCoroutine(Type *YieldType, std::vector<Type*> &Params) |
| 4296 | { |
| 4297 | // Types |
| 4298 | auto voidTy = ::llvm::Type::getVoidTy(*::context); |
| 4299 | auto i1Ty = ::llvm::Type::getInt1Ty(*::context); |
| 4300 | auto i8Ty = ::llvm::Type::getInt8Ty(*::context); |
| 4301 | auto i32Ty = ::llvm::Type::getInt32Ty(*::context); |
| 4302 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(*::context); |
| 4303 | auto promiseTy = T(YieldType); |
| 4304 | auto promisePtrTy = promiseTy->getPointerTo(); |
| 4305 | auto handleTy = i8PtrTy; |
| 4306 | auto boolTy = i1Ty; |
| 4307 | |
| 4308 | // LLVM intrinsics |
| 4309 | auto coro_id = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::coro_id); |
| 4310 | auto coro_size = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::coro_size, {i32Ty}); |
| 4311 | auto coro_begin = ::llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::coro_begin); |
| 4312 | auto coro_resume = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_resume); |
| 4313 | auto coro_end = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_end); |
| 4314 | auto coro_free = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_free); |
| 4315 | auto coro_destroy = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_destroy); |
| 4316 | auto coro_promise = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_promise); |
| 4317 | auto coro_done = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_done); |
| 4318 | auto coro_suspend = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_suspend); |
| 4319 | |
| 4320 | auto allocFrameTy = ::llvm::FunctionType::get(i8PtrTy, {i32Ty}, false); |
| 4321 | auto allocFrame = ::module->getOrInsertFunction("coroutine_alloc_frame", allocFrameTy); |
| 4322 | auto freeFrameTy = ::llvm::FunctionType::get(voidTy, {i8PtrTy}, false); |
| 4323 | auto freeFrame = ::module->getOrInsertFunction("coroutine_free_frame", freeFrameTy); |
| 4324 | |
| 4325 | // Build the coroutine_await() function: |
| 4326 | // |
| 4327 | // bool coroutine_await(CoroutineHandle* handle, YieldType* out) |
| 4328 | // { |
| 4329 | // if (llvm.coro.done(handle)) |
| 4330 | // { |
| 4331 | // return false; |
| 4332 | // } |
| 4333 | // else |
| 4334 | // { |
| 4335 | // *value = (T*)llvm.coro.promise(handle); |
| 4336 | // llvm.coro.resume(handle); |
| 4337 | // return true; |
| 4338 | // } |
| 4339 | // } |
| 4340 | // |
| 4341 | llvm::FunctionType *coroutineAwaitTy = llvm::FunctionType::get(boolTy, {handleTy, promisePtrTy}, false); |
| 4342 | ::coroutine.await = llvm::Function::Create(coroutineAwaitTy, llvm::GlobalValue::InternalLinkage, "coroutine_await", ::module); |
| 4343 | ::coroutine.await->setCallingConv(llvm::CallingConv::C); |
| 4344 | { |
| 4345 | auto args = ::coroutine.await->arg_begin(); |
| 4346 | auto handle = args++; |
| 4347 | auto outPtr = args++; |
| 4348 | ::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "co_await", ::coroutine.await)); |
| 4349 | auto doneBlock = llvm::BasicBlock::Create(*::context, "done", ::coroutine.await); |
| 4350 | auto resumeBlock = llvm::BasicBlock::Create(*::context, "resume", ::coroutine.await); |
| 4351 | |
| 4352 | auto done = ::builder->CreateCall(coro_done, {handle}, "done"); |
| 4353 | ::builder->CreateCondBr(done, doneBlock, resumeBlock); |
| 4354 | |
| 4355 | ::builder->SetInsertPoint(doneBlock); |
| 4356 | ::builder->CreateRet(::llvm::ConstantInt::getFalse(i1Ty)); |
| 4357 | |
| 4358 | ::builder->SetInsertPoint(resumeBlock); |
| 4359 | auto promiseAlignment = ::llvm::ConstantInt::get(i32Ty, 4); // TODO: Get correct alignment. |
| 4360 | auto promisePtr = ::builder->CreateCall(coro_promise, {handle, promiseAlignment, ::llvm::ConstantInt::get(i1Ty, 0)}); |
| 4361 | auto promise = ::builder->CreateLoad(::builder->CreatePointerCast(promisePtr, promisePtrTy)); |
| 4362 | ::builder->CreateStore(promise, outPtr); |
| 4363 | ::builder->CreateCall(coro_resume, {handle}); |
| 4364 | ::builder->CreateRet(::llvm::ConstantInt::getTrue(i1Ty)); |
| 4365 | } |
| 4366 | |
| 4367 | // Build the coroutine_destroy() function: |
| 4368 | // |
| 4369 | // void coroutine_destroy(CoroutineHandle* handle) |
| 4370 | // { |
| 4371 | // llvm.coro.destroy(handle); |
| 4372 | // } |
| 4373 | // |
| 4374 | llvm::FunctionType *coroutineDestroyTy = llvm::FunctionType::get(voidTy, handleTy, false); |
| 4375 | ::coroutine.destroy = llvm::Function::Create(coroutineDestroyTy, llvm::GlobalValue::InternalLinkage, "coroutine_destroy", ::module); |
| 4376 | ::coroutine.destroy->setCallingConv(llvm::CallingConv::C); |
| 4377 | { |
| 4378 | auto handle = ::coroutine.destroy->arg_begin(); |
| 4379 | ::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "", ::coroutine.destroy)); |
| 4380 | ::builder->CreateCall(coro_destroy, {handle}); |
| 4381 | ::builder->CreateRetVoid(); |
| 4382 | } |
| 4383 | |
| 4384 | // Begin building the main coroutine_begin() function. |
| 4385 | // |
| 4386 | // CoroutineHandle* coroutine_begin(<Arguments>) |
| 4387 | // { |
| 4388 | // YieldType promise; |
| 4389 | // auto id = llvm.coro.id(0, &promise, nullptr, nullptr); |
| 4390 | // void* frame = coroutine_alloc_frame(llvm.coro.size.i32()); |
| 4391 | // CoroutineHandle *handle = llvm.coro.begin(id, frame); |
| 4392 | // |
| 4393 | // ... <REACTOR CODE> ... |
| 4394 | // |
| 4395 | // end: |
| 4396 | // SuspendAction action = llvm.coro.suspend(none, true /* final */); // <-- RESUME POINT |
| 4397 | // switch (action) |
| 4398 | // { |
| 4399 | // case SuspendActionResume: |
| 4400 | // UNREACHABLE(); // Illegal to resume after final suspend. |
| 4401 | // case SuspendActionDestroy: |
| 4402 | // goto destroy; |
| 4403 | // default: // (SuspendActionSuspend) |
| 4404 | // goto suspend; |
| 4405 | // } |
| 4406 | // |
| 4407 | // destroy: |
| 4408 | // coroutine_free_frame(llvm.coro.free(id, handle)); |
| 4409 | // goto suspend; |
| 4410 | // |
| 4411 | // suspend: |
| 4412 | // llvm.coro.end(handle, false); |
| 4413 | // return handle; |
| 4414 | // } |
| 4415 | // |
| 4416 | llvm::FunctionType *functionType = llvm::FunctionType::get(handleTy, T(Params), false); |
| 4417 | ::function = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, "coroutine_begin", ::module); |
| 4418 | ::function->setCallingConv(llvm::CallingConv::C); |
| 4419 | |
| 4420 | #ifdef ENABLE_RR_DEBUG_INFO |
| 4421 | ::debugInfo = std::unique_ptr<DebugInfo>(new DebugInfo(::builder, ::context, ::module, ::function)); |
| 4422 | #endif // ENABLE_RR_DEBUG_INFO |
| 4423 | |
| 4424 | auto entryBlock = llvm::BasicBlock::Create(*::context, "coroutine", ::function); |
| 4425 | ::coroutine.suspendBlock = llvm::BasicBlock::Create(*::context, "suspend", ::function); |
| 4426 | ::coroutine.endBlock = llvm::BasicBlock::Create(*::context, "end", ::function); |
| 4427 | ::coroutine.destroyBlock = llvm::BasicBlock::Create(*::context, "destroy", ::function); |
| 4428 | |
| 4429 | ::builder->SetInsertPoint(entryBlock); |
| 4430 | Variable::materializeAll(); |
| 4431 | ::coroutine.promise = ::builder->CreateAlloca(T(YieldType), nullptr, "promise"); |
| 4432 | ::coroutine.id = ::builder->CreateCall(coro_id, { |
| 4433 | ::llvm::ConstantInt::get(i32Ty, 0), |
| 4434 | ::builder->CreatePointerCast(::coroutine.promise, i8PtrTy), |
| 4435 | ::llvm::ConstantPointerNull::get(i8PtrTy), |
| 4436 | ::llvm::ConstantPointerNull::get(i8PtrTy), |
| 4437 | }); |
| 4438 | auto size = ::builder->CreateCall(coro_size, {}); |
| 4439 | auto frame = ::builder->CreateCall(allocFrame, {size}); |
| 4440 | ::coroutine.handle = ::builder->CreateCall(coro_begin, {::coroutine.id, frame}); |
| 4441 | |
| 4442 | // Build the suspend block |
| 4443 | ::builder->SetInsertPoint(::coroutine.suspendBlock); |
| 4444 | ::builder->CreateCall(coro_end, {::coroutine.handle, ::llvm::ConstantInt::get(i1Ty, 0)}); |
| 4445 | ::builder->CreateRet(::coroutine.handle); |
| 4446 | |
| 4447 | // Build the end block |
| 4448 | ::builder->SetInsertPoint(::coroutine.endBlock); |
| 4449 | auto action = ::builder->CreateCall(coro_suspend, { |
| 4450 | ::llvm::ConstantTokenNone::get(*::context), |
| 4451 | ::llvm::ConstantInt::get(i1Ty, 1), // final: true |
| 4452 | }); |
| 4453 | auto switch_ = ::builder->CreateSwitch(action, ::coroutine.suspendBlock, 3); |
| 4454 | // switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), trapBlock); // TODO: Trap attempting to resume after final suspend |
| 4455 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), ::coroutine.destroyBlock); |
| 4456 | |
| 4457 | // Build the destroy block |
| 4458 | ::builder->SetInsertPoint(::coroutine.destroyBlock); |
| 4459 | auto memory = ::builder->CreateCall(coro_free, {::coroutine.id, ::coroutine.handle}); |
| 4460 | ::builder->CreateCall(freeFrame, {memory}); |
| 4461 | ::builder->CreateBr(::coroutine.suspendBlock); |
| 4462 | |
| 4463 | // Switch back to the entry block for reactor codegen. |
| 4464 | ::builder->SetInsertPoint(entryBlock); |
| 4465 | |
| 4466 | #if defined(_WIN32) |
| 4467 | // FIXME(capn): |
| 4468 | // On Windows, stack memory is committed in increments of 4 kB pages, with the last page |
| 4469 | // having a trap which allows the OS to grow the stack. For functions with a stack frame |
| 4470 | // larger than 4 kB this can cause an issue when a variable is accessed beyond the guard |
| 4471 | // page. Therefore the compiler emits a call to __chkstk in the function prolog to probe |
| 4472 | // the stack and ensure all pages have been committed. This is currently broken in LLVM |
| 4473 | // JIT, but we can prevent emitting the stack probe call: |
| 4474 | ::function->addFnAttr("stack-probe-size", "1048576"); |
| 4475 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4476 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4477 | |
| 4478 | void Nucleus::yield(Value* val) |
| 4479 | { |
| 4480 | ASSERT_MSG(::coroutine.id != nullptr, "yield() can only be called when building a Coroutine"); |
| 4481 | |
| 4482 | // promise = val; |
| 4483 | // |
| 4484 | // auto action = llvm.coro.suspend(none, false /* final */); // <-- RESUME POINT |
| 4485 | // switch (action) |
| 4486 | // { |
| 4487 | // case SuspendActionResume: |
| 4488 | // goto resume; |
| 4489 | // case SuspendActionDestroy: |
| 4490 | // goto destroy; |
| 4491 | // default: // (SuspendActionSuspend) |
| 4492 | // goto suspend; |
| 4493 | // } |
| 4494 | // resume: |
| 4495 | // |
| 4496 | |
| 4497 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 4498 | Variable::materializeAll(); |
| 4499 | |
| 4500 | // Types |
| 4501 | auto i1Ty = ::llvm::Type::getInt1Ty(*::context); |
| 4502 | auto i8Ty = ::llvm::Type::getInt8Ty(*::context); |
| 4503 | |
| 4504 | // Intrinsics |
| 4505 | auto coro_suspend = ::llvm::Intrinsic::getDeclaration(::module, ::llvm::Intrinsic::coro_suspend); |
| 4506 | |
| 4507 | // Create a block to resume execution. |
| 4508 | auto resumeBlock = llvm::BasicBlock::Create(*::context, "resume", ::function); |
| 4509 | |
| 4510 | // Store the promise (yield value) |
| 4511 | ::builder->CreateStore(V(val), ::coroutine.promise); |
| 4512 | auto action = ::builder->CreateCall(coro_suspend, { |
| 4513 | ::llvm::ConstantTokenNone::get(*::context), |
| 4514 | ::llvm::ConstantInt::get(i1Ty, 0), // final: true |
| 4515 | }); |
| 4516 | auto switch_ = ::builder->CreateSwitch(action, ::coroutine.suspendBlock, 3); |
| 4517 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), resumeBlock); |
| 4518 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), ::coroutine.destroyBlock); |
| 4519 | |
| 4520 | // Continue building in the resume block. |
| 4521 | ::builder->SetInsertPoint(resumeBlock); |
| 4522 | } |
| 4523 | |
| 4524 | Routine* Nucleus::acquireCoroutine(const char *name, bool runOptimizations) |
| 4525 | { |
| 4526 | ASSERT_MSG(::coroutine.id != nullptr, "acquireCoroutine() called without a call to createCoroutine()"); |
| 4527 | |
| 4528 | ::builder->CreateBr(::coroutine.endBlock); |
| 4529 | |
| 4530 | #ifdef ENABLE_RR_DEBUG_INFO |
| 4531 | if (debugInfo != nullptr) |
| 4532 | { |
| 4533 | debugInfo->Finalize(); |
| 4534 | } |
| 4535 | #endif // ENABLE_RR_DEBUG_INFO |
| 4536 | |
| 4537 | if(false) |
| 4538 | { |
| 4539 | std::error_code error; |
| 4540 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error); |
| 4541 | ::module->print(file, 0); |
| 4542 | } |
| 4543 | |
| 4544 | // Run manadory coroutine transforms. |
| 4545 | llvm::legacy::PassManager pm; |
| 4546 | pm.add(llvm::createCoroEarlyPass()); |
| 4547 | pm.add(llvm::createCoroSplitPass()); |
| 4548 | pm.add(llvm::createCoroElidePass()); |
| 4549 | pm.add(llvm::createBarrierNoopPass()); |
| 4550 | pm.add(llvm::createCoroCleanupPass()); |
| 4551 | pm.run(*::module); |
| 4552 | |
| 4553 | if(runOptimizations) |
| 4554 | { |
| 4555 | optimize(); |
| 4556 | } |
| 4557 | |
| 4558 | if(false) |
| 4559 | { |
| 4560 | std::error_code error; |
| 4561 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error); |
| 4562 | ::module->print(file, 0); |
| 4563 | } |
| 4564 | |
| 4565 | llvm::Function *funcs[Nucleus::CoroutineEntryCount]; |
| 4566 | funcs[Nucleus::CoroutineEntryBegin] = ::function; |
| 4567 | funcs[Nucleus::CoroutineEntryAwait] = ::coroutine.await; |
| 4568 | funcs[Nucleus::CoroutineEntryDestroy] = ::coroutine.destroy; |
| 4569 | Routine *routine = ::reactorJIT->acquireRoutine(funcs, Nucleus::CoroutineEntryCount); |
| 4570 | |
| 4571 | ::coroutine = CoroutineState{}; |
| 4572 | |
| 4573 | return routine; |
| 4574 | } |
| 4575 | |
| 4576 | } // namespace rr |