Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 15 | #include "Reactor.hpp" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 16 | |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 17 | #include "Optimizer.hpp" |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 18 | #include "ExecutableMemory.hpp" |
Nicolas Capens | a062f32 | 2018-09-06 15:34:46 -0400 | [diff] [blame] | 19 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 20 | #include "src/IceTypes.h" |
| 21 | #include "src/IceCfg.h" |
| 22 | #include "src/IceELFStreamer.h" |
| 23 | #include "src/IceGlobalContext.h" |
| 24 | #include "src/IceCfgNode.h" |
| 25 | #include "src/IceELFObjectWriter.h" |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 26 | #include "src/IceGlobalInits.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 27 | |
| 28 | #include "llvm/Support/FileSystem.h" |
| 29 | #include "llvm/Support/raw_os_ostream.h" |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 30 | #include "llvm/Support/Compiler.h" |
| 31 | |
| 32 | #if __has_feature(memory_sanitizer) |
| 33 | #include <sanitizer/msan_interface.h> |
| 34 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 35 | |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 36 | #if defined(_WIN32) |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 37 | #ifndef WIN32_LEAN_AND_MEAN |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 38 | #define WIN32_LEAN_AND_MEAN |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 39 | #endif // !WIN32_LEAN_AND_MEAN |
| 40 | #ifndef NOMINMAX |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 41 | #define NOMINMAX |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 42 | #endif // !NOMINMAX |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 43 | #include <Windows.h> |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 44 | #else |
| 45 | #include <sys/mman.h> |
Nicolas Capens | 411273e | 2017-01-26 15:13:36 -0800 | [diff] [blame] | 46 | #if !defined(MAP_ANONYMOUS) |
| 47 | #define MAP_ANONYMOUS MAP_ANON |
Nicolas Capens | 8b27574 | 2017-01-20 17:11:41 -0500 | [diff] [blame] | 48 | #endif |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 49 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 50 | |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 51 | #include <mutex> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 52 | #include <limits> |
| 53 | #include <iostream> |
| 54 | #include <cassert> |
| 55 | |
| 56 | namespace |
| 57 | { |
| 58 | Ice::GlobalContext *context = nullptr; |
| 59 | Ice::Cfg *function = nullptr; |
| 60 | Ice::CfgNode *basicBlock = nullptr; |
| 61 | Ice::CfgLocalAllocatorScope *allocator = nullptr; |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 62 | rr::Routine *routine = nullptr; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 63 | |
| 64 | std::mutex codegenMutex; |
| 65 | |
| 66 | Ice::ELFFileStreamer *elfFile = nullptr; |
| 67 | Ice::Fdstream *out = nullptr; |
| 68 | } |
| 69 | |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 70 | namespace |
| 71 | { |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 72 | #if !defined(__i386__) && defined(_M_IX86) |
| 73 | #define __i386__ 1 |
| 74 | #endif |
| 75 | |
| 76 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64)) |
| 77 | #define __x86_64__ 1 |
| 78 | #endif |
| 79 | |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 80 | class CPUID |
| 81 | { |
| 82 | public: |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 83 | const static bool ARM; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 84 | const static bool SSE4_1; |
| 85 | |
| 86 | private: |
| 87 | static void cpuid(int registers[4], int info) |
| 88 | { |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 89 | #if defined(__i386__) || defined(__x86_64__) |
| 90 | #if defined(_WIN32) |
| 91 | __cpuid(registers, info); |
| 92 | #else |
| 93 | __asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info)); |
| 94 | #endif |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 95 | #else |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 96 | registers[0] = 0; |
| 97 | registers[1] = 0; |
| 98 | registers[2] = 0; |
| 99 | registers[3] = 0; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 100 | #endif |
| 101 | } |
| 102 | |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 103 | static bool detectARM() |
| 104 | { |
Stephen Lanham | fe79649 | 2018-09-07 11:59:54 -0700 | [diff] [blame] | 105 | #if defined(__arm__) || defined(__aarch64__) |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 106 | return true; |
| 107 | #elif defined(__i386__) || defined(__x86_64__) |
| 108 | return false; |
Gordana Cmiljanovic | 082dfec | 2018-10-19 11:36:15 +0200 | [diff] [blame] | 109 | #elif defined(__mips__) |
| 110 | return false; |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 111 | #else |
| 112 | #error "Unknown architecture" |
| 113 | #endif |
| 114 | } |
| 115 | |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 116 | static bool detectSSE4_1() |
| 117 | { |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 118 | #if defined(__i386__) || defined(__x86_64__) |
| 119 | int registers[4]; |
| 120 | cpuid(registers, 1); |
| 121 | return (registers[2] & 0x00080000) != 0; |
| 122 | #else |
| 123 | return false; |
| 124 | #endif |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 125 | } |
| 126 | }; |
| 127 | |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 128 | const bool CPUID::ARM = CPUID::detectARM(); |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 129 | const bool CPUID::SSE4_1 = CPUID::detectSSE4_1(); |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 130 | const bool emulateIntrinsics = false; |
Nicolas Capens | 2d8c370 | 2017-07-25 13:56:46 -0400 | [diff] [blame] | 131 | const bool emulateMismatchedBitCast = CPUID::ARM; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 134 | namespace rr |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 135 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 136 | enum EmulatedType |
| 137 | { |
| 138 | EmulatedShift = 16, |
| 139 | EmulatedV2 = 2 << EmulatedShift, |
| 140 | EmulatedV4 = 4 << EmulatedShift, |
| 141 | EmulatedV8 = 8 << EmulatedShift, |
| 142 | EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8, |
| 143 | |
| 144 | Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2, |
| 145 | Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4, |
| 146 | Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2, |
| 147 | Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8, |
| 148 | Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4, |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 149 | Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2, |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 150 | }; |
| 151 | |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 152 | class Value : public Ice::Operand {}; |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 153 | class SwitchCases : public Ice::InstSwitch {}; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 154 | class BasicBlock : public Ice::CfgNode {}; |
| 155 | |
| 156 | Ice::Type T(Type *t) |
| 157 | { |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 158 | static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!"); |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 159 | return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | Type *T(Ice::Type t) |
| 163 | { |
| 164 | return reinterpret_cast<Type*>(t); |
| 165 | } |
| 166 | |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 167 | Type *T(EmulatedType t) |
| 168 | { |
| 169 | return reinterpret_cast<Type*>(t); |
| 170 | } |
| 171 | |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 172 | Value *V(Ice::Operand *v) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 173 | { |
| 174 | return reinterpret_cast<Value*>(v); |
| 175 | } |
| 176 | |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 177 | BasicBlock *B(Ice::CfgNode *b) |
| 178 | { |
| 179 | return reinterpret_cast<BasicBlock*>(b); |
| 180 | } |
| 181 | |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 182 | static size_t typeSize(Type *type) |
| 183 | { |
| 184 | if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits) |
| 185 | { |
| 186 | switch(reinterpret_cast<std::intptr_t>(type)) |
| 187 | { |
| 188 | case Type_v2i32: return 8; |
| 189 | case Type_v4i16: return 8; |
| 190 | case Type_v2i16: return 4; |
| 191 | case Type_v8i8: return 8; |
| 192 | case Type_v4i8: return 4; |
| 193 | case Type_v2f32: return 8; |
| 194 | default: assert(false); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return Ice::typeWidthInBytes(T(type)); |
| 199 | } |
| 200 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 201 | Optimization optimization[10] = {InstructionCombining, Disabled}; |
| 202 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 203 | using ElfHeader = std::conditional<sizeof(void*) == 8, Elf64_Ehdr, Elf32_Ehdr>::type; |
| 204 | using SectionHeader = std::conditional<sizeof(void*) == 8, Elf64_Shdr, Elf32_Shdr>::type; |
| 205 | |
| 206 | inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader) |
| 207 | { |
| 208 | return reinterpret_cast<const SectionHeader*>((intptr_t)elfHeader + elfHeader->e_shoff); |
| 209 | } |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 210 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 211 | inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index) |
| 212 | { |
| 213 | return §ionHeader(elfHeader)[index]; |
| 214 | } |
| 215 | |
| 216 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable) |
| 217 | { |
| 218 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 219 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 220 | uint32_t index = relocation.getSymbol(); |
| 221 | int table = relocationTable.sh_link; |
| 222 | void *symbolValue = nullptr; |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 223 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 224 | if(index != SHN_UNDEF) |
| 225 | { |
| 226 | if(table == SHN_UNDEF) return nullptr; |
| 227 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 228 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 229 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 230 | if(index >= symtab_entries) |
| 231 | { |
| 232 | assert(index < symtab_entries && "Symbol Index out of range"); |
| 233 | return nullptr; |
| 234 | } |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 235 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 236 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
| 237 | Elf32_Sym &symbol = ((Elf32_Sym*)symbolAddress)[index]; |
| 238 | uint16_t section = symbol.st_shndx; |
| 239 | |
| 240 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
| 241 | { |
| 242 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
| 243 | symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset); |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | return nullptr; |
| 248 | } |
| 249 | } |
| 250 | |
Nicolas Capens | 8d2cf75 | 2018-11-22 11:13:45 -0500 | [diff] [blame] | 251 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
| 252 | unaligned_ptr<int32_t> patchSite = (int32_t*)(address + relocation.r_offset); |
| 253 | |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 254 | if(CPUID::ARM) |
| 255 | { |
| 256 | switch(relocation.getType()) |
| 257 | { |
| 258 | case R_ARM_NONE: |
| 259 | // No relocation |
| 260 | break; |
| 261 | case R_ARM_MOVW_ABS_NC: |
| 262 | { |
| 263 | uint32_t thumb = 0; // Calls to Thumb code not supported. |
| 264 | uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb; |
| 265 | *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 0x0FFF); |
| 266 | } |
| 267 | break; |
| 268 | case R_ARM_MOVT_ABS: |
| 269 | { |
| 270 | uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16; |
| 271 | *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 0x0FFF); |
| 272 | } |
| 273 | break; |
| 274 | default: |
| 275 | assert(false && "Unsupported relocation type"); |
| 276 | return nullptr; |
| 277 | } |
| 278 | } |
| 279 | else |
| 280 | { |
Nicolas Capens | 30cd7d4 | 2017-04-25 15:17:25 -0400 | [diff] [blame] | 281 | switch(relocation.getType()) |
| 282 | { |
| 283 | case R_386_NONE: |
| 284 | // No relocation |
| 285 | break; |
| 286 | case R_386_32: |
| 287 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite); |
| 288 | break; |
| 289 | // case R_386_PC32: |
| 290 | // *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite); |
| 291 | // break; |
| 292 | default: |
| 293 | assert(false && "Unsupported relocation type"); |
| 294 | return nullptr; |
| 295 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 298 | return symbolValue; |
| 299 | } |
| 300 | |
| 301 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable) |
| 302 | { |
| 303 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 304 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 305 | uint32_t index = relocation.getSymbol(); |
| 306 | int table = relocationTable.sh_link; |
| 307 | void *symbolValue = nullptr; |
| 308 | |
| 309 | if(index != SHN_UNDEF) |
| 310 | { |
| 311 | if(table == SHN_UNDEF) return nullptr; |
| 312 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 313 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 314 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 315 | if(index >= symtab_entries) |
| 316 | { |
| 317 | assert(index < symtab_entries && "Symbol Index out of range"); |
| 318 | return nullptr; |
| 319 | } |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 320 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 321 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
| 322 | Elf64_Sym &symbol = ((Elf64_Sym*)symbolAddress)[index]; |
| 323 | uint16_t section = symbol.st_shndx; |
| 324 | |
| 325 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
| 326 | { |
| 327 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
| 328 | symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset); |
| 329 | } |
| 330 | else |
| 331 | { |
| 332 | return nullptr; |
| 333 | } |
| 334 | } |
| 335 | |
Nicolas Capens | 8d2cf75 | 2018-11-22 11:13:45 -0500 | [diff] [blame] | 336 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
| 337 | unaligned_ptr<int32_t> patchSite32 = (int32_t*)(address + relocation.r_offset); |
| 338 | unaligned_ptr<int64_t> patchSite64 = (int64_t*)(address + relocation.r_offset); |
| 339 | |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 340 | switch(relocation.getType()) |
| 341 | { |
| 342 | case R_X86_64_NONE: |
| 343 | // No relocation |
| 344 | break; |
| 345 | case R_X86_64_64: |
Nicolas Capens | 8d2cf75 | 2018-11-22 11:13:45 -0500 | [diff] [blame] | 346 | *patchSite64 = (int64_t)((intptr_t)symbolValue + *patchSite64 + relocation.r_addend); |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 347 | break; |
| 348 | case R_X86_64_PC32: |
Nicolas Capens | 8d2cf75 | 2018-11-22 11:13:45 -0500 | [diff] [blame] | 349 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 - (intptr_t)patchSite32 + relocation.r_addend); |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 350 | break; |
| 351 | case R_X86_64_32S: |
Nicolas Capens | 8d2cf75 | 2018-11-22 11:13:45 -0500 | [diff] [blame] | 352 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 + relocation.r_addend); |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 353 | break; |
| 354 | default: |
| 355 | assert(false && "Unsupported relocation type"); |
| 356 | return nullptr; |
| 357 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 358 | |
| 359 | return symbolValue; |
| 360 | } |
| 361 | |
Nicolas Capens | 1cc4438 | 2017-04-25 10:52:16 -0400 | [diff] [blame] | 362 | void *loadImage(uint8_t *const elfImage, size_t &codeSize) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 363 | { |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 364 | ElfHeader *elfHeader = (ElfHeader*)elfImage; |
| 365 | |
| 366 | if(!elfHeader->checkMagic()) |
| 367 | { |
| 368 | return nullptr; |
| 369 | } |
| 370 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 371 | // Expect ELF bitness to match platform |
Nicolas Capens | 6504711 | 2016-11-07 13:01:07 -0500 | [diff] [blame] | 372 | assert(sizeof(void*) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32); |
Nicolas Capens | 30cd7d4 | 2017-04-25 15:17:25 -0400 | [diff] [blame] | 373 | #if defined(__i386__) |
| 374 | assert(sizeof(void*) == 4 && elfHeader->e_machine == EM_386); |
| 375 | #elif defined(__x86_64__) |
| 376 | assert(sizeof(void*) == 8 && elfHeader->e_machine == EM_X86_64); |
| 377 | #elif defined(__arm__) |
| 378 | assert(sizeof(void*) == 4 && elfHeader->e_machine == EM_ARM); |
Stephen Lanham | fe79649 | 2018-09-07 11:59:54 -0700 | [diff] [blame] | 379 | #elif defined(__aarch64__) |
| 380 | assert(sizeof(void*) == 8 && elfHeader->e_machine == EM_AARCH64); |
Gordana Cmiljanovic | 082dfec | 2018-10-19 11:36:15 +0200 | [diff] [blame] | 381 | #elif defined(__mips__) |
| 382 | assert(sizeof(void*) == 4 && elfHeader->e_machine == EM_MIPS); |
Nicolas Capens | 30cd7d4 | 2017-04-25 15:17:25 -0400 | [diff] [blame] | 383 | #else |
| 384 | #error "Unsupported platform" |
| 385 | #endif |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 386 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 387 | SectionHeader *sectionHeader = (SectionHeader*)(elfImage + elfHeader->e_shoff); |
| 388 | void *entry = nullptr; |
| 389 | |
| 390 | for(int i = 0; i < elfHeader->e_shnum; i++) |
| 391 | { |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 392 | if(sectionHeader[i].sh_type == SHT_PROGBITS) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 393 | { |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 394 | if(sectionHeader[i].sh_flags & SHF_EXECINSTR) |
| 395 | { |
| 396 | entry = elfImage + sectionHeader[i].sh_offset; |
Nicolas Capens | 1cc4438 | 2017-04-25 10:52:16 -0400 | [diff] [blame] | 397 | codeSize = sectionHeader[i].sh_size; |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | else if(sectionHeader[i].sh_type == SHT_REL) |
| 401 | { |
| 402 | assert(sizeof(void*) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code |
| 403 | |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 404 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 405 | { |
| 406 | const Elf32_Rel &relocation = ((const Elf32_Rel*)(elfImage + sectionHeader[i].sh_offset))[index]; |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 407 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | else if(sectionHeader[i].sh_type == SHT_RELA) |
| 411 | { |
| 412 | assert(sizeof(void*) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code |
| 413 | |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 414 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 415 | { |
| 416 | const Elf64_Rela &relocation = ((const Elf64_Rela*)(elfImage + sectionHeader[i].sh_offset))[index]; |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 417 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 418 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | return entry; |
| 423 | } |
| 424 | |
| 425 | template<typename T> |
| 426 | struct ExecutableAllocator |
| 427 | { |
| 428 | ExecutableAllocator() {}; |
| 429 | template<class U> ExecutableAllocator(const ExecutableAllocator<U> &other) {}; |
| 430 | |
| 431 | using value_type = T; |
| 432 | using size_type = std::size_t; |
| 433 | |
| 434 | T *allocate(size_type n) |
| 435 | { |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 436 | return (T*)allocateExecutable(sizeof(T) * n); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void deallocate(T *p, size_type n) |
| 440 | { |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 441 | deallocateExecutable(p, sizeof(T) * n); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 442 | } |
| 443 | }; |
| 444 | |
| 445 | class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine |
| 446 | { |
| 447 | ELFMemoryStreamer(const ELFMemoryStreamer &) = delete; |
| 448 | ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete; |
| 449 | |
| 450 | public: |
Nicolas Capens | 58274b5 | 2016-10-19 23:45:19 -0400 | [diff] [blame] | 451 | ELFMemoryStreamer() : Routine(), entry(nullptr) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 452 | { |
| 453 | position = 0; |
| 454 | buffer.reserve(0x1000); |
| 455 | } |
| 456 | |
Nicolas Capens | 81aa97b | 2017-06-27 17:08:08 -0400 | [diff] [blame] | 457 | ~ELFMemoryStreamer() override |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 458 | { |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 459 | #if defined(_WIN32) |
| 460 | if(buffer.size() != 0) |
| 461 | { |
| 462 | DWORD exeProtection; |
| 463 | VirtualProtect(&buffer[0], buffer.size(), oldProtection, &exeProtection); |
| 464 | } |
| 465 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void write8(uint8_t Value) override |
| 469 | { |
| 470 | if(position == (uint64_t)buffer.size()) |
| 471 | { |
| 472 | buffer.push_back(Value); |
| 473 | position++; |
| 474 | } |
| 475 | else if(position < (uint64_t)buffer.size()) |
| 476 | { |
| 477 | buffer[position] = Value; |
| 478 | position++; |
| 479 | } |
| 480 | else assert(false && "UNIMPLEMENTED"); |
| 481 | } |
| 482 | |
| 483 | void writeBytes(llvm::StringRef Bytes) override |
| 484 | { |
| 485 | std::size_t oldSize = buffer.size(); |
| 486 | buffer.resize(oldSize + Bytes.size()); |
| 487 | memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size()); |
| 488 | position += Bytes.size(); |
| 489 | } |
| 490 | |
| 491 | uint64_t tell() const override { return position; } |
| 492 | |
| 493 | void seek(uint64_t Off) override { position = Off; } |
| 494 | |
| 495 | const void *getEntry() override |
| 496 | { |
Nicolas Capens | 58274b5 | 2016-10-19 23:45:19 -0400 | [diff] [blame] | 497 | if(!entry) |
| 498 | { |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 499 | position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 500 | |
Nicolas Capens | 1cc4438 | 2017-04-25 10:52:16 -0400 | [diff] [blame] | 501 | size_t codeSize = 0; |
| 502 | entry = loadImage(&buffer[0], codeSize); |
| 503 | |
| 504 | #if defined(_WIN32) |
Nicolas Capens | e745f5a | 2017-05-29 10:00:32 -0400 | [diff] [blame] | 505 | VirtualProtect(&buffer[0], buffer.size(), PAGE_EXECUTE_READ, &oldProtection); |
Nicolas Capens | 1cc4438 | 2017-04-25 10:52:16 -0400 | [diff] [blame] | 506 | FlushInstructionCache(GetCurrentProcess(), NULL, 0); |
| 507 | #else |
Nicolas Capens | e745f5a | 2017-05-29 10:00:32 -0400 | [diff] [blame] | 508 | mprotect(&buffer[0], buffer.size(), PROT_READ | PROT_EXEC); |
Nicolas Capens | 1cc4438 | 2017-04-25 10:52:16 -0400 | [diff] [blame] | 509 | __builtin___clear_cache((char*)entry, (char*)entry + codeSize); |
| 510 | #endif |
Nicolas Capens | 58274b5 | 2016-10-19 23:45:19 -0400 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | return entry; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | private: |
Nicolas Capens | 58274b5 | 2016-10-19 23:45:19 -0400 | [diff] [blame] | 517 | void *entry; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 518 | std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer; |
| 519 | std::size_t position; |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 520 | |
| 521 | #if defined(_WIN32) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 522 | DWORD oldProtection; |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 523 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | Nucleus::Nucleus() |
| 527 | { |
| 528 | ::codegenMutex.lock(); // Reactor is currently not thread safe |
| 529 | |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 530 | Ice::ClFlags &Flags = Ice::ClFlags::Flags; |
| 531 | Ice::ClFlags::getParsedClFlags(Flags); |
| 532 | |
Nicolas Capens | 30cd7d4 | 2017-04-25 15:17:25 -0400 | [diff] [blame] | 533 | #if defined(__arm__) |
| 534 | Flags.setTargetArch(Ice::Target_ARM32); |
| 535 | Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm); |
Gordana Cmiljanovic | 082dfec | 2018-10-19 11:36:15 +0200 | [diff] [blame] | 536 | #elif defined(__mips__) |
| 537 | Flags.setTargetArch(Ice::Target_MIPS32); |
| 538 | Flags.setTargetInstructionSet(Ice::BaseInstructionSet); |
Nicolas Capens | 30cd7d4 | 2017-04-25 15:17:25 -0400 | [diff] [blame] | 539 | #else // x86 |
| 540 | Flags.setTargetArch(sizeof(void*) == 8 ? Ice::Target_X8664 : Ice::Target_X8632); |
| 541 | Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2); |
| 542 | #endif |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 543 | Flags.setOutFileType(Ice::FT_Elf); |
| 544 | Flags.setOptLevel(Ice::Opt_2); |
| 545 | Flags.setApplicationBinaryInterface(Ice::ABI_Platform); |
Nicolas Capens | 30cd7d4 | 2017-04-25 15:17:25 -0400 | [diff] [blame] | 546 | Flags.setVerbose(false ? Ice::IceV_Most : Ice::IceV_None); |
| 547 | Flags.setDisableHybridAssembly(true); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 548 | |
Nicolas Capens | 6504711 | 2016-11-07 13:01:07 -0500 | [diff] [blame] | 549 | static llvm::raw_os_ostream cout(std::cout); |
| 550 | static llvm::raw_os_ostream cerr(std::cerr); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 551 | |
| 552 | if(false) // Write out to a file |
| 553 | { |
| 554 | std::error_code errorCode; |
| 555 | ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None); |
| 556 | ::elfFile = new Ice::ELFFileStreamer(*out); |
Nicolas Capens | 6504711 | 2016-11-07 13:01:07 -0500 | [diff] [blame] | 557 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 558 | } |
| 559 | else |
| 560 | { |
| 561 | ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer(); |
Nicolas Capens | 6504711 | 2016-11-07 13:01:07 -0500 | [diff] [blame] | 562 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 563 | ::routine = elfMemory; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | Nucleus::~Nucleus() |
| 568 | { |
Nicolas Capens | 619a8c5 | 2017-07-05 14:10:46 -0400 | [diff] [blame] | 569 | delete ::routine; |
| 570 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 571 | delete ::allocator; |
| 572 | delete ::function; |
| 573 | delete ::context; |
| 574 | |
| 575 | delete ::elfFile; |
| 576 | delete ::out; |
| 577 | |
| 578 | ::codegenMutex.unlock(); |
| 579 | } |
| 580 | |
Chris Forbes | 878d4b0 | 2019-01-21 10:48:35 -0800 | [diff] [blame] | 581 | Routine *Nucleus::acquireRoutine(const char *name, bool runOptimizations) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 582 | { |
| 583 | if(basicBlock->getInsts().empty() || basicBlock->getInsts().back().getKind() != Ice::Inst::Ret) |
| 584 | { |
| 585 | createRetVoid(); |
| 586 | } |
| 587 | |
Chris Forbes | 878d4b0 | 2019-01-21 10:48:35 -0800 | [diff] [blame] | 588 | ::function->setFunctionName(Ice::GlobalString::createWithString(::context, name)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 589 | |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 590 | optimize(); |
| 591 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 592 | ::function->translate(); |
Nicolas Capens | de19f39 | 2016-10-19 10:29:49 -0400 | [diff] [blame] | 593 | assert(!::function->hasError()); |
| 594 | |
Nicolas Capens | 83a6bb9 | 2017-07-05 15:04:00 -0400 | [diff] [blame] | 595 | auto globals = ::function->getGlobalInits(); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 596 | |
| 597 | if(globals && !globals->empty()) |
| 598 | { |
Nicolas Capens | 83a6bb9 | 2017-07-05 15:04:00 -0400 | [diff] [blame] | 599 | ::context->getGlobals()->merge(globals.get()); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 600 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 601 | |
| 602 | ::context->emitFileHeader(); |
| 603 | ::function->emitIAS(); |
| 604 | auto assembler = ::function->releaseAssembler(); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 605 | auto objectWriter = ::context->getObjectWriter(); |
| 606 | assembler->alignFunction(); |
| 607 | objectWriter->writeFunctionCode(::function->getFunctionName(), false, assembler.get()); |
| 608 | ::context->lowerGlobals("last"); |
Nicolas Capens | 73dd7a2 | 2016-10-20 13:20:34 -0400 | [diff] [blame] | 609 | ::context->lowerConstants(); |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 610 | ::context->lowerJumpTables(); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 611 | objectWriter->setUndefinedSyms(::context->getConstantExternSyms()); |
| 612 | objectWriter->writeNonUserSections(); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 613 | |
Nicolas Capens | 619a8c5 | 2017-07-05 14:10:46 -0400 | [diff] [blame] | 614 | Routine *handoffRoutine = ::routine; |
| 615 | ::routine = nullptr; |
| 616 | |
| 617 | return handoffRoutine; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | void Nucleus::optimize() |
| 621 | { |
Nicolas Capens | 4846150 | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 622 | rr::optimize(::function); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | Value *Nucleus::allocateStackVariable(Type *t, int arraySize) |
| 626 | { |
| 627 | Ice::Type type = T(t); |
Nicolas Capens | a8f9863 | 2016-10-20 11:25:55 -0400 | [diff] [blame] | 628 | int typeSize = Ice::typeWidthInBytes(type); |
| 629 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
Nicolas Capens | e12780d | 2016-09-27 14:18:07 -0400 | [diff] [blame] | 630 | |
Nicolas Capens | a8f9863 | 2016-10-20 11:25:55 -0400 | [diff] [blame] | 631 | auto bytes = Ice::ConstantInteger32::create(::context, type, totalSize); |
Nicolas Capens | e12780d | 2016-09-27 14:18:07 -0400 | [diff] [blame] | 632 | auto address = ::function->makeVariable(T(getPointerType(t))); |
Nicolas Capens | a8f9863 | 2016-10-20 11:25:55 -0400 | [diff] [blame] | 633 | auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize); |
Nicolas Capens | e12780d | 2016-09-27 14:18:07 -0400 | [diff] [blame] | 634 | ::function->getEntryNode()->getInsts().push_front(alloca); |
| 635 | |
| 636 | return V(address); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | BasicBlock *Nucleus::createBasicBlock() |
| 640 | { |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 641 | return B(::function->makeNode()); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | BasicBlock *Nucleus::getInsertBlock() |
| 645 | { |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 646 | return B(::basicBlock); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 650 | { |
Nicolas Capens | 9ed1a18 | 2016-10-24 09:52:23 -0400 | [diff] [blame] | 651 | // assert(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator"); |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 652 | |
| 653 | Variable::materializeAll(); |
| 654 | |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 655 | ::basicBlock = basicBlock; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 656 | } |
| 657 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 658 | void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params) |
| 659 | { |
| 660 | uint32_t sequenceNumber = 0; |
| 661 | ::function = Ice::Cfg::create(::context, sequenceNumber).release(); |
| 662 | ::allocator = new Ice::CfgLocalAllocatorScope(::function); |
| 663 | |
| 664 | for(Type *type : Params) |
| 665 | { |
| 666 | Ice::Variable *arg = ::function->makeVariable(T(type)); |
| 667 | ::function->addArg(arg); |
| 668 | } |
| 669 | |
| 670 | Ice::CfgNode *node = ::function->makeNode(); |
| 671 | ::function->setEntryNode(node); |
| 672 | ::basicBlock = node; |
| 673 | } |
| 674 | |
| 675 | Value *Nucleus::getArgument(unsigned int index) |
| 676 | { |
| 677 | return V(::function->getArgs()[index]); |
| 678 | } |
| 679 | |
| 680 | void Nucleus::createRetVoid() |
| 681 | { |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 682 | // Code generated after this point is unreachable, so any variables |
| 683 | // being read can safely return an undefined value. We have to avoid |
| 684 | // materializing variables after the terminator ret instruction. |
| 685 | Variable::killUnmaterialized(); |
| 686 | |
Nicolas Capens | fdcca2d | 2016-10-20 11:31:36 -0400 | [diff] [blame] | 687 | Ice::InstRet *ret = Ice::InstRet::create(::function); |
| 688 | ::basicBlock->appendInst(ret); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | void Nucleus::createRet(Value *v) |
| 692 | { |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 693 | // Code generated after this point is unreachable, so any variables |
| 694 | // being read can safely return an undefined value. We have to avoid |
| 695 | // materializing variables after the terminator ret instruction. |
| 696 | Variable::killUnmaterialized(); |
| 697 | |
Nicolas Capens | fdcca2d | 2016-10-20 11:31:36 -0400 | [diff] [blame] | 698 | Ice::InstRet *ret = Ice::InstRet::create(::function, v); |
| 699 | ::basicBlock->appendInst(ret); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | void Nucleus::createBr(BasicBlock *dest) |
| 703 | { |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 704 | Variable::materializeAll(); |
| 705 | |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 706 | auto br = Ice::InstBr::create(::function, dest); |
| 707 | ::basicBlock->appendInst(br); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
| 711 | { |
Nicolas Capens | 0192d15 | 2019-03-27 14:46:07 -0400 | [diff] [blame] | 712 | Variable::materializeAll(); |
| 713 | |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 714 | auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse); |
| 715 | ::basicBlock->appendInst(br); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 716 | } |
| 717 | |
Nicolas Capens | f8360ba | 2017-01-25 11:35:00 -0800 | [diff] [blame] | 718 | static bool isCommutative(Ice::InstArithmetic::OpKind op) |
| 719 | { |
| 720 | switch(op) |
| 721 | { |
| 722 | case Ice::InstArithmetic::Add: |
| 723 | case Ice::InstArithmetic::Fadd: |
| 724 | case Ice::InstArithmetic::Mul: |
| 725 | case Ice::InstArithmetic::Fmul: |
| 726 | case Ice::InstArithmetic::And: |
| 727 | case Ice::InstArithmetic::Or: |
| 728 | case Ice::InstArithmetic::Xor: |
| 729 | return true; |
| 730 | default: |
| 731 | return false; |
| 732 | } |
| 733 | } |
| 734 | |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 735 | static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs) |
| 736 | { |
Nicolas Capens | b64e0ce | 2018-01-26 01:24:57 +0000 | [diff] [blame] | 737 | assert(lhs->getType() == rhs->getType() || llvm::isa<Ice::Constant>(rhs)); |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 738 | |
Nicolas Capens | f8360ba | 2017-01-25 11:35:00 -0800 | [diff] [blame] | 739 | bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op); |
| 740 | |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 741 | Ice::Variable *result = ::function->makeVariable(lhs->getType()); |
Nicolas Capens | f8360ba | 2017-01-25 11:35:00 -0800 | [diff] [blame] | 742 | Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs); |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 743 | ::basicBlock->appendInst(arithmetic); |
| 744 | |
| 745 | return V(result); |
| 746 | } |
| 747 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 748 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 749 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 750 | return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 754 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 755 | return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 759 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 760 | return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 764 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 765 | return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 769 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 770 | return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 774 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 775 | return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 779 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 780 | return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 784 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 785 | return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 789 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 790 | return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 794 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 795 | return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 799 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 800 | return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 804 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 805 | return createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 809 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 810 | return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 814 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 815 | return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 819 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 820 | return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 824 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 825 | return createArithmetic(Ice::InstArithmetic::And, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 829 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 830 | return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 834 | { |
Nicolas Capens | 7d9f76d | 2016-09-29 13:39:44 -0400 | [diff] [blame] | 835 | return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | Value *Nucleus::createNeg(Value *v) |
| 839 | { |
Nicolas Capens | c5c0c33 | 2016-11-08 11:37:01 -0500 | [diff] [blame] | 840 | return createSub(createNullValue(T(v->getType())), v); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | Value *Nucleus::createFNeg(Value *v) |
| 844 | { |
Nicolas Capens | c5c0c33 | 2016-11-08 11:37:01 -0500 | [diff] [blame] | 845 | double c[4] = {-0.0, -0.0, -0.0, -0.0}; |
| 846 | Value *negativeZero = Ice::isVectorType(v->getType()) ? |
| 847 | createConstantVector(c, T(v->getType())) : |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 848 | V(::context->getConstantFloat(-0.0f)); |
Nicolas Capens | c5c0c33 | 2016-11-08 11:37:01 -0500 | [diff] [blame] | 849 | |
| 850 | return createFSub(negativeZero, v); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | Value *Nucleus::createNot(Value *v) |
| 854 | { |
Nicolas Capens | c5c0c33 | 2016-11-08 11:37:01 -0500 | [diff] [blame] | 855 | if(Ice::isScalarIntegerType(v->getType())) |
| 856 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 857 | return createXor(v, V(::context->getConstantInt(v->getType(), -1))); |
Nicolas Capens | c5c0c33 | 2016-11-08 11:37:01 -0500 | [diff] [blame] | 858 | } |
| 859 | else // Vector |
| 860 | { |
Nicolas Capens | f34d1ac | 2017-05-08 17:06:11 -0400 | [diff] [blame] | 861 | int64_t c[16] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
Nicolas Capens | c5c0c33 | 2016-11-08 11:37:01 -0500 | [diff] [blame] | 862 | return createXor(v, createConstantVector(c, T(v->getType()))); |
| 863 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 866 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 867 | { |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 868 | assert(!atomic); // Unimplemented |
| 869 | assert(memoryOrder == std::memory_order_relaxed); // Unimplemented |
| 870 | |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 871 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
| 872 | Ice::Variable *result = ::function->makeVariable(T(type)); |
| 873 | |
Nicolas Capens | f4c4eca | 2017-10-03 14:26:07 -0400 | [diff] [blame] | 874 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 875 | { |
Nicolas Capens | 070d9f4 | 2017-04-26 13:36:33 -0400 | [diff] [blame] | 876 | if(emulateIntrinsics) |
| 877 | { |
| 878 | if(typeSize(type) == 4) |
| 879 | { |
| 880 | auto pointer = RValue<Pointer<Byte>>(ptr); |
Nicolas Capens | 1894cfa | 2017-07-27 14:21:46 -0400 | [diff] [blame] | 881 | Int x = *Pointer<Int>(pointer); |
Nicolas Capens | 070d9f4 | 2017-04-26 13:36:33 -0400 | [diff] [blame] | 882 | |
| 883 | Int4 vector; |
| 884 | vector = Insert(vector, x, 0); |
| 885 | |
| 886 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 887 | ::basicBlock->appendInst(bitcast); |
| 888 | } |
| 889 | else if(typeSize(type) == 8) |
| 890 | { |
| 891 | auto pointer = RValue<Pointer<Byte>>(ptr); |
Nicolas Capens | 1894cfa | 2017-07-27 14:21:46 -0400 | [diff] [blame] | 892 | Int x = *Pointer<Int>(pointer); |
Nicolas Capens | 070d9f4 | 2017-04-26 13:36:33 -0400 | [diff] [blame] | 893 | Int y = *Pointer<Int>(pointer + 4); |
| 894 | |
| 895 | Int4 vector; |
| 896 | vector = Insert(vector, x, 0); |
| 897 | vector = Insert(vector, y, 1); |
| 898 | |
| 899 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 900 | ::basicBlock->appendInst(bitcast); |
| 901 | } |
| 902 | else assert(false); |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 907 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 908 | auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 909 | load->addArg(ptr); |
| 910 | load->addArg(::context->getConstantInt32(typeSize(type))); |
| 911 | ::basicBlock->appendInst(load); |
| 912 | } |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 913 | } |
| 914 | else |
| 915 | { |
| 916 | auto load = Ice::InstLoad::create(::function, result, ptr, align); |
| 917 | ::basicBlock->appendInst(load); |
| 918 | } |
| 919 | |
| 920 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 921 | } |
| 922 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 923 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 924 | { |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 925 | assert(!atomic); // Unimplemented |
| 926 | assert(memoryOrder == std::memory_order_relaxed); // Unimplemented |
| 927 | |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 928 | #if __has_feature(memory_sanitizer) |
| 929 | // Mark all (non-stack) memory writes as initialized by calling __msan_unpoison |
| 930 | if(align != 0) |
| 931 | { |
| 932 | auto call = Ice::InstCall::create(::function, 2, nullptr, ::context->getConstantInt64(reinterpret_cast<intptr_t>(__msan_unpoison)), false); |
| 933 | call->addArg(ptr); |
| 934 | call->addArg(::context->getConstantInt64(typeSize(type))); |
| 935 | ::basicBlock->appendInst(call); |
| 936 | } |
| 937 | #endif |
| 938 | |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 939 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
| 940 | |
Nicolas Capens | f4c4eca | 2017-10-03 14:26:07 -0400 | [diff] [blame] | 941 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 942 | { |
Nicolas Capens | 070d9f4 | 2017-04-26 13:36:33 -0400 | [diff] [blame] | 943 | if(emulateIntrinsics) |
| 944 | { |
| 945 | if(typeSize(type) == 4) |
| 946 | { |
| 947 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 948 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 949 | ::basicBlock->appendInst(bitcast); |
| 950 | |
| 951 | RValue<Int4> v(V(vector)); |
| 952 | |
| 953 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 954 | Int x = Extract(v, 0); |
| 955 | *Pointer<Int>(pointer) = x; |
| 956 | } |
| 957 | else if(typeSize(type) == 8) |
| 958 | { |
| 959 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 960 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 961 | ::basicBlock->appendInst(bitcast); |
| 962 | |
| 963 | RValue<Int4> v(V(vector)); |
| 964 | |
| 965 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 966 | Int x = Extract(v, 0); |
| 967 | *Pointer<Int>(pointer) = x; |
| 968 | Int y = Extract(v, 1); |
| 969 | *Pointer<Int>(pointer + 4) = y; |
| 970 | } |
| 971 | else assert(false); |
| 972 | } |
| 973 | else |
| 974 | { |
| 975 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T}; |
| 976 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 977 | auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic); |
| 978 | store->addArg(value); |
| 979 | store->addArg(ptr); |
| 980 | store->addArg(::context->getConstantInt32(typeSize(type))); |
| 981 | ::basicBlock->appendInst(store); |
| 982 | } |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 983 | } |
| 984 | else |
| 985 | { |
Nicolas Capens | f4c4eca | 2017-10-03 14:26:07 -0400 | [diff] [blame] | 986 | assert(value->getType() == T(type)); |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 987 | |
| 988 | auto store = Ice::InstStore::create(::function, value, ptr, align); |
| 989 | ::basicBlock->appendInst(store); |
| 990 | } |
| 991 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 992 | return value; |
| 993 | } |
| 994 | |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 995 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 996 | { |
Nicolas Capens | 8820f64 | 2016-09-30 04:42:43 -0400 | [diff] [blame] | 997 | assert(index->getType() == Ice::IceType_i32); |
| 998 | |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 999 | if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index)) |
| 1000 | { |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 1001 | int32_t offset = constant->getValue() * (int)typeSize(type); |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1002 | |
| 1003 | if(offset == 0) |
| 1004 | { |
| 1005 | return ptr; |
| 1006 | } |
| 1007 | |
| 1008 | return createAdd(ptr, createConstantInt(offset)); |
| 1009 | } |
| 1010 | |
Nicolas Capens | 8820f64 | 2016-09-30 04:42:43 -0400 | [diff] [blame] | 1011 | if(!Ice::isByteSizedType(T(type))) |
| 1012 | { |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 1013 | index = createMul(index, createConstantInt((int)typeSize(type))); |
Nicolas Capens | 8820f64 | 2016-09-30 04:42:43 -0400 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | if(sizeof(void*) == 8) |
| 1017 | { |
Nicolas Capens | d294def | 2017-01-26 17:44:37 -0800 | [diff] [blame] | 1018 | if(unsignedIndex) |
| 1019 | { |
| 1020 | index = createZExt(index, T(Ice::IceType_i64)); |
| 1021 | } |
| 1022 | else |
| 1023 | { |
| 1024 | index = createSExt(index, T(Ice::IceType_i64)); |
| 1025 | } |
Nicolas Capens | 8820f64 | 2016-09-30 04:42:43 -0400 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | return createAdd(ptr, index); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value) |
| 1032 | { |
| 1033 | assert(false && "UNIMPLEMENTED"); return nullptr; |
| 1034 | } |
| 1035 | |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1036 | static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType) |
| 1037 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1038 | if(v->getType() == T(destType)) |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1039 | { |
| 1040 | return v; |
| 1041 | } |
| 1042 | |
| 1043 | Ice::Variable *result = ::function->makeVariable(T(destType)); |
| 1044 | Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v); |
| 1045 | ::basicBlock->appendInst(cast); |
| 1046 | |
| 1047 | return V(result); |
| 1048 | } |
| 1049 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1050 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
| 1051 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1052 | return createCast(Ice::InstCast::Trunc, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | Value *Nucleus::createZExt(Value *v, Type *destType) |
| 1056 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1057 | return createCast(Ice::InstCast::Zext, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | Value *Nucleus::createSExt(Value *v, Type *destType) |
| 1061 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1062 | return createCast(Ice::InstCast::Sext, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
| 1066 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1067 | return createCast(Ice::InstCast::Fptosi, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1068 | } |
| 1069 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1070 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
| 1071 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1072 | return createCast(Ice::InstCast::Sitofp, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
| 1076 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1077 | return createCast(Ice::InstCast::Fptrunc, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
| 1081 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1082 | return createCast(Ice::InstCast::Fpext, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 1086 | { |
Nicolas Capens | 2d8c370 | 2017-07-25 13:56:46 -0400 | [diff] [blame] | 1087 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1088 | // support for casting between scalars and wide vectors. For platforms where this is not supported, |
| 1089 | // emulate them by writing to the stack and reading back as the destination type. |
| 1090 | if(emulateMismatchedBitCast) |
| 1091 | { |
| 1092 | if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType))) |
| 1093 | { |
| 1094 | Value *address = allocateStackVariable(destType); |
| 1095 | createStore(v, address, T(v->getType())); |
| 1096 | return createLoad(address, destType); |
| 1097 | } |
| 1098 | else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType))) |
| 1099 | { |
| 1100 | Value *address = allocateStackVariable(T(v->getType())); |
| 1101 | createStore(v, address, T(v->getType())); |
| 1102 | return createLoad(address, destType); |
| 1103 | } |
| 1104 | } |
| 1105 | |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1106 | return createCast(Ice::InstCast::Bitcast, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1107 | } |
| 1108 | |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1109 | static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1110 | { |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 1111 | assert(lhs->getType() == rhs->getType()); |
| 1112 | |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1113 | auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType()); |
| 1114 | auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs); |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 1115 | ::basicBlock->appendInst(cmp); |
| 1116 | |
| 1117 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1118 | } |
| 1119 | |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1120 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1121 | { |
| 1122 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1123 | } |
| 1124 | |
| 1125 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1126 | { |
| 1127 | return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs); |
| 1128 | } |
| 1129 | |
| 1130 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1131 | { |
| 1132 | return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs); |
| 1133 | } |
| 1134 | |
| 1135 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1136 | { |
| 1137 | return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs); |
| 1138 | } |
| 1139 | |
| 1140 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1141 | { |
| 1142 | return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs); |
| 1143 | } |
| 1144 | |
| 1145 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1146 | { |
| 1147 | return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs); |
| 1148 | } |
| 1149 | |
| 1150 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1151 | { |
| 1152 | return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs); |
| 1153 | } |
| 1154 | |
| 1155 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1156 | { |
| 1157 | return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs); |
| 1158 | } |
| 1159 | |
| 1160 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1161 | { |
| 1162 | return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs); |
| 1163 | } |
| 1164 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1165 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1166 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1167 | return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs); |
| 1168 | } |
| 1169 | |
| 1170 | static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs) |
| 1171 | { |
| 1172 | assert(lhs->getType() == rhs->getType()); |
| 1173 | assert(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32); |
| 1174 | |
| 1175 | auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32); |
| 1176 | auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs); |
| 1177 | ::basicBlock->appendInst(cmp); |
| 1178 | |
| 1179 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1183 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1184 | return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1188 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1189 | return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1193 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1194 | return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1198 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1199 | return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1203 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1204 | return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1208 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1209 | return createFloatCompare(Ice::InstFcmp::One, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1213 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1214 | return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1218 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1219 | return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1223 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1224 | return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1228 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1229 | return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1233 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1234 | return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1238 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1239 | return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1243 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1244 | return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1248 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1249 | return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1250 | } |
| 1251 | |
Nicolas Capens | e95d534 | 2016-09-30 11:37:28 -0400 | [diff] [blame] | 1252 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1253 | { |
Nicolas Capens | 9709d4f | 2016-09-30 11:44:14 -0400 | [diff] [blame] | 1254 | auto result = ::function->makeVariable(T(type)); |
| 1255 | auto extract = Ice::InstExtractElement::create(::function, result, vector, ::context->getConstantInt32(index)); |
| 1256 | ::basicBlock->appendInst(extract); |
| 1257 | |
| 1258 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1262 | { |
Nicolas Capens | 9709d4f | 2016-09-30 11:44:14 -0400 | [diff] [blame] | 1263 | auto result = ::function->makeVariable(vector->getType()); |
| 1264 | auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index)); |
| 1265 | ::basicBlock->appendInst(insert); |
| 1266 | |
| 1267 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 1270 | Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1271 | { |
Nicolas Capens | 619c0ab | 2016-09-30 14:46:24 -0400 | [diff] [blame] | 1272 | assert(V1->getType() == V2->getType()); |
| 1273 | |
| 1274 | int size = Ice::typeNumElements(V1->getType()); |
| 1275 | auto result = ::function->makeVariable(V1->getType()); |
| 1276 | auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2); |
| 1277 | |
| 1278 | for(int i = 0; i < size; i++) |
| 1279 | { |
| 1280 | shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i]))); |
| 1281 | } |
| 1282 | |
| 1283 | ::basicBlock->appendInst(shuffle); |
| 1284 | |
| 1285 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse) |
| 1289 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 1290 | assert(ifTrue->getType() == ifFalse->getType()); |
| 1291 | |
| 1292 | auto result = ::function->makeVariable(ifTrue->getType()); |
| 1293 | auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse); |
| 1294 | ::basicBlock->appendInst(select); |
| 1295 | |
| 1296 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1297 | } |
| 1298 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1299 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1300 | { |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1301 | auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch); |
| 1302 | ::basicBlock->appendInst(switchInst); |
| 1303 | |
| 1304 | return reinterpret_cast<SwitchCases*>(switchInst); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1305 | } |
| 1306 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1307 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1308 | { |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1309 | switchCases->addBranch(label, label, branch); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | void Nucleus::createUnreachable() |
| 1313 | { |
Nicolas Capens | fdcca2d | 2016-10-20 11:31:36 -0400 | [diff] [blame] | 1314 | Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function); |
| 1315 | ::basicBlock->appendInst(unreachable); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1316 | } |
| 1317 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1318 | Type *Nucleus::getPointerType(Type *ElementType) |
| 1319 | { |
Nicolas Capens | e12780d | 2016-09-27 14:18:07 -0400 | [diff] [blame] | 1320 | if(sizeof(void*) == 8) |
| 1321 | { |
| 1322 | return T(Ice::IceType_i64); |
| 1323 | } |
| 1324 | else |
| 1325 | { |
| 1326 | return T(Ice::IceType_i32); |
| 1327 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1328 | } |
| 1329 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1330 | Value *Nucleus::createNullValue(Type *Ty) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1331 | { |
Nicolas Capens | 73dd7a2 | 2016-10-20 13:20:34 -0400 | [diff] [blame] | 1332 | if(Ice::isVectorType(T(Ty))) |
| 1333 | { |
Nicolas Capens | 30385f0 | 2017-04-18 13:03:47 -0400 | [diff] [blame] | 1334 | assert(Ice::typeNumElements(T(Ty)) <= 16); |
| 1335 | int64_t c[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
Nicolas Capens | 73dd7a2 | 2016-10-20 13:20:34 -0400 | [diff] [blame] | 1336 | return createConstantVector(c, Ty); |
| 1337 | } |
| 1338 | else |
| 1339 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1340 | return V(::context->getConstantZero(T(Ty))); |
Nicolas Capens | 73dd7a2 | 2016-10-20 13:20:34 -0400 | [diff] [blame] | 1341 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1342 | } |
| 1343 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1344 | Value *Nucleus::createConstantLong(int64_t i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1345 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1346 | return V(::context->getConstantInt64(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1347 | } |
| 1348 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1349 | Value *Nucleus::createConstantInt(int i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1350 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1351 | return V(::context->getConstantInt32(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1352 | } |
| 1353 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1354 | Value *Nucleus::createConstantInt(unsigned int i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1355 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1356 | return V(::context->getConstantInt32(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1357 | } |
| 1358 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1359 | Value *Nucleus::createConstantBool(bool b) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1360 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1361 | return V(::context->getConstantInt1(b)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1362 | } |
| 1363 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1364 | Value *Nucleus::createConstantByte(signed char i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1365 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1366 | return V(::context->getConstantInt8(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1367 | } |
| 1368 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1369 | Value *Nucleus::createConstantByte(unsigned char i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1370 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1371 | return V(::context->getConstantInt8(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1372 | } |
| 1373 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1374 | Value *Nucleus::createConstantShort(short i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1375 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1376 | return V(::context->getConstantInt16(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1377 | } |
| 1378 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1379 | Value *Nucleus::createConstantShort(unsigned short i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1380 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1381 | return V(::context->getConstantInt16(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1382 | } |
| 1383 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1384 | Value *Nucleus::createConstantFloat(float x) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1385 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1386 | return V(::context->getConstantFloat(x)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1387 | } |
| 1388 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1389 | Value *Nucleus::createNullPointer(Type *Ty) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1390 | { |
Nicolas Capens | a29d653 | 2016-12-05 21:38:09 -0500 | [diff] [blame] | 1391 | return createNullValue(T(sizeof(void*) == 8 ? Ice::IceType_i64 : Ice::IceType_i32)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1392 | } |
| 1393 | |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1394 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1395 | { |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1396 | const int vectorSize = 16; |
| 1397 | assert(Ice::typeWidthInBytes(T(type)) == vectorSize); |
| 1398 | const int alignment = vectorSize; |
| 1399 | auto globalPool = ::function->getGlobalPool(); |
| 1400 | |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1401 | const int64_t *i = constants; |
| 1402 | const double *f = reinterpret_cast<const double*>(constants); |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1403 | Ice::VariableDeclaration::DataInitializer *dataInitializer = nullptr; |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1404 | |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1405 | switch((int)reinterpret_cast<intptr_t>(type)) |
| 1406 | { |
| 1407 | case Ice::IceType_v4i32: |
Nicolas Capens | a4c30b0 | 2016-11-08 15:43:17 -0500 | [diff] [blame] | 1408 | case Ice::IceType_v4i1: |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1409 | { |
| 1410 | const int initializer[4] = {(int)i[0], (int)i[1], (int)i[2], (int)i[3]}; |
| 1411 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1412 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1413 | } |
| 1414 | break; |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1415 | case Ice::IceType_v4f32: |
| 1416 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1417 | const float initializer[4] = {(float)f[0], (float)f[1], (float)f[2], (float)f[3]}; |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1418 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1419 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1420 | } |
| 1421 | break; |
| 1422 | case Ice::IceType_v8i16: |
Nicolas Capens | a4c30b0 | 2016-11-08 15:43:17 -0500 | [diff] [blame] | 1423 | case Ice::IceType_v8i1: |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1424 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1425 | const short initializer[8] = {(short)i[0], (short)i[1], (short)i[2], (short)i[3], (short)i[4], (short)i[5], (short)i[6], (short)i[7]}; |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1426 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1427 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1428 | } |
| 1429 | break; |
| 1430 | case Ice::IceType_v16i8: |
Nicolas Capens | a4c30b0 | 2016-11-08 15:43:17 -0500 | [diff] [blame] | 1431 | case Ice::IceType_v16i1: |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1432 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1433 | const char initializer[16] = {(char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7], (char)i[8], (char)i[9], (char)i[10], (char)i[11], (char)i[12], (char)i[13], (char)i[14], (char)i[15]}; |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1434 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1435 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1436 | } |
| 1437 | break; |
| 1438 | case Type_v2i32: |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1439 | { |
| 1440 | const int initializer[4] = {(int)i[0], (int)i[1], (int)i[0], (int)i[1]}; |
| 1441 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1442 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1443 | } |
| 1444 | break; |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1445 | case Type_v2f32: |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1446 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1447 | const float initializer[4] = {(float)f[0], (float)f[1], (float)f[0], (float)f[1]}; |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1448 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1449 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1450 | } |
| 1451 | break; |
| 1452 | case Type_v4i16: |
| 1453 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1454 | const short initializer[8] = {(short)i[0], (short)i[1], (short)i[2], (short)i[3], (short)i[0], (short)i[1], (short)i[2], (short)i[3]}; |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1455 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1456 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1457 | } |
| 1458 | break; |
| 1459 | case Type_v8i8: |
| 1460 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1461 | const char initializer[16] = {(char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7]}; |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1462 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1463 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1464 | } |
| 1465 | break; |
| 1466 | case Type_v4i8: |
| 1467 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1468 | const char initializer[16] = {(char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3]}; |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1469 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1470 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1471 | } |
| 1472 | break; |
| 1473 | default: |
| 1474 | assert(false && "Unknown constant vector type" && type); |
| 1475 | } |
| 1476 | |
| 1477 | auto name = Ice::GlobalString::createWithoutString(::context); |
| 1478 | auto *variableDeclaration = Ice::VariableDeclaration::create(globalPool); |
| 1479 | variableDeclaration->setName(name); |
| 1480 | variableDeclaration->setAlignment(alignment); |
| 1481 | variableDeclaration->setIsConstant(true); |
| 1482 | variableDeclaration->addInitializer(dataInitializer); |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 1483 | |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1484 | ::function->addGlobal(variableDeclaration); |
| 1485 | |
| 1486 | constexpr int32_t offset = 0; |
| 1487 | Ice::Operand *ptr = ::context->getConstantSym(offset, name); |
| 1488 | |
| 1489 | Ice::Variable *result = ::function->makeVariable(T(type)); |
| 1490 | auto load = Ice::InstLoad::create(::function, result, ptr, alignment); |
| 1491 | ::basicBlock->appendInst(load); |
| 1492 | |
| 1493 | return V(result); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1497 | { |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1498 | return createConstantVector((const int64_t*)constants, type); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | Type *Void::getType() |
| 1502 | { |
| 1503 | return T(Ice::IceType_void); |
| 1504 | } |
| 1505 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1506 | Type *Bool::getType() |
| 1507 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1508 | return T(Ice::IceType_i1); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1509 | } |
| 1510 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1511 | Type *Byte::getType() |
| 1512 | { |
Nicolas Capens | 6d73871 | 2016-09-30 04:15:22 -0400 | [diff] [blame] | 1513 | return T(Ice::IceType_i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1516 | Type *SByte::getType() |
| 1517 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1518 | return T(Ice::IceType_i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1519 | } |
| 1520 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1521 | Type *Short::getType() |
| 1522 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1523 | return T(Ice::IceType_i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1524 | } |
| 1525 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1526 | Type *UShort::getType() |
| 1527 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1528 | return T(Ice::IceType_i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | Type *Byte4::getType() |
| 1532 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1533 | return T(Type_v4i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | Type *SByte4::getType() |
| 1537 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1538 | return T(Type_v4i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1539 | } |
| 1540 | |
Nicolas Capens | b6d4ce3 | 2019-03-12 23:00:24 -0400 | [diff] [blame] | 1541 | namespace |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1542 | { |
Nicolas Capens | b6d4ce3 | 2019-03-12 23:00:24 -0400 | [diff] [blame] | 1543 | RValue<Byte> SaturateUnsigned(RValue<Short> x) |
| 1544 | { |
| 1545 | return Byte(IfThenElse(Int(x) > 0xFF, Int(0xFF), IfThenElse(Int(x) < 0, Int(0), Int(x)))); |
| 1546 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1547 | |
Nicolas Capens | b6d4ce3 | 2019-03-12 23:00:24 -0400 | [diff] [blame] | 1548 | RValue<Byte> Extract(RValue<Byte8> val, int i) |
| 1549 | { |
| 1550 | return RValue<Byte>(Nucleus::createExtractElement(val.value, Byte::getType(), i)); |
| 1551 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1552 | |
Nicolas Capens | b6d4ce3 | 2019-03-12 23:00:24 -0400 | [diff] [blame] | 1553 | RValue<Byte8> Insert(RValue<Byte8> val, RValue<Byte> element, int i) |
| 1554 | { |
| 1555 | return RValue<Byte8>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 1556 | } |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1557 | } |
| 1558 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1559 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 1560 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1561 | if(emulateIntrinsics) |
| 1562 | { |
| 1563 | Byte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1564 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 1565 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 1566 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 1567 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 1568 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 1569 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 1570 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 1571 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1572 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1573 | return result; |
| 1574 | } |
| 1575 | else |
| 1576 | { |
| 1577 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 1578 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1579 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1580 | auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1581 | paddusb->addArg(x.value); |
| 1582 | paddusb->addArg(y.value); |
| 1583 | ::basicBlock->appendInst(paddusb); |
| 1584 | |
| 1585 | return RValue<Byte8>(V(result)); |
| 1586 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 1590 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1591 | if(emulateIntrinsics) |
| 1592 | { |
| 1593 | Byte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1594 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 1595 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 1596 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 1597 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 1598 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 1599 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 1600 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 1601 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1602 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1603 | return result; |
| 1604 | } |
| 1605 | else |
| 1606 | { |
| 1607 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 1608 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1609 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1610 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1611 | psubusw->addArg(x.value); |
| 1612 | psubusw->addArg(y.value); |
| 1613 | ::basicBlock->appendInst(psubusw); |
| 1614 | |
| 1615 | return RValue<Byte8>(V(result)); |
| 1616 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1617 | } |
| 1618 | |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1619 | RValue<SByte> Extract(RValue<SByte8> val, int i) |
| 1620 | { |
| 1621 | return RValue<SByte>(Nucleus::createExtractElement(val.value, SByte::getType(), i)); |
| 1622 | } |
| 1623 | |
| 1624 | RValue<SByte8> Insert(RValue<SByte8> val, RValue<SByte> element, int i) |
| 1625 | { |
| 1626 | return RValue<SByte8>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 1627 | } |
| 1628 | |
| 1629 | RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 1630 | { |
| 1631 | if(emulateIntrinsics) |
| 1632 | { |
| 1633 | SByte8 result; |
| 1634 | result = Insert(result, Extract(lhs, 0) >> SByte(rhs), 0); |
| 1635 | result = Insert(result, Extract(lhs, 1) >> SByte(rhs), 1); |
| 1636 | result = Insert(result, Extract(lhs, 2) >> SByte(rhs), 2); |
| 1637 | result = Insert(result, Extract(lhs, 3) >> SByte(rhs), 3); |
| 1638 | result = Insert(result, Extract(lhs, 4) >> SByte(rhs), 4); |
| 1639 | result = Insert(result, Extract(lhs, 5) >> SByte(rhs), 5); |
| 1640 | result = Insert(result, Extract(lhs, 6) >> SByte(rhs), 6); |
| 1641 | result = Insert(result, Extract(lhs, 7) >> SByte(rhs), 7); |
| 1642 | |
| 1643 | return result; |
| 1644 | } |
| 1645 | else |
| 1646 | { |
| 1647 | #if defined(__i386__) || defined(__x86_64__) |
| 1648 | // SSE2 doesn't support byte vector shifts, so shift as shorts and recombine. |
Alexis Hetu | e18c530 | 2017-08-04 11:48:17 -0400 | [diff] [blame] | 1649 | RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u); |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1650 | RValue<Short4> lo = As<Short4>(As<UShort4>((As<Short4>(lhs) << 8) >> rhs) >> 8); |
| 1651 | |
| 1652 | return As<SByte8>(hi | lo); |
| 1653 | #else |
| 1654 | return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 1655 | #endif |
| 1656 | } |
| 1657 | } |
| 1658 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1659 | RValue<Int> SignMask(RValue<Byte8> x) |
| 1660 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 1661 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1662 | { |
| 1663 | Byte8 xx = As<Byte8>(As<SByte8>(x) >> 7) & Byte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 1664 | return Int(Extract(xx, 0)) | Int(Extract(xx, 1)) | Int(Extract(xx, 2)) | Int(Extract(xx, 3)) | Int(Extract(xx, 4)) | Int(Extract(xx, 5)) | Int(Extract(xx, 6)) | Int(Extract(xx, 7)); |
| 1665 | } |
| 1666 | else |
| 1667 | { |
| 1668 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 1669 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1670 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1671 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 1672 | movmsk->addArg(x.value); |
| 1673 | ::basicBlock->appendInst(movmsk); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1674 | |
Nicolas Capens | 0f70a7f | 2017-07-26 13:50:04 -0400 | [diff] [blame] | 1675 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1676 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
| 1680 | // { |
Nicolas Capens | 2f970b6 | 2016-11-08 14:28:59 -0500 | [diff] [blame] | 1681 | // return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1682 | // } |
| 1683 | |
| 1684 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 1685 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 1686 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | Type *Byte8::getType() |
| 1690 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1691 | return T(Type_v8i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1692 | } |
| 1693 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1694 | // RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs) |
| 1695 | // { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1696 | // return RValue<SByte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1697 | // } |
| 1698 | |
| 1699 | // RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 1700 | // { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1701 | // return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1702 | // } |
| 1703 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1704 | RValue<SByte> SaturateSigned(RValue<Short> x) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1705 | { |
| 1706 | return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x)))); |
| 1707 | } |
| 1708 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1709 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 1710 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1711 | if(emulateIntrinsics) |
| 1712 | { |
| 1713 | SByte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1714 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 1715 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 1716 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 1717 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 1718 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 1719 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 1720 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 1721 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1722 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1723 | return result; |
| 1724 | } |
| 1725 | else |
| 1726 | { |
| 1727 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 1728 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1729 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1730 | auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1731 | paddsb->addArg(x.value); |
| 1732 | paddsb->addArg(y.value); |
| 1733 | ::basicBlock->appendInst(paddsb); |
| 1734 | |
| 1735 | return RValue<SByte8>(V(result)); |
| 1736 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 1740 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1741 | if(emulateIntrinsics) |
| 1742 | { |
| 1743 | SByte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1744 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 1745 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 1746 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 1747 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 1748 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 1749 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 1750 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 1751 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1752 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1753 | return result; |
| 1754 | } |
| 1755 | else |
| 1756 | { |
| 1757 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 1758 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1759 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1760 | auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1761 | psubsb->addArg(x.value); |
| 1762 | psubsb->addArg(y.value); |
| 1763 | ::basicBlock->appendInst(psubsb); |
| 1764 | |
| 1765 | return RValue<SByte8>(V(result)); |
| 1766 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1767 | } |
| 1768 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1769 | RValue<Int> SignMask(RValue<SByte8> x) |
| 1770 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 1771 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1772 | { |
| 1773 | SByte8 xx = (x >> 7) & SByte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 1774 | return Int(Extract(xx, 0)) | Int(Extract(xx, 1)) | Int(Extract(xx, 2)) | Int(Extract(xx, 3)) | Int(Extract(xx, 4)) | Int(Extract(xx, 5)) | Int(Extract(xx, 6)) | Int(Extract(xx, 7)); |
| 1775 | } |
| 1776 | else |
| 1777 | { |
| 1778 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 1779 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1780 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1781 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 1782 | movmsk->addArg(x.value); |
| 1783 | ::basicBlock->appendInst(movmsk); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 1784 | |
Nicolas Capens | 0f70a7f | 2017-07-26 13:50:04 -0400 | [diff] [blame] | 1785 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1786 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
| 1790 | { |
Nicolas Capens | 2f970b6 | 2016-11-08 14:28:59 -0500 | [diff] [blame] | 1791 | return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 1795 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 1796 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | Type *SByte8::getType() |
| 1800 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1801 | return T(Type_v8i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1802 | } |
| 1803 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1804 | Type *Byte16::getType() |
| 1805 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1806 | return T(Ice::IceType_v16i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | Type *SByte16::getType() |
| 1810 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1811 | return T(Ice::IceType_v16i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1812 | } |
| 1813 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1814 | Type *Short2::getType() |
| 1815 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1816 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1817 | } |
| 1818 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1819 | Type *UShort2::getType() |
| 1820 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1821 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1822 | } |
| 1823 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1824 | Short4::Short4(RValue<Int4> cast) |
| 1825 | { |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 1826 | int select[8] = {0, 2, 4, 6, 0, 2, 4, 6}; |
| 1827 | Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType()); |
| 1828 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 1829 | |
Nicolas Capens | bea4dce | 2017-07-24 16:54:44 -0400 | [diff] [blame] | 1830 | Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value; |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 1831 | Value *short4 = Nucleus::createBitCast(int2, Short4::getType()); |
| 1832 | |
| 1833 | storeValue(short4); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | // Short4::Short4(RValue<Float> cast) |
| 1837 | // { |
| 1838 | // } |
| 1839 | |
| 1840 | Short4::Short4(RValue<Float4> cast) |
| 1841 | { |
| 1842 | assert(false && "UNIMPLEMENTED"); |
| 1843 | } |
| 1844 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1845 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 1846 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1847 | if(emulateIntrinsics) |
| 1848 | { |
| 1849 | Short4 result; |
| 1850 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 1851 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 1852 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 1853 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 1854 | |
| 1855 | return result; |
| 1856 | } |
| 1857 | else |
| 1858 | { |
| 1859 | return RValue<Short4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 1860 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 1864 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1865 | if(emulateIntrinsics) |
| 1866 | { |
| 1867 | Short4 result; |
| 1868 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 1869 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 1870 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 1871 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 1872 | |
| 1873 | return result; |
| 1874 | } |
| 1875 | else |
| 1876 | { |
| 1877 | return RValue<Short4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 1878 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1879 | } |
| 1880 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1881 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 1882 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 1883 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
| 1884 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value); |
| 1885 | ::basicBlock->appendInst(cmp); |
| 1886 | |
| 1887 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1888 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 1889 | ::basicBlock->appendInst(select); |
| 1890 | |
| 1891 | return RValue<Short4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 1895 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 1896 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
| 1897 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value); |
| 1898 | ::basicBlock->appendInst(cmp); |
| 1899 | |
| 1900 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1901 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 1902 | ::basicBlock->appendInst(select); |
| 1903 | |
| 1904 | return RValue<Short4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1905 | } |
| 1906 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1907 | RValue<Short> SaturateSigned(RValue<Int> x) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1908 | { |
| 1909 | return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x))); |
| 1910 | } |
| 1911 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1912 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 1913 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1914 | if(emulateIntrinsics) |
| 1915 | { |
| 1916 | Short4 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1917 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 1918 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 1919 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 1920 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1921 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1922 | return result; |
| 1923 | } |
| 1924 | else |
| 1925 | { |
| 1926 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1927 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1928 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1929 | auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1930 | paddsw->addArg(x.value); |
| 1931 | paddsw->addArg(y.value); |
| 1932 | ::basicBlock->appendInst(paddsw); |
| 1933 | |
| 1934 | return RValue<Short4>(V(result)); |
| 1935 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 1939 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1940 | if(emulateIntrinsics) |
| 1941 | { |
| 1942 | Short4 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1943 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 1944 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 1945 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 1946 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1947 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1948 | return result; |
| 1949 | } |
| 1950 | else |
| 1951 | { |
| 1952 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1953 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1954 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1955 | auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1956 | psubsw->addArg(x.value); |
| 1957 | psubsw->addArg(y.value); |
| 1958 | ::basicBlock->appendInst(psubsw); |
| 1959 | |
| 1960 | return RValue<Short4>(V(result)); |
| 1961 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 1965 | { |
Nicolas Capens | 6c15744 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1966 | if(emulateIntrinsics) |
| 1967 | { |
| 1968 | Short4 result; |
| 1969 | result = Insert(result, Short((Int(Extract(x, 0)) * Int(Extract(y, 0))) >> 16), 0); |
| 1970 | result = Insert(result, Short((Int(Extract(x, 1)) * Int(Extract(y, 1))) >> 16), 1); |
| 1971 | result = Insert(result, Short((Int(Extract(x, 2)) * Int(Extract(y, 2))) >> 16), 2); |
| 1972 | result = Insert(result, Short((Int(Extract(x, 3)) * Int(Extract(y, 3))) >> 16), 3); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1973 | |
Nicolas Capens | 6c15744 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1974 | return result; |
| 1975 | } |
| 1976 | else |
| 1977 | { |
| 1978 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1979 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1980 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1981 | auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1982 | pmulhw->addArg(x.value); |
| 1983 | pmulhw->addArg(y.value); |
| 1984 | ::basicBlock->appendInst(pmulhw); |
| 1985 | |
| 1986 | return RValue<Short4>(V(result)); |
| 1987 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 1991 | { |
Nicolas Capens | afe27e9 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1992 | if(emulateIntrinsics) |
| 1993 | { |
| 1994 | Int2 result; |
| 1995 | result = Insert(result, Int(Extract(x, 0)) * Int(Extract(y, 0)) + Int(Extract(x, 1)) * Int(Extract(y, 1)), 0); |
| 1996 | result = Insert(result, Int(Extract(x, 2)) * Int(Extract(y, 2)) + Int(Extract(x, 3)) * Int(Extract(y, 3)), 1); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1997 | |
Nicolas Capens | afe27e9 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1998 | return result; |
| 1999 | } |
| 2000 | else |
| 2001 | { |
| 2002 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2003 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2004 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2005 | auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2006 | pmaddwd->addArg(x.value); |
| 2007 | pmaddwd->addArg(y.value); |
| 2008 | ::basicBlock->appendInst(pmaddwd); |
| 2009 | |
| 2010 | return As<Int2>(V(result)); |
| 2011 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2012 | } |
| 2013 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2014 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2015 | { |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2016 | if(emulateIntrinsics) |
| 2017 | { |
| 2018 | SByte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2019 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2020 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2021 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2022 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2023 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2024 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2025 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2026 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 2027 | |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2028 | return result; |
| 2029 | } |
| 2030 | else |
| 2031 | { |
| 2032 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 2033 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2034 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2035 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2036 | pack->addArg(x.value); |
| 2037 | pack->addArg(y.value); |
| 2038 | ::basicBlock->appendInst(pack); |
| 2039 | |
| 2040 | return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x88)); |
| 2041 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2042 | } |
| 2043 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2044 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2045 | { |
| 2046 | if(emulateIntrinsics) |
| 2047 | { |
| 2048 | Byte8 result; |
| 2049 | result = Insert(result, SaturateUnsigned(Extract(x, 0)), 0); |
| 2050 | result = Insert(result, SaturateUnsigned(Extract(x, 1)), 1); |
| 2051 | result = Insert(result, SaturateUnsigned(Extract(x, 2)), 2); |
| 2052 | result = Insert(result, SaturateUnsigned(Extract(x, 3)), 3); |
| 2053 | result = Insert(result, SaturateUnsigned(Extract(y, 0)), 4); |
| 2054 | result = Insert(result, SaturateUnsigned(Extract(y, 1)), 5); |
| 2055 | result = Insert(result, SaturateUnsigned(Extract(y, 2)), 6); |
| 2056 | result = Insert(result, SaturateUnsigned(Extract(y, 3)), 7); |
| 2057 | |
| 2058 | return result; |
| 2059 | } |
| 2060 | else |
| 2061 | { |
| 2062 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 2063 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2064 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2065 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2066 | pack->addArg(x.value); |
| 2067 | pack->addArg(y.value); |
| 2068 | ::basicBlock->appendInst(pack); |
| 2069 | |
| 2070 | return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x88)); |
| 2071 | } |
| 2072 | } |
| 2073 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2074 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
| 2075 | { |
Nicolas Capens | 2f970b6 | 2016-11-08 14:28:59 -0500 | [diff] [blame] | 2076 | return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 2080 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2081 | return RValue<Short4>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | Type *Short4::getType() |
| 2085 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 2086 | return T(Type_v4i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2087 | } |
| 2088 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2089 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2090 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2091 | if(saturate) |
| 2092 | { |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 2093 | if(CPUID::SSE4_1) |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2094 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2095 | // x86 produces 0x80000000 on 32-bit integer overflow/underflow. |
| 2096 | // PackUnsigned takes care of 0x0000 saturation. |
| 2097 | Int4 int4(Min(cast, Float4(0xFFFF))); |
| 2098 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
| 2099 | } |
| 2100 | else if(CPUID::ARM) |
| 2101 | { |
| 2102 | // ARM saturates the 32-bit integer result on overflow/undeflow. |
| 2103 | Int4 int4(cast); |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2104 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2105 | } |
| 2106 | else |
| 2107 | { |
| 2108 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
| 2109 | } |
| 2110 | } |
| 2111 | else |
| 2112 | { |
| 2113 | *this = Short4(Int4(cast)); |
| 2114 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2115 | } |
| 2116 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2117 | RValue<UShort> Extract(RValue<UShort4> val, int i) |
| 2118 | { |
| 2119 | return RValue<UShort>(Nucleus::createExtractElement(val.value, UShort::getType(), i)); |
| 2120 | } |
| 2121 | |
| 2122 | RValue<UShort4> Insert(RValue<UShort4> val, RValue<UShort> element, int i) |
| 2123 | { |
| 2124 | return RValue<UShort4>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 2125 | } |
| 2126 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2127 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2128 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2129 | if(emulateIntrinsics) |
| 2130 | { |
| 2131 | UShort4 result; |
| 2132 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2133 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2134 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2135 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2136 | |
| 2137 | return result; |
| 2138 | } |
| 2139 | else |
| 2140 | { |
| 2141 | return RValue<UShort4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2142 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2146 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2147 | if(emulateIntrinsics) |
| 2148 | { |
| 2149 | UShort4 result; |
| 2150 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2151 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2152 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2153 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2154 | |
| 2155 | return result; |
| 2156 | } |
| 2157 | else |
| 2158 | { |
| 2159 | return RValue<UShort4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2160 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2161 | } |
| 2162 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2163 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2164 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 2165 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
| 2166 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value); |
| 2167 | ::basicBlock->appendInst(cmp); |
| 2168 | |
| 2169 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2170 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 2171 | ::basicBlock->appendInst(select); |
| 2172 | |
| 2173 | return RValue<UShort4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2177 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 2178 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
| 2179 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value); |
| 2180 | ::basicBlock->appendInst(cmp); |
| 2181 | |
| 2182 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2183 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 2184 | ::basicBlock->appendInst(select); |
| 2185 | |
| 2186 | return RValue<UShort4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2187 | } |
| 2188 | |
Nicolas Capens | 7f30181 | 2017-10-02 17:32:34 -0400 | [diff] [blame] | 2189 | RValue<UShort> SaturateUnsigned(RValue<Int> x) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2190 | { |
| 2191 | return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x))); |
| 2192 | } |
| 2193 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2194 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2195 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2196 | if(emulateIntrinsics) |
| 2197 | { |
| 2198 | UShort4 result; |
Nicolas Capens | 7f30181 | 2017-10-02 17:32:34 -0400 | [diff] [blame] | 2199 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2200 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2201 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2202 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2203 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2204 | return result; |
| 2205 | } |
| 2206 | else |
| 2207 | { |
| 2208 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2209 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2210 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2211 | auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2212 | paddusw->addArg(x.value); |
| 2213 | paddusw->addArg(y.value); |
| 2214 | ::basicBlock->appendInst(paddusw); |
| 2215 | |
| 2216 | return RValue<UShort4>(V(result)); |
| 2217 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2221 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2222 | if(emulateIntrinsics) |
| 2223 | { |
| 2224 | UShort4 result; |
Nicolas Capens | 7f30181 | 2017-10-02 17:32:34 -0400 | [diff] [blame] | 2225 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2226 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2227 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2228 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2229 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2230 | return result; |
| 2231 | } |
| 2232 | else |
| 2233 | { |
| 2234 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2235 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2236 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2237 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2238 | psubusw->addArg(x.value); |
| 2239 | psubusw->addArg(y.value); |
| 2240 | ::basicBlock->appendInst(psubusw); |
| 2241 | |
| 2242 | return RValue<UShort4>(V(result)); |
| 2243 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
| 2247 | { |
Nicolas Capens | 6c15744 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2248 | if(emulateIntrinsics) |
| 2249 | { |
| 2250 | UShort4 result; |
| 2251 | result = Insert(result, UShort((UInt(Extract(x, 0)) * UInt(Extract(y, 0))) >> 16), 0); |
| 2252 | result = Insert(result, UShort((UInt(Extract(x, 1)) * UInt(Extract(y, 1))) >> 16), 1); |
| 2253 | result = Insert(result, UShort((UInt(Extract(x, 2)) * UInt(Extract(y, 2))) >> 16), 2); |
| 2254 | result = Insert(result, UShort((UInt(Extract(x, 3)) * UInt(Extract(y, 3))) >> 16), 3); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2255 | |
Nicolas Capens | 6c15744 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2256 | return result; |
| 2257 | } |
| 2258 | else |
| 2259 | { |
| 2260 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2261 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2262 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2263 | auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2264 | pmulhuw->addArg(x.value); |
| 2265 | pmulhuw->addArg(y.value); |
| 2266 | ::basicBlock->appendInst(pmulhuw); |
| 2267 | |
| 2268 | return RValue<UShort4>(V(result)); |
| 2269 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2270 | } |
| 2271 | |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2272 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 2273 | { |
| 2274 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2275 | |
| 2276 | // Scalarized implementation. |
| 2277 | Int4 result; |
| 2278 | result = Insert(result, Int((Long(Extract(x, 0)) * Long(Extract(y, 0))) >> Long(Int(32))), 0); |
| 2279 | result = Insert(result, Int((Long(Extract(x, 1)) * Long(Extract(y, 1))) >> Long(Int(32))), 1); |
| 2280 | result = Insert(result, Int((Long(Extract(x, 2)) * Long(Extract(y, 2))) >> Long(Int(32))), 2); |
| 2281 | result = Insert(result, Int((Long(Extract(x, 3)) * Long(Extract(y, 3))) >> Long(Int(32))), 3); |
| 2282 | |
| 2283 | return result; |
| 2284 | } |
| 2285 | |
| 2286 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 2287 | { |
| 2288 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2289 | |
| 2290 | if(false) // Partial product based implementation. |
| 2291 | { |
| 2292 | auto xh = x >> 16; |
| 2293 | auto yh = y >> 16; |
| 2294 | auto xl = x & UInt4(0x0000FFFF); |
| 2295 | auto yl = y & UInt4(0x0000FFFF); |
| 2296 | auto xlyh = xl * yh; |
| 2297 | auto xhyl = xh * yl; |
| 2298 | auto xlyhh = xlyh >> 16; |
| 2299 | auto xhylh = xhyl >> 16; |
| 2300 | auto xlyhl = xlyh & UInt4(0x0000FFFF); |
| 2301 | auto xhyll = xhyl & UInt4(0x0000FFFF); |
| 2302 | auto xlylh = (xl * yl) >> 16; |
| 2303 | auto oflow = (xlyhl + xhyll + xlylh) >> 16; |
| 2304 | |
| 2305 | return (xh * yh) + (xlyhh + xhylh) + oflow; |
| 2306 | } |
| 2307 | |
| 2308 | // Scalarized implementation. |
| 2309 | Int4 result; |
| 2310 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 0))) * Long(UInt(Extract(As<Int4>(y), 0)))) >> Long(Int(32))), 0); |
| 2311 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 1))) * Long(UInt(Extract(As<Int4>(y), 1)))) >> Long(Int(32))), 1); |
| 2312 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 2))) * Long(UInt(Extract(As<Int4>(y), 2)))) >> Long(Int(32))), 2); |
| 2313 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 3))) * Long(UInt(Extract(As<Int4>(y), 3)))) >> Long(Int(32))), 3); |
| 2314 | |
| 2315 | return As<UInt4>(result); |
| 2316 | } |
| 2317 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2318 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 2319 | { |
Nicolas Capens | c37252c | 2016-09-28 16:11:54 -0400 | [diff] [blame] | 2320 | assert(false && "UNIMPLEMENTED"); return RValue<UShort4>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2321 | } |
| 2322 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2323 | Type *UShort4::getType() |
| 2324 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 2325 | return T(Type_v4i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2326 | } |
| 2327 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2328 | RValue<Short> Extract(RValue<Short8> val, int i) |
| 2329 | { |
| 2330 | return RValue<Short>(Nucleus::createExtractElement(val.value, Short::getType(), i)); |
| 2331 | } |
| 2332 | |
| 2333 | RValue<Short8> Insert(RValue<Short8> val, RValue<Short> element, int i) |
| 2334 | { |
| 2335 | return RValue<Short8>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 2336 | } |
| 2337 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2338 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 2339 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2340 | if(emulateIntrinsics) |
| 2341 | { |
| 2342 | Short8 result; |
| 2343 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 2344 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 2345 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 2346 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 2347 | result = Insert(result, Extract(lhs, 4) << Short(rhs), 4); |
| 2348 | result = Insert(result, Extract(lhs, 5) << Short(rhs), 5); |
| 2349 | result = Insert(result, Extract(lhs, 6) << Short(rhs), 6); |
| 2350 | result = Insert(result, Extract(lhs, 7) << Short(rhs), 7); |
| 2351 | |
| 2352 | return result; |
| 2353 | } |
| 2354 | else |
| 2355 | { |
| 2356 | return RValue<Short8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2357 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
| 2361 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2362 | if(emulateIntrinsics) |
| 2363 | { |
| 2364 | Short8 result; |
| 2365 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 2366 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 2367 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 2368 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 2369 | result = Insert(result, Extract(lhs, 4) >> Short(rhs), 4); |
| 2370 | result = Insert(result, Extract(lhs, 5) >> Short(rhs), 5); |
| 2371 | result = Insert(result, Extract(lhs, 6) >> Short(rhs), 6); |
| 2372 | result = Insert(result, Extract(lhs, 7) >> Short(rhs), 7); |
| 2373 | |
| 2374 | return result; |
| 2375 | } |
| 2376 | else |
| 2377 | { |
| 2378 | return RValue<Short8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2379 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 2383 | { |
Nicolas Capens | c37252c | 2016-09-28 16:11:54 -0400 | [diff] [blame] | 2384 | assert(false && "UNIMPLEMENTED"); return RValue<Int4>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2385 | } |
| 2386 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2387 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
| 2388 | { |
Nicolas Capens | c37252c | 2016-09-28 16:11:54 -0400 | [diff] [blame] | 2389 | assert(false && "UNIMPLEMENTED"); return RValue<Short8>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | Type *Short8::getType() |
| 2393 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2394 | return T(Ice::IceType_v8i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2395 | } |
| 2396 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2397 | RValue<UShort> Extract(RValue<UShort8> val, int i) |
| 2398 | { |
| 2399 | return RValue<UShort>(Nucleus::createExtractElement(val.value, UShort::getType(), i)); |
| 2400 | } |
| 2401 | |
| 2402 | RValue<UShort8> Insert(RValue<UShort8> val, RValue<UShort> element, int i) |
| 2403 | { |
| 2404 | return RValue<UShort8>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 2405 | } |
| 2406 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2407 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 2408 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2409 | if(emulateIntrinsics) |
| 2410 | { |
| 2411 | UShort8 result; |
| 2412 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2413 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2414 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2415 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2416 | result = Insert(result, Extract(lhs, 4) << UShort(rhs), 4); |
| 2417 | result = Insert(result, Extract(lhs, 5) << UShort(rhs), 5); |
| 2418 | result = Insert(result, Extract(lhs, 6) << UShort(rhs), 6); |
| 2419 | result = Insert(result, Extract(lhs, 7) << UShort(rhs), 7); |
| 2420 | |
| 2421 | return result; |
| 2422 | } |
| 2423 | else |
| 2424 | { |
| 2425 | return RValue<UShort8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2426 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
| 2430 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2431 | if(emulateIntrinsics) |
| 2432 | { |
| 2433 | UShort8 result; |
| 2434 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2435 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2436 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2437 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2438 | result = Insert(result, Extract(lhs, 4) >> UShort(rhs), 4); |
| 2439 | result = Insert(result, Extract(lhs, 5) >> UShort(rhs), 5); |
| 2440 | result = Insert(result, Extract(lhs, 6) >> UShort(rhs), 6); |
| 2441 | result = Insert(result, Extract(lhs, 7) >> UShort(rhs), 7); |
| 2442 | |
| 2443 | return result; |
| 2444 | } |
| 2445 | else |
| 2446 | { |
| 2447 | return RValue<UShort8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2448 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2449 | } |
| 2450 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2451 | RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7) |
| 2452 | { |
Nicolas Capens | c37252c | 2016-09-28 16:11:54 -0400 | [diff] [blame] | 2453 | assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 2457 | { |
Nicolas Capens | c37252c | 2016-09-28 16:11:54 -0400 | [diff] [blame] | 2458 | assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | // FIXME: Implement as Shuffle(x, y, Select(i0, ..., i16)) and Shuffle(x, y, SELECT_PACK_REPEAT(element)) |
| 2462 | // RValue<UShort8> PackRepeat(RValue<Byte16> x, RValue<Byte16> y, int element) |
| 2463 | // { |
Nicolas Capens | c37252c | 2016-09-28 16:11:54 -0400 | [diff] [blame] | 2464 | // assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2465 | // } |
| 2466 | |
| 2467 | Type *UShort8::getType() |
| 2468 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2469 | return T(Ice::IceType_v8i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2470 | } |
| 2471 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2472 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2473 | { |
Nicolas Capens | 5b41ba3 | 2016-12-08 14:34:00 -0500 | [diff] [blame] | 2474 | RValue<Int> res = val; |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2475 | val += 1; |
| 2476 | return res; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2477 | } |
| 2478 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2479 | const Int &operator++(Int &val) // Pre-increment |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2480 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2481 | val += 1; |
| 2482 | return val; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2483 | } |
| 2484 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2485 | RValue<Int> operator--(Int &val, int) // Post-decrement |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2486 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2487 | RValue<Int> res = val; |
| 2488 | val -= 1; |
| 2489 | return res; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2490 | } |
| 2491 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2492 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2493 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2494 | val -= 1; |
| 2495 | return val; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2496 | } |
| 2497 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2498 | RValue<Int> RoundInt(RValue<Float> cast) |
| 2499 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2500 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 2501 | { |
| 2502 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 2503 | return Int((cast + Float(0x00C00000)) - Float(0x00C00000)); |
| 2504 | } |
| 2505 | else |
| 2506 | { |
| 2507 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 2508 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2509 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2510 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 2511 | nearbyint->addArg(cast.value); |
| 2512 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 2513 | |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 2514 | return RValue<Int>(V(result)); |
| 2515 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | Type *Int::getType() |
| 2519 | { |
| 2520 | return T(Ice::IceType_i32); |
| 2521 | } |
| 2522 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2523 | Type *Long::getType() |
| 2524 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2525 | return T(Ice::IceType_i64); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2526 | } |
| 2527 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2528 | UInt::UInt(RValue<Float> cast) |
| 2529 | { |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 2530 | // Smallest positive value representable in UInt, but not in Int |
| 2531 | const unsigned int ustart = 0x80000000u; |
| 2532 | const float ustartf = float(ustart); |
| 2533 | |
| 2534 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2535 | storeValue((~(As<Int>(cast) >> 31) & |
| 2536 | // Check if the value can be represented as an Int |
| 2537 | IfThenElse(cast >= ustartf, |
| 2538 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2539 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 2540 | // Otherwise, just convert normally |
| 2541 | Int(cast))).value); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2542 | } |
| 2543 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2544 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2545 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2546 | RValue<UInt> res = val; |
| 2547 | val += 1; |
| 2548 | return res; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2549 | } |
| 2550 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2551 | const UInt &operator++(UInt &val) // Pre-increment |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2552 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2553 | val += 1; |
| 2554 | return val; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2555 | } |
| 2556 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2557 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2558 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2559 | RValue<UInt> res = val; |
| 2560 | val -= 1; |
| 2561 | return res; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2562 | } |
| 2563 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2564 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2565 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2566 | val -= 1; |
| 2567 | return val; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2568 | } |
| 2569 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2570 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
| 2571 | // { |
Nicolas Capens | c37252c | 2016-09-28 16:11:54 -0400 | [diff] [blame] | 2572 | // assert(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2573 | // } |
| 2574 | |
| 2575 | Type *UInt::getType() |
| 2576 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2577 | return T(Ice::IceType_i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | // Int2::Int2(RValue<Int> cast) |
| 2581 | // { |
| 2582 | // Value *extend = Nucleus::createZExt(cast.value, Long::getType()); |
| 2583 | // Value *vector = Nucleus::createBitCast(extend, Int2::getType()); |
| 2584 | // |
| 2585 | // Constant *shuffle[2]; |
| 2586 | // shuffle[0] = Nucleus::createConstantInt(0); |
| 2587 | // shuffle[1] = Nucleus::createConstantInt(0); |
| 2588 | // |
| 2589 | // Value *replicate = Nucleus::createShuffleVector(vector, UndefValue::get(Int2::getType()), Nucleus::createConstantVector(shuffle, 2)); |
| 2590 | // |
| 2591 | // storeValue(replicate); |
| 2592 | // } |
| 2593 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2594 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 2595 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2596 | if(emulateIntrinsics) |
| 2597 | { |
| 2598 | Int2 result; |
| 2599 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 2600 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 2601 | |
| 2602 | return result; |
| 2603 | } |
| 2604 | else |
| 2605 | { |
| 2606 | return RValue<Int2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2607 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 2611 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2612 | if(emulateIntrinsics) |
| 2613 | { |
| 2614 | Int2 result; |
| 2615 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 2616 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 2617 | |
| 2618 | return result; |
| 2619 | } |
| 2620 | else |
| 2621 | { |
| 2622 | return RValue<Int2>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2623 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2624 | } |
| 2625 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2626 | Type *Int2::getType() |
| 2627 | { |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 2628 | return T(Type_v2i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2629 | } |
| 2630 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2631 | RValue<UInt> Extract(RValue<UInt2> val, int i) |
| 2632 | { |
| 2633 | return RValue<UInt>(Nucleus::createExtractElement(val.value, UInt::getType(), i)); |
| 2634 | } |
| 2635 | |
| 2636 | RValue<UInt2> Insert(RValue<UInt2> val, RValue<UInt> element, int i) |
| 2637 | { |
| 2638 | return RValue<UInt2>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 2639 | } |
| 2640 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2641 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
| 2642 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2643 | if(emulateIntrinsics) |
| 2644 | { |
| 2645 | UInt2 result; |
| 2646 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 2647 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 2648 | |
| 2649 | return result; |
| 2650 | } |
| 2651 | else |
| 2652 | { |
| 2653 | return RValue<UInt2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2654 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 2658 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2659 | if(emulateIntrinsics) |
| 2660 | { |
| 2661 | UInt2 result; |
| 2662 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 2663 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 2664 | |
| 2665 | return result; |
| 2666 | } |
| 2667 | else |
| 2668 | { |
| 2669 | return RValue<UInt2>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2670 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2671 | } |
| 2672 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2673 | Type *UInt2::getType() |
| 2674 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2675 | return T(Type_v2i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2676 | } |
| 2677 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2678 | Int4::Int4(RValue<Byte4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2679 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2680 | Value *x = Nucleus::createBitCast(cast.value, Int::getType()); |
| 2681 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 2682 | |
| 2683 | Value *e; |
| 2684 | int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; |
| 2685 | Value *b = Nucleus::createBitCast(a, Byte16::getType()); |
| 2686 | Value *c = Nucleus::createShuffleVector(b, V(Nucleus::createNullValue(Byte16::getType())), swizzle); |
| 2687 | |
| 2688 | int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
| 2689 | Value *d = Nucleus::createBitCast(c, Short8::getType()); |
| 2690 | e = Nucleus::createShuffleVector(d, V(Nucleus::createNullValue(Short8::getType())), swizzle2); |
| 2691 | |
| 2692 | Value *f = Nucleus::createBitCast(e, Int4::getType()); |
| 2693 | storeValue(f); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2694 | } |
| 2695 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2696 | Int4::Int4(RValue<SByte4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2697 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2698 | Value *x = Nucleus::createBitCast(cast.value, Int::getType()); |
| 2699 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 2700 | |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2701 | int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; |
| 2702 | Value *b = Nucleus::createBitCast(a, Byte16::getType()); |
| 2703 | Value *c = Nucleus::createShuffleVector(b, b, swizzle); |
| 2704 | |
| 2705 | int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
| 2706 | Value *d = Nucleus::createBitCast(c, Short8::getType()); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2707 | Value *e = Nucleus::createShuffleVector(d, d, swizzle2); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2708 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2709 | *this = As<Int4>(e) >> 24; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2710 | } |
| 2711 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2712 | Int4::Int4(RValue<Short4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2713 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2714 | int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
| 2715 | Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2716 | |
| 2717 | *this = As<Int4>(c) >> 16; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2718 | } |
| 2719 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2720 | Int4::Int4(RValue<UShort4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2721 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2722 | int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
| 2723 | Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
| 2724 | Value *d = Nucleus::createBitCast(c, Int4::getType()); |
| 2725 | storeValue(d); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2726 | } |
| 2727 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2728 | Int4::Int4(RValue<Int> rhs) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2729 | { |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 2730 | Value *vector = Nucleus::createBitCast(rhs.value, Int4::getType()); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2731 | |
| 2732 | int swizzle[4] = {0, 0, 0, 0}; |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 2733 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2734 | |
| 2735 | storeValue(replicate); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2736 | } |
| 2737 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2738 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 2739 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2740 | if(emulateIntrinsics) |
| 2741 | { |
| 2742 | Int4 result; |
| 2743 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 2744 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 2745 | result = Insert(result, Extract(lhs, 2) << Int(rhs), 2); |
| 2746 | result = Insert(result, Extract(lhs, 3) << Int(rhs), 3); |
| 2747 | |
| 2748 | return result; |
| 2749 | } |
| 2750 | else |
| 2751 | { |
| 2752 | return RValue<Int4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2753 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 2757 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2758 | if(emulateIntrinsics) |
| 2759 | { |
| 2760 | Int4 result; |
| 2761 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 2762 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 2763 | result = Insert(result, Extract(lhs, 2) >> Int(rhs), 2); |
| 2764 | result = Insert(result, Extract(lhs, 3) >> Int(rhs), 3); |
| 2765 | |
| 2766 | return result; |
| 2767 | } |
| 2768 | else |
| 2769 | { |
| 2770 | return RValue<Int4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2771 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2772 | } |
| 2773 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2774 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 2775 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2776 | return RValue<Int4>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 2780 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2781 | return RValue<Int4>(Nucleus::createICmpSLT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 2785 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2786 | return RValue<Int4>(Nucleus::createICmpSLE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 2790 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2791 | return RValue<Int4>(Nucleus::createICmpNE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 2795 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2796 | return RValue<Int4>(Nucleus::createICmpSGE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 2800 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2801 | return RValue<Int4>(Nucleus::createICmpSGT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 2805 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 2806 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
| 2807 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value); |
| 2808 | ::basicBlock->appendInst(cmp); |
| 2809 | |
| 2810 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 2811 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 2812 | ::basicBlock->appendInst(select); |
| 2813 | |
| 2814 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 2818 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 2819 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
| 2820 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value); |
| 2821 | ::basicBlock->appendInst(cmp); |
| 2822 | |
| 2823 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 2824 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 2825 | ::basicBlock->appendInst(select); |
| 2826 | |
| 2827 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 2831 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2832 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 2833 | { |
| 2834 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 2835 | return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000)); |
| 2836 | } |
| 2837 | else |
| 2838 | { |
| 2839 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 2840 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2841 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2842 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 2843 | nearbyint->addArg(cast.value); |
| 2844 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 2845 | |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 2846 | return RValue<Int4>(V(result)); |
| 2847 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2848 | } |
| 2849 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2850 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2851 | { |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2852 | if(emulateIntrinsics) |
| 2853 | { |
| 2854 | Short8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2855 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2856 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2857 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2858 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2859 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2860 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2861 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2862 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 2863 | |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2864 | return result; |
| 2865 | } |
| 2866 | else |
| 2867 | { |
| 2868 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2869 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2870 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2871 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2872 | pack->addArg(x.value); |
| 2873 | pack->addArg(y.value); |
| 2874 | ::basicBlock->appendInst(pack); |
| 2875 | |
| 2876 | return RValue<Short8>(V(result)); |
| 2877 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2878 | } |
| 2879 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2880 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 2881 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2882 | if(emulateIntrinsics || !(CPUID::SSE4_1 || CPUID::ARM)) |
| 2883 | { |
| 2884 | RValue<Int4> sx = As<Int4>(x); |
| 2885 | RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000); |
| 2886 | |
| 2887 | RValue<Int4> sy = As<Int4>(y); |
| 2888 | RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000); |
| 2889 | |
| 2890 | return As<UShort8>(PackSigned(bx, by) + Short8(0x8000u)); |
| 2891 | } |
| 2892 | else |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2893 | { |
| 2894 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2895 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2896 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2897 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2898 | pack->addArg(x.value); |
| 2899 | pack->addArg(y.value); |
| 2900 | ::basicBlock->appendInst(pack); |
| 2901 | |
| 2902 | return RValue<UShort8>(V(result)); |
| 2903 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2904 | } |
| 2905 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2906 | RValue<Int> SignMask(RValue<Int4> x) |
| 2907 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2908 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2909 | { |
| 2910 | Int4 xx = (x >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 2911 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 2912 | } |
| 2913 | else |
| 2914 | { |
| 2915 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 2916 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2917 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2918 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 2919 | movmsk->addArg(x.value); |
| 2920 | ::basicBlock->appendInst(movmsk); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 2921 | |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2922 | return RValue<Int>(V(result)); |
| 2923 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2924 | } |
| 2925 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2926 | Type *Int4::getType() |
| 2927 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 2928 | return T(Ice::IceType_v4i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2929 | } |
| 2930 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2931 | UInt4::UInt4(RValue<Float4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2932 | { |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 2933 | // Smallest positive value representable in UInt, but not in Int |
| 2934 | const unsigned int ustart = 0x80000000u; |
| 2935 | const float ustartf = float(ustart); |
| 2936 | |
| 2937 | // Check if the value can be represented as an Int |
| 2938 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 2939 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2940 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
| 2941 | // Otherwise, just convert normally |
| 2942 | (~uiValue & Int4(cast)); |
| 2943 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2944 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2945 | } |
| 2946 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2947 | RValue<UInt> Extract(RValue<UInt4> x, int i) |
| 2948 | { |
| 2949 | return RValue<UInt>(Nucleus::createExtractElement(x.value, UInt::getType(), i)); |
| 2950 | } |
| 2951 | |
| 2952 | RValue<UInt4> Insert(RValue<UInt4> x, RValue<UInt> element, int i) |
| 2953 | { |
| 2954 | return RValue<UInt4>(Nucleus::createInsertElement(x.value, element.value, i)); |
| 2955 | } |
| 2956 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2957 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 2958 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2959 | if(emulateIntrinsics) |
| 2960 | { |
| 2961 | UInt4 result; |
| 2962 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 2963 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 2964 | result = Insert(result, Extract(lhs, 2) << UInt(rhs), 2); |
| 2965 | result = Insert(result, Extract(lhs, 3) << UInt(rhs), 3); |
| 2966 | |
| 2967 | return result; |
| 2968 | } |
| 2969 | else |
| 2970 | { |
| 2971 | return RValue<UInt4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2972 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 2976 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2977 | if(emulateIntrinsics) |
| 2978 | { |
| 2979 | UInt4 result; |
| 2980 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 2981 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 2982 | result = Insert(result, Extract(lhs, 2) >> UInt(rhs), 2); |
| 2983 | result = Insert(result, Extract(lhs, 3) >> UInt(rhs), 3); |
| 2984 | |
| 2985 | return result; |
| 2986 | } |
| 2987 | else |
| 2988 | { |
| 2989 | return RValue<UInt4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2990 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2991 | } |
| 2992 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2993 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 2994 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2995 | return RValue<UInt4>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 2999 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3000 | return RValue<UInt4>(Nucleus::createICmpULT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3004 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3005 | return RValue<UInt4>(Nucleus::createICmpULE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3009 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3010 | return RValue<UInt4>(Nucleus::createICmpNE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3014 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3015 | return RValue<UInt4>(Nucleus::createICmpUGE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3019 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3020 | return RValue<UInt4>(Nucleus::createICmpUGT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3024 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3025 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
| 3026 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value); |
| 3027 | ::basicBlock->appendInst(cmp); |
| 3028 | |
| 3029 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3030 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 3031 | ::basicBlock->appendInst(select); |
| 3032 | |
| 3033 | return RValue<UInt4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3037 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3038 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
| 3039 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value); |
| 3040 | ::basicBlock->appendInst(cmp); |
| 3041 | |
| 3042 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3043 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 3044 | ::basicBlock->appendInst(select); |
| 3045 | |
| 3046 | return RValue<UInt4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3047 | } |
| 3048 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3049 | Type *UInt4::getType() |
| 3050 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 3051 | return T(Ice::IceType_v4i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3052 | } |
| 3053 | |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3054 | Type *Half::getType() |
| 3055 | { |
| 3056 | return T(Ice::IceType_i16); |
| 3057 | } |
Alexis Hetu | 734e257 | 2018-12-20 14:00:49 -0500 | [diff] [blame] | 3058 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3059 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 3060 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3061 | return 1.0f / x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
| 3065 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3066 | return Rcp_pp(Sqrt(x)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3067 | } |
| 3068 | |
| 3069 | RValue<Float> Sqrt(RValue<Float> x) |
| 3070 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3071 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32); |
| 3072 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3073 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3074 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3075 | sqrt->addArg(x.value); |
| 3076 | ::basicBlock->appendInst(sqrt); |
| 3077 | |
| 3078 | return RValue<Float>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | RValue<Float> Round(RValue<Float> x) |
| 3082 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3083 | return Float4(Round(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | RValue<Float> Trunc(RValue<Float> x) |
| 3087 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3088 | return Float4(Trunc(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | RValue<Float> Frac(RValue<Float> x) |
| 3092 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3093 | return Float4(Frac(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | RValue<Float> Floor(RValue<Float> x) |
| 3097 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3098 | return Float4(Floor(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | RValue<Float> Ceil(RValue<Float> x) |
| 3102 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3103 | return Float4(Ceil(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | Type *Float::getType() |
| 3107 | { |
Nicolas Capens | 9709d4f | 2016-09-30 11:44:14 -0400 | [diff] [blame] | 3108 | return T(Ice::IceType_f32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3109 | } |
| 3110 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3111 | Type *Float2::getType() |
| 3112 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 3113 | return T(Type_v2f32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3114 | } |
| 3115 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3116 | Float4::Float4(RValue<Float> rhs) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3117 | { |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 3118 | Value *vector = Nucleus::createBitCast(rhs.value, Float4::getType()); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3119 | |
| 3120 | int swizzle[4] = {0, 0, 0, 0}; |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 3121 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3122 | |
| 3123 | storeValue(replicate); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3124 | } |
| 3125 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3126 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 3127 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3128 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | 5cdb91a | 2017-02-13 12:39:18 -0500 | [diff] [blame] | 3129 | auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ogt, condition, x.value, y.value); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3130 | ::basicBlock->appendInst(cmp); |
| 3131 | |
| 3132 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | 5cdb91a | 2017-02-13 12:39:18 -0500 | [diff] [blame] | 3133 | auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3134 | ::basicBlock->appendInst(select); |
| 3135 | |
| 3136 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 3140 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3141 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | 5cdb91a | 2017-02-13 12:39:18 -0500 | [diff] [blame] | 3142 | auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Olt, condition, x.value, y.value); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3143 | ::basicBlock->appendInst(cmp); |
| 3144 | |
| 3145 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | 5cdb91a | 2017-02-13 12:39:18 -0500 | [diff] [blame] | 3146 | auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3147 | ::basicBlock->appendInst(select); |
| 3148 | |
| 3149 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 3153 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3154 | return Float4(1.0f) / x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 3158 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3159 | return Rcp_pp(Sqrt(x)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 3163 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3164 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 9f737d3 | 2017-07-25 17:26:14 -0400 | [diff] [blame] | 3165 | { |
| 3166 | Float4 result; |
| 3167 | result.x = Sqrt(Float(Float4(x).x)); |
| 3168 | result.y = Sqrt(Float(Float4(x).y)); |
| 3169 | result.z = Sqrt(Float(Float4(x).z)); |
| 3170 | result.w = Sqrt(Float(Float4(x).w)); |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3171 | |
Nicolas Capens | 9f737d3 | 2017-07-25 17:26:14 -0400 | [diff] [blame] | 3172 | return result; |
| 3173 | } |
| 3174 | else |
| 3175 | { |
| 3176 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3177 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3178 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3179 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3180 | sqrt->addArg(x.value); |
| 3181 | ::basicBlock->appendInst(sqrt); |
| 3182 | |
| 3183 | return RValue<Float4>(V(result)); |
| 3184 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3185 | } |
| 3186 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3187 | RValue<Int> SignMask(RValue<Float4> x) |
| 3188 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3189 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3190 | { |
| 3191 | Int4 xx = (As<Int4>(x) >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 3192 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 3193 | } |
| 3194 | else |
| 3195 | { |
| 3196 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 3197 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3198 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3199 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3200 | movmsk->addArg(x.value); |
| 3201 | ::basicBlock->appendInst(movmsk); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 3202 | |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3203 | return RValue<Int>(V(result)); |
| 3204 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
| 3208 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3209 | return RValue<Int4>(Nucleus::createFCmpOEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
| 3213 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3214 | return RValue<Int4>(Nucleus::createFCmpOLT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
| 3218 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3219 | return RValue<Int4>(Nucleus::createFCmpOLE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
| 3223 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3224 | return RValue<Int4>(Nucleus::createFCmpONE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
| 3228 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3229 | return RValue<Int4>(Nucleus::createFCmpOGE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
| 3233 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3234 | return RValue<Int4>(Nucleus::createFCmpOGT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3235 | } |
| 3236 | |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3237 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 3238 | { |
| 3239 | return RValue<Int4>(Nucleus::createFCmpUEQ(x.value, y.value)); |
| 3240 | } |
| 3241 | |
| 3242 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 3243 | { |
| 3244 | return RValue<Int4>(Nucleus::createFCmpULT(x.value, y.value)); |
| 3245 | } |
| 3246 | |
| 3247 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 3248 | { |
| 3249 | return RValue<Int4>(Nucleus::createFCmpULE(x.value, y.value)); |
| 3250 | } |
| 3251 | |
| 3252 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 3253 | { |
| 3254 | return RValue<Int4>(Nucleus::createFCmpUNE(x.value, y.value)); |
| 3255 | } |
| 3256 | |
| 3257 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 3258 | { |
| 3259 | return RValue<Int4>(Nucleus::createFCmpUGE(x.value, y.value)); |
| 3260 | } |
| 3261 | |
| 3262 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 3263 | { |
| 3264 | return RValue<Int4>(Nucleus::createFCmpUGT(x.value, y.value)); |
| 3265 | } |
| 3266 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3267 | RValue<Float4> Round(RValue<Float4> x) |
| 3268 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3269 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 3270 | { |
| 3271 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3272 | return (x + Float4(0x00C00000)) - Float4(0x00C00000); |
| 3273 | } |
| 3274 | else if(CPUID::SSE4_1) |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3275 | { |
| 3276 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3277 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3278 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3279 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 3280 | round->addArg(x.value); |
| 3281 | round->addArg(::context->getConstantInt32(0)); |
| 3282 | ::basicBlock->appendInst(round); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3283 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3284 | return RValue<Float4>(V(result)); |
| 3285 | } |
| 3286 | else |
| 3287 | { |
| 3288 | return Float4(RoundInt(x)); |
| 3289 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | RValue<Float4> Trunc(RValue<Float4> x) |
| 3293 | { |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3294 | if(CPUID::SSE4_1) |
| 3295 | { |
| 3296 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3297 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3298 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3299 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 3300 | round->addArg(x.value); |
| 3301 | round->addArg(::context->getConstantInt32(3)); |
| 3302 | ::basicBlock->appendInst(round); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3303 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3304 | return RValue<Float4>(V(result)); |
| 3305 | } |
| 3306 | else |
| 3307 | { |
| 3308 | return Float4(Int4(x)); |
| 3309 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | RValue<Float4> Frac(RValue<Float4> x) |
| 3313 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3314 | Float4 frc; |
| 3315 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3316 | if(CPUID::SSE4_1) |
| 3317 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3318 | frc = x - Floor(x); |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3319 | } |
| 3320 | else |
| 3321 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3322 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3323 | |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3324 | frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1, 1, 1, 1))); // Add 1.0 if negative. |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3325 | } |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3326 | |
| 3327 | // x - floor(x) can be 1.0 for very small negative x. |
| 3328 | // Clamp against the value just below 1.0. |
| 3329 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | RValue<Float4> Floor(RValue<Float4> x) |
| 3333 | { |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3334 | if(CPUID::SSE4_1) |
| 3335 | { |
| 3336 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3337 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3338 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3339 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 3340 | round->addArg(x.value); |
| 3341 | round->addArg(::context->getConstantInt32(1)); |
| 3342 | ::basicBlock->appendInst(round); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3343 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3344 | return RValue<Float4>(V(result)); |
| 3345 | } |
| 3346 | else |
| 3347 | { |
| 3348 | return x - Frac(x); |
| 3349 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | RValue<Float4> Ceil(RValue<Float4> x) |
| 3353 | { |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3354 | if(CPUID::SSE4_1) |
| 3355 | { |
| 3356 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3357 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3358 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3359 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 3360 | round->addArg(x.value); |
| 3361 | round->addArg(::context->getConstantInt32(2)); |
| 3362 | ::basicBlock->appendInst(round); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3363 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3364 | return RValue<Float4>(V(result)); |
| 3365 | } |
| 3366 | else |
| 3367 | { |
| 3368 | return -Floor(-x); |
| 3369 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | Type *Float4::getType() |
| 3373 | { |
Nicolas Capens | 9709d4f | 2016-09-30 11:44:14 -0400 | [diff] [blame] | 3374 | return T(Ice::IceType_v4f32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3375 | } |
| 3376 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3377 | RValue<Long> Ticks() |
| 3378 | { |
Nicolas Capens | c37252c | 2016-09-28 16:11:54 -0400 | [diff] [blame] | 3379 | assert(false && "UNIMPLEMENTED"); return RValue<Long>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3380 | } |
| 3381 | } |