OSDN Git Service

Make Blitter part of Renderer.
[android-x86/external-swiftshader.git] / src / Reactor / SubzeroReactor.cpp
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "Nucleus.hpp"
16
17 #include "Reactor.hpp"
18 #include "Routine.hpp"
19
20 #include "Optimizer.hpp"
21
22 #include "src/IceTypes.h"
23 #include "src/IceCfg.h"
24 #include "src/IceELFStreamer.h"
25 #include "src/IceGlobalContext.h"
26 #include "src/IceCfgNode.h"
27 #include "src/IceELFObjectWriter.h"
28 #include "src/IceGlobalInits.h"
29
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/raw_os_ostream.h"
32
33 #if defined(_WIN32)
34 #ifndef WIN32_LEAN_AND_MEAN
35 #define WIN32_LEAN_AND_MEAN
36 #endif // !WIN32_LEAN_AND_MEAN
37 #ifndef NOMINMAX
38 #define NOMINMAX
39 #endif // !NOMINMAX
40 #include <Windows.h>
41 #else
42 #include <sys/mman.h>
43 #if !defined(MAP_ANONYMOUS)
44 #define MAP_ANONYMOUS MAP_ANON
45 #endif
46 #endif
47
48 #include <mutex>
49 #include <limits>
50 #include <iostream>
51 #include <cassert>
52
53 namespace
54 {
55         Ice::GlobalContext *context = nullptr;
56         Ice::Cfg *function = nullptr;
57         Ice::CfgNode *basicBlock = nullptr;
58         Ice::CfgLocalAllocatorScope *allocator = nullptr;
59         sw::Routine *routine = nullptr;
60
61         std::mutex codegenMutex;
62
63         Ice::ELFFileStreamer *elfFile = nullptr;
64         Ice::Fdstream *out = nullptr;
65 }
66
67 namespace
68 {
69         #if !defined(__i386__) && defined(_M_IX86)
70                 #define __i386__ 1
71         #endif
72
73         #if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64))
74                 #define __x86_64__ 1
75         #endif
76
77         class CPUID
78         {
79         public:
80                 const static bool ARM;
81                 const static bool SSE4_1;
82
83         private:
84                 static void cpuid(int registers[4], int info)
85                 {
86                         #if defined(__i386__) || defined(__x86_64__)
87                                 #if defined(_WIN32)
88                                         __cpuid(registers, info);
89                                 #else
90                                         __asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
91                                 #endif
92                         #else
93                                 registers[0] = 0;
94                                 registers[1] = 0;
95                                 registers[2] = 0;
96                                 registers[3] = 0;
97                         #endif
98                 }
99
100                 static bool detectARM()
101                 {
102                         #if defined(__arm__)
103                                 return true;
104                         #elif defined(__i386__) || defined(__x86_64__)
105                                 return false;
106                         #else
107                                 #error "Unknown architecture"
108                         #endif
109                 }
110
111                 static bool detectSSE4_1()
112                 {
113                         #if defined(__i386__) || defined(__x86_64__)
114                                 int registers[4];
115                                 cpuid(registers, 1);
116                                 return (registers[2] & 0x00080000) != 0;
117                         #else
118                                 return false;
119                         #endif
120                 }
121         };
122
123         const bool CPUID::ARM = CPUID::detectARM();
124         const bool CPUID::SSE4_1 = CPUID::detectSSE4_1();
125         const bool emulateIntrinsics = CPUID::ARM;
126 }
127
128 namespace sw
129 {
130         enum EmulatedType
131         {
132                 EmulatedShift = 16,
133                 EmulatedV2 = 2 << EmulatedShift,
134                 EmulatedV4 = 4 << EmulatedShift,
135                 EmulatedV8 = 8 << EmulatedShift,
136                 EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8,
137
138                 Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2,
139                 Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4,
140                 Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2,
141                 Type_v8i8 =  Ice::IceType_v16i8 | EmulatedV8,
142                 Type_v4i8 =  Ice::IceType_v16i8 | EmulatedV4,
143                 Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2,
144         };
145
146         class Value : public Ice::Operand {};
147         class SwitchCases : public Ice::InstSwitch {};
148         class BasicBlock : public Ice::CfgNode {};
149
150         Ice::Type T(Type *t)
151         {
152                 static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!");
153                 return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits);
154         }
155
156         Type *T(Ice::Type t)
157         {
158                 return reinterpret_cast<Type*>(t);
159         }
160
161         Type *T(EmulatedType t)
162         {
163                 return reinterpret_cast<Type*>(t);
164         }
165
166         Value *V(Ice::Operand *v)
167         {
168                 return reinterpret_cast<Value*>(v);
169         }
170
171         BasicBlock *B(Ice::CfgNode *b)
172         {
173                 return reinterpret_cast<BasicBlock*>(b);
174         }
175
176         static size_t typeSize(Type *type)
177         {
178                 if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits)
179                 {
180                         switch(reinterpret_cast<std::intptr_t>(type))
181                         {
182                         case Type_v2i32: return 8;
183                         case Type_v4i16: return 8;
184                         case Type_v2i16: return 4;
185                         case Type_v8i8:  return 8;
186                         case Type_v4i8:  return 4;
187                         case Type_v2f32: return 8;
188                         default: assert(false);
189                         }
190                 }
191
192                 return Ice::typeWidthInBytes(T(type));
193         }
194
195         Optimization optimization[10] = {InstructionCombining, Disabled};
196
197         using ElfHeader = std::conditional<sizeof(void*) == 8, Elf64_Ehdr, Elf32_Ehdr>::type;
198         using SectionHeader = std::conditional<sizeof(void*) == 8, Elf64_Shdr, Elf32_Shdr>::type;
199
200         inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader)
201         {
202                 return reinterpret_cast<const SectionHeader*>((intptr_t)elfHeader + elfHeader->e_shoff);
203         }
204
205         inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index)
206         {
207                 return &sectionHeader(elfHeader)[index];
208         }
209
210         static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable)
211         {
212                 const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info);
213
214                 intptr_t address = (intptr_t)elfHeader + target->sh_offset;
215                 int32_t *patchSite = (int*)(address + relocation.r_offset);
216                 uint32_t index = relocation.getSymbol();
217                 int table = relocationTable.sh_link;
218                 void *symbolValue = nullptr;
219
220                 if(index != SHN_UNDEF)
221                 {
222                         if(table == SHN_UNDEF) return nullptr;
223                         const SectionHeader *symbolTable = elfSection(elfHeader, table);
224
225                         uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
226                         if(index >= symtab_entries)
227                         {
228                                 assert(index < symtab_entries && "Symbol Index out of range");
229                                 return nullptr;
230                         }
231
232                         intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset;
233                         Elf32_Sym &symbol = ((Elf32_Sym*)symbolAddress)[index];
234                         uint16_t section = symbol.st_shndx;
235
236                         if(section != SHN_UNDEF && section < SHN_LORESERVE)
237                         {
238                                 const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx);
239                                 symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset);
240                         }
241                         else
242                         {
243                                 return nullptr;
244                         }
245                 }
246
247                 if(CPUID::ARM)
248                 {
249                         switch(relocation.getType())
250                         {
251                         case R_ARM_NONE:
252                                 // No relocation
253                                 break;
254                         case R_ARM_MOVW_ABS_NC:
255                                 {
256                                         uint32_t thumb = 0;   // Calls to Thumb code not supported.
257                                         uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb;
258                                         *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 0x0FFF);
259                                 }
260                                 break;
261                         case R_ARM_MOVT_ABS:
262                                 {
263                                         uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16;
264                                         *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 0x0FFF);
265                                 }
266                                 break;
267                         default:
268                                 assert(false && "Unsupported relocation type");
269                                 return nullptr;
270                         }
271                 }
272                 else
273                 {
274                         switch(relocation.getType())
275                         {
276                         case R_386_NONE:
277                                 // No relocation
278                                 break;
279                         case R_386_32:
280                                 *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite);
281                                 break;
282                 //      case R_386_PC32:
283                 //              *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite);
284                 //              break;
285                         default:
286                                 assert(false && "Unsupported relocation type");
287                                 return nullptr;
288                         }
289                 }
290
291
292                 return symbolValue;
293         }
294
295         static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable)
296         {
297                 const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info);
298
299                 intptr_t address = (intptr_t)elfHeader + target->sh_offset;
300                 int32_t *patchSite = (int*)(address + relocation.r_offset);
301                 uint32_t index = relocation.getSymbol();
302                 int table = relocationTable.sh_link;
303                 void *symbolValue = nullptr;
304
305                 if(index != SHN_UNDEF)
306                 {
307                         if(table == SHN_UNDEF) return nullptr;
308                         const SectionHeader *symbolTable = elfSection(elfHeader, table);
309
310                         uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize;
311                         if(index >= symtab_entries)
312                         {
313                                 assert(index < symtab_entries && "Symbol Index out of range");
314                                 return nullptr;
315                         }
316
317                         intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset;
318                         Elf64_Sym &symbol = ((Elf64_Sym*)symbolAddress)[index];
319                         uint16_t section = symbol.st_shndx;
320
321                         if(section != SHN_UNDEF && section < SHN_LORESERVE)
322                         {
323                                 const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx);
324                                 symbolValue = reinterpret_cast<void*>((intptr_t)elfHeader + symbol.st_value + target->sh_offset);
325                         }
326                         else
327                         {
328                                 return nullptr;
329                         }
330                 }
331
332                 switch(relocation.getType())
333                 {
334                 case R_X86_64_NONE:
335                         // No relocation
336                         break;
337                 case R_X86_64_64:
338                         *(int64_t*)patchSite = (int64_t)((intptr_t)symbolValue + *(int64_t*)patchSite) + relocation.r_addend;
339                         break;
340                 case R_X86_64_PC32:
341                         *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite) + relocation.r_addend;
342                         break;
343                 case R_X86_64_32S:
344                         *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite) + relocation.r_addend;
345                         break;
346                 default:
347                         assert(false && "Unsupported relocation type");
348                         return nullptr;
349                 }
350
351                 return symbolValue;
352         }
353
354         void *loadImage(uint8_t *const elfImage, size_t &codeSize)
355         {
356                 ElfHeader *elfHeader = (ElfHeader*)elfImage;
357
358                 if(!elfHeader->checkMagic())
359                 {
360                         return nullptr;
361                 }
362
363                 // Expect ELF bitness to match platform
364                 assert(sizeof(void*) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32);
365                 #if defined(__i386__)
366                         assert(sizeof(void*) == 4 && elfHeader->e_machine == EM_386);
367                 #elif defined(__x86_64__)
368                         assert(sizeof(void*) == 8 && elfHeader->e_machine == EM_X86_64);
369                 #elif defined(__arm__)
370                         assert(sizeof(void*) == 4 && elfHeader->e_machine == EM_ARM);
371                 #else
372                         #error "Unsupported platform"
373                 #endif
374
375                 SectionHeader *sectionHeader = (SectionHeader*)(elfImage + elfHeader->e_shoff);
376                 void *entry = nullptr;
377
378                 for(int i = 0; i < elfHeader->e_shnum; i++)
379                 {
380                         if(sectionHeader[i].sh_type == SHT_PROGBITS)
381                         {
382                                 if(sectionHeader[i].sh_flags & SHF_EXECINSTR)
383                                 {
384                                         entry = elfImage + sectionHeader[i].sh_offset;
385                                         codeSize = sectionHeader[i].sh_size;
386                                 }
387                         }
388                         else if(sectionHeader[i].sh_type == SHT_REL)
389                         {
390                                 assert(sizeof(void*) == 4 && "UNIMPLEMENTED");   // Only expected/implemented for 32-bit code
391
392                                 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
393                                 {
394                                         const Elf32_Rel &relocation = ((const Elf32_Rel*)(elfImage + sectionHeader[i].sh_offset))[index];
395                                         relocateSymbol(elfHeader, relocation, sectionHeader[i]);
396                                 }
397                         }
398                         else if(sectionHeader[i].sh_type == SHT_RELA)
399                         {
400                                 assert(sizeof(void*) == 8 && "UNIMPLEMENTED");   // Only expected/implemented for 64-bit code
401
402                                 for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++)
403                                 {
404                                         const Elf64_Rela &relocation = ((const Elf64_Rela*)(elfImage + sectionHeader[i].sh_offset))[index];
405                                         relocateSymbol(elfHeader, relocation, sectionHeader[i]);
406                                 }
407                         }
408                 }
409
410                 return entry;
411         }
412
413         template<typename T>
414         struct ExecutableAllocator
415         {
416                 ExecutableAllocator() {};
417                 template<class U> ExecutableAllocator(const ExecutableAllocator<U> &other) {};
418
419                 using value_type = T;
420                 using size_type = std::size_t;
421
422                 T *allocate(size_type n)
423                 {
424                         #if defined(_WIN32)
425                                 return (T*)VirtualAlloc(NULL, sizeof(T) * n, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
426                         #else
427                                 return (T*)mmap(nullptr, sizeof(T) * n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
428                         #endif
429                 }
430
431                 void deallocate(T *p, size_type n)
432                 {
433                         #if defined(_WIN32)
434                                 VirtualFree(p, 0, MEM_RELEASE);
435                         #else
436                                 munmap(p, sizeof(T) * n);
437                         #endif
438                 }
439         };
440
441         class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine
442         {
443                 ELFMemoryStreamer(const ELFMemoryStreamer &) = delete;
444                 ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete;
445
446         public:
447                 ELFMemoryStreamer() : Routine(), entry(nullptr)
448                 {
449                         position = 0;
450                         buffer.reserve(0x1000);
451                 }
452
453                 ~ELFMemoryStreamer() override
454                 {
455                         #if defined(_WIN32)
456                                 if(buffer.size() != 0)
457                                 {
458                                         DWORD exeProtection;
459                                         VirtualProtect(&buffer[0], buffer.size(), oldProtection, &exeProtection);
460                                 }
461                         #endif
462                 }
463
464                 void write8(uint8_t Value) override
465                 {
466                         if(position == (uint64_t)buffer.size())
467                         {
468                                 buffer.push_back(Value);
469                                 position++;
470                         }
471                         else if(position < (uint64_t)buffer.size())
472                         {
473                                 buffer[position] = Value;
474                                 position++;
475                         }
476                         else assert(false && "UNIMPLEMENTED");
477                 }
478
479                 void writeBytes(llvm::StringRef Bytes) override
480                 {
481                         std::size_t oldSize = buffer.size();
482                         buffer.resize(oldSize + Bytes.size());
483                         memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size());
484                         position += Bytes.size();
485                 }
486
487                 uint64_t tell() const override { return position; }
488
489                 void seek(uint64_t Off) override { position = Off; }
490
491                 const void *getEntry() override
492                 {
493                         if(!entry)
494                         {
495                                 position = std::numeric_limits<std::size_t>::max();   // Can't stream more data after this
496
497                                 size_t codeSize = 0;
498                                 entry = loadImage(&buffer[0], codeSize);
499
500                                 #if defined(_WIN32)
501                                         VirtualProtect(&buffer[0], buffer.size(), PAGE_EXECUTE_READ, &oldProtection);
502                                         FlushInstructionCache(GetCurrentProcess(), NULL, 0);
503                                 #else
504                                         mprotect(&buffer[0], buffer.size(), PROT_READ | PROT_EXEC);
505                                         __builtin___clear_cache((char*)entry, (char*)entry + codeSize);
506                                 #endif
507                         }
508
509                         return entry;
510                 }
511
512         private:
513                 void *entry;
514                 std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer;
515                 std::size_t position;
516
517                 #if defined(_WIN32)
518                 DWORD oldProtection;
519                 #endif
520         };
521
522         Nucleus::Nucleus()
523         {
524                 ::codegenMutex.lock();   // Reactor is currently not thread safe
525
526                 Ice::ClFlags &Flags = Ice::ClFlags::Flags;
527                 Ice::ClFlags::getParsedClFlags(Flags);
528
529                 #if defined(__arm__)
530                         Flags.setTargetArch(Ice::Target_ARM32);
531                         Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm);
532                 #else   // x86
533                         Flags.setTargetArch(sizeof(void*) == 8 ? Ice::Target_X8664 : Ice::Target_X8632);
534                         Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2);
535                 #endif
536                 Flags.setOutFileType(Ice::FT_Elf);
537                 Flags.setOptLevel(Ice::Opt_2);
538                 Flags.setApplicationBinaryInterface(Ice::ABI_Platform);
539                 Flags.setVerbose(false ? Ice::IceV_Most : Ice::IceV_None);
540                 Flags.setDisableHybridAssembly(true);
541
542                 static llvm::raw_os_ostream cout(std::cout);
543                 static llvm::raw_os_ostream cerr(std::cerr);
544
545                 if(false)   // Write out to a file
546                 {
547                         std::error_code errorCode;
548                         ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None);
549                         ::elfFile = new Ice::ELFFileStreamer(*out);
550                         ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile);
551                 }
552                 else
553                 {
554                         ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer();
555                         ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory);
556                         ::routine = elfMemory;
557                 }
558         }
559
560         Nucleus::~Nucleus()
561         {
562                 delete ::allocator;
563                 delete ::function;
564                 delete ::context;
565
566                 delete ::elfFile;
567                 delete ::out;
568
569                 ::codegenMutex.unlock();
570         }
571
572         Routine *Nucleus::acquireRoutine(const wchar_t *name, bool runOptimizations)
573         {
574                 if(basicBlock->getInsts().empty() || basicBlock->getInsts().back().getKind() != Ice::Inst::Ret)
575                 {
576                         createRetVoid();
577                 }
578
579                 std::wstring wideName(name);
580                 std::string asciiName(wideName.begin(), wideName.end());
581                 ::function->setFunctionName(Ice::GlobalString::createWithString(::context, asciiName));
582
583                 optimize();
584
585                 ::function->translate();
586                 assert(!::function->hasError());
587
588                 auto *globals = ::function->getGlobalInits().release();
589
590                 if(globals && !globals->empty())
591                 {
592                         ::context->getGlobals()->merge(globals);
593                 }
594
595                 ::context->emitFileHeader();
596                 ::function->emitIAS();
597                 auto assembler = ::function->releaseAssembler();
598                 auto objectWriter = ::context->getObjectWriter();
599                 assembler->alignFunction();
600                 objectWriter->writeFunctionCode(::function->getFunctionName(), false, assembler.get());
601                 ::context->lowerGlobals("last");
602                 ::context->lowerConstants();
603                 ::context->lowerJumpTables();
604                 objectWriter->setUndefinedSyms(::context->getConstantExternSyms());
605                 objectWriter->writeNonUserSections();
606
607                 return ::routine;
608         }
609
610         void Nucleus::optimize()
611         {
612                 sw::optimize(::function);
613         }
614
615         Value *Nucleus::allocateStackVariable(Type *t, int arraySize)
616         {
617                 Ice::Type type = T(t);
618                 int typeSize = Ice::typeWidthInBytes(type);
619                 int totalSize = typeSize * (arraySize ? arraySize : 1);
620
621                 auto bytes = Ice::ConstantInteger32::create(::context, type, totalSize);
622                 auto address = ::function->makeVariable(T(getPointerType(t)));
623                 auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize);
624                 ::function->getEntryNode()->getInsts().push_front(alloca);
625
626                 return V(address);
627         }
628
629         BasicBlock *Nucleus::createBasicBlock()
630         {
631                 return B(::function->makeNode());
632         }
633
634         BasicBlock *Nucleus::getInsertBlock()
635         {
636                 return B(::basicBlock);
637         }
638
639         void Nucleus::setInsertBlock(BasicBlock *basicBlock)
640         {
641         //      assert(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator");
642                 ::basicBlock = basicBlock;
643         }
644
645         void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params)
646         {
647                 uint32_t sequenceNumber = 0;
648                 ::function = Ice::Cfg::create(::context, sequenceNumber).release();
649                 ::allocator = new Ice::CfgLocalAllocatorScope(::function);
650
651                 for(Type *type : Params)
652                 {
653                         Ice::Variable *arg = ::function->makeVariable(T(type));
654                         ::function->addArg(arg);
655                 }
656
657                 Ice::CfgNode *node = ::function->makeNode();
658                 ::function->setEntryNode(node);
659                 ::basicBlock = node;
660         }
661
662         Value *Nucleus::getArgument(unsigned int index)
663         {
664                 return V(::function->getArgs()[index]);
665         }
666
667         void Nucleus::createRetVoid()
668         {
669                 Ice::InstRet *ret = Ice::InstRet::create(::function);
670                 ::basicBlock->appendInst(ret);
671         }
672
673         void Nucleus::createRet(Value *v)
674         {
675                 Ice::InstRet *ret = Ice::InstRet::create(::function, v);
676                 ::basicBlock->appendInst(ret);
677         }
678
679         void Nucleus::createBr(BasicBlock *dest)
680         {
681                 auto br = Ice::InstBr::create(::function, dest);
682                 ::basicBlock->appendInst(br);
683         }
684
685         void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse)
686         {
687                 auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse);
688                 ::basicBlock->appendInst(br);
689         }
690
691         static bool isCommutative(Ice::InstArithmetic::OpKind op)
692         {
693                 switch(op)
694                 {
695                 case Ice::InstArithmetic::Add:
696                 case Ice::InstArithmetic::Fadd:
697                 case Ice::InstArithmetic::Mul:
698                 case Ice::InstArithmetic::Fmul:
699                 case Ice::InstArithmetic::And:
700                 case Ice::InstArithmetic::Or:
701                 case Ice::InstArithmetic::Xor:
702                         return true;
703                 default:
704                         return false;
705                 }
706         }
707
708         static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs)
709         {
710                 assert(lhs->getType() == rhs->getType() || (llvm::isa<Ice::Constant>(rhs) && (op == Ice::InstArithmetic::Shl || Ice::InstArithmetic::Lshr || Ice::InstArithmetic::Ashr)));
711
712                 bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op);
713
714                 Ice::Variable *result = ::function->makeVariable(lhs->getType());
715                 Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs);
716                 ::basicBlock->appendInst(arithmetic);
717
718                 return V(result);
719         }
720
721         Value *Nucleus::createAdd(Value *lhs, Value *rhs)
722         {
723                 return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs);
724         }
725
726         Value *Nucleus::createSub(Value *lhs, Value *rhs)
727         {
728                 return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs);
729         }
730
731         Value *Nucleus::createMul(Value *lhs, Value *rhs)
732         {
733                 return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs);
734         }
735
736         Value *Nucleus::createUDiv(Value *lhs, Value *rhs)
737         {
738                 return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs);
739         }
740
741         Value *Nucleus::createSDiv(Value *lhs, Value *rhs)
742         {
743                 return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs);
744         }
745
746         Value *Nucleus::createFAdd(Value *lhs, Value *rhs)
747         {
748                 return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs);
749         }
750
751         Value *Nucleus::createFSub(Value *lhs, Value *rhs)
752         {
753                 return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs);
754         }
755
756         Value *Nucleus::createFMul(Value *lhs, Value *rhs)
757         {
758                 return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs);
759         }
760
761         Value *Nucleus::createFDiv(Value *lhs, Value *rhs)
762         {
763                 return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs);
764         }
765
766         Value *Nucleus::createURem(Value *lhs, Value *rhs)
767         {
768                 return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs);
769         }
770
771         Value *Nucleus::createSRem(Value *lhs, Value *rhs)
772         {
773                 return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs);
774         }
775
776         Value *Nucleus::createFRem(Value *lhs, Value *rhs)
777         {
778                 return createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs);
779         }
780
781         Value *Nucleus::createShl(Value *lhs, Value *rhs)
782         {
783                 return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs);
784         }
785
786         Value *Nucleus::createLShr(Value *lhs, Value *rhs)
787         {
788                 return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs);
789         }
790
791         Value *Nucleus::createAShr(Value *lhs, Value *rhs)
792         {
793                 return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs);
794         }
795
796         Value *Nucleus::createAnd(Value *lhs, Value *rhs)
797         {
798                 return createArithmetic(Ice::InstArithmetic::And, lhs, rhs);
799         }
800
801         Value *Nucleus::createOr(Value *lhs, Value *rhs)
802         {
803                 return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs);
804         }
805
806         Value *Nucleus::createXor(Value *lhs, Value *rhs)
807         {
808                 return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs);
809         }
810
811         Value *Nucleus::createNeg(Value *v)
812         {
813                 return createSub(createNullValue(T(v->getType())), v);
814         }
815
816         Value *Nucleus::createFNeg(Value *v)
817         {
818                 double c[4] = {-0.0, -0.0, -0.0, -0.0};
819                 Value *negativeZero = Ice::isVectorType(v->getType()) ?
820                                       createConstantVector(c, T(v->getType())) :
821                                       V(::context->getConstantFloat(-0.0f));
822
823                 return createFSub(negativeZero, v);
824         }
825
826         Value *Nucleus::createNot(Value *v)
827         {
828                 if(Ice::isScalarIntegerType(v->getType()))
829                 {
830                         return createXor(v, V(::context->getConstantInt(v->getType(), -1)));
831                 }
832                 else   // Vector
833                 {
834                         int64_t c[16] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
835                         return createXor(v, createConstantVector(c, T(v->getType())));
836                 }
837         }
838
839         Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align)
840         {
841                 int valueType = (int)reinterpret_cast<intptr_t>(type);
842                 Ice::Variable *result = ::function->makeVariable(T(type));
843
844                 if(valueType & EmulatedBits)
845                 {
846                         const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
847                         auto target = ::context->getConstantUndef(Ice::IceType_i32);
848                         auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
849                         load->addArg(ptr);
850                         load->addArg(::context->getConstantInt32(typeSize(type)));
851                         ::basicBlock->appendInst(load);
852                 }
853                 else
854                 {
855                         auto load = Ice::InstLoad::create(::function, result, ptr, align);
856                         ::basicBlock->appendInst(load);
857                 }
858
859                 return V(result);
860         }
861
862         Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align)
863         {
864                 int valueType = (int)reinterpret_cast<intptr_t>(type);
865
866                 if(valueType & EmulatedBits)
867                 {
868                         const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T};
869                         auto target = ::context->getConstantUndef(Ice::IceType_i32);
870                         auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic);
871                         store->addArg(value);
872                         store->addArg(ptr);
873                         store->addArg(::context->getConstantInt32(typeSize(type)));
874                         ::basicBlock->appendInst(store);
875                 }
876                 else
877                 {
878                         assert(T(value->getType()) == type);
879
880                         auto store = Ice::InstStore::create(::function, value, ptr, align);
881                         ::basicBlock->appendInst(store);
882                 }
883
884                 return value;
885         }
886
887         Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
888         {
889                 assert(index->getType() == Ice::IceType_i32);
890
891                 if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index))
892                 {
893                         int32_t offset = constant->getValue() * (int)typeSize(type);
894
895                         if(offset == 0)
896                         {
897                                 return ptr;
898                         }
899
900                         return createAdd(ptr, createConstantInt(offset));
901                 }
902
903                 if(!Ice::isByteSizedType(T(type)))
904                 {
905                         index = createMul(index, createConstantInt((int)typeSize(type)));
906                 }
907
908                 if(sizeof(void*) == 8)
909                 {
910                         if(unsignedIndex)
911                         {
912                                 index = createZExt(index, T(Ice::IceType_i64));
913                         }
914                         else
915                         {
916                                 index = createSExt(index, T(Ice::IceType_i64));
917                         }
918                 }
919
920                 return createAdd(ptr, index);
921         }
922
923         Value *Nucleus::createAtomicAdd(Value *ptr, Value *value)
924         {
925                 assert(false && "UNIMPLEMENTED"); return nullptr;
926         }
927
928         static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType)
929         {
930                 if(v->getType() == T(destType))
931                 {
932                         return v;
933                 }
934
935                 Ice::Variable *result = ::function->makeVariable(T(destType));
936                 Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v);
937                 ::basicBlock->appendInst(cast);
938
939                 return V(result);
940         }
941
942         Value *Nucleus::createTrunc(Value *v, Type *destType)
943         {
944                 return createCast(Ice::InstCast::Trunc, v, destType);
945         }
946
947         Value *Nucleus::createZExt(Value *v, Type *destType)
948         {
949                 return createCast(Ice::InstCast::Zext, v, destType);
950         }
951
952         Value *Nucleus::createSExt(Value *v, Type *destType)
953         {
954                 return createCast(Ice::InstCast::Sext, v, destType);
955         }
956
957         Value *Nucleus::createFPToSI(Value *v, Type *destType)
958         {
959                 return createCast(Ice::InstCast::Fptosi, v, destType);
960         }
961
962         Value *Nucleus::createSIToFP(Value *v, Type *destType)
963         {
964                 return createCast(Ice::InstCast::Sitofp, v, destType);
965         }
966
967         Value *Nucleus::createFPTrunc(Value *v, Type *destType)
968         {
969                 return createCast(Ice::InstCast::Fptrunc, v, destType);
970         }
971
972         Value *Nucleus::createFPExt(Value *v, Type *destType)
973         {
974                 return createCast(Ice::InstCast::Fpext, v, destType);
975         }
976
977         Value *Nucleus::createBitCast(Value *v, Type *destType)
978         {
979                 return createCast(Ice::InstCast::Bitcast, v, destType);
980         }
981
982         static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs)
983         {
984                 assert(lhs->getType() == rhs->getType());
985
986                 auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType());
987                 auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs);
988                 ::basicBlock->appendInst(cmp);
989
990                 return V(result);
991         }
992
993         Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs)
994         {
995                 return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs);
996         }
997
998         Value *Nucleus::createICmpNE(Value *lhs, Value *rhs)
999         {
1000                 return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs);
1001         }
1002
1003         Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs)
1004         {
1005                 return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs);
1006         }
1007
1008         Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs)
1009         {
1010                 return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs);
1011         }
1012
1013         Value *Nucleus::createICmpULT(Value *lhs, Value *rhs)
1014         {
1015                 return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs);
1016         }
1017
1018         Value *Nucleus::createICmpULE(Value *lhs, Value *rhs)
1019         {
1020                 return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs);
1021         }
1022
1023         Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs)
1024         {
1025                 return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs);
1026         }
1027
1028         Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs)
1029         {
1030                 return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs);
1031         }
1032
1033         Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs)
1034         {
1035                 return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs);
1036         }
1037
1038         Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs)
1039         {
1040                 return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs);
1041         }
1042
1043         static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs)
1044         {
1045                 assert(lhs->getType() == rhs->getType());
1046                 assert(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32);
1047
1048                 auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32);
1049                 auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs);
1050                 ::basicBlock->appendInst(cmp);
1051
1052                 return V(result);
1053         }
1054
1055         Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs)
1056         {
1057                 return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs);
1058         }
1059
1060         Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs)
1061         {
1062                 return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs);
1063         }
1064
1065         Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs)
1066         {
1067                 return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs);
1068         }
1069
1070         Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs)
1071         {
1072                 return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs);
1073         }
1074
1075         Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs)
1076         {
1077                 return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs);
1078         }
1079
1080         Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs)
1081         {
1082                 return createFloatCompare(Ice::InstFcmp::One, lhs, rhs);
1083         }
1084
1085         Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs)
1086         {
1087                 return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs);
1088         }
1089
1090         Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs)
1091         {
1092                 return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs);
1093         }
1094
1095         Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs)
1096         {
1097                 return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs);
1098         }
1099
1100         Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs)
1101         {
1102                 return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs);
1103         }
1104
1105         Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs)
1106         {
1107                 return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs);
1108         }
1109
1110         Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs)
1111         {
1112                 return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs);
1113         }
1114
1115         Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs)
1116         {
1117                 return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs);
1118         }
1119
1120         Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs)
1121         {
1122                 return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs);
1123         }
1124
1125         Value *Nucleus::createExtractElement(Value *vector, Type *type, int index)
1126         {
1127                 auto result = ::function->makeVariable(T(type));
1128                 auto extract = Ice::InstExtractElement::create(::function, result, vector, ::context->getConstantInt32(index));
1129                 ::basicBlock->appendInst(extract);
1130
1131                 return V(result);
1132         }
1133
1134         Value *Nucleus::createInsertElement(Value *vector, Value *element, int index)
1135         {
1136                 auto result = ::function->makeVariable(vector->getType());
1137                 auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index));
1138                 ::basicBlock->appendInst(insert);
1139
1140                 return V(result);
1141         }
1142
1143         Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select)
1144         {
1145                 assert(V1->getType() == V2->getType());
1146
1147                 int size = Ice::typeNumElements(V1->getType());
1148                 auto result = ::function->makeVariable(V1->getType());
1149                 auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2);
1150
1151                 for(int i = 0; i < size; i++)
1152                 {
1153                         shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i])));
1154                 }
1155
1156                 ::basicBlock->appendInst(shuffle);
1157
1158                 return V(result);
1159         }
1160
1161         Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse)
1162         {
1163                 assert(ifTrue->getType() == ifFalse->getType());
1164
1165                 auto result = ::function->makeVariable(ifTrue->getType());
1166                 auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse);
1167                 ::basicBlock->appendInst(select);
1168
1169                 return V(result);
1170         }
1171
1172         SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases)
1173         {
1174                 auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch);
1175                 ::basicBlock->appendInst(switchInst);
1176
1177                 return reinterpret_cast<SwitchCases*>(switchInst);
1178         }
1179
1180         void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch)
1181         {
1182                 switchCases->addBranch(label, label, branch);
1183         }
1184
1185         void Nucleus::createUnreachable()
1186         {
1187                 Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function);
1188                 ::basicBlock->appendInst(unreachable);
1189         }
1190
1191         static Value *createSwizzle4(Value *val, unsigned char select)
1192         {
1193                 int swizzle[4] =
1194                 {
1195                         (select >> 0) & 0x03,
1196                         (select >> 2) & 0x03,
1197                         (select >> 4) & 0x03,
1198                         (select >> 6) & 0x03,
1199                 };
1200
1201                 return Nucleus::createShuffleVector(val, val, swizzle);
1202         }
1203
1204         static Value *createMask4(Value *lhs, Value *rhs, unsigned char select)
1205         {
1206                 int64_t mask[4] = {0, 0, 0, 0};
1207
1208                 mask[(select >> 0) & 0x03] = -1;
1209                 mask[(select >> 2) & 0x03] = -1;
1210                 mask[(select >> 4) & 0x03] = -1;
1211                 mask[(select >> 6) & 0x03] = -1;
1212
1213                 Value *condition = Nucleus::createConstantVector(mask, T(Ice::IceType_v4i1));
1214                 Value *result = Nucleus::createSelect(condition, rhs, lhs);
1215
1216                 return result;
1217         }
1218
1219         Type *Nucleus::getPointerType(Type *ElementType)
1220         {
1221                 if(sizeof(void*) == 8)
1222                 {
1223                         return T(Ice::IceType_i64);
1224                 }
1225                 else
1226                 {
1227                         return T(Ice::IceType_i32);
1228                 }
1229         }
1230
1231         Value *Nucleus::createNullValue(Type *Ty)
1232         {
1233                 if(Ice::isVectorType(T(Ty)))
1234                 {
1235                         assert(Ice::typeNumElements(T(Ty)) <= 16);
1236                         int64_t c[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1237                         return createConstantVector(c, Ty);
1238                 }
1239                 else
1240                 {
1241                         return V(::context->getConstantZero(T(Ty)));
1242                 }
1243         }
1244
1245         Value *Nucleus::createConstantLong(int64_t i)
1246         {
1247                 return V(::context->getConstantInt64(i));
1248         }
1249
1250         Value *Nucleus::createConstantInt(int i)
1251         {
1252                 return V(::context->getConstantInt32(i));
1253         }
1254
1255         Value *Nucleus::createConstantInt(unsigned int i)
1256         {
1257                 return V(::context->getConstantInt32(i));
1258         }
1259
1260         Value *Nucleus::createConstantBool(bool b)
1261         {
1262                 return V(::context->getConstantInt1(b));
1263         }
1264
1265         Value *Nucleus::createConstantByte(signed char i)
1266         {
1267                 return V(::context->getConstantInt8(i));
1268         }
1269
1270         Value *Nucleus::createConstantByte(unsigned char i)
1271         {
1272                 return V(::context->getConstantInt8(i));
1273         }
1274
1275         Value *Nucleus::createConstantShort(short i)
1276         {
1277                 return V(::context->getConstantInt16(i));
1278         }
1279
1280         Value *Nucleus::createConstantShort(unsigned short i)
1281         {
1282                 return V(::context->getConstantInt16(i));
1283         }
1284
1285         Value *Nucleus::createConstantFloat(float x)
1286         {
1287                 return V(::context->getConstantFloat(x));
1288         }
1289
1290         Value *Nucleus::createNullPointer(Type *Ty)
1291         {
1292                 return createNullValue(T(sizeof(void*) == 8 ? Ice::IceType_i64 : Ice::IceType_i32));
1293         }
1294
1295         Value *Nucleus::createConstantVector(const int64_t *constants, Type *type)
1296         {
1297                 const int vectorSize = 16;
1298                 assert(Ice::typeWidthInBytes(T(type)) == vectorSize);
1299                 const int alignment = vectorSize;
1300                 auto globalPool = ::function->getGlobalPool();
1301
1302                 const int64_t *i = constants;
1303                 const double *f = reinterpret_cast<const double*>(constants);
1304                 Ice::VariableDeclaration::DataInitializer *dataInitializer = nullptr;
1305
1306                 switch((int)reinterpret_cast<intptr_t>(type))
1307                 {
1308                 case Ice::IceType_v4i32:
1309                 case Ice::IceType_v4i1:
1310                         {
1311                                 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[2], (int)i[3]};
1312                                 static_assert(sizeof(initializer) == vectorSize, "!");
1313                                 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1314                         }
1315                         break;
1316                 case Ice::IceType_v4f32:
1317                         {
1318                                 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[2], (float)f[3]};
1319                                 static_assert(sizeof(initializer) == vectorSize, "!");
1320                                 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1321                         }
1322                         break;
1323                 case Ice::IceType_v8i16:
1324                 case Ice::IceType_v8i1:
1325                         {
1326                                 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]};
1327                                 static_assert(sizeof(initializer) == vectorSize, "!");
1328                                 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1329                         }
1330                         break;
1331                 case Ice::IceType_v16i8:
1332                 case Ice::IceType_v16i1:
1333                         {
1334                                 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]};
1335                                 static_assert(sizeof(initializer) == vectorSize, "!");
1336                                 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1337                         }
1338                         break;
1339                 case Type_v2i32:
1340                         {
1341                                 const int initializer[4] = {(int)i[0], (int)i[1], (int)i[0], (int)i[1]};
1342                                 static_assert(sizeof(initializer) == vectorSize, "!");
1343                                 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1344                         }
1345                         break;
1346                 case Type_v2f32:
1347                         {
1348                                 const float initializer[4] = {(float)f[0], (float)f[1], (float)f[0], (float)f[1]};
1349                                 static_assert(sizeof(initializer) == vectorSize, "!");
1350                                 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1351                         }
1352                         break;
1353                 case Type_v4i16:
1354                         {
1355                                 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]};
1356                                 static_assert(sizeof(initializer) == vectorSize, "!");
1357                                 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1358                         }
1359                         break;
1360                 case Type_v8i8:
1361                         {
1362                                 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]};
1363                                 static_assert(sizeof(initializer) == vectorSize, "!");
1364                                 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1365                         }
1366                         break;
1367                 case Type_v4i8:
1368                         {
1369                                 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]};
1370                                 static_assert(sizeof(initializer) == vectorSize, "!");
1371                                 dataInitializer = Ice::VariableDeclaration::DataInitializer::create(globalPool, (const char*)initializer, vectorSize);
1372                         }
1373                         break;
1374                 default:
1375                         assert(false && "Unknown constant vector type" && type);
1376                 }
1377
1378                 auto name = Ice::GlobalString::createWithoutString(::context);
1379                 auto *variableDeclaration = Ice::VariableDeclaration::create(globalPool);
1380                 variableDeclaration->setName(name);
1381                 variableDeclaration->setAlignment(alignment);
1382                 variableDeclaration->setIsConstant(true);
1383                 variableDeclaration->addInitializer(dataInitializer);
1384
1385                 ::function->addGlobal(variableDeclaration);
1386
1387                 constexpr int32_t offset = 0;
1388                 Ice::Operand *ptr = ::context->getConstantSym(offset, name);
1389
1390                 Ice::Variable *result = ::function->makeVariable(T(type));
1391                 auto load = Ice::InstLoad::create(::function, result, ptr, alignment);
1392                 ::basicBlock->appendInst(load);
1393
1394                 return V(result);
1395         }
1396
1397         Value *Nucleus::createConstantVector(const double *constants, Type *type)
1398         {
1399                 return createConstantVector((const int64_t*)constants, type);
1400         }
1401
1402         Type *Void::getType()
1403         {
1404                 return T(Ice::IceType_void);
1405         }
1406
1407         Bool::Bool(Argument<Bool> argument)
1408         {
1409                 storeValue(argument.value);
1410         }
1411
1412         Bool::Bool(bool x)
1413         {
1414                 storeValue(Nucleus::createConstantBool(x));
1415         }
1416
1417         Bool::Bool(RValue<Bool> rhs)
1418         {
1419                 storeValue(rhs.value);
1420         }
1421
1422         Bool::Bool(const Bool &rhs)
1423         {
1424                 Value *value = rhs.loadValue();
1425                 storeValue(value);
1426         }
1427
1428         Bool::Bool(const Reference<Bool> &rhs)
1429         {
1430                 Value *value = rhs.loadValue();
1431                 storeValue(value);
1432         }
1433
1434         RValue<Bool> Bool::operator=(RValue<Bool> rhs)
1435         {
1436                 storeValue(rhs.value);
1437
1438                 return rhs;
1439         }
1440
1441         RValue<Bool> Bool::operator=(const Bool &rhs)
1442         {
1443                 Value *value = rhs.loadValue();
1444                 storeValue(value);
1445
1446                 return RValue<Bool>(value);
1447         }
1448
1449         RValue<Bool> Bool::operator=(const Reference<Bool> &rhs)
1450         {
1451                 Value *value = rhs.loadValue();
1452                 storeValue(value);
1453
1454                 return RValue<Bool>(value);
1455         }
1456
1457         RValue<Bool> operator!(RValue<Bool> val)
1458         {
1459                 return RValue<Bool>(Nucleus::createNot(val.value));
1460         }
1461
1462         RValue<Bool> operator&&(RValue<Bool> lhs, RValue<Bool> rhs)
1463         {
1464                 return RValue<Bool>(Nucleus::createAnd(lhs.value, rhs.value));
1465         }
1466
1467         RValue<Bool> operator||(RValue<Bool> lhs, RValue<Bool> rhs)
1468         {
1469                 return RValue<Bool>(Nucleus::createOr(lhs.value, rhs.value));
1470         }
1471
1472         Type *Bool::getType()
1473         {
1474                 return T(Ice::IceType_i1);
1475         }
1476
1477         Byte::Byte(Argument<Byte> argument)
1478         {
1479                 storeValue(argument.value);
1480         }
1481
1482         Byte::Byte(RValue<Int> cast)
1483         {
1484                 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1485
1486                 storeValue(integer);
1487         }
1488
1489         Byte::Byte(RValue<UInt> cast)
1490         {
1491                 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1492
1493                 storeValue(integer);
1494         }
1495
1496         Byte::Byte(RValue<UShort> cast)
1497         {
1498                 Value *integer = Nucleus::createTrunc(cast.value, Byte::getType());
1499
1500                 storeValue(integer);
1501         }
1502
1503         Byte::Byte(int x)
1504         {
1505                 storeValue(Nucleus::createConstantByte((unsigned char)x));
1506         }
1507
1508         Byte::Byte(unsigned char x)
1509         {
1510                 storeValue(Nucleus::createConstantByte(x));
1511         }
1512
1513         Byte::Byte(RValue<Byte> rhs)
1514         {
1515                 storeValue(rhs.value);
1516         }
1517
1518         Byte::Byte(const Byte &rhs)
1519         {
1520                 Value *value = rhs.loadValue();
1521                 storeValue(value);
1522         }
1523
1524         Byte::Byte(const Reference<Byte> &rhs)
1525         {
1526                 Value *value = rhs.loadValue();
1527                 storeValue(value);
1528         }
1529
1530         RValue<Byte> Byte::operator=(RValue<Byte> rhs)
1531         {
1532                 storeValue(rhs.value);
1533
1534                 return rhs;
1535         }
1536
1537         RValue<Byte> Byte::operator=(const Byte &rhs)
1538         {
1539                 Value *value = rhs.loadValue();
1540                 storeValue(value);
1541
1542                 return RValue<Byte>(value);
1543         }
1544
1545         RValue<Byte> Byte::operator=(const Reference<Byte> &rhs)
1546         {
1547                 Value *value = rhs.loadValue();
1548                 storeValue(value);
1549
1550                 return RValue<Byte>(value);
1551         }
1552
1553         RValue<Byte> operator+(RValue<Byte> lhs, RValue<Byte> rhs)
1554         {
1555                 return RValue<Byte>(Nucleus::createAdd(lhs.value, rhs.value));
1556         }
1557
1558         RValue<Byte> operator-(RValue<Byte> lhs, RValue<Byte> rhs)
1559         {
1560                 return RValue<Byte>(Nucleus::createSub(lhs.value, rhs.value));
1561         }
1562
1563         RValue<Byte> operator*(RValue<Byte> lhs, RValue<Byte> rhs)
1564         {
1565                 return RValue<Byte>(Nucleus::createMul(lhs.value, rhs.value));
1566         }
1567
1568         RValue<Byte> operator/(RValue<Byte> lhs, RValue<Byte> rhs)
1569         {
1570                 return RValue<Byte>(Nucleus::createUDiv(lhs.value, rhs.value));
1571         }
1572
1573         RValue<Byte> operator%(RValue<Byte> lhs, RValue<Byte> rhs)
1574         {
1575                 return RValue<Byte>(Nucleus::createURem(lhs.value, rhs.value));
1576         }
1577
1578         RValue<Byte> operator&(RValue<Byte> lhs, RValue<Byte> rhs)
1579         {
1580                 return RValue<Byte>(Nucleus::createAnd(lhs.value, rhs.value));
1581         }
1582
1583         RValue<Byte> operator|(RValue<Byte> lhs, RValue<Byte> rhs)
1584         {
1585                 return RValue<Byte>(Nucleus::createOr(lhs.value, rhs.value));
1586         }
1587
1588         RValue<Byte> operator^(RValue<Byte> lhs, RValue<Byte> rhs)
1589         {
1590                 return RValue<Byte>(Nucleus::createXor(lhs.value, rhs.value));
1591         }
1592
1593         RValue<Byte> operator<<(RValue<Byte> lhs, RValue<Byte> rhs)
1594         {
1595                 return RValue<Byte>(Nucleus::createShl(lhs.value, rhs.value));
1596         }
1597
1598         RValue<Byte> operator>>(RValue<Byte> lhs, RValue<Byte> rhs)
1599         {
1600                 return RValue<Byte>(Nucleus::createLShr(lhs.value, rhs.value));
1601         }
1602
1603         RValue<Byte> operator+=(Byte &lhs, RValue<Byte> rhs)
1604         {
1605                 return lhs = lhs + rhs;
1606         }
1607
1608         RValue<Byte> operator-=(Byte &lhs, RValue<Byte> rhs)
1609         {
1610                 return lhs = lhs - rhs;
1611         }
1612
1613         RValue<Byte> operator*=(Byte &lhs, RValue<Byte> rhs)
1614         {
1615                 return lhs = lhs * rhs;
1616         }
1617
1618         RValue<Byte> operator/=(Byte &lhs, RValue<Byte> rhs)
1619         {
1620                 return lhs = lhs / rhs;
1621         }
1622
1623         RValue<Byte> operator%=(Byte &lhs, RValue<Byte> rhs)
1624         {
1625                 return lhs = lhs % rhs;
1626         }
1627
1628         RValue<Byte> operator&=(Byte &lhs, RValue<Byte> rhs)
1629         {
1630                 return lhs = lhs & rhs;
1631         }
1632
1633         RValue<Byte> operator|=(Byte &lhs, RValue<Byte> rhs)
1634         {
1635                 return lhs = lhs | rhs;
1636         }
1637
1638         RValue<Byte> operator^=(Byte &lhs, RValue<Byte> rhs)
1639         {
1640                 return lhs = lhs ^ rhs;
1641         }
1642
1643         RValue<Byte> operator<<=(Byte &lhs, RValue<Byte> rhs)
1644         {
1645                 return lhs = lhs << rhs;
1646         }
1647
1648         RValue<Byte> operator>>=(Byte &lhs, RValue<Byte> rhs)
1649         {
1650                 return lhs = lhs >> rhs;
1651         }
1652
1653         RValue<Byte> operator+(RValue<Byte> val)
1654         {
1655                 return val;
1656         }
1657
1658         RValue<Byte> operator-(RValue<Byte> val)
1659         {
1660                 return RValue<Byte>(Nucleus::createNeg(val.value));
1661         }
1662
1663         RValue<Byte> operator~(RValue<Byte> val)
1664         {
1665                 return RValue<Byte>(Nucleus::createNot(val.value));
1666         }
1667
1668         RValue<Byte> operator++(Byte &val, int)   // Post-increment
1669         {
1670                 RValue<Byte> res = val;
1671                 val += Byte(1);
1672                 return res;
1673         }
1674
1675         const Byte &operator++(Byte &val)   // Pre-increment
1676         {
1677                 val += Byte(1);
1678                 return val;
1679         }
1680
1681         RValue<Byte> operator--(Byte &val, int)   // Post-decrement
1682         {
1683                 RValue<Byte> res = val;
1684                 val -= Byte(1);
1685                 return res;
1686         }
1687
1688         const Byte &operator--(Byte &val)   // Pre-decrement
1689         {
1690                 val -= Byte(1);
1691                 return val;
1692         }
1693
1694         RValue<Bool> operator<(RValue<Byte> lhs, RValue<Byte> rhs)
1695         {
1696                 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
1697         }
1698
1699         RValue<Bool> operator<=(RValue<Byte> lhs, RValue<Byte> rhs)
1700         {
1701                 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
1702         }
1703
1704         RValue<Bool> operator>(RValue<Byte> lhs, RValue<Byte> rhs)
1705         {
1706                 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
1707         }
1708
1709         RValue<Bool> operator>=(RValue<Byte> lhs, RValue<Byte> rhs)
1710         {
1711                 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
1712         }
1713
1714         RValue<Bool> operator!=(RValue<Byte> lhs, RValue<Byte> rhs)
1715         {
1716                 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
1717         }
1718
1719         RValue<Bool> operator==(RValue<Byte> lhs, RValue<Byte> rhs)
1720         {
1721                 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
1722         }
1723
1724         Type *Byte::getType()
1725         {
1726                 return T(Ice::IceType_i8);
1727         }
1728
1729         SByte::SByte(Argument<SByte> argument)
1730         {
1731                 storeValue(argument.value);
1732         }
1733
1734         SByte::SByte(RValue<Int> cast)
1735         {
1736                 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1737
1738                 storeValue(integer);
1739         }
1740
1741         SByte::SByte(RValue<Short> cast)
1742         {
1743                 Value *integer = Nucleus::createTrunc(cast.value, SByte::getType());
1744
1745                 storeValue(integer);
1746         }
1747
1748         SByte::SByte(signed char x)
1749         {
1750                 storeValue(Nucleus::createConstantByte(x));
1751         }
1752
1753         SByte::SByte(RValue<SByte> rhs)
1754         {
1755                 storeValue(rhs.value);
1756         }
1757
1758         SByte::SByte(const SByte &rhs)
1759         {
1760                 Value *value = rhs.loadValue();
1761                 storeValue(value);
1762         }
1763
1764         SByte::SByte(const Reference<SByte> &rhs)
1765         {
1766                 Value *value = rhs.loadValue();
1767                 storeValue(value);
1768         }
1769
1770         RValue<SByte> SByte::operator=(RValue<SByte> rhs)
1771         {
1772                 storeValue(rhs.value);
1773
1774                 return rhs;
1775         }
1776
1777         RValue<SByte> SByte::operator=(const SByte &rhs)
1778         {
1779                 Value *value = rhs.loadValue();
1780                 storeValue(value);
1781
1782                 return RValue<SByte>(value);
1783         }
1784
1785         RValue<SByte> SByte::operator=(const Reference<SByte> &rhs)
1786         {
1787                 Value *value = rhs.loadValue();
1788                 storeValue(value);
1789
1790                 return RValue<SByte>(value);
1791         }
1792
1793         RValue<SByte> operator+(RValue<SByte> lhs, RValue<SByte> rhs)
1794         {
1795                 return RValue<SByte>(Nucleus::createAdd(lhs.value, rhs.value));
1796         }
1797
1798         RValue<SByte> operator-(RValue<SByte> lhs, RValue<SByte> rhs)
1799         {
1800                 return RValue<SByte>(Nucleus::createSub(lhs.value, rhs.value));
1801         }
1802
1803         RValue<SByte> operator*(RValue<SByte> lhs, RValue<SByte> rhs)
1804         {
1805                 return RValue<SByte>(Nucleus::createMul(lhs.value, rhs.value));
1806         }
1807
1808         RValue<SByte> operator/(RValue<SByte> lhs, RValue<SByte> rhs)
1809         {
1810                 return RValue<SByte>(Nucleus::createSDiv(lhs.value, rhs.value));
1811         }
1812
1813         RValue<SByte> operator%(RValue<SByte> lhs, RValue<SByte> rhs)
1814         {
1815                 return RValue<SByte>(Nucleus::createSRem(lhs.value, rhs.value));
1816         }
1817
1818         RValue<SByte> operator&(RValue<SByte> lhs, RValue<SByte> rhs)
1819         {
1820                 return RValue<SByte>(Nucleus::createAnd(lhs.value, rhs.value));
1821         }
1822
1823         RValue<SByte> operator|(RValue<SByte> lhs, RValue<SByte> rhs)
1824         {
1825                 return RValue<SByte>(Nucleus::createOr(lhs.value, rhs.value));
1826         }
1827
1828         RValue<SByte> operator^(RValue<SByte> lhs, RValue<SByte> rhs)
1829         {
1830                 return RValue<SByte>(Nucleus::createXor(lhs.value, rhs.value));
1831         }
1832
1833         RValue<SByte> operator<<(RValue<SByte> lhs, RValue<SByte> rhs)
1834         {
1835                 return RValue<SByte>(Nucleus::createShl(lhs.value, rhs.value));
1836         }
1837
1838         RValue<SByte> operator>>(RValue<SByte> lhs, RValue<SByte> rhs)
1839         {
1840                 return RValue<SByte>(Nucleus::createAShr(lhs.value, rhs.value));
1841         }
1842
1843         RValue<SByte> operator+=(SByte &lhs, RValue<SByte> rhs)
1844         {
1845                 return lhs = lhs + rhs;
1846         }
1847
1848         RValue<SByte> operator-=(SByte &lhs, RValue<SByte> rhs)
1849         {
1850                 return lhs = lhs - rhs;
1851         }
1852
1853         RValue<SByte> operator*=(SByte &lhs, RValue<SByte> rhs)
1854         {
1855                 return lhs = lhs * rhs;
1856         }
1857
1858         RValue<SByte> operator/=(SByte &lhs, RValue<SByte> rhs)
1859         {
1860                 return lhs = lhs / rhs;
1861         }
1862
1863         RValue<SByte> operator%=(SByte &lhs, RValue<SByte> rhs)
1864         {
1865                 return lhs = lhs % rhs;
1866         }
1867
1868         RValue<SByte> operator&=(SByte &lhs, RValue<SByte> rhs)
1869         {
1870                 return lhs = lhs & rhs;
1871         }
1872
1873         RValue<SByte> operator|=(SByte &lhs, RValue<SByte> rhs)
1874         {
1875                 return lhs = lhs | rhs;
1876         }
1877
1878         RValue<SByte> operator^=(SByte &lhs, RValue<SByte> rhs)
1879         {
1880                 return lhs = lhs ^ rhs;
1881         }
1882
1883         RValue<SByte> operator<<=(SByte &lhs, RValue<SByte> rhs)
1884         {
1885                 return lhs = lhs << rhs;
1886         }
1887
1888         RValue<SByte> operator>>=(SByte &lhs, RValue<SByte> rhs)
1889         {
1890                 return lhs = lhs >> rhs;
1891         }
1892
1893         RValue<SByte> operator+(RValue<SByte> val)
1894         {
1895                 return val;
1896         }
1897
1898         RValue<SByte> operator-(RValue<SByte> val)
1899         {
1900                 return RValue<SByte>(Nucleus::createNeg(val.value));
1901         }
1902
1903         RValue<SByte> operator~(RValue<SByte> val)
1904         {
1905                 return RValue<SByte>(Nucleus::createNot(val.value));
1906         }
1907
1908         RValue<SByte> operator++(SByte &val, int)   // Post-increment
1909         {
1910                 RValue<SByte> res = val;
1911                 val += SByte(1);
1912                 return res;
1913         }
1914
1915         const SByte &operator++(SByte &val)   // Pre-increment
1916         {
1917                 val += SByte(1);
1918                 return val;
1919         }
1920
1921         RValue<SByte> operator--(SByte &val, int)   // Post-decrement
1922         {
1923                 RValue<SByte> res = val;
1924                 val -= SByte(1);
1925                 return res;
1926         }
1927
1928         const SByte &operator--(SByte &val)   // Pre-decrement
1929         {
1930                 val -= SByte(1);
1931                 return val;
1932         }
1933
1934         RValue<Bool> operator<(RValue<SByte> lhs, RValue<SByte> rhs)
1935         {
1936                 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
1937         }
1938
1939         RValue<Bool> operator<=(RValue<SByte> lhs, RValue<SByte> rhs)
1940         {
1941                 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
1942         }
1943
1944         RValue<Bool> operator>(RValue<SByte> lhs, RValue<SByte> rhs)
1945         {
1946                 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
1947         }
1948
1949         RValue<Bool> operator>=(RValue<SByte> lhs, RValue<SByte> rhs)
1950         {
1951                 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
1952         }
1953
1954         RValue<Bool> operator!=(RValue<SByte> lhs, RValue<SByte> rhs)
1955         {
1956                 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
1957         }
1958
1959         RValue<Bool> operator==(RValue<SByte> lhs, RValue<SByte> rhs)
1960         {
1961                 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
1962         }
1963
1964         Type *SByte::getType()
1965         {
1966                 return T(Ice::IceType_i8);
1967         }
1968
1969         Short::Short(Argument<Short> argument)
1970         {
1971                 storeValue(argument.value);
1972         }
1973
1974         Short::Short(RValue<Int> cast)
1975         {
1976                 Value *integer = Nucleus::createTrunc(cast.value, Short::getType());
1977
1978                 storeValue(integer);
1979         }
1980
1981         Short::Short(short x)
1982         {
1983                 storeValue(Nucleus::createConstantShort(x));
1984         }
1985
1986         Short::Short(RValue<Short> rhs)
1987         {
1988                 storeValue(rhs.value);
1989         }
1990
1991         Short::Short(const Short &rhs)
1992         {
1993                 Value *value = rhs.loadValue();
1994                 storeValue(value);
1995         }
1996
1997         Short::Short(const Reference<Short> &rhs)
1998         {
1999                 Value *value = rhs.loadValue();
2000                 storeValue(value);
2001         }
2002
2003         RValue<Short> Short::operator=(RValue<Short> rhs)
2004         {
2005                 storeValue(rhs.value);
2006
2007                 return rhs;
2008         }
2009
2010         RValue<Short> Short::operator=(const Short &rhs)
2011         {
2012                 Value *value = rhs.loadValue();
2013                 storeValue(value);
2014
2015                 return RValue<Short>(value);
2016         }
2017
2018         RValue<Short> Short::operator=(const Reference<Short> &rhs)
2019         {
2020                 Value *value = rhs.loadValue();
2021                 storeValue(value);
2022
2023                 return RValue<Short>(value);
2024         }
2025
2026         RValue<Short> operator+(RValue<Short> lhs, RValue<Short> rhs)
2027         {
2028                 return RValue<Short>(Nucleus::createAdd(lhs.value, rhs.value));
2029         }
2030
2031         RValue<Short> operator-(RValue<Short> lhs, RValue<Short> rhs)
2032         {
2033                 return RValue<Short>(Nucleus::createSub(lhs.value, rhs.value));
2034         }
2035
2036         RValue<Short> operator*(RValue<Short> lhs, RValue<Short> rhs)
2037         {
2038                 return RValue<Short>(Nucleus::createMul(lhs.value, rhs.value));
2039         }
2040
2041         RValue<Short> operator/(RValue<Short> lhs, RValue<Short> rhs)
2042         {
2043                 return RValue<Short>(Nucleus::createSDiv(lhs.value, rhs.value));
2044         }
2045
2046         RValue<Short> operator%(RValue<Short> lhs, RValue<Short> rhs)
2047         {
2048                 return RValue<Short>(Nucleus::createSRem(lhs.value, rhs.value));
2049         }
2050
2051         RValue<Short> operator&(RValue<Short> lhs, RValue<Short> rhs)
2052         {
2053                 return RValue<Short>(Nucleus::createAnd(lhs.value, rhs.value));
2054         }
2055
2056         RValue<Short> operator|(RValue<Short> lhs, RValue<Short> rhs)
2057         {
2058                 return RValue<Short>(Nucleus::createOr(lhs.value, rhs.value));
2059         }
2060
2061         RValue<Short> operator^(RValue<Short> lhs, RValue<Short> rhs)
2062         {
2063                 return RValue<Short>(Nucleus::createXor(lhs.value, rhs.value));
2064         }
2065
2066         RValue<Short> operator<<(RValue<Short> lhs, RValue<Short> rhs)
2067         {
2068                 return RValue<Short>(Nucleus::createShl(lhs.value, rhs.value));
2069         }
2070
2071         RValue<Short> operator>>(RValue<Short> lhs, RValue<Short> rhs)
2072         {
2073                 return RValue<Short>(Nucleus::createAShr(lhs.value, rhs.value));
2074         }
2075
2076         RValue<Short> operator+=(Short &lhs, RValue<Short> rhs)
2077         {
2078                 return lhs = lhs + rhs;
2079         }
2080
2081         RValue<Short> operator-=(Short &lhs, RValue<Short> rhs)
2082         {
2083                 return lhs = lhs - rhs;
2084         }
2085
2086         RValue<Short> operator*=(Short &lhs, RValue<Short> rhs)
2087         {
2088                 return lhs = lhs * rhs;
2089         }
2090
2091         RValue<Short> operator/=(Short &lhs, RValue<Short> rhs)
2092         {
2093                 return lhs = lhs / rhs;
2094         }
2095
2096         RValue<Short> operator%=(Short &lhs, RValue<Short> rhs)
2097         {
2098                 return lhs = lhs % rhs;
2099         }
2100
2101         RValue<Short> operator&=(Short &lhs, RValue<Short> rhs)
2102         {
2103                 return lhs = lhs & rhs;
2104         }
2105
2106         RValue<Short> operator|=(Short &lhs, RValue<Short> rhs)
2107         {
2108                 return lhs = lhs | rhs;
2109         }
2110
2111         RValue<Short> operator^=(Short &lhs, RValue<Short> rhs)
2112         {
2113                 return lhs = lhs ^ rhs;
2114         }
2115
2116         RValue<Short> operator<<=(Short &lhs, RValue<Short> rhs)
2117         {
2118                 return lhs = lhs << rhs;
2119         }
2120
2121         RValue<Short> operator>>=(Short &lhs, RValue<Short> rhs)
2122         {
2123                 return lhs = lhs >> rhs;
2124         }
2125
2126         RValue<Short> operator+(RValue<Short> val)
2127         {
2128                 return val;
2129         }
2130
2131         RValue<Short> operator-(RValue<Short> val)
2132         {
2133                 return RValue<Short>(Nucleus::createNeg(val.value));
2134         }
2135
2136         RValue<Short> operator~(RValue<Short> val)
2137         {
2138                 return RValue<Short>(Nucleus::createNot(val.value));
2139         }
2140
2141         RValue<Short> operator++(Short &val, int)   // Post-increment
2142         {
2143                 RValue<Short> res = val;
2144                 val += Short(1);
2145                 return res;
2146         }
2147
2148         const Short &operator++(Short &val)   // Pre-increment
2149         {
2150                 val += Short(1);
2151                 return val;
2152         }
2153
2154         RValue<Short> operator--(Short &val, int)   // Post-decrement
2155         {
2156                 RValue<Short> res = val;
2157                 val -= Short(1);
2158                 return res;
2159         }
2160
2161         const Short &operator--(Short &val)   // Pre-decrement
2162         {
2163                 val -= Short(1);
2164                 return val;
2165         }
2166
2167         RValue<Bool> operator<(RValue<Short> lhs, RValue<Short> rhs)
2168         {
2169                 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
2170         }
2171
2172         RValue<Bool> operator<=(RValue<Short> lhs, RValue<Short> rhs)
2173         {
2174                 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
2175         }
2176
2177         RValue<Bool> operator>(RValue<Short> lhs, RValue<Short> rhs)
2178         {
2179                 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
2180         }
2181
2182         RValue<Bool> operator>=(RValue<Short> lhs, RValue<Short> rhs)
2183         {
2184                 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
2185         }
2186
2187         RValue<Bool> operator!=(RValue<Short> lhs, RValue<Short> rhs)
2188         {
2189                 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2190         }
2191
2192         RValue<Bool> operator==(RValue<Short> lhs, RValue<Short> rhs)
2193         {
2194                 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2195         }
2196
2197         Type *Short::getType()
2198         {
2199                 return T(Ice::IceType_i16);
2200         }
2201
2202         UShort::UShort(Argument<UShort> argument)
2203         {
2204                 storeValue(argument.value);
2205         }
2206
2207         UShort::UShort(RValue<UInt> cast)
2208         {
2209                 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2210
2211                 storeValue(integer);
2212         }
2213
2214         UShort::UShort(RValue<Int> cast)
2215         {
2216                 Value *integer = Nucleus::createTrunc(cast.value, UShort::getType());
2217
2218                 storeValue(integer);
2219         }
2220
2221         UShort::UShort(unsigned short x)
2222         {
2223                 storeValue(Nucleus::createConstantShort(x));
2224         }
2225
2226         UShort::UShort(RValue<UShort> rhs)
2227         {
2228                 storeValue(rhs.value);
2229         }
2230
2231         UShort::UShort(const UShort &rhs)
2232         {
2233                 Value *value = rhs.loadValue();
2234                 storeValue(value);
2235         }
2236
2237         UShort::UShort(const Reference<UShort> &rhs)
2238         {
2239                 Value *value = rhs.loadValue();
2240                 storeValue(value);
2241         }
2242
2243         RValue<UShort> UShort::operator=(RValue<UShort> rhs)
2244         {
2245                 storeValue(rhs.value);
2246
2247                 return rhs;
2248         }
2249
2250         RValue<UShort> UShort::operator=(const UShort &rhs)
2251         {
2252                 Value *value = rhs.loadValue();
2253                 storeValue(value);
2254
2255                 return RValue<UShort>(value);
2256         }
2257
2258         RValue<UShort> UShort::operator=(const Reference<UShort> &rhs)
2259         {
2260                 Value *value = rhs.loadValue();
2261                 storeValue(value);
2262
2263                 return RValue<UShort>(value);
2264         }
2265
2266         RValue<UShort> operator+(RValue<UShort> lhs, RValue<UShort> rhs)
2267         {
2268                 return RValue<UShort>(Nucleus::createAdd(lhs.value, rhs.value));
2269         }
2270
2271         RValue<UShort> operator-(RValue<UShort> lhs, RValue<UShort> rhs)
2272         {
2273                 return RValue<UShort>(Nucleus::createSub(lhs.value, rhs.value));
2274         }
2275
2276         RValue<UShort> operator*(RValue<UShort> lhs, RValue<UShort> rhs)
2277         {
2278                 return RValue<UShort>(Nucleus::createMul(lhs.value, rhs.value));
2279         }
2280
2281         RValue<UShort> operator/(RValue<UShort> lhs, RValue<UShort> rhs)
2282         {
2283                 return RValue<UShort>(Nucleus::createUDiv(lhs.value, rhs.value));
2284         }
2285
2286         RValue<UShort> operator%(RValue<UShort> lhs, RValue<UShort> rhs)
2287         {
2288                 return RValue<UShort>(Nucleus::createURem(lhs.value, rhs.value));
2289         }
2290
2291         RValue<UShort> operator&(RValue<UShort> lhs, RValue<UShort> rhs)
2292         {
2293                 return RValue<UShort>(Nucleus::createAnd(lhs.value, rhs.value));
2294         }
2295
2296         RValue<UShort> operator|(RValue<UShort> lhs, RValue<UShort> rhs)
2297         {
2298                 return RValue<UShort>(Nucleus::createOr(lhs.value, rhs.value));
2299         }
2300
2301         RValue<UShort> operator^(RValue<UShort> lhs, RValue<UShort> rhs)
2302         {
2303                 return RValue<UShort>(Nucleus::createXor(lhs.value, rhs.value));
2304         }
2305
2306         RValue<UShort> operator<<(RValue<UShort> lhs, RValue<UShort> rhs)
2307         {
2308                 return RValue<UShort>(Nucleus::createShl(lhs.value, rhs.value));
2309         }
2310
2311         RValue<UShort> operator>>(RValue<UShort> lhs, RValue<UShort> rhs)
2312         {
2313                 return RValue<UShort>(Nucleus::createLShr(lhs.value, rhs.value));
2314         }
2315
2316         RValue<UShort> operator+=(UShort &lhs, RValue<UShort> rhs)
2317         {
2318                 return lhs = lhs + rhs;
2319         }
2320
2321         RValue<UShort> operator-=(UShort &lhs, RValue<UShort> rhs)
2322         {
2323                 return lhs = lhs - rhs;
2324         }
2325
2326         RValue<UShort> operator*=(UShort &lhs, RValue<UShort> rhs)
2327         {
2328                 return lhs = lhs * rhs;
2329         }
2330
2331         RValue<UShort> operator/=(UShort &lhs, RValue<UShort> rhs)
2332         {
2333                 return lhs = lhs / rhs;
2334         }
2335
2336         RValue<UShort> operator%=(UShort &lhs, RValue<UShort> rhs)
2337         {
2338                 return lhs = lhs % rhs;
2339         }
2340
2341         RValue<UShort> operator&=(UShort &lhs, RValue<UShort> rhs)
2342         {
2343                 return lhs = lhs & rhs;
2344         }
2345
2346         RValue<UShort> operator|=(UShort &lhs, RValue<UShort> rhs)
2347         {
2348                 return lhs = lhs | rhs;
2349         }
2350
2351         RValue<UShort> operator^=(UShort &lhs, RValue<UShort> rhs)
2352         {
2353                 return lhs = lhs ^ rhs;
2354         }
2355
2356         RValue<UShort> operator<<=(UShort &lhs, RValue<UShort> rhs)
2357         {
2358                 return lhs = lhs << rhs;
2359         }
2360
2361         RValue<UShort> operator>>=(UShort &lhs, RValue<UShort> rhs)
2362         {
2363                 return lhs = lhs >> rhs;
2364         }
2365
2366         RValue<UShort> operator+(RValue<UShort> val)
2367         {
2368                 return val;
2369         }
2370
2371         RValue<UShort> operator-(RValue<UShort> val)
2372         {
2373                 return RValue<UShort>(Nucleus::createNeg(val.value));
2374         }
2375
2376         RValue<UShort> operator~(RValue<UShort> val)
2377         {
2378                 return RValue<UShort>(Nucleus::createNot(val.value));
2379         }
2380
2381         RValue<UShort> operator++(UShort &val, int)   // Post-increment
2382         {
2383                 RValue<UShort> res = val;
2384                 val += UShort(1);
2385                 return res;
2386         }
2387
2388         const UShort &operator++(UShort &val)   // Pre-increment
2389         {
2390                 val += UShort(1);
2391                 return val;
2392         }
2393
2394         RValue<UShort> operator--(UShort &val, int)   // Post-decrement
2395         {
2396                 RValue<UShort> res = val;
2397                 val -= UShort(1);
2398                 return res;
2399         }
2400
2401         const UShort &operator--(UShort &val)   // Pre-decrement
2402         {
2403                 val -= UShort(1);
2404                 return val;
2405         }
2406
2407         RValue<Bool> operator<(RValue<UShort> lhs, RValue<UShort> rhs)
2408         {
2409                 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
2410         }
2411
2412         RValue<Bool> operator<=(RValue<UShort> lhs, RValue<UShort> rhs)
2413         {
2414                 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
2415         }
2416
2417         RValue<Bool> operator>(RValue<UShort> lhs, RValue<UShort> rhs)
2418         {
2419                 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
2420         }
2421
2422         RValue<Bool> operator>=(RValue<UShort> lhs, RValue<UShort> rhs)
2423         {
2424                 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
2425         }
2426
2427         RValue<Bool> operator!=(RValue<UShort> lhs, RValue<UShort> rhs)
2428         {
2429                 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
2430         }
2431
2432         RValue<Bool> operator==(RValue<UShort> lhs, RValue<UShort> rhs)
2433         {
2434                 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
2435         }
2436
2437         Type *UShort::getType()
2438         {
2439                 return T(Ice::IceType_i16);
2440         }
2441
2442         Byte4::Byte4(RValue<Byte8> cast)
2443         {
2444                 storeValue(Nucleus::createBitCast(cast.value, getType()));
2445         }
2446
2447         Byte4::Byte4(const Reference<Byte4> &rhs)
2448         {
2449                 Value *value = rhs.loadValue();
2450                 storeValue(value);
2451         }
2452
2453         Type *Byte4::getType()
2454         {
2455                 return T(Type_v4i8);
2456         }
2457
2458         Type *SByte4::getType()
2459         {
2460                 return T(Type_v4i8);
2461         }
2462
2463         Byte8::Byte8(uint8_t x0, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4, uint8_t x5, uint8_t x6, uint8_t x7)
2464         {
2465                 int64_t constantVector[8] = {x0, x1, x2, x3, x4, x5, x6, x7};
2466                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
2467         }
2468
2469         Byte8::Byte8(RValue<Byte8> rhs)
2470         {
2471                 storeValue(rhs.value);
2472         }
2473
2474         Byte8::Byte8(const Byte8 &rhs)
2475         {
2476                 Value *value = rhs.loadValue();
2477                 storeValue(value);
2478         }
2479
2480         Byte8::Byte8(const Reference<Byte8> &rhs)
2481         {
2482                 Value *value = rhs.loadValue();
2483                 storeValue(value);
2484         }
2485
2486         RValue<Byte8> Byte8::operator=(RValue<Byte8> rhs)
2487         {
2488                 storeValue(rhs.value);
2489
2490                 return rhs;
2491         }
2492
2493         RValue<Byte8> Byte8::operator=(const Byte8 &rhs)
2494         {
2495                 Value *value = rhs.loadValue();
2496                 storeValue(value);
2497
2498                 return RValue<Byte8>(value);
2499         }
2500
2501         RValue<Byte8> Byte8::operator=(const Reference<Byte8> &rhs)
2502         {
2503                 Value *value = rhs.loadValue();
2504                 storeValue(value);
2505
2506                 return RValue<Byte8>(value);
2507         }
2508
2509         RValue<Byte8> operator+(RValue<Byte8> lhs, RValue<Byte8> rhs)
2510         {
2511                 return RValue<Byte8>(Nucleus::createAdd(lhs.value, rhs.value));
2512         }
2513
2514         RValue<Byte8> operator-(RValue<Byte8> lhs, RValue<Byte8> rhs)
2515         {
2516                 return RValue<Byte8>(Nucleus::createSub(lhs.value, rhs.value));
2517         }
2518
2519 //      RValue<Byte8> operator*(RValue<Byte8> lhs, RValue<Byte8> rhs)
2520 //      {
2521 //              return RValue<Byte8>(Nucleus::createMul(lhs.value, rhs.value));
2522 //      }
2523
2524 //      RValue<Byte8> operator/(RValue<Byte8> lhs, RValue<Byte8> rhs)
2525 //      {
2526 //              return RValue<Byte8>(Nucleus::createUDiv(lhs.value, rhs.value));
2527 //      }
2528
2529 //      RValue<Byte8> operator%(RValue<Byte8> lhs, RValue<Byte8> rhs)
2530 //      {
2531 //              return RValue<Byte8>(Nucleus::createURem(lhs.value, rhs.value));
2532 //      }
2533
2534         RValue<Byte8> operator&(RValue<Byte8> lhs, RValue<Byte8> rhs)
2535         {
2536                 return RValue<Byte8>(Nucleus::createAnd(lhs.value, rhs.value));
2537         }
2538
2539         RValue<Byte8> operator|(RValue<Byte8> lhs, RValue<Byte8> rhs)
2540         {
2541                 return RValue<Byte8>(Nucleus::createOr(lhs.value, rhs.value));
2542         }
2543
2544         RValue<Byte8> operator^(RValue<Byte8> lhs, RValue<Byte8> rhs)
2545         {
2546                 return RValue<Byte8>(Nucleus::createXor(lhs.value, rhs.value));
2547         }
2548
2549 //      RValue<Byte8> operator<<(RValue<Byte8> lhs, unsigned char rhs)
2550 //      {
2551 //              return RValue<Byte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
2552 //      }
2553
2554 //      RValue<Byte8> operator>>(RValue<Byte8> lhs, unsigned char rhs)
2555 //      {
2556 //              return RValue<Byte8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
2557 //      }
2558
2559         RValue<Byte8> operator+=(Byte8 &lhs, RValue<Byte8> rhs)
2560         {
2561                 return lhs = lhs + rhs;
2562         }
2563
2564         RValue<Byte8> operator-=(Byte8 &lhs, RValue<Byte8> rhs)
2565         {
2566                 return lhs = lhs - rhs;
2567         }
2568
2569 //      RValue<Byte8> operator*=(Byte8 &lhs, RValue<Byte8> rhs)
2570 //      {
2571 //              return lhs = lhs * rhs;
2572 //      }
2573
2574 //      RValue<Byte8> operator/=(Byte8 &lhs, RValue<Byte8> rhs)
2575 //      {
2576 //              return lhs = lhs / rhs;
2577 //      }
2578
2579 //      RValue<Byte8> operator%=(Byte8 &lhs, RValue<Byte8> rhs)
2580 //      {
2581 //              return lhs = lhs % rhs;
2582 //      }
2583
2584         RValue<Byte8> operator&=(Byte8 &lhs, RValue<Byte8> rhs)
2585         {
2586                 return lhs = lhs & rhs;
2587         }
2588
2589         RValue<Byte8> operator|=(Byte8 &lhs, RValue<Byte8> rhs)
2590         {
2591                 return lhs = lhs | rhs;
2592         }
2593
2594         RValue<Byte8> operator^=(Byte8 &lhs, RValue<Byte8> rhs)
2595         {
2596                 return lhs = lhs ^ rhs;
2597         }
2598
2599 //      RValue<Byte8> operator<<=(Byte8 &lhs, RValue<Byte8> rhs)
2600 //      {
2601 //              return lhs = lhs << rhs;
2602 //      }
2603
2604 //      RValue<Byte8> operator>>=(Byte8 &lhs, RValue<Byte8> rhs)
2605 //      {
2606 //              return lhs = lhs >> rhs;
2607 //      }
2608
2609 //      RValue<Byte8> operator+(RValue<Byte8> val)
2610 //      {
2611 //              return val;
2612 //      }
2613
2614 //      RValue<Byte8> operator-(RValue<Byte8> val)
2615 //      {
2616 //              return RValue<Byte8>(Nucleus::createNeg(val.value));
2617 //      }
2618
2619         RValue<Byte8> operator~(RValue<Byte8> val)
2620         {
2621                 return RValue<Byte8>(Nucleus::createNot(val.value));
2622         }
2623
2624         RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y)
2625         {
2626                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2627                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2628                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2629                 auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2630                 paddusb->addArg(x.value);
2631                 paddusb->addArg(y.value);
2632                 ::basicBlock->appendInst(paddusb);
2633
2634                 return RValue<Byte8>(V(result));
2635         }
2636
2637         RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y)
2638         {
2639                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2640                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2641                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2642                 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2643                 psubusw->addArg(x.value);
2644                 psubusw->addArg(y.value);
2645                 ::basicBlock->appendInst(psubusw);
2646
2647                 return RValue<Byte8>(V(result));
2648         }
2649
2650         RValue<Short4> Unpack(RValue<Byte4> x)
2651         {
2652                 int shuffle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7};   // Real type is v16i8
2653                 return RValue<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
2654         }
2655
2656         RValue<Short4> Unpack(RValue<Byte4> x, RValue<Byte4> y)
2657         {
2658                 return UnpackLow(As<Byte8>(x), As<Byte8>(y));
2659         }
2660
2661         RValue<Short4> UnpackLow(RValue<Byte8> x, RValue<Byte8> y)
2662         {
2663                 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};   // Real type is v16i8
2664                 return RValue<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2665         }
2666
2667         RValue<Short4> UnpackHigh(RValue<Byte8> x, RValue<Byte8> y)
2668         {
2669                 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};   // Real type is v16i8
2670                 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2671                 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
2672         }
2673
2674         RValue<Int> SignMask(RValue<Byte8> x)
2675         {
2676                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2677                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2678                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2679                 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2680                 movmsk->addArg(x.value);
2681                 ::basicBlock->appendInst(movmsk);
2682
2683                 return RValue<Int>(V(result));
2684         }
2685
2686 //      RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y)
2687 //      {
2688 //              return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value, y.value));
2689 //      }
2690
2691         RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y)
2692         {
2693                 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
2694         }
2695
2696         Type *Byte8::getType()
2697         {
2698                 return T(Type_v8i8);
2699         }
2700
2701         SByte8::SByte8(uint8_t x0, uint8_t x1, uint8_t x2, uint8_t x3, uint8_t x4, uint8_t x5, uint8_t x6, uint8_t x7)
2702         {
2703                 int64_t constantVector[8] = { x0, x1, x2, x3, x4, x5, x6, x7 };
2704                 Value *vector = V(Nucleus::createConstantVector(constantVector, getType()));
2705
2706                 storeValue(Nucleus::createBitCast(vector, getType()));
2707         }
2708
2709         SByte8::SByte8(RValue<SByte8> rhs)
2710         {
2711                 storeValue(rhs.value);
2712         }
2713
2714         SByte8::SByte8(const SByte8 &rhs)
2715         {
2716                 Value *value = rhs.loadValue();
2717                 storeValue(value);
2718         }
2719
2720         SByte8::SByte8(const Reference<SByte8> &rhs)
2721         {
2722                 Value *value = rhs.loadValue();
2723                 storeValue(value);
2724         }
2725
2726         RValue<SByte8> SByte8::operator=(RValue<SByte8> rhs)
2727         {
2728                 storeValue(rhs.value);
2729
2730                 return rhs;
2731         }
2732
2733         RValue<SByte8> SByte8::operator=(const SByte8 &rhs)
2734         {
2735                 Value *value = rhs.loadValue();
2736                 storeValue(value);
2737
2738                 return RValue<SByte8>(value);
2739         }
2740
2741         RValue<SByte8> SByte8::operator=(const Reference<SByte8> &rhs)
2742         {
2743                 Value *value = rhs.loadValue();
2744                 storeValue(value);
2745
2746                 return RValue<SByte8>(value);
2747         }
2748
2749         RValue<SByte8> operator+(RValue<SByte8> lhs, RValue<SByte8> rhs)
2750         {
2751                 return RValue<SByte8>(Nucleus::createAdd(lhs.value, rhs.value));
2752         }
2753
2754         RValue<SByte8> operator-(RValue<SByte8> lhs, RValue<SByte8> rhs)
2755         {
2756                 return RValue<SByte8>(Nucleus::createSub(lhs.value, rhs.value));
2757         }
2758
2759 //      RValue<SByte8> operator*(RValue<SByte8> lhs, RValue<SByte8> rhs)
2760 //      {
2761 //              return RValue<SByte8>(Nucleus::createMul(lhs.value, rhs.value));
2762 //      }
2763
2764 //      RValue<SByte8> operator/(RValue<SByte8> lhs, RValue<SByte8> rhs)
2765 //      {
2766 //              return RValue<SByte8>(Nucleus::createSDiv(lhs.value, rhs.value));
2767 //      }
2768
2769 //      RValue<SByte8> operator%(RValue<SByte8> lhs, RValue<SByte8> rhs)
2770 //      {
2771 //              return RValue<SByte8>(Nucleus::createSRem(lhs.value, rhs.value));
2772 //      }
2773
2774         RValue<SByte8> operator&(RValue<SByte8> lhs, RValue<SByte8> rhs)
2775         {
2776                 return RValue<SByte8>(Nucleus::createAnd(lhs.value, rhs.value));
2777         }
2778
2779         RValue<SByte8> operator|(RValue<SByte8> lhs, RValue<SByte8> rhs)
2780         {
2781                 return RValue<SByte8>(Nucleus::createOr(lhs.value, rhs.value));
2782         }
2783
2784         RValue<SByte8> operator^(RValue<SByte8> lhs, RValue<SByte8> rhs)
2785         {
2786                 return RValue<SByte8>(Nucleus::createXor(lhs.value, rhs.value));
2787         }
2788
2789 //      RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs)
2790 //      {
2791 //              return RValue<SByte8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
2792 //      }
2793
2794 //      RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs)
2795 //      {
2796 //              return RValue<SByte8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
2797 //      }
2798
2799         RValue<SByte8> operator+=(SByte8 &lhs, RValue<SByte8> rhs)
2800         {
2801                 return lhs = lhs + rhs;
2802         }
2803
2804         RValue<SByte8> operator-=(SByte8 &lhs, RValue<SByte8> rhs)
2805         {
2806                 return lhs = lhs - rhs;
2807         }
2808
2809 //      RValue<SByte8> operator*=(SByte8 &lhs, RValue<SByte8> rhs)
2810 //      {
2811 //              return lhs = lhs * rhs;
2812 //      }
2813
2814 //      RValue<SByte8> operator/=(SByte8 &lhs, RValue<SByte8> rhs)
2815 //      {
2816 //              return lhs = lhs / rhs;
2817 //      }
2818
2819 //      RValue<SByte8> operator%=(SByte8 &lhs, RValue<SByte8> rhs)
2820 //      {
2821 //              return lhs = lhs % rhs;
2822 //      }
2823
2824         RValue<SByte8> operator&=(SByte8 &lhs, RValue<SByte8> rhs)
2825         {
2826                 return lhs = lhs & rhs;
2827         }
2828
2829         RValue<SByte8> operator|=(SByte8 &lhs, RValue<SByte8> rhs)
2830         {
2831                 return lhs = lhs | rhs;
2832         }
2833
2834         RValue<SByte8> operator^=(SByte8 &lhs, RValue<SByte8> rhs)
2835         {
2836                 return lhs = lhs ^ rhs;
2837         }
2838
2839 //      RValue<SByte8> operator<<=(SByte8 &lhs, RValue<SByte8> rhs)
2840 //      {
2841 //              return lhs = lhs << rhs;
2842 //      }
2843
2844 //      RValue<SByte8> operator>>=(SByte8 &lhs, RValue<SByte8> rhs)
2845 //      {
2846 //              return lhs = lhs >> rhs;
2847 //      }
2848
2849 //      RValue<SByte8> operator+(RValue<SByte8> val)
2850 //      {
2851 //              return val;
2852 //      }
2853
2854 //      RValue<SByte8> operator-(RValue<SByte8> val)
2855 //      {
2856 //              return RValue<SByte8>(Nucleus::createNeg(val.value));
2857 //      }
2858
2859         RValue<SByte8> operator~(RValue<SByte8> val)
2860         {
2861                 return RValue<SByte8>(Nucleus::createNot(val.value));
2862         }
2863
2864         RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y)
2865         {
2866                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2867                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2868                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2869                 auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2870                 paddsb->addArg(x.value);
2871                 paddsb->addArg(y.value);
2872                 ::basicBlock->appendInst(paddsb);
2873
2874                 return RValue<SByte8>(V(result));
2875         }
2876
2877         RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y)
2878         {
2879                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
2880                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2881                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2882                 auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
2883                 psubsb->addArg(x.value);
2884                 psubsb->addArg(y.value);
2885                 ::basicBlock->appendInst(psubsb);
2886
2887                 return RValue<SByte8>(V(result));
2888         }
2889
2890         RValue<Short4> UnpackLow(RValue<SByte8> x, RValue<SByte8> y)
2891         {
2892                 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};   // Real type is v16i8
2893                 return RValue<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2894         }
2895
2896         RValue<Short4> UnpackHigh(RValue<SByte8> x, RValue<SByte8> y)
2897         {
2898                 int shuffle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};   // Real type is v16i8
2899                 auto lowHigh = RValue<Byte16>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
2900                 return As<Short4>(Swizzle(As<Int4>(lowHigh), 0xEE));
2901         }
2902
2903         RValue<Int> SignMask(RValue<SByte8> x)
2904         {
2905                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
2906                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
2907                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
2908                 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
2909                 movmsk->addArg(x.value);
2910                 ::basicBlock->appendInst(movmsk);
2911
2912                 return RValue<Int>(V(result));
2913         }
2914
2915         RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y)
2916         {
2917                 return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
2918         }
2919
2920         RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y)
2921         {
2922                 return RValue<Byte8>(Nucleus::createICmpEQ(x.value, y.value));
2923         }
2924
2925         Type *SByte8::getType()
2926         {
2927                 return T(Type_v8i8);
2928         }
2929
2930         Byte16::Byte16(RValue<Byte16> rhs)
2931         {
2932                 storeValue(rhs.value);
2933         }
2934
2935         Byte16::Byte16(const Byte16 &rhs)
2936         {
2937                 Value *value = rhs.loadValue();
2938                 storeValue(value);
2939         }
2940
2941         Byte16::Byte16(const Reference<Byte16> &rhs)
2942         {
2943                 Value *value = rhs.loadValue();
2944                 storeValue(value);
2945         }
2946
2947         RValue<Byte16> Byte16::operator=(RValue<Byte16> rhs)
2948         {
2949                 storeValue(rhs.value);
2950
2951                 return rhs;
2952         }
2953
2954         RValue<Byte16> Byte16::operator=(const Byte16 &rhs)
2955         {
2956                 Value *value = rhs.loadValue();
2957                 storeValue(value);
2958
2959                 return RValue<Byte16>(value);
2960         }
2961
2962         RValue<Byte16> Byte16::operator=(const Reference<Byte16> &rhs)
2963         {
2964                 Value *value = rhs.loadValue();
2965                 storeValue(value);
2966
2967                 return RValue<Byte16>(value);
2968         }
2969
2970         Type *Byte16::getType()
2971         {
2972                 return T(Ice::IceType_v16i8);
2973         }
2974
2975         Type *SByte16::getType()
2976         {
2977                 return T(Ice::IceType_v16i8);
2978         }
2979
2980         Short2::Short2(RValue<Short4> cast)
2981         {
2982                 storeValue(Nucleus::createBitCast(cast.value, getType()));
2983         }
2984
2985         Type *Short2::getType()
2986         {
2987                 return T(Type_v2i16);
2988         }
2989
2990         UShort2::UShort2(RValue<UShort4> cast)
2991         {
2992                 storeValue(Nucleus::createBitCast(cast.value, getType()));
2993         }
2994
2995         Type *UShort2::getType()
2996         {
2997                 return T(Type_v2i16);
2998         }
2999
3000         Short4::Short4(RValue<Int> cast)
3001         {
3002                 Value *vector = loadValue();
3003                 Value *element = Nucleus::createTrunc(cast.value, Short::getType());
3004                 Value *insert = Nucleus::createInsertElement(vector, element, 0);
3005                 Value *swizzle = Swizzle(RValue<Short4>(insert), 0x00).value;
3006
3007                 storeValue(swizzle);
3008         }
3009
3010         Short4::Short4(RValue<Int4> cast)
3011         {
3012                 int select[8] = {0, 2, 4, 6, 0, 2, 4, 6};
3013                 Value *short8 = Nucleus::createBitCast(cast.value, Short8::getType());
3014                 Value *packed = Nucleus::createShuffleVector(short8, short8, select);
3015
3016                 Value *int2 = RValue<Int2>(Int2(RValue<Int4>(packed))).value;
3017                 Value *short4 = Nucleus::createBitCast(int2, Short4::getType());
3018
3019                 storeValue(short4);
3020         }
3021
3022 //      Short4::Short4(RValue<Float> cast)
3023 //      {
3024 //      }
3025
3026         Short4::Short4(RValue<Float4> cast)
3027         {
3028                 assert(false && "UNIMPLEMENTED");
3029         }
3030
3031         Short4::Short4(short xyzw)
3032         {
3033                 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
3034                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3035         }
3036
3037         Short4::Short4(short x, short y, short z, short w)
3038         {
3039                 int64_t constantVector[4] = {x, y, z, w};
3040                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3041         }
3042
3043         Short4::Short4(RValue<Short4> rhs)
3044         {
3045                 storeValue(rhs.value);
3046         }
3047
3048         Short4::Short4(const Short4 &rhs)
3049         {
3050                 Value *value = rhs.loadValue();
3051                 storeValue(value);
3052         }
3053
3054         Short4::Short4(const Reference<Short4> &rhs)
3055         {
3056                 Value *value = rhs.loadValue();
3057                 storeValue(value);
3058         }
3059
3060         Short4::Short4(RValue<UShort4> rhs)
3061         {
3062                 storeValue(rhs.value);
3063         }
3064
3065         Short4::Short4(const UShort4 &rhs)
3066         {
3067                 storeValue(rhs.loadValue());
3068         }
3069
3070         Short4::Short4(const Reference<UShort4> &rhs)
3071         {
3072                 storeValue(rhs.loadValue());
3073         }
3074
3075         RValue<Short4> Short4::operator=(RValue<Short4> rhs)
3076         {
3077                 storeValue(rhs.value);
3078
3079                 return rhs;
3080         }
3081
3082         RValue<Short4> Short4::operator=(const Short4 &rhs)
3083         {
3084                 Value *value = rhs.loadValue();
3085                 storeValue(value);
3086
3087                 return RValue<Short4>(value);
3088         }
3089
3090         RValue<Short4> Short4::operator=(const Reference<Short4> &rhs)
3091         {
3092                 Value *value = rhs.loadValue();
3093                 storeValue(value);
3094
3095                 return RValue<Short4>(value);
3096         }
3097
3098         RValue<Short4> Short4::operator=(RValue<UShort4> rhs)
3099         {
3100                 storeValue(rhs.value);
3101
3102                 return RValue<Short4>(rhs);
3103         }
3104
3105         RValue<Short4> Short4::operator=(const UShort4 &rhs)
3106         {
3107                 Value *value = rhs.loadValue();
3108                 storeValue(value);
3109
3110                 return RValue<Short4>(value);
3111         }
3112
3113         RValue<Short4> Short4::operator=(const Reference<UShort4> &rhs)
3114         {
3115                 Value *value = rhs.loadValue();
3116                 storeValue(value);
3117
3118                 return RValue<Short4>(value);
3119         }
3120
3121         RValue<Short4> operator+(RValue<Short4> lhs, RValue<Short4> rhs)
3122         {
3123                 return RValue<Short4>(Nucleus::createAdd(lhs.value, rhs.value));
3124         }
3125
3126         RValue<Short4> operator-(RValue<Short4> lhs, RValue<Short4> rhs)
3127         {
3128                 return RValue<Short4>(Nucleus::createSub(lhs.value, rhs.value));
3129         }
3130
3131         RValue<Short4> operator*(RValue<Short4> lhs, RValue<Short4> rhs)
3132         {
3133                 return RValue<Short4>(Nucleus::createMul(lhs.value, rhs.value));
3134         }
3135
3136 //      RValue<Short4> operator/(RValue<Short4> lhs, RValue<Short4> rhs)
3137 //      {
3138 //              return RValue<Short4>(Nucleus::createSDiv(lhs.value, rhs.value));
3139 //      }
3140
3141 //      RValue<Short4> operator%(RValue<Short4> lhs, RValue<Short4> rhs)
3142 //      {
3143 //              return RValue<Short4>(Nucleus::createSRem(lhs.value, rhs.value));
3144 //      }
3145
3146         RValue<Short4> operator&(RValue<Short4> lhs, RValue<Short4> rhs)
3147         {
3148                 return RValue<Short4>(Nucleus::createAnd(lhs.value, rhs.value));
3149         }
3150
3151         RValue<Short4> operator|(RValue<Short4> lhs, RValue<Short4> rhs)
3152         {
3153                 return RValue<Short4>(Nucleus::createOr(lhs.value, rhs.value));
3154         }
3155
3156         RValue<Short4> operator^(RValue<Short4> lhs, RValue<Short4> rhs)
3157         {
3158                 return RValue<Short4>(Nucleus::createXor(lhs.value, rhs.value));
3159         }
3160
3161         RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs)
3162         {
3163                 return RValue<Short4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
3164         }
3165
3166         RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs)
3167         {
3168                 return RValue<Short4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
3169         }
3170
3171         RValue<Short4> operator+=(Short4 &lhs, RValue<Short4> rhs)
3172         {
3173                 return lhs = lhs + rhs;
3174         }
3175
3176         RValue<Short4> operator-=(Short4 &lhs, RValue<Short4> rhs)
3177         {
3178                 return lhs = lhs - rhs;
3179         }
3180
3181         RValue<Short4> operator*=(Short4 &lhs, RValue<Short4> rhs)
3182         {
3183                 return lhs = lhs * rhs;
3184         }
3185
3186 //      RValue<Short4> operator/=(Short4 &lhs, RValue<Short4> rhs)
3187 //      {
3188 //              return lhs = lhs / rhs;
3189 //      }
3190
3191 //      RValue<Short4> operator%=(Short4 &lhs, RValue<Short4> rhs)
3192 //      {
3193 //              return lhs = lhs % rhs;
3194 //      }
3195
3196         RValue<Short4> operator&=(Short4 &lhs, RValue<Short4> rhs)
3197         {
3198                 return lhs = lhs & rhs;
3199         }
3200
3201         RValue<Short4> operator|=(Short4 &lhs, RValue<Short4> rhs)
3202         {
3203                 return lhs = lhs | rhs;
3204         }
3205
3206         RValue<Short4> operator^=(Short4 &lhs, RValue<Short4> rhs)
3207         {
3208                 return lhs = lhs ^ rhs;
3209         }
3210
3211         RValue<Short4> operator<<=(Short4 &lhs, unsigned char rhs)
3212         {
3213                 return lhs = lhs << rhs;
3214         }
3215
3216         RValue<Short4> operator>>=(Short4 &lhs, unsigned char rhs)
3217         {
3218                 return lhs = lhs >> rhs;
3219         }
3220
3221 //      RValue<Short4> operator+(RValue<Short4> val)
3222 //      {
3223 //              return val;
3224 //      }
3225
3226         RValue<Short4> operator-(RValue<Short4> val)
3227         {
3228                 return RValue<Short4>(Nucleus::createNeg(val.value));
3229         }
3230
3231         RValue<Short4> operator~(RValue<Short4> val)
3232         {
3233                 return RValue<Short4>(Nucleus::createNot(val.value));
3234         }
3235
3236         RValue<Short4> RoundShort4(RValue<Float4> cast)
3237         {
3238                 RValue<Int4> int4 = RoundInt(cast);
3239                 return As<Short4>(Pack(int4, int4));
3240         }
3241
3242         RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y)
3243         {
3244                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3245                 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value);
3246                 ::basicBlock->appendInst(cmp);
3247
3248                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3249                 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3250                 ::basicBlock->appendInst(select);
3251
3252                 return RValue<Short4>(V(result));
3253         }
3254
3255         RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y)
3256         {
3257                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3258                 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
3259                 ::basicBlock->appendInst(cmp);
3260
3261                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3262                 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3263                 ::basicBlock->appendInst(select);
3264
3265                 return RValue<Short4>(V(result));
3266         }
3267
3268         RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y)
3269         {
3270                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3271                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3272                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3273                 auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3274                 paddsw->addArg(x.value);
3275                 paddsw->addArg(y.value);
3276                 ::basicBlock->appendInst(paddsw);
3277
3278                 return RValue<Short4>(V(result));
3279         }
3280
3281         RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y)
3282         {
3283                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3284                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3285                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3286                 auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3287                 psubsw->addArg(x.value);
3288                 psubsw->addArg(y.value);
3289                 ::basicBlock->appendInst(psubsw);
3290
3291                 return RValue<Short4>(V(result));
3292         }
3293
3294         RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y)
3295         {
3296                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3297                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3298                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3299                 auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3300                 pmulhw->addArg(x.value);
3301                 pmulhw->addArg(y.value);
3302                 ::basicBlock->appendInst(pmulhw);
3303
3304                 return RValue<Short4>(V(result));
3305         }
3306
3307         RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y)
3308         {
3309                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3310                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3311                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3312                 auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3313                 pmaddwd->addArg(x.value);
3314                 pmaddwd->addArg(y.value);
3315                 ::basicBlock->appendInst(pmaddwd);
3316
3317                 return RValue<Int2>(V(result));
3318         }
3319
3320         RValue<SByte8> Pack(RValue<Short4> x, RValue<Short4> y)
3321         {
3322                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3323                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3324                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3325                 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3326                 pack->addArg(x.value);
3327                 pack->addArg(y.value);
3328                 ::basicBlock->appendInst(pack);
3329
3330                 return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x88));
3331         }
3332
3333         RValue<Int2> UnpackLow(RValue<Short4> x, RValue<Short4> y)
3334         {
3335                 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11};   // Real type is v8i16
3336                 return RValue<Int2>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
3337         }
3338
3339         RValue<Int2> UnpackHigh(RValue<Short4> x, RValue<Short4> y)
3340         {
3341                 int shuffle[8] = {0, 8, 1, 9, 2, 10, 3, 11};   // Real type is v8i16
3342                 auto lowHigh = RValue<Short8>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
3343                 return As<Int2>(Swizzle(As<Int4>(lowHigh), 0xEE));
3344         }
3345
3346         RValue<Short4> Swizzle(RValue<Short4> x, unsigned char select)
3347         {
3348                 // Real type is v8i16
3349                 int shuffle[8] =
3350                 {
3351                         (select >> 0) & 0x03,
3352                         (select >> 2) & 0x03,
3353                         (select >> 4) & 0x03,
3354                         (select >> 6) & 0x03,
3355                         (select >> 0) & 0x03,
3356                         (select >> 2) & 0x03,
3357                         (select >> 4) & 0x03,
3358                         (select >> 6) & 0x03,
3359                 };
3360
3361                 return RValue<Short4>(Nucleus::createShuffleVector(x.value, x.value, shuffle));
3362         }
3363
3364         RValue<Short4> Insert(RValue<Short4> val, RValue<Short> element, int i)
3365         {
3366                 return RValue<Short4>(Nucleus::createInsertElement(val.value, element.value, i));
3367         }
3368
3369         RValue<Short> Extract(RValue<Short4> val, int i)
3370         {
3371                 return RValue<Short>(Nucleus::createExtractElement(val.value, Short::getType(), i));
3372         }
3373
3374         RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y)
3375         {
3376                 return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value, y.value));
3377         }
3378
3379         RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y)
3380         {
3381                 return RValue<Short4>(Nucleus::createICmpEQ(x.value, y.value));
3382         }
3383
3384         Type *Short4::getType()
3385         {
3386                 return T(Type_v4i16);
3387         }
3388
3389         UShort4::UShort4(RValue<Int4> cast)
3390         {
3391                 *this = Short4(cast);
3392         }
3393
3394         UShort4::UShort4(RValue<Float4> cast, bool saturate)
3395         {
3396                 if(saturate)
3397                 {
3398                         if(CPUID::SSE4_1)
3399                         {
3400                                 Int4 int4(Min(cast, Float4(0xFFFF)));   // packusdw takes care of 0x0000 saturation
3401                                 *this = As<Short4>(Pack(As<UInt4>(int4), As<UInt4>(int4)));
3402                         }
3403                         else
3404                         {
3405                                 *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000))));
3406                         }
3407                 }
3408                 else
3409                 {
3410                         *this = Short4(Int4(cast));
3411                 }
3412         }
3413
3414         UShort4::UShort4(unsigned short xyzw)
3415         {
3416                 int64_t constantVector[4] = {xyzw, xyzw, xyzw, xyzw};
3417                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3418         }
3419
3420         UShort4::UShort4(unsigned short x, unsigned short y, unsigned short z, unsigned short w)
3421         {
3422                 int64_t constantVector[4] = {x, y, z, w};
3423                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3424         }
3425
3426         UShort4::UShort4(RValue<UShort4> rhs)
3427         {
3428                 storeValue(rhs.value);
3429         }
3430
3431         UShort4::UShort4(const UShort4 &rhs)
3432         {
3433                 Value *value = rhs.loadValue();
3434                 storeValue(value);
3435         }
3436
3437         UShort4::UShort4(const Reference<UShort4> &rhs)
3438         {
3439                 Value *value = rhs.loadValue();
3440                 storeValue(value);
3441         }
3442
3443         UShort4::UShort4(RValue<Short4> rhs)
3444         {
3445                 storeValue(rhs.value);
3446         }
3447
3448         UShort4::UShort4(const Short4 &rhs)
3449         {
3450                 Value *value = rhs.loadValue();
3451                 storeValue(value);
3452         }
3453
3454         UShort4::UShort4(const Reference<Short4> &rhs)
3455         {
3456                 Value *value = rhs.loadValue();
3457                 storeValue(value);
3458         }
3459
3460         RValue<UShort4> UShort4::operator=(RValue<UShort4> rhs)
3461         {
3462                 storeValue(rhs.value);
3463
3464                 return rhs;
3465         }
3466
3467         RValue<UShort4> UShort4::operator=(const UShort4 &rhs)
3468         {
3469                 Value *value = rhs.loadValue();
3470                 storeValue(value);
3471
3472                 return RValue<UShort4>(value);
3473         }
3474
3475         RValue<UShort4> UShort4::operator=(const Reference<UShort4> &rhs)
3476         {
3477                 Value *value = rhs.loadValue();
3478                 storeValue(value);
3479
3480                 return RValue<UShort4>(value);
3481         }
3482
3483         RValue<UShort4> UShort4::operator=(RValue<Short4> rhs)
3484         {
3485                 storeValue(rhs.value);
3486
3487                 return RValue<UShort4>(rhs);
3488         }
3489
3490         RValue<UShort4> UShort4::operator=(const Short4 &rhs)
3491         {
3492                 Value *value = rhs.loadValue();
3493                 storeValue(value);
3494
3495                 return RValue<UShort4>(value);
3496         }
3497
3498         RValue<UShort4> UShort4::operator=(const Reference<Short4> &rhs)
3499         {
3500                 Value *value = rhs.loadValue();
3501                 storeValue(value);
3502
3503                 return RValue<UShort4>(value);
3504         }
3505
3506         RValue<UShort4> operator+(RValue<UShort4> lhs, RValue<UShort4> rhs)
3507         {
3508                 return RValue<UShort4>(Nucleus::createAdd(lhs.value, rhs.value));
3509         }
3510
3511         RValue<UShort4> operator-(RValue<UShort4> lhs, RValue<UShort4> rhs)
3512         {
3513                 return RValue<UShort4>(Nucleus::createSub(lhs.value, rhs.value));
3514         }
3515
3516         RValue<UShort4> operator*(RValue<UShort4> lhs, RValue<UShort4> rhs)
3517         {
3518                 return RValue<UShort4>(Nucleus::createMul(lhs.value, rhs.value));
3519         }
3520
3521         RValue<UShort4> operator&(RValue<UShort4> lhs, RValue<UShort4> rhs)
3522         {
3523                 return RValue<UShort4>(Nucleus::createAnd(lhs.value, rhs.value));
3524         }
3525
3526         RValue<UShort4> operator|(RValue<UShort4> lhs, RValue<UShort4> rhs)
3527         {
3528                 return RValue<UShort4>(Nucleus::createOr(lhs.value, rhs.value));
3529         }
3530
3531         RValue<UShort4> operator^(RValue<UShort4> lhs, RValue<UShort4> rhs)
3532         {
3533                 return RValue<UShort4>(Nucleus::createXor(lhs.value, rhs.value));
3534         }
3535
3536         RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs)
3537         {
3538                 return RValue<UShort4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
3539         }
3540
3541         RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs)
3542         {
3543                 return RValue<UShort4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
3544         }
3545
3546         RValue<UShort4> operator<<=(UShort4 &lhs, unsigned char rhs)
3547         {
3548                 return lhs = lhs << rhs;
3549         }
3550
3551         RValue<UShort4> operator>>=(UShort4 &lhs, unsigned char rhs)
3552         {
3553                 return lhs = lhs >> rhs;
3554         }
3555
3556         RValue<UShort4> operator~(RValue<UShort4> val)
3557         {
3558                 return RValue<UShort4>(Nucleus::createNot(val.value));
3559         }
3560
3561         RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y)
3562         {
3563                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3564                 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value);
3565                 ::basicBlock->appendInst(cmp);
3566
3567                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3568                 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3569                 ::basicBlock->appendInst(select);
3570
3571                 return RValue<UShort4>(V(result));
3572         }
3573
3574         RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y)
3575         {
3576                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1);
3577                 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
3578                 ::basicBlock->appendInst(cmp);
3579
3580                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3581                 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
3582                 ::basicBlock->appendInst(select);
3583
3584                 return RValue<UShort4>(V(result));
3585         }
3586
3587         RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y)
3588         {
3589                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3590                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3591                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3592                 auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3593                 paddusw->addArg(x.value);
3594                 paddusw->addArg(y.value);
3595                 ::basicBlock->appendInst(paddusw);
3596
3597                 return RValue<UShort4>(V(result));
3598         }
3599
3600         RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y)
3601         {
3602                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3603                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3604                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3605                 auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3606                 psubusw->addArg(x.value);
3607                 psubusw->addArg(y.value);
3608                 ::basicBlock->appendInst(psubusw);
3609
3610                 return RValue<UShort4>(V(result));
3611         }
3612
3613         RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y)
3614         {
3615                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
3616                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3617                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3618                 auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3619                 pmulhuw->addArg(x.value);
3620                 pmulhuw->addArg(y.value);
3621                 ::basicBlock->appendInst(pmulhuw);
3622
3623                 return RValue<UShort4>(V(result));
3624         }
3625
3626         RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)
3627         {
3628                 assert(false && "UNIMPLEMENTED"); return RValue<UShort4>(V(nullptr));
3629         }
3630
3631         RValue<Byte8> Pack(RValue<UShort4> x, RValue<UShort4> y)
3632         {
3633                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8);
3634                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
3635                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
3636                 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
3637                 pack->addArg(x.value);
3638                 pack->addArg(y.value);
3639                 ::basicBlock->appendInst(pack);
3640
3641                 return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x88));
3642         }
3643
3644         Type *UShort4::getType()
3645         {
3646                 return T(Type_v4i16);
3647         }
3648
3649         Short8::Short8(short c)
3650         {
3651                 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3652                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3653         }
3654
3655         Short8::Short8(short c0, short c1, short c2, short c3, short c4, short c5, short c6, short c7)
3656         {
3657                 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3658                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3659         }
3660
3661         Short8::Short8(RValue<Short8> rhs)
3662         {
3663                 storeValue(rhs.value);
3664         }
3665
3666         Short8::Short8(const Reference<Short8> &rhs)
3667         {
3668                 Value *value = rhs.loadValue();
3669                 storeValue(value);
3670         }
3671
3672         Short8::Short8(RValue<Short4> lo, RValue<Short4> hi)
3673         {
3674                 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11};   // Real type is v8i16
3675                 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3676
3677                 storeValue(packed);
3678         }
3679
3680         RValue<Short8> operator+(RValue<Short8> lhs, RValue<Short8> rhs)
3681         {
3682                 return RValue<Short8>(Nucleus::createAdd(lhs.value, rhs.value));
3683         }
3684
3685         RValue<Short8> operator&(RValue<Short8> lhs, RValue<Short8> rhs)
3686         {
3687                 return RValue<Short8>(Nucleus::createAnd(lhs.value, rhs.value));
3688         }
3689
3690         RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs)
3691         {
3692                 return RValue<Short8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
3693         }
3694
3695         RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs)
3696         {
3697                 return RValue<Short8>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
3698         }
3699
3700         RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)
3701         {
3702                 assert(false && "UNIMPLEMENTED"); return RValue<Int4>(V(nullptr));
3703         }
3704
3705         RValue<Int4> Abs(RValue<Int4> x)
3706         {
3707                 auto negative = x >> 31;
3708                 return (x ^ negative) - negative;
3709         }
3710
3711         RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)
3712         {
3713                 assert(false && "UNIMPLEMENTED"); return RValue<Short8>(V(nullptr));
3714         }
3715
3716         Type *Short8::getType()
3717         {
3718                 return T(Ice::IceType_v8i16);
3719         }
3720
3721         UShort8::UShort8(unsigned short c)
3722         {
3723                 int64_t constantVector[8] = {c, c, c, c, c, c, c, c};
3724                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3725         }
3726
3727         UShort8::UShort8(unsigned short c0, unsigned short c1, unsigned short c2, unsigned short c3, unsigned short c4, unsigned short c5, unsigned short c6, unsigned short c7)
3728         {
3729                 int64_t constantVector[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
3730                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
3731         }
3732
3733         UShort8::UShort8(RValue<UShort8> rhs)
3734         {
3735                 storeValue(rhs.value);
3736         }
3737
3738         UShort8::UShort8(const Reference<UShort8> &rhs)
3739         {
3740                 Value *value = rhs.loadValue();
3741                 storeValue(value);
3742         }
3743
3744         UShort8::UShort8(RValue<UShort4> lo, RValue<UShort4> hi)
3745         {
3746                 int shuffle[8] = {0, 1, 2, 3, 8, 9, 10, 11};   // Real type is v8i16
3747                 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
3748
3749                 storeValue(packed);
3750         }
3751
3752         RValue<UShort8> UShort8::operator=(RValue<UShort8> rhs)
3753         {
3754                 storeValue(rhs.value);
3755
3756                 return rhs;
3757         }
3758
3759         RValue<UShort8> UShort8::operator=(const UShort8 &rhs)
3760         {
3761                 Value *value = rhs.loadValue();
3762                 storeValue(value);
3763
3764                 return RValue<UShort8>(value);
3765         }
3766
3767         RValue<UShort8> UShort8::operator=(const Reference<UShort8> &rhs)
3768         {
3769                 Value *value = rhs.loadValue();
3770                 storeValue(value);
3771
3772                 return RValue<UShort8>(value);
3773         }
3774
3775         RValue<UShort8> operator&(RValue<UShort8> lhs, RValue<UShort8> rhs)
3776         {
3777                 return RValue<UShort8>(Nucleus::createAnd(lhs.value, rhs.value));
3778         }
3779
3780         RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs)
3781         {
3782                 return RValue<UShort8>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
3783         }
3784
3785         RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs)
3786         {
3787                 return RValue<UShort8>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
3788         }
3789
3790         RValue<UShort8> operator+(RValue<UShort8> lhs, RValue<UShort8> rhs)
3791         {
3792                 return RValue<UShort8>(Nucleus::createAdd(lhs.value, rhs.value));
3793         }
3794
3795         RValue<UShort8> operator*(RValue<UShort8> lhs, RValue<UShort8> rhs)
3796         {
3797                 return RValue<UShort8>(Nucleus::createMul(lhs.value, rhs.value));
3798         }
3799
3800         RValue<UShort8> operator+=(UShort8 &lhs, RValue<UShort8> rhs)
3801         {
3802                 return lhs = lhs + rhs;
3803         }
3804
3805         RValue<UShort8> operator~(RValue<UShort8> val)
3806         {
3807                 return RValue<UShort8>(Nucleus::createNot(val.value));
3808         }
3809
3810         RValue<UShort8> Swizzle(RValue<UShort8> x, char select0, char select1, char select2, char select3, char select4, char select5, char select6, char select7)
3811         {
3812                 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
3813         }
3814
3815         RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)
3816         {
3817                 assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
3818         }
3819
3820         // FIXME: Implement as Shuffle(x, y, Select(i0, ..., i16)) and Shuffle(x, y, SELECT_PACK_REPEAT(element))
3821 //      RValue<UShort8> PackRepeat(RValue<Byte16> x, RValue<Byte16> y, int element)
3822 //      {
3823 //              assert(false && "UNIMPLEMENTED"); return RValue<UShort8>(V(nullptr));
3824 //      }
3825
3826         Type *UShort8::getType()
3827         {
3828                 return T(Ice::IceType_v8i16);
3829         }
3830
3831         Int::Int(Argument<Int> argument)
3832         {
3833                 storeValue(argument.value);
3834         }
3835
3836         Int::Int(RValue<Byte> cast)
3837         {
3838                 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3839
3840                 storeValue(integer);
3841         }
3842
3843         Int::Int(RValue<SByte> cast)
3844         {
3845                 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3846
3847                 storeValue(integer);
3848         }
3849
3850         Int::Int(RValue<Short> cast)
3851         {
3852                 Value *integer = Nucleus::createSExt(cast.value, Int::getType());
3853
3854                 storeValue(integer);
3855         }
3856
3857         Int::Int(RValue<UShort> cast)
3858         {
3859                 Value *integer = Nucleus::createZExt(cast.value, Int::getType());
3860
3861                 storeValue(integer);
3862         }
3863
3864         Int::Int(RValue<Int2> cast)
3865         {
3866                 *this = Extract(cast, 0);
3867         }
3868
3869         Int::Int(RValue<Long> cast)
3870         {
3871                 Value *integer = Nucleus::createTrunc(cast.value, Int::getType());
3872
3873                 storeValue(integer);
3874         }
3875
3876         Int::Int(RValue<Float> cast)
3877         {
3878                 Value *integer = Nucleus::createFPToSI(cast.value, Int::getType());
3879
3880                 storeValue(integer);
3881         }
3882
3883         Int::Int(int x)
3884         {
3885                 storeValue(Nucleus::createConstantInt(x));
3886         }
3887
3888         Int::Int(RValue<Int> rhs)
3889         {
3890                 storeValue(rhs.value);
3891         }
3892
3893         Int::Int(RValue<UInt> rhs)
3894         {
3895                 storeValue(rhs.value);
3896         }
3897
3898         Int::Int(const Int &rhs)
3899         {
3900                 Value *value = rhs.loadValue();
3901                 storeValue(value);
3902         }
3903
3904         Int::Int(const Reference<Int> &rhs)
3905         {
3906                 Value *value = rhs.loadValue();
3907                 storeValue(value);
3908         }
3909
3910         Int::Int(const UInt &rhs)
3911         {
3912                 Value *value = rhs.loadValue();
3913                 storeValue(value);
3914         }
3915
3916         Int::Int(const Reference<UInt> &rhs)
3917         {
3918                 Value *value = rhs.loadValue();
3919                 storeValue(value);
3920         }
3921
3922         RValue<Int> Int::operator=(int rhs)
3923         {
3924                 return RValue<Int>(storeValue(Nucleus::createConstantInt(rhs)));
3925         }
3926
3927         RValue<Int> Int::operator=(RValue<Int> rhs)
3928         {
3929                 storeValue(rhs.value);
3930
3931                 return rhs;
3932         }
3933
3934         RValue<Int> Int::operator=(RValue<UInt> rhs)
3935         {
3936                 storeValue(rhs.value);
3937
3938                 return RValue<Int>(rhs);
3939         }
3940
3941         RValue<Int> Int::operator=(const Int &rhs)
3942         {
3943                 Value *value = rhs.loadValue();
3944                 storeValue(value);
3945
3946                 return RValue<Int>(value);
3947         }
3948
3949         RValue<Int> Int::operator=(const Reference<Int> &rhs)
3950         {
3951                 Value *value = rhs.loadValue();
3952                 storeValue(value);
3953
3954                 return RValue<Int>(value);
3955         }
3956
3957         RValue<Int> Int::operator=(const UInt &rhs)
3958         {
3959                 Value *value = rhs.loadValue();
3960                 storeValue(value);
3961
3962                 return RValue<Int>(value);
3963         }
3964
3965         RValue<Int> Int::operator=(const Reference<UInt> &rhs)
3966         {
3967                 Value *value = rhs.loadValue();
3968                 storeValue(value);
3969
3970                 return RValue<Int>(value);
3971         }
3972
3973         RValue<Int> operator+(RValue<Int> lhs, RValue<Int> rhs)
3974         {
3975                 return RValue<Int>(Nucleus::createAdd(lhs.value, rhs.value));
3976         }
3977
3978         RValue<Int> operator-(RValue<Int> lhs, RValue<Int> rhs)
3979         {
3980                 return RValue<Int>(Nucleus::createSub(lhs.value, rhs.value));
3981         }
3982
3983         RValue<Int> operator*(RValue<Int> lhs, RValue<Int> rhs)
3984         {
3985                 return RValue<Int>(Nucleus::createMul(lhs.value, rhs.value));
3986         }
3987
3988         RValue<Int> operator/(RValue<Int> lhs, RValue<Int> rhs)
3989         {
3990                 return RValue<Int>(Nucleus::createSDiv(lhs.value, rhs.value));
3991         }
3992
3993         RValue<Int> operator%(RValue<Int> lhs, RValue<Int> rhs)
3994         {
3995                 return RValue<Int>(Nucleus::createSRem(lhs.value, rhs.value));
3996         }
3997
3998         RValue<Int> operator&(RValue<Int> lhs, RValue<Int> rhs)
3999         {
4000                 return RValue<Int>(Nucleus::createAnd(lhs.value, rhs.value));
4001         }
4002
4003         RValue<Int> operator|(RValue<Int> lhs, RValue<Int> rhs)
4004         {
4005                 return RValue<Int>(Nucleus::createOr(lhs.value, rhs.value));
4006         }
4007
4008         RValue<Int> operator^(RValue<Int> lhs, RValue<Int> rhs)
4009         {
4010                 return RValue<Int>(Nucleus::createXor(lhs.value, rhs.value));
4011         }
4012
4013         RValue<Int> operator<<(RValue<Int> lhs, RValue<Int> rhs)
4014         {
4015                 return RValue<Int>(Nucleus::createShl(lhs.value, rhs.value));
4016         }
4017
4018         RValue<Int> operator>>(RValue<Int> lhs, RValue<Int> rhs)
4019         {
4020                 return RValue<Int>(Nucleus::createAShr(lhs.value, rhs.value));
4021         }
4022
4023         RValue<Int> operator+=(Int &lhs, RValue<Int> rhs)
4024         {
4025                 return lhs = lhs + rhs;
4026         }
4027
4028         RValue<Int> operator-=(Int &lhs, RValue<Int> rhs)
4029         {
4030                 return lhs = lhs - rhs;
4031         }
4032
4033         RValue<Int> operator*=(Int &lhs, RValue<Int> rhs)
4034         {
4035                 return lhs = lhs * rhs;
4036         }
4037
4038         RValue<Int> operator/=(Int &lhs, RValue<Int> rhs)
4039         {
4040                 return lhs = lhs / rhs;
4041         }
4042
4043         RValue<Int> operator%=(Int &lhs, RValue<Int> rhs)
4044         {
4045                 return lhs = lhs % rhs;
4046         }
4047
4048         RValue<Int> operator&=(Int &lhs, RValue<Int> rhs)
4049         {
4050                 return lhs = lhs & rhs;
4051         }
4052
4053         RValue<Int> operator|=(Int &lhs, RValue<Int> rhs)
4054         {
4055                 return lhs = lhs | rhs;
4056         }
4057
4058         RValue<Int> operator^=(Int &lhs, RValue<Int> rhs)
4059         {
4060                 return lhs = lhs ^ rhs;
4061         }
4062
4063         RValue<Int> operator<<=(Int &lhs, RValue<Int> rhs)
4064         {
4065                 return lhs = lhs << rhs;
4066         }
4067
4068         RValue<Int> operator>>=(Int &lhs, RValue<Int> rhs)
4069         {
4070                 return lhs = lhs >> rhs;
4071         }
4072
4073         RValue<Int> operator+(RValue<Int> val)
4074         {
4075                 return val;
4076         }
4077
4078         RValue<Int> operator-(RValue<Int> val)
4079         {
4080                 return RValue<Int>(Nucleus::createNeg(val.value));
4081         }
4082
4083         RValue<Int> operator~(RValue<Int> val)
4084         {
4085                 return RValue<Int>(Nucleus::createNot(val.value));
4086         }
4087
4088         RValue<Int> operator++(Int &val, int)   // Post-increment
4089         {
4090                 RValue<Int> res = val;
4091                 val += 1;
4092                 return res;
4093         }
4094
4095         const Int &operator++(Int &val)   // Pre-increment
4096         {
4097                 val += 1;
4098                 return val;
4099         }
4100
4101         RValue<Int> operator--(Int &val, int)   // Post-decrement
4102         {
4103                 RValue<Int> res = val;
4104                 val -= 1;
4105                 return res;
4106         }
4107
4108         const Int &operator--(Int &val)   // Pre-decrement
4109         {
4110                 val -= 1;
4111                 return val;
4112         }
4113
4114         RValue<Bool> operator<(RValue<Int> lhs, RValue<Int> rhs)
4115         {
4116                 return RValue<Bool>(Nucleus::createICmpSLT(lhs.value, rhs.value));
4117         }
4118
4119         RValue<Bool> operator<=(RValue<Int> lhs, RValue<Int> rhs)
4120         {
4121                 return RValue<Bool>(Nucleus::createICmpSLE(lhs.value, rhs.value));
4122         }
4123
4124         RValue<Bool> operator>(RValue<Int> lhs, RValue<Int> rhs)
4125         {
4126                 return RValue<Bool>(Nucleus::createICmpSGT(lhs.value, rhs.value));
4127         }
4128
4129         RValue<Bool> operator>=(RValue<Int> lhs, RValue<Int> rhs)
4130         {
4131                 return RValue<Bool>(Nucleus::createICmpSGE(lhs.value, rhs.value));
4132         }
4133
4134         RValue<Bool> operator!=(RValue<Int> lhs, RValue<Int> rhs)
4135         {
4136                 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4137         }
4138
4139         RValue<Bool> operator==(RValue<Int> lhs, RValue<Int> rhs)
4140         {
4141                 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4142         }
4143
4144         RValue<Int> Max(RValue<Int> x, RValue<Int> y)
4145         {
4146                 return IfThenElse(x > y, x, y);
4147         }
4148
4149         RValue<Int> Min(RValue<Int> x, RValue<Int> y)
4150         {
4151                 return IfThenElse(x < y, x, y);
4152         }
4153
4154         RValue<Int> Clamp(RValue<Int> x, RValue<Int> min, RValue<Int> max)
4155         {
4156                 return Min(Max(x, min), max);
4157         }
4158
4159         RValue<Int> RoundInt(RValue<Float> cast)
4160         {
4161                 if(emulateIntrinsics)
4162                 {
4163                         // Push the fractional part off the mantissa. Accurate up to +/-2^22.
4164                         return Int((cast + Float(0x00C00000)) - Float(0x00C00000));
4165                 }
4166                 else
4167                 {
4168                         Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
4169                         const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
4170                         auto target = ::context->getConstantUndef(Ice::IceType_i32);
4171                         auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
4172                         nearbyint->addArg(cast.value);
4173                         ::basicBlock->appendInst(nearbyint);
4174
4175                         return RValue<Int>(V(result));
4176                 }
4177         }
4178
4179         Type *Int::getType()
4180         {
4181                 return T(Ice::IceType_i32);
4182         }
4183
4184         Long::Long(RValue<Int> cast)
4185         {
4186                 Value *integer = Nucleus::createSExt(cast.value, Long::getType());
4187
4188                 storeValue(integer);
4189         }
4190
4191         Long::Long(RValue<UInt> cast)
4192         {
4193                 Value *integer = Nucleus::createZExt(cast.value, Long::getType());
4194
4195                 storeValue(integer);
4196         }
4197
4198         Long::Long(RValue<Long> rhs)
4199         {
4200                 storeValue(rhs.value);
4201         }
4202
4203         RValue<Long> Long::operator=(int64_t rhs)
4204         {
4205                 return RValue<Long>(storeValue(Nucleus::createConstantLong(rhs)));
4206         }
4207
4208         RValue<Long> Long::operator=(RValue<Long> rhs)
4209         {
4210                 storeValue(rhs.value);
4211
4212                 return rhs;
4213         }
4214
4215         RValue<Long> Long::operator=(const Long &rhs)
4216         {
4217                 Value *value = rhs.loadValue();
4218                 storeValue(value);
4219
4220                 return RValue<Long>(value);
4221         }
4222
4223         RValue<Long> Long::operator=(const Reference<Long> &rhs)
4224         {
4225                 Value *value = rhs.loadValue();
4226                 storeValue(value);
4227
4228                 return RValue<Long>(value);
4229         }
4230
4231         RValue<Long> operator+(RValue<Long> lhs, RValue<Long> rhs)
4232         {
4233                 return RValue<Long>(Nucleus::createAdd(lhs.value, rhs.value));
4234         }
4235
4236         RValue<Long> operator-(RValue<Long> lhs, RValue<Long> rhs)
4237         {
4238                 return RValue<Long>(Nucleus::createSub(lhs.value, rhs.value));
4239         }
4240
4241         RValue<Long> operator+=(Long &lhs, RValue<Long> rhs)
4242         {
4243                 return lhs = lhs + rhs;
4244         }
4245
4246         RValue<Long> operator-=(Long &lhs, RValue<Long> rhs)
4247         {
4248                 return lhs = lhs - rhs;
4249         }
4250
4251         RValue<Long> AddAtomic(RValue<Pointer<Long> > x, RValue<Long> y)
4252         {
4253                 return RValue<Long>(Nucleus::createAtomicAdd(x.value, y.value));
4254         }
4255
4256         Type *Long::getType()
4257         {
4258                 return T(Ice::IceType_i64);
4259         }
4260
4261         UInt::UInt(Argument<UInt> argument)
4262         {
4263                 storeValue(argument.value);
4264         }
4265
4266         UInt::UInt(RValue<UShort> cast)
4267         {
4268                 Value *integer = Nucleus::createZExt(cast.value, UInt::getType());
4269
4270                 storeValue(integer);
4271         }
4272
4273         UInt::UInt(RValue<Long> cast)
4274         {
4275                 Value *integer = Nucleus::createTrunc(cast.value, UInt::getType());
4276
4277                 storeValue(integer);
4278         }
4279
4280         UInt::UInt(RValue<Float> cast)
4281         {
4282                 // Smallest positive value representable in UInt, but not in Int
4283                 const unsigned int ustart = 0x80000000u;
4284                 const float ustartf = float(ustart);
4285
4286                 // If the value is negative, store 0, otherwise store the result of the conversion
4287                 storeValue((~(As<Int>(cast) >> 31) &
4288                 // Check if the value can be represented as an Int
4289                         IfThenElse(cast >= ustartf,
4290                 // If the value is too large, subtract ustart and re-add it after conversion.
4291                                 As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)),
4292                 // Otherwise, just convert normally
4293                                 Int(cast))).value);
4294         }
4295
4296         UInt::UInt(int x)
4297         {
4298                 storeValue(Nucleus::createConstantInt(x));
4299         }
4300
4301         UInt::UInt(unsigned int x)
4302         {
4303                 storeValue(Nucleus::createConstantInt(x));
4304         }
4305
4306         UInt::UInt(RValue<UInt> rhs)
4307         {
4308                 storeValue(rhs.value);
4309         }
4310
4311         UInt::UInt(RValue<Int> rhs)
4312         {
4313                 storeValue(rhs.value);
4314         }
4315
4316         UInt::UInt(const UInt &rhs)
4317         {
4318                 Value *value = rhs.loadValue();
4319                 storeValue(value);
4320         }
4321
4322         UInt::UInt(const Reference<UInt> &rhs)
4323         {
4324                 Value *value = rhs.loadValue();
4325                 storeValue(value);
4326         }
4327
4328         UInt::UInt(const Int &rhs)
4329         {
4330                 Value *value = rhs.loadValue();
4331                 storeValue(value);
4332         }
4333
4334         UInt::UInt(const Reference<Int> &rhs)
4335         {
4336                 Value *value = rhs.loadValue();
4337                 storeValue(value);
4338         }
4339
4340         RValue<UInt> UInt::operator=(unsigned int rhs)
4341         {
4342                 return RValue<UInt>(storeValue(Nucleus::createConstantInt(rhs)));
4343         }
4344
4345         RValue<UInt> UInt::operator=(RValue<UInt> rhs)
4346         {
4347                 storeValue(rhs.value);
4348
4349                 return rhs;
4350         }
4351
4352         RValue<UInt> UInt::operator=(RValue<Int> rhs)
4353         {
4354                 storeValue(rhs.value);
4355
4356                 return RValue<UInt>(rhs);
4357         }
4358
4359         RValue<UInt> UInt::operator=(const UInt &rhs)
4360         {
4361                 Value *value = rhs.loadValue();
4362                 storeValue(value);
4363
4364                 return RValue<UInt>(value);
4365         }
4366
4367         RValue<UInt> UInt::operator=(const Reference<UInt> &rhs)
4368         {
4369                 Value *value = rhs.loadValue();
4370                 storeValue(value);
4371
4372                 return RValue<UInt>(value);
4373         }
4374
4375         RValue<UInt> UInt::operator=(const Int &rhs)
4376         {
4377                 Value *value = rhs.loadValue();
4378                 storeValue(value);
4379
4380                 return RValue<UInt>(value);
4381         }
4382
4383         RValue<UInt> UInt::operator=(const Reference<Int> &rhs)
4384         {
4385                 Value *value = rhs.loadValue();
4386                 storeValue(value);
4387
4388                 return RValue<UInt>(value);
4389         }
4390
4391         RValue<UInt> operator+(RValue<UInt> lhs, RValue<UInt> rhs)
4392         {
4393                 return RValue<UInt>(Nucleus::createAdd(lhs.value, rhs.value));
4394         }
4395
4396         RValue<UInt> operator-(RValue<UInt> lhs, RValue<UInt> rhs)
4397         {
4398                 return RValue<UInt>(Nucleus::createSub(lhs.value, rhs.value));
4399         }
4400
4401         RValue<UInt> operator*(RValue<UInt> lhs, RValue<UInt> rhs)
4402         {
4403                 return RValue<UInt>(Nucleus::createMul(lhs.value, rhs.value));
4404         }
4405
4406         RValue<UInt> operator/(RValue<UInt> lhs, RValue<UInt> rhs)
4407         {
4408                 return RValue<UInt>(Nucleus::createUDiv(lhs.value, rhs.value));
4409         }
4410
4411         RValue<UInt> operator%(RValue<UInt> lhs, RValue<UInt> rhs)
4412         {
4413                 return RValue<UInt>(Nucleus::createURem(lhs.value, rhs.value));
4414         }
4415
4416         RValue<UInt> operator&(RValue<UInt> lhs, RValue<UInt> rhs)
4417         {
4418                 return RValue<UInt>(Nucleus::createAnd(lhs.value, rhs.value));
4419         }
4420
4421         RValue<UInt> operator|(RValue<UInt> lhs, RValue<UInt> rhs)
4422         {
4423                 return RValue<UInt>(Nucleus::createOr(lhs.value, rhs.value));
4424         }
4425
4426         RValue<UInt> operator^(RValue<UInt> lhs, RValue<UInt> rhs)
4427         {
4428                 return RValue<UInt>(Nucleus::createXor(lhs.value, rhs.value));
4429         }
4430
4431         RValue<UInt> operator<<(RValue<UInt> lhs, RValue<UInt> rhs)
4432         {
4433                 return RValue<UInt>(Nucleus::createShl(lhs.value, rhs.value));
4434         }
4435
4436         RValue<UInt> operator>>(RValue<UInt> lhs, RValue<UInt> rhs)
4437         {
4438                 return RValue<UInt>(Nucleus::createLShr(lhs.value, rhs.value));
4439         }
4440
4441         RValue<UInt> operator+=(UInt &lhs, RValue<UInt> rhs)
4442         {
4443                 return lhs = lhs + rhs;
4444         }
4445
4446         RValue<UInt> operator-=(UInt &lhs, RValue<UInt> rhs)
4447         {
4448                 return lhs = lhs - rhs;
4449         }
4450
4451         RValue<UInt> operator*=(UInt &lhs, RValue<UInt> rhs)
4452         {
4453                 return lhs = lhs * rhs;
4454         }
4455
4456         RValue<UInt> operator/=(UInt &lhs, RValue<UInt> rhs)
4457         {
4458                 return lhs = lhs / rhs;
4459         }
4460
4461         RValue<UInt> operator%=(UInt &lhs, RValue<UInt> rhs)
4462         {
4463                 return lhs = lhs % rhs;
4464         }
4465
4466         RValue<UInt> operator&=(UInt &lhs, RValue<UInt> rhs)
4467         {
4468                 return lhs = lhs & rhs;
4469         }
4470
4471         RValue<UInt> operator|=(UInt &lhs, RValue<UInt> rhs)
4472         {
4473                 return lhs = lhs | rhs;
4474         }
4475
4476         RValue<UInt> operator^=(UInt &lhs, RValue<UInt> rhs)
4477         {
4478                 return lhs = lhs ^ rhs;
4479         }
4480
4481         RValue<UInt> operator<<=(UInt &lhs, RValue<UInt> rhs)
4482         {
4483                 return lhs = lhs << rhs;
4484         }
4485
4486         RValue<UInt> operator>>=(UInt &lhs, RValue<UInt> rhs)
4487         {
4488                 return lhs = lhs >> rhs;
4489         }
4490
4491         RValue<UInt> operator+(RValue<UInt> val)
4492         {
4493                 return val;
4494         }
4495
4496         RValue<UInt> operator-(RValue<UInt> val)
4497         {
4498                 return RValue<UInt>(Nucleus::createNeg(val.value));
4499         }
4500
4501         RValue<UInt> operator~(RValue<UInt> val)
4502         {
4503                 return RValue<UInt>(Nucleus::createNot(val.value));
4504         }
4505
4506         RValue<UInt> operator++(UInt &val, int)   // Post-increment
4507         {
4508                 RValue<UInt> res = val;
4509                 val += 1;
4510                 return res;
4511         }
4512
4513         const UInt &operator++(UInt &val)   // Pre-increment
4514         {
4515                 val += 1;
4516                 return val;
4517         }
4518
4519         RValue<UInt> operator--(UInt &val, int)   // Post-decrement
4520         {
4521                 RValue<UInt> res = val;
4522                 val -= 1;
4523                 return res;
4524         }
4525
4526         const UInt &operator--(UInt &val)   // Pre-decrement
4527         {
4528                 val -= 1;
4529                 return val;
4530         }
4531
4532         RValue<UInt> Max(RValue<UInt> x, RValue<UInt> y)
4533         {
4534                 return IfThenElse(x > y, x, y);
4535         }
4536
4537         RValue<UInt> Min(RValue<UInt> x, RValue<UInt> y)
4538         {
4539                 return IfThenElse(x < y, x, y);
4540         }
4541
4542         RValue<UInt> Clamp(RValue<UInt> x, RValue<UInt> min, RValue<UInt> max)
4543         {
4544                 return Min(Max(x, min), max);
4545         }
4546
4547         RValue<Bool> operator<(RValue<UInt> lhs, RValue<UInt> rhs)
4548         {
4549                 return RValue<Bool>(Nucleus::createICmpULT(lhs.value, rhs.value));
4550         }
4551
4552         RValue<Bool> operator<=(RValue<UInt> lhs, RValue<UInt> rhs)
4553         {
4554                 return RValue<Bool>(Nucleus::createICmpULE(lhs.value, rhs.value));
4555         }
4556
4557         RValue<Bool> operator>(RValue<UInt> lhs, RValue<UInt> rhs)
4558         {
4559                 return RValue<Bool>(Nucleus::createICmpUGT(lhs.value, rhs.value));
4560         }
4561
4562         RValue<Bool> operator>=(RValue<UInt> lhs, RValue<UInt> rhs)
4563         {
4564                 return RValue<Bool>(Nucleus::createICmpUGE(lhs.value, rhs.value));
4565         }
4566
4567         RValue<Bool> operator!=(RValue<UInt> lhs, RValue<UInt> rhs)
4568         {
4569                 return RValue<Bool>(Nucleus::createICmpNE(lhs.value, rhs.value));
4570         }
4571
4572         RValue<Bool> operator==(RValue<UInt> lhs, RValue<UInt> rhs)
4573         {
4574                 return RValue<Bool>(Nucleus::createICmpEQ(lhs.value, rhs.value));
4575         }
4576
4577 //      RValue<UInt> RoundUInt(RValue<Float> cast)
4578 //      {
4579 //              assert(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr));
4580 //      }
4581
4582         Type *UInt::getType()
4583         {
4584                 return T(Ice::IceType_i32);
4585         }
4586
4587 //      Int2::Int2(RValue<Int> cast)
4588 //      {
4589 //              Value *extend = Nucleus::createZExt(cast.value, Long::getType());
4590 //              Value *vector = Nucleus::createBitCast(extend, Int2::getType());
4591 //
4592 //              Constant *shuffle[2];
4593 //              shuffle[0] = Nucleus::createConstantInt(0);
4594 //              shuffle[1] = Nucleus::createConstantInt(0);
4595 //
4596 //              Value *replicate = Nucleus::createShuffleVector(vector, UndefValue::get(Int2::getType()), Nucleus::createConstantVector(shuffle, 2));
4597 //
4598 //              storeValue(replicate);
4599 //      }
4600
4601         Int2::Int2(RValue<Int4> cast)
4602         {
4603                 storeValue(Nucleus::createBitCast(cast.value, getType()));
4604         }
4605
4606         Int2::Int2(int x, int y)
4607         {
4608                 int64_t constantVector[2] = {x, y};
4609                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
4610         }
4611
4612         Int2::Int2(RValue<Int2> rhs)
4613         {
4614                 storeValue(rhs.value);
4615         }
4616
4617         Int2::Int2(const Int2 &rhs)
4618         {
4619                 Value *value = rhs.loadValue();
4620                 storeValue(value);
4621         }
4622
4623         Int2::Int2(const Reference<Int2> &rhs)
4624         {
4625                 Value *value = rhs.loadValue();
4626                 storeValue(value);
4627         }
4628
4629         Int2::Int2(RValue<Int> lo, RValue<Int> hi)
4630         {
4631                 int shuffle[4] = {0, 4, 1, 5};
4632                 Value *packed = Nucleus::createShuffleVector(Int4(lo).loadValue(), Int4(hi).loadValue(), shuffle);
4633
4634                 storeValue(Nucleus::createBitCast(packed, Int2::getType()));
4635         }
4636
4637         RValue<Int2> Int2::operator=(RValue<Int2> rhs)
4638         {
4639                 storeValue(rhs.value);
4640
4641                 return rhs;
4642         }
4643
4644         RValue<Int2> Int2::operator=(const Int2 &rhs)
4645         {
4646                 Value *value = rhs.loadValue();
4647                 storeValue(value);
4648
4649                 return RValue<Int2>(value);
4650         }
4651
4652         RValue<Int2> Int2::operator=(const Reference<Int2> &rhs)
4653         {
4654                 Value *value = rhs.loadValue();
4655                 storeValue(value);
4656
4657                 return RValue<Int2>(value);
4658         }
4659
4660         RValue<Int2> operator+(RValue<Int2> lhs, RValue<Int2> rhs)
4661         {
4662                 return RValue<Int2>(Nucleus::createAdd(lhs.value, rhs.value));
4663         }
4664
4665         RValue<Int2> operator-(RValue<Int2> lhs, RValue<Int2> rhs)
4666         {
4667                 return RValue<Int2>(Nucleus::createSub(lhs.value, rhs.value));
4668         }
4669
4670 //      RValue<Int2> operator*(RValue<Int2> lhs, RValue<Int2> rhs)
4671 //      {
4672 //              return RValue<Int2>(Nucleus::createMul(lhs.value, rhs.value));
4673 //      }
4674
4675 //      RValue<Int2> operator/(RValue<Int2> lhs, RValue<Int2> rhs)
4676 //      {
4677 //              return RValue<Int2>(Nucleus::createSDiv(lhs.value, rhs.value));
4678 //      }
4679
4680 //      RValue<Int2> operator%(RValue<Int2> lhs, RValue<Int2> rhs)
4681 //      {
4682 //              return RValue<Int2>(Nucleus::createSRem(lhs.value, rhs.value));
4683 //      }
4684
4685         RValue<Int2> operator&(RValue<Int2> lhs, RValue<Int2> rhs)
4686         {
4687                 return RValue<Int2>(Nucleus::createAnd(lhs.value, rhs.value));
4688         }
4689
4690         RValue<Int2> operator|(RValue<Int2> lhs, RValue<Int2> rhs)
4691         {
4692                 return RValue<Int2>(Nucleus::createOr(lhs.value, rhs.value));
4693         }
4694
4695         RValue<Int2> operator^(RValue<Int2> lhs, RValue<Int2> rhs)
4696         {
4697                 return RValue<Int2>(Nucleus::createXor(lhs.value, rhs.value));
4698         }
4699
4700         RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs)
4701         {
4702                 return RValue<Int2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
4703         }
4704
4705         RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs)
4706         {
4707                 return RValue<Int2>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
4708         }
4709
4710         RValue<Int2> operator+=(Int2 &lhs, RValue<Int2> rhs)
4711         {
4712                 return lhs = lhs + rhs;
4713         }
4714
4715         RValue<Int2> operator-=(Int2 &lhs, RValue<Int2> rhs)
4716         {
4717                 return lhs = lhs - rhs;
4718         }
4719
4720 //      RValue<Int2> operator*=(Int2 &lhs, RValue<Int2> rhs)
4721 //      {
4722 //              return lhs = lhs * rhs;
4723 //      }
4724
4725 //      RValue<Int2> operator/=(Int2 &lhs, RValue<Int2> rhs)
4726 //      {
4727 //              return lhs = lhs / rhs;
4728 //      }
4729
4730 //      RValue<Int2> operator%=(Int2 &lhs, RValue<Int2> rhs)
4731 //      {
4732 //              return lhs = lhs % rhs;
4733 //      }
4734
4735         RValue<Int2> operator&=(Int2 &lhs, RValue<Int2> rhs)
4736         {
4737                 return lhs = lhs & rhs;
4738         }
4739
4740         RValue<Int2> operator|=(Int2 &lhs, RValue<Int2> rhs)
4741         {
4742                 return lhs = lhs | rhs;
4743         }
4744
4745         RValue<Int2> operator^=(Int2 &lhs, RValue<Int2> rhs)
4746         {
4747                 return lhs = lhs ^ rhs;
4748         }
4749
4750         RValue<Int2> operator<<=(Int2 &lhs, unsigned char rhs)
4751         {
4752                 return lhs = lhs << rhs;
4753         }
4754
4755         RValue<Int2> operator>>=(Int2 &lhs, unsigned char rhs)
4756         {
4757                 return lhs = lhs >> rhs;
4758         }
4759
4760 //      RValue<Int2> operator+(RValue<Int2> val)
4761 //      {
4762 //              return val;
4763 //      }
4764
4765 //      RValue<Int2> operator-(RValue<Int2> val)
4766 //      {
4767 //              return RValue<Int2>(Nucleus::createNeg(val.value));
4768 //      }
4769
4770         RValue<Int2> operator~(RValue<Int2> val)
4771         {
4772                 return RValue<Int2>(Nucleus::createNot(val.value));
4773         }
4774
4775         RValue<Short4> UnpackLow(RValue<Int2> x, RValue<Int2> y)
4776         {
4777                 int shuffle[4] = {0, 4, 1, 5};   // Real type is v4i32
4778                 return As<Short4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
4779         }
4780
4781         RValue<Short4> UnpackHigh(RValue<Int2> x, RValue<Int2> y)
4782         {
4783                 int shuffle[4] = {0, 4, 1, 5};   // Real type is v4i32
4784                 auto lowHigh = RValue<Int4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
4785                 return As<Short4>(Swizzle(lowHigh, 0xEE));
4786         }
4787
4788         RValue<Int> Extract(RValue<Int2> val, int i)
4789         {
4790                 return RValue<Int>(Nucleus::createExtractElement(val.value, Int::getType(), i));
4791         }
4792
4793         RValue<Int2> Insert(RValue<Int2> val, RValue<Int> element, int i)
4794         {
4795                 return RValue<Int2>(Nucleus::createInsertElement(val.value, element.value, i));
4796         }
4797
4798         Type *Int2::getType()
4799         {
4800                 return T(Type_v2i32);
4801         }
4802
4803         UInt2::UInt2(unsigned int x, unsigned int y)
4804         {
4805                 int64_t constantVector[2] = {x, y};
4806                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
4807         }
4808
4809         UInt2::UInt2(RValue<UInt2> rhs)
4810         {
4811                 storeValue(rhs.value);
4812         }
4813
4814         UInt2::UInt2(const UInt2 &rhs)
4815         {
4816                 Value *value = rhs.loadValue();
4817                 storeValue(value);
4818         }
4819
4820         UInt2::UInt2(const Reference<UInt2> &rhs)
4821         {
4822                 Value *value = rhs.loadValue();
4823                 storeValue(value);
4824         }
4825
4826         RValue<UInt2> UInt2::operator=(RValue<UInt2> rhs)
4827         {
4828                 storeValue(rhs.value);
4829
4830                 return rhs;
4831         }
4832
4833         RValue<UInt2> UInt2::operator=(const UInt2 &rhs)
4834         {
4835                 Value *value = rhs.loadValue();
4836                 storeValue(value);
4837
4838                 return RValue<UInt2>(value);
4839         }
4840
4841         RValue<UInt2> UInt2::operator=(const Reference<UInt2> &rhs)
4842         {
4843                 Value *value = rhs.loadValue();
4844                 storeValue(value);
4845
4846                 return RValue<UInt2>(value);
4847         }
4848
4849         RValue<UInt2> operator+(RValue<UInt2> lhs, RValue<UInt2> rhs)
4850         {
4851                 return RValue<UInt2>(Nucleus::createAdd(lhs.value, rhs.value));
4852         }
4853
4854         RValue<UInt2> operator-(RValue<UInt2> lhs, RValue<UInt2> rhs)
4855         {
4856                 return RValue<UInt2>(Nucleus::createSub(lhs.value, rhs.value));
4857         }
4858
4859 //      RValue<UInt2> operator*(RValue<UInt2> lhs, RValue<UInt2> rhs)
4860 //      {
4861 //              return RValue<UInt2>(Nucleus::createMul(lhs.value, rhs.value));
4862 //      }
4863
4864 //      RValue<UInt2> operator/(RValue<UInt2> lhs, RValue<UInt2> rhs)
4865 //      {
4866 //              return RValue<UInt2>(Nucleus::createUDiv(lhs.value, rhs.value));
4867 //      }
4868
4869 //      RValue<UInt2> operator%(RValue<UInt2> lhs, RValue<UInt2> rhs)
4870 //      {
4871 //              return RValue<UInt2>(Nucleus::createURem(lhs.value, rhs.value));
4872 //      }
4873
4874         RValue<UInt2> operator&(RValue<UInt2> lhs, RValue<UInt2> rhs)
4875         {
4876                 return RValue<UInt2>(Nucleus::createAnd(lhs.value, rhs.value));
4877         }
4878
4879         RValue<UInt2> operator|(RValue<UInt2> lhs, RValue<UInt2> rhs)
4880         {
4881                 return RValue<UInt2>(Nucleus::createOr(lhs.value, rhs.value));
4882         }
4883
4884         RValue<UInt2> operator^(RValue<UInt2> lhs, RValue<UInt2> rhs)
4885         {
4886                 return RValue<UInt2>(Nucleus::createXor(lhs.value, rhs.value));
4887         }
4888
4889         RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs)
4890         {
4891                 return RValue<UInt2>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
4892         }
4893
4894         RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs)
4895         {
4896                 return RValue<UInt2>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
4897         }
4898
4899         RValue<UInt2> operator+=(UInt2 &lhs, RValue<UInt2> rhs)
4900         {
4901                 return lhs = lhs + rhs;
4902         }
4903
4904         RValue<UInt2> operator-=(UInt2 &lhs, RValue<UInt2> rhs)
4905         {
4906                 return lhs = lhs - rhs;
4907         }
4908
4909 //      RValue<UInt2> operator*=(UInt2 &lhs, RValue<UInt2> rhs)
4910 //      {
4911 //              return lhs = lhs * rhs;
4912 //      }
4913
4914 //      RValue<UInt2> operator/=(UInt2 &lhs, RValue<UInt2> rhs)
4915 //      {
4916 //              return lhs = lhs / rhs;
4917 //      }
4918
4919 //      RValue<UInt2> operator%=(UInt2 &lhs, RValue<UInt2> rhs)
4920 //      {
4921 //              return lhs = lhs % rhs;
4922 //      }
4923
4924         RValue<UInt2> operator&=(UInt2 &lhs, RValue<UInt2> rhs)
4925         {
4926                 return lhs = lhs & rhs;
4927         }
4928
4929         RValue<UInt2> operator|=(UInt2 &lhs, RValue<UInt2> rhs)
4930         {
4931                 return lhs = lhs | rhs;
4932         }
4933
4934         RValue<UInt2> operator^=(UInt2 &lhs, RValue<UInt2> rhs)
4935         {
4936                 return lhs = lhs ^ rhs;
4937         }
4938
4939         RValue<UInt2> operator<<=(UInt2 &lhs, unsigned char rhs)
4940         {
4941                 return lhs = lhs << rhs;
4942         }
4943
4944         RValue<UInt2> operator>>=(UInt2 &lhs, unsigned char rhs)
4945         {
4946                 return lhs = lhs >> rhs;
4947         }
4948
4949 //      RValue<UInt2> operator+(RValue<UInt2> val)
4950 //      {
4951 //              return val;
4952 //      }
4953
4954 //      RValue<UInt2> operator-(RValue<UInt2> val)
4955 //      {
4956 //              return RValue<UInt2>(Nucleus::createNeg(val.value));
4957 //      }
4958
4959         RValue<UInt2> operator~(RValue<UInt2> val)
4960         {
4961                 return RValue<UInt2>(Nucleus::createNot(val.value));
4962         }
4963
4964         Type *UInt2::getType()
4965         {
4966                 return T(Type_v2i32);
4967         }
4968
4969         Int4::Int4(RValue<Byte4> cast)
4970         {
4971                 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
4972                 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
4973
4974                 Value *e;
4975                 int swizzle[16] = {0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23};
4976                 Value *b = Nucleus::createBitCast(a, Byte16::getType());
4977                 Value *c = Nucleus::createShuffleVector(b, V(Nucleus::createNullValue(Byte16::getType())), swizzle);
4978
4979                 int swizzle2[8] = {0, 8, 1, 9, 2, 10, 3, 11};
4980                 Value *d = Nucleus::createBitCast(c, Short8::getType());
4981                 e = Nucleus::createShuffleVector(d, V(Nucleus::createNullValue(Short8::getType())), swizzle2);
4982
4983                 Value *f = Nucleus::createBitCast(e, Int4::getType());
4984                 storeValue(f);
4985         }
4986
4987         Int4::Int4(RValue<SByte4> cast)
4988         {
4989                 Value *x = Nucleus::createBitCast(cast.value, Int::getType());
4990                 Value *a = Nucleus::createInsertElement(loadValue(), x, 0);
4991
4992                 Value *e;
4993                 int swizzle[16] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7};
4994                 Value *b = Nucleus::createBitCast(a, Byte16::getType());
4995                 Value *c = Nucleus::createShuffleVector(b, b, swizzle);
4996
4997                 int swizzle2[8] = {0, 0, 1, 1, 2, 2, 3, 3};
4998                 Value *d = Nucleus::createBitCast(c, Short8::getType());
4999                 e = Nucleus::createShuffleVector(d, d, swizzle2);
5000
5001                 Value *f = Nucleus::createBitCast(e, Int4::getType());
5002                 Value *g = Nucleus::createAShr(f, V(::context->getConstantInt32(24)));
5003                 storeValue(g);
5004         }
5005
5006         Int4::Int4(RValue<Float4> cast)
5007         {
5008                 Value *xyzw = Nucleus::createFPToSI(cast.value, Int4::getType());
5009
5010                 storeValue(xyzw);
5011         }
5012
5013         Int4::Int4(RValue<Short4> cast)
5014         {
5015                 int swizzle[8] = {0, 0, 1, 1, 2, 2, 3, 3};
5016                 Value *c = Nucleus::createShuffleVector(cast.value, cast.value, swizzle);
5017                 Value *d = Nucleus::createBitCast(c, Int4::getType());
5018                 Value *e = Nucleus::createAShr(d, V(::context->getConstantInt32(16)));
5019                 storeValue(e);
5020         }
5021
5022         Int4::Int4(RValue<UShort4> cast)
5023         {
5024                 int swizzle[8] = {0, 8, 1, 9, 2, 10, 3, 11};
5025                 Value *c = Nucleus::createShuffleVector(cast.value, Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle);
5026                 Value *d = Nucleus::createBitCast(c, Int4::getType());
5027                 storeValue(d);
5028         }
5029
5030         Int4::Int4(int xyzw)
5031         {
5032                 constant(xyzw, xyzw, xyzw, xyzw);
5033         }
5034
5035         Int4::Int4(int x, int yzw)
5036         {
5037                 constant(x, yzw, yzw, yzw);
5038         }
5039
5040         Int4::Int4(int x, int y, int zw)
5041         {
5042                 constant(x, y, zw, zw);
5043         }
5044
5045         Int4::Int4(int x, int y, int z, int w)
5046         {
5047                 constant(x, y, z, w);
5048         }
5049
5050         void Int4::constant(int x, int y, int z, int w)
5051         {
5052                 int64_t constantVector[4] = {x, y, z, w};
5053                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
5054         }
5055
5056         Int4::Int4(RValue<Int4> rhs)
5057         {
5058                 storeValue(rhs.value);
5059         }
5060
5061         Int4::Int4(const Int4 &rhs)
5062         {
5063                 Value *value = rhs.loadValue();
5064                 storeValue(value);
5065         }
5066
5067         Int4::Int4(const Reference<Int4> &rhs)
5068         {
5069                 Value *value = rhs.loadValue();
5070                 storeValue(value);
5071         }
5072
5073         Int4::Int4(RValue<UInt4> rhs)
5074         {
5075                 storeValue(rhs.value);
5076         }
5077
5078         Int4::Int4(const UInt4 &rhs)
5079         {
5080                 Value *value = rhs.loadValue();
5081                 storeValue(value);
5082         }
5083
5084         Int4::Int4(const Reference<UInt4> &rhs)
5085         {
5086                 Value *value = rhs.loadValue();
5087                 storeValue(value);
5088         }
5089
5090         Int4::Int4(RValue<Int2> lo, RValue<Int2> hi)
5091         {
5092                 int shuffle[4] = {0, 1, 4, 5};   // Real type is v4i32
5093                 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5094
5095                 storeValue(packed);
5096         }
5097
5098         Int4::Int4(RValue<Int> rhs)
5099         {
5100                 Value *vector = Nucleus::createBitCast(rhs.value, Int4::getType());
5101
5102                 int swizzle[4] = {0, 0, 0, 0};
5103                 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
5104
5105                 storeValue(replicate);
5106         }
5107
5108         Int4::Int4(const Int &rhs)
5109         {
5110                 *this = RValue<Int>(rhs.loadValue());
5111         }
5112
5113         Int4::Int4(const Reference<Int> &rhs)
5114         {
5115                 *this = RValue<Int>(rhs.loadValue());
5116         }
5117
5118         RValue<Int4> Int4::operator=(RValue<Int4> rhs)
5119         {
5120                 storeValue(rhs.value);
5121
5122                 return rhs;
5123         }
5124
5125         RValue<Int4> Int4::operator=(const Int4 &rhs)
5126         {
5127                 Value *value = rhs.loadValue();
5128                 storeValue(value);
5129
5130                 return RValue<Int4>(value);
5131         }
5132
5133         RValue<Int4> Int4::operator=(const Reference<Int4> &rhs)
5134         {
5135                 Value *value = rhs.loadValue();
5136                 storeValue(value);
5137
5138                 return RValue<Int4>(value);
5139         }
5140
5141         RValue<Int4> operator+(RValue<Int4> lhs, RValue<Int4> rhs)
5142         {
5143                 return RValue<Int4>(Nucleus::createAdd(lhs.value, rhs.value));
5144         }
5145
5146         RValue<Int4> operator-(RValue<Int4> lhs, RValue<Int4> rhs)
5147         {
5148                 return RValue<Int4>(Nucleus::createSub(lhs.value, rhs.value));
5149         }
5150
5151         RValue<Int4> operator*(RValue<Int4> lhs, RValue<Int4> rhs)
5152         {
5153                 return RValue<Int4>(Nucleus::createMul(lhs.value, rhs.value));
5154         }
5155
5156         RValue<Int4> operator/(RValue<Int4> lhs, RValue<Int4> rhs)
5157         {
5158                 return RValue<Int4>(Nucleus::createSDiv(lhs.value, rhs.value));
5159         }
5160
5161         RValue<Int4> operator%(RValue<Int4> lhs, RValue<Int4> rhs)
5162         {
5163                 return RValue<Int4>(Nucleus::createSRem(lhs.value, rhs.value));
5164         }
5165
5166         RValue<Int4> operator&(RValue<Int4> lhs, RValue<Int4> rhs)
5167         {
5168                 return RValue<Int4>(Nucleus::createAnd(lhs.value, rhs.value));
5169         }
5170
5171         RValue<Int4> operator|(RValue<Int4> lhs, RValue<Int4> rhs)
5172         {
5173                 return RValue<Int4>(Nucleus::createOr(lhs.value, rhs.value));
5174         }
5175
5176         RValue<Int4> operator^(RValue<Int4> lhs, RValue<Int4> rhs)
5177         {
5178                 return RValue<Int4>(Nucleus::createXor(lhs.value, rhs.value));
5179         }
5180
5181         RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs)
5182         {
5183                 return RValue<Int4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
5184         }
5185
5186         RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs)
5187         {
5188                 return RValue<Int4>(Nucleus::createAShr(lhs.value, V(::context->getConstantInt32(rhs))));
5189         }
5190
5191         RValue<Int4> operator<<(RValue<Int4> lhs, RValue<Int4> rhs)
5192         {
5193                 return RValue<Int4>(Nucleus::createShl(lhs.value, rhs.value));
5194         }
5195
5196         RValue<Int4> operator>>(RValue<Int4> lhs, RValue<Int4> rhs)
5197         {
5198                 return RValue<Int4>(Nucleus::createAShr(lhs.value, rhs.value));
5199         }
5200
5201         RValue<Int4> operator+=(Int4 &lhs, RValue<Int4> rhs)
5202         {
5203                 return lhs = lhs + rhs;
5204         }
5205
5206         RValue<Int4> operator-=(Int4 &lhs, RValue<Int4> rhs)
5207         {
5208                 return lhs = lhs - rhs;
5209         }
5210
5211         RValue<Int4> operator*=(Int4 &lhs, RValue<Int4> rhs)
5212         {
5213                 return lhs = lhs * rhs;
5214         }
5215
5216 //      RValue<Int4> operator/=(Int4 &lhs, RValue<Int4> rhs)
5217 //      {
5218 //              return lhs = lhs / rhs;
5219 //      }
5220
5221 //      RValue<Int4> operator%=(Int4 &lhs, RValue<Int4> rhs)
5222 //      {
5223 //              return lhs = lhs % rhs;
5224 //      }
5225
5226         RValue<Int4> operator&=(Int4 &lhs, RValue<Int4> rhs)
5227         {
5228                 return lhs = lhs & rhs;
5229         }
5230
5231         RValue<Int4> operator|=(Int4 &lhs, RValue<Int4> rhs)
5232         {
5233                 return lhs = lhs | rhs;
5234         }
5235
5236         RValue<Int4> operator^=(Int4 &lhs, RValue<Int4> rhs)
5237         {
5238                 return lhs = lhs ^ rhs;
5239         }
5240
5241         RValue<Int4> operator<<=(Int4 &lhs, unsigned char rhs)
5242         {
5243                 return lhs = lhs << rhs;
5244         }
5245
5246         RValue<Int4> operator>>=(Int4 &lhs, unsigned char rhs)
5247         {
5248                 return lhs = lhs >> rhs;
5249         }
5250
5251         RValue<Int4> operator+(RValue<Int4> val)
5252         {
5253                 return val;
5254         }
5255
5256         RValue<Int4> operator-(RValue<Int4> val)
5257         {
5258                 return RValue<Int4>(Nucleus::createNeg(val.value));
5259         }
5260
5261         RValue<Int4> operator~(RValue<Int4> val)
5262         {
5263                 return RValue<Int4>(Nucleus::createNot(val.value));
5264         }
5265
5266         RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y)
5267         {
5268                 return RValue<Int4>(Nucleus::createICmpEQ(x.value, y.value));
5269         }
5270
5271         RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y)
5272         {
5273                 return RValue<Int4>(Nucleus::createICmpSLT(x.value, y.value));
5274         }
5275
5276         RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y)
5277         {
5278                 return RValue<Int4>(Nucleus::createICmpSLE(x.value, y.value));
5279         }
5280
5281         RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y)
5282         {
5283                 return RValue<Int4>(Nucleus::createICmpNE(x.value, y.value));
5284         }
5285
5286         RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y)
5287         {
5288                 return RValue<Int4>(Nucleus::createICmpSGE(x.value, y.value));
5289         }
5290
5291         RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y)
5292         {
5293                 return RValue<Int4>(Nucleus::createICmpSGT(x.value, y.value));
5294         }
5295
5296         RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y)
5297         {
5298                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5299                 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value, y.value);
5300                 ::basicBlock->appendInst(cmp);
5301
5302                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5303                 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5304                 ::basicBlock->appendInst(select);
5305
5306                 return RValue<Int4>(V(result));
5307         }
5308
5309         RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y)
5310         {
5311                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5312                 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value, y.value);
5313                 ::basicBlock->appendInst(cmp);
5314
5315                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5316                 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5317                 ::basicBlock->appendInst(select);
5318
5319                 return RValue<Int4>(V(result));
5320         }
5321
5322         RValue<Int4> RoundInt(RValue<Float4> cast)
5323         {
5324                 if(emulateIntrinsics)
5325                 {
5326                         // Push the fractional part off the mantissa. Accurate up to +/-2^22.
5327                         return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000));
5328                 }
5329                 else
5330                 {
5331                         Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5332                         const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5333                         auto target = ::context->getConstantUndef(Ice::IceType_i32);
5334                         auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5335                         nearbyint->addArg(cast.value);
5336                         ::basicBlock->appendInst(nearbyint);
5337
5338                         return RValue<Int4>(V(result));
5339                 }
5340         }
5341
5342         RValue<Short8> Pack(RValue<Int4> x, RValue<Int4> y)
5343         {
5344                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5345                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5346                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5347                 auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5348                 pack->addArg(x.value);
5349                 pack->addArg(y.value);
5350                 ::basicBlock->appendInst(pack);
5351
5352                 return RValue<Short8>(V(result));
5353         }
5354
5355         RValue<Int> Extract(RValue<Int4> x, int i)
5356         {
5357                 return RValue<Int>(Nucleus::createExtractElement(x.value, Int::getType(), i));
5358         }
5359
5360         RValue<Int4> Insert(RValue<Int4> x, RValue<Int> element, int i)
5361         {
5362                 return RValue<Int4>(Nucleus::createInsertElement(x.value, element.value, i));
5363         }
5364
5365         RValue<Int> SignMask(RValue<Int4> x)
5366         {
5367                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
5368                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5369                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5370                 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5371                 movmsk->addArg(x.value);
5372                 ::basicBlock->appendInst(movmsk);
5373
5374                 return RValue<Int>(V(result));
5375         }
5376
5377         RValue<Int4> Swizzle(RValue<Int4> x, unsigned char select)
5378         {
5379                 return RValue<Int4>(createSwizzle4(x.value, select));
5380         }
5381
5382         Type *Int4::getType()
5383         {
5384                 return T(Ice::IceType_v4i32);
5385         }
5386
5387         UInt4::UInt4(RValue<Float4> cast)
5388         {
5389                 // Smallest positive value representable in UInt, but not in Int
5390                 const unsigned int ustart = 0x80000000u;
5391                 const float ustartf = float(ustart);
5392
5393                 // Check if the value can be represented as an Int
5394                 Int4 uiValue = CmpNLT(cast, Float4(ustartf));
5395                 // If the value is too large, subtract ustart and re-add it after conversion.
5396                 uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) |
5397                 // Otherwise, just convert normally
5398                           (~uiValue & Int4(cast));
5399                 // If the value is negative, store 0, otherwise store the result of the conversion
5400                 storeValue((~(As<Int4>(cast) >> 31) & uiValue).value);
5401         }
5402
5403         UInt4::UInt4(int xyzw)
5404         {
5405                 constant(xyzw, xyzw, xyzw, xyzw);
5406         }
5407
5408         UInt4::UInt4(int x, int yzw)
5409         {
5410                 constant(x, yzw, yzw, yzw);
5411         }
5412
5413         UInt4::UInt4(int x, int y, int zw)
5414         {
5415                 constant(x, y, zw, zw);
5416         }
5417
5418         UInt4::UInt4(int x, int y, int z, int w)
5419         {
5420                 constant(x, y, z, w);
5421         }
5422
5423         void UInt4::constant(int x, int y, int z, int w)
5424         {
5425                 int64_t constantVector[4] = {x, y, z, w};
5426                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
5427         }
5428
5429         UInt4::UInt4(RValue<UInt4> rhs)
5430         {
5431                 storeValue(rhs.value);
5432         }
5433
5434         UInt4::UInt4(const UInt4 &rhs)
5435         {
5436                 Value *value = rhs.loadValue();
5437                 storeValue(value);
5438         }
5439
5440         UInt4::UInt4(const Reference<UInt4> &rhs)
5441         {
5442                 Value *value = rhs.loadValue();
5443                 storeValue(value);
5444         }
5445
5446         UInt4::UInt4(RValue<Int4> rhs)
5447         {
5448                 storeValue(rhs.value);
5449         }
5450
5451         UInt4::UInt4(const Int4 &rhs)
5452         {
5453                 Value *value = rhs.loadValue();
5454                 storeValue(value);
5455         }
5456
5457         UInt4::UInt4(const Reference<Int4> &rhs)
5458         {
5459                 Value *value = rhs.loadValue();
5460                 storeValue(value);
5461         }
5462
5463         UInt4::UInt4(RValue<UInt2> lo, RValue<UInt2> hi)
5464         {
5465                 int shuffle[4] = {0, 1, 4, 5};   // Real type is v4i32
5466                 Value *packed = Nucleus::createShuffleVector(lo.value, hi.value, shuffle);
5467
5468                 storeValue(packed);
5469         }
5470
5471         RValue<UInt4> UInt4::operator=(RValue<UInt4> rhs)
5472         {
5473                 storeValue(rhs.value);
5474
5475                 return rhs;
5476         }
5477
5478         RValue<UInt4> UInt4::operator=(const UInt4 &rhs)
5479         {
5480                 Value *value = rhs.loadValue();
5481                 storeValue(value);
5482
5483                 return RValue<UInt4>(value);
5484         }
5485
5486         RValue<UInt4> UInt4::operator=(const Reference<UInt4> &rhs)
5487         {
5488                 Value *value = rhs.loadValue();
5489                 storeValue(value);
5490
5491                 return RValue<UInt4>(value);
5492         }
5493
5494         RValue<UInt4> operator+(RValue<UInt4> lhs, RValue<UInt4> rhs)
5495         {
5496                 return RValue<UInt4>(Nucleus::createAdd(lhs.value, rhs.value));
5497         }
5498
5499         RValue<UInt4> operator-(RValue<UInt4> lhs, RValue<UInt4> rhs)
5500         {
5501                 return RValue<UInt4>(Nucleus::createSub(lhs.value, rhs.value));
5502         }
5503
5504         RValue<UInt4> operator*(RValue<UInt4> lhs, RValue<UInt4> rhs)
5505         {
5506                 return RValue<UInt4>(Nucleus::createMul(lhs.value, rhs.value));
5507         }
5508
5509         RValue<UInt4> operator/(RValue<UInt4> lhs, RValue<UInt4> rhs)
5510         {
5511                 return RValue<UInt4>(Nucleus::createUDiv(lhs.value, rhs.value));
5512         }
5513
5514         RValue<UInt4> operator%(RValue<UInt4> lhs, RValue<UInt4> rhs)
5515         {
5516                 return RValue<UInt4>(Nucleus::createURem(lhs.value, rhs.value));
5517         }
5518
5519         RValue<UInt4> operator&(RValue<UInt4> lhs, RValue<UInt4> rhs)
5520         {
5521                 return RValue<UInt4>(Nucleus::createAnd(lhs.value, rhs.value));
5522         }
5523
5524         RValue<UInt4> operator|(RValue<UInt4> lhs, RValue<UInt4> rhs)
5525         {
5526                 return RValue<UInt4>(Nucleus::createOr(lhs.value, rhs.value));
5527         }
5528
5529         RValue<UInt4> operator^(RValue<UInt4> lhs, RValue<UInt4> rhs)
5530         {
5531                 return RValue<UInt4>(Nucleus::createXor(lhs.value, rhs.value));
5532         }
5533
5534         RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs)
5535         {
5536                 return RValue<UInt4>(Nucleus::createShl(lhs.value, V(::context->getConstantInt32(rhs))));
5537         }
5538
5539         RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs)
5540         {
5541                 return RValue<UInt4>(Nucleus::createLShr(lhs.value, V(::context->getConstantInt32(rhs))));
5542         }
5543
5544         RValue<UInt4> operator<<(RValue<UInt4> lhs, RValue<UInt4> rhs)
5545         {
5546                 return RValue<UInt4>(Nucleus::createShl(lhs.value, rhs.value));
5547         }
5548
5549         RValue<UInt4> operator>>(RValue<UInt4> lhs, RValue<UInt4> rhs)
5550         {
5551                 return RValue<UInt4>(Nucleus::createLShr(lhs.value, rhs.value));
5552         }
5553
5554         RValue<UInt4> operator+=(UInt4 &lhs, RValue<UInt4> rhs)
5555         {
5556                 return lhs = lhs + rhs;
5557         }
5558
5559         RValue<UInt4> operator-=(UInt4 &lhs, RValue<UInt4> rhs)
5560         {
5561                 return lhs = lhs - rhs;
5562         }
5563
5564         RValue<UInt4> operator*=(UInt4 &lhs, RValue<UInt4> rhs)
5565         {
5566                 return lhs = lhs * rhs;
5567         }
5568
5569 //      RValue<UInt4> operator/=(UInt4 &lhs, RValue<UInt4> rhs)
5570 //      {
5571 //              return lhs = lhs / rhs;
5572 //      }
5573
5574 //      RValue<UInt4> operator%=(UInt4 &lhs, RValue<UInt4> rhs)
5575 //      {
5576 //              return lhs = lhs % rhs;
5577 //      }
5578
5579         RValue<UInt4> operator&=(UInt4 &lhs, RValue<UInt4> rhs)
5580         {
5581                 return lhs = lhs & rhs;
5582         }
5583
5584         RValue<UInt4> operator|=(UInt4 &lhs, RValue<UInt4> rhs)
5585         {
5586                 return lhs = lhs | rhs;
5587         }
5588
5589         RValue<UInt4> operator^=(UInt4 &lhs, RValue<UInt4> rhs)
5590         {
5591                 return lhs = lhs ^ rhs;
5592         }
5593
5594         RValue<UInt4> operator<<=(UInt4 &lhs, unsigned char rhs)
5595         {
5596                 return lhs = lhs << rhs;
5597         }
5598
5599         RValue<UInt4> operator>>=(UInt4 &lhs, unsigned char rhs)
5600         {
5601                 return lhs = lhs >> rhs;
5602         }
5603
5604         RValue<UInt4> operator+(RValue<UInt4> val)
5605         {
5606                 return val;
5607         }
5608
5609         RValue<UInt4> operator-(RValue<UInt4> val)
5610         {
5611                 return RValue<UInt4>(Nucleus::createNeg(val.value));
5612         }
5613
5614         RValue<UInt4> operator~(RValue<UInt4> val)
5615         {
5616                 return RValue<UInt4>(Nucleus::createNot(val.value));
5617         }
5618
5619         RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y)
5620         {
5621                 return RValue<UInt4>(Nucleus::createICmpEQ(x.value, y.value));
5622         }
5623
5624         RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y)
5625         {
5626                 return RValue<UInt4>(Nucleus::createICmpULT(x.value, y.value));
5627         }
5628
5629         RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y)
5630         {
5631                 return RValue<UInt4>(Nucleus::createICmpULE(x.value, y.value));
5632         }
5633
5634         RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y)
5635         {
5636                 return RValue<UInt4>(Nucleus::createICmpNE(x.value, y.value));
5637         }
5638
5639         RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y)
5640         {
5641                 return RValue<UInt4>(Nucleus::createICmpUGE(x.value, y.value));
5642         }
5643
5644         RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y)
5645         {
5646                 return RValue<UInt4>(Nucleus::createICmpUGT(x.value, y.value));
5647         }
5648
5649         RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y)
5650         {
5651                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5652                 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value, y.value);
5653                 ::basicBlock->appendInst(cmp);
5654
5655                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5656                 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5657                 ::basicBlock->appendInst(select);
5658
5659                 return RValue<UInt4>(V(result));
5660         }
5661
5662         RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y)
5663         {
5664                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
5665                 auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value, y.value);
5666                 ::basicBlock->appendInst(cmp);
5667
5668                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32);
5669                 auto select = Ice::InstSelect::create(::function, result, condition, y.value, x.value);
5670                 ::basicBlock->appendInst(select);
5671
5672                 return RValue<UInt4>(V(result));
5673         }
5674
5675         RValue<UShort8> Pack(RValue<UInt4> x, RValue<UInt4> y)
5676         {
5677                 if(CPUID::SSE4_1)
5678                 {
5679                         Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16);
5680                         const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5681                         auto target = ::context->getConstantUndef(Ice::IceType_i32);
5682                         auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
5683                         pack->addArg(x.value);
5684                         pack->addArg(y.value);
5685                         ::basicBlock->appendInst(pack);
5686
5687                         return RValue<UShort8>(V(result));
5688                 }
5689                 else
5690                 {
5691                         RValue<Int4> sx = As<Int4>(x);
5692                         RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000);
5693
5694                         RValue<Int4> sy = As<Int4>(y);
5695                         RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000);
5696
5697                         return As<UShort8>(Pack(bx, by) + Short8(0x8000u));
5698                 }
5699         }
5700
5701         Type *UInt4::getType()
5702         {
5703                 return T(Ice::IceType_v4i32);
5704         }
5705
5706         Float::Float(RValue<Int> cast)
5707         {
5708                 Value *integer = Nucleus::createSIToFP(cast.value, Float::getType());
5709
5710                 storeValue(integer);
5711         }
5712
5713         Float::Float(float x)
5714         {
5715                 storeValue(Nucleus::createConstantFloat(x));
5716         }
5717
5718         Float::Float(RValue<Float> rhs)
5719         {
5720                 storeValue(rhs.value);
5721         }
5722
5723         Float::Float(const Float &rhs)
5724         {
5725                 Value *value = rhs.loadValue();
5726                 storeValue(value);
5727         }
5728
5729         Float::Float(const Reference<Float> &rhs)
5730         {
5731                 Value *value = rhs.loadValue();
5732                 storeValue(value);
5733         }
5734
5735         RValue<Float> Float::operator=(RValue<Float> rhs)
5736         {
5737                 storeValue(rhs.value);
5738
5739                 return rhs;
5740         }
5741
5742         RValue<Float> Float::operator=(const Float &rhs)
5743         {
5744                 Value *value = rhs.loadValue();
5745                 storeValue(value);
5746
5747                 return RValue<Float>(value);
5748         }
5749
5750         RValue<Float> Float::operator=(const Reference<Float> &rhs)
5751         {
5752                 Value *value = rhs.loadValue();
5753                 storeValue(value);
5754
5755                 return RValue<Float>(value);
5756         }
5757
5758         RValue<Float> operator+(RValue<Float> lhs, RValue<Float> rhs)
5759         {
5760                 return RValue<Float>(Nucleus::createFAdd(lhs.value, rhs.value));
5761         }
5762
5763         RValue<Float> operator-(RValue<Float> lhs, RValue<Float> rhs)
5764         {
5765                 return RValue<Float>(Nucleus::createFSub(lhs.value, rhs.value));
5766         }
5767
5768         RValue<Float> operator*(RValue<Float> lhs, RValue<Float> rhs)
5769         {
5770                 return RValue<Float>(Nucleus::createFMul(lhs.value, rhs.value));
5771         }
5772
5773         RValue<Float> operator/(RValue<Float> lhs, RValue<Float> rhs)
5774         {
5775                 return RValue<Float>(Nucleus::createFDiv(lhs.value, rhs.value));
5776         }
5777
5778         RValue<Float> operator+=(Float &lhs, RValue<Float> rhs)
5779         {
5780                 return lhs = lhs + rhs;
5781         }
5782
5783         RValue<Float> operator-=(Float &lhs, RValue<Float> rhs)
5784         {
5785                 return lhs = lhs - rhs;
5786         }
5787
5788         RValue<Float> operator*=(Float &lhs, RValue<Float> rhs)
5789         {
5790                 return lhs = lhs * rhs;
5791         }
5792
5793         RValue<Float> operator/=(Float &lhs, RValue<Float> rhs)
5794         {
5795                 return lhs = lhs / rhs;
5796         }
5797
5798         RValue<Float> operator+(RValue<Float> val)
5799         {
5800                 return val;
5801         }
5802
5803         RValue<Float> operator-(RValue<Float> val)
5804         {
5805                 return RValue<Float>(Nucleus::createFNeg(val.value));
5806         }
5807
5808         RValue<Bool> operator<(RValue<Float> lhs, RValue<Float> rhs)
5809         {
5810                 return RValue<Bool>(Nucleus::createFCmpOLT(lhs.value, rhs.value));
5811         }
5812
5813         RValue<Bool> operator<=(RValue<Float> lhs, RValue<Float> rhs)
5814         {
5815                 return RValue<Bool>(Nucleus::createFCmpOLE(lhs.value, rhs.value));
5816         }
5817
5818         RValue<Bool> operator>(RValue<Float> lhs, RValue<Float> rhs)
5819         {
5820                 return RValue<Bool>(Nucleus::createFCmpOGT(lhs.value, rhs.value));
5821         }
5822
5823         RValue<Bool> operator>=(RValue<Float> lhs, RValue<Float> rhs)
5824         {
5825                 return RValue<Bool>(Nucleus::createFCmpOGE(lhs.value, rhs.value));
5826         }
5827
5828         RValue<Bool> operator!=(RValue<Float> lhs, RValue<Float> rhs)
5829         {
5830                 return RValue<Bool>(Nucleus::createFCmpONE(lhs.value, rhs.value));
5831         }
5832
5833         RValue<Bool> operator==(RValue<Float> lhs, RValue<Float> rhs)
5834         {
5835                 return RValue<Bool>(Nucleus::createFCmpOEQ(lhs.value, rhs.value));
5836         }
5837
5838         RValue<Float> Abs(RValue<Float> x)
5839         {
5840                 return IfThenElse(x > 0.0f, x, -x);
5841         }
5842
5843         RValue<Float> Max(RValue<Float> x, RValue<Float> y)
5844         {
5845                 return IfThenElse(x > y, x, y);
5846         }
5847
5848         RValue<Float> Min(RValue<Float> x, RValue<Float> y)
5849         {
5850                 return IfThenElse(x < y, x, y);
5851         }
5852
5853         RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2)
5854         {
5855                 return 1.0f / x;
5856         }
5857
5858         RValue<Float> RcpSqrt_pp(RValue<Float> x)
5859         {
5860                 return Rcp_pp(Sqrt(x));
5861         }
5862
5863         RValue<Float> Sqrt(RValue<Float> x)
5864         {
5865                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32);
5866                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
5867                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
5868                 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
5869                 sqrt->addArg(x.value);
5870                 ::basicBlock->appendInst(sqrt);
5871
5872                 return RValue<Float>(V(result));
5873         }
5874
5875         RValue<Float> Round(RValue<Float> x)
5876         {
5877                 return Float4(Round(Float4(x))).x;
5878         }
5879
5880         RValue<Float> Trunc(RValue<Float> x)
5881         {
5882                 return Float4(Trunc(Float4(x))).x;
5883         }
5884
5885         RValue<Float> Frac(RValue<Float> x)
5886         {
5887                 return Float4(Frac(Float4(x))).x;
5888         }
5889
5890         RValue<Float> Floor(RValue<Float> x)
5891         {
5892                 return Float4(Floor(Float4(x))).x;
5893         }
5894
5895         RValue<Float> Ceil(RValue<Float> x)
5896         {
5897                 return Float4(Ceil(Float4(x))).x;
5898         }
5899
5900         Type *Float::getType()
5901         {
5902                 return T(Ice::IceType_f32);
5903         }
5904
5905         Float2::Float2(RValue<Float4> cast)
5906         {
5907                 storeValue(Nucleus::createBitCast(cast.value, getType()));
5908         }
5909
5910         Type *Float2::getType()
5911         {
5912                 return T(Type_v2f32);
5913         }
5914
5915         Float4::Float4(RValue<Byte4> cast) : FloatXYZW(this)
5916         {
5917                 Value *a = Int4(cast).loadValue();
5918                 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
5919
5920                 storeValue(xyzw);
5921         }
5922
5923         Float4::Float4(RValue<SByte4> cast) : FloatXYZW(this)
5924         {
5925                 Value *a = Int4(cast).loadValue();
5926                 Value *xyzw = Nucleus::createSIToFP(a, Float4::getType());
5927
5928                 storeValue(xyzw);
5929         }
5930
5931         Float4::Float4(RValue<Short4> cast) : FloatXYZW(this)
5932         {
5933                 Int4 c(cast);
5934                 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
5935         }
5936
5937         Float4::Float4(RValue<UShort4> cast) : FloatXYZW(this)
5938         {
5939                 Int4 c(cast);
5940                 storeValue(Nucleus::createSIToFP(RValue<Int4>(c).value, Float4::getType()));
5941         }
5942
5943         Float4::Float4(RValue<Int4> cast) : FloatXYZW(this)
5944         {
5945                 Value *xyzw = Nucleus::createSIToFP(cast.value, Float4::getType());
5946
5947                 storeValue(xyzw);
5948         }
5949
5950         Float4::Float4(RValue<UInt4> cast) : FloatXYZW(this)
5951         {
5952                 RValue<Float4> result = Float4(Int4(cast & UInt4(0x7FFFFFFF))) +
5953                                         As<Float4>((As<Int4>(cast) >> 31) & As<Int4>(Float4(0x80000000u)));
5954
5955                 storeValue(result.value);
5956         }
5957
5958         Float4::Float4() : FloatXYZW(this)
5959         {
5960         }
5961
5962         Float4::Float4(float xyzw) : FloatXYZW(this)
5963         {
5964                 constant(xyzw, xyzw, xyzw, xyzw);
5965         }
5966
5967         Float4::Float4(float x, float yzw) : FloatXYZW(this)
5968         {
5969                 constant(x, yzw, yzw, yzw);
5970         }
5971
5972         Float4::Float4(float x, float y, float zw) : FloatXYZW(this)
5973         {
5974                 constant(x, y, zw, zw);
5975         }
5976
5977         Float4::Float4(float x, float y, float z, float w) : FloatXYZW(this)
5978         {
5979                 constant(x, y, z, w);
5980         }
5981
5982         void Float4::constant(float x, float y, float z, float w)
5983         {
5984                 double constantVector[4] = {x, y, z, w};
5985                 storeValue(Nucleus::createConstantVector(constantVector, getType()));
5986         }
5987
5988         Float4::Float4(RValue<Float4> rhs) : FloatXYZW(this)
5989         {
5990                 storeValue(rhs.value);
5991         }
5992
5993         Float4::Float4(const Float4 &rhs) : FloatXYZW(this)
5994         {
5995                 Value *value = rhs.loadValue();
5996                 storeValue(value);
5997         }
5998
5999         Float4::Float4(const Reference<Float4> &rhs) : FloatXYZW(this)
6000         {
6001                 Value *value = rhs.loadValue();
6002                 storeValue(value);
6003         }
6004
6005         Float4::Float4(RValue<Float> rhs) : FloatXYZW(this)
6006         {
6007                 Value *vector = Nucleus::createBitCast(rhs.value, Float4::getType());
6008
6009                 int swizzle[4] = {0, 0, 0, 0};
6010                 Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle);
6011
6012                 storeValue(replicate);
6013         }
6014
6015         Float4::Float4(const Float &rhs) : FloatXYZW(this)
6016         {
6017                 *this = RValue<Float>(rhs.loadValue());
6018         }
6019
6020         Float4::Float4(const Reference<Float> &rhs) : FloatXYZW(this)
6021         {
6022                 *this = RValue<Float>(rhs.loadValue());
6023         }
6024
6025         RValue<Float4> Float4::operator=(float x)
6026         {
6027                 return *this = Float4(x, x, x, x);
6028         }
6029
6030         RValue<Float4> Float4::operator=(RValue<Float4> rhs)
6031         {
6032                 storeValue(rhs.value);
6033
6034                 return rhs;
6035         }
6036
6037         RValue<Float4> Float4::operator=(const Float4 &rhs)
6038         {
6039                 Value *value = rhs.loadValue();
6040                 storeValue(value);
6041
6042                 return RValue<Float4>(value);
6043         }
6044
6045         RValue<Float4> Float4::operator=(const Reference<Float4> &rhs)
6046         {
6047                 Value *value = rhs.loadValue();
6048                 storeValue(value);
6049
6050                 return RValue<Float4>(value);
6051         }
6052
6053         RValue<Float4> Float4::operator=(RValue<Float> rhs)
6054         {
6055                 return *this = Float4(rhs);
6056         }
6057
6058         RValue<Float4> Float4::operator=(const Float &rhs)
6059         {
6060                 return *this = Float4(rhs);
6061         }
6062
6063         RValue<Float4> Float4::operator=(const Reference<Float> &rhs)
6064         {
6065                 return *this = Float4(rhs);
6066         }
6067
6068         RValue<Float4> operator+(RValue<Float4> lhs, RValue<Float4> rhs)
6069         {
6070                 return RValue<Float4>(Nucleus::createFAdd(lhs.value, rhs.value));
6071         }
6072
6073         RValue<Float4> operator-(RValue<Float4> lhs, RValue<Float4> rhs)
6074         {
6075                 return RValue<Float4>(Nucleus::createFSub(lhs.value, rhs.value));
6076         }
6077
6078         RValue<Float4> operator*(RValue<Float4> lhs, RValue<Float4> rhs)
6079         {
6080                 return RValue<Float4>(Nucleus::createFMul(lhs.value, rhs.value));
6081         }
6082
6083         RValue<Float4> operator/(RValue<Float4> lhs, RValue<Float4> rhs)
6084         {
6085                 return RValue<Float4>(Nucleus::createFDiv(lhs.value, rhs.value));
6086         }
6087
6088         RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs)
6089         {
6090                 return RValue<Float4>(Nucleus::createFRem(lhs.value, rhs.value));
6091         }
6092
6093         RValue<Float4> operator+=(Float4 &lhs, RValue<Float4> rhs)
6094         {
6095                 return lhs = lhs + rhs;
6096         }
6097
6098         RValue<Float4> operator-=(Float4 &lhs, RValue<Float4> rhs)
6099         {
6100                 return lhs = lhs - rhs;
6101         }
6102
6103         RValue<Float4> operator*=(Float4 &lhs, RValue<Float4> rhs)
6104         {
6105                 return lhs = lhs * rhs;
6106         }
6107
6108         RValue<Float4> operator/=(Float4 &lhs, RValue<Float4> rhs)
6109         {
6110                 return lhs = lhs / rhs;
6111         }
6112
6113         RValue<Float4> operator%=(Float4 &lhs, RValue<Float4> rhs)
6114         {
6115                 return lhs = lhs % rhs;
6116         }
6117
6118         RValue<Float4> operator+(RValue<Float4> val)
6119         {
6120                 return val;
6121         }
6122
6123         RValue<Float4> operator-(RValue<Float4> val)
6124         {
6125                 return RValue<Float4>(Nucleus::createFNeg(val.value));
6126         }
6127
6128         RValue<Float4> Abs(RValue<Float4> x)
6129         {
6130                 Value *vector = Nucleus::createBitCast(x.value, Int4::getType());
6131                 int64_t constantVector[4] = {0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF};
6132                 Value *result = Nucleus::createAnd(vector, V(Nucleus::createConstantVector(constantVector, Int4::getType())));
6133
6134                 return As<Float4>(result);
6135         }
6136
6137         RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y)
6138         {
6139                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
6140                 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ogt, condition, x.value, y.value);
6141                 ::basicBlock->appendInst(cmp);
6142
6143                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6144                 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
6145                 ::basicBlock->appendInst(select);
6146
6147                 return RValue<Float4>(V(result));
6148         }
6149
6150         RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y)
6151         {
6152                 Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1);
6153                 auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Olt, condition, x.value, y.value);
6154                 ::basicBlock->appendInst(cmp);
6155
6156                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6157                 auto select = Ice::InstSelect::create(::function, result, condition, x.value, y.value);
6158                 ::basicBlock->appendInst(select);
6159
6160                 return RValue<Float4>(V(result));
6161         }
6162
6163         RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2)
6164         {
6165                 return Float4(1.0f) / x;
6166         }
6167
6168         RValue<Float4> RcpSqrt_pp(RValue<Float4> x)
6169         {
6170                 return Rcp_pp(Sqrt(x));
6171         }
6172
6173         RValue<Float4> Sqrt(RValue<Float4> x)
6174         {
6175                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6176                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6177                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6178                 auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6179                 sqrt->addArg(x.value);
6180                 ::basicBlock->appendInst(sqrt);
6181
6182                 return RValue<Float4>(V(result));
6183         }
6184
6185         RValue<Float4> Insert(RValue<Float4> x, RValue<Float> element, int i)
6186         {
6187                 return RValue<Float4>(Nucleus::createInsertElement(x.value, element.value, i));
6188         }
6189
6190         RValue<Float> Extract(RValue<Float4> x, int i)
6191         {
6192                 return RValue<Float>(Nucleus::createExtractElement(x.value, Float::getType(), i));
6193         }
6194
6195         RValue<Float4> Swizzle(RValue<Float4> x, unsigned char select)
6196         {
6197                 return RValue<Float4>(createSwizzle4(x.value, select));
6198         }
6199
6200         RValue<Float4> ShuffleLowHigh(RValue<Float4> x, RValue<Float4> y, unsigned char imm)
6201         {
6202                 int shuffle[4] =
6203                 {
6204                         ((imm >> 0) & 0x03) + 0,
6205                         ((imm >> 2) & 0x03) + 0,
6206                         ((imm >> 4) & 0x03) + 4,
6207                         ((imm >> 6) & 0x03) + 4,
6208                 };
6209
6210                 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
6211         }
6212
6213         RValue<Float4> UnpackLow(RValue<Float4> x, RValue<Float4> y)
6214         {
6215                 int shuffle[4] = {0, 4, 1, 5};
6216                 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
6217         }
6218
6219         RValue<Float4> UnpackHigh(RValue<Float4> x, RValue<Float4> y)
6220         {
6221                 int shuffle[4] = {2, 6, 3, 7};
6222                 return RValue<Float4>(Nucleus::createShuffleVector(x.value, y.value, shuffle));
6223         }
6224
6225         RValue<Float4> Mask(Float4 &lhs, RValue<Float4> rhs, unsigned char select)
6226         {
6227                 Value *vector = lhs.loadValue();
6228                 Value *result = createMask4(vector, rhs.value, select);
6229                 lhs.storeValue(result);
6230
6231                 return RValue<Float4>(result);
6232         }
6233
6234         RValue<Int> SignMask(RValue<Float4> x)
6235         {
6236                 Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32);
6237                 const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6238                 auto target = ::context->getConstantUndef(Ice::IceType_i32);
6239                 auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic);
6240                 movmsk->addArg(x.value);
6241                 ::basicBlock->appendInst(movmsk);
6242
6243                 return RValue<Int>(V(result));
6244         }
6245
6246         RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y)
6247         {
6248                 return RValue<Int4>(Nucleus::createFCmpOEQ(x.value, y.value));
6249         }
6250
6251         RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y)
6252         {
6253                 return RValue<Int4>(Nucleus::createFCmpOLT(x.value, y.value));
6254         }
6255
6256         RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y)
6257         {
6258                 return RValue<Int4>(Nucleus::createFCmpOLE(x.value, y.value));
6259         }
6260
6261         RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y)
6262         {
6263                 return RValue<Int4>(Nucleus::createFCmpONE(x.value, y.value));
6264         }
6265
6266         RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y)
6267         {
6268                 return RValue<Int4>(Nucleus::createFCmpOGE(x.value, y.value));
6269         }
6270
6271         RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y)
6272         {
6273                 return RValue<Int4>(Nucleus::createFCmpOGT(x.value, y.value));
6274         }
6275
6276         RValue<Float4> Round(RValue<Float4> x)
6277         {
6278                 if(emulateIntrinsics)
6279                 {
6280                         // Push the fractional part off the mantissa. Accurate up to +/-2^22.
6281                         return (x + Float4(0x00C00000)) - Float4(0x00C00000);
6282                 }
6283                 else if(CPUID::SSE4_1)
6284                 {
6285                         Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6286                         const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6287                         auto target = ::context->getConstantUndef(Ice::IceType_i32);
6288                         auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6289                         round->addArg(x.value);
6290                         round->addArg(::context->getConstantInt32(0));
6291                         ::basicBlock->appendInst(round);
6292
6293                         return RValue<Float4>(V(result));
6294                 }
6295                 else
6296                 {
6297                         return Float4(RoundInt(x));
6298                 }
6299         }
6300
6301         RValue<Float4> Trunc(RValue<Float4> x)
6302         {
6303                 if(CPUID::SSE4_1)
6304                 {
6305                         Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6306                         const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6307                         auto target = ::context->getConstantUndef(Ice::IceType_i32);
6308                         auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6309                         round->addArg(x.value);
6310                         round->addArg(::context->getConstantInt32(3));
6311                         ::basicBlock->appendInst(round);
6312
6313                         return RValue<Float4>(V(result));
6314                 }
6315                 else
6316                 {
6317                         return Float4(Int4(x));
6318                 }
6319         }
6320
6321         RValue<Float4> Frac(RValue<Float4> x)
6322         {
6323                 if(CPUID::SSE4_1)
6324                 {
6325                         return x - Floor(x);
6326                 }
6327                 else
6328                 {
6329                         Float4 frc = x - Float4(Int4(x));   // Signed fractional part
6330
6331                         return frc + As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1, 1, 1, 1)));
6332                 }
6333         }
6334
6335         RValue<Float4> Floor(RValue<Float4> x)
6336         {
6337                 if(CPUID::SSE4_1)
6338                 {
6339                         Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6340                         const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6341                         auto target = ::context->getConstantUndef(Ice::IceType_i32);
6342                         auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6343                         round->addArg(x.value);
6344                         round->addArg(::context->getConstantInt32(1));
6345                         ::basicBlock->appendInst(round);
6346
6347                         return RValue<Float4>(V(result));
6348                 }
6349                 else
6350                 {
6351                         return x - Frac(x);
6352                 }
6353         }
6354
6355         RValue<Float4> Ceil(RValue<Float4> x)
6356         {
6357                 if(CPUID::SSE4_1)
6358                 {
6359                         Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32);
6360                         const Ice::Intrinsics::IntrinsicInfo intrinsic = {Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F};
6361                         auto target = ::context->getConstantUndef(Ice::IceType_i32);
6362                         auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic);
6363                         round->addArg(x.value);
6364                         round->addArg(::context->getConstantInt32(2));
6365                         ::basicBlock->appendInst(round);
6366
6367                         return RValue<Float4>(V(result));
6368                 }
6369                 else
6370                 {
6371                         return -Floor(-x);
6372                 }
6373         }
6374
6375         Type *Float4::getType()
6376         {
6377                 return T(Ice::IceType_v4f32);
6378         }
6379
6380         RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, int offset)
6381         {
6382                 return lhs + RValue<Int>(Nucleus::createConstantInt(offset));
6383         }
6384
6385         RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6386         {
6387                 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, false));
6388         }
6389
6390         RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6391         {
6392                 return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, true));
6393         }
6394
6395         RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, int offset)
6396         {
6397                 return lhs = lhs + offset;
6398         }
6399
6400         RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<Int> offset)
6401         {
6402                 return lhs = lhs + offset;
6403         }
6404
6405         RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, RValue<UInt> offset)
6406         {
6407                 return lhs = lhs + offset;
6408         }
6409
6410         RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, int offset)
6411         {
6412                 return lhs + -offset;
6413         }
6414
6415         RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
6416         {
6417                 return lhs + -offset;
6418         }
6419
6420         RValue<Pointer<Byte>> operator-(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
6421         {
6422                 return lhs + -offset;
6423         }
6424
6425         RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, int offset)
6426         {
6427                 return lhs = lhs - offset;
6428         }
6429
6430         RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<Int> offset)
6431         {
6432                 return lhs = lhs - offset;
6433         }
6434
6435         RValue<Pointer<Byte>> operator-=(Pointer<Byte> &lhs, RValue<UInt> offset)
6436         {
6437                 return lhs = lhs - offset;
6438         }
6439
6440         void Return()
6441         {
6442                 Nucleus::createRetVoid();
6443                 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6444                 Nucleus::createUnreachable();
6445         }
6446
6447         void Return(RValue<Int> ret)
6448         {
6449                 Nucleus::createRet(ret.value);
6450                 Nucleus::setInsertBlock(Nucleus::createBasicBlock());
6451                 Nucleus::createUnreachable();
6452         }
6453
6454         void branch(RValue<Bool> cmp, BasicBlock *bodyBB, BasicBlock *endBB)
6455         {
6456                 Nucleus::createCondBr(cmp.value, bodyBB, endBB);
6457                 Nucleus::setInsertBlock(bodyBB);
6458         }
6459
6460         RValue<Long> Ticks()
6461         {
6462                 assert(false && "UNIMPLEMENTED"); return RValue<Long>(V(nullptr));
6463         }
6464 }