diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h
index bd84305..4c27515 100644
--- a/include/clang/AST/Type.h
+++ b/include/clang/AST/Type.h
@@ -16,6 +16,7 @@
 
 #include "clang/AST/NestedNameSpecifier.h"
 #include "clang/AST/TemplateName.h"
+#include "clang/Basic/AddressSpaces.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/ExceptionSpecificationType.h"
 #include "clang/Basic/LLVM.h"
@@ -400,13 +401,28 @@ public:
     Mask |= qs.Mask;
   }
 
+  /// \brief Returns true if address spaces can be used interchangeably.
+  /// OpenCL2.0 defines conversion rules (OpenCLC 2.0 s6.5.5) and notion
+  /// of overlapping address spaces.
+  /// CL1.1 or CL1.2:
+  ///   address spaces overlap iff they are the same.
+  /// CL2.0:
+  ///   __generic overlaps with any named address space except __constant.
+  bool isAddressSpaceOverlapping(Qualifiers other) const {
+    return
+        // Address spaces must match exactly.
+        getAddressSpace() == other.getAddressSpace() ||
+        // Otherwise in OpenCLC v2.0 s6.5.5: every address space except
+        // __constant can be used as __generic.
+        (getAddressSpace() == LangAS::opencl_generic &&
+         other.getAddressSpace() != LangAS::opencl_constant);
+  }
+
   /// \brief Determines if these qualifiers compatibly include another set.
   /// Generally this answers the question of whether an object with the other
   /// qualifiers can be safely used as an object with these qualifiers.
   bool compatiblyIncludes(Qualifiers other) const {
-    return
-      // Address spaces must match exactly.
-      getAddressSpace() == other.getAddressSpace() &&
+    return isAddressSpaceOverlapping(other) &&
       // ObjC GC qualifiers can match, be added, or be removed, but can't be
       // changed.
       (getObjCGCAttr() == other.getObjCGCAttr() ||
@@ -1982,6 +1998,27 @@ public:
 
   QualType getPointeeType() const { return PointeeType; }
 
+  /// \brief Return true if address spaces of pointers overlap.
+  /// OpenCL2.0 defines conversion rules for pointers to different
+  /// address spaces (OpenCLC v2.0 s6.5.5) and notion of overlapping
+  /// address spaces.
+  /// CL1.1 or CL1.2:
+  ///   address spaces overlap iff they are they same.
+  /// CL2.0:
+  ///   __generic overlaps with any named address space except __constant.
+  bool isAddressSpaceOverlapping(const PointerType &other) const {
+    return PointeeType.getAddressSpace() ==
+               other.getPointeeType().getAddressSpace() ||
+           // OpenCLC v2.0 s6.5.5: every address space except __constant
+           // can be cast to __generic.
+           ((PointeeType.getAddressSpace() == LangAS::opencl_generic ||
+             other.getPointeeType().getAddressSpace() ==
+                 LangAS::opencl_generic) &&
+            (PointeeType.getAddressSpace() != LangAS::opencl_constant &&
+             other.getPointeeType().getAddressSpace() !=
+                 LangAS::opencl_constant));
+  }
+
   bool isSugared() const { return false; }
   QualType desugar() const { return QualType(this, 0); }
 
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 33e28c7..8a47d86 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4942,6 +4942,10 @@ def err_typecheck_comparison_of_distinct_pointers : Error<
 def ext_typecheck_comparison_of_distinct_pointers_nonstandard : ExtWarn<
   "comparison of distinct pointer types (%0 and %1) uses non-standard "
   "composite pointer type %2">, InGroup<CompareDistinctPointerType>;
+def err_typecheck_op_on_nonoverlapping_address_space_pointers : Error<
+  "%select{comparison between %diff{ ($ and $)|}0,1"
+  "|arithmetic operation with operands of type %diff{ ($ and $)|}0,1}2"
+  " which are pointers to non-overlapping address spaces">;
 def err_typecheck_assign_const : Error<"read-only variable is not assignable">;
 def err_stmtexpr_file_scope : Error<
   "statement expression not allowed at file scope">;
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 2ce5ac5..4d103c1 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -4802,6 +4802,7 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
   case CK_CPointerToObjCPointerCast:
   case CK_BlockPointerToObjCPointerCast:
   case CK_AnyPointerToBlockPointerCast:
+  case CK_AddressSpaceConversion:
     if (!Visit(SubExpr))
       return false;
     // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp
index 1009422..4248e19 100644
--- a/lib/CodeGen/CGExprScalar.cpp
+++ b/lib/CodeGen/CGExprScalar.cpp
@@ -1350,8 +1350,8 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
     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);
+      llvm_unreachable("wrong cast for pointers in different address spaces"
+                       "(must be an address space cast)!");
     }
     return Builder.CreateBitCast(Src, DstTy);
   }
diff --git a/lib/Sema/SemaCast.cpp b/lib/Sema/SemaCast.cpp
index ae5436c..dac37b3 100644
--- a/lib/Sema/SemaCast.cpp
+++ b/lib/Sema/SemaCast.cpp
@@ -2200,8 +2200,8 @@ void CastOperation::CheckCStyleCast() {
   // address space B is illegal.
   if (Self.getLangOpts().OpenCL && DestType->isPointerType() &&
       SrcType->isPointerType()) {
-    if (DestType->getPointeeType().getAddressSpace() !=
-        SrcType->getPointeeType().getAddressSpace()) {
+    const PointerType *DestPtr = DestType->getAs<PointerType>();
+    if (!DestPtr->isAddressSpaceOverlapping(*SrcType->getAs<PointerType>())) {
       Self.Diag(OpRange.getBegin(),
                 diag::err_typecheck_incompatible_address_space)
           << SrcType << DestType << Sema::AA_Casting
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 37a08cf..e190150 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -6150,7 +6150,7 @@ checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) {
 
   if (!lhq.compatiblyIncludes(rhq)) {
     // Treat address-space mismatches as fatal.  TODO: address subspaces
-    if (lhq.getAddressSpace() != rhq.getAddressSpace())
+    if (!lhq.isAddressSpaceOverlapping(rhq))
       ConvTy = Sema::IncompatiblePointerDiscardsQualifiers;
 
     // It's okay to add or remove GC or lifetime qualifiers when converting to
@@ -6435,7 +6435,9 @@ Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS,
   if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) {
     // U* -> T*
     if (isa<PointerType>(RHSType)) {
-      Kind = CK_BitCast;
+      unsigned AddrSpaceL = LHSPointer->getPointeeType().getAddressSpace();
+      unsigned AddrSpaceR = RHSType->getPointeeType().getAddressSpace();
+      Kind = AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion : CK_BitCast;
       return checkPointerTypesForAssignment(*this, LHSType, RHSType);
     }
 
@@ -7118,6 +7120,19 @@ static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc,
   if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType();
   if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType();
 
+  // if both are pointers check if operation is valid wrt address spaces
+  if (isLHSPointer && isRHSPointer) {
+    const PointerType *lhsPtr = LHSExpr->getType()->getAs<PointerType>();
+    const PointerType *rhsPtr = RHSExpr->getType()->getAs<PointerType>();
+    if (!lhsPtr->isAddressSpaceOverlapping(*rhsPtr)) {
+      S.Diag(Loc,
+             diag::err_typecheck_op_on_nonoverlapping_address_space_pointers)
+          << LHSExpr->getType() << RHSExpr->getType() << 1 /*arithmetic op*/
+          << LHSExpr->getSourceRange() << RHSExpr->getSourceRange();
+      return false;
+    }
+  }
+
   // Check for arithmetic on pointers to incomplete types.
   bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType();
   bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType();
@@ -8046,6 +8061,13 @@ QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS,
       diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false);
     }
     if (LCanPointeeTy != RCanPointeeTy) {
+      const PointerType *lhsPtr = LHSType->getAs<PointerType>();
+      if (!lhsPtr->isAddressSpaceOverlapping(*RHSType->getAs<PointerType>())) {
+        Diag(Loc,
+             diag::err_typecheck_op_on_nonoverlapping_address_space_pointers)
+            << LHSType << RHSType << 0 /* comparison */
+            << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
+      }
       unsigned AddrSpaceL = LCanPointeeTy.getAddressSpace();
       unsigned AddrSpaceR = RCanPointeeTy.getAddressSpace();
       CastKind Kind = AddrSpaceL != AddrSpaceR ? CK_AddressSpaceConversion
diff --git a/test/CodeGenOpenCL/address-spaces-conversions.cl b/test/CodeGenOpenCL/address-spaces-conversions.cl
new file mode 100644
index 0000000..83fb322
--- /dev/null
+++ b/test/CodeGenOpenCL/address-spaces-conversions.cl
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 %s -O0 -ffake-address-space-map -cl-std=CL2.0 -emit-llvm -o - | FileCheck %s
+
+// tests that we generate address space casts everywhere we need conversion of
+// pointers in different address spaces
+
+void test(global int *arg_glob, generic int *arg_gen) {
+  int var_priv;
+  arg_gen = arg_glob; // implicit cast global -> generic
+  // CHECK: %{{[0-9]+}} = addrspacecast i32 addrspace(1)* %{{[0-9]+}} to i32 addrspace(4)*
+  arg_gen = &var_priv; // implicit cast with obtaining adr, private -> generic
+  // CHECK: %{{[0-9]+}} = addrspacecast i32* %var_priv to i32 addrspace(4)*
+  arg_glob = (global int *)arg_gen; // explicit cast
+  // CHECK: %{{[0-9]+}} = addrspacecast i32 addrspace(4)* %{{[0-9]+}} to i32 addrspace(1)*
+  global int *var_glob =
+      (global int *)arg_glob; // explicit cast in the same address space
+  // CHECK-NOT: %{{[0-9]+}} = addrspacecast i32 addrspace(1)* %{{[0-9]+}} to i32 addrspace(1)*
+  var_priv = arg_gen - arg_glob; // arithmetic operation
+  // CHECK: %sub.ptr.lhs.cast = ptrtoint i32 addrspace(4)* %6 to i64
+  // CHECK: %sub.ptr.rhs.cast = ptrtoint i32 addrspace(1)* %7 to i64
+  var_priv = arg_gen > arg_glob; // comparison
+  // CHECK: %{{[0-9]+}} = addrspacecast i32 addrspace(1)* %{{[0-9]+}} to i32 addrspace(4)*
+}
diff --git a/test/SemaOpenCL/address-spaces-conversions-cl2.0.cl b/test/SemaOpenCL/address-spaces-conversions-cl2.0.cl
new file mode 100644
index 0000000..c45456e
--- /dev/null
+++ b/test/SemaOpenCL/address-spaces-conversions-cl2.0.cl
@@ -0,0 +1,230 @@
+// RUN: %clang_cc1 %s -ffake-address-space-map -verify -pedantic -fsyntax-only -DCONSTANT -cl-std=CL2.0
+// RUN: %clang_cc1 %s -ffake-address-space-map -verify -pedantic -fsyntax-only -DGLOBAL -cl-std=CL2.0
+// RUN: %clang_cc1 %s -ffake-address-space-map -verify -pedantic -fsyntax-only -DGENERIC -cl-std=CL2.0
+
+/* OpenCL C 2.0 adds set of restrictions for conversions between pointers of
+   different address spaces
+   mainly described in Sections 6.5.5 and 6.5.6.
+
+   It adds notion of overlapping address spaces. The main differention is that
+   an unnamed address space
+   is added called 'generic'. Pointers in generic adress space can be
+   interchangabley used with pointers
+   of any other address space except constant address space (Section 6.5.5).
+
+   Based on this we added 3 sets of tests: __generic, named (__global in this
+   test), __constant
+   that should cover all program paths for CL adress space conversions used in
+   initialisation,
+   assignments, casts, comparisons and arithmetic operations.
+*/
+
+#ifdef GENERIC
+#define AS generic
+#endif
+
+#ifdef GLOBAL
+#define AS global
+#endif
+
+#ifdef CONSTANT
+#define AS constant
+#endif
+
+void f_glob(global int *arg_glob) {}
+#ifndef GLOBAL
+// expected-note@-2{{passing argument to parameter 'arg_glob' here}}
+#endif
+
+void f_loc(local int *arg_loc) {
+} // expected-note@-1{{passing argument to parameter 'arg_loc' here}}
+
+void f_const(constant int *arg_const) {}
+#ifndef CONSTANT
+// expected-note@-2{{passing argument to parameter 'arg_const' here}}
+#endif
+
+void f_priv(private int *arg_priv) {
+} // expected-note@-1{{passing argument to parameter 'arg_priv' here}}
+
+void f_gen(generic int *arg_gen) {}
+#ifdef CONSTANT
+// expected-note@-2{{passing argument to parameter 'arg_gen' here}}
+#endif
+
+void test_conversion(global int *arg_glob, local int *arg_loc,
+                     constant int *arg_const, private int *arg_priv,
+                     generic int *arg_gen) {
+
+  AS int *var_init1 = arg_glob;
+#ifdef CONSTANT
+// expected-error@-2{{initializing '__constant int *' with an expression of type '__global int *' changes address space of pointer}}
+#endif
+
+  AS int *var_init2 = arg_loc;
+#ifndef GENERIC
+// expected-error-re@-2{{initializing '__{{global|constant}} int *' with an expression of type '__local int *' changes address space of pointer}}
+#endif
+
+  AS int *var_init3 = arg_const;
+#ifndef CONSTANT
+// expected-error-re@-2{{initializing '__{{global|generic}} int *' with an expression of type '__constant int *' changes address space of pointer}}
+#endif
+
+  AS int *var_init4 = arg_priv;
+#ifndef GENERIC
+// expected-error-re@-2{{initializing '__{{global|constant}} int *' with an expression of type 'int *' changes address space of pointer}}
+#endif
+
+  AS int *var_init5 = arg_gen;
+#ifndef GENERIC
+// expected-error-re@-2{{initializing '__{{global|constant}} int *' with an expression of type '__generic int *' changes address space of pointer}}
+#endif
+
+  AS int *var_cast1 = (AS int *)arg_glob;
+#ifdef CONSTANT
+// expected-error@-2{{casting '__global int *' to type '__constant int *' changes address space of pointer}}
+#endif
+
+  AS int *var_cast2 = (AS int *)arg_loc;
+#ifndef GENERIC
+// expected-error-re@-2{{casting '__local int *' to type '__{{global|constant}} int *' changes address space of pointer}}
+#endif
+
+  AS int *var_cast3 = (AS int *)arg_const;
+#ifndef CONSTANT
+// expected-error-re@-2{{casting '__constant int *' to type '__{{global|generic}} int *' changes address space of pointer}}
+#endif
+
+  AS int *var_cast4 = (AS int *)arg_priv;
+#ifndef GENERIC
+// expected-error-re@-2{{casting 'int *' to type '__{{global|constant}} int *' changes address space of pointer}}
+#endif
+
+  AS int *var_cast5 = (AS int *)arg_gen;
+#ifdef CONSTANT
+// expected-error@-2{{casting '__generic int *' to type '__constant int *' changes address space of pointer}}
+#endif
+
+  AS int *var_impl;
+  var_impl = arg_glob;
+#ifdef CONSTANT
+// expected-error@-2{{assigning '__global int *' to '__constant int *' changes address space of pointer}}
+#endif
+
+  var_impl = arg_loc;
+#ifndef GENERIC
+// expected-error-re@-2{{assigning '__local int *' to '__{{global|constant}} int *' changes address space of pointer}}
+#endif
+
+  var_impl = arg_const;
+#ifndef CONSTANT
+// expected-error-re@-2{{assigning '__constant int *' to '__{{global|generic}} int *' changes address space of pointer}}
+#endif
+
+  var_impl = arg_priv;
+#ifndef GENERIC
+// expected-error-re@-2{{assigning 'int *' to '__{{global|constant}} int *' changes address space of pointer}}
+#endif
+
+  var_impl = arg_gen;
+#ifndef GENERIC
+// expected-error-re@-2{{assigning '__generic int *' to '__{{global|constant}} int *' changes address space of pointer}}
+#endif
+
+  var_cast1 = (AS int *)arg_glob;
+#ifdef CONSTANT
+// expected-error@-2{{casting '__global int *' to type '__constant int *' changes address space of pointer}}
+#endif
+
+  var_cast2 = (AS int *)arg_loc;
+#ifndef GENERIC
+// expected-error-re@-2{{casting '__local int *' to type '__{{global|constant}} int *' changes address space of pointer}}
+#endif
+
+  var_cast3 = (AS int *)arg_const;
+#ifndef CONSTANT
+// expected-error-re@-2{{casting '__constant int *' to type '__{{global|generic}} int *' changes address space of pointer}}
+#endif
+
+  var_cast4 = (AS int *)arg_priv;
+#ifndef GENERIC
+// expected-error-re@-2{{casting 'int *' to type '__{{global|constant}} int *' changes address space of pointer}}
+#endif
+
+  var_cast5 = (AS int *)arg_gen;
+#ifdef CONSTANT
+// expected-error@-2{{casting '__generic int *' to type '__constant int *' changes address space of pointer}}
+#endif
+
+  AS int *var_cmp;
+  int b = var_cmp != arg_glob;
+#ifdef CONSTANT
+// expected-error@-2{{comparison between  ('__constant int *' and '__global int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  b = var_cmp != arg_loc;
+#ifndef GENERIC
+// expected-error-re@-2{{comparison between  ('__{{global|constant}} int *' and '__local int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  b = var_cmp == arg_const;
+#ifndef CONSTANT
+// expected-error-re@-2{{comparison between  ('__{{global|generic}} int *' and '__constant int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  b = var_cmp <= arg_priv;
+#ifndef GENERIC
+// expected-error-re@-2{{comparison between  ('__{{global|constant}} int *' and 'int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  b = var_cmp >= arg_gen;
+#ifdef CONSTANT
+// expected-error@-2{{comparison between  ('__constant int *' and '__generic int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  AS int *var_sub;
+  b = var_sub - arg_glob;
+#ifdef CONSTANT
+// expected-error@-2{{arithmetic operation with operands of type  ('__constant int *' and '__global int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  b = var_sub - arg_loc;
+#ifndef GENERIC
+// expected-error-re@-2{{arithmetic operation with operands of type  ('__{{global|constant}} int *' and '__local int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  b = var_sub - arg_const;
+#ifndef CONSTANT
+// expected-error-re@-2{{arithmetic operation with operands of type  ('__{{global|generic}} int *' and '__constant int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  b = var_sub - arg_priv;
+#ifndef GENERIC
+// expected-error-re@-2{{arithmetic operation with operands of type  ('__{{global|constant}} int *' and 'int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  b = var_sub - arg_gen;
+#ifdef CONSTANT
+// expected-error@-2{{arithmetic operation with operands of type  ('__constant int *' and '__generic int *') which are pointers to non-overlapping address spaces}}
+#endif
+
+  f_glob(var_sub);
+#ifndef GLOBAL
+// expected-error-re@-2{{passing '__{{constant|generic}} int *' to parameter of type '__global int *' changes address space of pointer}}
+#endif
+
+  f_loc(var_sub); // expected-error-re{{passing '__{{global|constant|generic}} int *' to parameter of type '__local int *' changes address space of pointer}}
+
+  f_const(var_sub);
+#ifndef CONSTANT
+// expected-error-re@-2{{passing '__{{global|generic}} int *' to parameter of type '__constant int *' changes address space of pointer}}
+#endif
+
+  f_priv(var_sub); // expected-error-re{{passing '__{{global|constant|generic}} int *' to parameter of type 'int *' changes address space of pointer}}
+
+  f_gen(var_sub);
+#ifdef CONSTANT
+// expected-error@-2{{passing '__constant int *' to parameter of type '__generic int *' changes address space of pointer}}
+#endif
+}
