[llvm-commits] CVS: llvm/lib/Analysis/ProfileInfoLoader.cpp

Chris Lattner lattner at cs.uiuc.edu
Mon Mar 8 14:05:02 PST 2004


Changes in directory llvm/lib/Analysis:

ProfileInfoLoader.cpp updated: 1.3 -> 1.4

---
Log message:

If we have edge counts, we can produce block counts.  I've verified that
using an edge profile to produce block counts gives the exact same numbers
as using a block count directly.


---
Diffs of the changes:  (+67 -11)

Index: llvm/lib/Analysis/ProfileInfoLoader.cpp
diff -u llvm/lib/Analysis/ProfileInfoLoader.cpp:1.3 llvm/lib/Analysis/ProfileInfoLoader.cpp:1.4
--- llvm/lib/Analysis/ProfileInfoLoader.cpp:1.3	Mon Mar  8 12:20:18 2004
+++ llvm/lib/Analysis/ProfileInfoLoader.cpp	Mon Mar  8 14:03:52 2004
@@ -16,6 +16,7 @@
 #include "llvm/Module.h"
 #include "llvm/InstrTypes.h"
 #include <cstdio>
+#include <map>
 using namespace llvm;
 
 enum ProfilingType {
@@ -145,15 +146,19 @@
 void ProfileInfoLoader::getFunctionCounts(std::vector<std::pair<Function*,
                                                       unsigned> > &Counts) {
   if (FunctionCounts.empty()) {
-    // Synthesize function frequency information from the number of times their
-    // entry blocks were executed.
-    std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
-    getBlockCounts(BlockCounts);
-
-    for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
-      if (&BlockCounts[i].first->getParent()->front() == BlockCounts[i].first)
-        Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
-                                        BlockCounts[i].second));
+    if (hasAccurateBlockCounts()) {
+      // Synthesize function frequency information from the number of times
+      // their entry blocks were executed.
+      std::vector<std::pair<BasicBlock*, unsigned> > BlockCounts;
+      getBlockCounts(BlockCounts);
+      
+      for (unsigned i = 0, e = BlockCounts.size(); i != e; ++i)
+        if (&BlockCounts[i].first->getParent()->front() == BlockCounts[i].first)
+          Counts.push_back(std::make_pair(BlockCounts[i].first->getParent(),
+                                          BlockCounts[i].second));
+    } else {
+      std::cerr << "Function counts are not available!\n";
+    }
     return;
   }
   
@@ -171,8 +176,59 @@
 void ProfileInfoLoader::getBlockCounts(std::vector<std::pair<BasicBlock*,
                                                          unsigned> > &Counts) {
   if (BlockCounts.empty()) {
-    std::cerr << "Block counts not available, and no synthesis "
-              << "is implemented yet!\n";
+    if (hasAccurateEdgeCounts()) {
+      // Synthesize block count information from edge frequency information.
+      // The block execution frequency is equal to the sum of the execution
+      // frequency of all outgoing edges from a block.
+      //
+      // If a block has no successors, this will not be correct, so we have to
+      // special case it. :(
+      std::vector<std::pair<Edge, unsigned> > EdgeCounts;
+      getEdgeCounts(EdgeCounts);
+
+      std::map<BasicBlock*, unsigned> InEdgeFreqs;
+
+      BasicBlock *LastBlock = 0;
+      TerminatorInst *TI = 0;
+      for (unsigned i = 0, e = EdgeCounts.size(); i != e; ++i) {
+        if (EdgeCounts[i].first.first != LastBlock) {
+          LastBlock = EdgeCounts[i].first.first;
+          TI = LastBlock->getTerminator();
+          Counts.push_back(std::make_pair(LastBlock, 0));
+        }
+        Counts.back().second += EdgeCounts[i].second;
+        unsigned SuccNum = EdgeCounts[i].first.second;
+        if (SuccNum >= TI->getNumSuccessors()) {
+          static bool Warned = false;
+          if (!Warned) {
+            std::cerr << "WARNING: profile info doesn't seem to match"
+                      << " the program!\n";
+            Warned = true;
+          }
+        } else {
+          // If this successor has no successors of its own, we will never
+          // compute an execution count for that block.  Remember the incoming
+          // edge frequencies to add later.
+          BasicBlock *Succ = TI->getSuccessor(SuccNum);
+          if (Succ->getTerminator()->getNumSuccessors() == 0)
+            InEdgeFreqs[Succ] += EdgeCounts[i].second;
+        }
+      }
+
+      // Now we have to accumulate information for those blocks without
+      // successors into our table.
+      for (std::map<BasicBlock*, unsigned>::iterator I = InEdgeFreqs.begin(),
+             E = InEdgeFreqs.end(); I != E; ++I) {
+        unsigned i = 0;
+        for (; i != Counts.size() && Counts[i].first != I->first; ++i)
+          /*empty*/;
+        if (i == Counts.size()) Counts.push_back(std::make_pair(I->first, 0));
+        Counts[i].second += I->second;
+      }
+
+    } else {
+      std::cerr << "Block counts are not available!\n";
+    }
     return;
   }
 





More information about the llvm-commits mailing list