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 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 15 | #include "LLVMReactor.hpp" |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 16 | |
| 17 | #include "CPUID.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 18 | #include "Debug.hpp" |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 19 | #include "LLVMReactorDebugInfo.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 20 | #include "Reactor.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 21 | #include "x86.hpp" |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 22 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Intrinsics.h" |
Nicolas Capens | ba9de97 | 2020-01-30 10:14:30 -0500 | [diff] [blame] | 24 | #if LLVM_VERSION_MAJOR >= 9 |
Nicolas Capens | 6c3dc35 | 2020-01-28 09:22:39 -0500 | [diff] [blame] | 25 | # include "llvm/IR/IntrinsicsX86.h" |
| 26 | #endif |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 27 | #include "llvm/IR/LegacyPassManager.h" |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 28 | #include "llvm/IR/Verifier.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 29 | #include "llvm/Transforms/Coroutines.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 30 | #include "llvm/Transforms/IPO.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 31 | #include "llvm/Transforms/Scalar.h" |
Antonio Maiorano | 992bf9d | 2019-10-22 09:41:17 -0400 | [diff] [blame] | 32 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 33 | #define ARGS(...) \ |
| 34 | { \ |
| 35 | __VA_ARGS__ \ |
| 36 | } |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 37 | #define CreateCall2 CreateCall |
| 38 | #define CreateCall3 CreateCall |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 39 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 40 | #include <fstream> |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 41 | #include <iostream> |
| 42 | #include <mutex> |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 43 | #include <numeric> |
| 44 | #include <thread> |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 45 | #include <unordered_map> |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 46 | |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 47 | #if defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 48 | # include <xmmintrin.h> |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 49 | #endif |
| 50 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 51 | #include <math.h> |
| 52 | |
Nicolas Capens | cb12258 | 2014-05-06 23:34:44 -0400 | [diff] [blame] | 53 | #if defined(__x86_64__) && defined(_WIN32) |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 54 | extern "C" void X86CompilationCallback() |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 55 | { |
| 56 | UNIMPLEMENTED("X86CompilationCallback"); |
| 57 | } |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 58 | #endif |
| 59 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 60 | namespace { |
| 61 | |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 62 | std::unique_ptr<rr::JITBuilder> jit; |
| 63 | std::mutex codegenMutex; |
| 64 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 65 | // Default configuration settings. Must be accessed under mutex lock. |
| 66 | std::mutex defaultConfigLock; |
| 67 | rr::Config &defaultConfig() |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 68 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 69 | // This uses a static in a function to avoid the cost of a global static |
| 70 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 71 | static rr::Config config = rr::Config::Edit() |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 72 | .add(rr::Optimization::Pass::ScalarReplAggregates) |
| 73 | .add(rr::Optimization::Pass::InstructionCombining) |
| 74 | .apply({}); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 75 | return config; |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 76 | } |
| 77 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 78 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 79 | std::string replace(std::string str, const std::string &substr, const std::string &replacement) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 80 | { |
| 81 | size_t pos = 0; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 82 | while((pos = str.find(substr, pos)) != std::string::npos) |
| 83 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 84 | str.replace(pos, substr.length(), replacement); |
| 85 | pos += replacement.length(); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 86 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 87 | return str; |
| 88 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 89 | #endif // ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 90 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 91 | llvm::Value *lowerPAVG(llvm::Value *x, llvm::Value *y) |
| 92 | { |
| 93 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 94 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 95 | llvm::VectorType *extTy = |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 96 | llvm::VectorType::getExtendedElementVectorType(ty); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 97 | x = jit->builder->CreateZExt(x, extTy); |
| 98 | y = jit->builder->CreateZExt(y, extTy); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 99 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 100 | // (x + y + 1) >> 1 |
| 101 | llvm::Constant *one = llvm::ConstantInt::get(extTy, 1); |
| 102 | llvm::Value *res = jit->builder->CreateAdd(x, y); |
| 103 | res = jit->builder->CreateAdd(res, one); |
| 104 | res = jit->builder->CreateLShr(res, one); |
| 105 | return jit->builder->CreateTrunc(res, ty); |
| 106 | } |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 107 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 108 | llvm::Value *lowerPMINMAX(llvm::Value *x, llvm::Value *y, |
| 109 | llvm::ICmpInst::Predicate pred) |
| 110 | { |
| 111 | return jit->builder->CreateSelect(jit->builder->CreateICmp(pred, x, y), x, y); |
| 112 | } |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 113 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 114 | llvm::Value *lowerPCMP(llvm::ICmpInst::Predicate pred, llvm::Value *x, |
| 115 | llvm::Value *y, llvm::Type *dstTy) |
| 116 | { |
| 117 | return jit->builder->CreateSExt(jit->builder->CreateICmp(pred, x, y), dstTy, ""); |
| 118 | } |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 119 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 120 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 121 | llvm::Value *lowerPMOV(llvm::Value *op, llvm::Type *dstType, bool sext) |
| 122 | { |
| 123 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(op->getType()); |
| 124 | llvm::VectorType *dstTy = llvm::cast<llvm::VectorType>(dstType); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 125 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 126 | llvm::Value *undef = llvm::UndefValue::get(srcTy); |
| 127 | llvm::SmallVector<uint32_t, 16> mask(dstTy->getNumElements()); |
| 128 | std::iota(mask.begin(), mask.end(), 0); |
| 129 | llvm::Value *v = jit->builder->CreateShuffleVector(op, undef, mask); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 130 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 131 | return sext ? jit->builder->CreateSExt(v, dstTy) |
| 132 | : jit->builder->CreateZExt(v, dstTy); |
| 133 | } |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 134 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 135 | llvm::Value *lowerPABS(llvm::Value *v) |
| 136 | { |
| 137 | llvm::Value *zero = llvm::Constant::getNullValue(v->getType()); |
| 138 | llvm::Value *cmp = jit->builder->CreateICmp(llvm::ICmpInst::ICMP_SGT, v, zero); |
| 139 | llvm::Value *neg = jit->builder->CreateNeg(v); |
| 140 | return jit->builder->CreateSelect(cmp, v, neg); |
| 141 | } |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 142 | #endif // defined(__i386__) || defined(__x86_64__) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 143 | |
| 144 | #if !defined(__i386__) && !defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 145 | llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y, |
| 146 | llvm::FCmpInst::Predicate pred) |
| 147 | { |
| 148 | return jit->builder->CreateSelect(jit->builder->CreateFCmp(pred, x, y), x, y); |
| 149 | } |
| 150 | |
| 151 | llvm::Value *lowerRound(llvm::Value *x) |
| 152 | { |
| 153 | llvm::Function *nearbyint = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 154 | jit->module.get(), llvm::Intrinsic::nearbyint, { x->getType() }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 155 | return jit->builder->CreateCall(nearbyint, ARGS(x)); |
| 156 | } |
| 157 | |
| 158 | llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty) |
| 159 | { |
| 160 | return jit->builder->CreateFPToSI(lowerRound(x), ty); |
| 161 | } |
| 162 | |
| 163 | llvm::Value *lowerFloor(llvm::Value *x) |
| 164 | { |
| 165 | llvm::Function *floor = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 166 | jit->module.get(), llvm::Intrinsic::floor, { x->getType() }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 167 | return jit->builder->CreateCall(floor, ARGS(x)); |
| 168 | } |
| 169 | |
| 170 | llvm::Value *lowerTrunc(llvm::Value *x) |
| 171 | { |
| 172 | llvm::Function *trunc = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 173 | jit->module.get(), llvm::Intrinsic::trunc, { x->getType() }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 174 | return jit->builder->CreateCall(trunc, ARGS(x)); |
| 175 | } |
| 176 | |
| 177 | // Packed add/sub with saturation |
| 178 | llvm::Value *lowerPSAT(llvm::Value *x, llvm::Value *y, bool isAdd, bool isSigned) |
| 179 | { |
| 180 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 181 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 182 | |
| 183 | unsigned numBits = ty->getScalarSizeInBits(); |
| 184 | |
| 185 | llvm::Value *max, *min, *extX, *extY; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 186 | if(isSigned) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 187 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 188 | max = llvm::ConstantInt::get(extTy, (1LL << (numBits - 1)) - 1, true); |
| 189 | min = llvm::ConstantInt::get(extTy, (-1LL << (numBits - 1)), true); |
| 190 | extX = jit->builder->CreateSExt(x, extTy); |
| 191 | extY = jit->builder->CreateSExt(y, extTy); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | ASSERT_MSG(numBits <= 64, "numBits: %d", int(numBits)); |
| 196 | uint64_t maxVal = (numBits == 64) ? ~0ULL : (1ULL << numBits) - 1; |
| 197 | max = llvm::ConstantInt::get(extTy, maxVal, false); |
| 198 | min = llvm::ConstantInt::get(extTy, 0, false); |
| 199 | extX = jit->builder->CreateZExt(x, extTy); |
| 200 | extY = jit->builder->CreateZExt(y, extTy); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 201 | } |
| 202 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 203 | llvm::Value *res = isAdd ? jit->builder->CreateAdd(extX, extY) |
| 204 | : jit->builder->CreateSub(extX, extY); |
| 205 | |
| 206 | res = lowerPMINMAX(res, min, llvm::ICmpInst::ICMP_SGT); |
| 207 | res = lowerPMINMAX(res, max, llvm::ICmpInst::ICMP_SLT); |
| 208 | |
| 209 | return jit->builder->CreateTrunc(res, ty); |
| 210 | } |
| 211 | |
| 212 | llvm::Value *lowerSQRT(llvm::Value *x) |
| 213 | { |
| 214 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 215 | jit->module.get(), llvm::Intrinsic::sqrt, { x->getType() }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 216 | return jit->builder->CreateCall(sqrt, ARGS(x)); |
| 217 | } |
| 218 | |
| 219 | llvm::Value *lowerRCP(llvm::Value *x) |
| 220 | { |
| 221 | llvm::Type *ty = x->getType(); |
| 222 | llvm::Constant *one; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 223 | if(llvm::VectorType *vectorTy = llvm::dyn_cast<llvm::VectorType>(ty)) |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 224 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 225 | one = llvm::ConstantVector::getSplat( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 226 | vectorTy->getNumElements(), |
| 227 | llvm::ConstantFP::get(vectorTy->getElementType(), 1)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 228 | } |
| 229 | else |
| 230 | { |
| 231 | one = llvm::ConstantFP::get(ty, 1); |
| 232 | } |
| 233 | return jit->builder->CreateFDiv(one, x); |
| 234 | } |
| 235 | |
| 236 | llvm::Value *lowerRSQRT(llvm::Value *x) |
| 237 | { |
| 238 | return lowerRCP(lowerSQRT(x)); |
| 239 | } |
| 240 | |
| 241 | llvm::Value *lowerVectorShl(llvm::Value *x, uint64_t scalarY) |
| 242 | { |
| 243 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 244 | llvm::Value *y = llvm::ConstantVector::getSplat( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 245 | ty->getNumElements(), |
| 246 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 247 | return jit->builder->CreateShl(x, y); |
| 248 | } |
| 249 | |
| 250 | llvm::Value *lowerVectorAShr(llvm::Value *x, uint64_t scalarY) |
| 251 | { |
| 252 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 253 | llvm::Value *y = llvm::ConstantVector::getSplat( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 254 | ty->getNumElements(), |
| 255 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 256 | return jit->builder->CreateAShr(x, y); |
| 257 | } |
| 258 | |
| 259 | llvm::Value *lowerVectorLShr(llvm::Value *x, uint64_t scalarY) |
| 260 | { |
| 261 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 262 | llvm::Value *y = llvm::ConstantVector::getSplat( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 263 | ty->getNumElements(), |
| 264 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 265 | return jit->builder->CreateLShr(x, y); |
| 266 | } |
| 267 | |
| 268 | llvm::Value *lowerMulAdd(llvm::Value *x, llvm::Value *y) |
| 269 | { |
| 270 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 271 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 272 | |
| 273 | llvm::Value *extX = jit->builder->CreateSExt(x, extTy); |
| 274 | llvm::Value *extY = jit->builder->CreateSExt(y, extTy); |
| 275 | llvm::Value *mult = jit->builder->CreateMul(extX, extY); |
| 276 | |
| 277 | llvm::Value *undef = llvm::UndefValue::get(extTy); |
| 278 | |
| 279 | llvm::SmallVector<uint32_t, 16> evenIdx; |
| 280 | llvm::SmallVector<uint32_t, 16> oddIdx; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 281 | for(uint64_t i = 0, n = ty->getNumElements(); i < n; i += 2) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 282 | { |
| 283 | evenIdx.push_back(i); |
| 284 | oddIdx.push_back(i + 1); |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 285 | } |
| 286 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 287 | llvm::Value *lhs = jit->builder->CreateShuffleVector(mult, undef, evenIdx); |
| 288 | llvm::Value *rhs = jit->builder->CreateShuffleVector(mult, undef, oddIdx); |
| 289 | return jit->builder->CreateAdd(lhs, rhs); |
| 290 | } |
| 291 | |
| 292 | llvm::Value *lowerPack(llvm::Value *x, llvm::Value *y, bool isSigned) |
| 293 | { |
| 294 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(x->getType()); |
| 295 | llvm::VectorType *dstTy = llvm::VectorType::getTruncatedElementVectorType(srcTy); |
| 296 | |
| 297 | llvm::IntegerType *dstElemTy = |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 298 | llvm::cast<llvm::IntegerType>(dstTy->getElementType()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 299 | |
| 300 | uint64_t truncNumBits = dstElemTy->getIntegerBitWidth(); |
| 301 | ASSERT_MSG(truncNumBits < 64, "shift 64 must be handled separately. truncNumBits: %d", int(truncNumBits)); |
| 302 | llvm::Constant *max, *min; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 303 | if(isSigned) |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 304 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 305 | max = llvm::ConstantInt::get(srcTy, (1LL << (truncNumBits - 1)) - 1, true); |
| 306 | min = llvm::ConstantInt::get(srcTy, (-1LL << (truncNumBits - 1)), true); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | max = llvm::ConstantInt::get(srcTy, (1ULL << truncNumBits) - 1, false); |
| 311 | min = llvm::ConstantInt::get(srcTy, 0, false); |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 312 | } |
| 313 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 314 | x = lowerPMINMAX(x, min, llvm::ICmpInst::ICMP_SGT); |
| 315 | x = lowerPMINMAX(x, max, llvm::ICmpInst::ICMP_SLT); |
| 316 | y = lowerPMINMAX(y, min, llvm::ICmpInst::ICMP_SGT); |
| 317 | y = lowerPMINMAX(y, max, llvm::ICmpInst::ICMP_SLT); |
| 318 | |
| 319 | x = jit->builder->CreateTrunc(x, dstTy); |
| 320 | y = jit->builder->CreateTrunc(y, dstTy); |
| 321 | |
| 322 | llvm::SmallVector<uint32_t, 16> index(srcTy->getNumElements() * 2); |
| 323 | std::iota(index.begin(), index.end(), 0); |
| 324 | |
| 325 | return jit->builder->CreateShuffleVector(x, y, index); |
| 326 | } |
| 327 | |
| 328 | llvm::Value *lowerSignMask(llvm::Value *x, llvm::Type *retTy) |
| 329 | { |
| 330 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 331 | llvm::Constant *zero = llvm::ConstantInt::get(ty, 0); |
| 332 | llvm::Value *cmp = jit->builder->CreateICmpSLT(x, zero); |
| 333 | |
| 334 | llvm::Value *ret = jit->builder->CreateZExt( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 335 | jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 336 | for(uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 337 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 338 | llvm::Value *elem = jit->builder->CreateZExt( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 339 | jit->builder->CreateExtractElement(cmp, i), retTy); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 340 | ret = jit->builder->CreateOr(ret, jit->builder->CreateShl(elem, i)); |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 341 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 342 | return ret; |
| 343 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 344 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 345 | llvm::Value *lowerFPSignMask(llvm::Value *x, llvm::Type *retTy) |
| 346 | { |
| 347 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 348 | llvm::Constant *zero = llvm::ConstantFP::get(ty, 0); |
| 349 | llvm::Value *cmp = jit->builder->CreateFCmpULT(x, zero); |
| 350 | |
| 351 | llvm::Value *ret = jit->builder->CreateZExt( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 352 | jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 353 | for(uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 354 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 355 | llvm::Value *elem = jit->builder->CreateZExt( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 356 | jit->builder->CreateExtractElement(cmp, i), retTy); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 357 | ret = jit->builder->CreateOr(ret, jit->builder->CreateShl(elem, i)); |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 358 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 359 | return ret; |
| 360 | } |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 361 | #endif // !defined(__i386__) && !defined(__x86_64__) |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 362 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 363 | #if(LLVM_VERSION_MAJOR >= 8) || (!defined(__i386__) && !defined(__x86_64__)) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 364 | llvm::Value *lowerPUADDSAT(llvm::Value *x, llvm::Value *y) |
| 365 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 366 | # if LLVM_VERSION_MAJOR >= 8 |
| 367 | return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::uadd_sat, x, y); |
| 368 | # else |
| 369 | return lowerPSAT(x, y, true, false); |
| 370 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 371 | } |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 372 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 373 | llvm::Value *lowerPSADDSAT(llvm::Value *x, llvm::Value *y) |
| 374 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 375 | # if LLVM_VERSION_MAJOR >= 8 |
| 376 | return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::sadd_sat, x, y); |
| 377 | # else |
| 378 | return lowerPSAT(x, y, true, true); |
| 379 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 380 | } |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 381 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 382 | llvm::Value *lowerPUSUBSAT(llvm::Value *x, llvm::Value *y) |
| 383 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 384 | # if LLVM_VERSION_MAJOR >= 8 |
| 385 | return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::usub_sat, x, y); |
| 386 | # else |
| 387 | return lowerPSAT(x, y, false, false); |
| 388 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 389 | } |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 390 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 391 | llvm::Value *lowerPSSUBSAT(llvm::Value *x, llvm::Value *y) |
| 392 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 393 | # if LLVM_VERSION_MAJOR >= 8 |
| 394 | return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::ssub_sat, x, y); |
| 395 | # else |
| 396 | return lowerPSAT(x, y, false, true); |
| 397 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 398 | } |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 399 | #endif // (LLVM_VERSION_MAJOR >= 8) || (!defined(__i386__) && !defined(__x86_64__)) |
| 400 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 401 | llvm::Value *lowerMulHigh(llvm::Value *x, llvm::Value *y, bool sext) |
| 402 | { |
| 403 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 404 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 405 | |
| 406 | llvm::Value *extX, *extY; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 407 | if(sext) |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 408 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 409 | extX = jit->builder->CreateSExt(x, extTy); |
| 410 | extY = jit->builder->CreateSExt(y, extTy); |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | extX = jit->builder->CreateZExt(x, extTy); |
| 415 | extY = jit->builder->CreateZExt(y, extTy); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 416 | } |
Antonio Maiorano | e6ab470 | 2019-11-29 11:26:30 -0500 | [diff] [blame] | 417 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 418 | llvm::Value *mult = jit->builder->CreateMul(extX, extY); |
| 419 | |
| 420 | llvm::IntegerType *intTy = llvm::cast<llvm::IntegerType>(ty->getElementType()); |
| 421 | llvm::Value *mulh = jit->builder->CreateAShr(mult, intTy->getBitWidth()); |
| 422 | return jit->builder->CreateTrunc(mulh, ty); |
| 423 | } |
| 424 | |
| 425 | llvm::Value *createGather(llvm::Value *base, llvm::Type *elTy, llvm::Value *offsets, llvm::Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 426 | { |
| 427 | ASSERT(base->getType()->isPointerTy()); |
| 428 | ASSERT(offsets->getType()->isVectorTy()); |
| 429 | ASSERT(mask->getType()->isVectorTy()); |
| 430 | |
| 431 | auto numEls = mask->getType()->getVectorNumElements(); |
| 432 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 433 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 434 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 435 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 436 | auto elPtrTy = elTy->getPointerTo(); |
| 437 | auto elVecTy = ::llvm::VectorType::get(elTy, numEls); |
| 438 | auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls); |
| 439 | auto i8Base = jit->builder->CreatePointerCast(base, i8PtrTy); |
| 440 | auto i8Ptrs = jit->builder->CreateGEP(i8Base, offsets); |
| 441 | auto elPtrs = jit->builder->CreatePointerCast(i8Ptrs, elPtrVecTy); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 442 | auto i8Mask = jit->builder->CreateIntCast(mask, ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 443 | auto passthrough = zeroMaskedLanes ? ::llvm::Constant::getNullValue(elVecTy) : llvm::UndefValue::get(elVecTy); |
| 444 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 445 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_gather, { elVecTy, elPtrVecTy }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 446 | return jit->builder->CreateCall(func, { elPtrs, align, i8Mask, passthrough }); |
| 447 | } |
| 448 | |
| 449 | void createScatter(llvm::Value *base, llvm::Value *val, llvm::Value *offsets, llvm::Value *mask, unsigned int alignment) |
| 450 | { |
| 451 | ASSERT(base->getType()->isPointerTy()); |
| 452 | ASSERT(val->getType()->isVectorTy()); |
| 453 | ASSERT(offsets->getType()->isVectorTy()); |
| 454 | ASSERT(mask->getType()->isVectorTy()); |
| 455 | |
| 456 | auto numEls = mask->getType()->getVectorNumElements(); |
| 457 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 458 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 459 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 460 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 461 | auto elVecTy = val->getType(); |
| 462 | auto elTy = elVecTy->getVectorElementType(); |
| 463 | auto elPtrTy = elTy->getPointerTo(); |
| 464 | auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls); |
| 465 | auto i8Base = jit->builder->CreatePointerCast(base, i8PtrTy); |
| 466 | auto i8Ptrs = jit->builder->CreateGEP(i8Base, offsets); |
| 467 | auto elPtrs = jit->builder->CreatePointerCast(i8Ptrs, elPtrVecTy); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 468 | auto i8Mask = jit->builder->CreateIntCast(mask, ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 469 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 470 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_scatter, { elVecTy, elPtrVecTy }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 471 | jit->builder->CreateCall(func, { val, elPtrs, align, i8Mask }); |
| 472 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 473 | } // namespace |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 474 | |
| 475 | namespace rr { |
| 476 | |
Antonio Maiorano | ab210f9 | 2019-12-13 16:26:24 -0500 | [diff] [blame] | 477 | std::string BackendName() |
| 478 | { |
| 479 | return std::string("LLVM ") + LLVM_VERSION_STRING; |
| 480 | } |
| 481 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 482 | const Capabilities Caps = { |
| 483 | true, // CoroutinesSupported |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 484 | }; |
| 485 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 486 | // The abstract Type* types are implemented as LLVM types, except that |
| 487 | // 64-bit vectors are emulated using 128-bit ones to avoid use of MMX in x86 |
| 488 | // and VFP in ARM, and eliminate the overhead of converting them to explicit |
| 489 | // 128-bit ones. LLVM types are pointers, so we can represent emulated types |
| 490 | // as abstract pointers with small enum values. |
| 491 | enum InternalType : uintptr_t |
| 492 | { |
| 493 | // Emulated types: |
| 494 | Type_v2i32, |
| 495 | Type_v4i16, |
| 496 | Type_v2i16, |
| 497 | Type_v8i8, |
| 498 | Type_v4i8, |
| 499 | Type_v2f32, |
| 500 | EmulatedTypeCount, |
| 501 | // Returned by asInternalType() to indicate that the abstract Type* |
| 502 | // should be interpreted as LLVM type pointer: |
| 503 | Type_LLVM |
| 504 | }; |
| 505 | |
| 506 | inline InternalType asInternalType(Type *type) |
| 507 | { |
| 508 | InternalType t = static_cast<InternalType>(reinterpret_cast<uintptr_t>(type)); |
| 509 | return (t < EmulatedTypeCount) ? t : Type_LLVM; |
| 510 | } |
| 511 | |
| 512 | llvm::Type *T(Type *t) |
| 513 | { |
| 514 | // Use 128-bit vectors to implement logically shorter ones. |
| 515 | switch(asInternalType(t)) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 516 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 517 | case Type_v2i32: return T(Int4::getType()); |
| 518 | case Type_v4i16: return T(Short8::getType()); |
| 519 | case Type_v2i16: return T(Short8::getType()); |
| 520 | case Type_v8i8: return T(Byte16::getType()); |
| 521 | case Type_v4i8: return T(Byte16::getType()); |
| 522 | case Type_v2f32: return T(Float4::getType()); |
| 523 | case Type_LLVM: return reinterpret_cast<llvm::Type *>(t); |
| 524 | default: |
| 525 | UNREACHABLE("asInternalType(t): %d", int(asInternalType(t))); |
| 526 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | Type *T(InternalType t) |
| 531 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 532 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 533 | } |
| 534 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 535 | inline const std::vector<llvm::Type *> &T(const std::vector<Type *> &t) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 536 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 537 | return reinterpret_cast<const std::vector<llvm::Type *> &>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | inline llvm::BasicBlock *B(BasicBlock *t) |
| 541 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 542 | return reinterpret_cast<llvm::BasicBlock *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | inline BasicBlock *B(llvm::BasicBlock *t) |
| 546 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 547 | return reinterpret_cast<BasicBlock *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static size_t typeSize(Type *type) |
| 551 | { |
| 552 | switch(asInternalType(type)) |
| 553 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 554 | case Type_v2i32: return 8; |
| 555 | case Type_v4i16: return 8; |
| 556 | case Type_v2i16: return 4; |
| 557 | case Type_v8i8: return 8; |
| 558 | case Type_v4i8: return 4; |
| 559 | case Type_v2f32: return 8; |
| 560 | case Type_LLVM: |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 561 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 562 | llvm::Type *t = T(type); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 563 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 564 | if(t->isPointerTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 565 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 566 | return sizeof(void *); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 567 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 568 | |
| 569 | // At this point we should only have LLVM 'primitive' types. |
| 570 | unsigned int bits = t->getPrimitiveSizeInBits(); |
| 571 | ASSERT_MSG(bits != 0, "bits: %d", int(bits)); |
| 572 | |
| 573 | // TODO(capn): Booleans are 1 bit integers in LLVM's SSA type system, |
| 574 | // but are typically stored as one byte. The DataLayout structure should |
| 575 | // be used here and many other places if this assumption fails. |
| 576 | return (bits + 7) / 8; |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 577 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 578 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 579 | default: |
| 580 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 581 | return 0; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 582 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 583 | } |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 584 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 585 | static unsigned int elementCount(Type *type) |
| 586 | { |
| 587 | switch(asInternalType(type)) |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 588 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 589 | case Type_v2i32: return 2; |
| 590 | case Type_v4i16: return 4; |
| 591 | case Type_v2i16: return 2; |
| 592 | case Type_v8i8: return 8; |
| 593 | case Type_v4i8: return 4; |
| 594 | case Type_v2f32: return 2; |
| 595 | case Type_LLVM: return llvm::cast<llvm::VectorType>(T(type))->getNumElements(); |
| 596 | default: |
| 597 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 598 | return 0; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 602 | static ::llvm::Function *createFunction(const char *name, ::llvm::Type *retTy, const std::vector<::llvm::Type *> ¶ms) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 603 | { |
| 604 | llvm::FunctionType *functionType = llvm::FunctionType::get(retTy, params, false); |
| 605 | auto func = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, name, jit->module.get()); |
| 606 | func->setDoesNotThrow(); |
| 607 | func->setCallingConv(llvm::CallingConv::C); |
| 608 | return func; |
| 609 | } |
| 610 | |
| 611 | Nucleus::Nucleus() |
| 612 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 613 | ::codegenMutex.lock(); // Reactor and LLVM are currently not thread safe |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 614 | |
| 615 | ASSERT(jit == nullptr); |
| 616 | jit.reset(new JITBuilder(Nucleus::getDefaultConfig())); |
| 617 | } |
| 618 | |
| 619 | Nucleus::~Nucleus() |
| 620 | { |
| 621 | jit.reset(); |
| 622 | ::codegenMutex.unlock(); |
| 623 | } |
| 624 | |
| 625 | void Nucleus::setDefaultConfig(const Config &cfg) |
| 626 | { |
| 627 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 628 | ::defaultConfig() = cfg; |
| 629 | } |
| 630 | |
| 631 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 632 | { |
| 633 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 634 | auto &config = ::defaultConfig(); |
| 635 | config = cfgEdit.apply(config); |
| 636 | } |
| 637 | |
| 638 | Config Nucleus::getDefaultConfig() |
| 639 | { |
| 640 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 641 | return ::defaultConfig(); |
| 642 | } |
| 643 | |
| 644 | std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 645 | { |
| 646 | auto cfg = cfgEdit.apply(jit->config); |
| 647 | |
| 648 | if(jit->builder->GetInsertBlock()->empty() || !jit->builder->GetInsertBlock()->back().isTerminator()) |
| 649 | { |
| 650 | llvm::Type *type = jit->function->getReturnType(); |
| 651 | |
| 652 | if(type->isVoidTy()) |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 653 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 654 | createRetVoid(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 655 | } |
| 656 | else |
| 657 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 658 | createRet(V(llvm::UndefValue::get(type))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 659 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 660 | } |
| 661 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 662 | #ifdef ENABLE_RR_DEBUG_INFO |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 663 | if(jit->debugInfo != nullptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 664 | { |
| 665 | jit->debugInfo->Finalize(); |
| 666 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 667 | #endif // ENABLE_RR_DEBUG_INFO |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 668 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 669 | if(false) |
| 670 | { |
| 671 | std::error_code error; |
| 672 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error); |
| 673 | jit->module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 674 | } |
| 675 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 676 | #if defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 677 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 678 | llvm::legacy::PassManager pm; |
| 679 | pm.add(llvm::createVerifierPass()); |
| 680 | pm.run(*jit->module); |
| 681 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 682 | #endif // defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 683 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 684 | jit->optimize(cfg); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 685 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 686 | if(false) |
| 687 | { |
| 688 | std::error_code error; |
| 689 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error); |
| 690 | jit->module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 691 | } |
| 692 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 693 | auto routine = jit->acquireRoutine(&jit->function, 1, cfg); |
| 694 | jit.reset(); |
| 695 | |
| 696 | return routine; |
| 697 | } |
| 698 | |
| 699 | Value *Nucleus::allocateStackVariable(Type *type, int arraySize) |
| 700 | { |
| 701 | // Need to allocate it in the entry block for mem2reg to work |
| 702 | llvm::BasicBlock &entryBlock = jit->function->getEntryBlock(); |
| 703 | |
| 704 | llvm::Instruction *declaration; |
| 705 | |
| 706 | if(arraySize) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 707 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 708 | declaration = new llvm::AllocaInst(T(type), 0, V(Nucleus::createConstantInt(arraySize))); |
| 709 | } |
| 710 | else |
| 711 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 712 | declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value *)nullptr); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 715 | entryBlock.getInstList().push_front(declaration); |
| 716 | |
| 717 | return V(declaration); |
| 718 | } |
| 719 | |
| 720 | BasicBlock *Nucleus::createBasicBlock() |
| 721 | { |
| 722 | return B(llvm::BasicBlock::Create(jit->context, "", jit->function)); |
| 723 | } |
| 724 | |
| 725 | BasicBlock *Nucleus::getInsertBlock() |
| 726 | { |
| 727 | return B(jit->builder->GetInsertBlock()); |
| 728 | } |
| 729 | |
| 730 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 731 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 732 | // assert(jit->builder->GetInsertBlock()->back().isTerminator()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 733 | |
| 734 | Variable::materializeAll(); |
| 735 | |
| 736 | jit->builder->SetInsertPoint(B(basicBlock)); |
| 737 | } |
| 738 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 739 | void Nucleus::createFunction(Type *ReturnType, const std::vector<Type *> &Params) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 740 | { |
| 741 | jit->function = rr::createFunction("", T(ReturnType), T(Params)); |
| 742 | |
| 743 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 368d39c | 2020-01-08 23:10:47 +0000 | [diff] [blame] | 744 | jit->debugInfo = std::make_unique<DebugInfo>(jit->builder.get(), &jit->context, jit->module.get(), jit->function); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 745 | #endif // ENABLE_RR_DEBUG_INFO |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 746 | |
| 747 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->function)); |
| 748 | } |
| 749 | |
| 750 | Value *Nucleus::getArgument(unsigned int index) |
| 751 | { |
| 752 | llvm::Function::arg_iterator args = jit->function->arg_begin(); |
| 753 | |
| 754 | while(index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 755 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 756 | args++; |
| 757 | index--; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 758 | } |
| 759 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 760 | return V(&*args); |
| 761 | } |
| 762 | |
| 763 | void Nucleus::createRetVoid() |
| 764 | { |
| 765 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 766 | |
| 767 | ASSERT_MSG(jit->function->getReturnType() == T(Void::getType()), "Return type mismatch"); |
| 768 | |
| 769 | // Code generated after this point is unreachable, so any variables |
| 770 | // being read can safely return an undefined value. We have to avoid |
| 771 | // materializing variables after the terminator ret instruction. |
| 772 | Variable::killUnmaterialized(); |
| 773 | |
| 774 | jit->builder->CreateRetVoid(); |
| 775 | } |
| 776 | |
| 777 | void Nucleus::createRet(Value *v) |
| 778 | { |
| 779 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 780 | |
| 781 | ASSERT_MSG(jit->function->getReturnType() == V(v)->getType(), "Return type mismatch"); |
| 782 | |
| 783 | // Code generated after this point is unreachable, so any variables |
| 784 | // being read can safely return an undefined value. We have to avoid |
| 785 | // materializing variables after the terminator ret instruction. |
| 786 | Variable::killUnmaterialized(); |
| 787 | |
| 788 | jit->builder->CreateRet(V(v)); |
| 789 | } |
| 790 | |
| 791 | void Nucleus::createBr(BasicBlock *dest) |
| 792 | { |
| 793 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 794 | Variable::materializeAll(); |
| 795 | |
| 796 | jit->builder->CreateBr(B(dest)); |
| 797 | } |
| 798 | |
| 799 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
| 800 | { |
| 801 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 802 | Variable::materializeAll(); |
| 803 | jit->builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse)); |
| 804 | } |
| 805 | |
| 806 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 807 | { |
| 808 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 809 | return V(jit->builder->CreateAdd(V(lhs), V(rhs))); |
| 810 | } |
| 811 | |
| 812 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 813 | { |
| 814 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 815 | return V(jit->builder->CreateSub(V(lhs), V(rhs))); |
| 816 | } |
| 817 | |
| 818 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 819 | { |
| 820 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 821 | return V(jit->builder->CreateMul(V(lhs), V(rhs))); |
| 822 | } |
| 823 | |
| 824 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 825 | { |
| 826 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 827 | return V(jit->builder->CreateUDiv(V(lhs), V(rhs))); |
| 828 | } |
| 829 | |
| 830 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 831 | { |
| 832 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 833 | return V(jit->builder->CreateSDiv(V(lhs), V(rhs))); |
| 834 | } |
| 835 | |
| 836 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 837 | { |
| 838 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 839 | return V(jit->builder->CreateFAdd(V(lhs), V(rhs))); |
| 840 | } |
| 841 | |
| 842 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 843 | { |
| 844 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 845 | return V(jit->builder->CreateFSub(V(lhs), V(rhs))); |
| 846 | } |
| 847 | |
| 848 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 849 | { |
| 850 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 851 | return V(jit->builder->CreateFMul(V(lhs), V(rhs))); |
| 852 | } |
| 853 | |
| 854 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 855 | { |
| 856 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 857 | return V(jit->builder->CreateFDiv(V(lhs), V(rhs))); |
| 858 | } |
| 859 | |
| 860 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 861 | { |
| 862 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 863 | return V(jit->builder->CreateURem(V(lhs), V(rhs))); |
| 864 | } |
| 865 | |
| 866 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 867 | { |
| 868 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 869 | return V(jit->builder->CreateSRem(V(lhs), V(rhs))); |
| 870 | } |
| 871 | |
| 872 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 873 | { |
| 874 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 875 | return V(jit->builder->CreateFRem(V(lhs), V(rhs))); |
| 876 | } |
| 877 | |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 878 | RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs) |
| 879 | { |
| 880 | return RValue<Float4>(Nucleus::createFRem(lhs.value, rhs.value)); |
| 881 | } |
| 882 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 883 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 884 | { |
| 885 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 886 | return V(jit->builder->CreateShl(V(lhs), V(rhs))); |
| 887 | } |
| 888 | |
| 889 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 890 | { |
| 891 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 892 | return V(jit->builder->CreateLShr(V(lhs), V(rhs))); |
| 893 | } |
| 894 | |
| 895 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 896 | { |
| 897 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 898 | return V(jit->builder->CreateAShr(V(lhs), V(rhs))); |
| 899 | } |
| 900 | |
| 901 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 902 | { |
| 903 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 904 | return V(jit->builder->CreateAnd(V(lhs), V(rhs))); |
| 905 | } |
| 906 | |
| 907 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 908 | { |
| 909 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 910 | return V(jit->builder->CreateOr(V(lhs), V(rhs))); |
| 911 | } |
| 912 | |
| 913 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 914 | { |
| 915 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 916 | return V(jit->builder->CreateXor(V(lhs), V(rhs))); |
| 917 | } |
| 918 | |
| 919 | Value *Nucleus::createNeg(Value *v) |
| 920 | { |
| 921 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 922 | return V(jit->builder->CreateNeg(V(v))); |
| 923 | } |
| 924 | |
| 925 | Value *Nucleus::createFNeg(Value *v) |
| 926 | { |
| 927 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 928 | return V(jit->builder->CreateFNeg(V(v))); |
| 929 | } |
| 930 | |
| 931 | Value *Nucleus::createNot(Value *v) |
| 932 | { |
| 933 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 934 | return V(jit->builder->CreateNot(V(v))); |
| 935 | } |
| 936 | |
| 937 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder) |
| 938 | { |
| 939 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 940 | switch(asInternalType(type)) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 941 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 942 | case Type_v2i32: |
| 943 | case Type_v4i16: |
| 944 | case Type_v8i8: |
| 945 | case Type_v2f32: |
| 946 | return createBitCast( |
| 947 | createInsertElement( |
| 948 | V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))), |
| 949 | createLoad(createBitCast(ptr, Pointer<Long>::getType()), Long::getType(), isVolatile, alignment, atomic, memoryOrder), |
| 950 | 0), |
| 951 | type); |
| 952 | case Type_v2i16: |
| 953 | case Type_v4i8: |
| 954 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
| 955 | { |
| 956 | Value *u = V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))); |
| 957 | Value *i = createLoad(createBitCast(ptr, Pointer<Int>::getType()), Int::getType(), isVolatile, alignment, atomic, memoryOrder); |
| 958 | i = createZExt(i, Long::getType()); |
| 959 | Value *v = createInsertElement(u, i, 0); |
| 960 | return createBitCast(v, type); |
| 961 | } |
| 962 | // Fallthrough to non-emulated case. |
| 963 | case Type_LLVM: |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 964 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 965 | auto elTy = T(type); |
| 966 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
| 967 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 968 | if(!atomic) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 969 | { |
| 970 | return V(jit->builder->CreateAlignedLoad(V(ptr), alignment, isVolatile)); |
| 971 | } |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 972 | else if(elTy->isIntegerTy() || elTy->isPointerTy()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 973 | { |
| 974 | // Integers and pointers can be atomically loaded by setting |
| 975 | // the ordering constraint on the load instruction. |
| 976 | auto load = jit->builder->CreateAlignedLoad(V(ptr), alignment, isVolatile); |
| 977 | load->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 978 | return V(load); |
| 979 | } |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 980 | else if(elTy->isFloatTy() || elTy->isDoubleTy()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 981 | { |
| 982 | // LLVM claims to support atomic loads of float types as |
| 983 | // above, but certain backends cannot deal with this. |
| 984 | // Load as an integer and bitcast. See b/136037244. |
| 985 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
| 986 | auto elAsIntTy = ::llvm::IntegerType::get(jit->context, size * 8); |
| 987 | auto ptrCast = jit->builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo()); |
| 988 | auto load = jit->builder->CreateAlignedLoad(ptrCast, alignment, isVolatile); |
| 989 | load->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 990 | auto loadCast = jit->builder->CreateBitCast(load, elTy); |
| 991 | return V(loadCast); |
| 992 | } |
| 993 | else |
| 994 | { |
| 995 | // More exotic types require falling back to the extern: |
| 996 | // void __atomic_load(size_t size, void *ptr, void *ret, int ordering) |
| 997 | auto sizetTy = ::llvm::IntegerType::get(jit->context, sizeof(size_t) * 8); |
| 998 | auto intTy = ::llvm::IntegerType::get(jit->context, sizeof(int) * 8); |
| 999 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 1000 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 1001 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1002 | auto funcTy = ::llvm::FunctionType::get(voidTy, { sizetTy, i8PtrTy, i8PtrTy, intTy }, false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1003 | auto func = jit->module->getOrInsertFunction("__atomic_load", funcTy); |
| 1004 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
| 1005 | auto out = allocateStackVariable(type); |
| 1006 | jit->builder->CreateCall(func, { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1007 | ::llvm::ConstantInt::get(sizetTy, size), |
| 1008 | jit->builder->CreatePointerCast(V(ptr), i8PtrTy), |
| 1009 | jit->builder->CreatePointerCast(V(out), i8PtrTy), |
| 1010 | ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))), |
| 1011 | }); |
| 1012 | return V(jit->builder->CreateLoad(V(out))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1013 | } |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1014 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1015 | default: |
| 1016 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1017 | return nullptr; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1018 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1019 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1020 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1021 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder) |
| 1022 | { |
| 1023 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1024 | switch(asInternalType(type)) |
Ben Clayton | 204a410 | 2019-07-31 13:17:47 +0100 | [diff] [blame] | 1025 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1026 | case Type_v2i32: |
| 1027 | case Type_v4i16: |
| 1028 | case Type_v8i8: |
| 1029 | case Type_v2f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1030 | createStore( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1031 | createExtractElement( |
| 1032 | createBitCast(value, T(llvm::VectorType::get(T(Long::getType()), 2))), Long::getType(), 0), |
| 1033 | createBitCast(ptr, Pointer<Long>::getType()), |
| 1034 | Long::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1035 | return value; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1036 | case Type_v2i16: |
| 1037 | case Type_v4i8: |
| 1038 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
| 1039 | { |
| 1040 | createStore( |
| 1041 | createExtractElement(createBitCast(value, Int4::getType()), Int::getType(), 0), |
| 1042 | createBitCast(ptr, Pointer<Int>::getType()), |
| 1043 | Int::getType(), isVolatile, alignment, atomic, memoryOrder); |
| 1044 | return value; |
| 1045 | } |
| 1046 | // Fallthrough to non-emulated case. |
| 1047 | case Type_LLVM: |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1048 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1049 | auto elTy = T(type); |
| 1050 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
| 1051 | |
Antonio Maiorano | dd48b7e | 2020-02-05 13:17:07 -0500 | [diff] [blame] | 1052 | #if __has_feature(memory_sanitizer) |
| 1053 | // Mark all memory writes as initialized by calling __msan_unpoison |
| 1054 | { |
| 1055 | // void __msan_unpoison(const volatile void *a, size_t size) |
| 1056 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
| 1057 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 1058 | auto voidPtrTy = i8Ty->getPointerTo(); |
| 1059 | auto sizetTy = ::llvm::IntegerType::get(jit->context, sizeof(size_t) * 8); |
| 1060 | auto funcTy = ::llvm::FunctionType::get(voidTy, { voidPtrTy, sizetTy }, false); |
| 1061 | auto func = jit->module->getOrInsertFunction("__msan_unpoison", funcTy); |
| 1062 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
| 1063 | jit->builder->CreateCall(func, { jit->builder->CreatePointerCast(V(ptr), voidPtrTy), |
| 1064 | ::llvm::ConstantInt::get(sizetTy, size) }); |
| 1065 | } |
| 1066 | #endif |
| 1067 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1068 | if(!atomic) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1069 | { |
| 1070 | jit->builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile); |
| 1071 | } |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1072 | else if(elTy->isIntegerTy() || elTy->isPointerTy()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1073 | { |
| 1074 | // Integers and pointers can be atomically stored by setting |
| 1075 | // the ordering constraint on the store instruction. |
| 1076 | auto store = jit->builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile); |
| 1077 | store->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1078 | } |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1079 | else if(elTy->isFloatTy() || elTy->isDoubleTy()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1080 | { |
| 1081 | // LLVM claims to support atomic stores of float types as |
| 1082 | // above, but certain backends cannot deal with this. |
| 1083 | // Store as an bitcast integer. See b/136037244. |
| 1084 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
| 1085 | auto elAsIntTy = ::llvm::IntegerType::get(jit->context, size * 8); |
| 1086 | auto valCast = jit->builder->CreateBitCast(V(value), elAsIntTy); |
| 1087 | auto ptrCast = jit->builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo()); |
| 1088 | auto store = jit->builder->CreateAlignedStore(valCast, ptrCast, alignment, isVolatile); |
| 1089 | store->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1090 | } |
| 1091 | else |
| 1092 | { |
| 1093 | // More exotic types require falling back to the extern: |
| 1094 | // void __atomic_store(size_t size, void *ptr, void *val, int ordering) |
| 1095 | auto sizetTy = ::llvm::IntegerType::get(jit->context, sizeof(size_t) * 8); |
| 1096 | auto intTy = ::llvm::IntegerType::get(jit->context, sizeof(int) * 8); |
| 1097 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 1098 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 1099 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1100 | auto funcTy = ::llvm::FunctionType::get(voidTy, { sizetTy, i8PtrTy, i8PtrTy, intTy }, false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1101 | auto func = jit->module->getOrInsertFunction("__atomic_store", funcTy); |
| 1102 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
| 1103 | auto copy = allocateStackVariable(type); |
| 1104 | jit->builder->CreateStore(V(value), V(copy)); |
| 1105 | jit->builder->CreateCall(func, { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1106 | ::llvm::ConstantInt::get(sizetTy, size), |
| 1107 | jit->builder->CreatePointerCast(V(ptr), i8PtrTy), |
| 1108 | jit->builder->CreatePointerCast(V(copy), i8PtrTy), |
| 1109 | ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))), |
| 1110 | }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | return value; |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1114 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1115 | default: |
| 1116 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1117 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1118 | } |
| 1119 | } |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1120 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1121 | Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 1122 | { |
| 1123 | ASSERT(V(ptr)->getType()->isPointerTy()); |
| 1124 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1125 | |
| 1126 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
| 1127 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 1128 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 1129 | auto elVecTy = ::llvm::VectorType::get(T(elTy), numEls); |
| 1130 | auto elVecPtrTy = elVecTy->getPointerTo(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1131 | auto i8Mask = jit->builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1132 | auto passthrough = zeroMaskedLanes ? ::llvm::Constant::getNullValue(elVecTy) : llvm::UndefValue::get(elVecTy); |
| 1133 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1134 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_load, { elVecTy, elVecPtrTy }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1135 | return V(jit->builder->CreateCall(func, { V(ptr), align, i8Mask, passthrough })); |
| 1136 | } |
| 1137 | |
| 1138 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 1139 | { |
| 1140 | ASSERT(V(ptr)->getType()->isPointerTy()); |
| 1141 | ASSERT(V(val)->getType()->isVectorTy()); |
| 1142 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1143 | |
| 1144 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
| 1145 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 1146 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 1147 | auto elVecTy = V(val)->getType(); |
| 1148 | auto elVecPtrTy = elVecTy->getPointerTo(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1149 | auto i8Mask = jit->builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1150 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1151 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_store, { elVecTy, elVecPtrTy }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1152 | jit->builder->CreateCall(func, { V(val), V(ptr), align, i8Mask }); |
| 1153 | } |
| 1154 | |
| 1155 | RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 1156 | { |
| 1157 | return As<Float4>(V(createGather(V(base.value), T(Float::getType()), V(offsets.value), V(mask.value), alignment, zeroMaskedLanes))); |
| 1158 | } |
| 1159 | |
| 1160 | RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 1161 | { |
| 1162 | return As<Int4>(V(createGather(V(base.value), T(Float::getType()), V(offsets.value), V(mask.value), alignment, zeroMaskedLanes))); |
| 1163 | } |
| 1164 | |
| 1165 | void Scatter(RValue<Pointer<Float>> base, RValue<Float4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 1166 | { |
| 1167 | return createScatter(V(base.value), V(val.value), V(offsets.value), V(mask.value), alignment); |
| 1168 | } |
| 1169 | |
| 1170 | void Scatter(RValue<Pointer<Int>> base, RValue<Int4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 1171 | { |
| 1172 | return createScatter(V(base.value), V(val.value), V(offsets.value), V(mask.value), alignment); |
| 1173 | } |
| 1174 | |
| 1175 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 1176 | { |
| 1177 | jit->builder->CreateFence(atomicOrdering(true, memoryOrder)); |
| 1178 | } |
| 1179 | |
| 1180 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
| 1181 | { |
| 1182 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1183 | ASSERT(V(ptr)->getType()->getContainedType(0) == T(type)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1184 | if(sizeof(void *) == 8) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1185 | { |
| 1186 | // LLVM manual: "When indexing into an array, pointer or vector, |
| 1187 | // integers of any width are allowed, and they are not required to |
| 1188 | // be constant. These integers are treated as signed values where |
| 1189 | // relevant." |
| 1190 | // |
| 1191 | // Thus if we want indexes to be treated as unsigned we have to |
| 1192 | // zero-extend them ourselves. |
| 1193 | // |
| 1194 | // Note that this is not because we want to address anywhere near |
| 1195 | // 4 GB of data. Instead this is important for performance because |
| 1196 | // x86 supports automatic zero-extending of 32-bit registers to |
| 1197 | // 64-bit. Thus when indexing into an array using a uint32 is |
| 1198 | // actually faster than an int32. |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1199 | index = unsignedIndex ? createZExt(index, Long::getType()) : createSExt(index, Long::getType()); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1200 | } |
| 1201 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1202 | // For non-emulated types we can rely on LLVM's GEP to calculate the |
| 1203 | // effective address correctly. |
| 1204 | if(asInternalType(type) == Type_LLVM) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1205 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1206 | return V(jit->builder->CreateGEP(V(ptr), V(index))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1207 | } |
| 1208 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1209 | // For emulated types we have to multiply the index by the intended |
| 1210 | // type size ourselves to obain the byte offset. |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1211 | index = (sizeof(void *) == 8) ? createMul(index, createConstantLong((int64_t)typeSize(type))) : createMul(index, createConstantInt((int)typeSize(type))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1212 | |
| 1213 | // Cast to a byte pointer, apply the byte offset, and cast back to the |
| 1214 | // original pointer type. |
| 1215 | return createBitCast( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1216 | V(jit->builder->CreateGEP(V(createBitCast(ptr, T(llvm::PointerType::get(T(Byte::getType()), 0)))), V(index))), |
| 1217 | T(llvm::PointerType::get(T(type), 0))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1221 | { |
| 1222 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1223 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Add, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1224 | } |
| 1225 | |
| 1226 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1227 | { |
| 1228 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1229 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Sub, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1230 | } |
| 1231 | |
| 1232 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1233 | { |
| 1234 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1235 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::And, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1236 | } |
| 1237 | |
| 1238 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1239 | { |
| 1240 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1241 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Or, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1242 | } |
| 1243 | |
| 1244 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1245 | { |
| 1246 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1247 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xor, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1248 | } |
| 1249 | |
| 1250 | Value *Nucleus::createAtomicMin(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1251 | { |
| 1252 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1253 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Min, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1254 | } |
| 1255 | |
| 1256 | Value *Nucleus::createAtomicMax(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1257 | { |
| 1258 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1259 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Max, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1260 | } |
| 1261 | |
| 1262 | Value *Nucleus::createAtomicUMin(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1263 | { |
| 1264 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1265 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMin, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1266 | } |
| 1267 | |
| 1268 | Value *Nucleus::createAtomicUMax(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1269 | { |
| 1270 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1271 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMax, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1272 | } |
| 1273 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1274 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1275 | { |
| 1276 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1277 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
| 1278 | } |
| 1279 | |
| 1280 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1281 | { |
| 1282 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1283 | // Note: AtomicCmpXchgInstruction returns a 2-member struct containing {result, success-flag}, not the result directly. |
| 1284 | return V(jit->builder->CreateExtractValue( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1285 | jit->builder->CreateAtomicCmpXchg(V(ptr), V(compare), V(value), atomicOrdering(true, memoryOrderEqual), atomicOrdering(true, memoryOrderUnequal)), |
| 1286 | llvm::ArrayRef<unsigned>(0u))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
| 1290 | { |
| 1291 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1292 | return V(jit->builder->CreateTrunc(V(v), T(destType))); |
| 1293 | } |
| 1294 | |
| 1295 | Value *Nucleus::createZExt(Value *v, Type *destType) |
| 1296 | { |
| 1297 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1298 | return V(jit->builder->CreateZExt(V(v), T(destType))); |
| 1299 | } |
| 1300 | |
| 1301 | Value *Nucleus::createSExt(Value *v, Type *destType) |
| 1302 | { |
| 1303 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1304 | return V(jit->builder->CreateSExt(V(v), T(destType))); |
| 1305 | } |
| 1306 | |
| 1307 | Value *Nucleus::createFPToUI(Value *v, Type *destType) |
| 1308 | { |
| 1309 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1310 | return V(jit->builder->CreateFPToUI(V(v), T(destType))); |
| 1311 | } |
| 1312 | |
| 1313 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
| 1314 | { |
| 1315 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1316 | return V(jit->builder->CreateFPToSI(V(v), T(destType))); |
| 1317 | } |
| 1318 | |
| 1319 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
| 1320 | { |
| 1321 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1322 | return V(jit->builder->CreateSIToFP(V(v), T(destType))); |
| 1323 | } |
| 1324 | |
| 1325 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
| 1326 | { |
| 1327 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1328 | return V(jit->builder->CreateFPTrunc(V(v), T(destType))); |
| 1329 | } |
| 1330 | |
| 1331 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
| 1332 | { |
| 1333 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1334 | return V(jit->builder->CreateFPExt(V(v), T(destType))); |
| 1335 | } |
| 1336 | |
| 1337 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 1338 | { |
| 1339 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1340 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1341 | // support for casting between scalars and wide vectors. Emulate them by writing to the stack and |
| 1342 | // reading back as the destination type. |
| 1343 | if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy()) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1344 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1345 | Value *readAddress = allocateStackVariable(destType); |
| 1346 | Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0))); |
| 1347 | createStore(v, writeAddress, T(V(v)->getType())); |
| 1348 | return createLoad(readAddress, destType); |
| 1349 | } |
| 1350 | else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy()) |
| 1351 | { |
| 1352 | Value *writeAddress = allocateStackVariable(T(V(v)->getType())); |
| 1353 | createStore(v, writeAddress, T(V(v)->getType())); |
| 1354 | Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0))); |
| 1355 | return createLoad(readAddress, destType); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1356 | } |
| 1357 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1358 | return V(jit->builder->CreateBitCast(V(v), T(destType))); |
| 1359 | } |
| 1360 | |
| 1361 | Value *Nucleus::createPtrEQ(Value *lhs, Value *rhs) |
| 1362 | { |
| 1363 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1364 | return V(jit->builder->CreateICmpEQ(V(lhs), V(rhs))); |
| 1365 | } |
| 1366 | |
| 1367 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1368 | { |
| 1369 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1370 | return V(jit->builder->CreateICmpEQ(V(lhs), V(rhs))); |
| 1371 | } |
| 1372 | |
| 1373 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1374 | { |
| 1375 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1376 | return V(jit->builder->CreateICmpNE(V(lhs), V(rhs))); |
| 1377 | } |
| 1378 | |
| 1379 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1380 | { |
| 1381 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1382 | return V(jit->builder->CreateICmpUGT(V(lhs), V(rhs))); |
| 1383 | } |
| 1384 | |
| 1385 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1386 | { |
| 1387 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1388 | return V(jit->builder->CreateICmpUGE(V(lhs), V(rhs))); |
| 1389 | } |
| 1390 | |
| 1391 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1392 | { |
| 1393 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1394 | return V(jit->builder->CreateICmpULT(V(lhs), V(rhs))); |
| 1395 | } |
| 1396 | |
| 1397 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1398 | { |
| 1399 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1400 | return V(jit->builder->CreateICmpULE(V(lhs), V(rhs))); |
| 1401 | } |
| 1402 | |
| 1403 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1404 | { |
| 1405 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1406 | return V(jit->builder->CreateICmpSGT(V(lhs), V(rhs))); |
| 1407 | } |
| 1408 | |
| 1409 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1410 | { |
| 1411 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1412 | return V(jit->builder->CreateICmpSGE(V(lhs), V(rhs))); |
| 1413 | } |
| 1414 | |
| 1415 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1416 | { |
| 1417 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1418 | return V(jit->builder->CreateICmpSLT(V(lhs), V(rhs))); |
| 1419 | } |
| 1420 | |
| 1421 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1422 | { |
| 1423 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1424 | return V(jit->builder->CreateICmpSLE(V(lhs), V(rhs))); |
| 1425 | } |
| 1426 | |
| 1427 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1428 | { |
| 1429 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1430 | return V(jit->builder->CreateFCmpOEQ(V(lhs), V(rhs))); |
| 1431 | } |
| 1432 | |
| 1433 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1434 | { |
| 1435 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1436 | return V(jit->builder->CreateFCmpOGT(V(lhs), V(rhs))); |
| 1437 | } |
| 1438 | |
| 1439 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1440 | { |
| 1441 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1442 | return V(jit->builder->CreateFCmpOGE(V(lhs), V(rhs))); |
| 1443 | } |
| 1444 | |
| 1445 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1446 | { |
| 1447 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1448 | return V(jit->builder->CreateFCmpOLT(V(lhs), V(rhs))); |
| 1449 | } |
| 1450 | |
| 1451 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1452 | { |
| 1453 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1454 | return V(jit->builder->CreateFCmpOLE(V(lhs), V(rhs))); |
| 1455 | } |
| 1456 | |
| 1457 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1458 | { |
| 1459 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1460 | return V(jit->builder->CreateFCmpONE(V(lhs), V(rhs))); |
| 1461 | } |
| 1462 | |
| 1463 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1464 | { |
| 1465 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1466 | return V(jit->builder->CreateFCmpORD(V(lhs), V(rhs))); |
| 1467 | } |
| 1468 | |
| 1469 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1470 | { |
| 1471 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1472 | return V(jit->builder->CreateFCmpUNO(V(lhs), V(rhs))); |
| 1473 | } |
| 1474 | |
| 1475 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1476 | { |
| 1477 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1478 | return V(jit->builder->CreateFCmpUEQ(V(lhs), V(rhs))); |
| 1479 | } |
| 1480 | |
| 1481 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1482 | { |
| 1483 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1484 | return V(jit->builder->CreateFCmpUGT(V(lhs), V(rhs))); |
| 1485 | } |
| 1486 | |
| 1487 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1488 | { |
| 1489 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1490 | return V(jit->builder->CreateFCmpUGE(V(lhs), V(rhs))); |
| 1491 | } |
| 1492 | |
| 1493 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1494 | { |
| 1495 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1496 | return V(jit->builder->CreateFCmpULT(V(lhs), V(rhs))); |
| 1497 | } |
| 1498 | |
| 1499 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1500 | { |
| 1501 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1502 | return V(jit->builder->CreateFCmpULE(V(lhs), V(rhs))); |
| 1503 | } |
| 1504 | |
| 1505 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1506 | { |
| 1507 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1508 | return V(jit->builder->CreateFCmpUNE(V(lhs), V(rhs))); |
| 1509 | } |
| 1510 | |
| 1511 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
| 1512 | { |
| 1513 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1514 | ASSERT(V(vector)->getType()->getContainedType(0) == T(type)); |
| 1515 | return V(jit->builder->CreateExtractElement(V(vector), V(createConstantInt(index)))); |
| 1516 | } |
| 1517 | |
| 1518 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1519 | { |
| 1520 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1521 | return V(jit->builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index)))); |
| 1522 | } |
| 1523 | |
| 1524 | Value *Nucleus::createShuffleVector(Value *v1, Value *v2, const int *select) |
| 1525 | { |
| 1526 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1527 | |
| 1528 | int size = llvm::cast<llvm::VectorType>(V(v1)->getType())->getNumElements(); |
| 1529 | const int maxSize = 16; |
| 1530 | llvm::Constant *swizzle[maxSize]; |
| 1531 | ASSERT(size <= maxSize); |
| 1532 | |
| 1533 | for(int i = 0; i < size; i++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1534 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1535 | swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), select[i]); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1536 | } |
| 1537 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1538 | llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant *>(swizzle, size)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1539 | |
| 1540 | return V(jit->builder->CreateShuffleVector(V(v1), V(v2), shuffle)); |
| 1541 | } |
| 1542 | |
| 1543 | Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse) |
| 1544 | { |
| 1545 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1546 | return V(jit->builder->CreateSelect(V(c), V(ifTrue), V(ifFalse))); |
| 1547 | } |
| 1548 | |
| 1549 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
| 1550 | { |
| 1551 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1552 | return reinterpret_cast<SwitchCases *>(jit->builder->CreateSwitch(V(control), B(defaultBranch), numCases)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
| 1556 | { |
| 1557 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1558 | llvm::SwitchInst *sw = reinterpret_cast<llvm::SwitchInst *>(switchCases); |
| 1559 | sw->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), label, true), B(branch)); |
| 1560 | } |
| 1561 | |
| 1562 | void Nucleus::createUnreachable() |
| 1563 | { |
| 1564 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1565 | jit->builder->CreateUnreachable(); |
| 1566 | } |
| 1567 | |
| 1568 | Type *Nucleus::getPointerType(Type *ElementType) |
| 1569 | { |
| 1570 | return T(llvm::PointerType::get(T(ElementType), 0)); |
| 1571 | } |
| 1572 | |
| 1573 | Value *Nucleus::createNullValue(Type *Ty) |
| 1574 | { |
| 1575 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1576 | return V(llvm::Constant::getNullValue(T(Ty))); |
| 1577 | } |
| 1578 | |
| 1579 | Value *Nucleus::createConstantLong(int64_t i) |
| 1580 | { |
| 1581 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1582 | return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(jit->context), i, true)); |
| 1583 | } |
| 1584 | |
| 1585 | Value *Nucleus::createConstantInt(int i) |
| 1586 | { |
| 1587 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1588 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), i, true)); |
| 1589 | } |
| 1590 | |
| 1591 | Value *Nucleus::createConstantInt(unsigned int i) |
| 1592 | { |
| 1593 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1594 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), i, false)); |
| 1595 | } |
| 1596 | |
| 1597 | Value *Nucleus::createConstantBool(bool b) |
| 1598 | { |
| 1599 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1600 | return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(jit->context), b)); |
| 1601 | } |
| 1602 | |
| 1603 | Value *Nucleus::createConstantByte(signed char i) |
| 1604 | { |
| 1605 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1606 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(jit->context), i, true)); |
| 1607 | } |
| 1608 | |
| 1609 | Value *Nucleus::createConstantByte(unsigned char i) |
| 1610 | { |
| 1611 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1612 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(jit->context), i, false)); |
| 1613 | } |
| 1614 | |
| 1615 | Value *Nucleus::createConstantShort(short i) |
| 1616 | { |
| 1617 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1618 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(jit->context), i, true)); |
| 1619 | } |
| 1620 | |
| 1621 | Value *Nucleus::createConstantShort(unsigned short i) |
| 1622 | { |
| 1623 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1624 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(jit->context), i, false)); |
| 1625 | } |
| 1626 | |
| 1627 | Value *Nucleus::createConstantFloat(float x) |
| 1628 | { |
| 1629 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1630 | return V(llvm::ConstantFP::get(T(Float::getType()), x)); |
| 1631 | } |
| 1632 | |
| 1633 | Value *Nucleus::createNullPointer(Type *Ty) |
| 1634 | { |
| 1635 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1636 | return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0))); |
| 1637 | } |
| 1638 | |
| 1639 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
| 1640 | { |
| 1641 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1642 | const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. |
| 1643 | const int numElements = llvm::cast<llvm::VectorType>(T(type))->getNumElements(); // Number of elements of the underlying vector type. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1644 | ASSERT(numElements <= 16 && numConstants <= numElements); |
| 1645 | llvm::Constant *constantVector[16]; |
| 1646 | |
| 1647 | for(int i = 0; i < numElements; i++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1648 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1649 | constantVector[i] = llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1650 | } |
| 1651 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1652 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant *>(constantVector, numElements))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 1656 | { |
| 1657 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1658 | const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. |
| 1659 | const int numElements = llvm::cast<llvm::VectorType>(T(type))->getNumElements(); // Number of elements of the underlying vector type. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1660 | ASSERT(numElements <= 8 && numConstants <= numElements); |
| 1661 | llvm::Constant *constantVector[8]; |
| 1662 | |
| 1663 | for(int i = 0; i < numElements; i++) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1664 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1665 | constantVector[i] = llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1666 | } |
| 1667 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1668 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant *>(constantVector, numElements))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1669 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1670 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1671 | Type *Void::getType() |
| 1672 | { |
| 1673 | return T(llvm::Type::getVoidTy(jit->context)); |
| 1674 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1675 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1676 | Type *Bool::getType() |
| 1677 | { |
| 1678 | return T(llvm::Type::getInt1Ty(jit->context)); |
| 1679 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1680 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1681 | Type *Byte::getType() |
| 1682 | { |
| 1683 | return T(llvm::Type::getInt8Ty(jit->context)); |
| 1684 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1685 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1686 | Type *SByte::getType() |
| 1687 | { |
| 1688 | return T(llvm::Type::getInt8Ty(jit->context)); |
| 1689 | } |
| 1690 | |
| 1691 | Type *Short::getType() |
| 1692 | { |
| 1693 | return T(llvm::Type::getInt16Ty(jit->context)); |
| 1694 | } |
| 1695 | |
| 1696 | Type *UShort::getType() |
| 1697 | { |
| 1698 | return T(llvm::Type::getInt16Ty(jit->context)); |
| 1699 | } |
| 1700 | |
| 1701 | Type *Byte4::getType() |
| 1702 | { |
| 1703 | return T(Type_v4i8); |
| 1704 | } |
| 1705 | |
| 1706 | Type *SByte4::getType() |
| 1707 | { |
| 1708 | return T(Type_v4i8); |
| 1709 | } |
| 1710 | |
| 1711 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 1712 | { |
| 1713 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1714 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1715 | return x86::paddusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1716 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1717 | return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1718 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1719 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1720 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1721 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 1722 | { |
| 1723 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1724 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1725 | return x86::psubusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1726 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1727 | return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1728 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1729 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1730 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1731 | RValue<Int> SignMask(RValue<Byte8> x) |
| 1732 | { |
| 1733 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1734 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1735 | return x86::pmovmskb(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1736 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1737 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1738 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1739 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1740 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1741 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1742 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1743 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1744 | // return x86::pcmpgtb(x, y); // FIXME: Signedness |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1745 | //#else |
| 1746 | // return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 1747 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1748 | // } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1749 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1750 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 1751 | { |
| 1752 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1753 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1754 | return x86::pcmpeqb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1755 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1756 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1757 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1758 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1759 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1760 | Type *Byte8::getType() |
| 1761 | { |
| 1762 | return T(Type_v8i8); |
| 1763 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1764 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1765 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 1766 | { |
| 1767 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1768 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1769 | return x86::paddsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1770 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1771 | return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1772 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1773 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1774 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1775 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 1776 | { |
| 1777 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1778 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1779 | return x86::psubsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1780 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1781 | return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1782 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1783 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1784 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1785 | RValue<Int> SignMask(RValue<SByte8> x) |
| 1786 | { |
| 1787 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1788 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1789 | return x86::pmovmskb(As<Byte8>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1790 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1791 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1792 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1793 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1794 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1795 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
| 1796 | { |
| 1797 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1798 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1799 | return x86::pcmpgtb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1800 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1801 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1802 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1803 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1804 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1805 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 1806 | { |
| 1807 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1808 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1809 | return x86::pcmpeqb(As<Byte8>(x), As<Byte8>(y)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1810 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1811 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1812 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1813 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1814 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1815 | Type *SByte8::getType() |
| 1816 | { |
| 1817 | return T(Type_v8i8); |
| 1818 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1819 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1820 | Type *Byte16::getType() |
| 1821 | { |
| 1822 | return T(llvm::VectorType::get(T(Byte::getType()), 16)); |
| 1823 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1824 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1825 | Type *SByte16::getType() |
| 1826 | { |
| 1827 | return T(llvm::VectorType::get(T(SByte::getType()), 16)); |
| 1828 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1829 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1830 | Type *Short2::getType() |
| 1831 | { |
| 1832 | return T(Type_v2i16); |
| 1833 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1834 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1835 | Type *UShort2::getType() |
| 1836 | { |
| 1837 | return T(Type_v2i16); |
| 1838 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1839 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1840 | Short4::Short4(RValue<Int4> cast) |
| 1841 | { |
| 1842 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1843 | int select[8] = { 0, 2, 4, 6, 0, 2, 4, 6 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1844 | Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1845 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1846 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 1847 | Value *short4 = As<Short4>(Int2(As<Int4>(packed))).value; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1848 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1849 | storeValue(short4); |
| 1850 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1851 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1852 | // Short4::Short4(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1853 | // { |
| 1854 | // } |
| 1855 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1856 | Short4::Short4(RValue<Float4> cast) |
| 1857 | { |
| 1858 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1859 | Int4 v4i32 = Int4(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1860 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1861 | v4i32 = As<Int4>(x86::packssdw(v4i32, v4i32)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1862 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1863 | Value *v = v4i32.loadValue(); |
| 1864 | v4i32 = As<Int4>(V(lowerPack(V(v), V(v), true))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1865 | #endif |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1866 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1867 | storeValue(As<Short4>(Int2(v4i32)).value); |
| 1868 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1869 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1870 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 1871 | { |
| 1872 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1873 | #if defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1874 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1875 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1876 | return x86::psllw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1877 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1878 | return As<Short4>(V(lowerVectorShl(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1879 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1880 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1881 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1882 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 1883 | { |
| 1884 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1885 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1886 | return x86::psraw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1887 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1888 | return As<Short4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1889 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1890 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1891 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1892 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 1893 | { |
| 1894 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1895 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1896 | return x86::pmaxsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1897 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1898 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1899 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1900 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1901 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1902 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 1903 | { |
| 1904 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1905 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1906 | return x86::pminsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1907 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1908 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1909 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1910 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1911 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1912 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 1913 | { |
| 1914 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1915 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1916 | return x86::paddsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1917 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1918 | return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1919 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1920 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1921 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1922 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 1923 | { |
| 1924 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1925 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1926 | return x86::psubsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1927 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1928 | return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1929 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1930 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1931 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1932 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 1933 | { |
| 1934 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1935 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1936 | return x86::pmulhw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1937 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1938 | return As<Short4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1939 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1940 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1941 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1942 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 1943 | { |
| 1944 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1945 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1946 | return x86::pmaddwd(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1947 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1948 | return As<Int2>(V(lowerMulAdd(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1949 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1950 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1951 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1952 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
| 1953 | { |
| 1954 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1955 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1956 | auto result = x86::packsswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1957 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1958 | auto result = V(lowerPack(V(x.value), V(y.value), true)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1959 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1960 | return As<SByte8>(Swizzle(As<Int4>(result), 0x0202)); |
| 1961 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1962 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1963 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 1964 | { |
| 1965 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1966 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1967 | auto result = x86::packuswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1968 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1969 | auto result = V(lowerPack(V(x.value), V(y.value), false)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1970 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1971 | return As<Byte8>(Swizzle(As<Int4>(result), 0x0202)); |
| 1972 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1973 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1974 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
| 1975 | { |
| 1976 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1977 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1978 | return x86::pcmpgtw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1979 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1980 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1981 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1982 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1983 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1984 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 1985 | { |
| 1986 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1987 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1988 | return x86::pcmpeqw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1989 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1990 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1991 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1992 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1993 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1994 | Type *Short4::getType() |
| 1995 | { |
| 1996 | return T(Type_v4i16); |
| 1997 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1998 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1999 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2000 | { |
| 2001 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2002 | if(saturate) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2003 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2004 | #if defined(__i386__) || defined(__x86_64__) |
| 2005 | if(CPUID::supportsSSE4_1()) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2006 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2007 | Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2008 | *this = As<Short4>(PackUnsigned(int4, int4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2009 | } |
| 2010 | else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2011 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2012 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2013 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2014 | } |
| 2015 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2016 | else |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2017 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2018 | *this = Short4(Int4(cast)); |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2023 | { |
| 2024 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2025 | #if defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2026 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2027 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2028 | return As<UShort4>(x86::psllw(As<Short4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2029 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2030 | return As<UShort4>(V(lowerVectorShl(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2031 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2032 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2033 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2034 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2035 | { |
| 2036 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2037 | #if defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2038 | // return RValue<Short4>(Nucleus::createLShr(lhs.value, rhs.value)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2039 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2040 | return x86::psrlw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2041 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2042 | return As<UShort4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2043 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2044 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2045 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2046 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2047 | { |
| 2048 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2049 | 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)); |
| 2050 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2051 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2052 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2053 | { |
| 2054 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2055 | 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)); |
| 2056 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2057 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2058 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2059 | { |
| 2060 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2061 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2062 | return x86::paddusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2063 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2064 | return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2065 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2066 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2067 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2068 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2069 | { |
| 2070 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2071 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2072 | return x86::psubusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2073 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2074 | return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2075 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2076 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2077 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2078 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
| 2079 | { |
| 2080 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2081 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2082 | return x86::pmulhuw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2083 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2084 | return As<UShort4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2085 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2086 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2087 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2088 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 2089 | { |
| 2090 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2091 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2092 | return x86::pavgw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2093 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2094 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2095 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2096 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2097 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2098 | Type *UShort4::getType() |
| 2099 | { |
| 2100 | return T(Type_v4i16); |
| 2101 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2102 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2103 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 2104 | { |
| 2105 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2106 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2107 | return x86::psllw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2108 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2109 | return As<Short8>(V(lowerVectorShl(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2110 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2111 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2112 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2113 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
| 2114 | { |
| 2115 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2116 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2117 | return x86::psraw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2118 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2119 | return As<Short8>(V(lowerVectorAShr(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2120 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2121 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2122 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2123 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 2124 | { |
| 2125 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2126 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2127 | return x86::pmaddwd(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2128 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2129 | return As<Int4>(V(lowerMulAdd(V(x.value), V(y.value)))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2130 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2131 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2132 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2133 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
| 2134 | { |
| 2135 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2136 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2137 | return x86::pmulhw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2138 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2139 | return As<Short8>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2140 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2141 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2142 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2143 | Type *Short8::getType() |
| 2144 | { |
| 2145 | return T(llvm::VectorType::get(T(Short::getType()), 8)); |
| 2146 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2147 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2148 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 2149 | { |
| 2150 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2151 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2152 | return As<UShort8>(x86::psllw(As<Short8>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2153 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2154 | return As<UShort8>(V(lowerVectorShl(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2155 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2156 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2157 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2158 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
| 2159 | { |
| 2160 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2161 | #if defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2162 | return x86::psrlw(lhs, rhs); // FIXME: Fallback required |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2163 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2164 | return As<UShort8>(V(lowerVectorLShr(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2165 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2166 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2167 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2168 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 2169 | { |
| 2170 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2171 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2172 | return x86::pmulhuw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2173 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2174 | return As<UShort8>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2175 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2176 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2177 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2178 | Type *UShort8::getType() |
| 2179 | { |
| 2180 | return T(llvm::VectorType::get(T(UShort::getType()), 8)); |
| 2181 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2182 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2183 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2184 | { |
| 2185 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2186 | RValue<Int> res = val; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2187 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2188 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
| 2189 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2190 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2191 | return res; |
| 2192 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2193 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2194 | const Int &operator++(Int &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2195 | { |
| 2196 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2197 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
| 2198 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2199 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2200 | return val; |
| 2201 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2202 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2203 | RValue<Int> operator--(Int &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2204 | { |
| 2205 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2206 | RValue<Int> res = val; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2207 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2208 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
| 2209 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2210 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2211 | return res; |
| 2212 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2213 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2214 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2215 | { |
| 2216 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2217 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
| 2218 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2219 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2220 | return val; |
| 2221 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2222 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2223 | RValue<Int> RoundInt(RValue<Float> cast) |
| 2224 | { |
| 2225 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2226 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2227 | return x86::cvtss2si(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2228 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2229 | return RValue<Int>(V(lowerRoundInt(V(cast.value), T(Int::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2230 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2231 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2232 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2233 | Type *Int::getType() |
| 2234 | { |
| 2235 | return T(llvm::Type::getInt32Ty(jit->context)); |
| 2236 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2237 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2238 | Type *Long::getType() |
| 2239 | { |
| 2240 | return T(llvm::Type::getInt64Ty(jit->context)); |
| 2241 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2242 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2243 | UInt::UInt(RValue<Float> cast) |
| 2244 | { |
| 2245 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2246 | Value *integer = Nucleus::createFPToUI(cast.value, UInt::getType()); |
| 2247 | storeValue(integer); |
| 2248 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2249 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2250 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2251 | { |
| 2252 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2253 | RValue<UInt> res = val; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2254 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2255 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
| 2256 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2257 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2258 | return res; |
| 2259 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2260 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2261 | const UInt &operator++(UInt &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2262 | { |
| 2263 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2264 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
| 2265 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2266 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2267 | return val; |
| 2268 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2269 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2270 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2271 | { |
| 2272 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2273 | RValue<UInt> res = val; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2274 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2275 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
| 2276 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2277 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2278 | return res; |
| 2279 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2280 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2281 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2282 | { |
| 2283 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2284 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
| 2285 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2286 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2287 | return val; |
| 2288 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2289 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2290 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2291 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2292 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2293 | // return x86::cvtss2si(val); // FIXME: Unsigned |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2294 | //#else |
| 2295 | // return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f)); |
| 2296 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2297 | // } |
| 2298 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2299 | Type *UInt::getType() |
| 2300 | { |
| 2301 | return T(llvm::Type::getInt32Ty(jit->context)); |
| 2302 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2303 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2304 | // Int2::Int2(RValue<Int> cast) |
| 2305 | // { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2306 | // Value *extend = Nucleus::createZExt(cast.value, Long::getType()); |
| 2307 | // Value *vector = Nucleus::createBitCast(extend, Int2::getType()); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2308 | // |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2309 | // int shuffle[2] = {0, 0}; |
| 2310 | // Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2311 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2312 | // storeValue(replicate); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2313 | // } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2314 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2315 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 2316 | { |
| 2317 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2318 | #if defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2319 | // return RValue<Int2>(Nucleus::createShl(lhs.value, rhs.value)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2320 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2321 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2322 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2323 | return As<Int2>(V(lowerVectorShl(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2324 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2325 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2326 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2327 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 2328 | { |
| 2329 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2330 | #if defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2331 | // return RValue<Int2>(Nucleus::createAShr(lhs.value, rhs.value)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2332 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2333 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2334 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2335 | return As<Int2>(V(lowerVectorAShr(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2336 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2337 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2338 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2339 | Type *Int2::getType() |
| 2340 | { |
| 2341 | return T(Type_v2i32); |
| 2342 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2343 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2344 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
| 2345 | { |
| 2346 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2347 | #if defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2348 | // return RValue<UInt2>(Nucleus::createShl(lhs.value, rhs.value)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2349 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2350 | return As<UInt2>(x86::pslld(As<Int2>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2351 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2352 | return As<UInt2>(V(lowerVectorShl(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2353 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2354 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2355 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2356 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 2357 | { |
| 2358 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2359 | #if defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2360 | // return RValue<UInt2>(Nucleus::createLShr(lhs.value, rhs.value)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2361 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2362 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2363 | #else |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2364 | return As<UInt2>(V(lowerVectorLShr(V(lhs.value), rhs))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2365 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2366 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2367 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2368 | Type *UInt2::getType() |
| 2369 | { |
| 2370 | return T(Type_v2i32); |
| 2371 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2372 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2373 | Int4::Int4(RValue<Byte4> cast) |
| 2374 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2375 | { |
| 2376 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2377 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2378 | if(CPUID::supportsSSE4_1()) |
| 2379 | { |
| 2380 | *this = x86::pmovzxbd(As<Byte16>(cast)); |
| 2381 | } |
| 2382 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2383 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2384 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2385 | int swizzle[16] = { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2386 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
| 2387 | Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::getType()), swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2388 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2389 | int swizzle2[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2390 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
| 2391 | Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::getType()), swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2392 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2393 | *this = As<Int4>(d); |
Nicolas Capens | 9770a46 | 2019-06-25 10:47:10 -0400 | [diff] [blame] | 2394 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2395 | } |
| 2396 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2397 | Int4::Int4(RValue<SByte4> cast) |
| 2398 | : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2399 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2400 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2401 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2402 | if(CPUID::supportsSSE4_1()) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2403 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2404 | *this = x86::pmovsxbd(As<SByte16>(cast)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2405 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2406 | else |
| 2407 | #endif |
| 2408 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2409 | int swizzle[16] = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2410 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
| 2411 | Value *b = Nucleus::createShuffleVector(a, a, swizzle); |
| 2412 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2413 | int swizzle2[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2414 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
| 2415 | Value *d = Nucleus::createShuffleVector(c, c, swizzle2); |
| 2416 | |
| 2417 | *this = As<Int4>(d) >> 24; |
| 2418 | } |
| 2419 | } |
| 2420 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2421 | Int4::Int4(RValue<Short4> cast) |
| 2422 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2423 | { |
| 2424 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2425 | #if defined(__i386__) || defined(__x86_64__) |
| 2426 | if(CPUID::supportsSSE4_1()) |
| 2427 | { |
| 2428 | *this = x86::pmovsxwd(As<Short8>(cast)); |
| 2429 | } |
| 2430 | else |
| 2431 | #endif |
| 2432 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2433 | int swizzle[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2434 | Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle); |
| 2435 | *this = As<Int4>(c) >> 16; |
| 2436 | } |
| 2437 | } |
| 2438 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2439 | Int4::Int4(RValue<UShort4> cast) |
| 2440 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2441 | { |
| 2442 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2443 | #if defined(__i386__) || defined(__x86_64__) |
| 2444 | if(CPUID::supportsSSE4_1()) |
| 2445 | { |
| 2446 | *this = x86::pmovzxwd(As<UShort8>(cast)); |
| 2447 | } |
| 2448 | else |
| 2449 | #endif |
| 2450 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2451 | int swizzle[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2452 | Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
| 2453 | *this = As<Int4>(c); |
| 2454 | } |
| 2455 | } |
| 2456 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2457 | Int4::Int4(RValue<Int> rhs) |
| 2458 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2459 | { |
| 2460 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2461 | Value *vector = loadValue(); |
| 2462 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 2463 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2464 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2465 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
| 2466 | |
| 2467 | storeValue(replicate); |
| 2468 | } |
| 2469 | |
| 2470 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 2471 | { |
| 2472 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2473 | #if defined(__i386__) || defined(__x86_64__) |
| 2474 | return x86::pslld(lhs, rhs); |
| 2475 | #else |
| 2476 | return As<Int4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2477 | #endif |
| 2478 | } |
| 2479 | |
| 2480 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 2481 | { |
| 2482 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2483 | #if defined(__i386__) || defined(__x86_64__) |
| 2484 | return x86::psrad(lhs, rhs); |
| 2485 | #else |
| 2486 | return As<Int4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2487 | #endif |
| 2488 | } |
| 2489 | |
| 2490 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 2491 | { |
| 2492 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2493 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
| 2494 | } |
| 2495 | |
| 2496 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 2497 | { |
| 2498 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2499 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType())); |
| 2500 | } |
| 2501 | |
| 2502 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 2503 | { |
| 2504 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2505 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType())); |
| 2506 | } |
| 2507 | |
| 2508 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 2509 | { |
| 2510 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2511 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
| 2512 | } |
| 2513 | |
| 2514 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 2515 | { |
| 2516 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2517 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType())); |
| 2518 | } |
| 2519 | |
| 2520 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 2521 | { |
| 2522 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2523 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType())); |
| 2524 | } |
| 2525 | |
| 2526 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 2527 | { |
| 2528 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2529 | #if defined(__i386__) || defined(__x86_64__) |
| 2530 | if(CPUID::supportsSSE4_1()) |
| 2531 | { |
| 2532 | return x86::pmaxsd(x, y); |
| 2533 | } |
| 2534 | else |
| 2535 | #endif |
| 2536 | { |
| 2537 | RValue<Int4> greater = CmpNLE(x, y); |
| 2538 | return (x & greater) | (y & ~greater); |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 2543 | { |
| 2544 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2545 | #if defined(__i386__) || defined(__x86_64__) |
| 2546 | if(CPUID::supportsSSE4_1()) |
| 2547 | { |
| 2548 | return x86::pminsd(x, y); |
| 2549 | } |
| 2550 | else |
| 2551 | #endif |
| 2552 | { |
| 2553 | RValue<Int4> less = CmpLT(x, y); |
| 2554 | return (x & less) | (y & ~less); |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 2559 | { |
| 2560 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2561 | #if defined(__i386__) || defined(__x86_64__) |
| 2562 | return x86::cvtps2dq(cast); |
| 2563 | #else |
| 2564 | return As<Int4>(V(lowerRoundInt(V(cast.value), T(Int4::getType())))); |
| 2565 | #endif |
| 2566 | } |
| 2567 | |
| 2568 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 2569 | { |
| 2570 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2571 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2572 | return As<Int4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2573 | } |
| 2574 | |
| 2575 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 2576 | { |
| 2577 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2578 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2579 | return As<UInt4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2580 | } |
| 2581 | |
| 2582 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
| 2583 | { |
| 2584 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2585 | #if defined(__i386__) || defined(__x86_64__) |
| 2586 | return x86::packssdw(x, y); |
| 2587 | #else |
| 2588 | return As<Short8>(V(lowerPack(V(x.value), V(y.value), true))); |
| 2589 | #endif |
| 2590 | } |
| 2591 | |
| 2592 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 2593 | { |
| 2594 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2595 | #if defined(__i386__) || defined(__x86_64__) |
| 2596 | return x86::packusdw(x, y); |
| 2597 | #else |
| 2598 | return As<UShort8>(V(lowerPack(V(x.value), V(y.value), false))); |
| 2599 | #endif |
| 2600 | } |
| 2601 | |
| 2602 | RValue<Int> SignMask(RValue<Int4> x) |
| 2603 | { |
| 2604 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2605 | #if defined(__i386__) || defined(__x86_64__) |
| 2606 | return x86::movmskps(As<Float4>(x)); |
| 2607 | #else |
| 2608 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2609 | #endif |
| 2610 | } |
| 2611 | |
| 2612 | Type *Int4::getType() |
| 2613 | { |
| 2614 | return T(llvm::VectorType::get(T(Int::getType()), 4)); |
| 2615 | } |
| 2616 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2617 | UInt4::UInt4(RValue<Float4> cast) |
| 2618 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2619 | { |
| 2620 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2621 | Value *xyzw = Nucleus::createFPToUI(cast.value, UInt4::getType()); |
| 2622 | storeValue(xyzw); |
| 2623 | } |
| 2624 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2625 | UInt4::UInt4(RValue<UInt> rhs) |
| 2626 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2627 | { |
| 2628 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2629 | Value *vector = loadValue(); |
| 2630 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 2631 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2632 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2633 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
| 2634 | |
| 2635 | storeValue(replicate); |
| 2636 | } |
| 2637 | |
| 2638 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 2639 | { |
| 2640 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2641 | #if defined(__i386__) || defined(__x86_64__) |
| 2642 | return As<UInt4>(x86::pslld(As<Int4>(lhs), rhs)); |
| 2643 | #else |
| 2644 | return As<UInt4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2645 | #endif |
| 2646 | } |
| 2647 | |
| 2648 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 2649 | { |
| 2650 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2651 | #if defined(__i386__) || defined(__x86_64__) |
| 2652 | return x86::psrld(lhs, rhs); |
| 2653 | #else |
| 2654 | return As<UInt4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2655 | #endif |
| 2656 | } |
| 2657 | |
| 2658 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 2659 | { |
| 2660 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2661 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
| 2662 | } |
| 2663 | |
| 2664 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 2665 | { |
| 2666 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2667 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType())); |
| 2668 | } |
| 2669 | |
| 2670 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 2671 | { |
| 2672 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2673 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULE(x.value, y.value), Int4::getType())); |
| 2674 | } |
| 2675 | |
| 2676 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 2677 | { |
| 2678 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2679 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
| 2680 | } |
| 2681 | |
| 2682 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 2683 | { |
| 2684 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2685 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGE(x.value, y.value), Int4::getType())); |
| 2686 | } |
| 2687 | |
| 2688 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 2689 | { |
| 2690 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2691 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType())); |
| 2692 | } |
| 2693 | |
| 2694 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 2695 | { |
| 2696 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2697 | #if defined(__i386__) || defined(__x86_64__) |
| 2698 | if(CPUID::supportsSSE4_1()) |
| 2699 | { |
| 2700 | return x86::pmaxud(x, y); |
| 2701 | } |
| 2702 | else |
| 2703 | #endif |
| 2704 | { |
| 2705 | RValue<UInt4> greater = CmpNLE(x, y); |
| 2706 | return (x & greater) | (y & ~greater); |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 2711 | { |
| 2712 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2713 | #if defined(__i386__) || defined(__x86_64__) |
| 2714 | if(CPUID::supportsSSE4_1()) |
| 2715 | { |
| 2716 | return x86::pminud(x, y); |
| 2717 | } |
| 2718 | else |
| 2719 | #endif |
| 2720 | { |
| 2721 | RValue<UInt4> less = CmpLT(x, y); |
| 2722 | return (x & less) | (y & ~less); |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | Type *UInt4::getType() |
| 2727 | { |
| 2728 | return T(llvm::VectorType::get(T(UInt::getType()), 4)); |
| 2729 | } |
| 2730 | |
| 2731 | Type *Half::getType() |
| 2732 | { |
| 2733 | return T(llvm::Type::getInt16Ty(jit->context)); |
| 2734 | } |
| 2735 | |
| 2736 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 2737 | { |
| 2738 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2739 | #if defined(__i386__) || defined(__x86_64__) |
| 2740 | if(exactAtPow2) |
| 2741 | { |
| 2742 | // rcpss uses a piecewise-linear approximation which minimizes the relative error |
| 2743 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 2744 | return x86::rcpss(x) * Float(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 2745 | } |
| 2746 | return x86::rcpss(x); |
| 2747 | #else |
| 2748 | return As<Float>(V(lowerRCP(V(x.value)))); |
| 2749 | #endif |
| 2750 | } |
| 2751 | |
| 2752 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
| 2753 | { |
| 2754 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2755 | #if defined(__i386__) || defined(__x86_64__) |
| 2756 | return x86::rsqrtss(x); |
| 2757 | #else |
| 2758 | return As<Float>(V(lowerRSQRT(V(x.value)))); |
| 2759 | #endif |
| 2760 | } |
| 2761 | |
| 2762 | RValue<Float> Sqrt(RValue<Float> x) |
| 2763 | { |
| 2764 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2765 | #if defined(__i386__) || defined(__x86_64__) |
| 2766 | return x86::sqrtss(x); |
| 2767 | #else |
| 2768 | return As<Float>(V(lowerSQRT(V(x.value)))); |
| 2769 | #endif |
| 2770 | } |
| 2771 | |
| 2772 | RValue<Float> Round(RValue<Float> x) |
| 2773 | { |
| 2774 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2775 | #if defined(__i386__) || defined(__x86_64__) |
| 2776 | if(CPUID::supportsSSE4_1()) |
| 2777 | { |
| 2778 | return x86::roundss(x, 0); |
| 2779 | } |
| 2780 | else |
| 2781 | { |
| 2782 | return Float4(Round(Float4(x))).x; |
| 2783 | } |
| 2784 | #else |
| 2785 | return RValue<Float>(V(lowerRound(V(x.value)))); |
| 2786 | #endif |
| 2787 | } |
| 2788 | |
| 2789 | RValue<Float> Trunc(RValue<Float> x) |
| 2790 | { |
| 2791 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2792 | #if defined(__i386__) || defined(__x86_64__) |
| 2793 | if(CPUID::supportsSSE4_1()) |
| 2794 | { |
| 2795 | return x86::roundss(x, 3); |
| 2796 | } |
| 2797 | else |
| 2798 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2799 | return Float(Int(x)); // Rounded toward zero |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2800 | } |
| 2801 | #else |
| 2802 | return RValue<Float>(V(lowerTrunc(V(x.value)))); |
| 2803 | #endif |
| 2804 | } |
| 2805 | |
| 2806 | RValue<Float> Frac(RValue<Float> x) |
| 2807 | { |
| 2808 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2809 | #if defined(__i386__) || defined(__x86_64__) |
| 2810 | if(CPUID::supportsSSE4_1()) |
| 2811 | { |
| 2812 | return x - x86::floorss(x); |
| 2813 | } |
| 2814 | else |
| 2815 | { |
| 2816 | return Float4(Frac(Float4(x))).x; |
| 2817 | } |
| 2818 | #else |
| 2819 | // x - floor(x) can be 1.0 for very small negative x. |
| 2820 | // Clamp against the value just below 1.0. |
| 2821 | return Min(x - Floor(x), As<Float>(Int(0x3F7FFFFF))); |
| 2822 | #endif |
| 2823 | } |
| 2824 | |
| 2825 | RValue<Float> Floor(RValue<Float> x) |
| 2826 | { |
| 2827 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2828 | #if defined(__i386__) || defined(__x86_64__) |
| 2829 | if(CPUID::supportsSSE4_1()) |
| 2830 | { |
| 2831 | return x86::floorss(x); |
| 2832 | } |
| 2833 | else |
| 2834 | { |
| 2835 | return Float4(Floor(Float4(x))).x; |
| 2836 | } |
| 2837 | #else |
| 2838 | return RValue<Float>(V(lowerFloor(V(x.value)))); |
| 2839 | #endif |
| 2840 | } |
| 2841 | |
| 2842 | RValue<Float> Ceil(RValue<Float> x) |
| 2843 | { |
| 2844 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2845 | #if defined(__i386__) || defined(__x86_64__) |
| 2846 | if(CPUID::supportsSSE4_1()) |
| 2847 | { |
| 2848 | return x86::ceilss(x); |
| 2849 | } |
| 2850 | else |
| 2851 | #endif |
| 2852 | { |
| 2853 | return Float4(Ceil(Float4(x))).x; |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | Type *Float::getType() |
| 2858 | { |
| 2859 | return T(llvm::Type::getFloatTy(jit->context)); |
| 2860 | } |
| 2861 | |
| 2862 | Type *Float2::getType() |
| 2863 | { |
| 2864 | return T(Type_v2f32); |
| 2865 | } |
| 2866 | |
| 2867 | RValue<Float> Exp2(RValue<Float> v) |
| 2868 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2869 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::exp2, { T(Float::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2870 | return RValue<Float>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 2871 | } |
| 2872 | |
| 2873 | RValue<Float> Log2(RValue<Float> v) |
| 2874 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2875 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::log2, { T(Float::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2876 | return RValue<Float>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 2877 | } |
| 2878 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2879 | Float4::Float4(RValue<Float> rhs) |
| 2880 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2881 | { |
| 2882 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2883 | Value *vector = loadValue(); |
| 2884 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 2885 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2886 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2887 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
| 2888 | |
| 2889 | storeValue(replicate); |
| 2890 | } |
| 2891 | |
| 2892 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 2893 | { |
| 2894 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2895 | #if defined(__i386__) || defined(__x86_64__) |
| 2896 | return x86::maxps(x, y); |
| 2897 | #else |
| 2898 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OGT))); |
| 2899 | #endif |
| 2900 | } |
| 2901 | |
| 2902 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 2903 | { |
| 2904 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2905 | #if defined(__i386__) || defined(__x86_64__) |
| 2906 | return x86::minps(x, y); |
| 2907 | #else |
| 2908 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OLT))); |
| 2909 | #endif |
| 2910 | } |
| 2911 | |
| 2912 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 2913 | { |
| 2914 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2915 | #if defined(__i386__) || defined(__x86_64__) |
| 2916 | if(exactAtPow2) |
| 2917 | { |
| 2918 | // rcpps uses a piecewise-linear approximation which minimizes the relative error |
| 2919 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 2920 | return x86::rcpps(x) * Float4(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 2921 | } |
| 2922 | return x86::rcpps(x); |
| 2923 | #else |
| 2924 | return As<Float4>(V(lowerRCP(V(x.value)))); |
| 2925 | #endif |
| 2926 | } |
| 2927 | |
| 2928 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 2929 | { |
| 2930 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2931 | #if defined(__i386__) || defined(__x86_64__) |
| 2932 | return x86::rsqrtps(x); |
| 2933 | #else |
| 2934 | return As<Float4>(V(lowerRSQRT(V(x.value)))); |
| 2935 | #endif |
| 2936 | } |
| 2937 | |
| 2938 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 2939 | { |
| 2940 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2941 | #if defined(__i386__) || defined(__x86_64__) |
| 2942 | return x86::sqrtps(x); |
| 2943 | #else |
| 2944 | return As<Float4>(V(lowerSQRT(V(x.value)))); |
| 2945 | #endif |
| 2946 | } |
| 2947 | |
| 2948 | RValue<Int> SignMask(RValue<Float4> x) |
| 2949 | { |
| 2950 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2951 | #if defined(__i386__) || defined(__x86_64__) |
| 2952 | return x86::movmskps(x); |
| 2953 | #else |
| 2954 | return As<Int>(V(lowerFPSignMask(V(x.value), T(Int::getType())))); |
| 2955 | #endif |
| 2956 | } |
| 2957 | |
| 2958 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
| 2959 | { |
| 2960 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2961 | // return As<Int4>(x86::cmpeqps(x, y)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2962 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value, y.value), Int4::getType())); |
| 2963 | } |
| 2964 | |
| 2965 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
| 2966 | { |
| 2967 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2968 | // return As<Int4>(x86::cmpltps(x, y)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2969 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value, y.value), Int4::getType())); |
| 2970 | } |
| 2971 | |
| 2972 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
| 2973 | { |
| 2974 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2975 | // return As<Int4>(x86::cmpleps(x, y)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2976 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value, y.value), Int4::getType())); |
| 2977 | } |
| 2978 | |
| 2979 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
| 2980 | { |
| 2981 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2982 | // return As<Int4>(x86::cmpneqps(x, y)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2983 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value, y.value), Int4::getType())); |
| 2984 | } |
| 2985 | |
| 2986 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
| 2987 | { |
| 2988 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2989 | // return As<Int4>(x86::cmpnltps(x, y)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2990 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value, y.value), Int4::getType())); |
| 2991 | } |
| 2992 | |
| 2993 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
| 2994 | { |
| 2995 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2996 | // return As<Int4>(x86::cmpnleps(x, y)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2997 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value, y.value), Int4::getType())); |
| 2998 | } |
| 2999 | |
| 3000 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 3001 | { |
| 3002 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3003 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUEQ(x.value, y.value), Int4::getType())); |
| 3004 | } |
| 3005 | |
| 3006 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 3007 | { |
| 3008 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3009 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULT(x.value, y.value), Int4::getType())); |
| 3010 | } |
| 3011 | |
| 3012 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 3013 | { |
| 3014 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3015 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULE(x.value, y.value), Int4::getType())); |
| 3016 | } |
| 3017 | |
| 3018 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 3019 | { |
| 3020 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3021 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUNE(x.value, y.value), Int4::getType())); |
| 3022 | } |
| 3023 | |
| 3024 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 3025 | { |
| 3026 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3027 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGE(x.value, y.value), Int4::getType())); |
| 3028 | } |
| 3029 | |
| 3030 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 3031 | { |
| 3032 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3033 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value, y.value), Int4::getType())); |
| 3034 | } |
| 3035 | |
| 3036 | RValue<Float4> Round(RValue<Float4> x) |
| 3037 | { |
| 3038 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3039 | #if defined(__i386__) || defined(__x86_64__) |
| 3040 | if(CPUID::supportsSSE4_1()) |
| 3041 | { |
| 3042 | return x86::roundps(x, 0); |
| 3043 | } |
| 3044 | else |
| 3045 | { |
| 3046 | return Float4(RoundInt(x)); |
| 3047 | } |
| 3048 | #else |
| 3049 | return RValue<Float4>(V(lowerRound(V(x.value)))); |
| 3050 | #endif |
| 3051 | } |
| 3052 | |
| 3053 | RValue<Float4> Trunc(RValue<Float4> x) |
| 3054 | { |
| 3055 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3056 | #if defined(__i386__) || defined(__x86_64__) |
| 3057 | if(CPUID::supportsSSE4_1()) |
| 3058 | { |
| 3059 | return x86::roundps(x, 3); |
| 3060 | } |
| 3061 | else |
| 3062 | { |
| 3063 | return Float4(Int4(x)); |
| 3064 | } |
| 3065 | #else |
| 3066 | return RValue<Float4>(V(lowerTrunc(V(x.value)))); |
| 3067 | #endif |
| 3068 | } |
| 3069 | |
| 3070 | RValue<Float4> Frac(RValue<Float4> x) |
| 3071 | { |
| 3072 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3073 | Float4 frc; |
| 3074 | |
| 3075 | #if defined(__i386__) || defined(__x86_64__) |
| 3076 | if(CPUID::supportsSSE4_1()) |
| 3077 | { |
| 3078 | frc = x - Floor(x); |
| 3079 | } |
| 3080 | else |
| 3081 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3082 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3083 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3084 | frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1.0f))); // Add 1.0 if negative. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3085 | } |
| 3086 | #else |
| 3087 | frc = x - Floor(x); |
| 3088 | #endif |
| 3089 | |
| 3090 | // x - floor(x) can be 1.0 for very small negative x. |
| 3091 | // Clamp against the value just below 1.0. |
| 3092 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
| 3093 | } |
| 3094 | |
| 3095 | RValue<Float4> Floor(RValue<Float4> x) |
| 3096 | { |
| 3097 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3098 | #if defined(__i386__) || defined(__x86_64__) |
| 3099 | if(CPUID::supportsSSE4_1()) |
| 3100 | { |
| 3101 | return x86::floorps(x); |
| 3102 | } |
| 3103 | else |
| 3104 | { |
| 3105 | return x - Frac(x); |
| 3106 | } |
| 3107 | #else |
| 3108 | return RValue<Float4>(V(lowerFloor(V(x.value)))); |
| 3109 | #endif |
| 3110 | } |
| 3111 | |
| 3112 | RValue<Float4> Ceil(RValue<Float4> x) |
| 3113 | { |
| 3114 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3115 | #if defined(__i386__) || defined(__x86_64__) |
| 3116 | if(CPUID::supportsSSE4_1()) |
| 3117 | { |
| 3118 | return x86::ceilps(x); |
| 3119 | } |
| 3120 | else |
| 3121 | #endif |
| 3122 | { |
| 3123 | return -Floor(-x); |
| 3124 | } |
| 3125 | } |
| 3126 | |
| 3127 | RValue<Float4> Sin(RValue<Float4> v) |
| 3128 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3129 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::sin, { V(v.value)->getType() }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3130 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 3131 | } |
| 3132 | |
| 3133 | RValue<Float4> Cos(RValue<Float4> v) |
| 3134 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3135 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cos, { V(v.value)->getType() }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3136 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 3137 | } |
| 3138 | |
| 3139 | RValue<Float4> Tan(RValue<Float4> v) |
| 3140 | { |
| 3141 | return Sin(v) / Cos(v); |
| 3142 | } |
| 3143 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3144 | static RValue<Float4> TransformFloat4PerElement(RValue<Float4> v, const char *name) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3145 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3146 | auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), ::llvm::ArrayRef<llvm::Type *>(T(Float::getType())), false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3147 | auto func = jit->module->getOrInsertFunction(name, funcTy); |
| 3148 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3149 | for(uint64_t i = 0; i < 4; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3150 | { |
| 3151 | auto el = jit->builder->CreateCall(func, V(Nucleus::createExtractElement(v.value, Float::getType(), i))); |
| 3152 | out = V(Nucleus::createInsertElement(V(out), V(el), i)); |
| 3153 | } |
| 3154 | return RValue<Float4>(V(out)); |
| 3155 | } |
| 3156 | |
| 3157 | RValue<Float4> Asin(RValue<Float4> v) |
| 3158 | { |
| 3159 | return TransformFloat4PerElement(v, "asinf"); |
| 3160 | } |
| 3161 | |
| 3162 | RValue<Float4> Acos(RValue<Float4> v) |
| 3163 | { |
| 3164 | return TransformFloat4PerElement(v, "acosf"); |
| 3165 | } |
| 3166 | |
| 3167 | RValue<Float4> Atan(RValue<Float4> v) |
| 3168 | { |
| 3169 | return TransformFloat4PerElement(v, "atanf"); |
| 3170 | } |
| 3171 | |
| 3172 | RValue<Float4> Sinh(RValue<Float4> v) |
| 3173 | { |
| 3174 | return Float4(0.5f) * (Exp(v) - Exp(-v)); |
| 3175 | } |
| 3176 | |
| 3177 | RValue<Float4> Cosh(RValue<Float4> v) |
| 3178 | { |
| 3179 | return Float4(0.5f) * (Exp(v) + Exp(-v)); |
| 3180 | } |
| 3181 | |
| 3182 | RValue<Float4> Tanh(RValue<Float4> v) |
| 3183 | { |
| 3184 | return TransformFloat4PerElement(v, "tanhf"); |
| 3185 | } |
| 3186 | |
| 3187 | RValue<Float4> Asinh(RValue<Float4> v) |
| 3188 | { |
| 3189 | return TransformFloat4PerElement(v, "asinhf"); |
| 3190 | } |
| 3191 | |
| 3192 | RValue<Float4> Acosh(RValue<Float4> v) |
| 3193 | { |
| 3194 | return TransformFloat4PerElement(v, "acoshf"); |
| 3195 | } |
| 3196 | |
| 3197 | RValue<Float4> Atanh(RValue<Float4> v) |
| 3198 | { |
| 3199 | return TransformFloat4PerElement(v, "atanhf"); |
| 3200 | } |
| 3201 | |
| 3202 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 3203 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3204 | ::llvm::SmallVector<::llvm::Type *, 2> paramTys; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3205 | paramTys.push_back(T(Float::getType())); |
| 3206 | paramTys.push_back(T(Float::getType())); |
| 3207 | auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), paramTys, false); |
| 3208 | auto func = jit->module->getOrInsertFunction("atan2f", funcTy); |
| 3209 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3210 | for(uint64_t i = 0; i < 4; i++) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3211 | { |
| 3212 | auto el = jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3213 | V(Nucleus::createExtractElement(x.value, Float::getType(), i)), |
| 3214 | V(Nucleus::createExtractElement(y.value, Float::getType(), i)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3215 | out = V(Nucleus::createInsertElement(V(out), V(el), i)); |
| 3216 | } |
| 3217 | return RValue<Float4>(V(out)); |
| 3218 | } |
| 3219 | |
| 3220 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 3221 | { |
| 3222 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::pow, { T(Float4::getType()) }); |
| 3223 | return RValue<Float4>(V(jit->builder->CreateCall2(func, ARGS(V(x.value), V(y.value))))); |
| 3224 | } |
| 3225 | |
| 3226 | RValue<Float4> Exp(RValue<Float4> v) |
| 3227 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3228 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::exp, { T(Float4::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3229 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 3230 | } |
| 3231 | |
| 3232 | RValue<Float4> Log(RValue<Float4> v) |
| 3233 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3234 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::log, { T(Float4::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3235 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 3236 | } |
| 3237 | |
| 3238 | RValue<Float4> Exp2(RValue<Float4> v) |
| 3239 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3240 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::exp2, { T(Float4::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3241 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 3242 | } |
| 3243 | |
| 3244 | RValue<Float4> Log2(RValue<Float4> v) |
| 3245 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3246 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::log2, { T(Float4::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3247 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 3248 | } |
| 3249 | |
| 3250 | RValue<UInt> Ctlz(RValue<UInt> v, bool isZeroUndef) |
| 3251 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3252 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::ctlz, { T(UInt::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3253 | return RValue<UInt>(V(jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3254 | V(v.value), |
| 3255 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context))))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | RValue<UInt4> Ctlz(RValue<UInt4> v, bool isZeroUndef) |
| 3259 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3260 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::ctlz, { T(UInt4::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3261 | return RValue<UInt4>(V(jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3262 | V(v.value), |
| 3263 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context))))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | RValue<UInt> Cttz(RValue<UInt> v, bool isZeroUndef) |
| 3267 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3268 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cttz, { T(UInt::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3269 | return RValue<UInt>(V(jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3270 | V(v.value), |
| 3271 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context))))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | RValue<UInt4> Cttz(RValue<UInt4> v, bool isZeroUndef) |
| 3275 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3276 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cttz, { T(UInt4::getType()) }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3277 | return RValue<UInt4>(V(jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3278 | V(v.value), |
| 3279 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context))))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3280 | } |
| 3281 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 3282 | RValue<Int> MinAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 3283 | { |
| 3284 | return RValue<Int>(Nucleus::createAtomicMin(x.value, y.value, memoryOrder)); |
| 3285 | } |
| 3286 | |
| 3287 | RValue<UInt> MinAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 3288 | { |
| 3289 | return RValue<UInt>(Nucleus::createAtomicUMin(x.value, y.value, memoryOrder)); |
| 3290 | } |
| 3291 | |
| 3292 | RValue<Int> MaxAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 3293 | { |
| 3294 | return RValue<Int>(Nucleus::createAtomicMax(x.value, y.value, memoryOrder)); |
| 3295 | } |
| 3296 | |
| 3297 | RValue<UInt> MaxAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 3298 | { |
| 3299 | return RValue<UInt>(Nucleus::createAtomicUMax(x.value, y.value, memoryOrder)); |
| 3300 | } |
| 3301 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3302 | Type *Float4::getType() |
| 3303 | { |
| 3304 | return T(llvm::VectorType::get(T(Float::getType()), 4)); |
| 3305 | } |
| 3306 | |
| 3307 | RValue<Long> Ticks() |
| 3308 | { |
| 3309 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3310 | llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::readcyclecounter); |
| 3311 | |
| 3312 | return RValue<Long>(V(jit->builder->CreateCall(rdtsc))); |
| 3313 | } |
| 3314 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3315 | RValue<Pointer<Byte>> ConstantPointer(void const *ptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3316 | { |
| 3317 | // Note: this should work for 32-bit pointers as well because 'inttoptr' |
| 3318 | // is defined to truncate (and zero extend) if necessary. |
| 3319 | auto ptrAsInt = ::llvm::ConstantInt::get(::llvm::Type::getInt64Ty(jit->context), reinterpret_cast<uintptr_t>(ptr)); |
| 3320 | return RValue<Pointer<Byte>>(V(jit->builder->CreateIntToPtr(ptrAsInt, T(Pointer<Byte>::getType())))); |
| 3321 | } |
| 3322 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3323 | RValue<Pointer<Byte>> ConstantData(void const *data, size_t size) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3324 | { |
Nicolas Capens | 79b36b6 | 2020-01-30 11:23:30 -0500 | [diff] [blame] | 3325 | auto str = ::std::string(reinterpret_cast<const char *>(data), size); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3326 | auto ptr = jit->builder->CreateGlobalStringPtr(str); |
| 3327 | return RValue<Pointer<Byte>>(V(ptr)); |
| 3328 | } |
| 3329 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3330 | Value *Call(RValue<Pointer<Byte>> fptr, Type *retTy, std::initializer_list<Value *> args, std::initializer_list<Type *> argTys) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3331 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3332 | ::llvm::SmallVector<::llvm::Type *, 8> paramTys; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3333 | for(auto ty : argTys) { paramTys.push_back(T(ty)); } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3334 | auto funcTy = ::llvm::FunctionType::get(T(retTy), paramTys, false); |
| 3335 | |
| 3336 | auto funcPtrTy = funcTy->getPointerTo(); |
| 3337 | auto funcPtr = jit->builder->CreatePointerCast(V(fptr.value), funcPtrTy); |
| 3338 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3339 | ::llvm::SmallVector<::llvm::Value *, 8> arguments; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3340 | for(auto arg : args) { arguments.push_back(V(arg)); } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3341 | return V(jit->builder->CreateCall(funcPtr, arguments)); |
| 3342 | } |
| 3343 | |
| 3344 | void Breakpoint() |
| 3345 | { |
| 3346 | llvm::Function *debugtrap = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::debugtrap); |
| 3347 | |
| 3348 | jit->builder->CreateCall(debugtrap); |
| 3349 | } |
| 3350 | |
| 3351 | } // namespace rr |
| 3352 | |
| 3353 | namespace rr { |
| 3354 | |
| 3355 | #if defined(__i386__) || defined(__x86_64__) |
| 3356 | namespace x86 { |
| 3357 | |
| 3358 | RValue<Int> cvtss2si(RValue<Float> val) |
| 3359 | { |
| 3360 | llvm::Function *cvtss2si = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_cvtss2si); |
| 3361 | |
| 3362 | Float4 vector; |
| 3363 | vector.x = val; |
| 3364 | |
| 3365 | return RValue<Int>(V(jit->builder->CreateCall(cvtss2si, ARGS(V(RValue<Float4>(vector).value))))); |
| 3366 | } |
| 3367 | |
| 3368 | RValue<Int4> cvtps2dq(RValue<Float4> val) |
| 3369 | { |
| 3370 | llvm::Function *cvtps2dq = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_cvtps2dq); |
| 3371 | |
| 3372 | return RValue<Int4>(V(jit->builder->CreateCall(cvtps2dq, ARGS(V(val.value))))); |
| 3373 | } |
| 3374 | |
| 3375 | RValue<Float> rcpss(RValue<Float> val) |
| 3376 | { |
| 3377 | llvm::Function *rcpss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rcp_ss); |
| 3378 | |
| 3379 | Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); |
| 3380 | |
| 3381 | return RValue<Float>(Nucleus::createExtractElement(V(jit->builder->CreateCall(rcpss, ARGS(V(vector)))), Float::getType(), 0)); |
| 3382 | } |
| 3383 | |
| 3384 | RValue<Float> sqrtss(RValue<Float> val) |
| 3385 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3386 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::sqrt, { V(val.value)->getType() }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3387 | return RValue<Float>(V(jit->builder->CreateCall(sqrt, ARGS(V(val.value))))); |
| 3388 | } |
| 3389 | |
| 3390 | RValue<Float> rsqrtss(RValue<Float> val) |
| 3391 | { |
| 3392 | llvm::Function *rsqrtss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rsqrt_ss); |
| 3393 | |
| 3394 | Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); |
| 3395 | |
| 3396 | return RValue<Float>(Nucleus::createExtractElement(V(jit->builder->CreateCall(rsqrtss, ARGS(V(vector)))), Float::getType(), 0)); |
| 3397 | } |
| 3398 | |
| 3399 | RValue<Float4> rcpps(RValue<Float4> val) |
| 3400 | { |
| 3401 | llvm::Function *rcpps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rcp_ps); |
| 3402 | |
| 3403 | return RValue<Float4>(V(jit->builder->CreateCall(rcpps, ARGS(V(val.value))))); |
| 3404 | } |
| 3405 | |
| 3406 | RValue<Float4> sqrtps(RValue<Float4> val) |
| 3407 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3408 | llvm::Function *sqrtps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::sqrt, { V(val.value)->getType() }); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3409 | |
| 3410 | return RValue<Float4>(V(jit->builder->CreateCall(sqrtps, ARGS(V(val.value))))); |
| 3411 | } |
| 3412 | |
| 3413 | RValue<Float4> rsqrtps(RValue<Float4> val) |
| 3414 | { |
| 3415 | llvm::Function *rsqrtps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rsqrt_ps); |
| 3416 | |
| 3417 | return RValue<Float4>(V(jit->builder->CreateCall(rsqrtps, ARGS(V(val.value))))); |
| 3418 | } |
| 3419 | |
| 3420 | RValue<Float4> maxps(RValue<Float4> x, RValue<Float4> y) |
| 3421 | { |
| 3422 | llvm::Function *maxps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_max_ps); |
| 3423 | |
| 3424 | return RValue<Float4>(V(jit->builder->CreateCall2(maxps, ARGS(V(x.value), V(y.value))))); |
| 3425 | } |
| 3426 | |
| 3427 | RValue<Float4> minps(RValue<Float4> x, RValue<Float4> y) |
| 3428 | { |
| 3429 | llvm::Function *minps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_min_ps); |
| 3430 | |
| 3431 | return RValue<Float4>(V(jit->builder->CreateCall2(minps, ARGS(V(x.value), V(y.value))))); |
| 3432 | } |
| 3433 | |
| 3434 | RValue<Float> roundss(RValue<Float> val, unsigned char imm) |
| 3435 | { |
| 3436 | llvm::Function *roundss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_round_ss); |
| 3437 | |
| 3438 | Value *undef = V(llvm::UndefValue::get(T(Float4::getType()))); |
| 3439 | Value *vector = Nucleus::createInsertElement(undef, val.value, 0); |
| 3440 | |
| 3441 | return RValue<Float>(Nucleus::createExtractElement(V(jit->builder->CreateCall3(roundss, ARGS(V(undef), V(vector), V(Nucleus::createConstantInt(imm))))), Float::getType(), 0)); |
| 3442 | } |
| 3443 | |
| 3444 | RValue<Float> floorss(RValue<Float> val) |
| 3445 | { |
| 3446 | return roundss(val, 1); |
| 3447 | } |
| 3448 | |
| 3449 | RValue<Float> ceilss(RValue<Float> val) |
| 3450 | { |
| 3451 | return roundss(val, 2); |
| 3452 | } |
| 3453 | |
| 3454 | RValue<Float4> roundps(RValue<Float4> val, unsigned char imm) |
| 3455 | { |
| 3456 | llvm::Function *roundps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_round_ps); |
| 3457 | |
| 3458 | return RValue<Float4>(V(jit->builder->CreateCall2(roundps, ARGS(V(val.value), V(Nucleus::createConstantInt(imm)))))); |
| 3459 | } |
| 3460 | |
| 3461 | RValue<Float4> floorps(RValue<Float4> val) |
| 3462 | { |
| 3463 | return roundps(val, 1); |
| 3464 | } |
| 3465 | |
| 3466 | RValue<Float4> ceilps(RValue<Float4> val) |
| 3467 | { |
| 3468 | return roundps(val, 2); |
| 3469 | } |
| 3470 | |
| 3471 | RValue<Int4> pabsd(RValue<Int4> x) |
| 3472 | { |
| 3473 | return RValue<Int4>(V(lowerPABS(V(x.value)))); |
| 3474 | } |
| 3475 | |
| 3476 | RValue<Short4> paddsw(RValue<Short4> x, RValue<Short4> y) |
| 3477 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3478 | # if LLVM_VERSION_MAJOR >= 8 |
| 3479 | return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 3480 | # else |
| 3481 | llvm::Function *paddsw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_padds_w); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3482 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3483 | return As<Short4>(V(jit->builder->CreateCall2(paddsw, ARGS(V(x.value), V(y.value))))); |
| 3484 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | RValue<Short4> psubsw(RValue<Short4> x, RValue<Short4> y) |
| 3488 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3489 | # if LLVM_VERSION_MAJOR >= 8 |
| 3490 | return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 3491 | # else |
| 3492 | llvm::Function *psubsw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psubs_w); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3493 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3494 | return As<Short4>(V(jit->builder->CreateCall2(psubsw, ARGS(V(x.value), V(y.value))))); |
| 3495 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | RValue<UShort4> paddusw(RValue<UShort4> x, RValue<UShort4> y) |
| 3499 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3500 | # if LLVM_VERSION_MAJOR >= 8 |
| 3501 | return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 3502 | # else |
| 3503 | llvm::Function *paddusw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_paddus_w); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3504 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3505 | return As<UShort4>(V(jit->builder->CreateCall2(paddusw, ARGS(V(x.value), V(y.value))))); |
| 3506 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | RValue<UShort4> psubusw(RValue<UShort4> x, RValue<UShort4> y) |
| 3510 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3511 | # if LLVM_VERSION_MAJOR >= 8 |
| 3512 | return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 3513 | # else |
| 3514 | llvm::Function *psubusw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psubus_w); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3515 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3516 | return As<UShort4>(V(jit->builder->CreateCall2(psubusw, ARGS(V(x.value), V(y.value))))); |
| 3517 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | RValue<SByte8> paddsb(RValue<SByte8> x, RValue<SByte8> y) |
| 3521 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3522 | # if LLVM_VERSION_MAJOR >= 8 |
| 3523 | return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 3524 | # else |
| 3525 | llvm::Function *paddsb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_padds_b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3526 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3527 | return As<SByte8>(V(jit->builder->CreateCall2(paddsb, ARGS(V(x.value), V(y.value))))); |
| 3528 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | RValue<SByte8> psubsb(RValue<SByte8> x, RValue<SByte8> y) |
| 3532 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3533 | # if LLVM_VERSION_MAJOR >= 8 |
| 3534 | return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 3535 | # else |
| 3536 | llvm::Function *psubsb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psubs_b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3537 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3538 | return As<SByte8>(V(jit->builder->CreateCall2(psubsb, ARGS(V(x.value), V(y.value))))); |
| 3539 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | RValue<Byte8> paddusb(RValue<Byte8> x, RValue<Byte8> y) |
| 3543 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3544 | # if LLVM_VERSION_MAJOR >= 8 |
| 3545 | return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 3546 | # else |
| 3547 | llvm::Function *paddusb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_paddus_b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3548 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3549 | return As<Byte8>(V(jit->builder->CreateCall2(paddusb, ARGS(V(x.value), V(y.value))))); |
| 3550 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | RValue<Byte8> psubusb(RValue<Byte8> x, RValue<Byte8> y) |
| 3554 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3555 | # if LLVM_VERSION_MAJOR >= 8 |
| 3556 | return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 3557 | # else |
| 3558 | llvm::Function *psubusb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psubus_b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3559 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3560 | return As<Byte8>(V(jit->builder->CreateCall2(psubusb, ARGS(V(x.value), V(y.value))))); |
| 3561 | # endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | RValue<UShort4> pavgw(RValue<UShort4> x, RValue<UShort4> y) |
| 3565 | { |
| 3566 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
| 3567 | } |
| 3568 | |
| 3569 | RValue<Short4> pmaxsw(RValue<Short4> x, RValue<Short4> y) |
| 3570 | { |
| 3571 | return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
| 3572 | } |
| 3573 | |
| 3574 | RValue<Short4> pminsw(RValue<Short4> x, RValue<Short4> y) |
| 3575 | { |
| 3576 | return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
| 3577 | } |
| 3578 | |
| 3579 | RValue<Short4> pcmpgtw(RValue<Short4> x, RValue<Short4> y) |
| 3580 | { |
| 3581 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); |
| 3582 | } |
| 3583 | |
| 3584 | RValue<Short4> pcmpeqw(RValue<Short4> x, RValue<Short4> y) |
| 3585 | { |
| 3586 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); |
| 3587 | } |
| 3588 | |
| 3589 | RValue<Byte8> pcmpgtb(RValue<SByte8> x, RValue<SByte8> y) |
| 3590 | { |
| 3591 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 3592 | } |
| 3593 | |
| 3594 | RValue<Byte8> pcmpeqb(RValue<Byte8> x, RValue<Byte8> y) |
| 3595 | { |
| 3596 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 3597 | } |
| 3598 | |
| 3599 | RValue<Short4> packssdw(RValue<Int2> x, RValue<Int2> y) |
| 3600 | { |
| 3601 | llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packssdw_128); |
| 3602 | |
| 3603 | return As<Short4>(V(jit->builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); |
| 3604 | } |
| 3605 | |
| 3606 | RValue<Short8> packssdw(RValue<Int4> x, RValue<Int4> y) |
| 3607 | { |
| 3608 | llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packssdw_128); |
| 3609 | |
| 3610 | return RValue<Short8>(V(jit->builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); |
| 3611 | } |
| 3612 | |
| 3613 | RValue<SByte8> packsswb(RValue<Short4> x, RValue<Short4> y) |
| 3614 | { |
| 3615 | llvm::Function *packsswb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packsswb_128); |
| 3616 | |
| 3617 | return As<SByte8>(V(jit->builder->CreateCall2(packsswb, ARGS(V(x.value), V(y.value))))); |
| 3618 | } |
| 3619 | |
| 3620 | RValue<Byte8> packuswb(RValue<Short4> x, RValue<Short4> y) |
| 3621 | { |
| 3622 | llvm::Function *packuswb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packuswb_128); |
| 3623 | |
| 3624 | return As<Byte8>(V(jit->builder->CreateCall2(packuswb, ARGS(V(x.value), V(y.value))))); |
| 3625 | } |
| 3626 | |
| 3627 | RValue<UShort8> packusdw(RValue<Int4> x, RValue<Int4> y) |
| 3628 | { |
| 3629 | if(CPUID::supportsSSE4_1()) |
| 3630 | { |
| 3631 | llvm::Function *packusdw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_packusdw); |
| 3632 | |
| 3633 | return RValue<UShort8>(V(jit->builder->CreateCall2(packusdw, ARGS(V(x.value), V(y.value))))); |
| 3634 | } |
| 3635 | else |
| 3636 | { |
| 3637 | RValue<Int4> bx = (x & ~(x >> 31)) - Int4(0x8000); |
| 3638 | RValue<Int4> by = (y & ~(y >> 31)) - Int4(0x8000); |
| 3639 | |
| 3640 | return As<UShort8>(packssdw(bx, by) + Short8(0x8000u)); |
| 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | RValue<UShort4> psrlw(RValue<UShort4> x, unsigned char y) |
| 3645 | { |
| 3646 | llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_w); |
| 3647 | |
| 3648 | return As<UShort4>(V(jit->builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3649 | } |
| 3650 | |
| 3651 | RValue<UShort8> psrlw(RValue<UShort8> x, unsigned char y) |
| 3652 | { |
| 3653 | llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_w); |
| 3654 | |
| 3655 | return RValue<UShort8>(V(jit->builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3656 | } |
| 3657 | |
| 3658 | RValue<Short4> psraw(RValue<Short4> x, unsigned char y) |
| 3659 | { |
| 3660 | llvm::Function *psraw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_w); |
| 3661 | |
| 3662 | return As<Short4>(V(jit->builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3663 | } |
| 3664 | |
| 3665 | RValue<Short8> psraw(RValue<Short8> x, unsigned char y) |
| 3666 | { |
| 3667 | llvm::Function *psraw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_w); |
| 3668 | |
| 3669 | return RValue<Short8>(V(jit->builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3670 | } |
| 3671 | |
| 3672 | RValue<Short4> psllw(RValue<Short4> x, unsigned char y) |
| 3673 | { |
| 3674 | llvm::Function *psllw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_w); |
| 3675 | |
| 3676 | return As<Short4>(V(jit->builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3677 | } |
| 3678 | |
| 3679 | RValue<Short8> psllw(RValue<Short8> x, unsigned char y) |
| 3680 | { |
| 3681 | llvm::Function *psllw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_w); |
| 3682 | |
| 3683 | return RValue<Short8>(V(jit->builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3684 | } |
| 3685 | |
| 3686 | RValue<Int2> pslld(RValue<Int2> x, unsigned char y) |
| 3687 | { |
| 3688 | llvm::Function *pslld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_d); |
| 3689 | |
| 3690 | return As<Int2>(V(jit->builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3691 | } |
| 3692 | |
| 3693 | RValue<Int4> pslld(RValue<Int4> x, unsigned char y) |
| 3694 | { |
| 3695 | llvm::Function *pslld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_d); |
| 3696 | |
| 3697 | return RValue<Int4>(V(jit->builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3698 | } |
| 3699 | |
| 3700 | RValue<Int2> psrad(RValue<Int2> x, unsigned char y) |
| 3701 | { |
| 3702 | llvm::Function *psrad = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_d); |
| 3703 | |
| 3704 | return As<Int2>(V(jit->builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3705 | } |
| 3706 | |
| 3707 | RValue<Int4> psrad(RValue<Int4> x, unsigned char y) |
| 3708 | { |
| 3709 | llvm::Function *psrad = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_d); |
| 3710 | |
| 3711 | return RValue<Int4>(V(jit->builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3712 | } |
| 3713 | |
| 3714 | RValue<UInt2> psrld(RValue<UInt2> x, unsigned char y) |
| 3715 | { |
| 3716 | llvm::Function *psrld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_d); |
| 3717 | |
| 3718 | return As<UInt2>(V(jit->builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3719 | } |
| 3720 | |
| 3721 | RValue<UInt4> psrld(RValue<UInt4> x, unsigned char y) |
| 3722 | { |
| 3723 | llvm::Function *psrld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_d); |
| 3724 | |
| 3725 | return RValue<UInt4>(V(jit->builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
| 3726 | } |
| 3727 | |
| 3728 | RValue<Int4> pmaxsd(RValue<Int4> x, RValue<Int4> y) |
| 3729 | { |
| 3730 | return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
| 3731 | } |
| 3732 | |
| 3733 | RValue<Int4> pminsd(RValue<Int4> x, RValue<Int4> y) |
| 3734 | { |
| 3735 | return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
| 3736 | } |
| 3737 | |
| 3738 | RValue<UInt4> pmaxud(RValue<UInt4> x, RValue<UInt4> y) |
| 3739 | { |
| 3740 | return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_UGT))); |
| 3741 | } |
| 3742 | |
| 3743 | RValue<UInt4> pminud(RValue<UInt4> x, RValue<UInt4> y) |
| 3744 | { |
| 3745 | return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_ULT))); |
| 3746 | } |
| 3747 | |
| 3748 | RValue<Short4> pmulhw(RValue<Short4> x, RValue<Short4> y) |
| 3749 | { |
| 3750 | llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulh_w); |
| 3751 | |
| 3752 | return As<Short4>(V(jit->builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); |
| 3753 | } |
| 3754 | |
| 3755 | RValue<UShort4> pmulhuw(RValue<UShort4> x, RValue<UShort4> y) |
| 3756 | { |
| 3757 | llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulhu_w); |
| 3758 | |
| 3759 | return As<UShort4>(V(jit->builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); |
| 3760 | } |
| 3761 | |
| 3762 | RValue<Int2> pmaddwd(RValue<Short4> x, RValue<Short4> y) |
| 3763 | { |
| 3764 | llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmadd_wd); |
| 3765 | |
| 3766 | return As<Int2>(V(jit->builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); |
| 3767 | } |
| 3768 | |
| 3769 | RValue<Short8> pmulhw(RValue<Short8> x, RValue<Short8> y) |
| 3770 | { |
| 3771 | llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulh_w); |
| 3772 | |
| 3773 | return RValue<Short8>(V(jit->builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); |
| 3774 | } |
| 3775 | |
| 3776 | RValue<UShort8> pmulhuw(RValue<UShort8> x, RValue<UShort8> y) |
| 3777 | { |
| 3778 | llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulhu_w); |
| 3779 | |
| 3780 | return RValue<UShort8>(V(jit->builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); |
| 3781 | } |
| 3782 | |
| 3783 | RValue<Int4> pmaddwd(RValue<Short8> x, RValue<Short8> y) |
| 3784 | { |
| 3785 | llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmadd_wd); |
| 3786 | |
| 3787 | return RValue<Int4>(V(jit->builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); |
| 3788 | } |
| 3789 | |
| 3790 | RValue<Int> movmskps(RValue<Float4> x) |
| 3791 | { |
| 3792 | llvm::Function *movmskps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_movmsk_ps); |
| 3793 | |
| 3794 | return RValue<Int>(V(jit->builder->CreateCall(movmskps, ARGS(V(x.value))))); |
| 3795 | } |
| 3796 | |
| 3797 | RValue<Int> pmovmskb(RValue<Byte8> x) |
| 3798 | { |
| 3799 | llvm::Function *pmovmskb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmovmskb_128); |
| 3800 | |
| 3801 | return RValue<Int>(V(jit->builder->CreateCall(pmovmskb, ARGS(V(x.value))))) & 0xFF; |
| 3802 | } |
| 3803 | |
| 3804 | RValue<Int4> pmovzxbd(RValue<Byte16> x) |
| 3805 | { |
| 3806 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
| 3807 | } |
| 3808 | |
| 3809 | RValue<Int4> pmovsxbd(RValue<SByte16> x) |
| 3810 | { |
| 3811 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
| 3812 | } |
| 3813 | |
| 3814 | RValue<Int4> pmovzxwd(RValue<UShort8> x) |
| 3815 | { |
| 3816 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
| 3817 | } |
| 3818 | |
| 3819 | RValue<Int4> pmovsxwd(RValue<Short8> x) |
| 3820 | { |
| 3821 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
| 3822 | } |
| 3823 | |
| 3824 | } // namespace x86 |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3825 | #endif // defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 3826 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 3827 | #ifdef ENABLE_RR_PRINT |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3828 | // extractAll returns a vector containing the extracted n scalar value of |
| 3829 | // the vector vec. |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3830 | static std::vector<Value *> extractAll(Value *vec, int n) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3831 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3832 | std::vector<Value *> elements; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3833 | elements.reserve(n); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3834 | for(int i = 0; i < n; i++) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 3835 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3836 | auto el = V(jit->builder->CreateExtractElement(V(vec), i)); |
| 3837 | elements.push_back(el); |
| 3838 | } |
| 3839 | return elements; |
| 3840 | } |
| 3841 | |
| 3842 | // toInt returns all the integer values in vals extended to a native width |
| 3843 | // integer. |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3844 | static std::vector<Value *> toInt(const std::vector<Value *> &vals, bool isSigned) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3845 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3846 | auto intTy = ::llvm::Type::getIntNTy(jit->context, sizeof(int) * 8); // Natural integer width. |
| 3847 | std::vector<Value *> elements; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3848 | elements.reserve(vals.size()); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3849 | for(auto v : vals) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3850 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3851 | if(isSigned) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 3852 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3853 | elements.push_back(V(jit->builder->CreateSExt(V(v), intTy))); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 3854 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3855 | else |
| 3856 | { |
| 3857 | elements.push_back(V(jit->builder->CreateZExt(V(v), intTy))); |
| 3858 | } |
| 3859 | } |
| 3860 | return elements; |
| 3861 | } |
| 3862 | |
| 3863 | // toDouble returns all the float values in vals extended to doubles. |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3864 | static std::vector<Value *> toDouble(const std::vector<Value *> &vals) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3865 | { |
| 3866 | auto doubleTy = ::llvm::Type::getDoubleTy(jit->context); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3867 | std::vector<Value *> elements; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3868 | elements.reserve(vals.size()); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3869 | for(auto v : vals) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3870 | { |
| 3871 | elements.push_back(V(jit->builder->CreateFPExt(V(v), doubleTy))); |
| 3872 | } |
| 3873 | return elements; |
| 3874 | } |
| 3875 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3876 | std::vector<Value *> PrintValue::Ty<Byte>::val(const RValue<Byte> &v) |
| 3877 | { |
| 3878 | return toInt({ v.value }, false); |
| 3879 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3880 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3881 | std::vector<Value *> PrintValue::Ty<Byte4>::val(const RValue<Byte4> &v) |
| 3882 | { |
| 3883 | return toInt(extractAll(v.value, 4), false); |
| 3884 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3885 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3886 | std::vector<Value *> PrintValue::Ty<Int>::val(const RValue<Int> &v) |
| 3887 | { |
| 3888 | return toInt({ v.value }, true); |
| 3889 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3890 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3891 | std::vector<Value *> PrintValue::Ty<Int2>::val(const RValue<Int2> &v) |
| 3892 | { |
| 3893 | return toInt(extractAll(v.value, 2), true); |
| 3894 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3895 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3896 | std::vector<Value *> PrintValue::Ty<Int4>::val(const RValue<Int4> &v) |
| 3897 | { |
| 3898 | return toInt(extractAll(v.value, 4), true); |
| 3899 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3900 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3901 | std::vector<Value *> PrintValue::Ty<UInt>::val(const RValue<UInt> &v) |
| 3902 | { |
| 3903 | return toInt({ v.value }, false); |
| 3904 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3905 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3906 | std::vector<Value *> PrintValue::Ty<UInt2>::val(const RValue<UInt2> &v) |
| 3907 | { |
| 3908 | return toInt(extractAll(v.value, 2), false); |
| 3909 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3910 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3911 | std::vector<Value *> PrintValue::Ty<UInt4>::val(const RValue<UInt4> &v) |
| 3912 | { |
| 3913 | return toInt(extractAll(v.value, 4), false); |
| 3914 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3915 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3916 | std::vector<Value *> PrintValue::Ty<Short>::val(const RValue<Short> &v) |
| 3917 | { |
| 3918 | return toInt({ v.value }, true); |
| 3919 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3920 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3921 | std::vector<Value *> PrintValue::Ty<Short4>::val(const RValue<Short4> &v) |
| 3922 | { |
| 3923 | return toInt(extractAll(v.value, 4), true); |
| 3924 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3925 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3926 | std::vector<Value *> PrintValue::Ty<UShort>::val(const RValue<UShort> &v) |
| 3927 | { |
| 3928 | return toInt({ v.value }, false); |
| 3929 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3930 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3931 | std::vector<Value *> PrintValue::Ty<UShort4>::val(const RValue<UShort4> &v) |
| 3932 | { |
| 3933 | return toInt(extractAll(v.value, 4), false); |
| 3934 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3935 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3936 | std::vector<Value *> PrintValue::Ty<Float>::val(const RValue<Float> &v) |
| 3937 | { |
| 3938 | return toDouble({ v.value }); |
| 3939 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3940 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3941 | std::vector<Value *> PrintValue::Ty<Float4>::val(const RValue<Float4> &v) |
| 3942 | { |
| 3943 | return toDouble(extractAll(v.value, 4)); |
| 3944 | } |
Nicolas Capens | 41a7302 | 2020-01-30 00:30:14 -0500 | [diff] [blame] | 3945 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3946 | std::vector<Value *> PrintValue::Ty<const char *>::val(const char *v) |
| 3947 | { |
| 3948 | return { V(jit->builder->CreateGlobalStringPtr(v)) }; |
| 3949 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3950 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3951 | void Printv(const char *function, const char *file, int line, const char *fmt, std::initializer_list<PrintValue> args) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3952 | { |
| 3953 | // LLVM types used below. |
| 3954 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3955 | auto intTy = ::llvm::Type::getIntNTy(jit->context, sizeof(int) * 8); // Natural integer width. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3956 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(jit->context); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3957 | auto funcTy = ::llvm::FunctionType::get(i32Ty, { i8PtrTy }, true); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3958 | |
| 3959 | auto func = jit->module->getOrInsertFunction("printf", funcTy); |
| 3960 | |
| 3961 | // Build the printf format message string. |
| 3962 | std::string str; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3963 | if(file != nullptr) { str += (line > 0) ? "%s:%d " : "%s "; } |
| 3964 | if(function != nullptr) { str += "%s "; } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3965 | str += fmt; |
| 3966 | |
| 3967 | // Perform subsitution on all '{n}' bracketed indices in the format |
| 3968 | // message. |
| 3969 | int i = 0; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3970 | for(const PrintValue &arg : args) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3971 | { |
| 3972 | str = replace(str, "{" + std::to_string(i++) + "}", arg.format); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3975 | ::llvm::SmallVector<::llvm::Value *, 8> vals; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3976 | |
| 3977 | // The format message is always the first argument. |
| 3978 | vals.push_back(jit->builder->CreateGlobalStringPtr(str)); |
| 3979 | |
| 3980 | // Add optional file, line and function info if provided. |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3981 | if(file != nullptr) |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 3982 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3983 | vals.push_back(jit->builder->CreateGlobalStringPtr(file)); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3984 | if(line > 0) |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 3985 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3986 | vals.push_back(::llvm::ConstantInt::get(intTy, line)); |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 3987 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3988 | } |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3989 | if(function != nullptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3990 | { |
| 3991 | vals.push_back(jit->builder->CreateGlobalStringPtr(function)); |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 3992 | } |
| 3993 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3994 | // Add all format arguments. |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3995 | for(const PrintValue &arg : args) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 3996 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 3997 | for(auto val : arg.values) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 3998 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3999 | vals.push_back(V(val)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4000 | } |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4003 | jit->builder->CreateCall(func, vals); |
| 4004 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4005 | #endif // ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4006 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4007 | void Nop() |
| 4008 | { |
| 4009 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
| 4010 | auto funcTy = ::llvm::FunctionType::get(voidTy, {}, false); |
| 4011 | auto func = jit->module->getOrInsertFunction("nop", funcTy); |
| 4012 | jit->builder->CreateCall(func); |
| 4013 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4014 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4015 | void EmitDebugLocation() |
| 4016 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4017 | #ifdef ENABLE_RR_DEBUG_INFO |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4018 | if(jit->debugInfo != nullptr) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4019 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4020 | jit->debugInfo->EmitLocation(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4021 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4022 | #endif // ENABLE_RR_DEBUG_INFO |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4023 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4024 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4025 | void EmitDebugVariable(Value *value) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4026 | { |
| 4027 | #ifdef ENABLE_RR_DEBUG_INFO |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4028 | if(jit->debugInfo != nullptr) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4029 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4030 | jit->debugInfo->EmitVariable(value); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4031 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4032 | #endif // ENABLE_RR_DEBUG_INFO |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4033 | } |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4034 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4035 | void FlushDebug() |
| 4036 | { |
| 4037 | #ifdef ENABLE_RR_DEBUG_INFO |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4038 | if(jit->debugInfo != nullptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4039 | { |
| 4040 | jit->debugInfo->Flush(); |
| 4041 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4042 | #endif // ENABLE_RR_DEBUG_INFO |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | } // namespace rr |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4046 | |
| 4047 | // ------------------------------ Coroutines ------------------------------ |
| 4048 | |
| 4049 | namespace { |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4050 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4051 | // Magic values retuned by llvm.coro.suspend. |
| 4052 | // See: https://llvm.org/docs/Coroutines.html#llvm-coro-suspend-intrinsic |
| 4053 | enum SuspendAction |
| 4054 | { |
| 4055 | SuspendActionSuspend = -1, |
| 4056 | SuspendActionResume = 0, |
| 4057 | SuspendActionDestroy = 1 |
| 4058 | }; |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4059 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4060 | void promoteFunctionToCoroutine() |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4061 | { |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4062 | ASSERT(jit->coroutine.id == nullptr); |
| 4063 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4064 | // Types |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4065 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
| 4066 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 4067 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 4068 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 4069 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(jit->context); |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4070 | auto promiseTy = jit->coroutine.yieldType; |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4071 | auto promisePtrTy = promiseTy->getPointerTo(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4072 | |
| 4073 | // LLVM intrinsics |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4074 | auto coro_id = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_id); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4075 | auto coro_size = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_size, { i32Ty }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4076 | auto coro_begin = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_begin); |
| 4077 | auto coro_resume = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_resume); |
| 4078 | auto coro_end = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_end); |
| 4079 | auto coro_free = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_free); |
| 4080 | auto coro_destroy = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_destroy); |
| 4081 | auto coro_promise = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_promise); |
| 4082 | auto coro_done = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_done); |
| 4083 | auto coro_suspend = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_suspend); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4084 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4085 | auto allocFrameTy = ::llvm::FunctionType::get(i8PtrTy, { i32Ty }, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4086 | auto allocFrame = jit->module->getOrInsertFunction("coroutine_alloc_frame", allocFrameTy); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4087 | auto freeFrameTy = ::llvm::FunctionType::get(voidTy, { i8PtrTy }, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4088 | auto freeFrame = jit->module->getOrInsertFunction("coroutine_free_frame", freeFrameTy); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4089 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4090 | auto oldInsertionPoint = jit->builder->saveIP(); |
| 4091 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4092 | // Build the coroutine_await() function: |
| 4093 | // |
| 4094 | // bool coroutine_await(CoroutineHandle* handle, YieldType* out) |
| 4095 | // { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4096 | // if(llvm.coro.done(handle)) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4097 | // { |
| 4098 | // return false; |
| 4099 | // } |
| 4100 | // else |
| 4101 | // { |
| 4102 | // *value = (T*)llvm.coro.promise(handle); |
| 4103 | // llvm.coro.resume(handle); |
| 4104 | // return true; |
| 4105 | // } |
| 4106 | // } |
| 4107 | // |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4108 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4109 | auto args = jit->coroutine.await->arg_begin(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4110 | auto handle = args++; |
| 4111 | auto outPtr = args++; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4112 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "co_await", jit->coroutine.await)); |
| 4113 | auto doneBlock = llvm::BasicBlock::Create(jit->context, "done", jit->coroutine.await); |
| 4114 | auto resumeBlock = llvm::BasicBlock::Create(jit->context, "resume", jit->coroutine.await); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4115 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4116 | auto done = jit->builder->CreateCall(coro_done, { handle }, "done"); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4117 | jit->builder->CreateCondBr(done, doneBlock, resumeBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4118 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4119 | jit->builder->SetInsertPoint(doneBlock); |
| 4120 | jit->builder->CreateRet(::llvm::ConstantInt::getFalse(i1Ty)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4121 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4122 | jit->builder->SetInsertPoint(resumeBlock); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4123 | auto promiseAlignment = ::llvm::ConstantInt::get(i32Ty, 4); // TODO: Get correct alignment. |
| 4124 | auto promisePtr = jit->builder->CreateCall(coro_promise, { handle, promiseAlignment, ::llvm::ConstantInt::get(i1Ty, 0) }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4125 | auto promise = jit->builder->CreateLoad(jit->builder->CreatePointerCast(promisePtr, promisePtrTy)); |
| 4126 | jit->builder->CreateStore(promise, outPtr); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4127 | jit->builder->CreateCall(coro_resume, { handle }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4128 | jit->builder->CreateRet(::llvm::ConstantInt::getTrue(i1Ty)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | // Build the coroutine_destroy() function: |
| 4132 | // |
| 4133 | // void coroutine_destroy(CoroutineHandle* handle) |
| 4134 | // { |
| 4135 | // llvm.coro.destroy(handle); |
| 4136 | // } |
| 4137 | // |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4138 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4139 | auto handle = jit->coroutine.destroy->arg_begin(); |
| 4140 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->coroutine.destroy)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4141 | jit->builder->CreateCall(coro_destroy, { handle }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4142 | jit->builder->CreateRetVoid(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | // Begin building the main coroutine_begin() function. |
| 4146 | // |
| 4147 | // CoroutineHandle* coroutine_begin(<Arguments>) |
| 4148 | // { |
| 4149 | // YieldType promise; |
| 4150 | // auto id = llvm.coro.id(0, &promise, nullptr, nullptr); |
| 4151 | // void* frame = coroutine_alloc_frame(llvm.coro.size.i32()); |
| 4152 | // CoroutineHandle *handle = llvm.coro.begin(id, frame); |
| 4153 | // |
| 4154 | // ... <REACTOR CODE> ... |
| 4155 | // |
| 4156 | // end: |
| 4157 | // SuspendAction action = llvm.coro.suspend(none, true /* final */); // <-- RESUME POINT |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4158 | // switch(action) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4159 | // { |
| 4160 | // case SuspendActionResume: |
| 4161 | // UNREACHABLE(); // Illegal to resume after final suspend. |
| 4162 | // case SuspendActionDestroy: |
| 4163 | // goto destroy; |
| 4164 | // default: // (SuspendActionSuspend) |
| 4165 | // goto suspend; |
| 4166 | // } |
| 4167 | // |
| 4168 | // destroy: |
| 4169 | // coroutine_free_frame(llvm.coro.free(id, handle)); |
| 4170 | // goto suspend; |
| 4171 | // |
| 4172 | // suspend: |
| 4173 | // llvm.coro.end(handle, false); |
| 4174 | // return handle; |
| 4175 | // } |
| 4176 | // |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4177 | |
| 4178 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 368d39c | 2020-01-08 23:10:47 +0000 | [diff] [blame] | 4179 | jit->debugInfo = std::make_unique<rr::DebugInfo>(jit->builder.get(), &jit->context, jit->module.get(), jit->function); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4180 | #endif // ENABLE_RR_DEBUG_INFO |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4181 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4182 | jit->coroutine.suspendBlock = llvm::BasicBlock::Create(jit->context, "suspend", jit->function); |
| 4183 | jit->coroutine.endBlock = llvm::BasicBlock::Create(jit->context, "end", jit->function); |
| 4184 | jit->coroutine.destroyBlock = llvm::BasicBlock::Create(jit->context, "destroy", jit->function); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4185 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4186 | jit->builder->SetInsertPoint(jit->coroutine.entryBlock, jit->coroutine.entryBlock->begin()); |
| 4187 | jit->coroutine.promise = jit->builder->CreateAlloca(promiseTy, nullptr, "promise"); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4188 | jit->coroutine.id = jit->builder->CreateCall(coro_id, { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4189 | ::llvm::ConstantInt::get(i32Ty, 0), |
| 4190 | jit->builder->CreatePointerCast(jit->coroutine.promise, i8PtrTy), |
| 4191 | ::llvm::ConstantPointerNull::get(i8PtrTy), |
| 4192 | ::llvm::ConstantPointerNull::get(i8PtrTy), |
| 4193 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4194 | auto size = jit->builder->CreateCall(coro_size, {}); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4195 | auto frame = jit->builder->CreateCall(allocFrame, { size }); |
| 4196 | jit->coroutine.handle = jit->builder->CreateCall(coro_begin, { jit->coroutine.id, frame }); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4197 | |
| 4198 | // Build the suspend block |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4199 | jit->builder->SetInsertPoint(jit->coroutine.suspendBlock); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4200 | jit->builder->CreateCall(coro_end, { jit->coroutine.handle, ::llvm::ConstantInt::get(i1Ty, 0) }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4201 | jit->builder->CreateRet(jit->coroutine.handle); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4202 | |
| 4203 | // Build the end block |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4204 | jit->builder->SetInsertPoint(jit->coroutine.endBlock); |
| 4205 | auto action = jit->builder->CreateCall(coro_suspend, { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4206 | ::llvm::ConstantTokenNone::get(jit->context), |
| 4207 | ::llvm::ConstantInt::get(i1Ty, 1), // final: true |
| 4208 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4209 | auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4210 | // switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), trapBlock); // TODO: Trap attempting to resume after final suspend |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4211 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4212 | |
| 4213 | // Build the destroy block |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4214 | jit->builder->SetInsertPoint(jit->coroutine.destroyBlock); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4215 | auto memory = jit->builder->CreateCall(coro_free, { jit->coroutine.id, jit->coroutine.handle }); |
| 4216 | jit->builder->CreateCall(freeFrame, { memory }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4217 | jit->builder->CreateBr(jit->coroutine.suspendBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4218 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4219 | // Switch back to original insert point to continue building the coroutine. |
| 4220 | jit->builder->restoreIP(oldInsertionPoint); |
| 4221 | } |
| 4222 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4223 | } // anonymous namespace |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4224 | |
| 4225 | namespace rr { |
| 4226 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4227 | void Nucleus::createCoroutine(Type *YieldType, const std::vector<Type *> &Params) |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4228 | { |
| 4229 | // Coroutines are initially created as a regular function. |
| 4230 | // Upon the first call to Yield(), the function is promoted to a true |
| 4231 | // coroutine. |
| 4232 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
| 4233 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 4234 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(jit->context); |
| 4235 | auto handleTy = i8PtrTy; |
| 4236 | auto boolTy = i1Ty; |
| 4237 | auto promiseTy = T(YieldType); |
| 4238 | auto promisePtrTy = promiseTy->getPointerTo(); |
| 4239 | |
| 4240 | jit->function = rr::createFunction("coroutine_begin", handleTy, T(Params)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4241 | jit->coroutine.await = rr::createFunction("coroutine_await", boolTy, { handleTy, promisePtrTy }); |
| 4242 | jit->coroutine.destroy = rr::createFunction("coroutine_destroy", voidTy, { handleTy }); |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4243 | jit->coroutine.yieldType = promiseTy; |
| 4244 | jit->coroutine.entryBlock = llvm::BasicBlock::Create(jit->context, "function", jit->function); |
| 4245 | |
| 4246 | jit->builder->SetInsertPoint(jit->coroutine.entryBlock); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4247 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4248 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4249 | void Nucleus::yield(Value *val) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4250 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4251 | if(jit->coroutine.id == nullptr) |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4252 | { |
| 4253 | // First call to yield(). |
| 4254 | // Promote the function to a full coroutine. |
| 4255 | promoteFunctionToCoroutine(); |
| 4256 | ASSERT(jit->coroutine.id != nullptr); |
| 4257 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4258 | |
| 4259 | // promise = val; |
| 4260 | // |
| 4261 | // auto action = llvm.coro.suspend(none, false /* final */); // <-- RESUME POINT |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4262 | // switch(action) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4263 | // { |
| 4264 | // case SuspendActionResume: |
| 4265 | // goto resume; |
| 4266 | // case SuspendActionDestroy: |
| 4267 | // goto destroy; |
| 4268 | // default: // (SuspendActionSuspend) |
| 4269 | // goto suspend; |
| 4270 | // } |
| 4271 | // resume: |
| 4272 | // |
| 4273 | |
| 4274 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 4275 | Variable::materializeAll(); |
| 4276 | |
| 4277 | // Types |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4278 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 4279 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4280 | |
| 4281 | // Intrinsics |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4282 | auto coro_suspend = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_suspend); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4283 | |
| 4284 | // Create a block to resume execution. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4285 | auto resumeBlock = llvm::BasicBlock::Create(jit->context, "resume", jit->function); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4286 | |
| 4287 | // Store the promise (yield value) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4288 | jit->builder->CreateStore(V(val), jit->coroutine.promise); |
| 4289 | auto action = jit->builder->CreateCall(coro_suspend, { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4290 | ::llvm::ConstantTokenNone::get(jit->context), |
| 4291 | ::llvm::ConstantInt::get(i1Ty, 0), // final: true |
| 4292 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4293 | auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4294 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), resumeBlock); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4295 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4296 | |
| 4297 | // Continue building in the resume block. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4298 | jit->builder->SetInsertPoint(resumeBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4299 | } |
| 4300 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 4301 | std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4302 | { |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4303 | bool isCoroutine = jit->coroutine.id != nullptr; |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4304 | if(isCoroutine) |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4305 | { |
| 4306 | jit->builder->CreateBr(jit->coroutine.endBlock); |
| 4307 | } |
| 4308 | else |
| 4309 | { |
| 4310 | // Coroutine without a Yield acts as a regular function. |
| 4311 | // The 'coroutine_begin' function returns a nullptr for the coroutine |
| 4312 | // handle. |
| 4313 | jit->builder->CreateRet(llvm::Constant::getNullValue(jit->function->getReturnType())); |
| 4314 | // The 'coroutine_await' function always returns false (coroutine done). |
| 4315 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->coroutine.await)); |
| 4316 | jit->builder->CreateRet(llvm::Constant::getNullValue(jit->coroutine.await->getReturnType())); |
| 4317 | // The 'coroutine_destroy' does nothing, returns void. |
| 4318 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->coroutine.destroy)); |
| 4319 | jit->builder->CreateRetVoid(); |
| 4320 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4321 | |
| 4322 | #ifdef ENABLE_RR_DEBUG_INFO |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4323 | if(jit->debugInfo != nullptr) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4324 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4325 | jit->debugInfo->Finalize(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4326 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4327 | #endif // ENABLE_RR_DEBUG_INFO |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4328 | |
| 4329 | if(false) |
| 4330 | { |
| 4331 | std::error_code error; |
| 4332 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4333 | jit->module->print(file, 0); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4334 | } |
| 4335 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4336 | if(isCoroutine) |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4337 | { |
| 4338 | // Run manadory coroutine transforms. |
| 4339 | llvm::legacy::PassManager pm; |
Nicolas Capens | 6c3dc35 | 2020-01-28 09:22:39 -0500 | [diff] [blame] | 4340 | |
Nicolas Capens | ba9de97 | 2020-01-30 10:14:30 -0500 | [diff] [blame] | 4341 | #if LLVM_VERSION_MAJOR >= 9 |
Nicolas Capens | 6c3dc35 | 2020-01-28 09:22:39 -0500 | [diff] [blame] | 4342 | pm.add(llvm::createCoroEarlyLegacyPass()); |
| 4343 | pm.add(llvm::createCoroSplitLegacyPass()); |
| 4344 | pm.add(llvm::createCoroElideLegacyPass()); |
| 4345 | pm.add(llvm::createBarrierNoopPass()); |
| 4346 | pm.add(llvm::createCoroCleanupLegacyPass()); |
| 4347 | #else |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4348 | pm.add(llvm::createCoroEarlyPass()); |
| 4349 | pm.add(llvm::createCoroSplitPass()); |
| 4350 | pm.add(llvm::createCoroElidePass()); |
| 4351 | pm.add(llvm::createBarrierNoopPass()); |
| 4352 | pm.add(llvm::createCoroCleanupPass()); |
Nicolas Capens | 6c3dc35 | 2020-01-28 09:22:39 -0500 | [diff] [blame] | 4353 | #endif |
| 4354 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4355 | pm.run(*jit->module); |
| 4356 | } |
| 4357 | |
| 4358 | #if defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
| 4359 | { |
| 4360 | llvm::legacy::PassManager pm; |
| 4361 | pm.add(llvm::createVerifierPass()); |
| 4362 | pm.run(*jit->module); |
| 4363 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4364 | #endif // defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4365 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 4366 | auto cfg = cfgEdit.apply(jit->config); |
| 4367 | jit->optimize(cfg); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4368 | |
| 4369 | if(false) |
| 4370 | { |
| 4371 | std::error_code error; |
| 4372 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4373 | jit->module->print(file, 0); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | llvm::Function *funcs[Nucleus::CoroutineEntryCount]; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4377 | funcs[Nucleus::CoroutineEntryBegin] = jit->function; |
| 4378 | funcs[Nucleus::CoroutineEntryAwait] = jit->coroutine.await; |
| 4379 | funcs[Nucleus::CoroutineEntryDestroy] = jit->coroutine.destroy; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 4380 | auto routine = jit->acquireRoutine(funcs, Nucleus::CoroutineEntryCount, cfg); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4381 | jit.reset(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4382 | |
| 4383 | return routine; |
| 4384 | } |
| 4385 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4386 | Nucleus::CoroutineHandle Nucleus::invokeCoroutineBegin(Routine &routine, std::function<Nucleus::CoroutineHandle()> func) |
| 4387 | { |
| 4388 | return func(); |
| 4389 | } |
| 4390 | |
Nicolas Capens | 6c3dc35 | 2020-01-28 09:22:39 -0500 | [diff] [blame] | 4391 | } // namespace rr |