289 const llvm::Type*
const targetType,
290 const std::string& twine =
"")
293#define BIND_ARITHMETIC_CAST_OP(Function, Twine) \
294 std::bind(&Function, \
295 std::placeholders::_1, \
296 std::placeholders::_2, \
297 std::placeholders::_3, \
300 if (targetType->isDoubleTy()) {
309 else if (targetType->isFloatTy()) {
318 else if (targetType->isHalfTy()) {
327 else if (targetType->isIntegerTy(64)) {
336 else if (targetType->isIntegerTy(32)) {
345 else if (targetType->isIntegerTy(16)) {
354 else if (targetType->isIntegerTy(8)) {
363 else if (targetType->isIntegerTy(1)) {
373#undef BIND_ARITHMETIC_CAST_OP
374 assert(
false &&
"invalid LLVM type conversion");
507 llvm::Type* targetElementType,
508 llvm::IRBuilder<>& builder)
510 assert(targetElementType && (targetElementType->isIntegerTy() ||
511 targetElementType->isFloatingPointTy()) &&
512 "Target element type is not a scalar type");
513 assert(ptrToArray && ptrToArray->getType()->isPointerTy() &&
514 "Input to arrayCast is not a pointer type.");
516 llvm::Type* arrayType = ptrToArray->getType()->getContainedType(0);
517 assert(arrayType && llvm::isa<llvm::ArrayType>(arrayType));
520 llvm::Type* sourceElementType = arrayType->getArrayElementType();
521 assert(sourceElementType && (sourceElementType->isIntegerTy() ||
522 sourceElementType->isFloatingPointTy()) &&
523 "Source element type is not a scalar type");
525 if (sourceElementType == targetElementType)
return ptrToArray;
529 const size_t elementSize = arrayType->getArrayNumElements();
530 llvm::Value* targetArray =
532 llvm::ArrayType::get(targetElementType, elementSize));
534 for (
size_t i = 0; i < elementSize; ++i) {
537 source =
ir_load(builder, source);
538 source = llvmCastFunction(builder, source, targetElementType);
539 builder.CreateStore(source, target);