Index: test/CodeGenCXX/microsoft-uuidof.cpp
===================================================================
--- test/CodeGenCXX/microsoft-uuidof.cpp	(revision 0)
+++ test/CodeGenCXX/microsoft-uuidof.cpp	(revision 0)
@@ -0,0 +1,72 @@
+// RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-win32 -fms-extensions | FileCheck %s
+
+typedef struct _GUID
+{
+    unsigned long  Data1;
+    unsigned short Data2;
+    unsigned short Data3;
+    unsigned char  Data4[8];
+} GUID;
+
+struct __declspec(uuid("12345678-1234-1234-1234-1234567890ab")) S1 { } s1;
+struct __declspec(uuid("87654321-4321-4321-4321-ba0987654321")) S2 { };
+
+// This gets initialized in a static initializer.
+// CHECK: @g = global %struct._GUID zeroinitializer, align 4
+GUID g = __uuidof(S1);
+
+// First global use of __uuidof(S1) forces the creation of the global.
+// CHECK: @__uuid_12345678-1234-1234-1234-1234567890ab = private unnamed_addr constant %struct._GUID { i32 305419896, i16 4660, i16 4660, [8 x i8] c"\124\124Vx\90\AB" }
+// CHECK: @gr = constant %struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab, align 4
+const GUID& gr = __uuidof(S1);
+
+// CHECK: @gp = global %struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab, align 4
+const GUID* gp = &__uuidof(S1);
+
+// Special case: _uuidof(0)
+// CHECK: @zeroiid = constant %struct._GUID* @__uuid_00000000-0000-0000-0000-000000000000, align 4
+const GUID& zeroiid = __uuidof(0);
+
+// __uuidof(S2) hasn't been used globally yet, so it's emitted when it's used
+// in a function and is emitted at the end of the globals section.
+// CHECK: @__uuid_87654321-4321-4321-4321-ba0987654321 = private unnamed_addr constant %struct._GUID { i32 -2023406815, i16 17185, i16 17185, [8 x i8] c"C!\BA\09\87eC!" }
+
+// The static initializer for g.
+// CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* bitcast (%struct._GUID* @g to i8*), i8* bitcast (%struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab to i8*), i32 16, i32 4, i1 false)
+
+void fun() {
+  // CHECK: %s1_1 = alloca %struct._GUID, align 4
+  // CHECK: %s1_2 = alloca %struct._GUID, align 4
+  // CHECK: %s1_3 = alloca %struct._GUID, align 4
+
+  // CHECK: %0 = bitcast %struct._GUID* %s1_1 to i8*
+  // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast (%struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab to i8*), i32 16, i32 4, i1 false)
+  GUID s1_1 = __uuidof(S1);
+
+  // CHECK: %1 = bitcast %struct._GUID* %s1_2 to i8*
+  // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %1, i8* bitcast (%struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab to i8*), i32 16, i32 4, i1 false)
+  GUID s1_2 = __uuidof(S1);
+
+  // CHECK: %2 = bitcast %struct._GUID* %s1_3 to i8*
+  // CHECK: call void @llvm.memcpy.p0i8.p0i8.i32(i8* %2, i8* bitcast (%struct._GUID* @__uuid_12345678-1234-1234-1234-1234567890ab to i8*), i32 16, i32 4, i1 false)
+  GUID s1_3 = __uuidof(s1);
+}
+
+void gun() {
+  // CHECK: %s2_1 = alloca %struct._GUID, align 4
+  // CHECK: %s2_2 = alloca %struct._GUID, align 4
+  // CHECK: %r = alloca %struct._GUID*, align 4
+  // CHECK: %p = alloca %struct._GUID*, align 4
+  // CHECK: %zeroiid = alloca %struct._GUID*, align 4
+  GUID s2_1 = __uuidof(S2);
+  GUID s2_2 = __uuidof(S2);
+
+  // CHECK: store %struct._GUID* @__uuid_87654321-4321-4321-4321-ba0987654321, %struct._GUID** %r, align 4
+  const GUID& r = __uuidof(S2);
+  // CHECK: store %struct._GUID* @__uuid_87654321-4321-4321-4321-ba0987654321, %struct._GUID** %p, align 4
+  const GUID* p = &__uuidof(S2);
+
+  // Special case _uuidof(0), local scope version.
+  // CHECK: store %struct._GUID* @__uuid_00000000-0000-0000-0000-000000000000, %struct._GUID** %zeroiid, align 4
+  const GUID& zeroiid = __uuidof(0);
+}
Index: include/clang/AST/ExprCXX.h
===================================================================
--- include/clang/AST/ExprCXX.h	(revision 164668)
+++ include/clang/AST/ExprCXX.h	(working copy)
@@ -612,6 +612,9 @@
   }
   static bool classof(const CXXUuidofExpr *) { return true; }
 
+  /// Grabs __declspec(uuid()) off a type, or returns 0 if there is none.
+  static UuidAttr *GetUuidAttrOfType(QualType QT);
+
   // Iterators
   child_range children() {
     if (isTypeOperand()) return child_range();
Index: lib/Sema/SemaExprCXX.cpp
===================================================================
--- lib/Sema/SemaExprCXX.cpp	(revision 164668)
+++ lib/Sema/SemaExprCXX.cpp	(working copy)
@@ -410,33 +410,13 @@
   return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc);
 }
 
-/// Retrieve the UuidAttr associated with QT.
-static UuidAttr *GetUuidAttrOfType(QualType QT) {
-  // Optionally remove one level of pointer, reference or array indirection.
-  const Type *Ty = QT.getTypePtr();
-  if (QT->isPointerType() || QT->isReferenceType())
-    Ty = QT->getPointeeType().getTypePtr();
-  else if (QT->isArrayType())
-    Ty = cast<ArrayType>(QT)->getElementType().getTypePtr();
-
-  // Loop all record redeclaration looking for an uuid attribute.
-  CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
-  for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
-       E = RD->redecls_end(); I != E; ++I) {
-    if (UuidAttr *Uuid = I->getAttr<UuidAttr>())
-      return Uuid;
-  }
-
-  return 0;
-}
-
 /// \brief Build a Microsoft __uuidof expression with a type operand.
 ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType,
                                 SourceLocation TypeidLoc,
                                 TypeSourceInfo *Operand,
                                 SourceLocation RParenLoc) {
   if (!Operand->getType()->isDependentType()) {
-    if (!GetUuidAttrOfType(Operand->getType()))
+    if (!CXXUuidofExpr::GetUuidAttrOfType(Operand->getType()))
       return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid));
   }
 
@@ -452,7 +432,7 @@
                                 Expr *E,
                                 SourceLocation RParenLoc) {
   if (!E->getType()->isDependentType()) {
-    if (!GetUuidAttrOfType(E->getType()) &&
+    if (!CXXUuidofExpr::GetUuidAttrOfType(E->getType()) &&
         !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
       return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid));
   }
Index: lib/AST/ExprCXX.cpp
===================================================================
--- lib/AST/ExprCXX.cpp	(revision 164668)
+++ lib/AST/ExprCXX.cpp	(working copy)
@@ -51,6 +51,26 @@
                                                         .getUnqualifiedType();
 }
 
+// static
+UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT) {
+  // Optionally remove one level of pointer, reference or array indirection.
+  const Type *Ty = QT.getTypePtr();
+  if (QT->isPointerType() || QT->isReferenceType())
+    Ty = QT->getPointeeType().getTypePtr();
+  else if (QT->isArrayType())
+    Ty = cast<ArrayType>(QT)->getElementType().getTypePtr();
+
+  // Loop all record redeclaration looking for an uuid attribute.
+  CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
+  for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
+       E = RD->redecls_end(); I != E; ++I) {
+    if (UuidAttr *Uuid = I->getAttr<UuidAttr>())
+      return Uuid;
+  }
+
+  return 0;
+}
+
 // CXXScalarValueInitExpr
 SourceRange CXXScalarValueInitExpr::getSourceRange() const {
   SourceLocation Start = RParenLoc;
Index: lib/CodeGen/CGExprConstant.cpp
===================================================================
--- lib/CodeGen/CGExprConstant.cpp	(revision 164668)
+++ lib/CodeGen/CGExprConstant.cpp	(working copy)
@@ -995,7 +995,10 @@
         T = Typeid->getExprOperand()->getType();
       return CGM.GetAddrOfRTTIDescriptor(T);
     }
+    case Expr::CXXUuidofExprClass: {
+      return CGM.GetAddrOfUuidDescriptor(cast<CXXUuidofExpr>(E));
     }
+    }
 
     return 0;
   }
Index: lib/CodeGen/CodeGenModule.h
===================================================================
--- lib/CodeGen/CodeGenModule.h	(revision 164668)
+++ lib/CodeGen/CodeGenModule.h	(working copy)
@@ -548,6 +548,9 @@
   /// for the given type.
   llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
 
+  /// GetAddrOfUuidDescriptor - Get the address of a uuid descriptor .
+  llvm::Constant *GetAddrOfUuidDescriptor(const CXXUuidofExpr* E);
+
   /// GetAddrOfThunk - Get the address of the thunk for the given global decl.
   llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
 
@@ -984,6 +987,9 @@
   /// to emit the .gcno and .gcda files in a way that persists in .bc files.
   void EmitCoverageFile();
 
+  /// Emits the initializer for a uuidof string.
+  llvm::Constant *EmitUuidofInitializer(StringRef uuidstr, QualType IIDType);
+
   /// MayDeferGeneration - Determine if the given decl can be emitted
   /// lazily; this is only relevant for definitions. The given decl
   /// must be either a function or var decl.
Index: lib/CodeGen/CGExpr.cpp
===================================================================
--- lib/CodeGen/CGExpr.cpp	(revision 164668)
+++ lib/CodeGen/CGExpr.cpp	(working copy)
@@ -708,7 +708,7 @@
     llvm_unreachable("cannot emit a property reference directly");
 
   case Expr::ObjCSelectorExprClass:
-  return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
+    return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
   case Expr::ObjCIsaExprClass:
     return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
   case Expr::BinaryOperatorClass:
@@ -745,6 +745,8 @@
     return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
   case Expr::CXXBindTemporaryExprClass:
     return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
+  case Expr::CXXUuidofExprClass:
+    return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E));
   case Expr::LambdaExprClass:
     return EmitLambdaLValue(cast<LambdaExpr>(E));
 
@@ -2719,6 +2721,14 @@
   return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType());
 }
 
+llvm::Value *CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) {
+  return CGM.GetAddrOfUuidDescriptor(E);
+}
+
+LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) {
+  return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType());
+}
+
 LValue
 CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
   AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
Index: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp	(revision 164668)
+++ lib/CodeGen/CodeGenModule.cpp	(working copy)
@@ -822,6 +822,37 @@
   return !getContext().DeclMustBeEmitted(Global);
 }
 
+llvm::Constant *CodeGenModule::GetAddrOfUuidDescriptor(
+    const CXXUuidofExpr* E) {
+  // Sema has verified that IIDSource has a __declspec(uuid()), and that its
+  // well-formed.
+  StringRef Uuid;
+  if (E->isTypeOperand())
+    Uuid = CXXUuidofExpr::GetUuidAttrOfType(E->getTypeOperand())->getGuid();
+  else {
+    // Special case: __uuidof(0) means an all-zero GUID.
+    Expr *Op = E->getExprOperand();
+    if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
+      Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
+    else
+      Uuid = "00000000-0000-0000-0000-000000000000";
+  }
+  std::string Name = "__uuid_" + Uuid.str();
+
+  // Look for an existing global.
+  if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
+    return GV;
+
+  llvm::Constant *Init = EmitUuidofInitializer(Uuid, E->getType());
+  assert(Init && "failed to initialize as constant");
+
+  llvm::GlobalVariable *GV = new llvm::GlobalVariable(getModule(),
+      getTypes().ConvertType(E->getType()), /*isConstant=*/true,
+      llvm::GlobalValue::PrivateLinkage, Init, Name);
+  GV->setUnnamedAddr(true);
+  return GV;
+}
+
 llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
   const AliasAttr *AA = VD->getAttr<AliasAttr>();
   assert(AA && "No alias?");
@@ -1275,7 +1306,7 @@
 
     // Because C++ name mangling, the only way we can end up with an already
     // existing global with the same name is if it has been declared extern "C".
-      assert(GV->isDeclaration() && "Declaration has wrong type!");
+    assert(GV->isDeclaration() && "Declaration has wrong type!");
     OldGV = GV;
   }
   
@@ -2745,3 +2776,40 @@
     }
   }
 }
+
+llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid,
+                                                     QualType GuidType) {
+  // Sema has checked that all uuid strings are of the form
+  // "12345678-1234-1234-1234-1234567890ab".
+  assert(Uuid.size() == 36);
+  const char *Uuidstr = Uuid.data();
+  for (int i = 0; i < 36; ++i) {
+    if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuidstr[i] == '-');
+    else                                         assert(isxdigit(Uuidstr[i]));
+  }
+  
+  llvm::APInt data1int(32, StringRef(Uuidstr, 8), 16);
+  llvm::APInt data2int(16, StringRef(Uuidstr + 9, 4), 16);
+  llvm::APInt data3int(16, StringRef(Uuidstr + 14, 4), 16);
+  llvm::APInt data4ints[] = {
+    llvm::APInt(8, StringRef(Uuidstr + 19, 2), 16),
+    llvm::APInt(8, StringRef(Uuidstr + 21, 2), 16),
+    llvm::APInt(8, StringRef(Uuidstr + 24, 2), 16),
+    llvm::APInt(8, StringRef(Uuidstr + 26, 2), 16),
+    llvm::APInt(8, StringRef(Uuidstr + 28, 2), 16),
+    llvm::APInt(8, StringRef(Uuidstr + 30, 2), 16),
+    llvm::APInt(8, StringRef(Uuidstr + 32, 2), 16),
+    llvm::APInt(8, StringRef(Uuidstr + 34, 2), 16),
+  };
+
+  APValue InitStruct(APValue::UninitStruct(), 0, 4);
+  InitStruct.getStructField(0) = APValue(llvm::APSInt(data1int));
+  InitStruct.getStructField(1) = APValue(llvm::APSInt(data2int));
+  InitStruct.getStructField(2) = APValue(llvm::APSInt(data3int));
+  APValue& Arr = InitStruct.getStructField(3);
+  Arr = APValue(APValue::UninitArray(), 8, 8);
+  for (int t = 0; t < 8; ++t)
+    Arr.getArrayInitializedElt(t) = APValue(llvm::APSInt(data4ints[t]));
+
+  return EmitConstantValue(InitStruct, GuidType);
+}
Index: lib/CodeGen/CodeGenFunction.h
===================================================================
--- lib/CodeGen/CodeGenFunction.h	(revision 164668)
+++ lib/CodeGen/CodeGenFunction.h	(working copy)
@@ -1828,6 +1828,7 @@
 
   llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
   llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
+  llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E);
 
   void MaybeEmitStdInitializerListCleanup(llvm::Value *loc, const Expr *init);
   void EmitStdInitializerListCleanup(llvm::Value *loc,
@@ -2197,6 +2198,7 @@
   LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
   LValue EmitLambdaLValue(const LambdaExpr *E);
   LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
+  LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
 
   LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
   LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
