diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -164,6 +164,8 @@ New->takeName(&AI); New->setUsedWithInAlloca(AI.isUsedWithInAlloca()); + replaceAllDbgUsesWith(AI, *New, *New, DT); + // If the allocation has multiple real uses, insert a cast and change all // things that used it to use the new cast. This will also hack on CI, but it // will die soon. diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -165,7 +165,7 @@ } static Instruction *simplifyAllocaArraySize(InstCombinerImpl &IC, - AllocaInst &AI) { + AllocaInst &AI, DominatorTree &DT) { // Check for array size of 1 (scalar allocation). if (!AI.isArrayAllocation()) { // i32 1 is the canonical array size for scalar allocations. @@ -184,6 +184,8 @@ nullptr, AI.getName()); New->setAlignment(AI.getAlign()); + replaceAllDbgUsesWith(AI, *New, *New, DT); + // Scan to the end of the allocation instructions, to skip over a block of // allocas if possible...also skip interleaved debug info // @@ -352,7 +354,7 @@ } Instruction *InstCombinerImpl::visitAllocaInst(AllocaInst &AI) { - if (auto *I = simplifyAllocaArraySize(*this, AI)) + if (auto *I = simplifyAllocaArraySize(*this, AI, DT)) return I; if (AI.getAllocatedType()->isSized()) { diff --git a/llvm/test/Transforms/InstCombine/no-undef-bitcast-to-gep-ret.ll b/llvm/test/Transforms/InstCombine/no-undef-bitcast-to-gep-ret.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/no-undef-bitcast-to-gep-ret.ll @@ -0,0 +1,44 @@ +; RUN: opt -S -instcombine %s | FileCheck %s + +; https://github.com/llvm/llvm-project/issues/56807 +%struct.Pixel = type { i8, i8, i8 } + +declare void @llvm.dbg.declare(metadata, metadata, metadata) + +; CHECK-LABEL: define dso_local i8* @toplevel( +; CHECK: entry: +; CHECK-NEXT: %pixels1 = alloca [1500 x i8], align 8 +; CHECK-NEXT: call void @llvm.dbg.declare(metadata [1500 x i8]* %pixels1, metadata !7, metadata !DIExpression()), !dbg !12 +; CHECK-NEXT: %pixels1.sub = getelementptr inbounds [1500 x i8], [1500 x i8]* %pixels1, i64 0, i64 0 +; CHECK-NEXT: ret i8* %pixels1.sub +define dso_local i8* @toplevel() { +entry: + %pixels = alloca [500 x %struct.Pixel] + call void @llvm.dbg.declare(metadata [500 x %struct.Pixel]* %pixels, metadata !11, metadata !DIExpression()), !dbg !12 + %arraydecay = bitcast [500 x %struct.Pixel]* %pixels to i8* + ret i8* %arraydecay +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 11.1.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "test.cpp", directory: "/path/to/test_cpp") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 11.1.0"} +!7 = distinct !DISubprogram(name: "toplevel", linkageName: "_Z8toplevelPc", scope: !1, file: !1, line: 9, type: !8, scopeLine: 9, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{!10, !10} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !DILocalVariable(name: "num", arg: 1, scope: !7, file: !1, line: 9, type: !10) +!12 = !DILocation(line: 9, column: 16, scope: !7) +!13 = !DILocalVariable(name: "Pixel", scope: !7, file: !1, line: 10, type: !14) +!14 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 16000, elements: !15) +!15 = !{!16} +!16 = !DISubrange(count: 500) +!17 = !DILocation(line: 10, column: 9, scope: !7) +!18 = !DILocation(line: 12, column: 5, scope: !7) diff --git a/llvm/test/Transforms/InstCombine/no-undef-bitcast-to-gep.ll b/llvm/test/Transforms/InstCombine/no-undef-bitcast-to-gep.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/no-undef-bitcast-to-gep.ll @@ -0,0 +1,48 @@ +; RUN: opt -S -instcombine %s | FileCheck %s + +; https://github.com/llvm/llvm-project/issues/56807 +%struct.Pixel = type { i8, i8, i8 } + +declare void @foo(i8* %pixels) + +declare void @llvm.dbg.declare(metadata, metadata, metadata) + +; CHECK-LABEL: @toplevel( +; CHECK: entry: +; CHECK-NEXT: %pixels1 = alloca [1500 x i8], align 8 +; CHECK-NEXT: call void @llvm.dbg.declare(metadata [1500 x i8]* %pixels1, metadata !7, metadata !DIExpression()), !dbg !12 +; CHECK-NEXT: %pixels1.sub = getelementptr inbounds [1500 x i8], [1500 x i8]* %pixels1, i64 0, i64 0 +; CHECK-NEXT: call void @foo(i8* nonnull %pixels1.sub) +; CHECK-NEXT: ret void +define dso_local void @toplevel() { +entry: + %pixels = alloca [500 x %struct.Pixel] + call void @llvm.dbg.declare(metadata [500 x %struct.Pixel]* %pixels, metadata !11, metadata !DIExpression()), !dbg !12 + %arraydecay = bitcast [500 x %struct.Pixel]* %pixels to i8* + call void @foo(i8* %arraydecay) + ret void +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 11.1.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "test.cpp", directory: "/path/to/test_cpp") +!2 = !{} +!3 = !{i32 7, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 4} +!6 = !{!"clang version 11.1.0"} +!7 = distinct !DISubprogram(name: "toplevel", linkageName: "_Z8toplevelv", scope: !1, file: !1, line: 9, type: !8, scopeLine: 9, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{!10, !10} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !DILocalVariable(name: "num", arg: 1, scope: !7, file: !1, line: 9, type: !10) +!12 = !DILocation(line: 9, column: 16, scope: !7) +!13 = !DILocalVariable(name: "Pixel", scope: !7, file: !1, line: 10, type: !14) +!14 = !DICompositeType(tag: DW_TAG_array_type, baseType: !10, size: 16000, elements: !15) +!15 = !{!16} +!16 = !DISubrange(count: 500) +!17 = !DILocation(line: 10, column: 9, scope: !7) +!18 = !DILocation(line: 12, column: 5, scope: !7) \ No newline at end of file