Skip to content

Commit 7175970

Browse files
authored
Add debug location to strlen in LoopIdiomRecognize pass (llvm#140164)
Pass down the debug location to the generated strlen call because LLVM maintains that calls to inlinable functions must have debug info.
1 parent fec9be9 commit 7175970

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,7 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
17461746
}
17471747

17481748
IRBuilder<> Builder(Preheader->getTerminator());
1749+
Builder.SetCurrentDebugLocation(CurLoop->getStartLoc());
17491750
SCEVExpander Expander(*SE, Preheader->getModule()->getDataLayout(),
17501751
"strlen_idiom");
17511752
Value *MaterialzedBase = Expander.expandCodeFor(

llvm/test/Transforms/LoopIdiom/strlen.ll

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

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

0 commit comments

Comments
 (0)