From 142f01674857b0fac52ddf1e4d315d74210a4584 Mon Sep 17 00:00:00 2001 From: David Tweed Date: Wed, 11 Dec 2013 13:39:46 +0000 Subject: Add front-end infrastructure now address space casts are in LLVM IR. With the introduction of explicit address space casts into LLVM, there's a need to provide a new cast kind the front-end can create for C/OpenCL/CUDA and code to produce address space casts from those kinds when appropriate. Patch by Michele Scandale! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@197036 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExprScalar.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'lib/CodeGen/CGExprScalar.cpp') diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index 789c986bbe..d734e3c927 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -1299,7 +1299,18 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { case CK_AnyPointerToBlockPointerCast: case CK_BitCast: { Value *Src = Visit(const_cast(E)); - return Builder.CreateBitCast(Src, ConvertType(DestTy)); + llvm::Type *SrcTy = Src->getType(); + llvm::Type *DstTy = ConvertType(DestTy); + if (SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() && + SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) { + llvm::Type *MidTy = CGF.CGM.getDataLayout().getIntPtrType(SrcTy); + return Builder.CreateIntToPtr(Builder.CreatePtrToInt(Src, MidTy), DstTy); + } + return Builder.CreateBitCast(Src, DstTy); + } + case CK_AddressSpaceConversion: { + Value *Src = Visit(const_cast(E)); + return Builder.CreateAddrSpaceCast(Src, ConvertType(DestTy)); } case CK_AtomicToNonAtomic: case CK_NonAtomicToAtomic: @@ -1360,7 +1371,7 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { // Make sure the array decay ends up being the right type. This matters if // the array type was of an incomplete type. - return CGF.Builder.CreateBitCast(V, ConvertType(CE->getType())); + return CGF.Builder.CreatePointerCast(V, ConvertType(CE->getType())); } case CK_FunctionToPointerDecay: return EmitLValue(E).getAddress(); -- cgit v1.2.3