[llvm-commits] [llvm] r129905 - /llvm/trunk/lib/Transforms/Instrumentation/GCOVProfiling.cpp

Nick Lewycky nicholas at mxc.ca
Wed Apr 20 20:18:00 PDT 2011


Author: nicholas
Date: Wed Apr 20 22:18:00 2011
New Revision: 129905

URL: http://llvm.org/viewvc/llvm-project?rev=129905&view=rev
Log:
In gcov profiling, give all functions an extra unified return block. This is
necessary since gcov counts transitions between blocks. It can't see if you've
run every line in a straight-line function, so we add an edge for it to notice.

Modified:
    llvm/trunk/lib/Transforms/Instrumentation/GCOVProfiling.cpp

Modified: llvm/trunk/lib/Transforms/Instrumentation/GCOVProfiling.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/GCOVProfiling.cpp?rev=129905&r1=129904&r2=129905&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/GCOVProfiling.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/GCOVProfiling.cpp Wed Apr 20 22:18:00 2011
@@ -244,6 +244,7 @@
       for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
         blocks[BB] = new GCOVBlock(i++, os);
       }
+      return_block = new GCOVBlock(i++, os);
 
       WriteBytes(function_tag, 4);
       uint32_t block_len = 1 + 1 + 1 + LengthOfGCOVString(SP.getName()) +
@@ -259,17 +260,22 @@
 
     ~GCOVFunction() {
       DeleteContainerSeconds(blocks);
+      delete return_block;
     }
 
     GCOVBlock &GetBlock(BasicBlock *BB) {
       return *blocks[BB];
     }
 
+    GCOVBlock &GetReturnBlock() {
+      return *return_block;
+    }
+
     void WriteOut() {
       // Emit count of blocks.
       WriteBytes(block_tag, 4);
-      Write(blocks.size());
-      for (int i = 0, e = blocks.size(); i != e; ++i) {
+      Write(blocks.size() + 1);
+      for (int i = 0, e = blocks.size() + 1; i != e; ++i) {
         Write(0);  // No flags on our blocks.
       }
 
@@ -297,6 +303,7 @@
 
    private:
     DenseMap<BasicBlock *, GCOVBlock *> blocks;
+    GCOVBlock *return_block;
   };
 }
 
@@ -347,6 +354,8 @@
         for (int i = 0; i != successors; ++i) {
           block.AddEdge(function.GetBlock(TI->getSuccessor(i)));
         }
+      } else if (isa<ReturnInst>(TI)) {
+        block.AddEdge(function.GetReturnBlock());
       }
 
       uint32_t line = 0;
@@ -383,11 +392,13 @@
     DISubprogram SP(*SPI);
     Function *F = SP.getFunction();
 
-    // TODO: GCOV format requires a distinct unified exit block.
     unsigned edges = 0;
     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
       TerminatorInst *TI = BB->getTerminator();
-      edges += TI->getNumSuccessors();
+      if (isa<ReturnInst>(TI))
+        ++edges;
+      else
+        edges += TI->getNumSuccessors();
     }
 
     const ArrayType *counter_type =
@@ -406,7 +417,8 @@
     unsigned edge_num = 0;
     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
       TerminatorInst *TI = BB->getTerminator();
-      if (int successors = TI->getNumSuccessors()) {
+      int successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
+      if (successors) {
         IRBuilder<> builder(TI);
 
         if (successors == 1) {





More information about the llvm-commits mailing list