Index: test/Sema/atomic-ops.c
===================================================================
--- test/Sema/atomic-ops.c	(revision 163918)
+++ test/Sema/atomic-ops.c	(working copy)
@@ -1,11 +1,8 @@
-// RUN: %clang_cc1 %s -verify -fsyntax-only -triple=i686-linux-gnu
+// RUN: %clang_cc1 %s -verify -fsyntax-only -triple=i686-linux-gnu -std=c11
 
 // Basic parsing/Sema tests for __c11_atomic_*
 
-typedef enum memory_order {
-  memory_order_relaxed, memory_order_consume, memory_order_acquire,
-  memory_order_release, memory_order_acq_rel, memory_order_seq_cst
-} memory_order;
+#include <stdatomic.h>
 
 struct S { char c[3]; };
 
@@ -36,6 +33,12 @@
 _Static_assert(__atomic_is_lock_free(16, 0), ""); // expected-error {{not an integral constant expression}}
 _Static_assert(__atomic_is_lock_free(17, 0), ""); // expected-error {{not an integral constant expression}}
 
+_Static_assert(atomic_is_lock_free((atomic_char*)0), "");
+_Static_assert(atomic_is_lock_free((atomic_short*)0), "");
+_Static_assert(atomic_is_lock_free((atomic_int*)0), "");
+_Static_assert(atomic_is_lock_free((atomic_long*)0), "");
+_Static_assert(atomic_is_lock_free((_Atomic(__int128)*)0), ""); // expected-error {{not an integral constant expression}}
+
 char i8;
 short i16;
 int i32;
@@ -162,4 +165,55 @@
   __atomic_clear(&flag_k, memory_order_seq_cst); // expected-warning {{passing 'const volatile int *' to parameter of type 'volatile void *'}}
   __atomic_clear(&flag, memory_order_seq_cst);
   (int)__atomic_clear(&flag, memory_order_seq_cst); // expected-error {{operand of type 'void'}}
+
+  // Ensure the <stdatomic.h> macros behave appropriately.
+  atomic_int n = ATOMIC_VAR_INIT(123);
+  atomic_init(&n, 456);
+  atomic_init(&n, (void*)0); // expected-warning {{passing 'void *' to parameter of type 'int'}}
+
+  const atomic_wchar_t cawt;
+  atomic_init(&cawt, L'x'); // expected-error {{non-const}}
+  atomic_wchar_t awt;
+  atomic_init(&awt, L'x');
+
+  int x = kill_dependency(12);
+
+  atomic_thread_fence(); // expected-error {{too few arguments to function call}}
+  atomic_thread_fence(memory_order_seq_cst);
+  atomic_signal_fence(memory_order_seq_cst);
+
+  int k = atomic_load_explicit(&n, memory_order_relaxed);
+  atomic_store_explicit(&n, k, memory_order_relaxed);
+  atomic_store(&n, atomic_load(&n));
+
+  k = atomic_exchange(&n, 72);
+  k = atomic_exchange_explicit(&n, k, memory_order_release);
+
+  atomic_compare_exchange_strong(&n, k, k); // expected-warning {{take the address with &}}
+  atomic_compare_exchange_weak(&n, &k, k);
+  atomic_compare_exchange_strong_explicit(&n, &k, k, memory_order_seq_cst); // expected-error {{too few arguments}}
+  atomic_compare_exchange_weak_explicit(&n, &k, k, memory_order_seq_cst, memory_order_acquire);
+
+  atomic_fetch_add(&k, n); // expected-error {{must be a pointer to _Atomic}}
+  k = atomic_fetch_add(&n, k);
+  k = atomic_fetch_sub(&n, k);
+  k = atomic_fetch_and(&n, k);
+  k = atomic_fetch_or(&n, k);
+  k = atomic_fetch_xor(&n, k);
+  k = atomic_fetch_add_explicit(&n, k, memory_order_acquire);
+  k = atomic_fetch_sub_explicit(&n, k, memory_order_release);
+  k = atomic_fetch_and_explicit(&n, k, memory_order_acq_rel);
+  k = atomic_fetch_or_explicit(&n, k, memory_order_consume);
+  k = atomic_fetch_xor_explicit(&n, k, memory_order_relaxed);
+
+  // C11 7.17.1/4: atomic_flag is a structure type.
+  struct atomic_flag must_be_struct = ATOMIC_FLAG_INIT;
+  // C11 7.17.8/5 implies that it is also a typedef type.
+  atomic_flag guard = ATOMIC_FLAG_INIT;
+  _Bool old_val = atomic_flag_test_and_set(&guard);
+  if (old_val) atomic_flag_clear(&guard);
+
+  const atomic_flag const_guard;
+  atomic_flag_test_and_set(&const_guard); // expected-error {{first argument to atomic operation must be a pointer to non-const _Atomic type ('const atomic_bool *' (aka 'const _Atomic(_Bool) *') invalid)}}
+  atomic_flag_clear(&const_guard); // expected-error {{first argument to atomic operation must be a pointer to non-const _Atomic type ('const atomic_bool *' (aka 'const _Atomic(_Bool) *') invalid)}}
 }
Index: test/CodeGen/atomic-ops.c
===================================================================
--- test/CodeGen/atomic-ops.c	(revision 163918)
+++ test/CodeGen/atomic-ops.c	(working copy)
@@ -7,13 +7,10 @@
 #ifndef ALREADY_INCLUDED
 #define ALREADY_INCLUDED
 
+#include <stdatomic.h>
+
 // Basic IRGen tests for __c11_atomic_* and GNU __atomic_*
 
-typedef enum memory_order {
-  memory_order_relaxed, memory_order_consume, memory_order_acquire,
-  memory_order_release, memory_order_acq_rel, memory_order_seq_cst
-} memory_order;
-
 int fi1(_Atomic(int) *i) {
   // CHECK: @fi1
   // CHECK: load atomic i32* {{.*}} seq_cst
@@ -34,6 +31,12 @@
   return __atomic_load_n(i, memory_order_seq_cst);
 }
 
+int fi1c(atomic_int *i) {
+  // CHECK: @fi1c
+  // CHECK: load atomic i32* {{.*}} seq_cst
+  return atomic_load(i);
+}
+
 void fi2(_Atomic(int) *i) {
   // CHECK: @fi2
   // CHECK: store atomic i32 {{.*}} seq_cst
@@ -53,6 +56,12 @@
   __atomic_store_n(i, 1, memory_order_seq_cst);
 }
 
+void fi2c(atomic_int *i) {
+  // CHECK: @fi2c
+  // CHECK: store atomic i32 {{.*}} seq_cst
+  atomic_store(i, 1);
+}
+
 int fi3(_Atomic(int) *i) {
   // CHECK: @fi3
   // CHECK: atomicrmw and
@@ -89,15 +98,22 @@
   return __atomic_nand_fetch(i, 1, memory_order_seq_cst);
 }
 
+int fi3e(atomic_int *i) {
+  // CHECK: @fi3e
+  // CHECK: atomicrmw or
+  // CHECK-NOT: {{ or }}
+  return atomic_fetch_or(i, 1);
+}
+
 _Bool fi4(_Atomic(int) *i) {
-  // CHECK: @fi4
+  // CHECK: @fi4(
   // CHECK: cmpxchg i32*
   int cmp = 0;
   return __c11_atomic_compare_exchange_strong(i, &cmp, 1, memory_order_acquire, memory_order_acquire);
 }
 
 _Bool fi4a(int *i) {
-  // CHECK: @fi4
+  // CHECK: @fi4a
   // CHECK: cmpxchg i32*
   int cmp = 0;
   int desired = 1;
@@ -105,12 +121,19 @@
 }
 
 _Bool fi4b(int *i) {
-  // CHECK: @fi4
+  // CHECK: @fi4b
   // CHECK: cmpxchg i32*
   int cmp = 0;
   return __atomic_compare_exchange_n(i, &cmp, 1, 1, memory_order_acquire, memory_order_acquire);
 }
 
+_Bool fi4c(atomic_int *i) {
+  // CHECK: @fi4c
+  // CHECK: cmpxchg i32*
+  int cmp = 0;
+  return atomic_compare_exchange_strong(i, &cmp, 1);
+}
+
 float ff1(_Atomic(float) *d) {
   // CHECK: @ff1
   // CHECK: load atomic i32* {{.*}} monotonic
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td	(revision 163918)
+++ include/clang/Basic/DiagnosticSemaKinds.td	(working copy)
@@ -4946,6 +4946,9 @@
 def err_atomic_op_needs_atomic : Error<
   "first argument to atomic operation must be a pointer to _Atomic "
   "type (%0 invalid)">;
+def err_atomic_op_needs_non_const_atomic : Error<
+  "first argument to atomic operation must be a pointer to non-const _Atomic "
+  "type (%0 invalid)">;
 def err_atomic_op_needs_trivial_copy : Error<
   "first argument to atomic operation must be a pointer to a trivially-copyable"
   " type (%0 invalid)">;
Index: docs/LanguageExtensions.html
===================================================================
--- docs/LanguageExtensions.html	(revision 163918)
+++ docs/LanguageExtensions.html	(working copy)
@@ -949,9 +949,8 @@
 
 <p>Use <tt>__has_feature(c_atomic)</tt> or <tt>__has_extension(c_atomic)</tt>
 to determine if support for atomic types using <tt>_Atomic</tt> is enabled.
-Clang also provides <a href="#__c11_atomic">a set of builtins</a> which can be
-used to implement the <tt>&lt;stdatomic.h&gt;</tt> operations on
-<tt>_Atomic</tt> types.</p>
+Use <tt>__has_include(&lt;stdatomic.h&gt;)</tt> to determine if C11's
+<tt>&lt;stdatomic.h&gt;</tt> header is available.</p>
 
 <h4 id="c_generic_selections">C11 generic selections</h4>
 
@@ -1537,14 +1536,16 @@
 <p>Clang provides a set of builtins which are intended to be used to implement
 C11's <tt>&lt;stdatomic.h&gt;</tt> header. These builtins provide the semantics
 of the <tt>_explicit</tt> form of the corresponding C11 operation, and are named
-with a <tt>__c11_</tt> prefix. The supported operations are:</p>
+with a <tt>__c11_</tt> prefix. The supported operations, and the differences from
+the corresponding C11 operations, are:</p>
 
 <ul>
   <li><tt>__c11_atomic_init</tt></li>
   <li><tt>__c11_atomic_thread_fence</tt></li>
   <li><tt>__c11_atomic_signal_fence</tt></li>
-  <li><tt>__c11_atomic_is_lock_free</tt></li>
-  <li><tt>__c11_atomic_store</tt></li>
+  <li><tt>__c11_atomic_is_lock_free</tt> Takes the size of the
+  <tt>_Atomic(...)</tt> object, instead of its address.</li>
+  <li><tt>__c11_atomic_store</tt> Allows <tt>memory_order_consume</tt>.</li>
   <li><tt>__c11_atomic_load</tt></li>
   <li><tt>__c11_atomic_exchange</tt></li>
   <li><tt>__c11_atomic_compare_exchange_strong</tt></li>
@@ -1556,6 +1557,11 @@
   <li><tt>__c11_atomic_fetch_xor</tt></li>
 </ul>
 
+<p>The macros <tt>__ATOMIC_RELAXED</tt>, <tt>__ATOMIC_CONSUME</tt>,
+<tt>__ATOMIC_ACQUIRE</tt>, <tt>__ATOMIC_RELEASE</tt>, <tt>__ATOMIC_ACQ_REL</tt>,
+and <tt>_ATOMIC_SEQ_CST</tt> are provided, with values corresponding to the
+enumerators of C11's <tt>memory_order</tt> enumeration.
+
 <!-- ======================================================================= -->
 <h2 id="non-standard-attributes">Non-standard C++11 Attributes</h2>
 <!-- ======================================================================= -->
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp	(revision 163918)
+++ lib/Sema/SemaChecking.cpp	(working copy)
@@ -742,6 +742,11 @@
         << Ptr->getType() << Ptr->getSourceRange();
       return ExprError();
     }
+    if (AtomTy.isConstQualified()) {
+      Diag(DRE->getLocStart(), diag::err_atomic_op_needs_non_const_atomic)
+        << Ptr->getType() << Ptr->getSourceRange();
+      return ExprError();
+    }
     ValType = AtomTy->getAs<AtomicType>()->getValueType();
   }
 
@@ -6109,4 +6114,3 @@
         << ArgumentExpr->getSourceRange()
         << TypeTagExpr->getSourceRange();
 }
-
Index: lib/Headers/CMakeLists.txt
===================================================================
--- lib/Headers/CMakeLists.txt	(revision 163918)
+++ lib/Headers/CMakeLists.txt	(working copy)
@@ -22,6 +22,7 @@
   smmintrin.h
   stdalign.h
   stdarg.h
+  stdatomic.h
   stdbool.h
   stddef.h
   stdint.h
Index: lib/Headers/stdatomic.h
===================================================================
--- lib/Headers/stdatomic.h	(revision 0)
+++ lib/Headers/stdatomic.h	(revision 0)
@@ -0,0 +1,160 @@
+/*===---- stdatomic.h - Standard header for atomic types and operations -----===
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *===-----------------------------------------------------------------------===
+ */
+
+#ifndef __STDATOMIC_H
+#define __STDATOMIC_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+/* 7.17.1 Introduction */
+
+#define ATOMIC_BOOL_LOCK_FREE       __GCC_ATOMIC_BOOL_LOCK_FREE
+#define ATOMIC_CHAR_LOCK_FREE       __GCC_ATOMIC_CHAR_LOCK_FREE
+#define ATOMIC_CHAR16_T_LOCK_FREE   __GCC_ATOMIC_CHAR16_T_LOCK_FREE
+#define ATOMIC_CHAR32_T_LOCK_FREE   __GCC_ATOMIC_CHAR32_T_LOCK_FREE
+#define ATOMIC_WCHAR_T_LOCK_FREE    __GCC_ATOMIC_WCHAR_T_LOCK_FREE
+#define ATOMIC_SHORT_T_LOCK_FREE    __GCC_ATOMIC_SHORT_T_LOCK_FREE
+#define ATOMIC_INT_T_LOCK_FREE      __GCC_ATOMIC_INT_T_LOCK_FREE
+#define ATOMIC_LONG_T_LOCK_FREE     __GCC_ATOMIC_LONG_T_LOCK_FREE
+#define ATOMIC_LLONG_T_LOCK_FREE    __GCC_ATOMIC_LLONG_T_LOCK_FREE
+#define ATOMIC_POINTER_T_LOCK_FREE  __GCC_ATOMIC_POINTER_T_LOCK_FREE
+
+/* 7.17.2 Initialization */
+
+#define ATOMIC_VAR_INIT(value) (value)
+#define atomic_init __c11_atomic_init
+
+/* 7.17.3 Order and consistency */
+
+typedef enum memory_order {
+  memory_order_relaxed = __ATOMIC_RELAXED,
+  memory_order_consume = __ATOMIC_CONSUME,
+  memory_order_acquire = __ATOMIC_ACQUIRE,
+  memory_order_release = __ATOMIC_RELEASE,
+  memory_order_acq_rel = __ATOMIC_ACQ_REL,
+  memory_order_seq_cst = __ATOMIC_SEQ_CST
+} memory_order;
+
+#define kill_dependency(y) (y)
+
+/* 7.17.4 Fences */
+
+#define atomic_thread_fence __c11_atomic_thread_fence
+#define atomic_signal_fence __c11_atomic_signal_fence
+
+/* 7.17.5 Lock-free property */
+
+#define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*obj))
+
+/* 7.17.6 Atomic integer types */
+
+#ifdef __cplusplus
+typedef _Atomic(bool)               atomic_bool;
+#else
+typedef _Atomic(_Bool)              atomic_bool;
+#endif
+typedef _Atomic(char)               atomic_char;
+typedef _Atomic(signed char)        atomic_schar;
+typedef _Atomic(unsigned char)      atomic_uchar;
+typedef _Atomic(short)              atomic_short;
+typedef _Atomic(unsigned short)     atomic_ushort;
+typedef _Atomic(int)                atomic_int;
+typedef _Atomic(unsigned int)       atomic_uint;
+typedef _Atomic(long)               atomic_long;
+typedef _Atomic(unsigned long)      atomic_ulong;
+typedef _Atomic(long long)          atomic_llong;
+typedef _Atomic(unsigned long long) atomic_ullong;
+typedef _Atomic(uint_least16_t)     atomic_char16_t;
+typedef _Atomic(uint_least32_t)     atomic_char32_t;
+typedef _Atomic(wchar_t)            atomic_wchar_t;
+typedef _Atomic(int_least8_t)       atomic_int_least8_t;
+typedef _Atomic(uint_least8_t)      atomic_uint_least8_t;
+typedef _Atomic(int_least16_t)      atomic_int_least16_t;
+typedef _Atomic(uint_least16_t)     atomic_uint_least16_t;
+typedef _Atomic(int_least32_t)      atomic_int_least32_t;
+typedef _Atomic(uint_least32_t)     atomic_uint_least32_t;
+typedef _Atomic(int_least64_t)      atomic_int_least64_t;
+typedef _Atomic(uint_least64_t)     atomic_uint_least64_t;
+typedef _Atomic(int_fast8_t)        atomic_int_fast8_t;
+typedef _Atomic(uint_fast8_t)       atomic_uint_fast8_t;
+typedef _Atomic(int_fast16_t)       atomic_int_fast16_t;
+typedef _Atomic(uint_fast16_t)      atomic_uint_fast16_t;
+typedef _Atomic(int_fast32_t)       atomic_int_fast32_t;
+typedef _Atomic(uint_fast32_t)      atomic_uint_fast32_t;
+typedef _Atomic(int_fast64_t)       atomic_int_fast64_t;
+typedef _Atomic(uint_fast64_t)      atomic_uint_fast64_t;
+typedef _Atomic(intptr_t)           atomic_intptr_t;
+typedef _Atomic(uintptr_t)          atomic_uintptr_t;
+typedef _Atomic(size_t)             atomic_size_t;
+typedef _Atomic(ptrdiff_t)          atomic_ptrdiff_t;
+typedef _Atomic(intmax_t)           atomic_intmax_t;
+typedef _Atomic(uintmax_t)          atomic_uintmax_t;
+
+/* 7.17.7 Operations on atomic types */
+
+#define atomic_store(object, desired) __c11_atomic_store(object, desired, __ATOMIC_SEQ_CST)
+#define atomic_store_explicit __c11_atomic_store
+
+#define atomic_load(object) __c11_atomic_load(object, __ATOMIC_SEQ_CST)
+#define atomic_load_explicit __c11_atomic_load
+
+#define atomic_exchange(object, desired) __c11_atomic_exchange(object, desired, __ATOMIC_SEQ_CST)
+#define atomic_exchange_explicit __c11_atomic_exchange
+
+#define atomic_compare_exchange_strong(object, expected, desired) __c11_atomic_compare_exchange_strong(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
+#define atomic_compare_exchange_strong_explicit __c11_atomic_compare_exchange_strong
+
+#define atomic_compare_exchange_weak(object, expected, desired) __c11_atomic_compare_exchange_weak(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
+#define atomic_compare_exchange_weak_explicit __c11_atomic_compare_exchange_weak
+
+#define atomic_fetch_add(object, operand) __c11_atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST)
+#define atomic_fetch_add_explicit __c11_atomic_fetch_add
+
+#define atomic_fetch_sub(object, operand) __c11_atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST)
+#define atomic_fetch_sub_explicit __c11_atomic_fetch_sub
+
+#define atomic_fetch_or(object, operand) __c11_atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST)
+#define atomic_fetch_or_explicit __c11_atomic_fetch_or
+
+#define atomic_fetch_xor(object, operand) __c11_atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST)
+#define atomic_fetch_xor_explicit __c11_atomic_fetch_xor
+
+#define atomic_fetch_and(object, operand) __c11_atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST)
+#define atomic_fetch_and_explicit __c11_atomic_fetch_and
+
+/* 7.17.8 Atomic flag type and operations */
+
+typedef struct atomic_flag { atomic_bool _Value; } atomic_flag;
+
+#define ATOMIC_FLAG_INIT { 0 }
+
+#define atomic_flag_test_and_set(object) __c11_atomic_exchange(&(object)->_Value, 1, __ATOMIC_SEQ_CST)
+#define atomic_flag_test_and_set_explicit(object, order) __c11_atomic_exchange(&(object)->_Value, 1, order)
+
+// Clang allows memory_order_consume ordering for __c11_atomic_store,
+// even though C11 doesn't allow it for atomic_store.
+#define atomic_flag_clear(object) __c11_atomic_store(&(object)->_Value, 0, __ATOMIC_SEQ_CST)
+#define atomic_flag_clear_explicit(object, order) __c11_atomic_store(&(object)->_Value, 0, order)
+
+#endif /* __STDATOMIC_H */
