1515#include  " llvm/Support/FormatVariadic.h" 
1616#include  " llvm/Support/raw_ostream.h" 
1717#include  " llvm/Support/xxhash.h" 
18+ #include  " llvm/TableGen/CodeGenHelpers.h" 
1819#include  " llvm/TableGen/Error.h" 
1920#include  " llvm/TableGen/Record.h" 
2021#include  " llvm/TableGen/SetTheory.h" 
@@ -290,8 +291,9 @@ class RuntimeLibcallEmitter {
290291} //  End anonymous namespace.
291292
292293void  RuntimeLibcallEmitter::emitGetRuntimeLibcallEnum (raw_ostream &OS) const  {
293-   OS << " #ifdef GET_RUNTIME_LIBCALL_ENUM\n " 
294-         " namespace llvm {\n " 
294+   IfDefEmitter IfDef (OS, " GET_RUNTIME_LIBCALL_ENUM"  );
295+ 
296+   OS << " namespace llvm {\n " 
295297        " namespace RTLIB {\n " 
296298        " enum Libcall : unsigned short {\n "  ;
297299
@@ -315,8 +317,7 @@ void RuntimeLibcallEmitter::emitGetRuntimeLibcallEnum(raw_ostream &OS) const {
315317     << RuntimeLibcallImplDefList.size () + 1 
316318     << " ;\n " 
317319        " } // End namespace RTLIB\n " 
318-         " } // End namespace llvm\n " 
319-         " #endif\n\n "  ;
320+         " } // End namespace llvm\n "  ;
320321}
321322
322323//  StringMap uses xxh3_64bits, truncated to uint32_t.
@@ -432,14 +433,16 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
432433  // 
433434  //  TODO: It may make more sense to split the search by string size more. There
434435  //  are a few outliers, most call names are small.
435-   OS << " #ifdef GET_LOOKUP_LIBCALL_IMPL_NAME_BODY\n " 
436-         "   size_t Size = Name.size();\n " 
437-         "   if (Size == 0 || Size > " 
438-      << MaxFuncNameSize
439-      << " )\n " 
440-         "     return enum_seq(RTLIB::Unsupported, RTLIB::Unsupported);\n " 
441-         "  return lookupLibcallImplNameImpl(Name);\n " 
442-         " #endif\n "  ;
436+   {
437+     IfDefEmitter IfDef (OS, " GET_LOOKUP_LIBCALL_IMPL_NAME_BODY"  );
438+ 
439+     OS << "   size_t Size = Name.size();\n " 
440+           "   if (Size == 0 || Size > " 
441+        << MaxFuncNameSize
442+        << " )\n " 
443+           "     return enum_seq(RTLIB::Unsupported, RTLIB::Unsupported);\n " 
444+           "  return lookupLibcallImplNameImpl(Name);\n "  ;
445+   }
443446
444447  auto  [Size, Collisions] = computePerfectHashParameters (Hashes);
445448  std::vector<unsigned > Lookup =
@@ -449,7 +452,7 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
449452  LLVM_DEBUG (dbgs () << " Runtime libcall perfect hashing parameters: Size = " 
450453                    << Size << " , maximum collisions = "   << Collisions << ' \n '  );
451454
452-   OS <<  " #ifdef  DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME\n "  ;
455+   IfDefEmitter  IfDef (OS,  " DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME" ) ;
453456  emitHashFunction (OS);
454457
455458  OS << " iota_range<RTLIB::LibcallImpl> RTLIB::RuntimeLibcallsInfo::" 
@@ -481,59 +484,57 @@ void RuntimeLibcallEmitter::emitNameMatchHashTable(
481484  return enum_seq(RTLIB::Unsupported, RTLIB::Unsupported); 
482485} 
483486)"  ;
484- 
485-   OS << " #endif\n\n "  ;
486487}
487488
488489void  RuntimeLibcallEmitter::emitGetInitRuntimeLibcallNames (
489490    raw_ostream &OS) const  {
490-   OS << " #ifdef GET_INIT_RUNTIME_LIBCALL_NAMES\n "  ;
491- 
492491  //  Emit the implementation names
493492  StringToOffsetTable Table (/* AppendZero=*/ true ,
494493                            " RTLIB::RuntimeLibcallsInfo::"  );
495494
496-   for  (const  RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
497-     Table.GetOrAddStringOffset (LibCallImpl.getLibcallFuncName ());
495+   {
496+     IfDefEmitter IfDef (OS, " GET_INIT_RUNTIME_LIBCALL_NAMES"  );
497+ 
498+     for  (const  RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
499+       Table.GetOrAddStringOffset (LibCallImpl.getLibcallFuncName ());
498500
499-   Table.EmitStringTableDef (OS, " RuntimeLibcallImplNameTable"  );
500-   OS << R"( 
501+      Table.EmitStringTableDef (OS, " RuntimeLibcallImplNameTable"  );
502+      OS << R"( 
501503const uint16_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameOffsetTable[] = { 
502504)"  ;
503505
504-   OS << formatv ("   {}, // {}\n "  , Table.GetStringOffset (" "  ),
505-                 " "  ); //  Unsupported entry
506-   for  (const  RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
507-     StringRef ImplName = LibCallImpl.getLibcallFuncName ();
508-     OS << formatv ("   {}, // {}\n "  , Table.GetStringOffset (ImplName), ImplName);
509-   }
510-   OS << " };\n "  ;
506+      OS << formatv ("   {}, // {}\n "  , Table.GetStringOffset (" "  ),
507+                    " "  ); //  Unsupported entry
508+      for  (const  RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
509+        StringRef ImplName = LibCallImpl.getLibcallFuncName ();
510+        OS << formatv ("   {}, // {}\n "  , Table.GetStringOffset (ImplName), ImplName);
511+      }
512+      OS << " };\n "  ;
511513
512-   OS << R"( 
514+      OS << R"( 
513515const uint8_t RTLIB::RuntimeLibcallsInfo::RuntimeLibcallNameSizeTable[] = { 
514516)"  ;
515517
516-   OS << "   0,\n "  ;
517-   for  (const  RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
518-     OS << "   "   << LibCallImpl.getLibcallFuncName ().size () << " ,\n "  ;
519-   OS << " };\n\n "  ;
518+      OS << "   0,\n "  ;
519+      for  (const  RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList)
520+        OS << "   "   << LibCallImpl.getLibcallFuncName ().size () << " ,\n "  ;
521+      OS << " };\n\n "  ;
520522
521-   //  Emit the reverse mapping from implementation libraries to RTLIB::Libcall
522-   OS << " const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::" 
523-         " ImplToLibcall[RTLIB::NumLibcallImpls] = {\n " 
524-         "   RTLIB::UNKNOWN_LIBCALL, // RTLIB::Unsupported\n "  ;
523+      //  Emit the reverse mapping from implementation libraries to RTLIB::Libcall
524+      OS << " const RTLIB::Libcall llvm::RTLIB::RuntimeLibcallsInfo::" 
525+            " ImplToLibcall[RTLIB::NumLibcallImpls] = {\n " 
526+            "   RTLIB::UNKNOWN_LIBCALL, // RTLIB::Unsupported\n "  ;
525527
526-   for  (const  RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
527-     const  RuntimeLibcall *Provides = LibCallImpl.getProvides ();
528-     OS << "   "  ;
529-     Provides->emitEnumEntry (OS);
530-     OS << " , // "  ;
531-     LibCallImpl.emitEnumEntry (OS);
532-     OS << ' \n '  ;
528+     for  (const  RuntimeLibcallImpl &LibCallImpl : RuntimeLibcallImplDefList) {
529+       const  RuntimeLibcall *Provides = LibCallImpl.getProvides ();
530+       OS << "   "  ;
531+       Provides->emitEnumEntry (OS);
532+       OS << " , // "  ;
533+       LibCallImpl.emitEnumEntry (OS);
534+       OS << ' \n '  ;
535+     }
536+     OS << " };\n\n "  ;
533537  }
534-   OS << " };\n\n "  ;
535- 
536-   OS << " #endif\n\n "  ;
537538
538539  emitNameMatchHashTable (OS, Table);
539540}
@@ -757,9 +758,10 @@ void RuntimeLibcallEmitter::run(raw_ostream &OS) {
757758
758759  emitGetInitRuntimeLibcallNames (OS);
759760
760-   OS << " #ifdef GET_SET_TARGET_RUNTIME_LIBCALL_SETS\n "  ;
761-   emitSystemRuntimeLibrarySetCalls (OS);
762-   OS << " #endif\n\n "  ;
761+   {
762+     IfDefEmitter IfDef (OS, " GET_SET_TARGET_RUNTIME_LIBCALL_SETS"  );
763+     emitSystemRuntimeLibrarySetCalls (OS);
764+   }
763765}
764766
765767void  LibcallPredicateExpander::expand (SetTheory &ST, const  Record *Def,
0 commit comments