Index: lib/CodeGen/CGDecl.cpp =================================================================== --- lib/CodeGen/CGDecl.cpp +++ lib/CodeGen/CGDecl.cpp @@ -1946,6 +1946,10 @@ } } + // Set artificial debug location to preserve the scope. This is later + // used by mem2reg to assign DL at the phi's it generates. + auto DL = ApplyDebugLocation::CreateArtificial(*this); + Address DeclPtr = Address::invalid(); bool DoStore = false; bool IsScalar = hasScalarEvaluationKind(Ty); Index: test/CodeGen/debug-info-preserve-scope.c =================================================================== --- /dev/null +++ test/CodeGen/debug-info-preserve-scope.c @@ -0,0 +1,29 @@ +// RUN: %clang_cc1 -debug-info-kind=limited -emit-llvm -o - %s | FileCheck %s + +// RUN: %clang_cc1 -O1 -debug-info-kind=limited -emit-llvm -mllvm \ +// RUN: -opt-bisect-limit=2 -o - %s 2> /dev/null | FileCheck %s \ +// RUN: --check-prefix PHI + +extern int map[]; +// PHI-LABEL: define void @test1 +void test1(int a, int n) { + for (int i = 0; i < n; ++i) + a = map[a]; +} + +// PHI: for.cond: +// PHI-NEXT: {{.*}} = phi i32 {{.*}} !dbg ![[test1DbgLoc:[0-9]+]] + +// PHI: ![[test1DbgLoc]] = !DILocation(line: 0 + + +static int i; +// CHECK-LABEL: define void @test2 +void test2(int b) { + i = b; +} + +// CHECK: store i32 {{.*}} !dbg ![[test2DbgLoc:[0-9]+]] + +// CHECK: ![[test2DbgLoc]] = !DILocation(line: 0 +