1<html lang="en"> 2<head> 3<title>Gcov and Optimization - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Gcov.html#Gcov" title="Gcov"> 9<link rel="prev" href="Invoking-Gcov.html#Invoking-Gcov" title="Invoking Gcov"> 10<link rel="next" href="Gcov-Data-Files.html#Gcov-Data-Files" title="Gcov Data Files"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Gcov-and-Optimization"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Gcov-Data-Files.html#Gcov-Data-Files">Gcov Data Files</a>, 53Previous: <a rel="previous" accesskey="p" href="Invoking-Gcov.html#Invoking-Gcov">Invoking Gcov</a>, 54Up: <a rel="up" accesskey="u" href="Gcov.html#Gcov">Gcov</a> 55<hr> 56</div> 57 58<h3 class="section">10.3 Using <samp><span class="command">gcov</span></samp> with GCC Optimization</h3> 59 60<p>If you plan to use <samp><span class="command">gcov</span></samp> to help optimize your code, you must 61first compile your program with two special GCC options: 62‘<samp><span class="samp">-fprofile-arcs -ftest-coverage</span></samp>’. Aside from that, you can use any 63other GCC options; but if you want to prove that every single line 64in your program was executed, you should not compile with optimization 65at the same time. On some machines the optimizer can eliminate some 66simple code lines by combining them with other lines. For example, code 67like this: 68 69<pre class="smallexample"> if (a != b) 70 c = 1; 71 else 72 c = 0; 73</pre> 74 <p class="noindent">can be compiled into one instruction on some machines. In this case, 75there is no way for <samp><span class="command">gcov</span></samp> to calculate separate execution counts 76for each line because there isn't separate code for each line. Hence 77the <samp><span class="command">gcov</span></samp> output looks like this if you compiled the program with 78optimization: 79 80<pre class="smallexample"> 100: 12:if (a != b) 81 100: 13: c = 1; 82 100: 14:else 83 100: 15: c = 0; 84</pre> 85 <p>The output shows that this block of code, combined by optimization, 86executed 100 times. In one sense this result is correct, because there 87was only one instruction representing all four of these lines. However, 88the output does not indicate how many times the result was 0 and how 89many times the result was 1. 90 91 <p>Inlineable functions can create unexpected line counts. Line counts are 92shown for the source code of the inlineable function, but what is shown 93depends on where the function is inlined, or if it is not inlined at all. 94 95 <p>If the function is not inlined, the compiler must emit an out of line 96copy of the function, in any object file that needs it. If 97<samp><span class="file">fileA.o</span></samp> and <samp><span class="file">fileB.o</span></samp> both contain out of line bodies of a 98particular inlineable function, they will also both contain coverage 99counts for that function. When <samp><span class="file">fileA.o</span></samp> and <samp><span class="file">fileB.o</span></samp> are 100linked together, the linker will, on many systems, select one of those 101out of line bodies for all calls to that function, and remove or ignore 102the other. Unfortunately, it will not remove the coverage counters for 103the unused function body. Hence when instrumented, all but one use of 104that function will show zero counts. 105 106 <p>If the function is inlined in several places, the block structure in 107each location might not be the same. For instance, a condition might 108now be calculable at compile time in some instances. Because the 109coverage of all the uses of the inline function will be shown for the 110same source lines, the line counts themselves might seem inconsistent. 111 112<!-- man end --> 113 </body></html> 114 115