Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:43

0001 //===- MemoryOpRemark.h - Memory operation remark analysis -*- C++ ------*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Provide more information about instructions that copy, move, or initialize
0010 // memory, including those with a "auto-init" !annotation metadata.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_TRANSFORMS_UTILS_MEMORYOPREMARK_H
0015 #define LLVM_TRANSFORMS_UTILS_MEMORYOPREMARK_H
0016 
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/Analysis/TargetLibraryInfo.h"
0019 #include "llvm/IR/DiagnosticInfo.h"
0020 #include <optional>
0021 
0022 namespace llvm {
0023 
0024 class CallInst;
0025 class DataLayout;
0026 class DiagnosticInfoIROptimization;
0027 class Instruction;
0028 class IntrinsicInst;
0029 class Value;
0030 class OptimizationRemarkEmitter;
0031 class StoreInst;
0032 
0033 // FIXME: Once we get to more remarks like this one, we need to re-evaluate how
0034 // much of this logic should actually go into the remark emitter.
0035 struct MemoryOpRemark {
0036   OptimizationRemarkEmitter &ORE;
0037   StringRef RemarkPass;
0038   const DataLayout &DL;
0039   const TargetLibraryInfo &TLI;
0040 
0041   MemoryOpRemark(OptimizationRemarkEmitter &ORE, StringRef RemarkPass,
0042                  const DataLayout &DL, const TargetLibraryInfo &TLI)
0043       : ORE(ORE), RemarkPass(RemarkPass), DL(DL), TLI(TLI) {}
0044 
0045   virtual ~MemoryOpRemark();
0046 
0047   /// \return true iff the instruction is understood by MemoryOpRemark.
0048   static bool canHandle(const Instruction *I, const TargetLibraryInfo &TLI);
0049 
0050   void visit(const Instruction *I);
0051 
0052 protected:
0053   virtual std::string explainSource(StringRef Type) const;
0054 
0055   enum RemarkKind { RK_Store, RK_Unknown, RK_IntrinsicCall, RK_Call };
0056   virtual StringRef remarkName(RemarkKind RK) const;
0057 
0058   virtual DiagnosticKind diagnosticKind() const { return DK_OptimizationRemarkAnalysis; }
0059 
0060 private:
0061   template<typename ...Ts>
0062   std::unique_ptr<DiagnosticInfoIROptimization> makeRemark(Ts... Args);
0063 
0064   /// Emit a remark using information from the store's destination, size, etc.
0065   void visitStore(const StoreInst &SI);
0066   /// Emit a generic auto-init remark.
0067   void visitUnknown(const Instruction &I);
0068   /// Emit a remark using information from known intrinsic calls.
0069   void visitIntrinsicCall(const IntrinsicInst &II);
0070   /// Emit a remark using information from known function calls.
0071   void visitCall(const CallInst &CI);
0072 
0073   /// Add callee information to a remark: whether it's known, the function name,
0074   /// etc.
0075   template <typename FTy>
0076   void visitCallee(FTy F, bool KnownLibCall, DiagnosticInfoIROptimization &R);
0077   /// Add operand information to a remark based on knowledge we have for known
0078   /// libcalls.
0079   void visitKnownLibCall(const CallInst &CI, LibFunc LF,
0080                          DiagnosticInfoIROptimization &R);
0081   /// Add the memory operation size to a remark.
0082   void visitSizeOperand(Value *V, DiagnosticInfoIROptimization &R);
0083 
0084   struct VariableInfo {
0085     std::optional<StringRef> Name;
0086     std::optional<uint64_t> Size;
0087     bool isEmpty() const { return !Name && !Size; }
0088   };
0089   /// Gather more information about \p V as a variable. This can be debug info,
0090   /// information from the alloca, etc. Since \p V can represent more than a
0091   /// single variable, they will all be added to the remark.
0092   void visitPtr(Value *V, bool IsSrc, DiagnosticInfoIROptimization &R);
0093   void visitVariable(const Value *V, SmallVectorImpl<VariableInfo> &Result);
0094 };
0095 
0096 /// Special case for -ftrivial-auto-var-init remarks.
0097 struct AutoInitRemark : public MemoryOpRemark {
0098   AutoInitRemark(OptimizationRemarkEmitter &ORE, StringRef RemarkPass,
0099                  const DataLayout &DL, const TargetLibraryInfo &TLI)
0100       : MemoryOpRemark(ORE, RemarkPass, DL, TLI) {}
0101 
0102   /// \return true iff the instruction is understood by AutoInitRemark.
0103   static bool canHandle(const Instruction *I);
0104 
0105 protected:
0106   std::string explainSource(StringRef Type) const override;
0107   StringRef remarkName(RemarkKind RK) const override;
0108   DiagnosticKind diagnosticKind() const override {
0109     return DK_OptimizationRemarkMissed;
0110   }
0111 };
0112 
0113 } // namespace llvm
0114 
0115 #endif