Index: clang/lib/CodeGen/CGExprScalar.cpp =================================================================== --- clang/lib/CodeGen/CGExprScalar.cpp +++ clang/lib/CodeGen/CGExprScalar.cpp @@ -3258,7 +3258,7 @@ // GNU void* casts amount to no-ops since our void* type is i8*, but this is // future proof. if (elementType->isVoidType() || elementType->isFunctionType()) { - Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy); + Value *result = CGF.EmitCastToVoidPtr(pointer); result = CGF.Builder.CreateGEP(result, index, "add.ptr"); return CGF.Builder.CreateBitCast(result, pointer->getType()); } Index: clang/test/CodeGen/address-space.c =================================================================== --- clang/test/CodeGen/address-space.c +++ clang/test/CodeGen/address-space.c @@ -1,5 +1,5 @@ -// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm < %s | FileCheck -check-prefixes=CHECK,X86 %s -// RUN: %clang_cc1 -triple amdgcn -emit-llvm < %s | FileCheck -check-prefixes=CHECK,AMDGCN %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm < %s | FileCheck -enable-var-scope -check-prefixes=CHECK,X86 %s +// RUN: %clang_cc1 -triple amdgcn -emit-llvm < %s | FileCheck -enable-var-scope -check-prefixes=CHECK,AMDGCN %s // CHECK: @foo = common addrspace(1) global int foo __attribute__((address_space(1))); @@ -10,11 +10,11 @@ // CHECK: @a = common global int a __attribute__((address_space(0))); -// CHECK-LABEL: define i32 @test1() +// CHECK-LABEL: define i32 @test1() // CHECK: load i32, i32 addrspace(1)* @foo int test1() { return foo; } -// CHECK-LABEL: define i32 @test2(i32 %i) +// CHECK-LABEL: define i32 @test2(i32 %i) // CHECK: load i32, i32 addrspace(1)* // CHECK-NEXT: ret i32 int test2(int i) { return ban[i]; } @@ -45,3 +45,17 @@ MyStruct s = pPtr[0]; pPtr[0] = s; } + +// Make sure the right address space is used when doing arithmetic on a void +// pointer. Make sure no invalid bitcast is introduced. + +// CHECK-LABEL: @void_ptr_arithmetic_test( +// X86: [[ALLOCA:%.*]] = alloca i8 addrspace(1)* +// X86-NEXT: store i8 addrspace(1)* %arg, i8 addrspace(1)** [[ALLOCA]] +// X86-NEXT: load i8 addrspace(1)*, i8 addrspace(1)** [[ALLOCA]] +// X86-NEXT: getelementptr i8, i8 addrspace(1)* +// X86-NEXT: ret i8 addrspace(1)* +void __attribute__((address_space(1)))* +void_ptr_arithmetic_test(void __attribute__((address_space(1))) *arg) { + return arg + 4; +}