Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -6520,6 +6520,7 @@ NewRec->SCEVs.push_back(SE.getSCEV(LocOp)); NewRec->LocationOps.push_back(LocOp); NewRec->HadLocationArgList = DVI->hasArgList(); + SE.forgetValue(LocOp); } SalvageableDVISCEVs.push_back(std::move(NewRec)); DVIHandles.insert(DVI); Index: llvm/test/Transforms/LoopStrengthReduce/dbg-inv-0.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/LoopStrengthReduce/dbg-inv-0.ll @@ -0,0 +1,72 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -loop-reduce -S | opt -S -strip-debug | FileCheck %s +; RUN: opt < %s -strip-debug -S | opt -S -loop-reduce | FileCheck %s + +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" + +; Verify that transformation is debug invariant. More specifically problems +; were observed where SCEV had cached expressions during debug salvage analysis +; (which only happens in presence of debug intrinsics) and then during +; SCEVExpand these cached results were found and reused instead of new +; instructions being generated for the transformed loop. + +define void @k(ptr %f) { +; CHECK-LABEL: @k( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[F:%.*]], align 1 +; CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[TMP0]], 1 +; CHECK-NEXT: store i32 [[INC]], ptr [[F]], align 1 +; CHECK-NEXT: br label [[FOR_COND_CLEANUP:%.*]] +; CHECK: for.cond.cleanup: +; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[TMP0]], 1 +; CHECK-NEXT: br label [[FOR_BODY3:%.*]] +; CHECK: for.body3: +; CHECK-NEXT: [[LSR_IV:%.*]] = phi i32 [ [[LSR_IV_NEXT:%.*]], [[FOR_BODY3]] ], [ [[TMP1]], [[FOR_COND_CLEANUP]] ] +; CHECK-NEXT: store i32 [[LSR_IV]], ptr null, align 1 +; CHECK-NEXT: [[LSR_IV_NEXT]] = add i32 [[LSR_IV]], 1 +; CHECK-NEXT: br label [[FOR_BODY3]] +; +entry: + %0 = load i32, ptr %f, align 1 + %inc = add nsw i32 %0, 1 + store i32 %inc, ptr %f, align 1 + br label %for.cond.cleanup + +for.cond.cleanup: ; preds = %entry + br label %for.body3 + +for.body3: ; preds = %for.body3, %for.cond.cleanup + %1 = phi i32 [ %0, %for.cond.cleanup ], [ %inc4, %for.body3 ] + %inc4 = add nsw i32 %1, 1 + store i32 %inc4, ptr null, align 1 + call void @llvm.dbg.value(metadata i32 %inc, metadata !7, metadata !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 16, DW_ATE_unsigned, DW_OP_stack_value)), !dbg !18 + br label %for.body3 +} + +; Function Attrs: nocallback nofree nosync nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #0 + +attributes #0 = { nocallback nofree nosync nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5, !6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !2, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "foo.c", directory: "/tmp") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 1} +!6 = !{i32 7, !"frame-pointer", i32 2} +!7 = !DILocalVariable(name: "m", scope: !8, file: !1, line: 12, type: !15) +!8 = distinct !DISubprogram(name: "k", scope: !1, file: !1, line: 7, type: !9, scopeLine: 7, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11) +!9 = !DISubroutineType(types: !10) +!10 = !{null} +!11 = !{!12, !7, !16} +!12 = !DILocalVariable(name: "l", scope: !13, file: !1, line: 10, type: !14) +!13 = distinct !DILexicalBlock(scope: !8, file: !1, line: 10, column: 3) +!14 = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: !15) +!15 = !DIBasicType(name: "int", size: 16, encoding: DW_ATE_signed) +!16 = !DILocalVariable(name: "n", scope: !17, file: !1, line: 13, type: !15) +!17 = distinct !DILexicalBlock(scope: !8, file: !1, line: 13, column: 3) +!18 = !DILocation(line: 0, scope: !8)