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" |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 16 | #include "Debug.hpp" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 17 | |
Nicolas Capens | 2ae9d74 | 2016-11-24 14:43:05 -0500 | [diff] [blame] | 18 | #include "Optimizer.hpp" |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 19 | #include "ExecutableMemory.hpp" |
Nicolas Capens | a062f32 | 2018-09-06 15:34:46 -0400 | [diff] [blame] | 20 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 21 | #include "src/IceTypes.h" |
| 22 | #include "src/IceCfg.h" |
| 23 | #include "src/IceELFStreamer.h" |
| 24 | #include "src/IceGlobalContext.h" |
| 25 | #include "src/IceCfgNode.h" |
| 26 | #include "src/IceELFObjectWriter.h" |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 27 | #include "src/IceGlobalInits.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 28 | |
| 29 | #include "llvm/Support/FileSystem.h" |
| 30 | #include "llvm/Support/raw_os_ostream.h" |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 31 | #include "llvm/Support/Compiler.h" |
| 32 | |
| 33 | #if __has_feature(memory_sanitizer) |
| 34 | #include <sanitizer/msan_interface.h> |
| 35 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 36 | |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 37 | #if defined(_WIN32) |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 38 | #ifndef WIN32_LEAN_AND_MEAN |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 39 | #define WIN32_LEAN_AND_MEAN |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 40 | #endif // !WIN32_LEAN_AND_MEAN |
| 41 | #ifndef NOMINMAX |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 42 | #define NOMINMAX |
Alexis Hetu | 113e33a | 2017-01-19 10:49:19 -0500 | [diff] [blame] | 43 | #endif // !NOMINMAX |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 44 | #include <Windows.h> |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 45 | #else |
| 46 | #include <sys/mman.h> |
Nicolas Capens | 411273e | 2017-01-26 15:13:36 -0800 | [diff] [blame] | 47 | #if !defined(MAP_ANONYMOUS) |
| 48 | #define MAP_ANONYMOUS MAP_ANON |
Nicolas Capens | 8b27574 | 2017-01-20 17:11:41 -0500 | [diff] [blame] | 49 | #endif |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 50 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 51 | |
Nicolas Capens | c07dc4b | 2018-08-06 14:20:45 -0400 | [diff] [blame] | 52 | #include <mutex> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 53 | #include <limits> |
| 54 | #include <iostream> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 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; |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 194 | default: ASSERT(false); |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 232 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 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: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 275 | ASSERT(false && "Unsupported relocation type"); |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 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: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 293 | ASSERT(false && "Unsupported relocation type"); |
Nicolas Capens | 30cd7d4 | 2017-04-25 15:17:25 -0400 | [diff] [blame] | 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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 317 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 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: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 355 | ASSERT(false && "Unsupported relocation type"); |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 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 |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [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__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 374 | ASSERT(sizeof(void*) == 4 && elfHeader->e_machine == EM_386); |
Nicolas Capens | 30cd7d4 | 2017-04-25 15:17:25 -0400 | [diff] [blame] | 375 | #elif defined(__x86_64__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 376 | ASSERT(sizeof(void*) == 8 && elfHeader->e_machine == EM_X86_64); |
Nicolas Capens | 30cd7d4 | 2017-04-25 15:17:25 -0400 | [diff] [blame] | 377 | #elif defined(__arm__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 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__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 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__) |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 402 | ASSERT(sizeof(void*) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 412 | ASSERT(sizeof(void*) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 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 | } |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 480 | else ASSERT(false && "UNIMPLEMENTED"); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 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(); |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 593 | ASSERT(!::function->hasError()); |
Nicolas Capens | de19f39 | 2016-10-19 10:29:49 -0400 | [diff] [blame] | 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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 868 | ASSERT(!atomic); // Unimplemented |
| 869 | ASSERT(memoryOrder == std::memory_order_relaxed); // Unimplemented |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 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 | } |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 902 | else UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Nicolas Capens | 070d9f4 | 2017-04-26 13:36:33 -0400 | [diff] [blame] | 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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 925 | ASSERT(!atomic); // Unimplemented |
| 926 | ASSERT(memoryOrder == std::memory_order_relaxed); // Unimplemented |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 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 | } |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 971 | else UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Nicolas Capens | 070d9f4 | 2017-04-26 13:36:33 -0400 | [diff] [blame] | 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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 997 | ASSERT(index->getType() == Ice::IceType_i32); |
Nicolas Capens | 8820f64 | 2016-09-30 04:42:43 -0400 | [diff] [blame] | 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 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1033 | UNIMPLEMENTED("createAtomicAdd"); |
| 1034 | return nullptr; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1035 | } |
| 1036 | |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1037 | static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType) |
| 1038 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1039 | if(v->getType() == T(destType)) |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1040 | { |
| 1041 | return v; |
| 1042 | } |
| 1043 | |
| 1044 | Ice::Variable *result = ::function->makeVariable(T(destType)); |
| 1045 | Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v); |
| 1046 | ::basicBlock->appendInst(cast); |
| 1047 | |
| 1048 | return V(result); |
| 1049 | } |
| 1050 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1051 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
| 1052 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1053 | return createCast(Ice::InstCast::Trunc, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | Value *Nucleus::createZExt(Value *v, Type *destType) |
| 1057 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1058 | return createCast(Ice::InstCast::Zext, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | Value *Nucleus::createSExt(Value *v, Type *destType) |
| 1062 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1063 | return createCast(Ice::InstCast::Sext, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
| 1067 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1068 | return createCast(Ice::InstCast::Fptosi, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1071 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
| 1072 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1073 | return createCast(Ice::InstCast::Sitofp, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
| 1077 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1078 | return createCast(Ice::InstCast::Fptrunc, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
| 1082 | { |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1083 | return createCast(Ice::InstCast::Fpext, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 1087 | { |
Nicolas Capens | 2d8c370 | 2017-07-25 13:56:46 -0400 | [diff] [blame] | 1088 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1089 | // support for casting between scalars and wide vectors. For platforms where this is not supported, |
| 1090 | // emulate them by writing to the stack and reading back as the destination type. |
| 1091 | if(emulateMismatchedBitCast) |
| 1092 | { |
| 1093 | if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType))) |
| 1094 | { |
| 1095 | Value *address = allocateStackVariable(destType); |
| 1096 | createStore(v, address, T(v->getType())); |
| 1097 | return createLoad(address, destType); |
| 1098 | } |
| 1099 | else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType))) |
| 1100 | { |
| 1101 | Value *address = allocateStackVariable(T(v->getType())); |
| 1102 | createStore(v, address, T(v->getType())); |
| 1103 | return createLoad(address, destType); |
| 1104 | } |
| 1105 | } |
| 1106 | |
Nicolas Capens | a0c2fc5 | 2016-09-30 05:04:21 -0400 | [diff] [blame] | 1107 | return createCast(Ice::InstCast::Bitcast, v, destType); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1108 | } |
| 1109 | |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1110 | static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1111 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1112 | ASSERT(lhs->getType() == rhs->getType()); |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 1113 | |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1114 | auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType()); |
| 1115 | auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs); |
Nicolas Capens | 611642a | 2016-09-28 16:45:04 -0400 | [diff] [blame] | 1116 | ::basicBlock->appendInst(cmp); |
| 1117 | |
| 1118 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1119 | } |
| 1120 | |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1121 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1122 | { |
| 1123 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1124 | } |
| 1125 | |
| 1126 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1127 | { |
| 1128 | return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs); |
| 1129 | } |
| 1130 | |
| 1131 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1132 | { |
| 1133 | return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs); |
| 1134 | } |
| 1135 | |
| 1136 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1137 | { |
| 1138 | return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs); |
| 1139 | } |
| 1140 | |
| 1141 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1142 | { |
| 1143 | return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs); |
| 1144 | } |
| 1145 | |
| 1146 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1147 | { |
| 1148 | return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs); |
| 1149 | } |
| 1150 | |
| 1151 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1152 | { |
| 1153 | return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs); |
| 1154 | } |
| 1155 | |
| 1156 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1157 | { |
| 1158 | return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs); |
| 1159 | } |
| 1160 | |
| 1161 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1162 | { |
| 1163 | return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs); |
| 1164 | } |
| 1165 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1166 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1167 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1168 | return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs); |
| 1169 | } |
| 1170 | |
| 1171 | static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs) |
| 1172 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1173 | ASSERT(lhs->getType() == rhs->getType()); |
| 1174 | ASSERT(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32); |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1175 | |
| 1176 | auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32); |
| 1177 | auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs); |
| 1178 | ::basicBlock->appendInst(cmp); |
| 1179 | |
| 1180 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1184 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1185 | return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1189 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1190 | return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1194 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1195 | return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1199 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1200 | return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1204 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1205 | return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1209 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1210 | return createFloatCompare(Ice::InstFcmp::One, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1214 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1215 | return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1219 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1220 | return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1224 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1225 | return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1229 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1230 | return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1234 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1235 | return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1239 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1240 | return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1244 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1245 | return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1249 | { |
Nicolas Capens | 43dc629 | 2016-10-20 00:01:38 -0400 | [diff] [blame] | 1250 | return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1251 | } |
| 1252 | |
Nicolas Capens | e95d534 | 2016-09-30 11:37:28 -0400 | [diff] [blame] | 1253 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1254 | { |
Nicolas Capens | 9709d4f | 2016-09-30 11:44:14 -0400 | [diff] [blame] | 1255 | auto result = ::function->makeVariable(T(type)); |
| 1256 | auto extract = Ice::InstExtractElement::create(::function, result, vector, ::context->getConstantInt32(index)); |
| 1257 | ::basicBlock->appendInst(extract); |
| 1258 | |
| 1259 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1263 | { |
Nicolas Capens | 9709d4f | 2016-09-30 11:44:14 -0400 | [diff] [blame] | 1264 | auto result = ::function->makeVariable(vector->getType()); |
| 1265 | auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index)); |
| 1266 | ::basicBlock->appendInst(insert); |
| 1267 | |
| 1268 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1269 | } |
| 1270 | |
Nicolas Capens | e89cd58 | 2016-09-30 14:23:47 -0400 | [diff] [blame] | 1271 | Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1272 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1273 | ASSERT(V1->getType() == V2->getType()); |
Nicolas Capens | 619c0ab | 2016-09-30 14:46:24 -0400 | [diff] [blame] | 1274 | |
| 1275 | int size = Ice::typeNumElements(V1->getType()); |
| 1276 | auto result = ::function->makeVariable(V1->getType()); |
| 1277 | auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2); |
| 1278 | |
| 1279 | for(int i = 0; i < size; i++) |
| 1280 | { |
| 1281 | shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i]))); |
| 1282 | } |
| 1283 | |
| 1284 | ::basicBlock->appendInst(shuffle); |
| 1285 | |
| 1286 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse) |
| 1290 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1291 | ASSERT(ifTrue->getType() == ifFalse->getType()); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 1292 | |
| 1293 | auto result = ::function->makeVariable(ifTrue->getType()); |
| 1294 | auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse); |
| 1295 | ::basicBlock->appendInst(select); |
| 1296 | |
| 1297 | return V(result); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1298 | } |
| 1299 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1300 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1301 | { |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1302 | auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch); |
| 1303 | ::basicBlock->appendInst(switchInst); |
| 1304 | |
| 1305 | return reinterpret_cast<SwitchCases*>(switchInst); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1306 | } |
| 1307 | |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1308 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1309 | { |
Nicolas Capens | b98fe5c | 2016-11-09 12:24:06 -0500 | [diff] [blame] | 1310 | switchCases->addBranch(label, label, branch); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | void Nucleus::createUnreachable() |
| 1314 | { |
Nicolas Capens | fdcca2d | 2016-10-20 11:31:36 -0400 | [diff] [blame] | 1315 | Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function); |
| 1316 | ::basicBlock->appendInst(unreachable); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1317 | } |
| 1318 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1319 | Type *Nucleus::getPointerType(Type *ElementType) |
| 1320 | { |
Nicolas Capens | e12780d | 2016-09-27 14:18:07 -0400 | [diff] [blame] | 1321 | if(sizeof(void*) == 8) |
| 1322 | { |
| 1323 | return T(Ice::IceType_i64); |
| 1324 | } |
| 1325 | else |
| 1326 | { |
| 1327 | return T(Ice::IceType_i32); |
| 1328 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1329 | } |
| 1330 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1331 | Value *Nucleus::createNullValue(Type *Ty) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1332 | { |
Nicolas Capens | 73dd7a2 | 2016-10-20 13:20:34 -0400 | [diff] [blame] | 1333 | if(Ice::isVectorType(T(Ty))) |
| 1334 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1335 | ASSERT(Ice::typeNumElements(T(Ty)) <= 16); |
Nicolas Capens | 30385f0 | 2017-04-18 13:03:47 -0400 | [diff] [blame] | 1336 | 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] | 1337 | return createConstantVector(c, Ty); |
| 1338 | } |
| 1339 | else |
| 1340 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1341 | return V(::context->getConstantZero(T(Ty))); |
Nicolas Capens | 73dd7a2 | 2016-10-20 13:20:34 -0400 | [diff] [blame] | 1342 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1343 | } |
| 1344 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1345 | Value *Nucleus::createConstantLong(int64_t i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1346 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1347 | return V(::context->getConstantInt64(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1348 | } |
| 1349 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1350 | Value *Nucleus::createConstantInt(int i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1351 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1352 | return V(::context->getConstantInt32(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1353 | } |
| 1354 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1355 | Value *Nucleus::createConstantInt(unsigned int i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1356 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1357 | return V(::context->getConstantInt32(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1358 | } |
| 1359 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1360 | Value *Nucleus::createConstantBool(bool b) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1361 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1362 | return V(::context->getConstantInt1(b)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1363 | } |
| 1364 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1365 | Value *Nucleus::createConstantByte(signed char i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1366 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1367 | return V(::context->getConstantInt8(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1368 | } |
| 1369 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1370 | Value *Nucleus::createConstantByte(unsigned char i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1371 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1372 | return V(::context->getConstantInt8(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1373 | } |
| 1374 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1375 | Value *Nucleus::createConstantShort(short i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1376 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1377 | return V(::context->getConstantInt16(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1378 | } |
| 1379 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1380 | Value *Nucleus::createConstantShort(unsigned short i) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1381 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1382 | return V(::context->getConstantInt16(i)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1385 | Value *Nucleus::createConstantFloat(float x) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1386 | { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1387 | return V(::context->getConstantFloat(x)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1388 | } |
| 1389 | |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1390 | Value *Nucleus::createNullPointer(Type *Ty) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1391 | { |
Nicolas Capens | a29d653 | 2016-12-05 21:38:09 -0500 | [diff] [blame] | 1392 | return createNullValue(T(sizeof(void*) == 8 ? Ice::IceType_i64 : Ice::IceType_i32)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1393 | } |
| 1394 | |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1395 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1396 | { |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1397 | const int vectorSize = 16; |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1398 | ASSERT(Ice::typeWidthInBytes(T(type)) == vectorSize); |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1399 | const int alignment = vectorSize; |
| 1400 | auto globalPool = ::function->getGlobalPool(); |
| 1401 | |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1402 | const int64_t *i = constants; |
| 1403 | const double *f = reinterpret_cast<const double*>(constants); |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1404 | Ice::VariableDeclaration::DataInitializer *dataInitializer = nullptr; |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1405 | |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1406 | switch((int)reinterpret_cast<intptr_t>(type)) |
| 1407 | { |
| 1408 | case Ice::IceType_v4i32: |
Nicolas Capens | a4c30b0 | 2016-11-08 15:43:17 -0500 | [diff] [blame] | 1409 | case Ice::IceType_v4i1: |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1410 | { |
| 1411 | const int initializer[4] = {(int)i[0], (int)i[1], (int)i[2], (int)i[3]}; |
| 1412 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1413 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1414 | } |
| 1415 | break; |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1416 | case Ice::IceType_v4f32: |
| 1417 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1418 | 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] | 1419 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1420 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1421 | } |
| 1422 | break; |
| 1423 | case Ice::IceType_v8i16: |
Nicolas Capens | a4c30b0 | 2016-11-08 15:43:17 -0500 | [diff] [blame] | 1424 | case Ice::IceType_v8i1: |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1425 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1426 | 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] | 1427 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1428 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1429 | } |
| 1430 | break; |
| 1431 | case Ice::IceType_v16i8: |
Nicolas Capens | a4c30b0 | 2016-11-08 15:43:17 -0500 | [diff] [blame] | 1432 | case Ice::IceType_v16i1: |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1433 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1434 | 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] | 1435 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1436 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1437 | } |
| 1438 | break; |
| 1439 | case Type_v2i32: |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1440 | { |
| 1441 | const int initializer[4] = {(int)i[0], (int)i[1], (int)i[0], (int)i[1]}; |
| 1442 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1443 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1444 | } |
| 1445 | break; |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1446 | case Type_v2f32: |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1447 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1448 | 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] | 1449 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1450 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1451 | } |
| 1452 | break; |
| 1453 | case Type_v4i16: |
| 1454 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1455 | 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] | 1456 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1457 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1458 | } |
| 1459 | break; |
| 1460 | case Type_v8i8: |
| 1461 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1462 | 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] | 1463 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1464 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1465 | } |
| 1466 | break; |
| 1467 | case Type_v4i8: |
| 1468 | { |
Nicolas Capens | 7f3f69c | 2016-10-20 01:29:33 -0400 | [diff] [blame] | 1469 | 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] | 1470 | static_assert(sizeof(initializer) == vectorSize, "!"); |
| 1471 | dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize); |
| 1472 | } |
| 1473 | break; |
| 1474 | default: |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1475 | UNREACHABLE("Unknown constant vector type: %d", (int)reinterpret_cast<intptr_t>(type)); |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | auto name = Ice::GlobalString::createWithoutString(::context); |
| 1479 | auto *variableDeclaration = Ice::VariableDeclaration::create(globalPool); |
| 1480 | variableDeclaration->setName(name); |
| 1481 | variableDeclaration->setAlignment(alignment); |
| 1482 | variableDeclaration->setIsConstant(true); |
| 1483 | variableDeclaration->addInitializer(dataInitializer); |
Nicolas Capens | 87852e1 | 2016-11-24 14:45:06 -0500 | [diff] [blame] | 1484 | |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1485 | ::function->addGlobal(variableDeclaration); |
| 1486 | |
| 1487 | constexpr int32_t offset = 0; |
| 1488 | Ice::Operand *ptr = ::context->getConstantSym(offset, name); |
| 1489 | |
| 1490 | Ice::Variable *result = ::function->makeVariable(T(type)); |
| 1491 | auto load = Ice::InstLoad::create(::function, result, ptr, alignment); |
| 1492 | ::basicBlock->appendInst(load); |
| 1493 | |
| 1494 | return V(result); |
Nicolas Capens | 13ac232 | 2016-10-13 14:52:12 -0400 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1498 | { |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 1499 | return createConstantVector((const int64_t*)constants, type); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | Type *Void::getType() |
| 1503 | { |
| 1504 | return T(Ice::IceType_void); |
| 1505 | } |
| 1506 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1507 | Type *Bool::getType() |
| 1508 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1509 | return T(Ice::IceType_i1); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1510 | } |
| 1511 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1512 | Type *Byte::getType() |
| 1513 | { |
Nicolas Capens | 6d73871 | 2016-09-30 04:15:22 -0400 | [diff] [blame] | 1514 | return T(Ice::IceType_i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1515 | } |
| 1516 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1517 | Type *SByte::getType() |
| 1518 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1519 | return T(Ice::IceType_i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1520 | } |
| 1521 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1522 | Type *Short::getType() |
| 1523 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1524 | return T(Ice::IceType_i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1525 | } |
| 1526 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1527 | Type *UShort::getType() |
| 1528 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1529 | return T(Ice::IceType_i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | Type *Byte4::getType() |
| 1533 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1534 | return T(Type_v4i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | Type *SByte4::getType() |
| 1538 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1539 | return T(Type_v4i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1540 | } |
| 1541 | |
Nicolas Capens | b6d4ce3 | 2019-03-12 23:00:24 -0400 | [diff] [blame] | 1542 | namespace |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1543 | { |
Nicolas Capens | b6d4ce3 | 2019-03-12 23:00:24 -0400 | [diff] [blame] | 1544 | RValue<Byte> SaturateUnsigned(RValue<Short> x) |
| 1545 | { |
| 1546 | return Byte(IfThenElse(Int(x) > 0xFF, Int(0xFF), IfThenElse(Int(x) < 0, Int(0), Int(x)))); |
| 1547 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1548 | |
Nicolas Capens | b6d4ce3 | 2019-03-12 23:00:24 -0400 | [diff] [blame] | 1549 | RValue<Byte> Extract(RValue<Byte8> val, int i) |
| 1550 | { |
| 1551 | return RValue<Byte>(Nucleus::createExtractElement(val.value, Byte::getType(), i)); |
| 1552 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1553 | |
Nicolas Capens | b6d4ce3 | 2019-03-12 23:00:24 -0400 | [diff] [blame] | 1554 | RValue<Byte8> Insert(RValue<Byte8> val, RValue<Byte> element, int i) |
| 1555 | { |
| 1556 | return RValue<Byte8>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 1557 | } |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1558 | } |
| 1559 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1560 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 1561 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1562 | if(emulateIntrinsics) |
| 1563 | { |
| 1564 | Byte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1565 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 1566 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 1567 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 1568 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 1569 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 1570 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 1571 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 1572 | 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] | 1573 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1574 | return result; |
| 1575 | } |
| 1576 | else |
| 1577 | { |
| 1578 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 1579 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1580 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1581 | auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1582 | paddusb->addArg(x.value); |
| 1583 | paddusb->addArg(y.value); |
| 1584 | ::basicBlock->appendInst(paddusb); |
| 1585 | |
| 1586 | return RValue<Byte8>(V(result)); |
| 1587 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 1591 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1592 | if(emulateIntrinsics) |
| 1593 | { |
| 1594 | Byte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1595 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 1596 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 1597 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 1598 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 1599 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 1600 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 1601 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 1602 | 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] | 1603 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1604 | return result; |
| 1605 | } |
| 1606 | else |
| 1607 | { |
| 1608 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 1609 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1610 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1611 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1612 | psubusw->addArg(x.value); |
| 1613 | psubusw->addArg(y.value); |
| 1614 | ::basicBlock->appendInst(psubusw); |
| 1615 | |
| 1616 | return RValue<Byte8>(V(result)); |
| 1617 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1618 | } |
| 1619 | |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1620 | RValue<SByte> Extract(RValue<SByte8> val, int i) |
| 1621 | { |
| 1622 | return RValue<SByte>(Nucleus::createExtractElement(val.value, SByte::getType(), i)); |
| 1623 | } |
| 1624 | |
| 1625 | RValue<SByte8> Insert(RValue<SByte8> val, RValue<SByte> element, int i) |
| 1626 | { |
| 1627 | return RValue<SByte8>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 1628 | } |
| 1629 | |
| 1630 | RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 1631 | { |
| 1632 | if(emulateIntrinsics) |
| 1633 | { |
| 1634 | SByte8 result; |
| 1635 | result = Insert(result, Extract(lhs, 0) >> SByte(rhs), 0); |
| 1636 | result = Insert(result, Extract(lhs, 1) >> SByte(rhs), 1); |
| 1637 | result = Insert(result, Extract(lhs, 2) >> SByte(rhs), 2); |
| 1638 | result = Insert(result, Extract(lhs, 3) >> SByte(rhs), 3); |
| 1639 | result = Insert(result, Extract(lhs, 4) >> SByte(rhs), 4); |
| 1640 | result = Insert(result, Extract(lhs, 5) >> SByte(rhs), 5); |
| 1641 | result = Insert(result, Extract(lhs, 6) >> SByte(rhs), 6); |
| 1642 | result = Insert(result, Extract(lhs, 7) >> SByte(rhs), 7); |
| 1643 | |
| 1644 | return result; |
| 1645 | } |
| 1646 | else |
| 1647 | { |
| 1648 | #if defined(__i386__) || defined(__x86_64__) |
| 1649 | // 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] | 1650 | RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u); |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1651 | RValue<Short4> lo = As<Short4>(As<UShort4>((As<Short4>(lhs) << 8) >> rhs) >> 8); |
| 1652 | |
| 1653 | return As<SByte8>(hi | lo); |
| 1654 | #else |
| 1655 | return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 1656 | #endif |
| 1657 | } |
| 1658 | } |
| 1659 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1660 | RValue<Int> SignMask(RValue<Byte8> x) |
| 1661 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 1662 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1663 | { |
| 1664 | Byte8 xx = As<Byte8>(As<SByte8>(x) >> 7) & Byte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 1665 | 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)); |
| 1666 | } |
| 1667 | else |
| 1668 | { |
| 1669 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 1670 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1671 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1672 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 1673 | movmsk->addArg(x.value); |
| 1674 | ::basicBlock->appendInst(movmsk); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 1675 | |
Nicolas Capens | 0f70a7f | 2017-07-26 13:50:04 -0400 | [diff] [blame] | 1676 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1677 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
| 1681 | // { |
Nicolas Capens | 2f970b6 | 2016-11-08 14:28:59 -0500 | [diff] [blame] | 1682 | // return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1683 | // } |
| 1684 | |
| 1685 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 1686 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 1687 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | Type *Byte8::getType() |
| 1691 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1692 | return T(Type_v8i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1693 | } |
| 1694 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1695 | // RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs) |
| 1696 | // { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1697 | // return RValue<SByte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1698 | // } |
| 1699 | |
| 1700 | // RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 1701 | // { |
Nicolas Capens | 15060bb | 2016-12-05 22:17:19 -0500 | [diff] [blame] | 1702 | // return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1703 | // } |
| 1704 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1705 | RValue<SByte> SaturateSigned(RValue<Short> x) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1706 | { |
| 1707 | return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x)))); |
| 1708 | } |
| 1709 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1710 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 1711 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1712 | if(emulateIntrinsics) |
| 1713 | { |
| 1714 | SByte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1715 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 1716 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 1717 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 1718 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 1719 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 1720 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 1721 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 1722 | 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] | 1723 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1724 | return result; |
| 1725 | } |
| 1726 | else |
| 1727 | { |
| 1728 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 1729 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1730 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1731 | auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1732 | paddsb->addArg(x.value); |
| 1733 | paddsb->addArg(y.value); |
| 1734 | ::basicBlock->appendInst(paddsb); |
| 1735 | |
| 1736 | return RValue<SByte8>(V(result)); |
| 1737 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 1741 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1742 | if(emulateIntrinsics) |
| 1743 | { |
| 1744 | SByte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1745 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 1746 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 1747 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 1748 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 1749 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 1750 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 1751 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 1752 | 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] | 1753 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1754 | return result; |
| 1755 | } |
| 1756 | else |
| 1757 | { |
| 1758 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 1759 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1760 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1761 | auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1762 | psubsb->addArg(x.value); |
| 1763 | psubsb->addArg(y.value); |
| 1764 | ::basicBlock->appendInst(psubsb); |
| 1765 | |
| 1766 | return RValue<SByte8>(V(result)); |
| 1767 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1768 | } |
| 1769 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1770 | RValue<Int> SignMask(RValue<SByte8> x) |
| 1771 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 1772 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1773 | { |
| 1774 | SByte8 xx = (x >> 7) & SByte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 1775 | 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)); |
| 1776 | } |
| 1777 | else |
| 1778 | { |
| 1779 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 1780 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1781 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1782 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 1783 | movmsk->addArg(x.value); |
| 1784 | ::basicBlock->appendInst(movmsk); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 1785 | |
Nicolas Capens | 0f70a7f | 2017-07-26 13:50:04 -0400 | [diff] [blame] | 1786 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1787 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
| 1791 | { |
Nicolas Capens | 2f970b6 | 2016-11-08 14:28:59 -0500 | [diff] [blame] | 1792 | return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 1796 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 1797 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | Type *SByte8::getType() |
| 1801 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 1802 | return T(Type_v8i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1803 | } |
| 1804 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1805 | Type *Byte16::getType() |
| 1806 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1807 | return T(Ice::IceType_v16i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | Type *SByte16::getType() |
| 1811 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1812 | return T(Ice::IceType_v16i8); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1813 | } |
| 1814 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1815 | Type *Short2::getType() |
| 1816 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1817 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1818 | } |
| 1819 | |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1820 | Type *UShort2::getType() |
| 1821 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 1822 | return T(Type_v2i16); |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 1823 | } |
| 1824 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1825 | Short4::Short4(RValue<Int4> cast) |
| 1826 | { |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 1827 | int select[8] = {0, 2, 4, 6, 0, 2, 4, 6}; |
| 1828 | Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType()); |
| 1829 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 1830 | |
Nicolas Capens | bea4dce | 2017-07-24 16:54:44 -0400 | [diff] [blame] | 1831 | Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value; |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 1832 | Value *short4 = Nucleus::createBitCast(int2, Short4::getType()); |
| 1833 | |
| 1834 | storeValue(short4); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | // Short4::Short4(RValue<Float> cast) |
| 1838 | // { |
| 1839 | // } |
| 1840 | |
| 1841 | Short4::Short4(RValue<Float4> cast) |
| 1842 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 1843 | UNIMPLEMENTED("Short4::Short4(RValue<Float4> cast)"); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1844 | } |
| 1845 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1846 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 1847 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1848 | if(emulateIntrinsics) |
| 1849 | { |
| 1850 | Short4 result; |
| 1851 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 1852 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 1853 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 1854 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 1855 | |
| 1856 | return result; |
| 1857 | } |
| 1858 | else |
| 1859 | { |
| 1860 | return RValue<Short4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 1861 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 1865 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1866 | if(emulateIntrinsics) |
| 1867 | { |
| 1868 | Short4 result; |
| 1869 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 1870 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 1871 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 1872 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 1873 | |
| 1874 | return result; |
| 1875 | } |
| 1876 | else |
| 1877 | { |
| 1878 | return RValue<Short4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 1879 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1880 | } |
| 1881 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1882 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 1883 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 1884 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
| 1885 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value); |
| 1886 | ::basicBlock->appendInst(cmp); |
| 1887 | |
| 1888 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1889 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 1890 | ::basicBlock->appendInst(select); |
| 1891 | |
| 1892 | return RValue<Short4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 1896 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 1897 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
| 1898 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value); |
| 1899 | ::basicBlock->appendInst(cmp); |
| 1900 | |
| 1901 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1902 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 1903 | ::basicBlock->appendInst(select); |
| 1904 | |
| 1905 | return RValue<Short4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1906 | } |
| 1907 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1908 | RValue<Short> SaturateSigned(RValue<Int> x) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1909 | { |
| 1910 | return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x))); |
| 1911 | } |
| 1912 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1913 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 1914 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1915 | if(emulateIntrinsics) |
| 1916 | { |
| 1917 | Short4 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1918 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 1919 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 1920 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 1921 | 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] | 1922 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1923 | return result; |
| 1924 | } |
| 1925 | else |
| 1926 | { |
| 1927 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1928 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1929 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1930 | auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1931 | paddsw->addArg(x.value); |
| 1932 | paddsw->addArg(y.value); |
| 1933 | ::basicBlock->appendInst(paddsw); |
| 1934 | |
| 1935 | return RValue<Short4>(V(result)); |
| 1936 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 1940 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1941 | if(emulateIntrinsics) |
| 1942 | { |
| 1943 | Short4 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 1944 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 1945 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 1946 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 1947 | 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] | 1948 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1949 | return result; |
| 1950 | } |
| 1951 | else |
| 1952 | { |
| 1953 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1954 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1955 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1956 | auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1957 | psubsw->addArg(x.value); |
| 1958 | psubsw->addArg(y.value); |
| 1959 | ::basicBlock->appendInst(psubsw); |
| 1960 | |
| 1961 | return RValue<Short4>(V(result)); |
| 1962 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 1966 | { |
Nicolas Capens | 6c15744 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1967 | if(emulateIntrinsics) |
| 1968 | { |
| 1969 | Short4 result; |
| 1970 | result = Insert(result, Short((Int(Extract(x, 0)) * Int(Extract(y, 0))) >> 16), 0); |
| 1971 | result = Insert(result, Short((Int(Extract(x, 1)) * Int(Extract(y, 1))) >> 16), 1); |
| 1972 | result = Insert(result, Short((Int(Extract(x, 2)) * Int(Extract(y, 2))) >> 16), 2); |
| 1973 | 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] | 1974 | |
Nicolas Capens | 6c15744 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1975 | return result; |
| 1976 | } |
| 1977 | else |
| 1978 | { |
| 1979 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 1980 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 1981 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1982 | auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1983 | pmulhw->addArg(x.value); |
| 1984 | pmulhw->addArg(y.value); |
| 1985 | ::basicBlock->appendInst(pmulhw); |
| 1986 | |
| 1987 | return RValue<Short4>(V(result)); |
| 1988 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 1992 | { |
Nicolas Capens | afe27e9 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1993 | if(emulateIntrinsics) |
| 1994 | { |
| 1995 | Int2 result; |
| 1996 | result = Insert(result, Int(Extract(x, 0)) * Int(Extract(y, 0)) + Int(Extract(x, 1)) * Int(Extract(y, 1)), 0); |
| 1997 | 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] | 1998 | |
Nicolas Capens | afe27e9 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 1999 | return result; |
| 2000 | } |
| 2001 | else |
| 2002 | { |
| 2003 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2004 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2005 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2006 | auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2007 | pmaddwd->addArg(x.value); |
| 2008 | pmaddwd->addArg(y.value); |
| 2009 | ::basicBlock->appendInst(pmaddwd); |
| 2010 | |
| 2011 | return As<Int2>(V(result)); |
| 2012 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2013 | } |
| 2014 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2015 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2016 | { |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2017 | if(emulateIntrinsics) |
| 2018 | { |
| 2019 | SByte8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2020 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2021 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2022 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2023 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2024 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2025 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2026 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2027 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 2028 | |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2029 | return result; |
| 2030 | } |
| 2031 | else |
| 2032 | { |
| 2033 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 2034 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2035 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2036 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2037 | pack->addArg(x.value); |
| 2038 | pack->addArg(y.value); |
| 2039 | ::basicBlock->appendInst(pack); |
| 2040 | |
| 2041 | return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x88)); |
| 2042 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2043 | } |
| 2044 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2045 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2046 | { |
| 2047 | if(emulateIntrinsics) |
| 2048 | { |
| 2049 | Byte8 result; |
| 2050 | result = Insert(result, SaturateUnsigned(Extract(x, 0)), 0); |
| 2051 | result = Insert(result, SaturateUnsigned(Extract(x, 1)), 1); |
| 2052 | result = Insert(result, SaturateUnsigned(Extract(x, 2)), 2); |
| 2053 | result = Insert(result, SaturateUnsigned(Extract(x, 3)), 3); |
| 2054 | result = Insert(result, SaturateUnsigned(Extract(y, 0)), 4); |
| 2055 | result = Insert(result, SaturateUnsigned(Extract(y, 1)), 5); |
| 2056 | result = Insert(result, SaturateUnsigned(Extract(y, 2)), 6); |
| 2057 | result = Insert(result, SaturateUnsigned(Extract(y, 3)), 7); |
| 2058 | |
| 2059 | return result; |
| 2060 | } |
| 2061 | else |
| 2062 | { |
| 2063 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
| 2064 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2065 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2066 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2067 | pack->addArg(x.value); |
| 2068 | pack->addArg(y.value); |
| 2069 | ::basicBlock->appendInst(pack); |
| 2070 | |
| 2071 | return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x88)); |
| 2072 | } |
| 2073 | } |
| 2074 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2075 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
| 2076 | { |
Nicolas Capens | 2f970b6 | 2016-11-08 14:28:59 -0500 | [diff] [blame] | 2077 | return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 2081 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2082 | return RValue<Short4>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | Type *Short4::getType() |
| 2086 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 2087 | return T(Type_v4i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2088 | } |
| 2089 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2090 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2091 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2092 | if(saturate) |
| 2093 | { |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 2094 | if(CPUID::SSE4_1) |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2095 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2096 | // x86 produces 0x80000000 on 32-bit integer overflow/underflow. |
| 2097 | // PackUnsigned takes care of 0x0000 saturation. |
| 2098 | Int4 int4(Min(cast, Float4(0xFFFF))); |
| 2099 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
| 2100 | } |
| 2101 | else if(CPUID::ARM) |
| 2102 | { |
| 2103 | // ARM saturates the 32-bit integer result on overflow/undeflow. |
| 2104 | Int4 int4(cast); |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2105 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2106 | } |
| 2107 | else |
| 2108 | { |
| 2109 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
| 2110 | } |
| 2111 | } |
| 2112 | else |
| 2113 | { |
| 2114 | *this = Short4(Int4(cast)); |
| 2115 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2116 | } |
| 2117 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2118 | RValue<UShort> Extract(RValue<UShort4> val, int i) |
| 2119 | { |
| 2120 | return RValue<UShort>(Nucleus::createExtractElement(val.value, UShort::getType(), i)); |
| 2121 | } |
| 2122 | |
| 2123 | RValue<UShort4> Insert(RValue<UShort4> val, RValue<UShort> element, int i) |
| 2124 | { |
| 2125 | return RValue<UShort4>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 2126 | } |
| 2127 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2128 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2129 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2130 | if(emulateIntrinsics) |
| 2131 | { |
| 2132 | UShort4 result; |
| 2133 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2134 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2135 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2136 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2137 | |
| 2138 | return result; |
| 2139 | } |
| 2140 | else |
| 2141 | { |
| 2142 | return RValue<UShort4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2143 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2147 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2148 | if(emulateIntrinsics) |
| 2149 | { |
| 2150 | UShort4 result; |
| 2151 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2152 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2153 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2154 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2155 | |
| 2156 | return result; |
| 2157 | } |
| 2158 | else |
| 2159 | { |
| 2160 | return RValue<UShort4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2161 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2162 | } |
| 2163 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2164 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2165 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 2166 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
| 2167 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value); |
| 2168 | ::basicBlock->appendInst(cmp); |
| 2169 | |
| 2170 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2171 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 2172 | ::basicBlock->appendInst(select); |
| 2173 | |
| 2174 | return RValue<UShort4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2178 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 2179 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
| 2180 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value); |
| 2181 | ::basicBlock->appendInst(cmp); |
| 2182 | |
| 2183 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2184 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 2185 | ::basicBlock->appendInst(select); |
| 2186 | |
| 2187 | return RValue<UShort4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2188 | } |
| 2189 | |
Nicolas Capens | 7f30181 | 2017-10-02 17:32:34 -0400 | [diff] [blame] | 2190 | RValue<UShort> SaturateUnsigned(RValue<Int> x) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2191 | { |
| 2192 | return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x))); |
| 2193 | } |
| 2194 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2195 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2196 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2197 | if(emulateIntrinsics) |
| 2198 | { |
| 2199 | UShort4 result; |
Nicolas Capens | 7f30181 | 2017-10-02 17:32:34 -0400 | [diff] [blame] | 2200 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2201 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2202 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2203 | 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] | 2204 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2205 | return result; |
| 2206 | } |
| 2207 | else |
| 2208 | { |
| 2209 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2210 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2211 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2212 | auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2213 | paddusw->addArg(x.value); |
| 2214 | paddusw->addArg(y.value); |
| 2215 | ::basicBlock->appendInst(paddusw); |
| 2216 | |
| 2217 | return RValue<UShort4>(V(result)); |
| 2218 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2222 | { |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2223 | if(emulateIntrinsics) |
| 2224 | { |
| 2225 | UShort4 result; |
Nicolas Capens | 7f30181 | 2017-10-02 17:32:34 -0400 | [diff] [blame] | 2226 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2227 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2228 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2229 | 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] | 2230 | |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2231 | return result; |
| 2232 | } |
| 2233 | else |
| 2234 | { |
| 2235 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2236 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2237 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2238 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2239 | psubusw->addArg(x.value); |
| 2240 | psubusw->addArg(y.value); |
| 2241 | ::basicBlock->appendInst(psubusw); |
| 2242 | |
| 2243 | return RValue<UShort4>(V(result)); |
| 2244 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
| 2248 | { |
Nicolas Capens | 6c15744 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2249 | if(emulateIntrinsics) |
| 2250 | { |
| 2251 | UShort4 result; |
| 2252 | result = Insert(result, UShort((UInt(Extract(x, 0)) * UInt(Extract(y, 0))) >> 16), 0); |
| 2253 | result = Insert(result, UShort((UInt(Extract(x, 1)) * UInt(Extract(y, 1))) >> 16), 1); |
| 2254 | result = Insert(result, UShort((UInt(Extract(x, 2)) * UInt(Extract(y, 2))) >> 16), 2); |
| 2255 | 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] | 2256 | |
Nicolas Capens | 6c15744 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2257 | return result; |
| 2258 | } |
| 2259 | else |
| 2260 | { |
| 2261 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2262 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2263 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2264 | auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2265 | pmulhuw->addArg(x.value); |
| 2266 | pmulhuw->addArg(y.value); |
| 2267 | ::basicBlock->appendInst(pmulhuw); |
| 2268 | |
| 2269 | return RValue<UShort4>(V(result)); |
| 2270 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2271 | } |
| 2272 | |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2273 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 2274 | { |
| 2275 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2276 | |
| 2277 | // Scalarized implementation. |
| 2278 | Int4 result; |
| 2279 | result = Insert(result, Int((Long(Extract(x, 0)) * Long(Extract(y, 0))) >> Long(Int(32))), 0); |
| 2280 | result = Insert(result, Int((Long(Extract(x, 1)) * Long(Extract(y, 1))) >> Long(Int(32))), 1); |
| 2281 | result = Insert(result, Int((Long(Extract(x, 2)) * Long(Extract(y, 2))) >> Long(Int(32))), 2); |
| 2282 | result = Insert(result, Int((Long(Extract(x, 3)) * Long(Extract(y, 3))) >> Long(Int(32))), 3); |
| 2283 | |
| 2284 | return result; |
| 2285 | } |
| 2286 | |
| 2287 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 2288 | { |
| 2289 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2290 | |
| 2291 | if(false) // Partial product based implementation. |
| 2292 | { |
| 2293 | auto xh = x >> 16; |
| 2294 | auto yh = y >> 16; |
| 2295 | auto xl = x & UInt4(0x0000FFFF); |
| 2296 | auto yl = y & UInt4(0x0000FFFF); |
| 2297 | auto xlyh = xl * yh; |
| 2298 | auto xhyl = xh * yl; |
| 2299 | auto xlyhh = xlyh >> 16; |
| 2300 | auto xhylh = xhyl >> 16; |
| 2301 | auto xlyhl = xlyh & UInt4(0x0000FFFF); |
| 2302 | auto xhyll = xhyl & UInt4(0x0000FFFF); |
| 2303 | auto xlylh = (xl * yl) >> 16; |
| 2304 | auto oflow = (xlyhl + xhyll + xlylh) >> 16; |
| 2305 | |
| 2306 | return (xh * yh) + (xlyhh + xhylh) + oflow; |
| 2307 | } |
| 2308 | |
| 2309 | // Scalarized implementation. |
| 2310 | Int4 result; |
| 2311 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 0))) * Long(UInt(Extract(As<Int4>(y), 0)))) >> Long(Int(32))), 0); |
| 2312 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 1))) * Long(UInt(Extract(As<Int4>(y), 1)))) >> Long(Int(32))), 1); |
| 2313 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 2))) * Long(UInt(Extract(As<Int4>(y), 2)))) >> Long(Int(32))), 2); |
| 2314 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 3))) * Long(UInt(Extract(As<Int4>(y), 3)))) >> Long(Int(32))), 3); |
| 2315 | |
| 2316 | return As<UInt4>(result); |
| 2317 | } |
| 2318 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2319 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 2320 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2321 | UNIMPLEMENTED("RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)"); |
| 2322 | return UShort4(0); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2323 | } |
| 2324 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2325 | Type *UShort4::getType() |
| 2326 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 2327 | return T(Type_v4i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2328 | } |
| 2329 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2330 | RValue<Short> Extract(RValue<Short8> val, int i) |
| 2331 | { |
| 2332 | return RValue<Short>(Nucleus::createExtractElement(val.value, Short::getType(), i)); |
| 2333 | } |
| 2334 | |
| 2335 | RValue<Short8> Insert(RValue<Short8> val, RValue<Short> element, int i) |
| 2336 | { |
| 2337 | return RValue<Short8>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 2338 | } |
| 2339 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2340 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 2341 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2342 | if(emulateIntrinsics) |
| 2343 | { |
| 2344 | Short8 result; |
| 2345 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 2346 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 2347 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 2348 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 2349 | result = Insert(result, Extract(lhs, 4) << Short(rhs), 4); |
| 2350 | result = Insert(result, Extract(lhs, 5) << Short(rhs), 5); |
| 2351 | result = Insert(result, Extract(lhs, 6) << Short(rhs), 6); |
| 2352 | result = Insert(result, Extract(lhs, 7) << Short(rhs), 7); |
| 2353 | |
| 2354 | return result; |
| 2355 | } |
| 2356 | else |
| 2357 | { |
| 2358 | return RValue<Short8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2359 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
| 2363 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2364 | if(emulateIntrinsics) |
| 2365 | { |
| 2366 | Short8 result; |
| 2367 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 2368 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 2369 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 2370 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 2371 | result = Insert(result, Extract(lhs, 4) >> Short(rhs), 4); |
| 2372 | result = Insert(result, Extract(lhs, 5) >> Short(rhs), 5); |
| 2373 | result = Insert(result, Extract(lhs, 6) >> Short(rhs), 6); |
| 2374 | result = Insert(result, Extract(lhs, 7) >> Short(rhs), 7); |
| 2375 | |
| 2376 | return result; |
| 2377 | } |
| 2378 | else |
| 2379 | { |
| 2380 | return RValue<Short8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2381 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 2385 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2386 | UNIMPLEMENTED("RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)"); |
| 2387 | return Int4(0); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2388 | } |
| 2389 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2390 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
| 2391 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2392 | UNIMPLEMENTED("RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)"); |
| 2393 | return Short8(0); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | Type *Short8::getType() |
| 2397 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2398 | return T(Ice::IceType_v8i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2399 | } |
| 2400 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2401 | RValue<UShort> Extract(RValue<UShort8> val, int i) |
| 2402 | { |
| 2403 | return RValue<UShort>(Nucleus::createExtractElement(val.value, UShort::getType(), i)); |
| 2404 | } |
| 2405 | |
| 2406 | RValue<UShort8> Insert(RValue<UShort8> val, RValue<UShort> element, int i) |
| 2407 | { |
| 2408 | return RValue<UShort8>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 2409 | } |
| 2410 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2411 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 2412 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2413 | if(emulateIntrinsics) |
| 2414 | { |
| 2415 | UShort8 result; |
| 2416 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2417 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2418 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2419 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2420 | result = Insert(result, Extract(lhs, 4) << UShort(rhs), 4); |
| 2421 | result = Insert(result, Extract(lhs, 5) << UShort(rhs), 5); |
| 2422 | result = Insert(result, Extract(lhs, 6) << UShort(rhs), 6); |
| 2423 | result = Insert(result, Extract(lhs, 7) << UShort(rhs), 7); |
| 2424 | |
| 2425 | return result; |
| 2426 | } |
| 2427 | else |
| 2428 | { |
| 2429 | return RValue<UShort8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2430 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
| 2434 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2435 | if(emulateIntrinsics) |
| 2436 | { |
| 2437 | UShort8 result; |
| 2438 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2439 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2440 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2441 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2442 | result = Insert(result, Extract(lhs, 4) >> UShort(rhs), 4); |
| 2443 | result = Insert(result, Extract(lhs, 5) >> UShort(rhs), 5); |
| 2444 | result = Insert(result, Extract(lhs, 6) >> UShort(rhs), 6); |
| 2445 | result = Insert(result, Extract(lhs, 7) >> UShort(rhs), 7); |
| 2446 | |
| 2447 | return result; |
| 2448 | } |
| 2449 | else |
| 2450 | { |
| 2451 | return RValue<UShort8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2452 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2453 | } |
| 2454 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2455 | RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7) |
| 2456 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2457 | UNIMPLEMENTED("RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7)"); |
| 2458 | return UShort8(0); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 2462 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2463 | UNIMPLEMENTED("RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)"); |
| 2464 | return UShort8(0); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | // FIXME: Implement as Shuffle(x, y, Select(i0, ..., i16)) and Shuffle(x, y, SELECT_PACK_REPEAT(element)) |
| 2468 | // RValue<UShort8> PackRepeat(RValue<Byte16> x, RValue<Byte16> y, int element) |
| 2469 | // { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2470 | // ASSERT(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2471 | // } |
| 2472 | |
| 2473 | Type *UShort8::getType() |
| 2474 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2475 | return T(Ice::IceType_v8i16); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2476 | } |
| 2477 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2478 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2479 | { |
Nicolas Capens | 5b41ba3 | 2016-12-08 14:34:00 -0500 | [diff] [blame] | 2480 | RValue<Int> res = val; |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2481 | val += 1; |
| 2482 | return res; |
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 | const Int &operator++(Int &val) // Pre-increment |
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 | val += 1; |
| 2488 | return val; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2489 | } |
| 2490 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2491 | RValue<Int> operator--(Int &val, int) // Post-decrement |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2492 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2493 | RValue<Int> res = val; |
| 2494 | val -= 1; |
| 2495 | return res; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2496 | } |
| 2497 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2498 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2499 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2500 | val -= 1; |
| 2501 | return val; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2502 | } |
| 2503 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2504 | RValue<Int> RoundInt(RValue<Float> cast) |
| 2505 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2506 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 2507 | { |
| 2508 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 2509 | return Int((cast + Float(0x00C00000)) - Float(0x00C00000)); |
| 2510 | } |
| 2511 | else |
| 2512 | { |
| 2513 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 2514 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2515 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2516 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 2517 | nearbyint->addArg(cast.value); |
| 2518 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 2519 | |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 2520 | return RValue<Int>(V(result)); |
| 2521 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | Type *Int::getType() |
| 2525 | { |
| 2526 | return T(Ice::IceType_i32); |
| 2527 | } |
| 2528 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2529 | Type *Long::getType() |
| 2530 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2531 | return T(Ice::IceType_i64); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2532 | } |
| 2533 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2534 | UInt::UInt(RValue<Float> cast) |
| 2535 | { |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 2536 | // Smallest positive value representable in UInt, but not in Int |
| 2537 | const unsigned int ustart = 0x80000000u; |
| 2538 | const float ustartf = float(ustart); |
| 2539 | |
| 2540 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2541 | storeValue((~(As<Int>(cast) >> 31) & |
| 2542 | // Check if the value can be represented as an Int |
| 2543 | IfThenElse(cast >= ustartf, |
| 2544 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2545 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 2546 | // Otherwise, just convert normally |
| 2547 | Int(cast))).value); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2548 | } |
| 2549 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2550 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2551 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2552 | RValue<UInt> res = val; |
| 2553 | val += 1; |
| 2554 | return res; |
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 | const UInt &operator++(UInt &val) // Pre-increment |
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 | val += 1; |
| 2560 | return val; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2561 | } |
| 2562 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2563 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2564 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2565 | RValue<UInt> res = val; |
| 2566 | val -= 1; |
| 2567 | return res; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2568 | } |
| 2569 | |
Nicolas Capens | 96d4e09 | 2016-11-18 14:22:38 -0500 | [diff] [blame] | 2570 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2571 | { |
Nicolas Capens | d122940 | 2016-11-07 16:05:22 -0500 | [diff] [blame] | 2572 | val -= 1; |
| 2573 | return val; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2574 | } |
| 2575 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2576 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
| 2577 | // { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 2578 | // ASSERT(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2579 | // } |
| 2580 | |
| 2581 | Type *UInt::getType() |
| 2582 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2583 | return T(Ice::IceType_i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | // Int2::Int2(RValue<Int> cast) |
| 2587 | // { |
| 2588 | // Value *extend = Nucleus::createZExt(cast.value, Long::getType()); |
| 2589 | // Value *vector = Nucleus::createBitCast(extend, Int2::getType()); |
| 2590 | // |
| 2591 | // Constant *shuffle[2]; |
| 2592 | // shuffle[0] = Nucleus::createConstantInt(0); |
| 2593 | // shuffle[1] = Nucleus::createConstantInt(0); |
| 2594 | // |
| 2595 | // Value *replicate = Nucleus::createShuffleVector(vector, UndefValue::get(Int2::getType()), Nucleus::createConstantVector(shuffle, 2)); |
| 2596 | // |
| 2597 | // storeValue(replicate); |
| 2598 | // } |
| 2599 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2600 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 2601 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2602 | if(emulateIntrinsics) |
| 2603 | { |
| 2604 | Int2 result; |
| 2605 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 2606 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 2607 | |
| 2608 | return result; |
| 2609 | } |
| 2610 | else |
| 2611 | { |
| 2612 | return RValue<Int2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2613 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 2617 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2618 | if(emulateIntrinsics) |
| 2619 | { |
| 2620 | Int2 result; |
| 2621 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 2622 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 2623 | |
| 2624 | return result; |
| 2625 | } |
| 2626 | else |
| 2627 | { |
| 2628 | return RValue<Int2>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2629 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2630 | } |
| 2631 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2632 | Type *Int2::getType() |
| 2633 | { |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 2634 | return T(Type_v2i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2635 | } |
| 2636 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2637 | RValue<UInt> Extract(RValue<UInt2> val, int i) |
| 2638 | { |
| 2639 | return RValue<UInt>(Nucleus::createExtractElement(val.value, UInt::getType(), i)); |
| 2640 | } |
| 2641 | |
| 2642 | RValue<UInt2> Insert(RValue<UInt2> val, RValue<UInt> element, int i) |
| 2643 | { |
| 2644 | return RValue<UInt2>(Nucleus::createInsertElement(val.value, element.value, i)); |
| 2645 | } |
| 2646 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2647 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
| 2648 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2649 | if(emulateIntrinsics) |
| 2650 | { |
| 2651 | UInt2 result; |
| 2652 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 2653 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 2654 | |
| 2655 | return result; |
| 2656 | } |
| 2657 | else |
| 2658 | { |
| 2659 | return RValue<UInt2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2660 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 2664 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2665 | if(emulateIntrinsics) |
| 2666 | { |
| 2667 | UInt2 result; |
| 2668 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 2669 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 2670 | |
| 2671 | return result; |
| 2672 | } |
| 2673 | else |
| 2674 | { |
| 2675 | return RValue<UInt2>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2676 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2677 | } |
| 2678 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2679 | Type *UInt2::getType() |
| 2680 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 2681 | return T(Type_v2i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2682 | } |
| 2683 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2684 | Int4::Int4(RValue<Byte4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2685 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2686 | Value *x = Nucleus::createBitCast(cast.value, Int::getType()); |
| 2687 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 2688 | |
| 2689 | Value *e; |
| 2690 | int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}; |
| 2691 | Value *b = Nucleus::createBitCast(a, Byte16::getType()); |
| 2692 | Value *c = Nucleus::createShuffleVector(b, V(Nucleus::createNullValue(Byte16::getType())), swizzle); |
| 2693 | |
| 2694 | int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
| 2695 | Value *d = Nucleus::createBitCast(c, Short8::getType()); |
| 2696 | e = Nucleus::createShuffleVector(d, V(Nucleus::createNullValue(Short8::getType())), swizzle2); |
| 2697 | |
| 2698 | Value *f = Nucleus::createBitCast(e, Int4::getType()); |
| 2699 | storeValue(f); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2700 | } |
| 2701 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2702 | Int4::Int4(RValue<SByte4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2703 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2704 | Value *x = Nucleus::createBitCast(cast.value, Int::getType()); |
| 2705 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 2706 | |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2707 | int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}; |
| 2708 | Value *b = Nucleus::createBitCast(a, Byte16::getType()); |
| 2709 | Value *c = Nucleus::createShuffleVector(b, b, swizzle); |
| 2710 | |
| 2711 | int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
| 2712 | Value *d = Nucleus::createBitCast(c, Short8::getType()); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2713 | Value *e = Nucleus::createShuffleVector(d, d, swizzle2); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2714 | |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2715 | *this = As<Int4>(e) >> 24; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2716 | } |
| 2717 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2718 | Int4::Int4(RValue<Short4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2719 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2720 | int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3}; |
| 2721 | Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2722 | |
| 2723 | *this = As<Int4>(c) >> 16; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2724 | } |
| 2725 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2726 | Int4::Int4(RValue<UShort4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2727 | { |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2728 | int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11}; |
| 2729 | Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
| 2730 | Value *d = Nucleus::createBitCast(c, Int4::getType()); |
| 2731 | storeValue(d); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2732 | } |
| 2733 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2734 | Int4::Int4(RValue<Int> rhs) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2735 | { |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 2736 | Value *vector = Nucleus::createBitCast(rhs.value, Int4::getType()); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2737 | |
| 2738 | int swizzle[4] = {0, 0, 0, 0}; |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 2739 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2740 | |
| 2741 | storeValue(replicate); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2742 | } |
| 2743 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2744 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 2745 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2746 | if(emulateIntrinsics) |
| 2747 | { |
| 2748 | Int4 result; |
| 2749 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 2750 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 2751 | result = Insert(result, Extract(lhs, 2) << Int(rhs), 2); |
| 2752 | result = Insert(result, Extract(lhs, 3) << Int(rhs), 3); |
| 2753 | |
| 2754 | return result; |
| 2755 | } |
| 2756 | else |
| 2757 | { |
| 2758 | return RValue<Int4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2759 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 2763 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2764 | if(emulateIntrinsics) |
| 2765 | { |
| 2766 | Int4 result; |
| 2767 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 2768 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 2769 | result = Insert(result, Extract(lhs, 2) >> Int(rhs), 2); |
| 2770 | result = Insert(result, Extract(lhs, 3) >> Int(rhs), 3); |
| 2771 | |
| 2772 | return result; |
| 2773 | } |
| 2774 | else |
| 2775 | { |
| 2776 | return RValue<Int4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2777 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2778 | } |
| 2779 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2780 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 2781 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2782 | return RValue<Int4>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 2786 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2787 | return RValue<Int4>(Nucleus::createICmpSLT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 2791 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2792 | return RValue<Int4>(Nucleus::createICmpSLE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 2796 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2797 | return RValue<Int4>(Nucleus::createICmpNE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 2801 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2802 | return RValue<Int4>(Nucleus::createICmpSGE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 2806 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2807 | return RValue<Int4>(Nucleus::createICmpSGT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 2811 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 2812 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
| 2813 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value); |
| 2814 | ::basicBlock->appendInst(cmp); |
| 2815 | |
| 2816 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 2817 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 2818 | ::basicBlock->appendInst(select); |
| 2819 | |
| 2820 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 2824 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 2825 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
| 2826 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value); |
| 2827 | ::basicBlock->appendInst(cmp); |
| 2828 | |
| 2829 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 2830 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 2831 | ::basicBlock->appendInst(select); |
| 2832 | |
| 2833 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 2837 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2838 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 2839 | { |
| 2840 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 2841 | return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000)); |
| 2842 | } |
| 2843 | else |
| 2844 | { |
| 2845 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 2846 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2847 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2848 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 2849 | nearbyint->addArg(cast.value); |
| 2850 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 2851 | |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 2852 | return RValue<Int4>(V(result)); |
| 2853 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2854 | } |
| 2855 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2856 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2857 | { |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2858 | if(emulateIntrinsics) |
| 2859 | { |
| 2860 | Short8 result; |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2861 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2862 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2863 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2864 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2865 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2866 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2867 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2868 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 2869 | |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2870 | return result; |
| 2871 | } |
| 2872 | else |
| 2873 | { |
| 2874 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2875 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2876 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2877 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2878 | pack->addArg(x.value); |
| 2879 | pack->addArg(y.value); |
| 2880 | ::basicBlock->appendInst(pack); |
| 2881 | |
| 2882 | return RValue<Short8>(V(result)); |
| 2883 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2884 | } |
| 2885 | |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2886 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 2887 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2888 | if(emulateIntrinsics || !(CPUID::SSE4_1 || CPUID::ARM)) |
| 2889 | { |
| 2890 | RValue<Int4> sx = As<Int4>(x); |
| 2891 | RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000); |
| 2892 | |
| 2893 | RValue<Int4> sy = As<Int4>(y); |
| 2894 | RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000); |
| 2895 | |
| 2896 | return As<UShort8>(PackSigned(bx, by) + Short8(0x8000u)); |
| 2897 | } |
| 2898 | else |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2899 | { |
| 2900 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
| 2901 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2902 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2903 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 2904 | pack->addArg(x.value); |
| 2905 | pack->addArg(y.value); |
| 2906 | ::basicBlock->appendInst(pack); |
| 2907 | |
| 2908 | return RValue<UShort8>(V(result)); |
| 2909 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 2910 | } |
| 2911 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2912 | RValue<Int> SignMask(RValue<Int4> x) |
| 2913 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 2914 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2915 | { |
| 2916 | Int4 xx = (x >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 2917 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 2918 | } |
| 2919 | else |
| 2920 | { |
| 2921 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 2922 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 2923 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2924 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 2925 | movmsk->addArg(x.value); |
| 2926 | ::basicBlock->appendInst(movmsk); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 2927 | |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2928 | return RValue<Int>(V(result)); |
| 2929 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2930 | } |
| 2931 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2932 | Type *Int4::getType() |
| 2933 | { |
Nicolas Capens | 23d99a4 | 2016-09-30 14:57:16 -0400 | [diff] [blame] | 2934 | return T(Ice::IceType_v4i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2935 | } |
| 2936 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 2937 | UInt4::UInt4(RValue<Float4> cast) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2938 | { |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 2939 | // Smallest positive value representable in UInt, but not in Int |
| 2940 | const unsigned int ustart = 0x80000000u; |
| 2941 | const float ustartf = float(ustart); |
| 2942 | |
| 2943 | // Check if the value can be represented as an Int |
| 2944 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 2945 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 2946 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
| 2947 | // Otherwise, just convert normally |
| 2948 | (~uiValue & Int4(cast)); |
| 2949 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 2950 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2951 | } |
| 2952 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2953 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 2954 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2955 | if(emulateIntrinsics) |
| 2956 | { |
| 2957 | UInt4 result; |
| 2958 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 2959 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 2960 | result = Insert(result, Extract(lhs, 2) << UInt(rhs), 2); |
| 2961 | result = Insert(result, Extract(lhs, 3) << UInt(rhs), 3); |
| 2962 | |
| 2963 | return result; |
| 2964 | } |
| 2965 | else |
| 2966 | { |
| 2967 | return RValue<UInt4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2968 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 2972 | { |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2973 | if(emulateIntrinsics) |
| 2974 | { |
| 2975 | UInt4 result; |
| 2976 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 2977 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 2978 | result = Insert(result, Extract(lhs, 2) >> UInt(rhs), 2); |
| 2979 | result = Insert(result, Extract(lhs, 3) >> UInt(rhs), 3); |
| 2980 | |
| 2981 | return result; |
| 2982 | } |
| 2983 | else |
| 2984 | { |
| 2985 | return RValue<UInt4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs)))); |
| 2986 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2987 | } |
| 2988 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2989 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 2990 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2991 | return RValue<UInt4>(Nucleus::createICmpEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 2995 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 2996 | return RValue<UInt4>(Nucleus::createICmpULT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3000 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3001 | return RValue<UInt4>(Nucleus::createICmpULE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3005 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3006 | return RValue<UInt4>(Nucleus::createICmpNE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3010 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3011 | return RValue<UInt4>(Nucleus::createICmpUGE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3015 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3016 | return RValue<UInt4>(Nucleus::createICmpUGT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3020 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3021 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
| 3022 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value); |
| 3023 | ::basicBlock->appendInst(cmp); |
| 3024 | |
| 3025 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3026 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 3027 | ::basicBlock->appendInst(select); |
| 3028 | |
| 3029 | return RValue<UInt4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3033 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3034 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
| 3035 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value); |
| 3036 | ::basicBlock->appendInst(cmp); |
| 3037 | |
| 3038 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3039 | auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value); |
| 3040 | ::basicBlock->appendInst(select); |
| 3041 | |
| 3042 | return RValue<UInt4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3043 | } |
| 3044 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3045 | Type *UInt4::getType() |
| 3046 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 3047 | return T(Ice::IceType_v4i32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3048 | } |
| 3049 | |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3050 | Type *Half::getType() |
| 3051 | { |
| 3052 | return T(Ice::IceType_i16); |
| 3053 | } |
Alexis Hetu | 734e257 | 2018-12-20 14:00:49 -0500 | [diff] [blame] | 3054 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3055 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 3056 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3057 | return 1.0f / x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
| 3061 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3062 | return Rcp_pp(Sqrt(x)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | RValue<Float> Sqrt(RValue<Float> x) |
| 3066 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3067 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32); |
| 3068 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3069 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3070 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3071 | sqrt->addArg(x.value); |
| 3072 | ::basicBlock->appendInst(sqrt); |
| 3073 | |
| 3074 | return RValue<Float>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | RValue<Float> Round(RValue<Float> x) |
| 3078 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3079 | return Float4(Round(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | RValue<Float> Trunc(RValue<Float> x) |
| 3083 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3084 | return Float4(Trunc(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | RValue<Float> Frac(RValue<Float> x) |
| 3088 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3089 | return Float4(Frac(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | RValue<Float> Floor(RValue<Float> x) |
| 3093 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3094 | return Float4(Floor(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | RValue<Float> Ceil(RValue<Float> x) |
| 3098 | { |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3099 | return Float4(Ceil(Float4(x))).x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | Type *Float::getType() |
| 3103 | { |
Nicolas Capens | 9709d4f | 2016-09-30 11:44:14 -0400 | [diff] [blame] | 3104 | return T(Ice::IceType_f32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3105 | } |
| 3106 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3107 | Type *Float2::getType() |
| 3108 | { |
Nicolas Capens | 4cfd457 | 2016-10-20 01:00:19 -0400 | [diff] [blame] | 3109 | return T(Type_v2f32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3110 | } |
| 3111 | |
Nicolas Capens | cb98676 | 2017-01-20 11:34:37 -0500 | [diff] [blame] | 3112 | Float4::Float4(RValue<Float> rhs) : XYZW(this) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3113 | { |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 3114 | Value *vector = Nucleus::createBitCast(rhs.value, Float4::getType()); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3115 | |
| 3116 | int swizzle[4] = {0, 0, 0, 0}; |
Nicolas Capens | f8beb4b | 2017-01-27 02:55:44 -0800 | [diff] [blame] | 3117 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3118 | |
| 3119 | storeValue(replicate); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3120 | } |
| 3121 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3122 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 3123 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3124 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | 5cdb91a | 2017-02-13 12:39:18 -0500 | [diff] [blame] | 3125 | 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] | 3126 | ::basicBlock->appendInst(cmp); |
| 3127 | |
| 3128 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | 5cdb91a | 2017-02-13 12:39:18 -0500 | [diff] [blame] | 3129 | auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3130 | ::basicBlock->appendInst(select); |
| 3131 | |
| 3132 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 3136 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3137 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | 5cdb91a | 2017-02-13 12:39:18 -0500 | [diff] [blame] | 3138 | 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] | 3139 | ::basicBlock->appendInst(cmp); |
| 3140 | |
| 3141 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | 5cdb91a | 2017-02-13 12:39:18 -0500 | [diff] [blame] | 3142 | auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3143 | ::basicBlock->appendInst(select); |
| 3144 | |
| 3145 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 3149 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3150 | return Float4(1.0f) / x; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 3154 | { |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3155 | return Rcp_pp(Sqrt(x)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 3159 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3160 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 9f737d3 | 2017-07-25 17:26:14 -0400 | [diff] [blame] | 3161 | { |
| 3162 | Float4 result; |
| 3163 | result.x = Sqrt(Float(Float4(x).x)); |
| 3164 | result.y = Sqrt(Float(Float4(x).y)); |
| 3165 | result.z = Sqrt(Float(Float4(x).z)); |
| 3166 | result.w = Sqrt(Float(Float4(x).w)); |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 3167 | |
Nicolas Capens | 9f737d3 | 2017-07-25 17:26:14 -0400 | [diff] [blame] | 3168 | return result; |
| 3169 | } |
| 3170 | else |
| 3171 | { |
| 3172 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3173 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3174 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3175 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3176 | sqrt->addArg(x.value); |
| 3177 | ::basicBlock->appendInst(sqrt); |
| 3178 | |
| 3179 | return RValue<Float4>(V(result)); |
| 3180 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3181 | } |
| 3182 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3183 | RValue<Int> SignMask(RValue<Float4> x) |
| 3184 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3185 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3186 | { |
| 3187 | Int4 xx = (As<Int4>(x) >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 3188 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 3189 | } |
| 3190 | else |
| 3191 | { |
| 3192 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
| 3193 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3194 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3195 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3196 | movmsk->addArg(x.value); |
| 3197 | ::basicBlock->appendInst(movmsk); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 3198 | |
Nicolas Capens | d6cacad | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3199 | return RValue<Int>(V(result)); |
| 3200 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
| 3204 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3205 | return RValue<Int4>(Nucleus::createFCmpOEQ(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
| 3209 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3210 | return RValue<Int4>(Nucleus::createFCmpOLT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
| 3214 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3215 | return RValue<Int4>(Nucleus::createFCmpOLE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3216 | } |
| 3217 | |
| 3218 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
| 3219 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3220 | return RValue<Int4>(Nucleus::createFCmpONE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
| 3224 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3225 | return RValue<Int4>(Nucleus::createFCmpOGE(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
| 3229 | { |
Nicolas Capens | 5e6ca09 | 2017-01-13 15:09:21 -0500 | [diff] [blame] | 3230 | return RValue<Int4>(Nucleus::createFCmpOGT(x.value, y.value)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3231 | } |
| 3232 | |
Ben Clayton | ec1aeb8 | 2019-03-04 19:33:27 +0000 | [diff] [blame] | 3233 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 3234 | { |
| 3235 | return RValue<Int4>(Nucleus::createFCmpUEQ(x.value, y.value)); |
| 3236 | } |
| 3237 | |
| 3238 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 3239 | { |
| 3240 | return RValue<Int4>(Nucleus::createFCmpULT(x.value, y.value)); |
| 3241 | } |
| 3242 | |
| 3243 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 3244 | { |
| 3245 | return RValue<Int4>(Nucleus::createFCmpULE(x.value, y.value)); |
| 3246 | } |
| 3247 | |
| 3248 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 3249 | { |
| 3250 | return RValue<Int4>(Nucleus::createFCmpUNE(x.value, y.value)); |
| 3251 | } |
| 3252 | |
| 3253 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 3254 | { |
| 3255 | return RValue<Int4>(Nucleus::createFCmpUGE(x.value, y.value)); |
| 3256 | } |
| 3257 | |
| 3258 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 3259 | { |
| 3260 | return RValue<Int4>(Nucleus::createFCmpUGT(x.value, y.value)); |
| 3261 | } |
| 3262 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3263 | RValue<Float4> Round(RValue<Float4> x) |
| 3264 | { |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3265 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 3266 | { |
| 3267 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3268 | return (x + Float4(0x00C00000)) - Float4(0x00C00000); |
| 3269 | } |
| 3270 | else if(CPUID::SSE4_1) |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3271 | { |
| 3272 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3273 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3274 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3275 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 3276 | round->addArg(x.value); |
| 3277 | round->addArg(::context->getConstantInt32(0)); |
| 3278 | ::basicBlock->appendInst(round); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3279 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3280 | return RValue<Float4>(V(result)); |
| 3281 | } |
| 3282 | else |
| 3283 | { |
| 3284 | return Float4(RoundInt(x)); |
| 3285 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | RValue<Float4> Trunc(RValue<Float4> x) |
| 3289 | { |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3290 | if(CPUID::SSE4_1) |
| 3291 | { |
| 3292 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3293 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3294 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3295 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 3296 | round->addArg(x.value); |
| 3297 | round->addArg(::context->getConstantInt32(3)); |
| 3298 | ::basicBlock->appendInst(round); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3299 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3300 | return RValue<Float4>(V(result)); |
| 3301 | } |
| 3302 | else |
| 3303 | { |
| 3304 | return Float4(Int4(x)); |
| 3305 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | RValue<Float4> Frac(RValue<Float4> x) |
| 3309 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3310 | Float4 frc; |
| 3311 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3312 | if(CPUID::SSE4_1) |
| 3313 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3314 | frc = x - Floor(x); |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3315 | } |
| 3316 | else |
| 3317 | { |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3318 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3319 | |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3320 | 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] | 3321 | } |
Nicolas Capens | b923042 | 2017-07-17 10:27:33 -0400 | [diff] [blame] | 3322 | |
| 3323 | // x - floor(x) can be 1.0 for very small negative x. |
| 3324 | // Clamp against the value just below 1.0. |
| 3325 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | RValue<Float4> Floor(RValue<Float4> x) |
| 3329 | { |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3330 | if(CPUID::SSE4_1) |
| 3331 | { |
| 3332 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3333 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3334 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3335 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 3336 | round->addArg(x.value); |
| 3337 | round->addArg(::context->getConstantInt32(1)); |
| 3338 | ::basicBlock->appendInst(round); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3339 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3340 | return RValue<Float4>(V(result)); |
| 3341 | } |
| 3342 | else |
| 3343 | { |
| 3344 | return x - Frac(x); |
| 3345 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | RValue<Float4> Ceil(RValue<Float4> x) |
| 3349 | { |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3350 | if(CPUID::SSE4_1) |
| 3351 | { |
| 3352 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
| 3353 | const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F}; |
| 3354 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3355 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 3356 | round->addArg(x.value); |
| 3357 | round->addArg(::context->getConstantInt32(2)); |
| 3358 | ::basicBlock->appendInst(round); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3359 | |
Nicolas Capens | 9ca48d5 | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 3360 | return RValue<Float4>(V(result)); |
| 3361 | } |
| 3362 | else |
| 3363 | { |
| 3364 | return -Floor(-x); |
| 3365 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | Type *Float4::getType() |
| 3369 | { |
Nicolas Capens | 9709d4f | 2016-09-30 11:44:14 -0400 | [diff] [blame] | 3370 | return T(Ice::IceType_v4f32); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3371 | } |
| 3372 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3373 | RValue<Long> Ticks() |
| 3374 | { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 3375 | UNIMPLEMENTED("RValue<Long> Ticks()"); |
| 3376 | return Long(Int(0)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3377 | } |
Ben Clayton | 147c491 | 2019-04-11 00:17:59 -0400 | [diff] [blame] | 3378 | |
| 3379 | // Below are functions currently unimplemented for the Subzero backend. |
| 3380 | // They are stubbed to satisfy the linker. |
Ben Clayton | 147c491 | 2019-04-11 00:17:59 -0400 | [diff] [blame] | 3381 | RValue<Float4> Sin(RValue<Float4> x) { UNIMPLEMENTED("Subzero Sin()"); return Float4(0); } |
| 3382 | RValue<Float4> Cos(RValue<Float4> x) { UNIMPLEMENTED("Subzero Cos()"); return Float4(0); } |
| 3383 | RValue<Float4> Tan(RValue<Float4> x) { UNIMPLEMENTED("Subzero Tan()"); return Float4(0); } |
| 3384 | RValue<Float4> Asin(RValue<Float4> x) { UNIMPLEMENTED("Subzero Asin()"); return Float4(0); } |
| 3385 | RValue<Float4> Acos(RValue<Float4> x) { UNIMPLEMENTED("Subzero Acos()"); return Float4(0); } |
| 3386 | RValue<Float4> Atan(RValue<Float4> x) { UNIMPLEMENTED("Subzero Atan()"); return Float4(0); } |
| 3387 | RValue<Float4> Sinh(RValue<Float4> x) { UNIMPLEMENTED("Subzero Sinh()"); return Float4(0); } |
| 3388 | RValue<Float4> Cosh(RValue<Float4> x) { UNIMPLEMENTED("Subzero Cosh()"); return Float4(0); } |
| 3389 | RValue<Float4> Tanh(RValue<Float4> x) { UNIMPLEMENTED("Subzero Tanh()"); return Float4(0); } |
| 3390 | RValue<Float4> Asinh(RValue<Float4> x) { UNIMPLEMENTED("Subzero Asinh()"); return Float4(0); } |
| 3391 | RValue<Float4> Acosh(RValue<Float4> x) { UNIMPLEMENTED("Subzero Acosh()"); return Float4(0); } |
| 3392 | RValue<Float4> Atanh(RValue<Float4> x) { UNIMPLEMENTED("Subzero Atanh()"); return Float4(0); } |
| 3393 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) { UNIMPLEMENTED("Subzero Atan2()"); return Float4(0); } |
| 3394 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) { UNIMPLEMENTED("Subzero Pow()"); return Float4(0); } |
| 3395 | RValue<Float4> Exp(RValue<Float4> x) { UNIMPLEMENTED("Subzero Exp()"); return Float4(0); } |
| 3396 | RValue<Float4> Log(RValue<Float4> x) { UNIMPLEMENTED("Subzero Log()"); return Float4(0); } |
| 3397 | RValue<Float4> Exp2(RValue<Float4> x) { UNIMPLEMENTED("Subzero Exp2()"); return Float4(0); } |
| 3398 | RValue<Float4> Log2(RValue<Float4> x) { UNIMPLEMENTED("Subzero Log2()"); return Float4(0); } |
| 3399 | RValue<UInt4> Ctlz(RValue<UInt4> x, bool isZeroUndef) { UNIMPLEMENTED("Subzero Ctlz()"); return UInt4(0); } |
| 3400 | RValue<UInt4> Cttz(RValue<UInt4> x, bool isZeroUndef) { UNIMPLEMENTED("Subzero Cttz()"); return UInt4(0); } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3401 | } |