diff --git a/llvm/test/tools/llvm-reduce/reduce-debug-info.ll b/llvm/test/tools/llvm-reduce/reduce-debug-info.ll new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-reduce/reduce-debug-info.ll @@ -0,0 +1,48 @@ +; RUN: llvm-reduce --delta-passes=debug-info --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTING --test-arg %s --test-arg --input-file %s -o %t +; RUN: cat %t | FileCheck %s + +; CHECK-INTERESTING-DAG: g_keepme +; CHECK-INTERESTING-DAG: E_keepme +; CHECK-INTERESTING-DAG: g_e_keepme +; CHECK-INTERESTING-DAG: A_keepme + +; CHECK: distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: {{![0-9]+}}, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: [[ENUMS:![0-9]+]], globals: [[GLOBALS:![0-9]+]], splitDebugInlining: false, nameTableKind: None) +; CHECK: [[ENUMS]] = !{[[ENUM:![0-9]+]]} +; CHECK: [[ENUM]] = distinct !DICompositeType({{.*}}name: "E_keepme" +; CHECK: [[GLOBALS]] = !{[[GLOBAL0:![0-9]+]], [[GLOBAL1:![0-9]+]]} +; CHECK: [[GLOBAL0]] = !DIGlobalVariableExpression(var: [[GLOBAL0VAR:![0-9]+]] +; CHECK: [[GLOBAL0VAR]] = distinct !DIGlobalVariable(name: "g_keepme" +; CHECK: [[GLOBAL1]] = !DIGlobalVariableExpression(var: [[GLOBAL1VAR:![0-9]+]] +; CHECK: [[GLOBAL1VAR]] = distinct !DIGlobalVariable(name: "g_e_keepme" + +target triple = "x86_64---" + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!23, !24} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !10, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "test.cpp", directory: "/") +!2 = !{!3, !7} +!3 = distinct !DICompositeType(tag: DW_TAG_enumeration_type, name: "E_keepme", file: !1, line: 15, baseType: !4, size: 32, elements: !5, identifier: "_ZTS8E_kee +pme") +!4 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned) +!5 = !{!6} +!6 = !DIEnumerator(name: "E_FOO", value: 5, isUnsigned: true) +!7 = distinct !DICompositeType(tag: DW_TAG_enumeration_type, name: "E_dropme", file: !1, line: 19, baseType: !4, size: 32, elements: !8, identifier: "_ZTS8E_dropme") +!8 = !{!9} +!9 = !DIEnumerator(name: "E_BAM", value: 13, isUnsigned: true) +!10 = !{!11, !15, !19, !21} +!11 = !DIGlobalVariableExpression(var: !12, expr: !DIExpression()) +!12 = distinct !DIGlobalVariable(name: "g_keepme", scope: !0, file: !1, line: 12, type: !13, isLocal: false, isDefinition: true) +!13 = !DIDerivedType(tag: DW_TAG_typedef, name: "A_keepme", file: !1, line: 9, baseType: !14) +!14 = !DIBasicType(name: "short", size: 16, encoding: DW_ATE_signed) +!15 = !DIGlobalVariableExpression(var: !16, expr: !DIExpression()) +!16 = distinct !DIGlobalVariable(name: "g_dropme", scope: !0, file: !1, line: 13, type: !17, isLocal: false, isDefinition: true) +!17 = !DIDerivedType(tag: DW_TAG_typedef, name: "A_dropme", file: !1, line: 10, baseType: !18) +!18 = !DIBasicType(name: "long long", size: 64, encoding: DW_ATE_signed) +!19 = !DIGlobalVariableExpression(var: !20, expr: !DIExpression()) +!20 = distinct !DIGlobalVariable(name: "g_e_keepme", scope: !0, file: !1, line: 23, type: !4, isLocal: false, isDefinition: true) +!21 = !DIGlobalVariableExpression(var: !22, expr: !DIExpression()) +!22 = distinct !DIGlobalVariable(name: "g_e_dropme", scope: !0, file: !1, line: 24, type: !7, isLocal: false, isDefinition: true) +!23 = !{i32 7, !"Dwarf Version", i32 5} +!24 = !{i32 2, !"Debug Info Version", i32 3} diff --git a/llvm/tools/llvm-reduce/CMakeLists.txt b/llvm/tools/llvm-reduce/CMakeLists.txt --- a/llvm/tools/llvm-reduce/CMakeLists.txt +++ b/llvm/tools/llvm-reduce/CMakeLists.txt @@ -28,6 +28,7 @@ deltas/ReduceArguments.cpp deltas/ReduceAttributes.cpp deltas/ReduceBasicBlocks.cpp + deltas/ReduceDebugInfo.cpp deltas/ReduceFunctionBodies.cpp deltas/ReduceFunctions.cpp deltas/ReduceGlobalObjects.cpp diff --git a/llvm/tools/llvm-reduce/DeltaManager.cpp b/llvm/tools/llvm-reduce/DeltaManager.cpp --- a/llvm/tools/llvm-reduce/DeltaManager.cpp +++ b/llvm/tools/llvm-reduce/DeltaManager.cpp @@ -19,6 +19,7 @@ #include "deltas/ReduceArguments.h" #include "deltas/ReduceAttributes.h" #include "deltas/ReduceBasicBlocks.h" +#include "deltas/ReduceDebugInfo.h" #include "deltas/ReduceFunctionBodies.h" #include "deltas/ReduceFunctions.h" #include "deltas/ReduceGlobalObjects.h" @@ -66,6 +67,7 @@ DELTA_PASS("global-initializers", reduceGlobalsInitializersDeltaPass) \ DELTA_PASS("global-variables", reduceGlobalsDeltaPass) \ DELTA_PASS("metadata", reduceMetadataDeltaPass) \ + DELTA_PASS("debug-info", reduceDebugInfoDeltaPass) \ DELTA_PASS("arguments", reduceArgumentsDeltaPass) \ DELTA_PASS("instructions", reduceInstructionsDeltaPass) \ DELTA_PASS("simplify-instructions", simplifyInstructionsDeltaPass) \ diff --git a/llvm/tools/llvm-reduce/deltas/ReduceDebugInfo.h b/llvm/tools/llvm-reduce/deltas/ReduceDebugInfo.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/ReduceDebugInfo.h @@ -0,0 +1,12 @@ +#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEDEBUGINFO_H +#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCEDEBUGINFO_H + +namespace llvm { + +class TestRunner; + +void reduceDebugInfoDeltaPass(TestRunner &Test); + +} // namespace llvm + +#endif diff --git a/llvm/tools/llvm-reduce/deltas/ReduceDebugInfo.cpp b/llvm/tools/llvm-reduce/deltas/ReduceDebugInfo.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-reduce/deltas/ReduceDebugInfo.cpp @@ -0,0 +1,49 @@ +#include "ReduceDebugInfo.h" + +#include "Delta.h" +#include "Utils.h" +#include "llvm/IR/DebugInfo.h" +#include "llvm/IR/DebugInfoMetadata.h" + +namespace llvm { + +static MDTuple *reduceTuple(Oracle &O, MDTuple *T) { + if (T == nullptr || T->getNumOperands() == 0) + return nullptr; + + std::vector NewEntries; + for (const MDOperand &MDO : T->operands()) { + if (O.shouldKeep()) + NewEntries.push_back(MDO.get()); + } + if (NewEntries.size() == T->getNumOperands()) + return nullptr; + + return MDTuple::get(T->getContext(), NewEntries); +} + +static void reduceCompileUnit(Oracle &O, DICompileUnit &CU) { + if (MDTuple *EnumTypes = reduceTuple(O, CU.getEnumTypes().get())) + CU.replaceEnumTypes(EnumTypes); + if (MDTuple *RetainedTypes = reduceTuple(O, CU.getRetainedTypes().get())) + CU.replaceRetainedTypes(RetainedTypes); + if (MDTuple *GlobalVariables = reduceTuple(O, CU.getGlobalVariables().get())) + CU.replaceGlobalVariables(GlobalVariables); + if (MDTuple *ImportedEntities = + reduceTuple(O, CU.getImportedEntities().get())) + CU.replaceImportedEntities(ImportedEntities); +} + +static void removeDebugInfoFromModule(Oracle &O, Module &M) { + for (DICompileUnit *CU : M.debug_compile_units()) { + reduceCompileUnit(O, *CU); + } +} + +void reduceDebugInfoDeltaPass(TestRunner &Test) { + outs() << "*** Reducing DebugInfo...\n"; + runDeltaPass(Test, removeDebugInfoFromModule); + outs() << "----------------------------\n"; +} + +} // namespace llvm