Abstract llvm::Type usage.

Bug swiftshader:10

Change-Id: I86b80cb03ff48ff7273aaf23a8e4995b3436f825
Reviewed-on: https://swiftshader-review.googlesource.com/7274
Tested-by: Nicolas Capens <capn@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/src/Reactor/Nucleus.cpp b/src/Reactor/Nucleus.cpp
index 7f7bf00..ab9a151 100644
--- a/src/Reactor/Nucleus.cpp
+++ b/src/Reactor/Nucleus.cpp
@@ -75,6 +75,20 @@
 
 	Optimization optimization[10] = {InstructionCombining, Disabled};
 
+	class Type : public llvm::Type
+	{
+	};
+
+	inline Type *T(llvm::Type *t)
+	{
+		return reinterpret_cast<Type*>(t);
+	}
+
+	inline std::vector<llvm::Type*> &T(std::vector<Type*> &t)
+	{
+		return reinterpret_cast<std::vector<llvm::Type*>&>(t);
+	}
+
 	Nucleus::Nucleus()
 	{
 		codegenMutex.lock();   // Reactor and LLVM are currently not thread safe
@@ -143,7 +157,7 @@
 	{
 		if(::builder->GetInsertBlock()->empty() || !::builder->GetInsertBlock()->back().isTerminator())
 		{
-			Type *type = ::function->getReturnType();
+			llvm::Type *type = ::function->getReturnType();
 
 			if(type->isVoidTy())
 			{
@@ -265,9 +279,9 @@
 		return *pred_begin(basicBlock);
 	}
 
-	void Nucleus::createFunction(llvm::Type *ReturnType, std::vector<llvm::Type*> &Params)
+	void Nucleus::createFunction(Type *ReturnType, std::vector<Type*> &Params)
 	{
-		llvm::FunctionType *functionType = llvm::FunctionType::get(ReturnType, Params, false);
+		llvm::FunctionType *functionType = llvm::FunctionType::get(ReturnType, T(Params), false);
 		::function = llvm::Function::Create(functionType, llvm::GlobalValue::InternalLinkage, "", ::module);
 		::function->setCallingConv(llvm::CallingConv::C);
 
@@ -714,7 +728,7 @@
 		return shuffle;
 	}
 
-	llvm::Constant *Nucleus::createConstantPointer(const void *address, llvm::Type *Ty, bool isConstant, unsigned int Align)
+	llvm::Constant *Nucleus::createConstantPointer(const void *address, Type *Ty, bool isConstant, unsigned int Align)
 	{
 		const GlobalValue *existingGlobal = ::executionEngine->getGlobalValueAtAddress(const_cast<void*>(address));   // FIXME: Const
 
@@ -723,7 +737,8 @@
 			return (llvm::Constant*)existingGlobal;
 		}
 
-		GlobalValue *global = new GlobalVariable(*::module, Ty, isConstant, GlobalValue::ExternalLinkage, 0, "");
+		llvm::GlobalValue *global = new llvm::GlobalVariable(*::module, Ty, isConstant, llvm::GlobalValue::ExternalLinkage, 0, "");
+
 		global->setAlignment(Align);
 
 		::executionEngine->addGlobalMapping(global, const_cast<void*>(address));
@@ -731,12 +746,12 @@
 		return global;
 	}
 
-	llvm::Type *Nucleus::getPointerType(llvm::Type *ElementType)
+	Type *Nucleus::getPointerType(Type *ElementType)
 	{
-		return llvm::PointerType::get(ElementType, 0);
+		return T(llvm::PointerType::get(ElementType, 0));
 	}
 
-	llvm::Constant *Nucleus::createNullValue(llvm::Type *Ty)
+	llvm::Constant *Nucleus::createNullValue(Type *Ty)
 	{
 		return llvm::Constant::getNullValue(Ty);
 	}
@@ -786,7 +801,7 @@
 		return ConstantFP::get(Float::getType(), x);
 	}
 
-	llvm::Value *Nucleus::createNullPointer(llvm::Type *Ty)
+	llvm::Value *Nucleus::createNullPointer(Type *Ty)
 	{
 		return llvm::ConstantPointerNull::get(llvm::PointerType::get(Ty, 0));
 	}
@@ -798,10 +813,10 @@
 
 	Type *Void::getType()
 	{
-		return Type::getVoidTy(*::context);
+		return T(llvm::Type::getVoidTy(*::context));
 	}
 
-	LValue::LValue(llvm::Type *type, int arraySize)
+	LValue::LValue(Type *type, int arraySize)
 	{
 		address = Nucleus::allocateStackVariable(type, arraySize);
 	}
@@ -834,7 +849,7 @@
 
 	Type *MMX::getType()
 	{
-		return Type::getX86_MMXTy(*::context);
+		return T(llvm::Type::getX86_MMXTy(*::context));
 	}
 
 	Bool::Bool(Argument<Bool> argument)
@@ -908,7 +923,7 @@
 
 	Type *Bool::getType()
 	{
-		return Type::getInt1Ty(*::context);
+		return T(llvm::Type::getInt1Ty(*::context));
 	}
 
 	Byte::Byte(Argument<Byte> argument)
@@ -1174,7 +1189,7 @@
 
 	Type *Byte::getType()
 	{
-		return Type::getInt8Ty(*::context);
+		return T(llvm::Type::getInt8Ty(*::context));
 	}
 
 	SByte::SByte(Argument<SByte> argument)
@@ -1428,7 +1443,7 @@
 
 	Type *SByte::getType()
 	{
-		return Type::getInt8Ty(*::context);
+		return T(llvm::Type::getInt8Ty(*::context));
 	}
 
 	Short::Short(Argument<Short> argument)
@@ -1675,7 +1690,7 @@
 
 	Type *Short::getType()
 	{
-		return Type::getInt16Ty(*::context);
+		return T(llvm::Type::getInt16Ty(*::context));
 	}
 
 	UShort::UShort(Argument<UShort> argument)
@@ -1929,13 +1944,13 @@
 
 	Type *UShort::getType()
 	{
-		return Type::getInt16Ty(*::context);
+		return T(llvm::Type::getInt16Ty(*::context));
 	}
 
 	Type *Byte4::getType()
 	{
 		#if 0
-			return VectorType::get(Byte::getType(), 4);
+			return T(VectorType::get(Byte::getType(), 4));
 		#else
 			return UInt::getType();   // FIXME: LLVM doesn't manipulate it as one 32-bit block
 		#endif
@@ -1944,7 +1959,7 @@
 	Type *SByte4::getType()
 	{
 		#if 0
-			return VectorType::get(SByte::getType(), 4);
+			return T(VectorType::get(SByte::getType(), 4));
 		#else
 			return Int::getType();   // FIXME: LLVM doesn't manipulate it as one 32-bit block
 		#endif
@@ -2283,7 +2298,7 @@
 		}
 		else
 		{
-			return VectorType::get(Byte::getType(), 8);
+			return T(VectorType::get(Byte::getType(), 8));
 		}
 	}
 
@@ -2591,7 +2606,7 @@
 		}
 		else
 		{
-			return VectorType::get(SByte::getType(), 8);
+			return T(VectorType::get(SByte::getType(), 8));
 		}
 	}
 
@@ -2643,12 +2658,12 @@
 
 	Type *Byte16::getType()
 	{
-		return VectorType::get(Byte::getType(), 16);
+		return T(VectorType::get(Byte::getType(), 16));
 	}
 
 	Type *SByte16::getType()
 	{
-		return VectorType::get(SByte::getType(), 16);
+		return T( VectorType::get(SByte::getType(), 16));
 	}
 
 	Short4::Short4(RValue<Int> cast)
@@ -3210,7 +3225,7 @@
 		}
 		else
 		{
-			return VectorType::get(Short::getType(), 4);
+			return T(VectorType::get(Short::getType(), 4));
 		}
 	}
 
@@ -3400,7 +3415,6 @@
 		}
 	}
 
-
 	RValue<UShort4> operator*(RValue<UShort4> lhs, RValue<UShort4> rhs)
 	{
 		if(CPUID::supportsMMX2())
@@ -3516,7 +3530,7 @@
 		}
 		else
 		{
-			return VectorType::get(UShort::getType(), 4);
+			return T(VectorType::get(UShort::getType(), 4));
 		}
 	}
 
@@ -3610,7 +3624,7 @@
 
 	Type *Short8::getType()
 	{
-		return VectorType::get(Short::getType(), 8);
+		return T(VectorType::get(Short::getType(), 8));
 	}
 
 	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)
@@ -3777,7 +3791,7 @@
 
 	Type *UShort8::getType()
 	{
-		return VectorType::get(UShort::getType(), 8);
+		return T(VectorType::get(UShort::getType(), 8));
 	}
 
 	Int::Int(Argument<Int> argument)
@@ -4131,13 +4145,11 @@
 
 	Type *Int::getType()
 	{
-		return Type::getInt32Ty(*::context);
+		return T(llvm::Type::getInt32Ty(*::context));
 	}
 
 	Long::Long(RValue<Int> cast)
 	{
-
-
 		Value *integer = Nucleus::createSExt(cast.value, Long::getType());
 
 		storeValue(integer);
@@ -4214,7 +4226,7 @@
 
 	Type *Long::getType()
 	{
-		return Type::getInt64Ty(*::context);
+		return T(llvm::Type::getInt64Ty(*::context));
 	}
 
 	Long1::Long1(const RValue<UInt> cast)
@@ -4238,7 +4250,7 @@
 		}
 		else
 		{
-			return VectorType::get(Long::getType(), 1);
+			return T(VectorType::get(Long::getType(), 1));
 		}
 	}
 
@@ -4255,7 +4267,7 @@
 
 	Type *Long2::getType()
 	{
-		return VectorType::get(Long::getType(), 2);
+		return T(VectorType::get(Long::getType(), 2));
 	}
 
 	UInt::UInt(Argument<UInt> argument)
@@ -4600,7 +4612,7 @@
 
 	Type *UInt::getType()
 	{
-		return Type::getInt32Ty(*::context);
+		return T(llvm::Type::getInt32Ty(*::context));
 	}
 
 //	Int2::Int2(RValue<Int> cast)
@@ -4681,7 +4693,7 @@
 			shuffle[0] = Nucleus::createConstantInt(0);
 			shuffle[1] = Nucleus::createConstantInt(1);
 
-			Value *packed = Nucleus::createShuffleVector(Nucleus::createBitCast(lo.value, VectorType::get(Int::getType(), 1)), Nucleus::createBitCast(hi.value, VectorType::get(Int::getType(), 1)), Nucleus::createConstantVector(shuffle, 2));
+			Value *packed = Nucleus::createShuffleVector(Nucleus::createBitCast(lo.value, T(VectorType::get(Int::getType(), 1))), Nucleus::createBitCast(hi.value, T(VectorType::get(Int::getType(), 1))), Nucleus::createConstantVector(shuffle, 2));
 
 			storeValue(Nucleus::createBitCast(packed, Int2::getType()));
 		}
@@ -4941,7 +4953,7 @@
 		{
 			if(i == 0)
 			{
-				return RValue<Int>(Nucleus::createExtractElement(Nucleus::createBitCast(val.value, VectorType::get(Int::getType(), 2)), 0));
+				return RValue<Int>(Nucleus::createExtractElement(Nucleus::createBitCast(val.value, T(VectorType::get(Int::getType(), 2))), 0));
 			}
 			else
 			{
@@ -4954,7 +4966,7 @@
 
 	RValue<Int2> Insert(RValue<Int2> val, RValue<Int> element, int i)
 	{
-		return RValue<Int2>(Nucleus::createBitCast(Nucleus::createInsertElement(Nucleus::createBitCast(val.value, VectorType::get(Int::getType(), 2)), element.value, i), Int2::getType()));
+		return RValue<Int2>(Nucleus::createBitCast(Nucleus::createInsertElement(Nucleus::createBitCast(val.value, T(VectorType::get(Int::getType(), 2))), element.value, i), Int2::getType()));
 	}
 
 	Type *Int2::getType()
@@ -4965,7 +4977,7 @@
 		}
 		else
 		{
-			return VectorType::get(Int::getType(), 2);
+			return T(VectorType::get(Int::getType(), 2));
 		}
 	}
 
@@ -5225,7 +5237,7 @@
 		}
 		else
 		{
-			return VectorType::get(UInt::getType(), 2);
+			return T(VectorType::get(UInt::getType(), 2));
 		}
 	}
 
@@ -5789,7 +5801,7 @@
 
 	Type *Int4::getType()
 	{
-		return VectorType::get(Int::getType(), 4);
+		return T(VectorType::get(Int::getType(), 4));
 	}
 
 	UInt4::UInt4(RValue<Float4> cast)
@@ -6130,7 +6142,7 @@
 
 	Type *UInt4::getType()
 	{
-		return VectorType::get(UInt::getType(), 4);
+		return T(VectorType::get(UInt::getType(), 4));
 	}
 
 	Float::Float(RValue<Int> cast)
@@ -6371,7 +6383,7 @@
 
 	Type *Float::getType()
 	{
-		return Type::getFloatTy(*::context);
+		return T(llvm::Type::getFloatTy(*::context));
 	}
 
 	Float2::Float2(RValue<Float4> cast)
@@ -6387,7 +6399,7 @@
 
 	Type *Float2::getType()
 	{
-		return VectorType::get(Float::getType(), 2);
+		return T(VectorType::get(Float::getType(), 2));
 	}
 
 	Float4::Float4(RValue<Byte4> cast)
@@ -6900,7 +6912,7 @@
 
 	Type *Float4::getType()
 	{
-		return VectorType::get(Float::getType(), 4);
+		return T(VectorType::get(Float::getType(), 4));
 	}
 
 	RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, int offset)