Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 8 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 14 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 15 | #include "Reactor.hpp" |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 16 | #include "Debug.hpp" |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 17 | #include "LLVMReactor.hpp" |
| 18 | #include "LLVMReactorDebugInfo.hpp" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 19 | |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 20 | #include "x86.hpp" |
| 21 | #include "CPUID.hpp" |
| 22 | #include "Thread.hpp" |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 23 | #include "ExecutableMemory.hpp" |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 24 | #include "MutexLock.hpp" |
| 25 | |
| 26 | #undef min |
| 27 | #undef max |
| 28 | |
Ben Clayton | 09a7f45 | 2019-04-25 15:22:43 +0100 | [diff] [blame] | 29 | #if defined(__clang__) |
| 30 | // LLVM has occurances of the extra-semi warning in its headers, which will be |
| 31 | // treated as an error in SwiftShader targets. |
| 32 | #pragma clang diagnostic push |
| 33 | #pragma clang diagnostic ignored "-Wextra-semi" |
| 34 | #endif // defined(__clang__) |
| 35 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 36 | #include "llvm/Analysis/LoopPass.h" |
| 37 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 38 | #include "llvm/ExecutionEngine/JITSymbol.h" |
| 39 | #include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
| 40 | #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
| 41 | #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" |
| 42 | #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" |
| 43 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
| 44 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
| 45 | #include "llvm/IR/Constants.h" |
| 46 | #include "llvm/IR/DataLayout.h" |
| 47 | #include "llvm/IR/Function.h" |
| 48 | #include "llvm/IR/GlobalVariable.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 49 | #include "llvm/IR/Intrinsics.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 50 | #include "llvm/IR/IRBuilder.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 51 | #include "llvm/IR/LegacyPassManager.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 52 | #include "llvm/IR/LLVMContext.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 53 | #include "llvm/IR/Mangler.h" |
| 54 | #include "llvm/IR/Module.h" |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 55 | #include "llvm/IR/Verifier.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 56 | #include "llvm/Support/Error.h" |
| 57 | #include "llvm/Support/TargetSelect.h" |
| 58 | #include "llvm/Target/TargetOptions.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 59 | #include "llvm/Transforms/Coroutines.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 60 | #include "llvm/Transforms/InstCombine/InstCombine.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 61 | #include "llvm/Transforms/IPO.h" |
| 62 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 63 | #include "llvm/Transforms/Scalar.h" |
| 64 | #include "llvm/Transforms/Scalar/GVN.h" |
Ben Clayton | 20507fa | 2019-04-20 01:40:15 -0400 | [diff] [blame] | 65 | |
Ben Clayton | 09a7f45 | 2019-04-25 15:22:43 +0100 | [diff] [blame] | 66 | #if defined(__clang__) |
| 67 | #pragma clang diagnostic pop |
| 68 | #endif // defined(__clang__) |
| 69 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 70 | #define ARGS(...) {__VA_ARGS__} |
| 71 | #define CreateCall2 CreateCall |
| 72 | #define CreateCall3 CreateCall |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 73 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 74 | #include <unordered_map> |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 75 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 76 | #include <fstream> |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 77 | #include <iostream> |
| 78 | #include <mutex> |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 79 | #include <numeric> |
| 80 | #include <thread> |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 81 | |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 82 | #if defined(__i386__) || defined(__x86_64__) |
| 83 | #include <xmmintrin.h> |
| 84 | #endif |
| 85 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 86 | #include <math.h> |
| 87 | |
Nicolas Capens | cb12258 | 2014-05-06 23:34:44 -0400 | [diff] [blame] | 88 | #if defined(__x86_64__) && defined(_WIN32) |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 89 | extern "C" void X86CompilationCallback() |
| 90 | { |
| 91 | UNIMPLEMENTED("X86CompilationCallback"); |
| 92 | } |
| 93 | #endif |
| 94 | |
| 95 | #if defined(_WIN64) |
| 96 | extern "C" void __chkstk(); |
| 97 | #elif defined(_WIN32) |
| 98 | extern "C" void _chkstk(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 99 | #endif |
| 100 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 101 | namespace rr |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 102 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 103 | void* resolveExternalSymbol(const char*); |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 104 | } |
| 105 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 106 | namespace |
| 107 | { |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 108 | // Default configuration settings. Must be accessed under mutex lock. |
| 109 | std::mutex defaultConfigLock; |
| 110 | rr::Config &defaultConfig() |
| 111 | { |
| 112 | // This uses a static in a function to avoid the cost of a global static |
| 113 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 114 | static rr::Config config = rr::Config::Edit() |
| 115 | .set(rr::Optimization::Level::Default) |
| 116 | .add(rr::Optimization::Pass::ScalarReplAggregates) |
| 117 | .add(rr::Optimization::Pass::InstructionCombining) |
| 118 | .apply({}); |
| 119 | return config; |
| 120 | } |
| 121 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 122 | // Cache provides a simple, thread-safe key-value store. |
| 123 | template <typename KEY, typename VALUE> |
| 124 | class Cache |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 125 | { |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 126 | public: |
| 127 | Cache() = default; |
| 128 | Cache(const Cache& other); |
| 129 | VALUE getOrCreate(KEY key, std::function<VALUE()> create); |
| 130 | private: |
| 131 | mutable std::mutex mutex; // mutable required for copy constructor. |
| 132 | std::unordered_map<KEY, VALUE> map; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 133 | }; |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 134 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 135 | template <typename KEY, typename VALUE> |
| 136 | Cache<KEY, VALUE>::Cache(const Cache& other) |
| 137 | { |
| 138 | std::unique_lock<std::mutex> lock(other.mutex); |
| 139 | map = other.map; |
| 140 | } |
| 141 | |
| 142 | template <typename KEY, typename VALUE> |
| 143 | VALUE Cache<KEY, VALUE>::getOrCreate(KEY key, std::function<VALUE()> create) |
| 144 | { |
| 145 | std::unique_lock<std::mutex> lock(mutex); |
| 146 | auto it = map.find(key); |
| 147 | if (it != map.end()) |
| 148 | { |
| 149 | return it->second; |
| 150 | } |
| 151 | auto value = create(); |
| 152 | map.emplace(key, value); |
| 153 | return value; |
| 154 | } |
| 155 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 156 | // JITGlobals is a singleton that holds all the immutable machine specific |
| 157 | // information for the host device. |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 158 | class JITGlobals |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 159 | { |
| 160 | public: |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 161 | using TargetMachineSPtr = std::shared_ptr<llvm::TargetMachine>; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 162 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 163 | static JITGlobals * get(); |
| 164 | |
| 165 | const std::string mcpu; |
| 166 | const std::vector<std::string> mattrs; |
| 167 | const char* const march; |
| 168 | const llvm::TargetOptions targetOptions; |
| 169 | const llvm::DataLayout dataLayout; |
| 170 | |
| 171 | TargetMachineSPtr getTargetMachine(rr::Optimization::Level optlevel); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 172 | |
| 173 | private: |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 174 | static JITGlobals create(); |
| 175 | static llvm::CodeGenOpt::Level toLLVM(rr::Optimization::Level level); |
| 176 | JITGlobals(const char *mcpu, |
| 177 | const std::vector<std::string> &mattrs, |
| 178 | const char *march, |
| 179 | const llvm::TargetOptions &targetOptions, |
| 180 | const llvm::DataLayout &dataLayout); |
| 181 | JITGlobals(const JITGlobals&) = default; |
| 182 | |
| 183 | // The cache key here is actually a rr::Optimization::Level. We use int |
| 184 | // as 'enum class' types do not provide builtin hash functions until |
| 185 | // C++14. See: https://stackoverflow.com/a/29618545. |
| 186 | Cache<int, TargetMachineSPtr> targetMachines; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 187 | }; |
| 188 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 189 | JITGlobals * JITGlobals::get() |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 190 | { |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 191 | static JITGlobals instance = create(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 192 | return &instance; |
| 193 | } |
| 194 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 195 | JITGlobals::TargetMachineSPtr JITGlobals::getTargetMachine(rr::Optimization::Level optlevel) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 196 | { |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 197 | return targetMachines.getOrCreate(static_cast<int>(optlevel), [&]() { |
| 198 | return TargetMachineSPtr(llvm::EngineBuilder() |
| 199 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | e031f36 | 2019-07-20 12:35:40 +0100 | [diff] [blame] | 200 | .setOptLevel(toLLVM(rr::Optimization::Level::None)) |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 201 | #else |
| 202 | .setOptLevel(toLLVM(optlevel)) |
| 203 | #endif // ENABLE_RR_DEBUG_INFO |
| 204 | .setMCPU(mcpu) |
| 205 | .setMArch(march) |
| 206 | .setMAttrs(mattrs) |
| 207 | .setTargetOptions(targetOptions) |
| 208 | .selectTarget()); |
| 209 | }); |
| 210 | } |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 211 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 212 | JITGlobals JITGlobals::create() |
| 213 | { |
| 214 | struct LLVMInitializer |
| 215 | { |
| 216 | LLVMInitializer() |
| 217 | { |
| 218 | llvm::InitializeNativeTarget(); |
| 219 | llvm::InitializeNativeTargetAsmPrinter(); |
| 220 | llvm::InitializeNativeTargetAsmParser(); |
| 221 | } |
| 222 | }; |
| 223 | static LLVMInitializer initializeLLVM; |
| 224 | |
| 225 | auto mcpu = llvm::sys::getHostCPUName(); |
| 226 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 227 | llvm::StringMap<bool> features; |
| 228 | bool ok = llvm::sys::getHostCPUFeatures(features); |
| 229 | |
| 230 | #if defined(__i386__) || defined(__x86_64__) || \ |
| 231 | (defined(__linux__) && (defined(__arm__) || defined(__aarch64__))) |
| 232 | ASSERT_MSG(ok, "llvm::sys::getHostCPUFeatures returned false"); |
| 233 | #else |
| 234 | (void) ok; // getHostCPUFeatures always returns false on other platforms |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 235 | #endif |
| 236 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 237 | std::vector<std::string> mattrs; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 238 | for (auto &feature : features) |
| 239 | { |
| 240 | if (feature.second) { mattrs.push_back(feature.first()); } |
| 241 | } |
| 242 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 243 | const char* march = nullptr; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 244 | #if defined(__x86_64__) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 245 | march = "x86-64"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 246 | #elif defined(__i386__) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 247 | march = "x86"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 248 | #elif defined(__aarch64__) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 249 | march = "arm64"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 250 | #elif defined(__arm__) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 251 | march = "arm"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 252 | #elif defined(__mips__) |
| 253 | #if defined(__mips64) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 254 | march = "mips64el"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 255 | #else |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 256 | march = "mipsel"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 257 | #endif |
| 258 | #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 259 | march = "ppc64le"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 260 | #else |
| 261 | #error "unknown architecture" |
| 262 | #endif |
| 263 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 264 | llvm::TargetOptions targetOptions; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 265 | targetOptions.UnsafeFPMath = false; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 266 | |
| 267 | auto targetMachine = std::unique_ptr<llvm::TargetMachine>( |
| 268 | llvm::EngineBuilder() |
| 269 | .setOptLevel(llvm::CodeGenOpt::None) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 270 | .setMCPU(mcpu) |
| 271 | .setMArch(march) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 272 | .setMAttrs(mattrs) |
| 273 | .setTargetOptions(targetOptions) |
| 274 | .selectTarget()); |
| 275 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 276 | auto dataLayout = targetMachine->createDataLayout(); |
| 277 | |
| 278 | return JITGlobals(mcpu.data(), mattrs, march, targetOptions, dataLayout); |
| 279 | } |
| 280 | |
| 281 | llvm::CodeGenOpt::Level JITGlobals::toLLVM(rr::Optimization::Level level) |
| 282 | { |
| 283 | switch (level) |
| 284 | { |
| 285 | case rr::Optimization::Level::None: return ::llvm::CodeGenOpt::None; |
| 286 | case rr::Optimization::Level::Less: return ::llvm::CodeGenOpt::Less; |
| 287 | case rr::Optimization::Level::Default: return ::llvm::CodeGenOpt::Default; |
| 288 | case rr::Optimization::Level::Aggressive: return ::llvm::CodeGenOpt::Aggressive; |
| 289 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
| 290 | } |
| 291 | return ::llvm::CodeGenOpt::Default; |
| 292 | } |
| 293 | |
| 294 | JITGlobals::JITGlobals(const char* mcpu, |
| 295 | const std::vector<std::string> &mattrs, |
| 296 | const char* march, |
| 297 | const llvm::TargetOptions &targetOptions, |
| 298 | const llvm::DataLayout &dataLayout) : |
| 299 | mcpu(mcpu), |
| 300 | mattrs(mattrs), |
| 301 | march(march), |
| 302 | targetOptions(targetOptions), |
| 303 | dataLayout(dataLayout) |
| 304 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // JITRoutine is a rr::Routine that holds a LLVM JIT session, compiler and |
| 308 | // object layer as each routine may require different target machine |
| 309 | // settings and no Reactor routine directly links against another. |
| 310 | class JITRoutine : public rr::Routine |
| 311 | { |
| 312 | using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer; |
| 313 | using CompileLayer = llvm::orc::IRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>; |
| 314 | public: |
Ben Clayton | 68cfc78 | 2019-06-29 12:31:08 +0100 | [diff] [blame] | 315 | JITRoutine( |
| 316 | std::unique_ptr<llvm::Module> module, |
| 317 | llvm::Function **funcs, |
| 318 | size_t count, |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 319 | const rr::Config &config) : |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 320 | resolver(createLegacyLookupResolver( |
| 321 | session, |
| 322 | [&](const std::string &name) { |
| 323 | void *func = rr::resolveExternalSymbol(name.c_str()); |
| 324 | if (func != nullptr) |
| 325 | { |
| 326 | return llvm::JITSymbol( |
| 327 | reinterpret_cast<uintptr_t>(func), llvm::JITSymbolFlags::Absolute); |
| 328 | } |
| 329 | return objLayer.findSymbol(name, true); |
| 330 | }, |
| 331 | [](llvm::Error err) { |
| 332 | if (err) |
| 333 | { |
| 334 | // TODO: Log the symbol resolution errors. |
| 335 | return; |
| 336 | } |
| 337 | })), |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 338 | targetMachine(JITGlobals::get()->getTargetMachine(config.getOptimization().getLevel())), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 339 | compileLayer(objLayer, llvm::orc::SimpleCompiler(*targetMachine)), |
| 340 | objLayer( |
| 341 | session, |
| 342 | [this](llvm::orc::VModuleKey) { |
| 343 | return ObjLayer::Resources{std::make_shared<llvm::SectionMemoryManager>(), resolver}; |
| 344 | }, |
| 345 | ObjLayer::NotifyLoadedFtor(), |
| 346 | [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj, const llvm::RuntimeDyld::LoadedObjectInfo &L) { |
| 347 | #ifdef ENABLE_RR_DEBUG_INFO |
| 348 | rr::DebugInfo::NotifyObjectEmitted(Obj, L); |
| 349 | #endif // ENABLE_RR_DEBUG_INFO |
| 350 | }, |
| 351 | [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj) { |
| 352 | #ifdef ENABLE_RR_DEBUG_INFO |
| 353 | rr::DebugInfo::NotifyFreeingObject(Obj); |
| 354 | #endif // ENABLE_RR_DEBUG_INFO |
| 355 | } |
| 356 | ), |
| 357 | addresses(count) |
| 358 | { |
| 359 | std::vector<std::string> mangledNames(count); |
| 360 | for (size_t i = 0; i < count; i++) |
| 361 | { |
| 362 | auto func = funcs[i]; |
| 363 | static size_t numEmittedFunctions = 0; |
| 364 | std::string name = "f" + llvm::Twine(numEmittedFunctions++).str(); |
| 365 | func->setName(name); |
| 366 | func->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 367 | func->setDoesNotThrow(); |
| 368 | |
| 369 | llvm::raw_string_ostream mangledNameStream(mangledNames[i]); |
| 370 | llvm::Mangler::getNameWithPrefix(mangledNameStream, name, JITGlobals::get()->dataLayout); |
| 371 | } |
| 372 | |
| 373 | auto moduleKey = session.allocateVModule(); |
| 374 | |
| 375 | // Once the module is passed to the compileLayer, the |
| 376 | // llvm::Functions are freed. Make sure funcs are not referenced |
| 377 | // after this point. |
| 378 | funcs = nullptr; |
| 379 | |
| 380 | llvm::cantFail(compileLayer.addModule(moduleKey, std::move(module))); |
| 381 | |
| 382 | // Resolve the function addresses. |
| 383 | for (size_t i = 0; i < count; i++) |
| 384 | { |
| 385 | auto symbol = compileLayer.findSymbolIn(moduleKey, mangledNames[i], false); |
| 386 | if(auto address = symbol.getAddress()) |
| 387 | { |
| 388 | addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(address.get())); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | const void *getEntry(int index) override |
| 394 | { |
| 395 | return addresses[index]; |
| 396 | } |
| 397 | |
| 398 | private: |
| 399 | std::shared_ptr<llvm::orc::SymbolResolver> resolver; |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 400 | std::shared_ptr<llvm::TargetMachine> targetMachine; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 401 | llvm::orc::ExecutionSession session; |
| 402 | CompileLayer compileLayer; |
| 403 | ObjLayer objLayer; |
| 404 | std::vector<const void *> addresses; |
| 405 | }; |
| 406 | |
| 407 | // JITBuilder holds all the LLVM state for building routines. |
| 408 | class JITBuilder |
| 409 | { |
| 410 | public: |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 411 | JITBuilder(const rr::Config &config) : |
| 412 | config(config), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 413 | module(new llvm::Module("", context)), |
| 414 | builder(new llvm::IRBuilder<>(context)) |
| 415 | { |
| 416 | module->setDataLayout(JITGlobals::get()->dataLayout); |
| 417 | } |
| 418 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 419 | void optimize(const rr::Config &cfg) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 420 | { |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 421 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 422 | #ifdef ENABLE_RR_DEBUG_INFO |
| 423 | if (debugInfo != nullptr) |
| 424 | { |
| 425 | return; // Don't optimize if we're generating debug info. |
| 426 | } |
| 427 | #endif // ENABLE_RR_DEBUG_INFO |
| 428 | |
| 429 | std::unique_ptr<llvm::legacy::PassManager> passManager( |
| 430 | new llvm::legacy::PassManager()); |
| 431 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 432 | for(auto pass : cfg.getOptimization().getPasses()) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 433 | { |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 434 | switch(pass) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 435 | { |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 436 | case rr::Optimization::Pass::Disabled: break; |
| 437 | case rr::Optimization::Pass::CFGSimplification: passManager->add(llvm::createCFGSimplificationPass()); break; |
| 438 | case rr::Optimization::Pass::LICM: passManager->add(llvm::createLICMPass()); break; |
| 439 | case rr::Optimization::Pass::AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); break; |
| 440 | case rr::Optimization::Pass::GVN: passManager->add(llvm::createGVNPass()); break; |
| 441 | case rr::Optimization::Pass::InstructionCombining: passManager->add(llvm::createInstructionCombiningPass()); break; |
| 442 | case rr::Optimization::Pass::Reassociate: passManager->add(llvm::createReassociatePass()); break; |
| 443 | case rr::Optimization::Pass::DeadStoreElimination: passManager->add(llvm::createDeadStoreEliminationPass()); break; |
| 444 | case rr::Optimization::Pass::SCCP: passManager->add(llvm::createSCCPPass()); break; |
| 445 | case rr::Optimization::Pass::ScalarReplAggregates: passManager->add(llvm::createSROAPass()); break; |
Ben Clayton | 28ae0a4 | 2019-07-10 00:50:23 +0100 | [diff] [blame] | 446 | case rr::Optimization::Pass::EarlyCSEPass: passManager->add(llvm::createEarlyCSEPass()); break; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 447 | default: |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 448 | UNREACHABLE("pass: %d", int(pass)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | passManager->run(*module); |
| 453 | } |
| 454 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 455 | std::shared_ptr<rr::Routine> acquireRoutine(llvm::Function **funcs, size_t count, const rr::Config &cfg) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 456 | { |
| 457 | ASSERT(module); |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 458 | return std::make_shared<JITRoutine>(std::move(module), funcs, count, cfg); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 459 | } |
| 460 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 461 | const rr::Config config; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 462 | llvm::LLVMContext context; |
| 463 | std::unique_ptr<llvm::Module> module; |
| 464 | std::unique_ptr<llvm::IRBuilder<>> builder; |
| 465 | llvm::Function *function = nullptr; |
| 466 | |
| 467 | struct CoroutineState |
| 468 | { |
| 469 | llvm::Function *await = nullptr; |
| 470 | llvm::Function *destroy = nullptr; |
| 471 | llvm::Value *handle = nullptr; |
| 472 | llvm::Value *id = nullptr; |
| 473 | llvm::Value *promise = nullptr; |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 474 | llvm::Type *yieldType = nullptr; |
| 475 | llvm::BasicBlock *entryBlock = nullptr; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 476 | llvm::BasicBlock *suspendBlock = nullptr; |
| 477 | llvm::BasicBlock *endBlock = nullptr; |
| 478 | llvm::BasicBlock *destroyBlock = nullptr; |
| 479 | }; |
| 480 | CoroutineState coroutine; |
| 481 | |
| 482 | #ifdef ENABLE_RR_DEBUG_INFO |
| 483 | std::unique_ptr<rr::DebugInfo> debugInfo; |
| 484 | #endif |
| 485 | }; |
| 486 | |
| 487 | std::unique_ptr<JITBuilder> jit; |
| 488 | std::mutex codegenMutex; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 489 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 490 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 491 | std::string replace(std::string str, const std::string& substr, const std::string& replacement) |
| 492 | { |
| 493 | size_t pos = 0; |
| 494 | while((pos = str.find(substr, pos)) != std::string::npos) { |
| 495 | str.replace(pos, substr.length(), replacement); |
| 496 | pos += replacement.length(); |
| 497 | } |
| 498 | return str; |
| 499 | } |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 500 | #endif // ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 501 | |
Ben Clayton | 40a885e | 2019-06-23 19:12:48 +0100 | [diff] [blame] | 502 | template <typename T> |
| 503 | T alignUp(T val, T alignment) |
| 504 | { |
| 505 | return alignment * ((val + alignment - 1) / alignment); |
| 506 | } |
| 507 | |
| 508 | void* alignedAlloc(size_t size, size_t alignment) |
| 509 | { |
| 510 | ASSERT(alignment < 256); |
| 511 | auto allocation = new uint8_t[size + sizeof(uint8_t) + alignment]; |
| 512 | auto aligned = allocation; |
| 513 | aligned += sizeof(uint8_t); // Make space for the base-address offset. |
| 514 | aligned = reinterpret_cast<uint8_t*>(alignUp(reinterpret_cast<uintptr_t>(aligned), alignment)); // align |
| 515 | auto offset = static_cast<uint8_t>(aligned - allocation); |
| 516 | aligned[-1] = offset; |
| 517 | return aligned; |
| 518 | } |
| 519 | |
| 520 | void alignedFree(void* ptr) |
| 521 | { |
| 522 | auto aligned = reinterpret_cast<uint8_t*>(ptr); |
| 523 | auto offset = aligned[-1]; |
| 524 | auto allocation = aligned - offset; |
| 525 | delete[] allocation; |
| 526 | } |
| 527 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 528 | llvm::Value *lowerPAVG(llvm::Value *x, llvm::Value *y) |
| 529 | { |
| 530 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 531 | |
| 532 | llvm::VectorType *extTy = |
| 533 | llvm::VectorType::getExtendedElementVectorType(ty); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 534 | x = jit->builder->CreateZExt(x, extTy); |
| 535 | y = jit->builder->CreateZExt(y, extTy); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 536 | |
| 537 | // (x + y + 1) >> 1 |
| 538 | llvm::Constant *one = llvm::ConstantInt::get(extTy, 1); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 539 | llvm::Value *res = jit->builder->CreateAdd(x, y); |
| 540 | res = jit->builder->CreateAdd(res, one); |
| 541 | res = jit->builder->CreateLShr(res, one); |
| 542 | return jit->builder->CreateTrunc(res, ty); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | llvm::Value *lowerPMINMAX(llvm::Value *x, llvm::Value *y, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 546 | llvm::ICmpInst::Predicate pred) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 547 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 548 | return jit->builder->CreateSelect(jit->builder->CreateICmp(pred, x, y), x, y); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | llvm::Value *lowerPCMP(llvm::ICmpInst::Predicate pred, llvm::Value *x, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 552 | llvm::Value *y, llvm::Type *dstTy) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 553 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 554 | return jit->builder->CreateSExt(jit->builder->CreateICmp(pred, x, y), dstTy, ""); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 555 | } |
| 556 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 557 | #if defined(__i386__) || defined(__x86_64__) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 558 | llvm::Value *lowerPMOV(llvm::Value *op, llvm::Type *dstType, bool sext) |
| 559 | { |
| 560 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(op->getType()); |
| 561 | llvm::VectorType *dstTy = llvm::cast<llvm::VectorType>(dstType); |
| 562 | |
| 563 | llvm::Value *undef = llvm::UndefValue::get(srcTy); |
| 564 | llvm::SmallVector<uint32_t, 16> mask(dstTy->getNumElements()); |
| 565 | std::iota(mask.begin(), mask.end(), 0); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 566 | llvm::Value *v = jit->builder->CreateShuffleVector(op, undef, mask); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 567 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 568 | return sext ? jit->builder->CreateSExt(v, dstTy) |
| 569 | : jit->builder->CreateZExt(v, dstTy); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | llvm::Value *lowerPABS(llvm::Value *v) |
| 573 | { |
| 574 | llvm::Value *zero = llvm::Constant::getNullValue(v->getType()); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 575 | llvm::Value *cmp = jit->builder->CreateICmp(llvm::ICmpInst::ICMP_SGT, v, zero); |
| 576 | llvm::Value *neg = jit->builder->CreateNeg(v); |
| 577 | return jit->builder->CreateSelect(cmp, v, neg); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 578 | } |
| 579 | #endif // defined(__i386__) || defined(__x86_64__) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 580 | |
| 581 | #if !defined(__i386__) && !defined(__x86_64__) |
| 582 | llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 583 | llvm::FCmpInst::Predicate pred) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 584 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 585 | return jit->builder->CreateSelect(jit->builder->CreateFCmp(pred, x, y), x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 586 | } |
| 587 | |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 588 | llvm::Value *lowerRound(llvm::Value *x) |
| 589 | { |
| 590 | llvm::Function *nearbyint = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 591 | jit->module.get(), llvm::Intrinsic::nearbyint, {x->getType()}); |
| 592 | return jit->builder->CreateCall(nearbyint, ARGS(x)); |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 593 | } |
| 594 | |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 595 | llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty) |
| 596 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 597 | return jit->builder->CreateFPToSI(lowerRound(x), ty); |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 598 | } |
| 599 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 600 | llvm::Value *lowerFloor(llvm::Value *x) |
| 601 | { |
| 602 | llvm::Function *floor = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 603 | jit->module.get(), llvm::Intrinsic::floor, {x->getType()}); |
| 604 | return jit->builder->CreateCall(floor, ARGS(x)); |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 605 | } |
| 606 | |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 607 | llvm::Value *lowerTrunc(llvm::Value *x) |
| 608 | { |
| 609 | llvm::Function *trunc = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 610 | jit->module.get(), llvm::Intrinsic::trunc, {x->getType()}); |
| 611 | return jit->builder->CreateCall(trunc, ARGS(x)); |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 612 | } |
| 613 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 614 | // Packed add/sub saturatation |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 615 | 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] | 616 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 617 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 618 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 619 | |
| 620 | unsigned numBits = ty->getScalarSizeInBits(); |
| 621 | |
| 622 | llvm::Value *max, *min, *extX, *extY; |
| 623 | if (isSigned) |
| 624 | { |
| 625 | max = llvm::ConstantInt::get(extTy, (1LL << (numBits - 1)) - 1, true); |
| 626 | min = llvm::ConstantInt::get(extTy, (-1LL << (numBits - 1)), true); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 627 | extX = jit->builder->CreateSExt(x, extTy); |
| 628 | extY = jit->builder->CreateSExt(y, extTy); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 629 | } |
| 630 | else |
| 631 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 632 | ASSERT_MSG(numBits <= 64, "numBits: %d", int(numBits)); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 633 | uint64_t maxVal = (numBits == 64) ? ~0ULL : (1ULL << numBits) - 1; |
| 634 | max = llvm::ConstantInt::get(extTy, maxVal, false); |
| 635 | min = llvm::ConstantInt::get(extTy, 0, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 636 | extX = jit->builder->CreateZExt(x, extTy); |
| 637 | extY = jit->builder->CreateZExt(y, extTy); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 638 | } |
| 639 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 640 | llvm::Value *res = isAdd ? jit->builder->CreateAdd(extX, extY) |
| 641 | : jit->builder->CreateSub(extX, extY); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 642 | |
| 643 | res = lowerPMINMAX(res, min, llvm::ICmpInst::ICMP_SGT); |
| 644 | res = lowerPMINMAX(res, max, llvm::ICmpInst::ICMP_SLT); |
| 645 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 646 | return jit->builder->CreateTrunc(res, ty); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | llvm::Value *lowerPUADDSAT(llvm::Value *x, llvm::Value *y) |
| 650 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 651 | return lowerPSAT(x, y, true, false); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | llvm::Value *lowerPSADDSAT(llvm::Value *x, llvm::Value *y) |
| 655 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 656 | return lowerPSAT(x, y, true, true); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | llvm::Value *lowerPUSUBSAT(llvm::Value *x, llvm::Value *y) |
| 660 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 661 | return lowerPSAT(x, y, false, false); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | llvm::Value *lowerPSSUBSAT(llvm::Value *x, llvm::Value *y) |
| 665 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 666 | return lowerPSAT(x, y, false, true); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | llvm::Value *lowerSQRT(llvm::Value *x) |
| 670 | { |
| 671 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 672 | jit->module.get(), llvm::Intrinsic::sqrt, {x->getType()}); |
| 673 | return jit->builder->CreateCall(sqrt, ARGS(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | llvm::Value *lowerRCP(llvm::Value *x) |
| 677 | { |
| 678 | llvm::Type *ty = x->getType(); |
| 679 | llvm::Constant *one; |
| 680 | if (llvm::VectorType *vectorTy = llvm::dyn_cast<llvm::VectorType>(ty)) |
| 681 | { |
| 682 | one = llvm::ConstantVector::getSplat( |
| 683 | vectorTy->getNumElements(), |
| 684 | llvm::ConstantFP::get(vectorTy->getElementType(), 1)); |
| 685 | } |
| 686 | else |
| 687 | { |
| 688 | one = llvm::ConstantFP::get(ty, 1); |
| 689 | } |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 690 | return jit->builder->CreateFDiv(one, x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | llvm::Value *lowerRSQRT(llvm::Value *x) |
| 694 | { |
| 695 | return lowerRCP(lowerSQRT(x)); |
| 696 | } |
| 697 | |
| 698 | llvm::Value *lowerVectorShl(llvm::Value *x, uint64_t scalarY) |
| 699 | { |
| 700 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 701 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 702 | ty->getNumElements(), |
| 703 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 704 | return jit->builder->CreateShl(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | llvm::Value *lowerVectorAShr(llvm::Value *x, uint64_t scalarY) |
| 708 | { |
| 709 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 710 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 711 | ty->getNumElements(), |
| 712 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 713 | return jit->builder->CreateAShr(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | llvm::Value *lowerVectorLShr(llvm::Value *x, uint64_t scalarY) |
| 717 | { |
| 718 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 719 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 720 | ty->getNumElements(), |
| 721 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 722 | return jit->builder->CreateLShr(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | llvm::Value *lowerMulAdd(llvm::Value *x, llvm::Value *y) |
| 726 | { |
| 727 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 728 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 729 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 730 | llvm::Value *extX = jit->builder->CreateSExt(x, extTy); |
| 731 | llvm::Value *extY = jit->builder->CreateSExt(y, extTy); |
| 732 | llvm::Value *mult = jit->builder->CreateMul(extX, extY); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 733 | |
| 734 | llvm::Value *undef = llvm::UndefValue::get(extTy); |
| 735 | |
| 736 | llvm::SmallVector<uint32_t, 16> evenIdx; |
| 737 | llvm::SmallVector<uint32_t, 16> oddIdx; |
| 738 | for (uint64_t i = 0, n = ty->getNumElements(); i < n; i += 2) |
| 739 | { |
| 740 | evenIdx.push_back(i); |
| 741 | oddIdx.push_back(i + 1); |
| 742 | } |
| 743 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 744 | llvm::Value *lhs = jit->builder->CreateShuffleVector(mult, undef, evenIdx); |
| 745 | llvm::Value *rhs = jit->builder->CreateShuffleVector(mult, undef, oddIdx); |
| 746 | return jit->builder->CreateAdd(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 747 | } |
| 748 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 749 | llvm::Value *lowerPack(llvm::Value *x, llvm::Value *y, bool isSigned) |
| 750 | { |
| 751 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(x->getType()); |
| 752 | llvm::VectorType *dstTy = llvm::VectorType::getTruncatedElementVectorType(srcTy); |
| 753 | |
| 754 | llvm::IntegerType *dstElemTy = |
| 755 | llvm::cast<llvm::IntegerType>(dstTy->getElementType()); |
| 756 | |
| 757 | uint64_t truncNumBits = dstElemTy->getIntegerBitWidth(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 758 | 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] | 759 | llvm::Constant *max, *min; |
| 760 | if (isSigned) |
| 761 | { |
| 762 | max = llvm::ConstantInt::get(srcTy, (1LL << (truncNumBits - 1)) - 1, true); |
| 763 | min = llvm::ConstantInt::get(srcTy, (-1LL << (truncNumBits - 1)), true); |
| 764 | } |
| 765 | else |
| 766 | { |
| 767 | max = llvm::ConstantInt::get(srcTy, (1ULL << truncNumBits) - 1, false); |
| 768 | min = llvm::ConstantInt::get(srcTy, 0, false); |
| 769 | } |
| 770 | |
| 771 | x = lowerPMINMAX(x, min, llvm::ICmpInst::ICMP_SGT); |
| 772 | x = lowerPMINMAX(x, max, llvm::ICmpInst::ICMP_SLT); |
| 773 | y = lowerPMINMAX(y, min, llvm::ICmpInst::ICMP_SGT); |
| 774 | y = lowerPMINMAX(y, max, llvm::ICmpInst::ICMP_SLT); |
| 775 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 776 | x = jit->builder->CreateTrunc(x, dstTy); |
| 777 | y = jit->builder->CreateTrunc(y, dstTy); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 778 | |
| 779 | llvm::SmallVector<uint32_t, 16> index(srcTy->getNumElements() * 2); |
| 780 | std::iota(index.begin(), index.end(), 0); |
| 781 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 782 | return jit->builder->CreateShuffleVector(x, y, index); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | llvm::Value *lowerSignMask(llvm::Value *x, llvm::Type *retTy) |
| 786 | { |
| 787 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 788 | llvm::Constant *zero = llvm::ConstantInt::get(ty, 0); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 789 | llvm::Value *cmp = jit->builder->CreateICmpSLT(x, zero); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 790 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 791 | llvm::Value *ret = jit->builder->CreateZExt( |
| 792 | jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 793 | for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
| 794 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 795 | llvm::Value *elem = jit->builder->CreateZExt( |
| 796 | jit->builder->CreateExtractElement(cmp, i), retTy); |
| 797 | ret = jit->builder->CreateOr(ret, jit->builder->CreateShl(elem, i)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 798 | } |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | llvm::Value *lowerFPSignMask(llvm::Value *x, llvm::Type *retTy) |
| 803 | { |
| 804 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 805 | llvm::Constant *zero = llvm::ConstantFP::get(ty, 0); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 806 | llvm::Value *cmp = jit->builder->CreateFCmpULT(x, zero); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 807 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 808 | llvm::Value *ret = jit->builder->CreateZExt( |
| 809 | jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 810 | for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
| 811 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 812 | llvm::Value *elem = jit->builder->CreateZExt( |
| 813 | jit->builder->CreateExtractElement(cmp, i), retTy); |
| 814 | ret = jit->builder->CreateOr(ret, jit->builder->CreateShl(elem, i)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 815 | } |
| 816 | return ret; |
| 817 | } |
| 818 | #endif // !defined(__i386__) && !defined(__x86_64__) |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 819 | |
| 820 | llvm::Value *lowerMulHigh(llvm::Value *x, llvm::Value *y, bool sext) |
| 821 | { |
| 822 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 823 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 824 | |
| 825 | llvm::Value *extX, *extY; |
| 826 | if (sext) |
| 827 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 828 | extX = jit->builder->CreateSExt(x, extTy); |
| 829 | extY = jit->builder->CreateSExt(y, extTy); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 830 | } |
| 831 | else |
| 832 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 833 | extX = jit->builder->CreateZExt(x, extTy); |
| 834 | extY = jit->builder->CreateZExt(y, extTy); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 837 | llvm::Value *mult = jit->builder->CreateMul(extX, extY); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 838 | |
| 839 | llvm::IntegerType *intTy = llvm::cast<llvm::IntegerType>(ty->getElementType()); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 840 | llvm::Value *mulh = jit->builder->CreateAShr(mult, intTy->getBitWidth()); |
| 841 | return jit->builder->CreateTrunc(mulh, ty); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 842 | } |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 843 | } |
| 844 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 845 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 846 | { |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 847 | const Capabilities Caps = |
| 848 | { |
| 849 | true, // CallSupported |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 850 | true, // CoroutinesSupported |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 851 | }; |
| 852 | |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 853 | static std::memory_order atomicOrdering(llvm::AtomicOrdering memoryOrder) |
| 854 | { |
| 855 | switch(memoryOrder) |
| 856 | { |
| 857 | case llvm::AtomicOrdering::Monotonic: return std::memory_order_relaxed; // https://llvm.org/docs/Atomics.html#monotonic |
| 858 | case llvm::AtomicOrdering::Acquire: return std::memory_order_acquire; |
| 859 | case llvm::AtomicOrdering::Release: return std::memory_order_release; |
| 860 | case llvm::AtomicOrdering::AcquireRelease: return std::memory_order_acq_rel; |
| 861 | case llvm::AtomicOrdering::SequentiallyConsistent: return std::memory_order_seq_cst; |
| 862 | default: |
Ben Clayton | fb28067 | 2019-04-25 11:16:15 +0100 | [diff] [blame] | 863 | UNREACHABLE("memoryOrder: %d", int(memoryOrder)); |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 864 | return std::memory_order_acq_rel; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | static llvm::AtomicOrdering atomicOrdering(bool atomic, std::memory_order memoryOrder) |
| 869 | { |
| 870 | if(!atomic) |
| 871 | { |
| 872 | return llvm::AtomicOrdering::NotAtomic; |
| 873 | } |
| 874 | |
| 875 | switch(memoryOrder) |
| 876 | { |
| 877 | case std::memory_order_relaxed: return llvm::AtomicOrdering::Monotonic; // https://llvm.org/docs/Atomics.html#monotonic |
| 878 | 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." |
| 879 | case std::memory_order_acquire: return llvm::AtomicOrdering::Acquire; |
| 880 | case std::memory_order_release: return llvm::AtomicOrdering::Release; |
| 881 | case std::memory_order_acq_rel: return llvm::AtomicOrdering::AcquireRelease; |
| 882 | case std::memory_order_seq_cst: return llvm::AtomicOrdering::SequentiallyConsistent; |
| 883 | default: |
| 884 | UNREACHABLE("memoryOrder: %d", int(memoryOrder)); |
| 885 | return llvm::AtomicOrdering::AcquireRelease; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | template <typename T> |
| 890 | static void atomicLoad(void *ptr, void *ret, llvm::AtomicOrdering ordering) |
| 891 | { |
| 892 | *reinterpret_cast<T*>(ret) = std::atomic_load_explicit<T>(reinterpret_cast<std::atomic<T>*>(ptr), atomicOrdering(ordering)); |
| 893 | } |
| 894 | |
| 895 | template <typename T> |
| 896 | static void atomicStore(void *ptr, void *val, llvm::AtomicOrdering ordering) |
| 897 | { |
| 898 | std::atomic_store_explicit<T>(reinterpret_cast<std::atomic<T>*>(ptr), *reinterpret_cast<T*>(val), atomicOrdering(ordering)); |
| 899 | } |
| 900 | |
Chris Forbes | fd4c96d | 2019-06-20 11:20:42 -0700 | [diff] [blame] | 901 | #ifdef __ANDROID__ |
| 902 | template<typename F> |
| 903 | static uint32_t sync_fetch_and_op(uint32_t volatile *ptr, uint32_t val, F f) |
| 904 | { |
| 905 | // Build an arbitrary op out of looped CAS |
| 906 | for (;;) |
| 907 | { |
| 908 | uint32_t expected = *ptr; |
| 909 | uint32_t desired = f(expected, val); |
| 910 | |
| 911 | if (expected == __sync_val_compare_and_swap_4(ptr, expected, desired)) |
| 912 | return expected; |
| 913 | } |
| 914 | } |
| 915 | #endif |
| 916 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 917 | void* resolveExternalSymbol(const char* name) |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 918 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 919 | struct Atomic |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 920 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 921 | static void load(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering) |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 922 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 923 | switch (size) |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 924 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 925 | case 1: atomicLoad<uint8_t>(ptr, ret, ordering); break; |
| 926 | case 2: atomicLoad<uint16_t>(ptr, ret, ordering); break; |
| 927 | case 4: atomicLoad<uint32_t>(ptr, ret, ordering); break; |
| 928 | case 8: atomicLoad<uint64_t>(ptr, ret, ordering); break; |
| 929 | default: |
| 930 | UNIMPLEMENTED("Atomic::load(size: %d)", int(size)); |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 931 | } |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 932 | } |
| 933 | static void store(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering) |
Chris Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 934 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 935 | switch (size) |
| 936 | { |
| 937 | case 1: atomicStore<uint8_t>(ptr, ret, ordering); break; |
| 938 | case 2: atomicStore<uint16_t>(ptr, ret, ordering); break; |
| 939 | case 4: atomicStore<uint32_t>(ptr, ret, ordering); break; |
| 940 | case 8: atomicStore<uint64_t>(ptr, ret, ordering); break; |
| 941 | default: |
| 942 | UNIMPLEMENTED("Atomic::store(size: %d)", int(size)); |
| 943 | } |
| 944 | } |
| 945 | }; |
Chris Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 946 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 947 | struct F |
| 948 | { |
| 949 | static void nop() {} |
| 950 | static void neverCalled() { UNREACHABLE("Should never be called"); } |
| 951 | |
| 952 | static void* coroutine_alloc_frame(size_t size) { return alignedAlloc(size, 16); } |
| 953 | static void coroutine_free_frame(void* ptr) { alignedFree(ptr); } |
Ben Clayton | 40a885e | 2019-06-23 19:12:48 +0100 | [diff] [blame] | 954 | |
Chris Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 955 | #ifdef __ANDROID__ |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 956 | // forwarders since we can't take address of builtins |
| 957 | static void sync_synchronize() { __sync_synchronize(); } |
| 958 | static uint32_t sync_fetch_and_add_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_add_4(ptr, val); } |
| 959 | static uint32_t sync_fetch_and_and_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_and_4(ptr, val); } |
| 960 | static uint32_t sync_fetch_and_or_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_or_4(ptr, val); } |
| 961 | static uint32_t sync_fetch_and_xor_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_xor_4(ptr, val); } |
| 962 | static uint32_t sync_fetch_and_sub_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_sub_4(ptr, val); } |
| 963 | static uint32_t sync_lock_test_and_set_4(uint32_t *ptr, uint32_t val) { return __sync_lock_test_and_set_4(ptr, val); } |
| 964 | static uint32_t sync_val_compare_and_swap_4(uint32_t *ptr, uint32_t expected, uint32_t desired) { return __sync_val_compare_and_swap_4(ptr, expected, desired); } |
Chris Forbes | fd4c96d | 2019-06-20 11:20:42 -0700 | [diff] [blame] | 965 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 966 | static uint32_t sync_fetch_and_max_4(uint32_t *ptr, uint32_t val) { return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::max(a,b);}); } |
| 967 | static uint32_t sync_fetch_and_min_4(uint32_t *ptr, uint32_t val) { return sync_fetch_and_op(ptr, val, [](int32_t a, int32_t b) { return std::min(a,b);}); } |
| 968 | static uint32_t sync_fetch_and_umax_4(uint32_t *ptr, uint32_t val) { return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::max(a,b);}); } |
| 969 | static uint32_t sync_fetch_and_umin_4(uint32_t *ptr, uint32_t val) { return sync_fetch_and_op(ptr, val, [](uint32_t a, uint32_t b) { return std::min(a,b);}); } |
Chris Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 970 | #endif |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 971 | }; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 972 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 973 | class Resolver |
| 974 | { |
| 975 | public: |
| 976 | using FunctionMap = std::unordered_map<std::string, void *>; |
Ben Clayton | b5f1a13 | 2019-06-24 11:00:53 +0100 | [diff] [blame] | 977 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 978 | FunctionMap functions; |
Ben Clayton | b5f1a13 | 2019-06-24 11:00:53 +0100 | [diff] [blame] | 979 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 980 | Resolver() |
| 981 | { |
| 982 | functions.emplace("nop", reinterpret_cast<void*>(F::nop)); |
| 983 | functions.emplace("floorf", reinterpret_cast<void*>(floorf)); |
| 984 | functions.emplace("nearbyintf", reinterpret_cast<void*>(nearbyintf)); |
| 985 | functions.emplace("truncf", reinterpret_cast<void*>(truncf)); |
| 986 | functions.emplace("printf", reinterpret_cast<void*>(printf)); |
| 987 | functions.emplace("puts", reinterpret_cast<void*>(puts)); |
| 988 | functions.emplace("fmodf", reinterpret_cast<void*>(fmodf)); |
Ben Clayton | b5f1a13 | 2019-06-24 11:00:53 +0100 | [diff] [blame] | 989 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 990 | functions.emplace("sinf", reinterpret_cast<void*>(sinf)); |
| 991 | functions.emplace("cosf", reinterpret_cast<void*>(cosf)); |
| 992 | functions.emplace("asinf", reinterpret_cast<void*>(asinf)); |
| 993 | functions.emplace("acosf", reinterpret_cast<void*>(acosf)); |
| 994 | functions.emplace("atanf", reinterpret_cast<void*>(atanf)); |
| 995 | functions.emplace("sinhf", reinterpret_cast<void*>(sinhf)); |
| 996 | functions.emplace("coshf", reinterpret_cast<void*>(coshf)); |
| 997 | functions.emplace("tanhf", reinterpret_cast<void*>(tanhf)); |
| 998 | functions.emplace("asinhf", reinterpret_cast<void*>(asinhf)); |
| 999 | functions.emplace("acoshf", reinterpret_cast<void*>(acoshf)); |
| 1000 | functions.emplace("atanhf", reinterpret_cast<void*>(atanhf)); |
| 1001 | functions.emplace("atan2f", reinterpret_cast<void*>(atan2f)); |
| 1002 | functions.emplace("powf", reinterpret_cast<void*>(powf)); |
| 1003 | functions.emplace("expf", reinterpret_cast<void*>(expf)); |
| 1004 | functions.emplace("logf", reinterpret_cast<void*>(logf)); |
| 1005 | functions.emplace("exp2f", reinterpret_cast<void*>(exp2f)); |
| 1006 | functions.emplace("log2f", reinterpret_cast<void*>(log2f)); |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 1007 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1008 | functions.emplace("sin", reinterpret_cast<void*>(static_cast<double(*)(double)>(sin))); |
| 1009 | functions.emplace("cos", reinterpret_cast<void*>(static_cast<double(*)(double)>(cos))); |
| 1010 | functions.emplace("asin", reinterpret_cast<void*>(static_cast<double(*)(double)>(asin))); |
| 1011 | functions.emplace("acos", reinterpret_cast<void*>(static_cast<double(*)(double)>(acos))); |
| 1012 | functions.emplace("atan", reinterpret_cast<void*>(static_cast<double(*)(double)>(atan))); |
| 1013 | functions.emplace("sinh", reinterpret_cast<void*>(static_cast<double(*)(double)>(sinh))); |
| 1014 | functions.emplace("cosh", reinterpret_cast<void*>(static_cast<double(*)(double)>(cosh))); |
| 1015 | functions.emplace("tanh", reinterpret_cast<void*>(static_cast<double(*)(double)>(tanh))); |
| 1016 | functions.emplace("asinh", reinterpret_cast<void*>(static_cast<double(*)(double)>(asinh))); |
| 1017 | functions.emplace("acosh", reinterpret_cast<void*>(static_cast<double(*)(double)>(acosh))); |
| 1018 | functions.emplace("atanh", reinterpret_cast<void*>(static_cast<double(*)(double)>(atanh))); |
| 1019 | functions.emplace("atan2", reinterpret_cast<void*>(static_cast<double(*)(double,double)>(atan2))); |
| 1020 | functions.emplace("pow", reinterpret_cast<void*>(static_cast<double(*)(double,double)>(pow))); |
| 1021 | functions.emplace("exp", reinterpret_cast<void*>(static_cast<double(*)(double)>(exp))); |
| 1022 | functions.emplace("log", reinterpret_cast<void*>(static_cast<double(*)(double)>(log))); |
| 1023 | functions.emplace("exp2", reinterpret_cast<void*>(static_cast<double(*)(double)>(exp2))); |
| 1024 | functions.emplace("log2", reinterpret_cast<void*>(static_cast<double(*)(double)>(log2))); |
| 1025 | |
| 1026 | functions.emplace("atomic_load", reinterpret_cast<void*>(Atomic::load)); |
| 1027 | functions.emplace("atomic_store", reinterpret_cast<void*>(Atomic::store)); |
| 1028 | |
| 1029 | // FIXME (b/119409619): use an allocator here so we can control all memory allocations |
| 1030 | functions.emplace("coroutine_alloc_frame", reinterpret_cast<void*>(F::coroutine_alloc_frame)); |
| 1031 | functions.emplace("coroutine_free_frame", reinterpret_cast<void*>(F::coroutine_free_frame)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 1032 | |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 1033 | #ifdef __APPLE__ |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1034 | functions.emplace("sincosf_stret", reinterpret_cast<void*>(__sincosf_stret)); |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 1035 | #elif defined(__linux__) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1036 | functions.emplace("sincosf", reinterpret_cast<void*>(sincosf)); |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 1037 | #elif defined(_WIN64) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1038 | functions.emplace("chkstk", reinterpret_cast<void*>(__chkstk)); |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 1039 | #elif defined(_WIN32) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1040 | functions.emplace("chkstk", reinterpret_cast<void*>(_chkstk)); |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 1041 | #endif |
Chris Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 1042 | |
| 1043 | #ifdef __ANDROID__ |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1044 | functions.emplace("aeabi_unwind_cpp_pr0", reinterpret_cast<void*>(F::neverCalled)); |
| 1045 | functions.emplace("sync_synchronize", reinterpret_cast<void*>(F::sync_synchronize)); |
| 1046 | functions.emplace("sync_fetch_and_add_4", reinterpret_cast<void*>(F::sync_fetch_and_add_4)); |
| 1047 | functions.emplace("sync_fetch_and_and_4", reinterpret_cast<void*>(F::sync_fetch_and_and_4)); |
| 1048 | functions.emplace("sync_fetch_and_or_4", reinterpret_cast<void*>(F::sync_fetch_and_or_4)); |
| 1049 | functions.emplace("sync_fetch_and_xor_4", reinterpret_cast<void*>(F::sync_fetch_and_xor_4)); |
| 1050 | functions.emplace("sync_fetch_and_sub_4", reinterpret_cast<void*>(F::sync_fetch_and_sub_4)); |
| 1051 | functions.emplace("sync_lock_test_and_set_4", reinterpret_cast<void*>(F::sync_lock_test_and_set_4)); |
| 1052 | functions.emplace("sync_val_compare_and_swap_4", reinterpret_cast<void*>(F::sync_val_compare_and_swap_4)); |
| 1053 | functions.emplace("sync_fetch_and_max_4", reinterpret_cast<void*>(F::sync_fetch_and_max_4)); |
| 1054 | functions.emplace("sync_fetch_and_min_4", reinterpret_cast<void*>(F::sync_fetch_and_min_4)); |
| 1055 | functions.emplace("sync_fetch_and_umax_4", reinterpret_cast<void*>(F::sync_fetch_and_umax_4)); |
| 1056 | functions.emplace("sync_fetch_and_umin_4", reinterpret_cast<void*>(F::sync_fetch_and_umin_4)); |
| 1057 | #endif |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 1058 | } |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1059 | }; |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 1060 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1061 | static Resolver resolver; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1062 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1063 | // Trim off any underscores from the start of the symbol. LLVM likes |
| 1064 | // to append these on macOS. |
| 1065 | const char* trimmed = name; |
| 1066 | while (trimmed[0] == '_') { trimmed++; } |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 1067 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1068 | auto it = resolver.functions.find(trimmed); |
| 1069 | // Missing functions will likely make the module fail in exciting non-obvious ways. |
| 1070 | ASSERT_MSG(it != resolver.functions.end(), "Missing external function: '%s'", name); |
| 1071 | return it->second; |
| 1072 | } |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1073 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1074 | // The abstract Type* types are implemented as LLVM types, except that |
| 1075 | // 64-bit vectors are emulated using 128-bit ones to avoid use of MMX in x86 |
| 1076 | // and VFP in ARM, and eliminate the overhead of converting them to explicit |
| 1077 | // 128-bit ones. LLVM types are pointers, so we can represent emulated types |
| 1078 | // as abstract pointers with small enum values. |
| 1079 | enum InternalType : uintptr_t |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1080 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1081 | // Emulated types: |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1082 | Type_v2i32, |
| 1083 | Type_v4i16, |
| 1084 | Type_v2i16, |
| 1085 | Type_v8i8, |
| 1086 | Type_v4i8, |
| 1087 | Type_v2f32, |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1088 | EmulatedTypeCount, |
| 1089 | // Returned by asInternalType() to indicate that the abstract Type* |
| 1090 | // should be interpreted as LLVM type pointer: |
| 1091 | Type_LLVM |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1092 | }; |
| 1093 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1094 | inline InternalType asInternalType(Type *type) |
| 1095 | { |
| 1096 | InternalType t = static_cast<InternalType>(reinterpret_cast<uintptr_t>(type)); |
| 1097 | return (t < EmulatedTypeCount) ? t : Type_LLVM; |
| 1098 | } |
| 1099 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1100 | llvm::Type *T(Type *t) |
| 1101 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1102 | // Use 128-bit vectors to implement logically shorter ones. |
| 1103 | switch(asInternalType(t)) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1104 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1105 | case Type_v2i32: return T(Int4::getType()); |
| 1106 | case Type_v4i16: return T(Short8::getType()); |
| 1107 | case Type_v2i16: return T(Short8::getType()); |
| 1108 | case Type_v8i8: return T(Byte16::getType()); |
| 1109 | case Type_v4i8: return T(Byte16::getType()); |
| 1110 | case Type_v2f32: return T(Float4::getType()); |
| 1111 | case Type_LLVM: return reinterpret_cast<llvm::Type*>(t); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1112 | default: |
| 1113 | UNREACHABLE("asInternalType(t): %d", int(asInternalType(t))); |
| 1114 | return nullptr; |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1115 | } |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1116 | } |
| 1117 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1118 | Type *T(InternalType t) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1119 | { |
| 1120 | return reinterpret_cast<Type*>(t); |
| 1121 | } |
| 1122 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1123 | inline std::vector<llvm::Type*> &T(std::vector<Type*> &t) |
| 1124 | { |
| 1125 | return reinterpret_cast<std::vector<llvm::Type*>&>(t); |
| 1126 | } |
| 1127 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1128 | inline llvm::BasicBlock *B(BasicBlock *t) |
| 1129 | { |
| 1130 | return reinterpret_cast<llvm::BasicBlock*>(t); |
| 1131 | } |
| 1132 | |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 1133 | inline BasicBlock *B(llvm::BasicBlock *t) |
| 1134 | { |
| 1135 | return reinterpret_cast<BasicBlock*>(t); |
| 1136 | } |
| 1137 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1138 | static size_t typeSize(Type *type) |
| 1139 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1140 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1141 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1142 | case Type_v2i32: return 8; |
| 1143 | case Type_v4i16: return 8; |
| 1144 | case Type_v2i16: return 4; |
| 1145 | case Type_v8i8: return 8; |
| 1146 | case Type_v4i8: return 4; |
| 1147 | case Type_v2f32: return 8; |
| 1148 | case Type_LLVM: |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1149 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1150 | llvm::Type *t = T(type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1151 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1152 | if(t->isPointerTy()) |
| 1153 | { |
| 1154 | return sizeof(void*); |
| 1155 | } |
| 1156 | |
| 1157 | // At this point we should only have LLVM 'primitive' types. |
| 1158 | unsigned int bits = t->getPrimitiveSizeInBits(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1159 | ASSERT_MSG(bits != 0, "bits: %d", int(bits)); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1160 | |
| 1161 | // TODO(capn): Booleans are 1 bit integers in LLVM's SSA type system, |
| 1162 | // but are typically stored as one byte. The DataLayout structure should |
| 1163 | // be used here and many other places if this assumption fails. |
| 1164 | return (bits + 7) / 8; |
| 1165 | } |
| 1166 | break; |
| 1167 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1168 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1169 | return 0; |
| 1170 | } |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1171 | } |
| 1172 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1173 | static unsigned int elementCount(Type *type) |
| 1174 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1175 | switch(asInternalType(type)) |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1176 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1177 | case Type_v2i32: return 2; |
| 1178 | case Type_v4i16: return 4; |
| 1179 | case Type_v2i16: return 2; |
| 1180 | case Type_v8i8: return 8; |
| 1181 | case Type_v4i8: return 4; |
| 1182 | case Type_v2f32: return 2; |
| 1183 | case Type_LLVM: return llvm::cast<llvm::VectorType>(T(type))->getNumElements(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1184 | default: |
| 1185 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1186 | return 0; |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1187 | } |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1188 | } |
| 1189 | |
Ben Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 1190 | static ::llvm::Function* createFunction(const char *name, ::llvm::Type *retTy, const std::vector<::llvm::Type*> ¶ms) |
| 1191 | { |
| 1192 | llvm::FunctionType *functionType = llvm::FunctionType::get(retTy, params, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1193 | auto func = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, name, jit->module.get()); |
Ben Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 1194 | func->setDoesNotThrow(); |
| 1195 | func->setCallingConv(llvm::CallingConv::C); |
Ben Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 1196 | return func; |
| 1197 | } |
| 1198 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1199 | Nucleus::Nucleus() |
| 1200 | { |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 1201 | ::codegenMutex.lock(); // Reactor and LLVM are currently not thread safe |
Nicolas Capens | b7ea984 | 2015-04-01 10:54:59 -0400 | [diff] [blame] | 1202 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1203 | ASSERT(jit == nullptr); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1204 | jit.reset(new JITBuilder(Nucleus::getDefaultConfig())); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | Nucleus::~Nucleus() |
| 1208 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1209 | jit.reset(); |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 1210 | ::codegenMutex.unlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1211 | } |
| 1212 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1213 | void Nucleus::setDefaultConfig(const Config &cfg) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1214 | { |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1215 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 1216 | ::defaultConfig() = cfg; |
| 1217 | } |
| 1218 | |
| 1219 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 1220 | { |
| 1221 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 1222 | auto &config = ::defaultConfig(); |
| 1223 | config = cfgEdit.apply(config); |
| 1224 | } |
| 1225 | |
| 1226 | Config Nucleus::getDefaultConfig() |
| 1227 | { |
| 1228 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 1229 | return ::defaultConfig(); |
| 1230 | } |
| 1231 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 1232 | std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1233 | { |
| 1234 | auto cfg = cfgEdit.apply(jit->config); |
| 1235 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1236 | if(jit->builder->GetInsertBlock()->empty() || !jit->builder->GetInsertBlock()->back().isTerminator()) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1237 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1238 | llvm::Type *type = jit->function->getReturnType(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1239 | |
| 1240 | if(type->isVoidTy()) |
| 1241 | { |
| 1242 | createRetVoid(); |
| 1243 | } |
| 1244 | else |
| 1245 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1246 | createRet(V(llvm::UndefValue::get(type))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1247 | } |
| 1248 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1249 | |
Ben Clayton | 97c13ad | 2019-05-02 11:59:30 +0100 | [diff] [blame] | 1250 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1251 | if (jit->debugInfo != nullptr) |
Ben Clayton | 97c13ad | 2019-05-02 11:59:30 +0100 | [diff] [blame] | 1252 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1253 | jit->debugInfo->Finalize(); |
Ben Clayton | 97c13ad | 2019-05-02 11:59:30 +0100 | [diff] [blame] | 1254 | } |
| 1255 | #endif // ENABLE_RR_DEBUG_INFO |
| 1256 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1257 | if(false) |
| 1258 | { |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 1259 | std::error_code error; |
| 1260 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1261 | jit->module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1262 | } |
| 1263 | |
Ben Clayton | 5375f47 | 2019-06-24 13:33:11 +0100 | [diff] [blame] | 1264 | #if defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 1265 | { |
| 1266 | llvm::legacy::PassManager pm; |
| 1267 | pm.add(llvm::createVerifierPass()); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1268 | pm.run(*jit->module); |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 1269 | } |
Ben Clayton | 5375f47 | 2019-06-24 13:33:11 +0100 | [diff] [blame] | 1270 | #endif // defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 1271 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1272 | jit->optimize(cfg); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1273 | |
| 1274 | if(false) |
| 1275 | { |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 1276 | std::error_code error; |
| 1277 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1278 | jit->module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1281 | auto routine = jit->acquireRoutine(&jit->function, 1, cfg); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1282 | jit.reset(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1283 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1284 | return routine; |
| 1285 | } |
| 1286 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1287 | Value *Nucleus::allocateStackVariable(Type *type, int arraySize) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1288 | { |
| 1289 | // Need to allocate it in the entry block for mem2reg to work |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1290 | llvm::BasicBlock &entryBlock = jit->function->getEntryBlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1291 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1292 | llvm::Instruction *declaration; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1293 | |
| 1294 | if(arraySize) |
| 1295 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1296 | declaration = new llvm::AllocaInst(T(type), 0, V(Nucleus::createConstantInt(arraySize))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1297 | } |
| 1298 | else |
| 1299 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1300 | declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value*)nullptr); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | entryBlock.getInstList().push_front(declaration); |
| 1304 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1305 | return V(declaration); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | BasicBlock *Nucleus::createBasicBlock() |
| 1309 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1310 | return B(llvm::BasicBlock::Create(jit->context, "", jit->function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | BasicBlock *Nucleus::getInsertBlock() |
| 1314 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1315 | return B(jit->builder->GetInsertBlock()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1319 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1320 | // assert(jit->builder->GetInsertBlock()->back().isTerminator()); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1321 | |
| 1322 | Variable::materializeAll(); |
| 1323 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1324 | jit->builder->SetInsertPoint(B(basicBlock)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1325 | } |
| 1326 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1327 | void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1328 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1329 | jit->function = rr::createFunction("", T(ReturnType), T(Params)); |
Nicolas Capens | 52551d1 | 2018-09-13 14:30:56 -0400 | [diff] [blame] | 1330 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1331 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | e031f36 | 2019-07-20 12:35:40 +0100 | [diff] [blame] | 1332 | jit->debugInfo = std::unique_ptr<DebugInfo>(new DebugInfo(jit->builder.get(), &jit->context, jit->module.get(), jit->function)); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1333 | #endif // ENABLE_RR_DEBUG_INFO |
| 1334 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1335 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1336 | } |
| 1337 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1338 | Value *Nucleus::getArgument(unsigned int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1339 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1340 | llvm::Function::arg_iterator args = jit->function->arg_begin(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1341 | |
| 1342 | while(index) |
| 1343 | { |
| 1344 | args++; |
| 1345 | index--; |
| 1346 | } |
| 1347 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1348 | return V(&*args); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1349 | } |
| 1350 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1351 | void Nucleus::createRetVoid() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1352 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1353 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1354 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1355 | ASSERT_MSG(jit->function->getReturnType() == T(Void::getType()), "Return type mismatch"); |
Ben Clayton | c958b17 | 2019-05-02 12:20:59 +0100 | [diff] [blame] | 1356 | |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1357 | // Code generated after this point is unreachable, so any variables |
| 1358 | // being read can safely return an undefined value. We have to avoid |
| 1359 | // materializing variables after the terminator ret instruction. |
| 1360 | Variable::killUnmaterialized(); |
| 1361 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1362 | jit->builder->CreateRetVoid(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1363 | } |
| 1364 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1365 | void Nucleus::createRet(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1366 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1367 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1368 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1369 | ASSERT_MSG(jit->function->getReturnType() == V(v)->getType(), "Return type mismatch"); |
Ben Clayton | c958b17 | 2019-05-02 12:20:59 +0100 | [diff] [blame] | 1370 | |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1371 | // Code generated after this point is unreachable, so any variables |
| 1372 | // being read can safely return an undefined value. We have to avoid |
| 1373 | // materializing variables after the terminator ret instruction. |
| 1374 | Variable::killUnmaterialized(); |
| 1375 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1376 | jit->builder->CreateRet(V(v)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1377 | } |
| 1378 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1379 | void Nucleus::createBr(BasicBlock *dest) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1380 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1381 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1382 | Variable::materializeAll(); |
| 1383 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1384 | jit->builder->CreateBr(B(dest)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1387 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1388 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1389 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1390 | Variable::materializeAll(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1391 | jit->builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1395 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1396 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1397 | return V(jit->builder->CreateAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1401 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1402 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1403 | return V(jit->builder->CreateSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1407 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1408 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1409 | return V(jit->builder->CreateMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1413 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1414 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1415 | return V(jit->builder->CreateUDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1419 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1420 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1421 | return V(jit->builder->CreateSDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1425 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1426 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1427 | return V(jit->builder->CreateFAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1431 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1432 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1433 | return V(jit->builder->CreateFSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1437 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1438 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1439 | return V(jit->builder->CreateFMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1443 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1444 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1445 | return V(jit->builder->CreateFDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1449 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1450 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1451 | return V(jit->builder->CreateURem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1455 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1456 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1457 | return V(jit->builder->CreateSRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1461 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1462 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1463 | return V(jit->builder->CreateFRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1467 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1468 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1469 | return V(jit->builder->CreateShl(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1473 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1474 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1475 | return V(jit->builder->CreateLShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1479 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1480 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1481 | return V(jit->builder->CreateAShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1485 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1486 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1487 | return V(jit->builder->CreateAnd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1491 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1492 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1493 | return V(jit->builder->CreateOr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1497 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1498 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1499 | return V(jit->builder->CreateXor(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1500 | } |
| 1501 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1502 | Value *Nucleus::createNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1503 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1504 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1505 | return V(jit->builder->CreateNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1506 | } |
| 1507 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1508 | Value *Nucleus::createFNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1509 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1510 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1511 | return V(jit->builder->CreateFNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1512 | } |
| 1513 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1514 | Value *Nucleus::createNot(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1515 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1516 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1517 | return V(jit->builder->CreateNot(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1518 | } |
| 1519 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1520 | 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] | 1521 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1522 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1523 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1524 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1525 | case Type_v2i32: |
| 1526 | case Type_v4i16: |
| 1527 | case Type_v8i8: |
| 1528 | case Type_v2f32: |
| 1529 | return createBitCast( |
| 1530 | createInsertElement( |
| 1531 | V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1532 | createLoad(createBitCast(ptr, Pointer<Long>::getType()), Long::getType(), isVolatile, alignment, atomic, memoryOrder), |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1533 | 0), |
| 1534 | type); |
| 1535 | case Type_v2i16: |
| 1536 | case Type_v4i8: |
| 1537 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1538 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1539 | 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] | 1540 | 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] | 1541 | i = createZExt(i, Long::getType()); |
| 1542 | Value *v = createInsertElement(u, i, 0); |
| 1543 | return createBitCast(v, type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1544 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1545 | // Fallthrough to non-emulated case. |
| 1546 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1547 | { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1548 | auto elTy = T(type); |
| 1549 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1550 | |
| 1551 | if (!atomic) |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1552 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1553 | return V(jit->builder->CreateAlignedLoad(V(ptr), alignment, isVolatile)); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1554 | } |
| 1555 | else if (elTy->isIntegerTy() || elTy->isPointerTy()) |
| 1556 | { |
| 1557 | // Integers and pointers can be atomically loaded by setting |
| 1558 | // the ordering constraint on the load instruction. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1559 | auto load = jit->builder->CreateAlignedLoad(V(ptr), alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1560 | load->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1561 | return V(load); |
| 1562 | } |
| 1563 | else if (elTy->isFloatTy() || elTy->isDoubleTy()) |
| 1564 | { |
| 1565 | // LLVM claims to support atomic loads of float types as |
| 1566 | // above, but certain backends cannot deal with this. |
| 1567 | // Load as an integer and bitcast. See b/136037244. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1568 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
| 1569 | auto elAsIntTy = ::llvm::IntegerType::get(jit->context, size * 8); |
| 1570 | auto ptrCast = jit->builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo()); |
| 1571 | auto load = jit->builder->CreateAlignedLoad(ptrCast, alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1572 | load->setAtomic(atomicOrdering(atomic, memoryOrder)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1573 | auto loadCast = jit->builder->CreateBitCast(load, elTy); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1574 | return V(loadCast); |
| 1575 | } |
| 1576 | else |
| 1577 | { |
| 1578 | // More exotic types require falling back to the extern: |
| 1579 | // void __atomic_load(size_t size, void *ptr, void *ret, int ordering) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1580 | auto sizetTy = ::llvm::IntegerType::get(jit->context, sizeof(size_t) * 8); |
| 1581 | auto intTy = ::llvm::IntegerType::get(jit->context, sizeof(int) * 8); |
| 1582 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1583 | auto i8PtrTy = i8Ty->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1584 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1585 | auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1586 | auto func = jit->module->getOrInsertFunction("__atomic_load", funcTy); |
| 1587 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1588 | auto out = allocateStackVariable(type); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1589 | jit->builder->CreateCall(func, { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1590 | ::llvm::ConstantInt::get(sizetTy, size), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1591 | jit->builder->CreatePointerCast(V(ptr), i8PtrTy), |
| 1592 | jit->builder->CreatePointerCast(V(out), i8PtrTy), |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1593 | ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))), |
| 1594 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1595 | return V(jit->builder->CreateLoad(V(out))); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1596 | } |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1597 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1598 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1599 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1600 | return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1601 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1602 | } |
| 1603 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1604 | 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] | 1605 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1606 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1607 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1608 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1609 | case Type_v2i32: |
| 1610 | case Type_v4i16: |
| 1611 | case Type_v8i8: |
| 1612 | case Type_v2f32: |
| 1613 | createStore( |
| 1614 | createExtractElement( |
| 1615 | createBitCast(value, T(llvm::VectorType::get(T(Long::getType()), 2))), Long::getType(), 0), |
| 1616 | createBitCast(ptr, Pointer<Long>::getType()), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1617 | Long::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1618 | return value; |
| 1619 | case Type_v2i16: |
| 1620 | case Type_v4i8: |
| 1621 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1622 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1623 | createStore( |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1624 | createExtractElement(createBitCast(value, Int4::getType()), Int::getType(), 0), |
| 1625 | createBitCast(ptr, Pointer<Int>::getType()), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1626 | Int::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1627 | return value; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1628 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1629 | // Fallthrough to non-emulated case. |
| 1630 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1631 | { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1632 | auto elTy = T(type); |
| 1633 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1634 | |
| 1635 | if (!atomic) |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1636 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1637 | jit->builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1638 | } |
| 1639 | else if (elTy->isIntegerTy() || elTy->isPointerTy()) |
| 1640 | { |
| 1641 | // Integers and pointers can be atomically stored by setting |
| 1642 | // the ordering constraint on the store instruction. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1643 | auto store = jit->builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1644 | store->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1645 | } |
| 1646 | else if (elTy->isFloatTy() || elTy->isDoubleTy()) |
| 1647 | { |
| 1648 | // LLVM claims to support atomic stores of float types as |
| 1649 | // above, but certain backends cannot deal with this. |
| 1650 | // Store as an bitcast integer. See b/136037244. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1651 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
| 1652 | auto elAsIntTy = ::llvm::IntegerType::get(jit->context, size * 8); |
| 1653 | auto valCast = jit->builder->CreateBitCast(V(value), elAsIntTy); |
| 1654 | auto ptrCast = jit->builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo()); |
| 1655 | auto store = jit->builder->CreateAlignedStore(valCast, ptrCast, alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1656 | store->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1657 | } |
| 1658 | else |
| 1659 | { |
| 1660 | // More exotic types require falling back to the extern: |
| 1661 | // void __atomic_store(size_t size, void *ptr, void *val, int ordering) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1662 | auto sizetTy = ::llvm::IntegerType::get(jit->context, sizeof(size_t) * 8); |
| 1663 | auto intTy = ::llvm::IntegerType::get(jit->context, sizeof(int) * 8); |
| 1664 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1665 | auto i8PtrTy = i8Ty->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1666 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1667 | auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1668 | auto func = jit->module->getOrInsertFunction("__atomic_store", funcTy); |
| 1669 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1670 | auto copy = allocateStackVariable(type); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1671 | jit->builder->CreateStore(V(value), V(copy)); |
| 1672 | jit->builder->CreateCall(func, { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1673 | ::llvm::ConstantInt::get(sizetTy, size), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1674 | jit->builder->CreatePointerCast(V(ptr), i8PtrTy), |
| 1675 | jit->builder->CreatePointerCast(V(copy), i8PtrTy), |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1676 | ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))), |
| 1677 | }); |
| 1678 | } |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1679 | |
| 1680 | return value; |
| 1681 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1682 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1683 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1684 | return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1685 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1686 | } |
| 1687 | |
Ben Clayton | 0b00b95 | 2019-07-03 15:51:19 +0100 | [diff] [blame] | 1688 | Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1689 | { |
| 1690 | ASSERT(V(ptr)->getType()->isPointerTy()); |
| 1691 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1692 | |
| 1693 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1694 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 1695 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1696 | auto elVecTy = ::llvm::VectorType::get(T(elTy), numEls); |
| 1697 | auto elVecPtrTy = elVecTy->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1698 | auto i8Mask = jit->builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
Ben Clayton | 0b00b95 | 2019-07-03 15:51:19 +0100 | [diff] [blame] | 1699 | auto passthrough = zeroMaskedLanes ? ::llvm::Constant::getNullValue(elVecTy) : llvm::UndefValue::get(elVecTy); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1700 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1701 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_load, { elVecTy, elVecPtrTy } ); |
| 1702 | return V(jit->builder->CreateCall(func, { V(ptr), align, i8Mask, passthrough })); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 1706 | { |
| 1707 | ASSERT(V(ptr)->getType()->isPointerTy()); |
| 1708 | ASSERT(V(val)->getType()->isVectorTy()); |
| 1709 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1710 | |
| 1711 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1712 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 1713 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1714 | auto elVecTy = V(val)->getType(); |
| 1715 | auto elVecPtrTy = elVecTy->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1716 | auto i8Mask = jit->builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1717 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1718 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_store, { elVecTy, elVecPtrTy } ); |
| 1719 | jit->builder->CreateCall(func, { V(val), V(ptr), align, i8Mask }); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1720 | } |
| 1721 | |
Ben Clayton | 0b00b95 | 2019-07-03 15:51:19 +0100 | [diff] [blame] | 1722 | Value *Nucleus::createGather(Value *base, Type *elTy, Value *offsets, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1723 | { |
| 1724 | ASSERT(V(base)->getType()->isPointerTy()); |
| 1725 | ASSERT(V(offsets)->getType()->isVectorTy()); |
| 1726 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1727 | |
| 1728 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1729 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 1730 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 1731 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1732 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 1733 | auto elPtrTy = T(elTy)->getPointerTo(); |
| 1734 | auto elVecTy = ::llvm::VectorType::get(T(elTy), numEls); |
| 1735 | auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1736 | auto i8Base = jit->builder->CreatePointerCast(V(base), i8PtrTy); |
| 1737 | auto i8Ptrs = jit->builder->CreateGEP(i8Base, V(offsets)); |
| 1738 | auto elPtrs = jit->builder->CreatePointerCast(i8Ptrs, elPtrVecTy); |
| 1739 | auto i8Mask = jit->builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
Ben Clayton | 0b00b95 | 2019-07-03 15:51:19 +0100 | [diff] [blame] | 1740 | auto passthrough = zeroMaskedLanes ? ::llvm::Constant::getNullValue(elVecTy) : llvm::UndefValue::get(elVecTy); |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1741 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1742 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_gather, { elVecTy, elPtrVecTy } ); |
| 1743 | return V(jit->builder->CreateCall(func, { elPtrs, align, i8Mask, passthrough })); |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | void Nucleus::createScatter(Value *base, Value *val, Value *offsets, Value *mask, unsigned int alignment) |
| 1747 | { |
| 1748 | ASSERT(V(base)->getType()->isPointerTy()); |
| 1749 | ASSERT(V(val)->getType()->isVectorTy()); |
| 1750 | ASSERT(V(offsets)->getType()->isVectorTy()); |
| 1751 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1752 | |
| 1753 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1754 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 1755 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 1756 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1757 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 1758 | auto elVecTy = V(val)->getType(); |
| 1759 | auto elTy = elVecTy->getVectorElementType(); |
| 1760 | auto elPtrTy = elTy->getPointerTo(); |
| 1761 | auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1762 | auto i8Base = jit->builder->CreatePointerCast(V(base), i8PtrTy); |
| 1763 | auto i8Ptrs = jit->builder->CreateGEP(i8Base, V(offsets)); |
| 1764 | auto elPtrs = jit->builder->CreatePointerCast(i8Ptrs, elPtrVecTy); |
| 1765 | auto i8Mask = jit->builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1766 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1767 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_scatter, { elVecTy, elPtrVecTy } ); |
| 1768 | jit->builder->CreateCall(func, { V(val), elPtrs, align, i8Mask }); |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1769 | } |
| 1770 | |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 1771 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 1772 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1773 | jit->builder->CreateFence(atomicOrdering(true, memoryOrder)); |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 1774 | } |
| 1775 | |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1776 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1777 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1778 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1779 | ASSERT(V(ptr)->getType()->getContainedType(0) == T(type)); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1780 | if(sizeof(void*) == 8) |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1781 | { |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1782 | // LLVM manual: "When indexing into an array, pointer or vector, |
| 1783 | // integers of any width are allowed, and they are not required to |
| 1784 | // be constant. These integers are treated as signed values where |
| 1785 | // relevant." |
| 1786 | // |
| 1787 | // Thus if we want indexes to be treated as unsigned we have to |
| 1788 | // zero-extend them ourselves. |
| 1789 | // |
| 1790 | // Note that this is not because we want to address anywhere near |
| 1791 | // 4 GB of data. Instead this is important for performance because |
| 1792 | // x86 supports automatic zero-extending of 32-bit registers to |
| 1793 | // 64-bit. Thus when indexing into an array using a uint32 is |
| 1794 | // actually faster than an int32. |
| 1795 | index = unsignedIndex ? |
| 1796 | createZExt(index, Long::getType()) : |
| 1797 | createSExt(index, Long::getType()); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1798 | } |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1799 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1800 | // For non-emulated types we can rely on LLVM's GEP to calculate the |
| 1801 | // effective address correctly. |
| 1802 | if(asInternalType(type) == Type_LLVM) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1803 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1804 | return V(jit->builder->CreateGEP(V(ptr), V(index))); |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1805 | } |
| 1806 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1807 | // For emulated types we have to multiply the index by the intended |
| 1808 | // type size ourselves to obain the byte offset. |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1809 | index = (sizeof(void*) == 8) ? |
| 1810 | createMul(index, createConstantLong((int64_t)typeSize(type))) : |
| 1811 | createMul(index, createConstantInt((int)typeSize(type))); |
| 1812 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1813 | // Cast to a byte pointer, apply the byte offset, and cast back to the |
| 1814 | // original pointer type. |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1815 | return createBitCast( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1816 | V(jit->builder->CreateGEP(V(createBitCast(ptr, T(llvm::PointerType::get(T(Byte::getType()), 0)))), V(index))), |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1817 | T(llvm::PointerType::get(T(type), 0))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1818 | } |
| 1819 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1820 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1821 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1822 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1823 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Add, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1824 | } |
| 1825 | |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 1826 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1827 | { |
| 1828 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1829 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Sub, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1832 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1833 | { |
| 1834 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1835 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::And, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1839 | { |
| 1840 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1841 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Or, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1845 | { |
| 1846 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1847 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xor, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | Value *Nucleus::createAtomicMin(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1851 | { |
| 1852 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1853 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Min, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | Value *Nucleus::createAtomicMax(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1857 | { |
| 1858 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1859 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Max, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
Chris Forbes | f31bdad | 2019-05-23 14:58:08 -0700 | [diff] [blame] | 1862 | Value *Nucleus::createAtomicUMin(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1863 | { |
| 1864 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1865 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMin, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
Chris Forbes | f31bdad | 2019-05-23 14:58:08 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | Value *Nucleus::createAtomicUMax(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1869 | { |
| 1870 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1871 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMax, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
Chris Forbes | f31bdad | 2019-05-23 14:58:08 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1875 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1876 | { |
| 1877 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1878 | return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, V(ptr), V(value), atomicOrdering(true, memoryOrder))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1879 | } |
| 1880 | |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 1881 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1882 | { |
| 1883 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | c9ca99e | 2019-04-19 07:53:34 -0700 | [diff] [blame] | 1884 | // Note: AtomicCmpXchgInstruction returns a 2-member struct containing {result, success-flag}, not the result directly. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1885 | return V(jit->builder->CreateExtractValue( |
| 1886 | jit->builder->CreateAtomicCmpXchg(V(ptr), V(compare), V(value), atomicOrdering(true, memoryOrderEqual), atomicOrdering(true, memoryOrderUnequal)), |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 1887 | llvm::ArrayRef<unsigned>(0u))); |
| 1888 | } |
| 1889 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1890 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1891 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1892 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1893 | return V(jit->builder->CreateTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1894 | } |
| 1895 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1896 | Value *Nucleus::createZExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1897 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1898 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1899 | return V(jit->builder->CreateZExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1900 | } |
| 1901 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1902 | Value *Nucleus::createSExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1903 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1904 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1905 | return V(jit->builder->CreateSExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1906 | } |
| 1907 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1908 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1909 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1910 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1911 | return V(jit->builder->CreateFPToSI(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1912 | } |
| 1913 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1914 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1915 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1916 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1917 | return V(jit->builder->CreateSIToFP(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1918 | } |
| 1919 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1920 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1921 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1922 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1923 | return V(jit->builder->CreateFPTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1924 | } |
| 1925 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1926 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1927 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1928 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1929 | return V(jit->builder->CreateFPExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1930 | } |
| 1931 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1932 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1933 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1934 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1935 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1936 | // support for casting between scalars and wide vectors. Emulate them by writing to the stack and |
| 1937 | // reading back as the destination type. |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1938 | if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1939 | { |
| 1940 | Value *readAddress = allocateStackVariable(destType); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1941 | Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0))); |
| 1942 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1943 | return createLoad(readAddress, destType); |
| 1944 | } |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1945 | else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1946 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1947 | Value *writeAddress = allocateStackVariable(T(V(v)->getType())); |
| 1948 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1949 | Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0))); |
| 1950 | return createLoad(readAddress, destType); |
| 1951 | } |
| 1952 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1953 | return V(jit->builder->CreateBitCast(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1954 | } |
| 1955 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1956 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1957 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1958 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1959 | return V(jit->builder->CreateICmpEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1963 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1964 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1965 | return V(jit->builder->CreateICmpNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1969 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1970 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1971 | return V(jit->builder->CreateICmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1975 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1976 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1977 | return V(jit->builder->CreateICmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1981 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1982 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1983 | return V(jit->builder->CreateICmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1987 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1988 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1989 | return V(jit->builder->CreateICmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1993 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1994 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1995 | return V(jit->builder->CreateICmpSGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1999 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2000 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2001 | return V(jit->builder->CreateICmpSGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 2005 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2006 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2007 | return V(jit->builder->CreateICmpSLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 2011 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2012 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2013 | return V(jit->builder->CreateICmpSLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 2017 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2018 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2019 | return V(jit->builder->CreateFCmpOEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 2023 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2024 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2025 | return V(jit->builder->CreateFCmpOGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 2029 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2030 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2031 | return V(jit->builder->CreateFCmpOGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 2035 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2036 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2037 | return V(jit->builder->CreateFCmpOLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 2041 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2042 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2043 | return V(jit->builder->CreateFCmpOLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 2047 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2048 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2049 | return V(jit->builder->CreateFCmpONE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 2053 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2054 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2055 | return V(jit->builder->CreateFCmpORD(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 2059 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2060 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2061 | return V(jit->builder->CreateFCmpUNO(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 2065 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2066 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2067 | return V(jit->builder->CreateFCmpUEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 2071 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2072 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2073 | return V(jit->builder->CreateFCmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 2077 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2078 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2079 | return V(jit->builder->CreateFCmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 2083 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2084 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2085 | return V(jit->builder->CreateFCmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 2089 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2090 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2091 | return V(jit->builder->CreateFCmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 2095 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2096 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2097 | return V(jit->builder->CreateFCmpUNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2098 | } |
| 2099 | |
Nicolas Capens | e95d534 | 2016-09-30 11:37:28 -0400 | [diff] [blame] | 2100 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2101 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2102 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2103 | ASSERT(V(vector)->getType()->getContainedType(0) == T(type)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2104 | return V(jit->builder->CreateExtractElement(V(vector), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 2108 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2109 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2110 | return V(jit->builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2111 | } |
| 2112 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2113 | Value *Nucleus::createShuffleVector(Value *v1, Value *v2, const int *select) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2114 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2115 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2116 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2117 | int size = llvm::cast<llvm::VectorType>(V(v1)->getType())->getNumElements(); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2118 | const int maxSize = 16; |
| 2119 | llvm::Constant *swizzle[maxSize]; |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2120 | ASSERT(size <= maxSize); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2121 | |
| 2122 | for(int i = 0; i < size; i++) |
| 2123 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2124 | swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), select[i]); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(swizzle, size)); |
| 2128 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2129 | return V(jit->builder->CreateShuffleVector(V(v1), V(v2), shuffle)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2130 | } |
| 2131 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2132 | Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2133 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2134 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2135 | return V(jit->builder->CreateSelect(V(c), V(ifTrue), V(ifFalse))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2136 | } |
| 2137 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 2138 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2139 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2140 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2141 | return reinterpret_cast<SwitchCases*>(jit->builder->CreateSwitch(V(control), B(defaultBranch), numCases)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2142 | } |
| 2143 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 2144 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
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 | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2147 | llvm::SwitchInst *sw = reinterpret_cast<llvm::SwitchInst *>(switchCases); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2148 | sw->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), label, true), B(branch)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2149 | } |
| 2150 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 2151 | void Nucleus::createUnreachable() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2152 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2153 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2154 | jit->builder->CreateUnreachable(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2155 | } |
| 2156 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2157 | Type *Nucleus::getPointerType(Type *ElementType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2158 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2159 | return T(llvm::PointerType::get(T(ElementType), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2160 | } |
| 2161 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2162 | Value *Nucleus::createNullValue(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2163 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2164 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2165 | return V(llvm::Constant::getNullValue(T(Ty))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2166 | } |
| 2167 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2168 | Value *Nucleus::createConstantLong(int64_t i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2169 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2170 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2171 | return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(jit->context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2172 | } |
| 2173 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2174 | Value *Nucleus::createConstantInt(int i) |
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(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2177 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2178 | } |
| 2179 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2180 | Value *Nucleus::createConstantInt(unsigned int i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2181 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2182 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2183 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2184 | } |
| 2185 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2186 | Value *Nucleus::createConstantBool(bool b) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2187 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2188 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2189 | return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(jit->context), b)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2190 | } |
| 2191 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2192 | Value *Nucleus::createConstantByte(signed char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2193 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2194 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2195 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(jit->context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2196 | } |
| 2197 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2198 | Value *Nucleus::createConstantByte(unsigned char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2199 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2200 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2201 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(jit->context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2202 | } |
| 2203 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2204 | Value *Nucleus::createConstantShort(short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2205 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2206 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2207 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(jit->context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2208 | } |
| 2209 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2210 | Value *Nucleus::createConstantShort(unsigned short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2211 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2212 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2213 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(jit->context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2214 | } |
| 2215 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2216 | Value *Nucleus::createConstantFloat(float x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2217 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2218 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2219 | return V(llvm::ConstantFP::get(T(Float::getType()), x)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2220 | } |
| 2221 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2222 | Value *Nucleus::createNullPointer(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2223 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2224 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2225 | return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2226 | } |
| 2227 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2228 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2229 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2230 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2231 | const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. |
| 2232 | 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] | 2233 | ASSERT(numElements <= 16 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2234 | llvm::Constant *constantVector[16]; |
| 2235 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2236 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2237 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2238 | constantVector[i] = llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2239 | } |
| 2240 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2241 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2245 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2246 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2247 | const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. |
| 2248 | 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] | 2249 | ASSERT(numElements <= 8 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2250 | llvm::Constant *constantVector[8]; |
| 2251 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2252 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2253 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2254 | constantVector[i] = llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2255 | } |
| 2256 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2257 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2258 | } |
| 2259 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2260 | Type *Void::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2261 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2262 | return T(llvm::Type::getVoidTy(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2263 | } |
| 2264 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2265 | Type *Bool::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2266 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2267 | return T(llvm::Type::getInt1Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2268 | } |
| 2269 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2270 | Type *Byte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2271 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2272 | return T(llvm::Type::getInt8Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2273 | } |
| 2274 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2275 | Type *SByte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2276 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2277 | return T(llvm::Type::getInt8Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2278 | } |
| 2279 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2280 | Type *Short::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2281 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2282 | return T(llvm::Type::getInt16Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2283 | } |
| 2284 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2285 | Type *UShort::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2286 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2287 | return T(llvm::Type::getInt16Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2288 | } |
| 2289 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2290 | Type *Byte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2291 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2292 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2293 | } |
| 2294 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2295 | Type *SByte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2296 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2297 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2298 | } |
| 2299 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2300 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2301 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2302 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2303 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2304 | return x86::paddusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2305 | #else |
| 2306 | return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 2307 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2308 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2309 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2310 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2311 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2312 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2313 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2314 | return x86::psubusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2315 | #else |
| 2316 | return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 2317 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2318 | } |
| 2319 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2320 | RValue<Int> SignMask(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2321 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2322 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2323 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2324 | return x86::pmovmskb(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2325 | #else |
| 2326 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2327 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2328 | } |
| 2329 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2330 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2331 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2332 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2333 | // return x86::pcmpgtb(x, y); // FIXME: Signedness |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2334 | //#else |
| 2335 | // return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2336 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2337 | // } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2338 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2339 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2340 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2341 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2342 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2343 | return x86::pcmpeqb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2344 | #else |
| 2345 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2346 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2347 | } |
| 2348 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2349 | Type *Byte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2350 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2351 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2352 | } |
| 2353 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2354 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
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(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2357 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2358 | return x86::paddsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2359 | #else |
| 2360 | return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 2361 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2362 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2363 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2364 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2365 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2366 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2367 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2368 | return x86::psubsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2369 | #else |
| 2370 | return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 2371 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2372 | } |
| 2373 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2374 | RValue<Int> SignMask(RValue<SByte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2375 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2376 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2377 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2378 | return x86::pmovmskb(As<Byte8>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2379 | #else |
| 2380 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2381 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2382 | } |
| 2383 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2384 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2385 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2386 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2387 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2388 | return x86::pcmpgtb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2389 | #else |
| 2390 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2391 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2392 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2393 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2394 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2395 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2396 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2397 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2398 | return x86::pcmpeqb(As<Byte8>(x), As<Byte8>(y)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2399 | #else |
| 2400 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2401 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2402 | } |
| 2403 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2404 | Type *SByte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2405 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2406 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2407 | } |
| 2408 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2409 | Type *Byte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2410 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2411 | return T(llvm::VectorType::get(T(Byte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2412 | } |
| 2413 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2414 | Type *SByte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2415 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2416 | return T(llvm::VectorType::get(T(SByte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2417 | } |
| 2418 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2419 | Type *Short2::getType() |
| 2420 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2421 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2422 | } |
| 2423 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2424 | Type *UShort2::getType() |
| 2425 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2426 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2427 | } |
| 2428 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2429 | Short4::Short4(RValue<Int4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2430 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2431 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2432 | int select[8] = {0, 2, 4, 6, 0, 2, 4, 6}; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2433 | Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType()); |
| 2434 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2435 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2436 | Value *short4 = As<Short4>(Int2(As<Int4>(packed))).value; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2437 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2438 | storeValue(short4); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2439 | } |
| 2440 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2441 | // Short4::Short4(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2442 | // { |
| 2443 | // } |
| 2444 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2445 | Short4::Short4(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2446 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2447 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2448 | Int4 v4i32 = Int4(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2449 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2450 | v4i32 = As<Int4>(x86::packssdw(v4i32, v4i32)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2451 | #else |
| 2452 | Value *v = v4i32.loadValue(); |
| 2453 | v4i32 = As<Int4>(V(lowerPack(V(v), V(v), true))); |
| 2454 | #endif |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2455 | |
| 2456 | storeValue(As<Short4>(Int2(v4i32)).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2457 | } |
| 2458 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2459 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2460 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2461 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2462 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2463 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2464 | |
| 2465 | return x86::psllw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2466 | #else |
| 2467 | return As<Short4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2468 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2469 | } |
| 2470 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2471 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2472 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2473 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2474 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2475 | return x86::psraw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2476 | #else |
| 2477 | return As<Short4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2478 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2479 | } |
| 2480 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2481 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2482 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2483 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2484 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2485 | return x86::pmaxsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2486 | #else |
| 2487 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
| 2488 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2489 | } |
| 2490 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2491 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2492 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2493 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2494 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2495 | return x86::pminsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2496 | #else |
| 2497 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
| 2498 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2499 | } |
| 2500 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2501 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2502 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2503 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2504 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2505 | return x86::paddsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2506 | #else |
| 2507 | return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 2508 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2509 | } |
| 2510 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2511 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2512 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2513 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2514 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2515 | return x86::psubsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2516 | #else |
| 2517 | return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 2518 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2519 | } |
| 2520 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2521 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2522 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2523 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2524 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2525 | return x86::pmulhw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2526 | #else |
| 2527 | return As<Short4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2528 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2529 | } |
| 2530 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2531 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2532 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2533 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2534 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2535 | return x86::pmaddwd(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2536 | #else |
| 2537 | return As<Int2>(V(lowerMulAdd(V(x.value), V(y.value)))); |
| 2538 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2539 | } |
| 2540 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2541 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2542 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2543 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2544 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2545 | auto result = x86::packsswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2546 | #else |
| 2547 | auto result = V(lowerPack(V(x.value), V(y.value), true)); |
| 2548 | #endif |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2549 | return As<SByte8>(Swizzle(As<Int4>(result), 0x88)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2550 | } |
| 2551 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2552 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2553 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2554 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2555 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2556 | auto result = x86::packuswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2557 | #else |
| 2558 | auto result = V(lowerPack(V(x.value), V(y.value), false)); |
| 2559 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2560 | return As<Byte8>(Swizzle(As<Int4>(result), 0x88)); |
| 2561 | } |
| 2562 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2563 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2564 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2565 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2566 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2567 | return x86::pcmpgtw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2568 | #else |
| 2569 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); |
| 2570 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2571 | } |
| 2572 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2573 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2574 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2575 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2576 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2577 | return x86::pcmpeqw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2578 | #else |
| 2579 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); |
| 2580 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2581 | } |
| 2582 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2583 | Type *Short4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2584 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2585 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2586 | } |
| 2587 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2588 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2589 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2590 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2591 | if(saturate) |
| 2592 | { |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 2593 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2594 | if(CPUID::supportsSSE4_1()) |
| 2595 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2596 | Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2597 | *this = As<Short4>(PackUnsigned(int4, int4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2598 | } |
| 2599 | else |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 2600 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2601 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2602 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | else |
| 2606 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2607 | *this = Short4(Int4(cast)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2608 | } |
| 2609 | } |
| 2610 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2611 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2612 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2613 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2614 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2615 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2616 | |
| 2617 | return As<UShort4>(x86::psllw(As<Short4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2618 | #else |
| 2619 | return As<UShort4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2620 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2621 | } |
| 2622 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2623 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 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 | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2627 | // return RValue<Short4>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 2628 | |
| 2629 | return x86::psrlw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2630 | #else |
| 2631 | return As<UShort4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2632 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2633 | } |
| 2634 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2635 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2636 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2637 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2638 | 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] | 2639 | } |
| 2640 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2641 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2642 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2643 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2644 | 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] | 2645 | } |
| 2646 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2647 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2648 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2649 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2650 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2651 | return x86::paddusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2652 | #else |
| 2653 | return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 2654 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2655 | } |
| 2656 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2657 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2658 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2659 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2660 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2661 | return x86::psubusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2662 | #else |
| 2663 | return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 2664 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2665 | } |
| 2666 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2667 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2668 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2669 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2670 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2671 | return x86::pmulhuw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2672 | #else |
| 2673 | return As<UShort4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2674 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2675 | } |
| 2676 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2677 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2678 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2679 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2680 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2681 | return x86::pavgw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2682 | #else |
| 2683 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
| 2684 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2685 | } |
| 2686 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2687 | Type *UShort4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2688 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2689 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2690 | } |
| 2691 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2692 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2693 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2694 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2695 | #if defined(__i386__) || defined(__x86_64__) |
| 2696 | return x86::psllw(lhs, rhs); |
| 2697 | #else |
| 2698 | return As<Short8>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2699 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2700 | } |
| 2701 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2702 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2703 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2704 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2705 | #if defined(__i386__) || defined(__x86_64__) |
| 2706 | return x86::psraw(lhs, rhs); |
| 2707 | #else |
| 2708 | return As<Short8>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2709 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2710 | } |
| 2711 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2712 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2713 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2714 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2715 | #if defined(__i386__) || defined(__x86_64__) |
| 2716 | return x86::pmaddwd(x, y); |
| 2717 | #else |
| 2718 | return As<Int4>(V(lowerMulAdd(V(x.value), V(y.value)))); |
| 2719 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2720 | } |
| 2721 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2722 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2723 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2724 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2725 | #if defined(__i386__) || defined(__x86_64__) |
| 2726 | return x86::pmulhw(x, y); |
| 2727 | #else |
| 2728 | return As<Short8>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2729 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2730 | } |
| 2731 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2732 | Type *Short8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2733 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2734 | return T(llvm::VectorType::get(T(Short::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2735 | } |
| 2736 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2737 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2738 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2739 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2740 | #if defined(__i386__) || defined(__x86_64__) |
| 2741 | return As<UShort8>(x86::psllw(As<Short8>(lhs), rhs)); |
| 2742 | #else |
| 2743 | return As<UShort8>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2744 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2745 | } |
| 2746 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2747 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2748 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2749 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2750 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2751 | return x86::psrlw(lhs, rhs); // FIXME: Fallback required |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2752 | #else |
| 2753 | return As<UShort8>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2754 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2755 | } |
| 2756 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2757 | 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] | 2758 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2759 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2760 | int pshufb[16] = |
| 2761 | { |
| 2762 | select0 + 0, |
| 2763 | select0 + 1, |
| 2764 | select1 + 0, |
| 2765 | select1 + 1, |
| 2766 | select2 + 0, |
| 2767 | select2 + 1, |
| 2768 | select3 + 0, |
| 2769 | select3 + 1, |
| 2770 | select4 + 0, |
| 2771 | select4 + 1, |
| 2772 | select5 + 0, |
| 2773 | select5 + 1, |
| 2774 | select6 + 0, |
| 2775 | select6 + 1, |
| 2776 | select7 + 0, |
| 2777 | select7 + 1, |
| 2778 | }; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2779 | |
| 2780 | Value *byte16 = Nucleus::createBitCast(x.value, Byte16::getType()); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2781 | Value *shuffle = Nucleus::createShuffleVector(byte16, byte16, pshufb); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2782 | Value *short8 = Nucleus::createBitCast(shuffle, UShort8::getType()); |
| 2783 | |
| 2784 | return RValue<UShort8>(short8); |
| 2785 | } |
| 2786 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2787 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2788 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2789 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2790 | #if defined(__i386__) || defined(__x86_64__) |
| 2791 | return x86::pmulhuw(x, y); |
| 2792 | #else |
| 2793 | return As<UShort8>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2794 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2795 | } |
| 2796 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2797 | Type *UShort8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2798 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2799 | return T(llvm::VectorType::get(T(UShort::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2800 | } |
| 2801 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2802 | RValue<Int> operator++(Int &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2803 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2804 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2805 | RValue<Int> res = val; |
| 2806 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2807 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2808 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2809 | |
| 2810 | return res; |
| 2811 | } |
| 2812 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2813 | const Int &operator++(Int &val) // Pre-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2814 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2815 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2816 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2817 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2818 | |
| 2819 | return val; |
| 2820 | } |
| 2821 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2822 | RValue<Int> operator--(Int &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2823 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2824 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2825 | RValue<Int> res = val; |
| 2826 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2827 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2828 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2829 | |
| 2830 | return res; |
| 2831 | } |
| 2832 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2833 | const Int &operator--(Int &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2834 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2835 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2836 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2837 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2838 | |
| 2839 | return val; |
| 2840 | } |
| 2841 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2842 | RValue<Int> RoundInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2843 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2844 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2845 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2846 | return x86::cvtss2si(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2847 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 2848 | return RValue<Int>(V(lowerRoundInt(V(cast.value), T(Int::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2849 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2850 | } |
| 2851 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2852 | Type *Int::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2853 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2854 | return T(llvm::Type::getInt32Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2855 | } |
| 2856 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2857 | Type *Long::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2858 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2859 | return T(llvm::Type::getInt64Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2860 | } |
| 2861 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2862 | UInt::UInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2863 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2864 | RR_DEBUG_INFO_UPDATE_LOC(); |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2865 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 2866 | // Value *integer = Nucleus::createFPToUI(cast.value, UInt::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2867 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2868 | // Smallest positive value representable in UInt, but not in Int |
| 2869 | const unsigned int ustart = 0x80000000u; |
| 2870 | const float ustartf = float(ustart); |
| 2871 | |
| 2872 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2873 | storeValue((~(As<Int>(cast) >> 31) & |
| 2874 | // Check if the value can be represented as an Int |
| 2875 | IfThenElse(cast >= ustartf, |
| 2876 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2877 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 2878 | // Otherwise, just convert normally |
| 2879 | Int(cast))).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2880 | } |
| 2881 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2882 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2883 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2884 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2885 | RValue<UInt> res = val; |
| 2886 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2887 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2888 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2889 | |
| 2890 | return res; |
| 2891 | } |
| 2892 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2893 | const UInt &operator++(UInt &val) // Pre-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2894 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2895 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2896 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2897 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2898 | |
| 2899 | return val; |
| 2900 | } |
| 2901 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2902 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2903 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2904 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2905 | RValue<UInt> res = val; |
| 2906 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2907 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2908 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2909 | |
| 2910 | return res; |
| 2911 | } |
| 2912 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2913 | const UInt &operator--(UInt &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2914 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2915 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2916 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2917 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2918 | |
| 2919 | return val; |
| 2920 | } |
| 2921 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2922 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2923 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2924 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2925 | // return x86::cvtss2si(val); // FIXME: Unsigned |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2926 | //#else |
| 2927 | // return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f)); |
| 2928 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2929 | // } |
| 2930 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2931 | Type *UInt::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2932 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2933 | return T(llvm::Type::getInt32Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2934 | } |
| 2935 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2936 | // Int2::Int2(RValue<Int> cast) |
| 2937 | // { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2938 | // Value *extend = Nucleus::createZExt(cast.value, Long::getType()); |
| 2939 | // Value *vector = Nucleus::createBitCast(extend, Int2::getType()); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2940 | // |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2941 | // int shuffle[2] = {0, 0}; |
| 2942 | // Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2943 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2944 | // storeValue(replicate); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2945 | // } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2946 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2947 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
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 | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2951 | // return RValue<Int2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2952 | |
| 2953 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2954 | #else |
| 2955 | return As<Int2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2956 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2957 | } |
| 2958 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2959 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2960 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2961 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2962 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2963 | // return RValue<Int2>(Nucleus::createAShr(lhs.value, rhs.value)); |
| 2964 | |
| 2965 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2966 | #else |
| 2967 | return As<Int2>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2968 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2969 | } |
| 2970 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2971 | Type *Int2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2972 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2973 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2974 | } |
| 2975 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2976 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2977 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2978 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2979 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2980 | // return RValue<UInt2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2981 | |
| 2982 | return As<UInt2>(x86::pslld(As<Int2>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2983 | #else |
| 2984 | return As<UInt2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2985 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2986 | } |
| 2987 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2988 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2989 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2990 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2991 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2992 | // return RValue<UInt2>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 2993 | |
| 2994 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2995 | #else |
| 2996 | return As<UInt2>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2997 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2998 | } |
| 2999 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3000 | Type *UInt2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3001 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3002 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3003 | } |
| 3004 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3005 | Int4::Int4(RValue<Byte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3006 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3007 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3008 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3009 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3010 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3011 | *this = x86::pmovzxbd(As<Byte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3012 | } |
| 3013 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3014 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3015 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3016 | 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] | 3017 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 3018 | Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::getType()), swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3019 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3020 | int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3021 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 3022 | Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::getType()), swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3023 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3024 | *this = As<Int4>(d); |
| 3025 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3026 | } |
| 3027 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3028 | Int4::Int4(RValue<SByte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3029 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3030 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3031 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3032 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3033 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3034 | *this = x86::pmovsxbd(As<SByte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3035 | } |
| 3036 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3037 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3038 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3039 | int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; |
| 3040 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
| 3041 | Value *b = Nucleus::createShuffleVector(a, a, swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3042 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3043 | int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3044 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
| 3045 | Value *d = Nucleus::createShuffleVector(c, c, swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3046 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3047 | *this = As<Int4>(d) >> 24; |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3048 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3049 | } |
| 3050 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3051 | Int4::Int4(RValue<Short4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3052 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3053 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3054 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3055 | if(CPUID::supportsSSE4_1()) |
| 3056 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3057 | *this = x86::pmovsxwd(As<Short8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3058 | } |
| 3059 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3060 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3061 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3062 | int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3063 | Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle); |
| 3064 | *this = As<Int4>(c) >> 16; |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3065 | } |
| 3066 | } |
| 3067 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3068 | Int4::Int4(RValue<UShort4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3069 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3070 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3071 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3072 | if(CPUID::supportsSSE4_1()) |
| 3073 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3074 | *this = x86::pmovzxwd(As<UShort8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3075 | } |
| 3076 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3077 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3078 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3079 | int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3080 | Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
| 3081 | *this = As<Int4>(c); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3085 | Int4::Int4(RValue<Int> rhs) : XYZW(this) |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 3086 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3087 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 3088 | Value *vector = loadValue(); |
| 3089 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 3090 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3091 | int swizzle[4] = {0, 0, 0, 0}; |
| 3092 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 3093 | |
| 3094 | storeValue(replicate); |
| 3095 | } |
| 3096 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3097 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3098 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3099 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3100 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3101 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3102 | #else |
| 3103 | return As<Int4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 3104 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3105 | } |
| 3106 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3107 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3108 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3109 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3110 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3111 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3112 | #else |
| 3113 | return As<Int4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 3114 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3115 | } |
| 3116 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3117 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 3118 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3119 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3120 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 3124 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3125 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3126 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 3130 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3131 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3132 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 3136 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3137 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3138 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 3142 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3143 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3144 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3148 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3149 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3150 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3154 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3155 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3156 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3157 | if(CPUID::supportsSSE4_1()) |
| 3158 | { |
| 3159 | return x86::pmaxsd(x, y); |
| 3160 | } |
| 3161 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3162 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3163 | { |
| 3164 | RValue<Int4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3165 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3170 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3171 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3172 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3173 | if(CPUID::supportsSSE4_1()) |
| 3174 | { |
| 3175 | return x86::pminsd(x, y); |
| 3176 | } |
| 3177 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3178 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3179 | { |
| 3180 | RValue<Int4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3181 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | RValue<Int4> RoundInt(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3186 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3187 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3188 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3189 | return x86::cvtps2dq(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3190 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 3191 | return As<Int4>(V(lowerRoundInt(V(cast.value), T(Int4::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3192 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3193 | } |
| 3194 | |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3195 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 3196 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3197 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3198 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3199 | return As<Int4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 3200 | } |
| 3201 | |
| 3202 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 3203 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3204 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3205 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3206 | return As<UInt4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 3207 | } |
| 3208 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3209 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3210 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3211 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3212 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3213 | return x86::packssdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3214 | #else |
| 3215 | return As<Short8>(V(lowerPack(V(x.value), V(y.value), true))); |
| 3216 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3217 | } |
| 3218 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3219 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3220 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3221 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3222 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3223 | return x86::packusdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3224 | #else |
| 3225 | return As<UShort8>(V(lowerPack(V(x.value), V(y.value), false))); |
| 3226 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3227 | } |
| 3228 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3229 | RValue<Int> SignMask(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3230 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3231 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3232 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3233 | return x86::movmskps(As<Float4>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3234 | #else |
| 3235 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 3236 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3237 | } |
| 3238 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3239 | Type *Int4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3240 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3241 | return T(llvm::VectorType::get(T(Int::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3242 | } |
| 3243 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3244 | UInt4::UInt4(RValue<Float4> cast) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3245 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3246 | RR_DEBUG_INFO_UPDATE_LOC(); |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 3247 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 3248 | // Value *xyzw = Nucleus::createFPToUI(cast.value, UInt4::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3249 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 3250 | // Smallest positive value representable in UInt, but not in Int |
| 3251 | const unsigned int ustart = 0x80000000u; |
| 3252 | const float ustartf = float(ustart); |
| 3253 | |
| 3254 | // Check if the value can be represented as an Int |
| 3255 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 3256 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3257 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
| 3258 | // Otherwise, just convert normally |
| 3259 | (~uiValue & Int4(cast)); |
| 3260 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 3261 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3262 | } |
| 3263 | |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3264 | UInt4::UInt4(RValue<UInt> rhs) : XYZW(this) |
| 3265 | { |
| 3266 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3267 | Value *vector = loadValue(); |
| 3268 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 3269 | |
| 3270 | int swizzle[4] = {0, 0, 0, 0}; |
| 3271 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
| 3272 | |
| 3273 | storeValue(replicate); |
| 3274 | } |
| 3275 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3276 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3277 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3278 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3279 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3280 | return As<UInt4>(x86::pslld(As<Int4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3281 | #else |
| 3282 | return As<UInt4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 3283 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3284 | } |
| 3285 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3286 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3287 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3288 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3289 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3290 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3291 | #else |
| 3292 | return As<UInt4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 3293 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3294 | } |
| 3295 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3296 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3297 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3298 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3299 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3303 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3304 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3305 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType())); |
| 3306 | } |
| 3307 | |
| 3308 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3309 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3310 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3311 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3315 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3316 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3317 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
| 3318 | } |
| 3319 | |
| 3320 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3321 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3322 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3323 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGE(x.value, y.value), Int4::getType())); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3327 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3328 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3329 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType())); |
| 3330 | } |
| 3331 | |
| 3332 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3333 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3334 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3335 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3336 | if(CPUID::supportsSSE4_1()) |
| 3337 | { |
| 3338 | return x86::pmaxud(x, y); |
| 3339 | } |
| 3340 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3341 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3342 | { |
| 3343 | RValue<UInt4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3344 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3345 | } |
| 3346 | } |
| 3347 | |
| 3348 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3349 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3350 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3351 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3352 | if(CPUID::supportsSSE4_1()) |
| 3353 | { |
| 3354 | return x86::pminud(x, y); |
| 3355 | } |
| 3356 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3357 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3358 | { |
| 3359 | RValue<UInt4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3360 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3364 | Type *UInt4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3365 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3366 | return T(llvm::VectorType::get(T(UInt::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3367 | } |
| 3368 | |
Alexis Hetu | 734e257 | 2018-12-20 14:00:49 -0500 | [diff] [blame] | 3369 | Type *Half::getType() |
| 3370 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3371 | return T(llvm::Type::getInt16Ty(jit->context)); |
Alexis Hetu | 734e257 | 2018-12-20 14:00:49 -0500 | [diff] [blame] | 3372 | } |
| 3373 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 3374 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3375 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3376 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3377 | #if defined(__i386__) || defined(__x86_64__) |
| 3378 | if(exactAtPow2) |
| 3379 | { |
| 3380 | // rcpss uses a piecewise-linear approximation which minimizes the relative error |
| 3381 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 3382 | return x86::rcpss(x) * Float(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 3383 | } |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 3384 | return x86::rcpss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3385 | #else |
| 3386 | return As<Float>(V(lowerRCP(V(x.value)))); |
| 3387 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3388 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3389 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3390 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3391 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3392 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3393 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3394 | return x86::rsqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3395 | #else |
| 3396 | return As<Float>(V(lowerRSQRT(V(x.value)))); |
| 3397 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3398 | } |
| 3399 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3400 | RValue<Float> Sqrt(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3401 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3402 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3403 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3404 | return x86::sqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3405 | #else |
| 3406 | return As<Float>(V(lowerSQRT(V(x.value)))); |
| 3407 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3408 | } |
| 3409 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3410 | RValue<Float> Round(RValue<Float> x) |
| 3411 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3412 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3413 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3414 | if(CPUID::supportsSSE4_1()) |
| 3415 | { |
| 3416 | return x86::roundss(x, 0); |
| 3417 | } |
| 3418 | else |
| 3419 | { |
| 3420 | return Float4(Round(Float4(x))).x; |
| 3421 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 3422 | #else |
| 3423 | return RValue<Float>(V(lowerRound(V(x.value)))); |
| 3424 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | RValue<Float> Trunc(RValue<Float> x) |
| 3428 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3429 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3430 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3431 | if(CPUID::supportsSSE4_1()) |
| 3432 | { |
| 3433 | return x86::roundss(x, 3); |
| 3434 | } |
| 3435 | else |
| 3436 | { |
| 3437 | return Float(Int(x)); // Rounded toward zero |
| 3438 | } |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 3439 | #else |
| 3440 | return RValue<Float>(V(lowerTrunc(V(x.value)))); |
| 3441 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | RValue<Float> Frac(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3445 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3446 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3447 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3448 | if(CPUID::supportsSSE4_1()) |
| 3449 | { |
| 3450 | return x - x86::floorss(x); |
| 3451 | } |
| 3452 | else |
| 3453 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3454 | return Float4(Frac(Float4(x))).x; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3455 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 3456 | #else |
| 3457 | // x - floor(x) can be 1.0 for very small negative x. |
| 3458 | // Clamp against the value just below 1.0. |
| 3459 | return Min(x - Floor(x), As<Float>(Int(0x3F7FFFFF))); |
| 3460 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3461 | } |
| 3462 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3463 | RValue<Float> Floor(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3464 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3465 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3466 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3467 | if(CPUID::supportsSSE4_1()) |
| 3468 | { |
| 3469 | return x86::floorss(x); |
| 3470 | } |
| 3471 | else |
| 3472 | { |
| 3473 | return Float4(Floor(Float4(x))).x; |
| 3474 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3475 | #else |
| 3476 | return RValue<Float>(V(lowerFloor(V(x.value)))); |
| 3477 | #endif |
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<Float> Ceil(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3481 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3482 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3483 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3484 | if(CPUID::supportsSSE4_1()) |
| 3485 | { |
| 3486 | return x86::ceilss(x); |
| 3487 | } |
| 3488 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3489 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3490 | { |
| 3491 | return Float4(Ceil(Float4(x))).x; |
| 3492 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3493 | } |
| 3494 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3495 | Type *Float::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3496 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3497 | return T(llvm::Type::getFloatTy(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3498 | } |
| 3499 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3500 | Type *Float2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3501 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3502 | return T(Type_v2f32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3503 | } |
| 3504 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3505 | Float4::Float4(RValue<Float> rhs) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3506 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3507 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3508 | Value *vector = loadValue(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3509 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 3510 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3511 | int swizzle[4] = {0, 0, 0, 0}; |
| 3512 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3513 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3514 | storeValue(replicate); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3515 | } |
| 3516 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3517 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3518 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3519 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3520 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3521 | return x86::maxps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3522 | #else |
| 3523 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OGT))); |
| 3524 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3525 | } |
| 3526 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3527 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3528 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3529 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3530 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3531 | return x86::minps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3532 | #else |
| 3533 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OLT))); |
| 3534 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3535 | } |
| 3536 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 3537 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3538 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3539 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3540 | #if defined(__i386__) || defined(__x86_64__) |
| 3541 | if(exactAtPow2) |
| 3542 | { |
| 3543 | // rcpps uses a piecewise-linear approximation which minimizes the relative error |
| 3544 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 3545 | return x86::rcpps(x) * Float4(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 3546 | } |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 3547 | return x86::rcpps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3548 | #else |
| 3549 | return As<Float4>(V(lowerRCP(V(x.value)))); |
| 3550 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3551 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3552 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3553 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3554 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3555 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3556 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3557 | return x86::rsqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3558 | #else |
| 3559 | return As<Float4>(V(lowerRSQRT(V(x.value)))); |
| 3560 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3561 | } |
| 3562 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3563 | RValue<Float4> Sqrt(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3564 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3565 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3566 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3567 | return x86::sqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3568 | #else |
| 3569 | return As<Float4>(V(lowerSQRT(V(x.value)))); |
| 3570 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3571 | } |
| 3572 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3573 | RValue<Int> SignMask(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3574 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3575 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3576 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3577 | return x86::movmskps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3578 | #else |
| 3579 | return As<Int>(V(lowerFPSignMask(V(x.value), T(Int::getType())))); |
| 3580 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3581 | } |
| 3582 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3583 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3584 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3585 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3586 | // return As<Int4>(x86::cmpeqps(x, y)); |
| 3587 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value, y.value), Int4::getType())); |
| 3588 | } |
| 3589 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3590 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3591 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3592 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3593 | // return As<Int4>(x86::cmpltps(x, y)); |
| 3594 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value, y.value), Int4::getType())); |
| 3595 | } |
| 3596 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3597 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3598 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3599 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3600 | // return As<Int4>(x86::cmpleps(x, y)); |
| 3601 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value, y.value), Int4::getType())); |
| 3602 | } |
| 3603 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3604 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3605 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3606 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3607 | // return As<Int4>(x86::cmpneqps(x, y)); |
| 3608 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value, y.value), Int4::getType())); |
| 3609 | } |
| 3610 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3611 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3612 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3613 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3614 | // return As<Int4>(x86::cmpnltps(x, y)); |
| 3615 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value, y.value), Int4::getType())); |
| 3616 | } |
| 3617 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3618 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3619 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3620 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3621 | // return As<Int4>(x86::cmpnleps(x, y)); |
| 3622 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value, y.value), Int4::getType())); |
| 3623 | } |
| 3624 | |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3625 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 3626 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3627 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3628 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUEQ(x.value, y.value), Int4::getType())); |
| 3629 | } |
| 3630 | |
| 3631 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 3632 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3633 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3634 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULT(x.value, y.value), Int4::getType())); |
| 3635 | } |
| 3636 | |
| 3637 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 3638 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3639 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3640 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULE(x.value, y.value), Int4::getType())); |
| 3641 | } |
| 3642 | |
| 3643 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 3644 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3645 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3646 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUNE(x.value, y.value), Int4::getType())); |
| 3647 | } |
| 3648 | |
| 3649 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 3650 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3651 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3652 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGE(x.value, y.value), Int4::getType())); |
| 3653 | } |
| 3654 | |
| 3655 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 3656 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3657 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3658 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value, y.value), Int4::getType())); |
| 3659 | } |
| 3660 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3661 | RValue<Float4> Round(RValue<Float4> x) |
| 3662 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3663 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3664 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3665 | if(CPUID::supportsSSE4_1()) |
| 3666 | { |
| 3667 | return x86::roundps(x, 0); |
| 3668 | } |
| 3669 | else |
| 3670 | { |
| 3671 | return Float4(RoundInt(x)); |
| 3672 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 3673 | #else |
| 3674 | return RValue<Float4>(V(lowerRound(V(x.value)))); |
| 3675 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | RValue<Float4> Trunc(RValue<Float4> x) |
| 3679 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3680 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3681 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3682 | if(CPUID::supportsSSE4_1()) |
| 3683 | { |
| 3684 | return x86::roundps(x, 3); |
| 3685 | } |
| 3686 | else |
| 3687 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3688 | return Float4(Int4(x)); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3689 | } |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 3690 | #else |
| 3691 | return RValue<Float4>(V(lowerTrunc(V(x.value)))); |
| 3692 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | RValue<Float4> Frac(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3696 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3697 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3698 | Float4 frc; |
| 3699 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3700 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3701 | if(CPUID::supportsSSE4_1()) |
| 3702 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3703 | frc = x - Floor(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3704 | } |
| 3705 | else |
| 3706 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3707 | frc = x - Float4(Int4(x)); // Signed fractional part. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3708 | |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3709 | 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] | 3710 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 3711 | #else |
| 3712 | frc = x - Floor(x); |
| 3713 | #endif |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3714 | |
| 3715 | // x - floor(x) can be 1.0 for very small negative x. |
| 3716 | // Clamp against the value just below 1.0. |
| 3717 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3718 | } |
| 3719 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3720 | RValue<Float4> Floor(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3721 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3722 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3723 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3724 | if(CPUID::supportsSSE4_1()) |
| 3725 | { |
| 3726 | return x86::floorps(x); |
| 3727 | } |
| 3728 | else |
| 3729 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3730 | return x - Frac(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3731 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3732 | #else |
| 3733 | return RValue<Float4>(V(lowerFloor(V(x.value)))); |
| 3734 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3735 | } |
| 3736 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3737 | RValue<Float4> Ceil(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3738 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3739 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3740 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3741 | if(CPUID::supportsSSE4_1()) |
| 3742 | { |
| 3743 | return x86::ceilps(x); |
| 3744 | } |
| 3745 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3746 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3747 | { |
| 3748 | return -Floor(-x); |
| 3749 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3750 | } |
| 3751 | |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 3752 | RValue<Float4> Sin(RValue<Float4> v) |
| 3753 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3754 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::sin, { V(v.value)->getType() } ); |
| 3755 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 3756 | } |
| 3757 | |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 3758 | RValue<Float4> Cos(RValue<Float4> v) |
| 3759 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3760 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cos, { V(v.value)->getType() } ); |
| 3761 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 3762 | } |
| 3763 | |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 3764 | RValue<Float4> Tan(RValue<Float4> v) |
| 3765 | { |
| 3766 | return Sin(v) / Cos(v); |
| 3767 | } |
| 3768 | |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 3769 | static RValue<Float4> TransformFloat4PerElement(RValue<Float4> v, const char* name) |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3770 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3771 | auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), ::llvm::ArrayRef<llvm::Type*>(T(Float::getType())), false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3772 | auto func = jit->module->getOrInsertFunction(name, funcTy); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3773 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
| 3774 | for (uint64_t i = 0; i < 4; i++) |
| 3775 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3776 | auto el = jit->builder->CreateCall(func, V(Nucleus::createExtractElement(v.value, Float::getType(), i))); |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3777 | out = V(Nucleus::createInsertElement(V(out), V(el), i)); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3778 | } |
| 3779 | return RValue<Float4>(V(out)); |
| 3780 | } |
| 3781 | |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 3782 | RValue<Float4> Asin(RValue<Float4> v) |
| 3783 | { |
| 3784 | return TransformFloat4PerElement(v, "asinf"); |
| 3785 | } |
| 3786 | |
| 3787 | RValue<Float4> Acos(RValue<Float4> v) |
| 3788 | { |
| 3789 | return TransformFloat4PerElement(v, "acosf"); |
| 3790 | } |
| 3791 | |
Ben Clayton | 749b4e0 | 2019-04-09 14:27:43 -0400 | [diff] [blame] | 3792 | RValue<Float4> Atan(RValue<Float4> v) |
| 3793 | { |
| 3794 | return TransformFloat4PerElement(v, "atanf"); |
| 3795 | } |
| 3796 | |
Ben Clayton | d963697 | 2019-04-09 15:09:54 -0400 | [diff] [blame] | 3797 | RValue<Float4> Sinh(RValue<Float4> v) |
| 3798 | { |
| 3799 | return TransformFloat4PerElement(v, "sinhf"); |
| 3800 | } |
| 3801 | |
Ben Clayton | 900ea2c | 2019-04-09 15:25:36 -0400 | [diff] [blame] | 3802 | RValue<Float4> Cosh(RValue<Float4> v) |
| 3803 | { |
| 3804 | return TransformFloat4PerElement(v, "coshf"); |
| 3805 | } |
| 3806 | |
Ben Clayton | 3928bd9 | 2019-04-09 15:27:41 -0400 | [diff] [blame] | 3807 | RValue<Float4> Tanh(RValue<Float4> v) |
| 3808 | { |
| 3809 | return TransformFloat4PerElement(v, "tanhf"); |
| 3810 | } |
| 3811 | |
Ben Clayton | f6d77ab | 2019-04-09 15:30:04 -0400 | [diff] [blame] | 3812 | RValue<Float4> Asinh(RValue<Float4> v) |
| 3813 | { |
| 3814 | return TransformFloat4PerElement(v, "asinhf"); |
| 3815 | } |
| 3816 | |
Ben Clayton | 28ebcb0 | 2019-04-09 15:33:38 -0400 | [diff] [blame] | 3817 | RValue<Float4> Acosh(RValue<Float4> v) |
| 3818 | { |
| 3819 | return TransformFloat4PerElement(v, "acoshf"); |
| 3820 | } |
| 3821 | |
Ben Clayton | fa6a539 | 2019-04-09 15:35:24 -0400 | [diff] [blame] | 3822 | RValue<Float4> Atanh(RValue<Float4> v) |
| 3823 | { |
| 3824 | return TransformFloat4PerElement(v, "atanhf"); |
| 3825 | } |
| 3826 | |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3827 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 3828 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3829 | ::llvm::SmallVector<::llvm::Type*, 2> paramTys; |
| 3830 | paramTys.push_back(T(Float::getType())); |
| 3831 | paramTys.push_back(T(Float::getType())); |
| 3832 | auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), paramTys, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3833 | auto func = jit->module->getOrInsertFunction("atan2f", funcTy); |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3834 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
| 3835 | for (uint64_t i = 0; i < 4; i++) |
| 3836 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3837 | auto el = jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3838 | V(Nucleus::createExtractElement(x.value, Float::getType(), i)), |
| 3839 | V(Nucleus::createExtractElement(y.value, Float::getType(), i)) |
| 3840 | )); |
| 3841 | out = V(Nucleus::createInsertElement(V(out), V(el), i)); |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3842 | } |
| 3843 | return RValue<Float4>(V(out)); |
| 3844 | } |
| 3845 | |
Ben Clayton | bfe94f0 | 2019-04-09 15:52:12 -0400 | [diff] [blame] | 3846 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 3847 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3848 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::pow, { T(Float4::getType()) }); |
| 3849 | return RValue<Float4>(V(jit->builder->CreateCall2(func, ARGS(V(x.value), V(y.value))))); |
Ben Clayton | bfe94f0 | 2019-04-09 15:52:12 -0400 | [diff] [blame] | 3850 | } |
| 3851 | |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 3852 | RValue<Float4> Exp(RValue<Float4> v) |
| 3853 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3854 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::exp, { T(Float4::getType()) } ); |
| 3855 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 3856 | } |
| 3857 | |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 3858 | RValue<Float4> Log(RValue<Float4> v) |
| 3859 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3860 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::log, { T(Float4::getType()) } ); |
| 3861 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 3862 | } |
| 3863 | |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 3864 | RValue<Float4> Exp2(RValue<Float4> v) |
| 3865 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3866 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::exp2, { T(Float4::getType()) } ); |
| 3867 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 3868 | } |
| 3869 | |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 3870 | RValue<Float4> Log2(RValue<Float4> v) |
| 3871 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3872 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::log2, { T(Float4::getType()) } ); |
| 3873 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 3874 | } |
| 3875 | |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3876 | RValue<UInt4> Ctlz(RValue<UInt4> v, bool isZeroUndef) |
| 3877 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3878 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::ctlz, { T(UInt4::getType()) } ); |
| 3879 | return RValue<UInt4>(V(jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3880 | V(v.value), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3881 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context) |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3882 | )))); |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3883 | } |
| 3884 | |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3885 | RValue<UInt4> Cttz(RValue<UInt4> v, bool isZeroUndef) |
| 3886 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3887 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cttz, { T(UInt4::getType()) } ); |
| 3888 | return RValue<UInt4>(V(jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3889 | V(v.value), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3890 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context) |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3891 | )))); |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3892 | } |
| 3893 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3894 | Type *Float4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3895 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3896 | return T(llvm::VectorType::get(T(Float::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3897 | } |
| 3898 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3899 | RValue<Long> Ticks() |
| 3900 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3901 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3902 | llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::readcyclecounter); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3903 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3904 | return RValue<Long>(V(jit->builder->CreateCall(rdtsc))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3905 | } |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 3906 | |
| 3907 | RValue<Pointer<Byte>> ConstantPointer(void const * ptr) |
| 3908 | { |
| 3909 | // Note: this should work for 32-bit pointers as well because 'inttoptr' |
| 3910 | // is defined to truncate (and zero extend) if necessary. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3911 | auto ptrAsInt = ::llvm::ConstantInt::get(::llvm::Type::getInt64Ty(jit->context), reinterpret_cast<uintptr_t>(ptr)); |
| 3912 | return RValue<Pointer<Byte>>(V(jit->builder->CreateIntToPtr(ptrAsInt, T(Pointer<Byte>::getType())))); |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 3913 | } |
| 3914 | |
| 3915 | Value* Call(RValue<Pointer<Byte>> fptr, Type* retTy, std::initializer_list<Value*> args, std::initializer_list<Type*> argTys) |
| 3916 | { |
| 3917 | ::llvm::SmallVector<::llvm::Type*, 8> paramTys; |
| 3918 | for (auto ty : argTys) { paramTys.push_back(T(ty)); } |
| 3919 | auto funcTy = ::llvm::FunctionType::get(T(retTy), paramTys, false); |
| 3920 | |
| 3921 | auto funcPtrTy = funcTy->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3922 | auto funcPtr = jit->builder->CreatePointerCast(V(fptr.value), funcPtrTy); |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 3923 | |
| 3924 | ::llvm::SmallVector<::llvm::Value*, 8> arguments; |
| 3925 | for (auto arg : args) { arguments.push_back(V(arg)); } |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3926 | return V(jit->builder->CreateCall(funcPtr, arguments)); |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 3927 | } |
Nicolas Capens | 9770a46 | 2019-06-25 10:47:10 -0400 | [diff] [blame] | 3928 | |
| 3929 | void Breakpoint() |
| 3930 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3931 | llvm::Function *debugtrap = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::debugtrap); |
Nicolas Capens | 9770a46 | 2019-06-25 10:47:10 -0400 | [diff] [blame] | 3932 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3933 | jit->builder->CreateCall(debugtrap); |
Nicolas Capens | 9770a46 | 2019-06-25 10:47:10 -0400 | [diff] [blame] | 3934 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3935 | } |
| 3936 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 3937 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3938 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3939 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3940 | namespace x86 |
| 3941 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3942 | RValue<Int> cvtss2si(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3943 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3944 | llvm::Function *cvtss2si = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_cvtss2si); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3945 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3946 | Float4 vector; |
| 3947 | vector.x = val; |
| 3948 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3949 | return RValue<Int>(V(jit->builder->CreateCall(cvtss2si, ARGS(V(RValue<Float4>(vector).value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3950 | } |
| 3951 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3952 | RValue<Int4> cvtps2dq(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3953 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3954 | llvm::Function *cvtps2dq = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_cvtps2dq); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3955 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3956 | return RValue<Int4>(V(jit->builder->CreateCall(cvtps2dq, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3957 | } |
| 3958 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3959 | RValue<Float> rcpss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3960 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3961 | llvm::Function *rcpss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rcp_ss); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3962 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3963 | 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] | 3964 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3965 | return RValue<Float>(Nucleus::createExtractElement(V(jit->builder->CreateCall(rcpss, ARGS(V(vector)))), Float::getType(), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3966 | } |
| 3967 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3968 | RValue<Float> sqrtss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3969 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3970 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::sqrt, {V(val.value)->getType()}); |
| 3971 | return RValue<Float>(V(jit->builder->CreateCall(sqrt, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3972 | } |
| 3973 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3974 | RValue<Float> rsqrtss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3975 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3976 | llvm::Function *rsqrtss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rsqrt_ss); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3977 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3978 | 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] | 3979 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3980 | return RValue<Float>(Nucleus::createExtractElement(V(jit->builder->CreateCall(rsqrtss, ARGS(V(vector)))), Float::getType(), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3981 | } |
| 3982 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3983 | RValue<Float4> rcpps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3984 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3985 | llvm::Function *rcpps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rcp_ps); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3986 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3987 | return RValue<Float4>(V(jit->builder->CreateCall(rcpps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3988 | } |
| 3989 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3990 | RValue<Float4> sqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3991 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3992 | llvm::Function *sqrtps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::sqrt, {V(val.value)->getType()}); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3993 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3994 | return RValue<Float4>(V(jit->builder->CreateCall(sqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3995 | } |
| 3996 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3997 | RValue<Float4> rsqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3998 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3999 | llvm::Function *rsqrtps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rsqrt_ps); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4000 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4001 | return RValue<Float4>(V(jit->builder->CreateCall(rsqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4002 | } |
| 4003 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4004 | RValue<Float4> maxps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4005 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4006 | llvm::Function *maxps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_max_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4007 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4008 | return RValue<Float4>(V(jit->builder->CreateCall2(maxps, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4009 | } |
| 4010 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4011 | RValue<Float4> minps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4012 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4013 | llvm::Function *minps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_min_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4014 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4015 | return RValue<Float4>(V(jit->builder->CreateCall2(minps, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4016 | } |
| 4017 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4018 | RValue<Float> roundss(RValue<Float> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4019 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4020 | llvm::Function *roundss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_round_ss); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4021 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4022 | Value *undef = V(llvm::UndefValue::get(T(Float4::getType()))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4023 | Value *vector = Nucleus::createInsertElement(undef, val.value, 0); |
| 4024 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4025 | return RValue<Float>(Nucleus::createExtractElement(V(jit->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] | 4026 | } |
| 4027 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4028 | RValue<Float> floorss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4029 | { |
| 4030 | return roundss(val, 1); |
| 4031 | } |
| 4032 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4033 | RValue<Float> ceilss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4034 | { |
| 4035 | return roundss(val, 2); |
| 4036 | } |
| 4037 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4038 | RValue<Float4> roundps(RValue<Float4> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4039 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4040 | llvm::Function *roundps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_round_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4041 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4042 | return RValue<Float4>(V(jit->builder->CreateCall2(roundps, ARGS(V(val.value), V(Nucleus::createConstantInt(imm)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4043 | } |
| 4044 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4045 | RValue<Float4> floorps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4046 | { |
| 4047 | return roundps(val, 1); |
| 4048 | } |
| 4049 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4050 | RValue<Float4> ceilps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4051 | { |
| 4052 | return roundps(val, 2); |
| 4053 | } |
| 4054 | |
Alexis Hetu | 0f44807 | 2016-03-18 10:56:08 -0400 | [diff] [blame] | 4055 | RValue<Int4> pabsd(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4056 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4057 | return RValue<Int4>(V(lowerPABS(V(x.value)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4058 | } |
| 4059 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4060 | RValue<Short4> paddsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4061 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4062 | llvm::Function *paddsw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_padds_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4063 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4064 | return As<Short4>(V(jit->builder->CreateCall2(paddsw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4065 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4066 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4067 | RValue<Short4> psubsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4068 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4069 | llvm::Function *psubsw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psubs_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4070 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4071 | return As<Short4>(V(jit->builder->CreateCall2(psubsw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4072 | } |
| 4073 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4074 | RValue<UShort4> paddusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4075 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4076 | llvm::Function *paddusw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_paddus_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4077 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4078 | return As<UShort4>(V(jit->builder->CreateCall2(paddusw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4079 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4080 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4081 | RValue<UShort4> psubusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4082 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4083 | llvm::Function *psubusw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psubus_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4084 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4085 | return As<UShort4>(V(jit->builder->CreateCall2(psubusw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4086 | } |
| 4087 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4088 | RValue<SByte8> paddsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4089 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4090 | llvm::Function *paddsb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_padds_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4091 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4092 | return As<SByte8>(V(jit->builder->CreateCall2(paddsb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4093 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4094 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4095 | RValue<SByte8> psubsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4096 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4097 | llvm::Function *psubsb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psubs_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4098 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4099 | return As<SByte8>(V(jit->builder->CreateCall2(psubsb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4100 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4101 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4102 | RValue<Byte8> paddusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4103 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4104 | llvm::Function *paddusb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_paddus_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4105 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4106 | return As<Byte8>(V(jit->builder->CreateCall2(paddusb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4107 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4108 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4109 | RValue<Byte8> psubusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4110 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4111 | llvm::Function *psubusb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psubus_b); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4112 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4113 | return As<Byte8>(V(jit->builder->CreateCall2(psubusb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4114 | } |
| 4115 | |
| 4116 | RValue<UShort4> pavgw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4117 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4118 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4119 | } |
| 4120 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4121 | RValue<Short4> pmaxsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4122 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4123 | 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] | 4124 | } |
| 4125 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4126 | RValue<Short4> pminsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4127 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4128 | 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] | 4129 | } |
| 4130 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4131 | RValue<Short4> pcmpgtw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4132 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4133 | 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] | 4134 | } |
| 4135 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4136 | RValue<Short4> pcmpeqw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4137 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4138 | 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] | 4139 | } |
| 4140 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4141 | RValue<Byte8> pcmpgtb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4142 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4143 | 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] | 4144 | } |
| 4145 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4146 | RValue<Byte8> pcmpeqb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4147 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4148 | 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] | 4149 | } |
| 4150 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4151 | RValue<Short4> packssdw(RValue<Int2> x, RValue<Int2> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4152 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4153 | llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packssdw_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4154 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4155 | return As<Short4>(V(jit->builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4156 | } |
| 4157 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4158 | RValue<Short8> packssdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4159 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4160 | llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packssdw_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4161 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4162 | return RValue<Short8>(V(jit->builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4163 | } |
| 4164 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4165 | RValue<SByte8> packsswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4166 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4167 | llvm::Function *packsswb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packsswb_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4168 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4169 | return As<SByte8>(V(jit->builder->CreateCall2(packsswb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4170 | } |
| 4171 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 4172 | RValue<Byte8> packuswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4173 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4174 | llvm::Function *packuswb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packuswb_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4175 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4176 | return As<Byte8>(V(jit->builder->CreateCall2(packuswb, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4177 | } |
| 4178 | |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 4179 | RValue<UShort8> packusdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4180 | { |
| 4181 | if(CPUID::supportsSSE4_1()) |
| 4182 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4183 | llvm::Function *packusdw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_packusdw); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4184 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4185 | return RValue<UShort8>(V(jit->builder->CreateCall2(packusdw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4186 | } |
| 4187 | else |
| 4188 | { |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 4189 | RValue<Int4> bx = (x & ~(x >> 31)) - Int4(0x8000); |
| 4190 | RValue<Int4> by = (y & ~(y >> 31)) - Int4(0x8000); |
| 4191 | |
| 4192 | return As<UShort8>(packssdw(bx, by) + Short8(0x8000u)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4196 | RValue<UShort4> psrlw(RValue<UShort4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4197 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4198 | llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4199 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4200 | return As<UShort4>(V(jit->builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4201 | } |
| 4202 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4203 | RValue<UShort8> psrlw(RValue<UShort8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4204 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4205 | llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4206 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4207 | return RValue<UShort8>(V(jit->builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4208 | } |
| 4209 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4210 | RValue<Short4> psraw(RValue<Short4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4211 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4212 | llvm::Function *psraw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4213 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4214 | return As<Short4>(V(jit->builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4215 | } |
| 4216 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4217 | RValue<Short8> psraw(RValue<Short8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4218 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4219 | llvm::Function *psraw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4220 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4221 | return RValue<Short8>(V(jit->builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4222 | } |
| 4223 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4224 | RValue<Short4> psllw(RValue<Short4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4225 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4226 | llvm::Function *psllw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4227 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4228 | return As<Short4>(V(jit->builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4229 | } |
| 4230 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4231 | RValue<Short8> psllw(RValue<Short8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4232 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4233 | llvm::Function *psllw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4234 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4235 | return RValue<Short8>(V(jit->builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4236 | } |
| 4237 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4238 | RValue<Int2> pslld(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4239 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4240 | llvm::Function *pslld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4241 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4242 | return As<Int2>(V(jit->builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4243 | } |
| 4244 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4245 | RValue<Int4> pslld(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4246 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4247 | llvm::Function *pslld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4248 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4249 | return RValue<Int4>(V(jit->builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4250 | } |
| 4251 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4252 | RValue<Int2> psrad(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4253 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4254 | llvm::Function *psrad = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4255 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4256 | return As<Int2>(V(jit->builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4257 | } |
| 4258 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4259 | RValue<Int4> psrad(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4260 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4261 | llvm::Function *psrad = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4262 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4263 | return RValue<Int4>(V(jit->builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4264 | } |
| 4265 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4266 | RValue<UInt2> psrld(RValue<UInt2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4267 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4268 | llvm::Function *psrld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4269 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4270 | return As<UInt2>(V(jit->builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4271 | } |
| 4272 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4273 | RValue<UInt4> psrld(RValue<UInt4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4274 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4275 | llvm::Function *psrld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_d); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4276 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4277 | return RValue<UInt4>(V(jit->builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y)))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4278 | } |
| 4279 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4280 | RValue<Int4> pmaxsd(RValue<Int4> x, RValue<Int4> y) |
| 4281 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4282 | 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] | 4283 | } |
| 4284 | |
| 4285 | RValue<Int4> pminsd(RValue<Int4> x, RValue<Int4> y) |
| 4286 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4287 | 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] | 4288 | } |
| 4289 | |
| 4290 | RValue<UInt4> pmaxud(RValue<UInt4> x, RValue<UInt4> y) |
| 4291 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4292 | 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] | 4293 | } |
| 4294 | |
| 4295 | RValue<UInt4> pminud(RValue<UInt4> x, RValue<UInt4> y) |
| 4296 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4297 | 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] | 4298 | } |
| 4299 | |
| 4300 | RValue<Short4> pmulhw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4301 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4302 | llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulh_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4303 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4304 | return As<Short4>(V(jit->builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4305 | } |
| 4306 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4307 | RValue<UShort4> pmulhuw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4308 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4309 | llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulhu_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4310 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4311 | return As<UShort4>(V(jit->builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4312 | } |
| 4313 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4314 | RValue<Int2> pmaddwd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4315 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4316 | llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmadd_wd); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4317 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4318 | return As<Int2>(V(jit->builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4319 | } |
| 4320 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4321 | RValue<Short8> pmulhw(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4322 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4323 | llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulh_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4324 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4325 | return RValue<Short8>(V(jit->builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4326 | } |
| 4327 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4328 | RValue<UShort8> pmulhuw(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4329 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4330 | llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulhu_w); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4331 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4332 | return RValue<UShort8>(V(jit->builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4333 | } |
| 4334 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4335 | RValue<Int4> pmaddwd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4336 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4337 | llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmadd_wd); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4338 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4339 | return RValue<Int4>(V(jit->builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4340 | } |
| 4341 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4342 | RValue<Int> movmskps(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4343 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4344 | llvm::Function *movmskps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_movmsk_ps); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4345 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4346 | return RValue<Int>(V(jit->builder->CreateCall(movmskps, ARGS(V(x.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4347 | } |
| 4348 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4349 | RValue<Int> pmovmskb(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4350 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4351 | llvm::Function *pmovmskb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmovmskb_128); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4352 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4353 | return RValue<Int>(V(jit->builder->CreateCall(pmovmskb, ARGS(V(x.value))))) & 0xFF; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4354 | } |
| 4355 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4356 | RValue<Int4> pmovzxbd(RValue<Byte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4357 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4358 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4359 | } |
| 4360 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4361 | RValue<Int4> pmovsxbd(RValue<SByte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4362 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4363 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4364 | } |
| 4365 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4366 | RValue<Int4> pmovzxwd(RValue<UShort8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4367 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4368 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4369 | } |
| 4370 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4371 | RValue<Int4> pmovsxwd(RValue<Short8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4372 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4373 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4374 | } |
| 4375 | } |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 4376 | #endif // defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4377 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 4378 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4379 | // extractAll returns a vector containing the extracted n scalar value of |
| 4380 | // the vector vec. |
| 4381 | static std::vector<Value*> extractAll(Value* vec, int n) |
| 4382 | { |
| 4383 | std::vector<Value*> elements; |
| 4384 | elements.reserve(n); |
| 4385 | for (int i = 0; i < n; i++) |
| 4386 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4387 | auto el = V(jit->builder->CreateExtractElement(V(vec), i)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4388 | elements.push_back(el); |
| 4389 | } |
| 4390 | return elements; |
| 4391 | } |
| 4392 | |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4393 | // toInt returns all the integer values in vals extended to a native width |
| 4394 | // integer. |
| 4395 | static std::vector<Value*> toInt(const std::vector<Value*>& vals, bool isSigned) |
| 4396 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4397 | auto intTy = ::llvm::Type::getIntNTy(jit->context, sizeof(int) * 8); // Natural integer width. |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4398 | std::vector<Value*> elements; |
| 4399 | elements.reserve(vals.size()); |
| 4400 | for (auto v : vals) |
| 4401 | { |
| 4402 | if (isSigned) |
| 4403 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4404 | elements.push_back(V(jit->builder->CreateSExt(V(v), intTy))); |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4405 | } |
| 4406 | else |
| 4407 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4408 | elements.push_back(V(jit->builder->CreateZExt(V(v), intTy))); |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4409 | } |
| 4410 | } |
| 4411 | return elements; |
| 4412 | } |
| 4413 | |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4414 | // toDouble returns all the float values in vals extended to doubles. |
| 4415 | static std::vector<Value*> toDouble(const std::vector<Value*>& vals) |
| 4416 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4417 | auto doubleTy = ::llvm::Type::getDoubleTy(jit->context); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4418 | std::vector<Value*> elements; |
| 4419 | elements.reserve(vals.size()); |
| 4420 | for (auto v : vals) |
| 4421 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4422 | elements.push_back(V(jit->builder->CreateFPExt(V(v), doubleTy))); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4423 | } |
| 4424 | return elements; |
| 4425 | } |
| 4426 | |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4427 | std::vector<Value*> PrintValue::Ty<Byte4>::val(const RValue<Byte4>& v) { return toInt(extractAll(v.value, 4), false); } |
| 4428 | std::vector<Value*> PrintValue::Ty<Int>::val(const RValue<Int>& v) { return toInt({v.value}, true); } |
| 4429 | std::vector<Value*> PrintValue::Ty<Int2>::val(const RValue<Int2>& v) { return toInt(extractAll(v.value, 2), true); } |
| 4430 | std::vector<Value*> PrintValue::Ty<Int4>::val(const RValue<Int4>& v) { return toInt(extractAll(v.value, 4), true); } |
| 4431 | std::vector<Value*> PrintValue::Ty<UInt>::val(const RValue<UInt>& v) { return toInt({v.value}, false); } |
| 4432 | std::vector<Value*> PrintValue::Ty<UInt2>::val(const RValue<UInt2>& v) { return toInt(extractAll(v.value, 2), false); } |
| 4433 | std::vector<Value*> PrintValue::Ty<UInt4>::val(const RValue<UInt4>& v) { return toInt(extractAll(v.value, 4), false); } |
| 4434 | std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return toInt(extractAll(v.value, 4), true); } |
| 4435 | std::vector<Value*> PrintValue::Ty<UShort4>::val(const RValue<UShort4>& v) { return toInt(extractAll(v.value, 4), false); } |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4436 | std::vector<Value*> PrintValue::Ty<Float>::val(const RValue<Float>& v) { return toDouble({v.value}); } |
| 4437 | std::vector<Value*> PrintValue::Ty<Float4>::val(const RValue<Float4>& v) { return toDouble(extractAll(v.value, 4)); } |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4438 | std::vector<Value*> PrintValue::Ty<const char*>::val(const char* v) { return {V(jit->builder->CreateGlobalStringPtr(v))}; } |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4439 | |
| 4440 | void Printv(const char* function, const char* file, int line, const char* fmt, std::initializer_list<PrintValue> args) |
| 4441 | { |
| 4442 | // LLVM types used below. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4443 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 4444 | auto intTy = ::llvm::Type::getIntNTy(jit->context, sizeof(int) * 8); // Natural integer width. |
| 4445 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(jit->context); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4446 | auto funcTy = ::llvm::FunctionType::get(i32Ty, {i8PtrTy}, true); |
| 4447 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4448 | auto func = jit->module->getOrInsertFunction("printf", funcTy); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4449 | |
| 4450 | // Build the printf format message string. |
| 4451 | std::string str; |
| 4452 | if (file != nullptr) { str += (line > 0) ? "%s:%d " : "%s "; } |
| 4453 | if (function != nullptr) { str += "%s "; } |
| 4454 | str += fmt; |
| 4455 | |
| 4456 | // Perform subsitution on all '{n}' bracketed indices in the format |
| 4457 | // message. |
| 4458 | int i = 0; |
| 4459 | for (const PrintValue& arg : args) |
| 4460 | { |
| 4461 | str = replace(str, "{" + std::to_string(i++) + "}", arg.format); |
| 4462 | } |
| 4463 | |
| 4464 | ::llvm::SmallVector<::llvm::Value*, 8> vals; |
| 4465 | |
| 4466 | // The format message is always the first argument. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4467 | vals.push_back(jit->builder->CreateGlobalStringPtr(str)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4468 | |
| 4469 | // Add optional file, line and function info if provided. |
| 4470 | if (file != nullptr) |
| 4471 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4472 | vals.push_back(jit->builder->CreateGlobalStringPtr(file)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4473 | if (line > 0) |
| 4474 | { |
| 4475 | vals.push_back(::llvm::ConstantInt::get(intTy, line)); |
| 4476 | } |
| 4477 | } |
| 4478 | if (function != nullptr) |
| 4479 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4480 | vals.push_back(jit->builder->CreateGlobalStringPtr(function)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | // Add all format arguments. |
| 4484 | for (const PrintValue& arg : args) |
| 4485 | { |
| 4486 | for (auto val : arg.values) |
| 4487 | { |
| 4488 | vals.push_back(V(val)); |
| 4489 | } |
| 4490 | } |
| 4491 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4492 | jit->builder->CreateCall(func, vals); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4493 | } |
| 4494 | #endif // ENABLE_RR_PRINT |
| 4495 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4496 | void Nop() |
| 4497 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4498 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4499 | auto funcTy = ::llvm::FunctionType::get(voidTy, {}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4500 | auto func = jit->module->getOrInsertFunction("nop", funcTy); |
| 4501 | jit->builder->CreateCall(func); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | void EmitDebugLocation() |
| 4505 | { |
| 4506 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4507 | if (jit->debugInfo != nullptr) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4508 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4509 | jit->debugInfo->EmitLocation(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4510 | } |
| 4511 | #endif // ENABLE_RR_DEBUG_INFO |
| 4512 | } |
| 4513 | |
| 4514 | void EmitDebugVariable(Value* value) |
| 4515 | { |
| 4516 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4517 | if (jit->debugInfo != nullptr) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4518 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4519 | jit->debugInfo->EmitVariable(value); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4520 | } |
| 4521 | #endif // ENABLE_RR_DEBUG_INFO |
| 4522 | } |
| 4523 | |
| 4524 | void FlushDebug() |
| 4525 | { |
| 4526 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4527 | if (jit->debugInfo != nullptr) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4528 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4529 | jit->debugInfo->Flush(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4530 | } |
| 4531 | #endif // ENABLE_RR_DEBUG_INFO |
| 4532 | } |
| 4533 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4534 | } // namespace rr |
| 4535 | |
| 4536 | // ------------------------------ Coroutines ------------------------------ |
| 4537 | |
| 4538 | namespace { |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4539 | // Magic values retuned by llvm.coro.suspend. |
| 4540 | // See: https://llvm.org/docs/Coroutines.html#llvm-coro-suspend-intrinsic |
| 4541 | enum SuspendAction |
| 4542 | { |
| 4543 | SuspendActionSuspend = -1, |
| 4544 | SuspendActionResume = 0, |
| 4545 | SuspendActionDestroy = 1 |
| 4546 | }; |
| 4547 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4548 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4549 | void promoteFunctionToCoroutine() |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4550 | { |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4551 | ASSERT(jit->coroutine.id == nullptr); |
| 4552 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4553 | // Types |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4554 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
| 4555 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 4556 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 4557 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 4558 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(jit->context); |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4559 | auto promiseTy = jit->coroutine.yieldType; |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4560 | auto promisePtrTy = promiseTy->getPointerTo(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4561 | |
| 4562 | // LLVM intrinsics |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4563 | auto coro_id = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_id); |
| 4564 | auto coro_size = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_size, {i32Ty}); |
| 4565 | auto coro_begin = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_begin); |
| 4566 | auto coro_resume = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_resume); |
| 4567 | auto coro_end = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_end); |
| 4568 | auto coro_free = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_free); |
| 4569 | auto coro_destroy = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_destroy); |
| 4570 | auto coro_promise = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_promise); |
| 4571 | auto coro_done = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_done); |
| 4572 | auto coro_suspend = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_suspend); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4573 | |
| 4574 | auto allocFrameTy = ::llvm::FunctionType::get(i8PtrTy, {i32Ty}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4575 | auto allocFrame = jit->module->getOrInsertFunction("coroutine_alloc_frame", allocFrameTy); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4576 | auto freeFrameTy = ::llvm::FunctionType::get(voidTy, {i8PtrTy}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4577 | auto freeFrame = jit->module->getOrInsertFunction("coroutine_free_frame", freeFrameTy); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4578 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4579 | auto oldInsertionPoint = jit->builder->saveIP(); |
| 4580 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4581 | // Build the coroutine_await() function: |
| 4582 | // |
| 4583 | // bool coroutine_await(CoroutineHandle* handle, YieldType* out) |
| 4584 | // { |
| 4585 | // if (llvm.coro.done(handle)) |
| 4586 | // { |
| 4587 | // return false; |
| 4588 | // } |
| 4589 | // else |
| 4590 | // { |
| 4591 | // *value = (T*)llvm.coro.promise(handle); |
| 4592 | // llvm.coro.resume(handle); |
| 4593 | // return true; |
| 4594 | // } |
| 4595 | // } |
| 4596 | // |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4597 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4598 | auto args = jit->coroutine.await->arg_begin(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4599 | auto handle = args++; |
| 4600 | auto outPtr = args++; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4601 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "co_await", jit->coroutine.await)); |
| 4602 | auto doneBlock = llvm::BasicBlock::Create(jit->context, "done", jit->coroutine.await); |
| 4603 | auto resumeBlock = llvm::BasicBlock::Create(jit->context, "resume", jit->coroutine.await); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4604 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4605 | auto done = jit->builder->CreateCall(coro_done, {handle}, "done"); |
| 4606 | jit->builder->CreateCondBr(done, doneBlock, resumeBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4607 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4608 | jit->builder->SetInsertPoint(doneBlock); |
| 4609 | jit->builder->CreateRet(::llvm::ConstantInt::getFalse(i1Ty)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4610 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4611 | jit->builder->SetInsertPoint(resumeBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4612 | auto promiseAlignment = ::llvm::ConstantInt::get(i32Ty, 4); // TODO: Get correct alignment. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4613 | auto promisePtr = jit->builder->CreateCall(coro_promise, {handle, promiseAlignment, ::llvm::ConstantInt::get(i1Ty, 0)}); |
| 4614 | auto promise = jit->builder->CreateLoad(jit->builder->CreatePointerCast(promisePtr, promisePtrTy)); |
| 4615 | jit->builder->CreateStore(promise, outPtr); |
| 4616 | jit->builder->CreateCall(coro_resume, {handle}); |
| 4617 | jit->builder->CreateRet(::llvm::ConstantInt::getTrue(i1Ty)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | // Build the coroutine_destroy() function: |
| 4621 | // |
| 4622 | // void coroutine_destroy(CoroutineHandle* handle) |
| 4623 | // { |
| 4624 | // llvm.coro.destroy(handle); |
| 4625 | // } |
| 4626 | // |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4627 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4628 | auto handle = jit->coroutine.destroy->arg_begin(); |
| 4629 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->coroutine.destroy)); |
| 4630 | jit->builder->CreateCall(coro_destroy, {handle}); |
| 4631 | jit->builder->CreateRetVoid(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | // Begin building the main coroutine_begin() function. |
| 4635 | // |
| 4636 | // CoroutineHandle* coroutine_begin(<Arguments>) |
| 4637 | // { |
| 4638 | // YieldType promise; |
| 4639 | // auto id = llvm.coro.id(0, &promise, nullptr, nullptr); |
| 4640 | // void* frame = coroutine_alloc_frame(llvm.coro.size.i32()); |
| 4641 | // CoroutineHandle *handle = llvm.coro.begin(id, frame); |
| 4642 | // |
| 4643 | // ... <REACTOR CODE> ... |
| 4644 | // |
| 4645 | // end: |
| 4646 | // SuspendAction action = llvm.coro.suspend(none, true /* final */); // <-- RESUME POINT |
| 4647 | // switch (action) |
| 4648 | // { |
| 4649 | // case SuspendActionResume: |
| 4650 | // UNREACHABLE(); // Illegal to resume after final suspend. |
| 4651 | // case SuspendActionDestroy: |
| 4652 | // goto destroy; |
| 4653 | // default: // (SuspendActionSuspend) |
| 4654 | // goto suspend; |
| 4655 | // } |
| 4656 | // |
| 4657 | // destroy: |
| 4658 | // coroutine_free_frame(llvm.coro.free(id, handle)); |
| 4659 | // goto suspend; |
| 4660 | // |
| 4661 | // suspend: |
| 4662 | // llvm.coro.end(handle, false); |
| 4663 | // return handle; |
| 4664 | // } |
| 4665 | // |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4666 | |
| 4667 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4668 | jit->debugInfo = std::unique_ptr<rr::DebugInfo>(new rr::DebugInfo(jit->builder.get(), &jit->context, jit->module.get(), jit->function)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4669 | #endif // ENABLE_RR_DEBUG_INFO |
| 4670 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4671 | jit->coroutine.suspendBlock = llvm::BasicBlock::Create(jit->context, "suspend", jit->function); |
| 4672 | jit->coroutine.endBlock = llvm::BasicBlock::Create(jit->context, "end", jit->function); |
| 4673 | jit->coroutine.destroyBlock = llvm::BasicBlock::Create(jit->context, "destroy", jit->function); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4674 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4675 | jit->builder->SetInsertPoint(jit->coroutine.entryBlock, jit->coroutine.entryBlock->begin()); |
| 4676 | jit->coroutine.promise = jit->builder->CreateAlloca(promiseTy, nullptr, "promise"); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4677 | jit->coroutine.id = jit->builder->CreateCall(coro_id, { |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4678 | ::llvm::ConstantInt::get(i32Ty, 0), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4679 | jit->builder->CreatePointerCast(jit->coroutine.promise, i8PtrTy), |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4680 | ::llvm::ConstantPointerNull::get(i8PtrTy), |
| 4681 | ::llvm::ConstantPointerNull::get(i8PtrTy), |
| 4682 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4683 | auto size = jit->builder->CreateCall(coro_size, {}); |
| 4684 | auto frame = jit->builder->CreateCall(allocFrame, {size}); |
| 4685 | jit->coroutine.handle = jit->builder->CreateCall(coro_begin, {jit->coroutine.id, frame}); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4686 | |
| 4687 | // Build the suspend block |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4688 | jit->builder->SetInsertPoint(jit->coroutine.suspendBlock); |
| 4689 | jit->builder->CreateCall(coro_end, {jit->coroutine.handle, ::llvm::ConstantInt::get(i1Ty, 0)}); |
| 4690 | jit->builder->CreateRet(jit->coroutine.handle); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4691 | |
| 4692 | // Build the end block |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4693 | jit->builder->SetInsertPoint(jit->coroutine.endBlock); |
| 4694 | auto action = jit->builder->CreateCall(coro_suspend, { |
| 4695 | ::llvm::ConstantTokenNone::get(jit->context), |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4696 | ::llvm::ConstantInt::get(i1Ty, 1), // final: true |
| 4697 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4698 | auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4699 | // switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), trapBlock); // TODO: Trap attempting to resume after final suspend |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4700 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4701 | |
| 4702 | // Build the destroy block |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4703 | jit->builder->SetInsertPoint(jit->coroutine.destroyBlock); |
| 4704 | auto memory = jit->builder->CreateCall(coro_free, {jit->coroutine.id, jit->coroutine.handle}); |
| 4705 | jit->builder->CreateCall(freeFrame, {memory}); |
| 4706 | jit->builder->CreateBr(jit->coroutine.suspendBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4707 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4708 | // Switch back to original insert point to continue building the coroutine. |
| 4709 | jit->builder->restoreIP(oldInsertionPoint); |
| 4710 | } |
| 4711 | |
| 4712 | } // anonymous namespace |
| 4713 | |
| 4714 | namespace rr { |
| 4715 | |
| 4716 | void Nucleus::createCoroutine(Type *YieldType, std::vector<Type*> &Params) |
| 4717 | { |
| 4718 | // Coroutines are initially created as a regular function. |
| 4719 | // Upon the first call to Yield(), the function is promoted to a true |
| 4720 | // coroutine. |
| 4721 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
| 4722 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 4723 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(jit->context); |
| 4724 | auto handleTy = i8PtrTy; |
| 4725 | auto boolTy = i1Ty; |
| 4726 | auto promiseTy = T(YieldType); |
| 4727 | auto promisePtrTy = promiseTy->getPointerTo(); |
| 4728 | |
| 4729 | jit->function = rr::createFunction("coroutine_begin", handleTy, T(Params)); |
| 4730 | jit->coroutine.await = rr::createFunction("coroutine_await", boolTy, {handleTy, promisePtrTy}); |
| 4731 | jit->coroutine.destroy = rr::createFunction("coroutine_destroy", voidTy, {handleTy}); |
| 4732 | jit->coroutine.yieldType = promiseTy; |
| 4733 | jit->coroutine.entryBlock = llvm::BasicBlock::Create(jit->context, "function", jit->function); |
| 4734 | |
| 4735 | jit->builder->SetInsertPoint(jit->coroutine.entryBlock); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4736 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4737 | |
| 4738 | void Nucleus::yield(Value* val) |
| 4739 | { |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4740 | if (jit->coroutine.id == nullptr) |
| 4741 | { |
| 4742 | // First call to yield(). |
| 4743 | // Promote the function to a full coroutine. |
| 4744 | promoteFunctionToCoroutine(); |
| 4745 | ASSERT(jit->coroutine.id != nullptr); |
| 4746 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4747 | |
| 4748 | // promise = val; |
| 4749 | // |
| 4750 | // auto action = llvm.coro.suspend(none, false /* final */); // <-- RESUME POINT |
| 4751 | // switch (action) |
| 4752 | // { |
| 4753 | // case SuspendActionResume: |
| 4754 | // goto resume; |
| 4755 | // case SuspendActionDestroy: |
| 4756 | // goto destroy; |
| 4757 | // default: // (SuspendActionSuspend) |
| 4758 | // goto suspend; |
| 4759 | // } |
| 4760 | // resume: |
| 4761 | // |
| 4762 | |
| 4763 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 4764 | Variable::materializeAll(); |
| 4765 | |
| 4766 | // Types |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4767 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 4768 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4769 | |
| 4770 | // Intrinsics |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4771 | auto coro_suspend = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_suspend); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4772 | |
| 4773 | // Create a block to resume execution. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4774 | auto resumeBlock = llvm::BasicBlock::Create(jit->context, "resume", jit->function); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4775 | |
| 4776 | // Store the promise (yield value) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4777 | jit->builder->CreateStore(V(val), jit->coroutine.promise); |
| 4778 | auto action = jit->builder->CreateCall(coro_suspend, { |
| 4779 | ::llvm::ConstantTokenNone::get(jit->context), |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4780 | ::llvm::ConstantInt::get(i1Ty, 0), // final: true |
| 4781 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4782 | auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4783 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), resumeBlock); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4784 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4785 | |
| 4786 | // Continue building in the resume block. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4787 | jit->builder->SetInsertPoint(resumeBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4788 | } |
| 4789 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 4790 | std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4791 | { |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4792 | bool isCoroutine = jit->coroutine.id != nullptr; |
| 4793 | if (isCoroutine) |
| 4794 | { |
| 4795 | jit->builder->CreateBr(jit->coroutine.endBlock); |
| 4796 | } |
| 4797 | else |
| 4798 | { |
| 4799 | // Coroutine without a Yield acts as a regular function. |
| 4800 | // The 'coroutine_begin' function returns a nullptr for the coroutine |
| 4801 | // handle. |
| 4802 | jit->builder->CreateRet(llvm::Constant::getNullValue(jit->function->getReturnType())); |
| 4803 | // The 'coroutine_await' function always returns false (coroutine done). |
| 4804 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->coroutine.await)); |
| 4805 | jit->builder->CreateRet(llvm::Constant::getNullValue(jit->coroutine.await->getReturnType())); |
| 4806 | // The 'coroutine_destroy' does nothing, returns void. |
| 4807 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->coroutine.destroy)); |
| 4808 | jit->builder->CreateRetVoid(); |
| 4809 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4810 | |
| 4811 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4812 | if (jit->debugInfo != nullptr) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4813 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4814 | jit->debugInfo->Finalize(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4815 | } |
| 4816 | #endif // ENABLE_RR_DEBUG_INFO |
| 4817 | |
| 4818 | if(false) |
| 4819 | { |
| 4820 | std::error_code error; |
| 4821 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4822 | jit->module->print(file, 0); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4823 | } |
| 4824 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4825 | if (isCoroutine) |
| 4826 | { |
| 4827 | // Run manadory coroutine transforms. |
| 4828 | llvm::legacy::PassManager pm; |
| 4829 | pm.add(llvm::createCoroEarlyPass()); |
| 4830 | pm.add(llvm::createCoroSplitPass()); |
| 4831 | pm.add(llvm::createCoroElidePass()); |
| 4832 | pm.add(llvm::createBarrierNoopPass()); |
| 4833 | pm.add(llvm::createCoroCleanupPass()); |
| 4834 | pm.run(*jit->module); |
| 4835 | } |
| 4836 | |
| 4837 | #if defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
| 4838 | { |
| 4839 | llvm::legacy::PassManager pm; |
| 4840 | pm.add(llvm::createVerifierPass()); |
| 4841 | pm.run(*jit->module); |
| 4842 | } |
| 4843 | #endif // defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4844 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 4845 | auto cfg = cfgEdit.apply(jit->config); |
| 4846 | jit->optimize(cfg); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4847 | |
| 4848 | if(false) |
| 4849 | { |
| 4850 | std::error_code error; |
| 4851 | llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4852 | jit->module->print(file, 0); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | llvm::Function *funcs[Nucleus::CoroutineEntryCount]; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4856 | funcs[Nucleus::CoroutineEntryBegin] = jit->function; |
| 4857 | funcs[Nucleus::CoroutineEntryAwait] = jit->coroutine.await; |
| 4858 | funcs[Nucleus::CoroutineEntryDestroy] = jit->coroutine.destroy; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 4859 | auto routine = jit->acquireRoutine(funcs, Nucleus::CoroutineEntryCount, cfg); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4860 | jit.reset(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4861 | |
| 4862 | return routine; |
| 4863 | } |
| 4864 | |
| 4865 | } // namespace rr |