From 87823f8e4d861a03da136d8de02e9140c53bad25 Mon Sep 17 00:00:00 2001 From: Dehao Chen Date: Thu, 8 Sep 2016 21:53:33 +0000 Subject: [PATCH] Remove debug info when hoisting instruction from then/else branch. Summary: The hoisted instruction is executed speculatively. It could affect the debugging experience as user would see gdb go into code that may not be expected to execute. It will also affect sample profile accuracy by assigning incorrect frequency to source within then/else branch. Reviewers: davidxl, dblaikie, chandlerc, kcc, echristo Subscribers: mehdi_amini, probinson, eric_niebler, andreadb, llvm-commits Differential Revision: https://reviews.llvm.org/D24164 llvm-svn: 280995 --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 8 ++ .../Transforms/SimplifyCFG/remove-debug.ll | 83 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 llvm/test/Transforms/SimplifyCFG/remove-debug.ll diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 1d5d85309be40..921f325f29a92 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -1242,6 +1242,14 @@ static bool HoistThenElseCodeToIf(BranchInst *BI, LLVMContext::MD_dereferenceable_or_null, LLVMContext::MD_mem_parallel_loop_access}; combineMetadata(I1, I2, KnownIDs); + + // If the debug loc for I1 and I2 are different, as we are combining them + // into one instruction, we do not want to select debug loc randomly from + // I1 or I2. Instead, we set the 0-line DebugLoc to note that we do not + // know the debug loc of the hoisted instruction. + if (!isa(I1) && I1->getDebugLoc() != I2->getDebugLoc()) + I1->setDebugLoc(DebugLoc()); + I2->eraseFromParent(); Changed = true; diff --git a/llvm/test/Transforms/SimplifyCFG/remove-debug.ll b/llvm/test/Transforms/SimplifyCFG/remove-debug.ll new file mode 100644 index 0000000000000..d4b2373ebf82a --- /dev/null +++ b/llvm/test/Transforms/SimplifyCFG/remove-debug.ll @@ -0,0 +1,83 @@ +; RUN: opt < %s -simplifycfg -S | FileCheck %s + +; TODO: Track the acutal DebugLoc of the hoisted instruction when no-line +; DebugLoc is supported (https://reviews.llvm.org/D24180) +; CHECK: line: 6 +; CHECK-NOT: line: 7 +; CHECK: line: 8 +; CHECK: line: 9 +; CHECK-NOT: line: 10 +; CHECK: line: 11 + +; Checks if the debug info for hoisted "x = i" is removed +; int x; +; void bar(); +; void baz(); +; +; void foo(int i) { +; if (i == 0) { +; x = i; +; bar(); +; } else { +; x = i; +; baz(); +; } +; } + +target triple = "x86_64-unknown-linux-gnu" + +@x = global i32 0, align 4 + +; Function Attrs: uwtable +define void @_Z3fooi(i32) #0 !dbg !6 { + %2 = alloca i32, align 4 + store i32 %0, i32* %2, align 4, !tbaa !8 + %3 = load i32, i32* %2, align 4, !dbg !12, !tbaa !8 + %4 = icmp eq i32 %3, 0, !dbg !13 + br i1 %4, label %5, label %7, !dbg !12 + +;