From 0e33ae3e1ed5a90a11039502bdde2fa680a90b59 Mon Sep 17 00:00:00 2001 From: Nicolas Capens Date: Fri, 23 Sep 2016 17:37:27 -0400 Subject: [PATCH] Eliminate llvm::ConstantInt and llvm::GlobalValue exposure. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Alexis Hétu Reviewed-by: Nicolas Capens --- src/Reactor/Nucleus.cpp | 47 ++++++++++++++++++++++++++++------------------- src/Reactor/Nucleus.hpp | 44 +++++++++++++++++--------------------------- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/Reactor/Nucleus.cpp b/src/Reactor/Nucleus.cpp index 993ba2840..7f7bf000b 100644 --- a/src/Reactor/Nucleus.cpp +++ b/src/Reactor/Nucleus.cpp @@ -426,6 +426,11 @@ namespace sw 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 @@ namespace sw void Nucleus::addSwitchCase(llvm::Value *Switch, int Case, llvm::BasicBlock *Branch) { - static_cast(Switch)->addCase(Nucleus::createConstantInt(Case), Branch); + static_cast(Switch)->addCase(llvm::ConstantInt::get(Type::getInt32Ty(*::context), Case, true), Branch); } Value *Nucleus::createUnreachable() @@ -709,21 +714,20 @@ namespace sw 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(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(address)); + return global; } @@ -737,42 +741,42 @@ namespace sw 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 @@ namespace sw 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 61245085d..c9ed8318a 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 @@ namespace sw // 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 @@ namespace sw 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 @@ namespace sw 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 @@ namespace sw template Pointer::Pointer(const void *external) : alignment((intptr_t)external & 0x0000000F ? 1 : 16) { - const llvm::GlobalValue *globalPointer = Nucleus::getGlobalValueAtAddress(const_cast(external)); // FIXME: Const - - if(!globalPointer) - { - globalPointer = Nucleus::createGlobalValue(T::getType(), false, alignment); - - Nucleus::addGlobalMapping(globalPointer, const_cast(external)); // FIXME: Const - } + llvm::Constant *globalPointer = Nucleus::createConstantPointer(external, T::getType(), false, alignment); - LValue::storeValue((llvm::Value*)globalPointer); // FIXME: Const + LValue::storeValue(globalPointer); } template -- 2.11.0