diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp index f9aef064641d8..0bfce13b07f1c 100644 --- a/llvm/lib/Transforms/Scalar/Reassociate.cpp +++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp @@ -1086,13 +1086,15 @@ static unsigned FindInOperandList(const SmallVectorImpl &Ops, /// Emit a tree of add instructions, summing Ops together /// and returning the result. Insert the tree before I. -static Value *EmitAddTreeOfValues(BasicBlock::iterator It, +static Value *EmitAddTreeOfValues(Instruction *I, SmallVectorImpl &Ops) { if (Ops.size() == 1) return Ops.back(); Value *V1 = Ops.pop_back_val(); - Value *V2 = EmitAddTreeOfValues(It, Ops); - return CreateAdd(V2, V1, "reass.add", It, &*It); + Value *V2 = EmitAddTreeOfValues(I, Ops); + auto *NewAdd = CreateAdd(V2, V1, "reass.add", I->getIterator(), I); + NewAdd->setDebugLoc(I->getDebugLoc()); + return NewAdd; } /// If V is an expression tree that is a multiplication sequence, @@ -1682,7 +1684,7 @@ Value *ReassociatePass::OptimizeAdd(Instruction *I, DummyInst->deleteValue(); unsigned NumAddedValues = NewMulOps.size(); - Value *V = EmitAddTreeOfValues(I->getIterator(), NewMulOps); + Value *V = EmitAddTreeOfValues(I, NewMulOps); // Now that we have inserted the add tree, optimize it. This allows us to // handle cases that require multiple factoring steps, such as this: @@ -1694,6 +1696,7 @@ Value *ReassociatePass::OptimizeAdd(Instruction *I, // Create the multiply. Instruction *V2 = CreateMul(V, MaxOccVal, "reass.mul", I->getIterator(), I); + V2->setDebugLoc(I->getDebugLoc()); // Rerun associate on the multiply in case the inner expression turned into // a multiply. We want to make sure that we keep things in canonical form. diff --git a/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll b/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll new file mode 100644 index 0000000000000..aa04837431cd2 --- /dev/null +++ b/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll @@ -0,0 +1,56 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt -p=reassociate -S < %s | FileCheck %s + +;; Tests that when we reassociate %add93, we apply its debug location to the new +;; instructions. + +define void @foo(i32 %0) { +; CHECK-LABEL: define void @foo( +; CHECK-SAME: i32 [[TMP0:%.*]]) { +; CHECK-NEXT: [[ENTRY:.*:]] +; CHECK-NEXT: br label %[[FOR_COND23:.*]] +; CHECK: [[FOR_COND23]]: +; CHECK-NEXT: [[SUB59:%.*]] = sub i32 0, 0 +; CHECK-NEXT: [[MUL68:%.*]] = mul i32 0, [[TMP0]] +; CHECK-NEXT: [[REASS_ADD:%.*]] = add i32 [[MUL68]], [[TMP0]], !dbg [[DBG3:![0-9]+]] +; CHECK-NEXT: [[REASS_MUL1:%.*]] = mul i32 [[REASS_ADD]], [[SUB59]], !dbg [[DBG3]] +; CHECK-NEXT: [[REASS_MUL:%.*]] = add i32 [[REASS_MUL1]], 1, !dbg [[DBG3]] +; CHECK-NEXT: [[CONV95:%.*]] = trunc i32 [[REASS_MUL]] to i16 +; CHECK-NEXT: store i16 [[CONV95]], ptr null, align 2 +; CHECK-NEXT: br label %[[FOR_COND23]] +; +entry: + br label %for.cond23 + +for.cond23: ; preds = %for.cond23, %entry + %sub59 = sub i32 0, 0 + %mul62 = mul i32 %sub59, %0 + %mul68 = mul i32 %mul62, 0 + %mul77 = mul i32 %sub59, %0 + %add84 = or i32 %mul68, %mul77 + %add93 = add i32 %add84, 1, !dbg !4 + %conv95 = trunc i32 %add93 to i16 + store i16 %conv95, ptr null, align 2 + br label %for.cond23 +} + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3} + +!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git") +!1 = !DIFile(filename: "test.c", directory: "/tmp") +!2 = !{} +!3 = !{i32 2, !"Debug Info Version", i32 3} +!4 = !DILocation(line: 15, column: 50, scope: !5) +!5 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 14, type: !6, scopeLine: 14, unit: !0, retainedNodes: !2) +!6 = distinct !DISubroutineType(types: !7) +!7 = !{null} +;. +; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug) +; CHECK: [[META1]] = !DIFile(filename: "test.c", directory: {{.*}}) +; CHECK: [[DBG3]] = !DILocation(line: 15, column: 50, scope: [[META4:![0-9]+]]) +; CHECK: [[META4]] = distinct !DISubprogram(name: "foo", scope: [[META1]], file: [[META1]], line: 14, type: [[META5:![0-9]+]], scopeLine: 14, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META7:![0-9]+]]) +; CHECK: [[META5]] = distinct !DISubroutineType(types: [[META6:![0-9]+]]) +; CHECK: [[META6]] = !{null} +; CHECK: [[META7]] = !{} +;.