//===- llvm/Analysis/MemoryProfileInfo.h - memory profile info ---*- C++ -*-==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file contains utilities to analyze memory profile information. // //===----------------------------------------------------------------------===// #ifndef LLVM_ANALYSIS_MEMORYPROFILEINFO_H #define LLVM_ANALYSIS_MEMORYPROFILEINFO_H #include "llvm/IR/Metadata.h" #include "llvm/IR/ModuleSummaryIndex.h" #include <map> namespace llvm { namespace memprof { /// Return the allocation type for a given set of memory profile values. AllocationType getAllocType(uint64_t TotalLifetimeAccessDensity, uint64_t AllocCount, uint64_t TotalLifetime); /// Build callstack metadata from the provided list of call stack ids. Returns /// the resulting metadata node. MDNode *buildCallstackMetadata(ArrayRef<uint64_t> CallStack, LLVMContext &Ctx); /// Returns the stack node from an MIB metadata node. MDNode *getMIBStackNode(const MDNode *MIB); /// Returns the allocation type from an MIB metadata node. AllocationType getMIBAllocType(const MDNode *MIB); /// Returns the total size from an MIB metadata node, or 0 if it was not /// recorded. uint64_t getMIBTotalSize(const MDNode *MIB); /// Returns the string to use in attributes with the given type. std::string getAllocTypeAttributeString(AllocationType Type); /// True if the AllocTypes bitmask contains just a single type. bool hasSingleAllocType(uint8_t AllocTypes); /// Class to build a trie of call stack contexts for a particular profiled /// allocation call, along with their associated allocation types. /// The allocation will be at the root of the trie, which is then used to /// compute the minimum lists of context ids needed to associate a call context /// with a single allocation type. class CallStackTrie { … }; /// Helper class to iterate through stack ids in both metadata (memprof MIB and /// callsite) and the corresponding ThinLTO summary data structures /// (CallsiteInfo and MIBInfo). This simplifies implementation of client code /// which doesn't need to worry about whether we are operating with IR (Regular /// LTO), or summary (ThinLTO). template <class NodeT, class IteratorT> class CallStack { … }; template <class NodeT, class IteratorT> CallStack<NodeT, IteratorT>::CallStackIterator::CallStackIterator( const NodeT *N, bool End) : … { … } template <class NodeT, class IteratorT> uint64_t CallStack<NodeT, IteratorT>::CallStackIterator::operator*() { … } template <class NodeT, class IteratorT> uint64_t CallStack<NodeT, IteratorT>::back() const { … } template <class NodeT, class IteratorT> typename CallStack<NodeT, IteratorT>::CallStackIterator CallStack<NodeT, IteratorT>::begin() const { … } template <class NodeT, class IteratorT> typename CallStack<NodeT, IteratorT>::CallStackIterator CallStack<NodeT, IteratorT>::beginAfterSharedPrefix(CallStack &Other) { … } /// Specializations for iterating through IR metadata stack contexts. template <> CallStack<MDNode, MDNode::op_iterator>::CallStackIterator::CallStackIterator( const MDNode *N, bool End); template <> uint64_t CallStack<MDNode, MDNode::op_iterator>::CallStackIterator::operator*(); template <> uint64_t CallStack<MDNode, MDNode::op_iterator>::back() const; } // end namespace memprof } // end namespace llvm #endif