Rename everything to follow LLVM style ... I think.

Add support for switch and indirectbr edges. This works by densely numbering
all blocks which have such terminators, and then separately numbering the
possible successors. The predecessors write down a number, the successor knows
its own number (as a ConstantInt) and sends that and the pointer to the number
the predecessor wrote down to the runtime, who looks up the counter in a
per-function table.

Coverage data should now be functional, but I haven't tested it on anything
other than my 2-file synthetic test program for coverage.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@130186 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/runtime/libprofile/GCDAProfiling.c b/runtime/libprofile/GCDAProfiling.c
index 0bd445c..c8161d4 100644
--- a/runtime/libprofile/GCDAProfiling.c
+++ b/runtime/libprofile/GCDAProfiling.c
@@ -15,6 +15,9 @@
 |* are only close enough that LCOV will happily parse them. Anything that lcov
 |* ignores is missing.
 |*
+|* TODO: gcov is multi-process safe by having each exit open the existing file
+|* and append to it. We'd like to achieve that and be thread-safe too.
+|*
 \*===----------------------------------------------------------------------===*/
 
 #include "llvm/Support/DataTypes.h"
@@ -58,13 +61,33 @@
   fwrite("adcg*404MVLL", 12, 1, output_file);
 
 #ifdef DEBUG_GCDAPROFILING
-  printf("[%s]\n", filename);
+  printf("llvmgcda: [%s]\n", filename);
+#endif
+}
+
+/* Given an array of pointers to counters (counters), increment the n-th one,
+ * where we're also given a pointer to n (predecessor).
+ */
+void llvm_gcda_increment_indirect_counter(uint32_t *predecessor,
+                                          uint64_t **counters) {
+  uint64_t *counter;
+  if (*predecessor == 0xffffffff)
+    return;
+
+  /* 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]))
+    ++*counter;
+#ifdef DEBUG_GCDAPROFILING
+  else
+    printf("llvmgcda: increment_indirect_counter counters=%x, pred=%u\n",
+           state_table_row, *predecessor);
 #endif
 }
 
 void llvm_gcda_emit_function(uint32_t ident) {
 #ifdef DEBUG_GCDAPROFILING
-  printf("function id=%x\n", ident);
+  printf("llvmgcda: function id=%x\n", ident);
 #endif
 
   /* function tag */  
@@ -84,9 +107,9 @@
   }
 
 #ifdef DEBUG_GCDAPROFILING
-  printf("  %u arcs\n", num_counters);
+  printf("llvmgcda:   %u arcs\n", num_counters);
   for (i = 0; i < num_counters; ++i) {
-    printf("  %llu\n", (unsigned long long)counters[i]);
+    printf("llvmgcda:   %llu\n", (unsigned long long)counters[i]);
   }
 #endif
 }
@@ -98,6 +121,6 @@
   output_file = NULL;
 
 #ifdef DEBUG_GCDAPROFILING
-  printf("-----\n");
+  printf("llvmgcda: -----\n");
 #endif
 }