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