Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions llvm/lib/Transforms/Scalar/GVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre",
static cl::opt<bool> GVNEnableMemDep("enable-gvn-memdep", cl::init(true));
static cl::opt<bool> GVNEnableMemorySSA("enable-gvn-memoryssa",
cl::init(false));
static cl::opt<bool> GVNSkipZeroEntryCount("gvn-skip-zero-entry-count",
cl::init(false));

static cl::opt<uint32_t> MaxNumDeps(
"gvn-max-num-deps", cl::Hidden, cl::init(100),
Expand Down Expand Up @@ -894,6 +896,17 @@ PreservedAnalyses GVNPass::run(Function &F, FunctionAnalysisManager &AM) {
MSSA = &AM.getResult<MemorySSAAnalysis>(F);
}
auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);

// Skip the pass if function has zero entry count in PGO.
// This indicates that the function is never executed according to the profile
// data.
auto EntryCount = F.getEntryCount();
if (GVNSkipZeroEntryCount && EntryCount && EntryCount->getCount() == 0) {
LLVM_DEBUG(dbgs() << "GVN: Skipping function '" << F.getName()
<< "' with zero profile entry count\n");
return PreservedAnalyses::all();
}

bool Changed = runImpl(F, AC, DT, TLI, AA, MemDep, LI, &ORE,
MSSA ? &MSSA->getMSSA() : nullptr);
if (!Changed)
Expand Down
41 changes: 41 additions & 0 deletions llvm/test/Transforms/GVN/skip-gvn-blockfreq.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; Test that GVN is skipped when function has zero entry count in PGO
; RUN: opt -passes='gvn' -gvn-skip-zero-entry-count=true -S < %s | FileCheck %s

; Function with ZERO entry count - GVN should skip this function
; The redundant computation should remain because GVN doesn't run
; CHECK-LABEL: @zero_freq_function(
; CHECK-NEXT: entry:
; CHECK-NEXT: %a = add i32 %x, 1
; CHECK-NEXT: %b = add i32 %a, 2
; CHECK-NEXT: %c = add i32 %a, 2
; CHECK-NEXT: %result = add i32 %b, %c
; CHECK-NEXT: ret i32 %result
define i32 @zero_freq_function(i32 %x) !prof !0 {
entry:
%a = add i32 %x, 1
%b = add i32 %a, 2
%c = add i32 %a, 2 ; Redundant - but GVN should not optimize due to zero freq
%result = add i32 %b, %c
ret i32 %result
}

; Function with NON-ZERO entry count - GVN should run normally
; The redundant computation should be eliminated by GVN
; CHECK-LABEL: @nonzero_freq_function(
; CHECK-NEXT: entry:
; CHECK-NEXT: %a = add i32 %x, 1
; CHECK-NEXT: %b = add i32 %a, 2
; CHECK-NEXT: %result = add i32 %b, %b
; CHECK-NEXT: ret i32 %result
define i32 @nonzero_freq_function(i32 %x) !prof !1 {
entry:
%a = add i32 %x, 1
%b = add i32 %a, 2
%c = add i32 %a, 2 ; Redundant - GVN optimizes this
%result = add i32 %b, %c
ret i32 %result
}

!0 = !{!"function_entry_count", i64 0} ; Zero frequency
!1 = !{!"function_entry_count", i64 1000} ; Non-zero frequency

Loading