MemoryOpRemark.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MemoryOpRemark.h - Memory operation remark analysis -*- C++ ------*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Provide more information about instructions that copy, move, or initialize
  15. // memory, including those with a "auto-init" !annotation metadata.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_UTILS_MEMORYOPREMARK_H
  19. #define LLVM_TRANSFORMS_UTILS_MEMORYOPREMARK_H
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/Analysis/TargetLibraryInfo.h"
  22. #include "llvm/IR/DiagnosticInfo.h"
  23. #include <optional>
  24. namespace llvm {
  25. class CallInst;
  26. class DataLayout;
  27. class DiagnosticInfoIROptimization;
  28. class Instruction;
  29. class IntrinsicInst;
  30. class Value;
  31. class OptimizationRemarkEmitter;
  32. class StoreInst;
  33. // FIXME: Once we get to more remarks like this one, we need to re-evaluate how
  34. // much of this logic should actually go into the remark emitter.
  35. struct MemoryOpRemark {
  36. OptimizationRemarkEmitter &ORE;
  37. StringRef RemarkPass;
  38. const DataLayout &DL;
  39. const TargetLibraryInfo &TLI;
  40. MemoryOpRemark(OptimizationRemarkEmitter &ORE, StringRef RemarkPass,
  41. const DataLayout &DL, const TargetLibraryInfo &TLI)
  42. : ORE(ORE), RemarkPass(RemarkPass), DL(DL), TLI(TLI) {}
  43. virtual ~MemoryOpRemark();
  44. /// \return true iff the instruction is understood by MemoryOpRemark.
  45. static bool canHandle(const Instruction *I, const TargetLibraryInfo &TLI);
  46. void visit(const Instruction *I);
  47. protected:
  48. virtual std::string explainSource(StringRef Type) const;
  49. enum RemarkKind { RK_Store, RK_Unknown, RK_IntrinsicCall, RK_Call };
  50. virtual StringRef remarkName(RemarkKind RK) const;
  51. virtual DiagnosticKind diagnosticKind() const { return DK_OptimizationRemarkAnalysis; }
  52. private:
  53. template<typename ...Ts>
  54. std::unique_ptr<DiagnosticInfoIROptimization> makeRemark(Ts... Args);
  55. /// Emit a remark using information from the store's destination, size, etc.
  56. void visitStore(const StoreInst &SI);
  57. /// Emit a generic auto-init remark.
  58. void visitUnknown(const Instruction &I);
  59. /// Emit a remark using information from known intrinsic calls.
  60. void visitIntrinsicCall(const IntrinsicInst &II);
  61. /// Emit a remark using information from known function calls.
  62. void visitCall(const CallInst &CI);
  63. /// Add callee information to a remark: whether it's known, the function name,
  64. /// etc.
  65. template <typename FTy>
  66. void visitCallee(FTy F, bool KnownLibCall, DiagnosticInfoIROptimization &R);
  67. /// Add operand information to a remark based on knowledge we have for known
  68. /// libcalls.
  69. void visitKnownLibCall(const CallInst &CI, LibFunc LF,
  70. DiagnosticInfoIROptimization &R);
  71. /// Add the memory operation size to a remark.
  72. void visitSizeOperand(Value *V, DiagnosticInfoIROptimization &R);
  73. struct VariableInfo {
  74. std::optional<StringRef> Name;
  75. std::optional<uint64_t> Size;
  76. bool isEmpty() const { return !Name && !Size; }
  77. };
  78. /// Gather more information about \p V as a variable. This can be debug info,
  79. /// information from the alloca, etc. Since \p V can represent more than a
  80. /// single variable, they will all be added to the remark.
  81. void visitPtr(Value *V, bool IsSrc, DiagnosticInfoIROptimization &R);
  82. void visitVariable(const Value *V, SmallVectorImpl<VariableInfo> &Result);
  83. };
  84. /// Special case for -ftrivial-auto-var-init remarks.
  85. struct AutoInitRemark : public MemoryOpRemark {
  86. AutoInitRemark(OptimizationRemarkEmitter &ORE, StringRef RemarkPass,
  87. const DataLayout &DL, const TargetLibraryInfo &TLI)
  88. : MemoryOpRemark(ORE, RemarkPass, DL, TLI) {}
  89. /// \return true iff the instruction is understood by AutoInitRemark.
  90. static bool canHandle(const Instruction *I);
  91. protected:
  92. std::string explainSource(StringRef Type) const override;
  93. StringRef remarkName(RemarkKind RK) const override;
  94. DiagnosticKind diagnosticKind() const override {
  95. return DK_OptimizationRemarkMissed;
  96. }
  97. };
  98. } // namespace llvm
  99. #endif
  100. #ifdef __GNUC__
  101. #pragma GCC diagnostic pop
  102. #endif