New testcase that is not actually handled yet, but should be in the future.
[oota-llvm.git] / tools / llvm-prof / llvm-prof.cpp
index 5911918b161b4d99c151406d399deaa8b7d85be1..438fecf07a4a7ca5c5212f82b729a58be4f24f58 100644 (file)
@@ -23,6 +23,8 @@
 #include <map>
 #include <set>
 
+using namespace llvm;
+
 namespace {
   cl::opt<std::string> 
   BytecodeFile(cl::Positional, cl::desc("<program bytecode file>"),
@@ -37,6 +39,9 @@ namespace {
                      cl::desc("Print LLVM code with frequency annotations"));
   cl::alias PrintAnnotated2("A", cl::desc("Alias for --annotated-llvm"),
                             cl::aliasopt(PrintAnnotatedLLVM));
+  cl::opt<bool>
+  PrintAllCode("print-all-code",
+               cl::desc("Print annotated code for the entire program"));
 }
 
 // PairSecondSort - A sorting predicate to sort by the second element of a pair.
@@ -104,7 +109,7 @@ int main(int argc, char **argv) {
   std::sort(FunctionCounts.begin(), FunctionCounts.end(),
             PairSecondSortReverse<Function*>());
 
-  unsigned TotalExecutions = 0;
+  unsigned long long TotalExecutions = 0;
   for (unsigned i = 0, e = FunctionCounts.size(); i != e; ++i)
     TotalExecutions += FunctionCounts[i].second;
   
@@ -131,7 +136,7 @@ int main(int argc, char **argv) {
       break;
     }
 
-    printf("%3d. %5d/%d %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
+    printf("%3d. %5u/%llu %s\n", i+1, FunctionCounts[i].second, TotalExecutions,
            FunctionCounts[i].first->getName().c_str());
   }
 
@@ -155,12 +160,15 @@ int main(int argc, char **argv) {
     std::cout << "Top 20 most frequently executed basic blocks:\n\n";
 
     // Print out the function frequencies...
-    printf(" ##   Frequency\n");
+    printf(" ##      %%%% \tFrequency\n");
     unsigned BlocksToPrint = Counts.size();
     if (BlocksToPrint > 20) BlocksToPrint = 20;
     for (unsigned i = 0; i != BlocksToPrint; ++i) {
+      if (Counts[i].second == 0) break;
       Function *F = Counts[i].first->getParent();
-      printf("%3d. %5d/%d %s() - %s\n", i+1, Counts[i].second, TotalExecutions,
+      printf("%3d. %5.2f%% %5u/%llu\t%s() - %s\n", i+1,
+             Counts[i].second/(double)TotalExecutions*100,
+             Counts[i].second, TotalExecutions,
              F->getName().c_str(), Counts[i].first->getName().c_str());
       FunctionsToPrint.insert(F);
     }
@@ -168,19 +176,19 @@ int main(int argc, char **argv) {
     BlockFreqs.insert(Counts.begin(), Counts.end());
   }
   
-  if (PrintAnnotatedLLVM) {
+  if (PrintAnnotatedLLVM || PrintAllCode) {
     std::cout << "\n===" << std::string(73, '-') << "===\n";
     std::cout << "Annotated LLVM code for the module:\n\n";
     
-    if (FunctionsToPrint.empty())
-      for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
-        FunctionsToPrint.insert(I);
-    
     ProfileAnnotator PA(FuncFreqs, BlockFreqs);
 
-    for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
-           E = FunctionsToPrint.end(); I != E; ++I)
-      (*I)->print(std::cout, &PA);
+    if (FunctionsToPrint.empty() || PrintAllCode)
+      M->print(std::cout, &PA);
+    else
+      // Print just a subset of the functions...
+      for (std::set<Function*>::iterator I = FunctionsToPrint.begin(),
+             E = FunctionsToPrint.end(); I != E; ++I)
+        (*I)->print(std::cout, &PA);
   }
 
   return 0;