OSDN Git Service

Bitcode: Remove the remnants of the BitcodeDiagnosticInfo class.
[android-x86/external-llvm.git] / include / llvm / IR / DiagnosticInfo.h
1 //===- llvm/IR/DiagnosticInfo.h - Diagnostic Declaration --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the different classes involved in low level diagnostics.
11 //
12 // Diagnostics reporting is still done as part of the LLVMContext.
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_IR_DIAGNOSTICINFO_H
16 #define LLVM_IR_DIAGNOSTICINFO_H
17
18 #include "llvm-c/Types.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/IR/DebugLoc.h"
24 #include "llvm/Support/CBindingWrapping.h"
25 #include "llvm/Support/YAMLTraits.h"
26 #include <functional>
27 #include <string>
28
29 namespace llvm {
30
31 // Forward declarations.
32 class DiagnosticPrinter;
33 class Function;
34 class Instruction;
35 class LLVMContext;
36 class Module;
37 class SMDiagnostic;
38
39 /// \brief Defines the different supported severity of a diagnostic.
40 enum DiagnosticSeverity : char {
41   DS_Error,
42   DS_Warning,
43   DS_Remark,
44   // A note attaches additional information to one of the previous diagnostic
45   // types.
46   DS_Note
47 };
48
49 /// \brief Defines the different supported kind of a diagnostic.
50 /// This enum should be extended with a new ID for each added concrete subclass.
51 enum DiagnosticKind {
52   DK_InlineAsm,
53   DK_ResourceLimit,
54   DK_StackSize,
55   DK_Linker,
56   DK_DebugMetadataVersion,
57   DK_DebugMetadataInvalid,
58   DK_ISelFallback,
59   DK_SampleProfile,
60   DK_OptimizationRemark,
61   DK_OptimizationRemarkMissed,
62   DK_OptimizationRemarkAnalysis,
63   DK_OptimizationRemarkAnalysisFPCommute,
64   DK_OptimizationRemarkAnalysisAliasing,
65   DK_OptimizationFailure,
66   DK_FirstRemark = DK_OptimizationRemark,
67   DK_LastRemark = DK_OptimizationFailure,
68   DK_MIRParser,
69   DK_PGOProfile,
70   DK_Unsupported,
71   DK_FirstPluginKind
72 };
73
74 /// \brief Get the next available kind ID for a plugin diagnostic.
75 /// Each time this function is called, it returns a different number.
76 /// Therefore, a plugin that wants to "identify" its own classes
77 /// with a dynamic identifier, just have to use this method to get a new ID
78 /// and assign it to each of its classes.
79 /// The returned ID will be greater than or equal to DK_FirstPluginKind.
80 /// Thus, the plugin identifiers will not conflict with the
81 /// DiagnosticKind values.
82 int getNextAvailablePluginDiagnosticKind();
83
84 /// \brief This is the base abstract class for diagnostic reporting in
85 /// the backend.
86 /// The print method must be overloaded by the subclasses to print a
87 /// user-friendly message in the client of the backend (let us call it a
88 /// frontend).
89 class DiagnosticInfo {
90 private:
91   /// Kind defines the kind of report this is about.
92   const /* DiagnosticKind */ int Kind;
93   /// Severity gives the severity of the diagnostic.
94   const DiagnosticSeverity Severity;
95
96 public:
97   DiagnosticInfo(/* DiagnosticKind */ int Kind, DiagnosticSeverity Severity)
98       : Kind(Kind), Severity(Severity) {}
99
100   virtual ~DiagnosticInfo() {}
101
102   /* DiagnosticKind */ int getKind() const { return Kind; }
103   DiagnosticSeverity getSeverity() const { return Severity; }
104
105   /// Print using the given \p DP a user-friendly message.
106   /// This is the default message that will be printed to the user.
107   /// It is used when the frontend does not directly take advantage
108   /// of the information contained in fields of the subclasses.
109   /// The printed message must not end with '.' nor start with a severity
110   /// keyword.
111   virtual void print(DiagnosticPrinter &DP) const = 0;
112 };
113
114 typedef std::function<void(const DiagnosticInfo &)> DiagnosticHandlerFunction;
115
116 /// Diagnostic information for inline asm reporting.
117 /// This is basically a message and an optional location.
118 class DiagnosticInfoInlineAsm : public DiagnosticInfo {
119 private:
120   /// Optional line information. 0 if not set.
121   unsigned LocCookie;
122   /// Message to be reported.
123   const Twine &MsgStr;
124   /// Optional origin of the problem.
125   const Instruction *Instr;
126
127 public:
128   /// \p MsgStr is the message to be reported to the frontend.
129   /// This class does not copy \p MsgStr, therefore the reference must be valid
130   /// for the whole life time of the Diagnostic.
131   DiagnosticInfoInlineAsm(const Twine &MsgStr,
132                           DiagnosticSeverity Severity = DS_Error)
133       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(0), MsgStr(MsgStr),
134         Instr(nullptr) {}
135
136   /// \p LocCookie if non-zero gives the line number for this report.
137   /// \p MsgStr gives the message.
138   /// This class does not copy \p MsgStr, therefore the reference must be valid
139   /// for the whole life time of the Diagnostic.
140   DiagnosticInfoInlineAsm(unsigned LocCookie, const Twine &MsgStr,
141                           DiagnosticSeverity Severity = DS_Error)
142       : DiagnosticInfo(DK_InlineAsm, Severity), LocCookie(LocCookie),
143         MsgStr(MsgStr), Instr(nullptr) {}
144
145   /// \p Instr gives the original instruction that triggered the diagnostic.
146   /// \p MsgStr gives the message.
147   /// This class does not copy \p MsgStr, therefore the reference must be valid
148   /// for the whole life time of the Diagnostic.
149   /// Same for \p I.
150   DiagnosticInfoInlineAsm(const Instruction &I, const Twine &MsgStr,
151                           DiagnosticSeverity Severity = DS_Error);
152
153   unsigned getLocCookie() const { return LocCookie; }
154   const Twine &getMsgStr() const { return MsgStr; }
155   const Instruction *getInstruction() const { return Instr; }
156
157   /// \see DiagnosticInfo::print.
158   void print(DiagnosticPrinter &DP) const override;
159
160   static bool classof(const DiagnosticInfo *DI) {
161     return DI->getKind() == DK_InlineAsm;
162   }
163 };
164
165 /// Diagnostic information for stack size etc. reporting.
166 /// This is basically a function and a size.
167 class DiagnosticInfoResourceLimit : public DiagnosticInfo {
168 private:
169   /// The function that is concerned by this resource limit diagnostic.
170   const Function &Fn;
171
172   /// Description of the resource type (e.g. stack size)
173   const char *ResourceName;
174
175   /// The computed size usage
176   uint64_t ResourceSize;
177
178   // Threshould passed
179   uint64_t ResourceLimit;
180
181 public:
182   /// \p The function that is concerned by this stack size diagnostic.
183   /// \p The computed stack size.
184   DiagnosticInfoResourceLimit(const Function &Fn,
185                               const char *ResourceName,
186                               uint64_t ResourceSize,
187                               DiagnosticSeverity Severity = DS_Warning,
188                               DiagnosticKind Kind = DK_ResourceLimit,
189                               uint64_t ResourceLimit = 0)
190       : DiagnosticInfo(Kind, Severity),
191         Fn(Fn),
192         ResourceName(ResourceName),
193         ResourceSize(ResourceSize),
194         ResourceLimit(ResourceLimit) {}
195
196   const Function &getFunction() const { return Fn; }
197   const char *getResourceName() const { return ResourceName; }
198   uint64_t getResourceSize() const { return ResourceSize; }
199   uint64_t getResourceLimit() const { return ResourceLimit; }
200
201   /// \see DiagnosticInfo::print.
202   void print(DiagnosticPrinter &DP) const override;
203
204   static bool classof(const DiagnosticInfo *DI) {
205     return DI->getKind() == DK_ResourceLimit ||
206            DI->getKind() == DK_StackSize;
207   }
208 };
209
210 class DiagnosticInfoStackSize : public DiagnosticInfoResourceLimit {
211 public:
212   DiagnosticInfoStackSize(const Function &Fn,
213                           uint64_t StackSize,
214                           DiagnosticSeverity Severity = DS_Warning,
215                           uint64_t StackLimit = 0)
216     : DiagnosticInfoResourceLimit(Fn, "stack size", StackSize,
217                                   Severity, DK_StackSize, StackLimit) {}
218
219   uint64_t getStackSize() const { return getResourceSize(); }
220   uint64_t getStackLimit() const { return getResourceLimit(); }
221
222   static bool classof(const DiagnosticInfo *DI) {
223     return DI->getKind() == DK_StackSize;
224   }
225 };
226
227 /// Diagnostic information for debug metadata version reporting.
228 /// This is basically a module and a version.
229 class DiagnosticInfoDebugMetadataVersion : public DiagnosticInfo {
230 private:
231   /// The module that is concerned by this debug metadata version diagnostic.
232   const Module &M;
233   /// The actual metadata version.
234   unsigned MetadataVersion;
235
236 public:
237   /// \p The module that is concerned by this debug metadata version diagnostic.
238   /// \p The actual metadata version.
239   DiagnosticInfoDebugMetadataVersion(const Module &M, unsigned MetadataVersion,
240                           DiagnosticSeverity Severity = DS_Warning)
241       : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M),
242         MetadataVersion(MetadataVersion) {}
243
244   const Module &getModule() const { return M; }
245   unsigned getMetadataVersion() const { return MetadataVersion; }
246
247   /// \see DiagnosticInfo::print.
248   void print(DiagnosticPrinter &DP) const override;
249
250   static bool classof(const DiagnosticInfo *DI) {
251     return DI->getKind() == DK_DebugMetadataVersion;
252   }
253 };
254
255 /// Diagnostic information for stripping invalid debug metadata.
256 class DiagnosticInfoIgnoringInvalidDebugMetadata : public DiagnosticInfo {
257 private:
258   /// The module that is concerned by this debug metadata version diagnostic.
259   const Module &M;
260
261 public:
262   /// \p The module that is concerned by this debug metadata version diagnostic.
263   DiagnosticInfoIgnoringInvalidDebugMetadata(
264       const Module &M, DiagnosticSeverity Severity = DS_Warning)
265       : DiagnosticInfo(DK_DebugMetadataVersion, Severity), M(M) {}
266
267   const Module &getModule() const { return M; }
268
269   /// \see DiagnosticInfo::print.
270   void print(DiagnosticPrinter &DP) const override;
271
272   static bool classof(const DiagnosticInfo *DI) {
273     return DI->getKind() == DK_DebugMetadataInvalid;
274   }
275 };
276
277
278 /// Diagnostic information for the sample profiler.
279 class DiagnosticInfoSampleProfile : public DiagnosticInfo {
280 public:
281   DiagnosticInfoSampleProfile(StringRef FileName, unsigned LineNum,
282                               const Twine &Msg,
283                               DiagnosticSeverity Severity = DS_Error)
284       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
285         LineNum(LineNum), Msg(Msg) {}
286   DiagnosticInfoSampleProfile(StringRef FileName, const Twine &Msg,
287                               DiagnosticSeverity Severity = DS_Error)
288       : DiagnosticInfo(DK_SampleProfile, Severity), FileName(FileName),
289         LineNum(0), Msg(Msg) {}
290   DiagnosticInfoSampleProfile(const Twine &Msg,
291                               DiagnosticSeverity Severity = DS_Error)
292       : DiagnosticInfo(DK_SampleProfile, Severity), LineNum(0), Msg(Msg) {}
293
294   /// \see DiagnosticInfo::print.
295   void print(DiagnosticPrinter &DP) const override;
296
297   static bool classof(const DiagnosticInfo *DI) {
298     return DI->getKind() == DK_SampleProfile;
299   }
300
301   StringRef getFileName() const { return FileName; }
302   unsigned getLineNum() const { return LineNum; }
303   const Twine &getMsg() const { return Msg; }
304
305 private:
306   /// Name of the input file associated with this diagnostic.
307   StringRef FileName;
308
309   /// Line number where the diagnostic occurred. If 0, no line number will
310   /// be emitted in the message.
311   unsigned LineNum;
312
313   /// Message to report.
314   const Twine &Msg;
315 };
316
317 /// Diagnostic information for the PGO profiler.
318 class DiagnosticInfoPGOProfile : public DiagnosticInfo {
319 public:
320   DiagnosticInfoPGOProfile(const char *FileName, const Twine &Msg,
321                            DiagnosticSeverity Severity = DS_Error)
322       : DiagnosticInfo(DK_PGOProfile, Severity), FileName(FileName), Msg(Msg) {}
323
324   /// \see DiagnosticInfo::print.
325   void print(DiagnosticPrinter &DP) const override;
326
327   static bool classof(const DiagnosticInfo *DI) {
328     return DI->getKind() == DK_PGOProfile;
329   }
330
331   const char *getFileName() const { return FileName; }
332   const Twine &getMsg() const { return Msg; }
333
334 private:
335   /// Name of the input file associated with this diagnostic.
336   const char *FileName;
337
338   /// Message to report.
339   const Twine &Msg;
340 };
341
342 /// Common features for diagnostics with an associated DebugLoc
343 class DiagnosticInfoWithDebugLocBase : public DiagnosticInfo {
344 public:
345   /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
346   /// the location information to use in the diagnostic.
347   DiagnosticInfoWithDebugLocBase(enum DiagnosticKind Kind,
348                                  enum DiagnosticSeverity Severity,
349                                  const Function &Fn,
350                                  const DebugLoc &DLoc)
351       : DiagnosticInfo(Kind, Severity), Fn(Fn), DLoc(DLoc) {}
352
353   /// Return true if location information is available for this diagnostic.
354   bool isLocationAvailable() const;
355
356   /// Return a string with the location information for this diagnostic
357   /// in the format "file:line:col". If location information is not available,
358   /// it returns "<unknown>:0:0".
359   const std::string getLocationStr() const;
360
361   /// Return location information for this diagnostic in three parts:
362   /// the source file name, line number and column.
363   void getLocation(StringRef *Filename, unsigned *Line, unsigned *Column) const;
364
365   const Function &getFunction() const { return Fn; }
366   const DebugLoc &getDebugLoc() const { return DLoc; }
367
368 private:
369   /// Function where this diagnostic is triggered.
370   const Function &Fn;
371
372   /// Debug location where this diagnostic is triggered.
373   DebugLoc DLoc;
374 };
375
376 /// Common features for diagnostics dealing with optimization remarks.
377 class DiagnosticInfoOptimizationBase : public DiagnosticInfoWithDebugLocBase {
378 public:
379   /// \brief Used to set IsVerbose via the stream interface.
380   struct setIsVerbose {};
381
382   /// \brief Used in the streaming interface as the general argument type.  It
383   /// internally converts everything into a key-value pair.
384   struct Argument {
385     StringRef Key;
386     std::string Val;
387     // If set, the debug location corresponding to the value.
388     DebugLoc DLoc;
389
390     explicit Argument(StringRef Str = "") : Key("String"), Val(Str) {}
391     Argument(StringRef Key, Value *V);
392     Argument(StringRef Key, int N);
393     Argument(StringRef Key, unsigned N);
394     Argument(StringRef Key, bool B) : Key(Key), Val(B ? "true" : "false") {}
395   };
396
397   /// \p PassName is the name of the pass emitting this diagnostic. \p
398   /// RemarkName is a textual identifier for the remark.  \p Fn is the function
399   /// where the diagnostic is being emitted. \p DLoc is the location information
400   /// to use in the diagnostic. If line table information is available, the
401   /// diagnostic will include the source code location. \p CodeRegion is IR
402   /// value (currently basic block) that the optimization operates on.  This is
403   /// currently used to provide run-time hotness information with PGO.
404   DiagnosticInfoOptimizationBase(enum DiagnosticKind Kind,
405                                  enum DiagnosticSeverity Severity,
406                                  const char *PassName, StringRef RemarkName,
407                                  const Function &Fn, const DebugLoc &DLoc,
408                                  Value *CodeRegion = nullptr)
409       : DiagnosticInfoWithDebugLocBase(Kind, Severity, Fn, DLoc),
410         PassName(PassName), RemarkName(RemarkName), CodeRegion(CodeRegion) {}
411
412   /// \brief This is ctor variant allows a pass to build an optimization remark
413   /// from an existing remark.
414   ///
415   /// This is useful when a transformation pass (e.g LV) wants to emit a remark
416   /// (\p Orig) generated by one of its analyses (e.g. LAA) as its own analysis
417   /// remark.  The string \p Prepend will be emitted before the original
418   /// message.
419   DiagnosticInfoOptimizationBase(const char *PassName, StringRef Prepend,
420                                  const DiagnosticInfoOptimizationBase &Orig)
421       : DiagnosticInfoWithDebugLocBase((DiagnosticKind)Orig.getKind(),
422                                        Orig.getSeverity(), Orig.getFunction(),
423                                        Orig.getDebugLoc()),
424         PassName(PassName), RemarkName(Orig.RemarkName),
425         CodeRegion(Orig.getCodeRegion()) {
426     *this << Prepend;
427     std::copy(Orig.Args.begin(), Orig.Args.end(), std::back_inserter(Args));
428   }
429
430   /// Legacy interface.
431   /// \p PassName is the name of the pass emitting this diagnostic.
432   /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
433   /// the location information to use in the diagnostic. If line table
434   /// information is available, the diagnostic will include the source code
435   /// location. \p Msg is the message to show. Note that this class does not
436   /// copy this message, so this reference must be valid for the whole life time
437   /// of the diagnostic.
438   DiagnosticInfoOptimizationBase(enum DiagnosticKind Kind,
439                                  enum DiagnosticSeverity Severity,
440                                  const char *PassName, const Function &Fn,
441                                  const DebugLoc &DLoc, const Twine &Msg,
442                                  Optional<uint64_t> Hotness = None)
443       : DiagnosticInfoWithDebugLocBase(Kind, Severity, Fn, DLoc),
444         PassName(PassName), Hotness(Hotness) {
445     Args.push_back(Argument(Msg.str()));
446   }
447
448   DiagnosticInfoOptimizationBase &operator<<(StringRef S);
449   DiagnosticInfoOptimizationBase &operator<<(Argument A);
450   DiagnosticInfoOptimizationBase &operator<<(setIsVerbose V);
451
452   /// \see DiagnosticInfo::print.
453   void print(DiagnosticPrinter &DP) const override;
454
455   /// Return true if this optimization remark is enabled by one of
456   /// of the LLVM command line flags (-pass-remarks, -pass-remarks-missed,
457   /// or -pass-remarks-analysis). Note that this only handles the LLVM
458   /// flags. We cannot access Clang flags from here (they are handled
459   /// in BackendConsumer::OptimizationRemarkHandler).
460   virtual bool isEnabled() const = 0;
461
462   StringRef getPassName() const { return PassName; }
463   std::string getMsg() const;
464   Optional<uint64_t> getHotness() const { return Hotness; }
465   void setHotness(Optional<uint64_t> H) { Hotness = H; }
466
467   Value *getCodeRegion() const { return CodeRegion; }
468
469   bool isVerbose() const { return IsVerbose; }
470
471   static bool classof(const DiagnosticInfo *DI) {
472     return DI->getKind() >= DK_FirstRemark &&
473            DI->getKind() <= DK_LastRemark;
474   }
475
476 private:
477   /// Name of the pass that triggers this report. If this matches the
478   /// regular expression given in -Rpass=regexp, then the remark will
479   /// be emitted.
480   const char *PassName;
481
482   /// Textual identifier for the remark.  Can be used by external tools reading
483   /// the YAML output file for optimization remarks to identify the remark.
484   StringRef RemarkName;
485
486   /// If profile information is available, this is the number of times the
487   /// corresponding code was executed in a profile instrumentation run.
488   Optional<uint64_t> Hotness;
489
490   /// The IR value (currently basic block) that the optimization operates on.
491   /// This is currently used to provide run-time hotness information with PGO.
492   Value *CodeRegion;
493
494   /// Arguments collected via the streaming interface.
495   SmallVector<Argument, 4> Args;
496
497   /// The remark is expected to be noisy.
498   bool IsVerbose = false;
499
500   friend struct yaml::MappingTraits<DiagnosticInfoOptimizationBase *>;
501 };
502
503 /// Diagnostic information for applied optimization remarks.
504 class OptimizationRemark : public DiagnosticInfoOptimizationBase {
505 public:
506   /// \p PassName is the name of the pass emitting this diagnostic. If
507   /// this name matches the regular expression given in -Rpass=, then the
508   /// diagnostic will be emitted. \p Fn is the function where the diagnostic
509   /// is being emitted. \p DLoc is the location information to use in the
510   /// diagnostic. If line table information is available, the diagnostic
511   /// will include the source code location. \p Msg is the message to show.
512   /// Note that this class does not copy this message, so this reference
513   /// must be valid for the whole life time of the diagnostic.
514   OptimizationRemark(const char *PassName, const Function &Fn,
515                      const DebugLoc &DLoc, const Twine &Msg,
516                      Optional<uint64_t> Hotness = None)
517       : DiagnosticInfoOptimizationBase(DK_OptimizationRemark, DS_Remark,
518                                        PassName, Fn, DLoc, Msg, Hotness) {}
519
520   /// \p PassName is the name of the pass emitting this diagnostic. If this name
521   /// matches the regular expression given in -Rpass=, then the diagnostic will
522   /// be emitted.  \p RemarkName is a textual identifier for the remark.  \p
523   /// DLoc is the debug location and \p CodeRegion is the region that the
524   /// optimization operates on (currently on block is supported).
525   OptimizationRemark(const char *PassName, StringRef RemarkName,
526                      const DebugLoc &DLoc, Value *CodeRegion);
527
528   /// Same as above but the debug location and code region is derived from \p
529   /// Instr.
530   OptimizationRemark(const char *PassName, StringRef RemarkName,
531                      Instruction *Inst);
532
533   static bool classof(const DiagnosticInfo *DI) {
534     return DI->getKind() == DK_OptimizationRemark;
535   }
536
537   /// \see DiagnosticInfoOptimizationBase::isEnabled.
538   bool isEnabled() const override;
539 };
540
541 /// Diagnostic information for missed-optimization remarks.
542 class OptimizationRemarkMissed : public DiagnosticInfoOptimizationBase {
543 public:
544   /// \p PassName is the name of the pass emitting this diagnostic. If
545   /// this name matches the regular expression given in -Rpass-missed=, then the
546   /// diagnostic will be emitted. \p Fn is the function where the diagnostic
547   /// is being emitted. \p DLoc is the location information to use in the
548   /// diagnostic. If line table information is available, the diagnostic
549   /// will include the source code location. \p Msg is the message to show.
550   /// Note that this class does not copy this message, so this reference
551   /// must be valid for the whole life time of the diagnostic.
552   OptimizationRemarkMissed(const char *PassName, const Function &Fn,
553                            const DebugLoc &DLoc, const Twine &Msg,
554                            Optional<uint64_t> Hotness = None)
555       : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkMissed, DS_Remark,
556                                        PassName, Fn, DLoc, Msg, Hotness) {}
557
558   /// \p PassName is the name of the pass emitting this diagnostic. If this name
559   /// matches the regular expression given in -Rpass-missed=, then the
560   /// diagnostic will be emitted.  \p RemarkName is a textual identifier for the
561   /// remark.  \p DLoc is the debug location and \p CodeRegion is the region
562   /// that the optimization operates on (currently on block is supported).
563   OptimizationRemarkMissed(const char *PassName, StringRef RemarkName,
564                            const DebugLoc &DLoc, Value *CodeRegion);
565
566   /// \brief Same as above but \p Inst is used to derive code region and debug
567   /// location.
568   OptimizationRemarkMissed(const char *PassName, StringRef RemarkName,
569                            Instruction *Inst);
570
571   static bool classof(const DiagnosticInfo *DI) {
572     return DI->getKind() == DK_OptimizationRemarkMissed;
573   }
574
575   /// \see DiagnosticInfoOptimizationBase::isEnabled.
576   bool isEnabled() const override;
577 };
578
579 /// Diagnostic information for optimization analysis remarks.
580 class OptimizationRemarkAnalysis : public DiagnosticInfoOptimizationBase {
581 public:
582   /// \p PassName is the name of the pass emitting this diagnostic. If
583   /// this name matches the regular expression given in -Rpass-analysis=, then
584   /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
585   /// is being emitted. \p DLoc is the location information to use in the
586   /// diagnostic. If line table information is available, the diagnostic will
587   /// include the source code location. \p Msg is the message to show. Note that
588   /// this class does not copy this message, so this reference must be valid for
589   /// the whole life time of the diagnostic.
590   OptimizationRemarkAnalysis(const char *PassName, const Function &Fn,
591                              const DebugLoc &DLoc, const Twine &Msg,
592                              Optional<uint64_t> Hotness = None)
593       : DiagnosticInfoOptimizationBase(DK_OptimizationRemarkAnalysis, DS_Remark,
594                                        PassName, Fn, DLoc, Msg, Hotness) {}
595
596   /// \p PassName is the name of the pass emitting this diagnostic. If this name
597   /// matches the regular expression given in -Rpass-analysis=, then the
598   /// diagnostic will be emitted.  \p RemarkName is a textual identifier for the
599   /// remark.  \p DLoc is the debug location and \p CodeRegion is the region
600   /// that the optimization operates on (currently on block is supported).
601   OptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName,
602                              const DebugLoc &DLoc, Value *CodeRegion);
603
604   /// \brief This is ctor variant allows a pass to build an optimization remark
605   /// from an existing remark.
606   ///
607   /// This is useful when a transformation pass (e.g LV) wants to emit a remark
608   /// (\p Orig) generated by one of its analyses (e.g. LAA) as its own analysis
609   /// remark.  The string \p Prepend will be emitted before the original
610   /// message.
611   OptimizationRemarkAnalysis(const char *PassName, StringRef Prepend,
612                              const OptimizationRemarkAnalysis &Orig)
613       : DiagnosticInfoOptimizationBase(PassName, Prepend, Orig) {}
614
615   /// \brief Same as above but \p Inst is used to derive code region and debug
616   /// location.
617   OptimizationRemarkAnalysis(const char *PassName, StringRef RemarkName,
618                              Instruction *Inst);
619
620   static bool classof(const DiagnosticInfo *DI) {
621     return DI->getKind() == DK_OptimizationRemarkAnalysis;
622   }
623
624   /// \see DiagnosticInfoOptimizationBase::isEnabled.
625   bool isEnabled() const override;
626
627   static const char *AlwaysPrint;
628
629   bool shouldAlwaysPrint() const { return getPassName() == AlwaysPrint; }
630
631 protected:
632   OptimizationRemarkAnalysis(enum DiagnosticKind Kind, const char *PassName,
633                              const Function &Fn, const DebugLoc &DLoc,
634                              const Twine &Msg, Optional<uint64_t> Hotness)
635       : DiagnosticInfoOptimizationBase(Kind, DS_Remark, PassName, Fn, DLoc, Msg,
636                                        Hotness) {}
637
638   OptimizationRemarkAnalysis(enum DiagnosticKind Kind, const char *PassName,
639                              StringRef RemarkName, const DebugLoc &DLoc,
640                              Value *CodeRegion);
641 };
642
643 /// Diagnostic information for optimization analysis remarks related to
644 /// floating-point non-commutativity.
645 class OptimizationRemarkAnalysisFPCommute : public OptimizationRemarkAnalysis {
646 public:
647   /// \p PassName is the name of the pass emitting this diagnostic. If
648   /// this name matches the regular expression given in -Rpass-analysis=, then
649   /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
650   /// is being emitted. \p DLoc is the location information to use in the
651   /// diagnostic. If line table information is available, the diagnostic will
652   /// include the source code location. \p Msg is the message to show. The
653   /// front-end will append its own message related to options that address
654   /// floating-point non-commutativity. Note that this class does not copy this
655   /// message, so this reference must be valid for the whole life time of the
656   /// diagnostic.
657   OptimizationRemarkAnalysisFPCommute(const char *PassName, const Function &Fn,
658                                       const DebugLoc &DLoc, const Twine &Msg,
659                                       Optional<uint64_t> Hotness = None)
660       : OptimizationRemarkAnalysis(DK_OptimizationRemarkAnalysisFPCommute,
661                                    PassName, Fn, DLoc, Msg, Hotness) {}
662
663   /// \p PassName is the name of the pass emitting this diagnostic. If this name
664   /// matches the regular expression given in -Rpass-analysis=, then the
665   /// diagnostic will be emitted.  \p RemarkName is a textual identifier for the
666   /// remark.  \p DLoc is the debug location and \p CodeRegion is the region
667   /// that the optimization operates on (currently on block is supported). The
668   /// front-end will append its own message related to options that address
669   /// floating-point non-commutativity.
670   OptimizationRemarkAnalysisFPCommute(const char *PassName,
671                                       StringRef RemarkName,
672                                       const DebugLoc &DLoc, Value *CodeRegion)
673       : OptimizationRemarkAnalysis(DK_OptimizationRemarkAnalysisFPCommute,
674                                    PassName, RemarkName, DLoc, CodeRegion) {}
675
676   static bool classof(const DiagnosticInfo *DI) {
677     return DI->getKind() == DK_OptimizationRemarkAnalysisFPCommute;
678   }
679 };
680
681 /// Diagnostic information for optimization analysis remarks related to
682 /// pointer aliasing.
683 class OptimizationRemarkAnalysisAliasing : public OptimizationRemarkAnalysis {
684 public:
685   /// \p PassName is the name of the pass emitting this diagnostic. If
686   /// this name matches the regular expression given in -Rpass-analysis=, then
687   /// the diagnostic will be emitted. \p Fn is the function where the diagnostic
688   /// is being emitted. \p DLoc is the location information to use in the
689   /// diagnostic. If line table information is available, the diagnostic will
690   /// include the source code location. \p Msg is the message to show. The
691   /// front-end will append its own message related to options that address
692   /// pointer aliasing legality. Note that this class does not copy this
693   /// message, so this reference must be valid for the whole life time of the
694   /// diagnostic.
695   OptimizationRemarkAnalysisAliasing(const char *PassName, const Function &Fn,
696                                      const DebugLoc &DLoc, const Twine &Msg,
697                                      Optional<uint64_t> Hotness = None)
698       : OptimizationRemarkAnalysis(DK_OptimizationRemarkAnalysisAliasing,
699                                    PassName, Fn, DLoc, Msg, Hotness) {}
700
701   /// \p PassName is the name of the pass emitting this diagnostic. If this name
702   /// matches the regular expression given in -Rpass-analysis=, then the
703   /// diagnostic will be emitted.  \p RemarkName is a textual identifier for the
704   /// remark.  \p DLoc is the debug location and \p CodeRegion is the region
705   /// that the optimization operates on (currently on block is supported). The
706   /// front-end will append its own message related to options that address
707   /// pointer aliasing legality.
708   OptimizationRemarkAnalysisAliasing(const char *PassName, StringRef RemarkName,
709                                      const DebugLoc &DLoc, Value *CodeRegion)
710       : OptimizationRemarkAnalysis(DK_OptimizationRemarkAnalysisAliasing,
711                                    PassName, RemarkName, DLoc, CodeRegion) {}
712
713   static bool classof(const DiagnosticInfo *DI) {
714     return DI->getKind() == DK_OptimizationRemarkAnalysisAliasing;
715   }
716 };
717
718 /// Diagnostic information for machine IR parser.
719 class DiagnosticInfoMIRParser : public DiagnosticInfo {
720   const SMDiagnostic &Diagnostic;
721
722 public:
723   DiagnosticInfoMIRParser(DiagnosticSeverity Severity,
724                           const SMDiagnostic &Diagnostic)
725       : DiagnosticInfo(DK_MIRParser, Severity), Diagnostic(Diagnostic) {}
726
727   const SMDiagnostic &getDiagnostic() const { return Diagnostic; }
728
729   void print(DiagnosticPrinter &DP) const override;
730
731   static bool classof(const DiagnosticInfo *DI) {
732     return DI->getKind() == DK_MIRParser;
733   }
734 };
735
736 /// Diagnostic information for ISel fallback path.
737 class DiagnosticInfoISelFallback : public DiagnosticInfo {
738   /// The function that is concerned by this diagnostic.
739   const Function &Fn;
740
741 public:
742   DiagnosticInfoISelFallback(const Function &Fn,
743                              DiagnosticSeverity Severity = DS_Warning)
744       : DiagnosticInfo(DK_ISelFallback, Severity), Fn(Fn) {}
745
746   const Function &getFunction() const { return Fn; }
747
748   void print(DiagnosticPrinter &DP) const override;
749
750   static bool classof(const DiagnosticInfo *DI) {
751     return DI->getKind() == DK_ISelFallback;
752   }
753 };
754
755 // Create wrappers for C Binding types (see CBindingWrapping.h).
756 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DiagnosticInfo, LLVMDiagnosticInfoRef)
757
758 /// Emit an optimization-applied message. \p PassName is the name of the pass
759 /// emitting the message. If -Rpass= is given and \p PassName matches the
760 /// regular expression in -Rpass, then the remark will be emitted. \p Fn is
761 /// the function triggering the remark, \p DLoc is the debug location where
762 /// the diagnostic is generated. \p Msg is the message string to use.
763 void emitOptimizationRemark(LLVMContext &Ctx, const char *PassName,
764                             const Function &Fn, const DebugLoc &DLoc,
765                             const Twine &Msg);
766
767 /// Emit an optimization-missed message. \p PassName is the name of the
768 /// pass emitting the message. If -Rpass-missed= is given and \p PassName
769 /// matches the regular expression in -Rpass, then the remark will be
770 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the
771 /// debug location where the diagnostic is generated. \p Msg is the
772 /// message string to use.
773 void emitOptimizationRemarkMissed(LLVMContext &Ctx, const char *PassName,
774                                   const Function &Fn, const DebugLoc &DLoc,
775                                   const Twine &Msg);
776
777 /// Emit an optimization analysis remark message. \p PassName is the name of
778 /// the pass emitting the message. If -Rpass-analysis= is given and \p
779 /// PassName matches the regular expression in -Rpass, then the remark will be
780 /// emitted. \p Fn is the function triggering the remark, \p DLoc is the debug
781 /// location where the diagnostic is generated. \p Msg is the message string
782 /// to use.
783 void emitOptimizationRemarkAnalysis(LLVMContext &Ctx, const char *PassName,
784                                     const Function &Fn, const DebugLoc &DLoc,
785                                     const Twine &Msg);
786
787 /// Emit an optimization analysis remark related to messages about
788 /// floating-point non-commutativity. \p PassName is the name of the pass
789 /// emitting the message. If -Rpass-analysis= is given and \p PassName matches
790 /// the regular expression in -Rpass, then the remark will be emitted. \p Fn is
791 /// the function triggering the remark, \p DLoc is the debug location where the
792 /// diagnostic is generated. \p Msg is the message string to use.
793 void emitOptimizationRemarkAnalysisFPCommute(LLVMContext &Ctx,
794                                              const char *PassName,
795                                              const Function &Fn,
796                                              const DebugLoc &DLoc,
797                                              const Twine &Msg);
798
799 /// Emit an optimization analysis remark related to messages about
800 /// pointer aliasing. \p PassName is the name of the pass emitting the message.
801 /// If -Rpass-analysis= is given and \p PassName matches the regular expression
802 /// in -Rpass, then the remark will be emitted. \p Fn is the function triggering
803 /// the remark, \p DLoc is the debug location where the diagnostic is generated.
804 /// \p Msg is the message string to use.
805 void emitOptimizationRemarkAnalysisAliasing(LLVMContext &Ctx,
806                                             const char *PassName,
807                                             const Function &Fn,
808                                             const DebugLoc &DLoc,
809                                             const Twine &Msg);
810
811 /// Diagnostic information for optimization failures.
812 class DiagnosticInfoOptimizationFailure
813     : public DiagnosticInfoOptimizationBase {
814 public:
815   /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
816   /// the location information to use in the diagnostic. If line table
817   /// information is available, the diagnostic will include the source code
818   /// location. \p Msg is the message to show. Note that this class does not
819   /// copy this message, so this reference must be valid for the whole life time
820   /// of the diagnostic.
821   DiagnosticInfoOptimizationFailure(const Function &Fn, const DebugLoc &DLoc,
822                                     const Twine &Msg)
823       : DiagnosticInfoOptimizationBase(DK_OptimizationFailure, DS_Warning,
824                                        nullptr, Fn, DLoc, Msg) {}
825
826   static bool classof(const DiagnosticInfo *DI) {
827     return DI->getKind() == DK_OptimizationFailure;
828   }
829
830   /// \see DiagnosticInfoOptimizationBase::isEnabled.
831   bool isEnabled() const override;
832 };
833
834 /// Diagnostic information for unsupported feature in backend.
835 class DiagnosticInfoUnsupported
836     : public DiagnosticInfoWithDebugLocBase {
837 private:
838   Twine Msg;
839
840 public:
841   /// \p Fn is the function where the diagnostic is being emitted. \p DLoc is
842   /// the location information to use in the diagnostic. If line table
843   /// information is available, the diagnostic will include the source code
844   /// location. \p Msg is the message to show. Note that this class does not
845   /// copy this message, so this reference must be valid for the whole life time
846   /// of the diagnostic.
847   DiagnosticInfoUnsupported(const Function &Fn, const Twine &Msg,
848                             DebugLoc DLoc = DebugLoc(),
849                             DiagnosticSeverity Severity = DS_Error)
850       : DiagnosticInfoWithDebugLocBase(DK_Unsupported, Severity, Fn, DLoc),
851         Msg(Msg) {}
852
853   static bool classof(const DiagnosticInfo *DI) {
854     return DI->getKind() == DK_Unsupported;
855   }
856
857   const Twine &getMessage() const { return Msg; }
858
859   void print(DiagnosticPrinter &DP) const override;
860 };
861
862 /// Emit a warning when loop vectorization is specified but fails. \p Fn is the
863 /// function triggering the warning, \p DLoc is the debug location where the
864 /// diagnostic is generated. \p Msg is the message string to use.
865 void emitLoopVectorizeWarning(LLVMContext &Ctx, const Function &Fn,
866                               const DebugLoc &DLoc, const Twine &Msg);
867
868 /// Emit a warning when loop interleaving is specified but fails. \p Fn is the
869 /// function triggering the warning, \p DLoc is the debug location where the
870 /// diagnostic is generated. \p Msg is the message string to use.
871 void emitLoopInterleaveWarning(LLVMContext &Ctx, const Function &Fn,
872                                const DebugLoc &DLoc, const Twine &Msg);
873
874 } // end namespace llvm
875
876 #endif // LLVM_IR_DIAGNOSTICINFO_H