blob: 63d7bd6a716ce0b54dcf0f6468f259597afc2dba [file] [log] [blame]
Nicolas Capens598f8d82016-09-26 15:09:10 -04001// 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 Capens598f8d82016-09-26 15:09:10 -040015#include "Reactor.hpp"
Ben Claytoneb50d252019-04-15 13:50:01 -040016#include "Debug.hpp"
Nicolas Capens598f8d82016-09-26 15:09:10 -040017
Nicolas Capens2ae9d742016-11-24 14:43:05 -050018#include "Optimizer.hpp"
Nicolas Capens1a3ce872018-10-10 10:42:36 -040019#include "ExecutableMemory.hpp"
Nicolas Capensa062f322018-09-06 15:34:46 -040020
Nicolas Capens598f8d82016-09-26 15:09:10 -040021#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 Capens8dfd9a72016-10-13 17:44:51 -040027#include "src/IceGlobalInits.h"
Nicolas Capens598f8d82016-09-26 15:09:10 -040028
29#include "llvm/Support/FileSystem.h"
30#include "llvm/Support/raw_os_ostream.h"
Nicolas Capens6a990f82018-07-06 15:54:07 -040031#include "llvm/Support/Compiler.h"
32
33#if __has_feature(memory_sanitizer)
34#include <sanitizer/msan_interface.h>
35#endif
Nicolas Capens598f8d82016-09-26 15:09:10 -040036
Nicolas Capensbd65da92017-01-05 16:31:06 -050037#if defined(_WIN32)
Alexis Hetu113e33a2017-01-19 10:49:19 -050038#ifndef WIN32_LEAN_AND_MEAN
Nicolas Capens598f8d82016-09-26 15:09:10 -040039#define WIN32_LEAN_AND_MEAN
Alexis Hetu113e33a2017-01-19 10:49:19 -050040#endif // !WIN32_LEAN_AND_MEAN
41#ifndef NOMINMAX
Nicolas Capens598f8d82016-09-26 15:09:10 -040042#define NOMINMAX
Alexis Hetu113e33a2017-01-19 10:49:19 -050043#endif // !NOMINMAX
Nicolas Capens598f8d82016-09-26 15:09:10 -040044#include <Windows.h>
Nicolas Capensbd65da92017-01-05 16:31:06 -050045#else
46#include <sys/mman.h>
Nicolas Capens411273e2017-01-26 15:13:36 -080047#if !defined(MAP_ANONYMOUS)
48#define MAP_ANONYMOUS MAP_ANON
Nicolas Capens8b275742017-01-20 17:11:41 -050049#endif
Nicolas Capensbd65da92017-01-05 16:31:06 -050050#endif
Nicolas Capens598f8d82016-09-26 15:09:10 -040051
Nicolas Capensc07dc4b2018-08-06 14:20:45 -040052#include <mutex>
Nicolas Capens598f8d82016-09-26 15:09:10 -040053#include <limits>
54#include <iostream>
Nicolas Capens598f8d82016-09-26 15:09:10 -040055
56namespace
57{
58 Ice::GlobalContext *context = nullptr;
59 Ice::Cfg *function = nullptr;
60 Ice::CfgNode *basicBlock = nullptr;
61 Ice::CfgLocalAllocatorScope *allocator = nullptr;
Nicolas Capens48461502018-08-06 14:20:45 -040062 rr::Routine *routine = nullptr;
Nicolas Capens598f8d82016-09-26 15:09:10 -040063
64 std::mutex codegenMutex;
65
66 Ice::ELFFileStreamer *elfFile = nullptr;
67 Ice::Fdstream *out = nullptr;
68}
69
Nicolas Capensccd5ecb2017-01-14 12:52:55 -050070namespace
71{
Nicolas Capens47dc8672017-04-25 12:54:39 -040072 #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 Capensccd5ecb2017-01-14 12:52:55 -050080 class CPUID
81 {
82 public:
Nicolas Capensf7b75882017-04-26 09:30:47 -040083 const static bool ARM;
Nicolas Capensccd5ecb2017-01-14 12:52:55 -050084 const static bool SSE4_1;
85
86 private:
87 static void cpuid(int registers[4], int info)
88 {
Nicolas Capens47dc8672017-04-25 12:54:39 -040089 #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 Capensccd5ecb2017-01-14 12:52:55 -050095 #else
Nicolas Capens47dc8672017-04-25 12:54:39 -040096 registers[0] = 0;
97 registers[1] = 0;
98 registers[2] = 0;
99 registers[3] = 0;
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500100 #endif
101 }
102
Nicolas Capensf7b75882017-04-26 09:30:47 -0400103 static bool detectARM()
104 {
Stephen Lanhamfe796492018-09-07 11:59:54 -0700105 #if defined(__arm__) || defined(__aarch64__)
Nicolas Capensf7b75882017-04-26 09:30:47 -0400106 return true;
107 #elif defined(__i386__) || defined(__x86_64__)
108 return false;
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +0200109 #elif defined(__mips__)
110 return false;
Nicolas Capensf7b75882017-04-26 09:30:47 -0400111 #else
112 #error "Unknown architecture"
113 #endif
114 }
115
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500116 static bool detectSSE4_1()
117 {
Nicolas Capens47dc8672017-04-25 12:54:39 -0400118 #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 Capensccd5ecb2017-01-14 12:52:55 -0500125 }
126 };
127
Nicolas Capensf7b75882017-04-26 09:30:47 -0400128 const bool CPUID::ARM = CPUID::detectARM();
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500129 const bool CPUID::SSE4_1 = CPUID::detectSSE4_1();
Nicolas Capens091f3502017-10-03 14:56:49 -0400130 const bool emulateIntrinsics = false;
Nicolas Capens2d8c3702017-07-25 13:56:46 -0400131 const bool emulateMismatchedBitCast = CPUID::ARM;
Nicolas Capensccd5ecb2017-01-14 12:52:55 -0500132}
133
Nicolas Capens48461502018-08-06 14:20:45 -0400134namespace rr
Nicolas Capens598f8d82016-09-26 15:09:10 -0400135{
Nicolas Capens23d99a42016-09-30 14:57:16 -0400136 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 Capens4cfd4572016-10-20 01:00:19 -0400149 Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2,
Nicolas Capens23d99a42016-09-30 14:57:16 -0400150 };
151
Nicolas Capens15060bb2016-12-05 22:17:19 -0500152 class Value : public Ice::Operand {};
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500153 class SwitchCases : public Ice::InstSwitch {};
Nicolas Capens598f8d82016-09-26 15:09:10 -0400154 class BasicBlock : public Ice::CfgNode {};
155
156 Ice::Type T(Type *t)
157 {
Alexis Hetu113e33a2017-01-19 10:49:19 -0500158 static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!");
Nicolas Capens23d99a42016-09-30 14:57:16 -0400159 return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400160 }
161
162 Type *T(Ice::Type t)
163 {
164 return reinterpret_cast<Type*>(t);
165 }
166
Nicolas Capens23d99a42016-09-30 14:57:16 -0400167 Type *T(EmulatedType t)
168 {
169 return reinterpret_cast<Type*>(t);
170 }
171
Nicolas Capens15060bb2016-12-05 22:17:19 -0500172 Value *V(Ice::Operand *v)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400173 {
174 return reinterpret_cast<Value*>(v);
175 }
176
Nicolas Capens611642a2016-09-28 16:45:04 -0400177 BasicBlock *B(Ice::CfgNode *b)
178 {
179 return reinterpret_cast<BasicBlock*>(b);
180 }
181
Nicolas Capens584088c2017-01-26 16:05:18 -0800182 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 Claytoneb50d252019-04-15 13:50:01 -0400194 default: ASSERT(false);
Nicolas Capens584088c2017-01-26 16:05:18 -0800195 }
196 }
197
198 return Ice::typeWidthInBytes(T(type));
199 }
200
Nicolas Capens598f8d82016-09-26 15:09:10 -0400201 Optimization optimization[10] = {InstructionCombining, Disabled};
202
Nicolas Capens66478362016-10-13 15:36:36 -0400203 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 Capens87852e12016-11-24 14:45:06 -0500210
Nicolas Capens66478362016-10-13 15:36:36 -0400211 inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index)
212 {
213 return &sectionHeader(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 Capens87852e12016-11-24 14:45:06 -0500219
Nicolas Capens66478362016-10-13 15:36:36 -0400220 uint32_t index = relocation.getSymbol();
221 int table = relocationTable.sh_link;
222 void *symbolValue = nullptr;
Nicolas Capens87852e12016-11-24 14:45:06 -0500223
Nicolas Capens66478362016-10-13 15:36:36 -0400224 if(index != SHN_UNDEF)
225 {
226 if(table == SHN_UNDEF) return nullptr;
227 const SectionHeader *symbolTable = elfSection(elfHeader, table);
Nicolas Capens87852e12016-11-24 14:45:06 -0500228
Nicolas Capens66478362016-10-13 15:36:36 -0400229 uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
230 if(index >= symtab_entries)
231 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400232 ASSERT(index < symtab_entries && "Symbol Index out of range");
Nicolas Capens66478362016-10-13 15:36:36 -0400233 return nullptr;
234 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500235
Nicolas Capens66478362016-10-13 15:36:36 -0400236 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 Capens8d2cf752018-11-22 11:13:45 -0500251 intptr_t address = (intptr_t)elfHeader + target->sh_offset;
252 unaligned_ptr<int32_t> patchSite = (int32_t*)(address + relocation.r_offset);
253
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400254 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 Claytoneb50d252019-04-15 13:50:01 -0400275 ASSERT(false && "Unsupported relocation type");
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400276 return nullptr;
277 }
278 }
279 else
280 {
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400281 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 Claytoneb50d252019-04-15 13:50:01 -0400293 ASSERT(false && "Unsupported relocation type");
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400294 return nullptr;
295 }
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400296 }
297
Nicolas Capens66478362016-10-13 15:36:36 -0400298 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 Capens87852e12016-11-24 14:45:06 -0500304
Nicolas Capens66478362016-10-13 15:36:36 -0400305 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 Capens87852e12016-11-24 14:45:06 -0500313
Nicolas Capens66478362016-10-13 15:36:36 -0400314 uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
315 if(index >= symtab_entries)
316 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400317 ASSERT(index < symtab_entries && "Symbol Index out of range");
Nicolas Capens66478362016-10-13 15:36:36 -0400318 return nullptr;
319 }
Nicolas Capens87852e12016-11-24 14:45:06 -0500320
Nicolas Capens66478362016-10-13 15:36:36 -0400321 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 Capens8d2cf752018-11-22 11:13:45 -0500336 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 Capensf110e4d2017-05-03 15:33:49 -0400340 switch(relocation.getType())
341 {
342 case R_X86_64_NONE:
343 // No relocation
344 break;
345 case R_X86_64_64:
Nicolas Capens8d2cf752018-11-22 11:13:45 -0500346 *patchSite64 = (int64_t)((intptr_t)symbolValue + *patchSite64 + relocation.r_addend);
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400347 break;
348 case R_X86_64_PC32:
Nicolas Capens8d2cf752018-11-22 11:13:45 -0500349 *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 - (intptr_t)patchSite32 + relocation.r_addend);
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400350 break;
351 case R_X86_64_32S:
Nicolas Capens8d2cf752018-11-22 11:13:45 -0500352 *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 + relocation.r_addend);
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400353 break;
354 default:
Ben Claytoneb50d252019-04-15 13:50:01 -0400355 ASSERT(false && "Unsupported relocation type");
Nicolas Capensf110e4d2017-05-03 15:33:49 -0400356 return nullptr;
357 }
Nicolas Capens66478362016-10-13 15:36:36 -0400358
359 return symbolValue;
360 }
361
Nicolas Capens1cc44382017-04-25 10:52:16 -0400362 void *loadImage(uint8_t *const elfImage, size_t &codeSize)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400363 {
Nicolas Capens598f8d82016-09-26 15:09:10 -0400364 ElfHeader *elfHeader = (ElfHeader*)elfImage;
365
366 if(!elfHeader->checkMagic())
367 {
368 return nullptr;
369 }
370
Nicolas Capens66478362016-10-13 15:36:36 -0400371 // Expect ELF bitness to match platform
Ben Claytoneb50d252019-04-15 13:50:01 -0400372 ASSERT(sizeof(void*) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32);
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400373 #if defined(__i386__)
Ben Claytoneb50d252019-04-15 13:50:01 -0400374 ASSERT(sizeof(void*) == 4 && elfHeader->e_machine == EM_386);
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400375 #elif defined(__x86_64__)
Ben Claytoneb50d252019-04-15 13:50:01 -0400376 ASSERT(sizeof(void*) == 8 && elfHeader->e_machine == EM_X86_64);
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400377 #elif defined(__arm__)
Ben Claytoneb50d252019-04-15 13:50:01 -0400378 ASSERT(sizeof(void*) == 4 && elfHeader->e_machine == EM_ARM);
Stephen Lanhamfe796492018-09-07 11:59:54 -0700379 #elif defined(__aarch64__)
Ben Claytoneb50d252019-04-15 13:50:01 -0400380 ASSERT(sizeof(void*) == 8 && elfHeader->e_machine == EM_AARCH64);
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +0200381 #elif defined(__mips__)
Ben Claytoneb50d252019-04-15 13:50:01 -0400382 ASSERT(sizeof(void*) == 4 && elfHeader->e_machine == EM_MIPS);
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400383 #else
384 #error "Unsupported platform"
385 #endif
Nicolas Capens66478362016-10-13 15:36:36 -0400386
Nicolas Capens598f8d82016-09-26 15:09:10 -0400387 SectionHeader *sectionHeader = (SectionHeader*)(elfImage + elfHeader->e_shoff);
388 void *entry = nullptr;
389
390 for(int i = 0; i < elfHeader->e_shnum; i++)
391 {
Nicolas Capens66478362016-10-13 15:36:36 -0400392 if(sectionHeader[i].sh_type == SHT_PROGBITS)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400393 {
Nicolas Capens66478362016-10-13 15:36:36 -0400394 if(sectionHeader[i].sh_flags & SHF_EXECINSTR)
395 {
396 entry = elfImage + sectionHeader[i].sh_offset;
Nicolas Capens1cc44382017-04-25 10:52:16 -0400397 codeSize = sectionHeader[i].sh_size;
Nicolas Capens66478362016-10-13 15:36:36 -0400398 }
399 }
400 else if(sectionHeader[i].sh_type == SHT_REL)
401 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400402 ASSERT(sizeof(void*) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code
Nicolas Capens66478362016-10-13 15:36:36 -0400403
Alexis Hetu113e33a2017-01-19 10:49:19 -0500404 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
Nicolas Capens66478362016-10-13 15:36:36 -0400405 {
406 const Elf32_Rel &relocation = ((const Elf32_Rel*)(elfImage + sectionHeader[i].sh_offset))[index];
Alexis Hetu113e33a2017-01-19 10:49:19 -0500407 relocateSymbol(elfHeader, relocation, sectionHeader[i]);
Nicolas Capens66478362016-10-13 15:36:36 -0400408 }
409 }
410 else if(sectionHeader[i].sh_type == SHT_RELA)
411 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400412 ASSERT(sizeof(void*) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code
Nicolas Capens66478362016-10-13 15:36:36 -0400413
Alexis Hetu113e33a2017-01-19 10:49:19 -0500414 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
Nicolas Capens66478362016-10-13 15:36:36 -0400415 {
416 const Elf64_Rela &relocation = ((const Elf64_Rela*)(elfImage + sectionHeader[i].sh_offset))[index];
Alexis Hetu113e33a2017-01-19 10:49:19 -0500417 relocateSymbol(elfHeader, relocation, sectionHeader[i]);
Nicolas Capens66478362016-10-13 15:36:36 -0400418 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400419 }
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 Capensc07dc4b2018-08-06 14:20:45 -0400436 return (T*)allocateExecutable(sizeof(T) * n);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400437 }
438
439 void deallocate(T *p, size_type n)
440 {
Nicolas Capensc07dc4b2018-08-06 14:20:45 -0400441 deallocateExecutable(p, sizeof(T) * n);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400442 }
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 Capens58274b52016-10-19 23:45:19 -0400451 ELFMemoryStreamer() : Routine(), entry(nullptr)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400452 {
453 position = 0;
454 buffer.reserve(0x1000);
455 }
456
Nicolas Capens81aa97b2017-06-27 17:08:08 -0400457 ~ELFMemoryStreamer() override
Nicolas Capens598f8d82016-09-26 15:09:10 -0400458 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500459 #if defined(_WIN32)
460 if(buffer.size() != 0)
461 {
462 DWORD exeProtection;
463 VirtualProtect(&buffer[0], buffer.size(), oldProtection, &exeProtection);
464 }
465 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400466 }
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 Claytoneb50d252019-04-15 13:50:01 -0400480 else ASSERT(false && "UNIMPLEMENTED");
Nicolas Capens598f8d82016-09-26 15:09:10 -0400481 }
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 Capens58274b52016-10-19 23:45:19 -0400497 if(!entry)
498 {
Nicolas Capensbd65da92017-01-05 16:31:06 -0500499 position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this
Nicolas Capens598f8d82016-09-26 15:09:10 -0400500
Nicolas Capens1cc44382017-04-25 10:52:16 -0400501 size_t codeSize = 0;
502 entry = loadImage(&buffer[0], codeSize);
503
504 #if defined(_WIN32)
Nicolas Capense745f5a2017-05-29 10:00:32 -0400505 VirtualProtect(&buffer[0], buffer.size(), PAGE_EXECUTE_READ, &oldProtection);
Nicolas Capens1cc44382017-04-25 10:52:16 -0400506 FlushInstructionCache(GetCurrentProcess(), NULL, 0);
507 #else
Nicolas Capense745f5a2017-05-29 10:00:32 -0400508 mprotect(&buffer[0], buffer.size(), PROT_READ | PROT_EXEC);
Nicolas Capens1cc44382017-04-25 10:52:16 -0400509 __builtin___clear_cache((char*)entry, (char*)entry + codeSize);
510 #endif
Nicolas Capens58274b52016-10-19 23:45:19 -0400511 }
512
513 return entry;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400514 }
515
516 private:
Nicolas Capens58274b52016-10-19 23:45:19 -0400517 void *entry;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400518 std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer;
519 std::size_t position;
Nicolas Capensbd65da92017-01-05 16:31:06 -0500520
521 #if defined(_WIN32)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400522 DWORD oldProtection;
Nicolas Capensbd65da92017-01-05 16:31:06 -0500523 #endif
Nicolas Capens598f8d82016-09-26 15:09:10 -0400524 };
525
526 Nucleus::Nucleus()
527 {
528 ::codegenMutex.lock(); // Reactor is currently not thread safe
529
Nicolas Capens66478362016-10-13 15:36:36 -0400530 Ice::ClFlags &Flags = Ice::ClFlags::Flags;
531 Ice::ClFlags::getParsedClFlags(Flags);
532
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400533 #if defined(__arm__)
534 Flags.setTargetArch(Ice::Target_ARM32);
535 Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm);
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +0200536 #elif defined(__mips__)
537 Flags.setTargetArch(Ice::Target_MIPS32);
538 Flags.setTargetInstructionSet(Ice::BaseInstructionSet);
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400539 #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 Capens66478362016-10-13 15:36:36 -0400543 Flags.setOutFileType(Ice::FT_Elf);
544 Flags.setOptLevel(Ice::Opt_2);
545 Flags.setApplicationBinaryInterface(Ice::ABI_Platform);
Nicolas Capens30cd7d42017-04-25 15:17:25 -0400546 Flags.setVerbose(false ? Ice::IceV_Most : Ice::IceV_None);
547 Flags.setDisableHybridAssembly(true);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400548
Nicolas Capens65047112016-11-07 13:01:07 -0500549 static llvm::raw_os_ostream cout(std::cout);
550 static llvm::raw_os_ostream cerr(std::cerr);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400551
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 Capens65047112016-11-07 13:01:07 -0500557 ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400558 }
559 else
560 {
561 ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer();
Nicolas Capens65047112016-11-07 13:01:07 -0500562 ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400563 ::routine = elfMemory;
564 }
565 }
566
567 Nucleus::~Nucleus()
568 {
Nicolas Capens619a8c52017-07-05 14:10:46 -0400569 delete ::routine;
570
Nicolas Capens598f8d82016-09-26 15:09:10 -0400571 delete ::allocator;
572 delete ::function;
573 delete ::context;
574
575 delete ::elfFile;
576 delete ::out;
577
578 ::codegenMutex.unlock();
579 }
580
Chris Forbes878d4b02019-01-21 10:48:35 -0800581 Routine *Nucleus::acquireRoutine(const char *name, bool runOptimizations)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400582 {
583 if(basicBlock->getInsts().empty() || basicBlock->getInsts().back().getKind() != Ice::Inst::Ret)
584 {
585 createRetVoid();
586 }
587
Chris Forbes878d4b02019-01-21 10:48:35 -0800588 ::function->setFunctionName(Ice::GlobalString::createWithString(::context, name));
Nicolas Capens598f8d82016-09-26 15:09:10 -0400589
Nicolas Capens2ae9d742016-11-24 14:43:05 -0500590 optimize();
591
Nicolas Capens598f8d82016-09-26 15:09:10 -0400592 ::function->translate();
Ben Claytoneb50d252019-04-15 13:50:01 -0400593 ASSERT(!::function->hasError());
Nicolas Capensde19f392016-10-19 10:29:49 -0400594
Nicolas Capens83a6bb92017-07-05 15:04:00 -0400595 auto globals = ::function->getGlobalInits();
Nicolas Capens66478362016-10-13 15:36:36 -0400596
597 if(globals && !globals->empty())
598 {
Nicolas Capens83a6bb92017-07-05 15:04:00 -0400599 ::context->getGlobals()->merge(globals.get());
Nicolas Capens66478362016-10-13 15:36:36 -0400600 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400601
602 ::context->emitFileHeader();
603 ::function->emitIAS();
604 auto assembler = ::function->releaseAssembler();
Nicolas Capens66478362016-10-13 15:36:36 -0400605 auto objectWriter = ::context->getObjectWriter();
606 assembler->alignFunction();
607 objectWriter->writeFunctionCode(::function->getFunctionName(), false, assembler.get());
608 ::context->lowerGlobals("last");
Nicolas Capens73dd7a22016-10-20 13:20:34 -0400609 ::context->lowerConstants();
Nicolas Capensb98fe5c2016-11-09 12:24:06 -0500610 ::context->lowerJumpTables();
Nicolas Capens66478362016-10-13 15:36:36 -0400611 objectWriter->setUndefinedSyms(::context->getConstantExternSyms());
612 objectWriter->writeNonUserSections();
Nicolas Capens598f8d82016-09-26 15:09:10 -0400613
Nicolas Capens619a8c52017-07-05 14:10:46 -0400614 Routine *handoffRoutine = ::routine;
615 ::routine = nullptr;
616
617 return handoffRoutine;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400618 }
619
620 void Nucleus::optimize()
621 {
Nicolas Capens48461502018-08-06 14:20:45 -0400622 rr::optimize(::function);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400623 }
624
625 Value *Nucleus::allocateStackVariable(Type *t, int arraySize)
626 {
627 Ice::Type type = T(t);
Nicolas Capensa8f98632016-10-20 11:25:55 -0400628 int typeSize = Ice::typeWidthInBytes(type);
629 int totalSize = typeSize * (arraySize ? arraySize : 1);
Nicolas Capense12780d2016-09-27 14:18:07 -0400630
Nicolas Capensa8f98632016-10-20 11:25:55 -0400631 auto bytes = Ice::ConstantInteger32::create(::context, type, totalSize);
Nicolas Capense12780d2016-09-27 14:18:07 -0400632 auto address = ::function->makeVariable(T(getPointerType(t)));
Nicolas Capensa8f98632016-10-20 11:25:55 -0400633 auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize);
Nicolas Capense12780d2016-09-27 14:18:07 -0400634 ::function->getEntryNode()->getInsts().push_front(alloca);
635
636 return V(address);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400637 }
638
639 BasicBlock *Nucleus::createBasicBlock()
640 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400641 return B(::function->makeNode());
Nicolas Capens598f8d82016-09-26 15:09:10 -0400642 }
643
644 BasicBlock *Nucleus::getInsertBlock()
645 {
Nicolas Capens611642a2016-09-28 16:45:04 -0400646 return B(::basicBlock);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400647 }
648
649 void Nucleus::setInsertBlock(BasicBlock *basicBlock)
650 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400651 // ASSERT(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator");
Nicolas Capens0192d152019-03-27 14:46:07 -0400652
653 Variable::materializeAll();
654
Nicolas Capens611642a2016-09-28 16:45:04 -0400655 ::basicBlock = basicBlock;
Nicolas Capens598f8d82016-09-26 15:09:10 -0400656 }
657
Nicolas Capens598f8d82016-09-26 15:09:10 -0400658 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 Capens0192d152019-03-27 14:46:07 -0400682 // 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 Capensfdcca2d2016-10-20 11:31:36 -0400687 Ice::InstRet *ret = Ice::InstRet::create(::function);
688 ::basicBlock->appendInst(ret);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400689 }
690
691 void Nucleus::createRet(Value *v)
692 {
Nicolas Capens0192d152019-03-27 14:46:07 -0400693 // 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 Capensfdcca2d2016-10-20 11:31:36 -0400698 Ice::InstRet *ret = Ice::InstRet::create(::function, v);
699 ::basicBlock->appendInst(ret);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400700 }
701
702 void Nucleus::createBr(BasicBlock *dest)
703 {
Nicolas Capens0192d152019-03-27 14:46:07 -0400704 Variable::materializeAll();
705
Nicolas Capens611642a2016-09-28 16:45:04 -0400706 auto br = Ice::InstBr::create(::function, dest);
707 ::basicBlock->appendInst(br);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400708 }
709
710 void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse)
711 {
Nicolas Capens0192d152019-03-27 14:46:07 -0400712 Variable::materializeAll();
713
Nicolas Capens611642a2016-09-28 16:45:04 -0400714 auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse);
715 ::basicBlock->appendInst(br);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400716 }
717
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800718 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 Capens7d9f76d2016-09-29 13:39:44 -0400735 static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs)
736 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400737 ASSERT(lhs->getType() == rhs->getType() || llvm::isa<Ice::Constant>(rhs));
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400738
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800739 bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op);
740
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400741 Ice::Variable *result = ::function->makeVariable(lhs->getType());
Nicolas Capensf8360ba2017-01-25 11:35:00 -0800742 Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs);
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400743 ::basicBlock->appendInst(arithmetic);
744
745 return V(result);
746 }
747
Nicolas Capens598f8d82016-09-26 15:09:10 -0400748 Value *Nucleus::createAdd(Value *lhs, Value *rhs)
749 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400750 return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400751 }
752
753 Value *Nucleus::createSub(Value *lhs, Value *rhs)
754 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400755 return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400756 }
757
758 Value *Nucleus::createMul(Value *lhs, Value *rhs)
759 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400760 return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400761 }
762
763 Value *Nucleus::createUDiv(Value *lhs, Value *rhs)
764 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400765 return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400766 }
767
768 Value *Nucleus::createSDiv(Value *lhs, Value *rhs)
769 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400770 return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400771 }
772
773 Value *Nucleus::createFAdd(Value *lhs, Value *rhs)
774 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400775 return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400776 }
777
778 Value *Nucleus::createFSub(Value *lhs, Value *rhs)
779 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400780 return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400781 }
782
783 Value *Nucleus::createFMul(Value *lhs, Value *rhs)
784 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400785 return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400786 }
787
788 Value *Nucleus::createFDiv(Value *lhs, Value *rhs)
789 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400790 return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400791 }
792
793 Value *Nucleus::createURem(Value *lhs, Value *rhs)
794 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400795 return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400796 }
797
798 Value *Nucleus::createSRem(Value *lhs, Value *rhs)
799 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400800 return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400801 }
802
803 Value *Nucleus::createFRem(Value *lhs, Value *rhs)
804 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400805 return createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400806 }
807
808 Value *Nucleus::createShl(Value *lhs, Value *rhs)
809 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400810 return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400811 }
812
813 Value *Nucleus::createLShr(Value *lhs, Value *rhs)
814 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400815 return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400816 }
817
818 Value *Nucleus::createAShr(Value *lhs, Value *rhs)
819 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400820 return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400821 }
822
823 Value *Nucleus::createAnd(Value *lhs, Value *rhs)
824 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400825 return createArithmetic(Ice::InstArithmetic::And, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400826 }
827
828 Value *Nucleus::createOr(Value *lhs, Value *rhs)
829 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400830 return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400831 }
832
833 Value *Nucleus::createXor(Value *lhs, Value *rhs)
834 {
Nicolas Capens7d9f76d2016-09-29 13:39:44 -0400835 return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400836 }
837
838 Value *Nucleus::createNeg(Value *v)
839 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500840 return createSub(createNullValue(T(v->getType())), v);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400841 }
842
843 Value *Nucleus::createFNeg(Value *v)
844 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500845 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 Capens15060bb2016-12-05 22:17:19 -0500848 V(::context->getConstantFloat(-0.0f));
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500849
850 return createFSub(negativeZero, v);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400851 }
852
853 Value *Nucleus::createNot(Value *v)
854 {
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500855 if(Ice::isScalarIntegerType(v->getType()))
856 {
Nicolas Capens15060bb2016-12-05 22:17:19 -0500857 return createXor(v, V(::context->getConstantInt(v->getType(), -1)));
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500858 }
859 else // Vector
860 {
Nicolas Capensf34d1ac2017-05-08 17:06:11 -0400861 int64_t c[16] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
Nicolas Capensc5c0c332016-11-08 11:37:01 -0500862 return createXor(v, createConstantVector(c, T(v->getType())));
863 }
Nicolas Capens598f8d82016-09-26 15:09:10 -0400864 }
865
Nicolas Capens86509d92019-03-21 13:23:50 -0400866 Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400867 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400868 ASSERT(!atomic); // Unimplemented
869 ASSERT(memoryOrder == std::memory_order_relaxed); // Unimplemented
Nicolas Capens86509d92019-03-21 13:23:50 -0400870
Nicolas Capens23d99a42016-09-30 14:57:16 -0400871 int valueType = (int)reinterpret_cast<intptr_t>(type);
872 Ice::Variable *result = ::function->makeVariable(T(type));
873
Nicolas Capensf4c4eca2017-10-03 14:26:07 -0400874 if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack.
Nicolas Capens23d99a42016-09-30 14:57:16 -0400875 {
Nicolas Capens070d9f42017-04-26 13:36:33 -0400876 if(emulateIntrinsics)
877 {
878 if(typeSize(type) == 4)
879 {
880 auto pointer = RValue<Pointer<Byte>>(ptr);
Nicolas Capens1894cfa2017-07-27 14:21:46 -0400881 Int x = *Pointer<Int>(pointer);
Nicolas Capens070d9f42017-04-26 13:36:33 -0400882
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 Capens1894cfa2017-07-27 14:21:46 -0400892 Int x = *Pointer<Int>(pointer);
Nicolas Capens070d9f42017-04-26 13:36:33 -0400893 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 Claytoneb50d252019-04-15 13:50:01 -0400902 else UNREACHABLE("typeSize(type): %d", int(typeSize(type)));
Nicolas Capens070d9f42017-04-26 13:36:33 -0400903 }
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 Capens23d99a42016-09-30 14:57:16 -0400913 }
914 else
915 {
916 auto load = Ice::InstLoad::create(::function, result, ptr, align);
917 ::basicBlock->appendInst(load);
918 }
919
920 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -0400921 }
922
Nicolas Capens86509d92019-03-21 13:23:50 -0400923 Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400924 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400925 ASSERT(!atomic); // Unimplemented
926 ASSERT(memoryOrder == std::memory_order_relaxed); // Unimplemented
Nicolas Capens86509d92019-03-21 13:23:50 -0400927
Nicolas Capens6a990f82018-07-06 15:54:07 -0400928 #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 Capens23d99a42016-09-30 14:57:16 -0400939 int valueType = (int)reinterpret_cast<intptr_t>(type);
940
Nicolas Capensf4c4eca2017-10-03 14:26:07 -0400941 if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack.
Nicolas Capens23d99a42016-09-30 14:57:16 -0400942 {
Nicolas Capens070d9f42017-04-26 13:36:33 -0400943 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 Claytoneb50d252019-04-15 13:50:01 -0400971 else UNREACHABLE("typeSize(type): %d", int(typeSize(type)));
Nicolas Capens070d9f42017-04-26 13:36:33 -0400972 }
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 Capens23d99a42016-09-30 14:57:16 -0400983 }
984 else
985 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400986 ASSERT(value->getType() == T(type));
Nicolas Capens23d99a42016-09-30 14:57:16 -0400987
988 auto store = Ice::InstStore::create(::function, value, ptr, align);
989 ::basicBlock->appendInst(store);
990 }
991
Nicolas Capens598f8d82016-09-26 15:09:10 -0400992 return value;
993 }
994
Nicolas Capensd294def2017-01-26 17:44:37 -0800995 Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
Nicolas Capens598f8d82016-09-26 15:09:10 -0400996 {
Ben Claytoneb50d252019-04-15 13:50:01 -0400997 ASSERT(index->getType() == Ice::IceType_i32);
Nicolas Capens8820f642016-09-30 04:42:43 -0400998
Nicolas Capens15060bb2016-12-05 22:17:19 -0500999 if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index))
1000 {
Nicolas Capens584088c2017-01-26 16:05:18 -08001001 int32_t offset = constant->getValue() * (int)typeSize(type);
Nicolas Capens15060bb2016-12-05 22:17:19 -05001002
1003 if(offset == 0)
1004 {
1005 return ptr;
1006 }
1007
1008 return createAdd(ptr, createConstantInt(offset));
1009 }
1010
Nicolas Capens8820f642016-09-30 04:42:43 -04001011 if(!Ice::isByteSizedType(T(type)))
1012 {
Nicolas Capens584088c2017-01-26 16:05:18 -08001013 index = createMul(index, createConstantInt((int)typeSize(type)));
Nicolas Capens8820f642016-09-30 04:42:43 -04001014 }
1015
1016 if(sizeof(void*) == 8)
1017 {
Nicolas Capensd294def2017-01-26 17:44:37 -08001018 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 Capens8820f642016-09-30 04:42:43 -04001026 }
1027
1028 return createAdd(ptr, index);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001029 }
1030
1031 Value *Nucleus::createAtomicAdd(Value *ptr, Value *value)
1032 {
Ben Claytoneb50d252019-04-15 13:50:01 -04001033 UNIMPLEMENTED("createAtomicAdd");
1034 return nullptr;
Nicolas Capens598f8d82016-09-26 15:09:10 -04001035 }
1036
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001037 static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType)
1038 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04001039 if(v->getType() == T(destType))
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001040 {
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 Capens598f8d82016-09-26 15:09:10 -04001051 Value *Nucleus::createTrunc(Value *v, Type *destType)
1052 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001053 return createCast(Ice::InstCast::Trunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001054 }
1055
1056 Value *Nucleus::createZExt(Value *v, Type *destType)
1057 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001058 return createCast(Ice::InstCast::Zext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001059 }
1060
1061 Value *Nucleus::createSExt(Value *v, Type *destType)
1062 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001063 return createCast(Ice::InstCast::Sext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001064 }
1065
1066 Value *Nucleus::createFPToSI(Value *v, Type *destType)
1067 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001068 return createCast(Ice::InstCast::Fptosi, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001069 }
1070
Nicolas Capens598f8d82016-09-26 15:09:10 -04001071 Value *Nucleus::createSIToFP(Value *v, Type *destType)
1072 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001073 return createCast(Ice::InstCast::Sitofp, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001074 }
1075
1076 Value *Nucleus::createFPTrunc(Value *v, Type *destType)
1077 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001078 return createCast(Ice::InstCast::Fptrunc, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001079 }
1080
1081 Value *Nucleus::createFPExt(Value *v, Type *destType)
1082 {
Nicolas Capensa0c2fc52016-09-30 05:04:21 -04001083 return createCast(Ice::InstCast::Fpext, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001084 }
1085
1086 Value *Nucleus::createBitCast(Value *v, Type *destType)
1087 {
Nicolas Capens2d8c3702017-07-25 13:56:46 -04001088 // 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 Capensa0c2fc52016-09-30 05:04:21 -04001107 return createCast(Ice::InstCast::Bitcast, v, destType);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001108 }
1109
Nicolas Capens43dc6292016-10-20 00:01:38 -04001110 static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001111 {
Ben Claytoneb50d252019-04-15 13:50:01 -04001112 ASSERT(lhs->getType() == rhs->getType());
Nicolas Capens611642a2016-09-28 16:45:04 -04001113
Nicolas Capens43dc6292016-10-20 00:01:38 -04001114 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 Capens611642a2016-09-28 16:45:04 -04001116 ::basicBlock->appendInst(cmp);
1117
1118 return V(result);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001119 }
1120
Nicolas Capens43dc6292016-10-20 00:01:38 -04001121 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 Capens598f8d82016-09-26 15:09:10 -04001166 Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs)
1167 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001168 return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs);
1169 }
1170
1171 static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs)
1172 {
Ben Claytoneb50d252019-04-15 13:50:01 -04001173 ASSERT(lhs->getType() == rhs->getType());
1174 ASSERT(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32);
Nicolas Capens43dc6292016-10-20 00:01:38 -04001175
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 Capens598f8d82016-09-26 15:09:10 -04001181 }
1182
1183 Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs)
1184 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001185 return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001186 }
1187
1188 Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs)
1189 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001190 return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001191 }
1192
1193 Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs)
1194 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001195 return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001196 }
1197
1198 Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs)
1199 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001200 return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001201 }
1202
1203 Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs)
1204 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001205 return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001206 }
1207
1208 Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs)
1209 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001210 return createFloatCompare(Ice::InstFcmp::One, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001211 }
1212
1213 Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs)
1214 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001215 return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001216 }
1217
1218 Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs)
1219 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001220 return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001221 }
1222
1223 Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs)
1224 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001225 return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001226 }
1227
1228 Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs)
1229 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001230 return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001231 }
1232
1233 Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs)
1234 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001235 return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001236 }
1237
1238 Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs)
1239 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001240 return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001241 }
1242
1243 Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs)
1244 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001245 return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001246 }
1247
1248 Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs)
1249 {
Nicolas Capens43dc6292016-10-20 00:01:38 -04001250 return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001251 }
1252
Nicolas Capense95d5342016-09-30 11:37:28 -04001253 Value *Nucleus::createExtractElement(Value *vector, Type *type, int index)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001254 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001255 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 Capens598f8d82016-09-26 15:09:10 -04001260 }
1261
1262 Value *Nucleus::createInsertElement(Value *vector, Value *element, int index)
1263 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04001264 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 Capens598f8d82016-09-26 15:09:10 -04001269 }
1270
Nicolas Capense89cd582016-09-30 14:23:47 -04001271 Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001272 {
Ben Claytoneb50d252019-04-15 13:50:01 -04001273 ASSERT(V1->getType() == V2->getType());
Nicolas Capens619c0ab2016-09-30 14:46:24 -04001274
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 Capens598f8d82016-09-26 15:09:10 -04001287 }
1288
1289 Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse)
1290 {
Ben Claytoneb50d252019-04-15 13:50:01 -04001291 ASSERT(ifTrue->getType() == ifFalse->getType());
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04001292
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 Capens598f8d82016-09-26 15:09:10 -04001298 }
1299
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001300 SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001301 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001302 auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch);
1303 ::basicBlock->appendInst(switchInst);
1304
1305 return reinterpret_cast<SwitchCases*>(switchInst);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001306 }
1307
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001308 void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001309 {
Nicolas Capensb98fe5c2016-11-09 12:24:06 -05001310 switchCases->addBranch(label, label, branch);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001311 }
1312
1313 void Nucleus::createUnreachable()
1314 {
Nicolas Capensfdcca2d2016-10-20 11:31:36 -04001315 Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function);
1316 ::basicBlock->appendInst(unreachable);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001317 }
1318
Nicolas Capens598f8d82016-09-26 15:09:10 -04001319 Type *Nucleus::getPointerType(Type *ElementType)
1320 {
Nicolas Capense12780d2016-09-27 14:18:07 -04001321 if(sizeof(void*) == 8)
1322 {
1323 return T(Ice::IceType_i64);
1324 }
1325 else
1326 {
1327 return T(Ice::IceType_i32);
1328 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001329 }
1330
Nicolas Capens13ac2322016-10-13 14:52:12 -04001331 Value *Nucleus::createNullValue(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001332 {
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001333 if(Ice::isVectorType(T(Ty)))
1334 {
Ben Claytoneb50d252019-04-15 13:50:01 -04001335 ASSERT(Ice::typeNumElements(T(Ty)) <= 16);
Nicolas Capens30385f02017-04-18 13:03:47 -04001336 int64_t c[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001337 return createConstantVector(c, Ty);
1338 }
1339 else
1340 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001341 return V(::context->getConstantZero(T(Ty)));
Nicolas Capens73dd7a22016-10-20 13:20:34 -04001342 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001343 }
1344
Nicolas Capens13ac2322016-10-13 14:52:12 -04001345 Value *Nucleus::createConstantLong(int64_t i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001346 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001347 return V(::context->getConstantInt64(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001348 }
1349
Nicolas Capens13ac2322016-10-13 14:52:12 -04001350 Value *Nucleus::createConstantInt(int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001351 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001352 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001353 }
1354
Nicolas Capens13ac2322016-10-13 14:52:12 -04001355 Value *Nucleus::createConstantInt(unsigned int i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001356 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001357 return V(::context->getConstantInt32(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001358 }
1359
Nicolas Capens13ac2322016-10-13 14:52:12 -04001360 Value *Nucleus::createConstantBool(bool b)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001361 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001362 return V(::context->getConstantInt1(b));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001363 }
1364
Nicolas Capens13ac2322016-10-13 14:52:12 -04001365 Value *Nucleus::createConstantByte(signed char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001366 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001367 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001368 }
1369
Nicolas Capens13ac2322016-10-13 14:52:12 -04001370 Value *Nucleus::createConstantByte(unsigned char i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001371 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001372 return V(::context->getConstantInt8(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001373 }
1374
Nicolas Capens13ac2322016-10-13 14:52:12 -04001375 Value *Nucleus::createConstantShort(short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001376 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001377 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001378 }
1379
Nicolas Capens13ac2322016-10-13 14:52:12 -04001380 Value *Nucleus::createConstantShort(unsigned short i)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001381 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001382 return V(::context->getConstantInt16(i));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001383 }
1384
Nicolas Capens13ac2322016-10-13 14:52:12 -04001385 Value *Nucleus::createConstantFloat(float x)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001386 {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001387 return V(::context->getConstantFloat(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001388 }
1389
Nicolas Capens13ac2322016-10-13 14:52:12 -04001390 Value *Nucleus::createNullPointer(Type *Ty)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001391 {
Nicolas Capensa29d6532016-12-05 21:38:09 -05001392 return createNullValue(T(sizeof(void*) == 8 ? Ice::IceType_i64 : Ice::IceType_i32));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001393 }
1394
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001395 Value *Nucleus::createConstantVector(const int64_t *constants, Type *type)
Nicolas Capens13ac2322016-10-13 14:52:12 -04001396 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001397 const int vectorSize = 16;
Ben Claytoneb50d252019-04-15 13:50:01 -04001398 ASSERT(Ice::typeWidthInBytes(T(type)) == vectorSize);
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001399 const int alignment = vectorSize;
1400 auto globalPool = ::function->getGlobalPool();
1401
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001402 const int64_t *i = constants;
1403 const double *f = reinterpret_cast<const double*>(constants);
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001404 Ice::VariableDeclaration::DataInitializer *dataInitializer = nullptr;
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001405
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001406 switch((int)reinterpret_cast<intptr_t>(type))
1407 {
1408 case Ice::IceType_v4i32:
Nicolas Capensa4c30b02016-11-08 15:43:17 -05001409 case Ice::IceType_v4i1:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001410 {
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 Capens8dfd9a72016-10-13 17:44:51 -04001416 case Ice::IceType_v4f32:
1417 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001418 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[2], (float)f[3]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001419 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 Capensa4c30b02016-11-08 15:43:17 -05001424 case Ice::IceType_v8i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001425 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001426 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 Capens8dfd9a72016-10-13 17:44:51 -04001427 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 Capensa4c30b02016-11-08 15:43:17 -05001432 case Ice::IceType_v16i1:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001433 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001434 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 Capens8dfd9a72016-10-13 17:44:51 -04001435 static_assert(sizeof(initializer) == vectorSize, "!");
1436 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1437 }
1438 break;
1439 case Type_v2i32:
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001440 {
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 Capens4cfd4572016-10-20 01:00:19 -04001446 case Type_v2f32:
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001447 {
Nicolas Capens7f3f69c2016-10-20 01:29:33 -04001448 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[0], (float)f[1]};
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001449 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 Capens7f3f69c2016-10-20 01:29:33 -04001455 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 Capens8dfd9a72016-10-13 17:44:51 -04001456 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 Capens7f3f69c2016-10-20 01:29:33 -04001462 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 Capens8dfd9a72016-10-13 17:44:51 -04001463 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 Capens7f3f69c2016-10-20 01:29:33 -04001469 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 Capens8dfd9a72016-10-13 17:44:51 -04001470 static_assert(sizeof(initializer) == vectorSize, "!");
1471 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1472 }
1473 break;
1474 default:
Ben Claytoneb50d252019-04-15 13:50:01 -04001475 UNREACHABLE("Unknown constant vector type: %d", (int)reinterpret_cast<intptr_t>(type));
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001476 }
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 Capens87852e12016-11-24 14:45:06 -05001484
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001485 ::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 Capens13ac2322016-10-13 14:52:12 -04001495 }
1496
1497 Value *Nucleus::createConstantVector(const double *constants, Type *type)
Nicolas Capens598f8d82016-09-26 15:09:10 -04001498 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04001499 return createConstantVector((const int64_t*)constants, type);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001500 }
1501
1502 Type *Void::getType()
1503 {
1504 return T(Ice::IceType_void);
1505 }
1506
Nicolas Capens598f8d82016-09-26 15:09:10 -04001507 Type *Bool::getType()
1508 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001509 return T(Ice::IceType_i1);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001510 }
1511
Nicolas Capens598f8d82016-09-26 15:09:10 -04001512 Type *Byte::getType()
1513 {
Nicolas Capens6d738712016-09-30 04:15:22 -04001514 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001515 }
1516
Nicolas Capens598f8d82016-09-26 15:09:10 -04001517 Type *SByte::getType()
1518 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001519 return T(Ice::IceType_i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001520 }
1521
Nicolas Capens598f8d82016-09-26 15:09:10 -04001522 Type *Short::getType()
1523 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001524 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001525 }
1526
Nicolas Capens598f8d82016-09-26 15:09:10 -04001527 Type *UShort::getType()
1528 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001529 return T(Ice::IceType_i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001530 }
1531
1532 Type *Byte4::getType()
1533 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04001534 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001535 }
1536
1537 Type *SByte4::getType()
1538 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001539 return T(Type_v4i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001540 }
1541
Nicolas Capensb6d4ce32019-03-12 23:00:24 -04001542 namespace
Nicolas Capens598f8d82016-09-26 15:09:10 -04001543 {
Nicolas Capensb6d4ce32019-03-12 23:00:24 -04001544 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 Capens598f8d82016-09-26 15:09:10 -04001548
Nicolas Capensb6d4ce32019-03-12 23:00:24 -04001549 RValue<Byte> Extract(RValue<Byte8> val, int i)
1550 {
1551 return RValue<Byte>(Nucleus::createExtractElement(val.value, Byte::getType(), i));
1552 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001553
Nicolas Capensb6d4ce32019-03-12 23:00:24 -04001554 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 Capens98436732017-07-25 15:32:12 -04001558 }
1559
Nicolas Capens598f8d82016-09-26 15:09:10 -04001560 RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y)
1561 {
Nicolas Capens98436732017-07-25 15:32:12 -04001562 if(emulateIntrinsics)
1563 {
1564 Byte8 result;
Nicolas Capens33438a62017-09-27 11:47:35 -04001565 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 Capensc71bed22016-11-07 22:25:14 -05001573
Nicolas Capens98436732017-07-25 15:32:12 -04001574 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 Capens598f8d82016-09-26 15:09:10 -04001588 }
1589
1590 RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y)
1591 {
Nicolas Capens98436732017-07-25 15:32:12 -04001592 if(emulateIntrinsics)
1593 {
1594 Byte8 result;
Nicolas Capens33438a62017-09-27 11:47:35 -04001595 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 Capensc71bed22016-11-07 22:25:14 -05001603
Nicolas Capens98436732017-07-25 15:32:12 -04001604 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 Capens598f8d82016-09-26 15:09:10 -04001618 }
1619
Nicolas Capensd6cacad2017-07-25 15:32:12 -04001620 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 Hetue18c5302017-08-04 11:48:17 -04001650 RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u);
Nicolas Capensd6cacad2017-07-25 15:32:12 -04001651 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 Capens598f8d82016-09-26 15:09:10 -04001660 RValue<Int> SignMask(RValue<Byte8> x)
1661 {
Nicolas Capens091f3502017-10-03 14:56:49 -04001662 if(emulateIntrinsics || CPUID::ARM)
Nicolas Capensd6cacad2017-07-25 15:32:12 -04001663 {
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 Capensc71bed22016-11-07 22:25:14 -05001675
Nicolas Capens0f70a7f2017-07-26 13:50:04 -04001676 return RValue<Int>(V(result)) & 0xFF;
Nicolas Capensd6cacad2017-07-25 15:32:12 -04001677 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001678 }
1679
1680// RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y)
1681// {
Nicolas Capens2f970b62016-11-08 14:28:59 -05001682// return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001683// }
1684
1685 RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y)
1686 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05001687 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001688 }
1689
1690 Type *Byte8::getType()
1691 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04001692 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001693 }
1694
Nicolas Capens598f8d82016-09-26 15:09:10 -04001695// RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs)
1696// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001697// return RValue<SByte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001698// }
1699
1700// RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs)
1701// {
Nicolas Capens15060bb2016-12-05 22:17:19 -05001702// return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001703// }
1704
Nicolas Capens33438a62017-09-27 11:47:35 -04001705 RValue<SByte> SaturateSigned(RValue<Short> x)
Nicolas Capens98436732017-07-25 15:32:12 -04001706 {
1707 return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x))));
1708 }
1709
Nicolas Capens598f8d82016-09-26 15:09:10 -04001710 RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y)
1711 {
Nicolas Capens98436732017-07-25 15:32:12 -04001712 if(emulateIntrinsics)
1713 {
1714 SByte8 result;
Nicolas Capens33438a62017-09-27 11:47:35 -04001715 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 Capensc71bed22016-11-07 22:25:14 -05001723
Nicolas Capens98436732017-07-25 15:32:12 -04001724 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 Capens598f8d82016-09-26 15:09:10 -04001738 }
1739
1740 RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y)
1741 {
Nicolas Capens98436732017-07-25 15:32:12 -04001742 if(emulateIntrinsics)
1743 {
1744 SByte8 result;
Nicolas Capens33438a62017-09-27 11:47:35 -04001745 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 Capensc71bed22016-11-07 22:25:14 -05001753
Nicolas Capens98436732017-07-25 15:32:12 -04001754 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 Capens598f8d82016-09-26 15:09:10 -04001768 }
1769
Nicolas Capens598f8d82016-09-26 15:09:10 -04001770 RValue<Int> SignMask(RValue<SByte8> x)
1771 {
Nicolas Capens091f3502017-10-03 14:56:49 -04001772 if(emulateIntrinsics || CPUID::ARM)
Nicolas Capensd6cacad2017-07-25 15:32:12 -04001773 {
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 Capensf2cb9df2016-10-21 17:26:13 -04001785
Nicolas Capens0f70a7f2017-07-26 13:50:04 -04001786 return RValue<Int>(V(result)) & 0xFF;
Nicolas Capensd6cacad2017-07-25 15:32:12 -04001787 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04001788 }
1789
1790 RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y)
1791 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05001792 return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001793 }
1794
1795 RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y)
1796 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05001797 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04001798 }
1799
1800 Type *SByte8::getType()
1801 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04001802 return T(Type_v8i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001803 }
1804
Nicolas Capens598f8d82016-09-26 15:09:10 -04001805 Type *Byte16::getType()
1806 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04001807 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001808 }
1809
1810 Type *SByte16::getType()
1811 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04001812 return T(Ice::IceType_v16i8);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001813 }
1814
Nicolas Capens16b5f152016-10-13 13:39:01 -04001815 Type *Short2::getType()
1816 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04001817 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04001818 }
1819
Nicolas Capens16b5f152016-10-13 13:39:01 -04001820 Type *UShort2::getType()
1821 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04001822 return T(Type_v2i16);
Nicolas Capens16b5f152016-10-13 13:39:01 -04001823 }
1824
Nicolas Capens598f8d82016-09-26 15:09:10 -04001825 Short4::Short4(RValue<Int4> cast)
1826 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08001827 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 Capensd4227962016-11-09 14:24:25 -05001830
Nicolas Capensbea4dce2017-07-24 16:54:44 -04001831 Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value;
Nicolas Capensd4227962016-11-09 14:24:25 -05001832 Value *short4 = Nucleus::createBitCast(int2, Short4::getType());
1833
1834 storeValue(short4);
Nicolas Capens598f8d82016-09-26 15:09:10 -04001835 }
1836
1837// Short4::Short4(RValue<Float> cast)
1838// {
1839// }
1840
1841 Short4::Short4(RValue<Float4> cast)
1842 {
Ben Claytoneb50d252019-04-15 13:50:01 -04001843 UNIMPLEMENTED("Short4::Short4(RValue<Float4> cast)");
Nicolas Capens598f8d82016-09-26 15:09:10 -04001844 }
1845
Nicolas Capens598f8d82016-09-26 15:09:10 -04001846 RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs)
1847 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04001848 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 Capens598f8d82016-09-26 15:09:10 -04001862 }
1863
1864 RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs)
1865 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04001866 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 Capens598f8d82016-09-26 15:09:10 -04001880 }
1881
Nicolas Capens598f8d82016-09-26 15:09:10 -04001882 RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y)
1883 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04001884 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 Capens598f8d82016-09-26 15:09:10 -04001893 }
1894
1895 RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y)
1896 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04001897 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 Capens598f8d82016-09-26 15:09:10 -04001906 }
1907
Nicolas Capens33438a62017-09-27 11:47:35 -04001908 RValue<Short> SaturateSigned(RValue<Int> x)
Nicolas Capens98436732017-07-25 15:32:12 -04001909 {
1910 return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x)));
1911 }
1912
Nicolas Capens598f8d82016-09-26 15:09:10 -04001913 RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y)
1914 {
Nicolas Capens98436732017-07-25 15:32:12 -04001915 if(emulateIntrinsics)
1916 {
1917 Short4 result;
Nicolas Capens33438a62017-09-27 11:47:35 -04001918 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 Capensc71bed22016-11-07 22:25:14 -05001922
Nicolas Capens98436732017-07-25 15:32:12 -04001923 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 Capens598f8d82016-09-26 15:09:10 -04001937 }
1938
1939 RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y)
1940 {
Nicolas Capens98436732017-07-25 15:32:12 -04001941 if(emulateIntrinsics)
1942 {
1943 Short4 result;
Nicolas Capens33438a62017-09-27 11:47:35 -04001944 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 Capensc71bed22016-11-07 22:25:14 -05001948
Nicolas Capens98436732017-07-25 15:32:12 -04001949 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 Capens598f8d82016-09-26 15:09:10 -04001963 }
1964
1965 RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y)
1966 {
Nicolas Capens6c157442017-07-25 15:32:12 -04001967 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 Capensc71bed22016-11-07 22:25:14 -05001974
Nicolas Capens6c157442017-07-25 15:32:12 -04001975 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 Capens598f8d82016-09-26 15:09:10 -04001989 }
1990
1991 RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y)
1992 {
Nicolas Capensafe27e92017-07-25 15:32:12 -04001993 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 Capensc71bed22016-11-07 22:25:14 -05001998
Nicolas Capensafe27e92017-07-25 15:32:12 -04001999 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 Capens598f8d82016-09-26 15:09:10 -04002013 }
2014
Nicolas Capens33438a62017-09-27 11:47:35 -04002015 RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002016 {
Nicolas Capens8960fbf2017-07-25 15:32:12 -04002017 if(emulateIntrinsics)
2018 {
2019 SByte8 result;
Nicolas Capens33438a62017-09-27 11:47:35 -04002020 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 Capensec54a172016-10-25 17:32:37 -04002028
Nicolas Capens8960fbf2017-07-25 15:32:12 -04002029 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 Capens598f8d82016-09-26 15:09:10 -04002043 }
2044
Nicolas Capens33438a62017-09-27 11:47:35 -04002045 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 Capens598f8d82016-09-26 15:09:10 -04002075 RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y)
2076 {
Nicolas Capens2f970b62016-11-08 14:28:59 -05002077 return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002078 }
2079
2080 RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y)
2081 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002082 return RValue<Short4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002083 }
2084
2085 Type *Short4::getType()
2086 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002087 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002088 }
2089
Nicolas Capens598f8d82016-09-26 15:09:10 -04002090 UShort4::UShort4(RValue<Float4> cast, bool saturate)
2091 {
Nicolas Capensd4227962016-11-09 14:24:25 -05002092 if(saturate)
2093 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05002094 if(CPUID::SSE4_1)
Nicolas Capensd4227962016-11-09 14:24:25 -05002095 {
Nicolas Capens091f3502017-10-03 14:56:49 -04002096 // 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 Capens33438a62017-09-27 11:47:35 -04002105 *this = As<UShort4>(PackUnsigned(int4, int4));
Nicolas Capensd4227962016-11-09 14:24:25 -05002106 }
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 Capens598f8d82016-09-26 15:09:10 -04002116 }
2117
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002118 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 Capens598f8d82016-09-26 15:09:10 -04002128 RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs)
2129 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002130 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 Capens598f8d82016-09-26 15:09:10 -04002144 }
2145
2146 RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs)
2147 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002148 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 Capens598f8d82016-09-26 15:09:10 -04002162 }
2163
Nicolas Capens598f8d82016-09-26 15:09:10 -04002164 RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y)
2165 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04002166 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 Capens598f8d82016-09-26 15:09:10 -04002175 }
2176
2177 RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y)
2178 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04002179 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 Capens598f8d82016-09-26 15:09:10 -04002188 }
2189
Nicolas Capens7f301812017-10-02 17:32:34 -04002190 RValue<UShort> SaturateUnsigned(RValue<Int> x)
Nicolas Capens98436732017-07-25 15:32:12 -04002191 {
2192 return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x)));
2193 }
2194
Nicolas Capens598f8d82016-09-26 15:09:10 -04002195 RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y)
2196 {
Nicolas Capens98436732017-07-25 15:32:12 -04002197 if(emulateIntrinsics)
2198 {
2199 UShort4 result;
Nicolas Capens7f301812017-10-02 17:32:34 -04002200 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 Capensc71bed22016-11-07 22:25:14 -05002204
Nicolas Capens98436732017-07-25 15:32:12 -04002205 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 Capens598f8d82016-09-26 15:09:10 -04002219 }
2220
2221 RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y)
2222 {
Nicolas Capens98436732017-07-25 15:32:12 -04002223 if(emulateIntrinsics)
2224 {
2225 UShort4 result;
Nicolas Capens7f301812017-10-02 17:32:34 -04002226 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 Capensc71bed22016-11-07 22:25:14 -05002230
Nicolas Capens98436732017-07-25 15:32:12 -04002231 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 Capens598f8d82016-09-26 15:09:10 -04002245 }
2246
2247 RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y)
2248 {
Nicolas Capens6c157442017-07-25 15:32:12 -04002249 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 Capensc71bed22016-11-07 22:25:14 -05002256
Nicolas Capens6c157442017-07-25 15:32:12 -04002257 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 Capens598f8d82016-09-26 15:09:10 -04002271 }
2272
Chris Forbesaa8f6992019-03-01 14:18:30 -08002273 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 Capens598f8d82016-09-26 15:09:10 -04002319 RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)
2320 {
Ben Claytoneb50d252019-04-15 13:50:01 -04002321 UNIMPLEMENTED("RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)");
2322 return UShort4(0);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002323 }
2324
Nicolas Capens598f8d82016-09-26 15:09:10 -04002325 Type *UShort4::getType()
2326 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002327 return T(Type_v4i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002328 }
2329
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002330 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 Capens598f8d82016-09-26 15:09:10 -04002340 RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs)
2341 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002342 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 Capens598f8d82016-09-26 15:09:10 -04002360 }
2361
2362 RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs)
2363 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002364 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 Capens598f8d82016-09-26 15:09:10 -04002382 }
2383
2384 RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)
2385 {
Ben Claytoneb50d252019-04-15 13:50:01 -04002386 UNIMPLEMENTED("RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)");
2387 return Int4(0);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002388 }
2389
Nicolas Capens598f8d82016-09-26 15:09:10 -04002390 RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)
2391 {
Ben Claytoneb50d252019-04-15 13:50:01 -04002392 UNIMPLEMENTED("RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)");
2393 return Short8(0);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002394 }
2395
2396 Type *Short8::getType()
2397 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002398 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002399 }
2400
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002401 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 Capens598f8d82016-09-26 15:09:10 -04002411 RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs)
2412 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002413 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 Capens598f8d82016-09-26 15:09:10 -04002431 }
2432
2433 RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs)
2434 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002435 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 Capens598f8d82016-09-26 15:09:10 -04002453 }
2454
Nicolas Capens598f8d82016-09-26 15:09:10 -04002455 RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7)
2456 {
Ben Claytoneb50d252019-04-15 13:50:01 -04002457 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 Capens598f8d82016-09-26 15:09:10 -04002459 }
2460
2461 RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)
2462 {
Ben Claytoneb50d252019-04-15 13:50:01 -04002463 UNIMPLEMENTED("RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)");
2464 return UShort8(0);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002465 }
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 Claytoneb50d252019-04-15 13:50:01 -04002470// ASSERT(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002471// }
2472
2473 Type *UShort8::getType()
2474 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002475 return T(Ice::IceType_v8i16);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002476 }
2477
Nicolas Capens96d4e092016-11-18 14:22:38 -05002478 RValue<Int> operator++(Int &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002479 {
Nicolas Capens5b41ba32016-12-08 14:34:00 -05002480 RValue<Int> res = val;
Nicolas Capensd1229402016-11-07 16:05:22 -05002481 val += 1;
2482 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002483 }
2484
Nicolas Capens96d4e092016-11-18 14:22:38 -05002485 const Int &operator++(Int &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002486 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002487 val += 1;
2488 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002489 }
2490
Nicolas Capens96d4e092016-11-18 14:22:38 -05002491 RValue<Int> operator--(Int &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002492 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002493 RValue<Int> res = val;
2494 val -= 1;
2495 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002496 }
2497
Nicolas Capens96d4e092016-11-18 14:22:38 -05002498 const Int &operator--(Int &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002499 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002500 val -= 1;
2501 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002502 }
2503
Nicolas Capens598f8d82016-09-26 15:09:10 -04002504 RValue<Int> RoundInt(RValue<Float> cast)
2505 {
Nicolas Capens091f3502017-10-03 14:56:49 -04002506 if(emulateIntrinsics || CPUID::ARM)
Nicolas Capensf7b75882017-04-26 09:30:47 -04002507 {
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 Capensa8086512016-11-07 17:32:17 -05002519
Nicolas Capensf7b75882017-04-26 09:30:47 -04002520 return RValue<Int>(V(result));
2521 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04002522 }
2523
2524 Type *Int::getType()
2525 {
2526 return T(Ice::IceType_i32);
2527 }
2528
Nicolas Capens598f8d82016-09-26 15:09:10 -04002529 Type *Long::getType()
2530 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002531 return T(Ice::IceType_i64);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002532 }
2533
Nicolas Capens598f8d82016-09-26 15:09:10 -04002534 UInt::UInt(RValue<Float> cast)
2535 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05002536 // 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 Capens598f8d82016-09-26 15:09:10 -04002548 }
2549
Nicolas Capens96d4e092016-11-18 14:22:38 -05002550 RValue<UInt> operator++(UInt &val, int) // Post-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002551 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002552 RValue<UInt> res = val;
2553 val += 1;
2554 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002555 }
2556
Nicolas Capens96d4e092016-11-18 14:22:38 -05002557 const UInt &operator++(UInt &val) // Pre-increment
Nicolas Capens598f8d82016-09-26 15:09:10 -04002558 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002559 val += 1;
2560 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002561 }
2562
Nicolas Capens96d4e092016-11-18 14:22:38 -05002563 RValue<UInt> operator--(UInt &val, int) // Post-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002564 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002565 RValue<UInt> res = val;
2566 val -= 1;
2567 return res;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002568 }
2569
Nicolas Capens96d4e092016-11-18 14:22:38 -05002570 const UInt &operator--(UInt &val) // Pre-decrement
Nicolas Capens598f8d82016-09-26 15:09:10 -04002571 {
Nicolas Capensd1229402016-11-07 16:05:22 -05002572 val -= 1;
2573 return val;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002574 }
2575
Nicolas Capens598f8d82016-09-26 15:09:10 -04002576// RValue<UInt> RoundUInt(RValue<Float> cast)
2577// {
Ben Claytoneb50d252019-04-15 13:50:01 -04002578// ASSERT(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002579// }
2580
2581 Type *UInt::getType()
2582 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002583 return T(Ice::IceType_i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002584 }
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 Capens598f8d82016-09-26 15:09:10 -04002600 RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs)
2601 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002602 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 Capens598f8d82016-09-26 15:09:10 -04002614 }
2615
2616 RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs)
2617 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002618 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 Capens598f8d82016-09-26 15:09:10 -04002630 }
2631
Nicolas Capens598f8d82016-09-26 15:09:10 -04002632 Type *Int2::getType()
2633 {
Nicolas Capens8dfd9a72016-10-13 17:44:51 -04002634 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002635 }
2636
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002637 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 Capens598f8d82016-09-26 15:09:10 -04002647 RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs)
2648 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002649 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 Capens598f8d82016-09-26 15:09:10 -04002661 }
2662
2663 RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs)
2664 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002665 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 Capens598f8d82016-09-26 15:09:10 -04002677 }
2678
Nicolas Capens598f8d82016-09-26 15:09:10 -04002679 Type *UInt2::getType()
2680 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04002681 return T(Type_v2i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002682 }
2683
Nicolas Capenscb986762017-01-20 11:34:37 -05002684 Int4::Int4(RValue<Byte4> cast) : XYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002685 {
Nicolas Capensd4227962016-11-09 14:24:25 -05002686 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 Capens598f8d82016-09-26 15:09:10 -04002700 }
2701
Nicolas Capenscb986762017-01-20 11:34:37 -05002702 Int4::Int4(RValue<SByte4> cast) : XYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002703 {
Nicolas Capensd4227962016-11-09 14:24:25 -05002704 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
2705 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
2706
Nicolas Capensd4227962016-11-09 14:24:25 -05002707 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 Capens8be6c7b2017-07-25 15:32:12 -04002713 Value *e = Nucleus::createShuffleVector(d, d, swizzle2);
Nicolas Capensd4227962016-11-09 14:24:25 -05002714
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002715 *this = As<Int4>(e) >> 24;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002716 }
2717
Nicolas Capenscb986762017-01-20 11:34:37 -05002718 Int4::Int4(RValue<Short4> cast) : XYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002719 {
Nicolas Capensd4227962016-11-09 14:24:25 -05002720 int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3};
2721 Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle);
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002722
2723 *this = As<Int4>(c) >> 16;
Nicolas Capens598f8d82016-09-26 15:09:10 -04002724 }
2725
Nicolas Capenscb986762017-01-20 11:34:37 -05002726 Int4::Int4(RValue<UShort4> cast) : XYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002727 {
Nicolas Capensd4227962016-11-09 14:24:25 -05002728 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 Capens598f8d82016-09-26 15:09:10 -04002732 }
2733
Nicolas Capenscb986762017-01-20 11:34:37 -05002734 Int4::Int4(RValue<Int> rhs) : XYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002735 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08002736 Value *vector = Nucleus::createBitCast(rhs.value, Int4::getType());
Nicolas Capensd4227962016-11-09 14:24:25 -05002737
2738 int swizzle[4] = {0, 0, 0, 0};
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08002739 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
Nicolas Capensd4227962016-11-09 14:24:25 -05002740
2741 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002742 }
2743
Nicolas Capens598f8d82016-09-26 15:09:10 -04002744 RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs)
2745 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002746 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 Capens598f8d82016-09-26 15:09:10 -04002760 }
2761
2762 RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs)
2763 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002764 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 Capens598f8d82016-09-26 15:09:10 -04002778 }
2779
Nicolas Capens598f8d82016-09-26 15:09:10 -04002780 RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y)
2781 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002782 return RValue<Int4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002783 }
2784
2785 RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y)
2786 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002787 return RValue<Int4>(Nucleus::createICmpSLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002788 }
2789
2790 RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y)
2791 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002792 return RValue<Int4>(Nucleus::createICmpSLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002793 }
2794
2795 RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y)
2796 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002797 return RValue<Int4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002798 }
2799
2800 RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y)
2801 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002802 return RValue<Int4>(Nucleus::createICmpSGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002803 }
2804
2805 RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y)
2806 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002807 return RValue<Int4>(Nucleus::createICmpSGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002808 }
2809
2810 RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y)
2811 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04002812 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 Capens598f8d82016-09-26 15:09:10 -04002821 }
2822
2823 RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y)
2824 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04002825 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 Capens598f8d82016-09-26 15:09:10 -04002834 }
2835
2836 RValue<Int4> RoundInt(RValue<Float4> cast)
2837 {
Nicolas Capens091f3502017-10-03 14:56:49 -04002838 if(emulateIntrinsics || CPUID::ARM)
Nicolas Capensf7b75882017-04-26 09:30:47 -04002839 {
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 Capensa8086512016-11-07 17:32:17 -05002851
Nicolas Capensf7b75882017-04-26 09:30:47 -04002852 return RValue<Int4>(V(result));
2853 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04002854 }
2855
Nicolas Capens33438a62017-09-27 11:47:35 -04002856 RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002857 {
Nicolas Capens8960fbf2017-07-25 15:32:12 -04002858 if(emulateIntrinsics)
2859 {
2860 Short8 result;
Nicolas Capens33438a62017-09-27 11:47:35 -04002861 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 Capensec54a172016-10-25 17:32:37 -04002869
Nicolas Capens8960fbf2017-07-25 15:32:12 -04002870 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 Capens598f8d82016-09-26 15:09:10 -04002884 }
2885
Nicolas Capens33438a62017-09-27 11:47:35 -04002886 RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y)
2887 {
Nicolas Capens091f3502017-10-03 14:56:49 -04002888 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 Capens33438a62017-09-27 11:47:35 -04002899 {
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 Capens33438a62017-09-27 11:47:35 -04002910 }
2911
Nicolas Capens598f8d82016-09-26 15:09:10 -04002912 RValue<Int> SignMask(RValue<Int4> x)
2913 {
Nicolas Capens091f3502017-10-03 14:56:49 -04002914 if(emulateIntrinsics || CPUID::ARM)
Nicolas Capensd6cacad2017-07-25 15:32:12 -04002915 {
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 Capensf2cb9df2016-10-21 17:26:13 -04002927
Nicolas Capensd6cacad2017-07-25 15:32:12 -04002928 return RValue<Int>(V(result));
2929 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04002930 }
2931
Nicolas Capens598f8d82016-09-26 15:09:10 -04002932 Type *Int4::getType()
2933 {
Nicolas Capens23d99a42016-09-30 14:57:16 -04002934 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04002935 }
2936
Nicolas Capenscb986762017-01-20 11:34:37 -05002937 UInt4::UInt4(RValue<Float4> cast) : XYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04002938 {
Nicolas Capensc70a1162016-12-03 00:16:14 -05002939 // 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 Capens598f8d82016-09-26 15:09:10 -04002951 }
2952
Nicolas Capens598f8d82016-09-26 15:09:10 -04002953 RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs)
2954 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002955 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 Capens598f8d82016-09-26 15:09:10 -04002969 }
2970
2971 RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs)
2972 {
Nicolas Capens8be6c7b2017-07-25 15:32:12 -04002973 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 Capens598f8d82016-09-26 15:09:10 -04002987 }
2988
Nicolas Capens598f8d82016-09-26 15:09:10 -04002989 RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y)
2990 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002991 return RValue<UInt4>(Nucleus::createICmpEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002992 }
2993
2994 RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y)
2995 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05002996 return RValue<UInt4>(Nucleus::createICmpULT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04002997 }
2998
2999 RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y)
3000 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003001 return RValue<UInt4>(Nucleus::createICmpULE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003002 }
3003
3004 RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y)
3005 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003006 return RValue<UInt4>(Nucleus::createICmpNE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003007 }
3008
3009 RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y)
3010 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003011 return RValue<UInt4>(Nucleus::createICmpUGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003012 }
3013
3014 RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y)
3015 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003016 return RValue<UInt4>(Nucleus::createICmpUGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003017 }
3018
3019 RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y)
3020 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003021 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 Capens598f8d82016-09-26 15:09:10 -04003030 }
3031
3032 RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y)
3033 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003034 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 Capens598f8d82016-09-26 15:09:10 -04003043 }
3044
Nicolas Capens598f8d82016-09-26 15:09:10 -04003045 Type *UInt4::getType()
3046 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003047 return T(Ice::IceType_v4i32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003048 }
3049
Ben Claytonec1aeb82019-03-04 19:33:27 +00003050 Type *Half::getType()
3051 {
3052 return T(Ice::IceType_i16);
3053 }
Alexis Hetu734e2572018-12-20 14:00:49 -05003054
Nicolas Capens598f8d82016-09-26 15:09:10 -04003055 RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2)
3056 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04003057 return 1.0f / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003058 }
3059
3060 RValue<Float> RcpSqrt_pp(RValue<Float> x)
3061 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04003062 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003063 }
3064
3065 RValue<Float> Sqrt(RValue<Float> x)
3066 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04003067 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 Capens598f8d82016-09-26 15:09:10 -04003075 }
3076
3077 RValue<Float> Round(RValue<Float> x)
3078 {
Nicolas Capensa8086512016-11-07 17:32:17 -05003079 return Float4(Round(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003080 }
3081
3082 RValue<Float> Trunc(RValue<Float> x)
3083 {
Nicolas Capensa8086512016-11-07 17:32:17 -05003084 return Float4(Trunc(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003085 }
3086
3087 RValue<Float> Frac(RValue<Float> x)
3088 {
Nicolas Capensa8086512016-11-07 17:32:17 -05003089 return Float4(Frac(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003090 }
3091
3092 RValue<Float> Floor(RValue<Float> x)
3093 {
Nicolas Capensa8086512016-11-07 17:32:17 -05003094 return Float4(Floor(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003095 }
3096
3097 RValue<Float> Ceil(RValue<Float> x)
3098 {
Nicolas Capensa8086512016-11-07 17:32:17 -05003099 return Float4(Ceil(Float4(x))).x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003100 }
3101
3102 Type *Float::getType()
3103 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04003104 return T(Ice::IceType_f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003105 }
3106
Nicolas Capens598f8d82016-09-26 15:09:10 -04003107 Type *Float2::getType()
3108 {
Nicolas Capens4cfd4572016-10-20 01:00:19 -04003109 return T(Type_v2f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003110 }
3111
Nicolas Capenscb986762017-01-20 11:34:37 -05003112 Float4::Float4(RValue<Float> rhs) : XYZW(this)
Nicolas Capens598f8d82016-09-26 15:09:10 -04003113 {
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08003114 Value *vector = Nucleus::createBitCast(rhs.value, Float4::getType());
Nicolas Capensd4227962016-11-09 14:24:25 -05003115
3116 int swizzle[4] = {0, 0, 0, 0};
Nicolas Capensf8beb4b2017-01-27 02:55:44 -08003117 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
Nicolas Capensd4227962016-11-09 14:24:25 -05003118
3119 storeValue(replicate);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003120 }
3121
Nicolas Capens598f8d82016-09-26 15:09:10 -04003122 RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y)
3123 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003124 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05003125 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ogt, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003126 ::basicBlock->appendInst(cmp);
3127
3128 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05003129 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003130 ::basicBlock->appendInst(select);
3131
3132 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003133 }
3134
3135 RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y)
3136 {
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003137 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05003138 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Olt, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003139 ::basicBlock->appendInst(cmp);
3140
3141 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
Nicolas Capens5cdb91a2017-02-13 12:39:18 -05003142 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
Nicolas Capens53a8a3f2016-10-26 00:23:12 -04003143 ::basicBlock->appendInst(select);
3144
3145 return RValue<Float4>(V(result));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003146 }
3147
3148 RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2)
3149 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04003150 return Float4(1.0f) / x;
Nicolas Capens598f8d82016-09-26 15:09:10 -04003151 }
3152
3153 RValue<Float4> RcpSqrt_pp(RValue<Float4> x)
3154 {
Nicolas Capensd52e9362016-10-31 23:23:15 -04003155 return Rcp_pp(Sqrt(x));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003156 }
3157
3158 RValue<Float4> Sqrt(RValue<Float4> x)
3159 {
Nicolas Capens091f3502017-10-03 14:56:49 -04003160 if(emulateIntrinsics || CPUID::ARM)
Nicolas Capens9f737d32017-07-25 17:26:14 -04003161 {
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 Capensd52e9362016-10-31 23:23:15 -04003167
Nicolas Capens9f737d32017-07-25 17:26:14 -04003168 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 Capens598f8d82016-09-26 15:09:10 -04003181 }
3182
Nicolas Capens598f8d82016-09-26 15:09:10 -04003183 RValue<Int> SignMask(RValue<Float4> x)
3184 {
Nicolas Capens091f3502017-10-03 14:56:49 -04003185 if(emulateIntrinsics || CPUID::ARM)
Nicolas Capensd6cacad2017-07-25 15:32:12 -04003186 {
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 Capensf2cb9df2016-10-21 17:26:13 -04003198
Nicolas Capensd6cacad2017-07-25 15:32:12 -04003199 return RValue<Int>(V(result));
3200 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04003201 }
3202
3203 RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y)
3204 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003205 return RValue<Int4>(Nucleus::createFCmpOEQ(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003206 }
3207
3208 RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y)
3209 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003210 return RValue<Int4>(Nucleus::createFCmpOLT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003211 }
3212
3213 RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y)
3214 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003215 return RValue<Int4>(Nucleus::createFCmpOLE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003216 }
3217
3218 RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y)
3219 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003220 return RValue<Int4>(Nucleus::createFCmpONE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003221 }
3222
3223 RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y)
3224 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003225 return RValue<Int4>(Nucleus::createFCmpOGE(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003226 }
3227
3228 RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y)
3229 {
Nicolas Capens5e6ca092017-01-13 15:09:21 -05003230 return RValue<Int4>(Nucleus::createFCmpOGT(x.value, y.value));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003231 }
3232
Ben Claytonec1aeb82019-03-04 19:33:27 +00003233 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 Capens598f8d82016-09-26 15:09:10 -04003263 RValue<Float4> Round(RValue<Float4> x)
3264 {
Nicolas Capens091f3502017-10-03 14:56:49 -04003265 if(emulateIntrinsics || CPUID::ARM)
Nicolas Capensf7b75882017-04-26 09:30:47 -04003266 {
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 Capens9ca48d52017-01-14 12:52:55 -05003271 {
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 Capensa8086512016-11-07 17:32:17 -05003279
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003280 return RValue<Float4>(V(result));
3281 }
3282 else
3283 {
3284 return Float4(RoundInt(x));
3285 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04003286 }
3287
3288 RValue<Float4> Trunc(RValue<Float4> x)
3289 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003290 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 Capensa8086512016-11-07 17:32:17 -05003299
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003300 return RValue<Float4>(V(result));
3301 }
3302 else
3303 {
3304 return Float4(Int4(x));
3305 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04003306 }
3307
3308 RValue<Float4> Frac(RValue<Float4> x)
3309 {
Nicolas Capensb9230422017-07-17 10:27:33 -04003310 Float4 frc;
3311
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003312 if(CPUID::SSE4_1)
3313 {
Nicolas Capensb9230422017-07-17 10:27:33 -04003314 frc = x - Floor(x);
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003315 }
3316 else
3317 {
Nicolas Capensb9230422017-07-17 10:27:33 -04003318 frc = x - Float4(Int4(x)); // Signed fractional part.
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003319
Nicolas Capensb9230422017-07-17 10:27:33 -04003320 frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1, 1, 1, 1))); // Add 1.0 if negative.
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003321 }
Nicolas Capensb9230422017-07-17 10:27:33 -04003322
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 Capens598f8d82016-09-26 15:09:10 -04003326 }
3327
3328 RValue<Float4> Floor(RValue<Float4> x)
3329 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003330 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 Capensa8086512016-11-07 17:32:17 -05003339
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003340 return RValue<Float4>(V(result));
3341 }
3342 else
3343 {
3344 return x - Frac(x);
3345 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04003346 }
3347
3348 RValue<Float4> Ceil(RValue<Float4> x)
3349 {
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003350 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 Capensa8086512016-11-07 17:32:17 -05003359
Nicolas Capens9ca48d52017-01-14 12:52:55 -05003360 return RValue<Float4>(V(result));
3361 }
3362 else
3363 {
3364 return -Floor(-x);
3365 }
Nicolas Capens598f8d82016-09-26 15:09:10 -04003366 }
3367
3368 Type *Float4::getType()
3369 {
Nicolas Capens9709d4f2016-09-30 11:44:14 -04003370 return T(Ice::IceType_v4f32);
Nicolas Capens598f8d82016-09-26 15:09:10 -04003371 }
3372
Nicolas Capens598f8d82016-09-26 15:09:10 -04003373 RValue<Long> Ticks()
3374 {
Ben Claytoneb50d252019-04-15 13:50:01 -04003375 UNIMPLEMENTED("RValue<Long> Ticks()");
3376 return Long(Int(0));
Nicolas Capens598f8d82016-09-26 15:09:10 -04003377 }
Ben Clayton147c4912019-04-11 00:17:59 -04003378
3379 // Below are functions currently unimplemented for the Subzero backend.
3380 // They are stubbed to satisfy the linker.
Ben Clayton147c4912019-04-11 00:17:59 -04003381 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 Capens598f8d82016-09-26 15:09:10 -04003401}