[PGO] Allow input value node list to be null
[oota-llvm.git] / include / llvm / ProfileData / InstrProfData.inc
index 656ceae34a20184dcd151a1bc1aa9c92459dbc75..aefdbc1b3e4710d9024590f622f1a5d9cacef9d2 100644 (file)
@@ -291,19 +291,20 @@ typedef struct ValueProfData {
    */
   void deserializeTo(InstrProfRecord &Record,
                      InstrProfRecord::ValueMapType *VMap);
    */
   void deserializeTo(InstrProfRecord &Record,
                      InstrProfRecord::ValueMapType *VMap);
+  void operator delete(void *ptr) { ::operator delete(ptr); }
 #endif
 } ValueProfData;
 
 /* 
  * The closure is designed to abstact away two types of value profile data:
 #endif
 } ValueProfData;
 
 /* 
  * The closure is designed to abstact away two types of value profile data:
- *  - InstrProfRecord which is the primary data structure used to
- *    represent profile data in host tools (reader, writer, and profile-use)
+ * - InstrProfRecord which is the primary data structure used to
+ *   represent profile data in host tools (reader, writer, and profile-use)
  * - value profile runtime data structure suitable to be used by C
  * - value profile runtime data structure suitable to be used by C
- *    runtime library.
+ *   runtime library.
  *
  * Both sources of data need to serialize to disk/memory-buffer in common
  * format: ValueProfData. The abstraction allows compiler-rt's raw profiler
  *
  * Both sources of data need to serialize to disk/memory-buffer in common
  * format: ValueProfData. The abstraction allows compiler-rt's raw profiler
- * writer to share * the same code with indexed profile writer.
+ * writer to share the same format and code with indexed profile writer.
  *
  * For documentation of the member methods below, refer to corresponding methods
  * in class InstrProfRecord.
  *
  * For documentation of the member methods below, refer to corresponding methods
  * in class InstrProfRecord.
@@ -337,7 +338,7 @@ typedef struct ValueProfRecordClosure {
  */
 typedef struct ValueProfRuntimeRecord {
   /* Number of sites for each value profile kind.  */
  */
 typedef struct ValueProfRuntimeRecord {
   /* Number of sites for each value profile kind.  */
-  uint16_t *NumValueSites;
+  const uint16_t *NumValueSites;
   /* An array of linked-list headers. The size of of the array is the
    * total number of value profile sites : sum(NumValueSites[*])). Each
    * linked-list stores the values profiled for a value profile site. */
   /* An array of linked-list headers. The size of of the array is the
    * total number of value profile sites : sum(NumValueSites[*])). Each
    * linked-list stores the values profiled for a value profile site. */
@@ -352,6 +353,16 @@ typedef struct ValueProfRuntimeRecord {
   ValueProfNode **NodesKind[IPVK_Last + 1];
 } ValueProfRuntimeRecord;
 
   ValueProfNode **NodesKind[IPVK_Last + 1];
 } ValueProfRuntimeRecord;
 
+/* Forward declarations of C interfaces.  */
+int initializeValueProfRuntimeRecord(ValueProfRuntimeRecord *RuntimeRecord,
+                                     const uint16_t *NumValueSites,
+                                     ValueProfNode **Nodes);
+void finalizeValueProfRuntimeRecord(ValueProfRuntimeRecord *RuntimeRecord);
+uint32_t getValueProfDataSizeRT(const ValueProfRuntimeRecord *Record);
+ValueProfData *
+serializeValueProfDataFromRT(const ValueProfRuntimeRecord *Record,
+                             ValueProfData *Dst);
+
 #undef INSTR_PROF_VALUE_PROF_DATA
 #endif  /* INSTR_PROF_VALUE_PROF_DATA */ 
 
 #undef INSTR_PROF_VALUE_PROF_DATA
 #endif  /* INSTR_PROF_VALUE_PROF_DATA */ 
 
@@ -506,13 +517,15 @@ ValueProfData *serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
 
 /* 
  * The value profiler runtime library stores the value profile data
 
 /* 
  * The value profiler runtime library stores the value profile data
- * for a given function in NumValueSites and Nodes. This is the
- * method to initialize the RuntimeRecord with the runtime data to
- * pre-compute the information needed to efficiently implement
- * ValueProfRecordClosure's callback interfaces.
+ * for a given function in \c NumValueSites and \c Nodes structures.
+ * \c ValueProfRuntimeRecord class is used to encapsulate the runtime
+ * profile data and provides fast interfaces to retrieve the profile
+ * information. This interface is used to initialize the runtime record
+ * and pre-compute the information needed for efficient implementation
+ * of callbacks required by ValueProfRecordClosure class.
  */
 int initializeValueProfRuntimeRecord(ValueProfRuntimeRecord *RuntimeRecord,
  */
 int initializeValueProfRuntimeRecord(ValueProfRuntimeRecord *RuntimeRecord,
-                                     uint16_t *NumValueSites,
+                                     const uint16_t *NumValueSites,
                                      ValueProfNode **Nodes) {
   unsigned I, J, S = 0, NumValueKinds = 0;
   RuntimeRecord->NumValueSites = NumValueSites;
                                      ValueProfNode **Nodes) {
   unsigned I, J, S = 0, NumValueKinds = 0;
   RuntimeRecord->NumValueSites = NumValueSites;
@@ -525,12 +538,13 @@ int initializeValueProfRuntimeRecord(ValueProfRuntimeRecord *RuntimeRecord,
     }
     NumValueKinds++;
     RuntimeRecord->SiteCountArray[I] = (uint8_t *)calloc(N, 1);
     }
     NumValueKinds++;
     RuntimeRecord->SiteCountArray[I] = (uint8_t *)calloc(N, 1);
-    RuntimeRecord->NodesKind[I] = &RuntimeRecord->Nodes[S];
-    if (!RuntimeRecord->NodesKind[I])
+    if (!RuntimeRecord->SiteCountArray[I])
       return 1;
       return 1;
+    RuntimeRecord->NodesKind[I] = Nodes ? &Nodes[S] : NULL;
     for (J = 0; J < N; J++) {
     for (J = 0; J < N; J++) {
+      /* Compute value count for each site. */
       uint32_t C = 0;
       uint32_t C = 0;
-      ValueProfNode *Site = RuntimeRecord->Nodes[S + J];
+      ValueProfNode *Site = Nodes ? RuntimeRecord->NodesKind[I][J] : NULL;
       while (Site) {
         C++;
         Site = Site->Next;
       while (Site) {
         C++;
         Site = Site->Next;
@@ -583,6 +597,8 @@ void getValueForSiteRT(const void *R, InstrProfValueData *Dst, uint32_t VK,
   unsigned I, N = 0;
   const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R;
   N = getNumValueDataForSiteRT(R, VK, S);
   unsigned I, N = 0;
   const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R;
   N = getNumValueDataForSiteRT(R, VK, S);
+  if (N == 0)
+    return;
   ValueProfNode *VNode = Record->NodesKind[VK][S];
   for (I = 0; I < N; I++) {
     Dst[I] = VNode->VData;
   ValueProfNode *VNode = Record->NodesKind[VK][S];
   for (I = 0; I < N; I++) {
     Dst[I] = VNode->VData;