From 6b283333243cf618bd4b25ab5886beb3094eab96 Mon Sep 17 00:00:00 2001 From: Xinliang David Li Date: Sun, 5 Jun 2016 05:12:23 +0000 Subject: [PATCH] [PM] Port GCOVProfiler pass to the new pass manager git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271823 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Transforms/GCOVProfiler.h | 31 ++++++++++++++++++++++ lib/Passes/PassBuilder.cpp | 1 + lib/Passes/PassRegistry.def | 1 + lib/Transforms/Instrumentation/GCOVProfiling.cpp | 14 +++++++++- .../Transforms/GCOVProfiling/function-numbering.ll | 4 +++ test/Transforms/GCOVProfiling/global-ctor.ll | 4 +++ test/Transforms/GCOVProfiling/linezero.ll | 3 +++ test/Transforms/GCOVProfiling/linkagename.ll | 4 +++ test/Transforms/GCOVProfiling/modules.ll | 1 + test/Transforms/GCOVProfiling/return-block.ll | 9 +++++++ test/Transforms/GCOVProfiling/version.ll | 8 ++++++ 11 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 include/llvm/Transforms/GCOVProfiler.h diff --git a/include/llvm/Transforms/GCOVProfiler.h b/include/llvm/Transforms/GCOVProfiler.h new file mode 100644 index 00000000000..f6521901a33 --- /dev/null +++ b/include/llvm/Transforms/GCOVProfiler.h @@ -0,0 +1,31 @@ +//===- Transforms/GCOVProfiler.h - GCOVProfiler pass ----------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// \file +/// This file provides the interface for the GCOV style profiler pass. +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_GCOVPROFILER_H +#define LLVM_TRANSFORMS_GCOVPROFILER_H + +#include "llvm/IR/PassManager.h" +#include "llvm/Transforms/Instrumentation.h" + +namespace llvm { +/// The gcov-style instrumentation pass +class GCOVProfilerPass : public PassInfoMixin { +public: + GCOVProfilerPass(const GCOVOptions &Options = GCOVOptions::getDefault()) : GCOVOpts(Options) { } + PreservedAnalyses run(Module &M, AnalysisManager &AM); + +private: + GCOVOptions GCOVOpts; +}; + +} // End llvm namespace +#endif diff --git a/lib/Passes/PassBuilder.cpp b/lib/Passes/PassBuilder.cpp index 7ca5c7b7b3f..fca3e2534ed 100644 --- a/lib/Passes/PassBuilder.cpp +++ b/lib/Passes/PassBuilder.cpp @@ -50,6 +50,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/Regex.h" #include "llvm/Target/TargetMachine.h" +#include "llvm/Transforms/GCOVProfiler.h" #include "llvm/Transforms/IPO/ConstantMerge.h" #include "llvm/Transforms/IPO/ElimAvailExtern.h" #include "llvm/Transforms/IPO/ForceFunctionAttrs.h" diff --git a/lib/Passes/PassRegistry.def b/lib/Passes/PassRegistry.def index fcc00bafcae..398da320171 100644 --- a/lib/Passes/PassRegistry.def +++ b/lib/Passes/PassRegistry.def @@ -43,6 +43,7 @@ MODULE_PASS("forceattrs", ForceFunctionAttrsPass()) MODULE_PASS("globaldce", GlobalDCEPass()) MODULE_PASS("globalopt", GlobalOptPass()) MODULE_PASS("inferattrs", InferFunctionAttrsPass()) +MODULE_PASS("insert-gcov-profiling", GCOVProfilerPass()) MODULE_PASS("internalize", InternalizePass()) MODULE_PASS("instrprof", InstrProfiling()) MODULE_PASS("invalidate", InvalidateAllAnalysesPass()) diff --git a/lib/Transforms/Instrumentation/GCOVProfiling.cpp b/lib/Transforms/Instrumentation/GCOVProfiling.cpp index f7ace857a37..9b19833cb75 100644 --- a/lib/Transforms/Instrumentation/GCOVProfiling.cpp +++ b/lib/Transforms/Instrumentation/GCOVProfiling.cpp @@ -14,7 +14,6 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Transforms/Instrumentation.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/STLExtras.h" @@ -35,6 +34,8 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/GCOVProfiler.h" +#include "llvm/Transforms/Instrumentation.h" #include "llvm/Transforms/Utils/ModuleUtils.h" #include #include @@ -464,6 +465,17 @@ bool GCOVProfiler::runOnModule(Module &M) { return false; } +PreservedAnalyses GCOVProfilerPass::run(Module &M, + AnalysisManager &AM) { + + GCOVProfiler Profiler(GCOVOpts); + + if (!Profiler.runOnModule(M)) + return PreservedAnalyses::all(); + + return PreservedAnalyses::none(); +} + static bool functionHasLines(Function &F) { // Check whether this function actually has any source lines. Not only // do these waste space, they also can crash gcov. diff --git a/test/Transforms/GCOVProfiling/function-numbering.ll b/test/Transforms/GCOVProfiling/function-numbering.ll index f66ce243ac3..6f10fd23cb0 100644 --- a/test/Transforms/GCOVProfiling/function-numbering.ll +++ b/test/Transforms/GCOVProfiling/function-numbering.ll @@ -7,6 +7,10 @@ ; RUN: opt -insert-gcov-profiling -S < %t2 | FileCheck --check-prefix GCDA %s ; RUN: llvm-cov gcov -n -dump %T/function-numbering.gcno 2>&1 | FileCheck --check-prefix GCNO %s +; RUNN: rm %T/function-numbering.gcno + +; RUN: opt -passes=insert-gcov-profiling -S < %t2 | FileCheck --check-prefix GCDA %s +; RUN: llvm-cov gcov -n -dump %T/function-numbering.gcno 2>&1 | FileCheck --check-prefix GCNO %s target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.10.0" diff --git a/test/Transforms/GCOVProfiling/global-ctor.ll b/test/Transforms/GCOVProfiling/global-ctor.ll index 1275933850f..e20da85eb58 100644 --- a/test/Transforms/GCOVProfiling/global-ctor.ll +++ b/test/Transforms/GCOVProfiling/global-ctor.ll @@ -4,6 +4,10 @@ ; RUN: not grep '_GLOBAL__sub_I_global-ctor' %T/global-ctor.gcno ; RUN: rm %T/global-ctor.gcno +; RUN: opt -passes=insert-gcov-profiling -disable-output < %t2 +; RUN: not grep '_GLOBAL__sub_I_global-ctor' %T/global-ctor.gcno +; RUN: rm %T/global-ctor.gcno + @x = global i32 0, align 4 @llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_global-ctor.ll, i8* null }] diff --git a/test/Transforms/GCOVProfiling/linezero.ll b/test/Transforms/GCOVProfiling/linezero.ll index 2298f125440..a2e5381e4db 100644 --- a/test/Transforms/GCOVProfiling/linezero.ll +++ b/test/Transforms/GCOVProfiling/linezero.ll @@ -1,6 +1,9 @@ ; RUN: sed -e 's|PATTERN|%/T|g' %s | opt -insert-gcov-profiling -disable-output ; RUN: rm %T/linezero.gcno +; RUN: sed -e 's|PATTERN|%/T|g' %s | opt -passes=insert-gcov-profiling -disable-output +; RUN: rm %T/linezero.gcno + ; This is a crash test. target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" diff --git a/test/Transforms/GCOVProfiling/linkagename.ll b/test/Transforms/GCOVProfiling/linkagename.ll index b076d932ea8..dc46a35a185 100644 --- a/test/Transforms/GCOVProfiling/linkagename.ll +++ b/test/Transforms/GCOVProfiling/linkagename.ll @@ -4,6 +4,10 @@ ; RUN: grep _Z3foov %T/linkagename.gcno ; RUN: rm %T/linkagename.gcno +; RUN: opt -passes=insert-gcov-profiling -disable-output < %t2 +; RUN: grep _Z3foov %T/linkagename.gcno +; RUN: rm %T/linkagename.gcno + define void @_Z3foov() !dbg !5 { entry: ret void, !dbg !8 diff --git a/test/Transforms/GCOVProfiling/modules.ll b/test/Transforms/GCOVProfiling/modules.ll index fa51d2943b8..460d4dfdfa1 100644 --- a/test/Transforms/GCOVProfiling/modules.ll +++ b/test/Transforms/GCOVProfiling/modules.ll @@ -1,4 +1,5 @@ ; RUN: opt -insert-gcov-profiling -o - < %s | llvm-dis | FileCheck -check-prefix=EMIT-ARCS %s +; RUN: opt -passes=insert-gcov-profiling -o - < %s | llvm-dis | FileCheck -check-prefix=EMIT-ARCS %s ; EMIT-ARCS-NOT: call void @llvm_gcda_start_file diff --git a/test/Transforms/GCOVProfiling/return-block.ll b/test/Transforms/GCOVProfiling/return-block.ll index ee9ca1da23a..424e0b581f0 100644 --- a/test/Transforms/GCOVProfiling/return-block.ll +++ b/test/Transforms/GCOVProfiling/return-block.ll @@ -9,6 +9,15 @@ ; But we can optionally emit it second, to match newer gcc versions. ; RUN: opt -insert-gcov-profiling -gcov-exit-block-before-body -disable-output %t2 ; RUN: llvm-cov gcov -n -dump %T/return-block.gcno 2>&1 | FileCheck -check-prefix=CHECK -check-prefix=RETURN-SECOND %s +; RUN: rm %T/return-block.gcno + +; By default, the return block is last. +; RUN: opt -passes=insert-gcov-profiling -disable-output %t2 +; RUN: llvm-cov gcov -n -dump %T/return-block.gcno 2>&1 | FileCheck -check-prefix=CHECK -check-prefix=RETURN-LAST %s + +; But we can optionally emit it second, to match newer gcc versions. +; RUN: opt -passes=insert-gcov-profiling -gcov-exit-block-before-body -disable-output %t2 +; RUN: llvm-cov gcov -n -dump %T/return-block.gcno 2>&1 | FileCheck -check-prefix=CHECK -check-prefix=RETURN-SECOND %s target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" diff --git a/test/Transforms/GCOVProfiling/version.ll b/test/Transforms/GCOVProfiling/version.ll index e5ffa33c648..da70ac46357 100644 --- a/test/Transforms/GCOVProfiling/version.ll +++ b/test/Transforms/GCOVProfiling/version.ll @@ -8,6 +8,14 @@ ; RUN: head -c8 %T/version.gcno | grep '^oncg.704' ; RUN: rm %T/version.gcno +; RUN: opt -passes=insert-gcov-profiling -disable-output < %t2 +; RUN: head -c8 %T/version.gcno | grep '^oncg.204' +; RUN: rm %T/version.gcno +; RUN: not opt -passes=insert-gcov-profiling -default-gcov-version=asdfasdf -disable-output < %t2 +; RUN: opt -passes=insert-gcov-profiling -default-gcov-version=407* -disable-output < %t2 +; RUN: head -c8 %T/version.gcno | grep '^oncg.704' +; RUN: rm %T/version.gcno + define void @test() !dbg !5 { ret void, !dbg !8 } -- 2.11.0