Index: lib/TableGen/TGParser.cpp
===================================================================
--- lib/TableGen/TGParser.cpp	(revision 198043)
+++ lib/TableGen/TGParser.cpp	(working copy)
@@ -2277,7 +2277,7 @@
   // Add in the defm name.  If the defm prefix is empty, give each
   // instantiated def a unique name.  Otherwise, if "#NAME#" exists in the
   // name, substitute the prefix for #NAME#.  Otherwise, use the defm name
-  // as a prefix.
+  // as a prefix or suffix, depending on the format of #NAME#.
 
   bool IsAnonymous = false;
   if (DefmPrefix == 0) {
@@ -2291,12 +2291,22 @@
 
   if (DefNameString != 0) {
     // We have a fully expanded string so there are no operators to
-    // resolve.  We should concatenate the given prefix and name.
-    DefName =
-      BinOpInit::get(BinOpInit::STRCONCAT,
-                     UnOpInit::get(UnOpInit::CAST, DefmPrefix,
-                                   StringRecTy::get())->Fold(DefProto, &MC),
-                     DefName, StringRecTy::get())->Fold(DefProto, &MC);
+    // resolve. We should concatenate the given prefix and name, depending
+    // on whether the "prefix" ends with an underscore or not. If it ends with
+    // an underscore, it is a prefix to the DefName, otherwise it is a suffix.
+    std::string Str = DefNameString->getAsUnquotedString();
+    if (!Str.empty() && *(Str.end() - 1) == '_')
+      DefName =
+        BinOpInit::get(BinOpInit::STRCONCAT, DefName,
+                       UnOpInit::get(UnOpInit::CAST, DefmPrefix,
+                                     StringRecTy::get())->Fold(DefProto, &MC),
+                       StringRecTy::get())->Fold(DefProto, &MC);
+    else
+      DefName =
+        BinOpInit::get(BinOpInit::STRCONCAT,
+                       UnOpInit::get(UnOpInit::CAST, DefmPrefix,
+                                     StringRecTy::get())->Fold(DefProto, &MC),
+                       DefName, StringRecTy::get())->Fold(DefProto, &MC);
   }
 
   // Make a trail of SMLocs from the multiclass instantiations.
