Skip to content

Commit

Permalink
[GVN] Improve analysis for missed optimization remark
Browse files Browse the repository at this point in the history
This change tries to handle multiple dominating users of the pointer operand
by choosing the most immediately dominating one, if possible.  While making
this change I also found that the previous implementation had a missing break
statement, making all loads with an odd number of dominating users emit an
OtherAccess value, so that has also been fixed.

Patch by Henrik G Olsson!

Differential Revision: https://reviews.llvm.org/D79097
  • Loading branch information
anemet committed May 18, 2021
1 parent 15d4ed6 commit ab1f6ff
Show file tree
Hide file tree
Showing 3 changed files with 388 additions and 7 deletions.
54 changes: 47 additions & 7 deletions llvm/lib/Transforms/Scalar/GVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,17 @@ static bool isLifetimeStart(const Instruction *Inst) {
return false;
}

/// Assuming To can be reached from both From and Between, does Between lie on
/// every path from From to To?
static bool liesBetween(const Instruction *From, Instruction *Between,
const Instruction *To, DominatorTree *DT) {
if (From->getParent() == Between->getParent())
return DT->dominates(From, Between);
SmallSet<BasicBlock *, 1> Exclusion;
Exclusion.insert(Between->getParent());
return !isPotentiallyReachable(From, To, &Exclusion, DT);
}

/// Try to locate the three instruction involved in a missed
/// load-elimination case that is due to an intervening store.
static void reportMayClobberedLoad(LoadInst *Load, MemDepResult DepInfo,
Expand All @@ -944,17 +955,46 @@ static void reportMayClobberedLoad(LoadInst *Load, MemDepResult DepInfo,
R << "load of type " << NV("Type", Load->getType()) << " not eliminated"
<< setExtraArgs();

for (auto *U : Load->getPointerOperand()->users())
for (auto *U : Load->getPointerOperand()->users()) {
if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U)) &&
cast<Instruction>(U)->getFunction() == Load->getFunction() &&
DT->dominates(cast<Instruction>(U), Load)) {
// FIXME: for now give up if there are multiple memory accesses that
// dominate the load. We need further analysis to decide which one is
// that we're forwarding from.
if (OtherAccess)
OtherAccess = nullptr;
else
// Use the most immediately dominating value
if (OtherAccess) {
if (DT->dominates(cast<Instruction>(OtherAccess), cast<Instruction>(U)))
OtherAccess = U;
else
assert(DT->dominates(cast<Instruction>(U),
cast<Instruction>(OtherAccess)));
} else
OtherAccess = U;
}
}

if (!OtherAccess) {
// There is no dominating use, check if we can find a closest non-dominating
// use that lies between any other potentially available use and Load.
for (auto *U : Load->getPointerOperand()->users()) {
if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U)) &&
cast<Instruction>(U)->getFunction() == Load->getFunction() &&
isPotentiallyReachable(cast<Instruction>(U), Load, nullptr, DT)) {
if (OtherAccess) {
if (liesBetween(cast<Instruction>(OtherAccess), cast<Instruction>(U),
Load, DT)) {
OtherAccess = U;
} else if (!liesBetween(cast<Instruction>(U),
cast<Instruction>(OtherAccess), Load, DT)) {
// These uses are both partially available at Load were it not for
// the clobber, but neither lies strictly after the other.
OtherAccess = nullptr;
break;
} // else: keep current OtherAccess since it lies between U and Load
} else {
OtherAccess = U;
}
}
}
}

if (OtherAccess)
R << " in favor of " << NV("OtherAccess", OtherAccess);
Expand Down
136 changes: 136 additions & 0 deletions llvm/test/Transforms/GVN/opt-remarks-multiple-users.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
; RUN: opt < %s -gvn -o /dev/null -pass-remarks-output=%t -S
; RUN: cat %t | FileCheck %s

; CHECK: --- !Missed
; CHECK-NEXT: Pass: gvn
; CHECK-NEXT: Name: LoadClobbered
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 2, Column: 2 }
; CHECK-NEXT: Function: multipleUsers
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: 'load of type '
; CHECK-NEXT: - Type: i32
; CHECK-NEXT: - String: ' not eliminated'
; CHECK-NEXT: - String: ' in favor of '
; CHECK-NEXT: - OtherAccess: store
; CHECK-NEXT: - String: ' because it is clobbered by '
; CHECK-NEXT: - ClobberedBy: call
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 1, Column: 1 }
; CHECK-NEXT: ...
; CHECK: --- !Missed
; CHECK-NEXT: Pass: gvn
; CHECK-NEXT: Name: LoadClobbered
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 4, Column: 4 }
; CHECK-NEXT: Function: multipleUsers
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: 'load of type '
; CHECK-NEXT: - Type: i32
; CHECK-NEXT: - String: ' not eliminated'
; CHECK-NEXT: - String: ' in favor of '
; CHECK-NEXT: - OtherAccess: load
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 2, Column: 2 }
; CHECK-NEXT: - String: ' because it is clobbered by '
; CHECK-NEXT: - ClobberedBy: call
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 3, Column: 3 }
; CHECK-NEXT: ...

; ModuleID = 'bugpoint-reduced-simplified.bc'
source_filename = "gvn-test.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Tests that a last clobbering use can be determined even in the presence of
; multiple users, given that one of them lies on a path between every other
; potentially clobbering use and the load.

define dso_local void @multipleUsers(i32* %a, i32 %b) local_unnamed_addr #0 {
entry:
store i32 %b, i32* %a, align 4
tail call void @clobberingFunc() #1, !dbg !10
%0 = load i32, i32* %a, align 4, !dbg !11
tail call void @clobberingFunc() #1, !dbg !12
%1 = load i32, i32* %a, align 4, !dbg !13
%add2 = add nsw i32 %1, %0
ret void
}

; CHECK: --- !Missed
; CHECK-NEXT: Pass: gvn
; CHECK-NEXT: Name: LoadClobbered
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 2, Column: 2 }
; CHECK-NEXT: Function: multipleUsers2
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: 'load of type '
; CHECK-NEXT: - Type: i32
; CHECK-NEXT: - String: ' not eliminated'
; CHECK-NEXT: - String: ' in favor of '
; CHECK-NEXT: - OtherAccess: store
; CHECK-NEXT: - String: ' because it is clobbered by '
; CHECK-NEXT: - ClobberedBy: call
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 1, Column: 1 }
; CHECK-NEXT: ...
; CHECK: --- !Missed
; CHECK-NEXT: Pass: gvn
; CHECK-NEXT: Name: LoadClobbered
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 4, Column: 4 }
; CHECK-NEXT: Function: multipleUsers2
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: 'load of type '
; CHECK-NEXT: - Type: i32
; CHECK-NEXT: - String: ' not eliminated'
; CHECK-NEXT: - String: ' in favor of '
; CHECK-NEXT: - OtherAccess: load
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 2, Column: 2 }
; CHECK-NEXT: - String: ' because it is clobbered by '
; CHECK-NEXT: - ClobberedBy: call
; CHECK-NEXT: DebugLoc: { File: '/tmp/s.c', Line: 3, Column: 3 }
; CHECK-NEXT: ...

; Ignore uses in other functions

define dso_local void @multipleUsers2(i32 %b) local_unnamed_addr #0 {
entry:
store i32 %b, i32* @g, align 4
tail call void @clobberingFunc() #1, !dbg !15
%0 = load i32, i32* @g, align 4, !dbg !16
tail call void @clobberingFunc() #1, !dbg !17
%1 = load i32, i32* @g, align 4, !dbg !18
%add3 = add nsw i32 %1, %0
ret void
}

declare dso_local void @clobberingFunc() local_unnamed_addr #0

@g = external global i32

define dso_local void @globalUser(i32 %b) local_unnamed_addr #0 {
entry:
store i32 %b, i32* @g, align 4
ret void
}


attributes #0 = { "use-soft-float"="false" }
attributes #1 = { nounwind }

!llvm.dbg.cu = !{!1}
!llvm.module.flags = !{!4, !5, !6}
!llvm.ident = !{!0}

!0 = !{!"clang version 10.0.0 (git@github.com:llvm/llvm-project.git a2f6ae9abffcba260c22bb235879f0576bf3b783)"}
!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "clang version 4.0.0 (trunk 282540) (llvm/trunk 282542)", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !3)
!2 = !DIFile(filename: "/tmp/s.c", directory: "/tmp")
!3 = !{}
!4 = !{i32 2, !"Dwarf Version", i32 4}
!5 = !{i32 2, !"Debug Info Version", i32 3}
!6 = !{i32 1, !"PIC Level", i32 2}
!8 = distinct !DISubprogram(name: "multipleUsers", scope: !2, file: !2, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !1, retainedNodes: !3)
!9 = !DISubroutineType(types: !3)
!10 = !DILocation(line: 1, column: 1, scope: !8)
!11 = !DILocation(line: 2, column: 2, scope: !8)
!12 = !DILocation(line: 3, column: 3, scope: !8)
!13 = !DILocation(line: 4, column: 4, scope: !8)
!14 = distinct !DISubprogram(name: "multipleUsers2", scope: !2, file: !2, line: 2, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !1, retainedNodes: !3)
!15 = !DILocation(line: 1, column: 1, scope: !14)
!16 = !DILocation(line: 2, column: 2, scope: !14)
!17 = !DILocation(line: 3, column: 3, scope: !14)
!18 = !DILocation(line: 4, column: 4, scope: !14)
Loading

0 comments on commit ab1f6ff

Please sign in to comment.