Skip to content

Commit 35fb19b

Browse files
committed
Add debug location to strlen in LoopIdiomRecognize pass
1 parent 9981afc commit 35fb19b

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,9 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
17781778
}
17791779
assert(StrLenFunc && "Failed to emit strlen function.");
17801780

1781+
// Set debug location to the start of the loop.
1782+
cast<Instruction>(StrLenFunc)->setDebugLoc(CurLoop->getStartLoc());
1783+
17811784
const SCEV *StrlenEv = SE->getSCEV(StrLenFunc);
17821785
SmallVector<PHINode *, 4> Cleanup;
17831786
for (PHINode &PN : LoopExitBB->phis()) {

llvm/test/Transforms/LoopIdiom/strlen.ll

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,51 @@ while.end:
612612
ret i64 %sub.ptr.sub
613613
}
614614

615+
define i64 @valid_basic_strlen_with_dbg(ptr %str) {
616+
; CHECK-LABEL: define i64 @valid_basic_strlen_with_dbg(
617+
; CHECK-SAME: ptr [[STR:%.*]]) {
618+
; CHECK-NEXT: [[ENTRY:.*]]:
619+
; CHECK-NEXT: [[STRLEN:%.*]] = call i64 @strlen(ptr [[STR]]), !dbg !{{[0-9]+}}
620+
; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[STR]], i64 [[STRLEN]]
621+
; CHECK-NEXT: br label %[[WHILE_COND:.*]]
622+
; CHECK: [[WHILE_COND]]:
623+
; CHECK-NEXT: [[STR_ADDR_0:%.*]] = phi ptr [ [[STR]], %[[ENTRY]] ], [ [[INCDEC_PTR:%.*]], %[[WHILE_COND]] ]
624+
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[STR_ADDR_0]], align 1
625+
; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp eq i8 [[TMP0]], 0
626+
; CHECK-NEXT: [[INCDEC_PTR]] = getelementptr i8, ptr [[STR_ADDR_0]], i64 1
627+
; CHECK-NEXT: br i1 true, label %[[WHILE_END:.*]], label %[[WHILE_COND]]
628+
; CHECK: [[WHILE_END]]:
629+
; CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[SCEVGEP]] to i64
630+
; CHECK-NEXT: [[SUB_PTR_RHS_CAST:%.*]] = ptrtoint ptr [[STR]] to i64
631+
; CHECK-NEXT: [[SUB_PTR_SUB:%.*]] = sub i64 [[SUB_PTR_LHS_CAST]], [[SUB_PTR_RHS_CAST]]
632+
; CHECK-NEXT: ret i64 [[SUB_PTR_SUB]]
633+
;
634+
entry:
635+
br label %while.cond
636+
637+
while.cond:
638+
%str.addr.0 = phi ptr [ %str, %entry ], [ %incdec.ptr, %while.cond ]
639+
%0 = load i8, ptr %str.addr.0, align 1, !dbg !6
640+
%cmp.not = icmp eq i8 %0, 0, !dbg !6
641+
%incdec.ptr = getelementptr i8, ptr %str.addr.0, i64 1
642+
br i1 %cmp.not, label %while.end, label %while.cond, !dbg !6
643+
644+
while.end:
645+
%sub.ptr.lhs.cast = ptrtoint ptr %str.addr.0 to i64
646+
%sub.ptr.rhs.cast = ptrtoint ptr %str to i64
647+
%sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
648+
ret i64 %sub.ptr.sub
649+
}
650+
651+
652+
!llvm.module.flags = !{!7}
653+
!llvm.dbg.cu = !{!2}
654+
655+
!0 = distinct !DISubprogram(name: "foo", line: 2, isLocal: false, isDefinition: true, virtualIndex: 6, flags: DIFlagPrototyped, isOptimized: false, unit: !2, file: !1, scope: !1, type: !3)
656+
!1 = !DIFile(filename: "strlen.c", directory: "/tmp")
657+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, producer: "clang version 2.9 (trunk 127165:127174)", isOptimized: true, emissionKind: FullDebug, file: !1, enums: !4, retainedTypes: !4)
658+
!3 = !DISubroutineType(types: !4)
659+
!4 = !{}
660+
!5 = distinct !DILexicalBlock(line: 2, column: 21, file: !1, scope: !0)
661+
!6 = !DILocation(line: 3, column: 3, scope: !5)
662+
!7 = !{i32 1, !"Debug Info Version", i32 3}

0 commit comments

Comments
 (0)