blob: 0360138c8dc2f4591d2fc6505534ebc29e281ad1 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman89401822014-05-06 15:04:28 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// 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 Bauman89401822014-05-06 15:04:28 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
John Bauman89401822014-05-06 15:04:28 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// 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 Bauman89401822014-05-06 15:04:28 -040014
Nicolas Capenscb986762017-01-20 11:34:37 -050015#include "Reactor.hpp"
Ben Claytoneb50d252019-04-15 13:50:01 -040016#include "Debug.hpp"
Ben Claytonac07ed82019-03-26 14:17:41 +000017#include "LLVMReactor.hpp"
18#include "LLVMReactorDebugInfo.hpp"
John Bauman89401822014-05-06 15:04:28 -040019
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040020#include "x86.hpp"
21#include "CPUID.hpp"
22#include "Thread.hpp"
Nicolas Capens1a3ce872018-10-10 10:42:36 -040023#include "ExecutableMemory.hpp"
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040024#include "MutexLock.hpp"
25
26#undef min
27#undef max
28
Ben Clayton09a7f452019-04-25 15:22:43 +010029#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 Maiorano992bf9d2019-10-22 09:41:17 -040036#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 Clayton5875be52019-04-11 14:57:40 -040041#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 Clayton5875be52019-04-11 14:57:40 -040054#include "llvm/IR/Intrinsics.h"
Ben Clayton1c82c7b2019-04-30 12:49:27 +010055#include "llvm/IR/IRBuilder.h"
Ben Clayton5875be52019-04-11 14:57:40 -040056#include "llvm/IR/LegacyPassManager.h"
Ben Clayton1c82c7b2019-04-30 12:49:27 +010057#include "llvm/IR/LLVMContext.h"
Ben Clayton5875be52019-04-11 14:57:40 -040058#include "llvm/IR/Mangler.h"
59#include "llvm/IR/Module.h"
Ben Clayton4b944652019-05-02 10:56:19 +010060#include "llvm/IR/Verifier.h"
Ben Clayton5875be52019-04-11 14:57:40 -040061#include "llvm/Support/Error.h"
62#include "llvm/Support/TargetSelect.h"
63#include "llvm/Target/TargetOptions.h"
Ben Clayton1c82c7b2019-04-30 12:49:27 +010064#include "llvm/Transforms/Coroutines.h"
Ben Clayton5875be52019-04-11 14:57:40 -040065#include "llvm/Transforms/InstCombine/InstCombine.h"
Ben Clayton1c82c7b2019-04-30 12:49:27 +010066#include "llvm/Transforms/IPO.h"
67#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Ben Clayton5875be52019-04-11 14:57:40 -040068#include "llvm/Transforms/Scalar.h"
69#include "llvm/Transforms/Scalar/GVN.h"
Ben Clayton20507fa2019-04-20 01:40:15 -040070
Ben Clayton09a7f452019-04-25 15:22:43 +010071#if defined(__clang__)
72#pragma clang diagnostic pop
73#endif // defined(__clang__)
74
Antonio Maiorano992bf9d2019-10-22 09:41:17 -040075#ifdef _MSC_VER
76__pragma(warning(pop))
77#endif
78
Ben Clayton5875be52019-04-11 14:57:40 -040079#define ARGS(...) {__VA_ARGS__}
80#define CreateCall2 CreateCall
81#define CreateCall3 CreateCall
Logan Chien0eedc8c2018-08-21 09:34:28 +080082
Ben Clayton5875be52019-04-11 14:57:40 -040083#include <unordered_map>
Logan Chien0eedc8c2018-08-21 09:34:28 +080084
John Bauman89401822014-05-06 15:04:28 -040085#include <fstream>
Ben Claytoncee3dff2019-05-22 12:01:22 +010086#include <iostream>
87#include <mutex>
Ben Clayton1bc7ee92019-02-14 18:43:22 +000088#include <numeric>
89#include <thread>
John Bauman89401822014-05-06 15:04:28 -040090
Nicolas Capens47dc8672017-04-25 12:54:39 -040091#if defined(__i386__) || defined(__x86_64__)
92#include <xmmintrin.h>
93#endif
94
Logan Chien40a60052018-09-26 19:03:53 +080095#include <math.h>
96
Nicolas Capenscb122582014-05-06 23:34:44 -040097#if defined(__x86_64__) && defined(_WIN32)
Ben Clayton2f58df32019-06-23 21:29:25 +010098 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 Bauman66b8ab22014-05-06 15:57:45 -0400108#endif
109
Nicolas Capens48461502018-08-06 14:20:45 -0400110namespace rr
Logan Chien52cde602018-09-03 19:37:57 +0800111{
Ben Clayton6f8e5652019-06-29 01:58:02 +0100112 void* resolveExternalSymbol(const char*);
Logan Chien52cde602018-09-03 19:37:57 +0800113}
114
Nicolas Capens5c1f5cc2016-09-23 16:45:13 -0400115namespace
116{
Ben Clayton55bc37a2019-07-04 12:17:12 +0100117 // 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 Clayton52ce1e92019-07-15 11:41:00 +0100131 // Cache provides a simple, thread-safe key-value store.
132 template <typename KEY, typename VALUE>
133 class Cache
Ben Clayton6f8e5652019-06-29 01:58:02 +0100134 {
Ben Clayton52ce1e92019-07-15 11:41:00 +0100135 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 Clayton6f8e5652019-06-29 01:58:02 +0100142 };
Nicolas Capens3bbc5e12016-09-27 10:49:52 -0400143
Ben Clayton52ce1e92019-07-15 11:41:00 +0100144 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 Clayton6f8e5652019-06-29 01:58:02 +0100165 // JITGlobals is a singleton that holds all the immutable machine specific
166 // information for the host device.
Ben Clayton52ce1e92019-07-15 11:41:00 +0100167 class JITGlobals
Ben Clayton6f8e5652019-06-29 01:58:02 +0100168 {
169 public:
Ben Clayton52ce1e92019-07-15 11:41:00 +0100170 using TargetMachineSPtr = std::shared_ptr<llvm::TargetMachine>;
Ben Clayton6f8e5652019-06-29 01:58:02 +0100171
Ben Clayton52ce1e92019-07-15 11:41:00 +0100172 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 Clayton6f8e5652019-06-29 01:58:02 +0100181
182 private:
Ben Clayton52ce1e92019-07-15 11:41:00 +0100183 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 Clayton6f8e5652019-06-29 01:58:02 +0100196 };
197
Ben Clayton52ce1e92019-07-15 11:41:00 +0100198 JITGlobals * JITGlobals::get()
Ben Clayton6f8e5652019-06-29 01:58:02 +0100199 {
Ben Clayton52ce1e92019-07-15 11:41:00 +0100200 static JITGlobals instance = create();
Ben Clayton6f8e5652019-06-29 01:58:02 +0100201 return &instance;
202 }
203
Ben Clayton52ce1e92019-07-15 11:41:00 +0100204 JITGlobals::TargetMachineSPtr JITGlobals::getTargetMachine(rr::Optimization::Level optlevel)
Ben Clayton6f8e5652019-06-29 01:58:02 +0100205 {
Ben Clayton52ce1e92019-07-15 11:41:00 +0100206 return targetMachines.getOrCreate(static_cast<int>(optlevel), [&]() {
207 return TargetMachineSPtr(llvm::EngineBuilder()
208#ifdef ENABLE_RR_DEBUG_INFO
Ben Claytone031f362019-07-20 12:35:40 +0100209 .setOptLevel(toLLVM(rr::Optimization::Level::None))
Ben Clayton52ce1e92019-07-15 11:41:00 +0100210#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 Clayton49f80512019-07-04 17:30:54 +0100220
Ben Clayton52ce1e92019-07-15 11:41:00 +0100221 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 Clayton6f8e5652019-06-29 01:58:02 +0100236 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 Claytonac07ed82019-03-26 14:17:41 +0000244#endif
245
Ben Clayton52ce1e92019-07-15 11:41:00 +0100246 std::vector<std::string> mattrs;
Ben Clayton6f8e5652019-06-29 01:58:02 +0100247 for (auto &feature : features)
248 {
249 if (feature.second) { mattrs.push_back(feature.first()); }
250 }
251
Ben Clayton52ce1e92019-07-15 11:41:00 +0100252 const char* march = nullptr;
Ben Clayton6f8e5652019-06-29 01:58:02 +0100253#if defined(__x86_64__)
Ben Clayton49f80512019-07-04 17:30:54 +0100254 march = "x86-64";
Ben Clayton6f8e5652019-06-29 01:58:02 +0100255#elif defined(__i386__)
Ben Clayton49f80512019-07-04 17:30:54 +0100256 march = "x86";
Ben Clayton6f8e5652019-06-29 01:58:02 +0100257#elif defined(__aarch64__)
Ben Clayton49f80512019-07-04 17:30:54 +0100258 march = "arm64";
Ben Clayton6f8e5652019-06-29 01:58:02 +0100259#elif defined(__arm__)
Ben Clayton49f80512019-07-04 17:30:54 +0100260 march = "arm";
Ben Clayton6f8e5652019-06-29 01:58:02 +0100261#elif defined(__mips__)
262#if defined(__mips64)
Ben Clayton49f80512019-07-04 17:30:54 +0100263 march = "mips64el";
Ben Clayton6f8e5652019-06-29 01:58:02 +0100264#else
Ben Clayton49f80512019-07-04 17:30:54 +0100265 march = "mipsel";
Ben Clayton6f8e5652019-06-29 01:58:02 +0100266#endif
267#elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
Ben Clayton49f80512019-07-04 17:30:54 +0100268 march = "ppc64le";
Ben Clayton6f8e5652019-06-29 01:58:02 +0100269#else
270 #error "unknown architecture"
271#endif
272
Ben Clayton52ce1e92019-07-15 11:41:00 +0100273 llvm::TargetOptions targetOptions;
Ben Clayton6f8e5652019-06-29 01:58:02 +0100274 targetOptions.UnsafeFPMath = false;
Ben Clayton6f8e5652019-06-29 01:58:02 +0100275
276 auto targetMachine = std::unique_ptr<llvm::TargetMachine>(
277 llvm::EngineBuilder()
278 .setOptLevel(llvm::CodeGenOpt::None)
Ben Clayton49f80512019-07-04 17:30:54 +0100279 .setMCPU(mcpu)
280 .setMArch(march)
Ben Clayton6f8e5652019-06-29 01:58:02 +0100281 .setMAttrs(mattrs)
282 .setTargetOptions(targetOptions)
283 .selectTarget());
284
Ben Clayton52ce1e92019-07-15 11:41:00 +0100285 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 Clayton6f8e5652019-06-29 01:58:02 +0100314 }
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 Jacksonee1af662019-09-27 15:26:26 -0600321#if LLVM_VERSION_MAJOR >= 8
322 using ObjLayer = llvm::orc::LegacyRTDyldObjectLinkingLayer;
323 using CompileLayer = llvm::orc::LegacyIRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>;
324#else
Ben Clayton6f8e5652019-06-29 01:58:02 +0100325 using ObjLayer = llvm::orc::RTDyldObjectLinkingLayer;
326 using CompileLayer = llvm::orc::IRCompileLayer<ObjLayer, llvm::orc::SimpleCompiler>;
Preston Jacksonee1af662019-09-27 15:26:26 -0600327#endif
328
Ben Clayton6f8e5652019-06-29 01:58:02 +0100329 public:
Ben Clayton68cfc782019-06-29 12:31:08 +0100330 JITRoutine(
331 std::unique_ptr<llvm::Module> module,
332 llvm::Function **funcs,
333 size_t count,
Ben Clayton55bc37a2019-07-04 12:17:12 +0100334 const rr::Config &config) :
Ben Clayton6f8e5652019-06-29 01:58:02 +0100335 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 Clayton52ce1e92019-07-15 11:41:00 +0100353 targetMachine(JITGlobals::get()->getTargetMachine(config.getOptimization().getLevel())),
Ben Clayton6f8e5652019-06-29 01:58:02 +0100354 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 Maiorano9c0617c2019-11-29 10:43:16 -0500408 const void *getEntry(int index) const override
Ben Clayton6f8e5652019-06-29 01:58:02 +0100409 {
410 return addresses[index];
411 }
412
413 private:
414 std::shared_ptr<llvm::orc::SymbolResolver> resolver;
Ben Clayton52ce1e92019-07-15 11:41:00 +0100415 std::shared_ptr<llvm::TargetMachine> targetMachine;
Ben Clayton6f8e5652019-06-29 01:58:02 +0100416 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 Clayton55bc37a2019-07-04 12:17:12 +0100426 JITBuilder(const rr::Config &config) :
427 config(config),
Ben Clayton6f8e5652019-06-29 01:58:02 +0100428 module(new llvm::Module("", context)),
429 builder(new llvm::IRBuilder<>(context))
430 {
431 module->setDataLayout(JITGlobals::get()->dataLayout);
432 }
433
Ben Clayton55bc37a2019-07-04 12:17:12 +0100434 void optimize(const rr::Config &cfg)
Ben Clayton6f8e5652019-06-29 01:58:02 +0100435 {
Ben Clayton55bc37a2019-07-04 12:17:12 +0100436
Ben Clayton6f8e5652019-06-29 01:58:02 +0100437#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 Clayton55bc37a2019-07-04 12:17:12 +0100447 for(auto pass : cfg.getOptimization().getPasses())
Ben Clayton6f8e5652019-06-29 01:58:02 +0100448 {
Ben Clayton55bc37a2019-07-04 12:17:12 +0100449 switch(pass)
Ben Clayton6f8e5652019-06-29 01:58:02 +0100450 {
Ben Clayton55bc37a2019-07-04 12:17:12 +0100451 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 Clayton28ae0a42019-07-10 00:50:23 +0100461 case rr::Optimization::Pass::EarlyCSEPass: passManager->add(llvm::createEarlyCSEPass()); break;
Ben Clayton6f8e5652019-06-29 01:58:02 +0100462 default:
Ben Clayton55bc37a2019-07-04 12:17:12 +0100463 UNREACHABLE("pass: %d", int(pass));
Ben Clayton6f8e5652019-06-29 01:58:02 +0100464 }
465 }
466
467 passManager->run(*module);
468 }
469
Ben Clayton6897e9b2019-07-16 17:27:27 +0100470 std::shared_ptr<rr::Routine> acquireRoutine(llvm::Function **funcs, size_t count, const rr::Config &cfg)
Ben Clayton6f8e5652019-06-29 01:58:02 +0100471 {
472 ASSERT(module);
Ben Clayton6897e9b2019-07-16 17:27:27 +0100473 return std::make_shared<JITRoutine>(std::move(module), funcs, count, cfg);
Ben Clayton6f8e5652019-06-29 01:58:02 +0100474 }
475
Ben Clayton55bc37a2019-07-04 12:17:12 +0100476 const rr::Config config;
Ben Clayton6f8e5652019-06-29 01:58:02 +0100477 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 Clayton16da2812019-07-09 23:28:51 +0100489 llvm::Type *yieldType = nullptr;
490 llvm::BasicBlock *entryBlock = nullptr;
Ben Clayton6f8e5652019-06-29 01:58:02 +0100491 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 Chien0eedc8c2018-08-21 09:34:28 +0800504
Ben Clayton60a3d6f2019-02-26 17:24:46 +0000505#ifdef ENABLE_RR_PRINT
Ben Clayton1bc7ee92019-02-14 18:43:22 +0000506 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 Clayton60a3d6f2019-02-26 17:24:46 +0000515#endif // ENABLE_RR_PRINT
Ben Clayton1bc7ee92019-02-14 18:43:22 +0000516
Ben Clayton40a885e2019-06-23 19:12:48 +0100517 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 Chien0eedc8c2018-08-21 09:34:28 +0800543 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 Clayton6f8e5652019-06-29 01:58:02 +0100549 x = jit->builder->CreateZExt(x, extTy);
550 y = jit->builder->CreateZExt(y, extTy);
Logan Chien0eedc8c2018-08-21 09:34:28 +0800551
552 // (x + y + 1) >> 1
553 llvm::Constant *one = llvm::ConstantInt::get(extTy, 1);
Ben Clayton6f8e5652019-06-29 01:58:02 +0100554 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 Chien0eedc8c2018-08-21 09:34:28 +0800558 }
559
560 llvm::Value *lowerPMINMAX(llvm::Value *x, llvm::Value *y,
Logan Chienb5ce5092018-09-27 18:45:58 +0800561 llvm::ICmpInst::Predicate pred)
Logan Chien0eedc8c2018-08-21 09:34:28 +0800562 {
Ben Clayton6f8e5652019-06-29 01:58:02 +0100563 return jit->builder->CreateSelect(jit->builder->CreateICmp(pred, x, y), x, y);
Logan Chien0eedc8c2018-08-21 09:34:28 +0800564 }
565
566 llvm::Value *lowerPCMP(llvm::ICmpInst::Predicate pred, llvm::Value *x,
Logan Chienb5ce5092018-09-27 18:45:58 +0800567 llvm::Value *y, llvm::Type *dstTy)
Logan Chien0eedc8c2018-08-21 09:34:28 +0800568 {
Ben Clayton6f8e5652019-06-29 01:58:02 +0100569 return jit->builder->CreateSExt(jit->builder->CreateICmp(pred, x, y), dstTy, "");
Logan Chien0eedc8c2018-08-21 09:34:28 +0800570 }
571
Logan Chiene3191012018-08-24 22:01:50 +0800572#if defined(__i386__) || defined(__x86_64__)
Logan Chien0eedc8c2018-08-21 09:34:28 +0800573 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 Clayton6f8e5652019-06-29 01:58:02 +0100581 llvm::Value *v = jit->builder->CreateShuffleVector(op, undef, mask);
Logan Chien0eedc8c2018-08-21 09:34:28 +0800582
Ben Clayton6f8e5652019-06-29 01:58:02 +0100583 return sext ? jit->builder->CreateSExt(v, dstTy)
584 : jit->builder->CreateZExt(v, dstTy);
Logan Chien0eedc8c2018-08-21 09:34:28 +0800585 }
586
587 llvm::Value *lowerPABS(llvm::Value *v)
588 {
589 llvm::Value *zero = llvm::Constant::getNullValue(v->getType());
Ben Clayton6f8e5652019-06-29 01:58:02 +0100590 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 Chien0eedc8c2018-08-21 09:34:28 +0800593 }
594#endif // defined(__i386__) || defined(__x86_64__)
Logan Chiene3191012018-08-24 22:01:50 +0800595
596#if !defined(__i386__) && !defined(__x86_64__)
597 llvm::Value *lowerPFMINMAX(llvm::Value *x, llvm::Value *y,
Logan Chienb5ce5092018-09-27 18:45:58 +0800598 llvm::FCmpInst::Predicate pred)
Logan Chiene3191012018-08-24 22:01:50 +0800599 {
Ben Clayton6f8e5652019-06-29 01:58:02 +0100600 return jit->builder->CreateSelect(jit->builder->CreateFCmp(pred, x, y), x, y);
Logan Chiene3191012018-08-24 22:01:50 +0800601 }
602
Logan Chien83fc07a2018-09-26 22:14:00 +0800603 llvm::Value *lowerRound(llvm::Value *x)
604 {
605 llvm::Function *nearbyint = llvm::Intrinsic::getDeclaration(
Ben Clayton6f8e5652019-06-29 01:58:02 +0100606 jit->module.get(), llvm::Intrinsic::nearbyint, {x->getType()});
607 return jit->builder->CreateCall(nearbyint, ARGS(x));
Logan Chien83fc07a2018-09-26 22:14:00 +0800608 }
609
Logan Chien2faa24a2018-09-26 19:59:32 +0800610 llvm::Value *lowerRoundInt(llvm::Value *x, llvm::Type *ty)
611 {
Ben Clayton6f8e5652019-06-29 01:58:02 +0100612 return jit->builder->CreateFPToSI(lowerRound(x), ty);
Logan Chien2faa24a2018-09-26 19:59:32 +0800613 }
614
Logan Chien40a60052018-09-26 19:03:53 +0800615 llvm::Value *lowerFloor(llvm::Value *x)
616 {
617 llvm::Function *floor = llvm::Intrinsic::getDeclaration(
Ben Clayton6f8e5652019-06-29 01:58:02 +0100618 jit->module.get(), llvm::Intrinsic::floor, {x->getType()});
619 return jit->builder->CreateCall(floor, ARGS(x));
Logan Chien40a60052018-09-26 19:03:53 +0800620 }
621
Logan Chien8c5ca8d2018-09-27 21:05:53 +0800622 llvm::Value *lowerTrunc(llvm::Value *x)
623 {
624 llvm::Function *trunc = llvm::Intrinsic::getDeclaration(
Ben Clayton6f8e5652019-06-29 01:58:02 +0100625 jit->module.get(), llvm::Intrinsic::trunc, {x->getType()});
626 return jit->builder->CreateCall(trunc, ARGS(x));
Logan Chien8c5ca8d2018-09-27 21:05:53 +0800627 }
628
Preston Jacksonee1af662019-09-27 15:26:26 -0600629 // Packed add/sub with saturation
Logan Chien28794cf2018-09-26 18:58:03 +0800630 llvm::Value *lowerPSAT(llvm::Value *x, llvm::Value *y, bool isAdd, bool isSigned)
Logan Chiene3191012018-08-24 22:01:50 +0800631 {
Logan Chien28794cf2018-09-26 18:58:03 +0800632 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 Clayton6f8e5652019-06-29 01:58:02 +0100642 extX = jit->builder->CreateSExt(x, extTy);
643 extY = jit->builder->CreateSExt(y, extTy);
Logan Chien28794cf2018-09-26 18:58:03 +0800644 }
645 else
646 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400647 ASSERT_MSG(numBits <= 64, "numBits: %d", int(numBits));
Logan Chien28794cf2018-09-26 18:58:03 +0800648 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 Clayton6f8e5652019-06-29 01:58:02 +0100651 extX = jit->builder->CreateZExt(x, extTy);
652 extY = jit->builder->CreateZExt(y, extTy);
Logan Chien28794cf2018-09-26 18:58:03 +0800653 }
654
Ben Clayton6f8e5652019-06-29 01:58:02 +0100655 llvm::Value *res = isAdd ? jit->builder->CreateAdd(extX, extY)
656 : jit->builder->CreateSub(extX, extY);
Logan Chien28794cf2018-09-26 18:58:03 +0800657
658 res = lowerPMINMAX(res, min, llvm::ICmpInst::ICMP_SGT);
659 res = lowerPMINMAX(res, max, llvm::ICmpInst::ICMP_SLT);
660
Ben Clayton6f8e5652019-06-29 01:58:02 +0100661 return jit->builder->CreateTrunc(res, ty);
Logan Chiene3191012018-08-24 22:01:50 +0800662 }
663
Logan Chiene3191012018-08-24 22:01:50 +0800664 llvm::Value *lowerSQRT(llvm::Value *x)
665 {
666 llvm::Function *sqrt = llvm::Intrinsic::getDeclaration(
Ben Clayton6f8e5652019-06-29 01:58:02 +0100667 jit->module.get(), llvm::Intrinsic::sqrt, {x->getType()});
668 return jit->builder->CreateCall(sqrt, ARGS(x));
Logan Chiene3191012018-08-24 22:01:50 +0800669 }
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 Clayton6f8e5652019-06-29 01:58:02 +0100685 return jit->builder->CreateFDiv(one, x);
Logan Chiene3191012018-08-24 22:01:50 +0800686 }
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 Clayton6f8e5652019-06-29 01:58:02 +0100699 return jit->builder->CreateShl(x, y);
Logan Chiene3191012018-08-24 22:01:50 +0800700 }
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 Clayton6f8e5652019-06-29 01:58:02 +0100708 return jit->builder->CreateAShr(x, y);
Logan Chiene3191012018-08-24 22:01:50 +0800709 }
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 Clayton6f8e5652019-06-29 01:58:02 +0100717 return jit->builder->CreateLShr(x, y);
Logan Chiene3191012018-08-24 22:01:50 +0800718 }
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 Clayton6f8e5652019-06-29 01:58:02 +0100725 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 Chiene3191012018-08-24 22:01:50 +0800728
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 Clayton6f8e5652019-06-29 01:58:02 +0100739 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 Chiene3191012018-08-24 22:01:50 +0800742 }
743
Logan Chiene3191012018-08-24 22:01:50 +0800744 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 Claytoneb50d252019-04-15 13:50:01 -0400753 ASSERT_MSG(truncNumBits < 64, "shift 64 must be handled separately. truncNumBits: %d", int(truncNumBits));
Logan Chiene3191012018-08-24 22:01:50 +0800754 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 Clayton6f8e5652019-06-29 01:58:02 +0100771 x = jit->builder->CreateTrunc(x, dstTy);
772 y = jit->builder->CreateTrunc(y, dstTy);
Logan Chiene3191012018-08-24 22:01:50 +0800773
774 llvm::SmallVector<uint32_t, 16> index(srcTy->getNumElements() * 2);
775 std::iota(index.begin(), index.end(), 0);
776
Ben Clayton6f8e5652019-06-29 01:58:02 +0100777 return jit->builder->CreateShuffleVector(x, y, index);
Logan Chiene3191012018-08-24 22:01:50 +0800778 }
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 Clayton6f8e5652019-06-29 01:58:02 +0100784 llvm::Value *cmp = jit->builder->CreateICmpSLT(x, zero);
Logan Chiene3191012018-08-24 22:01:50 +0800785
Ben Clayton6f8e5652019-06-29 01:58:02 +0100786 llvm::Value *ret = jit->builder->CreateZExt(
787 jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy);
Logan Chiene3191012018-08-24 22:01:50 +0800788 for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i)
789 {
Ben Clayton6f8e5652019-06-29 01:58:02 +0100790 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 Chiene3191012018-08-24 22:01:50 +0800793 }
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 Clayton6f8e5652019-06-29 01:58:02 +0100801 llvm::Value *cmp = jit->builder->CreateFCmpULT(x, zero);
Logan Chiene3191012018-08-24 22:01:50 +0800802
Ben Clayton6f8e5652019-06-29 01:58:02 +0100803 llvm::Value *ret = jit->builder->CreateZExt(
804 jit->builder->CreateExtractElement(cmp, static_cast<uint64_t>(0)), retTy);
Logan Chiene3191012018-08-24 22:01:50 +0800805 for (uint64_t i = 1, n = ty->getNumElements(); i < n; ++i)
806 {
Ben Clayton6f8e5652019-06-29 01:58:02 +0100807 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 Chiene3191012018-08-24 22:01:50 +0800810 }
811 return ret;
812 }
813#endif // !defined(__i386__) && !defined(__x86_64__)
Chris Forbese86b6dc2019-03-01 09:08:47 -0800814
Preston Jacksonee1af662019-09-27 15:26:26 -0600815#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 Forbese86b6dc2019-03-01 09:08:47 -0800853 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 Clayton6f8e5652019-06-29 01:58:02 +0100861 extX = jit->builder->CreateSExt(x, extTy);
862 extY = jit->builder->CreateSExt(y, extTy);
Chris Forbese86b6dc2019-03-01 09:08:47 -0800863 }
864 else
865 {
Ben Clayton6f8e5652019-06-29 01:58:02 +0100866 extX = jit->builder->CreateZExt(x, extTy);
867 extY = jit->builder->CreateZExt(y, extTy);
Chris Forbese86b6dc2019-03-01 09:08:47 -0800868 }
869
Ben Clayton6f8e5652019-06-29 01:58:02 +0100870 llvm::Value *mult = jit->builder->CreateMul(extX, extY);
Chris Forbese86b6dc2019-03-01 09:08:47 -0800871
872 llvm::IntegerType *intTy = llvm::cast<llvm::IntegerType>(ty->getElementType());
Ben Clayton6f8e5652019-06-29 01:58:02 +0100873 llvm::Value *mulh = jit->builder->CreateAShr(mult, intTy->getBitWidth());
874 return jit->builder->CreateTrunc(mulh, ty);
Chris Forbese86b6dc2019-03-01 09:08:47 -0800875 }
Antonio Maioranoe6ab4702019-11-29 11:26:30 -0500876
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 Capens5c1f5cc2016-09-23 16:45:13 -0400925}
926
Nicolas Capens48461502018-08-06 14:20:45 -0400927namespace rr
John Bauman89401822014-05-06 15:04:28 -0400928{
Ben Claytonc7904162019-04-17 17:35:48 -0400929 const Capabilities Caps =
930 {
Ben Clayton1c82c7b2019-04-30 12:49:27 +0100931 true, // CoroutinesSupported
Ben Claytonc7904162019-04-17 17:35:48 -0400932 };
933
Ben Clayton4d1f8d02019-04-17 23:47:35 -0400934 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 Claytonfb280672019-04-25 11:16:15 +0100944 UNREACHABLE("memoryOrder: %d", int(memoryOrder));
Ben Clayton4d1f8d02019-04-17 23:47:35 -0400945 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 Forbesfd4c96d2019-06-20 11:20:42 -0700982#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 Clayton6f8e5652019-06-29 01:58:02 +0100998 void* resolveExternalSymbol(const char* name)
Logan Chien40a60052018-09-26 19:03:53 +0800999 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001000 struct Atomic
Logan Chien40a60052018-09-26 19:03:53 +08001001 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001002 static void load(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering)
Ben Clayton4d1f8d02019-04-17 23:47:35 -04001003 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001004 switch (size)
Ben Clayton4d1f8d02019-04-17 23:47:35 -04001005 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001006 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 Clayton4d1f8d02019-04-17 23:47:35 -04001012 }
Ben Clayton6f8e5652019-06-29 01:58:02 +01001013 }
1014 static void store(size_t size, void *ptr, void *ret, llvm::AtomicOrdering ordering)
Chris Forbes9283b252019-06-17 09:44:28 -07001015 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001016 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 Forbes9283b252019-06-17 09:44:28 -07001027
Ben Clayton6f8e5652019-06-29 01:58:02 +01001028 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 Clayton40a885e2019-06-23 19:12:48 +01001035
Chris Forbes9283b252019-06-17 09:44:28 -07001036#ifdef __ANDROID__
Ben Clayton6f8e5652019-06-29 01:58:02 +01001037 // 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 Forbesfd4c96d2019-06-20 11:20:42 -07001046
Ben Clayton6f8e5652019-06-29 01:58:02 +01001047 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 Forbes9283b252019-06-17 09:44:28 -07001051#endif
Ben Clayton6f8e5652019-06-29 01:58:02 +01001052 };
Ben Claytonac07ed82019-03-26 14:17:41 +00001053
Ben Clayton6f8e5652019-06-29 01:58:02 +01001054 class Resolver
1055 {
1056 public:
1057 using FunctionMap = std::unordered_map<std::string, void *>;
Ben Claytonb5f1a132019-06-24 11:00:53 +01001058
Ben Clayton6f8e5652019-06-29 01:58:02 +01001059 FunctionMap functions;
Ben Claytonb5f1a132019-06-24 11:00:53 +01001060
Ben Clayton6f8e5652019-06-29 01:58:02 +01001061 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 Claytonb5f1a132019-06-24 11:00:53 +01001070
Ben Clayton6f8e5652019-06-29 01:58:02 +01001071 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 Clayton14740062019-04-09 13:48:41 -04001088
Ben Clayton6f8e5652019-06-29 01:58:02 +01001089 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 Clayton1c82c7b2019-04-30 12:49:27 +01001113
Ben Clayton14740062019-04-09 13:48:41 -04001114#ifdef __APPLE__
Ben Clayton6f8e5652019-06-29 01:58:02 +01001115 functions.emplace("sincosf_stret", reinterpret_cast<void*>(__sincosf_stret));
Ben Clayton14740062019-04-09 13:48:41 -04001116#elif defined(__linux__)
Ben Clayton6f8e5652019-06-29 01:58:02 +01001117 functions.emplace("sincosf", reinterpret_cast<void*>(sincosf));
Ben Clayton2f58df32019-06-23 21:29:25 +01001118#elif defined(_WIN64)
Ben Clayton6f8e5652019-06-29 01:58:02 +01001119 functions.emplace("chkstk", reinterpret_cast<void*>(__chkstk));
Ben Clayton2f58df32019-06-23 21:29:25 +01001120#elif defined(_WIN32)
Ben Clayton6f8e5652019-06-29 01:58:02 +01001121 functions.emplace("chkstk", reinterpret_cast<void*>(_chkstk));
Ben Clayton2f58df32019-06-23 21:29:25 +01001122#endif
Chris Forbes9283b252019-06-17 09:44:28 -07001123
1124#ifdef __ANDROID__
Ben Clayton6f8e5652019-06-29 01:58:02 +01001125 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 Clayton1c82c7b2019-04-30 12:49:27 +01001139 }
Ben Clayton6f8e5652019-06-29 01:58:02 +01001140 };
Ben Clayton1c82c7b2019-04-30 12:49:27 +01001141
Ben Clayton6f8e5652019-06-29 01:58:02 +01001142 static Resolver resolver;
Logan Chien0eedc8c2018-08-21 09:34:28 +08001143
Ben Clayton6f8e5652019-06-29 01:58:02 +01001144 // 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 Claytoncee3dff2019-05-22 12:01:22 +01001148
Ben Clayton6f8e5652019-06-29 01:58:02 +01001149 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 Chien52cde602018-09-03 19:37:57 +08001154
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001155 // 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 Capensfbf2bc52017-07-26 17:26:17 -04001161 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001162 // Emulated types:
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001163 Type_v2i32,
1164 Type_v4i16,
1165 Type_v2i16,
1166 Type_v8i8,
1167 Type_v4i8,
1168 Type_v2f32,
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001169 EmulatedTypeCount,
1170 // Returned by asInternalType() to indicate that the abstract Type*
1171 // should be interpreted as LLVM type pointer:
1172 Type_LLVM
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001173 };
1174
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001175 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 Capensfbf2bc52017-07-26 17:26:17 -04001181 llvm::Type *T(Type *t)
1182 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001183 // Use 128-bit vectors to implement logically shorter ones.
1184 switch(asInternalType(t))
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001185 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001186 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 Claytoneb50d252019-04-15 13:50:01 -04001193 default:
1194 UNREACHABLE("asInternalType(t): %d", int(asInternalType(t)));
1195 return nullptr;
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001196 }
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001197 }
1198
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001199 Type *T(InternalType t)
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001200 {
1201 return reinterpret_cast<Type*>(t);
1202 }
1203
Nicolas Capensac230122016-09-20 14:30:06 -04001204 inline std::vector<llvm::Type*> &T(std::vector<Type*> &t)
1205 {
1206 return reinterpret_cast<std::vector<llvm::Type*>&>(t);
1207 }
1208
Logan Chien191b3052018-08-31 16:57:15 +08001209 inline llvm::BasicBlock *B(BasicBlock *t)
1210 {
1211 return reinterpret_cast<llvm::BasicBlock*>(t);
1212 }
1213
Nicolas Capensc8b67a42016-09-25 15:02:52 -04001214 inline BasicBlock *B(llvm::BasicBlock *t)
1215 {
1216 return reinterpret_cast<BasicBlock*>(t);
1217 }
1218
Nicolas Capens01a97962017-07-28 17:30:51 -04001219 static size_t typeSize(Type *type)
1220 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001221 switch(asInternalType(type))
Nicolas Capens01a97962017-07-28 17:30:51 -04001222 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001223 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 Capens01a97962017-07-28 17:30:51 -04001230 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001231 llvm::Type *t = T(type);
Nicolas Capens01a97962017-07-28 17:30:51 -04001232
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001233 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 Claytoneb50d252019-04-15 13:50:01 -04001240 ASSERT_MSG(bits != 0, "bits: %d", int(bits));
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001241
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 Claytoneb50d252019-04-15 13:50:01 -04001249 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001250 return 0;
1251 }
Nicolas Capens01a97962017-07-28 17:30:51 -04001252 }
1253
Nicolas Capens69674fb2017-09-01 11:08:44 -04001254 static unsigned int elementCount(Type *type)
1255 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001256 switch(asInternalType(type))
Nicolas Capens69674fb2017-09-01 11:08:44 -04001257 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001258 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 Claytoneb50d252019-04-15 13:50:01 -04001265 default:
1266 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
1267 return 0;
Nicolas Capens69674fb2017-09-01 11:08:44 -04001268 }
Nicolas Capens69674fb2017-09-01 11:08:44 -04001269 }
1270
Ben Claytonea38f952019-06-17 13:56:56 +01001271 static ::llvm::Function* createFunction(const char *name, ::llvm::Type *retTy, const std::vector<::llvm::Type*> &params)
1272 {
1273 llvm::FunctionType *functionType = llvm::FunctionType::get(retTy, params, false);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001274 auto func = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, name, jit->module.get());
Ben Claytonea38f952019-06-17 13:56:56 +01001275 func->setDoesNotThrow();
1276 func->setCallingConv(llvm::CallingConv::C);
Ben Claytonea38f952019-06-17 13:56:56 +01001277 return func;
1278 }
1279
John Bauman89401822014-05-06 15:04:28 -04001280 Nucleus::Nucleus()
1281 {
Nicolas Capens3bbc5e12016-09-27 10:49:52 -04001282 ::codegenMutex.lock(); // Reactor and LLVM are currently not thread safe
Nicolas Capensb7ea9842015-04-01 10:54:59 -04001283
Ben Clayton6f8e5652019-06-29 01:58:02 +01001284 ASSERT(jit == nullptr);
Ben Clayton55bc37a2019-07-04 12:17:12 +01001285 jit.reset(new JITBuilder(Nucleus::getDefaultConfig()));
John Bauman89401822014-05-06 15:04:28 -04001286 }
1287
1288 Nucleus::~Nucleus()
1289 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001290 jit.reset();
Nicolas Capens3bbc5e12016-09-27 10:49:52 -04001291 ::codegenMutex.unlock();
John Bauman89401822014-05-06 15:04:28 -04001292 }
1293
Ben Clayton55bc37a2019-07-04 12:17:12 +01001294 void Nucleus::setDefaultConfig(const Config &cfg)
John Bauman89401822014-05-06 15:04:28 -04001295 {
Ben Clayton55bc37a2019-07-04 12:17:12 +01001296 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 Clayton6897e9b2019-07-16 17:27:27 +01001313 std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */)
Ben Clayton55bc37a2019-07-04 12:17:12 +01001314 {
1315 auto cfg = cfgEdit.apply(jit->config);
1316
Ben Clayton6f8e5652019-06-29 01:58:02 +01001317 if(jit->builder->GetInsertBlock()->empty() || !jit->builder->GetInsertBlock()->back().isTerminator())
John Bauman19bac1e2014-05-06 15:23:49 -04001318 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001319 llvm::Type *type = jit->function->getReturnType();
John Bauman19bac1e2014-05-06 15:23:49 -04001320
1321 if(type->isVoidTy())
1322 {
1323 createRetVoid();
1324 }
1325 else
1326 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001327 createRet(V(llvm::UndefValue::get(type)));
John Bauman19bac1e2014-05-06 15:23:49 -04001328 }
1329 }
John Bauman89401822014-05-06 15:04:28 -04001330
Ben Clayton97c13ad2019-05-02 11:59:30 +01001331#ifdef ENABLE_RR_DEBUG_INFO
Ben Clayton6f8e5652019-06-29 01:58:02 +01001332 if (jit->debugInfo != nullptr)
Ben Clayton97c13ad2019-05-02 11:59:30 +01001333 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001334 jit->debugInfo->Finalize();
Ben Clayton97c13ad2019-05-02 11:59:30 +01001335 }
1336#endif // ENABLE_RR_DEBUG_INFO
1337
John Bauman89401822014-05-06 15:04:28 -04001338 if(false)
1339 {
Ben Clayton5875be52019-04-11 14:57:40 -04001340 std::error_code error;
1341 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-unopt.txt", error);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001342 jit->module->print(file, 0);
John Bauman89401822014-05-06 15:04:28 -04001343 }
1344
Ben Clayton5375f472019-06-24 13:33:11 +01001345#if defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG)
Ben Clayton4b944652019-05-02 10:56:19 +01001346 {
1347 llvm::legacy::PassManager pm;
1348 pm.add(llvm::createVerifierPass());
Ben Clayton6f8e5652019-06-29 01:58:02 +01001349 pm.run(*jit->module);
Ben Clayton4b944652019-05-02 10:56:19 +01001350 }
Ben Clayton5375f472019-06-24 13:33:11 +01001351#endif // defined(ENABLE_RR_LLVM_IR_VERIFICATION) || !defined(NDEBUG)
Ben Clayton4b944652019-05-02 10:56:19 +01001352
Ben Clayton55bc37a2019-07-04 12:17:12 +01001353 jit->optimize(cfg);
John Bauman89401822014-05-06 15:04:28 -04001354
1355 if(false)
1356 {
Ben Clayton5875be52019-04-11 14:57:40 -04001357 std::error_code error;
1358 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001359 jit->module->print(file, 0);
John Bauman89401822014-05-06 15:04:28 -04001360 }
1361
Ben Clayton55bc37a2019-07-04 12:17:12 +01001362 auto routine = jit->acquireRoutine(&jit->function, 1, cfg);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001363 jit.reset();
John Bauman89401822014-05-06 15:04:28 -04001364
John Bauman89401822014-05-06 15:04:28 -04001365 return routine;
1366 }
1367
John Bauman19bac1e2014-05-06 15:23:49 -04001368 Value *Nucleus::allocateStackVariable(Type *type, int arraySize)
John Bauman89401822014-05-06 15:04:28 -04001369 {
1370 // Need to allocate it in the entry block for mem2reg to work
Ben Clayton6f8e5652019-06-29 01:58:02 +01001371 llvm::BasicBlock &entryBlock = jit->function->getEntryBlock();
John Bauman89401822014-05-06 15:04:28 -04001372
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04001373 llvm::Instruction *declaration;
John Bauman89401822014-05-06 15:04:28 -04001374
1375 if(arraySize)
1376 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08001377 declaration = new llvm::AllocaInst(T(type), 0, V(Nucleus::createConstantInt(arraySize)));
John Bauman89401822014-05-06 15:04:28 -04001378 }
1379 else
1380 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08001381 declaration = new llvm::AllocaInst(T(type), 0, (llvm::Value*)nullptr);
John Bauman89401822014-05-06 15:04:28 -04001382 }
1383
1384 entryBlock.getInstList().push_front(declaration);
1385
Nicolas Capens19336542016-09-26 10:32:29 -04001386 return V(declaration);
John Bauman89401822014-05-06 15:04:28 -04001387 }
1388
1389 BasicBlock *Nucleus::createBasicBlock()
1390 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001391 return B(llvm::BasicBlock::Create(jit->context, "", jit->function));
John Bauman89401822014-05-06 15:04:28 -04001392 }
1393
1394 BasicBlock *Nucleus::getInsertBlock()
1395 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001396 return B(jit->builder->GetInsertBlock());
John Bauman89401822014-05-06 15:04:28 -04001397 }
1398
1399 void Nucleus::setInsertBlock(BasicBlock *basicBlock)
1400 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001401 // assert(jit->builder->GetInsertBlock()->back().isTerminator());
Nicolas Capens0192d152019-03-27 14:46:07 -04001402
1403 Variable::materializeAll();
1404
Ben Clayton6f8e5652019-06-29 01:58:02 +01001405 jit->builder->SetInsertPoint(B(basicBlock));
John Bauman89401822014-05-06 15:04:28 -04001406 }
1407
Nicolas Capensac230122016-09-20 14:30:06 -04001408 void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params)
John Bauman89401822014-05-06 15:04:28 -04001409 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001410 jit->function = rr::createFunction("", T(ReturnType), T(Params));
Nicolas Capens52551d12018-09-13 14:30:56 -04001411
Ben Claytonac07ed82019-03-26 14:17:41 +00001412#ifdef ENABLE_RR_DEBUG_INFO
Ben Claytone031f362019-07-20 12:35:40 +01001413 jit->debugInfo = std::unique_ptr<DebugInfo>(new DebugInfo(jit->builder.get(), &jit->context, jit->module.get(), jit->function));
Ben Claytonac07ed82019-03-26 14:17:41 +00001414#endif // ENABLE_RR_DEBUG_INFO
1415
Ben Clayton6f8e5652019-06-29 01:58:02 +01001416 jit->builder->SetInsertPoint(llvm::BasicBlock::Create(jit->context, "", jit->function));
John Bauman89401822014-05-06 15:04:28 -04001417 }
1418
Nicolas Capens19336542016-09-26 10:32:29 -04001419 Value *Nucleus::getArgument(unsigned int index)
John Bauman89401822014-05-06 15:04:28 -04001420 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001421 llvm::Function::arg_iterator args = jit->function->arg_begin();
John Bauman89401822014-05-06 15:04:28 -04001422
1423 while(index)
1424 {
1425 args++;
1426 index--;
1427 }
1428
Nicolas Capens19336542016-09-26 10:32:29 -04001429 return V(&*args);
John Bauman89401822014-05-06 15:04:28 -04001430 }
1431
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04001432 void Nucleus::createRetVoid()
John Bauman89401822014-05-06 15:04:28 -04001433 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001434 RR_DEBUG_INFO_UPDATE_LOC();
1435
Ben Clayton6f8e5652019-06-29 01:58:02 +01001436 ASSERT_MSG(jit->function->getReturnType() == T(Void::getType()), "Return type mismatch");
Ben Claytonc958b172019-05-02 12:20:59 +01001437
Nicolas Capens0192d152019-03-27 14:46:07 -04001438 // 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 Clayton6f8e5652019-06-29 01:58:02 +01001443 jit->builder->CreateRetVoid();
John Bauman89401822014-05-06 15:04:28 -04001444 }
1445
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04001446 void Nucleus::createRet(Value *v)
John Bauman89401822014-05-06 15:04:28 -04001447 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001448 RR_DEBUG_INFO_UPDATE_LOC();
1449
Ben Clayton6f8e5652019-06-29 01:58:02 +01001450 ASSERT_MSG(jit->function->getReturnType() == V(v)->getType(), "Return type mismatch");
Ben Claytonc958b172019-05-02 12:20:59 +01001451
Nicolas Capens0192d152019-03-27 14:46:07 -04001452 // 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 Clayton6f8e5652019-06-29 01:58:02 +01001457 jit->builder->CreateRet(V(v));
John Bauman89401822014-05-06 15:04:28 -04001458 }
1459
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04001460 void Nucleus::createBr(BasicBlock *dest)
John Bauman89401822014-05-06 15:04:28 -04001461 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001462 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens0192d152019-03-27 14:46:07 -04001463 Variable::materializeAll();
1464
Ben Clayton6f8e5652019-06-29 01:58:02 +01001465 jit->builder->CreateBr(B(dest));
John Bauman89401822014-05-06 15:04:28 -04001466 }
1467
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04001468 void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse)
John Bauman89401822014-05-06 15:04:28 -04001469 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001470 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens0192d152019-03-27 14:46:07 -04001471 Variable::materializeAll();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001472 jit->builder->CreateCondBr(V(cond), B(ifTrue), B(ifFalse));
John Bauman89401822014-05-06 15:04:28 -04001473 }
1474
1475 Value *Nucleus::createAdd(Value *lhs, Value *rhs)
1476 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001477 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001478 return V(jit->builder->CreateAdd(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001479 }
1480
1481 Value *Nucleus::createSub(Value *lhs, Value *rhs)
1482 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001483 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001484 return V(jit->builder->CreateSub(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001485 }
1486
1487 Value *Nucleus::createMul(Value *lhs, Value *rhs)
1488 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001489 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001490 return V(jit->builder->CreateMul(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001491 }
1492
1493 Value *Nucleus::createUDiv(Value *lhs, Value *rhs)
1494 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001495 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001496 return V(jit->builder->CreateUDiv(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001497 }
1498
1499 Value *Nucleus::createSDiv(Value *lhs, Value *rhs)
1500 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001501 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001502 return V(jit->builder->CreateSDiv(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001503 }
1504
1505 Value *Nucleus::createFAdd(Value *lhs, Value *rhs)
1506 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001507 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001508 return V(jit->builder->CreateFAdd(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001509 }
1510
1511 Value *Nucleus::createFSub(Value *lhs, Value *rhs)
1512 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001513 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001514 return V(jit->builder->CreateFSub(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001515 }
1516
1517 Value *Nucleus::createFMul(Value *lhs, Value *rhs)
1518 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001519 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001520 return V(jit->builder->CreateFMul(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001521 }
1522
1523 Value *Nucleus::createFDiv(Value *lhs, Value *rhs)
1524 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001525 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001526 return V(jit->builder->CreateFDiv(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001527 }
1528
1529 Value *Nucleus::createURem(Value *lhs, Value *rhs)
1530 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001531 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001532 return V(jit->builder->CreateURem(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001533 }
1534
1535 Value *Nucleus::createSRem(Value *lhs, Value *rhs)
1536 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001537 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001538 return V(jit->builder->CreateSRem(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001539 }
1540
1541 Value *Nucleus::createFRem(Value *lhs, Value *rhs)
1542 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001543 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001544 return V(jit->builder->CreateFRem(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001545 }
1546
1547 Value *Nucleus::createShl(Value *lhs, Value *rhs)
1548 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001549 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001550 return V(jit->builder->CreateShl(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001551 }
1552
1553 Value *Nucleus::createLShr(Value *lhs, Value *rhs)
1554 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001555 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001556 return V(jit->builder->CreateLShr(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001557 }
1558
1559 Value *Nucleus::createAShr(Value *lhs, Value *rhs)
1560 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001561 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001562 return V(jit->builder->CreateAShr(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001563 }
1564
1565 Value *Nucleus::createAnd(Value *lhs, Value *rhs)
1566 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001567 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001568 return V(jit->builder->CreateAnd(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001569 }
1570
1571 Value *Nucleus::createOr(Value *lhs, Value *rhs)
1572 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001573 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001574 return V(jit->builder->CreateOr(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001575 }
1576
1577 Value *Nucleus::createXor(Value *lhs, Value *rhs)
1578 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001579 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001580 return V(jit->builder->CreateXor(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04001581 }
1582
Nicolas Capens19336542016-09-26 10:32:29 -04001583 Value *Nucleus::createNeg(Value *v)
John Bauman89401822014-05-06 15:04:28 -04001584 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001585 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001586 return V(jit->builder->CreateNeg(V(v)));
John Bauman89401822014-05-06 15:04:28 -04001587 }
1588
Nicolas Capens19336542016-09-26 10:32:29 -04001589 Value *Nucleus::createFNeg(Value *v)
John Bauman89401822014-05-06 15:04:28 -04001590 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001591 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001592 return V(jit->builder->CreateFNeg(V(v)));
John Bauman89401822014-05-06 15:04:28 -04001593 }
1594
Nicolas Capens19336542016-09-26 10:32:29 -04001595 Value *Nucleus::createNot(Value *v)
John Bauman89401822014-05-06 15:04:28 -04001596 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001597 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001598 return V(jit->builder->CreateNot(V(v)));
John Bauman89401822014-05-06 15:04:28 -04001599 }
1600
Nicolas Capens86509d92019-03-21 13:23:50 -04001601 Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder)
John Bauman89401822014-05-06 15:04:28 -04001602 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001603 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001604 switch(asInternalType(type))
Nicolas Capens01a97962017-07-28 17:30:51 -04001605 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001606 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 Capens86509d92019-03-21 13:23:50 -04001613 createLoad(createBitCast(ptr, Pointer<Long>::getType()), Long::getType(), isVolatile, alignment, atomic, memoryOrder),
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001614 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 Capens01a97962017-07-28 17:30:51 -04001619 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001620 Value *u = V(llvm::UndefValue::get(llvm::VectorType::get(T(Long::getType()), 2)));
Nicolas Capens86509d92019-03-21 13:23:50 -04001621 Value *i = createLoad(createBitCast(ptr, Pointer<Int>::getType()), Int::getType(), isVolatile, alignment, atomic, memoryOrder);
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001622 i = createZExt(i, Long::getType());
1623 Value *v = createInsertElement(u, i, 0);
1624 return createBitCast(v, type);
Nicolas Capens01a97962017-07-28 17:30:51 -04001625 }
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001626 // Fallthrough to non-emulated case.
1627 case Type_LLVM:
Nicolas Capens86509d92019-03-21 13:23:50 -04001628 {
Ben Clayton99e57192019-05-03 13:25:08 +01001629 auto elTy = T(type);
1630 ASSERT(V(ptr)->getType()->getContainedType(0) == elTy);
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001631
1632 if (!atomic)
Ben Clayton99e57192019-05-03 13:25:08 +01001633 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001634 return V(jit->builder->CreateAlignedLoad(V(ptr), alignment, isVolatile));
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001635 }
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 Clayton6f8e5652019-06-29 01:58:02 +01001640 auto load = jit->builder->CreateAlignedLoad(V(ptr), alignment, isVolatile);
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001641 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 Clayton6f8e5652019-06-29 01:58:02 +01001649 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 Clayton2fa1dad2019-06-26 14:07:05 +01001653 load->setAtomic(atomicOrdering(atomic, memoryOrder));
Ben Clayton6f8e5652019-06-29 01:58:02 +01001654 auto loadCast = jit->builder->CreateBitCast(load, elTy);
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001655 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 Clayton6f8e5652019-06-29 01:58:02 +01001661 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 Clayton99e57192019-05-03 13:25:08 +01001664 auto i8PtrTy = i8Ty->getPointerTo();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001665 auto voidTy = ::llvm::Type::getVoidTy(jit->context);
Ben Clayton99e57192019-05-03 13:25:08 +01001666 auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001667 auto func = jit->module->getOrInsertFunction("__atomic_load", funcTy);
1668 auto size = jit->module->getDataLayout().getTypeStoreSize(elTy);
Ben Clayton99e57192019-05-03 13:25:08 +01001669 auto out = allocateStackVariable(type);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001670 jit->builder->CreateCall(func, {
Ben Clayton99e57192019-05-03 13:25:08 +01001671 ::llvm::ConstantInt::get(sizetTy, size),
Ben Clayton6f8e5652019-06-29 01:58:02 +01001672 jit->builder->CreatePointerCast(V(ptr), i8PtrTy),
1673 jit->builder->CreatePointerCast(V(out), i8PtrTy),
Ben Clayton99e57192019-05-03 13:25:08 +01001674 ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))),
1675 });
Ben Clayton6f8e5652019-06-29 01:58:02 +01001676 return V(jit->builder->CreateLoad(V(out)));
Ben Clayton99e57192019-05-03 13:25:08 +01001677 }
Nicolas Capens86509d92019-03-21 13:23:50 -04001678 }
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001679 default:
Ben Claytoneb50d252019-04-15 13:50:01 -04001680 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
1681 return nullptr;
Nicolas Capens01a97962017-07-28 17:30:51 -04001682 }
John Bauman89401822014-05-06 15:04:28 -04001683 }
1684
Nicolas Capens86509d92019-03-21 13:23:50 -04001685 Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int alignment, bool atomic, std::memory_order memoryOrder)
John Bauman89401822014-05-06 15:04:28 -04001686 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001687 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001688 switch(asInternalType(type))
Nicolas Capens01a97962017-07-28 17:30:51 -04001689 {
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001690 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 Capens86509d92019-03-21 13:23:50 -04001698 Long::getType(), isVolatile, alignment, atomic, memoryOrder);
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001699 return value;
1700 case Type_v2i16:
1701 case Type_v4i8:
1702 if(alignment != 0) // Not a local variable (all vectors are 128-bit).
Nicolas Capens01a97962017-07-28 17:30:51 -04001703 {
Logan Chien191b3052018-08-31 16:57:15 +08001704 createStore(
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001705 createExtractElement(createBitCast(value, Int4::getType()), Int::getType(), 0),
1706 createBitCast(ptr, Pointer<Int>::getType()),
Nicolas Capens86509d92019-03-21 13:23:50 -04001707 Int::getType(), isVolatile, alignment, atomic, memoryOrder);
Nicolas Capens01a97962017-07-28 17:30:51 -04001708 return value;
Nicolas Capens01a97962017-07-28 17:30:51 -04001709 }
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001710 // Fallthrough to non-emulated case.
1711 case Type_LLVM:
Nicolas Capens86509d92019-03-21 13:23:50 -04001712 {
Ben Clayton99e57192019-05-03 13:25:08 +01001713 auto elTy = T(type);
1714 ASSERT(V(ptr)->getType()->getContainedType(0) == elTy);
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001715
1716 if (!atomic)
Ben Clayton99e57192019-05-03 13:25:08 +01001717 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001718 jit->builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile);
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001719 }
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 Clayton6f8e5652019-06-29 01:58:02 +01001724 auto store = jit->builder->CreateAlignedStore(V(value), V(ptr), alignment, isVolatile);
Ben Clayton2fa1dad2019-06-26 14:07:05 +01001725 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 Clayton6f8e5652019-06-29 01:58:02 +01001732 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 Clayton2fa1dad2019-06-26 14:07:05 +01001737 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 Clayton6f8e5652019-06-29 01:58:02 +01001743 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 Clayton99e57192019-05-03 13:25:08 +01001746 auto i8PtrTy = i8Ty->getPointerTo();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001747 auto voidTy = ::llvm::Type::getVoidTy(jit->context);
Ben Clayton99e57192019-05-03 13:25:08 +01001748 auto funcTy = ::llvm::FunctionType::get(voidTy, {sizetTy, i8PtrTy, i8PtrTy, intTy}, false);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001749 auto func = jit->module->getOrInsertFunction("__atomic_store", funcTy);
1750 auto size = jit->module->getDataLayout().getTypeStoreSize(elTy);
Ben Clayton99e57192019-05-03 13:25:08 +01001751 auto copy = allocateStackVariable(type);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001752 jit->builder->CreateStore(V(value), V(copy));
1753 jit->builder->CreateCall(func, {
Ben Clayton99e57192019-05-03 13:25:08 +01001754 ::llvm::ConstantInt::get(sizetTy, size),
Ben Clayton6f8e5652019-06-29 01:58:02 +01001755 jit->builder->CreatePointerCast(V(ptr), i8PtrTy),
1756 jit->builder->CreatePointerCast(V(copy), i8PtrTy),
Ben Clayton99e57192019-05-03 13:25:08 +01001757 ::llvm::ConstantInt::get(intTy, uint64_t(atomicOrdering(true, memoryOrder))),
1758 });
1759 }
Nicolas Capens86509d92019-03-21 13:23:50 -04001760
1761 return value;
1762 }
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001763 default:
Ben Claytoneb50d252019-04-15 13:50:01 -04001764 UNREACHABLE("asInternalType(type): %d", int(asInternalType(type)));
1765 return nullptr;
Nicolas Capens01a97962017-07-28 17:30:51 -04001766 }
John Bauman89401822014-05-06 15:04:28 -04001767 }
1768
Ben Clayton0b00b952019-07-03 15:51:19 +01001769 Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes)
Ben Claytoncb2ebc92019-06-20 00:18:03 +01001770 {
1771 ASSERT(V(ptr)->getType()->isPointerTy());
1772 ASSERT(V(mask)->getType()->isVectorTy());
1773
1774 auto numEls = V(mask)->getType()->getVectorNumElements();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001775 auto i1Ty = ::llvm::Type::getInt1Ty(jit->context);
1776 auto i32Ty = ::llvm::Type::getInt32Ty(jit->context);
Ben Claytoncb2ebc92019-06-20 00:18:03 +01001777 auto elVecTy = ::llvm::VectorType::get(T(elTy), numEls);
1778 auto elVecPtrTy = elVecTy->getPointerTo();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001779 auto i8Mask = jit->builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...>
Ben Clayton0b00b952019-07-03 15:51:19 +01001780 auto passthrough = zeroMaskedLanes ? ::llvm::Constant::getNullValue(elVecTy) : llvm::UndefValue::get(elVecTy);
Ben Claytoncb2ebc92019-06-20 00:18:03 +01001781 auto align = ::llvm::ConstantInt::get(i32Ty, alignment);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001782 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 Claytoncb2ebc92019-06-20 00:18:03 +01001784 }
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 Clayton6f8e5652019-06-29 01:58:02 +01001793 auto i1Ty = ::llvm::Type::getInt1Ty(jit->context);
1794 auto i32Ty = ::llvm::Type::getInt32Ty(jit->context);
Ben Claytoncb2ebc92019-06-20 00:18:03 +01001795 auto elVecTy = V(val)->getType();
1796 auto elVecPtrTy = elVecTy->getPointerTo();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001797 auto i8Mask = jit->builder->CreateIntCast(V(mask), ::llvm::VectorType::get(i1Ty, numEls), false); // vec<int, int, ...> -> vec<bool, bool, ...>
Ben Claytoncb2ebc92019-06-20 00:18:03 +01001798 auto align = ::llvm::ConstantInt::get(i32Ty, alignment);
Ben Clayton6f8e5652019-06-29 01:58:02 +01001799 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 Claytoncb2ebc92019-06-20 00:18:03 +01001801 }
1802
Antonio Maioranoe6ab4702019-11-29 11:26:30 -05001803 RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */)
Ben Clayton0fc611f2019-04-18 11:23:27 -04001804 {
Antonio Maioranoe6ab4702019-11-29 11:26:30 -05001805 return As<Float4>(V(createGather(V(base.value), T(Float::getType()), V(offsets.value), V(mask.value), alignment, zeroMaskedLanes)));
Ben Clayton0fc611f2019-04-18 11:23:27 -04001806 }
1807
Antonio Maioranoe6ab4702019-11-29 11:26:30 -05001808 RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */)
Ben Clayton0fc611f2019-04-18 11:23:27 -04001809 {
Antonio Maioranoe6ab4702019-11-29 11:26:30 -05001810 return As<Int4>(V(createGather(V(base.value), T(Float::getType()), V(offsets.value), V(mask.value), alignment, zeroMaskedLanes)));
1811 }
Ben Clayton0fc611f2019-04-18 11:23:27 -04001812
Antonio Maioranoe6ab4702019-11-29 11:26:30 -05001813 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 Clayton0fc611f2019-04-18 11:23:27 -04001821 }
1822
Ben Claytonb16c5862019-05-08 14:01:38 +01001823 void Nucleus::createFence(std::memory_order memoryOrder)
1824 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001825 jit->builder->CreateFence(atomicOrdering(true, memoryOrder));
Ben Claytonb16c5862019-05-08 14:01:38 +01001826 }
1827
Nicolas Capensd294def2017-01-26 17:44:37 -08001828 Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
John Bauman89401822014-05-06 15:04:28 -04001829 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001830 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytoneb50d252019-04-15 13:50:01 -04001831 ASSERT(V(ptr)->getType()->getContainedType(0) == T(type));
Nicolas Capens01a97962017-07-28 17:30:51 -04001832 if(sizeof(void*) == 8)
Nicolas Capensd294def2017-01-26 17:44:37 -08001833 {
Ben Claytonb1243732019-02-27 23:56:18 +00001834 // 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 Capens01a97962017-07-28 17:30:51 -04001850 }
Ben Claytonb1243732019-02-27 23:56:18 +00001851
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001852 // 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 Capens01a97962017-07-28 17:30:51 -04001855 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01001856 return V(jit->builder->CreateGEP(V(ptr), V(index)));
Nicolas Capensd294def2017-01-26 17:44:37 -08001857 }
1858
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001859 // For emulated types we have to multiply the index by the intended
1860 // type size ourselves to obain the byte offset.
Ben Claytonb1243732019-02-27 23:56:18 +00001861 index = (sizeof(void*) == 8) ?
1862 createMul(index, createConstantLong((int64_t)typeSize(type))) :
1863 createMul(index, createConstantInt((int)typeSize(type)));
1864
Nicolas Capens1a5c3b92019-03-08 17:26:43 -05001865 // Cast to a byte pointer, apply the byte offset, and cast back to the
1866 // original pointer type.
Logan Chien191b3052018-08-31 16:57:15 +08001867 return createBitCast(
Ben Clayton6f8e5652019-06-29 01:58:02 +01001868 V(jit->builder->CreateGEP(V(createBitCast(ptr, T(llvm::PointerType::get(T(Byte::getType()), 0)))), V(index))),
Logan Chien191b3052018-08-31 16:57:15 +08001869 T(llvm::PointerType::get(T(type), 0)));
John Bauman89401822014-05-06 15:04:28 -04001870 }
1871
Chris Forbes17813932019-04-18 11:45:54 -07001872 Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder)
John Bauman19bac1e2014-05-06 15:23:49 -04001873 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001874 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001875 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Add, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
Chris Forbes17813932019-04-18 11:45:54 -07001876 }
1877
Chris Forbes707ed992019-04-18 18:17:35 -07001878 Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder)
1879 {
1880 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001881 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Sub, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
Chris Forbes707ed992019-04-18 18:17:35 -07001882 }
1883
Chris Forbes17813932019-04-18 11:45:54 -07001884 Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder)
1885 {
1886 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001887 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::And, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
Chris Forbes17813932019-04-18 11:45:54 -07001888 }
1889
1890 Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder)
1891 {
1892 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001893 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Or, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
Chris Forbes17813932019-04-18 11:45:54 -07001894 }
1895
1896 Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder)
1897 {
1898 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001899 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xor, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
Chris Forbes17813932019-04-18 11:45:54 -07001900 }
1901
1902 Value *Nucleus::createAtomicMin(Value *ptr, Value *value, std::memory_order memoryOrder)
1903 {
1904 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001905 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Min, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
Chris Forbes17813932019-04-18 11:45:54 -07001906 }
1907
1908 Value *Nucleus::createAtomicMax(Value *ptr, Value *value, std::memory_order memoryOrder)
1909 {
1910 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001911 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Max, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
Chris Forbes17813932019-04-18 11:45:54 -07001912 }
1913
Chris Forbesf31bdad2019-05-23 14:58:08 -07001914 Value *Nucleus::createAtomicUMin(Value *ptr, Value *value, std::memory_order memoryOrder)
1915 {
1916 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001917 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMin, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
Chris Forbesf31bdad2019-05-23 14:58:08 -07001918 }
1919
1920 Value *Nucleus::createAtomicUMax(Value *ptr, Value *value, std::memory_order memoryOrder)
1921 {
1922 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001923 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::UMax, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
Chris Forbesf31bdad2019-05-23 14:58:08 -07001924 }
1925
1926
Chris Forbes17813932019-04-18 11:45:54 -07001927 Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder)
1928 {
1929 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001930 return V(jit->builder->CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, V(ptr), V(value), atomicOrdering(true, memoryOrder)));
John Bauman19bac1e2014-05-06 15:23:49 -04001931 }
1932
Chris Forbesa16238d2019-04-18 16:31:54 -07001933 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 Forbesc9ca99e2019-04-19 07:53:34 -07001936 // Note: AtomicCmpXchgInstruction returns a 2-member struct containing {result, success-flag}, not the result directly.
Ben Clayton6f8e5652019-06-29 01:58:02 +01001937 return V(jit->builder->CreateExtractValue(
1938 jit->builder->CreateAtomicCmpXchg(V(ptr), V(compare), V(value), atomicOrdering(true, memoryOrderEqual), atomicOrdering(true, memoryOrderUnequal)),
Chris Forbesa16238d2019-04-18 16:31:54 -07001939 llvm::ArrayRef<unsigned>(0u)));
1940 }
1941
Nicolas Capens19336542016-09-26 10:32:29 -04001942 Value *Nucleus::createTrunc(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001943 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001944 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001945 return V(jit->builder->CreateTrunc(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001946 }
1947
Nicolas Capens19336542016-09-26 10:32:29 -04001948 Value *Nucleus::createZExt(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001949 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001950 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001951 return V(jit->builder->CreateZExt(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001952 }
1953
Nicolas Capens19336542016-09-26 10:32:29 -04001954 Value *Nucleus::createSExt(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001955 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001956 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001957 return V(jit->builder->CreateSExt(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001958 }
1959
Nicolas Capens19336542016-09-26 10:32:29 -04001960 Value *Nucleus::createFPToSI(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001961 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001962 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001963 return V(jit->builder->CreateFPToSI(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001964 }
1965
Nicolas Capens19336542016-09-26 10:32:29 -04001966 Value *Nucleus::createSIToFP(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001967 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001968 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001969 return V(jit->builder->CreateSIToFP(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001970 }
1971
Nicolas Capens19336542016-09-26 10:32:29 -04001972 Value *Nucleus::createFPTrunc(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001973 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001974 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001975 return V(jit->builder->CreateFPTrunc(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001976 }
1977
Nicolas Capens19336542016-09-26 10:32:29 -04001978 Value *Nucleus::createFPExt(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001979 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001980 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01001981 return V(jit->builder->CreateFPExt(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04001982 }
1983
Nicolas Capens19336542016-09-26 10:32:29 -04001984 Value *Nucleus::createBitCast(Value *v, Type *destType)
John Bauman89401822014-05-06 15:04:28 -04001985 {
Ben Claytonac07ed82019-03-26 14:17:41 +00001986 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens01a97962017-07-28 17:30:51 -04001987 // 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 Chien191b3052018-08-31 16:57:15 +08001990 if(!V(v)->getType()->isVectorTy() && T(destType)->isVectorTy())
Nicolas Capens01a97962017-07-28 17:30:51 -04001991 {
1992 Value *readAddress = allocateStackVariable(destType);
Logan Chien191b3052018-08-31 16:57:15 +08001993 Value *writeAddress = createBitCast(readAddress, T(llvm::PointerType::get(V(v)->getType(), 0)));
1994 createStore(v, writeAddress, T(V(v)->getType()));
Nicolas Capens01a97962017-07-28 17:30:51 -04001995 return createLoad(readAddress, destType);
1996 }
Logan Chien191b3052018-08-31 16:57:15 +08001997 else if(V(v)->getType()->isVectorTy() && !T(destType)->isVectorTy())
Nicolas Capens01a97962017-07-28 17:30:51 -04001998 {
Logan Chien191b3052018-08-31 16:57:15 +08001999 Value *writeAddress = allocateStackVariable(T(V(v)->getType()));
2000 createStore(v, writeAddress, T(V(v)->getType()));
Nicolas Capens01a97962017-07-28 17:30:51 -04002001 Value *readAddress = createBitCast(writeAddress, T(llvm::PointerType::get(T(destType), 0)));
2002 return createLoad(readAddress, destType);
2003 }
2004
Ben Clayton6f8e5652019-06-29 01:58:02 +01002005 return V(jit->builder->CreateBitCast(V(v), T(destType)));
John Bauman89401822014-05-06 15:04:28 -04002006 }
2007
Ben Clayton204a4102019-07-31 13:17:47 +01002008 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 Bauman89401822014-05-06 15:04:28 -04002014 Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs)
2015 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002016 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002017 return V(jit->builder->CreateICmpEQ(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002018 }
2019
2020 Value *Nucleus::createICmpNE(Value *lhs, Value *rhs)
2021 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002022 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002023 return V(jit->builder->CreateICmpNE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002024 }
2025
2026 Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs)
2027 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002028 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002029 return V(jit->builder->CreateICmpUGT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002030 }
2031
2032 Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs)
2033 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002034 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002035 return V(jit->builder->CreateICmpUGE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002036 }
2037
2038 Value *Nucleus::createICmpULT(Value *lhs, Value *rhs)
2039 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002040 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002041 return V(jit->builder->CreateICmpULT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002042 }
2043
2044 Value *Nucleus::createICmpULE(Value *lhs, Value *rhs)
2045 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002046 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002047 return V(jit->builder->CreateICmpULE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002048 }
2049
2050 Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs)
2051 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002052 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002053 return V(jit->builder->CreateICmpSGT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002054 }
2055
2056 Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs)
2057 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002058 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002059 return V(jit->builder->CreateICmpSGE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002060 }
2061
2062 Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs)
2063 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002064 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002065 return V(jit->builder->CreateICmpSLT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002066 }
2067
2068 Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs)
2069 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002070 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002071 return V(jit->builder->CreateICmpSLE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002072 }
2073
2074 Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs)
2075 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002076 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002077 return V(jit->builder->CreateFCmpOEQ(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002078 }
2079
2080 Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs)
2081 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002082 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002083 return V(jit->builder->CreateFCmpOGT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002084 }
2085
2086 Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs)
2087 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002088 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002089 return V(jit->builder->CreateFCmpOGE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002090 }
2091
2092 Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs)
2093 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002094 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002095 return V(jit->builder->CreateFCmpOLT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002096 }
2097
2098 Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs)
2099 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002100 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002101 return V(jit->builder->CreateFCmpOLE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002102 }
2103
2104 Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs)
2105 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002106 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002107 return V(jit->builder->CreateFCmpONE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002108 }
2109
2110 Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs)
2111 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002112 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002113 return V(jit->builder->CreateFCmpORD(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002114 }
2115
2116 Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs)
2117 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002118 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002119 return V(jit->builder->CreateFCmpUNO(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002120 }
2121
2122 Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs)
2123 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002124 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002125 return V(jit->builder->CreateFCmpUEQ(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002126 }
2127
2128 Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs)
2129 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002130 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002131 return V(jit->builder->CreateFCmpUGT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002132 }
2133
2134 Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs)
2135 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002136 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002137 return V(jit->builder->CreateFCmpUGE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002138 }
2139
2140 Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs)
2141 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002142 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002143 return V(jit->builder->CreateFCmpULT(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002144 }
2145
2146 Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs)
2147 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002148 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002149 return V(jit->builder->CreateFCmpULE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002150 }
2151
2152 Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs)
2153 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002154 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002155 return V(jit->builder->CreateFCmpUNE(V(lhs), V(rhs)));
John Bauman89401822014-05-06 15:04:28 -04002156 }
2157
Nicolas Capense95d5342016-09-30 11:37:28 -04002158 Value *Nucleus::createExtractElement(Value *vector, Type *type, int index)
John Bauman89401822014-05-06 15:04:28 -04002159 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002160 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytoneb50d252019-04-15 13:50:01 -04002161 ASSERT(V(vector)->getType()->getContainedType(0) == T(type));
Ben Clayton6f8e5652019-06-29 01:58:02 +01002162 return V(jit->builder->CreateExtractElement(V(vector), V(createConstantInt(index))));
John Bauman89401822014-05-06 15:04:28 -04002163 }
2164
2165 Value *Nucleus::createInsertElement(Value *vector, Value *element, int index)
2166 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002167 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002168 return V(jit->builder->CreateInsertElement(V(vector), V(element), V(createConstantInt(index))));
John Bauman89401822014-05-06 15:04:28 -04002169 }
2170
Logan Chien191b3052018-08-31 16:57:15 +08002171 Value *Nucleus::createShuffleVector(Value *v1, Value *v2, const int *select)
John Bauman89401822014-05-06 15:04:28 -04002172 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002173 RR_DEBUG_INFO_UPDATE_LOC();
2174
Logan Chien191b3052018-08-31 16:57:15 +08002175 int size = llvm::cast<llvm::VectorType>(V(v1)->getType())->getNumElements();
Nicolas Capense89cd582016-09-30 14:23:47 -04002176 const int maxSize = 16;
2177 llvm::Constant *swizzle[maxSize];
Ben Claytoneb50d252019-04-15 13:50:01 -04002178 ASSERT(size <= maxSize);
Nicolas Capense89cd582016-09-30 14:23:47 -04002179
2180 for(int i = 0; i < size; i++)
2181 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002182 swizzle[i] = llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), select[i]);
Nicolas Capense89cd582016-09-30 14:23:47 -04002183 }
2184
2185 llvm::Value *shuffle = llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(swizzle, size));
2186
Ben Clayton6f8e5652019-06-29 01:58:02 +01002187 return V(jit->builder->CreateShuffleVector(V(v1), V(v2), shuffle));
John Bauman89401822014-05-06 15:04:28 -04002188 }
2189
Logan Chien191b3052018-08-31 16:57:15 +08002190 Value *Nucleus::createSelect(Value *c, Value *ifTrue, Value *ifFalse)
John Bauman89401822014-05-06 15:04:28 -04002191 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002192 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002193 return V(jit->builder->CreateSelect(V(c), V(ifTrue), V(ifFalse)));
John Bauman89401822014-05-06 15:04:28 -04002194 }
2195
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05002196 SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
John Bauman89401822014-05-06 15:04:28 -04002197 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002198 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002199 return reinterpret_cast<SwitchCases*>(jit->builder->CreateSwitch(V(control), B(defaultBranch), numCases));
John Bauman89401822014-05-06 15:04:28 -04002200 }
2201
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05002202 void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
John Bauman89401822014-05-06 15:04:28 -04002203 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002204 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002205 llvm::SwitchInst *sw = reinterpret_cast<llvm::SwitchInst *>(switchCases);
Ben Clayton6f8e5652019-06-29 01:58:02 +01002206 sw->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), label, true), B(branch));
John Bauman89401822014-05-06 15:04:28 -04002207 }
2208
Nicolas Capens3d7c35f2016-09-28 10:36:57 -04002209 void Nucleus::createUnreachable()
John Bauman89401822014-05-06 15:04:28 -04002210 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002211 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002212 jit->builder->CreateUnreachable();
John Bauman89401822014-05-06 15:04:28 -04002213 }
2214
Nicolas Capensac230122016-09-20 14:30:06 -04002215 Type *Nucleus::getPointerType(Type *ElementType)
John Bauman89401822014-05-06 15:04:28 -04002216 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002217 return T(llvm::PointerType::get(T(ElementType), 0));
John Bauman89401822014-05-06 15:04:28 -04002218 }
2219
Nicolas Capens13ac2322016-10-13 14:52:12 -04002220 Value *Nucleus::createNullValue(Type *Ty)
John Bauman89401822014-05-06 15:04:28 -04002221 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002222 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002223 return V(llvm::Constant::getNullValue(T(Ty)));
John Bauman89401822014-05-06 15:04:28 -04002224 }
2225
Nicolas Capens13ac2322016-10-13 14:52:12 -04002226 Value *Nucleus::createConstantLong(int64_t i)
John Bauman89401822014-05-06 15:04:28 -04002227 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002228 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002229 return V(llvm::ConstantInt::get(llvm::Type::getInt64Ty(jit->context), i, true));
John Bauman89401822014-05-06 15:04:28 -04002230 }
2231
Nicolas Capens13ac2322016-10-13 14:52:12 -04002232 Value *Nucleus::createConstantInt(int i)
John Bauman89401822014-05-06 15:04:28 -04002233 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002234 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002235 return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), i, true));
John Bauman89401822014-05-06 15:04:28 -04002236 }
2237
Nicolas Capens13ac2322016-10-13 14:52:12 -04002238 Value *Nucleus::createConstantInt(unsigned int i)
John Bauman89401822014-05-06 15:04:28 -04002239 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002240 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002241 return V(llvm::ConstantInt::get(llvm::Type::getInt32Ty(jit->context), i, false));
John Bauman89401822014-05-06 15:04:28 -04002242 }
2243
Nicolas Capens13ac2322016-10-13 14:52:12 -04002244 Value *Nucleus::createConstantBool(bool b)
John Bauman89401822014-05-06 15:04:28 -04002245 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002246 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002247 return V(llvm::ConstantInt::get(llvm::Type::getInt1Ty(jit->context), b));
John Bauman89401822014-05-06 15:04:28 -04002248 }
2249
Nicolas Capens13ac2322016-10-13 14:52:12 -04002250 Value *Nucleus::createConstantByte(signed char i)
John Bauman89401822014-05-06 15:04:28 -04002251 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002252 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002253 return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(jit->context), i, true));
John Bauman89401822014-05-06 15:04:28 -04002254 }
2255
Nicolas Capens13ac2322016-10-13 14:52:12 -04002256 Value *Nucleus::createConstantByte(unsigned char i)
John Bauman89401822014-05-06 15:04:28 -04002257 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002258 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002259 return V(llvm::ConstantInt::get(llvm::Type::getInt8Ty(jit->context), i, false));
John Bauman89401822014-05-06 15:04:28 -04002260 }
2261
Nicolas Capens13ac2322016-10-13 14:52:12 -04002262 Value *Nucleus::createConstantShort(short i)
John Bauman89401822014-05-06 15:04:28 -04002263 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002264 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002265 return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(jit->context), i, true));
John Bauman89401822014-05-06 15:04:28 -04002266 }
2267
Nicolas Capens13ac2322016-10-13 14:52:12 -04002268 Value *Nucleus::createConstantShort(unsigned short i)
John Bauman89401822014-05-06 15:04:28 -04002269 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002270 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01002271 return V(llvm::ConstantInt::get(llvm::Type::getInt16Ty(jit->context), i, false));
John Bauman89401822014-05-06 15:04:28 -04002272 }
2273
Nicolas Capens13ac2322016-10-13 14:52:12 -04002274 Value *Nucleus::createConstantFloat(float x)
John Bauman89401822014-05-06 15:04:28 -04002275 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002276 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002277 return V(llvm::ConstantFP::get(T(Float::getType()), x));
John Bauman89401822014-05-06 15:04:28 -04002278 }
2279
Nicolas Capens13ac2322016-10-13 14:52:12 -04002280 Value *Nucleus::createNullPointer(Type *Ty)
John Bauman89401822014-05-06 15:04:28 -04002281 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002282 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002283 return V(llvm::ConstantPointerNull::get(llvm::PointerType::get(T(Ty), 0)));
John Bauman89401822014-05-06 15:04:28 -04002284 }
2285
Nicolas Capens13ac2322016-10-13 14:52:12 -04002286 Value *Nucleus::createConstantVector(const int64_t *constants, Type *type)
John Bauman89401822014-05-06 15:04:28 -04002287 {
Ben Claytoneb50d252019-04-15 13:50:01 -04002288 ASSERT(llvm::isa<llvm::VectorType>(T(type)));
Nicolas Capens69674fb2017-09-01 11:08:44 -04002289 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 Claytoneb50d252019-04-15 13:50:01 -04002291 ASSERT(numElements <= 16 && numConstants <= numElements);
Nicolas Capens13ac2322016-10-13 14:52:12 -04002292 llvm::Constant *constantVector[16];
2293
Nicolas Capens69674fb2017-09-01 11:08:44 -04002294 for(int i = 0; i < numElements; i++)
Nicolas Capens13ac2322016-10-13 14:52:12 -04002295 {
Nicolas Capens69674fb2017-09-01 11:08:44 -04002296 constantVector[i] = llvm::ConstantInt::get(T(type)->getContainedType(0), constants[i % numConstants]);
Nicolas Capens13ac2322016-10-13 14:52:12 -04002297 }
2298
Nicolas Capens69674fb2017-09-01 11:08:44 -04002299 return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements)));
Nicolas Capens13ac2322016-10-13 14:52:12 -04002300 }
2301
2302 Value *Nucleus::createConstantVector(const double *constants, Type *type)
2303 {
Ben Claytoneb50d252019-04-15 13:50:01 -04002304 ASSERT(llvm::isa<llvm::VectorType>(T(type)));
Nicolas Capens69674fb2017-09-01 11:08:44 -04002305 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 Claytoneb50d252019-04-15 13:50:01 -04002307 ASSERT(numElements <= 8 && numConstants <= numElements);
Nicolas Capens13ac2322016-10-13 14:52:12 -04002308 llvm::Constant *constantVector[8];
2309
Nicolas Capens69674fb2017-09-01 11:08:44 -04002310 for(int i = 0; i < numElements; i++)
Nicolas Capens13ac2322016-10-13 14:52:12 -04002311 {
Nicolas Capens69674fb2017-09-01 11:08:44 -04002312 constantVector[i] = llvm::ConstantFP::get(T(type)->getContainedType(0), constants[i % numConstants]);
Nicolas Capens13ac2322016-10-13 14:52:12 -04002313 }
2314
Nicolas Capens69674fb2017-09-01 11:08:44 -04002315 return V(llvm::ConstantVector::get(llvm::ArrayRef<llvm::Constant*>(constantVector, numElements)));
John Bauman89401822014-05-06 15:04:28 -04002316 }
2317
John Bauman19bac1e2014-05-06 15:23:49 -04002318 Type *Void::getType()
John Bauman89401822014-05-06 15:04:28 -04002319 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002320 return T(llvm::Type::getVoidTy(jit->context));
John Bauman89401822014-05-06 15:04:28 -04002321 }
2322
John Bauman19bac1e2014-05-06 15:23:49 -04002323 Type *Bool::getType()
John Bauman89401822014-05-06 15:04:28 -04002324 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002325 return T(llvm::Type::getInt1Ty(jit->context));
John Bauman89401822014-05-06 15:04:28 -04002326 }
2327
John Bauman19bac1e2014-05-06 15:23:49 -04002328 Type *Byte::getType()
John Bauman89401822014-05-06 15:04:28 -04002329 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002330 return T(llvm::Type::getInt8Ty(jit->context));
John Bauman89401822014-05-06 15:04:28 -04002331 }
2332
John Bauman19bac1e2014-05-06 15:23:49 -04002333 Type *SByte::getType()
John Bauman89401822014-05-06 15:04:28 -04002334 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002335 return T(llvm::Type::getInt8Ty(jit->context));
John Bauman89401822014-05-06 15:04:28 -04002336 }
2337
John Bauman19bac1e2014-05-06 15:23:49 -04002338 Type *Short::getType()
John Bauman89401822014-05-06 15:04:28 -04002339 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002340 return T(llvm::Type::getInt16Ty(jit->context));
John Bauman89401822014-05-06 15:04:28 -04002341 }
2342
John Bauman19bac1e2014-05-06 15:23:49 -04002343 Type *UShort::getType()
John Bauman89401822014-05-06 15:04:28 -04002344 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002345 return T(llvm::Type::getInt16Ty(jit->context));
John Bauman89401822014-05-06 15:04:28 -04002346 }
2347
John Bauman19bac1e2014-05-06 15:23:49 -04002348 Type *Byte4::getType()
John Bauman89401822014-05-06 15:04:28 -04002349 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002350 return T(Type_v4i8);
John Bauman89401822014-05-06 15:04:28 -04002351 }
2352
John Bauman19bac1e2014-05-06 15:23:49 -04002353 Type *SByte4::getType()
John Bauman89401822014-05-06 15:04:28 -04002354 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002355 return T(Type_v4i8);
John Bauman89401822014-05-06 15:04:28 -04002356 }
2357
John Bauman19bac1e2014-05-06 15:23:49 -04002358 RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04002359 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002360 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002361#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002362 return x86::paddusb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002363#else
2364 return As<Byte8>(V(lowerPUADDSAT(V(x.value), V(y.value))));
2365#endif
John Bauman89401822014-05-06 15:04:28 -04002366 }
John Bauman66b8ab22014-05-06 15:57:45 -04002367
John Bauman19bac1e2014-05-06 15:23:49 -04002368 RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04002369 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002370 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002371#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002372 return x86::psubusb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002373#else
2374 return As<Byte8>(V(lowerPUSUBSAT(V(x.value), V(y.value))));
2375#endif
John Bauman89401822014-05-06 15:04:28 -04002376 }
2377
John Bauman19bac1e2014-05-06 15:23:49 -04002378 RValue<Int> SignMask(RValue<Byte8> x)
John Bauman89401822014-05-06 15:04:28 -04002379 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002380 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002381#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002382 return x86::pmovmskb(x);
Logan Chiene3191012018-08-24 22:01:50 +08002383#else
2384 return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType()))));
2385#endif
John Bauman89401822014-05-06 15:04:28 -04002386 }
2387
John Bauman19bac1e2014-05-06 15:23:49 -04002388// RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04002389// {
Logan Chiene3191012018-08-24 22:01:50 +08002390//#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002391// return x86::pcmpgtb(x, y); // FIXME: Signedness
Logan Chiene3191012018-08-24 22:01:50 +08002392//#else
2393// return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType()))));
2394//#endif
John Bauman89401822014-05-06 15:04:28 -04002395// }
John Bauman66b8ab22014-05-06 15:57:45 -04002396
John Bauman19bac1e2014-05-06 15:23:49 -04002397 RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04002398 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002399 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002400#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002401 return x86::pcmpeqb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002402#else
2403 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType()))));
2404#endif
John Bauman89401822014-05-06 15:04:28 -04002405 }
2406
John Bauman19bac1e2014-05-06 15:23:49 -04002407 Type *Byte8::getType()
John Bauman89401822014-05-06 15:04:28 -04002408 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002409 return T(Type_v8i8);
John Bauman89401822014-05-06 15:04:28 -04002410 }
2411
John Bauman19bac1e2014-05-06 15:23:49 -04002412 RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04002413 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002414 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002415#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002416 return x86::paddsb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002417#else
2418 return As<SByte8>(V(lowerPSADDSAT(V(x.value), V(y.value))));
2419#endif
John Bauman89401822014-05-06 15:04:28 -04002420 }
John Bauman66b8ab22014-05-06 15:57:45 -04002421
John Bauman19bac1e2014-05-06 15:23:49 -04002422 RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04002423 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002424 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002425#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002426 return x86::psubsb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002427#else
2428 return As<SByte8>(V(lowerPSSUBSAT(V(x.value), V(y.value))));
2429#endif
John Bauman89401822014-05-06 15:04:28 -04002430 }
2431
John Bauman19bac1e2014-05-06 15:23:49 -04002432 RValue<Int> SignMask(RValue<SByte8> x)
John Bauman89401822014-05-06 15:04:28 -04002433 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002434 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002435#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002436 return x86::pmovmskb(As<Byte8>(x));
Logan Chiene3191012018-08-24 22:01:50 +08002437#else
2438 return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType()))));
2439#endif
John Bauman89401822014-05-06 15:04:28 -04002440 }
2441
John Bauman19bac1e2014-05-06 15:23:49 -04002442 RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04002443 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002444 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002445#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002446 return x86::pcmpgtb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002447#else
2448 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType()))));
2449#endif
John Bauman89401822014-05-06 15:04:28 -04002450 }
John Bauman66b8ab22014-05-06 15:57:45 -04002451
John Bauman19bac1e2014-05-06 15:23:49 -04002452 RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04002453 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002454 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002455#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002456 return x86::pcmpeqb(As<Byte8>(x), As<Byte8>(y));
Logan Chiene3191012018-08-24 22:01:50 +08002457#else
2458 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType()))));
2459#endif
John Bauman89401822014-05-06 15:04:28 -04002460 }
2461
John Bauman19bac1e2014-05-06 15:23:49 -04002462 Type *SByte8::getType()
John Bauman89401822014-05-06 15:04:28 -04002463 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002464 return T(Type_v8i8);
John Bauman89401822014-05-06 15:04:28 -04002465 }
2466
John Bauman19bac1e2014-05-06 15:23:49 -04002467 Type *Byte16::getType()
John Bauman89401822014-05-06 15:04:28 -04002468 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002469 return T(llvm::VectorType::get(T(Byte::getType()), 16));
John Bauman89401822014-05-06 15:04:28 -04002470 }
2471
John Bauman19bac1e2014-05-06 15:23:49 -04002472 Type *SByte16::getType()
John Bauman89401822014-05-06 15:04:28 -04002473 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002474 return T(llvm::VectorType::get(T(SByte::getType()), 16));
John Bauman89401822014-05-06 15:04:28 -04002475 }
2476
Nicolas Capens16b5f152016-10-13 13:39:01 -04002477 Type *Short2::getType()
2478 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002479 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002480 }
2481
Nicolas Capens16b5f152016-10-13 13:39:01 -04002482 Type *UShort2::getType()
2483 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002484 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04002485 }
2486
John Bauman19bac1e2014-05-06 15:23:49 -04002487 Short4::Short4(RValue<Int4> cast)
John Bauman89401822014-05-06 15:04:28 -04002488 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002489 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens01a97962017-07-28 17:30:51 -04002490 int select[8] = {0, 2, 4, 6, 0, 2, 4, 6};
John Bauman89401822014-05-06 15:04:28 -04002491 Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType());
2492
Nicolas Capens01a97962017-07-28 17:30:51 -04002493 Value *packed = Nucleus::createShuffleVector(short8, short8, select);
2494 Value *short4 = As<Short4>(Int2(As<Int4>(packed))).value;
John Bauman89401822014-05-06 15:04:28 -04002495
John Bauman66b8ab22014-05-06 15:57:45 -04002496 storeValue(short4);
John Bauman89401822014-05-06 15:04:28 -04002497 }
2498
John Bauman19bac1e2014-05-06 15:23:49 -04002499// Short4::Short4(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04002500// {
2501// }
2502
John Bauman19bac1e2014-05-06 15:23:49 -04002503 Short4::Short4(RValue<Float4> cast)
John Bauman89401822014-05-06 15:04:28 -04002504 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002505 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002506 Int4 v4i32 = Int4(cast);
Logan Chiene3191012018-08-24 22:01:50 +08002507#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002508 v4i32 = As<Int4>(x86::packssdw(v4i32, v4i32));
Logan Chiene3191012018-08-24 22:01:50 +08002509#else
2510 Value *v = v4i32.loadValue();
2511 v4i32 = As<Int4>(V(lowerPack(V(v), V(v), true)));
2512#endif
John Bauman66b8ab22014-05-06 15:57:45 -04002513
2514 storeValue(As<Short4>(Int2(v4i32)).value);
John Bauman89401822014-05-06 15:04:28 -04002515 }
2516
John Bauman19bac1e2014-05-06 15:23:49 -04002517 RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002518 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002519 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002520#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002521 // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value));
2522
2523 return x86::psllw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002524#else
2525 return As<Short4>(V(lowerVectorShl(V(lhs.value), rhs)));
2526#endif
John Bauman89401822014-05-06 15:04:28 -04002527 }
2528
John Bauman19bac1e2014-05-06 15:23:49 -04002529 RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002530 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002531 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002532#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002533 return x86::psraw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002534#else
2535 return As<Short4>(V(lowerVectorAShr(V(lhs.value), rhs)));
2536#endif
John Bauman89401822014-05-06 15:04:28 -04002537 }
2538
John Bauman19bac1e2014-05-06 15:23:49 -04002539 RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002540 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002541 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002542#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002543 return x86::pmaxsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002544#else
2545 return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT)));
2546#endif
John Bauman89401822014-05-06 15:04:28 -04002547 }
2548
John Bauman19bac1e2014-05-06 15:23:49 -04002549 RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002550 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002551 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002552#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002553 return x86::pminsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002554#else
2555 return RValue<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT)));
2556#endif
John Bauman89401822014-05-06 15:04:28 -04002557 }
2558
John Bauman19bac1e2014-05-06 15:23:49 -04002559 RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002560 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002561 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002562#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002563 return x86::paddsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002564#else
2565 return As<Short4>(V(lowerPSADDSAT(V(x.value), V(y.value))));
2566#endif
John Bauman89401822014-05-06 15:04:28 -04002567 }
2568
John Bauman19bac1e2014-05-06 15:23:49 -04002569 RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002570 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002571 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002572#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002573 return x86::psubsw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002574#else
2575 return As<Short4>(V(lowerPSSUBSAT(V(x.value), V(y.value))));
2576#endif
John Bauman89401822014-05-06 15:04:28 -04002577 }
2578
John Bauman19bac1e2014-05-06 15:23:49 -04002579 RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002580 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002581 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002582#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002583 return x86::pmulhw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002584#else
2585 return As<Short4>(V(lowerMulHigh(V(x.value), V(y.value), true)));
2586#endif
John Bauman89401822014-05-06 15:04:28 -04002587 }
2588
John Bauman19bac1e2014-05-06 15:23:49 -04002589 RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002590 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002591 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002592#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002593 return x86::pmaddwd(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002594#else
2595 return As<Int2>(V(lowerMulAdd(V(x.value), V(y.value))));
2596#endif
John Bauman89401822014-05-06 15:04:28 -04002597 }
2598
Nicolas Capens33438a62017-09-27 11:47:35 -04002599 RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002600 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002601 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002602#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens01a97962017-07-28 17:30:51 -04002603 auto result = x86::packsswb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002604#else
2605 auto result = V(lowerPack(V(x.value), V(y.value), true));
2606#endif
Nicolas Capens01a97962017-07-28 17:30:51 -04002607 return As<SByte8>(Swizzle(As<Int4>(result), 0x88));
John Bauman89401822014-05-06 15:04:28 -04002608 }
2609
Nicolas Capens33438a62017-09-27 11:47:35 -04002610 RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y)
2611 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002612 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002613#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens33438a62017-09-27 11:47:35 -04002614 auto result = x86::packuswb(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002615#else
2616 auto result = V(lowerPack(V(x.value), V(y.value), false));
2617#endif
Nicolas Capens33438a62017-09-27 11:47:35 -04002618 return As<Byte8>(Swizzle(As<Int4>(result), 0x88));
2619 }
2620
John Bauman19bac1e2014-05-06 15:23:49 -04002621 RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002622 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002623 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002624#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002625 return x86::pcmpgtw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002626#else
2627 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType()))));
2628#endif
John Bauman89401822014-05-06 15:04:28 -04002629 }
2630
John Bauman19bac1e2014-05-06 15:23:49 -04002631 RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04002632 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002633 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002634#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002635 return x86::pcmpeqw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002636#else
2637 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType()))));
2638#endif
John Bauman89401822014-05-06 15:04:28 -04002639 }
2640
John Bauman19bac1e2014-05-06 15:23:49 -04002641 Type *Short4::getType()
John Bauman89401822014-05-06 15:04:28 -04002642 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002643 return T(Type_v4i16);
John Bauman89401822014-05-06 15:04:28 -04002644 }
2645
John Bauman19bac1e2014-05-06 15:23:49 -04002646 UShort4::UShort4(RValue<Float4> cast, bool saturate)
John Bauman89401822014-05-06 15:04:28 -04002647 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002648 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002649 if(saturate)
2650 {
Logan Chiena8385ed2018-09-26 19:22:54 +08002651#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002652 if(CPUID::supportsSSE4_1())
2653 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002654 Int4 int4(Min(cast, Float4(0xFFFF))); // packusdw takes care of 0x0000 saturation
Nicolas Capens33438a62017-09-27 11:47:35 -04002655 *this = As<Short4>(PackUnsigned(int4, int4));
John Bauman89401822014-05-06 15:04:28 -04002656 }
2657 else
Logan Chiena8385ed2018-09-26 19:22:54 +08002658#endif
John Bauman89401822014-05-06 15:04:28 -04002659 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002660 *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000))));
John Bauman89401822014-05-06 15:04:28 -04002661 }
2662 }
2663 else
2664 {
Nicolas Capens01a97962017-07-28 17:30:51 -04002665 *this = Short4(Int4(cast));
John Bauman89401822014-05-06 15:04:28 -04002666 }
2667 }
2668
John Bauman19bac1e2014-05-06 15:23:49 -04002669 RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002670 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002671 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002672#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002673 // return RValue<Short4>(Nucleus::createShl(lhs.value, rhs.value));
2674
2675 return As<UShort4>(x86::psllw(As<Short4>(lhs), rhs));
Logan Chiene3191012018-08-24 22:01:50 +08002676#else
2677 return As<UShort4>(V(lowerVectorShl(V(lhs.value), rhs)));
2678#endif
John Bauman89401822014-05-06 15:04:28 -04002679 }
2680
John Bauman19bac1e2014-05-06 15:23:49 -04002681 RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002682 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002683 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002684#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002685 // return RValue<Short4>(Nucleus::createLShr(lhs.value, rhs.value));
2686
2687 return x86::psrlw(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08002688#else
2689 return As<UShort4>(V(lowerVectorLShr(V(lhs.value), rhs)));
2690#endif
John Bauman89401822014-05-06 15:04:28 -04002691 }
2692
John Bauman19bac1e2014-05-06 15:23:49 -04002693 RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002694 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002695 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman66b8ab22014-05-06 15:57:45 -04002696 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 Bauman89401822014-05-06 15:04:28 -04002697 }
2698
John Bauman19bac1e2014-05-06 15:23:49 -04002699 RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002700 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002701 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman66b8ab22014-05-06 15:57:45 -04002702 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 Bauman89401822014-05-06 15:04:28 -04002703 }
2704
John Bauman19bac1e2014-05-06 15:23:49 -04002705 RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002706 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002707 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002708#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002709 return x86::paddusw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002710#else
2711 return As<UShort4>(V(lowerPUADDSAT(V(x.value), V(y.value))));
2712#endif
John Bauman89401822014-05-06 15:04:28 -04002713 }
2714
John Bauman19bac1e2014-05-06 15:23:49 -04002715 RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002716 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002717 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002718#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002719 return x86::psubusw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002720#else
2721 return As<UShort4>(V(lowerPUSUBSAT(V(x.value), V(y.value))));
2722#endif
John Bauman89401822014-05-06 15:04:28 -04002723 }
2724
John Bauman19bac1e2014-05-06 15:23:49 -04002725 RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002726 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002727 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002728#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002729 return x86::pmulhuw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002730#else
2731 return As<UShort4>(V(lowerMulHigh(V(x.value), V(y.value), false)));
2732#endif
John Bauman89401822014-05-06 15:04:28 -04002733 }
2734
John Bauman19bac1e2014-05-06 15:23:49 -04002735 RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04002736 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002737 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002738#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002739 return x86::pavgw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08002740#else
2741 return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value))));
2742#endif
John Bauman89401822014-05-06 15:04:28 -04002743 }
2744
John Bauman19bac1e2014-05-06 15:23:49 -04002745 Type *UShort4::getType()
John Bauman89401822014-05-06 15:04:28 -04002746 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002747 return T(Type_v4i16);
John Bauman89401822014-05-06 15:04:28 -04002748 }
2749
John Bauman19bac1e2014-05-06 15:23:49 -04002750 RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002751 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002752 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002753#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 Bauman89401822014-05-06 15:04:28 -04002758 }
2759
John Bauman19bac1e2014-05-06 15:23:49 -04002760 RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002761 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002762 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002763#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 Bauman89401822014-05-06 15:04:28 -04002768 }
2769
John Bauman19bac1e2014-05-06 15:23:49 -04002770 RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)
John Bauman89401822014-05-06 15:04:28 -04002771 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002772 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002773#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 Bauman89401822014-05-06 15:04:28 -04002778 }
2779
John Bauman19bac1e2014-05-06 15:23:49 -04002780 RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)
John Bauman89401822014-05-06 15:04:28 -04002781 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002782 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002783#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 Bauman89401822014-05-06 15:04:28 -04002788 }
2789
John Bauman19bac1e2014-05-06 15:23:49 -04002790 Type *Short8::getType()
John Bauman89401822014-05-06 15:04:28 -04002791 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002792 return T(llvm::VectorType::get(T(Short::getType()), 8));
John Bauman89401822014-05-06 15:04:28 -04002793 }
2794
John Bauman19bac1e2014-05-06 15:23:49 -04002795 RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002796 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002797 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002798#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 Bauman89401822014-05-06 15:04:28 -04002803 }
2804
John Bauman19bac1e2014-05-06 15:23:49 -04002805 RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04002806 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002807 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002808#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002809 return x86::psrlw(lhs, rhs); // FIXME: Fallback required
Logan Chiene3191012018-08-24 22:01:50 +08002810#else
2811 return As<UShort8>(V(lowerVectorLShr(V(lhs.value), rhs)));
2812#endif
John Bauman89401822014-05-06 15:04:28 -04002813 }
2814
John Bauman19bac1e2014-05-06 15:23:49 -04002815 RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7)
John Bauman89401822014-05-06 15:04:28 -04002816 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002817 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capense89cd582016-09-30 14:23:47 -04002818 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 Bauman89401822014-05-06 15:04:28 -04002837
2838 Value *byte16 = Nucleus::createBitCast(x.value, Byte16::getType());
Nicolas Capense89cd582016-09-30 14:23:47 -04002839 Value *shuffle = Nucleus::createShuffleVector(byte16, byte16, pshufb);
John Bauman89401822014-05-06 15:04:28 -04002840 Value *short8 = Nucleus::createBitCast(shuffle, UShort8::getType());
2841
2842 return RValue<UShort8>(short8);
2843 }
2844
John Bauman19bac1e2014-05-06 15:23:49 -04002845 RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)
John Bauman89401822014-05-06 15:04:28 -04002846 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002847 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002848#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 Bauman89401822014-05-06 15:04:28 -04002853 }
2854
John Bauman19bac1e2014-05-06 15:23:49 -04002855 Type *UShort8::getType()
John Bauman89401822014-05-06 15:04:28 -04002856 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04002857 return T(llvm::VectorType::get(T(UShort::getType()), 8));
John Bauman89401822014-05-06 15:04:28 -04002858 }
2859
Nicolas Capens96d4e092016-11-18 14:22:38 -05002860 RValue<Int> operator++(Int &val, int) // Post-increment
John Bauman89401822014-05-06 15:04:28 -04002861 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002862 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002863 RValue<Int> res = val;
2864
Logan Chien191b3052018-08-31 16:57:15 +08002865 Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002866 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002867
2868 return res;
2869 }
2870
Nicolas Capens96d4e092016-11-18 14:22:38 -05002871 const Int &operator++(Int &val) // Pre-increment
John Bauman89401822014-05-06 15:04:28 -04002872 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002873 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002874 Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002875 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002876
2877 return val;
2878 }
2879
Nicolas Capens96d4e092016-11-18 14:22:38 -05002880 RValue<Int> operator--(Int &val, int) // Post-decrement
John Bauman89401822014-05-06 15:04:28 -04002881 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002882 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002883 RValue<Int> res = val;
2884
Logan Chien191b3052018-08-31 16:57:15 +08002885 Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002886 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002887
2888 return res;
2889 }
2890
Nicolas Capens96d4e092016-11-18 14:22:38 -05002891 const Int &operator--(Int &val) // Pre-decrement
John Bauman89401822014-05-06 15:04:28 -04002892 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002893 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002894 Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002895 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002896
2897 return val;
2898 }
2899
John Bauman19bac1e2014-05-06 15:23:49 -04002900 RValue<Int> RoundInt(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04002901 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002902 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08002903#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002904 return x86::cvtss2si(cast);
Logan Chiene3191012018-08-24 22:01:50 +08002905#else
Logan Chien2faa24a2018-09-26 19:59:32 +08002906 return RValue<Int>(V(lowerRoundInt(V(cast.value), T(Int::getType()))));
Logan Chiene3191012018-08-24 22:01:50 +08002907#endif
John Bauman89401822014-05-06 15:04:28 -04002908 }
2909
John Bauman19bac1e2014-05-06 15:23:49 -04002910 Type *Int::getType()
John Bauman89401822014-05-06 15:04:28 -04002911 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002912 return T(llvm::Type::getInt32Ty(jit->context));
John Bauman89401822014-05-06 15:04:28 -04002913 }
2914
John Bauman19bac1e2014-05-06 15:23:49 -04002915 Type *Long::getType()
John Bauman89401822014-05-06 15:04:28 -04002916 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002917 return T(llvm::Type::getInt64Ty(jit->context));
John Bauman89401822014-05-06 15:04:28 -04002918 }
2919
John Bauman19bac1e2014-05-06 15:23:49 -04002920 UInt::UInt(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04002921 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002922 RR_DEBUG_INFO_UPDATE_LOC();
Alexis Hetu764d1422016-09-28 08:44:22 -04002923 // Note: createFPToUI is broken, must perform conversion using createFPtoSI
2924 // Value *integer = Nucleus::createFPToUI(cast.value, UInt::getType());
John Bauman89401822014-05-06 15:04:28 -04002925
Alexis Hetu764d1422016-09-28 08:44:22 -04002926 // 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 Bauman89401822014-05-06 15:04:28 -04002938 }
2939
Nicolas Capens96d4e092016-11-18 14:22:38 -05002940 RValue<UInt> operator++(UInt &val, int) // Post-increment
John Bauman89401822014-05-06 15:04:28 -04002941 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002942 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002943 RValue<UInt> res = val;
2944
Logan Chien191b3052018-08-31 16:57:15 +08002945 Value *inc = Nucleus::createAdd(res.value, Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002946 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002947
2948 return res;
2949 }
2950
Nicolas Capens96d4e092016-11-18 14:22:38 -05002951 const UInt &operator++(UInt &val) // Pre-increment
John Bauman89401822014-05-06 15:04:28 -04002952 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002953 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002954 Value *inc = Nucleus::createAdd(val.loadValue(), Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002955 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002956
2957 return val;
2958 }
2959
Nicolas Capens96d4e092016-11-18 14:22:38 -05002960 RValue<UInt> operator--(UInt &val, int) // Post-decrement
John Bauman89401822014-05-06 15:04:28 -04002961 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002962 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04002963 RValue<UInt> res = val;
2964
Logan Chien191b3052018-08-31 16:57:15 +08002965 Value *inc = Nucleus::createSub(res.value, Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002966 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002967
2968 return res;
2969 }
2970
Nicolas Capens96d4e092016-11-18 14:22:38 -05002971 const UInt &operator--(UInt &val) // Pre-decrement
John Bauman89401822014-05-06 15:04:28 -04002972 {
Ben Claytonac07ed82019-03-26 14:17:41 +00002973 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chien191b3052018-08-31 16:57:15 +08002974 Value *inc = Nucleus::createSub(val.loadValue(), Nucleus::createConstantInt(1));
John Bauman66b8ab22014-05-06 15:57:45 -04002975 val.storeValue(inc);
John Bauman89401822014-05-06 15:04:28 -04002976
2977 return val;
2978 }
2979
John Bauman19bac1e2014-05-06 15:23:49 -04002980// RValue<UInt> RoundUInt(RValue<Float> cast)
John Bauman89401822014-05-06 15:04:28 -04002981// {
Logan Chiene3191012018-08-24 22:01:50 +08002982//#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04002983// return x86::cvtss2si(val); // FIXME: Unsigned
Logan Chiene3191012018-08-24 22:01:50 +08002984//#else
2985// return IfThenElse(cast > 0.0f, Int(cast + 0.5f), Int(cast - 0.5f));
2986//#endif
John Bauman89401822014-05-06 15:04:28 -04002987// }
2988
John Bauman19bac1e2014-05-06 15:23:49 -04002989 Type *UInt::getType()
John Bauman89401822014-05-06 15:04:28 -04002990 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01002991 return T(llvm::Type::getInt32Ty(jit->context));
John Bauman89401822014-05-06 15:04:28 -04002992 }
2993
John Bauman19bac1e2014-05-06 15:23:49 -04002994// Int2::Int2(RValue<Int> cast)
2995// {
John Bauman19bac1e2014-05-06 15:23:49 -04002996// Value *extend = Nucleus::createZExt(cast.value, Long::getType());
2997// Value *vector = Nucleus::createBitCast(extend, Int2::getType());
John Bauman66b8ab22014-05-06 15:57:45 -04002998//
Nicolas Capense89cd582016-09-30 14:23:47 -04002999// int shuffle[2] = {0, 0};
3000// Value *replicate = Nucleus::createShuffleVector(vector, vector, shuffle);
John Bauman19bac1e2014-05-06 15:23:49 -04003001//
John Bauman66b8ab22014-05-06 15:57:45 -04003002// storeValue(replicate);
John Bauman19bac1e2014-05-06 15:23:49 -04003003// }
John Bauman89401822014-05-06 15:04:28 -04003004
John Bauman19bac1e2014-05-06 15:23:49 -04003005 RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003006 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003007 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003008#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003009 // return RValue<Int2>(Nucleus::createShl(lhs.value, rhs.value));
3010
3011 return x86::pslld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08003012#else
3013 return As<Int2>(V(lowerVectorShl(V(lhs.value), rhs)));
3014#endif
John Bauman89401822014-05-06 15:04:28 -04003015 }
3016
John Bauman19bac1e2014-05-06 15:23:49 -04003017 RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003018 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003019 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003020#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003021 // return RValue<Int2>(Nucleus::createAShr(lhs.value, rhs.value));
3022
3023 return x86::psrad(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08003024#else
3025 return As<Int2>(V(lowerVectorAShr(V(lhs.value), rhs)));
3026#endif
John Bauman89401822014-05-06 15:04:28 -04003027 }
3028
John Bauman19bac1e2014-05-06 15:23:49 -04003029 Type *Int2::getType()
John Bauman89401822014-05-06 15:04:28 -04003030 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003031 return T(Type_v2i32);
John Bauman89401822014-05-06 15:04:28 -04003032 }
3033
John Bauman19bac1e2014-05-06 15:23:49 -04003034 RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003035 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003036 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003037#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003038 // return RValue<UInt2>(Nucleus::createShl(lhs.value, rhs.value));
3039
3040 return As<UInt2>(x86::pslld(As<Int2>(lhs), rhs));
Logan Chiene3191012018-08-24 22:01:50 +08003041#else
3042 return As<UInt2>(V(lowerVectorShl(V(lhs.value), rhs)));
3043#endif
John Bauman89401822014-05-06 15:04:28 -04003044 }
3045
John Bauman19bac1e2014-05-06 15:23:49 -04003046 RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003047 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003048 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003049#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003050 // return RValue<UInt2>(Nucleus::createLShr(lhs.value, rhs.value));
3051
3052 return x86::psrld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08003053#else
3054 return As<UInt2>(V(lowerVectorLShr(V(lhs.value), rhs)));
3055#endif
John Bauman89401822014-05-06 15:04:28 -04003056 }
3057
John Bauman19bac1e2014-05-06 15:23:49 -04003058 Type *UInt2::getType()
John Bauman89401822014-05-06 15:04:28 -04003059 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003060 return T(Type_v2i32);
John Bauman89401822014-05-06 15:04:28 -04003061 }
3062
Nicolas Capenscb986762017-01-20 11:34:37 -05003063 Int4::Int4(RValue<Byte4> cast) : XYZW(this)
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003064 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003065 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003066#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens01a97962017-07-28 17:30:51 -04003067 if(CPUID::supportsSSE4_1())
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003068 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003069 *this = x86::pmovzxbd(As<Byte16>(cast));
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003070 }
3071 else
Logan Chiene3191012018-08-24 22:01:50 +08003072#endif
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003073 {
Nicolas Capense89cd582016-09-30 14:23:47 -04003074 int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};
Nicolas Capens01a97962017-07-28 17:30:51 -04003075 Value *a = Nucleus::createBitCast(cast.value, Byte16::getType());
Logan Chien191b3052018-08-31 16:57:15 +08003076 Value *b = Nucleus::createShuffleVector(a, Nucleus::createNullValue(Byte16::getType()), swizzle);
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003077
Nicolas Capense89cd582016-09-30 14:23:47 -04003078 int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11};
Nicolas Capens01a97962017-07-28 17:30:51 -04003079 Value *c = Nucleus::createBitCast(b, Short8::getType());
Logan Chien191b3052018-08-31 16:57:15 +08003080 Value *d = Nucleus::createShuffleVector(c, Nucleus::createNullValue(Short8::getType()), swizzle2);
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003081
Nicolas Capens01a97962017-07-28 17:30:51 -04003082 *this = As<Int4>(d);
3083 }
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003084 }
3085
Nicolas Capenscb986762017-01-20 11:34:37 -05003086 Int4::Int4(RValue<SByte4> cast) : XYZW(this)
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003087 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003088 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003089#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens01a97962017-07-28 17:30:51 -04003090 if(CPUID::supportsSSE4_1())
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003091 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003092 *this = x86::pmovsxbd(As<SByte16>(cast));
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003093 }
3094 else
Logan Chiene3191012018-08-24 22:01:50 +08003095#endif
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003096 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003097 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 Wu601d0052016-06-10 14:18:41 -04003100
Nicolas Capense89cd582016-09-30 14:23:47 -04003101 int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3};
Nicolas Capens01a97962017-07-28 17:30:51 -04003102 Value *c = Nucleus::createBitCast(b, Short8::getType());
3103 Value *d = Nucleus::createShuffleVector(c, c, swizzle2);
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003104
Nicolas Capens01a97962017-07-28 17:30:51 -04003105 *this = As<Int4>(d) >> 24;
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003106 }
Meng-Lin Wu601d0052016-06-10 14:18:41 -04003107 }
3108
Nicolas Capenscb986762017-01-20 11:34:37 -05003109 Int4::Int4(RValue<Short4> cast) : XYZW(this)
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003110 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003111 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003112#if defined(__i386__) || defined(__x86_64__)
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003113 if(CPUID::supportsSSE4_1())
3114 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003115 *this = x86::pmovsxwd(As<Short8>(cast));
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003116 }
3117 else
Logan Chiene3191012018-08-24 22:01:50 +08003118#endif
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003119 {
Nicolas Capense89cd582016-09-30 14:23:47 -04003120 int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3};
Nicolas Capens01a97962017-07-28 17:30:51 -04003121 Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle);
3122 *this = As<Int4>(c) >> 16;
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003123 }
3124 }
3125
Nicolas Capenscb986762017-01-20 11:34:37 -05003126 Int4::Int4(RValue<UShort4> cast) : XYZW(this)
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003127 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003128 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003129#if defined(__i386__) || defined(__x86_64__)
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003130 if(CPUID::supportsSSE4_1())
3131 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003132 *this = x86::pmovzxwd(As<UShort8>(cast));
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003133 }
3134 else
Logan Chiene3191012018-08-24 22:01:50 +08003135#endif
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003136 {
Nicolas Capense89cd582016-09-30 14:23:47 -04003137 int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11};
Nicolas Capens01a97962017-07-28 17:30:51 -04003138 Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle);
3139 *this = As<Int4>(c);
Alexis Hetu2aa852f2015-10-14 16:32:39 -04003140 }
3141 }
3142
Nicolas Capenscb986762017-01-20 11:34:37 -05003143 Int4::Int4(RValue<Int> rhs) : XYZW(this)
Nicolas Capens24c8cf02016-08-15 15:33:14 -04003144 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003145 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capens24c8cf02016-08-15 15:33:14 -04003146 Value *vector = loadValue();
3147 Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0);
3148
Nicolas Capense89cd582016-09-30 14:23:47 -04003149 int swizzle[4] = {0, 0, 0, 0};
3150 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
Nicolas Capens24c8cf02016-08-15 15:33:14 -04003151
3152 storeValue(replicate);
3153 }
3154
John Bauman19bac1e2014-05-06 15:23:49 -04003155 RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003156 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003157 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003158#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003159 return x86::pslld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08003160#else
3161 return As<Int4>(V(lowerVectorShl(V(lhs.value), rhs)));
3162#endif
John Bauman89401822014-05-06 15:04:28 -04003163 }
3164
John Bauman19bac1e2014-05-06 15:23:49 -04003165 RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003166 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003167 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003168#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003169 return x86::psrad(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08003170#else
3171 return As<Int4>(V(lowerVectorAShr(V(lhs.value), rhs)));
3172#endif
John Bauman89401822014-05-06 15:04:28 -04003173 }
3174
John Bauman19bac1e2014-05-06 15:23:49 -04003175 RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y)
3176 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003177 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003178 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003179 }
3180
3181 RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y)
3182 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003183 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003184 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLT(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003185 }
3186
3187 RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y)
3188 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003189 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003190 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSLE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003191 }
3192
3193 RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y)
3194 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003195 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003196 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpNE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003197 }
3198
3199 RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y)
3200 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003201 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003202 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003203 }
3204
3205 RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y)
3206 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003207 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003208 return RValue<Int4>(Nucleus::createSExt(Nucleus::createICmpSGT(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003209 }
3210
3211 RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y)
3212 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003213 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003214#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003215 if(CPUID::supportsSSE4_1())
3216 {
3217 return x86::pmaxsd(x, y);
3218 }
3219 else
Logan Chiene3191012018-08-24 22:01:50 +08003220#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003221 {
3222 RValue<Int4> greater = CmpNLE(x, y);
Tom Anderson69bc6e82017-03-20 11:54:29 -07003223 return (x & greater) | (y & ~greater);
John Bauman19bac1e2014-05-06 15:23:49 -04003224 }
3225 }
3226
3227 RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y)
3228 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003229 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003230#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003231 if(CPUID::supportsSSE4_1())
3232 {
3233 return x86::pminsd(x, y);
3234 }
3235 else
Logan Chiene3191012018-08-24 22:01:50 +08003236#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003237 {
3238 RValue<Int4> less = CmpLT(x, y);
Tom Anderson69bc6e82017-03-20 11:54:29 -07003239 return (x & less) | (y & ~less);
John Bauman19bac1e2014-05-06 15:23:49 -04003240 }
3241 }
3242
3243 RValue<Int4> RoundInt(RValue<Float4> cast)
John Bauman89401822014-05-06 15:04:28 -04003244 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003245 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003246#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003247 return x86::cvtps2dq(cast);
Logan Chiene3191012018-08-24 22:01:50 +08003248#else
Logan Chien2faa24a2018-09-26 19:59:32 +08003249 return As<Int4>(V(lowerRoundInt(V(cast.value), T(Int4::getType()))));
Logan Chiene3191012018-08-24 22:01:50 +08003250#endif
John Bauman89401822014-05-06 15:04:28 -04003251 }
3252
Chris Forbese86b6dc2019-03-01 09:08:47 -08003253 RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y)
3254 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003255 RR_DEBUG_INFO_UPDATE_LOC();
Chris Forbese86b6dc2019-03-01 09:08:47 -08003256 // 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 Claytonac07ed82019-03-26 14:17:41 +00003262 RR_DEBUG_INFO_UPDATE_LOC();
Chris Forbese86b6dc2019-03-01 09:08:47 -08003263 // 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 Capens33438a62017-09-27 11:47:35 -04003267 RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y)
John Bauman89401822014-05-06 15:04:28 -04003268 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003269 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003270#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003271 return x86::packssdw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08003272#else
3273 return As<Short8>(V(lowerPack(V(x.value), V(y.value), true)));
3274#endif
John Bauman89401822014-05-06 15:04:28 -04003275 }
3276
Nicolas Capens33438a62017-09-27 11:47:35 -04003277 RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y)
3278 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003279 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003280#if defined(__i386__) || defined(__x86_64__)
Nicolas Capens33438a62017-09-27 11:47:35 -04003281 return x86::packusdw(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08003282#else
3283 return As<UShort8>(V(lowerPack(V(x.value), V(y.value), false)));
3284#endif
Nicolas Capens33438a62017-09-27 11:47:35 -04003285 }
3286
John Bauman19bac1e2014-05-06 15:23:49 -04003287 RValue<Int> SignMask(RValue<Int4> x)
John Bauman89401822014-05-06 15:04:28 -04003288 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003289 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003290#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003291 return x86::movmskps(As<Float4>(x));
Logan Chiene3191012018-08-24 22:01:50 +08003292#else
3293 return As<Int>(V(lowerSignMask(V(x.value), T(Int::getType()))));
3294#endif
John Bauman89401822014-05-06 15:04:28 -04003295 }
3296
John Bauman19bac1e2014-05-06 15:23:49 -04003297 Type *Int4::getType()
John Bauman89401822014-05-06 15:04:28 -04003298 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003299 return T(llvm::VectorType::get(T(Int::getType()), 4));
John Bauman89401822014-05-06 15:04:28 -04003300 }
3301
Nicolas Capenscb986762017-01-20 11:34:37 -05003302 UInt4::UInt4(RValue<Float4> cast) : XYZW(this)
John Bauman89401822014-05-06 15:04:28 -04003303 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003304 RR_DEBUG_INFO_UPDATE_LOC();
Alexis Hetu764d1422016-09-28 08:44:22 -04003305 // Note: createFPToUI is broken, must perform conversion using createFPtoSI
3306 // Value *xyzw = Nucleus::createFPToUI(cast.value, UInt4::getType());
John Bauman89401822014-05-06 15:04:28 -04003307
Alexis Hetu764d1422016-09-28 08:44:22 -04003308 // 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 Bauman89401822014-05-06 15:04:28 -04003320 }
3321
Ben Clayton88816fa2019-05-15 17:08:14 +01003322 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 Bauman19bac1e2014-05-06 15:23:49 -04003334 RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003335 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003336 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003337#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003338 return As<UInt4>(x86::pslld(As<Int4>(lhs), rhs));
Logan Chiene3191012018-08-24 22:01:50 +08003339#else
3340 return As<UInt4>(V(lowerVectorShl(V(lhs.value), rhs)));
3341#endif
John Bauman89401822014-05-06 15:04:28 -04003342 }
3343
John Bauman19bac1e2014-05-06 15:23:49 -04003344 RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs)
John Bauman89401822014-05-06 15:04:28 -04003345 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003346 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003347#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003348 return x86::psrld(lhs, rhs);
Logan Chiene3191012018-08-24 22:01:50 +08003349#else
3350 return As<UInt4>(V(lowerVectorLShr(V(lhs.value), rhs)));
3351#endif
John Bauman89401822014-05-06 15:04:28 -04003352 }
3353
John Bauman19bac1e2014-05-06 15:23:49 -04003354 RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y)
3355 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003356 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003357 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpEQ(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003358 }
3359
3360 RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y)
3361 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003362 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman19bac1e2014-05-06 15:23:49 -04003363 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 Claytonac07ed82019-03-26 14:17:41 +00003368 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003369 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpULE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003370 }
3371
3372 RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y)
3373 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003374 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman19bac1e2014-05-06 15:23:49 -04003375 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 Claytonac07ed82019-03-26 14:17:41 +00003380 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytona7edc1c2019-06-20 12:17:03 +01003381 return RValue<UInt4>(Nucleus::createSExt(Nucleus::createICmpUGE(x.value, y.value), Int4::getType()));
John Bauman19bac1e2014-05-06 15:23:49 -04003382 }
3383
3384 RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y)
3385 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003386 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman19bac1e2014-05-06 15:23:49 -04003387 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 Claytonac07ed82019-03-26 14:17:41 +00003392 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003393#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003394 if(CPUID::supportsSSE4_1())
3395 {
3396 return x86::pmaxud(x, y);
3397 }
3398 else
Logan Chiene3191012018-08-24 22:01:50 +08003399#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003400 {
3401 RValue<UInt4> greater = CmpNLE(x, y);
Tom Anderson69bc6e82017-03-20 11:54:29 -07003402 return (x & greater) | (y & ~greater);
John Bauman19bac1e2014-05-06 15:23:49 -04003403 }
3404 }
3405
3406 RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y)
3407 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003408 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003409#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003410 if(CPUID::supportsSSE4_1())
3411 {
3412 return x86::pminud(x, y);
3413 }
3414 else
Logan Chiene3191012018-08-24 22:01:50 +08003415#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003416 {
3417 RValue<UInt4> less = CmpLT(x, y);
Tom Anderson69bc6e82017-03-20 11:54:29 -07003418 return (x & less) | (y & ~less);
John Bauman19bac1e2014-05-06 15:23:49 -04003419 }
3420 }
3421
John Bauman19bac1e2014-05-06 15:23:49 -04003422 Type *UInt4::getType()
John Bauman89401822014-05-06 15:04:28 -04003423 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003424 return T(llvm::VectorType::get(T(UInt::getType()), 4));
John Bauman89401822014-05-06 15:04:28 -04003425 }
3426
Alexis Hetu734e2572018-12-20 14:00:49 -05003427 Type *Half::getType()
3428 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003429 return T(llvm::Type::getInt16Ty(jit->context));
Alexis Hetu734e2572018-12-20 14:00:49 -05003430 }
3431
Nicolas Capens05b3d662016-02-25 23:58:33 -05003432 RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2)
John Bauman89401822014-05-06 15:04:28 -04003433 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003434 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003435#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 Capens47dc8672017-04-25 12:54:39 -04003442 return x86::rcpss(x);
Logan Chiene3191012018-08-24 22:01:50 +08003443#else
3444 return As<Float>(V(lowerRCP(V(x.value))));
3445#endif
John Bauman89401822014-05-06 15:04:28 -04003446 }
John Bauman66b8ab22014-05-06 15:57:45 -04003447
John Bauman19bac1e2014-05-06 15:23:49 -04003448 RValue<Float> RcpSqrt_pp(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003449 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003450 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003451#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003452 return x86::rsqrtss(x);
Logan Chiene3191012018-08-24 22:01:50 +08003453#else
3454 return As<Float>(V(lowerRSQRT(V(x.value))));
3455#endif
John Bauman89401822014-05-06 15:04:28 -04003456 }
3457
John Bauman19bac1e2014-05-06 15:23:49 -04003458 RValue<Float> Sqrt(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003459 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003460 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003461#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003462 return x86::sqrtss(x);
Logan Chiene3191012018-08-24 22:01:50 +08003463#else
3464 return As<Float>(V(lowerSQRT(V(x.value))));
3465#endif
John Bauman89401822014-05-06 15:04:28 -04003466 }
3467
John Bauman19bac1e2014-05-06 15:23:49 -04003468 RValue<Float> Round(RValue<Float> x)
3469 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003470 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003471#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003472 if(CPUID::supportsSSE4_1())
3473 {
3474 return x86::roundss(x, 0);
3475 }
3476 else
3477 {
3478 return Float4(Round(Float4(x))).x;
3479 }
Logan Chien83fc07a2018-09-26 22:14:00 +08003480#else
3481 return RValue<Float>(V(lowerRound(V(x.value))));
3482#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003483 }
3484
3485 RValue<Float> Trunc(RValue<Float> x)
3486 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003487 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003488#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003489 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 Chien8c5ca8d2018-09-27 21:05:53 +08003497#else
3498 return RValue<Float>(V(lowerTrunc(V(x.value))));
3499#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003500 }
3501
3502 RValue<Float> Frac(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003503 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003504 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003505#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003506 if(CPUID::supportsSSE4_1())
3507 {
3508 return x - x86::floorss(x);
3509 }
3510 else
3511 {
John Bauman19bac1e2014-05-06 15:23:49 -04003512 return Float4(Frac(Float4(x))).x;
John Bauman89401822014-05-06 15:04:28 -04003513 }
Logan Chien3c6a1ae2018-09-26 22:18:16 +08003514#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 Bauman89401822014-05-06 15:04:28 -04003519 }
3520
John Bauman19bac1e2014-05-06 15:23:49 -04003521 RValue<Float> Floor(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003522 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003523 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003524#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003525 if(CPUID::supportsSSE4_1())
3526 {
3527 return x86::floorss(x);
3528 }
3529 else
3530 {
3531 return Float4(Floor(Float4(x))).x;
3532 }
Logan Chien40a60052018-09-26 19:03:53 +08003533#else
3534 return RValue<Float>(V(lowerFloor(V(x.value))));
3535#endif
John Bauman89401822014-05-06 15:04:28 -04003536 }
3537
John Bauman19bac1e2014-05-06 15:23:49 -04003538 RValue<Float> Ceil(RValue<Float> x)
John Bauman89401822014-05-06 15:04:28 -04003539 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003540 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003541#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003542 if(CPUID::supportsSSE4_1())
3543 {
3544 return x86::ceilss(x);
3545 }
3546 else
Logan Chiene3191012018-08-24 22:01:50 +08003547#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003548 {
3549 return Float4(Ceil(Float4(x))).x;
3550 }
John Bauman89401822014-05-06 15:04:28 -04003551 }
3552
John Bauman19bac1e2014-05-06 15:23:49 -04003553 Type *Float::getType()
John Bauman89401822014-05-06 15:04:28 -04003554 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003555 return T(llvm::Type::getFloatTy(jit->context));
John Bauman89401822014-05-06 15:04:28 -04003556 }
3557
John Bauman19bac1e2014-05-06 15:23:49 -04003558 Type *Float2::getType()
John Bauman89401822014-05-06 15:04:28 -04003559 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003560 return T(Type_v2f32);
John Bauman89401822014-05-06 15:04:28 -04003561 }
3562
Nicolas Capens88ac3672019-08-01 13:22:34 -04003563 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 Capenscb986762017-01-20 11:34:37 -05003575 Float4::Float4(RValue<Float> rhs) : XYZW(this)
John Bauman89401822014-05-06 15:04:28 -04003576 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003577 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman66b8ab22014-05-06 15:57:45 -04003578 Value *vector = loadValue();
John Bauman89401822014-05-06 15:04:28 -04003579 Value *insert = Nucleus::createInsertElement(vector, rhs.value, 0);
3580
Nicolas Capense89cd582016-09-30 14:23:47 -04003581 int swizzle[4] = {0, 0, 0, 0};
3582 Value *replicate = Nucleus::createShuffleVector(insert, insert, swizzle);
John Bauman89401822014-05-06 15:04:28 -04003583
John Bauman66b8ab22014-05-06 15:57:45 -04003584 storeValue(replicate);
John Bauman89401822014-05-06 15:04:28 -04003585 }
3586
John Bauman19bac1e2014-05-06 15:23:49 -04003587 RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003588 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003589 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003590#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003591 return x86::maxps(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08003592#else
3593 return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OGT)));
3594#endif
John Bauman89401822014-05-06 15:04:28 -04003595 }
3596
John Bauman19bac1e2014-05-06 15:23:49 -04003597 RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003598 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003599 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003600#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003601 return x86::minps(x, y);
Logan Chiene3191012018-08-24 22:01:50 +08003602#else
3603 return As<Float4>(V(lowerPFMINMAX(V(x.value), V(y.value), llvm::FCmpInst::FCMP_OLT)));
3604#endif
John Bauman89401822014-05-06 15:04:28 -04003605 }
3606
Nicolas Capens05b3d662016-02-25 23:58:33 -05003607 RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2)
John Bauman89401822014-05-06 15:04:28 -04003608 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003609 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003610#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 Capens47dc8672017-04-25 12:54:39 -04003617 return x86::rcpps(x);
Logan Chiene3191012018-08-24 22:01:50 +08003618#else
3619 return As<Float4>(V(lowerRCP(V(x.value))));
3620#endif
John Bauman89401822014-05-06 15:04:28 -04003621 }
John Bauman66b8ab22014-05-06 15:57:45 -04003622
John Bauman19bac1e2014-05-06 15:23:49 -04003623 RValue<Float4> RcpSqrt_pp(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003624 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003625 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003626#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003627 return x86::rsqrtps(x);
Logan Chiene3191012018-08-24 22:01:50 +08003628#else
3629 return As<Float4>(V(lowerRSQRT(V(x.value))));
3630#endif
John Bauman89401822014-05-06 15:04:28 -04003631 }
3632
John Bauman19bac1e2014-05-06 15:23:49 -04003633 RValue<Float4> Sqrt(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003634 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003635 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003636#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003637 return x86::sqrtps(x);
Logan Chiene3191012018-08-24 22:01:50 +08003638#else
3639 return As<Float4>(V(lowerSQRT(V(x.value))));
3640#endif
John Bauman89401822014-05-06 15:04:28 -04003641 }
3642
John Bauman19bac1e2014-05-06 15:23:49 -04003643 RValue<Int> SignMask(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003644 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003645 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003646#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003647 return x86::movmskps(x);
Logan Chiene3191012018-08-24 22:01:50 +08003648#else
3649 return As<Int>(V(lowerFPSignMask(V(x.value), T(Int::getType()))));
3650#endif
John Bauman89401822014-05-06 15:04:28 -04003651 }
3652
John Bauman19bac1e2014-05-06 15:23:49 -04003653 RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003654 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003655 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003656 // return As<Int4>(x86::cmpeqps(x, y));
3657 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOEQ(x.value, y.value), Int4::getType()));
3658 }
3659
John Bauman19bac1e2014-05-06 15:23:49 -04003660 RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003661 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003662 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003663 // return As<Int4>(x86::cmpltps(x, y));
3664 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLT(x.value, y.value), Int4::getType()));
3665 }
3666
John Bauman19bac1e2014-05-06 15:23:49 -04003667 RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003668 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003669 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003670 // return As<Int4>(x86::cmpleps(x, y));
3671 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOLE(x.value, y.value), Int4::getType()));
3672 }
3673
John Bauman19bac1e2014-05-06 15:23:49 -04003674 RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003675 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003676 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003677 // return As<Int4>(x86::cmpneqps(x, y));
3678 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpONE(x.value, y.value), Int4::getType()));
3679 }
3680
John Bauman19bac1e2014-05-06 15:23:49 -04003681 RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003682 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003683 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003684 // return As<Int4>(x86::cmpnltps(x, y));
3685 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGE(x.value, y.value), Int4::getType()));
3686 }
3687
John Bauman19bac1e2014-05-06 15:23:49 -04003688 RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04003689 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003690 RR_DEBUG_INFO_UPDATE_LOC();
John Bauman89401822014-05-06 15:04:28 -04003691 // return As<Int4>(x86::cmpnleps(x, y));
3692 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpOGT(x.value, y.value), Int4::getType()));
3693 }
3694
Ben Claytonec1aeb82019-03-04 19:33:27 +00003695 RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y)
3696 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003697 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003698 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 Claytonac07ed82019-03-26 14:17:41 +00003703 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003704 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 Claytonac07ed82019-03-26 14:17:41 +00003709 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003710 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 Claytonac07ed82019-03-26 14:17:41 +00003715 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003716 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 Claytonac07ed82019-03-26 14:17:41 +00003721 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003722 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 Claytonac07ed82019-03-26 14:17:41 +00003727 RR_DEBUG_INFO_UPDATE_LOC();
Ben Claytonec1aeb82019-03-04 19:33:27 +00003728 return RValue<Int4>(Nucleus::createSExt(Nucleus::createFCmpUGT(x.value, y.value), Int4::getType()));
3729 }
3730
John Bauman19bac1e2014-05-06 15:23:49 -04003731 RValue<Float4> Round(RValue<Float4> x)
3732 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003733 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003734#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003735 if(CPUID::supportsSSE4_1())
3736 {
3737 return x86::roundps(x, 0);
3738 }
3739 else
3740 {
3741 return Float4(RoundInt(x));
3742 }
Logan Chien83fc07a2018-09-26 22:14:00 +08003743#else
3744 return RValue<Float4>(V(lowerRound(V(x.value))));
3745#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003746 }
3747
3748 RValue<Float4> Trunc(RValue<Float4> x)
3749 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003750 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003751#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003752 if(CPUID::supportsSSE4_1())
3753 {
3754 return x86::roundps(x, 3);
3755 }
3756 else
3757 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003758 return Float4(Int4(x));
John Bauman19bac1e2014-05-06 15:23:49 -04003759 }
Logan Chien8c5ca8d2018-09-27 21:05:53 +08003760#else
3761 return RValue<Float4>(V(lowerTrunc(V(x.value))));
3762#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003763 }
3764
3765 RValue<Float4> Frac(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003766 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003767 RR_DEBUG_INFO_UPDATE_LOC();
Nicolas Capensb9230422017-07-17 10:27:33 -04003768 Float4 frc;
3769
Logan Chien40a60052018-09-26 19:03:53 +08003770#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003771 if(CPUID::supportsSSE4_1())
3772 {
Nicolas Capens01a97962017-07-28 17:30:51 -04003773 frc = x - Floor(x);
John Bauman89401822014-05-06 15:04:28 -04003774 }
3775 else
3776 {
Nicolas Capensb9230422017-07-17 10:27:33 -04003777 frc = x - Float4(Int4(x)); // Signed fractional part.
John Bauman89401822014-05-06 15:04:28 -04003778
Nicolas Capensb9230422017-07-17 10:27:33 -04003779 frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1.0f))); // Add 1.0 if negative.
John Bauman89401822014-05-06 15:04:28 -04003780 }
Logan Chien3c6a1ae2018-09-26 22:18:16 +08003781#else
3782 frc = x - Floor(x);
3783#endif
Nicolas Capensb9230422017-07-17 10:27:33 -04003784
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 Bauman89401822014-05-06 15:04:28 -04003788 }
3789
John Bauman19bac1e2014-05-06 15:23:49 -04003790 RValue<Float4> Floor(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003791 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003792 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003793#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04003794 if(CPUID::supportsSSE4_1())
3795 {
3796 return x86::floorps(x);
3797 }
3798 else
3799 {
John Bauman19bac1e2014-05-06 15:23:49 -04003800 return x - Frac(x);
John Bauman89401822014-05-06 15:04:28 -04003801 }
Logan Chien40a60052018-09-26 19:03:53 +08003802#else
3803 return RValue<Float4>(V(lowerFloor(V(x.value))));
3804#endif
John Bauman89401822014-05-06 15:04:28 -04003805 }
3806
John Bauman19bac1e2014-05-06 15:23:49 -04003807 RValue<Float4> Ceil(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04003808 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003809 RR_DEBUG_INFO_UPDATE_LOC();
Logan Chiene3191012018-08-24 22:01:50 +08003810#if defined(__i386__) || defined(__x86_64__)
John Bauman19bac1e2014-05-06 15:23:49 -04003811 if(CPUID::supportsSSE4_1())
3812 {
3813 return x86::ceilps(x);
3814 }
3815 else
Logan Chiene3191012018-08-24 22:01:50 +08003816#endif
John Bauman19bac1e2014-05-06 15:23:49 -04003817 {
3818 return -Floor(-x);
3819 }
John Bauman89401822014-05-06 15:04:28 -04003820 }
3821
Ben Claytona2c8b772019-04-09 13:42:36 -04003822 RValue<Float4> Sin(RValue<Float4> v)
3823 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003824 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 Claytona2c8b772019-04-09 13:42:36 -04003826 }
3827
Ben Clayton1b6f8c72019-04-09 13:47:43 -04003828 RValue<Float4> Cos(RValue<Float4> v)
3829 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003830 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 Clayton1b6f8c72019-04-09 13:47:43 -04003832 }
3833
Ben Clayton14740062019-04-09 13:48:41 -04003834 RValue<Float4> Tan(RValue<Float4> v)
3835 {
3836 return Sin(v) / Cos(v);
3837 }
3838
Ben Claytoneafae472019-04-09 14:22:38 -04003839 static RValue<Float4> TransformFloat4PerElement(RValue<Float4> v, const char* name)
Ben Claytonf9350d72019-04-09 14:19:02 -04003840 {
Ben Claytonc38fc122019-04-11 08:58:49 -04003841 auto funcTy = ::llvm::FunctionType::get(T(Float::getType()), ::llvm::ArrayRef<llvm::Type*>(T(Float::getType())), false);
Ben Clayton6f8e5652019-06-29 01:58:02 +01003842 auto func = jit->module->getOrInsertFunction(name, funcTy);
Ben Claytonf9350d72019-04-09 14:19:02 -04003843 llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType()));
3844 for (uint64_t i = 0; i < 4; i++)
3845 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003846 auto el = jit->builder->CreateCall(func, V(Nucleus::createExtractElement(v.value, Float::getType(), i)));
Ben Claytonc38fc122019-04-11 08:58:49 -04003847 out = V(Nucleus::createInsertElement(V(out), V(el), i));
Ben Claytonf9350d72019-04-09 14:19:02 -04003848 }
3849 return RValue<Float4>(V(out));
3850 }
3851
Ben Claytoneafae472019-04-09 14:22:38 -04003852 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 Clayton749b4e02019-04-09 14:27:43 -04003862 RValue<Float4> Atan(RValue<Float4> v)
3863 {
3864 return TransformFloat4PerElement(v, "atanf");
3865 }
3866
Ben Claytond9636972019-04-09 15:09:54 -04003867 RValue<Float4> Sinh(RValue<Float4> v)
3868 {
Chris Forbes4d838a82019-09-04 19:24:02 +01003869 return Float4(0.5f) * (Exp(v) - Exp(-v));
Ben Claytond9636972019-04-09 15:09:54 -04003870 }
3871
Ben Clayton900ea2c2019-04-09 15:25:36 -04003872 RValue<Float4> Cosh(RValue<Float4> v)
3873 {
Chris Forbes4d838a82019-09-04 19:24:02 +01003874 return Float4(0.5f) * (Exp(v) + Exp(-v));
Ben Clayton900ea2c2019-04-09 15:25:36 -04003875 }
3876
Ben Clayton3928bd92019-04-09 15:27:41 -04003877 RValue<Float4> Tanh(RValue<Float4> v)
3878 {
3879 return TransformFloat4PerElement(v, "tanhf");
3880 }
3881
Ben Claytonf6d77ab2019-04-09 15:30:04 -04003882 RValue<Float4> Asinh(RValue<Float4> v)
3883 {
3884 return TransformFloat4PerElement(v, "asinhf");
3885 }
3886
Ben Clayton28ebcb02019-04-09 15:33:38 -04003887 RValue<Float4> Acosh(RValue<Float4> v)
3888 {
3889 return TransformFloat4PerElement(v, "acoshf");
3890 }
3891
Ben Claytonfa6a5392019-04-09 15:35:24 -04003892 RValue<Float4> Atanh(RValue<Float4> v)
3893 {
3894 return TransformFloat4PerElement(v, "atanhf");
3895 }
3896
Ben Claytona520c3e2019-04-09 15:43:45 -04003897 RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y)
3898 {
Ben Claytonc38fc122019-04-11 08:58:49 -04003899 ::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 Clayton6f8e5652019-06-29 01:58:02 +01003903 auto func = jit->module->getOrInsertFunction("atan2f", funcTy);
Ben Claytona520c3e2019-04-09 15:43:45 -04003904 llvm::Value *out = ::llvm::UndefValue::get(T(Float4::getType()));
3905 for (uint64_t i = 0; i < 4; i++)
3906 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003907 auto el = jit->builder->CreateCall2(func, ARGS(
Ben Claytonc38fc122019-04-11 08:58:49 -04003908 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 Claytona520c3e2019-04-09 15:43:45 -04003912 }
3913 return RValue<Float4>(V(out));
3914 }
3915
Ben Claytonbfe94f02019-04-09 15:52:12 -04003916 RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y)
3917 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003918 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 Claytonbfe94f02019-04-09 15:52:12 -04003920 }
3921
Ben Clayton242f0022019-04-09 16:00:53 -04003922 RValue<Float4> Exp(RValue<Float4> v)
3923 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003924 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 Clayton242f0022019-04-09 16:00:53 -04003926 }
3927
Ben Clayton2c1da722019-04-09 16:03:03 -04003928 RValue<Float4> Log(RValue<Float4> v)
3929 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003930 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 Clayton2c1da722019-04-09 16:03:03 -04003932 }
3933
Ben Claytonf40b56c2019-04-09 16:06:55 -04003934 RValue<Float4> Exp2(RValue<Float4> v)
3935 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003936 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 Claytonf40b56c2019-04-09 16:06:55 -04003938 }
3939
Ben Claytone17acfe2019-04-09 16:09:13 -04003940 RValue<Float4> Log2(RValue<Float4> v)
3941 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003942 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 Claytone17acfe2019-04-09 16:09:13 -04003944 }
3945
Ben Claytonff6e8c02019-08-06 21:23:44 +01003946 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 Clayton60958262019-04-10 14:53:30 -04003955 RValue<UInt4> Ctlz(RValue<UInt4> v, bool isZeroUndef)
3956 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003957 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 Clayton60958262019-04-10 14:53:30 -04003959 V(v.value),
Ben Clayton6f8e5652019-06-29 01:58:02 +01003960 isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context)
Ben Claytonc38fc122019-04-11 08:58:49 -04003961 ))));
Ben Clayton60958262019-04-10 14:53:30 -04003962 }
3963
Ben Claytonff6e8c02019-08-06 21:23:44 +01003964 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 Clayton3f007c42019-04-10 14:54:23 -04003973 RValue<UInt4> Cttz(RValue<UInt4> v, bool isZeroUndef)
3974 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01003975 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 Clayton3f007c42019-04-10 14:54:23 -04003977 V(v.value),
Ben Clayton6f8e5652019-06-29 01:58:02 +01003978 isZeroUndef ? ::llvm::ConstantInt::getTrue(jit->context) : ::llvm::ConstantInt::getFalse(jit->context)
Ben Claytonc38fc122019-04-11 08:58:49 -04003979 ))));
Ben Clayton3f007c42019-04-10 14:54:23 -04003980 }
3981
John Bauman19bac1e2014-05-06 15:23:49 -04003982 Type *Float4::getType()
John Bauman89401822014-05-06 15:04:28 -04003983 {
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04003984 return T(llvm::VectorType::get(T(Float::getType()), 4));
John Bauman89401822014-05-06 15:04:28 -04003985 }
3986
John Bauman89401822014-05-06 15:04:28 -04003987 RValue<Long> Ticks()
3988 {
Ben Claytonac07ed82019-03-26 14:17:41 +00003989 RR_DEBUG_INFO_UPDATE_LOC();
Ben Clayton6f8e5652019-06-29 01:58:02 +01003990 llvm::Function *rdtsc = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::readcyclecounter);
John Bauman89401822014-05-06 15:04:28 -04003991
Ben Clayton6f8e5652019-06-29 01:58:02 +01003992 return RValue<Long>(V(jit->builder->CreateCall(rdtsc)));
John Bauman89401822014-05-06 15:04:28 -04003993 }
Ben Claytond853c122019-04-16 17:51:49 -04003994
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 Clayton6f8e5652019-06-29 01:58:02 +01003999 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 Claytond853c122019-04-16 17:51:49 -04004001 }
4002
Ben Claytonb7eb3a82019-11-19 00:43:50 +00004003 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 Claytond853c122019-04-16 17:51:49 -04004010 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 Clayton6f8e5652019-06-29 01:58:02 +01004017 auto funcPtr = jit->builder->CreatePointerCast(V(fptr.value), funcPtrTy);
Ben Claytond853c122019-04-16 17:51:49 -04004018
4019 ::llvm::SmallVector<::llvm::Value*, 8> arguments;
4020 for (auto arg : args) { arguments.push_back(V(arg)); }
Ben Clayton6f8e5652019-06-29 01:58:02 +01004021 return V(jit->builder->CreateCall(funcPtr, arguments));
Ben Claytond853c122019-04-16 17:51:49 -04004022 }
Nicolas Capens9770a462019-06-25 10:47:10 -04004023
4024 void Breakpoint()
4025 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004026 llvm::Function *debugtrap = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::debugtrap);
Nicolas Capens9770a462019-06-25 10:47:10 -04004027
Ben Clayton6f8e5652019-06-29 01:58:02 +01004028 jit->builder->CreateCall(debugtrap);
Nicolas Capens9770a462019-06-25 10:47:10 -04004029 }
John Bauman89401822014-05-06 15:04:28 -04004030}
4031
Nicolas Capens48461502018-08-06 14:20:45 -04004032namespace rr
John Bauman89401822014-05-06 15:04:28 -04004033{
Logan Chiene3191012018-08-24 22:01:50 +08004034#if defined(__i386__) || defined(__x86_64__)
John Bauman89401822014-05-06 15:04:28 -04004035 namespace x86
4036 {
John Bauman19bac1e2014-05-06 15:23:49 -04004037 RValue<Int> cvtss2si(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04004038 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004039 llvm::Function *cvtss2si = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_cvtss2si);
John Bauman66b8ab22014-05-06 15:57:45 -04004040
John Bauman89401822014-05-06 15:04:28 -04004041 Float4 vector;
4042 vector.x = val;
4043
Ben Clayton6f8e5652019-06-29 01:58:02 +01004044 return RValue<Int>(V(jit->builder->CreateCall(cvtss2si, ARGS(V(RValue<Float4>(vector).value)))));
John Bauman89401822014-05-06 15:04:28 -04004045 }
4046
John Bauman19bac1e2014-05-06 15:23:49 -04004047 RValue<Int4> cvtps2dq(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04004048 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004049 llvm::Function *cvtps2dq = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_cvtps2dq);
John Bauman89401822014-05-06 15:04:28 -04004050
Ben Clayton6f8e5652019-06-29 01:58:02 +01004051 return RValue<Int4>(V(jit->builder->CreateCall(cvtps2dq, ARGS(V(val.value)))));
John Bauman89401822014-05-06 15:04:28 -04004052 }
4053
John Bauman19bac1e2014-05-06 15:23:49 -04004054 RValue<Float> rcpss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04004055 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004056 llvm::Function *rcpss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rcp_ss);
John Bauman89401822014-05-06 15:04:28 -04004057
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004058 Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0);
John Bauman66b8ab22014-05-06 15:57:45 -04004059
Ben Clayton6f8e5652019-06-29 01:58:02 +01004060 return RValue<Float>(Nucleus::createExtractElement(V(jit->builder->CreateCall(rcpss, ARGS(V(vector)))), Float::getType(), 0));
John Bauman89401822014-05-06 15:04:28 -04004061 }
4062
John Bauman19bac1e2014-05-06 15:23:49 -04004063 RValue<Float> sqrtss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04004064 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004065 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 Bauman89401822014-05-06 15:04:28 -04004067 }
4068
John Bauman19bac1e2014-05-06 15:23:49 -04004069 RValue<Float> rsqrtss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04004070 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004071 llvm::Function *rsqrtss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rsqrt_ss);
John Bauman66b8ab22014-05-06 15:57:45 -04004072
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004073 Value *vector = Nucleus::createInsertElement(V(llvm::UndefValue::get(T(Float4::getType()))), val.value, 0);
John Bauman89401822014-05-06 15:04:28 -04004074
Ben Clayton6f8e5652019-06-29 01:58:02 +01004075 return RValue<Float>(Nucleus::createExtractElement(V(jit->builder->CreateCall(rsqrtss, ARGS(V(vector)))), Float::getType(), 0));
John Bauman89401822014-05-06 15:04:28 -04004076 }
4077
John Bauman19bac1e2014-05-06 15:23:49 -04004078 RValue<Float4> rcpps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04004079 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004080 llvm::Function *rcpps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rcp_ps);
John Bauman66b8ab22014-05-06 15:57:45 -04004081
Ben Clayton6f8e5652019-06-29 01:58:02 +01004082 return RValue<Float4>(V(jit->builder->CreateCall(rcpps, ARGS(V(val.value)))));
John Bauman89401822014-05-06 15:04:28 -04004083 }
4084
John Bauman19bac1e2014-05-06 15:23:49 -04004085 RValue<Float4> sqrtps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04004086 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004087 llvm::Function *sqrtps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::sqrt, {V(val.value)->getType()});
John Bauman66b8ab22014-05-06 15:57:45 -04004088
Ben Clayton6f8e5652019-06-29 01:58:02 +01004089 return RValue<Float4>(V(jit->builder->CreateCall(sqrtps, ARGS(V(val.value)))));
John Bauman89401822014-05-06 15:04:28 -04004090 }
4091
John Bauman19bac1e2014-05-06 15:23:49 -04004092 RValue<Float4> rsqrtps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04004093 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004094 llvm::Function *rsqrtps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_rsqrt_ps);
John Bauman66b8ab22014-05-06 15:57:45 -04004095
Ben Clayton6f8e5652019-06-29 01:58:02 +01004096 return RValue<Float4>(V(jit->builder->CreateCall(rsqrtps, ARGS(V(val.value)))));
John Bauman89401822014-05-06 15:04:28 -04004097 }
4098
John Bauman19bac1e2014-05-06 15:23:49 -04004099 RValue<Float4> maxps(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04004100 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004101 llvm::Function *maxps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_max_ps);
John Bauman89401822014-05-06 15:04:28 -04004102
Ben Clayton6f8e5652019-06-29 01:58:02 +01004103 return RValue<Float4>(V(jit->builder->CreateCall2(maxps, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004104 }
4105
John Bauman19bac1e2014-05-06 15:23:49 -04004106 RValue<Float4> minps(RValue<Float4> x, RValue<Float4> y)
John Bauman89401822014-05-06 15:04:28 -04004107 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004108 llvm::Function *minps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_min_ps);
John Bauman89401822014-05-06 15:04:28 -04004109
Ben Clayton6f8e5652019-06-29 01:58:02 +01004110 return RValue<Float4>(V(jit->builder->CreateCall2(minps, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004111 }
4112
John Bauman19bac1e2014-05-06 15:23:49 -04004113 RValue<Float> roundss(RValue<Float> val, unsigned char imm)
John Bauman89401822014-05-06 15:04:28 -04004114 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004115 llvm::Function *roundss = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_round_ss);
John Bauman89401822014-05-06 15:04:28 -04004116
Nicolas Capensfbf2bc52017-07-26 17:26:17 -04004117 Value *undef = V(llvm::UndefValue::get(T(Float4::getType())));
John Bauman89401822014-05-06 15:04:28 -04004118 Value *vector = Nucleus::createInsertElement(undef, val.value, 0);
4119
Ben Clayton6f8e5652019-06-29 01:58:02 +01004120 return RValue<Float>(Nucleus::createExtractElement(V(jit->builder->CreateCall3(roundss, ARGS(V(undef), V(vector), V(Nucleus::createConstantInt(imm))))), Float::getType(), 0));
John Bauman89401822014-05-06 15:04:28 -04004121 }
4122
John Bauman19bac1e2014-05-06 15:23:49 -04004123 RValue<Float> floorss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04004124 {
4125 return roundss(val, 1);
4126 }
4127
John Bauman19bac1e2014-05-06 15:23:49 -04004128 RValue<Float> ceilss(RValue<Float> val)
John Bauman89401822014-05-06 15:04:28 -04004129 {
4130 return roundss(val, 2);
4131 }
4132
John Bauman19bac1e2014-05-06 15:23:49 -04004133 RValue<Float4> roundps(RValue<Float4> val, unsigned char imm)
John Bauman89401822014-05-06 15:04:28 -04004134 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004135 llvm::Function *roundps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_round_ps);
John Bauman89401822014-05-06 15:04:28 -04004136
Ben Clayton6f8e5652019-06-29 01:58:02 +01004137 return RValue<Float4>(V(jit->builder->CreateCall2(roundps, ARGS(V(val.value), V(Nucleus::createConstantInt(imm))))));
John Bauman89401822014-05-06 15:04:28 -04004138 }
4139
John Bauman19bac1e2014-05-06 15:23:49 -04004140 RValue<Float4> floorps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04004141 {
4142 return roundps(val, 1);
4143 }
4144
John Bauman19bac1e2014-05-06 15:23:49 -04004145 RValue<Float4> ceilps(RValue<Float4> val)
John Bauman89401822014-05-06 15:04:28 -04004146 {
4147 return roundps(val, 2);
4148 }
4149
Alexis Hetu0f448072016-03-18 10:56:08 -04004150 RValue<Int4> pabsd(RValue<Int4> x)
John Bauman89401822014-05-06 15:04:28 -04004151 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004152 return RValue<Int4>(V(lowerPABS(V(x.value))));
John Bauman89401822014-05-06 15:04:28 -04004153 }
4154
John Bauman19bac1e2014-05-06 15:23:49 -04004155 RValue<Short4> paddsw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004156 {
Preston Jacksonee1af662019-09-27 15:26:26 -06004157 #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 Bauman89401822014-05-06 15:04:28 -04004161
Preston Jacksonee1af662019-09-27 15:26:26 -06004162 return As<Short4>(V(jit->builder->CreateCall2(paddsw, ARGS(V(x.value), V(y.value)))));
4163 #endif
John Bauman89401822014-05-06 15:04:28 -04004164 }
John Bauman66b8ab22014-05-06 15:57:45 -04004165
John Bauman19bac1e2014-05-06 15:23:49 -04004166 RValue<Short4> psubsw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004167 {
Preston Jacksonee1af662019-09-27 15:26:26 -06004168 #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 Bauman89401822014-05-06 15:04:28 -04004172
Preston Jacksonee1af662019-09-27 15:26:26 -06004173 return As<Short4>(V(jit->builder->CreateCall2(psubsw, ARGS(V(x.value), V(y.value)))));
4174 #endif
John Bauman89401822014-05-06 15:04:28 -04004175 }
4176
John Bauman19bac1e2014-05-06 15:23:49 -04004177 RValue<UShort4> paddusw(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04004178 {
Preston Jacksonee1af662019-09-27 15:26:26 -06004179 #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 Bauman89401822014-05-06 15:04:28 -04004183
Preston Jacksonee1af662019-09-27 15:26:26 -06004184 return As<UShort4>(V(jit->builder->CreateCall2(paddusw, ARGS(V(x.value), V(y.value)))));
4185 #endif
John Bauman89401822014-05-06 15:04:28 -04004186 }
John Bauman66b8ab22014-05-06 15:57:45 -04004187
John Bauman19bac1e2014-05-06 15:23:49 -04004188 RValue<UShort4> psubusw(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04004189 {
Preston Jacksonee1af662019-09-27 15:26:26 -06004190 #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 Bauman89401822014-05-06 15:04:28 -04004194
Preston Jacksonee1af662019-09-27 15:26:26 -06004195 return As<UShort4>(V(jit->builder->CreateCall2(psubusw, ARGS(V(x.value), V(y.value)))));
4196 #endif
John Bauman89401822014-05-06 15:04:28 -04004197 }
4198
John Bauman19bac1e2014-05-06 15:23:49 -04004199 RValue<SByte8> paddsb(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04004200 {
Preston Jacksonee1af662019-09-27 15:26:26 -06004201 #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 Bauman89401822014-05-06 15:04:28 -04004205
Preston Jacksonee1af662019-09-27 15:26:26 -06004206 return As<SByte8>(V(jit->builder->CreateCall2(paddsb, ARGS(V(x.value), V(y.value)))));
4207 #endif
John Bauman89401822014-05-06 15:04:28 -04004208 }
John Bauman66b8ab22014-05-06 15:57:45 -04004209
John Bauman19bac1e2014-05-06 15:23:49 -04004210 RValue<SByte8> psubsb(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04004211 {
Preston Jacksonee1af662019-09-27 15:26:26 -06004212 #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 Bauman89401822014-05-06 15:04:28 -04004216
Preston Jacksonee1af662019-09-27 15:26:26 -06004217 return As<SByte8>(V(jit->builder->CreateCall2(psubsb, ARGS(V(x.value), V(y.value)))));
4218 #endif
John Bauman89401822014-05-06 15:04:28 -04004219 }
John Bauman66b8ab22014-05-06 15:57:45 -04004220
John Bauman19bac1e2014-05-06 15:23:49 -04004221 RValue<Byte8> paddusb(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04004222 {
Preston Jacksonee1af662019-09-27 15:26:26 -06004223 #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 Bauman89401822014-05-06 15:04:28 -04004227
Preston Jacksonee1af662019-09-27 15:26:26 -06004228 return As<Byte8>(V(jit->builder->CreateCall2(paddusb, ARGS(V(x.value), V(y.value)))));
4229 #endif
John Bauman89401822014-05-06 15:04:28 -04004230 }
John Bauman66b8ab22014-05-06 15:57:45 -04004231
John Bauman19bac1e2014-05-06 15:23:49 -04004232 RValue<Byte8> psubusb(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04004233 {
Preston Jacksonee1af662019-09-27 15:26:26 -06004234 #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 Bauman89401822014-05-06 15:04:28 -04004238
Preston Jacksonee1af662019-09-27 15:26:26 -06004239 return As<Byte8>(V(jit->builder->CreateCall2(psubusb, ARGS(V(x.value), V(y.value)))));
4240 #endif
John Bauman19bac1e2014-05-06 15:23:49 -04004241 }
4242
4243 RValue<UShort4> pavgw(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04004244 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004245 return As<UShort4>(V(lowerPAVG(V(x.value), V(y.value))));
John Bauman89401822014-05-06 15:04:28 -04004246 }
4247
John Bauman19bac1e2014-05-06 15:23:49 -04004248 RValue<Short4> pmaxsw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004249 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004250 return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT)));
John Bauman89401822014-05-06 15:04:28 -04004251 }
4252
John Bauman19bac1e2014-05-06 15:23:49 -04004253 RValue<Short4> pminsw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004254 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004255 return As<Short4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT)));
John Bauman89401822014-05-06 15:04:28 -04004256 }
4257
John Bauman19bac1e2014-05-06 15:23:49 -04004258 RValue<Short4> pcmpgtw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004259 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004260 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Short4::getType()))));
John Bauman89401822014-05-06 15:04:28 -04004261 }
4262
John Bauman19bac1e2014-05-06 15:23:49 -04004263 RValue<Short4> pcmpeqw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004264 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004265 return As<Short4>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Short4::getType()))));
John Bauman89401822014-05-06 15:04:28 -04004266 }
4267
John Bauman19bac1e2014-05-06 15:23:49 -04004268 RValue<Byte8> pcmpgtb(RValue<SByte8> x, RValue<SByte8> y)
John Bauman89401822014-05-06 15:04:28 -04004269 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004270 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_SGT, V(x.value), V(y.value), T(Byte8::getType()))));
John Bauman89401822014-05-06 15:04:28 -04004271 }
4272
John Bauman19bac1e2014-05-06 15:23:49 -04004273 RValue<Byte8> pcmpeqb(RValue<Byte8> x, RValue<Byte8> y)
John Bauman89401822014-05-06 15:04:28 -04004274 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004275 return As<Byte8>(V(lowerPCMP(llvm::ICmpInst::ICMP_EQ, V(x.value), V(y.value), T(Byte8::getType()))));
John Bauman89401822014-05-06 15:04:28 -04004276 }
4277
John Bauman19bac1e2014-05-06 15:23:49 -04004278 RValue<Short4> packssdw(RValue<Int2> x, RValue<Int2> y)
John Bauman89401822014-05-06 15:04:28 -04004279 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004280 llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packssdw_128);
John Bauman89401822014-05-06 15:04:28 -04004281
Ben Clayton6f8e5652019-06-29 01:58:02 +01004282 return As<Short4>(V(jit->builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004283 }
4284
John Bauman19bac1e2014-05-06 15:23:49 -04004285 RValue<Short8> packssdw(RValue<Int4> x, RValue<Int4> y)
John Bauman89401822014-05-06 15:04:28 -04004286 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004287 llvm::Function *packssdw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packssdw_128);
John Bauman89401822014-05-06 15:04:28 -04004288
Ben Clayton6f8e5652019-06-29 01:58:02 +01004289 return RValue<Short8>(V(jit->builder->CreateCall2(packssdw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004290 }
4291
John Bauman19bac1e2014-05-06 15:23:49 -04004292 RValue<SByte8> packsswb(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004293 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004294 llvm::Function *packsswb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packsswb_128);
John Bauman89401822014-05-06 15:04:28 -04004295
Ben Clayton6f8e5652019-06-29 01:58:02 +01004296 return As<SByte8>(V(jit->builder->CreateCall2(packsswb, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004297 }
4298
Nicolas Capens33438a62017-09-27 11:47:35 -04004299 RValue<Byte8> packuswb(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004300 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004301 llvm::Function *packuswb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_packuswb_128);
John Bauman89401822014-05-06 15:04:28 -04004302
Ben Clayton6f8e5652019-06-29 01:58:02 +01004303 return As<Byte8>(V(jit->builder->CreateCall2(packuswb, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004304 }
4305
Nicolas Capens3e7062b2017-01-17 14:01:33 -05004306 RValue<UShort8> packusdw(RValue<Int4> x, RValue<Int4> y)
John Bauman89401822014-05-06 15:04:28 -04004307 {
4308 if(CPUID::supportsSSE4_1())
4309 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004310 llvm::Function *packusdw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse41_packusdw);
John Bauman66b8ab22014-05-06 15:57:45 -04004311
Ben Clayton6f8e5652019-06-29 01:58:02 +01004312 return RValue<UShort8>(V(jit->builder->CreateCall2(packusdw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004313 }
4314 else
4315 {
Nicolas Capens3e7062b2017-01-17 14:01:33 -05004316 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 Bauman89401822014-05-06 15:04:28 -04004320 }
4321 }
4322
John Bauman19bac1e2014-05-06 15:23:49 -04004323 RValue<UShort4> psrlw(RValue<UShort4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004324 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004325 llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_w);
John Bauman89401822014-05-06 15:04:28 -04004326
Ben Clayton6f8e5652019-06-29 01:58:02 +01004327 return As<UShort4>(V(jit->builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004328 }
4329
John Bauman19bac1e2014-05-06 15:23:49 -04004330 RValue<UShort8> psrlw(RValue<UShort8> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004331 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004332 llvm::Function *psrlw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_w);
John Bauman89401822014-05-06 15:04:28 -04004333
Ben Clayton6f8e5652019-06-29 01:58:02 +01004334 return RValue<UShort8>(V(jit->builder->CreateCall2(psrlw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004335 }
4336
John Bauman19bac1e2014-05-06 15:23:49 -04004337 RValue<Short4> psraw(RValue<Short4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004338 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004339 llvm::Function *psraw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_w);
John Bauman89401822014-05-06 15:04:28 -04004340
Ben Clayton6f8e5652019-06-29 01:58:02 +01004341 return As<Short4>(V(jit->builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004342 }
4343
John Bauman19bac1e2014-05-06 15:23:49 -04004344 RValue<Short8> psraw(RValue<Short8> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004345 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004346 llvm::Function *psraw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_w);
John Bauman89401822014-05-06 15:04:28 -04004347
Ben Clayton6f8e5652019-06-29 01:58:02 +01004348 return RValue<Short8>(V(jit->builder->CreateCall2(psraw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004349 }
4350
John Bauman19bac1e2014-05-06 15:23:49 -04004351 RValue<Short4> psllw(RValue<Short4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004352 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004353 llvm::Function *psllw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_w);
John Bauman89401822014-05-06 15:04:28 -04004354
Ben Clayton6f8e5652019-06-29 01:58:02 +01004355 return As<Short4>(V(jit->builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004356 }
4357
John Bauman19bac1e2014-05-06 15:23:49 -04004358 RValue<Short8> psllw(RValue<Short8> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004359 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004360 llvm::Function *psllw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_w);
John Bauman89401822014-05-06 15:04:28 -04004361
Ben Clayton6f8e5652019-06-29 01:58:02 +01004362 return RValue<Short8>(V(jit->builder->CreateCall2(psllw, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004363 }
4364
John Bauman19bac1e2014-05-06 15:23:49 -04004365 RValue<Int2> pslld(RValue<Int2> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004366 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004367 llvm::Function *pslld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_d);
John Bauman89401822014-05-06 15:04:28 -04004368
Ben Clayton6f8e5652019-06-29 01:58:02 +01004369 return As<Int2>(V(jit->builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004370 }
4371
John Bauman19bac1e2014-05-06 15:23:49 -04004372 RValue<Int4> pslld(RValue<Int4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004373 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004374 llvm::Function *pslld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pslli_d);
John Bauman89401822014-05-06 15:04:28 -04004375
Ben Clayton6f8e5652019-06-29 01:58:02 +01004376 return RValue<Int4>(V(jit->builder->CreateCall2(pslld, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004377 }
4378
John Bauman19bac1e2014-05-06 15:23:49 -04004379 RValue<Int2> psrad(RValue<Int2> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004380 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004381 llvm::Function *psrad = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_d);
John Bauman89401822014-05-06 15:04:28 -04004382
Ben Clayton6f8e5652019-06-29 01:58:02 +01004383 return As<Int2>(V(jit->builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004384 }
4385
John Bauman19bac1e2014-05-06 15:23:49 -04004386 RValue<Int4> psrad(RValue<Int4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004387 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004388 llvm::Function *psrad = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrai_d);
John Bauman89401822014-05-06 15:04:28 -04004389
Ben Clayton6f8e5652019-06-29 01:58:02 +01004390 return RValue<Int4>(V(jit->builder->CreateCall2(psrad, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004391 }
4392
John Bauman19bac1e2014-05-06 15:23:49 -04004393 RValue<UInt2> psrld(RValue<UInt2> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004394 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004395 llvm::Function *psrld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_d);
John Bauman89401822014-05-06 15:04:28 -04004396
Ben Clayton6f8e5652019-06-29 01:58:02 +01004397 return As<UInt2>(V(jit->builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004398 }
4399
John Bauman19bac1e2014-05-06 15:23:49 -04004400 RValue<UInt4> psrld(RValue<UInt4> x, unsigned char y)
John Bauman89401822014-05-06 15:04:28 -04004401 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004402 llvm::Function *psrld = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_psrli_d);
John Bauman89401822014-05-06 15:04:28 -04004403
Ben Clayton6f8e5652019-06-29 01:58:02 +01004404 return RValue<UInt4>(V(jit->builder->CreateCall2(psrld, ARGS(V(x.value), V(Nucleus::createConstantInt(y))))));
John Bauman89401822014-05-06 15:04:28 -04004405 }
4406
John Bauman19bac1e2014-05-06 15:23:49 -04004407 RValue<Int4> pmaxsd(RValue<Int4> x, RValue<Int4> y)
4408 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004409 return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SGT)));
John Bauman19bac1e2014-05-06 15:23:49 -04004410 }
4411
4412 RValue<Int4> pminsd(RValue<Int4> x, RValue<Int4> y)
4413 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004414 return RValue<Int4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_SLT)));
John Bauman19bac1e2014-05-06 15:23:49 -04004415 }
4416
4417 RValue<UInt4> pmaxud(RValue<UInt4> x, RValue<UInt4> y)
4418 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004419 return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_UGT)));
John Bauman19bac1e2014-05-06 15:23:49 -04004420 }
4421
4422 RValue<UInt4> pminud(RValue<UInt4> x, RValue<UInt4> y)
4423 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004424 return RValue<UInt4>(V(lowerPMINMAX(V(x.value), V(y.value), llvm::ICmpInst::ICMP_ULT)));
John Bauman19bac1e2014-05-06 15:23:49 -04004425 }
4426
4427 RValue<Short4> pmulhw(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004428 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004429 llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulh_w);
John Bauman89401822014-05-06 15:04:28 -04004430
Ben Clayton6f8e5652019-06-29 01:58:02 +01004431 return As<Short4>(V(jit->builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004432 }
4433
John Bauman19bac1e2014-05-06 15:23:49 -04004434 RValue<UShort4> pmulhuw(RValue<UShort4> x, RValue<UShort4> y)
John Bauman89401822014-05-06 15:04:28 -04004435 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004436 llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulhu_w);
John Bauman89401822014-05-06 15:04:28 -04004437
Ben Clayton6f8e5652019-06-29 01:58:02 +01004438 return As<UShort4>(V(jit->builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004439 }
4440
John Bauman19bac1e2014-05-06 15:23:49 -04004441 RValue<Int2> pmaddwd(RValue<Short4> x, RValue<Short4> y)
John Bauman89401822014-05-06 15:04:28 -04004442 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004443 llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmadd_wd);
John Bauman89401822014-05-06 15:04:28 -04004444
Ben Clayton6f8e5652019-06-29 01:58:02 +01004445 return As<Int2>(V(jit->builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004446 }
4447
John Bauman19bac1e2014-05-06 15:23:49 -04004448 RValue<Short8> pmulhw(RValue<Short8> x, RValue<Short8> y)
John Bauman89401822014-05-06 15:04:28 -04004449 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004450 llvm::Function *pmulhw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulh_w);
John Bauman89401822014-05-06 15:04:28 -04004451
Ben Clayton6f8e5652019-06-29 01:58:02 +01004452 return RValue<Short8>(V(jit->builder->CreateCall2(pmulhw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004453 }
4454
John Bauman19bac1e2014-05-06 15:23:49 -04004455 RValue<UShort8> pmulhuw(RValue<UShort8> x, RValue<UShort8> y)
John Bauman89401822014-05-06 15:04:28 -04004456 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004457 llvm::Function *pmulhuw = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmulhu_w);
John Bauman89401822014-05-06 15:04:28 -04004458
Ben Clayton6f8e5652019-06-29 01:58:02 +01004459 return RValue<UShort8>(V(jit->builder->CreateCall2(pmulhuw, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004460 }
4461
John Bauman19bac1e2014-05-06 15:23:49 -04004462 RValue<Int4> pmaddwd(RValue<Short8> x, RValue<Short8> y)
John Bauman89401822014-05-06 15:04:28 -04004463 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004464 llvm::Function *pmaddwd = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmadd_wd);
John Bauman89401822014-05-06 15:04:28 -04004465
Ben Clayton6f8e5652019-06-29 01:58:02 +01004466 return RValue<Int4>(V(jit->builder->CreateCall2(pmaddwd, ARGS(V(x.value), V(y.value)))));
John Bauman89401822014-05-06 15:04:28 -04004467 }
4468
John Bauman19bac1e2014-05-06 15:23:49 -04004469 RValue<Int> movmskps(RValue<Float4> x)
John Bauman89401822014-05-06 15:04:28 -04004470 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004471 llvm::Function *movmskps = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse_movmsk_ps);
John Bauman89401822014-05-06 15:04:28 -04004472
Ben Clayton6f8e5652019-06-29 01:58:02 +01004473 return RValue<Int>(V(jit->builder->CreateCall(movmskps, ARGS(V(x.value)))));
John Bauman89401822014-05-06 15:04:28 -04004474 }
4475
John Bauman19bac1e2014-05-06 15:23:49 -04004476 RValue<Int> pmovmskb(RValue<Byte8> x)
John Bauman89401822014-05-06 15:04:28 -04004477 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004478 llvm::Function *pmovmskb = llvm::Intrinsic::getDeclaration(jit->module.get(), llvm::Intrinsic::x86_sse2_pmovmskb_128);
John Bauman89401822014-05-06 15:04:28 -04004479
Ben Clayton6f8e5652019-06-29 01:58:02 +01004480 return RValue<Int>(V(jit->builder->CreateCall(pmovmskb, ARGS(V(x.value))))) & 0xFF;
John Bauman89401822014-05-06 15:04:28 -04004481 }
4482
Nicolas Capens01a97962017-07-28 17:30:51 -04004483 RValue<Int4> pmovzxbd(RValue<Byte16> x)
John Bauman89401822014-05-06 15:04:28 -04004484 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004485 return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false)));
John Bauman89401822014-05-06 15:04:28 -04004486 }
4487
Nicolas Capens01a97962017-07-28 17:30:51 -04004488 RValue<Int4> pmovsxbd(RValue<SByte16> x)
John Bauman89401822014-05-06 15:04:28 -04004489 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004490 return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true)));
John Bauman89401822014-05-06 15:04:28 -04004491 }
4492
Nicolas Capens01a97962017-07-28 17:30:51 -04004493 RValue<Int4> pmovzxwd(RValue<UShort8> x)
John Bauman89401822014-05-06 15:04:28 -04004494 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004495 return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), false)));
John Bauman89401822014-05-06 15:04:28 -04004496 }
4497
Nicolas Capens01a97962017-07-28 17:30:51 -04004498 RValue<Int4> pmovsxwd(RValue<Short8> x)
John Bauman89401822014-05-06 15:04:28 -04004499 {
Logan Chien0eedc8c2018-08-21 09:34:28 +08004500 return RValue<Int4>(V(lowerPMOV(V(x.value), T(Int4::getType()), true)));
John Bauman89401822014-05-06 15:04:28 -04004501 }
4502 }
Logan Chiene3191012018-08-24 22:01:50 +08004503#endif // defined(__i386__) || defined(__x86_64__)
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004504
Ben Clayton60a3d6f2019-02-26 17:24:46 +00004505#ifdef ENABLE_RR_PRINT
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004506 // 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 Clayton6f8e5652019-06-29 01:58:02 +01004514 auto el = V(jit->builder->CreateExtractElement(V(vec), i));
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004515 elements.push_back(el);
4516 }
4517 return elements;
4518 }
4519
Ben Claytonca8e3d72019-05-14 16:51:05 +01004520 // 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 Clayton6f8e5652019-06-29 01:58:02 +01004524 auto intTy = ::llvm::Type::getIntNTy(jit->context, sizeof(int) * 8); // Natural integer width.
Ben Claytonca8e3d72019-05-14 16:51:05 +01004525 std::vector<Value*> elements;
4526 elements.reserve(vals.size());
4527 for (auto v : vals)
4528 {
4529 if (isSigned)
4530 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004531 elements.push_back(V(jit->builder->CreateSExt(V(v), intTy)));
Ben Claytonca8e3d72019-05-14 16:51:05 +01004532 }
4533 else
4534 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004535 elements.push_back(V(jit->builder->CreateZExt(V(v), intTy)));
Ben Claytonca8e3d72019-05-14 16:51:05 +01004536 }
4537 }
4538 return elements;
4539 }
4540
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004541 // toDouble returns all the float values in vals extended to doubles.
4542 static std::vector<Value*> toDouble(const std::vector<Value*>& vals)
4543 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004544 auto doubleTy = ::llvm::Type::getDoubleTy(jit->context);
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004545 std::vector<Value*> elements;
4546 elements.reserve(vals.size());
4547 for (auto v : vals)
4548 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004549 elements.push_back(V(jit->builder->CreateFPExt(V(v), doubleTy)));
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004550 }
4551 return elements;
4552 }
4553
Ben Claytona6833282019-05-28 17:15:02 +01004554 std::vector<Value*> PrintValue::Ty<Byte>::val(const RValue<Byte>& v) { return toInt({v.value}, false); }
Ben Claytonca8e3d72019-05-14 16:51:05 +01004555 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 Claytona6833282019-05-28 17:15:02 +01004562 std::vector<Value*> PrintValue::Ty<Short>::val(const RValue<Short>& v) { return toInt({v.value}, true); }
Ben Claytonca8e3d72019-05-14 16:51:05 +01004563 std::vector<Value*> PrintValue::Ty<Short4>::val(const RValue<Short4>& v) { return toInt(extractAll(v.value, 4), true); }
Ben Claytona6833282019-05-28 17:15:02 +01004564 std::vector<Value*> PrintValue::Ty<UShort>::val(const RValue<UShort>& v) { return toInt({v.value}, false); }
Ben Claytonca8e3d72019-05-14 16:51:05 +01004565 std::vector<Value*> PrintValue::Ty<UShort4>::val(const RValue<UShort4>& v) { return toInt(extractAll(v.value, 4), false); }
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004566 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 Clayton6f8e5652019-06-29 01:58:02 +01004568 std::vector<Value*> PrintValue::Ty<const char*>::val(const char* v) { return {V(jit->builder->CreateGlobalStringPtr(v))}; }
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004569
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 Clayton6f8e5652019-06-29 01:58:02 +01004573 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 Clayton1bc7ee92019-02-14 18:43:22 +00004576 auto funcTy = ::llvm::FunctionType::get(i32Ty, {i8PtrTy}, true);
4577
Ben Clayton6f8e5652019-06-29 01:58:02 +01004578 auto func = jit->module->getOrInsertFunction("printf", funcTy);
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004579
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 Clayton6f8e5652019-06-29 01:58:02 +01004597 vals.push_back(jit->builder->CreateGlobalStringPtr(str));
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004598
4599 // Add optional file, line and function info if provided.
4600 if (file != nullptr)
4601 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004602 vals.push_back(jit->builder->CreateGlobalStringPtr(file));
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004603 if (line > 0)
4604 {
4605 vals.push_back(::llvm::ConstantInt::get(intTy, line));
4606 }
4607 }
4608 if (function != nullptr)
4609 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004610 vals.push_back(jit->builder->CreateGlobalStringPtr(function));
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004611 }
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 Clayton6f8e5652019-06-29 01:58:02 +01004622 jit->builder->CreateCall(func, vals);
Ben Clayton1bc7ee92019-02-14 18:43:22 +00004623 }
4624#endif // ENABLE_RR_PRINT
4625
Ben Claytonac07ed82019-03-26 14:17:41 +00004626 void Nop()
4627 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004628 auto voidTy = ::llvm::Type::getVoidTy(jit->context);
Ben Claytonac07ed82019-03-26 14:17:41 +00004629 auto funcTy = ::llvm::FunctionType::get(voidTy, {}, false);
Ben Clayton6f8e5652019-06-29 01:58:02 +01004630 auto func = jit->module->getOrInsertFunction("nop", funcTy);
4631 jit->builder->CreateCall(func);
Ben Claytonac07ed82019-03-26 14:17:41 +00004632 }
4633
4634 void EmitDebugLocation()
4635 {
4636#ifdef ENABLE_RR_DEBUG_INFO
Ben Clayton6f8e5652019-06-29 01:58:02 +01004637 if (jit->debugInfo != nullptr)
Ben Claytonac07ed82019-03-26 14:17:41 +00004638 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004639 jit->debugInfo->EmitLocation();
Ben Claytonac07ed82019-03-26 14:17:41 +00004640 }
4641#endif // ENABLE_RR_DEBUG_INFO
4642 }
4643
4644 void EmitDebugVariable(Value* value)
4645 {
4646#ifdef ENABLE_RR_DEBUG_INFO
Ben Clayton6f8e5652019-06-29 01:58:02 +01004647 if (jit->debugInfo != nullptr)
Ben Claytonac07ed82019-03-26 14:17:41 +00004648 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004649 jit->debugInfo->EmitVariable(value);
Ben Claytonac07ed82019-03-26 14:17:41 +00004650 }
4651#endif // ENABLE_RR_DEBUG_INFO
4652 }
4653
4654 void FlushDebug()
4655 {
4656#ifdef ENABLE_RR_DEBUG_INFO
Ben Clayton6f8e5652019-06-29 01:58:02 +01004657 if (jit->debugInfo != nullptr)
Ben Claytonac07ed82019-03-26 14:17:41 +00004658 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004659 jit->debugInfo->Flush();
Ben Claytonac07ed82019-03-26 14:17:41 +00004660 }
4661#endif // ENABLE_RR_DEBUG_INFO
4662 }
4663
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004664} // namespace rr
4665
4666// ------------------------------ Coroutines ------------------------------
4667
4668namespace {
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004669 // 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 Clayton1c82c7b2019-04-30 12:49:27 +01004678
Ben Clayton16da2812019-07-09 23:28:51 +01004679void promoteFunctionToCoroutine()
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004680{
Ben Clayton16da2812019-07-09 23:28:51 +01004681 ASSERT(jit->coroutine.id == nullptr);
4682
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004683 // Types
Ben Clayton6f8e5652019-06-29 01:58:02 +01004684 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 Clayton16da2812019-07-09 23:28:51 +01004689 auto promiseTy = jit->coroutine.yieldType;
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004690 auto promisePtrTy = promiseTy->getPointerTo();
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004691
4692 // LLVM intrinsics
Ben Clayton6f8e5652019-06-29 01:58:02 +01004693 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 Clayton1c82c7b2019-04-30 12:49:27 +01004703
4704 auto allocFrameTy = ::llvm::FunctionType::get(i8PtrTy, {i32Ty}, false);
Ben Clayton6f8e5652019-06-29 01:58:02 +01004705 auto allocFrame = jit->module->getOrInsertFunction("coroutine_alloc_frame", allocFrameTy);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004706 auto freeFrameTy = ::llvm::FunctionType::get(voidTy, {i8PtrTy}, false);
Ben Clayton6f8e5652019-06-29 01:58:02 +01004707 auto freeFrame = jit->module->getOrInsertFunction("coroutine_free_frame", freeFrameTy);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004708
Ben Clayton16da2812019-07-09 23:28:51 +01004709 auto oldInsertionPoint = jit->builder->saveIP();
4710
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004711 // 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 Clayton1c82c7b2019-04-30 12:49:27 +01004727 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004728 auto args = jit->coroutine.await->arg_begin();
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004729 auto handle = args++;
4730 auto outPtr = args++;
Ben Clayton6f8e5652019-06-29 01:58:02 +01004731 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 Clayton1c82c7b2019-04-30 12:49:27 +01004734
Ben Clayton6f8e5652019-06-29 01:58:02 +01004735 auto done = jit->builder->CreateCall(coro_done, {handle}, "done");
4736 jit->builder->CreateCondBr(done, doneBlock, resumeBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004737
Ben Clayton6f8e5652019-06-29 01:58:02 +01004738 jit->builder->SetInsertPoint(doneBlock);
4739 jit->builder->CreateRet(::llvm::ConstantInt::getFalse(i1Ty));
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004740
Ben Clayton6f8e5652019-06-29 01:58:02 +01004741 jit->builder->SetInsertPoint(resumeBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004742 auto promiseAlignment = ::llvm::ConstantInt::get(i32Ty, 4); // TODO: Get correct alignment.
Ben Clayton6f8e5652019-06-29 01:58:02 +01004743 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 Clayton1c82c7b2019-04-30 12:49:27 +01004748 }
4749
4750 // Build the coroutine_destroy() function:
4751 //
4752 // void coroutine_destroy(CoroutineHandle* handle)
4753 // {
4754 // llvm.coro.destroy(handle);
4755 // }
4756 //
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004757 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004758 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 Clayton1c82c7b2019-04-30 12:49:27 +01004762 }
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 Clayton1c82c7b2019-04-30 12:49:27 +01004796
4797#ifdef ENABLE_RR_DEBUG_INFO
Ben Clayton16da2812019-07-09 23:28:51 +01004798 jit->debugInfo = std::unique_ptr<rr::DebugInfo>(new rr::DebugInfo(jit->builder.get(), &jit->context, jit->module.get(), jit->function));
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004799#endif // ENABLE_RR_DEBUG_INFO
4800
Ben Clayton6f8e5652019-06-29 01:58:02 +01004801 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 Clayton1c82c7b2019-04-30 12:49:27 +01004804
Ben Clayton16da2812019-07-09 23:28:51 +01004805 jit->builder->SetInsertPoint(jit->coroutine.entryBlock, jit->coroutine.entryBlock->begin());
4806 jit->coroutine.promise = jit->builder->CreateAlloca(promiseTy, nullptr, "promise");
Ben Clayton6f8e5652019-06-29 01:58:02 +01004807 jit->coroutine.id = jit->builder->CreateCall(coro_id, {
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004808 ::llvm::ConstantInt::get(i32Ty, 0),
Ben Clayton6f8e5652019-06-29 01:58:02 +01004809 jit->builder->CreatePointerCast(jit->coroutine.promise, i8PtrTy),
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004810 ::llvm::ConstantPointerNull::get(i8PtrTy),
4811 ::llvm::ConstantPointerNull::get(i8PtrTy),
4812 });
Ben Clayton6f8e5652019-06-29 01:58:02 +01004813 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 Clayton1c82c7b2019-04-30 12:49:27 +01004816
4817 // Build the suspend block
Ben Clayton6f8e5652019-06-29 01:58:02 +01004818 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 Clayton1c82c7b2019-04-30 12:49:27 +01004821
4822 // Build the end block
Ben Clayton6f8e5652019-06-29 01:58:02 +01004823 jit->builder->SetInsertPoint(jit->coroutine.endBlock);
4824 auto action = jit->builder->CreateCall(coro_suspend, {
4825 ::llvm::ConstantTokenNone::get(jit->context),
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004826 ::llvm::ConstantInt::get(i1Ty, 1), // final: true
4827 });
Ben Clayton6f8e5652019-06-29 01:58:02 +01004828 auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004829 // switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), trapBlock); // TODO: Trap attempting to resume after final suspend
Ben Clayton6f8e5652019-06-29 01:58:02 +01004830 switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004831
4832 // Build the destroy block
Ben Clayton6f8e5652019-06-29 01:58:02 +01004833 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 Clayton1c82c7b2019-04-30 12:49:27 +01004837
Ben Clayton16da2812019-07-09 23:28:51 +01004838 // Switch back to original insert point to continue building the coroutine.
4839 jit->builder->restoreIP(oldInsertionPoint);
4840}
4841
4842} // anonymous namespace
4843
4844namespace rr {
4845
4846void 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 Bauman89401822014-05-06 15:04:28 -04004866}
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004867
4868void Nucleus::yield(Value* val)
4869{
Ben Clayton16da2812019-07-09 23:28:51 +01004870 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 Clayton1c82c7b2019-04-30 12:49:27 +01004877
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 Clayton6f8e5652019-06-29 01:58:02 +01004897 auto i1Ty = ::llvm::Type::getInt1Ty(jit->context);
4898 auto i8Ty = ::llvm::Type::getInt8Ty(jit->context);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004899
4900 // Intrinsics
Ben Clayton6f8e5652019-06-29 01:58:02 +01004901 auto coro_suspend = ::llvm::Intrinsic::getDeclaration(jit->module.get(), ::llvm::Intrinsic::coro_suspend);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004902
4903 // Create a block to resume execution.
Ben Clayton6f8e5652019-06-29 01:58:02 +01004904 auto resumeBlock = llvm::BasicBlock::Create(jit->context, "resume", jit->function);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004905
4906 // Store the promise (yield value)
Ben Clayton6f8e5652019-06-29 01:58:02 +01004907 jit->builder->CreateStore(V(val), jit->coroutine.promise);
4908 auto action = jit->builder->CreateCall(coro_suspend, {
4909 ::llvm::ConstantTokenNone::get(jit->context),
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004910 ::llvm::ConstantInt::get(i1Ty, 0), // final: true
4911 });
Ben Clayton6f8e5652019-06-29 01:58:02 +01004912 auto switch_ = jit->builder->CreateSwitch(action, jit->coroutine.suspendBlock, 3);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004913 switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionResume), resumeBlock);
Ben Clayton6f8e5652019-06-29 01:58:02 +01004914 switch_->addCase(::llvm::ConstantInt::get(i8Ty, SuspendActionDestroy), jit->coroutine.destroyBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004915
4916 // Continue building in the resume block.
Ben Clayton6f8e5652019-06-29 01:58:02 +01004917 jit->builder->SetInsertPoint(resumeBlock);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004918}
4919
Ben Clayton6897e9b2019-07-16 17:27:27 +01004920std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */)
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004921{
Ben Clayton16da2812019-07-09 23:28:51 +01004922 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 Clayton1c82c7b2019-04-30 12:49:27 +01004940
4941#ifdef ENABLE_RR_DEBUG_INFO
Ben Clayton6f8e5652019-06-29 01:58:02 +01004942 if (jit->debugInfo != nullptr)
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004943 {
Ben Clayton6f8e5652019-06-29 01:58:02 +01004944 jit->debugInfo->Finalize();
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004945 }
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 Clayton6f8e5652019-06-29 01:58:02 +01004952 jit->module->print(file, 0);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004953 }
4954
Ben Clayton16da2812019-07-09 23:28:51 +01004955 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 Clayton1c82c7b2019-04-30 12:49:27 +01004974
Ben Clayton55bc37a2019-07-04 12:17:12 +01004975 auto cfg = cfgEdit.apply(jit->config);
4976 jit->optimize(cfg);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004977
4978 if(false)
4979 {
4980 std::error_code error;
4981 llvm::raw_fd_ostream file(std::string(name) + "-llvm-dump-opt.txt", error);
Ben Clayton6f8e5652019-06-29 01:58:02 +01004982 jit->module->print(file, 0);
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004983 }
4984
4985 llvm::Function *funcs[Nucleus::CoroutineEntryCount];
Ben Clayton6f8e5652019-06-29 01:58:02 +01004986 funcs[Nucleus::CoroutineEntryBegin] = jit->function;
4987 funcs[Nucleus::CoroutineEntryAwait] = jit->coroutine.await;
4988 funcs[Nucleus::CoroutineEntryDestroy] = jit->coroutine.destroy;
Ben Clayton55bc37a2019-07-04 12:17:12 +01004989 auto routine = jit->acquireRoutine(funcs, Nucleus::CoroutineEntryCount, cfg);
Ben Clayton6f8e5652019-06-29 01:58:02 +01004990 jit.reset();
Ben Clayton1c82c7b2019-04-30 12:49:27 +01004991
4992 return routine;
4993}
4994
4995} // namespace rr