Add LLVM{U}IntAsType() utility routine

This commit is contained in:
Matt Pharr
2012-03-05 06:22:40 -08:00
parent ff48dd7bfb
commit e482d29951
2 changed files with 44 additions and 0 deletions

View File

@@ -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<LLVM_TYPE_CONST llvm::VectorType>(type);
if (vecType != NULL) {
llvm::Constant *v = llvm::ConstantInt::get(vecType->getElementType(),
val, true /* signed */);
std::vector<llvm::Constant *> 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<LLVM_TYPE_CONST llvm::VectorType>(type);
if (vecType != NULL) {
llvm::Constant *v = llvm::ConstantInt::get(vecType->getElementType(),
val, false /* unsigned */);
std::vector<llvm::Constant *> 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 /** Conservative test to see if two llvm::Values are equal. There are
(potentially many) cases where the two values actually are equal but (potentially many) cases where the two values actually are equal but
this will return false. However, if it does return true, the two this will return false. However, if it does return true, the two

View File

@@ -173,6 +173,14 @@ extern llvm::Constant *LLVMFloatVector(float f);
across all elements */ across all elements */
extern llvm::Constant *LLVMDoubleVector(double f); 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. /** Returns an LLVM boolean vector based on the given array of values.
The array should have g->target.vectorWidth elements. */ The array should have g->target.vectorWidth elements. */
extern llvm::Constant *LLVMBoolVector(const bool *v); extern llvm::Constant *LLVMBoolVector(const bool *v);