Skip to content

Commit 4b14148

Browse files
authored
[GVN] Skip debug instructions in findDominatingValue function (llvm#65977)
findDominatingValue has a search limit, and when it is reached, optimization is not applied. This patch fixes the issue that this limit also takes into account debug intrinsics, so the result of optimization can depend from the presence of debug info.
1 parent 69b056d commit 4b14148

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,13 +1147,11 @@ static Value *findDominatingValue(const MemoryLocation &Loc, Type *LoadTy,
11471147
BasicBlock *FromBB = From->getParent();
11481148
BatchAAResults BatchAA(*AA);
11491149
for (BasicBlock *BB = FromBB; BB; BB = BB->getSinglePredecessor())
1150-
for (auto I = BB == FromBB ? From->getReverseIterator() : BB->rbegin(),
1151-
E = BB->rend();
1152-
I != E; ++I) {
1150+
for (auto *Inst = BB == FromBB ? From : BB->getTerminator();
1151+
Inst != nullptr; Inst = Inst->getPrevNonDebugInstruction()) {
11531152
// Stop the search if limit is reached.
11541153
if (++NumVisitedInsts > MaxNumVisitedInsts)
11551154
return nullptr;
1156-
Instruction *Inst = &*I;
11571155
if (isModSet(BatchAA.getModRefInfo(Inst, Loc)))
11581156
return nullptr;
11591157
if (auto *LI = dyn_cast<LoadInst>(Inst))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
2+
; RUN: opt < %s -passes=gvn -gvn-max-num-visited-insts=4 -S | FileCheck %s
3+
4+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
5+
6+
define i32 @foo(ptr %a, ptr %b) {
7+
; CHECK-LABEL: define i32 @foo(
8+
; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]]) {
9+
; CHECK-NEXT: entry:
10+
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[A]], align 4
11+
; CHECK-NEXT: call void @llvm.dbg.declare(metadata ptr undef, metadata [[META4:![0-9]+]], metadata !DIExpression()), !dbg [[DBG10:![0-9]+]]
12+
; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[B]], align 4
13+
; CHECK-NEXT: [[COND:%.*]] = icmp slt i32 [[TMP0]], [[TMP1]]
14+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[COND]], i32 [[TMP0]], i32 [[TMP1]]
15+
; CHECK-NEXT: [[PTR:%.*]] = select i1 [[COND]], ptr [[A]], ptr [[B]]
16+
; CHECK-NEXT: ret i32 [[TMP2]]
17+
;
18+
entry:
19+
%0 = load i32, ptr %a, align 4
20+
call void @llvm.dbg.declare(metadata ptr undef, metadata !46, metadata !DIExpression()), !dbg !40
21+
%1 = load i32, ptr %b, align 4
22+
%cond = icmp slt i32 %0, %1
23+
%ptr = select i1 %cond, ptr %a, ptr %b
24+
%res = load i32, ptr %ptr, align 4
25+
ret i32 %res
26+
}
27+
28+
!llvm.dbg.cu = !{!0}
29+
!llvm.module.flags = !{!35, !36}
30+
31+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 17.0.0.prerel", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
32+
!1 = !DIFile(filename: "bbi-78272.c", directory: "/tmp")
33+
!5 = !DIBasicType(name: "int", size: 16, encoding: DW_ATE_signed)
34+
35+
!35 = !{i32 7, !"Dwarf Version", i32 4}
36+
!36 = !{i32 2, !"Debug Info Version", i32 3}
37+
!40 = !DILocation(line: 15, column: 7, scope: !41)
38+
!41 = distinct !DISubprogram(name: "x", scope: !1, file: !1, line: 14, type: !42, scopeLine: 14, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !45)
39+
!42 = !DISubroutineType(types: !43)
40+
!43 = !{!5}
41+
!45 = !{!46}
42+
!46 = !DILocalVariable(name: "t", scope: !41, file: !1, line: 15, type: !5)

0 commit comments

Comments
 (0)