Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 8 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 14 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 15 | #include "Reactor.hpp" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 16 | |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 17 | #include "x86.hpp" |
| 18 | #include "CPUID.hpp" |
| 19 | #include "Thread.hpp" |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 20 | #include "ExecutableMemory.hpp" |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 21 | #include "MutexLock.hpp" |
| 22 | |
| 23 | #undef min |
| 24 | #undef max |
| 25 | |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 26 | #if REACTOR_LLVM_VERSION < 7 |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 27 | #include "llvm/Analysis/LoopPass.h" |
| 28 | #include "llvm/Constants.h" |
| 29 | #include "llvm/Function.h" |
| 30 | #include "llvm/GlobalVariable.h" |
| 31 | #include "llvm/Intrinsics.h" |
| 32 | #include "llvm/LLVMContext.h" |
| 33 | #include "llvm/Module.h" |
| 34 | #include "llvm/PassManager.h" |
| 35 | #include "llvm/Support/IRBuilder.h" |
| 36 | #include "llvm/Support/TargetSelect.h" |
| 37 | #include "llvm/Target/TargetData.h" |
| 38 | #include "llvm/Target/TargetOptions.h" |
| 39 | #include "llvm/Transforms/Scalar.h" |
| 40 | #include "../lib/ExecutionEngine/JIT/JIT.h" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 41 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 42 | #include "LLVMRoutine.hpp" |
| 43 | #include "LLVMRoutineManager.hpp" |
| 44 | |
| 45 | #define ARGS(...) __VA_ARGS__ |
| 46 | #else |
| 47 | #include "llvm/Analysis/LoopPass.h" |
| 48 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 49 | #include "llvm/ExecutionEngine/JITSymbol.h" |
| 50 | #include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
| 51 | #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
| 52 | #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" |
| 53 | #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" |
| 54 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
| 55 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
| 56 | #include "llvm/IR/Constants.h" |
| 57 | #include "llvm/IR/DataLayout.h" |
| 58 | #include "llvm/IR/Function.h" |
| 59 | #include "llvm/IR/GlobalVariable.h" |
| 60 | #include "llvm/IR/IRBuilder.h" |
| 61 | #include "llvm/IR/Intrinsics.h" |
| 62 | #include "llvm/IR/LLVMContext.h" |
| 63 | #include "llvm/IR/LegacyPassManager.h" |
Nicolas Capens | adfbbcb | 2018-10-31 14:38:53 -0400 | [diff] [blame] | 64 | #include "llvm/IR/Mangler.h" |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 65 | #include "llvm/IR/Module.h" |
| 66 | #include "llvm/Support/Error.h" |
| 67 | #include "llvm/Support/TargetSelect.h" |
| 68 | #include "llvm/Target/TargetOptions.h" |
| 69 | #include "llvm/Transforms/InstCombine/InstCombine.h" |
| 70 | #include "llvm/Transforms/Scalar.h" |
| 71 | #include "llvm/Transforms/Scalar/GVN.h" |
| 72 | |
| 73 | #include "LLVMRoutine.hpp" |
| 74 | |
| 75 | #define ARGS(...) {__VA_ARGS__} |
| 76 | #define CreateCall2 CreateCall |
| 77 | #define CreateCall3 CreateCall |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 78 | |
| 79 | #include <unordered_map> |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 80 | #endif |
| 81 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 82 | #include <fstream> |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 83 | #include <numeric> |
| 84 | #include <thread> |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 85 | |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 86 | #if defined(__i386__) || defined(__x86_64__) |
| 87 | #include <xmmintrin.h> |
| 88 | #endif |
| 89 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 90 | #include <math.h> |
| 91 | |
Nicolas Capens | cb12258 | 2014-05-06 23:34:44 -0400 | [diff] [blame] | 92 | #if defined(__x86_64__) && defined(_WIN32) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 93 | extern "C" void X86CompilationCallback() |
| 94 | { |
| 95 | assert(false); // UNIMPLEMENTED |
| 96 | } |
| 97 | #endif |
| 98 | |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 99 | #if REACTOR_LLVM_VERSION < 7 |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 100 | namespace llvm |
| 101 | { |
| 102 | extern bool JITEmitDebugInfo; |
| 103 | } |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 104 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 105 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 106 | namespace rr |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 107 | { |
| 108 | class LLVMReactorJIT; |
| 109 | } |
| 110 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 111 | namespace |
| 112 | { |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 113 | rr::LLVMReactorJIT *reactorJIT = nullptr; |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 114 | llvm::IRBuilder<> *builder = nullptr; |
| 115 | llvm::LLVMContext *context = nullptr; |
| 116 | llvm::Module *module = nullptr; |
| 117 | llvm::Function *function = nullptr; |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 118 | |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 119 | rr::MutexLock codegenMutex; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 120 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 121 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 122 | std::string replace(std::string str, const std::string& substr, const std::string& replacement) |
| 123 | { |
| 124 | size_t pos = 0; |
| 125 | while((pos = str.find(substr, pos)) != std::string::npos) { |
| 126 | str.replace(pos, substr.length(), replacement); |
| 127 | pos += replacement.length(); |
| 128 | } |
| 129 | return str; |
| 130 | } |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 131 | #endif // ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 132 | |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 133 | #if REACTOR_LLVM_VERSION >= 7 |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 134 | llvm::Value *lowerPAVG(llvm::Value *x, llvm::Value *y) |
| 135 | { |
| 136 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 137 | |
| 138 | llvm::VectorType *extTy = |
| 139 | llvm::VectorType::getExtendedElementVectorType(ty); |
| 140 | x = ::builder->CreateZExt(x, extTy); |
| 141 | y = ::builder->CreateZExt(y, extTy); |
| 142 | |
| 143 | // (x + y + 1) >> 1 |
| 144 | llvm::Constant *one = llvm::ConstantInt::get(extTy, 1); |
| 145 | llvm::Value *res = ::builder->CreateAdd(x, y); |
| 146 | res = ::builder->CreateAdd(res, one); |
| 147 | res = ::builder->CreateLShr(res, one); |
| 148 | return ::builder->CreateTrunc(res, ty); |
| 149 | } |
| 150 | |
| 151 | llvm::Value *lowerPMINMAX(llvm::Value *x, llvm::Value *y, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 152 | llvm::ICmpInst::Predicate pred) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 153 | { |
| 154 | return ::builder->CreateSelect(::builder->CreateICmp(pred, x, y), x, y); |
| 155 | } |
| 156 | |
| 157 | llvm::Value *lowerPCMP(llvm::ICmpInst::Predicate pred, llvm::Value *x, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 158 | llvm::Value *y, llvm::Type *dstTy) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 159 | { |
| 160 | return ::builder->CreateSExt(::builder->CreateICmp(pred, x, y), dstTy, ""); |
| 161 | } |
| 162 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 163 | #if defined(__i386__) || defined(__x86_64__) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 164 | llvm::Value *lowerPMOV(llvm::Value *op, llvm::Type *dstType, bool sext) |
| 165 | { |
| 166 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(op->getType()); |
| 167 | llvm::VectorType *dstTy = llvm::cast<llvm::VectorType>(dstType); |
| 168 | |
| 169 | llvm::Value *undef = llvm::UndefValue::get(srcTy); |
| 170 | llvm::SmallVector<uint32_t, 16> mask(dstTy->getNumElements()); |
| 171 | std::iota(mask.begin(), mask.end(), 0); |
| 172 | llvm::Value *v = ::builder->CreateShuffleVector(op, undef, mask); |
| 173 | |
| 174 | return sext ? ::builder->CreateSExt(v, dstTy) |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 175 | : ::builder->CreateZExt(v, dstTy); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | llvm::Value *lowerPABS(llvm::Value *v) |
| 179 | { |
| 180 | llvm::Value *zero = llvm::Constant::getNullValue(v->getType()); |
| 181 | llvm::Value *cmp = ::builder->CreateICmp(llvm::ICmpInst::ICMP_SGT, v, zero); |
| 182 | llvm::Value *neg = ::builder->CreateNeg(v); |
| 183 | return ::builder->CreateSelect(cmp, v, neg); |
| 184 | } |
| 185 | #endif // defined(__i386__) || defined(__x86_64__) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 186 | |
| 187 | #if !defined(__i386__) && !defined(__x86_64__) |
| 188 | llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 189 | llvm::FCmpInst::Predicate pred) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 190 | { |
| 191 | return ::builder->CreateSelect(::builder->CreateFCmp(pred, x, y), x, y); |
| 192 | } |
| 193 | |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 194 | llvm::Value *lowerRound(llvm::Value *x) |
| 195 | { |
| 196 | llvm::Function *nearbyint = llvm::Intrinsic::getDeclaration( |
| 197 | ::module, llvm::Intrinsic::nearbyint, {x->getType()}); |
| 198 | return ::builder->CreateCall(nearbyint, ARGS(x)); |
| 199 | } |
| 200 | |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 201 | llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty) |
| 202 | { |
| 203 | return ::builder->CreateFPToSI(lowerRound(x), ty); |
| 204 | } |
| 205 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 206 | llvm::Value *lowerFloor(llvm::Value *x) |
| 207 | { |
| 208 | llvm::Function *floor = llvm::Intrinsic::getDeclaration( |
| 209 | ::module, llvm::Intrinsic::floor, {x->getType()}); |
| 210 | return ::builder->CreateCall(floor, ARGS(x)); |
| 211 | } |
| 212 | |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 213 | llvm::Value *lowerTrunc(llvm::Value *x) |
| 214 | { |
| 215 | llvm::Function *trunc = llvm::Intrinsic::getDeclaration( |
| 216 | ::module, llvm::Intrinsic::trunc, {x->getType()}); |
| 217 | return ::builder->CreateCall(trunc, ARGS(x)); |
| 218 | } |
| 219 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 220 | // Packed add/sub saturatation |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 221 | llvm::Value *lowerPSAT(llvm::Value *x, llvm::Value *y, bool isAdd, bool isSigned) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 222 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 223 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 224 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 225 | |
| 226 | unsigned numBits = ty->getScalarSizeInBits(); |
| 227 | |
| 228 | llvm::Value *max, *min, *extX, *extY; |
| 229 | if (isSigned) |
| 230 | { |
| 231 | max = llvm::ConstantInt::get(extTy, (1LL << (numBits - 1)) - 1, true); |
| 232 | min = llvm::ConstantInt::get(extTy, (-1LL << (numBits - 1)), true); |
| 233 | extX = ::builder->CreateSExt(x, extTy); |
| 234 | extY = ::builder->CreateSExt(y, extTy); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | assert(numBits <= 64); |
| 239 | uint64_t maxVal = (numBits == 64) ? ~0ULL : (1ULL << numBits) - 1; |
| 240 | max = llvm::ConstantInt::get(extTy, maxVal, false); |
| 241 | min = llvm::ConstantInt::get(extTy, 0, false); |
| 242 | extX = ::builder->CreateZExt(x, extTy); |
| 243 | extY = ::builder->CreateZExt(y, extTy); |
| 244 | } |
| 245 | |
| 246 | llvm::Value *res = isAdd ? ::builder->CreateAdd(extX, extY) |
| 247 | : ::builder->CreateSub(extX, extY); |
| 248 | |
| 249 | res = lowerPMINMAX(res, min, llvm::ICmpInst::ICMP_SGT); |
| 250 | res = lowerPMINMAX(res, max, llvm::ICmpInst::ICMP_SLT); |
| 251 | |
| 252 | return ::builder->CreateTrunc(res, ty); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | llvm::Value *lowerPUADDSAT(llvm::Value *x, llvm::Value *y) |
| 256 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 257 | return lowerPSAT(x, y, true, false); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | llvm::Value *lowerPSADDSAT(llvm::Value *x, llvm::Value *y) |
| 261 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 262 | return lowerPSAT(x, y, true, true); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | llvm::Value *lowerPUSUBSAT(llvm::Value *x, llvm::Value *y) |
| 266 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 267 | return lowerPSAT(x, y, false, false); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | llvm::Value *lowerPSSUBSAT(llvm::Value *x, llvm::Value *y) |
| 271 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 272 | return lowerPSAT(x, y, false, true); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | llvm::Value *lowerSQRT(llvm::Value *x) |
| 276 | { |
| 277 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration( |
| 278 | ::module, llvm::Intrinsic::sqrt, {x->getType()}); |
| 279 | return ::builder->CreateCall(sqrt, ARGS(x)); |
| 280 | } |
| 281 | |
| 282 | llvm::Value *lowerRCP(llvm::Value *x) |
| 283 | { |
| 284 | llvm::Type *ty = x->getType(); |
| 285 | llvm::Constant *one; |
| 286 | if (llvm::VectorType *vectorTy = llvm::dyn_cast<llvm::VectorType>(ty)) |
| 287 | { |
| 288 | one = llvm::ConstantVector::getSplat( |
| 289 | vectorTy->getNumElements(), |
| 290 | llvm::ConstantFP::get(vectorTy->getElementType(), 1)); |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | one = llvm::ConstantFP::get(ty, 1); |
| 295 | } |
| 296 | return ::builder->CreateFDiv(one, x); |
| 297 | } |
| 298 | |
| 299 | llvm::Value *lowerRSQRT(llvm::Value *x) |
| 300 | { |
| 301 | return lowerRCP(lowerSQRT(x)); |
| 302 | } |
| 303 | |
| 304 | llvm::Value *lowerVectorShl(llvm::Value *x, uint64_t scalarY) |
| 305 | { |
| 306 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 307 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 308 | ty->getNumElements(), |
| 309 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
| 310 | return ::builder->CreateShl(x, y); |
| 311 | } |
| 312 | |
| 313 | llvm::Value *lowerVectorAShr(llvm::Value *x, uint64_t scalarY) |
| 314 | { |
| 315 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 316 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 317 | ty->getNumElements(), |
| 318 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
| 319 | return ::builder->CreateAShr(x, y); |
| 320 | } |
| 321 | |
| 322 | llvm::Value *lowerVectorLShr(llvm::Value *x, uint64_t scalarY) |
| 323 | { |
| 324 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 325 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 326 | ty->getNumElements(), |
| 327 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
| 328 | return ::builder->CreateLShr(x, y); |
| 329 | } |
| 330 | |
| 331 | llvm::Value *lowerMulAdd(llvm::Value *x, llvm::Value *y) |
| 332 | { |
| 333 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 334 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 335 | |
| 336 | llvm::Value *extX = ::builder->CreateSExt(x, extTy); |
| 337 | llvm::Value *extY = ::builder->CreateSExt(y, extTy); |
| 338 | llvm::Value *mult = ::builder->CreateMul(extX, extY); |
| 339 | |
| 340 | llvm::Value *undef = llvm::UndefValue::get(extTy); |
| 341 | |
| 342 | llvm::SmallVector<uint32_t, 16> evenIdx; |
| 343 | llvm::SmallVector<uint32_t, 16> oddIdx; |
| 344 | for (uint64_t i = 0, n = ty->getNumElements(); i < n; i += 2) |
| 345 | { |
| 346 | evenIdx.push_back(i); |
| 347 | oddIdx.push_back(i + 1); |
| 348 | } |
| 349 | |
| 350 | llvm::Value *lhs = ::builder->CreateShuffleVector(mult, undef, evenIdx); |
| 351 | llvm::Value *rhs = ::builder->CreateShuffleVector(mult, undef, oddIdx); |
| 352 | return ::builder->CreateAdd(lhs, rhs); |
| 353 | } |
| 354 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 355 | llvm::Value *lowerPack(llvm::Value *x, llvm::Value *y, bool isSigned) |
| 356 | { |
| 357 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(x->getType()); |
| 358 | llvm::VectorType *dstTy = llvm::VectorType::getTruncatedElementVectorType(srcTy); |
| 359 | |
| 360 | llvm::IntegerType *dstElemTy = |
| 361 | llvm::cast<llvm::IntegerType>(dstTy->getElementType()); |
| 362 | |
| 363 | uint64_t truncNumBits = dstElemTy->getIntegerBitWidth(); |
| 364 | assert(truncNumBits < 64 && "shift 64 must be handled separately"); |
| 365 | llvm::Constant *max, *min; |
| 366 | if (isSigned) |
| 367 | { |
| 368 | max = llvm::ConstantInt::get(srcTy, (1LL << (truncNumBits - 1)) - 1, true); |
| 369 | min = llvm::ConstantInt::get(srcTy, (-1LL << (truncNumBits - 1)), true); |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | max = llvm::ConstantInt::get(srcTy, (1ULL << truncNumBits) - 1, false); |
| 374 | min = llvm::ConstantInt::get(srcTy, 0, false); |
| 375 | } |
| 376 | |
| 377 | x = lowerPMINMAX(x, min, llvm::ICmpInst::ICMP_SGT); |
| 378 | x = lowerPMINMAX(x, max, llvm::ICmpInst::ICMP_SLT); |
| 379 | y = lowerPMINMAX(y, min, llvm::ICmpInst::ICMP_SGT); |
| 380 | y = lowerPMINMAX(y, max, llvm::ICmpInst::ICMP_SLT); |
| 381 | |
| 382 | x = ::builder->CreateTrunc(x, dstTy); |
| 383 | y = ::builder->CreateTrunc(y, dstTy); |
| 384 | |
| 385 | llvm::SmallVector<uint32_t, 16> index(srcTy->getNumElements() * 2); |
| 386 | std::iota(index.begin(), index.end(), 0); |
| 387 | |
| 388 | return ::builder->CreateShuffleVector(x, y, index); |
| 389 | } |
| 390 | |
| 391 | llvm::Value *lowerSignMask(llvm::Value *x, llvm::Type *retTy) |
| 392 | { |
| 393 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 394 | llvm::Constant *zero = llvm::ConstantInt::get(ty, 0); |
| 395 | llvm::Value *cmp = ::builder->CreateICmpSLT(x, zero); |
| 396 | |
| 397 | llvm::Value *ret = ::builder->CreateZExt( |
| 398 | ::builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
| 399 | for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
| 400 | { |
| 401 | llvm::Value *elem = ::builder->CreateZExt( |
| 402 | ::builder->CreateExtractElement(cmp, i), retTy); |
| 403 | ret = ::builder->CreateOr(ret, ::builder->CreateShl(elem, i)); |
| 404 | } |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | llvm::Value *lowerFPSignMask(llvm::Value *x, llvm::Type *retTy) |
| 409 | { |
| 410 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 411 | llvm::Constant *zero = llvm::ConstantFP::get(ty, 0); |
| 412 | llvm::Value *cmp = ::builder->CreateFCmpULT(x, zero); |
| 413 | |
| 414 | llvm::Value *ret = ::builder->CreateZExt( |
| 415 | ::builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
| 416 | for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
| 417 | { |
| 418 | llvm::Value *elem = ::builder->CreateZExt( |
| 419 | ::builder->CreateExtractElement(cmp, i), retTy); |
| 420 | ret = ::builder->CreateOr(ret, ::builder->CreateShl(elem, i)); |
| 421 | } |
| 422 | return ret; |
| 423 | } |
| 424 | #endif // !defined(__i386__) && !defined(__x86_64__) |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 425 | #endif // REACTOR_LLVM_VERSION >= 7 |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 426 | |
| 427 | llvm::Value *lowerMulHigh(llvm::Value *x, llvm::Value *y, bool sext) |
| 428 | { |
| 429 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 430 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 431 | |
| 432 | llvm::Value *extX, *extY; |
| 433 | if (sext) |
| 434 | { |
| 435 | extX = ::builder->CreateSExt(x, extTy); |
| 436 | extY = ::builder->CreateSExt(y, extTy); |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | extX = ::builder->CreateZExt(x, extTy); |
| 441 | extY = ::builder->CreateZExt(y, extTy); |
| 442 | } |
| 443 | |
| 444 | llvm::Value *mult = ::builder->CreateMul(extX, extY); |
| 445 | |
| 446 | llvm::IntegerType *intTy = llvm::cast<llvm::IntegerType>(ty->getElementType()); |
| 447 | llvm::Value *mulh = ::builder->CreateAShr(mult, intTy->getBitWidth()); |
| 448 | return ::builder->CreateTrunc(mulh, ty); |
| 449 | } |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 450 | } |
| 451 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 452 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 453 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 454 | #if REACTOR_LLVM_VERSION < 7 |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 455 | class LLVMReactorJIT |
| 456 | { |
| 457 | private: |
| 458 | std::string arch; |
| 459 | llvm::SmallVector<std::string, 16> mattrs; |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 460 | llvm::ExecutionEngine *executionEngine; |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 461 | LLVMRoutineManager *routineManager; |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 462 | |
| 463 | public: |
| 464 | LLVMReactorJIT(const std::string &arch_, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 465 | const llvm::SmallVectorImpl<std::string> &mattrs_) : |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 466 | arch(arch_), |
| 467 | mattrs(mattrs_.begin(), mattrs_.end()), |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 468 | executionEngine(nullptr), |
| 469 | routineManager(nullptr) |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 470 | { |
| 471 | } |
| 472 | |
| 473 | void startSession() |
| 474 | { |
| 475 | std::string error; |
| 476 | |
| 477 | ::module = new llvm::Module("", *::context); |
| 478 | |
| 479 | routineManager = new LLVMRoutineManager(); |
| 480 | |
| 481 | llvm::TargetMachine *targetMachine = |
| 482 | llvm::EngineBuilder::selectTarget( |
| 483 | ::module, arch, "", mattrs, llvm::Reloc::Default, |
| 484 | llvm::CodeModel::JITDefault, &error); |
| 485 | |
| 486 | executionEngine = llvm::JIT::createJIT( |
| 487 | ::module, &error, routineManager, llvm::CodeGenOpt::Aggressive, |
| 488 | true, targetMachine); |
| 489 | } |
| 490 | |
| 491 | void endSession() |
| 492 | { |
| 493 | delete executionEngine; |
| 494 | executionEngine = nullptr; |
| 495 | routineManager = nullptr; |
| 496 | |
| 497 | ::function = nullptr; |
| 498 | ::module = nullptr; |
| 499 | } |
| 500 | |
| 501 | LLVMRoutine *acquireRoutine(llvm::Function *func) |
| 502 | { |
| 503 | void *entry = executionEngine->getPointerToFunction(::function); |
| 504 | return routineManager->acquireRoutine(entry); |
| 505 | } |
| 506 | |
| 507 | void optimize(llvm::Module *module) |
| 508 | { |
| 509 | static llvm::PassManager *passManager = nullptr; |
| 510 | |
| 511 | if(!passManager) |
| 512 | { |
| 513 | passManager = new llvm::PassManager(); |
| 514 | |
| 515 | passManager->add(new llvm::TargetData(*executionEngine->getTargetData())); |
| 516 | passManager->add(llvm::createScalarReplAggregatesPass()); |
| 517 | |
| 518 | for(int pass = 0; pass < 10 && optimization[pass] != Disabled; pass++) |
| 519 | { |
| 520 | switch(optimization[pass]) |
| 521 | { |
| 522 | case Disabled: break; |
| 523 | case CFGSimplification: passManager->add(llvm::createCFGSimplificationPass()); break; |
| 524 | case LICM: passManager->add(llvm::createLICMPass()); break; |
| 525 | case AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); break; |
| 526 | case GVN: passManager->add(llvm::createGVNPass()); break; |
| 527 | case InstructionCombining: passManager->add(llvm::createInstructionCombiningPass()); break; |
| 528 | case Reassociate: passManager->add(llvm::createReassociatePass()); break; |
| 529 | case DeadStoreElimination: passManager->add(llvm::createDeadStoreEliminationPass()); break; |
| 530 | case SCCP: passManager->add(llvm::createSCCPPass()); break; |
| 531 | case ScalarReplAggregates: passManager->add(llvm::createScalarReplAggregatesPass()); break; |
| 532 | default: |
| 533 | assert(false); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | passManager->run(*::module); |
| 539 | } |
| 540 | }; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 541 | #else |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 542 | class ExternalFunctionSymbolResolver |
| 543 | { |
| 544 | private: |
| 545 | using FunctionMap = std::unordered_map<std::string, void *>; |
| 546 | FunctionMap func_; |
| 547 | |
| 548 | public: |
| 549 | ExternalFunctionSymbolResolver() |
| 550 | { |
| 551 | func_.emplace("floorf", reinterpret_cast<void*>(floorf)); |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 552 | func_.emplace("nearbyintf", reinterpret_cast<void*>(nearbyintf)); |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 553 | func_.emplace("truncf", reinterpret_cast<void*>(truncf)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 554 | func_.emplace("printf", reinterpret_cast<void*>(printf)); |
| 555 | func_.emplace("puts", reinterpret_cast<void*>(puts)); |
Chris Forbes | 1a4c712 | 2019-03-15 14:50:47 -0700 | [diff] [blame] | 556 | func_.emplace("fmodf", reinterpret_cast<void*>(fmodf)); |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 557 | func_.emplace("sinf", reinterpret_cast<void*>(sinf)); |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 558 | func_.emplace("cosf", reinterpret_cast<void*>(cosf)); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame^] | 559 | func_.emplace("asinf", reinterpret_cast<void*>(asinf)); |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 560 | |
| 561 | #ifdef __APPLE__ |
| 562 | // LLVM uses this function on macOS for tan. |
| 563 | func_.emplace("sincosf_stret", reinterpret_cast<void*>(__sincosf_stret)); |
| 564 | #elif defined(__linux__) |
| 565 | func_.emplace("sincosf", reinterpret_cast<void*>(sincosf)); |
| 566 | #endif // __APPLE__ |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | void *findSymbol(const std::string &name) const |
| 570 | { |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 571 | // Trim off any underscores from the start of the symbol. LLVM likes |
| 572 | // to append these on macOS. |
| 573 | const char* trimmed = name.c_str(); |
| 574 | while (trimmed[0] == '_') { trimmed++; } |
| 575 | |
| 576 | FunctionMap::const_iterator it = func_.find(trimmed); |
| 577 | assert(it != func_.end()); // Missing functions will likely make the module fail in exciting non-obvious ways. |
| 578 | return it->second; |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 579 | } |
| 580 | }; |
| 581 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 582 | class LLVMReactorJIT |
| 583 | { |
| 584 | private: |
| 585 | using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer; |
| 586 | using CompileLayer = llvm::orc::IRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>; |
| 587 | |
| 588 | llvm::orc::ExecutionSession session; |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 589 | ExternalFunctionSymbolResolver externalSymbolResolver; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 590 | std::shared_ptr<llvm::orc::SymbolResolver> resolver; |
| 591 | std::unique_ptr<llvm::TargetMachine> targetMachine; |
| 592 | const llvm::DataLayout dataLayout; |
| 593 | ObjLayer objLayer; |
| 594 | CompileLayer compileLayer; |
| 595 | size_t emittedFunctionsNum; |
| 596 | |
| 597 | public: |
| 598 | LLVMReactorJIT(const char *arch, const llvm::SmallVectorImpl<std::string>& mattrs, |
| 599 | const llvm::TargetOptions &targetOpts): |
| 600 | resolver(createLegacyLookupResolver( |
| 601 | session, |
| 602 | [this](const std::string &name) { |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 603 | void *func = externalSymbolResolver.findSymbol(name); |
| 604 | if (func != nullptr) |
| 605 | { |
| 606 | return llvm::JITSymbol( |
| 607 | reinterpret_cast<uintptr_t>(func), llvm::JITSymbolFlags::Absolute); |
| 608 | } |
| 609 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 610 | return objLayer.findSymbol(name, true); |
| 611 | }, |
| 612 | [](llvm::Error err) { |
| 613 | if (err) |
| 614 | { |
| 615 | // TODO: Log the symbol resolution errors. |
| 616 | return; |
| 617 | } |
| 618 | })), |
| 619 | targetMachine(llvm::EngineBuilder() |
| 620 | .setMArch(arch) |
| 621 | .setMAttrs(mattrs) |
| 622 | .setTargetOptions(targetOpts) |
| 623 | .selectTarget()), |
| 624 | dataLayout(targetMachine->createDataLayout()), |
| 625 | objLayer( |
| 626 | session, |
| 627 | [this](llvm::orc::VModuleKey) { |
| 628 | return ObjLayer::Resources{ |
| 629 | std::make_shared<llvm::SectionMemoryManager>(), |
| 630 | resolver}; |
| 631 | }), |
| 632 | compileLayer(objLayer, llvm::orc::SimpleCompiler(*targetMachine)), |
| 633 | emittedFunctionsNum(0) |
| 634 | { |
| 635 | } |
| 636 | |
| 637 | void startSession() |
| 638 | { |
| 639 | ::module = new llvm::Module("", *::context); |
| 640 | } |
| 641 | |
| 642 | void endSession() |
| 643 | { |
| 644 | ::function = nullptr; |
| 645 | ::module = nullptr; |
| 646 | } |
| 647 | |
| 648 | LLVMRoutine *acquireRoutine(llvm::Function *func) |
| 649 | { |
| 650 | std::string name = "f" + llvm::Twine(emittedFunctionsNum++).str(); |
| 651 | func->setName(name); |
| 652 | func->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 653 | func->setDoesNotThrow(); |
| 654 | |
| 655 | std::unique_ptr<llvm::Module> mod(::module); |
| 656 | ::module = nullptr; |
| 657 | mod->setDataLayout(dataLayout); |
| 658 | |
| 659 | auto moduleKey = session.allocateVModule(); |
| 660 | llvm::cantFail(compileLayer.addModule(moduleKey, std::move(mod))); |
| 661 | |
Nicolas Capens | adfbbcb | 2018-10-31 14:38:53 -0400 | [diff] [blame] | 662 | std::string mangledName; |
| 663 | { |
| 664 | llvm::raw_string_ostream mangledNameStream(mangledName); |
| 665 | llvm::Mangler::getNameWithPrefix(mangledNameStream, name, dataLayout); |
| 666 | } |
| 667 | |
| 668 | llvm::JITSymbol symbol = compileLayer.findSymbolIn(moduleKey, mangledName, false); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 669 | |
| 670 | llvm::Expected<llvm::JITTargetAddress> expectAddr = symbol.getAddress(); |
Nicolas Capens | adfbbcb | 2018-10-31 14:38:53 -0400 | [diff] [blame] | 671 | if(!expectAddr) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 672 | { |
| 673 | return nullptr; |
| 674 | } |
| 675 | |
| 676 | void *addr = reinterpret_cast<void *>(static_cast<intptr_t>(expectAddr.get())); |
| 677 | return new LLVMRoutine(addr, releaseRoutineCallback, this, moduleKey); |
| 678 | } |
| 679 | |
| 680 | void optimize(llvm::Module *module) |
| 681 | { |
| 682 | std::unique_ptr<llvm::legacy::PassManager> passManager( |
| 683 | new llvm::legacy::PassManager()); |
| 684 | |
| 685 | passManager->add(llvm::createSROAPass()); |
| 686 | |
| 687 | for(int pass = 0; pass < 10 && optimization[pass] != Disabled; pass++) |
| 688 | { |
| 689 | switch(optimization[pass]) |
| 690 | { |
| 691 | case Disabled: break; |
| 692 | case CFGSimplification: passManager->add(llvm::createCFGSimplificationPass()); break; |
| 693 | case LICM: passManager->add(llvm::createLICMPass()); break; |
| 694 | case AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); break; |
| 695 | case GVN: passManager->add(llvm::createGVNPass()); break; |
| 696 | case InstructionCombining: passManager->add(llvm::createInstructionCombiningPass()); break; |
| 697 | case Reassociate: passManager->add(llvm::createReassociatePass()); break; |
| 698 | case DeadStoreElimination: passManager->add(llvm::createDeadStoreEliminationPass()); break; |
| 699 | case SCCP: passManager->add(llvm::createSCCPPass()); break; |
| 700 | case ScalarReplAggregates: passManager->add(llvm::createSROAPass()); break; |
| 701 | default: |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 702 | assert(false); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
| 706 | passManager->run(*::module); |
| 707 | } |
| 708 | |
| 709 | private: |
| 710 | void releaseRoutineModule(llvm::orc::VModuleKey moduleKey) |
| 711 | { |
| 712 | llvm::cantFail(compileLayer.removeModule(moduleKey)); |
| 713 | } |
| 714 | |
| 715 | static void releaseRoutineCallback(LLVMReactorJIT *jit, uint64_t moduleKey) |
| 716 | { |
| 717 | jit->releaseRoutineModule(moduleKey); |
| 718 | } |
| 719 | }; |
| 720 | #endif |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 721 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 722 | Optimization optimization[10] = {InstructionCombining, Disabled}; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 723 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 724 | // The abstract Type* types are implemented as LLVM types, except that |
| 725 | // 64-bit vectors are emulated using 128-bit ones to avoid use of MMX in x86 |
| 726 | // and VFP in ARM, and eliminate the overhead of converting them to explicit |
| 727 | // 128-bit ones. LLVM types are pointers, so we can represent emulated types |
| 728 | // as abstract pointers with small enum values. |
| 729 | enum InternalType : uintptr_t |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 730 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 731 | // Emulated types: |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 732 | Type_v2i32, |
| 733 | Type_v4i16, |
| 734 | Type_v2i16, |
| 735 | Type_v8i8, |
| 736 | Type_v4i8, |
| 737 | Type_v2f32, |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 738 | EmulatedTypeCount, |
| 739 | // Returned by asInternalType() to indicate that the abstract Type* |
| 740 | // should be interpreted as LLVM type pointer: |
| 741 | Type_LLVM |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 742 | }; |
| 743 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 744 | inline InternalType asInternalType(Type *type) |
| 745 | { |
| 746 | InternalType t = static_cast<InternalType>(reinterpret_cast<uintptr_t>(type)); |
| 747 | return (t < EmulatedTypeCount) ? t : Type_LLVM; |
| 748 | } |
| 749 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 750 | llvm::Type *T(Type *t) |
| 751 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 752 | // Use 128-bit vectors to implement logically shorter ones. |
| 753 | switch(asInternalType(t)) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 754 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 755 | case Type_v2i32: return T(Int4::getType()); |
| 756 | case Type_v4i16: return T(Short8::getType()); |
| 757 | case Type_v2i16: return T(Short8::getType()); |
| 758 | case Type_v8i8: return T(Byte16::getType()); |
| 759 | case Type_v4i8: return T(Byte16::getType()); |
| 760 | case Type_v2f32: return T(Float4::getType()); |
| 761 | case Type_LLVM: return reinterpret_cast<llvm::Type*>(t); |
| 762 | default: assert(false); return nullptr; |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 763 | } |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 764 | } |
| 765 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 766 | inline Type *T(llvm::Type *t) |
| 767 | { |
| 768 | return reinterpret_cast<Type*>(t); |
| 769 | } |
| 770 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 771 | Type *T(InternalType t) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 772 | { |
| 773 | return reinterpret_cast<Type*>(t); |
| 774 | } |
| 775 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 776 | inline llvm::Value *V(Value *t) |
| 777 | { |
| 778 | return reinterpret_cast<llvm::Value*>(t); |
| 779 | } |
| 780 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 781 | inline Value *V(llvm::Value *t) |
| 782 | { |
| 783 | return reinterpret_cast<Value*>(t); |
| 784 | } |
| 785 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 786 | inline std::vector<llvm::Type*> &T(std::vector<Type*> &t) |
| 787 | { |
| 788 | return reinterpret_cast<std::vector<llvm::Type*>&>(t); |
| 789 | } |
| 790 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 791 | inline llvm::BasicBlock *B(BasicBlock *t) |
| 792 | { |
| 793 | return reinterpret_cast<llvm::BasicBlock*>(t); |
| 794 | } |
| 795 | |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 796 | inline BasicBlock *B(llvm::BasicBlock *t) |
| 797 | { |
| 798 | return reinterpret_cast<BasicBlock*>(t); |
| 799 | } |
| 800 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 801 | static size_t typeSize(Type *type) |
| 802 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 803 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 804 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 805 | case Type_v2i32: return 8; |
| 806 | case Type_v4i16: return 8; |
| 807 | case Type_v2i16: return 4; |
| 808 | case Type_v8i8: return 8; |
| 809 | case Type_v4i8: return 4; |
| 810 | case Type_v2f32: return 8; |
| 811 | case Type_LLVM: |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 812 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 813 | llvm::Type *t = T(type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 814 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 815 | if(t->isPointerTy()) |
| 816 | { |
| 817 | return sizeof(void*); |
| 818 | } |
| 819 | |
| 820 | // At this point we should only have LLVM 'primitive' types. |
| 821 | unsigned int bits = t->getPrimitiveSizeInBits(); |
| 822 | assert(bits != 0); |
| 823 | |
| 824 | // TODO(capn): Booleans are 1 bit integers in LLVM's SSA type system, |
| 825 | // but are typically stored as one byte. The DataLayout structure should |
| 826 | // be used here and many other places if this assumption fails. |
| 827 | return (bits + 7) / 8; |
| 828 | } |
| 829 | break; |
| 830 | default: |
| 831 | assert(false); |
| 832 | return 0; |
| 833 | } |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 834 | } |
| 835 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 836 | static unsigned int elementCount(Type *type) |
| 837 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 838 | switch(asInternalType(type)) |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 839 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 840 | case Type_v2i32: return 2; |
| 841 | case Type_v4i16: return 4; |
| 842 | case Type_v2i16: return 2; |
| 843 | case Type_v8i8: return 8; |
| 844 | case Type_v4i8: return 4; |
| 845 | case Type_v2f32: return 2; |
| 846 | case Type_LLVM: return llvm::cast<llvm::VectorType>(T(type))->getNumElements(); |
| 847 | default: assert(false); return 0; |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 848 | } |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 849 | } |
| 850 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 851 | static llvm::AtomicOrdering atomicOrdering(bool atomic, std::memory_order memoryOrder) |
| 852 | { |
| 853 | #if REACTOR_LLVM_VERSION < 7 |
| 854 | return llvm::AtomicOrdering::NotAtomic; |
| 855 | #endif |
| 856 | |
| 857 | if(!atomic) |
| 858 | { |
| 859 | return llvm::AtomicOrdering::NotAtomic; |
| 860 | } |
| 861 | |
| 862 | switch(memoryOrder) |
| 863 | { |
| 864 | case std::memory_order_relaxed: return llvm::AtomicOrdering::Monotonic; // https://llvm.org/docs/Atomics.html#monotonic |
| 865 | case std::memory_order_consume: return llvm::AtomicOrdering::Acquire; // https://llvm.org/docs/Atomics.html#acquire: "It should also be used for C++11/C11 memory_order_consume." |
| 866 | case std::memory_order_acquire: return llvm::AtomicOrdering::Acquire; |
| 867 | case std::memory_order_release: return llvm::AtomicOrdering::Release; |
| 868 | case std::memory_order_acq_rel: return llvm::AtomicOrdering::AcquireRelease; |
| 869 | case std::memory_order_seq_cst: return llvm::AtomicOrdering::SequentiallyConsistent; |
| 870 | default: assert(false); return llvm::AtomicOrdering::AcquireRelease; |
| 871 | } |
| 872 | } |
| 873 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 874 | Nucleus::Nucleus() |
| 875 | { |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 876 | ::codegenMutex.lock(); // Reactor and LLVM are currently not thread safe |
Nicolas Capens | b7ea984 | 2015-04-01 10:54:59 -0400 | [diff] [blame] | 877 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 878 | llvm::InitializeNativeTarget(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 879 | |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 880 | #if REACTOR_LLVM_VERSION >= 7 |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 881 | llvm::InitializeNativeTargetAsmPrinter(); |
| 882 | llvm::InitializeNativeTargetAsmParser(); |
| 883 | #endif |
| 884 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 885 | if(!::context) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 886 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 887 | ::context = new llvm::LLVMContext(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 888 | } |
| 889 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 890 | #if defined(__x86_64__) |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 891 | static const char arch[] = "x86-64"; |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 892 | #elif defined(__i386__) |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 893 | static const char arch[] = "x86"; |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 894 | #elif defined(__aarch64__) |
| 895 | static const char arch[] = "arm64"; |
| 896 | #elif defined(__arm__) |
| 897 | static const char arch[] = "arm"; |
Gordana Cmiljanovic | 082dfec | 2018-10-19 11:36:15 +0200 | [diff] [blame] | 898 | #elif defined(__mips__) |
Gordana Cmiljanovic | 20622c0 | 2018-11-05 15:00:11 +0100 | [diff] [blame] | 899 | #if defined(__mips64) |
| 900 | static const char arch[] = "mips64el"; |
| 901 | #else |
| 902 | static const char arch[] = "mipsel"; |
| 903 | #endif |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 904 | #else |
| 905 | #error "unknown architecture" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 906 | #endif |
| 907 | |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 908 | llvm::SmallVector<std::string, 1> mattrs; |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 909 | #if defined(__i386__) || defined(__x86_64__) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 910 | mattrs.push_back(CPUID::supportsMMX() ? "+mmx" : "-mmx"); |
| 911 | mattrs.push_back(CPUID::supportsCMOV() ? "+cmov" : "-cmov"); |
| 912 | mattrs.push_back(CPUID::supportsSSE() ? "+sse" : "-sse"); |
| 913 | mattrs.push_back(CPUID::supportsSSE2() ? "+sse2" : "-sse2"); |
| 914 | mattrs.push_back(CPUID::supportsSSE3() ? "+sse3" : "-sse3"); |
| 915 | mattrs.push_back(CPUID::supportsSSSE3() ? "+ssse3" : "-ssse3"); |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 916 | #if REACTOR_LLVM_VERSION < 7 |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 917 | mattrs.push_back(CPUID::supportsSSE4_1() ? "+sse41" : "-sse41"); |
| 918 | #else |
| 919 | mattrs.push_back(CPUID::supportsSSE4_1() ? "+sse4.1" : "-sse4.1"); |
| 920 | #endif |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 921 | #elif defined(__arm__) |
| 922 | #if __ARM_ARCH >= 8 |
| 923 | mattrs.push_back("+armv8-a"); |
| 924 | #else |
| 925 | // armv7-a requires compiler-rt routines; otherwise, compiled kernel |
| 926 | // might fail to link. |
| 927 | #endif |
| 928 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 929 | |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 930 | #if REACTOR_LLVM_VERSION < 7 |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 931 | llvm::JITEmitDebugInfo = false; |
| 932 | llvm::UnsafeFPMath = true; |
| 933 | // llvm::NoInfsFPMath = true; |
| 934 | // llvm::NoNaNsFPMath = true; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 935 | #else |
| 936 | llvm::TargetOptions targetOpts; |
Nicolas Capens | a764381 | 2018-09-13 14:20:06 -0400 | [diff] [blame] | 937 | targetOpts.UnsafeFPMath = false; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 938 | // targetOpts.NoInfsFPMath = true; |
| 939 | // targetOpts.NoNaNsFPMath = true; |
| 940 | #endif |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 941 | |
| 942 | if(!::reactorJIT) |
| 943 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 944 | #if REACTOR_LLVM_VERSION < 7 |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 945 | ::reactorJIT = new LLVMReactorJIT(arch, mattrs); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 946 | #else |
| 947 | ::reactorJIT = new LLVMReactorJIT(arch, mattrs, targetOpts); |
| 948 | #endif |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | ::reactorJIT->startSession(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 952 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 953 | if(!::builder) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 954 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 955 | ::builder = new llvm::IRBuilder<>(*::context); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
| 959 | Nucleus::~Nucleus() |
| 960 | { |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 961 | ::reactorJIT->endSession(); |
Nicolas Capens | b7ea984 | 2015-04-01 10:54:59 -0400 | [diff] [blame] | 962 | |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 963 | ::codegenMutex.unlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 964 | } |
| 965 | |
Chris Forbes | 878d4b0 | 2019-01-21 10:48:35 -0800 | [diff] [blame] | 966 | Routine *Nucleus::acquireRoutine(const char *name, bool runOptimizations) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 967 | { |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 968 | if(::builder->GetInsertBlock()->empty() || !::builder->GetInsertBlock()->back().isTerminator()) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 969 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 970 | llvm::Type *type = ::function->getReturnType(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 971 | |
| 972 | if(type->isVoidTy()) |
| 973 | { |
| 974 | createRetVoid(); |
| 975 | } |
| 976 | else |
| 977 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 978 | createRet(V(llvm::UndefValue::get(type))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 979 | } |
| 980 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 981 | |
| 982 | if(false) |
| 983 | { |
Nicolas Capens | 543629b | 2019-01-28 11:36:01 -0500 | [diff] [blame] | 984 | #if REACTOR_LLVM_VERSION < 7 |
| 985 | std::string error; |
| 986 | llvm::raw_fd_ostream file((std::string(name) + "-llvm-dump-unopt.txt").c_str(), error); |
| 987 | #else |
| 988 | std::error_code error; |
| 989 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error); |
| 990 | #endif |
| 991 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 992 | ::module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | if(runOptimizations) |
| 996 | { |
| 997 | optimize(); |
| 998 | } |
| 999 | |
| 1000 | if(false) |
| 1001 | { |
Nicolas Capens | 543629b | 2019-01-28 11:36:01 -0500 | [diff] [blame] | 1002 | #if REACTOR_LLVM_VERSION < 7 |
| 1003 | std::string error; |
| 1004 | llvm::raw_fd_ostream file((std::string(name) + "-llvm-dump-opt.txt").c_str(), error); |
| 1005 | #else |
| 1006 | std::error_code error; |
| 1007 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error); |
| 1008 | #endif |
| 1009 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1010 | ::module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1011 | } |
| 1012 | |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1013 | LLVMRoutine *routine = ::reactorJIT->acquireRoutine(::function); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1014 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1015 | return routine; |
| 1016 | } |
| 1017 | |
| 1018 | void Nucleus::optimize() |
| 1019 | { |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1020 | ::reactorJIT->optimize(::module); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1023 | Value *Nucleus::allocateStackVariable(Type *type, int arraySize) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1024 | { |
| 1025 | // Need to allocate it in the entry block for mem2reg to work |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 1026 | llvm::BasicBlock &entryBlock = ::function->getEntryBlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1027 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1028 | llvm::Instruction *declaration; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1029 | |
| 1030 | if(arraySize) |
| 1031 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 1032 | #if REACTOR_LLVM_VERSION < 7 |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1033 | declaration = new llvm::AllocaInst(T(type), V(Nucleus::createConstantInt(arraySize))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1034 | #else |
| 1035 | declaration = new llvm::AllocaInst(T(type), 0, V(Nucleus::createConstantInt(arraySize))); |
| 1036 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1037 | } |
| 1038 | else |
| 1039 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 1040 | #if REACTOR_LLVM_VERSION < 7 |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1041 | declaration = new llvm::AllocaInst(T(type), (llvm::Value*)nullptr); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1042 | #else |
| 1043 | declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value*)nullptr); |
| 1044 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | entryBlock.getInstList().push_front(declaration); |
| 1048 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1049 | return V(declaration); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | BasicBlock *Nucleus::createBasicBlock() |
| 1053 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1054 | return B(llvm::BasicBlock::Create(*::context, "", ::function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | BasicBlock *Nucleus::getInsertBlock() |
| 1058 | { |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 1059 | return B(::builder->GetInsertBlock()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1063 | { |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1064 | // assert(::builder->GetInsertBlock()->back().isTerminator()); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1065 | |
| 1066 | Variable::materializeAll(); |
| 1067 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1068 | ::builder->SetInsertPoint(B(basicBlock)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1071 | void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1072 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1073 | llvm::FunctionType *functionType = llvm::FunctionType::get(T(ReturnType), T(Params), false); |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1074 | ::function = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, "", ::module); |
| 1075 | ::function->setCallingConv(llvm::CallingConv::C); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1076 | |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 1077 | #if defined(_WIN32) && REACTOR_LLVM_VERSION >= 7 |
Nicolas Capens | 52551d1 | 2018-09-13 14:30:56 -0400 | [diff] [blame] | 1078 | // FIXME(capn): |
| 1079 | // On Windows, stack memory is committed in increments of 4 kB pages, with the last page |
| 1080 | // having a trap which allows the OS to grow the stack. For functions with a stack frame |
| 1081 | // larger than 4 kB this can cause an issue when a variable is accessed beyond the guard |
| 1082 | // page. Therefore the compiler emits a call to __chkstk in the function prolog to probe |
| 1083 | // the stack and ensure all pages have been committed. This is currently broken in LLVM |
| 1084 | // JIT, but we can prevent emitting the stack probe call: |
| 1085 | ::function->addFnAttr("stack-probe-size", "1048576"); |
| 1086 | #endif |
| 1087 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1088 | ::builder->SetInsertPoint(llvm::BasicBlock::Create(*::context, "", ::function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1089 | } |
| 1090 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1091 | Value *Nucleus::getArgument(unsigned int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1092 | { |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 1093 | llvm::Function::arg_iterator args = ::function->arg_begin(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1094 | |
| 1095 | while(index) |
| 1096 | { |
| 1097 | args++; |
| 1098 | index--; |
| 1099 | } |
| 1100 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1101 | return V(&*args); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1102 | } |
| 1103 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1104 | void Nucleus::createRetVoid() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1105 | { |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1106 | // Code generated after this point is unreachable, so any variables |
| 1107 | // being read can safely return an undefined value. We have to avoid |
| 1108 | // materializing variables after the terminator ret instruction. |
| 1109 | Variable::killUnmaterialized(); |
| 1110 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1111 | ::builder->CreateRetVoid(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1112 | } |
| 1113 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1114 | void Nucleus::createRet(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1115 | { |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1116 | // Code generated after this point is unreachable, so any variables |
| 1117 | // being read can safely return an undefined value. We have to avoid |
| 1118 | // materializing variables after the terminator ret instruction. |
| 1119 | Variable::killUnmaterialized(); |
| 1120 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1121 | ::builder->CreateRet(V(v)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1122 | } |
| 1123 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1124 | void Nucleus::createBr(BasicBlock *dest) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1125 | { |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1126 | Variable::materializeAll(); |
| 1127 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1128 | ::builder->CreateBr(B(dest)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1129 | } |
| 1130 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1131 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1132 | { |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1133 | Variable::materializeAll(); |
| 1134 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1135 | ::builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1139 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1140 | return V(::builder->CreateAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1144 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1145 | return V(::builder->CreateSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1149 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1150 | return V(::builder->CreateMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1154 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1155 | return V(::builder->CreateUDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1159 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1160 | return V(::builder->CreateSDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1164 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1165 | return V(::builder->CreateFAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1169 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1170 | return V(::builder->CreateFSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1174 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1175 | return V(::builder->CreateFMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1179 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1180 | return V(::builder->CreateFDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1184 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1185 | return V(::builder->CreateURem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1189 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1190 | return V(::builder->CreateSRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1194 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1195 | return V(::builder->CreateFRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1199 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1200 | return V(::builder->CreateShl(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1204 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1205 | return V(::builder->CreateLShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1209 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1210 | return V(::builder->CreateAShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1214 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1215 | return V(::builder->CreateAnd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1219 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1220 | return V(::builder->CreateOr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1224 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1225 | return V(::builder->CreateXor(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1228 | Value *Nucleus::createNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1229 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1230 | return V(::builder->CreateNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1231 | } |
| 1232 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1233 | Value *Nucleus::createFNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1234 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1235 | return V(::builder->CreateFNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1236 | } |
| 1237 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1238 | Value *Nucleus::createNot(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1239 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1240 | return V(::builder->CreateNot(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1241 | } |
| 1242 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1243 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1244 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1245 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1246 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1247 | case Type_v2i32: |
| 1248 | case Type_v4i16: |
| 1249 | case Type_v8i8: |
| 1250 | case Type_v2f32: |
| 1251 | return createBitCast( |
| 1252 | createInsertElement( |
| 1253 | V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1254 | createLoad(createBitCast(ptr, Pointer<Long>::getType()), Long::getType(), isVolatile, alignment, atomic, memoryOrder), |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1255 | 0), |
| 1256 | type); |
| 1257 | case Type_v2i16: |
| 1258 | case Type_v4i8: |
| 1259 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1260 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1261 | Value *u = V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))); |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1262 | Value *i = createLoad(createBitCast(ptr, Pointer<Int>::getType()), Int::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1263 | i = createZExt(i, Long::getType()); |
| 1264 | Value *v = createInsertElement(u, i, 0); |
| 1265 | return createBitCast(v, type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1266 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1267 | // Fallthrough to non-emulated case. |
| 1268 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1269 | { |
| 1270 | assert(V(ptr)->getType()->getContainedType(0) == T(type)); |
| 1271 | auto load = new llvm::LoadInst(V(ptr), "", isVolatile, alignment); |
| 1272 | load->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1273 | |
| 1274 | return V(::builder->Insert(load)); |
| 1275 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1276 | default: |
| 1277 | assert(false); return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1278 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1281 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1282 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1283 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1284 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1285 | case Type_v2i32: |
| 1286 | case Type_v4i16: |
| 1287 | case Type_v8i8: |
| 1288 | case Type_v2f32: |
| 1289 | createStore( |
| 1290 | createExtractElement( |
| 1291 | createBitCast(value, T(llvm::VectorType::get(T(Long::getType()), 2))), Long::getType(), 0), |
| 1292 | createBitCast(ptr, Pointer<Long>::getType()), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1293 | Long::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1294 | return value; |
| 1295 | case Type_v2i16: |
| 1296 | case Type_v4i8: |
| 1297 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1298 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1299 | createStore( |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1300 | createExtractElement(createBitCast(value, Int4::getType()), Int::getType(), 0), |
| 1301 | createBitCast(ptr, Pointer<Int>::getType()), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1302 | Int::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1303 | return value; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1304 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1305 | // Fallthrough to non-emulated case. |
| 1306 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1307 | { |
| 1308 | assert(V(ptr)->getType()->getContainedType(0) == T(type)); |
| 1309 | auto store = ::builder->Insert(new llvm::StoreInst(V(value), V(ptr), isVolatile, alignment)); |
| 1310 | store->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1311 | |
| 1312 | return value; |
| 1313 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1314 | default: |
| 1315 | assert(false); return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1316 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1317 | } |
| 1318 | |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1319 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1320 | { |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1321 | assert(V(ptr)->getType()->getContainedType(0) == T(type)); |
| 1322 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1323 | if(sizeof(void*) == 8) |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1324 | { |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1325 | // LLVM manual: "When indexing into an array, pointer or vector, |
| 1326 | // integers of any width are allowed, and they are not required to |
| 1327 | // be constant. These integers are treated as signed values where |
| 1328 | // relevant." |
| 1329 | // |
| 1330 | // Thus if we want indexes to be treated as unsigned we have to |
| 1331 | // zero-extend them ourselves. |
| 1332 | // |
| 1333 | // Note that this is not because we want to address anywhere near |
| 1334 | // 4 GB of data. Instead this is important for performance because |
| 1335 | // x86 supports automatic zero-extending of 32-bit registers to |
| 1336 | // 64-bit. Thus when indexing into an array using a uint32 is |
| 1337 | // actually faster than an int32. |
| 1338 | index = unsignedIndex ? |
| 1339 | createZExt(index, Long::getType()) : |
| 1340 | createSExt(index, Long::getType()); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1341 | } |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1342 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1343 | // For non-emulated types we can rely on LLVM's GEP to calculate the |
| 1344 | // effective address correctly. |
| 1345 | if(asInternalType(type) == Type_LLVM) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1346 | { |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1347 | return V(::builder->CreateGEP(V(ptr), V(index))); |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1350 | // For emulated types we have to multiply the index by the intended |
| 1351 | // type size ourselves to obain the byte offset. |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1352 | index = (sizeof(void*) == 8) ? |
| 1353 | createMul(index, createConstantLong((int64_t)typeSize(type))) : |
| 1354 | createMul(index, createConstantInt((int)typeSize(type))); |
| 1355 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1356 | // Cast to a byte pointer, apply the byte offset, and cast back to the |
| 1357 | // original pointer type. |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1358 | return createBitCast( |
| 1359 | V(::builder->CreateGEP(V(createBitCast(ptr, T(llvm::PointerType::get(T(Byte::getType()), 0)))), V(index))), |
| 1360 | T(llvm::PointerType::get(T(type), 0))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1361 | } |
| 1362 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1363 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value) |
| 1364 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1365 | return V(::builder->CreateAtomicRMW(llvm::AtomicRMWInst::Add, V(ptr), V(value), llvm::AtomicOrdering::SequentiallyConsistent)); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1366 | } |
| 1367 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1368 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1369 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1370 | return V(::builder->CreateTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1371 | } |
| 1372 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1373 | Value *Nucleus::createZExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1374 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1375 | return V(::builder->CreateZExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1376 | } |
| 1377 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1378 | Value *Nucleus::createSExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1379 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1380 | return V(::builder->CreateSExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1381 | } |
| 1382 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1383 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1384 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1385 | return V(::builder->CreateFPToSI(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1386 | } |
| 1387 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1388 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1389 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1390 | return V(::builder->CreateSIToFP(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1391 | } |
| 1392 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1393 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1394 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1395 | return V(::builder->CreateFPTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1396 | } |
| 1397 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1398 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1399 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1400 | return V(::builder->CreateFPExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1401 | } |
| 1402 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1403 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1404 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1405 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1406 | // support for casting between scalars and wide vectors. Emulate them by writing to the stack and |
| 1407 | // reading back as the destination type. |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1408 | if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1409 | { |
| 1410 | Value *readAddress = allocateStackVariable(destType); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1411 | Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0))); |
| 1412 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1413 | return createLoad(readAddress, destType); |
| 1414 | } |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1415 | else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1416 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1417 | Value *writeAddress = allocateStackVariable(T(V(v)->getType())); |
| 1418 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1419 | Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0))); |
| 1420 | return createLoad(readAddress, destType); |
| 1421 | } |
| 1422 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1423 | return V(::builder->CreateBitCast(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1424 | } |
| 1425 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1426 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1427 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1428 | return V(::builder->CreateICmpEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1432 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1433 | return V(::builder->CreateICmpNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1437 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1438 | return V(::builder->CreateICmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1442 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1443 | return V(::builder->CreateICmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1447 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1448 | return V(::builder->CreateICmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1452 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1453 | return V(::builder->CreateICmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1457 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1458 | return V(::builder->CreateICmpSGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1462 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1463 | return V(::builder->CreateICmpSGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1467 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1468 | return V(::builder->CreateICmpSLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1472 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1473 | return V(::builder->CreateICmpSLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1477 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1478 | return V(::builder->CreateFCmpOEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1482 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1483 | return V(::builder->CreateFCmpOGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1487 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1488 | return V(::builder->CreateFCmpOGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1492 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1493 | return V(::builder->CreateFCmpOLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1497 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1498 | return V(::builder->CreateFCmpOLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1502 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1503 | return V(::builder->CreateFCmpONE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1507 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1508 | return V(::builder->CreateFCmpORD(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1512 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1513 | return V(::builder->CreateFCmpUNO(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1517 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1518 | return V(::builder->CreateFCmpUEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1522 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1523 | return V(::builder->CreateFCmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1527 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1528 | return V(::builder->CreateFCmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1532 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1533 | return V(::builder->CreateFCmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1537 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1538 | return V(::builder->CreateFCmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1542 | { |
Ben Clayton | 71008d8 | 2019-03-05 17:17:59 +0000 | [diff] [blame] | 1543 | return V(::builder->CreateFCmpUNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1544 | } |
| 1545 | |
Nicolas Capens | e95d534 | 2016-09-30 11:37:28 -0400 | [diff] [blame] | 1546 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1547 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1548 | assert(V(vector)->getType()->getContainedType(0) == T(type)); |
| 1549 | return V(::builder->CreateExtractElement(V(vector), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1553 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1554 | return V(::builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1557 | Value *Nucleus::createShuffleVector(Value *v1, Value *v2, const int *select) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1558 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1559 | int size = llvm::cast<llvm::VectorType>(V(v1)->getType())->getNumElements(); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 1560 | const int maxSize = 16; |
| 1561 | llvm::Constant *swizzle[maxSize]; |
| 1562 | assert(size <= maxSize); |
| 1563 | |
| 1564 | for(int i = 0; i < size; i++) |
| 1565 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1566 | swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), select[i]); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(swizzle, size)); |
| 1570 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1571 | return V(::builder->CreateShuffleVector(V(v1), V(v2), shuffle)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1572 | } |
| 1573 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1574 | Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1575 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1576 | return V(::builder->CreateSelect(V(c), V(ifTrue), V(ifFalse))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1577 | } |
| 1578 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1579 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1580 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1581 | return reinterpret_cast<SwitchCases*>(::builder->CreateSwitch(V(control), B(defaultBranch), numCases)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1582 | } |
| 1583 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1584 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1585 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1586 | llvm::SwitchInst *sw = reinterpret_cast<llvm::SwitchInst *>(switchCases); |
| 1587 | sw->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), label, true), B(branch)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1588 | } |
| 1589 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1590 | void Nucleus::createUnreachable() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1591 | { |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1592 | ::builder->CreateUnreachable(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1593 | } |
| 1594 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1595 | Type *Nucleus::getPointerType(Type *ElementType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1596 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1597 | return T(llvm::PointerType::get(T(ElementType), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1598 | } |
| 1599 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1600 | Value *Nucleus::createNullValue(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1601 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1602 | return V(llvm::Constant::getNullValue(T(Ty))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1603 | } |
| 1604 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1605 | Value *Nucleus::createConstantLong(int64_t i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1606 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1607 | return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1608 | } |
| 1609 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1610 | Value *Nucleus::createConstantInt(int i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1611 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1612 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1613 | } |
| 1614 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1615 | Value *Nucleus::createConstantInt(unsigned int i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1616 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1617 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*::context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1618 | } |
| 1619 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1620 | Value *Nucleus::createConstantBool(bool b) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1621 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1622 | return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(*::context), b)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1623 | } |
| 1624 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1625 | Value *Nucleus::createConstantByte(signed char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1626 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1627 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1628 | } |
| 1629 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1630 | Value *Nucleus::createConstantByte(unsigned char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1631 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1632 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(*::context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1633 | } |
| 1634 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1635 | Value *Nucleus::createConstantShort(short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1636 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1637 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1638 | } |
| 1639 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1640 | Value *Nucleus::createConstantShort(unsigned short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1641 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1642 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(*::context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1643 | } |
| 1644 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1645 | Value *Nucleus::createConstantFloat(float x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1646 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1647 | return V(llvm::ConstantFP::get(T(Float::getType()), x)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1648 | } |
| 1649 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1650 | Value *Nucleus::createNullPointer(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1651 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1652 | return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1653 | } |
| 1654 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1655 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1656 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1657 | assert(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [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. |
| 1660 | assert(numElements <= 16 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1661 | llvm::Constant *constantVector[16]; |
| 1662 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1663 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1664 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1665 | constantVector[i] = llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1666 | } |
| 1667 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1668 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 1672 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1673 | assert(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1674 | const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. |
| 1675 | const int numElements = llvm::cast<llvm::VectorType>(T(type))->getNumElements(); // Number of elements of the underlying vector type. |
| 1676 | assert(numElements <= 8 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1677 | llvm::Constant *constantVector[8]; |
| 1678 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1679 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1680 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1681 | constantVector[i] = llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1682 | } |
| 1683 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1684 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1685 | } |
| 1686 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1687 | Type *Void::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1688 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1689 | return T(llvm::Type::getVoidTy(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1690 | } |
| 1691 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1692 | Type *Bool::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1693 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1694 | return T(llvm::Type::getInt1Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1695 | } |
| 1696 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1697 | Type *Byte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1698 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1699 | return T(llvm::Type::getInt8Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1700 | } |
| 1701 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1702 | Type *SByte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1703 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1704 | return T(llvm::Type::getInt8Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1705 | } |
| 1706 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1707 | Type *Short::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1708 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1709 | return T(llvm::Type::getInt16Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1710 | } |
| 1711 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1712 | Type *UShort::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1713 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1714 | return T(llvm::Type::getInt16Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1715 | } |
| 1716 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1717 | Type *Byte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1718 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1719 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1720 | } |
| 1721 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1722 | Type *SByte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1723 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1724 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1725 | } |
| 1726 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1727 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1728 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1729 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1730 | return x86::paddusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1731 | #else |
| 1732 | return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 1733 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1734 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1735 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1736 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1737 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1738 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1739 | return x86::psubusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1740 | #else |
| 1741 | return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 1742 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1743 | } |
| 1744 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1745 | RValue<Int> SignMask(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1746 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1747 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1748 | return x86::pmovmskb(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1749 | #else |
| 1750 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 1751 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1752 | } |
| 1753 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1754 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1755 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1756 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1757 | // return x86::pcmpgtb(x, y); // FIXME: Signedness |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1758 | //#else |
| 1759 | // return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 1760 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1761 | // } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1762 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1763 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1764 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1765 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1766 | return x86::pcmpeqb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1767 | #else |
| 1768 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 1769 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1770 | } |
| 1771 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1772 | Type *Byte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1773 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1774 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1775 | } |
| 1776 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1777 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1778 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1779 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1780 | return x86::paddsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1781 | #else |
| 1782 | return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 1783 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1784 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1785 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1786 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1787 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1788 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1789 | return x86::psubsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1790 | #else |
| 1791 | return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 1792 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1793 | } |
| 1794 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1795 | RValue<Int> SignMask(RValue<SByte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1796 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1797 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1798 | return x86::pmovmskb(As<Byte8>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1799 | #else |
| 1800 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 1801 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1802 | } |
| 1803 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1804 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1805 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1806 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1807 | return x86::pcmpgtb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1808 | #else |
| 1809 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 1810 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1811 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1812 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1813 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1814 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1815 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1816 | return x86::pcmpeqb(As<Byte8>(x), As<Byte8>(y)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1817 | #else |
| 1818 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 1819 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1820 | } |
| 1821 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1822 | Type *SByte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1823 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1824 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1825 | } |
| 1826 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1827 | Type *Byte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1828 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1829 | return T(llvm::VectorType::get(T(Byte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1830 | } |
| 1831 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1832 | Type *SByte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1833 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1834 | return T(llvm::VectorType::get(T(SByte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1835 | } |
| 1836 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1837 | Type *Short2::getType() |
| 1838 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1839 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1840 | } |
| 1841 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1842 | Type *UShort2::getType() |
| 1843 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1844 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1845 | } |
| 1846 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1847 | Short4::Short4(RValue<Int4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1848 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1849 | int select[8] = {0, 2, 4, 6, 0, 2, 4, 6}; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1850 | Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType()); |
| 1851 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1852 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 1853 | Value *short4 = As<Short4>(Int2(As<Int4>(packed))).value; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1854 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1855 | storeValue(short4); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1856 | } |
| 1857 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1858 | // Short4::Short4(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1859 | // { |
| 1860 | // } |
| 1861 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1862 | Short4::Short4(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1863 | { |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1864 | Int4 v4i32 = Int4(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1865 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1866 | v4i32 = As<Int4>(x86::packssdw(v4i32, v4i32)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1867 | #else |
| 1868 | Value *v = v4i32.loadValue(); |
| 1869 | v4i32 = As<Int4>(V(lowerPack(V(v), V(v), true))); |
| 1870 | #endif |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1871 | |
| 1872 | storeValue(As<Short4>(Int2(v4i32)).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1873 | } |
| 1874 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1875 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1876 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1877 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1878 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 1879 | |
| 1880 | return x86::psllw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1881 | #else |
| 1882 | return As<Short4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 1883 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1884 | } |
| 1885 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1886 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1887 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1888 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1889 | return x86::psraw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1890 | #else |
| 1891 | return As<Short4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 1892 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1893 | } |
| 1894 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1895 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1896 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1897 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1898 | return x86::pmaxsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1899 | #else |
| 1900 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
| 1901 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1902 | } |
| 1903 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1904 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1905 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1906 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1907 | return x86::pminsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1908 | #else |
| 1909 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
| 1910 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1911 | } |
| 1912 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1913 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1914 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1915 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1916 | return x86::paddsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1917 | #else |
| 1918 | return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 1919 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1920 | } |
| 1921 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1922 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1923 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1924 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1925 | return x86::psubsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1926 | #else |
| 1927 | return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 1928 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1929 | } |
| 1930 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1931 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1932 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1933 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1934 | return x86::pmulhw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1935 | #else |
| 1936 | return As<Short4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 1937 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1938 | } |
| 1939 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1940 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1941 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1942 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1943 | return x86::pmaddwd(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1944 | #else |
| 1945 | return As<Int2>(V(lowerMulAdd(V(x.value), V(y.value)))); |
| 1946 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1947 | } |
| 1948 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1949 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1950 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1951 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1952 | auto result = x86::packsswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1953 | #else |
| 1954 | auto result = V(lowerPack(V(x.value), V(y.value), true)); |
| 1955 | #endif |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1956 | return As<SByte8>(Swizzle(As<Int4>(result), 0x88)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1957 | } |
| 1958 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1959 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 1960 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1961 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1962 | auto result = x86::packuswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1963 | #else |
| 1964 | auto result = V(lowerPack(V(x.value), V(y.value), false)); |
| 1965 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1966 | return As<Byte8>(Swizzle(As<Int4>(result), 0x88)); |
| 1967 | } |
| 1968 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1969 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1970 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1971 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1972 | return x86::pcmpgtw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1973 | #else |
| 1974 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); |
| 1975 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1976 | } |
| 1977 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1978 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1979 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1980 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1981 | return x86::pcmpeqw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 1982 | #else |
| 1983 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); |
| 1984 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1985 | } |
| 1986 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1987 | Type *Short4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1988 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1989 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1990 | } |
| 1991 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1992 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1993 | { |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1994 | if(saturate) |
| 1995 | { |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 1996 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1997 | if(CPUID::supportsSSE4_1()) |
| 1998 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1999 | Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2000 | *this = As<Short4>(PackUnsigned(int4, int4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2001 | } |
| 2002 | else |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 2003 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2004 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2005 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2006 | } |
| 2007 | } |
| 2008 | else |
| 2009 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2010 | *this = Short4(Int4(cast)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2011 | } |
| 2012 | } |
| 2013 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2014 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2015 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2016 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2017 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2018 | |
| 2019 | return As<UShort4>(x86::psllw(As<Short4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2020 | #else |
| 2021 | return As<UShort4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2022 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2023 | } |
| 2024 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2025 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2026 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2027 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2028 | // return RValue<Short4>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 2029 | |
| 2030 | return x86::psrlw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2031 | #else |
| 2032 | return As<UShort4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2033 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2034 | } |
| 2035 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2036 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2037 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2038 | return RValue<UShort4>(Max(As<Short4>(x) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u), As<Short4>(y) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)) + Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2039 | } |
| 2040 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2041 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2042 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2043 | return RValue<UShort4>(Min(As<Short4>(x) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u), As<Short4>(y) - Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)) + Short4(0x8000u, 0x8000u, 0x8000u, 0x8000u)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2044 | } |
| 2045 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2046 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2047 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2048 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2049 | return x86::paddusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2050 | #else |
| 2051 | return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 2052 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2053 | } |
| 2054 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2055 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2056 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2057 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2058 | return x86::psubusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2059 | #else |
| 2060 | return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 2061 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2062 | } |
| 2063 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2064 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2065 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2066 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2067 | return x86::pmulhuw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2068 | #else |
| 2069 | return As<UShort4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2070 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2071 | } |
| 2072 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2073 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2074 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2075 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2076 | return x86::pavgw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2077 | #else |
| 2078 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
| 2079 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2080 | } |
| 2081 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2082 | Type *UShort4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2083 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2084 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2085 | } |
| 2086 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2087 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2088 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2089 | #if defined(__i386__) || defined(__x86_64__) |
| 2090 | return x86::psllw(lhs, rhs); |
| 2091 | #else |
| 2092 | return As<Short8>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2093 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2094 | } |
| 2095 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2096 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2097 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2098 | #if defined(__i386__) || defined(__x86_64__) |
| 2099 | return x86::psraw(lhs, rhs); |
| 2100 | #else |
| 2101 | return As<Short8>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2102 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2103 | } |
| 2104 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2105 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2106 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2107 | #if defined(__i386__) || defined(__x86_64__) |
| 2108 | return x86::pmaddwd(x, y); |
| 2109 | #else |
| 2110 | return As<Int4>(V(lowerMulAdd(V(x.value), V(y.value)))); |
| 2111 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2112 | } |
| 2113 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2114 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2115 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2116 | #if defined(__i386__) || defined(__x86_64__) |
| 2117 | return x86::pmulhw(x, y); |
| 2118 | #else |
| 2119 | return As<Short8>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2120 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2121 | } |
| 2122 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2123 | Type *Short8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2124 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2125 | return T(llvm::VectorType::get(T(Short::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2126 | } |
| 2127 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2128 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2129 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2130 | #if defined(__i386__) || defined(__x86_64__) |
| 2131 | return As<UShort8>(x86::psllw(As<Short8>(lhs), rhs)); |
| 2132 | #else |
| 2133 | return As<UShort8>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2134 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2135 | } |
| 2136 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2137 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2138 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2139 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2140 | return x86::psrlw(lhs, rhs); // FIXME: Fallback required |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2141 | #else |
| 2142 | return As<UShort8>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2143 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2144 | } |
| 2145 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2146 | RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2147 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2148 | int pshufb[16] = |
| 2149 | { |
| 2150 | select0 + 0, |
| 2151 | select0 + 1, |
| 2152 | select1 + 0, |
| 2153 | select1 + 1, |
| 2154 | select2 + 0, |
| 2155 | select2 + 1, |
| 2156 | select3 + 0, |
| 2157 | select3 + 1, |
| 2158 | select4 + 0, |
| 2159 | select4 + 1, |
| 2160 | select5 + 0, |
| 2161 | select5 + 1, |
| 2162 | select6 + 0, |
| 2163 | select6 + 1, |
| 2164 | select7 + 0, |
| 2165 | select7 + 1, |
| 2166 | }; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2167 | |
| 2168 | Value *byte16 = Nucleus::createBitCast(x.value, Byte16::getType()); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2169 | Value *shuffle = Nucleus::createShuffleVector(byte16, byte16, pshufb); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2170 | Value *short8 = Nucleus::createBitCast(shuffle, UShort8::getType()); |
| 2171 | |
| 2172 | return RValue<UShort8>(short8); |
| 2173 | } |
| 2174 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2175 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2176 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2177 | #if defined(__i386__) || defined(__x86_64__) |
| 2178 | return x86::pmulhuw(x, y); |
| 2179 | #else |
| 2180 | return As<UShort8>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2181 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2182 | } |
| 2183 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2184 | Type *UShort8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2185 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2186 | return T(llvm::VectorType::get(T(UShort::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2187 | } |
| 2188 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2189 | RValue<Int> operator++(Int &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2190 | { |
| 2191 | RValue<Int> res = val; |
| 2192 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2193 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2194 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2195 | |
| 2196 | return res; |
| 2197 | } |
| 2198 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2199 | const Int &operator++(Int &val) // Pre-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2200 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2201 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2202 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2203 | |
| 2204 | return val; |
| 2205 | } |
| 2206 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2207 | RValue<Int> operator--(Int &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2208 | { |
| 2209 | RValue<Int> res = val; |
| 2210 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2211 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2212 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2213 | |
| 2214 | return res; |
| 2215 | } |
| 2216 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2217 | const Int &operator--(Int &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2218 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2219 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2220 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2221 | |
| 2222 | return val; |
| 2223 | } |
| 2224 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2225 | RValue<Int> RoundInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2226 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2227 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2228 | return x86::cvtss2si(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2229 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 2230 | return RValue<Int>(V(lowerRoundInt(V(cast.value), T(Int::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2231 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2232 | } |
| 2233 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2234 | Type *Int::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2235 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2236 | return T(llvm::Type::getInt32Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2237 | } |
| 2238 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2239 | Type *Long::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2240 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2241 | return T(llvm::Type::getInt64Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2242 | } |
| 2243 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2244 | UInt::UInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2245 | { |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2246 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 2247 | // Value *integer = Nucleus::createFPToUI(cast.value, UInt::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2248 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2249 | // Smallest positive value representable in UInt, but not in Int |
| 2250 | const unsigned int ustart = 0x80000000u; |
| 2251 | const float ustartf = float(ustart); |
| 2252 | |
| 2253 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2254 | storeValue((~(As<Int>(cast) >> 31) & |
| 2255 | // Check if the value can be represented as an Int |
| 2256 | IfThenElse(cast >= ustartf, |
| 2257 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2258 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 2259 | // Otherwise, just convert normally |
| 2260 | Int(cast))).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2261 | } |
| 2262 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2263 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2264 | { |
| 2265 | RValue<UInt> res = val; |
| 2266 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2267 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2268 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2269 | |
| 2270 | return res; |
| 2271 | } |
| 2272 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2273 | const UInt &operator++(UInt &val) // Pre-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2274 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2275 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2276 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2277 | |
| 2278 | return val; |
| 2279 | } |
| 2280 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2281 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2282 | { |
| 2283 | RValue<UInt> res = val; |
| 2284 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2285 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2286 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2287 | |
| 2288 | return res; |
| 2289 | } |
| 2290 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2291 | const UInt &operator--(UInt &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2292 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2293 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2294 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2295 | |
| 2296 | return val; |
| 2297 | } |
| 2298 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2299 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2300 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2301 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2302 | // return x86::cvtss2si(val); // FIXME: Unsigned |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2303 | //#else |
| 2304 | // return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f)); |
| 2305 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2306 | // } |
| 2307 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2308 | Type *UInt::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2309 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2310 | return T(llvm::Type::getInt32Ty(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2311 | } |
| 2312 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2313 | // Int2::Int2(RValue<Int> cast) |
| 2314 | // { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2315 | // Value *extend = Nucleus::createZExt(cast.value, Long::getType()); |
| 2316 | // Value *vector = Nucleus::createBitCast(extend, Int2::getType()); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2317 | // |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2318 | // int shuffle[2] = {0, 0}; |
| 2319 | // Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2320 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2321 | // storeValue(replicate); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2322 | // } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2323 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2324 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2325 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2326 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2327 | // return RValue<Int2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2328 | |
| 2329 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2330 | #else |
| 2331 | return As<Int2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2332 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2333 | } |
| 2334 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2335 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2336 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2337 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2338 | // return RValue<Int2>(Nucleus::createAShr(lhs.value, rhs.value)); |
| 2339 | |
| 2340 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2341 | #else |
| 2342 | return As<Int2>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2343 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2344 | } |
| 2345 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2346 | Type *Int2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2347 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2348 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2349 | } |
| 2350 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2351 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2352 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2353 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2354 | // return RValue<UInt2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2355 | |
| 2356 | return As<UInt2>(x86::pslld(As<Int2>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2357 | #else |
| 2358 | return As<UInt2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2359 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2360 | } |
| 2361 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2362 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2363 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2364 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2365 | // return RValue<UInt2>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 2366 | |
| 2367 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2368 | #else |
| 2369 | return As<UInt2>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2370 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2371 | } |
| 2372 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2373 | Type *UInt2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2374 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2375 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2376 | } |
| 2377 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2378 | Int4::Int4(RValue<Byte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2379 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2380 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2381 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2382 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2383 | *this = x86::pmovzxbd(As<Byte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2384 | } |
| 2385 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2386 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2387 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2388 | int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2389 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2390 | Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::getType()), swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2391 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2392 | int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2393 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2394 | Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::getType()), swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2395 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2396 | *this = As<Int4>(d); |
| 2397 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2398 | } |
| 2399 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2400 | Int4::Int4(RValue<SByte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2401 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2402 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2403 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2404 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2405 | *this = x86::pmovsxbd(As<SByte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2406 | } |
| 2407 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2408 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2409 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2410 | int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; |
| 2411 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
| 2412 | Value *b = Nucleus::createShuffleVector(a, a, swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2413 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2414 | int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2415 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
| 2416 | Value *d = Nucleus::createShuffleVector(c, c, swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2417 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2418 | *this = As<Int4>(d) >> 24; |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2419 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 2420 | } |
| 2421 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2422 | Int4::Int4(RValue<Short4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2423 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2424 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2425 | if(CPUID::supportsSSE4_1()) |
| 2426 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2427 | *this = x86::pmovsxwd(As<Short8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2428 | } |
| 2429 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2430 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2431 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2432 | int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2433 | Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle); |
| 2434 | *this = As<Int4>(c) >> 16; |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2435 | } |
| 2436 | } |
| 2437 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2438 | Int4::Int4(RValue<UShort4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2439 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2440 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2441 | if(CPUID::supportsSSE4_1()) |
| 2442 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2443 | *this = x86::pmovzxwd(As<UShort8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2444 | } |
| 2445 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2446 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2447 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2448 | int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2449 | Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
| 2450 | *this = As<Int4>(c); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 2451 | } |
| 2452 | } |
| 2453 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2454 | Int4::Int4(RValue<Int> rhs) : XYZW(this) |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 2455 | { |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 2456 | Value *vector = loadValue(); |
| 2457 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 2458 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2459 | int swizzle[4] = {0, 0, 0, 0}; |
| 2460 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 2461 | |
| 2462 | storeValue(replicate); |
| 2463 | } |
| 2464 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2465 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2466 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2467 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2468 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2469 | #else |
| 2470 | return As<Int4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2471 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2472 | } |
| 2473 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2474 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2475 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2476 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2477 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2478 | #else |
| 2479 | return As<Int4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2480 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2481 | } |
| 2482 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2483 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 2484 | { |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 2485 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
Alexis Hetu | fb60399 | 2016-04-26 11:50:40 -0400 | [diff] [blame] | 2486 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2487 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
| 2488 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 2492 | { |
Nicolas Capens | 9ae6cfd | 2017-11-27 14:58:53 -0500 | [diff] [blame] | 2493 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2494 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2495 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType())); |
| 2496 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 2500 | { |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 2501 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2502 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2503 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType())); |
| 2504 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 2508 | { |
Nicolas Capens | 9ae6cfd | 2017-11-27 14:58:53 -0500 | [diff] [blame] | 2509 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2510 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2511 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
| 2512 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 2516 | { |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 2517 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2518 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2519 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType())); |
| 2520 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 2524 | { |
Nicolas Capens | 9ae6cfd | 2017-11-27 14:58:53 -0500 | [diff] [blame] | 2525 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2526 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2527 | // return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType())); |
| 2528 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType())) ^ Int4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 2532 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2533 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2534 | if(CPUID::supportsSSE4_1()) |
| 2535 | { |
| 2536 | return x86::pmaxsd(x, y); |
| 2537 | } |
| 2538 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2539 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2540 | { |
| 2541 | RValue<Int4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 2542 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 2547 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2548 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2549 | if(CPUID::supportsSSE4_1()) |
| 2550 | { |
| 2551 | return x86::pminsd(x, y); |
| 2552 | } |
| 2553 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2554 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2555 | { |
| 2556 | RValue<Int4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 2557 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | RValue<Int4> RoundInt(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2562 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2563 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2564 | return x86::cvtps2dq(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2565 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 2566 | return As<Int4>(V(lowerRoundInt(V(cast.value), T(Int4::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2567 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2568 | } |
| 2569 | |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 2570 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 2571 | { |
| 2572 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2573 | return As<Int4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2574 | } |
| 2575 | |
| 2576 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 2577 | { |
| 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 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2582 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2583 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2584 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2585 | return x86::packssdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2586 | #else |
| 2587 | return As<Short8>(V(lowerPack(V(x.value), V(y.value), true))); |
| 2588 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2589 | } |
| 2590 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2591 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 2592 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2593 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2594 | return x86::packusdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2595 | #else |
| 2596 | return As<UShort8>(V(lowerPack(V(x.value), V(y.value), false))); |
| 2597 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2598 | } |
| 2599 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2600 | RValue<Int> SignMask(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2601 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2602 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2603 | return x86::movmskps(As<Float4>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2604 | #else |
| 2605 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2606 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2607 | } |
| 2608 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2609 | Type *Int4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2610 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2611 | return T(llvm::VectorType::get(T(Int::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2612 | } |
| 2613 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2614 | UInt4::UInt4(RValue<Float4> cast) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2615 | { |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2616 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 2617 | // Value *xyzw = Nucleus::createFPToUI(cast.value, UInt4::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2618 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2619 | // Smallest positive value representable in UInt, but not in Int |
| 2620 | const unsigned int ustart = 0x80000000u; |
| 2621 | const float ustartf = float(ustart); |
| 2622 | |
| 2623 | // Check if the value can be represented as an Int |
| 2624 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 2625 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2626 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
| 2627 | // Otherwise, just convert normally |
| 2628 | (~uiValue & Int4(cast)); |
| 2629 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2630 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2631 | } |
| 2632 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2633 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2634 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2635 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2636 | return As<UInt4>(x86::pslld(As<Int4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2637 | #else |
| 2638 | return As<UInt4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2639 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2640 | } |
| 2641 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2642 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2643 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2644 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2645 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2646 | #else |
| 2647 | return As<UInt4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2648 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2649 | } |
| 2650 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2651 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 2652 | { |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 2653 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
Alexis Hetu | fb60399 | 2016-04-26 11:50:40 -0400 | [diff] [blame] | 2654 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2655 | // return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
| 2656 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())) ^ UInt4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 2660 | { |
| 2661 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType())); |
| 2662 | } |
| 2663 | |
| 2664 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 2665 | { |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 2666 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2667 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2668 | // return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULE(x.value, y.value), Int4::getType())); |
| 2669 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType())) ^ UInt4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 2673 | { |
| 2674 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
| 2675 | } |
| 2676 | |
| 2677 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 2678 | { |
Nicolas Capens | 197226a | 2016-04-27 23:08:50 -0400 | [diff] [blame] | 2679 | // FIXME: An LLVM bug causes SExt(ICmpCC()) to produce 0 or 1 instead of 0 or ~0 |
| 2680 | // Restore the following line when LLVM is updated to a version where this issue is fixed. |
| 2681 | // return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGE(x.value, y.value), Int4::getType())); |
| 2682 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType())) ^ UInt4(0xFFFFFFFF); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 2686 | { |
| 2687 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType())); |
| 2688 | } |
| 2689 | |
| 2690 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 2691 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2692 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2693 | if(CPUID::supportsSSE4_1()) |
| 2694 | { |
| 2695 | return x86::pmaxud(x, y); |
| 2696 | } |
| 2697 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2698 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2699 | { |
| 2700 | RValue<UInt4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 2701 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 2706 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2707 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2708 | if(CPUID::supportsSSE4_1()) |
| 2709 | { |
| 2710 | return x86::pminud(x, y); |
| 2711 | } |
| 2712 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2713 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2714 | { |
| 2715 | RValue<UInt4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 2716 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2717 | } |
| 2718 | } |
| 2719 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2720 | Type *UInt4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2721 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2722 | return T(llvm::VectorType::get(T(UInt::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2723 | } |
| 2724 | |
Alexis Hetu | 734e257 | 2018-12-20 14:00:49 -0500 | [diff] [blame] | 2725 | Type *Half::getType() |
| 2726 | { |
| 2727 | return T(llvm::Type::getInt16Ty(*::context)); |
| 2728 | } |
| 2729 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 2730 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2731 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2732 | #if defined(__i386__) || defined(__x86_64__) |
| 2733 | if(exactAtPow2) |
| 2734 | { |
| 2735 | // rcpss uses a piecewise-linear approximation which minimizes the relative error |
| 2736 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 2737 | return x86::rcpss(x) * Float(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 2738 | } |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 2739 | return x86::rcpss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2740 | #else |
| 2741 | return As<Float>(V(lowerRCP(V(x.value)))); |
| 2742 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2743 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2744 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2745 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2746 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2747 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2748 | return x86::rsqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2749 | #else |
| 2750 | return As<Float>(V(lowerRSQRT(V(x.value)))); |
| 2751 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2752 | } |
| 2753 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2754 | RValue<Float> Sqrt(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2755 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2756 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2757 | return x86::sqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2758 | #else |
| 2759 | return As<Float>(V(lowerSQRT(V(x.value)))); |
| 2760 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2761 | } |
| 2762 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2763 | RValue<Float> Round(RValue<Float> x) |
| 2764 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2765 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2766 | if(CPUID::supportsSSE4_1()) |
| 2767 | { |
| 2768 | return x86::roundss(x, 0); |
| 2769 | } |
| 2770 | else |
| 2771 | { |
| 2772 | return Float4(Round(Float4(x))).x; |
| 2773 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 2774 | #else |
| 2775 | return RValue<Float>(V(lowerRound(V(x.value)))); |
| 2776 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | RValue<Float> Trunc(RValue<Float> x) |
| 2780 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2781 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2782 | if(CPUID::supportsSSE4_1()) |
| 2783 | { |
| 2784 | return x86::roundss(x, 3); |
| 2785 | } |
| 2786 | else |
| 2787 | { |
| 2788 | return Float(Int(x)); // Rounded toward zero |
| 2789 | } |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 2790 | #else |
| 2791 | return RValue<Float>(V(lowerTrunc(V(x.value)))); |
| 2792 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | RValue<Float> Frac(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2796 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2797 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2798 | if(CPUID::supportsSSE4_1()) |
| 2799 | { |
| 2800 | return x - x86::floorss(x); |
| 2801 | } |
| 2802 | else |
| 2803 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2804 | return Float4(Frac(Float4(x))).x; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2805 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 2806 | #else |
| 2807 | // x - floor(x) can be 1.0 for very small negative x. |
| 2808 | // Clamp against the value just below 1.0. |
| 2809 | return Min(x - Floor(x), As<Float>(Int(0x3F7FFFFF))); |
| 2810 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2811 | } |
| 2812 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2813 | RValue<Float> Floor(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2814 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2815 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2816 | if(CPUID::supportsSSE4_1()) |
| 2817 | { |
| 2818 | return x86::floorss(x); |
| 2819 | } |
| 2820 | else |
| 2821 | { |
| 2822 | return Float4(Floor(Float4(x))).x; |
| 2823 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 2824 | #else |
| 2825 | return RValue<Float>(V(lowerFloor(V(x.value)))); |
| 2826 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2827 | } |
| 2828 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2829 | RValue<Float> Ceil(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2830 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2831 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2832 | if(CPUID::supportsSSE4_1()) |
| 2833 | { |
| 2834 | return x86::ceilss(x); |
| 2835 | } |
| 2836 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2837 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2838 | { |
| 2839 | return Float4(Ceil(Float4(x))).x; |
| 2840 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2841 | } |
| 2842 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2843 | Type *Float::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2844 | { |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2845 | return T(llvm::Type::getFloatTy(*::context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2846 | } |
| 2847 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2848 | Type *Float2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2849 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2850 | return T(Type_v2f32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2851 | } |
| 2852 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2853 | Float4::Float4(RValue<Float> rhs) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2854 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2855 | Value *vector = loadValue(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2856 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 2857 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2858 | int swizzle[4] = {0, 0, 0, 0}; |
| 2859 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2860 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2861 | storeValue(replicate); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2862 | } |
| 2863 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2864 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2865 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2866 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2867 | return x86::maxps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2868 | #else |
| 2869 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OGT))); |
| 2870 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2871 | } |
| 2872 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2873 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2874 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2875 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2876 | return x86::minps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2877 | #else |
| 2878 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OLT))); |
| 2879 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2880 | } |
| 2881 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 2882 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2883 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2884 | #if defined(__i386__) || defined(__x86_64__) |
| 2885 | if(exactAtPow2) |
| 2886 | { |
| 2887 | // rcpps uses a piecewise-linear approximation which minimizes the relative error |
| 2888 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 2889 | return x86::rcpps(x) * Float4(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 2890 | } |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 2891 | return x86::rcpps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2892 | #else |
| 2893 | return As<Float4>(V(lowerRCP(V(x.value)))); |
| 2894 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2895 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2896 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2897 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2898 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2899 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2900 | return x86::rsqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2901 | #else |
| 2902 | return As<Float4>(V(lowerRSQRT(V(x.value)))); |
| 2903 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2904 | } |
| 2905 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2906 | RValue<Float4> Sqrt(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2907 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2908 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2909 | return x86::sqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2910 | #else |
| 2911 | return As<Float4>(V(lowerSQRT(V(x.value)))); |
| 2912 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2913 | } |
| 2914 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2915 | RValue<Int> SignMask(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2916 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2917 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2918 | return x86::movmskps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2919 | #else |
| 2920 | return As<Int>(V(lowerFPSignMask(V(x.value), T(Int::getType())))); |
| 2921 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2922 | } |
| 2923 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2924 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2925 | { |
| 2926 | // return As<Int4>(x86::cmpeqps(x, y)); |
| 2927 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value, y.value), Int4::getType())); |
| 2928 | } |
| 2929 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2930 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2931 | { |
| 2932 | // return As<Int4>(x86::cmpltps(x, y)); |
| 2933 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value, y.value), Int4::getType())); |
| 2934 | } |
| 2935 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2936 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2937 | { |
| 2938 | // return As<Int4>(x86::cmpleps(x, y)); |
| 2939 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value, y.value), Int4::getType())); |
| 2940 | } |
| 2941 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2942 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2943 | { |
| 2944 | // return As<Int4>(x86::cmpneqps(x, y)); |
| 2945 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value, y.value), Int4::getType())); |
| 2946 | } |
| 2947 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2948 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2949 | { |
| 2950 | // return As<Int4>(x86::cmpnltps(x, y)); |
| 2951 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value, y.value), Int4::getType())); |
| 2952 | } |
| 2953 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2954 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2955 | { |
| 2956 | // return As<Int4>(x86::cmpnleps(x, y)); |
| 2957 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value, y.value), Int4::getType())); |
| 2958 | } |
| 2959 | |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 2960 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 2961 | { |
| 2962 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUEQ(x.value, y.value), Int4::getType())); |
| 2963 | } |
| 2964 | |
| 2965 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 2966 | { |
| 2967 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULT(x.value, y.value), Int4::getType())); |
| 2968 | } |
| 2969 | |
| 2970 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 2971 | { |
| 2972 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULE(x.value, y.value), Int4::getType())); |
| 2973 | } |
| 2974 | |
| 2975 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 2976 | { |
| 2977 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUNE(x.value, y.value), Int4::getType())); |
| 2978 | } |
| 2979 | |
| 2980 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 2981 | { |
| 2982 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGE(x.value, y.value), Int4::getType())); |
| 2983 | } |
| 2984 | |
| 2985 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 2986 | { |
| 2987 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value, y.value), Int4::getType())); |
| 2988 | } |
| 2989 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2990 | RValue<Float4> Round(RValue<Float4> x) |
| 2991 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2992 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2993 | if(CPUID::supportsSSE4_1()) |
| 2994 | { |
| 2995 | return x86::roundps(x, 0); |
| 2996 | } |
| 2997 | else |
| 2998 | { |
| 2999 | return Float4(RoundInt(x)); |
| 3000 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 3001 | #else |
| 3002 | return RValue<Float4>(V(lowerRound(V(x.value)))); |
| 3003 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | RValue<Float4> Trunc(RValue<Float4> x) |
| 3007 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3008 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3009 | if(CPUID::supportsSSE4_1()) |
| 3010 | { |
| 3011 | return x86::roundps(x, 3); |
| 3012 | } |
| 3013 | else |
| 3014 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3015 | return Float4(Int4(x)); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3016 | } |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 3017 | #else |
| 3018 | return RValue<Float4>(V(lowerTrunc(V(x.value)))); |
| 3019 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | RValue<Float4> Frac(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3023 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3024 | Float4 frc; |
| 3025 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3026 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3027 | if(CPUID::supportsSSE4_1()) |
| 3028 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3029 | frc = x - Floor(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3030 | } |
| 3031 | else |
| 3032 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3033 | frc = x - Float4(Int4(x)); // Signed fractional part. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3034 | |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3035 | frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1.0f))); // Add 1.0 if negative. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3036 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 3037 | #else |
| 3038 | frc = x - Floor(x); |
| 3039 | #endif |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3040 | |
| 3041 | // x - floor(x) can be 1.0 for very small negative x. |
| 3042 | // Clamp against the value just below 1.0. |
| 3043 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3044 | } |
| 3045 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3046 | RValue<Float4> Floor(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3047 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3048 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3049 | if(CPUID::supportsSSE4_1()) |
| 3050 | { |
| 3051 | return x86::floorps(x); |
| 3052 | } |
| 3053 | else |
| 3054 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3055 | return x - Frac(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3056 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3057 | #else |
| 3058 | return RValue<Float4>(V(lowerFloor(V(x.value)))); |
| 3059 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3060 | } |
| 3061 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3062 | RValue<Float4> Ceil(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3063 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3064 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3065 | if(CPUID::supportsSSE4_1()) |
| 3066 | { |
| 3067 | return x86::ceilps(x); |
| 3068 | } |
| 3069 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3070 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3071 | { |
| 3072 | return -Floor(-x); |
| 3073 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3074 | } |
| 3075 | |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 3076 | RValue<Float4> Sin(RValue<Float4> v) |
| 3077 | { |
| 3078 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sin, { V(v.value)->getType() } ); |
| 3079 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
| 3080 | } |
| 3081 | |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 3082 | RValue<Float4> Cos(RValue<Float4> v) |
| 3083 | { |
| 3084 | auto func = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::cos, { V(v.value)->getType() } ); |
| 3085 | return RValue<Float4>(V(::builder->CreateCall(func, V(v.value)))); |
| 3086 | } |
| 3087 | |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 3088 | RValue<Float4> Tan(RValue<Float4> v) |
| 3089 | { |
| 3090 | return Sin(v) / Cos(v); |
| 3091 | } |
| 3092 | |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame^] | 3093 | RValue<Float4> Asin(RValue<Float4> v) |
| 3094 | { |
| 3095 | auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), {T(Float::getType())}, false); |
| 3096 | auto func = ::module->getOrInsertFunction("asinf", funcTy); |
| 3097 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
| 3098 | for (uint64_t i = 0; i < 4; i++) |
| 3099 | { |
| 3100 | auto el = ::builder->CreateCall(func, ::builder->CreateExtractElement(V(v.value), i)); |
| 3101 | out = ::builder->CreateInsertElement(out, el, i); |
| 3102 | } |
| 3103 | return RValue<Float4>(V(out)); |
| 3104 | } |
| 3105 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3106 | Type *Float4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3107 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3108 | return T(llvm::VectorType::get(T(Float::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3109 | } |
| 3110 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3111 | RValue<Long> Ticks() |
| 3112 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3113 | llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::readcyclecounter); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3114 | |
Nicolas Capens | 2ab69ee | 2016-09-26 11:45:17 -0400 | [diff] [blame] | 3115 | return RValue<Long>(V(::builder->CreateCall(rdtsc))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3116 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3117 | } |
| 3118 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 3119 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3120 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3121 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3122 | namespace x86 |
| 3123 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3124 | RValue<Int> cvtss2si(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3125 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3126 | llvm::Function *cvtss2si = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_cvtss2si); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3127 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3128 | Float4 vector; |
| 3129 | vector.x = val; |
| 3130 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3131 | return RValue<Int>(V(::builder->CreateCall(cvtss2si, ARGS(V(RValue<Float4>(vector).value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3132 | } |
| 3133 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3134 | RValue<Int4> cvtps2dq(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3135 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3136 | llvm::Function *cvtps2dq = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_cvtps2dq); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3137 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3138 | return RValue<Int4>(V(::builder->CreateCall(cvtps2dq, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3139 | } |
| 3140 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3141 | RValue<Float> rcpss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3142 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3143 | llvm::Function *rcpss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rcp_ss); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3144 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3145 | Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3146 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3147 | return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall(rcpss, ARGS(V(vector)))), Float::getType(), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3148 | } |
| 3149 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3150 | RValue<Float> sqrtss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3151 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3152 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3153 | llvm::Function *sqrtss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_sqrt_ss); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3154 | Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3155 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3156 | return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall(sqrtss, ARGS(V(vector)))), Float::getType(), 0)); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3157 | #else |
| 3158 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sqrt, {V(val.value)->getType()}); |
| 3159 | return RValue<Float>(V(::builder->CreateCall(sqrt, ARGS(V(val.value))))); |
| 3160 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3161 | } |
| 3162 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3163 | RValue<Float> rsqrtss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3164 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3165 | llvm::Function *rsqrtss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rsqrt_ss); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3166 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3167 | Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3168 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3169 | return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall(rsqrtss, ARGS(V(vector)))), Float::getType(), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3170 | } |
| 3171 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3172 | RValue<Float4> rcpps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3173 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3174 | llvm::Function *rcpps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rcp_ps); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3175 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3176 | return RValue<Float4>(V(::builder->CreateCall(rcpps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3177 | } |
| 3178 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3179 | RValue<Float4> sqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3180 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3181 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3182 | llvm::Function *sqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_sqrt_ps); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3183 | #else |
| 3184 | llvm::Function *sqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::sqrt, {V(val.value)->getType()}); |
| 3185 | #endif |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3186 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3187 | return RValue<Float4>(V(::builder->CreateCall(sqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3188 | } |
| 3189 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3190 | RValue<Float4> rsqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3191 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3192 | llvm::Function *rsqrtps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_rsqrt_ps); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3193 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3194 | return RValue<Float4>(V(::builder->CreateCall(rsqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3195 | } |
| 3196 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3197 | RValue<Float4> maxps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3198 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3199 | llvm::Function *maxps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_max_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3200 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3201 | return RValue<Float4>(V(::builder->CreateCall2(maxps, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3202 | } |
| 3203 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3204 | RValue<Float4> minps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3205 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3206 | llvm::Function *minps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_min_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3207 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3208 | return RValue<Float4>(V(::builder->CreateCall2(minps, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3209 | } |
| 3210 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3211 | RValue<Float> roundss(RValue<Float> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3212 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3213 | llvm::Function *roundss = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_round_ss); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3214 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3215 | Value *undef = V(llvm::UndefValue::get(T(Float4::getType()))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3216 | Value *vector = Nucleus::createInsertElement(undef, val.value, 0); |
| 3217 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3218 | return RValue<Float>(Nucleus::createExtractElement(V(::builder->CreateCall3(roundss, ARGS(V(undef), V(vector), V(Nucleus::createConstantInt(imm))))), Float::getType(), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3219 | } |
| 3220 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3221 | RValue<Float> floorss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3222 | { |
| 3223 | return roundss(val, 1); |
| 3224 | } |
| 3225 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3226 | RValue<Float> ceilss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3227 | { |
| 3228 | return roundss(val, 2); |
| 3229 | } |
| 3230 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3231 | RValue<Float4> roundps(RValue<Float4> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3232 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3233 | llvm::Function *roundps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_round_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3234 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3235 | return RValue<Float4>(V(::builder->CreateCall2(roundps, ARGS(V(val.value), V(Nucleus::createConstantInt(imm)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3236 | } |
| 3237 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3238 | RValue<Float4> floorps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3239 | { |
| 3240 | return roundps(val, 1); |
| 3241 | } |
| 3242 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3243 | RValue<Float4> ceilps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3244 | { |
| 3245 | return roundps(val, 2); |
| 3246 | } |
| 3247 | |
Alexis Hetu | 0f44807 | 2016-03-18 10:56:08 -0400 | [diff] [blame] | 3248 | RValue<Int4> pabsd(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3249 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3250 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3251 | llvm::Function *pabsd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_ssse3_pabs_d_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3252 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3253 | return RValue<Int4>(V(::builder->CreateCall(pabsd, ARGS(V(x.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3254 | #else |
| 3255 | return RValue<Int4>(V(lowerPABS(V(x.value)))); |
| 3256 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3257 | } |
| 3258 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3259 | RValue<Short4> paddsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3260 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3261 | llvm::Function *paddsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_padds_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3262 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3263 | return As<Short4>(V(::builder->CreateCall2(paddsw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3264 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3265 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3266 | RValue<Short4> psubsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3267 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3268 | llvm::Function *psubsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubs_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3269 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3270 | return As<Short4>(V(::builder->CreateCall2(psubsw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3271 | } |
| 3272 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3273 | RValue<UShort4> paddusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3274 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3275 | llvm::Function *paddusw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_paddus_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3276 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3277 | return As<UShort4>(V(::builder->CreateCall2(paddusw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3278 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3279 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3280 | RValue<UShort4> psubusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3281 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3282 | llvm::Function *psubusw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubus_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3283 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3284 | return As<UShort4>(V(::builder->CreateCall2(psubusw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3285 | } |
| 3286 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3287 | RValue<SByte8> paddsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3288 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3289 | llvm::Function *paddsb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_padds_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3290 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3291 | return As<SByte8>(V(::builder->CreateCall2(paddsb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3292 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3293 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3294 | RValue<SByte8> psubsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3295 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3296 | llvm::Function *psubsb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubs_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3297 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3298 | return As<SByte8>(V(::builder->CreateCall2(psubsb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3299 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3300 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3301 | RValue<Byte8> paddusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3302 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3303 | llvm::Function *paddusb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_paddus_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3304 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3305 | return As<Byte8>(V(::builder->CreateCall2(paddusb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3306 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3307 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3308 | RValue<Byte8> psubusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3309 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3310 | llvm::Function *psubusb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psubus_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3311 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3312 | return As<Byte8>(V(::builder->CreateCall2(psubusb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | RValue<UShort4> pavgw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3316 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3317 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3318 | llvm::Function *pavgw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pavg_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3319 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3320 | return As<UShort4>(V(::builder->CreateCall2(pavgw, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3321 | #else |
| 3322 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
| 3323 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3324 | } |
| 3325 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3326 | RValue<Short4> pmaxsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3327 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3328 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3329 | llvm::Function *pmaxsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmaxs_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3330 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3331 | return As<Short4>(V(::builder->CreateCall2(pmaxsw, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3332 | #else |
| 3333 | return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
| 3334 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3335 | } |
| 3336 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3337 | RValue<Short4> pminsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3338 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3339 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3340 | llvm::Function *pminsw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmins_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3341 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3342 | return As<Short4>(V(::builder->CreateCall2(pminsw, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3343 | #else |
| 3344 | return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
| 3345 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3346 | } |
| 3347 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3348 | RValue<Short4> pcmpgtw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3349 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3350 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3351 | llvm::Function *pcmpgtw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pcmpgt_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3352 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3353 | return As<Short4>(V(::builder->CreateCall2(pcmpgtw, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3354 | #else |
| 3355 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); |
| 3356 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3357 | } |
| 3358 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3359 | RValue<Short4> pcmpeqw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3360 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3361 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3362 | llvm::Function *pcmpeqw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pcmpeq_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3363 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3364 | return As<Short4>(V(::builder->CreateCall2(pcmpeqw, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3365 | #else |
| 3366 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); |
| 3367 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3368 | } |
| 3369 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3370 | RValue<Byte8> pcmpgtb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3371 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3372 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3373 | llvm::Function *pcmpgtb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pcmpgt_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3374 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3375 | return As<Byte8>(V(::builder->CreateCall2(pcmpgtb, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3376 | #else |
| 3377 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 3378 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3379 | } |
| 3380 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3381 | RValue<Byte8> pcmpeqb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3382 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3383 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3384 | llvm::Function *pcmpeqb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pcmpeq_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3385 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3386 | return As<Byte8>(V(::builder->CreateCall2(pcmpeqb, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3387 | #else |
| 3388 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 3389 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3390 | } |
| 3391 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3392 | RValue<Short4> packssdw(RValue<Int2> x, RValue<Int2> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3393 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3394 | llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packssdw_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3395 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3396 | return As<Short4>(V(::builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3397 | } |
| 3398 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3399 | RValue<Short8> packssdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3400 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3401 | llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packssdw_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3402 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3403 | return RValue<Short8>(V(::builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3404 | } |
| 3405 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3406 | RValue<SByte8> packsswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3407 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3408 | llvm::Function *packsswb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packsswb_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3409 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3410 | return As<SByte8>(V(::builder->CreateCall2(packsswb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3411 | } |
| 3412 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3413 | RValue<Byte8> packuswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3414 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3415 | llvm::Function *packuswb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_packuswb_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3416 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3417 | return As<Byte8>(V(::builder->CreateCall2(packuswb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3418 | } |
| 3419 | |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 3420 | RValue<UShort8> packusdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3421 | { |
| 3422 | if(CPUID::supportsSSE4_1()) |
| 3423 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3424 | llvm::Function *packusdw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_packusdw); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3425 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3426 | return RValue<UShort8>(V(::builder->CreateCall2(packusdw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3427 | } |
| 3428 | else |
| 3429 | { |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 3430 | RValue<Int4> bx = (x & ~(x >> 31)) - Int4(0x8000); |
| 3431 | RValue<Int4> by = (y & ~(y >> 31)) - Int4(0x8000); |
| 3432 | |
| 3433 | return As<UShort8>(packssdw(bx, by) + Short8(0x8000u)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3434 | } |
| 3435 | } |
| 3436 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3437 | RValue<UShort4> psrlw(RValue<UShort4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3438 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3439 | llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3440 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3441 | return As<UShort4>(V(::builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3442 | } |
| 3443 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3444 | RValue<UShort8> psrlw(RValue<UShort8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3445 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3446 | llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3447 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3448 | return RValue<UShort8>(V(::builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3449 | } |
| 3450 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3451 | RValue<Short4> psraw(RValue<Short4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3452 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3453 | llvm::Function *psraw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3454 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3455 | return As<Short4>(V(::builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3456 | } |
| 3457 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3458 | RValue<Short8> psraw(RValue<Short8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3459 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3460 | llvm::Function *psraw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3461 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3462 | return RValue<Short8>(V(::builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3463 | } |
| 3464 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3465 | RValue<Short4> psllw(RValue<Short4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3466 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3467 | llvm::Function *psllw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3468 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3469 | return As<Short4>(V(::builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3470 | } |
| 3471 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3472 | RValue<Short8> psllw(RValue<Short8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3473 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3474 | llvm::Function *psllw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3475 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3476 | return RValue<Short8>(V(::builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3477 | } |
| 3478 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3479 | RValue<Int2> pslld(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3480 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3481 | llvm::Function *pslld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3482 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3483 | return As<Int2>(V(::builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3484 | } |
| 3485 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3486 | RValue<Int4> pslld(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3487 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3488 | llvm::Function *pslld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pslli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3489 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3490 | return RValue<Int4>(V(::builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3491 | } |
| 3492 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3493 | RValue<Int2> psrad(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3494 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3495 | llvm::Function *psrad = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3496 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3497 | return As<Int2>(V(::builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3498 | } |
| 3499 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3500 | RValue<Int4> psrad(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3501 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3502 | llvm::Function *psrad = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrai_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3503 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3504 | return RValue<Int4>(V(::builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3505 | } |
| 3506 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3507 | RValue<UInt2> psrld(RValue<UInt2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3508 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3509 | llvm::Function *psrld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3510 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3511 | return As<UInt2>(V(::builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3512 | } |
| 3513 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3514 | RValue<UInt4> psrld(RValue<UInt4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3515 | { |
Nicolas Capens | 9e013d4 | 2017-07-28 17:26:14 -0400 | [diff] [blame] | 3516 | llvm::Function *psrld = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_psrli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3517 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3518 | return RValue<UInt4>(V(::builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3519 | } |
| 3520 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3521 | RValue<Int4> pmaxsd(RValue<Int4> x, RValue<Int4> y) |
| 3522 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3523 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3524 | llvm::Function *pmaxsd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmaxsd); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3525 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3526 | return RValue<Int4>(V(::builder->CreateCall2(pmaxsd, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3527 | #else |
| 3528 | return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
| 3529 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | RValue<Int4> pminsd(RValue<Int4> x, RValue<Int4> y) |
| 3533 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3534 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3535 | llvm::Function *pminsd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pminsd); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3536 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3537 | return RValue<Int4>(V(::builder->CreateCall2(pminsd, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3538 | #else |
| 3539 | return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
| 3540 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | RValue<UInt4> pmaxud(RValue<UInt4> x, RValue<UInt4> y) |
| 3544 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3545 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3546 | llvm::Function *pmaxud = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmaxud); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3547 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3548 | return RValue<UInt4>(V(::builder->CreateCall2(pmaxud, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3549 | #else |
| 3550 | return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_UGT))); |
| 3551 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | RValue<UInt4> pminud(RValue<UInt4> x, RValue<UInt4> y) |
| 3555 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3556 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3557 | llvm::Function *pminud = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pminud); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3558 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3559 | return RValue<UInt4>(V(::builder->CreateCall2(pminud, ARGS(V(x.value), V(y.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3560 | #else |
| 3561 | return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_ULT))); |
| 3562 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | RValue<Short4> pmulhw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3566 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3567 | llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulh_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3568 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3569 | return As<Short4>(V(::builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3570 | } |
| 3571 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3572 | RValue<UShort4> pmulhuw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3573 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3574 | llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulhu_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3575 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3576 | return As<UShort4>(V(::builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3577 | } |
| 3578 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3579 | RValue<Int2> pmaddwd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3580 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3581 | llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmadd_wd); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3582 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3583 | return As<Int2>(V(::builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3584 | } |
| 3585 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3586 | RValue<Short8> pmulhw(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3587 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3588 | llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulh_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3589 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3590 | return RValue<Short8>(V(::builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3591 | } |
| 3592 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3593 | RValue<UShort8> pmulhuw(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3594 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3595 | llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmulhu_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3596 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3597 | return RValue<UShort8>(V(::builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3598 | } |
| 3599 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3600 | RValue<Int4> pmaddwd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3601 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3602 | llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmadd_wd); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3603 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3604 | return RValue<Int4>(V(::builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3605 | } |
| 3606 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3607 | RValue<Int> movmskps(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3608 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3609 | llvm::Function *movmskps = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse_movmsk_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3610 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3611 | return RValue<Int>(V(::builder->CreateCall(movmskps, ARGS(V(x.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3612 | } |
| 3613 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3614 | RValue<Int> pmovmskb(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3615 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3616 | llvm::Function *pmovmskb = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse2_pmovmskb_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3617 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3618 | return RValue<Int>(V(::builder->CreateCall(pmovmskb, ARGS(V(x.value))))) & 0xFF; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3619 | } |
| 3620 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3621 | RValue<Int4> pmovzxbd(RValue<Byte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3622 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3623 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3624 | llvm::Function *pmovzxbd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmovzxbd); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3625 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3626 | return RValue<Int4>(V(::builder->CreateCall(pmovzxbd, ARGS(V(x.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3627 | #else |
| 3628 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
| 3629 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3630 | } |
| 3631 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3632 | RValue<Int4> pmovsxbd(RValue<SByte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3633 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3634 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3635 | llvm::Function *pmovsxbd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmovsxbd); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3636 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3637 | return RValue<Int4>(V(::builder->CreateCall(pmovsxbd, ARGS(V(x.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3638 | #else |
| 3639 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
| 3640 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3641 | } |
| 3642 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3643 | RValue<Int4> pmovzxwd(RValue<UShort8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3644 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3645 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3646 | llvm::Function *pmovzxwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmovzxwd); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3647 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3648 | return RValue<Int4>(V(::builder->CreateCall(pmovzxwd, ARGS(V(x.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3649 | #else |
| 3650 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
| 3651 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3652 | } |
| 3653 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3654 | RValue<Int4> pmovsxwd(RValue<Short8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3655 | { |
Nicolas Capens | f417d9d | 2018-10-10 10:49:30 -0400 | [diff] [blame] | 3656 | #if REACTOR_LLVM_VERSION < 7 |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3657 | llvm::Function *pmovsxwd = llvm::Intrinsic::getDeclaration(::module, llvm::Intrinsic::x86_sse41_pmovsxwd); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3658 | |
Logan Chien | 813d503 | 2018-08-31 17:19:45 +0800 | [diff] [blame] | 3659 | return RValue<Int4>(V(::builder->CreateCall(pmovsxwd, ARGS(V(x.value))))); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 3660 | #else |
| 3661 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
| 3662 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3663 | } |
| 3664 | } |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3665 | #endif // defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 3666 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 3667 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 3668 | // extractAll returns a vector containing the extracted n scalar value of |
| 3669 | // the vector vec. |
| 3670 | static std::vector<Value*> extractAll(Value* vec, int n) |
| 3671 | { |
| 3672 | std::vector<Value*> elements; |
| 3673 | elements.reserve(n); |
| 3674 | for (int i = 0; i < n; i++) |
| 3675 | { |
| 3676 | auto el = V(::builder->CreateExtractElement(V(vec), i)); |
| 3677 | elements.push_back(el); |
| 3678 | } |
| 3679 | return elements; |
| 3680 | } |
| 3681 | |
| 3682 | // toDouble returns all the float values in vals extended to doubles. |
| 3683 | static std::vector<Value*> toDouble(const std::vector<Value*>& vals) |
| 3684 | { |
| 3685 | auto doubleTy = ::llvm::Type::getDoubleTy(*::context); |
| 3686 | std::vector<Value*> elements; |
| 3687 | elements.reserve(vals.size()); |
| 3688 | for (auto v : vals) |
| 3689 | { |
| 3690 | elements.push_back(V(::builder->CreateFPExt(V(v), doubleTy))); |
| 3691 | } |
| 3692 | return elements; |
| 3693 | } |
| 3694 | |
| 3695 | std::vector<Value*> PrintValue::Ty<Byte4>::val(const RValue<Byte4>& v) { return extractAll(v.value, 4); } |
| 3696 | std::vector<Value*> PrintValue::Ty<Int4>::val(const RValue<Int4>& v) { return extractAll(v.value, 4); } |
| 3697 | std::vector<Value*> PrintValue::Ty<UInt4>::val(const RValue<UInt4>& v) { return extractAll(v.value, 4); } |
| 3698 | std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return extractAll(v.value, 4); } |
| 3699 | std::vector<Value*> PrintValue::Ty<UShort4>::val(const RValue<UShort4>& v) { return extractAll(v.value, 4); } |
| 3700 | std::vector<Value*> PrintValue::Ty<Float>::val(const RValue<Float>& v) { return toDouble({v.value}); } |
| 3701 | std::vector<Value*> PrintValue::Ty<Float4>::val(const RValue<Float4>& v) { return toDouble(extractAll(v.value, 4)); } |
| 3702 | |
| 3703 | void Printv(const char* function, const char* file, int line, const char* fmt, std::initializer_list<PrintValue> args) |
| 3704 | { |
| 3705 | // LLVM types used below. |
| 3706 | auto i32Ty = ::llvm::Type::getInt32Ty(*::context); |
| 3707 | auto intTy = ::llvm::Type::getInt64Ty(*::context); // TODO: Natural int width. |
| 3708 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(*::context); |
| 3709 | auto funcTy = ::llvm::FunctionType::get(i32Ty, {i8PtrTy}, true); |
| 3710 | |
| 3711 | auto func = ::module->getOrInsertFunction("printf", funcTy); |
| 3712 | |
| 3713 | // Build the printf format message string. |
| 3714 | std::string str; |
| 3715 | if (file != nullptr) { str += (line > 0) ? "%s:%d " : "%s "; } |
| 3716 | if (function != nullptr) { str += "%s "; } |
| 3717 | str += fmt; |
| 3718 | |
| 3719 | // Perform subsitution on all '{n}' bracketed indices in the format |
| 3720 | // message. |
| 3721 | int i = 0; |
| 3722 | for (const PrintValue& arg : args) |
| 3723 | { |
| 3724 | str = replace(str, "{" + std::to_string(i++) + "}", arg.format); |
| 3725 | } |
| 3726 | |
| 3727 | ::llvm::SmallVector<::llvm::Value*, 8> vals; |
| 3728 | |
| 3729 | // The format message is always the first argument. |
| 3730 | vals.push_back(::builder->CreateGlobalStringPtr(str)); |
| 3731 | |
| 3732 | // Add optional file, line and function info if provided. |
| 3733 | if (file != nullptr) |
| 3734 | { |
| 3735 | vals.push_back(::builder->CreateGlobalStringPtr(file)); |
| 3736 | if (line > 0) |
| 3737 | { |
| 3738 | vals.push_back(::llvm::ConstantInt::get(intTy, line)); |
| 3739 | } |
| 3740 | } |
| 3741 | if (function != nullptr) |
| 3742 | { |
| 3743 | vals.push_back(::builder->CreateGlobalStringPtr(function)); |
| 3744 | } |
| 3745 | |
| 3746 | // Add all format arguments. |
| 3747 | for (const PrintValue& arg : args) |
| 3748 | { |
| 3749 | for (auto val : arg.values) |
| 3750 | { |
| 3751 | vals.push_back(V(val)); |
| 3752 | } |
| 3753 | } |
| 3754 | |
| 3755 | ::builder->CreateCall(func, vals); |
| 3756 | } |
| 3757 | #endif // ENABLE_RR_PRINT |
| 3758 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3759 | } |