InstrProf: Reorganize files; no functionality change

Move functions around to prepare for some other changes.

  - Merge InstrProfilingExtras.h with InstrProfiling.h.  There's no
    benefit to having these split.

  - Rename InstrProfilingExtras.c to InstrProfilingFile.c.

  - Split actual buffer writing code out of InstrProfiling.c into
    InstrProfilingBuffer.c.

  - Drive-by corrections of a couple of header comments.

<rdar://problem/15943240>

llvm-svn: 204497
diff --git a/compiler-rt/lib/profile/CMakeLists.txt b/compiler-rt/lib/profile/CMakeLists.txt
index 949e174..526017f 100644
--- a/compiler-rt/lib/profile/CMakeLists.txt
+++ b/compiler-rt/lib/profile/CMakeLists.txt
@@ -6,9 +6,10 @@
   set(PROFILE_SOURCES
     GCDAProfiling.c
     InstrProfiling.c
+    InstrProfilingBuffer.c
+    InstrProfilingFile.c
     InstrProfilingPlatformDarwin.c
-    InstrProfilingRuntime.cc
-    InstrProfilingExtras.c)
+    InstrProfilingRuntime.cc)
 
   add_compiler_rt_osx_static_runtime(clang_rt.profile_osx
     ARCH ${PROFILE_SUPPORTED_ARCH}
@@ -18,9 +19,10 @@
   set(PROFILE_SOURCES
     GCDAProfiling.c
     InstrProfiling.c
+    InstrProfilingBuffer.c
+    InstrProfilingFile.c
     InstrProfilingPlatformOther.c
-    InstrProfilingRuntime.cc
-    InstrProfilingExtras.c)
+    InstrProfilingRuntime.cc)
 
   foreach(arch ${PROFILE_SUPPORTED_ARCH})
     add_compiler_rt_static_runtime(clang_rt.profile-${arch}
diff --git a/compiler-rt/lib/profile/InstrProfiling.c b/compiler-rt/lib/profile/InstrProfiling.c
index a62c214..de88f1b 100644
--- a/compiler-rt/lib/profile/InstrProfiling.c
+++ b/compiler-rt/lib/profile/InstrProfiling.c
@@ -10,9 +10,8 @@
 #include "InstrProfiling.h"
 #include <string.h>
 
-/* TODO: void __llvm_profile_get_size_for_buffer(void);  */
-
-static uint64_t getMagic(void) {
+uint64_t __llvm_profile_get_magic(void) {
+  /* Magic number to detect file format and endianness. */
   return
     (uint64_t)'l' << 56 |
     (uint64_t)'p' << 48 |
@@ -24,53 +23,11 @@
     (uint64_t)'w';
 }
 
-static uint64_t getVersion(void) {
+uint64_t __llvm_profile_get_version(void) {
+  /* This should be bumped any time the output format changes. */
   return 1;
 }
 
-int __llvm_profile_write_buffer(FILE *OutputFile) {
-  /* TODO: Requires libc: break requirement by taking a char* buffer instead of
-   * a FILE stream.
-   */
-  const __llvm_profile_data *DataBegin = __llvm_profile_data_begin();
-  const __llvm_profile_data *DataEnd = __llvm_profile_data_end();
-  const uint64_t *CountersBegin = __llvm_profile_counters_begin();
-  const uint64_t *CountersEnd   = __llvm_profile_counters_end();
-  const char *NamesBegin = __llvm_profile_names_begin();
-  const char *NamesEnd   = __llvm_profile_names_end();
-
-  /* Calculate size of sections. */
-  const uint64_t DataSize = DataEnd - DataBegin;
-  const uint64_t CountersSize = CountersEnd - CountersBegin;
-  const uint64_t NamesSize = NamesEnd - NamesBegin;
-
-  /* Get rest of header data. */
-  const uint64_t Magic = getMagic();
-  const uint64_t Version = getVersion();
-  const uint64_t CountersDelta = (uint64_t)CountersBegin;
-  const uint64_t NamesDelta = (uint64_t)NamesBegin;
-
-#define CHECK_fwrite(Data, Size, Length, File) \
-  do { if (fwrite(Data, Size, Length, File) != Length) return -1; } while (0)
-
-  /* Write the header. */
-  CHECK_fwrite(&Magic,         sizeof(uint64_t), 1, OutputFile);
-  CHECK_fwrite(&Version,       sizeof(uint64_t), 1, OutputFile);
-  CHECK_fwrite(&DataSize,      sizeof(uint64_t), 1, OutputFile);
-  CHECK_fwrite(&CountersSize,  sizeof(uint64_t), 1, OutputFile);
-  CHECK_fwrite(&NamesSize,     sizeof(uint64_t), 1, OutputFile);
-  CHECK_fwrite(&CountersDelta, sizeof(uint64_t), 1, OutputFile);
-  CHECK_fwrite(&NamesDelta,    sizeof(uint64_t), 1, OutputFile);
-
-  /* Write the data. */
-  CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, OutputFile);
-  CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, OutputFile);
-  CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, OutputFile);
-#undef CHECK_fwrite
-
-  return 0;
-}
-
 void __llvm_profile_reset_counters(void) {
   uint64_t *I = __llvm_profile_counters_begin();
   uint64_t *E = __llvm_profile_counters_end();
diff --git a/compiler-rt/lib/profile/InstrProfiling.h b/compiler-rt/lib/profile/InstrProfiling.h
index 17d3fd4..d3600ac 100644
--- a/compiler-rt/lib/profile/InstrProfiling.h
+++ b/compiler-rt/lib/profile/InstrProfiling.h
@@ -7,8 +7,10 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
+#ifndef PROFILE_INSTRPROFILING_H__
+#define PROFILE_INSTRPROFILING_H__
+
 #include <stdio.h>
-#include <stdlib.h>
 
 #define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
 
@@ -40,7 +42,7 @@
   uint64_t *const Counters;
 } __llvm_profile_data;
 
-/* TODO: void __llvm_profile_get_size_for_buffer(void);  */
+/* TODO: void __llvm_profile_get_size_for_buffer(void); */
 
 /*!
  * \brief Write instrumentation data to the given buffer.
@@ -57,3 +59,37 @@
 const char *__llvm_profile_names_end(void);
 uint64_t *__llvm_profile_counters_begin(void);
 uint64_t *__llvm_profile_counters_end(void);
+
+#define PROFILE_RANGE_SIZE(Range) \
+  (__llvm_profile_ ## Range ## _end() - __llvm_profile_ ## Range ## _begin())
+
+/*!
+ * \brief Write instrumentation data to the current file.
+ *
+ * Writes to the file with the last name given to \a __llvm_profile_set_filename(),
+ * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
+ * or if that's not set, \c "default.profdata".
+ */
+int __llvm_profile_write_file(void);
+
+/*!
+ * \brief Set the filename for writing instrumentation data.
+ *
+ * Sets the filename to be used for subsequent calls to
+ * \a __llvm_profile_write_file().
+ *
+ * \c Name is not copied, so it must remain valid.  Passing NULL resets the
+ * filename logic to the default behaviour.
+ */
+void __llvm_profile_set_filename(const char *Name);
+
+/*! \brief Register to write instrumentation data to file at exit. */
+int __llvm_profile_register_write_file_atexit(void);
+
+/*! \brief Get the magic token for the file format. */
+uint64_t __llvm_profile_get_magic(void);
+
+/*! \brief Get the version of the file format. */
+uint64_t __llvm_profile_get_version(void);
+
+#endif /* PROFILE_INSTRPROFILING_H__ */
diff --git a/compiler-rt/lib/profile/InstrProfilingBuffer.c b/compiler-rt/lib/profile/InstrProfilingBuffer.c
new file mode 100644
index 0000000..7b662ca
--- /dev/null
+++ b/compiler-rt/lib/profile/InstrProfilingBuffer.c
@@ -0,0 +1,54 @@
+/*===- InstrProfilingBuffer.c - Write instrumentation to a memory buffer --===*\
+|*
+|*                     The LLVM Compiler Infrastructure
+|*
+|* This file is distributed under the University of Illinois Open Source
+|* License. See LICENSE.TXT for details.
+|*
+\*===----------------------------------------------------------------------===*/
+
+#include "InstrProfiling.h"
+#include <string.h>
+
+/* TODO: uint64_t __llvm_profile_get_size_for_buffer(void) */
+
+int __llvm_profile_write_buffer(FILE *OutputFile) {
+  const __llvm_profile_data *DataBegin = __llvm_profile_data_begin();
+  const __llvm_profile_data *DataEnd = __llvm_profile_data_end();
+  const uint64_t *CountersBegin = __llvm_profile_counters_begin();
+  const uint64_t *CountersEnd   = __llvm_profile_counters_end();
+  const char *NamesBegin = __llvm_profile_names_begin();
+  const char *NamesEnd   = __llvm_profile_names_end();
+
+  /* Calculate size of sections. */
+  const uint64_t DataSize = DataEnd - DataBegin;
+  const uint64_t CountersSize = CountersEnd - CountersBegin;
+  const uint64_t NamesSize = NamesEnd - NamesBegin;
+
+  /* Get rest of header data. */
+  const uint64_t Magic = __llvm_profile_get_magic();
+  const uint64_t Version = __llvm_profile_get_version();
+  const uint64_t CountersDelta = (uint64_t)CountersBegin;
+  const uint64_t NamesDelta = (uint64_t)NamesBegin;
+
+#define CHECK_fwrite(Data, Size, Length, File) \
+  do { if (fwrite(Data, Size, Length, File) != Length) return -1; } while (0)
+
+  /* Write the header. */
+  CHECK_fwrite(&Magic,         sizeof(uint64_t), 1, OutputFile);
+  CHECK_fwrite(&Version,       sizeof(uint64_t), 1, OutputFile);
+  CHECK_fwrite(&DataSize,      sizeof(uint64_t), 1, OutputFile);
+  CHECK_fwrite(&CountersSize,  sizeof(uint64_t), 1, OutputFile);
+  CHECK_fwrite(&NamesSize,     sizeof(uint64_t), 1, OutputFile);
+  CHECK_fwrite(&CountersDelta, sizeof(uint64_t), 1, OutputFile);
+  CHECK_fwrite(&NamesDelta,    sizeof(uint64_t), 1, OutputFile);
+
+  /* Write the data. */
+  CHECK_fwrite(DataBegin, sizeof(__llvm_profile_data), DataSize, OutputFile);
+  CHECK_fwrite(CountersBegin, sizeof(uint64_t), CountersSize, OutputFile);
+  CHECK_fwrite(NamesBegin, sizeof(char), NamesSize, OutputFile);
+
+#undef CHECK_fwrite
+
+   return 0;
+}
diff --git a/compiler-rt/lib/profile/InstrProfilingExtras.h b/compiler-rt/lib/profile/InstrProfilingExtras.h
deleted file mode 100644
index 1ab90cc..0000000
--- a/compiler-rt/lib/profile/InstrProfilingExtras.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*===- InstrProfilingExtras.h - Support library for PGO instrumentation ---===*\
-|*
-|*                     The LLVM Compiler Infrastructure
-|*
-|* This file is distributed under the University of Illinois Open Source
-|* License. See LICENSE.TXT for details.
-|*
-\*===----------------------------------------------------------------------===*/
-
-/*!
- * \brief Write instrumentation data to the current file.
- *
- * Writes to the file with the last name given to \a __llvm_profile_set_filename(),
- * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
- * or if that's not set, \c "default.profdata".
- */
-int __llvm_profile_write_file(void);
-
-/*!
- * \brief Set the filename for writing instrumentation data.
- *
- * Sets the filename to be used for subsequent calls to
- * \a __llvm_profile_write_file().
- *
- * \c Name is not copied, so it must remain valid.  Passing NULL resets the
- * filename logic to the default behaviour.
- */
-void __llvm_profile_set_filename(const char *Name);
-
-/*! \brief Register to write instrumentation data to file at exit. */
-int __llvm_profile_register_write_file_atexit(void);
diff --git a/compiler-rt/lib/profile/InstrProfilingExtras.c b/compiler-rt/lib/profile/InstrProfilingFile.c
similarity index 71%
rename from compiler-rt/lib/profile/InstrProfilingExtras.c
rename to compiler-rt/lib/profile/InstrProfilingFile.c
index 723952f..505d748 100644
--- a/compiler-rt/lib/profile/InstrProfilingExtras.c
+++ b/compiler-rt/lib/profile/InstrProfilingFile.c
@@ -1,4 +1,4 @@
-/*===- InstrProfilingExtras.c - Support library for PGO instrumentation ---===*\
+/*===- InstrProfilingFile.c - Write instrumentation to a file -------------===*\
 |*
 |*                     The LLVM Compiler Infrastructure
 |*
@@ -8,9 +8,11 @@
 \*===----------------------------------------------------------------------===*/
 
 #include "InstrProfiling.h"
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
-static int __llvm_profile_write_file_with_name(const char *OutputName) {
+static int writeFileWithName(const char *OutputName) {
   int RetVal;
   FILE *OutputFile;
   if (!OutputName || !OutputName[0])
@@ -19,9 +21,6 @@
   if (!OutputFile)
     return -1;
 
-  /* TODO: mmap file to buffer of size __llvm_profile_get_size_for_buffer() and
-   * pass the buffer in, instead of the file.
-   */
   RetVal = __llvm_profile_write_buffer(OutputFile);
 
   fclose(OutputFile);
@@ -44,7 +43,7 @@
   int PidLength = 0;
   int NumPids = 0;
 
-  // Get the filename.
+  /* Get the filename. */
   const char *Filename = CurrentFilename;
 #define UPDATE_FILENAME(NextFilename) \
   if (!Filename || !Filename[0]) Filename = NextFilename
@@ -52,7 +51,7 @@
   UPDATE_FILENAME("default.profdata");
 #undef UPDATE_FILENAME
 
-  // Check the filename for "%p", which indicates a pid-substitution.
+  /* Check the filename for "%p", which indicates a pid-substitution. */
   for (I = 0; Filename[I]; ++I)
     if (Filename[I] == '%' && Filename[++I] == 'p')
       if (!NumPids++) {
@@ -61,31 +60,31 @@
           return -1;
       }
   if (NumPids) {
-    // Allocate enough space for the substituted filename.
+    /* Allocate enough space for the substituted filename. */
     AllocatedFilename = (char*)malloc(I + NumPids*(PidLength - 2) + 1);
     if (!AllocatedFilename)
       return -1;
 
-    // Construct the new filename.
+    /* Construct the new filename. */
     for (I = 0, J = 0; Filename[I]; ++I)
       if (Filename[I] == '%') {
         if (Filename[++I] == 'p') {
           memcpy(AllocatedFilename + J, PidChars, PidLength);
           J += PidLength;
         }
-        // Drop any unknown substitutions.
+        /* Drop any unknown substitutions. */
       } else
         AllocatedFilename[J++] = Filename[I];
     AllocatedFilename[J] = 0;
 
-    // Actually use the computed name.
+    /* Actually use the computed name. */
     Filename = AllocatedFilename;
   }
 
-  // Write the file.
-  RetVal = __llvm_profile_write_file_with_name(Filename);
+  /* Write the file. */
+  RetVal = writeFileWithName(Filename);
 
-  // Free the filename.
+  /* Free the filename. */
   if (AllocatedFilename)
     free(AllocatedFilename);
 
@@ -95,11 +94,13 @@
 static void writeFileWithoutReturn(void) {
   __llvm_profile_write_file();
 }
-void __llvm_profile_register_write_file_atexit(void) {
+
+int __llvm_profile_register_write_file_atexit(void) {
   static int HasBeenRegistered = 0;
 
-  if (!HasBeenRegistered) {
-    HasBeenRegistered = 1;
-    atexit(writeFileWithoutReturn);
-  }
+  if (HasBeenRegistered)
+    return 0;
+
+  HasBeenRegistered = 1;
+  return atexit(writeFileWithoutReturn);
 }
diff --git a/compiler-rt/lib/profile/InstrProfilingPlatformDarwin.c b/compiler-rt/lib/profile/InstrProfilingPlatformDarwin.c
index fdb1d66..1ba672d 100644
--- a/compiler-rt/lib/profile/InstrProfilingPlatformDarwin.c
+++ b/compiler-rt/lib/profile/InstrProfilingPlatformDarwin.c
@@ -1,4 +1,4 @@
-/*===- InstrProfilingDarwin.c - Profile data on Darwin --------------------===*\
+/*===- InstrProfilingPlatformDarwin.c - Profile data on Darwin ------------===*\
 |*
 |*                     The LLVM Compiler Infrastructure
 |*
diff --git a/compiler-rt/lib/profile/InstrProfilingPlatformOther.c b/compiler-rt/lib/profile/InstrProfilingPlatformOther.c
index da24f59f..69b5925 100644
--- a/compiler-rt/lib/profile/InstrProfilingPlatformOther.c
+++ b/compiler-rt/lib/profile/InstrProfilingPlatformOther.c
@@ -1,4 +1,4 @@
-/*===- InstrProfilingDefault.c - Profile data default platfrom ------------===*\
+/*===- InstrProfilingPlatformOther.c - Profile data default platfrom ------===*\
 |*
 |*                     The LLVM Compiler Infrastructure
 |*
@@ -8,6 +8,7 @@
 \*===----------------------------------------------------------------------===*/
 
 #include "InstrProfiling.h"
+#include <stdlib.h>
 
 static const __llvm_profile_data *DataFirst = NULL;
 static const __llvm_profile_data *DataLast = NULL;
diff --git a/compiler-rt/lib/profile/InstrProfilingRuntime.cc b/compiler-rt/lib/profile/InstrProfilingRuntime.cc
index 6116b0c..709b96c 100644
--- a/compiler-rt/lib/profile/InstrProfilingRuntime.cc
+++ b/compiler-rt/lib/profile/InstrProfilingRuntime.cc
@@ -9,7 +9,7 @@
 
 extern "C" {
 
-#include "InstrProfilingExtras.h"
+#include "InstrProfiling.h"
 
 extern int __llvm_profile_runtime;
 int __llvm_profile_runtime;
diff --git a/compiler-rt/make/platform/clang_darwin.mk b/compiler-rt/make/platform/clang_darwin.mk
index 2fdf051..32803af 100644
--- a/compiler-rt/make/platform/clang_darwin.mk
+++ b/compiler-rt/make/platform/clang_darwin.mk
@@ -222,9 +222,9 @@
 
 FUNCTIONS.osx	:= mulosi4 mulodi4 muloti4
 
-FUNCTIONS.profile_osx := GCDAProfiling InstrProfiling \
-                         InstrProfilingPlatformDarwin InstrProfilingRuntime \
-                         InstrProfilingExtras
+FUNCTIONS.profile_osx := GCDAProfiling InstrProfiling InstrProfilingBuffer \
+                         InstrProfilingFile InstrProfilingPlatformDarwin \
+                         InstrProfilingRuntime
 FUNCTIONS.profile_ios := $(FUNCTIONS.profile_osx)
 
 FUNCTIONS.asan_osx_dynamic := $(AsanFunctions) $(InterceptionFunctions) \