1 //===--- MemoryTree.h - A special tree for components and sizes -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "support/MemoryTree.h"
11 #include "llvm/ADT/StringRef.h"
19 size_t traverseTree(const MemoryTree
&MT
, std::string
&ComponentName
,
20 const trace::Metric
&Out
) {
21 size_t OriginalLen
= ComponentName
.size();
22 if (!ComponentName
.empty())
24 size_t Total
= MT
.self();
25 for (const auto &Entry
: MT
.children()) {
26 ComponentName
+= Entry
.first
;
27 Total
+= traverseTree(Entry
.getSecond(), ComponentName
, Out
);
28 ComponentName
.resize(OriginalLen
+ 1);
30 ComponentName
.resize(OriginalLen
);
31 Out
.record(Total
, ComponentName
);
36 MemoryTree
&MemoryTree::createChild(llvm::StringRef Name
) {
37 auto &Child
= Children
.try_emplace(Name
, DetailAlloc
).first
->getSecond();
41 const llvm::DenseMap
<llvm::StringRef
, MemoryTree
> &
42 MemoryTree::children() const {
46 size_t MemoryTree::total() const {
48 for (const auto &Entry
: Children
)
49 Total
+= Entry
.getSecond().total();
53 void record(const MemoryTree
&MT
, std::string RootName
,
54 const trace::Metric
&Out
) {
55 traverseTree(MT
, RootName
, Out
);