From e482d29951c7af081e46192eaba809e9cfa9583a Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Mon, 5 Mar 2012 06:22:40 -0800 Subject: [PATCH] Add LLVM{U}IntAsType() utility routine --- llvmutil.cpp | 36 ++++++++++++++++++++++++++++++++++++ llvmutil.h | 8 ++++++++ 2 files changed, 44 insertions(+) diff --git a/llvmutil.cpp b/llvmutil.cpp index bbad2886..0f5bfd1b 100644 --- a/llvmutil.cpp +++ b/llvmutil.cpp @@ -469,6 +469,42 @@ LLVMBoolVector(const bool *bvec) { } +llvm::Constant * +LLVMIntAsType(int64_t val, LLVM_TYPE_CONST llvm::Type *type) { + LLVM_TYPE_CONST llvm::VectorType *vecType = + llvm::dyn_cast(type); + + if (vecType != NULL) { + llvm::Constant *v = llvm::ConstantInt::get(vecType->getElementType(), + val, true /* signed */); + std::vector vals; + for (int i = 0; i < (int)vecType->getNumElements(); ++i) + vals.push_back(v); + return llvm::ConstantVector::get(vals); + } + else + return llvm::ConstantInt::get(type, val, true /* signed */); +} + + +llvm::Constant * +LLVMUIntAsType(uint64_t val, LLVM_TYPE_CONST llvm::Type *type) { + LLVM_TYPE_CONST llvm::VectorType *vecType = + llvm::dyn_cast(type); + + if (vecType != NULL) { + llvm::Constant *v = llvm::ConstantInt::get(vecType->getElementType(), + val, false /* unsigned */); + std::vector vals; + for (int i = 0; i < (int)vecType->getNumElements(); ++i) + vals.push_back(v); + return llvm::ConstantVector::get(vals); + } + else + return llvm::ConstantInt::get(type, val, false /* unsigned */); +} + + /** Conservative test to see if two llvm::Values are equal. There are (potentially many) cases where the two values actually are equal but this will return false. However, if it does return true, the two diff --git a/llvmutil.h b/llvmutil.h index a1257084..02857e34 100644 --- a/llvmutil.h +++ b/llvmutil.h @@ -173,6 +173,14 @@ extern llvm::Constant *LLVMFloatVector(float f); across all elements */ extern llvm::Constant *LLVMDoubleVector(double f); +/** Returns a constant integer or vector (according to the given type) of + the given signed integer value. */ +extern llvm::Constant *LLVMIntAsType(int64_t, LLVM_TYPE_CONST llvm::Type *t); + +/** Returns a constant integer or vector (according to the given type) of + the given unsigned integer value. */ +extern llvm::Constant *LLVMUIntAsType(uint64_t, LLVM_TYPE_CONST llvm::Type *t); + /** Returns an LLVM boolean vector based on the given array of values. The array should have g->target.vectorWidth elements. */ extern llvm::Constant *LLVMBoolVector(const bool *v);