Only read *predecessor once so as to fix a theoretical issue where it changes
between two reads (threading).

Fix an off-by-one in the indirect counter table that I meant to revert after an
earlier experiment. Whoops!

Implement GCOV_PREFIX. Doesn't handle GCOV_PREFIX_STRIP yet.

Fix an off-by-one in string emission. Extra whoops!

Tolerate DISubprograms that have null Function*'s attached to them. I don't yet
understand what this means, but it happens when you have a global static with
a non-trivial constructor/destructor.

Fix a crash on switch statements with a single successor (default-only).


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@130443 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/runtime/libprofile/GCDAProfiling.c b/runtime/libprofile/GCDAProfiling.c
index c8161d4..2dcf22d 100644
--- a/runtime/libprofile/GCDAProfiling.c
+++ b/runtime/libprofile/GCDAProfiling.c
@@ -46,6 +46,24 @@
   write_int32(hi);
 }
 
+static char *mangle_filename(const char *orig_filename) {
+  /* TODO: handle GCOV_PREFIX_STRIP */
+  const char *prefix;
+  char *filename = 0;
+
+  prefix = getenv("GCOV_PREFIX");
+
+  if (!prefix)
+    return strdup(filename);
+
+  filename = malloc(strlen(prefix) + 1 + strlen(orig_filename) + 1);
+  strcpy(filename, prefix);
+  strcat(filename, "/");
+  strcat(filename, orig_filename);
+
+  return filename;
+}
+
 /*
  * --- LLVM line counter API ---
  */
@@ -54,15 +72,19 @@
  * profiling enabled will emit to a different file. Only one file may be
  * started at a time.
  */
-void llvm_gcda_start_file(const char *filename) {
-  output_file = fopen(filename, "w+");
+void llvm_gcda_start_file(const char *orig_filename) {
+  char *filename;
+  filename = mangle_filename(orig_filename);
+  output_file = fopen(filename, "wb");
 
   /* gcda file, version 404*, stamp LLVM. */
   fwrite("adcg*404MVLL", 12, 1, output_file);
 
 #ifdef DEBUG_GCDAPROFILING
-  printf("llvmgcda: [%s]\n", filename);
+  printf("llvmgcda: [%s]\n", orig_filename);
 #endif
+
+  free(filename);
 }
 
 /* Given an array of pointers to counters (counters), increment the n-th one,
@@ -71,12 +93,16 @@
 void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
                                           uint64_t **counters) {
   uint64_t *counter;
-  if (*predecessor == 0xffffffff)
+  uint32_t pred;
+
+  pred = *predecessor;
+  if (pred == 0xffffffff)
     return;
+  counter = counters[pred];
 
   /* Don't crash if the pred# is out of sync. This can happen due to threads,
      or because of a TODO in GCOVProfiling.cpp buildEdgeLookupTable(). */
-  if ((counter = counters[*predecessor]))
+  if (counter)
     ++*counter;
 #ifdef DEBUG_GCDAPROFILING
   else