From 25665f0841fcee0f0a9a26338ef8555cd73f0570 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Tue, 31 Jan 2012 09:37:39 -0800 Subject: [PATCH] Implement NullPointerExpr::GetConstant() Also reworked TypeCastExpr::GetConstant() to just forward the request along and moved the code that was previously there to handle uniform->varying smears of function pointers to FunctionSymbolExpr::GetConstant(). Fixes issue #168. --- expr.cpp | 72 ++++++++++++++++++++++++++++++++++++-------------------- expr.h | 1 + 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/expr.cpp b/expr.cpp index 24df7fb3..65907d94 100644 --- a/expr.cpp +++ b/expr.cpp @@ -6037,28 +6037,15 @@ llvm::Constant * TypeCastExpr::GetConstant(const Type *constType) const { // We don't need to worry about most the basic cases where the type // cast can resolve to a constant here, since the - // TypeCastExpr::Optimize() method ends up doing the type conversion - // and returning a ConstExpr, which in turn will have its GetConstant() - // method called. Thus, the only case we do need to worry about here - // is converting a uniform function pointer to a varying function - // pointer of the same type. + // TypeCastExpr::Optimize() method generally ends up doing the type + // conversion and returning a ConstExpr, which in turn will have its + // GetConstant() method called. However, because ConstExpr currently + // can't represent pointer values, we have to handle two cases here: + // 1. Null pointers (NULL, 0) valued initializers, and + // 2. Converting a uniform function pointer to a varying function + // pointer of the same type. Assert(Type::Equal(constType, type)); - const FunctionType *ft = NULL; - if (dynamic_cast(type) == NULL || - (ft = dynamic_cast(type->GetBaseType())) == NULL) - return NULL; - - llvm::Constant *ec = expr->GetConstant(expr->GetType()); - if (ec == NULL) - return NULL; - - ec = llvm::ConstantExpr::getPtrToInt(ec, LLVMTypes::PointerIntType); - - Assert(type->IsVaryingType()); - std::vector smear; - for (int i = 0; i < g->target.vectorWidth; ++i) - smear.push_back(ec); - return llvm::ConstantVector::get(smear); + return expr->GetConstant(constType); } @@ -6553,13 +6540,30 @@ FunctionSymbolExpr::Print() const { llvm::Constant * FunctionSymbolExpr::GetConstant(const Type *type) const { - Assert(type->IsUniformType()); - Assert(GetType()->IsUniformType()); - - if (Type::EqualIgnoringConst(type, GetType()) == false) + if (matchingFunc == NULL || matchingFunc->function == NULL) return NULL; - return matchingFunc ? matchingFunc->function : NULL; + const FunctionType *ft; + if (dynamic_cast(type) == NULL || + (ft = dynamic_cast(type->GetBaseType())) == NULL) + return NULL; + + LLVM_TYPE_CONST llvm::Type *llvmUnifType = + type->GetAsUniformType()->LLVMType(g->ctx); + if (llvmUnifType != matchingFunc->function->getType()) + return NULL; + + if (type->IsUniformType()) + return matchingFunc->function; + else { + llvm::Constant *intPtr = + llvm::ConstantExpr::getPtrToInt(matchingFunc->function, + LLVMTypes::PointerIntType); + std::vector smear; + for (int i = 0; i < g->target.vectorWidth; ++i) + smear.push_back(intPtr); + return llvm::ConstantVector::get(smear); + } } @@ -6985,6 +6989,22 @@ NullPointerExpr::Optimize() { } +llvm::Constant * +NullPointerExpr::GetConstant(const Type *type) const { + const PointerType *pt = dynamic_cast(type); + if (pt == NULL) + return NULL; + + LLVM_TYPE_CONST llvm::Type *llvmType = type->LLVMType(g->ctx); + if (llvmType == NULL) { + Assert(m->errorCount > 0); + return NULL; + } + + return llvm::Constant::getNullValue(llvmType); +} + + void NullPointerExpr::Print() const { printf("NULL"); diff --git a/expr.h b/expr.h index fad1d0bc..70224a7f 100644 --- a/expr.h +++ b/expr.h @@ -684,6 +684,7 @@ public: const Type *GetType() const; Expr *TypeCheck(); Expr *Optimize(); + llvm::Constant *GetConstant(const Type *type) const; void Print() const; int EstimateCost() const; };