Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84fde79

Browse files
authored
[Reassociate] Apply Debugloc to instrs produced when optimizing add (llvm#134676)
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. Found using llvm#107279.
1 parent 4a7b34d commit 84fde79

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

llvm/lib/Transforms/Scalar/Reassociate.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -1086,13 +1086,15 @@ static unsigned FindInOperandList(const SmallVectorImpl<ValueEntry> &Ops,
10861086

10871087
/// Emit a tree of add instructions, summing Ops together
10881088
/// and returning the result. Insert the tree before I.
1089-
static Value *EmitAddTreeOfValues(BasicBlock::iterator It,
1089+
static Value *EmitAddTreeOfValues(Instruction *I,
10901090
SmallVectorImpl<WeakTrackingVH> &Ops) {
10911091
if (Ops.size() == 1) return Ops.back();
10921092

10931093
Value *V1 = Ops.pop_back_val();
1094-
Value *V2 = EmitAddTreeOfValues(It, Ops);
1095-
return CreateAdd(V2, V1, "reass.add", It, &*It);
1094+
Value *V2 = EmitAddTreeOfValues(I, Ops);
1095+
auto *NewAdd = CreateAdd(V2, V1, "reass.add", I->getIterator(), I);
1096+
NewAdd->setDebugLoc(I->getDebugLoc());
1097+
return NewAdd;
10961098
}
10971099

10981100
/// If V is an expression tree that is a multiplication sequence,
@@ -1682,7 +1684,7 @@ Value *ReassociatePass::OptimizeAdd(Instruction *I,
16821684
DummyInst->deleteValue();
16831685

16841686
unsigned NumAddedValues = NewMulOps.size();
1685-
Value *V = EmitAddTreeOfValues(I->getIterator(), NewMulOps);
1687+
Value *V = EmitAddTreeOfValues(I, NewMulOps);
16861688

16871689
// Now that we have inserted the add tree, optimize it. This allows us to
16881690
// handle cases that require multiple factoring steps, such as this:
@@ -1694,6 +1696,7 @@ Value *ReassociatePass::OptimizeAdd(Instruction *I,
16941696

16951697
// Create the multiply.
16961698
Instruction *V2 = CreateMul(V, MaxOccVal, "reass.mul", I->getIterator(), I);
1699+
V2->setDebugLoc(I->getDebugLoc());
16971700

16981701
// Rerun associate on the multiply in case the inner expression turned into
16991702
// a multiply. We want to make sure that we keep things in canonical form.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -p=reassociate -S < %s | FileCheck %s
3+
4+
;; Tests that when we reassociate %add93, we apply its debug location to the new
5+
;; instructions.
6+
7+
define void @foo(i32 %0) {
8+
; CHECK-LABEL: define void @foo(
9+
; CHECK-SAME: i32 [[TMP0:%.*]]) {
10+
; CHECK-NEXT: [[ENTRY:.*:]]
11+
; CHECK-NEXT: br label %[[FOR_COND23:.*]]
12+
; CHECK: [[FOR_COND23]]:
13+
; CHECK-NEXT: [[SUB59:%.*]] = sub i32 0, 0
14+
; CHECK-NEXT: [[MUL68:%.*]] = mul i32 0, [[TMP0]]
15+
; CHECK-NEXT: [[REASS_ADD:%.*]] = add i32 [[MUL68]], [[TMP0]], !dbg [[DBG3:![0-9]+]]
16+
; CHECK-NEXT: [[REASS_MUL1:%.*]] = mul i32 [[REASS_ADD]], [[SUB59]], !dbg [[DBG3]]
17+
; CHECK-NEXT: [[REASS_MUL:%.*]] = add i32 [[REASS_MUL1]], 1, !dbg [[DBG3]]
18+
; CHECK-NEXT: [[CONV95:%.*]] = trunc i32 [[REASS_MUL]] to i16
19+
; CHECK-NEXT: store i16 [[CONV95]], ptr null, align 2
20+
; CHECK-NEXT: br label %[[FOR_COND23]]
21+
;
22+
entry:
23+
br label %for.cond23
24+
25+
for.cond23: ; preds = %for.cond23, %entry
26+
%sub59 = sub i32 0, 0
27+
%mul62 = mul i32 %sub59, %0
28+
%mul68 = mul i32 %mul62, 0
29+
%mul77 = mul i32 %sub59, %0
30+
%add84 = or i32 %mul68, %mul77
31+
%add93 = add i32 %add84, 1, !dbg !4
32+
%conv95 = trunc i32 %add93 to i16
33+
store i16 %conv95, ptr null, align 2
34+
br label %for.cond23
35+
}
36+
37+
!llvm.dbg.cu = !{!0}
38+
!llvm.module.flags = !{!3}
39+
40+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 20.0.0git")
41+
!1 = !DIFile(filename: "test.c", directory: "/tmp")
42+
!2 = !{}
43+
!3 = !{i32 2, !"Debug Info Version", i32 3}
44+
!4 = !DILocation(line: 15, column: 50, scope: !5)
45+
!5 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 14, type: !6, scopeLine: 14, unit: !0, retainedNodes: !2)
46+
!6 = distinct !DISubroutineType(types: !7)
47+
!7 = !{null}
48+
;.
49+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], producer: "{{.*}}clang version {{.*}}", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug)
50+
; CHECK: [[META1]] = !DIFile(filename: "test.c", directory: {{.*}})
51+
; CHECK: [[DBG3]] = !DILocation(line: 15, column: 50, scope: [[META4:![0-9]+]])
52+
; 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]+]])
53+
; CHECK: [[META5]] = distinct !DISubroutineType(types: [[META6:![0-9]+]])
54+
; CHECK: [[META6]] = !{null}
55+
; CHECK: [[META7]] = !{}
56+
;.

0 commit comments

Comments
 (0)