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 | |
Antonio Maiorano | 992bf9d | 2019-10-22 09:41:17 -0400 | [diff] [blame] | 36 | #ifdef _MSC_VER |
| 37 | __pragma(warning(push)) |
| 38 | __pragma(warning(disable : 4146)) // unary minus operator applied to unsigned type, result still unsigned |
| 39 | #endif |
| 40 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 41 | #include "llvm/Analysis/LoopPass.h" |
| 42 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 43 | #include "llvm/ExecutionEngine/JITSymbol.h" |
| 44 | #include "llvm/ExecutionEngine/Orc/CompileUtils.h" |
| 45 | #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
| 46 | #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" |
| 47 | #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" |
| 48 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
| 49 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
| 50 | #include "llvm/IR/Constants.h" |
| 51 | #include "llvm/IR/DataLayout.h" |
| 52 | #include "llvm/IR/Function.h" |
| 53 | #include "llvm/IR/GlobalVariable.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 54 | #include "llvm/IR/Intrinsics.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 55 | #include "llvm/IR/IRBuilder.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 56 | #include "llvm/IR/LegacyPassManager.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 57 | #include "llvm/IR/LLVMContext.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 58 | #include "llvm/IR/Mangler.h" |
| 59 | #include "llvm/IR/Module.h" |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 60 | #include "llvm/IR/Verifier.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 61 | #include "llvm/Support/Error.h" |
| 62 | #include "llvm/Support/TargetSelect.h" |
| 63 | #include "llvm/Target/TargetOptions.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 64 | #include "llvm/Transforms/Coroutines.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 65 | #include "llvm/Transforms/InstCombine/InstCombine.h" |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 66 | #include "llvm/Transforms/IPO.h" |
| 67 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 68 | #include "llvm/Transforms/Scalar.h" |
| 69 | #include "llvm/Transforms/Scalar/GVN.h" |
Ben Clayton | 20507fa | 2019-04-20 01:40:15 -0400 | [diff] [blame] | 70 | |
Ben Clayton | 09a7f45 | 2019-04-25 15:22:43 +0100 | [diff] [blame] | 71 | #if defined(__clang__) |
| 72 | #pragma clang diagnostic pop |
| 73 | #endif // defined(__clang__) |
| 74 | |
Antonio Maiorano | 992bf9d | 2019-10-22 09:41:17 -0400 | [diff] [blame] | 75 | #ifdef _MSC_VER |
| 76 | __pragma(warning(pop)) |
| 77 | #endif |
| 78 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 79 | #define ARGS(...) {__VA_ARGS__} |
| 80 | #define CreateCall2 CreateCall |
| 81 | #define CreateCall3 CreateCall |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 82 | |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 83 | #include <unordered_map> |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 84 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 85 | #include <fstream> |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 86 | #include <iostream> |
| 87 | #include <mutex> |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 88 | #include <numeric> |
| 89 | #include <thread> |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 90 | |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 91 | #if defined(__i386__) || defined(__x86_64__) |
| 92 | #include <xmmintrin.h> |
| 93 | #endif |
| 94 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 95 | #include <math.h> |
| 96 | |
Nicolas Capens | cb12258 | 2014-05-06 23:34:44 -0400 | [diff] [blame] | 97 | #if defined(__x86_64__) && defined(_WIN32) |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 98 | extern "C" void X86CompilationCallback() |
| 99 | { |
| 100 | UNIMPLEMENTED("X86CompilationCallback"); |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | #if defined(_WIN64) |
| 105 | extern "C" void __chkstk(); |
| 106 | #elif defined(_WIN32) |
| 107 | extern "C" void _chkstk(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 108 | #endif |
| 109 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 110 | namespace rr |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 111 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 112 | void* resolveExternalSymbol(const char*); |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 115 | namespace |
| 116 | { |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 117 | // Default configuration settings. Must be accessed under mutex lock. |
| 118 | std::mutex defaultConfigLock; |
| 119 | rr::Config &defaultConfig() |
| 120 | { |
| 121 | // This uses a static in a function to avoid the cost of a global static |
| 122 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 123 | static rr::Config config = rr::Config::Edit() |
| 124 | .set(rr::Optimization::Level::Default) |
| 125 | .add(rr::Optimization::Pass::ScalarReplAggregates) |
| 126 | .add(rr::Optimization::Pass::InstructionCombining) |
| 127 | .apply({}); |
| 128 | return config; |
| 129 | } |
| 130 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 131 | // Cache provides a simple, thread-safe key-value store. |
| 132 | template <typename KEY, typename VALUE> |
| 133 | class Cache |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 134 | { |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 135 | public: |
| 136 | Cache() = default; |
| 137 | Cache(const Cache& other); |
| 138 | VALUE getOrCreate(KEY key, std::function<VALUE()> create); |
| 139 | private: |
| 140 | mutable std::mutex mutex; // mutable required for copy constructor. |
| 141 | std::unordered_map<KEY, VALUE> map; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 142 | }; |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 143 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 144 | template <typename KEY, typename VALUE> |
| 145 | Cache<KEY, VALUE>::Cache(const Cache& other) |
| 146 | { |
| 147 | std::unique_lock<std::mutex> lock(other.mutex); |
| 148 | map = other.map; |
| 149 | } |
| 150 | |
| 151 | template <typename KEY, typename VALUE> |
| 152 | VALUE Cache<KEY, VALUE>::getOrCreate(KEY key, std::function<VALUE()> create) |
| 153 | { |
| 154 | std::unique_lock<std::mutex> lock(mutex); |
| 155 | auto it = map.find(key); |
| 156 | if (it != map.end()) |
| 157 | { |
| 158 | return it->second; |
| 159 | } |
| 160 | auto value = create(); |
| 161 | map.emplace(key, value); |
| 162 | return value; |
| 163 | } |
| 164 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 165 | // JITGlobals is a singleton that holds all the immutable machine specific |
| 166 | // information for the host device. |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 167 | class JITGlobals |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 168 | { |
| 169 | public: |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 170 | using TargetMachineSPtr = std::shared_ptr<llvm::TargetMachine>; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 171 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 172 | static JITGlobals * get(); |
| 173 | |
| 174 | const std::string mcpu; |
| 175 | const std::vector<std::string> mattrs; |
| 176 | const char* const march; |
| 177 | const llvm::TargetOptions targetOptions; |
| 178 | const llvm::DataLayout dataLayout; |
| 179 | |
| 180 | TargetMachineSPtr getTargetMachine(rr::Optimization::Level optlevel); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 181 | |
| 182 | private: |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 183 | static JITGlobals create(); |
| 184 | static llvm::CodeGenOpt::Level toLLVM(rr::Optimization::Level level); |
| 185 | JITGlobals(const char *mcpu, |
| 186 | const std::vector<std::string> &mattrs, |
| 187 | const char *march, |
| 188 | const llvm::TargetOptions &targetOptions, |
| 189 | const llvm::DataLayout &dataLayout); |
| 190 | JITGlobals(const JITGlobals&) = default; |
| 191 | |
| 192 | // The cache key here is actually a rr::Optimization::Level. We use int |
| 193 | // as 'enum class' types do not provide builtin hash functions until |
| 194 | // C++14. See: https://stackoverflow.com/a/29618545. |
| 195 | Cache<int, TargetMachineSPtr> targetMachines; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 196 | }; |
| 197 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 198 | JITGlobals * JITGlobals::get() |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 199 | { |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 200 | static JITGlobals instance = create(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 201 | return &instance; |
| 202 | } |
| 203 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 204 | JITGlobals::TargetMachineSPtr JITGlobals::getTargetMachine(rr::Optimization::Level optlevel) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 205 | { |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 206 | return targetMachines.getOrCreate(static_cast<int>(optlevel), [&]() { |
| 207 | return TargetMachineSPtr(llvm::EngineBuilder() |
| 208 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | e031f36 | 2019-07-20 12:35:40 +0100 | [diff] [blame] | 209 | .setOptLevel(toLLVM(rr::Optimization::Level::None)) |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 210 | #else |
| 211 | .setOptLevel(toLLVM(optlevel)) |
| 212 | #endif // ENABLE_RR_DEBUG_INFO |
| 213 | .setMCPU(mcpu) |
| 214 | .setMArch(march) |
| 215 | .setMAttrs(mattrs) |
| 216 | .setTargetOptions(targetOptions) |
| 217 | .selectTarget()); |
| 218 | }); |
| 219 | } |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 220 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 221 | JITGlobals JITGlobals::create() |
| 222 | { |
| 223 | struct LLVMInitializer |
| 224 | { |
| 225 | LLVMInitializer() |
| 226 | { |
| 227 | llvm::InitializeNativeTarget(); |
| 228 | llvm::InitializeNativeTargetAsmPrinter(); |
| 229 | llvm::InitializeNativeTargetAsmParser(); |
| 230 | } |
| 231 | }; |
| 232 | static LLVMInitializer initializeLLVM; |
| 233 | |
| 234 | auto mcpu = llvm::sys::getHostCPUName(); |
| 235 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 236 | llvm::StringMap<bool> features; |
| 237 | bool ok = llvm::sys::getHostCPUFeatures(features); |
| 238 | |
| 239 | #if defined(__i386__) || defined(__x86_64__) || \ |
| 240 | (defined(__linux__) && (defined(__arm__) || defined(__aarch64__))) |
| 241 | ASSERT_MSG(ok, "llvm::sys::getHostCPUFeatures returned false"); |
| 242 | #else |
| 243 | (void) ok; // getHostCPUFeatures always returns false on other platforms |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 244 | #endif |
| 245 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 246 | std::vector<std::string> mattrs; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 247 | for (auto &feature : features) |
| 248 | { |
| 249 | if (feature.second) { mattrs.push_back(feature.first()); } |
| 250 | } |
| 251 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 252 | const char* march = nullptr; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 253 | #if defined(__x86_64__) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 254 | march = "x86-64"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 255 | #elif defined(__i386__) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 256 | march = "x86"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 257 | #elif defined(__aarch64__) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 258 | march = "arm64"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 259 | #elif defined(__arm__) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 260 | march = "arm"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 261 | #elif defined(__mips__) |
| 262 | #if defined(__mips64) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 263 | march = "mips64el"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 264 | #else |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 265 | march = "mipsel"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 266 | #endif |
| 267 | #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 268 | march = "ppc64le"; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 269 | #else |
| 270 | #error "unknown architecture" |
| 271 | #endif |
| 272 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 273 | llvm::TargetOptions targetOptions; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 274 | targetOptions.UnsafeFPMath = false; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 275 | |
| 276 | auto targetMachine = std::unique_ptr<llvm::TargetMachine>( |
| 277 | llvm::EngineBuilder() |
| 278 | .setOptLevel(llvm::CodeGenOpt::None) |
Ben Clayton | 49f8051 | 2019-07-04 17:30:54 +0100 | [diff] [blame] | 279 | .setMCPU(mcpu) |
| 280 | .setMArch(march) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 281 | .setMAttrs(mattrs) |
| 282 | .setTargetOptions(targetOptions) |
| 283 | .selectTarget()); |
| 284 | |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 285 | auto dataLayout = targetMachine->createDataLayout(); |
| 286 | |
| 287 | return JITGlobals(mcpu.data(), mattrs, march, targetOptions, dataLayout); |
| 288 | } |
| 289 | |
| 290 | llvm::CodeGenOpt::Level JITGlobals::toLLVM(rr::Optimization::Level level) |
| 291 | { |
| 292 | switch (level) |
| 293 | { |
| 294 | case rr::Optimization::Level::None: return ::llvm::CodeGenOpt::None; |
| 295 | case rr::Optimization::Level::Less: return ::llvm::CodeGenOpt::Less; |
| 296 | case rr::Optimization::Level::Default: return ::llvm::CodeGenOpt::Default; |
| 297 | case rr::Optimization::Level::Aggressive: return ::llvm::CodeGenOpt::Aggressive; |
| 298 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
| 299 | } |
| 300 | return ::llvm::CodeGenOpt::Default; |
| 301 | } |
| 302 | |
| 303 | JITGlobals::JITGlobals(const char* mcpu, |
| 304 | const std::vector<std::string> &mattrs, |
| 305 | const char* march, |
| 306 | const llvm::TargetOptions &targetOptions, |
| 307 | const llvm::DataLayout &dataLayout) : |
| 308 | mcpu(mcpu), |
| 309 | mattrs(mattrs), |
| 310 | march(march), |
| 311 | targetOptions(targetOptions), |
| 312 | dataLayout(dataLayout) |
| 313 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // JITRoutine is a rr::Routine that holds a LLVM JIT session, compiler and |
| 317 | // object layer as each routine may require different target machine |
| 318 | // settings and no Reactor routine directly links against another. |
| 319 | class JITRoutine : public rr::Routine |
| 320 | { |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 321 | #if LLVM_VERSION_MAJOR >= 8 |
| 322 | using ObjLayer = llvm::orc::LegacyRTDyldObjectLinkingLayer; |
| 323 | using CompileLayer = llvm::orc::LegacyIRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>; |
| 324 | #else |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 325 | using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer; |
| 326 | using CompileLayer = llvm::orc::IRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>; |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 327 | #endif |
| 328 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 329 | public: |
Ben Clayton | 68cfc78 | 2019-06-29 12:31:08 +0100 | [diff] [blame] | 330 | JITRoutine( |
| 331 | std::unique_ptr<llvm::Module> module, |
| 332 | llvm::Function **funcs, |
| 333 | size_t count, |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 334 | const rr::Config &config) : |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 335 | resolver(createLegacyLookupResolver( |
| 336 | session, |
| 337 | [&](const std::string &name) { |
| 338 | void *func = rr::resolveExternalSymbol(name.c_str()); |
| 339 | if (func != nullptr) |
| 340 | { |
| 341 | return llvm::JITSymbol( |
| 342 | reinterpret_cast<uintptr_t>(func), llvm::JITSymbolFlags::Absolute); |
| 343 | } |
| 344 | return objLayer.findSymbol(name, true); |
| 345 | }, |
| 346 | [](llvm::Error err) { |
| 347 | if (err) |
| 348 | { |
| 349 | // TODO: Log the symbol resolution errors. |
| 350 | return; |
| 351 | } |
| 352 | })), |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 353 | targetMachine(JITGlobals::get()->getTargetMachine(config.getOptimization().getLevel())), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 354 | compileLayer(objLayer, llvm::orc::SimpleCompiler(*targetMachine)), |
| 355 | objLayer( |
| 356 | session, |
| 357 | [this](llvm::orc::VModuleKey) { |
| 358 | return ObjLayer::Resources{std::make_shared<llvm::SectionMemoryManager>(), resolver}; |
| 359 | }, |
| 360 | ObjLayer::NotifyLoadedFtor(), |
| 361 | [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj, const llvm::RuntimeDyld::LoadedObjectInfo &L) { |
| 362 | #ifdef ENABLE_RR_DEBUG_INFO |
| 363 | rr::DebugInfo::NotifyObjectEmitted(Obj, L); |
| 364 | #endif // ENABLE_RR_DEBUG_INFO |
| 365 | }, |
| 366 | [](llvm::orc::VModuleKey, const llvm::object::ObjectFile &Obj) { |
| 367 | #ifdef ENABLE_RR_DEBUG_INFO |
| 368 | rr::DebugInfo::NotifyFreeingObject(Obj); |
| 369 | #endif // ENABLE_RR_DEBUG_INFO |
| 370 | } |
| 371 | ), |
| 372 | addresses(count) |
| 373 | { |
| 374 | std::vector<std::string> mangledNames(count); |
| 375 | for (size_t i = 0; i < count; i++) |
| 376 | { |
| 377 | auto func = funcs[i]; |
| 378 | static size_t numEmittedFunctions = 0; |
| 379 | std::string name = "f" + llvm::Twine(numEmittedFunctions++).str(); |
| 380 | func->setName(name); |
| 381 | func->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 382 | func->setDoesNotThrow(); |
| 383 | |
| 384 | llvm::raw_string_ostream mangledNameStream(mangledNames[i]); |
| 385 | llvm::Mangler::getNameWithPrefix(mangledNameStream, name, JITGlobals::get()->dataLayout); |
| 386 | } |
| 387 | |
| 388 | auto moduleKey = session.allocateVModule(); |
| 389 | |
| 390 | // Once the module is passed to the compileLayer, the |
| 391 | // llvm::Functions are freed. Make sure funcs are not referenced |
| 392 | // after this point. |
| 393 | funcs = nullptr; |
| 394 | |
| 395 | llvm::cantFail(compileLayer.addModule(moduleKey, std::move(module))); |
| 396 | |
| 397 | // Resolve the function addresses. |
| 398 | for (size_t i = 0; i < count; i++) |
| 399 | { |
| 400 | auto symbol = compileLayer.findSymbolIn(moduleKey, mangledNames[i], false); |
| 401 | if(auto address = symbol.getAddress()) |
| 402 | { |
| 403 | addresses[i] = reinterpret_cast<void *>(static_cast<intptr_t>(address.get())); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 408 | const void *getEntry(int index) const override |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 409 | { |
| 410 | return addresses[index]; |
| 411 | } |
| 412 | |
| 413 | private: |
| 414 | std::shared_ptr<llvm::orc::SymbolResolver> resolver; |
Ben Clayton | 52ce1e9 | 2019-07-15 11:41:00 +0100 | [diff] [blame] | 415 | std::shared_ptr<llvm::TargetMachine> targetMachine; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 416 | llvm::orc::ExecutionSession session; |
| 417 | CompileLayer compileLayer; |
| 418 | ObjLayer objLayer; |
| 419 | std::vector<const void *> addresses; |
| 420 | }; |
| 421 | |
| 422 | // JITBuilder holds all the LLVM state for building routines. |
| 423 | class JITBuilder |
| 424 | { |
| 425 | public: |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 426 | JITBuilder(const rr::Config &config) : |
| 427 | config(config), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 428 | module(new llvm::Module("", context)), |
| 429 | builder(new llvm::IRBuilder<>(context)) |
| 430 | { |
| 431 | module->setDataLayout(JITGlobals::get()->dataLayout); |
| 432 | } |
| 433 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 434 | void optimize(const rr::Config &cfg) |
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 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 437 | #ifdef ENABLE_RR_DEBUG_INFO |
| 438 | if (debugInfo != nullptr) |
| 439 | { |
| 440 | return; // Don't optimize if we're generating debug info. |
| 441 | } |
| 442 | #endif // ENABLE_RR_DEBUG_INFO |
| 443 | |
| 444 | std::unique_ptr<llvm::legacy::PassManager> passManager( |
| 445 | new llvm::legacy::PassManager()); |
| 446 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 447 | for(auto pass : cfg.getOptimization().getPasses()) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 448 | { |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 449 | switch(pass) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 450 | { |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 451 | case rr::Optimization::Pass::Disabled: break; |
| 452 | case rr::Optimization::Pass::CFGSimplification: passManager->add(llvm::createCFGSimplificationPass()); break; |
| 453 | case rr::Optimization::Pass::LICM: passManager->add(llvm::createLICMPass()); break; |
| 454 | case rr::Optimization::Pass::AggressiveDCE: passManager->add(llvm::createAggressiveDCEPass()); break; |
| 455 | case rr::Optimization::Pass::GVN: passManager->add(llvm::createGVNPass()); break; |
| 456 | case rr::Optimization::Pass::InstructionCombining: passManager->add(llvm::createInstructionCombiningPass()); break; |
| 457 | case rr::Optimization::Pass::Reassociate: passManager->add(llvm::createReassociatePass()); break; |
| 458 | case rr::Optimization::Pass::DeadStoreElimination: passManager->add(llvm::createDeadStoreEliminationPass()); break; |
| 459 | case rr::Optimization::Pass::SCCP: passManager->add(llvm::createSCCPPass()); break; |
| 460 | case rr::Optimization::Pass::ScalarReplAggregates: passManager->add(llvm::createSROAPass()); break; |
Ben Clayton | 28ae0a4 | 2019-07-10 00:50:23 +0100 | [diff] [blame] | 461 | case rr::Optimization::Pass::EarlyCSEPass: passManager->add(llvm::createEarlyCSEPass()); break; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 462 | default: |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 463 | UNREACHABLE("pass: %d", int(pass)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | passManager->run(*module); |
| 468 | } |
| 469 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 470 | 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] | 471 | { |
| 472 | ASSERT(module); |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 473 | return std::make_shared<JITRoutine>(std::move(module), funcs, count, cfg); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 476 | const rr::Config config; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 477 | llvm::LLVMContext context; |
| 478 | std::unique_ptr<llvm::Module> module; |
| 479 | std::unique_ptr<llvm::IRBuilder<>> builder; |
| 480 | llvm::Function *function = nullptr; |
| 481 | |
| 482 | struct CoroutineState |
| 483 | { |
| 484 | llvm::Function *await = nullptr; |
| 485 | llvm::Function *destroy = nullptr; |
| 486 | llvm::Value *handle = nullptr; |
| 487 | llvm::Value *id = nullptr; |
| 488 | llvm::Value *promise = nullptr; |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 489 | llvm::Type *yieldType = nullptr; |
| 490 | llvm::BasicBlock *entryBlock = nullptr; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 491 | llvm::BasicBlock *suspendBlock = nullptr; |
| 492 | llvm::BasicBlock *endBlock = nullptr; |
| 493 | llvm::BasicBlock *destroyBlock = nullptr; |
| 494 | }; |
| 495 | CoroutineState coroutine; |
| 496 | |
| 497 | #ifdef ENABLE_RR_DEBUG_INFO |
| 498 | std::unique_ptr<rr::DebugInfo> debugInfo; |
| 499 | #endif |
| 500 | }; |
| 501 | |
| 502 | std::unique_ptr<JITBuilder> jit; |
| 503 | std::mutex codegenMutex; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 504 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 505 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 506 | std::string replace(std::string str, const std::string& substr, const std::string& replacement) |
| 507 | { |
| 508 | size_t pos = 0; |
| 509 | while((pos = str.find(substr, pos)) != std::string::npos) { |
| 510 | str.replace(pos, substr.length(), replacement); |
| 511 | pos += replacement.length(); |
| 512 | } |
| 513 | return str; |
| 514 | } |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 515 | #endif // ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 516 | |
Ben Clayton | 40a885e | 2019-06-23 19:12:48 +0100 | [diff] [blame] | 517 | template <typename T> |
| 518 | T alignUp(T val, T alignment) |
| 519 | { |
| 520 | return alignment * ((val + alignment - 1) / alignment); |
| 521 | } |
| 522 | |
| 523 | void* alignedAlloc(size_t size, size_t alignment) |
| 524 | { |
| 525 | ASSERT(alignment < 256); |
| 526 | auto allocation = new uint8_t[size + sizeof(uint8_t) + alignment]; |
| 527 | auto aligned = allocation; |
| 528 | aligned += sizeof(uint8_t); // Make space for the base-address offset. |
| 529 | aligned = reinterpret_cast<uint8_t*>(alignUp(reinterpret_cast<uintptr_t>(aligned), alignment)); // align |
| 530 | auto offset = static_cast<uint8_t>(aligned - allocation); |
| 531 | aligned[-1] = offset; |
| 532 | return aligned; |
| 533 | } |
| 534 | |
| 535 | void alignedFree(void* ptr) |
| 536 | { |
| 537 | auto aligned = reinterpret_cast<uint8_t*>(ptr); |
| 538 | auto offset = aligned[-1]; |
| 539 | auto allocation = aligned - offset; |
| 540 | delete[] allocation; |
| 541 | } |
| 542 | |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 543 | llvm::Value *lowerPAVG(llvm::Value *x, llvm::Value *y) |
| 544 | { |
| 545 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 546 | |
| 547 | llvm::VectorType *extTy = |
| 548 | llvm::VectorType::getExtendedElementVectorType(ty); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 549 | x = jit->builder->CreateZExt(x, extTy); |
| 550 | y = jit->builder->CreateZExt(y, extTy); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 551 | |
| 552 | // (x + y + 1) >> 1 |
| 553 | llvm::Constant *one = llvm::ConstantInt::get(extTy, 1); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 554 | llvm::Value *res = jit->builder->CreateAdd(x, y); |
| 555 | res = jit->builder->CreateAdd(res, one); |
| 556 | res = jit->builder->CreateLShr(res, one); |
| 557 | return jit->builder->CreateTrunc(res, ty); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | llvm::Value *lowerPMINMAX(llvm::Value *x, llvm::Value *y, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 561 | llvm::ICmpInst::Predicate pred) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 562 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 563 | return jit->builder->CreateSelect(jit->builder->CreateICmp(pred, x, y), x, y); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | llvm::Value *lowerPCMP(llvm::ICmpInst::Predicate pred, llvm::Value *x, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 567 | llvm::Value *y, llvm::Type *dstTy) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 568 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 569 | return jit->builder->CreateSExt(jit->builder->CreateICmp(pred, x, y), dstTy, ""); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 570 | } |
| 571 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 572 | #if defined(__i386__) || defined(__x86_64__) |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 573 | llvm::Value *lowerPMOV(llvm::Value *op, llvm::Type *dstType, bool sext) |
| 574 | { |
| 575 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(op->getType()); |
| 576 | llvm::VectorType *dstTy = llvm::cast<llvm::VectorType>(dstType); |
| 577 | |
| 578 | llvm::Value *undef = llvm::UndefValue::get(srcTy); |
| 579 | llvm::SmallVector<uint32_t, 16> mask(dstTy->getNumElements()); |
| 580 | std::iota(mask.begin(), mask.end(), 0); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 581 | llvm::Value *v = jit->builder->CreateShuffleVector(op, undef, mask); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 582 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 583 | return sext ? jit->builder->CreateSExt(v, dstTy) |
| 584 | : jit->builder->CreateZExt(v, dstTy); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | llvm::Value *lowerPABS(llvm::Value *v) |
| 588 | { |
| 589 | llvm::Value *zero = llvm::Constant::getNullValue(v->getType()); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 590 | llvm::Value *cmp = jit->builder->CreateICmp(llvm::ICmpInst::ICMP_SGT, v, zero); |
| 591 | llvm::Value *neg = jit->builder->CreateNeg(v); |
| 592 | return jit->builder->CreateSelect(cmp, v, neg); |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 593 | } |
| 594 | #endif // defined(__i386__) || defined(__x86_64__) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 595 | |
| 596 | #if !defined(__i386__) && !defined(__x86_64__) |
| 597 | llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y, |
Logan Chien | b5ce509 | 2018-09-27 18:45:58 +0800 | [diff] [blame] | 598 | llvm::FCmpInst::Predicate pred) |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 599 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 600 | return jit->builder->CreateSelect(jit->builder->CreateFCmp(pred, x, y), x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 601 | } |
| 602 | |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 603 | llvm::Value *lowerRound(llvm::Value *x) |
| 604 | { |
| 605 | llvm::Function *nearbyint = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 606 | jit->module.get(), llvm::Intrinsic::nearbyint, {x->getType()}); |
| 607 | return jit->builder->CreateCall(nearbyint, ARGS(x)); |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 608 | } |
| 609 | |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 610 | llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty) |
| 611 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 612 | return jit->builder->CreateFPToSI(lowerRound(x), ty); |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 613 | } |
| 614 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 615 | llvm::Value *lowerFloor(llvm::Value *x) |
| 616 | { |
| 617 | llvm::Function *floor = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 618 | jit->module.get(), llvm::Intrinsic::floor, {x->getType()}); |
| 619 | return jit->builder->CreateCall(floor, ARGS(x)); |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 620 | } |
| 621 | |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 622 | llvm::Value *lowerTrunc(llvm::Value *x) |
| 623 | { |
| 624 | llvm::Function *trunc = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 625 | jit->module.get(), llvm::Intrinsic::trunc, {x->getType()}); |
| 626 | return jit->builder->CreateCall(trunc, ARGS(x)); |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 627 | } |
| 628 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 629 | // Packed add/sub with saturation |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 630 | 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] | 631 | { |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 632 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 633 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 634 | |
| 635 | unsigned numBits = ty->getScalarSizeInBits(); |
| 636 | |
| 637 | llvm::Value *max, *min, *extX, *extY; |
| 638 | if (isSigned) |
| 639 | { |
| 640 | max = llvm::ConstantInt::get(extTy, (1LL << (numBits - 1)) - 1, true); |
| 641 | min = llvm::ConstantInt::get(extTy, (-1LL << (numBits - 1)), true); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 642 | extX = jit->builder->CreateSExt(x, extTy); |
| 643 | extY = jit->builder->CreateSExt(y, extTy); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 644 | } |
| 645 | else |
| 646 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 647 | ASSERT_MSG(numBits <= 64, "numBits: %d", int(numBits)); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 648 | uint64_t maxVal = (numBits == 64) ? ~0ULL : (1ULL << numBits) - 1; |
| 649 | max = llvm::ConstantInt::get(extTy, maxVal, false); |
| 650 | min = llvm::ConstantInt::get(extTy, 0, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 651 | extX = jit->builder->CreateZExt(x, extTy); |
| 652 | extY = jit->builder->CreateZExt(y, extTy); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 653 | } |
| 654 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 655 | llvm::Value *res = isAdd ? jit->builder->CreateAdd(extX, extY) |
| 656 | : jit->builder->CreateSub(extX, extY); |
Logan Chien | 28794cf | 2018-09-26 18:58:03 +0800 | [diff] [blame] | 657 | |
| 658 | res = lowerPMINMAX(res, min, llvm::ICmpInst::ICMP_SGT); |
| 659 | res = lowerPMINMAX(res, max, llvm::ICmpInst::ICMP_SLT); |
| 660 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 661 | return jit->builder->CreateTrunc(res, ty); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 662 | } |
| 663 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 664 | llvm::Value *lowerSQRT(llvm::Value *x) |
| 665 | { |
| 666 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 667 | jit->module.get(), llvm::Intrinsic::sqrt, {x->getType()}); |
| 668 | return jit->builder->CreateCall(sqrt, ARGS(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | llvm::Value *lowerRCP(llvm::Value *x) |
| 672 | { |
| 673 | llvm::Type *ty = x->getType(); |
| 674 | llvm::Constant *one; |
| 675 | if (llvm::VectorType *vectorTy = llvm::dyn_cast<llvm::VectorType>(ty)) |
| 676 | { |
| 677 | one = llvm::ConstantVector::getSplat( |
| 678 | vectorTy->getNumElements(), |
| 679 | llvm::ConstantFP::get(vectorTy->getElementType(), 1)); |
| 680 | } |
| 681 | else |
| 682 | { |
| 683 | one = llvm::ConstantFP::get(ty, 1); |
| 684 | } |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 685 | return jit->builder->CreateFDiv(one, x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | llvm::Value *lowerRSQRT(llvm::Value *x) |
| 689 | { |
| 690 | return lowerRCP(lowerSQRT(x)); |
| 691 | } |
| 692 | |
| 693 | llvm::Value *lowerVectorShl(llvm::Value *x, uint64_t scalarY) |
| 694 | { |
| 695 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 696 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 697 | ty->getNumElements(), |
| 698 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 699 | return jit->builder->CreateShl(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | llvm::Value *lowerVectorAShr(llvm::Value *x, uint64_t scalarY) |
| 703 | { |
| 704 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 705 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 706 | ty->getNumElements(), |
| 707 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 708 | return jit->builder->CreateAShr(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | llvm::Value *lowerVectorLShr(llvm::Value *x, uint64_t scalarY) |
| 712 | { |
| 713 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 714 | llvm::Value *y = llvm::ConstantVector::getSplat( |
| 715 | ty->getNumElements(), |
| 716 | llvm::ConstantInt::get(ty->getElementType(), scalarY)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 717 | return jit->builder->CreateLShr(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | llvm::Value *lowerMulAdd(llvm::Value *x, llvm::Value *y) |
| 721 | { |
| 722 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 723 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 724 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 725 | llvm::Value *extX = jit->builder->CreateSExt(x, extTy); |
| 726 | llvm::Value *extY = jit->builder->CreateSExt(y, extTy); |
| 727 | llvm::Value *mult = jit->builder->CreateMul(extX, extY); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 728 | |
| 729 | llvm::Value *undef = llvm::UndefValue::get(extTy); |
| 730 | |
| 731 | llvm::SmallVector<uint32_t, 16> evenIdx; |
| 732 | llvm::SmallVector<uint32_t, 16> oddIdx; |
| 733 | for (uint64_t i = 0, n = ty->getNumElements(); i < n; i += 2) |
| 734 | { |
| 735 | evenIdx.push_back(i); |
| 736 | oddIdx.push_back(i + 1); |
| 737 | } |
| 738 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 739 | llvm::Value *lhs = jit->builder->CreateShuffleVector(mult, undef, evenIdx); |
| 740 | llvm::Value *rhs = jit->builder->CreateShuffleVector(mult, undef, oddIdx); |
| 741 | return jit->builder->CreateAdd(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 742 | } |
| 743 | |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 744 | llvm::Value *lowerPack(llvm::Value *x, llvm::Value *y, bool isSigned) |
| 745 | { |
| 746 | llvm::VectorType *srcTy = llvm::cast<llvm::VectorType>(x->getType()); |
| 747 | llvm::VectorType *dstTy = llvm::VectorType::getTruncatedElementVectorType(srcTy); |
| 748 | |
| 749 | llvm::IntegerType *dstElemTy = |
| 750 | llvm::cast<llvm::IntegerType>(dstTy->getElementType()); |
| 751 | |
| 752 | uint64_t truncNumBits = dstElemTy->getIntegerBitWidth(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 753 | 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] | 754 | llvm::Constant *max, *min; |
| 755 | if (isSigned) |
| 756 | { |
| 757 | max = llvm::ConstantInt::get(srcTy, (1LL << (truncNumBits - 1)) - 1, true); |
| 758 | min = llvm::ConstantInt::get(srcTy, (-1LL << (truncNumBits - 1)), true); |
| 759 | } |
| 760 | else |
| 761 | { |
| 762 | max = llvm::ConstantInt::get(srcTy, (1ULL << truncNumBits) - 1, false); |
| 763 | min = llvm::ConstantInt::get(srcTy, 0, false); |
| 764 | } |
| 765 | |
| 766 | x = lowerPMINMAX(x, min, llvm::ICmpInst::ICMP_SGT); |
| 767 | x = lowerPMINMAX(x, max, llvm::ICmpInst::ICMP_SLT); |
| 768 | y = lowerPMINMAX(y, min, llvm::ICmpInst::ICMP_SGT); |
| 769 | y = lowerPMINMAX(y, max, llvm::ICmpInst::ICMP_SLT); |
| 770 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 771 | x = jit->builder->CreateTrunc(x, dstTy); |
| 772 | y = jit->builder->CreateTrunc(y, dstTy); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 773 | |
| 774 | llvm::SmallVector<uint32_t, 16> index(srcTy->getNumElements() * 2); |
| 775 | std::iota(index.begin(), index.end(), 0); |
| 776 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 777 | return jit->builder->CreateShuffleVector(x, y, index); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | llvm::Value *lowerSignMask(llvm::Value *x, llvm::Type *retTy) |
| 781 | { |
| 782 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 783 | llvm::Constant *zero = llvm::ConstantInt::get(ty, 0); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 784 | llvm::Value *cmp = jit->builder->CreateICmpSLT(x, zero); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 785 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 786 | llvm::Value *ret = jit->builder->CreateZExt( |
| 787 | jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 788 | for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
| 789 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 790 | llvm::Value *elem = jit->builder->CreateZExt( |
| 791 | jit->builder->CreateExtractElement(cmp, i), retTy); |
| 792 | ret = jit->builder->CreateOr(ret, jit->builder->CreateShl(elem, i)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 793 | } |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | llvm::Value *lowerFPSignMask(llvm::Value *x, llvm::Type *retTy) |
| 798 | { |
| 799 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 800 | llvm::Constant *zero = llvm::ConstantFP::get(ty, 0); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 801 | llvm::Value *cmp = jit->builder->CreateFCmpULT(x, zero); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 802 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 803 | llvm::Value *ret = jit->builder->CreateZExt( |
| 804 | jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 805 | for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i) |
| 806 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 807 | llvm::Value *elem = jit->builder->CreateZExt( |
| 808 | jit->builder->CreateExtractElement(cmp, i), retTy); |
| 809 | ret = jit->builder->CreateOr(ret, jit->builder->CreateShl(elem, i)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 810 | } |
| 811 | return ret; |
| 812 | } |
| 813 | #endif // !defined(__i386__) && !defined(__x86_64__) |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 814 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 815 | #if (LLVM_VERSION_MAJOR >= 8) || (!defined(__i386__) && !defined(__x86_64__)) |
| 816 | llvm::Value *lowerPUADDSAT(llvm::Value *x, llvm::Value *y) |
| 817 | { |
| 818 | #if LLVM_VERSION_MAJOR >= 8 |
| 819 | return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::uadd_sat, x, y); |
| 820 | #else |
| 821 | return lowerPSAT(x, y, true, false); |
| 822 | #endif |
| 823 | } |
| 824 | |
| 825 | llvm::Value *lowerPSADDSAT(llvm::Value *x, llvm::Value *y) |
| 826 | { |
| 827 | #if LLVM_VERSION_MAJOR >= 8 |
| 828 | return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::sadd_sat, x, y); |
| 829 | #else |
| 830 | return lowerPSAT(x, y, true, true); |
| 831 | #endif |
| 832 | } |
| 833 | |
| 834 | llvm::Value *lowerPUSUBSAT(llvm::Value *x, llvm::Value *y) |
| 835 | { |
| 836 | #if LLVM_VERSION_MAJOR >= 8 |
| 837 | return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::usub_sat, x, y); |
| 838 | #else |
| 839 | return lowerPSAT(x, y, false, false); |
| 840 | #endif |
| 841 | } |
| 842 | |
| 843 | llvm::Value *lowerPSSUBSAT(llvm::Value *x, llvm::Value *y) |
| 844 | { |
| 845 | #if LLVM_VERSION_MAJOR >= 8 |
| 846 | return jit->builder->CreateBinaryIntrinsic(llvm::Intrinsic::ssub_sat, x, y); |
| 847 | #else |
| 848 | return lowerPSAT(x, y, false, true); |
| 849 | #endif |
| 850 | } |
| 851 | #endif // (LLVM_VERSION_MAJOR >= 8) || (!defined(__i386__) && !defined(__x86_64__)) |
| 852 | |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 853 | llvm::Value *lowerMulHigh(llvm::Value *x, llvm::Value *y, bool sext) |
| 854 | { |
| 855 | llvm::VectorType *ty = llvm::cast<llvm::VectorType>(x->getType()); |
| 856 | llvm::VectorType *extTy = llvm::VectorType::getExtendedElementVectorType(ty); |
| 857 | |
| 858 | llvm::Value *extX, *extY; |
| 859 | if (sext) |
| 860 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 861 | extX = jit->builder->CreateSExt(x, extTy); |
| 862 | extY = jit->builder->CreateSExt(y, extTy); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 863 | } |
| 864 | else |
| 865 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 866 | extX = jit->builder->CreateZExt(x, extTy); |
| 867 | extY = jit->builder->CreateZExt(y, extTy); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 870 | llvm::Value *mult = jit->builder->CreateMul(extX, extY); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 871 | |
| 872 | llvm::IntegerType *intTy = llvm::cast<llvm::IntegerType>(ty->getElementType()); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 873 | llvm::Value *mulh = jit->builder->CreateAShr(mult, intTy->getBitWidth()); |
| 874 | return jit->builder->CreateTrunc(mulh, ty); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 875 | } |
Antonio Maiorano | e6ab470 | 2019-11-29 11:26:30 -0500 | [diff] [blame^] | 876 | |
| 877 | llvm::Value *createGather(llvm::Value *base, llvm::Type *elTy, llvm::Value *offsets, llvm::Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 878 | { |
| 879 | ASSERT(base->getType()->isPointerTy()); |
| 880 | ASSERT(offsets->getType()->isVectorTy()); |
| 881 | ASSERT(mask->getType()->isVectorTy()); |
| 882 | |
| 883 | auto numEls = mask->getType()->getVectorNumElements(); |
| 884 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 885 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 886 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 887 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 888 | auto elPtrTy = elTy->getPointerTo(); |
| 889 | auto elVecTy = ::llvm::VectorType::get(elTy, numEls); |
| 890 | auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls); |
| 891 | auto i8Base = jit->builder->CreatePointerCast(base, i8PtrTy); |
| 892 | auto i8Ptrs = jit->builder->CreateGEP(i8Base, offsets); |
| 893 | auto elPtrs = jit->builder->CreatePointerCast(i8Ptrs, elPtrVecTy); |
| 894 | auto i8Mask = jit->builder->CreateIntCast(mask, ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
| 895 | auto passthrough = zeroMaskedLanes ? ::llvm::Constant::getNullValue(elVecTy) : llvm::UndefValue::get(elVecTy); |
| 896 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
| 897 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_gather, { elVecTy, elPtrVecTy } ); |
| 898 | return jit->builder->CreateCall(func, { elPtrs, align, i8Mask, passthrough }); |
| 899 | } |
| 900 | |
| 901 | void createScatter(llvm::Value *base, llvm::Value *val, llvm::Value *offsets, llvm::Value *mask, unsigned int alignment) |
| 902 | { |
| 903 | ASSERT(base->getType()->isPointerTy()); |
| 904 | ASSERT(val->getType()->isVectorTy()); |
| 905 | ASSERT(offsets->getType()->isVectorTy()); |
| 906 | ASSERT(mask->getType()->isVectorTy()); |
| 907 | |
| 908 | auto numEls = mask->getType()->getVectorNumElements(); |
| 909 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 910 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 911 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 912 | auto i8PtrTy = i8Ty->getPointerTo(); |
| 913 | auto elVecTy = val->getType(); |
| 914 | auto elTy = elVecTy->getVectorElementType(); |
| 915 | auto elPtrTy = elTy->getPointerTo(); |
| 916 | auto elPtrVecTy = ::llvm::VectorType::get(elPtrTy, numEls); |
| 917 | auto i8Base = jit->builder->CreatePointerCast(base, i8PtrTy); |
| 918 | auto i8Ptrs = jit->builder->CreateGEP(i8Base, offsets); |
| 919 | auto elPtrs = jit->builder->CreatePointerCast(i8Ptrs, elPtrVecTy); |
| 920 | auto i8Mask = jit->builder->CreateIntCast(mask, ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...> |
| 921 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
| 922 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_scatter, { elVecTy, elPtrVecTy } ); |
| 923 | jit->builder->CreateCall(func, { val, elPtrs, align, i8Mask }); |
| 924 | } |
Nicolas Capens | 5c1f5cc | 2016-09-23 16:45:13 -0400 | [diff] [blame] | 925 | } |
| 926 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 927 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 928 | { |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 929 | const Capabilities Caps = |
| 930 | { |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 931 | true, // CoroutinesSupported |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 932 | }; |
| 933 | |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 934 | static std::memory_order atomicOrdering(llvm::AtomicOrdering memoryOrder) |
| 935 | { |
| 936 | switch(memoryOrder) |
| 937 | { |
| 938 | case llvm::AtomicOrdering::Monotonic: return std::memory_order_relaxed; // https://llvm.org/docs/Atomics.html#monotonic |
| 939 | case llvm::AtomicOrdering::Acquire: return std::memory_order_acquire; |
| 940 | case llvm::AtomicOrdering::Release: return std::memory_order_release; |
| 941 | case llvm::AtomicOrdering::AcquireRelease: return std::memory_order_acq_rel; |
| 942 | case llvm::AtomicOrdering::SequentiallyConsistent: return std::memory_order_seq_cst; |
| 943 | default: |
Ben Clayton | fb28067 | 2019-04-25 11:16:15 +0100 | [diff] [blame] | 944 | UNREACHABLE("memoryOrder: %d", int(memoryOrder)); |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 945 | return std::memory_order_acq_rel; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | static llvm::AtomicOrdering atomicOrdering(bool atomic, std::memory_order memoryOrder) |
| 950 | { |
| 951 | if(!atomic) |
| 952 | { |
| 953 | return llvm::AtomicOrdering::NotAtomic; |
| 954 | } |
| 955 | |
| 956 | switch(memoryOrder) |
| 957 | { |
| 958 | case std::memory_order_relaxed: return llvm::AtomicOrdering::Monotonic; // https://llvm.org/docs/Atomics.html#monotonic |
| 959 | 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." |
| 960 | case std::memory_order_acquire: return llvm::AtomicOrdering::Acquire; |
| 961 | case std::memory_order_release: return llvm::AtomicOrdering::Release; |
| 962 | case std::memory_order_acq_rel: return llvm::AtomicOrdering::AcquireRelease; |
| 963 | case std::memory_order_seq_cst: return llvm::AtomicOrdering::SequentiallyConsistent; |
| 964 | default: |
| 965 | UNREACHABLE("memoryOrder: %d", int(memoryOrder)); |
| 966 | return llvm::AtomicOrdering::AcquireRelease; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | template <typename T> |
| 971 | static void atomicLoad(void *ptr, void *ret, llvm::AtomicOrdering ordering) |
| 972 | { |
| 973 | *reinterpret_cast<T*>(ret) = std::atomic_load_explicit<T>(reinterpret_cast<std::atomic<T>*>(ptr), atomicOrdering(ordering)); |
| 974 | } |
| 975 | |
| 976 | template <typename T> |
| 977 | static void atomicStore(void *ptr, void *val, llvm::AtomicOrdering ordering) |
| 978 | { |
| 979 | std::atomic_store_explicit<T>(reinterpret_cast<std::atomic<T>*>(ptr), *reinterpret_cast<T*>(val), atomicOrdering(ordering)); |
| 980 | } |
| 981 | |
Chris Forbes | fd4c96d | 2019-06-20 11:20:42 -0700 | [diff] [blame] | 982 | #ifdef __ANDROID__ |
| 983 | template<typename F> |
| 984 | static uint32_t sync_fetch_and_op(uint32_t volatile *ptr, uint32_t val, F f) |
| 985 | { |
| 986 | // Build an arbitrary op out of looped CAS |
| 987 | for (;;) |
| 988 | { |
| 989 | uint32_t expected = *ptr; |
| 990 | uint32_t desired = f(expected, val); |
| 991 | |
| 992 | if (expected == __sync_val_compare_and_swap_4(ptr, expected, desired)) |
| 993 | return expected; |
| 994 | } |
| 995 | } |
| 996 | #endif |
| 997 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 998 | void* resolveExternalSymbol(const char* name) |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 999 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1000 | struct Atomic |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 1001 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1002 | 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] | 1003 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1004 | switch (size) |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 1005 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1006 | case 1: atomicLoad<uint8_t>(ptr, ret, ordering); break; |
| 1007 | case 2: atomicLoad<uint16_t>(ptr, ret, ordering); break; |
| 1008 | case 4: atomicLoad<uint32_t>(ptr, ret, ordering); break; |
| 1009 | case 8: atomicLoad<uint64_t>(ptr, ret, ordering); break; |
| 1010 | default: |
| 1011 | UNIMPLEMENTED("Atomic::load(size: %d)", int(size)); |
Ben Clayton | 4d1f8d0 | 2019-04-17 23:47:35 -0400 | [diff] [blame] | 1012 | } |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1013 | } |
| 1014 | 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] | 1015 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1016 | switch (size) |
| 1017 | { |
| 1018 | case 1: atomicStore<uint8_t>(ptr, ret, ordering); break; |
| 1019 | case 2: atomicStore<uint16_t>(ptr, ret, ordering); break; |
| 1020 | case 4: atomicStore<uint32_t>(ptr, ret, ordering); break; |
| 1021 | case 8: atomicStore<uint64_t>(ptr, ret, ordering); break; |
| 1022 | default: |
| 1023 | UNIMPLEMENTED("Atomic::store(size: %d)", int(size)); |
| 1024 | } |
| 1025 | } |
| 1026 | }; |
Chris Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 1027 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1028 | struct F |
| 1029 | { |
| 1030 | static void nop() {} |
| 1031 | static void neverCalled() { UNREACHABLE("Should never be called"); } |
| 1032 | |
| 1033 | static void* coroutine_alloc_frame(size_t size) { return alignedAlloc(size, 16); } |
| 1034 | static void coroutine_free_frame(void* ptr) { alignedFree(ptr); } |
Ben Clayton | 40a885e | 2019-06-23 19:12:48 +0100 | [diff] [blame] | 1035 | |
Chris Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 1036 | #ifdef __ANDROID__ |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1037 | // forwarders since we can't take address of builtins |
| 1038 | static void sync_synchronize() { __sync_synchronize(); } |
| 1039 | static uint32_t sync_fetch_and_add_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_add_4(ptr, val); } |
| 1040 | static uint32_t sync_fetch_and_and_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_and_4(ptr, val); } |
| 1041 | static uint32_t sync_fetch_and_or_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_or_4(ptr, val); } |
| 1042 | static uint32_t sync_fetch_and_xor_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_xor_4(ptr, val); } |
| 1043 | static uint32_t sync_fetch_and_sub_4(uint32_t *ptr, uint32_t val) { return __sync_fetch_and_sub_4(ptr, val); } |
| 1044 | static uint32_t sync_lock_test_and_set_4(uint32_t *ptr, uint32_t val) { return __sync_lock_test_and_set_4(ptr, val); } |
| 1045 | 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] | 1046 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1047 | 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);}); } |
| 1048 | 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);}); } |
| 1049 | 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);}); } |
| 1050 | 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] | 1051 | #endif |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1052 | }; |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1053 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1054 | class Resolver |
| 1055 | { |
| 1056 | public: |
| 1057 | using FunctionMap = std::unordered_map<std::string, void *>; |
Ben Clayton | b5f1a13 | 2019-06-24 11:00:53 +0100 | [diff] [blame] | 1058 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1059 | FunctionMap functions; |
Ben Clayton | b5f1a13 | 2019-06-24 11:00:53 +0100 | [diff] [blame] | 1060 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1061 | Resolver() |
| 1062 | { |
| 1063 | functions.emplace("nop", reinterpret_cast<void*>(F::nop)); |
| 1064 | functions.emplace("floorf", reinterpret_cast<void*>(floorf)); |
| 1065 | functions.emplace("nearbyintf", reinterpret_cast<void*>(nearbyintf)); |
| 1066 | functions.emplace("truncf", reinterpret_cast<void*>(truncf)); |
| 1067 | functions.emplace("printf", reinterpret_cast<void*>(printf)); |
| 1068 | functions.emplace("puts", reinterpret_cast<void*>(puts)); |
| 1069 | functions.emplace("fmodf", reinterpret_cast<void*>(fmodf)); |
Ben Clayton | b5f1a13 | 2019-06-24 11:00:53 +0100 | [diff] [blame] | 1070 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1071 | functions.emplace("sinf", reinterpret_cast<void*>(sinf)); |
| 1072 | functions.emplace("cosf", reinterpret_cast<void*>(cosf)); |
| 1073 | functions.emplace("asinf", reinterpret_cast<void*>(asinf)); |
| 1074 | functions.emplace("acosf", reinterpret_cast<void*>(acosf)); |
| 1075 | functions.emplace("atanf", reinterpret_cast<void*>(atanf)); |
| 1076 | functions.emplace("sinhf", reinterpret_cast<void*>(sinhf)); |
| 1077 | functions.emplace("coshf", reinterpret_cast<void*>(coshf)); |
| 1078 | functions.emplace("tanhf", reinterpret_cast<void*>(tanhf)); |
| 1079 | functions.emplace("asinhf", reinterpret_cast<void*>(asinhf)); |
| 1080 | functions.emplace("acoshf", reinterpret_cast<void*>(acoshf)); |
| 1081 | functions.emplace("atanhf", reinterpret_cast<void*>(atanhf)); |
| 1082 | functions.emplace("atan2f", reinterpret_cast<void*>(atan2f)); |
| 1083 | functions.emplace("powf", reinterpret_cast<void*>(powf)); |
| 1084 | functions.emplace("expf", reinterpret_cast<void*>(expf)); |
| 1085 | functions.emplace("logf", reinterpret_cast<void*>(logf)); |
| 1086 | functions.emplace("exp2f", reinterpret_cast<void*>(exp2f)); |
| 1087 | functions.emplace("log2f", reinterpret_cast<void*>(log2f)); |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 1088 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1089 | functions.emplace("sin", reinterpret_cast<void*>(static_cast<double(*)(double)>(sin))); |
| 1090 | functions.emplace("cos", reinterpret_cast<void*>(static_cast<double(*)(double)>(cos))); |
| 1091 | functions.emplace("asin", reinterpret_cast<void*>(static_cast<double(*)(double)>(asin))); |
| 1092 | functions.emplace("acos", reinterpret_cast<void*>(static_cast<double(*)(double)>(acos))); |
| 1093 | functions.emplace("atan", reinterpret_cast<void*>(static_cast<double(*)(double)>(atan))); |
| 1094 | functions.emplace("sinh", reinterpret_cast<void*>(static_cast<double(*)(double)>(sinh))); |
| 1095 | functions.emplace("cosh", reinterpret_cast<void*>(static_cast<double(*)(double)>(cosh))); |
| 1096 | functions.emplace("tanh", reinterpret_cast<void*>(static_cast<double(*)(double)>(tanh))); |
| 1097 | functions.emplace("asinh", reinterpret_cast<void*>(static_cast<double(*)(double)>(asinh))); |
| 1098 | functions.emplace("acosh", reinterpret_cast<void*>(static_cast<double(*)(double)>(acosh))); |
| 1099 | functions.emplace("atanh", reinterpret_cast<void*>(static_cast<double(*)(double)>(atanh))); |
| 1100 | functions.emplace("atan2", reinterpret_cast<void*>(static_cast<double(*)(double,double)>(atan2))); |
| 1101 | functions.emplace("pow", reinterpret_cast<void*>(static_cast<double(*)(double,double)>(pow))); |
| 1102 | functions.emplace("exp", reinterpret_cast<void*>(static_cast<double(*)(double)>(exp))); |
| 1103 | functions.emplace("log", reinterpret_cast<void*>(static_cast<double(*)(double)>(log))); |
| 1104 | functions.emplace("exp2", reinterpret_cast<void*>(static_cast<double(*)(double)>(exp2))); |
| 1105 | functions.emplace("log2", reinterpret_cast<void*>(static_cast<double(*)(double)>(log2))); |
| 1106 | |
| 1107 | functions.emplace("atomic_load", reinterpret_cast<void*>(Atomic::load)); |
| 1108 | functions.emplace("atomic_store", reinterpret_cast<void*>(Atomic::store)); |
| 1109 | |
| 1110 | // FIXME (b/119409619): use an allocator here so we can control all memory allocations |
| 1111 | functions.emplace("coroutine_alloc_frame", reinterpret_cast<void*>(F::coroutine_alloc_frame)); |
| 1112 | functions.emplace("coroutine_free_frame", reinterpret_cast<void*>(F::coroutine_free_frame)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 1113 | |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 1114 | #ifdef __APPLE__ |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1115 | functions.emplace("sincosf_stret", reinterpret_cast<void*>(__sincosf_stret)); |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 1116 | #elif defined(__linux__) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1117 | functions.emplace("sincosf", reinterpret_cast<void*>(sincosf)); |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 1118 | #elif defined(_WIN64) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1119 | functions.emplace("chkstk", reinterpret_cast<void*>(__chkstk)); |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 1120 | #elif defined(_WIN32) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1121 | functions.emplace("chkstk", reinterpret_cast<void*>(_chkstk)); |
Ben Clayton | 2f58df3 | 2019-06-23 21:29:25 +0100 | [diff] [blame] | 1122 | #endif |
Chris Forbes | 9283b25 | 2019-06-17 09:44:28 -0700 | [diff] [blame] | 1123 | |
| 1124 | #ifdef __ANDROID__ |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1125 | functions.emplace("aeabi_unwind_cpp_pr0", reinterpret_cast<void*>(F::neverCalled)); |
| 1126 | functions.emplace("sync_synchronize", reinterpret_cast<void*>(F::sync_synchronize)); |
| 1127 | functions.emplace("sync_fetch_and_add_4", reinterpret_cast<void*>(F::sync_fetch_and_add_4)); |
| 1128 | functions.emplace("sync_fetch_and_and_4", reinterpret_cast<void*>(F::sync_fetch_and_and_4)); |
| 1129 | functions.emplace("sync_fetch_and_or_4", reinterpret_cast<void*>(F::sync_fetch_and_or_4)); |
| 1130 | functions.emplace("sync_fetch_and_xor_4", reinterpret_cast<void*>(F::sync_fetch_and_xor_4)); |
| 1131 | functions.emplace("sync_fetch_and_sub_4", reinterpret_cast<void*>(F::sync_fetch_and_sub_4)); |
| 1132 | functions.emplace("sync_lock_test_and_set_4", reinterpret_cast<void*>(F::sync_lock_test_and_set_4)); |
| 1133 | functions.emplace("sync_val_compare_and_swap_4", reinterpret_cast<void*>(F::sync_val_compare_and_swap_4)); |
| 1134 | functions.emplace("sync_fetch_and_max_4", reinterpret_cast<void*>(F::sync_fetch_and_max_4)); |
| 1135 | functions.emplace("sync_fetch_and_min_4", reinterpret_cast<void*>(F::sync_fetch_and_min_4)); |
| 1136 | functions.emplace("sync_fetch_and_umax_4", reinterpret_cast<void*>(F::sync_fetch_and_umax_4)); |
| 1137 | functions.emplace("sync_fetch_and_umin_4", reinterpret_cast<void*>(F::sync_fetch_and_umin_4)); |
| 1138 | #endif |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 1139 | } |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1140 | }; |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 1141 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1142 | static Resolver resolver; |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1143 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1144 | // Trim off any underscores from the start of the symbol. LLVM likes |
| 1145 | // to append these on macOS. |
| 1146 | const char* trimmed = name; |
| 1147 | while (trimmed[0] == '_') { trimmed++; } |
Ben Clayton | cee3dff | 2019-05-22 12:01:22 +0100 | [diff] [blame] | 1148 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1149 | auto it = resolver.functions.find(trimmed); |
| 1150 | // Missing functions will likely make the module fail in exciting non-obvious ways. |
| 1151 | ASSERT_MSG(it != resolver.functions.end(), "Missing external function: '%s'", name); |
| 1152 | return it->second; |
| 1153 | } |
Logan Chien | 52cde60 | 2018-09-03 19:37:57 +0800 | [diff] [blame] | 1154 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1155 | // The abstract Type* types are implemented as LLVM types, except that |
| 1156 | // 64-bit vectors are emulated using 128-bit ones to avoid use of MMX in x86 |
| 1157 | // and VFP in ARM, and eliminate the overhead of converting them to explicit |
| 1158 | // 128-bit ones. LLVM types are pointers, so we can represent emulated types |
| 1159 | // as abstract pointers with small enum values. |
| 1160 | enum InternalType : uintptr_t |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1161 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1162 | // Emulated types: |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1163 | Type_v2i32, |
| 1164 | Type_v4i16, |
| 1165 | Type_v2i16, |
| 1166 | Type_v8i8, |
| 1167 | Type_v4i8, |
| 1168 | Type_v2f32, |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1169 | EmulatedTypeCount, |
| 1170 | // Returned by asInternalType() to indicate that the abstract Type* |
| 1171 | // should be interpreted as LLVM type pointer: |
| 1172 | Type_LLVM |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1173 | }; |
| 1174 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1175 | inline InternalType asInternalType(Type *type) |
| 1176 | { |
| 1177 | InternalType t = static_cast<InternalType>(reinterpret_cast<uintptr_t>(type)); |
| 1178 | return (t < EmulatedTypeCount) ? t : Type_LLVM; |
| 1179 | } |
| 1180 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1181 | llvm::Type *T(Type *t) |
| 1182 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1183 | // Use 128-bit vectors to implement logically shorter ones. |
| 1184 | switch(asInternalType(t)) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1185 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1186 | case Type_v2i32: return T(Int4::getType()); |
| 1187 | case Type_v4i16: return T(Short8::getType()); |
| 1188 | case Type_v2i16: return T(Short8::getType()); |
| 1189 | case Type_v8i8: return T(Byte16::getType()); |
| 1190 | case Type_v4i8: return T(Byte16::getType()); |
| 1191 | case Type_v2f32: return T(Float4::getType()); |
| 1192 | case Type_LLVM: return reinterpret_cast<llvm::Type*>(t); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1193 | default: |
| 1194 | UNREACHABLE("asInternalType(t): %d", int(asInternalType(t))); |
| 1195 | return nullptr; |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1196 | } |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1197 | } |
| 1198 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1199 | Type *T(InternalType t) |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1200 | { |
| 1201 | return reinterpret_cast<Type*>(t); |
| 1202 | } |
| 1203 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1204 | inline std::vector<llvm::Type*> &T(std::vector<Type*> &t) |
| 1205 | { |
| 1206 | return reinterpret_cast<std::vector<llvm::Type*>&>(t); |
| 1207 | } |
| 1208 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1209 | inline llvm::BasicBlock *B(BasicBlock *t) |
| 1210 | { |
| 1211 | return reinterpret_cast<llvm::BasicBlock*>(t); |
| 1212 | } |
| 1213 | |
Nicolas Capens | c8b67a4 | 2016-09-25 15:02:52 -0400 | [diff] [blame] | 1214 | inline BasicBlock *B(llvm::BasicBlock *t) |
| 1215 | { |
| 1216 | return reinterpret_cast<BasicBlock*>(t); |
| 1217 | } |
| 1218 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1219 | static size_t typeSize(Type *type) |
| 1220 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1221 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1222 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1223 | case Type_v2i32: return 8; |
| 1224 | case Type_v4i16: return 8; |
| 1225 | case Type_v2i16: return 4; |
| 1226 | case Type_v8i8: return 8; |
| 1227 | case Type_v4i8: return 4; |
| 1228 | case Type_v2f32: return 8; |
| 1229 | case Type_LLVM: |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1230 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1231 | llvm::Type *t = T(type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1232 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1233 | if(t->isPointerTy()) |
| 1234 | { |
| 1235 | return sizeof(void*); |
| 1236 | } |
| 1237 | |
| 1238 | // At this point we should only have LLVM 'primitive' types. |
| 1239 | unsigned int bits = t->getPrimitiveSizeInBits(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1240 | ASSERT_MSG(bits != 0, "bits: %d", int(bits)); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1241 | |
| 1242 | // TODO(capn): Booleans are 1 bit integers in LLVM's SSA type system, |
| 1243 | // but are typically stored as one byte. The DataLayout structure should |
| 1244 | // be used here and many other places if this assumption fails. |
| 1245 | return (bits + 7) / 8; |
| 1246 | } |
| 1247 | break; |
| 1248 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1249 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1252 | } |
| 1253 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1254 | static unsigned int elementCount(Type *type) |
| 1255 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1256 | switch(asInternalType(type)) |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1257 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1258 | case Type_v2i32: return 2; |
| 1259 | case Type_v4i16: return 4; |
| 1260 | case Type_v2i16: return 2; |
| 1261 | case Type_v8i8: return 8; |
| 1262 | case Type_v4i8: return 4; |
| 1263 | case Type_v2f32: return 2; |
| 1264 | case Type_LLVM: return llvm::cast<llvm::VectorType>(T(type))->getNumElements(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1265 | default: |
| 1266 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1267 | return 0; |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1268 | } |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 1269 | } |
| 1270 | |
Ben Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 1271 | static ::llvm::Function* createFunction(const char *name, ::llvm::Type *retTy, const std::vector<::llvm::Type*> ¶ms) |
| 1272 | { |
| 1273 | llvm::FunctionType *functionType = llvm::FunctionType::get(retTy, params, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1274 | 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] | 1275 | func->setDoesNotThrow(); |
| 1276 | func->setCallingConv(llvm::CallingConv::C); |
Ben Clayton | ea38f95 | 2019-06-17 13:56:56 +0100 | [diff] [blame] | 1277 | return func; |
| 1278 | } |
| 1279 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1280 | Nucleus::Nucleus() |
| 1281 | { |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 1282 | ::codegenMutex.lock(); // Reactor and LLVM are currently not thread safe |
Nicolas Capens | b7ea984 | 2015-04-01 10:54:59 -0400 | [diff] [blame] | 1283 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1284 | ASSERT(jit == nullptr); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1285 | jit.reset(new JITBuilder(Nucleus::getDefaultConfig())); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | Nucleus::~Nucleus() |
| 1289 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1290 | jit.reset(); |
Nicolas Capens | 3bbc5e1 | 2016-09-27 10:49:52 -0400 | [diff] [blame] | 1291 | ::codegenMutex.unlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1292 | } |
| 1293 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1294 | void Nucleus::setDefaultConfig(const Config &cfg) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1295 | { |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1296 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 1297 | ::defaultConfig() = cfg; |
| 1298 | } |
| 1299 | |
| 1300 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 1301 | { |
| 1302 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 1303 | auto &config = ::defaultConfig(); |
| 1304 | config = cfgEdit.apply(config); |
| 1305 | } |
| 1306 | |
| 1307 | Config Nucleus::getDefaultConfig() |
| 1308 | { |
| 1309 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 1310 | return ::defaultConfig(); |
| 1311 | } |
| 1312 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 1313 | 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] | 1314 | { |
| 1315 | auto cfg = cfgEdit.apply(jit->config); |
| 1316 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1317 | if(jit->builder->GetInsertBlock()->empty() || !jit->builder->GetInsertBlock()->back().isTerminator()) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1318 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1319 | llvm::Type *type = jit->function->getReturnType(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1320 | |
| 1321 | if(type->isVoidTy()) |
| 1322 | { |
| 1323 | createRetVoid(); |
| 1324 | } |
| 1325 | else |
| 1326 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1327 | createRet(V(llvm::UndefValue::get(type))); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1328 | } |
| 1329 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1330 | |
Ben Clayton | 97c13ad | 2019-05-02 11:59:30 +0100 | [diff] [blame] | 1331 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1332 | if (jit->debugInfo != nullptr) |
Ben Clayton | 97c13ad | 2019-05-02 11:59:30 +0100 | [diff] [blame] | 1333 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1334 | jit->debugInfo->Finalize(); |
Ben Clayton | 97c13ad | 2019-05-02 11:59:30 +0100 | [diff] [blame] | 1335 | } |
| 1336 | #endif // ENABLE_RR_DEBUG_INFO |
| 1337 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1338 | if(false) |
| 1339 | { |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 1340 | std::error_code error; |
| 1341 | 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] | 1342 | jit->module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1343 | } |
| 1344 | |
Ben Clayton | 5375f47 | 2019-06-24 13:33:11 +0100 | [diff] [blame] | 1345 | #if defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 1346 | { |
| 1347 | llvm::legacy::PassManager pm; |
| 1348 | pm.add(llvm::createVerifierPass()); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1349 | pm.run(*jit->module); |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 1350 | } |
Ben Clayton | 5375f47 | 2019-06-24 13:33:11 +0100 | [diff] [blame] | 1351 | #endif // defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
Ben Clayton | 4b94465 | 2019-05-02 10:56:19 +0100 | [diff] [blame] | 1352 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1353 | jit->optimize(cfg); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1354 | |
| 1355 | if(false) |
| 1356 | { |
Ben Clayton | 5875be5 | 2019-04-11 14:57:40 -0400 | [diff] [blame] | 1357 | std::error_code error; |
| 1358 | 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] | 1359 | jit->module->print(file, 0); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1360 | } |
| 1361 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 1362 | auto routine = jit->acquireRoutine(&jit->function, 1, cfg); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1363 | jit.reset(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1364 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1365 | return routine; |
| 1366 | } |
| 1367 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1368 | Value *Nucleus::allocateStackVariable(Type *type, int arraySize) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1369 | { |
| 1370 | // Need to allocate it in the entry block for mem2reg to work |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1371 | llvm::BasicBlock &entryBlock = jit->function->getEntryBlock(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1372 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 1373 | llvm::Instruction *declaration; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1374 | |
| 1375 | if(arraySize) |
| 1376 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1377 | declaration = new llvm::AllocaInst(T(type), 0, V(Nucleus::createConstantInt(arraySize))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1378 | } |
| 1379 | else |
| 1380 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 1381 | declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value*)nullptr); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | entryBlock.getInstList().push_front(declaration); |
| 1385 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1386 | return V(declaration); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | BasicBlock *Nucleus::createBasicBlock() |
| 1390 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1391 | return B(llvm::BasicBlock::Create(jit->context, "", jit->function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | BasicBlock *Nucleus::getInsertBlock() |
| 1395 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1396 | return B(jit->builder->GetInsertBlock()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1400 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1401 | // assert(jit->builder->GetInsertBlock()->back().isTerminator()); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1402 | |
| 1403 | Variable::materializeAll(); |
| 1404 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1405 | jit->builder->SetInsertPoint(B(basicBlock)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1406 | } |
| 1407 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 1408 | void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1409 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1410 | jit->function = rr::createFunction("", T(ReturnType), T(Params)); |
Nicolas Capens | 52551d1 | 2018-09-13 14:30:56 -0400 | [diff] [blame] | 1411 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1412 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | e031f36 | 2019-07-20 12:35:40 +0100 | [diff] [blame] | 1413 | 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] | 1414 | #endif // ENABLE_RR_DEBUG_INFO |
| 1415 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1416 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->function)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1417 | } |
| 1418 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1419 | Value *Nucleus::getArgument(unsigned int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1420 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1421 | llvm::Function::arg_iterator args = jit->function->arg_begin(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1422 | |
| 1423 | while(index) |
| 1424 | { |
| 1425 | args++; |
| 1426 | index--; |
| 1427 | } |
| 1428 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1429 | return V(&*args); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1430 | } |
| 1431 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1432 | void Nucleus::createRetVoid() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1433 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1434 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1435 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1436 | ASSERT_MSG(jit->function->getReturnType() == T(Void::getType()), "Return type mismatch"); |
Ben Clayton | c958b17 | 2019-05-02 12:20:59 +0100 | [diff] [blame] | 1437 | |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1438 | // Code generated after this point is unreachable, so any variables |
| 1439 | // being read can safely return an undefined value. We have to avoid |
| 1440 | // materializing variables after the terminator ret instruction. |
| 1441 | Variable::killUnmaterialized(); |
| 1442 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1443 | jit->builder->CreateRetVoid(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1444 | } |
| 1445 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1446 | void Nucleus::createRet(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1447 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1448 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1449 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1450 | ASSERT_MSG(jit->function->getReturnType() == V(v)->getType(), "Return type mismatch"); |
Ben Clayton | c958b17 | 2019-05-02 12:20:59 +0100 | [diff] [blame] | 1451 | |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1452 | // Code generated after this point is unreachable, so any variables |
| 1453 | // being read can safely return an undefined value. We have to avoid |
| 1454 | // materializing variables after the terminator ret instruction. |
| 1455 | Variable::killUnmaterialized(); |
| 1456 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1457 | jit->builder->CreateRet(V(v)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1458 | } |
| 1459 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1460 | void Nucleus::createBr(BasicBlock *dest) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1461 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1462 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1463 | Variable::materializeAll(); |
| 1464 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1465 | jit->builder->CreateBr(B(dest)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1466 | } |
| 1467 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 1468 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1469 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1470 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 1471 | Variable::materializeAll(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1472 | jit->builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1476 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1477 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1478 | return V(jit->builder->CreateAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1482 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1483 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1484 | return V(jit->builder->CreateSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1488 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1489 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1490 | return V(jit->builder->CreateMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1494 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1495 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1496 | return V(jit->builder->CreateUDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1500 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1501 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1502 | return V(jit->builder->CreateSDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1506 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1507 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1508 | return V(jit->builder->CreateFAdd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1512 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1513 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1514 | return V(jit->builder->CreateFSub(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1518 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1519 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1520 | return V(jit->builder->CreateFMul(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1524 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1525 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1526 | return V(jit->builder->CreateFDiv(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1530 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1531 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1532 | return V(jit->builder->CreateURem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1536 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1537 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1538 | return V(jit->builder->CreateSRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1542 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1543 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1544 | return V(jit->builder->CreateFRem(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1548 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1549 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1550 | return V(jit->builder->CreateShl(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1554 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1555 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1556 | return V(jit->builder->CreateLShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1560 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1561 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1562 | return V(jit->builder->CreateAShr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1566 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1567 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1568 | return V(jit->builder->CreateAnd(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1572 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1573 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1574 | return V(jit->builder->CreateOr(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1578 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1579 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1580 | return V(jit->builder->CreateXor(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1581 | } |
| 1582 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1583 | Value *Nucleus::createNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1584 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1585 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1586 | return V(jit->builder->CreateNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1587 | } |
| 1588 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1589 | Value *Nucleus::createFNeg(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1590 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1591 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1592 | return V(jit->builder->CreateFNeg(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1593 | } |
| 1594 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1595 | Value *Nucleus::createNot(Value *v) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1596 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1597 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1598 | return V(jit->builder->CreateNot(V(v))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1599 | } |
| 1600 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1601 | 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] | 1602 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1603 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1604 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1605 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1606 | case Type_v2i32: |
| 1607 | case Type_v4i16: |
| 1608 | case Type_v8i8: |
| 1609 | case Type_v2f32: |
| 1610 | return createBitCast( |
| 1611 | createInsertElement( |
| 1612 | V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2))), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1613 | createLoad(createBitCast(ptr, Pointer<Long>::getType()), Long::getType(), isVolatile, alignment, atomic, memoryOrder), |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1614 | 0), |
| 1615 | type); |
| 1616 | case Type_v2i16: |
| 1617 | case Type_v4i8: |
| 1618 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1619 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1620 | 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] | 1621 | 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] | 1622 | i = createZExt(i, Long::getType()); |
| 1623 | Value *v = createInsertElement(u, i, 0); |
| 1624 | return createBitCast(v, type); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1625 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1626 | // Fallthrough to non-emulated case. |
| 1627 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1628 | { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1629 | auto elTy = T(type); |
| 1630 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1631 | |
| 1632 | if (!atomic) |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1633 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1634 | return V(jit->builder->CreateAlignedLoad(V(ptr), alignment, isVolatile)); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1635 | } |
| 1636 | else if (elTy->isIntegerTy() || elTy->isPointerTy()) |
| 1637 | { |
| 1638 | // Integers and pointers can be atomically loaded by setting |
| 1639 | // the ordering constraint on the load instruction. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1640 | auto load = jit->builder->CreateAlignedLoad(V(ptr), alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1641 | load->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1642 | return V(load); |
| 1643 | } |
| 1644 | else if (elTy->isFloatTy() || elTy->isDoubleTy()) |
| 1645 | { |
| 1646 | // LLVM claims to support atomic loads of float types as |
| 1647 | // above, but certain backends cannot deal with this. |
| 1648 | // Load as an integer and bitcast. See b/136037244. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1649 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
| 1650 | auto elAsIntTy = ::llvm::IntegerType::get(jit->context, size * 8); |
| 1651 | auto ptrCast = jit->builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo()); |
| 1652 | auto load = jit->builder->CreateAlignedLoad(ptrCast, alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1653 | load->setAtomic(atomicOrdering(atomic, memoryOrder)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1654 | auto loadCast = jit->builder->CreateBitCast(load, elTy); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1655 | return V(loadCast); |
| 1656 | } |
| 1657 | else |
| 1658 | { |
| 1659 | // More exotic types require falling back to the extern: |
| 1660 | // void __atomic_load(size_t size, void *ptr, void *ret, int ordering) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1661 | auto sizetTy = ::llvm::IntegerType::get(jit->context, sizeof(size_t) * 8); |
| 1662 | auto intTy = ::llvm::IntegerType::get(jit->context, sizeof(int) * 8); |
| 1663 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1664 | auto i8PtrTy = i8Ty->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1665 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1666 | auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1667 | auto func = jit->module->getOrInsertFunction("__atomic_load", funcTy); |
| 1668 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1669 | auto out = allocateStackVariable(type); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1670 | jit->builder->CreateCall(func, { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1671 | ::llvm::ConstantInt::get(sizetTy, size), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1672 | jit->builder->CreatePointerCast(V(ptr), i8PtrTy), |
| 1673 | jit->builder->CreatePointerCast(V(out), i8PtrTy), |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1674 | ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))), |
| 1675 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1676 | return V(jit->builder->CreateLoad(V(out))); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1677 | } |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1678 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1679 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1680 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1681 | return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1682 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1683 | } |
| 1684 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1685 | 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] | 1686 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1687 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1688 | switch(asInternalType(type)) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1689 | { |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1690 | case Type_v2i32: |
| 1691 | case Type_v4i16: |
| 1692 | case Type_v8i8: |
| 1693 | case Type_v2f32: |
| 1694 | createStore( |
| 1695 | createExtractElement( |
| 1696 | createBitCast(value, T(llvm::VectorType::get(T(Long::getType()), 2))), Long::getType(), 0), |
| 1697 | createBitCast(ptr, Pointer<Long>::getType()), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1698 | Long::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1699 | return value; |
| 1700 | case Type_v2i16: |
| 1701 | case Type_v4i8: |
| 1702 | if(alignment != 0) // Not a local variable (all vectors are 128-bit). |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1703 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1704 | createStore( |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1705 | createExtractElement(createBitCast(value, Int4::getType()), Int::getType(), 0), |
| 1706 | createBitCast(ptr, Pointer<Int>::getType()), |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1707 | Int::getType(), isVolatile, alignment, atomic, memoryOrder); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1708 | return value; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1709 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1710 | // Fallthrough to non-emulated case. |
| 1711 | case Type_LLVM: |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1712 | { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1713 | auto elTy = T(type); |
| 1714 | ASSERT(V(ptr)->getType()->getContainedType(0) == elTy); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1715 | |
| 1716 | if (!atomic) |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1717 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1718 | jit->builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1719 | } |
| 1720 | else if (elTy->isIntegerTy() || elTy->isPointerTy()) |
| 1721 | { |
| 1722 | // Integers and pointers can be atomically stored by setting |
| 1723 | // the ordering constraint on the store instruction. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1724 | auto store = jit->builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1725 | store->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1726 | } |
| 1727 | else if (elTy->isFloatTy() || elTy->isDoubleTy()) |
| 1728 | { |
| 1729 | // LLVM claims to support atomic stores of float types as |
| 1730 | // above, but certain backends cannot deal with this. |
| 1731 | // Store as an bitcast integer. See b/136037244. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1732 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
| 1733 | auto elAsIntTy = ::llvm::IntegerType::get(jit->context, size * 8); |
| 1734 | auto valCast = jit->builder->CreateBitCast(V(value), elAsIntTy); |
| 1735 | auto ptrCast = jit->builder->CreatePointerCast(V(ptr), elAsIntTy->getPointerTo()); |
| 1736 | auto store = jit->builder->CreateAlignedStore(valCast, ptrCast, alignment, isVolatile); |
Ben Clayton | 2fa1dad | 2019-06-26 14:07:05 +0100 | [diff] [blame] | 1737 | store->setAtomic(atomicOrdering(atomic, memoryOrder)); |
| 1738 | } |
| 1739 | else |
| 1740 | { |
| 1741 | // More exotic types require falling back to the extern: |
| 1742 | // void __atomic_store(size_t size, void *ptr, void *val, int ordering) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1743 | auto sizetTy = ::llvm::IntegerType::get(jit->context, sizeof(size_t) * 8); |
| 1744 | auto intTy = ::llvm::IntegerType::get(jit->context, sizeof(int) * 8); |
| 1745 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1746 | auto i8PtrTy = i8Ty->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1747 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1748 | auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1749 | auto func = jit->module->getOrInsertFunction("__atomic_store", funcTy); |
| 1750 | auto size = jit->module->getDataLayout().getTypeStoreSize(elTy); |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1751 | auto copy = allocateStackVariable(type); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1752 | jit->builder->CreateStore(V(value), V(copy)); |
| 1753 | jit->builder->CreateCall(func, { |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1754 | ::llvm::ConstantInt::get(sizetTy, size), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1755 | jit->builder->CreatePointerCast(V(ptr), i8PtrTy), |
| 1756 | jit->builder->CreatePointerCast(V(copy), i8PtrTy), |
Ben Clayton | 99e5719 | 2019-05-03 13:25:08 +0100 | [diff] [blame] | 1757 | ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))), |
| 1758 | }); |
| 1759 | } |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 1760 | |
| 1761 | return value; |
| 1762 | } |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1763 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1764 | UNREACHABLE("asInternalType(type): %d", int(asInternalType(type))); |
| 1765 | return nullptr; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1766 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1767 | } |
| 1768 | |
Ben Clayton | 0b00b95 | 2019-07-03 15:51:19 +0100 | [diff] [blame] | 1769 | 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] | 1770 | { |
| 1771 | ASSERT(V(ptr)->getType()->isPointerTy()); |
| 1772 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1773 | |
| 1774 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1775 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 1776 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1777 | auto elVecTy = ::llvm::VectorType::get(T(elTy), numEls); |
| 1778 | auto elVecPtrTy = elVecTy->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1779 | 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] | 1780 | auto passthrough = zeroMaskedLanes ? ::llvm::Constant::getNullValue(elVecTy) : llvm::UndefValue::get(elVecTy); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1781 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1782 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_load, { elVecTy, elVecPtrTy } ); |
| 1783 | return V(jit->builder->CreateCall(func, { V(ptr), align, i8Mask, passthrough })); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 1787 | { |
| 1788 | ASSERT(V(ptr)->getType()->isPointerTy()); |
| 1789 | ASSERT(V(val)->getType()->isVectorTy()); |
| 1790 | ASSERT(V(mask)->getType()->isVectorTy()); |
| 1791 | |
| 1792 | auto numEls = V(mask)->getType()->getVectorNumElements(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1793 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 1794 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1795 | auto elVecTy = V(val)->getType(); |
| 1796 | auto elVecPtrTy = elVecTy->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1797 | 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] | 1798 | auto align = ::llvm::ConstantInt::get(i32Ty, alignment); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1799 | auto func = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::masked_store, { elVecTy, elVecPtrTy } ); |
| 1800 | jit->builder->CreateCall(func, { V(val), V(ptr), align, i8Mask }); |
Ben Clayton | cb2ebc9 | 2019-06-20 00:18:03 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
Antonio Maiorano | e6ab470 | 2019-11-29 11:26:30 -0500 | [diff] [blame^] | 1803 | RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1804 | { |
Antonio Maiorano | e6ab470 | 2019-11-29 11:26:30 -0500 | [diff] [blame^] | 1805 | return As<Float4>(V(createGather(V(base.value), T(Float::getType()), V(offsets.value), V(mask.value), alignment, zeroMaskedLanes))); |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1806 | } |
| 1807 | |
Antonio Maiorano | e6ab470 | 2019-11-29 11:26:30 -0500 | [diff] [blame^] | 1808 | RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1809 | { |
Antonio Maiorano | e6ab470 | 2019-11-29 11:26:30 -0500 | [diff] [blame^] | 1810 | return As<Int4>(V(createGather(V(base.value), T(Float::getType()), V(offsets.value), V(mask.value), alignment, zeroMaskedLanes))); |
| 1811 | } |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1812 | |
Antonio Maiorano | e6ab470 | 2019-11-29 11:26:30 -0500 | [diff] [blame^] | 1813 | void Scatter(RValue<Pointer<Float>> base, RValue<Float4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 1814 | { |
| 1815 | return createScatter(V(base.value), V(val.value), V(offsets.value), V(mask.value), alignment); |
| 1816 | } |
| 1817 | |
| 1818 | void Scatter(RValue<Pointer<Int>> base, RValue<Int4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 1819 | { |
| 1820 | return createScatter(V(base.value), V(val.value), V(offsets.value), V(mask.value), alignment); |
Ben Clayton | 0fc611f | 2019-04-18 11:23:27 -0400 | [diff] [blame] | 1821 | } |
| 1822 | |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 1823 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 1824 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1825 | jit->builder->CreateFence(atomicOrdering(true, memoryOrder)); |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 1826 | } |
| 1827 | |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1828 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1829 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1830 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1831 | ASSERT(V(ptr)->getType()->getContainedType(0) == T(type)); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1832 | if(sizeof(void*) == 8) |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1833 | { |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1834 | // LLVM manual: "When indexing into an array, pointer or vector, |
| 1835 | // integers of any width are allowed, and they are not required to |
| 1836 | // be constant. These integers are treated as signed values where |
| 1837 | // relevant." |
| 1838 | // |
| 1839 | // Thus if we want indexes to be treated as unsigned we have to |
| 1840 | // zero-extend them ourselves. |
| 1841 | // |
| 1842 | // Note that this is not because we want to address anywhere near |
| 1843 | // 4 GB of data. Instead this is important for performance because |
| 1844 | // x86 supports automatic zero-extending of 32-bit registers to |
| 1845 | // 64-bit. Thus when indexing into an array using a uint32 is |
| 1846 | // actually faster than an int32. |
| 1847 | index = unsignedIndex ? |
| 1848 | createZExt(index, Long::getType()) : |
| 1849 | createSExt(index, Long::getType()); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1850 | } |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1851 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1852 | // For non-emulated types we can rely on LLVM's GEP to calculate the |
| 1853 | // effective address correctly. |
| 1854 | if(asInternalType(type) == Type_LLVM) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1855 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1856 | return V(jit->builder->CreateGEP(V(ptr), V(index))); |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1859 | // For emulated types we have to multiply the index by the intended |
| 1860 | // type size ourselves to obain the byte offset. |
Ben Clayton | b124373 | 2019-02-27 23:56:18 +0000 | [diff] [blame] | 1861 | index = (sizeof(void*) == 8) ? |
| 1862 | createMul(index, createConstantLong((int64_t)typeSize(type))) : |
| 1863 | createMul(index, createConstantInt((int)typeSize(type))); |
| 1864 | |
Nicolas Capens | 1a5c3b9 | 2019-03-08 17:26:43 -0500 | [diff] [blame] | 1865 | // Cast to a byte pointer, apply the byte offset, and cast back to the |
| 1866 | // original pointer type. |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1867 | return createBitCast( |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1868 | 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] | 1869 | T(llvm::PointerType::get(T(type), 0))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1870 | } |
| 1871 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1872 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 1873 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1874 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1875 | 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] | 1876 | } |
| 1877 | |
Chris Forbes | 707ed99 | 2019-04-18 18:17:35 -0700 | [diff] [blame] | 1878 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1879 | { |
| 1880 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1881 | 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] | 1882 | } |
| 1883 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1884 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1885 | { |
| 1886 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1887 | 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] | 1888 | } |
| 1889 | |
| 1890 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1891 | { |
| 1892 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1893 | 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] | 1894 | } |
| 1895 | |
| 1896 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1897 | { |
| 1898 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1899 | 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] | 1900 | } |
| 1901 | |
| 1902 | Value *Nucleus::createAtomicMin(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1903 | { |
| 1904 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1905 | 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] | 1906 | } |
| 1907 | |
| 1908 | Value *Nucleus::createAtomicMax(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1909 | { |
| 1910 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1911 | 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] | 1912 | } |
| 1913 | |
Chris Forbes | f31bdad | 2019-05-23 14:58:08 -0700 | [diff] [blame] | 1914 | Value *Nucleus::createAtomicUMin(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1915 | { |
| 1916 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1917 | 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] | 1918 | } |
| 1919 | |
| 1920 | Value *Nucleus::createAtomicUMax(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1921 | { |
| 1922 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1923 | 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] | 1924 | } |
| 1925 | |
| 1926 | |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 1927 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1928 | { |
| 1929 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1930 | 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] | 1931 | } |
| 1932 | |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 1933 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1934 | { |
| 1935 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | c9ca99e | 2019-04-19 07:53:34 -0700 | [diff] [blame] | 1936 | // 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] | 1937 | return V(jit->builder->CreateExtractValue( |
| 1938 | 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] | 1939 | llvm::ArrayRef<unsigned>(0u))); |
| 1940 | } |
| 1941 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1942 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1943 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1944 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1945 | return V(jit->builder->CreateTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1946 | } |
| 1947 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1948 | Value *Nucleus::createZExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1949 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1950 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1951 | return V(jit->builder->CreateZExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1952 | } |
| 1953 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1954 | Value *Nucleus::createSExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1955 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1956 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1957 | return V(jit->builder->CreateSExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1958 | } |
| 1959 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1960 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1961 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1962 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1963 | return V(jit->builder->CreateFPToSI(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1964 | } |
| 1965 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1966 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1967 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1968 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1969 | return V(jit->builder->CreateSIToFP(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1970 | } |
| 1971 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1972 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1973 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1974 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1975 | return V(jit->builder->CreateFPTrunc(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1976 | } |
| 1977 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1978 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1979 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1980 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 1981 | return V(jit->builder->CreateFPExt(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1982 | } |
| 1983 | |
Nicolas Capens | 1933654 | 2016-09-26 10:32:29 -0400 | [diff] [blame] | 1984 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1985 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 1986 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1987 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1988 | // support for casting between scalars and wide vectors. Emulate them by writing to the stack and |
| 1989 | // reading back as the destination type. |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1990 | if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1991 | { |
| 1992 | Value *readAddress = allocateStackVariable(destType); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1993 | Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0))); |
| 1994 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1995 | return createLoad(readAddress, destType); |
| 1996 | } |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1997 | else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy()) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 1998 | { |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 1999 | Value *writeAddress = allocateStackVariable(T(V(v)->getType())); |
| 2000 | createStore(v, writeAddress, T(V(v)->getType())); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2001 | Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0))); |
| 2002 | return createLoad(readAddress, destType); |
| 2003 | } |
| 2004 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2005 | return V(jit->builder->CreateBitCast(V(v), T(destType))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2006 | } |
| 2007 | |
Ben Clayton | 204a410 | 2019-07-31 13:17:47 +0100 | [diff] [blame] | 2008 | Value *Nucleus::createPtrEQ(Value *lhs, Value *rhs) |
| 2009 | { |
| 2010 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2011 | return V(jit->builder->CreateICmpEQ(V(lhs), V(rhs))); |
| 2012 | } |
| 2013 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2014 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 2015 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2016 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2017 | return V(jit->builder->CreateICmpEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 2021 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2022 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2023 | return V(jit->builder->CreateICmpNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 2027 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2028 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2029 | return V(jit->builder->CreateICmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 2033 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2034 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2035 | return V(jit->builder->CreateICmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 2039 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2040 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2041 | return V(jit->builder->CreateICmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 2045 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2046 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2047 | return V(jit->builder->CreateICmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 2051 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2052 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2053 | return V(jit->builder->CreateICmpSGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 2057 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2058 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2059 | return V(jit->builder->CreateICmpSGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 2063 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2064 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2065 | return V(jit->builder->CreateICmpSLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 2069 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2070 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2071 | return V(jit->builder->CreateICmpSLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 2075 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2076 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2077 | return V(jit->builder->CreateFCmpOEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 2081 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2082 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2083 | return V(jit->builder->CreateFCmpOGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 2087 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2088 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2089 | return V(jit->builder->CreateFCmpOGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 2093 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2094 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2095 | return V(jit->builder->CreateFCmpOLT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 2099 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2100 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2101 | return V(jit->builder->CreateFCmpOLE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 2105 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2106 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2107 | return V(jit->builder->CreateFCmpONE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 2111 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2112 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2113 | return V(jit->builder->CreateFCmpORD(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 2117 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2118 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2119 | return V(jit->builder->CreateFCmpUNO(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 2123 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2124 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2125 | return V(jit->builder->CreateFCmpUEQ(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 2129 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2130 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2131 | return V(jit->builder->CreateFCmpUGT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 2135 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2136 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2137 | return V(jit->builder->CreateFCmpUGE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 2141 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2142 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2143 | return V(jit->builder->CreateFCmpULT(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 2147 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2148 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2149 | return V(jit->builder->CreateFCmpULE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 2153 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2154 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2155 | return V(jit->builder->CreateFCmpUNE(V(lhs), V(rhs))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2156 | } |
| 2157 | |
Nicolas Capens | e95d534 | 2016-09-30 11:37:28 -0400 | [diff] [blame] | 2158 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2159 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2160 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2161 | ASSERT(V(vector)->getType()->getContainedType(0) == T(type)); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2162 | return V(jit->builder->CreateExtractElement(V(vector), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 2166 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2167 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2168 | return V(jit->builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2169 | } |
| 2170 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2171 | Value *Nucleus::createShuffleVector(Value *v1, Value *v2, const int *select) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2172 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2173 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 2174 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2175 | int size = llvm::cast<llvm::VectorType>(V(v1)->getType())->getNumElements(); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2176 | const int maxSize = 16; |
| 2177 | llvm::Constant *swizzle[maxSize]; |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2178 | ASSERT(size <= maxSize); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2179 | |
| 2180 | for(int i = 0; i < size; i++) |
| 2181 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2182 | swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), select[i]); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(swizzle, size)); |
| 2186 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2187 | return V(jit->builder->CreateShuffleVector(V(v1), V(v2), shuffle)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2188 | } |
| 2189 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2190 | Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2191 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2192 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2193 | return V(jit->builder->CreateSelect(V(c), V(ifTrue), V(ifFalse))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2194 | } |
| 2195 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 2196 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2197 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2198 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2199 | return reinterpret_cast<SwitchCases*>(jit->builder->CreateSwitch(V(control), B(defaultBranch), numCases)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2200 | } |
| 2201 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 2202 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2203 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2204 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2205 | llvm::SwitchInst *sw = reinterpret_cast<llvm::SwitchInst *>(switchCases); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2206 | 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] | 2207 | } |
| 2208 | |
Nicolas Capens | 3d7c35f | 2016-09-28 10:36:57 -0400 | [diff] [blame] | 2209 | void Nucleus::createUnreachable() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2210 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2211 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2212 | jit->builder->CreateUnreachable(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2213 | } |
| 2214 | |
Nicolas Capens | ac23012 | 2016-09-20 14:30:06 -0400 | [diff] [blame] | 2215 | Type *Nucleus::getPointerType(Type *ElementType) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2216 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2217 | return T(llvm::PointerType::get(T(ElementType), 0)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2218 | } |
| 2219 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2220 | Value *Nucleus::createNullValue(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2221 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2222 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2223 | return V(llvm::Constant::getNullValue(T(Ty))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2224 | } |
| 2225 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2226 | Value *Nucleus::createConstantLong(int64_t i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2227 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2228 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2229 | return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(jit->context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2230 | } |
| 2231 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2232 | Value *Nucleus::createConstantInt(int i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2233 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2234 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2235 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2236 | } |
| 2237 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2238 | Value *Nucleus::createConstantInt(unsigned int i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2239 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2240 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2241 | return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2242 | } |
| 2243 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2244 | Value *Nucleus::createConstantBool(bool b) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2245 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2246 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2247 | return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(jit->context), b)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2248 | } |
| 2249 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2250 | Value *Nucleus::createConstantByte(signed char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2251 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2252 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2253 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(jit->context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2254 | } |
| 2255 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2256 | Value *Nucleus::createConstantByte(unsigned char i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2257 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2258 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2259 | return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(jit->context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2260 | } |
| 2261 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2262 | Value *Nucleus::createConstantShort(short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2263 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2264 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2265 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(jit->context), i, true)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2266 | } |
| 2267 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2268 | Value *Nucleus::createConstantShort(unsigned short i) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2269 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2270 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2271 | return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(jit->context), i, false)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2272 | } |
| 2273 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2274 | Value *Nucleus::createConstantFloat(float x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2275 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2276 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2277 | return V(llvm::ConstantFP::get(T(Float::getType()), x)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2278 | } |
| 2279 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2280 | Value *Nucleus::createNullPointer(Type *Ty) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2281 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2282 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2283 | return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2284 | } |
| 2285 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2286 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2287 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2288 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2289 | const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. |
| 2290 | 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] | 2291 | ASSERT(numElements <= 16 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2292 | llvm::Constant *constantVector[16]; |
| 2293 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2294 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2295 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2296 | constantVector[i] = llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2297 | } |
| 2298 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2299 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2303 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2304 | ASSERT(llvm::isa<llvm::VectorType>(T(type))); |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2305 | const int numConstants = elementCount(type); // Number of provided constants for the (emulated) type. |
| 2306 | 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] | 2307 | ASSERT(numElements <= 8 && numConstants <= numElements); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2308 | llvm::Constant *constantVector[8]; |
| 2309 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2310 | for(int i = 0; i < numElements; i++) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2311 | { |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2312 | constantVector[i] = llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 2313 | } |
| 2314 | |
Nicolas Capens | 69674fb | 2017-09-01 11:08:44 -0400 | [diff] [blame] | 2315 | return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2316 | } |
| 2317 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2318 | Type *Void::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2319 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2320 | return T(llvm::Type::getVoidTy(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2321 | } |
| 2322 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2323 | Type *Bool::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2324 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2325 | return T(llvm::Type::getInt1Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2326 | } |
| 2327 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2328 | Type *Byte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2329 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2330 | return T(llvm::Type::getInt8Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2331 | } |
| 2332 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2333 | Type *SByte::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2334 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2335 | return T(llvm::Type::getInt8Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2336 | } |
| 2337 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2338 | Type *Short::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2339 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2340 | return T(llvm::Type::getInt16Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2341 | } |
| 2342 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2343 | Type *UShort::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2344 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2345 | return T(llvm::Type::getInt16Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2346 | } |
| 2347 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2348 | Type *Byte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2349 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2350 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2351 | } |
| 2352 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2353 | Type *SByte4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2354 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2355 | return T(Type_v4i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2356 | } |
| 2357 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2358 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2359 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2360 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2361 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2362 | return x86::paddusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2363 | #else |
| 2364 | return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 2365 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2366 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2367 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2368 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2369 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2370 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2371 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2372 | return x86::psubusb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2373 | #else |
| 2374 | return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 2375 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2376 | } |
| 2377 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2378 | RValue<Int> SignMask(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2379 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2380 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2381 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2382 | return x86::pmovmskb(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2383 | #else |
| 2384 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2385 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2386 | } |
| 2387 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2388 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2389 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2390 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2391 | // return x86::pcmpgtb(x, y); // FIXME: Signedness |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2392 | //#else |
| 2393 | // return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2394 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2395 | // } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2396 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2397 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2398 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2399 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2400 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2401 | return x86::pcmpeqb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2402 | #else |
| 2403 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2404 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2405 | } |
| 2406 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2407 | Type *Byte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2408 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2409 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2410 | } |
| 2411 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2412 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2413 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2414 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2415 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2416 | return x86::paddsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2417 | #else |
| 2418 | return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 2419 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2420 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2421 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2422 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2423 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2424 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2425 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2426 | return x86::psubsb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2427 | #else |
| 2428 | return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 2429 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2430 | } |
| 2431 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2432 | RValue<Int> SignMask(RValue<SByte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2433 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2434 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2435 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2436 | return x86::pmovmskb(As<Byte8>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2437 | #else |
| 2438 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 2439 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2440 | } |
| 2441 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2442 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2443 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2444 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2445 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2446 | return x86::pcmpgtb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2447 | #else |
| 2448 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2449 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2450 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2451 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2452 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2453 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2454 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2455 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2456 | return x86::pcmpeqb(As<Byte8>(x), As<Byte8>(y)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2457 | #else |
| 2458 | return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType())))); |
| 2459 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2460 | } |
| 2461 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2462 | Type *SByte8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2463 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2464 | return T(Type_v8i8); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2465 | } |
| 2466 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2467 | Type *Byte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2468 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2469 | return T(llvm::VectorType::get(T(Byte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2470 | } |
| 2471 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2472 | Type *SByte16::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2473 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2474 | return T(llvm::VectorType::get(T(SByte::getType()), 16)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2475 | } |
| 2476 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2477 | Type *Short2::getType() |
| 2478 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2479 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2480 | } |
| 2481 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2482 | Type *UShort2::getType() |
| 2483 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2484 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2485 | } |
| 2486 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2487 | Short4::Short4(RValue<Int4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2488 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2489 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2490 | int select[8] = {0, 2, 4, 6, 0, 2, 4, 6}; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2491 | Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType()); |
| 2492 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2493 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2494 | Value *short4 = As<Short4>(Int2(As<Int4>(packed))).value; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2495 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2496 | storeValue(short4); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2497 | } |
| 2498 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2499 | // Short4::Short4(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2500 | // { |
| 2501 | // } |
| 2502 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2503 | Short4::Short4(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2504 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2505 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2506 | Int4 v4i32 = Int4(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2507 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2508 | v4i32 = As<Int4>(x86::packssdw(v4i32, v4i32)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2509 | #else |
| 2510 | Value *v = v4i32.loadValue(); |
| 2511 | v4i32 = As<Int4>(V(lowerPack(V(v), V(v), true))); |
| 2512 | #endif |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2513 | |
| 2514 | storeValue(As<Short4>(Int2(v4i32)).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2515 | } |
| 2516 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2517 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2518 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2519 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2520 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2521 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2522 | |
| 2523 | return x86::psllw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2524 | #else |
| 2525 | return As<Short4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2526 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2527 | } |
| 2528 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2529 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2530 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2531 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2532 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2533 | return x86::psraw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2534 | #else |
| 2535 | return As<Short4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2536 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2537 | } |
| 2538 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2539 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2540 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2541 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2542 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2543 | return x86::pmaxsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2544 | #else |
| 2545 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT))); |
| 2546 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2547 | } |
| 2548 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2549 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2550 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2551 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2552 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2553 | return x86::pminsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2554 | #else |
| 2555 | return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT))); |
| 2556 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2557 | } |
| 2558 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2559 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2560 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2561 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2562 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2563 | return x86::paddsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2564 | #else |
| 2565 | return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 2566 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2567 | } |
| 2568 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2569 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2570 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2571 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2572 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2573 | return x86::psubsw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2574 | #else |
| 2575 | return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 2576 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2577 | } |
| 2578 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2579 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2580 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2581 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2582 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2583 | return x86::pmulhw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2584 | #else |
| 2585 | return As<Short4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2586 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2587 | } |
| 2588 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2589 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2590 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2591 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2592 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2593 | return x86::pmaddwd(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2594 | #else |
| 2595 | return As<Int2>(V(lowerMulAdd(V(x.value), V(y.value)))); |
| 2596 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2597 | } |
| 2598 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2599 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2600 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2601 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2602 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2603 | auto result = x86::packsswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2604 | #else |
| 2605 | auto result = V(lowerPack(V(x.value), V(y.value), true)); |
| 2606 | #endif |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2607 | return As<SByte8>(Swizzle(As<Int4>(result), 0x88)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2608 | } |
| 2609 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2610 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2611 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2612 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2613 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2614 | auto result = x86::packuswb(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2615 | #else |
| 2616 | auto result = V(lowerPack(V(x.value), V(y.value), false)); |
| 2617 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2618 | return As<Byte8>(Swizzle(As<Int4>(result), 0x88)); |
| 2619 | } |
| 2620 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2621 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2622 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2623 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2624 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2625 | return x86::pcmpgtw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2626 | #else |
| 2627 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType())))); |
| 2628 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2629 | } |
| 2630 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2631 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2632 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2633 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2634 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2635 | return x86::pcmpeqw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2636 | #else |
| 2637 | return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType())))); |
| 2638 | #endif |
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 | Type *Short4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2642 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2643 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2644 | } |
| 2645 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2646 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2647 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2648 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2649 | if(saturate) |
| 2650 | { |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 2651 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2652 | if(CPUID::supportsSSE4_1()) |
| 2653 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2654 | Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2655 | *this = As<Short4>(PackUnsigned(int4, int4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2656 | } |
| 2657 | else |
Logan Chien | a8385ed | 2018-09-26 19:22:54 +0800 | [diff] [blame] | 2658 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2659 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2660 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2661 | } |
| 2662 | } |
| 2663 | else |
| 2664 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 2665 | *this = Short4(Int4(cast)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2666 | } |
| 2667 | } |
| 2668 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2669 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2670 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2671 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2672 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2673 | // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value)); |
| 2674 | |
| 2675 | return As<UShort4>(x86::psllw(As<Short4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2676 | #else |
| 2677 | return As<UShort4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2678 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2679 | } |
| 2680 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2681 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2682 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2683 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2684 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2685 | // return RValue<Short4>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 2686 | |
| 2687 | return x86::psrlw(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2688 | #else |
| 2689 | return As<UShort4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2690 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2691 | } |
| 2692 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2693 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2694 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2695 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2696 | 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] | 2697 | } |
| 2698 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2699 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2700 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2701 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2702 | 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] | 2703 | } |
| 2704 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2705 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2706 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2707 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2708 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2709 | return x86::paddusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2710 | #else |
| 2711 | return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 2712 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2713 | } |
| 2714 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2715 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2716 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2717 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2718 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2719 | return x86::psubusw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2720 | #else |
| 2721 | return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 2722 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2723 | } |
| 2724 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2725 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2726 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2727 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2728 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2729 | return x86::pmulhuw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2730 | #else |
| 2731 | return As<UShort4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2732 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2733 | } |
| 2734 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2735 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2736 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2737 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2738 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2739 | return x86::pavgw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2740 | #else |
| 2741 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
| 2742 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2743 | } |
| 2744 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2745 | Type *UShort4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2746 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2747 | return T(Type_v4i16); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2748 | } |
| 2749 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2750 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2751 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2752 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2753 | #if defined(__i386__) || defined(__x86_64__) |
| 2754 | return x86::psllw(lhs, rhs); |
| 2755 | #else |
| 2756 | return As<Short8>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2757 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2758 | } |
| 2759 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2760 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2761 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2762 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2763 | #if defined(__i386__) || defined(__x86_64__) |
| 2764 | return x86::psraw(lhs, rhs); |
| 2765 | #else |
| 2766 | return As<Short8>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 2767 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2768 | } |
| 2769 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2770 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2771 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2772 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2773 | #if defined(__i386__) || defined(__x86_64__) |
| 2774 | return x86::pmaddwd(x, y); |
| 2775 | #else |
| 2776 | return As<Int4>(V(lowerMulAdd(V(x.value), V(y.value)))); |
| 2777 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2778 | } |
| 2779 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2780 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2781 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2782 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2783 | #if defined(__i386__) || defined(__x86_64__) |
| 2784 | return x86::pmulhw(x, y); |
| 2785 | #else |
| 2786 | return As<Short8>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 2787 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2788 | } |
| 2789 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2790 | Type *Short8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2791 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2792 | return T(llvm::VectorType::get(T(Short::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2793 | } |
| 2794 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2795 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2796 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2797 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2798 | #if defined(__i386__) || defined(__x86_64__) |
| 2799 | return As<UShort8>(x86::psllw(As<Short8>(lhs), rhs)); |
| 2800 | #else |
| 2801 | return As<UShort8>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 2802 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2803 | } |
| 2804 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2805 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2806 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2807 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2808 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2809 | return x86::psrlw(lhs, rhs); // FIXME: Fallback required |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2810 | #else |
| 2811 | return As<UShort8>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 2812 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2813 | } |
| 2814 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2815 | 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] | 2816 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2817 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2818 | int pshufb[16] = |
| 2819 | { |
| 2820 | select0 + 0, |
| 2821 | select0 + 1, |
| 2822 | select1 + 0, |
| 2823 | select1 + 1, |
| 2824 | select2 + 0, |
| 2825 | select2 + 1, |
| 2826 | select3 + 0, |
| 2827 | select3 + 1, |
| 2828 | select4 + 0, |
| 2829 | select4 + 1, |
| 2830 | select5 + 0, |
| 2831 | select5 + 1, |
| 2832 | select6 + 0, |
| 2833 | select6 + 1, |
| 2834 | select7 + 0, |
| 2835 | select7 + 1, |
| 2836 | }; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2837 | |
| 2838 | Value *byte16 = Nucleus::createBitCast(x.value, Byte16::getType()); |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2839 | Value *shuffle = Nucleus::createShuffleVector(byte16, byte16, pshufb); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2840 | Value *short8 = Nucleus::createBitCast(shuffle, UShort8::getType()); |
| 2841 | |
| 2842 | return RValue<UShort8>(short8); |
| 2843 | } |
| 2844 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2845 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2846 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2847 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2848 | #if defined(__i386__) || defined(__x86_64__) |
| 2849 | return x86::pmulhuw(x, y); |
| 2850 | #else |
| 2851 | return As<UShort8>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 2852 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2853 | } |
| 2854 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2855 | Type *UShort8::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2856 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 2857 | return T(llvm::VectorType::get(T(UShort::getType()), 8)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2858 | } |
| 2859 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2860 | RValue<Int> operator++(Int &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2861 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2862 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2863 | RValue<Int> res = val; |
| 2864 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2865 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2866 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2867 | |
| 2868 | return res; |
| 2869 | } |
| 2870 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2871 | const Int &operator++(Int &val) // Pre-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2872 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2873 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2874 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2875 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2876 | |
| 2877 | return val; |
| 2878 | } |
| 2879 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2880 | RValue<Int> operator--(Int &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2881 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2882 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2883 | RValue<Int> res = val; |
| 2884 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2885 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2886 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2887 | |
| 2888 | return res; |
| 2889 | } |
| 2890 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2891 | const Int &operator--(Int &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2892 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2893 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2894 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2895 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2896 | |
| 2897 | return val; |
| 2898 | } |
| 2899 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2900 | RValue<Int> RoundInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2901 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2902 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2903 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2904 | return x86::cvtss2si(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2905 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 2906 | return RValue<Int>(V(lowerRoundInt(V(cast.value), T(Int::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2907 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2908 | } |
| 2909 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2910 | Type *Int::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2911 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2912 | return T(llvm::Type::getInt32Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2913 | } |
| 2914 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2915 | Type *Long::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2916 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2917 | return T(llvm::Type::getInt64Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2918 | } |
| 2919 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2920 | UInt::UInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2921 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2922 | RR_DEBUG_INFO_UPDATE_LOC(); |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2923 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 2924 | // Value *integer = Nucleus::createFPToUI(cast.value, UInt::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2925 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 2926 | // Smallest positive value representable in UInt, but not in Int |
| 2927 | const unsigned int ustart = 0x80000000u; |
| 2928 | const float ustartf = float(ustart); |
| 2929 | |
| 2930 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2931 | storeValue((~(As<Int>(cast) >> 31) & |
| 2932 | // Check if the value can be represented as an Int |
| 2933 | IfThenElse(cast >= ustartf, |
| 2934 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2935 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 2936 | // Otherwise, just convert normally |
| 2937 | Int(cast))).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2938 | } |
| 2939 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2940 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2941 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2942 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2943 | RValue<UInt> res = val; |
| 2944 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2945 | Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2946 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2947 | |
| 2948 | return res; |
| 2949 | } |
| 2950 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2951 | const UInt &operator++(UInt &val) // Pre-increment |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2952 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2953 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2954 | Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2955 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2956 | |
| 2957 | return val; |
| 2958 | } |
| 2959 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2960 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2961 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2962 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2963 | RValue<UInt> res = val; |
| 2964 | |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2965 | Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2966 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2967 | |
| 2968 | return res; |
| 2969 | } |
| 2970 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2971 | const UInt &operator--(UInt &val) // Pre-decrement |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2972 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 2973 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 2974 | Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1)); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2975 | val.storeValue(inc); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2976 | |
| 2977 | return val; |
| 2978 | } |
| 2979 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2980 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2981 | // { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2982 | //#if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2983 | // return x86::cvtss2si(val); // FIXME: Unsigned |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 2984 | //#else |
| 2985 | // return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f)); |
| 2986 | //#endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2987 | // } |
| 2988 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2989 | Type *UInt::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2990 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 2991 | return T(llvm::Type::getInt32Ty(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2992 | } |
| 2993 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2994 | // Int2::Int2(RValue<Int> cast) |
| 2995 | // { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 2996 | // Value *extend = Nucleus::createZExt(cast.value, Long::getType()); |
| 2997 | // Value *vector = Nucleus::createBitCast(extend, Int2::getType()); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2998 | // |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 2999 | // int shuffle[2] = {0, 0}; |
| 3000 | // Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3001 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3002 | // storeValue(replicate); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3003 | // } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3004 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3005 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -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__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3009 | // return RValue<Int2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 3010 | |
| 3011 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3012 | #else |
| 3013 | return As<Int2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 3014 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3015 | } |
| 3016 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3017 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3018 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3019 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3020 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3021 | // return RValue<Int2>(Nucleus::createAShr(lhs.value, rhs.value)); |
| 3022 | |
| 3023 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3024 | #else |
| 3025 | return As<Int2>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 3026 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3027 | } |
| 3028 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3029 | Type *Int2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3030 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3031 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3032 | } |
| 3033 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3034 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3035 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3036 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3037 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3038 | // return RValue<UInt2>(Nucleus::createShl(lhs.value, rhs.value)); |
| 3039 | |
| 3040 | return As<UInt2>(x86::pslld(As<Int2>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3041 | #else |
| 3042 | return As<UInt2>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 3043 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3044 | } |
| 3045 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3046 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3047 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3048 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3049 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3050 | // return RValue<UInt2>(Nucleus::createLShr(lhs.value, rhs.value)); |
| 3051 | |
| 3052 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3053 | #else |
| 3054 | return As<UInt2>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 3055 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3056 | } |
| 3057 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3058 | Type *UInt2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3059 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3060 | return T(Type_v2i32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3061 | } |
| 3062 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3063 | Int4::Int4(RValue<Byte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3064 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3065 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3066 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3067 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3068 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3069 | *this = x86::pmovzxbd(As<Byte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3070 | } |
| 3071 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3072 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3073 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3074 | 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] | 3075 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 3076 | Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::getType()), swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3077 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3078 | int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3079 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
Logan Chien | 191b305 | 2018-08-31 16:57:15 +0800 | [diff] [blame] | 3080 | Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::getType()), swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3081 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3082 | *this = As<Int4>(d); |
| 3083 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3084 | } |
| 3085 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3086 | Int4::Int4(RValue<SByte4> cast) : XYZW(this) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3087 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3088 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3089 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3090 | if(CPUID::supportsSSE4_1()) |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3091 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3092 | *this = x86::pmovsxbd(As<SByte16>(cast)); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3093 | } |
| 3094 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3095 | #endif |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3096 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3097 | int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; |
| 3098 | Value *a = Nucleus::createBitCast(cast.value, Byte16::getType()); |
| 3099 | Value *b = Nucleus::createShuffleVector(a, a, swizzle); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3100 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3101 | int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3102 | Value *c = Nucleus::createBitCast(b, Short8::getType()); |
| 3103 | Value *d = Nucleus::createShuffleVector(c, c, swizzle2); |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3104 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3105 | *this = As<Int4>(d) >> 24; |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3106 | } |
Meng-Lin Wu | 601d005 | 2016-06-10 14:18:41 -0400 | [diff] [blame] | 3107 | } |
| 3108 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3109 | Int4::Int4(RValue<Short4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3110 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3111 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3112 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3113 | if(CPUID::supportsSSE4_1()) |
| 3114 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3115 | *this = x86::pmovsxwd(As<Short8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3116 | } |
| 3117 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3118 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3119 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3120 | int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3121 | Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle); |
| 3122 | *this = As<Int4>(c) >> 16; |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3126 | Int4::Int4(RValue<UShort4> cast) : XYZW(this) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3127 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3128 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3129 | #if defined(__i386__) || defined(__x86_64__) |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3130 | if(CPUID::supportsSSE4_1()) |
| 3131 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3132 | *this = x86::pmovzxwd(As<UShort8>(cast)); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3133 | } |
| 3134 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3135 | #endif |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3136 | { |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3137 | int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3138 | Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
| 3139 | *this = As<Int4>(c); |
Alexis Hetu | 2aa852f | 2015-10-14 16:32:39 -0400 | [diff] [blame] | 3140 | } |
| 3141 | } |
| 3142 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3143 | Int4::Int4(RValue<Int> rhs) : XYZW(this) |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 3144 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3145 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 3146 | Value *vector = loadValue(); |
| 3147 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 3148 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3149 | int swizzle[4] = {0, 0, 0, 0}; |
| 3150 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
Nicolas Capens | 24c8cf0 | 2016-08-15 15:33:14 -0400 | [diff] [blame] | 3151 | |
| 3152 | storeValue(replicate); |
| 3153 | } |
| 3154 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3155 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3156 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3157 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3158 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3159 | return x86::pslld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3160 | #else |
| 3161 | return As<Int4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 3162 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3163 | } |
| 3164 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3165 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3166 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3167 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3168 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3169 | return x86::psrad(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3170 | #else |
| 3171 | return As<Int4>(V(lowerVectorAShr(V(lhs.value), rhs))); |
| 3172 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3173 | } |
| 3174 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3175 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 3176 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3177 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3178 | 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] | 3179 | } |
| 3180 | |
| 3181 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 3182 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3183 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3184 | 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] | 3185 | } |
| 3186 | |
| 3187 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 3188 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3189 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3190 | 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] | 3191 | } |
| 3192 | |
| 3193 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 3194 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3195 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3196 | 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] | 3197 | } |
| 3198 | |
| 3199 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 3200 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3201 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3202 | 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] | 3203 | } |
| 3204 | |
| 3205 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3206 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3207 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3208 | 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] | 3209 | } |
| 3210 | |
| 3211 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3212 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3213 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3214 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3215 | if(CPUID::supportsSSE4_1()) |
| 3216 | { |
| 3217 | return x86::pmaxsd(x, y); |
| 3218 | } |
| 3219 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3220 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3221 | { |
| 3222 | RValue<Int4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3223 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3228 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3229 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3230 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3231 | if(CPUID::supportsSSE4_1()) |
| 3232 | { |
| 3233 | return x86::pminsd(x, y); |
| 3234 | } |
| 3235 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3236 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3237 | { |
| 3238 | RValue<Int4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3239 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3240 | } |
| 3241 | } |
| 3242 | |
| 3243 | RValue<Int4> RoundInt(RValue<Float4> cast) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3244 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3245 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3246 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3247 | return x86::cvtps2dq(cast); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3248 | #else |
Logan Chien | 2faa24a | 2018-09-26 19:59:32 +0800 | [diff] [blame] | 3249 | return As<Int4>(V(lowerRoundInt(V(cast.value), T(Int4::getType())))); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3250 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3251 | } |
| 3252 | |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3253 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 3254 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3255 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3256 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3257 | return As<Int4>(V(lowerMulHigh(V(x.value), V(y.value), true))); |
| 3258 | } |
| 3259 | |
| 3260 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 3261 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3262 | RR_DEBUG_INFO_UPDATE_LOC(); |
Chris Forbes | e86b6dc | 2019-03-01 09:08:47 -0800 | [diff] [blame] | 3263 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3264 | return As<UInt4>(V(lowerMulHigh(V(x.value), V(y.value), false))); |
| 3265 | } |
| 3266 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3267 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3268 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3269 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3270 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3271 | return x86::packssdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3272 | #else |
| 3273 | return As<Short8>(V(lowerPack(V(x.value), V(y.value), true))); |
| 3274 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3275 | } |
| 3276 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3277 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3278 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3279 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3280 | #if defined(__i386__) || defined(__x86_64__) |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3281 | return x86::packusdw(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3282 | #else |
| 3283 | return As<UShort8>(V(lowerPack(V(x.value), V(y.value), false))); |
| 3284 | #endif |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3285 | } |
| 3286 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3287 | RValue<Int> SignMask(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3288 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3289 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3290 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3291 | return x86::movmskps(As<Float4>(x)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3292 | #else |
| 3293 | return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType())))); |
| 3294 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3295 | } |
| 3296 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3297 | Type *Int4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3298 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3299 | return T(llvm::VectorType::get(T(Int::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3300 | } |
| 3301 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3302 | UInt4::UInt4(RValue<Float4> cast) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3303 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3304 | RR_DEBUG_INFO_UPDATE_LOC(); |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 3305 | // Note: createFPToUI is broken, must perform conversion using createFPtoSI |
| 3306 | // Value *xyzw = Nucleus::createFPToUI(cast.value, UInt4::getType()); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3307 | |
Alexis Hetu | 764d142 | 2016-09-28 08:44:22 -0400 | [diff] [blame] | 3308 | // Smallest positive value representable in UInt, but not in Int |
| 3309 | const unsigned int ustart = 0x80000000u; |
| 3310 | const float ustartf = float(ustart); |
| 3311 | |
| 3312 | // Check if the value can be represented as an Int |
| 3313 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 3314 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3315 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
| 3316 | // Otherwise, just convert normally |
| 3317 | (~uiValue & Int4(cast)); |
| 3318 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 3319 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3320 | } |
| 3321 | |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3322 | UInt4::UInt4(RValue<UInt> rhs) : XYZW(this) |
| 3323 | { |
| 3324 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3325 | Value *vector = loadValue(); |
| 3326 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 3327 | |
| 3328 | int swizzle[4] = {0, 0, 0, 0}; |
| 3329 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
| 3330 | |
| 3331 | storeValue(replicate); |
| 3332 | } |
| 3333 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3334 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3335 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3336 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3337 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3338 | return As<UInt4>(x86::pslld(As<Int4>(lhs), rhs)); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3339 | #else |
| 3340 | return As<UInt4>(V(lowerVectorShl(V(lhs.value), rhs))); |
| 3341 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3342 | } |
| 3343 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3344 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3345 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3346 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3347 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3348 | return x86::psrld(lhs, rhs); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3349 | #else |
| 3350 | return As<UInt4>(V(lowerVectorLShr(V(lhs.value), rhs))); |
| 3351 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3352 | } |
| 3353 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3354 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3355 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3356 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3357 | 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] | 3358 | } |
| 3359 | |
| 3360 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3361 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3362 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3363 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULT(x.value, y.value), Int4::getType())); |
| 3364 | } |
| 3365 | |
| 3366 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3367 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3368 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3369 | 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] | 3370 | } |
| 3371 | |
| 3372 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3373 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3374 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3375 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType())); |
| 3376 | } |
| 3377 | |
| 3378 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3379 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3380 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | a7edc1c | 2019-06-20 12:17:03 +0100 | [diff] [blame] | 3381 | 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] | 3382 | } |
| 3383 | |
| 3384 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3385 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3386 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3387 | return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGT(x.value, y.value), Int4::getType())); |
| 3388 | } |
| 3389 | |
| 3390 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 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 | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3394 | if(CPUID::supportsSSE4_1()) |
| 3395 | { |
| 3396 | return x86::pmaxud(x, y); |
| 3397 | } |
| 3398 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3399 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3400 | { |
| 3401 | RValue<UInt4> greater = CmpNLE(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3402 | return (x & greater) | (y & ~greater); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3407 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3408 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3409 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3410 | if(CPUID::supportsSSE4_1()) |
| 3411 | { |
| 3412 | return x86::pminud(x, y); |
| 3413 | } |
| 3414 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3415 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3416 | { |
| 3417 | RValue<UInt4> less = CmpLT(x, y); |
Tom Anderson | 69bc6e8 | 2017-03-20 11:54:29 -0700 | [diff] [blame] | 3418 | return (x & less) | (y & ~less); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3419 | } |
| 3420 | } |
| 3421 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3422 | Type *UInt4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3423 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3424 | return T(llvm::VectorType::get(T(UInt::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3425 | } |
| 3426 | |
Alexis Hetu | 734e257 | 2018-12-20 14:00:49 -0500 | [diff] [blame] | 3427 | Type *Half::getType() |
| 3428 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3429 | return T(llvm::Type::getInt16Ty(jit->context)); |
Alexis Hetu | 734e257 | 2018-12-20 14:00:49 -0500 | [diff] [blame] | 3430 | } |
| 3431 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 3432 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3433 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3434 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3435 | #if defined(__i386__) || defined(__x86_64__) |
| 3436 | if(exactAtPow2) |
| 3437 | { |
| 3438 | // rcpss uses a piecewise-linear approximation which minimizes the relative error |
| 3439 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 3440 | return x86::rcpss(x) * Float(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 3441 | } |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 3442 | return x86::rcpss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3443 | #else |
| 3444 | return As<Float>(V(lowerRCP(V(x.value)))); |
| 3445 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3446 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3447 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3448 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3449 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3450 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3451 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3452 | return x86::rsqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3453 | #else |
| 3454 | return As<Float>(V(lowerRSQRT(V(x.value)))); |
| 3455 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3456 | } |
| 3457 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3458 | RValue<Float> Sqrt(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3459 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3460 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3461 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3462 | return x86::sqrtss(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3463 | #else |
| 3464 | return As<Float>(V(lowerSQRT(V(x.value)))); |
| 3465 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3466 | } |
| 3467 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3468 | RValue<Float> Round(RValue<Float> x) |
| 3469 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3470 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3471 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3472 | if(CPUID::supportsSSE4_1()) |
| 3473 | { |
| 3474 | return x86::roundss(x, 0); |
| 3475 | } |
| 3476 | else |
| 3477 | { |
| 3478 | return Float4(Round(Float4(x))).x; |
| 3479 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 3480 | #else |
| 3481 | return RValue<Float>(V(lowerRound(V(x.value)))); |
| 3482 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3483 | } |
| 3484 | |
| 3485 | RValue<Float> Trunc(RValue<Float> x) |
| 3486 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3487 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3488 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3489 | if(CPUID::supportsSSE4_1()) |
| 3490 | { |
| 3491 | return x86::roundss(x, 3); |
| 3492 | } |
| 3493 | else |
| 3494 | { |
| 3495 | return Float(Int(x)); // Rounded toward zero |
| 3496 | } |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 3497 | #else |
| 3498 | return RValue<Float>(V(lowerTrunc(V(x.value)))); |
| 3499 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | RValue<Float> Frac(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3503 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3504 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3505 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3506 | if(CPUID::supportsSSE4_1()) |
| 3507 | { |
| 3508 | return x - x86::floorss(x); |
| 3509 | } |
| 3510 | else |
| 3511 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3512 | return Float4(Frac(Float4(x))).x; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3513 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 3514 | #else |
| 3515 | // x - floor(x) can be 1.0 for very small negative x. |
| 3516 | // Clamp against the value just below 1.0. |
| 3517 | return Min(x - Floor(x), As<Float>(Int(0x3F7FFFFF))); |
| 3518 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3519 | } |
| 3520 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3521 | RValue<Float> Floor(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3522 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3523 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3524 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3525 | if(CPUID::supportsSSE4_1()) |
| 3526 | { |
| 3527 | return x86::floorss(x); |
| 3528 | } |
| 3529 | else |
| 3530 | { |
| 3531 | return Float4(Floor(Float4(x))).x; |
| 3532 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3533 | #else |
| 3534 | return RValue<Float>(V(lowerFloor(V(x.value)))); |
| 3535 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3536 | } |
| 3537 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3538 | RValue<Float> Ceil(RValue<Float> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3539 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3540 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3541 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3542 | if(CPUID::supportsSSE4_1()) |
| 3543 | { |
| 3544 | return x86::ceilss(x); |
| 3545 | } |
| 3546 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3547 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3548 | { |
| 3549 | return Float4(Ceil(Float4(x))).x; |
| 3550 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3551 | } |
| 3552 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3553 | Type *Float::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3554 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3555 | return T(llvm::Type::getFloatTy(jit->context)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3556 | } |
| 3557 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3558 | Type *Float2::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3559 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3560 | return T(Type_v2f32); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3561 | } |
| 3562 | |
Nicolas Capens | 88ac367 | 2019-08-01 13:22:34 -0400 | [diff] [blame] | 3563 | RValue<Float> Exp2(RValue<Float> v) |
| 3564 | { |
| 3565 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::exp2, { T(Float::getType()) } ); |
| 3566 | return RValue<Float>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 3567 | } |
| 3568 | |
| 3569 | RValue<Float> Log2(RValue<Float> v) |
| 3570 | { |
| 3571 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::log2, { T(Float::getType()) } ); |
| 3572 | return RValue<Float>(V(jit->builder->CreateCall(func, V(v.value)))); |
| 3573 | } |
| 3574 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3575 | Float4::Float4(RValue<Float> rhs) : XYZW(this) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3576 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3577 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3578 | Value *vector = loadValue(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3579 | Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0); |
| 3580 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 3581 | int swizzle[4] = {0, 0, 0, 0}; |
| 3582 | Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3583 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3584 | storeValue(replicate); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3585 | } |
| 3586 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3587 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3588 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3589 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3590 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3591 | return x86::maxps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3592 | #else |
| 3593 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OGT))); |
| 3594 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3595 | } |
| 3596 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3597 | RValue<Float4> Min(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(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3600 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3601 | return x86::minps(x, y); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3602 | #else |
| 3603 | return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OLT))); |
| 3604 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3605 | } |
| 3606 | |
Nicolas Capens | 05b3d66 | 2016-02-25 23:58:33 -0500 | [diff] [blame] | 3607 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3608 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3609 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3610 | #if defined(__i386__) || defined(__x86_64__) |
| 3611 | if(exactAtPow2) |
| 3612 | { |
| 3613 | // rcpps uses a piecewise-linear approximation which minimizes the relative error |
| 3614 | // but is not exact at power-of-two values. Rectify by multiplying by the inverse. |
| 3615 | return x86::rcpps(x) * Float4(1.0f / _mm_cvtss_f32(_mm_rcp_ss(_mm_set_ps1(1.0f)))); |
| 3616 | } |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 3617 | return x86::rcpps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3618 | #else |
| 3619 | return As<Float4>(V(lowerRCP(V(x.value)))); |
| 3620 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3621 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3622 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3623 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3624 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3625 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3626 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3627 | return x86::rsqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3628 | #else |
| 3629 | return As<Float4>(V(lowerRSQRT(V(x.value)))); |
| 3630 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3631 | } |
| 3632 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3633 | RValue<Float4> Sqrt(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3634 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3635 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3636 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3637 | return x86::sqrtps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3638 | #else |
| 3639 | return As<Float4>(V(lowerSQRT(V(x.value)))); |
| 3640 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3641 | } |
| 3642 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3643 | RValue<Int> SignMask(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3644 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3645 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3646 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3647 | return x86::movmskps(x); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3648 | #else |
| 3649 | return As<Int>(V(lowerFPSignMask(V(x.value), T(Int::getType())))); |
| 3650 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3651 | } |
| 3652 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3653 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3654 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3655 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3656 | // return As<Int4>(x86::cmpeqps(x, y)); |
| 3657 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value, y.value), Int4::getType())); |
| 3658 | } |
| 3659 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3660 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3661 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3662 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3663 | // return As<Int4>(x86::cmpltps(x, y)); |
| 3664 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value, y.value), Int4::getType())); |
| 3665 | } |
| 3666 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3667 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3668 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3669 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3670 | // return As<Int4>(x86::cmpleps(x, y)); |
| 3671 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value, y.value), Int4::getType())); |
| 3672 | } |
| 3673 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3674 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3675 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3676 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3677 | // return As<Int4>(x86::cmpneqps(x, y)); |
| 3678 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value, y.value), Int4::getType())); |
| 3679 | } |
| 3680 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3681 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3682 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3683 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3684 | // return As<Int4>(x86::cmpnltps(x, y)); |
| 3685 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value, y.value), Int4::getType())); |
| 3686 | } |
| 3687 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3688 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3689 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3690 | RR_DEBUG_INFO_UPDATE_LOC(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3691 | // return As<Int4>(x86::cmpnleps(x, y)); |
| 3692 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value, y.value), Int4::getType())); |
| 3693 | } |
| 3694 | |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3695 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 3696 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3697 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3698 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUEQ(x.value, y.value), Int4::getType())); |
| 3699 | } |
| 3700 | |
| 3701 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 3702 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3703 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3704 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULT(x.value, y.value), Int4::getType())); |
| 3705 | } |
| 3706 | |
| 3707 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 3708 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3709 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3710 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpULE(x.value, y.value), Int4::getType())); |
| 3711 | } |
| 3712 | |
| 3713 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 3714 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3715 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3716 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUNE(x.value, y.value), Int4::getType())); |
| 3717 | } |
| 3718 | |
| 3719 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 3720 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3721 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3722 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGE(x.value, y.value), Int4::getType())); |
| 3723 | } |
| 3724 | |
| 3725 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 3726 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3727 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3728 | return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value, y.value), Int4::getType())); |
| 3729 | } |
| 3730 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3731 | RValue<Float4> Round(RValue<Float4> x) |
| 3732 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3733 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3734 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3735 | if(CPUID::supportsSSE4_1()) |
| 3736 | { |
| 3737 | return x86::roundps(x, 0); |
| 3738 | } |
| 3739 | else |
| 3740 | { |
| 3741 | return Float4(RoundInt(x)); |
| 3742 | } |
Logan Chien | 83fc07a | 2018-09-26 22:14:00 +0800 | [diff] [blame] | 3743 | #else |
| 3744 | return RValue<Float4>(V(lowerRound(V(x.value)))); |
| 3745 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | RValue<Float4> Trunc(RValue<Float4> x) |
| 3749 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3750 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3751 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3752 | if(CPUID::supportsSSE4_1()) |
| 3753 | { |
| 3754 | return x86::roundps(x, 3); |
| 3755 | } |
| 3756 | else |
| 3757 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3758 | return Float4(Int4(x)); |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3759 | } |
Logan Chien | 8c5ca8d | 2018-09-27 21:05:53 +0800 | [diff] [blame] | 3760 | #else |
| 3761 | return RValue<Float4>(V(lowerTrunc(V(x.value)))); |
| 3762 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | RValue<Float4> Frac(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3766 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3767 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3768 | Float4 frc; |
| 3769 | |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3770 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3771 | if(CPUID::supportsSSE4_1()) |
| 3772 | { |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 3773 | frc = x - Floor(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3774 | } |
| 3775 | else |
| 3776 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3777 | frc = x - Float4(Int4(x)); // Signed fractional part. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3778 | |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3779 | 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] | 3780 | } |
Logan Chien | 3c6a1ae | 2018-09-26 22:18:16 +0800 | [diff] [blame] | 3781 | #else |
| 3782 | frc = x - Floor(x); |
| 3783 | #endif |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3784 | |
| 3785 | // x - floor(x) can be 1.0 for very small negative x. |
| 3786 | // Clamp against the value just below 1.0. |
| 3787 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3788 | } |
| 3789 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3790 | RValue<Float4> Floor(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3791 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3792 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3793 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3794 | if(CPUID::supportsSSE4_1()) |
| 3795 | { |
| 3796 | return x86::floorps(x); |
| 3797 | } |
| 3798 | else |
| 3799 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3800 | return x - Frac(x); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3801 | } |
Logan Chien | 40a6005 | 2018-09-26 19:03:53 +0800 | [diff] [blame] | 3802 | #else |
| 3803 | return RValue<Float4>(V(lowerFloor(V(x.value)))); |
| 3804 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3805 | } |
| 3806 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3807 | RValue<Float4> Ceil(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3808 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3809 | RR_DEBUG_INFO_UPDATE_LOC(); |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3810 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3811 | if(CPUID::supportsSSE4_1()) |
| 3812 | { |
| 3813 | return x86::ceilps(x); |
| 3814 | } |
| 3815 | else |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 3816 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3817 | { |
| 3818 | return -Floor(-x); |
| 3819 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3820 | } |
| 3821 | |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 3822 | RValue<Float4> Sin(RValue<Float4> v) |
| 3823 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3824 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::sin, { V(v.value)->getType() } ); |
| 3825 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | a2c8b77 | 2019-04-09 13:42:36 -0400 | [diff] [blame] | 3826 | } |
| 3827 | |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 3828 | RValue<Float4> Cos(RValue<Float4> v) |
| 3829 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3830 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cos, { V(v.value)->getType() } ); |
| 3831 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 1b6f8c7 | 2019-04-09 13:47:43 -0400 | [diff] [blame] | 3832 | } |
| 3833 | |
Ben Clayton | 1474006 | 2019-04-09 13:48:41 -0400 | [diff] [blame] | 3834 | RValue<Float4> Tan(RValue<Float4> v) |
| 3835 | { |
| 3836 | return Sin(v) / Cos(v); |
| 3837 | } |
| 3838 | |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 3839 | static RValue<Float4> TransformFloat4PerElement(RValue<Float4> v, const char* name) |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3840 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3841 | 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] | 3842 | auto func = jit->module->getOrInsertFunction(name, funcTy); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3843 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
| 3844 | for (uint64_t i = 0; i < 4; i++) |
| 3845 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3846 | 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] | 3847 | out = V(Nucleus::createInsertElement(V(out), V(el), i)); |
Ben Clayton | f9350d7 | 2019-04-09 14:19:02 -0400 | [diff] [blame] | 3848 | } |
| 3849 | return RValue<Float4>(V(out)); |
| 3850 | } |
| 3851 | |
Ben Clayton | eafae47 | 2019-04-09 14:22:38 -0400 | [diff] [blame] | 3852 | RValue<Float4> Asin(RValue<Float4> v) |
| 3853 | { |
| 3854 | return TransformFloat4PerElement(v, "asinf"); |
| 3855 | } |
| 3856 | |
| 3857 | RValue<Float4> Acos(RValue<Float4> v) |
| 3858 | { |
| 3859 | return TransformFloat4PerElement(v, "acosf"); |
| 3860 | } |
| 3861 | |
Ben Clayton | 749b4e0 | 2019-04-09 14:27:43 -0400 | [diff] [blame] | 3862 | RValue<Float4> Atan(RValue<Float4> v) |
| 3863 | { |
| 3864 | return TransformFloat4PerElement(v, "atanf"); |
| 3865 | } |
| 3866 | |
Ben Clayton | d963697 | 2019-04-09 15:09:54 -0400 | [diff] [blame] | 3867 | RValue<Float4> Sinh(RValue<Float4> v) |
| 3868 | { |
Chris Forbes | 4d838a8 | 2019-09-04 19:24:02 +0100 | [diff] [blame] | 3869 | return Float4(0.5f) * (Exp(v) - Exp(-v)); |
Ben Clayton | d963697 | 2019-04-09 15:09:54 -0400 | [diff] [blame] | 3870 | } |
| 3871 | |
Ben Clayton | 900ea2c | 2019-04-09 15:25:36 -0400 | [diff] [blame] | 3872 | RValue<Float4> Cosh(RValue<Float4> v) |
| 3873 | { |
Chris Forbes | 4d838a8 | 2019-09-04 19:24:02 +0100 | [diff] [blame] | 3874 | return Float4(0.5f) * (Exp(v) + Exp(-v)); |
Ben Clayton | 900ea2c | 2019-04-09 15:25:36 -0400 | [diff] [blame] | 3875 | } |
| 3876 | |
Ben Clayton | 3928bd9 | 2019-04-09 15:27:41 -0400 | [diff] [blame] | 3877 | RValue<Float4> Tanh(RValue<Float4> v) |
| 3878 | { |
| 3879 | return TransformFloat4PerElement(v, "tanhf"); |
| 3880 | } |
| 3881 | |
Ben Clayton | f6d77ab | 2019-04-09 15:30:04 -0400 | [diff] [blame] | 3882 | RValue<Float4> Asinh(RValue<Float4> v) |
| 3883 | { |
| 3884 | return TransformFloat4PerElement(v, "asinhf"); |
| 3885 | } |
| 3886 | |
Ben Clayton | 28ebcb0 | 2019-04-09 15:33:38 -0400 | [diff] [blame] | 3887 | RValue<Float4> Acosh(RValue<Float4> v) |
| 3888 | { |
| 3889 | return TransformFloat4PerElement(v, "acoshf"); |
| 3890 | } |
| 3891 | |
Ben Clayton | fa6a539 | 2019-04-09 15:35:24 -0400 | [diff] [blame] | 3892 | RValue<Float4> Atanh(RValue<Float4> v) |
| 3893 | { |
| 3894 | return TransformFloat4PerElement(v, "atanhf"); |
| 3895 | } |
| 3896 | |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3897 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 3898 | { |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3899 | ::llvm::SmallVector<::llvm::Type*, 2> paramTys; |
| 3900 | paramTys.push_back(T(Float::getType())); |
| 3901 | paramTys.push_back(T(Float::getType())); |
| 3902 | auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), paramTys, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3903 | auto func = jit->module->getOrInsertFunction("atan2f", funcTy); |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3904 | llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType())); |
| 3905 | for (uint64_t i = 0; i < 4; i++) |
| 3906 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3907 | auto el = jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3908 | V(Nucleus::createExtractElement(x.value, Float::getType(), i)), |
| 3909 | V(Nucleus::createExtractElement(y.value, Float::getType(), i)) |
| 3910 | )); |
| 3911 | out = V(Nucleus::createInsertElement(V(out), V(el), i)); |
Ben Clayton | a520c3e | 2019-04-09 15:43:45 -0400 | [diff] [blame] | 3912 | } |
| 3913 | return RValue<Float4>(V(out)); |
| 3914 | } |
| 3915 | |
Ben Clayton | bfe94f0 | 2019-04-09 15:52:12 -0400 | [diff] [blame] | 3916 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 3917 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3918 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::pow, { T(Float4::getType()) }); |
| 3919 | 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] | 3920 | } |
| 3921 | |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 3922 | RValue<Float4> Exp(RValue<Float4> v) |
| 3923 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3924 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::exp, { T(Float4::getType()) } ); |
| 3925 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 242f002 | 2019-04-09 16:00:53 -0400 | [diff] [blame] | 3926 | } |
| 3927 | |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 3928 | RValue<Float4> Log(RValue<Float4> v) |
| 3929 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3930 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::log, { T(Float4::getType()) } ); |
| 3931 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | 2c1da72 | 2019-04-09 16:03:03 -0400 | [diff] [blame] | 3932 | } |
| 3933 | |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 3934 | RValue<Float4> Exp2(RValue<Float4> v) |
| 3935 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3936 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::exp2, { T(Float4::getType()) } ); |
| 3937 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | f40b56c | 2019-04-09 16:06:55 -0400 | [diff] [blame] | 3938 | } |
| 3939 | |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 3940 | RValue<Float4> Log2(RValue<Float4> v) |
| 3941 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3942 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::log2, { T(Float4::getType()) } ); |
| 3943 | return RValue<Float4>(V(jit->builder->CreateCall(func, V(v.value)))); |
Ben Clayton | e17acfe | 2019-04-09 16:09:13 -0400 | [diff] [blame] | 3944 | } |
| 3945 | |
Ben Clayton | ff6e8c0 | 2019-08-06 21:23:44 +0100 | [diff] [blame] | 3946 | RValue<UInt> Ctlz(RValue<UInt> v, bool isZeroUndef) |
| 3947 | { |
| 3948 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::ctlz, { T(UInt::getType()) } ); |
| 3949 | return RValue<UInt>(V(jit->builder->CreateCall2(func, ARGS( |
| 3950 | V(v.value), |
| 3951 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context) |
| 3952 | )))); |
| 3953 | } |
| 3954 | |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3955 | RValue<UInt4> Ctlz(RValue<UInt4> v, bool isZeroUndef) |
| 3956 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3957 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::ctlz, { T(UInt4::getType()) } ); |
| 3958 | return RValue<UInt4>(V(jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3959 | V(v.value), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3960 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context) |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3961 | )))); |
Ben Clayton | 6095826 | 2019-04-10 14:53:30 -0400 | [diff] [blame] | 3962 | } |
| 3963 | |
Ben Clayton | ff6e8c0 | 2019-08-06 21:23:44 +0100 | [diff] [blame] | 3964 | RValue<UInt> Cttz(RValue<UInt> v, bool isZeroUndef) |
| 3965 | { |
| 3966 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cttz, { T(UInt::getType()) } ); |
| 3967 | return RValue<UInt>(V(jit->builder->CreateCall2(func, ARGS( |
| 3968 | V(v.value), |
| 3969 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context) |
| 3970 | )))); |
| 3971 | } |
| 3972 | |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3973 | RValue<UInt4> Cttz(RValue<UInt4> v, bool isZeroUndef) |
| 3974 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3975 | auto func = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::cttz, { T(UInt4::getType()) } ); |
| 3976 | return RValue<UInt4>(V(jit->builder->CreateCall2(func, ARGS( |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3977 | V(v.value), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3978 | isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context) |
Ben Clayton | c38fc12 | 2019-04-11 08:58:49 -0400 | [diff] [blame] | 3979 | )))); |
Ben Clayton | 3f007c4 | 2019-04-10 14:54:23 -0400 | [diff] [blame] | 3980 | } |
| 3981 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 3982 | Type *Float4::getType() |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3983 | { |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 3984 | return T(llvm::VectorType::get(T(Float::getType()), 4)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3985 | } |
| 3986 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3987 | RValue<Long> Ticks() |
| 3988 | { |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 3989 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3990 | llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::readcyclecounter); |
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 | return RValue<Long>(V(jit->builder->CreateCall(rdtsc))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3993 | } |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 3994 | |
| 3995 | RValue<Pointer<Byte>> ConstantPointer(void const * ptr) |
| 3996 | { |
| 3997 | // Note: this should work for 32-bit pointers as well because 'inttoptr' |
| 3998 | // is defined to truncate (and zero extend) if necessary. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 3999 | auto ptrAsInt = ::llvm::ConstantInt::get(::llvm::Type::getInt64Ty(jit->context), reinterpret_cast<uintptr_t>(ptr)); |
| 4000 | 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] | 4001 | } |
| 4002 | |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 4003 | RValue<Pointer<Byte>> ConstantData(void const * data, size_t size) |
| 4004 | { |
| 4005 | auto str = ::llvm::StringRef(reinterpret_cast<const char*>(data), size); |
| 4006 | auto ptr = jit->builder->CreateGlobalStringPtr(str); |
| 4007 | return RValue<Pointer<Byte>>(V(ptr)); |
| 4008 | } |
| 4009 | |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 4010 | Value* Call(RValue<Pointer<Byte>> fptr, Type* retTy, std::initializer_list<Value*> args, std::initializer_list<Type*> argTys) |
| 4011 | { |
| 4012 | ::llvm::SmallVector<::llvm::Type*, 8> paramTys; |
| 4013 | for (auto ty : argTys) { paramTys.push_back(T(ty)); } |
| 4014 | auto funcTy = ::llvm::FunctionType::get(T(retTy), paramTys, false); |
| 4015 | |
| 4016 | auto funcPtrTy = funcTy->getPointerTo(); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4017 | auto funcPtr = jit->builder->CreatePointerCast(V(fptr.value), funcPtrTy); |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 4018 | |
| 4019 | ::llvm::SmallVector<::llvm::Value*, 8> arguments; |
| 4020 | for (auto arg : args) { arguments.push_back(V(arg)); } |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4021 | return V(jit->builder->CreateCall(funcPtr, arguments)); |
Ben Clayton | d853c12 | 2019-04-16 17:51:49 -0400 | [diff] [blame] | 4022 | } |
Nicolas Capens | 9770a46 | 2019-06-25 10:47:10 -0400 | [diff] [blame] | 4023 | |
| 4024 | void Breakpoint() |
| 4025 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4026 | llvm::Function *debugtrap = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::debugtrap); |
Nicolas Capens | 9770a46 | 2019-06-25 10:47:10 -0400 | [diff] [blame] | 4027 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4028 | jit->builder->CreateCall(debugtrap); |
Nicolas Capens | 9770a46 | 2019-06-25 10:47:10 -0400 | [diff] [blame] | 4029 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4030 | } |
| 4031 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 4032 | namespace rr |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4033 | { |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 4034 | #if defined(__i386__) || defined(__x86_64__) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4035 | namespace x86 |
| 4036 | { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4037 | RValue<Int> cvtss2si(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4038 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4039 | 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] | 4040 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4041 | Float4 vector; |
| 4042 | vector.x = val; |
| 4043 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4044 | 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] | 4045 | } |
| 4046 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4047 | RValue<Int4> cvtps2dq(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4048 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4049 | 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] | 4050 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4051 | return RValue<Int4>(V(jit->builder->CreateCall(cvtps2dq, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4052 | } |
| 4053 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4054 | RValue<Float> rcpss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4055 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4056 | 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] | 4057 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4058 | 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] | 4059 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4060 | 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] | 4061 | } |
| 4062 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4063 | RValue<Float> sqrtss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4064 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4065 | llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::sqrt, {V(val.value)->getType()}); |
| 4066 | return RValue<Float>(V(jit->builder->CreateCall(sqrt, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4067 | } |
| 4068 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4069 | RValue<Float> rsqrtss(RValue<Float> val) |
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 | 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] | 4072 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4073 | 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] | 4074 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4075 | 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] | 4076 | } |
| 4077 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4078 | RValue<Float4> rcpps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4079 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4080 | 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] | 4081 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4082 | return RValue<Float4>(V(jit->builder->CreateCall(rcpps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4083 | } |
| 4084 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4085 | RValue<Float4> sqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4086 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4087 | 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] | 4088 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4089 | return RValue<Float4>(V(jit->builder->CreateCall(sqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4090 | } |
| 4091 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4092 | RValue<Float4> rsqrtps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4093 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4094 | 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] | 4095 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4096 | return RValue<Float4>(V(jit->builder->CreateCall(rsqrtps, ARGS(V(val.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4097 | } |
| 4098 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4099 | RValue<Float4> maxps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4100 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4101 | 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] | 4102 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4103 | 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] | 4104 | } |
| 4105 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4106 | RValue<Float4> minps(RValue<Float4> x, RValue<Float4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4107 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4108 | 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] | 4109 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4110 | 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] | 4111 | } |
| 4112 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4113 | RValue<Float> roundss(RValue<Float> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4114 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4115 | 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] | 4116 | |
Nicolas Capens | fbf2bc5 | 2017-07-26 17:26:17 -0400 | [diff] [blame] | 4117 | Value *undef = V(llvm::UndefValue::get(T(Float4::getType()))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4118 | Value *vector = Nucleus::createInsertElement(undef, val.value, 0); |
| 4119 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4120 | 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] | 4121 | } |
| 4122 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4123 | RValue<Float> floorss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4124 | { |
| 4125 | return roundss(val, 1); |
| 4126 | } |
| 4127 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4128 | RValue<Float> ceilss(RValue<Float> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4129 | { |
| 4130 | return roundss(val, 2); |
| 4131 | } |
| 4132 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4133 | RValue<Float4> roundps(RValue<Float4> val, unsigned char imm) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4134 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4135 | 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] | 4136 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4137 | 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] | 4138 | } |
| 4139 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4140 | RValue<Float4> floorps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4141 | { |
| 4142 | return roundps(val, 1); |
| 4143 | } |
| 4144 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4145 | RValue<Float4> ceilps(RValue<Float4> val) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4146 | { |
| 4147 | return roundps(val, 2); |
| 4148 | } |
| 4149 | |
Alexis Hetu | 0f44807 | 2016-03-18 10:56:08 -0400 | [diff] [blame] | 4150 | RValue<Int4> pabsd(RValue<Int4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4151 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4152 | return RValue<Int4>(V(lowerPABS(V(x.value)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4153 | } |
| 4154 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4155 | RValue<Short4> paddsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4156 | { |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4157 | #if LLVM_VERSION_MAJOR >= 8 |
| 4158 | return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 4159 | #else |
| 4160 | 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] | 4161 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4162 | return As<Short4>(V(jit->builder->CreateCall2(paddsw, ARGS(V(x.value), V(y.value))))); |
| 4163 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4164 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4165 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4166 | RValue<Short4> psubsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4167 | { |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4168 | #if LLVM_VERSION_MAJOR >= 8 |
| 4169 | return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 4170 | #else |
| 4171 | 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] | 4172 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4173 | return As<Short4>(V(jit->builder->CreateCall2(psubsw, ARGS(V(x.value), V(y.value))))); |
| 4174 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4175 | } |
| 4176 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4177 | RValue<UShort4> paddusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4178 | { |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4179 | #if LLVM_VERSION_MAJOR >= 8 |
| 4180 | return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 4181 | #else |
| 4182 | 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] | 4183 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4184 | return As<UShort4>(V(jit->builder->CreateCall2(paddusw, ARGS(V(x.value), V(y.value))))); |
| 4185 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4186 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4187 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4188 | RValue<UShort4> psubusw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4189 | { |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4190 | #if LLVM_VERSION_MAJOR >= 8 |
| 4191 | return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 4192 | #else |
| 4193 | 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] | 4194 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4195 | return As<UShort4>(V(jit->builder->CreateCall2(psubusw, ARGS(V(x.value), V(y.value))))); |
| 4196 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4197 | } |
| 4198 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4199 | RValue<SByte8> paddsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4200 | { |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4201 | #if LLVM_VERSION_MAJOR >= 8 |
| 4202 | return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value)))); |
| 4203 | #else |
| 4204 | 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] | 4205 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4206 | return As<SByte8>(V(jit->builder->CreateCall2(paddsb, ARGS(V(x.value), V(y.value))))); |
| 4207 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4208 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4209 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4210 | RValue<SByte8> psubsb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4211 | { |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4212 | #if LLVM_VERSION_MAJOR >= 8 |
| 4213 | return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value)))); |
| 4214 | #else |
| 4215 | 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] | 4216 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4217 | return As<SByte8>(V(jit->builder->CreateCall2(psubsb, ARGS(V(x.value), V(y.value))))); |
| 4218 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4219 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4220 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4221 | RValue<Byte8> paddusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4222 | { |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4223 | #if LLVM_VERSION_MAJOR >= 8 |
| 4224 | return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value)))); |
| 4225 | #else |
| 4226 | 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] | 4227 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4228 | return As<Byte8>(V(jit->builder->CreateCall2(paddusb, ARGS(V(x.value), V(y.value))))); |
| 4229 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4230 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4231 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4232 | RValue<Byte8> psubusb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4233 | { |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4234 | #if LLVM_VERSION_MAJOR >= 8 |
| 4235 | return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value)))); |
| 4236 | #else |
| 4237 | 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] | 4238 | |
Preston Jackson | ee1af66 | 2019-09-27 15:26:26 -0600 | [diff] [blame] | 4239 | return As<Byte8>(V(jit->builder->CreateCall2(psubusb, ARGS(V(x.value), V(y.value))))); |
| 4240 | #endif |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | RValue<UShort4> pavgw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4244 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4245 | return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value)))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4246 | } |
| 4247 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4248 | RValue<Short4> pmaxsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4249 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4250 | 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] | 4251 | } |
| 4252 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4253 | RValue<Short4> pminsw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4254 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4255 | 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] | 4256 | } |
| 4257 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4258 | RValue<Short4> pcmpgtw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4259 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4260 | 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] | 4261 | } |
| 4262 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4263 | RValue<Short4> pcmpeqw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4264 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4265 | 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] | 4266 | } |
| 4267 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4268 | RValue<Byte8> pcmpgtb(RValue<SByte8> x, RValue<SByte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4269 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4270 | 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] | 4271 | } |
| 4272 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4273 | RValue<Byte8> pcmpeqb(RValue<Byte8> x, RValue<Byte8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4274 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4275 | 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] | 4276 | } |
| 4277 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4278 | RValue<Short4> packssdw(RValue<Int2> x, RValue<Int2> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4279 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4280 | 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] | 4281 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4282 | 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] | 4283 | } |
| 4284 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4285 | RValue<Short8> packssdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4286 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4287 | 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] | 4288 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4289 | 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] | 4290 | } |
| 4291 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4292 | RValue<SByte8> packsswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4293 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4294 | 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] | 4295 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4296 | 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] | 4297 | } |
| 4298 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 4299 | RValue<Byte8> packuswb(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4300 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4301 | 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] | 4302 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4303 | 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] | 4304 | } |
| 4305 | |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 4306 | RValue<UShort8> packusdw(RValue<Int4> x, RValue<Int4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4307 | { |
| 4308 | if(CPUID::supportsSSE4_1()) |
| 4309 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4310 | 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] | 4311 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4312 | 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] | 4313 | } |
| 4314 | else |
| 4315 | { |
Nicolas Capens | 3e7062b | 2017-01-17 14:01:33 -0500 | [diff] [blame] | 4316 | RValue<Int4> bx = (x & ~(x >> 31)) - Int4(0x8000); |
| 4317 | RValue<Int4> by = (y & ~(y >> 31)) - Int4(0x8000); |
| 4318 | |
| 4319 | return As<UShort8>(packssdw(bx, by) + Short8(0x8000u)); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4320 | } |
| 4321 | } |
| 4322 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4323 | RValue<UShort4> psrlw(RValue<UShort4> x, unsigned char y) |
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 | 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] | 4326 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4327 | 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] | 4328 | } |
| 4329 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4330 | RValue<UShort8> psrlw(RValue<UShort8> x, unsigned char y) |
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 | 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] | 4333 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4334 | 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] | 4335 | } |
| 4336 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4337 | RValue<Short4> psraw(RValue<Short4> x, unsigned char y) |
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 | 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] | 4340 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4341 | 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] | 4342 | } |
| 4343 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4344 | RValue<Short8> psraw(RValue<Short8> x, unsigned char y) |
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 | 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] | 4347 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4348 | 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] | 4349 | } |
| 4350 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4351 | RValue<Short4> psllw(RValue<Short4> x, unsigned char y) |
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 | 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] | 4354 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4355 | 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] | 4356 | } |
| 4357 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4358 | RValue<Short8> psllw(RValue<Short8> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4359 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4360 | 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] | 4361 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4362 | 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] | 4363 | } |
| 4364 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4365 | RValue<Int2> pslld(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4366 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4367 | 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] | 4368 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4369 | 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] | 4370 | } |
| 4371 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4372 | RValue<Int4> pslld(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4373 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4374 | 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] | 4375 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4376 | 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] | 4377 | } |
| 4378 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4379 | RValue<Int2> psrad(RValue<Int2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4380 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4381 | 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] | 4382 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4383 | 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] | 4384 | } |
| 4385 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4386 | RValue<Int4> psrad(RValue<Int4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4387 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4388 | 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] | 4389 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4390 | 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] | 4391 | } |
| 4392 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4393 | RValue<UInt2> psrld(RValue<UInt2> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4394 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4395 | 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] | 4396 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4397 | 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] | 4398 | } |
| 4399 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4400 | RValue<UInt4> psrld(RValue<UInt4> x, unsigned char y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4401 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4402 | 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] | 4403 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4404 | 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] | 4405 | } |
| 4406 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4407 | RValue<Int4> pmaxsd(RValue<Int4> x, RValue<Int4> y) |
| 4408 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4409 | 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] | 4410 | } |
| 4411 | |
| 4412 | RValue<Int4> pminsd(RValue<Int4> x, RValue<Int4> y) |
| 4413 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4414 | 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] | 4415 | } |
| 4416 | |
| 4417 | RValue<UInt4> pmaxud(RValue<UInt4> x, RValue<UInt4> y) |
| 4418 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4419 | 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] | 4420 | } |
| 4421 | |
| 4422 | RValue<UInt4> pminud(RValue<UInt4> x, RValue<UInt4> y) |
| 4423 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4424 | 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] | 4425 | } |
| 4426 | |
| 4427 | RValue<Short4> pmulhw(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4428 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4429 | 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] | 4430 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4431 | 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] | 4432 | } |
| 4433 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4434 | RValue<UShort4> pmulhuw(RValue<UShort4> x, RValue<UShort4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4435 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4436 | 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] | 4437 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4438 | 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] | 4439 | } |
| 4440 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4441 | RValue<Int2> pmaddwd(RValue<Short4> x, RValue<Short4> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4442 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4443 | 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] | 4444 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4445 | 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] | 4446 | } |
| 4447 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4448 | RValue<Short8> pmulhw(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4449 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4450 | 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] | 4451 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4452 | 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] | 4453 | } |
| 4454 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4455 | RValue<UShort8> pmulhuw(RValue<UShort8> x, RValue<UShort8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4456 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4457 | 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] | 4458 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4459 | 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] | 4460 | } |
| 4461 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4462 | RValue<Int4> pmaddwd(RValue<Short8> x, RValue<Short8> y) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4463 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4464 | 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] | 4465 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4466 | 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] | 4467 | } |
| 4468 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4469 | RValue<Int> movmskps(RValue<Float4> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4470 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4471 | 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] | 4472 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4473 | return RValue<Int>(V(jit->builder->CreateCall(movmskps, ARGS(V(x.value))))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4474 | } |
| 4475 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 4476 | RValue<Int> pmovmskb(RValue<Byte8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4477 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4478 | 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] | 4479 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4480 | 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] | 4481 | } |
| 4482 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4483 | RValue<Int4> pmovzxbd(RValue<Byte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4484 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4485 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4486 | } |
| 4487 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4488 | RValue<Int4> pmovsxbd(RValue<SByte16> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4489 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4490 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4491 | } |
| 4492 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4493 | RValue<Int4> pmovzxwd(RValue<UShort8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4494 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4495 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4496 | } |
| 4497 | |
Nicolas Capens | 01a9796 | 2017-07-28 17:30:51 -0400 | [diff] [blame] | 4498 | RValue<Int4> pmovsxwd(RValue<Short8> x) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4499 | { |
Logan Chien | 0eedc8c | 2018-08-21 09:34:28 +0800 | [diff] [blame] | 4500 | return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true))); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4501 | } |
| 4502 | } |
Logan Chien | e319101 | 2018-08-24 22:01:50 +0800 | [diff] [blame] | 4503 | #endif // defined(__i386__) || defined(__x86_64__) |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4504 | |
Ben Clayton | 60a3d6f | 2019-02-26 17:24:46 +0000 | [diff] [blame] | 4505 | #ifdef ENABLE_RR_PRINT |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4506 | // extractAll returns a vector containing the extracted n scalar value of |
| 4507 | // the vector vec. |
| 4508 | static std::vector<Value*> extractAll(Value* vec, int n) |
| 4509 | { |
| 4510 | std::vector<Value*> elements; |
| 4511 | elements.reserve(n); |
| 4512 | for (int i = 0; i < n; i++) |
| 4513 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4514 | auto el = V(jit->builder->CreateExtractElement(V(vec), i)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4515 | elements.push_back(el); |
| 4516 | } |
| 4517 | return elements; |
| 4518 | } |
| 4519 | |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4520 | // toInt returns all the integer values in vals extended to a native width |
| 4521 | // integer. |
| 4522 | static std::vector<Value*> toInt(const std::vector<Value*>& vals, bool isSigned) |
| 4523 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4524 | 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] | 4525 | std::vector<Value*> elements; |
| 4526 | elements.reserve(vals.size()); |
| 4527 | for (auto v : vals) |
| 4528 | { |
| 4529 | if (isSigned) |
| 4530 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4531 | elements.push_back(V(jit->builder->CreateSExt(V(v), intTy))); |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4532 | } |
| 4533 | else |
| 4534 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4535 | elements.push_back(V(jit->builder->CreateZExt(V(v), intTy))); |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | return elements; |
| 4539 | } |
| 4540 | |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4541 | // toDouble returns all the float values in vals extended to doubles. |
| 4542 | static std::vector<Value*> toDouble(const std::vector<Value*>& vals) |
| 4543 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4544 | auto doubleTy = ::llvm::Type::getDoubleTy(jit->context); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4545 | std::vector<Value*> elements; |
| 4546 | elements.reserve(vals.size()); |
| 4547 | for (auto v : vals) |
| 4548 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4549 | elements.push_back(V(jit->builder->CreateFPExt(V(v), doubleTy))); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4550 | } |
| 4551 | return elements; |
| 4552 | } |
| 4553 | |
Ben Clayton | a683328 | 2019-05-28 17:15:02 +0100 | [diff] [blame] | 4554 | std::vector<Value*> PrintValue::Ty<Byte>::val(const RValue<Byte>& v) { return toInt({v.value}, false); } |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4555 | std::vector<Value*> PrintValue::Ty<Byte4>::val(const RValue<Byte4>& v) { return toInt(extractAll(v.value, 4), false); } |
| 4556 | std::vector<Value*> PrintValue::Ty<Int>::val(const RValue<Int>& v) { return toInt({v.value}, true); } |
| 4557 | std::vector<Value*> PrintValue::Ty<Int2>::val(const RValue<Int2>& v) { return toInt(extractAll(v.value, 2), true); } |
| 4558 | std::vector<Value*> PrintValue::Ty<Int4>::val(const RValue<Int4>& v) { return toInt(extractAll(v.value, 4), true); } |
| 4559 | std::vector<Value*> PrintValue::Ty<UInt>::val(const RValue<UInt>& v) { return toInt({v.value}, false); } |
| 4560 | std::vector<Value*> PrintValue::Ty<UInt2>::val(const RValue<UInt2>& v) { return toInt(extractAll(v.value, 2), false); } |
| 4561 | std::vector<Value*> PrintValue::Ty<UInt4>::val(const RValue<UInt4>& v) { return toInt(extractAll(v.value, 4), false); } |
Ben Clayton | a683328 | 2019-05-28 17:15:02 +0100 | [diff] [blame] | 4562 | std::vector<Value*> PrintValue::Ty<Short>::val(const RValue<Short>& v) { return toInt({v.value}, true); } |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4563 | std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return toInt(extractAll(v.value, 4), true); } |
Ben Clayton | a683328 | 2019-05-28 17:15:02 +0100 | [diff] [blame] | 4564 | std::vector<Value*> PrintValue::Ty<UShort>::val(const RValue<UShort>& v) { return toInt({v.value}, false); } |
Ben Clayton | ca8e3d7 | 2019-05-14 16:51:05 +0100 | [diff] [blame] | 4565 | 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] | 4566 | std::vector<Value*> PrintValue::Ty<Float>::val(const RValue<Float>& v) { return toDouble({v.value}); } |
| 4567 | 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] | 4568 | 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] | 4569 | |
| 4570 | void Printv(const char* function, const char* file, int line, const char* fmt, std::initializer_list<PrintValue> args) |
| 4571 | { |
| 4572 | // LLVM types used below. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4573 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 4574 | auto intTy = ::llvm::Type::getIntNTy(jit->context, sizeof(int) * 8); // Natural integer width. |
| 4575 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(jit->context); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4576 | auto funcTy = ::llvm::FunctionType::get(i32Ty, {i8PtrTy}, true); |
| 4577 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4578 | auto func = jit->module->getOrInsertFunction("printf", funcTy); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4579 | |
| 4580 | // Build the printf format message string. |
| 4581 | std::string str; |
| 4582 | if (file != nullptr) { str += (line > 0) ? "%s:%d " : "%s "; } |
| 4583 | if (function != nullptr) { str += "%s "; } |
| 4584 | str += fmt; |
| 4585 | |
| 4586 | // Perform subsitution on all '{n}' bracketed indices in the format |
| 4587 | // message. |
| 4588 | int i = 0; |
| 4589 | for (const PrintValue& arg : args) |
| 4590 | { |
| 4591 | str = replace(str, "{" + std::to_string(i++) + "}", arg.format); |
| 4592 | } |
| 4593 | |
| 4594 | ::llvm::SmallVector<::llvm::Value*, 8> vals; |
| 4595 | |
| 4596 | // The format message is always the first argument. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4597 | vals.push_back(jit->builder->CreateGlobalStringPtr(str)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4598 | |
| 4599 | // Add optional file, line and function info if provided. |
| 4600 | if (file != nullptr) |
| 4601 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4602 | vals.push_back(jit->builder->CreateGlobalStringPtr(file)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4603 | if (line > 0) |
| 4604 | { |
| 4605 | vals.push_back(::llvm::ConstantInt::get(intTy, line)); |
| 4606 | } |
| 4607 | } |
| 4608 | if (function != nullptr) |
| 4609 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4610 | vals.push_back(jit->builder->CreateGlobalStringPtr(function)); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | // Add all format arguments. |
| 4614 | for (const PrintValue& arg : args) |
| 4615 | { |
| 4616 | for (auto val : arg.values) |
| 4617 | { |
| 4618 | vals.push_back(V(val)); |
| 4619 | } |
| 4620 | } |
| 4621 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4622 | jit->builder->CreateCall(func, vals); |
Ben Clayton | 1bc7ee9 | 2019-02-14 18:43:22 +0000 | [diff] [blame] | 4623 | } |
| 4624 | #endif // ENABLE_RR_PRINT |
| 4625 | |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4626 | void Nop() |
| 4627 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4628 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4629 | auto funcTy = ::llvm::FunctionType::get(voidTy, {}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4630 | auto func = jit->module->getOrInsertFunction("nop", funcTy); |
| 4631 | jit->builder->CreateCall(func); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | void EmitDebugLocation() |
| 4635 | { |
| 4636 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4637 | if (jit->debugInfo != nullptr) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4638 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4639 | jit->debugInfo->EmitLocation(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4640 | } |
| 4641 | #endif // ENABLE_RR_DEBUG_INFO |
| 4642 | } |
| 4643 | |
| 4644 | void EmitDebugVariable(Value* value) |
| 4645 | { |
| 4646 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4647 | if (jit->debugInfo != nullptr) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4648 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4649 | jit->debugInfo->EmitVariable(value); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4650 | } |
| 4651 | #endif // ENABLE_RR_DEBUG_INFO |
| 4652 | } |
| 4653 | |
| 4654 | void FlushDebug() |
| 4655 | { |
| 4656 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4657 | if (jit->debugInfo != nullptr) |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4658 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4659 | jit->debugInfo->Flush(); |
Ben Clayton | ac07ed8 | 2019-03-26 14:17:41 +0000 | [diff] [blame] | 4660 | } |
| 4661 | #endif // ENABLE_RR_DEBUG_INFO |
| 4662 | } |
| 4663 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4664 | } // namespace rr |
| 4665 | |
| 4666 | // ------------------------------ Coroutines ------------------------------ |
| 4667 | |
| 4668 | namespace { |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4669 | // Magic values retuned by llvm.coro.suspend. |
| 4670 | // See: https://llvm.org/docs/Coroutines.html#llvm-coro-suspend-intrinsic |
| 4671 | enum SuspendAction |
| 4672 | { |
| 4673 | SuspendActionSuspend = -1, |
| 4674 | SuspendActionResume = 0, |
| 4675 | SuspendActionDestroy = 1 |
| 4676 | }; |
| 4677 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4678 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4679 | void promoteFunctionToCoroutine() |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4680 | { |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4681 | ASSERT(jit->coroutine.id == nullptr); |
| 4682 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4683 | // Types |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4684 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
| 4685 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 4686 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
| 4687 | auto i32Ty = ::llvm::Type::getInt32Ty(jit->context); |
| 4688 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(jit->context); |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4689 | auto promiseTy = jit->coroutine.yieldType; |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4690 | auto promisePtrTy = promiseTy->getPointerTo(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4691 | |
| 4692 | // LLVM intrinsics |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4693 | auto coro_id = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_id); |
| 4694 | auto coro_size = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_size, {i32Ty}); |
| 4695 | auto coro_begin = ::llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::coro_begin); |
| 4696 | auto coro_resume = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_resume); |
| 4697 | auto coro_end = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_end); |
| 4698 | auto coro_free = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_free); |
| 4699 | auto coro_destroy = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_destroy); |
| 4700 | auto coro_promise = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_promise); |
| 4701 | auto coro_done = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_done); |
| 4702 | 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] | 4703 | |
| 4704 | auto allocFrameTy = ::llvm::FunctionType::get(i8PtrTy, {i32Ty}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4705 | auto allocFrame = jit->module->getOrInsertFunction("coroutine_alloc_frame", allocFrameTy); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4706 | auto freeFrameTy = ::llvm::FunctionType::get(voidTy, {i8PtrTy}, false); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4707 | auto freeFrame = jit->module->getOrInsertFunction("coroutine_free_frame", freeFrameTy); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4708 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4709 | auto oldInsertionPoint = jit->builder->saveIP(); |
| 4710 | |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4711 | // Build the coroutine_await() function: |
| 4712 | // |
| 4713 | // bool coroutine_await(CoroutineHandle* handle, YieldType* out) |
| 4714 | // { |
| 4715 | // if (llvm.coro.done(handle)) |
| 4716 | // { |
| 4717 | // return false; |
| 4718 | // } |
| 4719 | // else |
| 4720 | // { |
| 4721 | // *value = (T*)llvm.coro.promise(handle); |
| 4722 | // llvm.coro.resume(handle); |
| 4723 | // return true; |
| 4724 | // } |
| 4725 | // } |
| 4726 | // |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4727 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4728 | auto args = jit->coroutine.await->arg_begin(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4729 | auto handle = args++; |
| 4730 | auto outPtr = args++; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4731 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "co_await", jit->coroutine.await)); |
| 4732 | auto doneBlock = llvm::BasicBlock::Create(jit->context, "done", jit->coroutine.await); |
| 4733 | auto resumeBlock = llvm::BasicBlock::Create(jit->context, "resume", jit->coroutine.await); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4734 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4735 | auto done = jit->builder->CreateCall(coro_done, {handle}, "done"); |
| 4736 | jit->builder->CreateCondBr(done, doneBlock, resumeBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4737 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4738 | jit->builder->SetInsertPoint(doneBlock); |
| 4739 | jit->builder->CreateRet(::llvm::ConstantInt::getFalse(i1Ty)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4740 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4741 | jit->builder->SetInsertPoint(resumeBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4742 | auto promiseAlignment = ::llvm::ConstantInt::get(i32Ty, 4); // TODO: Get correct alignment. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4743 | auto promisePtr = jit->builder->CreateCall(coro_promise, {handle, promiseAlignment, ::llvm::ConstantInt::get(i1Ty, 0)}); |
| 4744 | auto promise = jit->builder->CreateLoad(jit->builder->CreatePointerCast(promisePtr, promisePtrTy)); |
| 4745 | jit->builder->CreateStore(promise, outPtr); |
| 4746 | jit->builder->CreateCall(coro_resume, {handle}); |
| 4747 | jit->builder->CreateRet(::llvm::ConstantInt::getTrue(i1Ty)); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | // Build the coroutine_destroy() function: |
| 4751 | // |
| 4752 | // void coroutine_destroy(CoroutineHandle* handle) |
| 4753 | // { |
| 4754 | // llvm.coro.destroy(handle); |
| 4755 | // } |
| 4756 | // |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4757 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4758 | auto handle = jit->coroutine.destroy->arg_begin(); |
| 4759 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->coroutine.destroy)); |
| 4760 | jit->builder->CreateCall(coro_destroy, {handle}); |
| 4761 | jit->builder->CreateRetVoid(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4762 | } |
| 4763 | |
| 4764 | // Begin building the main coroutine_begin() function. |
| 4765 | // |
| 4766 | // CoroutineHandle* coroutine_begin(<Arguments>) |
| 4767 | // { |
| 4768 | // YieldType promise; |
| 4769 | // auto id = llvm.coro.id(0, &promise, nullptr, nullptr); |
| 4770 | // void* frame = coroutine_alloc_frame(llvm.coro.size.i32()); |
| 4771 | // CoroutineHandle *handle = llvm.coro.begin(id, frame); |
| 4772 | // |
| 4773 | // ... <REACTOR CODE> ... |
| 4774 | // |
| 4775 | // end: |
| 4776 | // SuspendAction action = llvm.coro.suspend(none, true /* final */); // <-- RESUME POINT |
| 4777 | // switch (action) |
| 4778 | // { |
| 4779 | // case SuspendActionResume: |
| 4780 | // UNREACHABLE(); // Illegal to resume after final suspend. |
| 4781 | // case SuspendActionDestroy: |
| 4782 | // goto destroy; |
| 4783 | // default: // (SuspendActionSuspend) |
| 4784 | // goto suspend; |
| 4785 | // } |
| 4786 | // |
| 4787 | // destroy: |
| 4788 | // coroutine_free_frame(llvm.coro.free(id, handle)); |
| 4789 | // goto suspend; |
| 4790 | // |
| 4791 | // suspend: |
| 4792 | // llvm.coro.end(handle, false); |
| 4793 | // return handle; |
| 4794 | // } |
| 4795 | // |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4796 | |
| 4797 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4798 | 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] | 4799 | #endif // ENABLE_RR_DEBUG_INFO |
| 4800 | |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4801 | jit->coroutine.suspendBlock = llvm::BasicBlock::Create(jit->context, "suspend", jit->function); |
| 4802 | jit->coroutine.endBlock = llvm::BasicBlock::Create(jit->context, "end", jit->function); |
| 4803 | jit->coroutine.destroyBlock = llvm::BasicBlock::Create(jit->context, "destroy", jit->function); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4804 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4805 | jit->builder->SetInsertPoint(jit->coroutine.entryBlock, jit->coroutine.entryBlock->begin()); |
| 4806 | jit->coroutine.promise = jit->builder->CreateAlloca(promiseTy, nullptr, "promise"); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4807 | jit->coroutine.id = jit->builder->CreateCall(coro_id, { |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4808 | ::llvm::ConstantInt::get(i32Ty, 0), |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4809 | jit->builder->CreatePointerCast(jit->coroutine.promise, i8PtrTy), |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4810 | ::llvm::ConstantPointerNull::get(i8PtrTy), |
| 4811 | ::llvm::ConstantPointerNull::get(i8PtrTy), |
| 4812 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4813 | auto size = jit->builder->CreateCall(coro_size, {}); |
| 4814 | auto frame = jit->builder->CreateCall(allocFrame, {size}); |
| 4815 | jit->coroutine.handle = jit->builder->CreateCall(coro_begin, {jit->coroutine.id, frame}); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4816 | |
| 4817 | // Build the suspend block |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4818 | jit->builder->SetInsertPoint(jit->coroutine.suspendBlock); |
| 4819 | jit->builder->CreateCall(coro_end, {jit->coroutine.handle, ::llvm::ConstantInt::get(i1Ty, 0)}); |
| 4820 | jit->builder->CreateRet(jit->coroutine.handle); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4821 | |
| 4822 | // Build the end block |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4823 | jit->builder->SetInsertPoint(jit->coroutine.endBlock); |
| 4824 | auto action = jit->builder->CreateCall(coro_suspend, { |
| 4825 | ::llvm::ConstantTokenNone::get(jit->context), |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4826 | ::llvm::ConstantInt::get(i1Ty, 1), // final: true |
| 4827 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4828 | auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4829 | // 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] | 4830 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4831 | |
| 4832 | // Build the destroy block |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4833 | jit->builder->SetInsertPoint(jit->coroutine.destroyBlock); |
| 4834 | auto memory = jit->builder->CreateCall(coro_free, {jit->coroutine.id, jit->coroutine.handle}); |
| 4835 | jit->builder->CreateCall(freeFrame, {memory}); |
| 4836 | jit->builder->CreateBr(jit->coroutine.suspendBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4837 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4838 | // Switch back to original insert point to continue building the coroutine. |
| 4839 | jit->builder->restoreIP(oldInsertionPoint); |
| 4840 | } |
| 4841 | |
| 4842 | } // anonymous namespace |
| 4843 | |
| 4844 | namespace rr { |
| 4845 | |
| 4846 | void Nucleus::createCoroutine(Type *YieldType, std::vector<Type*> &Params) |
| 4847 | { |
| 4848 | // Coroutines are initially created as a regular function. |
| 4849 | // Upon the first call to Yield(), the function is promoted to a true |
| 4850 | // coroutine. |
| 4851 | auto voidTy = ::llvm::Type::getVoidTy(jit->context); |
| 4852 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 4853 | auto i8PtrTy = ::llvm::Type::getInt8PtrTy(jit->context); |
| 4854 | auto handleTy = i8PtrTy; |
| 4855 | auto boolTy = i1Ty; |
| 4856 | auto promiseTy = T(YieldType); |
| 4857 | auto promisePtrTy = promiseTy->getPointerTo(); |
| 4858 | |
| 4859 | jit->function = rr::createFunction("coroutine_begin", handleTy, T(Params)); |
| 4860 | jit->coroutine.await = rr::createFunction("coroutine_await", boolTy, {handleTy, promisePtrTy}); |
| 4861 | jit->coroutine.destroy = rr::createFunction("coroutine_destroy", voidTy, {handleTy}); |
| 4862 | jit->coroutine.yieldType = promiseTy; |
| 4863 | jit->coroutine.entryBlock = llvm::BasicBlock::Create(jit->context, "function", jit->function); |
| 4864 | |
| 4865 | jit->builder->SetInsertPoint(jit->coroutine.entryBlock); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 4866 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4867 | |
| 4868 | void Nucleus::yield(Value* val) |
| 4869 | { |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4870 | if (jit->coroutine.id == nullptr) |
| 4871 | { |
| 4872 | // First call to yield(). |
| 4873 | // Promote the function to a full coroutine. |
| 4874 | promoteFunctionToCoroutine(); |
| 4875 | ASSERT(jit->coroutine.id != nullptr); |
| 4876 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4877 | |
| 4878 | // promise = val; |
| 4879 | // |
| 4880 | // auto action = llvm.coro.suspend(none, false /* final */); // <-- RESUME POINT |
| 4881 | // switch (action) |
| 4882 | // { |
| 4883 | // case SuspendActionResume: |
| 4884 | // goto resume; |
| 4885 | // case SuspendActionDestroy: |
| 4886 | // goto destroy; |
| 4887 | // default: // (SuspendActionSuspend) |
| 4888 | // goto suspend; |
| 4889 | // } |
| 4890 | // resume: |
| 4891 | // |
| 4892 | |
| 4893 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 4894 | Variable::materializeAll(); |
| 4895 | |
| 4896 | // Types |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4897 | auto i1Ty = ::llvm::Type::getInt1Ty(jit->context); |
| 4898 | auto i8Ty = ::llvm::Type::getInt8Ty(jit->context); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4899 | |
| 4900 | // Intrinsics |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4901 | 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] | 4902 | |
| 4903 | // Create a block to resume execution. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4904 | auto resumeBlock = llvm::BasicBlock::Create(jit->context, "resume", jit->function); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4905 | |
| 4906 | // Store the promise (yield value) |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4907 | jit->builder->CreateStore(V(val), jit->coroutine.promise); |
| 4908 | auto action = jit->builder->CreateCall(coro_suspend, { |
| 4909 | ::llvm::ConstantTokenNone::get(jit->context), |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4910 | ::llvm::ConstantInt::get(i1Ty, 0), // final: true |
| 4911 | }); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4912 | auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4913 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), resumeBlock); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4914 | switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4915 | |
| 4916 | // Continue building in the resume block. |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4917 | jit->builder->SetInsertPoint(resumeBlock); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4918 | } |
| 4919 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 4920 | 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] | 4921 | { |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4922 | bool isCoroutine = jit->coroutine.id != nullptr; |
| 4923 | if (isCoroutine) |
| 4924 | { |
| 4925 | jit->builder->CreateBr(jit->coroutine.endBlock); |
| 4926 | } |
| 4927 | else |
| 4928 | { |
| 4929 | // Coroutine without a Yield acts as a regular function. |
| 4930 | // The 'coroutine_begin' function returns a nullptr for the coroutine |
| 4931 | // handle. |
| 4932 | jit->builder->CreateRet(llvm::Constant::getNullValue(jit->function->getReturnType())); |
| 4933 | // The 'coroutine_await' function always returns false (coroutine done). |
| 4934 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->coroutine.await)); |
| 4935 | jit->builder->CreateRet(llvm::Constant::getNullValue(jit->coroutine.await->getReturnType())); |
| 4936 | // The 'coroutine_destroy' does nothing, returns void. |
| 4937 | jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->coroutine.destroy)); |
| 4938 | jit->builder->CreateRetVoid(); |
| 4939 | } |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4940 | |
| 4941 | #ifdef ENABLE_RR_DEBUG_INFO |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4942 | if (jit->debugInfo != nullptr) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4943 | { |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4944 | jit->debugInfo->Finalize(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4945 | } |
| 4946 | #endif // ENABLE_RR_DEBUG_INFO |
| 4947 | |
| 4948 | if(false) |
| 4949 | { |
| 4950 | std::error_code error; |
| 4951 | 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] | 4952 | jit->module->print(file, 0); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4953 | } |
| 4954 | |
Ben Clayton | 16da281 | 2019-07-09 23:28:51 +0100 | [diff] [blame] | 4955 | if (isCoroutine) |
| 4956 | { |
| 4957 | // Run manadory coroutine transforms. |
| 4958 | llvm::legacy::PassManager pm; |
| 4959 | pm.add(llvm::createCoroEarlyPass()); |
| 4960 | pm.add(llvm::createCoroSplitPass()); |
| 4961 | pm.add(llvm::createCoroElidePass()); |
| 4962 | pm.add(llvm::createBarrierNoopPass()); |
| 4963 | pm.add(llvm::createCoroCleanupPass()); |
| 4964 | pm.run(*jit->module); |
| 4965 | } |
| 4966 | |
| 4967 | #if defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
| 4968 | { |
| 4969 | llvm::legacy::PassManager pm; |
| 4970 | pm.add(llvm::createVerifierPass()); |
| 4971 | pm.run(*jit->module); |
| 4972 | } |
| 4973 | #endif // defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG) |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4974 | |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 4975 | auto cfg = cfgEdit.apply(jit->config); |
| 4976 | jit->optimize(cfg); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4977 | |
| 4978 | if(false) |
| 4979 | { |
| 4980 | std::error_code error; |
| 4981 | 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] | 4982 | jit->module->print(file, 0); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4983 | } |
| 4984 | |
| 4985 | llvm::Function *funcs[Nucleus::CoroutineEntryCount]; |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4986 | funcs[Nucleus::CoroutineEntryBegin] = jit->function; |
| 4987 | funcs[Nucleus::CoroutineEntryAwait] = jit->coroutine.await; |
| 4988 | funcs[Nucleus::CoroutineEntryDestroy] = jit->coroutine.destroy; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 4989 | auto routine = jit->acquireRoutine(funcs, Nucleus::CoroutineEntryCount, cfg); |
Ben Clayton | 6f8e565 | 2019-06-29 01:58:02 +0100 | [diff] [blame] | 4990 | jit.reset(); |
Ben Clayton | 1c82c7b | 2019-04-30 12:49:27 +0100 | [diff] [blame] | 4991 | |
| 4992 | return routine; |
| 4993 | } |
| 4994 | |
| 4995 | } // namespace rr |