Reapply r261657.
Remove an unnecessary workaround introduced in r259975. (NFC)

Now that LLVM r259973 allows replacing a temporary type with another
temporary we can rely on the original implementation.

It is possible for enums to be created as part of
their own declcontext. In this case a FwdDecl will be created
twice. This doesn't cause a problem because both FwdDecls are
entered into the ReplaceMap: finalize() will replace the first
FwdDecl with the second and then replace the second with
complete type.

Thanks to echristo for pointing this out.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@261673 91177308-0d34-0410-b5e6-96231b3b80d8
(cherry picked from commit 544b2b7bbae5f19aa31c3714b65c3f9cfacc63e3)
<rdar://problem/25078246>
diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp
index 266cd0c..a765b33 100644
--- a/lib/CodeGen/CGDebugInfo.cpp
+++ b/lib/CodeGen/CGDebugInfo.cpp
@@ -2038,26 +2038,23 @@
   // If this is just a forward declaration, construct an appropriately
   // marked node and just return it.
   if (isImportedFromModule || !ED->getDefinition()) {
+    // Note that it is possible for enums to be created as part of
+    // their own declcontext. In this case a FwdDecl will be created
+    // twice. This doesn't cause a problem because both FwdDecls are
+    // entered into the ReplaceMap: finalize() will replace the first
+    // FwdDecl with the second and then replace the second with
+    // complete type.
+    llvm::DIScope *EDContext = getDeclContextDescriptor(ED);
     llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
-
-    // It is possible for enums to be created as part of their own
-    // declcontext. We need to cache a placeholder to avoid the type being
-    // created twice before hitting the cache.
-    llvm::DIScope *TmpContext = DBuilder.createReplaceableCompositeType(
-      llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0);
+    llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
+        llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
 
     unsigned Line = getLineNumber(ED->getLocation());
     StringRef EDName = ED->getName();
     llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
-        llvm::dwarf::DW_TAG_enumeration_type, EDName, TmpContext, DefUnit, Line,
+        llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
         0, Size, Align, llvm::DINode::FlagFwdDecl, FullName);
 
-    // Cache the enum type so it is available when building the declcontext
-    // and replace the declcontect with the real thing.
-    TypeCache[Ty].reset(RetTy);
-    TmpContext->replaceAllUsesWith(getDeclContextDescriptor(ED));
-    llvm::MDNode::deleteTemporary(TmpContext);
-
     ReplaceMap.emplace_back(
         std::piecewise_construct, std::make_tuple(Ty),
         std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));