Index: lib/AST/MicrosoftMangle.cpp
===================================================================
--- lib/AST/MicrosoftMangle.cpp	(revision 158591)
+++ lib/AST/MicrosoftMangle.cpp	(working copy)
@@ -31,6 +31,9 @@
   MangleContext &Context;
   raw_ostream &Out;
 
+  typedef llvm::DenseMap<const IdentifierInfo*, unsigned> BackRefMap;
+  BackRefMap back_references;
+
   ASTContext &getASTContext() const { return Context.getASTContext(); }
 
 public:
@@ -641,7 +644,14 @@
 
 void MicrosoftCXXNameMangler::mangleSourceName(const IdentifierInfo *II) {
   // <source name> ::= <identifier> @
-  Out << II->getName() << '@';
+  BackRefMap::iterator found = back_references.find(II);
+  if (found == back_references.end()) {
+    Out << II->getName() << '@';
+    if (back_references.size() < 10)
+      back_references[II] = back_references.size();  // size++
+  } else {
+    Out << found->second;
+  }
 }
 
 void MicrosoftCXXNameMangler::mangleObjCMethodName(const ObjCMethodDecl *MD) {
@@ -938,17 +948,19 @@
   case BuiltinType::ObjCId: Out << "PAUobjc_object@@"; break;
   case BuiltinType::ObjCClass: Out << "PAUobjc_class@@"; break;
   case BuiltinType::ObjCSel: Out << "PAUobjc_selector@@"; break;
+ 
+  case BuiltinType::NullPtr: Out << "$$T"; break;
 
   case BuiltinType::Char16:
   case BuiltinType::Char32:
-  case BuiltinType::Half:
-  case BuiltinType::NullPtr: {
+  case BuiltinType::Half: {
     DiagnosticsEngine &Diags = Context.getDiags();
     unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
       "cannot mangle this built-in %0 type yet");
     Diags.Report(Range.getBegin(), DiagID)
       << T->getName(Context.getASTContext().getPrintingPolicy())
       << Range;
+    break;
   }
   }
 }
@@ -998,17 +1010,28 @@
   if (Proto->getNumArgs() == 0 && !Proto->isVariadic()) {
     Out << 'X';
   } else {
+    typedef llvm::DenseMap<void*, unsigned> BackRef;
+    BackRef back_refs;
     if (D) {
       // If we got a decl, use the type-as-written to make sure arrays
       // get mangled right.  Note that we can't rely on the TSI
       // existing if (for example) the parameter was synthesized.
       for (FunctionDecl::param_const_iterator Parm = D->param_begin(),
              ParmEnd = D->param_end(); Parm != ParmEnd; ++Parm) {
-        if (TypeSourceInfo *typeAsWritten = (*Parm)->getTypeSourceInfo())
-          mangleType(typeAsWritten->getType(),
-                     typeAsWritten->getTypeLoc().getSourceRange());
-        else
-          mangleType((*Parm)->getType(), SourceRange());
+        TypeSourceInfo *typeAsWritten = (*Parm)->getTypeSourceInfo();
+        QualType type = typeAsWritten ? typeAsWritten->getType() : (*Parm)->getType();
+        CanQualType canonical = getASTContext().getCanonicalType(type);
+        void *type_ptr = canonical.getAsOpaquePtr();
+        BackRef::iterator found = back_refs.find(type_ptr);
+        if (found == back_refs.end()) {
+          SourceRange SR = typeAsWritten ? typeAsWritten->getTypeLoc().getSourceRange()
+              : (*Parm)->getSourceRange();
+          mangleType(type, SR);
+          if (back_refs.size() < 10 && canonical->getTypeClass() != Type::Builtin)
+            back_refs[type_ptr] = back_refs.size();  // size++
+        } else {
+          Out << found->second;
+        }
       }
     } else {
       for (FunctionProtoType::arg_type_iterator Arg = Proto->arg_type_begin(),
@@ -1316,13 +1339,16 @@
   mangleType(PointeeTy, Range);
 }
 
+// <type> ::= <r-value-reference-type>
+// <r-value-reference-type> ::= $$Q <cvr-qualifiers> <type>
 void MicrosoftCXXNameMangler::mangleType(const RValueReferenceType *T,
                                          SourceRange Range) {
-  DiagnosticsEngine &Diags = Context.getDiags();
-  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
-    "cannot mangle this r-value reference type yet");
-  Diags.Report(Range.getBegin(), DiagID)
-    << Range;
+  Out << "$$Q";
+  QualType PointeeTy = T->getPointeeType();
+  if (!PointeeTy.hasQualifiers())
+    // Lack of qualifiers is mangled as 'A'.
+    Out << 'A';
+  mangleType(PointeeTy, Range);
 }
 
 void MicrosoftCXXNameMangler::mangleType(const ComplexType *T,
Index: test/CodeGenCXX/mangle-ms-back-references.cpp
===================================================================
--- test/CodeGenCXX/mangle-ms-back-references.cpp	(revision 0)
+++ test/CodeGenCXX/mangle-ms-back-references.cpp	(working copy)
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -fms-extensions -fblocks -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 | FileCheck %s
+
+void f1(const char* a, const char* b) {}
+// CHECK: "\01?f1@@YAXPBD0@Z"
+
+void f2(const char* a, char* b) {}
+// CHECK: "\01?f2@@YAXPBDPAD@Z"
+
+void f3(int a, const char* b, const char* c) {}
+// CHECK: "\01?f3@@YAXHPBD0@Z"
+
+const char *f4(const char* a, const char* b) {}
+// CHECK: "\01?f4@@YAPBDPBD0@Z"
+
+// FIXME: tests for more than 10 types?
+
+struct S {};
+
+void g4(const char* a, struct S* b, const char *c, struct S* d) {}
+// CHECK: "\01?g4@@YAXPBDPAUS@@01@Z"
+
+typedef void (*VoidFunc)();
+
+void foo_ptr(const char* a, const char* b, VoidFunc c, VoidFunc d) {}
+// CHECK: @"\01?foo_ptr@@YAXPBD0P6AXXZ1@Z"
+
+// Make sure that different aliases of built-in types end up mangled as the
+// built-ins.
+typedef unsigned int uintptr_t;
+typedef unsigned int size_t;
+void *h(size_t a, uintptr_t b) {}
+// CHECK: "\01?h@@YAPAXII@Z"
Index: test/CodeGenCXX/mangle-ms-cpp11.cpp
===================================================================
--- test/CodeGenCXX/mangle-ms-cpp11.cpp	(revision 0)
+++ test/CodeGenCXX/mangle-ms-cpp11.cpp	(working copy)
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++11 -fms-extensions -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 | FileCheck %s
+
+// CHECK: "\01?LRef@@YAXAAH@Z"
+void LRef(int& a) { }
+
+// CHECK: "\01?RRef@@YAH$$QAH@Z"
+int RRef(int&& a) { return a; }
+
+// CHECK: "\01?Null@@YAX$$T@Z"
+namespace std { typedef decltype(__nullptr) nullptr_t; }
+void Null(std::nullptr_t) {}
