Eliminate llvm::ConstantInt and llvm::GlobalValue exposure.

Use only llvm::Constant in the interface instead.

Bug swiftshader:10

Change-Id: Ideb2ed1ee083e4e6896c4bc8e8830ea02972b487
Reviewed-on: https://swiftshader-review.googlesource.com/7273
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 993ba28..7f7bf00 100644
--- a/src/Reactor/Nucleus.cpp
+++ b/src/Reactor/Nucleus.cpp
@@ -426,6 +426,11 @@
 		return ::builder->Insert(new StoreInst(value, ptr, isVolatile, align));
 	}
 
+	Value *Nucleus::createStore(Constant *constant, Value *ptr, bool isVolatile, unsigned int align)
+	{
+		return ::builder->Insert(new StoreInst(constant, ptr, isVolatile, align));
+	}
+
 	Value *Nucleus::createGEP(Value *ptr, Value *index)
 	{
 		return ::builder->CreateGEP(ptr, index);
@@ -668,7 +673,7 @@
 
 	void Nucleus::addSwitchCase(llvm::Value *Switch, int Case, llvm::BasicBlock *Branch)
 	{
-		static_cast<SwitchInst*>(Switch)->addCase(Nucleus::createConstantInt(Case), Branch);
+		static_cast<SwitchInst*>(Switch)->addCase(llvm::ConstantInt::get(Type::getInt32Ty(*::context), Case, true), Branch);
 	}
 
 	Value *Nucleus::createUnreachable()
@@ -709,21 +714,20 @@
 		return shuffle;
 	}
 
-	const llvm::GlobalValue *Nucleus::getGlobalValueAtAddress(void *Addr)
+	llvm::Constant *Nucleus::createConstantPointer(const void *address, llvm::Type *Ty, bool isConstant, unsigned int Align)
 	{
-		return ::executionEngine->getGlobalValueAtAddress(Addr);
-	}
+		const GlobalValue *existingGlobal = ::executionEngine->getGlobalValueAtAddress(const_cast<void*>(address));   // FIXME: Const
 
-	void Nucleus::addGlobalMapping(const llvm::GlobalValue *GV, void *Addr)
-	{
-		::executionEngine->addGlobalMapping(GV, Addr);
-	}
+		if(existingGlobal)
+		{
+			return (llvm::Constant*)existingGlobal;
+		}
 
-	llvm::GlobalValue *Nucleus::createGlobalValue(llvm::Type *Ty, bool isConstant, unsigned int Align)
-	{
-		llvm::GlobalValue *global = new llvm::GlobalVariable(*::module, Ty, isConstant, llvm::GlobalValue::ExternalLinkage, 0, "");
+		GlobalValue *global = new GlobalVariable(*::module, Ty, isConstant, GlobalValue::ExternalLinkage, 0, "");
 		global->setAlignment(Align);
 
+		::executionEngine->addGlobalMapping(global, const_cast<void*>(address));
+
 		return global;
 	}
 
@@ -737,42 +741,42 @@
 		return llvm::Constant::getNullValue(Ty);
 	}
 
-	llvm::ConstantInt *Nucleus::createConstantInt(int64_t i)
+	llvm::Constant *Nucleus::createConstantInt(int64_t i)
 	{
 		return llvm::ConstantInt::get(Type::getInt64Ty(*::context), i, true);
 	}
 
-	llvm::ConstantInt *Nucleus::createConstantInt(int i)
+	llvm::Constant *Nucleus::createConstantInt(int i)
 	{
 		return llvm::ConstantInt::get(Type::getInt32Ty(*::context), i, true);
 	}
 
-	llvm::ConstantInt *Nucleus::createConstantInt(unsigned int i)
+	llvm::Constant *Nucleus::createConstantInt(unsigned int i)
 	{
 		return llvm::ConstantInt::get(Type::getInt32Ty(*::context), i, false);
 	}
 
-	llvm::ConstantInt *Nucleus::createConstantBool(bool b)
+	llvm::Constant *Nucleus::createConstantBool(bool b)
 	{
 		return llvm::ConstantInt::get(Type::getInt1Ty(*::context), b);
 	}
 
-	llvm::ConstantInt *Nucleus::createConstantByte(signed char i)
+	llvm::Constant *Nucleus::createConstantByte(signed char i)
 	{
 		return llvm::ConstantInt::get(Type::getInt8Ty(*::context), i, true);
 	}
 
-	llvm::ConstantInt *Nucleus::createConstantByte(unsigned char i)
+	llvm::Constant *Nucleus::createConstantByte(unsigned char i)
 	{
 		return llvm::ConstantInt::get(Type::getInt8Ty(*::context), i, false);
 	}
 
-	llvm::ConstantInt *Nucleus::createConstantShort(short i)
+	llvm::Constant *Nucleus::createConstantShort(short i)
 	{
 		return llvm::ConstantInt::get(Type::getInt16Ty(*::context), i, true);
 	}
 
-	llvm::ConstantInt *Nucleus::createConstantShort(unsigned short i)
+	llvm::Constant *Nucleus::createConstantShort(unsigned short i)
 	{
 		return llvm::ConstantInt::get(Type::getInt16Ty(*::context), i, false);
 	}
@@ -812,6 +816,11 @@
 		return Nucleus::createStore(value, address, false, alignment);
 	}
 
+	llvm::Value *LValue::storeValue(llvm::Constant *constant, unsigned int alignment) const
+	{
+		return Nucleus::createStore(constant, address, false, alignment);
+	}
+
 	llvm::Value *LValue::getAddress(llvm::Value *index) const
 	{
 		return Nucleus::createGEP(address, index);
diff --git a/src/Reactor/Nucleus.hpp b/src/Reactor/Nucleus.hpp
index 6124508..c9ed831 100644
--- a/src/Reactor/Nucleus.hpp
+++ b/src/Reactor/Nucleus.hpp
@@ -30,12 +30,10 @@
 
 namespace llvm
 {
-	class BasicBlock;
+	class Type;
 	class Value;
 	class Constant;
-	class ConstantInt;
-	class Type;
-	class GlobalValue;
+	class BasicBlock;
 }
 
 namespace sw
@@ -112,6 +110,7 @@
 		// Memory instructions
 		static llvm::Value *createLoad(llvm::Value *ptr, bool isVolatile = false, unsigned int align = 0);
 		static llvm::Value *createStore(llvm::Value *value, llvm::Value *ptr, bool isVolatile = false, unsigned int align = 0);
+		static llvm::Value *createStore(llvm::Constant *constant, llvm::Value *ptr, bool isVolatile = false, unsigned int align = 0);
 		static llvm::Value *createGEP(llvm::Value *ptr, llvm::Value *index);
 
 		// Atomic instructions
@@ -179,25 +178,22 @@
 		static llvm::Value *createSwizzle(llvm::Value *val, unsigned char select);
 		static llvm::Value *createMask(llvm::Value *lhs, llvm::Value *rhs, unsigned char select);
 
-		// Global values
-		static const llvm::GlobalValue *getGlobalValueAtAddress(void *Addr);
-		static void addGlobalMapping(const llvm::GlobalValue *GV, void *Addr);
-		static llvm::GlobalValue *createGlobalValue(llvm::Type *Ty, bool isConstant, unsigned int Align);
-		static llvm::Type *getPointerType(llvm::Type *ElementType);
-
 		// Constant values
 		static llvm::Constant *createNullValue(llvm::Type *Ty);
-		static llvm::ConstantInt *createConstantInt(int64_t i);
-		static llvm::ConstantInt *createConstantInt(int i);
-		static llvm::ConstantInt *createConstantInt(unsigned int i);
-		static llvm::ConstantInt *createConstantBool(bool b);
-		static llvm::ConstantInt *createConstantByte(signed char i);
-		static llvm::ConstantInt *createConstantByte(unsigned char i);
-		static llvm::ConstantInt *createConstantShort(short i);
-		static llvm::ConstantInt *createConstantShort(unsigned short i);
+		static llvm::Constant *createConstantInt(int64_t i);
+		static llvm::Constant *createConstantInt(int i);
+		static llvm::Constant *createConstantInt(unsigned int i);
+		static llvm::Constant *createConstantBool(bool b);
+		static llvm::Constant *createConstantByte(signed char i);
+		static llvm::Constant *createConstantByte(unsigned char i);
+		static llvm::Constant *createConstantShort(short i);
+		static llvm::Constant *createConstantShort(unsigned short i);
 		static llvm::Constant *createConstantFloat(float x);
 		static llvm::Value *createNullPointer(llvm::Type *Ty);
 		static llvm::Value *createConstantVector(llvm::Constant *const *Vals, unsigned NumVals);
+		static llvm::Constant *createConstantPointer(const void *external, llvm::Type *Ty, bool isConstant, unsigned int Align);
+
+		static llvm::Type *getPointerType(llvm::Type *ElementType);
 
 	private:
 		void optimize();
@@ -263,6 +259,7 @@
 
 		llvm::Value *loadValue(unsigned int alignment = 0) const;
 		llvm::Value *storeValue(llvm::Value *value, unsigned int alignment = 0) const;
+		llvm::Value *storeValue(llvm::Constant *constant, unsigned int alignment = 0) const;
 		llvm::Value *getAddress(llvm::Value *index) const;
 
 	protected:
@@ -2770,16 +2767,9 @@
 	template<class T>
 	Pointer<T>::Pointer(const void *external) : alignment((intptr_t)external & 0x0000000F ? 1 : 16)
 	{
-		const llvm::GlobalValue *globalPointer = Nucleus::getGlobalValueAtAddress(const_cast<void*>(external));   // FIXME: Const
+		llvm::Constant *globalPointer = Nucleus::createConstantPointer(external, T::getType(), false, alignment);
 
-		if(!globalPointer)
-		{
-			globalPointer = Nucleus::createGlobalValue(T::getType(), false, alignment);
-
-			Nucleus::addGlobalMapping(globalPointer, const_cast<void*>(external));   // FIXME: Const
-		}
-
-		LValue::storeValue((llvm::Value*)globalPointer);   // FIXME: Const
+		LValue::storeValue(globalPointer);
 	}
 
 	template<class T>