220 lines · cpp
1//===- DebugSSAUpdater.cpp - Unit tests for debug variable tracking -------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/Transforms/Utils/DebugSSAUpdater.h"10#include "llvm/ADT/STLExtras.h"11#include "llvm/AsmParser/Parser.h"12#include "llvm/IR/BasicBlock.h"13#include "llvm/IR/DebugInfoMetadata.h"14#include "llvm/IR/DebugProgramInstruction.h"15#include "llvm/IR/Function.h"16#include "llvm/IR/GlobalValue.h"17#include "llvm/IR/Module.h"18#include "llvm/Support/SourceMgr.h"19#include "gtest/gtest.h"20 21using namespace llvm;22 23static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {24 SMDiagnostic Err;25 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);26 if (!Mod)27 Err.print("DebugSSAUpdaterTests", errs());28 return Mod;29}30 31namespace {32 33// Verify that two conflicting live-in values result in no live-in range for a34// block.35TEST(DebugSSAUpdater, EmptyPHIRange) {36 LLVMContext C;37 38 std::unique_ptr<Module> M =39 parseIR(C,40 R"(define i32 @foo(i32 %a, i1 %b) !dbg !7 {41entry:42 #dbg_value(i32 %a, !6, !DIExpression(), !10)43 br i1 %b, label %if.then, label %if.else, !dbg !1144 45if.then:46 %c = add i32 %a, 10, !dbg !1247 #dbg_value(i32 %c, !6, !DIExpression(), !13)48 br label %exit, !dbg !1449 50if.else:51 %d = mul i32 %a, 3, !dbg !1552 #dbg_value(i32 %d, !6, !DIExpression(), !16)53 br label %exit, !dbg !1754 55exit:56 %res = phi i32 [ %c, %if.then ], [ %d, %if.else ], !dbg !1857 ret i32 %res, !dbg !1958}59 60!llvm.dbg.cu = !{!0}61!llvm.module.flags = !{!4}62 63!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_17, file: !1, producer: "clang version 20.0.0")64!1 = !DIFile(filename: "test.cpp", directory: ".")65!2 = !{}66!3 = !{i32 7, !"Dwarf Version", i32 4}67!4 = !{i32 2, !"Debug Info Version", i32 3}68!5 = !{!"clang version 20.0.0"}69!6 = !DILocalVariable(name: "a", scope: !7, file: !1, line: 11, type: !8)70!7 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 10, type: !9, scopeLine: 10, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)71!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)72!9 = !DISubroutineType(types: !2)73!10 = !DILocation(line: 10, scope: !7)74!11 = !DILocation(line: 11, scope: !7)75!12 = !DILocation(line: 12, scope: !7)76!13 = !DILocation(line: 13, scope: !7)77!14 = !DILocation(line: 14, scope: !7)78!15 = !DILocation(line: 15, scope: !7)79!16 = !DILocation(line: 16, scope: !7)80!17 = !DILocation(line: 17, scope: !7)81!18 = !DILocation(line: 18, scope: !7)82!19 = !DILocation(line: 19, scope: !7)83)");84 85 Function *Foo = &*M->begin();86 DebugVariableAggregate VarA(cast<DbgVariableRecord>(87 Foo->begin()->begin()->getDbgRecordRange().begin()));88 DbgValueRangeTable DbgValueRanges;89 DbgValueRanges.addVariable(Foo, VarA);90 BasicBlock *ExitBlock = &Foo->back();91 // We should have 5 ranges: 1 in the entry block, and 2 in each `if` block,92 // while there should be no range for the exit block.93 EXPECT_EQ(DbgValueRanges.getVariableRanges(VarA).size(), 5u);94 EXPECT_TRUE(none_of(DbgValueRanges.getVariableRanges(VarA),95 [&](DbgRangeEntry VarRange) {96 return VarRange.Start->getParent() == ExitBlock;97 }));98}99 100// Verify that we correctly set live-in variable values through loops.101TEST(DebugSSAUpdater, LoopPHI) {102 LLVMContext C;103 104 std::unique_ptr<Module> M =105 parseIR(C,106 R"(define i32 @foo(i32 %a, i32 %max) !dbg !7 {107entry:108 #dbg_value(i32 %a, !6, !DIExpression(), !10)109 %cond.entry = icmp slt i32 %a, %max, !dbg !11110 br i1 %cond.entry, label %loop, label %exit, !dbg !12111 112loop:113 %loop.a = phi i32 [ %a, %entry ], [ %inc, %loop ]114 %inc = add i32 %loop.a, 1, !dbg !13115 %cond.loop = icmp slt i32 %inc, %max, !dbg !14116 br i1 %cond.loop, label %loop, label %exit, !dbg !15117 118exit:119 %res = phi i32 [ %a, %entry ], [ %loop.a, %loop ]120 ret i32 %res, !dbg !16121}122 123!llvm.dbg.cu = !{!0}124!llvm.module.flags = !{!4}125 126!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_17, file: !1, producer: "clang version 20.0.0")127!1 = !DIFile(filename: "test.cpp", directory: ".")128!2 = !{}129!3 = !{i32 7, !"Dwarf Version", i32 4}130!4 = !{i32 2, !"Debug Info Version", i32 3}131!5 = !{!"clang version 20.0.0"}132!6 = !DILocalVariable(name: "a", scope: !7, file: !1, line: 11, type: !8)133!7 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 10, type: !9, scopeLine: 10, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)134!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)135!9 = !DISubroutineType(types: !2)136!10 = !DILocation(line: 10, scope: !7)137!11 = !DILocation(line: 11, scope: !7)138!12 = !DILocation(line: 12, scope: !7)139!13 = !DILocation(line: 13, scope: !7)140!14 = !DILocation(line: 14, scope: !7)141!15 = !DILocation(line: 15, scope: !7)142!16 = !DILocation(line: 16, scope: !7)143)");144 145 Function *Foo = &*M->begin();146 DebugVariableAggregate VarA(cast<DbgVariableRecord>(147 Foo->begin()->begin()->getDbgRecordRange().begin()));148 DbgValueRangeTable DbgValueRanges;149 DbgValueRanges.addVariable(Foo, VarA);150 // We should have 3 ranges: 1 in the entry block, and 1 live-in entry for each151 // of the loops.152 EXPECT_EQ(DbgValueRanges.getVariableRanges(VarA).size(), 3u);153 EXPECT_TRUE(154 all_of(DbgValueRanges.getVariableRanges(VarA),155 [&](DbgRangeEntry VarRange) { return !VarRange.Value.IsUndef; }));156}157 158// Verify that when a variable has only undef debug values, it has no live159// ranges.160TEST(DebugSSAUpdater, AllUndefVar) {161 LLVMContext C;162 163 std::unique_ptr<Module> M =164 parseIR(C,165 R"(define i32 @foo(i32 %a, i1 %b) !dbg !7 {166entry:167 #dbg_value(i32 poison, !6, !DIExpression(), !10)168 br i1 %b, label %if.then, label %if.else, !dbg !11169 170if.then:171 %c = add i32 %a, 10, !dbg !12172 #dbg_value(i32 poison, !6, !DIExpression(), !13)173 br label %exit, !dbg !14174 175if.else:176 %d = mul i32 %a, 3, !dbg !15177 #dbg_value(i32 poison, !6, !DIExpression(), !16)178 br label %exit, !dbg !17179 180exit:181 %res = phi i32 [ %c, %if.then ], [ %d, %if.else ], !dbg !18182 ret i32 %res, !dbg !19183}184 185!llvm.dbg.cu = !{!0}186!llvm.module.flags = !{!4}187 188!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_17, file: !1, producer: "clang version 20.0.0")189!1 = !DIFile(filename: "test.cpp", directory: ".")190!2 = !{}191!3 = !{i32 7, !"Dwarf Version", i32 4}192!4 = !{i32 2, !"Debug Info Version", i32 3}193!5 = !{!"clang version 20.0.0"}194!6 = !DILocalVariable(name: "a", scope: !7, file: !1, line: 11, type: !8)195!7 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 10, type: !9, scopeLine: 10, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)196!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)197!9 = !DISubroutineType(types: !2)198!10 = !DILocation(line: 10, scope: !7)199!11 = !DILocation(line: 11, scope: !7)200!12 = !DILocation(line: 12, scope: !7)201!13 = !DILocation(line: 13, scope: !7)202!14 = !DILocation(line: 14, scope: !7)203!15 = !DILocation(line: 15, scope: !7)204!16 = !DILocation(line: 16, scope: !7)205!17 = !DILocation(line: 17, scope: !7)206!18 = !DILocation(line: 18, scope: !7)207!19 = !DILocation(line: 19, scope: !7)208)");209 210 Function *Foo = &*M->begin();211 DebugVariableAggregate VarA(cast<DbgVariableRecord>(212 Foo->begin()->begin()->getDbgRecordRange().begin()));213 DbgValueRangeTable DbgValueRanges;214 DbgValueRanges.addVariable(Foo, VarA);215 // There should be no variable ranges emitted for a variable that has only216 // undef dbg_values.217 EXPECT_EQ(DbgValueRanges.getVariableRanges(VarA).size(), 0u);218}219} // namespace220