title: Get precise line/column debug info from LLVM IR
body: <p>I am trying to locate instructions in an LLVM Pass by line and column number (reported by an third-party tool) to instrument them. To achieve this, I am compiling my source files with <code>clang -g -O0 -emit-llvm</code> and looking for the information in the metadata using this code:</p>
  
  <pre><code>const DebugLoc &amp;location = instruction-&gt;getDebugLoc();
  // location.getLine()
  // location.getCol()
  </code></pre>
  
  <p>Unfortunately, this information is absolutely imprecise. Consider the following implementation of the Fibonacci function: </p>
  
  <pre><code>unsigned fib(unsigned n) {
      if (n &lt; 2)
          return n;
  
      unsigned f = fib(n - 1) + fib(n - 2);
      return f;
  }
  </code></pre>
  
  <p>I would like to locate the single LLVM instruction corresponding to the assignment <code>unsigned f = ...</code> in the resulting LLVM IR. I am not interested in all the calculations of the right-hand side. The generated LLVM block including relevant debug metadata is: </p>
  
  <pre><code>[...]
  
  if.end:                                           ; preds = %entry
    call void @llvm.dbg.declare(metadata !{i32* %f}, metadata !17), !dbg !18
    %2 = load i32* %n.addr, align 4, !dbg !19
    %sub = sub i32 %2, 1, !dbg !19
    %call = call i32 @fib(i32 %sub), !dbg !19
    %3 = load i32* %n.addr, align 4, !dbg !20
    %sub1 = sub i32 %3, 2, !dbg !20
    %call2 = call i32 @fib(i32 %sub1), !dbg !20
    %add = add i32 %call, %call2, !dbg !20
    store i32 %add, i32* %f, align 4, !dbg !20
    %4 = load i32* %f, align 4, !dbg !21
    store i32 %4, i32* %retval, !dbg !21
    br label %return, !dbg !21
  
  [...]
  
  !17 = metadata !{i32 786688, metadata !4, metadata !"f", metadata !5, i32 5, metadata !8, i32 0, i32 0} ; [ DW_TAG_auto_variable ] [f] [line 5]
  !18 = metadata !{i32 5, i32 11, metadata !4, null}
  !19 = metadata !{i32 5, i32 15, metadata !4, null}
  !20 = metadata !{i32 5, i32 28, metadata !4, null}
  !21 = metadata !{i32 6, i32 2, metadata !4, null}
  !22 = metadata !{i32 7, i32 1, metadata !4, null}
  </code></pre>
  
  <p>As you can see, the metadata <code>!dbg !20</code> of the <code>store</code> instruction points to <strong>line 5 column 28</strong>, which is the call to <code>fib(n - 2)</code>. Even worse, the add operation and the subtraction <code>n - 2</code> both also point to that function call, identified by <code>!dbg !20</code>. </p>
  
  <p>Interestingly, the Clang AST emitted by <code>clang -Xclang -ast-dump -fsyntax-only</code> has all that information. Thus, I suspect that it is somehow lost during the code generation phase. It seems that during code generation Clang reaches some internal sequence point and associates all following instructions to that position until the next sequence point (e.g. function call) occurs. For completeness, here is the declaration statement in the AST: </p>
  
  <pre><code>|-DeclStmt 0x7ffec3869f48 &lt;line:5:2, col:38&gt;
  | `-VarDecl 0x7ffec382d680 &lt;col:2, col:37&gt; col:11 used f 'unsigned int' cinit
  |   `-BinaryOperator 0x7ffec3869f20 &lt;col:15, col:37&gt; 'unsigned int' '+'
  |     |-CallExpr 0x7ffec382d7e0 &lt;col:15, col:24&gt; 'unsigned int'
  |     | |-ImplicitCastExpr 0x7ffec382d7c8 &lt;col:15&gt; 'unsigned int (*)(unsigned int)' &lt;FunctionToPointerDecay&gt;
  |     | | `-DeclRefExpr 0x7ffec382d6d8 &lt;col:15&gt; 'unsigned int (unsigned int)' Function 0x7ffec382d490 'fib' 'unsigned int (unsigned int)'
  |     | `-BinaryOperator 0x7ffec382d778 &lt;col:19, col:23&gt; 'unsigned int' '-'
  |     |   |-ImplicitCastExpr 0x7ffec382d748 &lt;col:19&gt; 'unsigned int' &lt;LValueToRValue&gt;
  |     |   | `-DeclRefExpr 0x7ffec382d700 &lt;col:19&gt; 'unsigned int' lvalue ParmVar 0x7ffec382d3d0 'n' 'unsigned int'
  |     |   `-ImplicitCastExpr 0x7ffec382d760 &lt;col:23&gt; 'unsigned int' &lt;IntegralCast&gt;
  |     |     `-IntegerLiteral 0x7ffec382d728 &lt;col:23&gt; 'int' 1
  |     `-CallExpr 0x7ffec3869ef0 &lt;col:28, col:37&gt; 'unsigned int'
  |       |-ImplicitCastExpr 0x7ffec3869ed8 &lt;col:28&gt; 'unsigned int (*)(unsigned int)' &lt;FunctionToPointerDecay&gt;
  |       | `-DeclRefExpr 0x7ffec3869e10 &lt;col:28&gt; 'unsigned int (unsigned int)' Function 0x7ffec382d490 'fib' 'unsigned int (unsigned int)'
  |       `-BinaryOperator 0x7ffec3869eb0 &lt;col:32, col:36&gt; 'unsigned int' '-'
  |         |-ImplicitCastExpr 0x7ffec3869e80 &lt;col:32&gt; 'unsigned int' &lt;LValueToRValue&gt;
  |         | `-DeclRefExpr 0x7ffec3869e38 &lt;col:32&gt; 'unsigned int' lvalue ParmVar 0x7ffec382d3d0 'n' 'unsigned int'
  |         `-ImplicitCastExpr 0x7ffec3869e98 &lt;col:36&gt; 'unsigned int' &lt;IntegralCast&gt;
  |           `-IntegerLiteral 0x7ffec3869e60 &lt;col:36&gt; 'int' 2
  </code></pre>
  
  <p><strong>Is it either possible to improve the accuracy of the debug metadata, or resolve the corresponding instruction in a different way?</strong> Ideally, I would like to leave Clang untouched, i.e. not modify and recompile it.</p>