OSDN Git Service

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