Make __atomic_init() (soon to be __c11_atomic_init()) work with non-scalar types.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154507 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp
index dbf41f3..260fa5b 100644
--- a/lib/CodeGen/CGExpr.cpp
+++ b/lib/CodeGen/CGExpr.cpp
@@ -2786,10 +2786,21 @@
 
   if (E->getOp() == AtomicExpr::Init) {
     assert(!Dest && "Init does not return a value");
-    Val1 = EmitScalarExpr(E->getVal1());
-    llvm::StoreInst *Store = Builder.CreateStore(Val1, Ptr);
-    Store->setAlignment(Size);
-    Store->setVolatile(E->isVolatile());
+    if (!hasAggregateLLVMType(E->getVal1()->getType())) {
+      llvm::StoreInst *Store =
+        Builder.CreateStore(EmitScalarExpr(E->getVal1()), Ptr);
+      Store->setAlignment(Size);
+      Store->setVolatile(E->isVolatile());
+    } else if (E->getType()->isAnyComplexType()) {
+      EmitComplexExprIntoAddr(E->getVal1(), Ptr, E->isVolatile());
+    } else {
+      AggValueSlot Slot = AggValueSlot::forAddr(Ptr, alignChars,
+                                        AtomicTy.getQualifiers(),
+                                        AggValueSlot::IsNotDestructed,
+                                        AggValueSlot::DoesNotNeedGCBarriers,
+                                        AggValueSlot::IsNotAliased);
+      EmitAggExpr(E->getVal1(), Slot);
+    }
     return RValue::get(0);
   }
 
diff --git a/test/CodeGenCXX/atomicinit.cpp b/test/CodeGenCXX/atomicinit.cpp
index 17c5b62..63e3811 100644
--- a/test/CodeGenCXX/atomicinit.cpp
+++ b/test/CodeGenCXX/atomicinit.cpp
@@ -10,3 +10,16 @@
 // Initialising atomic values should not be atomic
 // CHECK-NOT: store atomic 
 A::A(int j) : i(j) {}
+
+struct B {
+  int i;
+  B(int x) : i(x) {}
+};
+
+_Atomic(B) b;
+
+// CHECK: define void @_Z11atomic_initR1Ai
+void atomic_init(A& a, int i) {
+  // CHECK-NOT: atomic
+  __atomic_init(&b, B(i));
+}