[CodeGenPrepare] Reset the debug location when promoting trunc(s)

The promotion machinery in CGP moves instructions retaining
debug locations. When the transformation is local, this is mostly
correct, but when instructions are moved cross-BBs, this is not
always true and causes jumpiness in line tables. This is the first
of a series of commits. sext(s) and zext(s) need to be treated
similarly.

Differential Revision:  https://reviews.llvm.org/D81879

GitOrigin-RevId: c2dccf9d5e34278cb2ed0a018405848850508b99
diff --git a/lib/CodeGen/CodeGenPrepare.cpp b/lib/CodeGen/CodeGenPrepare.cpp
index d238804..935fc95 100644
--- a/lib/CodeGen/CodeGenPrepare.cpp
+++ b/lib/CodeGen/CodeGenPrepare.cpp
@@ -2605,6 +2605,7 @@
     /// trunc Opnd to Ty.
     TruncBuilder(Instruction *Opnd, Type *Ty) : TypePromotionAction(Opnd) {
       IRBuilder<> Builder(Opnd);
+      Builder.SetCurrentDebugLocation(DebugLoc());
       Val = Builder.CreateTrunc(Opnd, Ty, "promoted");
       LLVM_DEBUG(dbgs() << "Do: TruncBuilder: " << *Val << "\n");
     }
diff --git a/test/Transforms/CodeGenPrepare/X86/promoted-trunc-loc.ll b/test/Transforms/CodeGenPrepare/X86/promoted-trunc-loc.ll
new file mode 100644
index 0000000..12b5a148
--- /dev/null
+++ b/test/Transforms/CodeGenPrepare/X86/promoted-trunc-loc.ll
@@ -0,0 +1,44 @@
+; RUN: opt < %s -codegenprepare -S -mtriple=x86_64-unknown-unknown | FileCheck %s
+
+; Make sure the promoted trunc doesn't get a debug location associated.
+; CHECK: %promoted = trunc i32 %or to i16
+
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.13.0"
+
+@b = global i16 0, align 2
+
+declare void @bar(i64)
+
+define i32 @foo(i16 %kkk) !dbg !6 {
+entry:
+  %t4 = load i16, i16* @b, align 2, !dbg !8
+  %conv4 = zext i16 %t4 to i32, !dbg !9
+  %or = or i16 %kkk, %t4, !dbg !10
+  %c = sext i16 %or to i64, !dbg !11
+  call void @bar(i64 %c), !dbg !12
+  %t5 = and i16 %or, 5, !dbg !13
+  %z = zext i16 %t5 to i32, !dbg !14
+  ret i32 %z, !dbg !15
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!3, !4}
+!llvm.module.flags = !{!5}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
+!1 = !DIFile(filename: "/Users/davide/work/llvm-project/patatino.ll", directory: "/")
+!2 = !{}
+!3 = !{i32 8}
+!4 = !{i32 0}
+!5 = !{i32 2, !"Debug Info Version", i32 3}
+!6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
+!7 = !DISubroutineType(types: !2)
+!8 = !DILocation(line: 1, column: 1, scope: !6)
+!9 = !DILocation(line: 2, column: 1, scope: !6)
+!10 = !DILocation(line: 3, column: 1, scope: !6)
+!11 = !DILocation(line: 4, column: 1, scope: !6)
+!12 = !DILocation(line: 5, column: 1, scope: !6)
+!13 = !DILocation(line: 6, column: 1, scope: !6)
+!14 = !DILocation(line: 7, column: 1, scope: !6)
+!15 = !DILocation(line: 8, column: 1, scope: !6)