Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -6216,6 +6216,13 @@ DVIRec.DVI->setExpression(DVIRec.Expr); } + // SCEVUnknowns may have null Value pointers if the corresponding + // instruction was deleted, these are not safe to pass to + // the SCEV utilities. + if (auto *U = dyn_cast(DVIRec.SCEV)) + if (U->getValue() == nullptr) + continue; + LLVM_DEBUG(dbgs() << "scev-salvage: value to recover SCEV: " << *DVIRec.SCEV << '\n'); Index: llvm/test/Transforms/LoopStrengthReduce/deleted-debug-phi.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/LoopStrengthReduce/deleted-debug-phi.ll @@ -0,0 +1,106 @@ +; The phi in while.cond179 is dead and is removed by LoopStrengthReduce. +; When the SCEVs are salvaged to update the debug MD, the SCEV for the phi +; should not be analyzed, as it is a SCEVUnknown with a null Value. +; This is exposed by the debug printout which will crash when dumping that SCEV. + +; REQUIRES: asserts +; RUN: opt -debugify -loop-reduce -debug -S < %s | FileCheck %s +source_filename = "deleted-debug-phi.ll" +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" +target triple = "x86_64-apple-darwin10.0.0" + +; Function Attrs: nounwind +define void @nopreheader(i8* %end) #0 { + +; CHECK-LABEL: @nopreheader( +; CHECK: while.cond179: +; CHECK-NEXT: call void @llvm.dbg.value(metadata i8* undef, metadata [[META9:![0-9]+]], metadata !DIExpression()), !dbg [[DBG16:![0-9]+]] + +; CHECK: while.cond238: +; CHECK-NEXT: [[TMP0:%.*]] = phi i64 {{.*}} !dbg [[DBG25:![0-9]+]] +; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 [[TMP0]], metadata [[META11:![0-9]+]], metadata !DIExpression()), !dbg [[DBG25]] +; CHECK-NEXT: [[TMPX:%.*]] = add i64 undef, [[TMP0]], !dbg [[DBG26:![0-9]+]] +; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 [[TMPX]], metadata [[META12:![0-9]+]], metadata !DIExpression()), !dbg [[DBG26]] + +; CHECK: land.rhs243: +; CHECK-NEXT: [[INDVAR_NEXT15:%.*]] = add i64 [[TMP0]], 1, !dbg [[DBG28:![0-9]+]] +; CHECK-NEXT: call void @llvm.dbg.value(metadata i64 [[INDVAR_NEXT15]], metadata [[META13:![0-9]+]], metadata !DIExpression()), !dbg [[DBG28]] + +entry: + br label %while.cond179 + +while.cond179: ; preds = %if.end348, %entry + %s.1 = phi i8* [ %incdec.ptr356, %if.end348 ], [ undef, %entry ] + indirectbr i8* undef, [label %land.rhs184, label %while.end453] + +land.rhs184: ; preds = %while.cond179 + indirectbr i8* undef, [label %while.end453, label %while.cond197] + +while.cond197: ; preds = %land.rhs202, %land.rhs184 + indirectbr i8* undef, [label %land.rhs202, label %while.end215] + +land.rhs202: ; preds = %while.cond197 + indirectbr i8* undef, [label %while.end215, label %while.cond197] + +while.end215: ; preds = %land.rhs202, %while.cond197 + indirectbr i8* undef, [label %PREMATURE, label %if.end221] + +if.end221: ; preds = %while.end215 + indirectbr i8* undef, [label %while.cond238.preheader, label %lor.lhs.false227] + +lor.lhs.false227: ; preds = %if.end221 + indirectbr i8* undef, [label %while.cond238.preheader, label %if.else] + +while.cond238.preheader: ; preds = %lor.lhs.false227, %if.end221 + indirectbr i8* undef, [label %while.cond238] + +while.cond238: ; preds = %land.rhs243, %while.cond238.preheader + %0 = phi i64 [ %indvar.next15, %land.rhs243 ], [ 0, %while.cond238.preheader ] + %tmpx = add i64 undef, %0 + indirectbr i8* undef, [label %land.rhs243, label %while.end256] + +land.rhs243: ; preds = %while.cond238 + %indvar.next15 = add i64 %0, 1 + indirectbr i8* undef, [label %while.end256, label %while.cond238] + +while.end256: ; preds = %land.rhs243, %while.cond238 + unreachable + +if.else: ; preds = %lor.lhs.false227 + indirectbr i8* undef, [label %if.then297, label %if.else386] + +if.then297: ; preds = %if.else + indirectbr i8* undef, [label %PREMATURE, label %if.end307] + +if.end307: ; preds = %if.then297 + indirectbr i8* undef, [label %if.end314, label %FAIL] + +if.end314: ; preds = %if.end307 + indirectbr i8* undef, [label %PREMATURE, label %if.end348] + +if.end348: ; preds = %if.end314 + %incdec.ptr356 = getelementptr inbounds i8, i8* undef, i64 2 + indirectbr i8* undef, [label %while.cond179] + +if.else386: ; preds = %if.else + unreachable + +while.end453: ; preds = %land.rhs184, %while.cond179 + unreachable + +FAIL: ; preds = %if.end307 + unreachable + +PREMATURE: ; preds = %if.end314, %if.then297, %while.end215 + unreachable +} + +; Function Attrs: nofree nosync nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + +attributes #0 = { nounwind } +attributes #1 = { nofree nosync nounwind readnone speculatable willreturn } + +!llvm.module.flags = !{!0} + +!0 = !{i32 2, !"Debug Info Version", i32 3}