[llvm] [Reassociate] Apply Debugloc to instrs produced when optimizing add (PR #134676)
Stephen Tozer via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 8 09:02:05 PDT 2025
https://github.com/SLTozer updated https://github.com/llvm/llvm-project/pull/134676
>From 97aace373beb90b06d16f2cb15a3e9bbb1d35783 Mon Sep 17 00:00:00 2001
From: Stephen Tozer <stephen.tozer at sony.com>
Date: Tue, 29 Oct 2024 18:21:43 +0000
Subject: [PATCH 1/2] [Reassociate] Apply Debugloc to instrs produced when
optimizing add
Currently in Reassociate we may create a set of new instructions when
optimizing an `add`, but we do not set DebugLocs on the new instructions;
this patch propagates the add's DebugLoc to the new instructions.
---
llvm/lib/Transforms/Scalar/Reassociate.cpp | 11 ++--
.../Reassociate/debugloc-reass-add.ll | 59 +++++++++++++++++++
2 files changed, 66 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/Transforms/Reassociate/debugloc-reass-add.ll
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<ValueEntry> &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<WeakTrackingVH> &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..7fc94fdc13eca
--- /dev/null
+++ b/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll
@@ -0,0 +1,59 @@
+; 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.
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+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]] = !{}
+;.
>From 2ae20b8f12ce1a2e1d065bde1d305754ceac879d Mon Sep 17 00:00:00 2001
From: Stephen Tozer <stephen.tozer at sony.com>
Date: Tue, 8 Apr 2025 17:01:48 +0100
Subject: [PATCH 2/2] Remove explicit triple from test
---
llvm/test/Transforms/Reassociate/debugloc-reass-add.ll | 3 ---
1 file changed, 3 deletions(-)
diff --git a/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll b/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll
index 7fc94fdc13eca..aa04837431cd2 100644
--- a/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll
+++ b/llvm/test/Transforms/Reassociate/debugloc-reass-add.ll
@@ -4,9 +4,6 @@
;; Tests that when we reassociate %add93, we apply its debug location to the new
;; instructions.
-target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
-target triple = "x86_64-unknown-linux-gnu"
-
define void @foo(i32 %0) {
; CHECK-LABEL: define void @foo(
; CHECK-SAME: i32 [[TMP0:%.*]]) {
More information about the llvm-commits
mailing list