Index: llvm/lib/Transforms/Utils/SSAUpdater.cpp =================================================================== --- llvm/lib/Transforms/Utils/SSAUpdater.cpp +++ llvm/lib/Transforms/Utils/SSAUpdater.cpp @@ -19,6 +19,7 @@ #include "llvm/IR/BasicBlock.h" #include "llvm/IR/CFG.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DebugLoc.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" @@ -279,6 +280,11 @@ SSAUpdater *Updater) { PHINode *PHI = PHINode::Create(Updater->ProtoType, NumPreds, Updater->ProtoName, &BB->front()); + // Add debug location with Line 0 and function scope to the new PHI node + if (BB->getParent()->getSubprogram()) { + PHI->setDebugLoc(DILocation::get(BB->getContext(), 0, 0, + BB->getParent()->getSubprogram())); + } return PHI; } Index: llvm/test/Transforms/GVN/debugloc.ll =================================================================== --- llvm/test/Transforms/GVN/debugloc.ll +++ llvm/test/Transforms/GVN/debugloc.ll @@ -1,10 +1,13 @@ ; RUN: opt < %s -gvn -S | FileCheck %s +; CHECK-LABEL: @foo( +; CHECK-SAME: !dbg ![[FNDBG:[0-9]+]] ; CHECK: {{^}}for.body: ; CHECK-NEXT: [[VREG1:%[^ ]+]] = phi{{.*}}[[VREG2:%[^ ]+]],{{.*}}%.sink, -; CHECK-NOT: !dbg +; CHECK: !dbg ![[DBG:[0-9]+]] ; CHECK-SAME: {{$}} ; CHECK: {{^}}for.inc: ; CHECK-NEXT: [[VREG2]] = phi{{.*}}%inc,{{.*}}[[VREG1]] +; CHECK: ![[DBG]] = !DILocation(line: 0, scope: ![[FNDBG]] target triple = "x86_64-unknown-linux-gnu"