[SimplifyCFG] FoldTwoEntryPHINode(): consider *total* speculation cost, not per-BB...
[llvm-complete.git] / include / llvm / IR / RemarkStreamer.h
blob7b2fc95d15e5646b592d17be5d36b13f57d8f697
1 //===- llvm/IR/RemarkStreamer.h - Remark Streamer ---------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the main interface for outputting remarks.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_IR_REMARKSTREAMER_H
14 #define LLVM_IR_REMARKSTREAMER_H
16 #include "llvm/IR/DiagnosticInfo.h"
17 #include "llvm/Remarks/RemarkSerializer.h"
18 #include "llvm/Support/Error.h"
19 #include "llvm/Support/Regex.h"
20 #include "llvm/Support/ToolOutputFile.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <string>
23 #include <vector>
25 namespace llvm {
26 /// Streamer for remarks.
27 class RemarkStreamer {
28 /// The filename that the remark diagnostics are emitted to.
29 const std::string Filename;
30 /// The regex used to filter remarks based on the passes that emit them.
31 Optional<Regex> PassFilter;
32 /// The object used to serialize the remarks to a specific format.
33 std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer;
35 /// Convert diagnostics into remark objects.
36 /// The lifetime of the members of the result is bound to the lifetime of
37 /// the LLVM diagnostics.
38 remarks::Remark toRemark(const DiagnosticInfoOptimizationBase &Diag);
40 public:
41 RemarkStreamer(StringRef Filename,
42 std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer);
43 /// Return the filename that the remark diagnostics are emitted to.
44 StringRef getFilename() const { return Filename; }
45 /// Return stream that the remark diagnostics are emitted to.
46 raw_ostream &getStream() { return RemarkSerializer->OS; }
47 /// Return the serializer used for this stream.
48 remarks::RemarkSerializer &getSerializer() { return *RemarkSerializer; }
49 /// Set a pass filter based on a regex \p Filter.
50 /// Returns an error if the regex is invalid.
51 Error setFilter(StringRef Filter);
52 /// Emit a diagnostic through the streamer.
53 void emit(const DiagnosticInfoOptimizationBase &Diag);
56 template <typename ThisError>
57 struct RemarkSetupErrorInfo : public ErrorInfo<ThisError> {
58 std::string Msg;
59 std::error_code EC;
61 RemarkSetupErrorInfo(Error E) {
62 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
63 Msg = EIB.message();
64 EC = EIB.convertToErrorCode();
65 });
68 void log(raw_ostream &OS) const override { OS << Msg; }
69 std::error_code convertToErrorCode() const override { return EC; }
72 struct RemarkSetupFileError : RemarkSetupErrorInfo<RemarkSetupFileError> {
73 static char ID;
74 using RemarkSetupErrorInfo<RemarkSetupFileError>::RemarkSetupErrorInfo;
77 struct RemarkSetupPatternError : RemarkSetupErrorInfo<RemarkSetupPatternError> {
78 static char ID;
79 using RemarkSetupErrorInfo<RemarkSetupPatternError>::RemarkSetupErrorInfo;
82 struct RemarkSetupFormatError : RemarkSetupErrorInfo<RemarkSetupFormatError> {
83 static char ID;
84 using RemarkSetupErrorInfo<RemarkSetupFormatError>::RemarkSetupErrorInfo;
87 /// Setup optimization remarks.
88 Expected<std::unique_ptr<ToolOutputFile>>
89 setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
90 StringRef RemarksPasses, StringRef RemarksFormat,
91 bool RemarksWithHotness,
92 unsigned RemarksHotnessThreshold = 0);
94 } // end namespace llvm
96 #endif // LLVM_IR_REMARKSTREAMER_H