MemoryOpRemark.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. namespace llvm {
  24. class CallInst;
  25. class DataLayout;
  26. class DiagnosticInfoIROptimization;
  27. class Instruction;
  28. class IntrinsicInst;
  29. class Value;
  30. class OptimizationRemarkEmitter;
  31. class StoreInst;
  32. // FIXME: Once we get to more remarks like this one, we need to re-evaluate how
  33. // much of this logic should actually go into the remark emitter.
  34. struct MemoryOpRemark {
  35. OptimizationRemarkEmitter &ORE;
  36. StringRef RemarkPass;
  37. const DataLayout &DL;
  38. const TargetLibraryInfo &TLI;
  39. MemoryOpRemark(OptimizationRemarkEmitter &ORE, StringRef RemarkPass,
  40. const DataLayout &DL, const TargetLibraryInfo &TLI)
  41. : ORE(ORE), RemarkPass(RemarkPass), DL(DL), TLI(TLI) {}
  42. virtual ~MemoryOpRemark();
  43. /// \return true iff the instruction is understood by MemoryOpRemark.
  44. static bool canHandle(const Instruction *I, const TargetLibraryInfo &TLI);
  45. void visit(const Instruction *I);
  46. protected:
  47. virtual std::string explainSource(StringRef Type) const;
  48. enum RemarkKind { RK_Store, RK_Unknown, RK_IntrinsicCall, RK_Call };
  49. virtual StringRef remarkName(RemarkKind RK) const;
  50. virtual DiagnosticKind diagnosticKind() const { return DK_OptimizationRemarkAnalysis; }
  51. private:
  52. template<typename ...Ts>
  53. std::unique_ptr<DiagnosticInfoIROptimization> makeRemark(Ts... Args);
  54. /// Emit a remark using information from the store's destination, size, etc.
  55. void visitStore(const StoreInst &SI);
  56. /// Emit a generic auto-init remark.
  57. void visitUnknown(const Instruction &I);
  58. /// Emit a remark using information from known intrinsic calls.
  59. void visitIntrinsicCall(const IntrinsicInst &II);
  60. /// Emit a remark using information from known function calls.
  61. void visitCall(const CallInst &CI);
  62. /// Add callee information to a remark: whether it's known, the function name,
  63. /// etc.
  64. template <typename FTy>
  65. void visitCallee(FTy F, bool KnownLibCall, DiagnosticInfoIROptimization &R);
  66. /// Add operand information to a remark based on knowledge we have for known
  67. /// libcalls.
  68. void visitKnownLibCall(const CallInst &CI, LibFunc LF,
  69. DiagnosticInfoIROptimization &R);
  70. /// Add the memory operation size to a remark.
  71. void visitSizeOperand(Value *V, DiagnosticInfoIROptimization &R);
  72. struct VariableInfo {
  73. Optional<StringRef> Name;
  74. Optional<uint64_t> Size;
  75. bool isEmpty() const { return !Name && !Size; }
  76. };
  77. /// Gather more information about \p V as a variable. This can be debug info,
  78. /// information from the alloca, etc. Since \p V can represent more than a
  79. /// single variable, they will all be added to the remark.
  80. void visitPtr(Value *V, bool IsSrc, DiagnosticInfoIROptimization &R);
  81. void visitVariable(const Value *V, SmallVectorImpl<VariableInfo> &Result);
  82. };
  83. /// Special case for -ftrivial-auto-var-init remarks.
  84. struct AutoInitRemark : public MemoryOpRemark {
  85. AutoInitRemark(OptimizationRemarkEmitter &ORE, StringRef RemarkPass,
  86. const DataLayout &DL, const TargetLibraryInfo &TLI)
  87. : MemoryOpRemark(ORE, RemarkPass, DL, TLI) {}
  88. /// \return true iff the instruction is understood by AutoInitRemark.
  89. static bool canHandle(const Instruction *I);
  90. protected:
  91. virtual std::string explainSource(StringRef Type) const override;
  92. virtual StringRef remarkName(RemarkKind RK) const override;
  93. virtual DiagnosticKind diagnosticKind() const override {
  94. return DK_OptimizationRemarkMissed;
  95. }
  96. };
  97. } // namespace llvm
  98. #endif
  99. #ifdef __GNUC__
  100. #pragma GCC diagnostic pop
  101. #endif