This is a follow-up of D25872.
When the loop exit condition is canonicalized as a != comparison, reuse the debug loc associated to the original (non canonical) comparison.
extern int foo(int); // line 1 int bar(int X, int *A) { for (int i = 0; i < X; ++i) // line 4 A[i] = foo(i); return A[0]; }
Before IndVarSimplify is run, the optimized LLVM IR of the loop latch looks like this:
for.inc: %inc = add nsw i32 %i.06, 1, !dbg !13 %cmp = icmp slt i32 %inc, %X, !dbg !7 br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge, !dbg !9, !llvm.loop !15
Where !dbg !7 is defined as:
!7 = !DILocation(line: 4, column: 21, scope: !8) !8 = !DILexicalBlockFile(scope: !5, file: !1, discriminator: 1)
IndVarSimplify rewrites the exit condition of the loop to be a canonical != comparison. In this particular case, it rewrites the comparison in the loop latch as follows:
%wide.trip.count = zext i32 %X to i64, !dbg !9 %exitcond = icmp ne i64 %indvars.iv.next, %wide.trip.count, !dbg !9
However, !dbg !9 is defined as:
!8 = !DILexicalBlockFile(scope: !5, file: !1, discriminator: 1) !9 = !DILocation(line: 4, column: 3, scope: !8)
Instead, it should be:
!7 = !DILocation(line: 4, column: 21, scope: !8) !8 = !DILexicalBlockFile(scope: !5, file: !1, discriminator: 1)
The problem is indirectly caused by the fact that the debug location of the new icmp is obtained from the loop latch terminator (i.e. our IRBuilder "insertion point").
This patch fixes the issue by correctly setting the IRBuilder's "current debug location" to the location of the original compare instruction.
Please let me know if okay to commit.
Thanks,
Andrea