Jlm
Loading...
Searching...
No Matches
ModRefSummary.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2026 Nico Reißmann <nico.reissmann@gmail.com>
3 * See COPYING for terms of redistribution.
4 */
5
14#include <jlm/rvsdg/Phi.hpp>
15
16namespace jlm::llvm::aa
17{
18
19std::vector<MemoryStateSummary>
20collectMemoryStateDistribution(const rvsdg::Graph & rvsdg, const ModRefSummary & modRefSummary)
21{
22 std::function<
23 void(const rvsdg::Region &, const rvsdg::LambdaNode *, std::vector<MemoryStateSummary> &)>
24 collect = [&](const rvsdg::Region & region,
25 const rvsdg::LambdaNode * lambdaNode,
26 std::vector<MemoryStateSummary> & summaries)
27 {
28 for (auto & node : region.Nodes())
29 {
31 node,
32 [&](const rvsdg::PhiNode & phiNode)
33 {
34 JLM_ASSERT(lambdaNode == nullptr);
35 collect(*phiNode.subregion(), lambdaNode, summaries);
36 },
37 [&](const rvsdg::DeltaNode &)
38 {
39 JLM_ASSERT(lambdaNode == nullptr);
40 // Nothing needs to be done
41 },
42 [&](const rvsdg::LambdaNode & n)
43 {
44 JLM_ASSERT(lambdaNode == nullptr);
45 auto entrySize = modRefSummary.GetLambdaEntryModRef(n).getModRefNodes().size();
46 auto exitSize = modRefSummary.GetLambdaExitModRef(n).getModRefNodes().size();
47 summaries.push_back({ &n, &n, entrySize, exitSize });
48
49 collect(*n.subregion(), &n, summaries);
50 },
51 [&](const rvsdg::ThetaNode & thetaNode)
52 {
53 JLM_ASSERT(lambdaNode != nullptr);
54 auto size = modRefSummary.GetThetaModRef(thetaNode).getModRefNodes().size();
55 summaries.push_back({ lambdaNode, &thetaNode, size, size });
56
57 collect(*thetaNode.subregion(), lambdaNode, summaries);
58 },
59 [&](const rvsdg::GammaNode & gammaNode)
60 {
61 JLM_ASSERT(lambdaNode != nullptr);
62 auto entrySize = modRefSummary.GetGammaEntryModRef(gammaNode).getModRefNodes().size();
63 auto exitSize = modRefSummary.GetGammaExitModRef(gammaNode).getModRefNodes().size();
64 summaries.push_back({ lambdaNode, &gammaNode, entrySize, exitSize });
65
66 for (auto & subregion : gammaNode.Subregions())
67 collect(subregion, lambdaNode, summaries);
68 },
69 [&](const rvsdg::SimpleNode & simpleNode)
70 {
71 MatchTypeWithDefault(
72 simpleNode.GetOperation(),
73 [&](const StoreOperation &)
74 {
75 JLM_ASSERT(lambdaNode != nullptr);
76 auto size = modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size();
77 summaries.push_back({ lambdaNode, &simpleNode, size, size });
78 },
79 [&](const LoadOperation &)
80 {
81 JLM_ASSERT(lambdaNode != nullptr);
82 auto size = modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size();
83 summaries.push_back({ lambdaNode, &simpleNode, size, size });
84 },
85 [&](const MemCpyOperation &)
86 {
87 JLM_ASSERT(lambdaNode != nullptr);
88 auto size = modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size();
89 summaries.push_back({ lambdaNode, &simpleNode, size, size });
90 },
91 [&](const MemMoveOperation &)
92 {
93 JLM_ASSERT(lambdaNode != nullptr);
94 auto size = modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size();
95 summaries.push_back({ lambdaNode, &simpleNode, size, size });
96 },
97 [&](const MemSetOperation &)
98 {
99 JLM_ASSERT(lambdaNode != nullptr);
100 auto size = modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size();
101 summaries.push_back({ lambdaNode, &simpleNode, size, size });
102 },
103 [&](const FreeOperation &)
104 {
105 JLM_ASSERT(lambdaNode != nullptr);
106 auto size = modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size();
107 summaries.push_back({ lambdaNode, &simpleNode, size, size });
108 },
109 [&](const AllocaOperation &)
110 {
111 JLM_ASSERT(lambdaNode != nullptr);
112 auto size = modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size();
113 summaries.push_back({ lambdaNode, &simpleNode, size, size });
114 },
115 [&](const MallocOperation &)
116 {
117 JLM_ASSERT(lambdaNode != nullptr);
118 auto size = modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size();
119 summaries.push_back({ lambdaNode, &simpleNode, size, size });
120 },
121 [&](const CallOperation &)
122 {
123 JLM_ASSERT(lambdaNode != nullptr);
124 auto size = modRefSummary.GetSimpleNodeModRef(simpleNode).getModRefNodes().size();
125 summaries.push_back({ lambdaNode, &simpleNode, size, size });
126 },
127 [&](const MemoryStateOperation &)
128 {
129 JLM_ASSERT(lambdaNode != nullptr);
130 // Nothing needs to be done
131 },
132 [&]()
133 {
134 // Any remaining type of node should not involve any memory states
136 });
137 });
138 }
139 };
140
141 std::vector<MemoryStateSummary> summaries;
142 collect(rvsdg.GetRootRegion(), nullptr, summaries);
143 return summaries;
144}
145
146std::string
147toString(const std::vector<MemoryStateSummary> & memoryStateDistribution)
148{
149 auto toString = [](const MemoryStateSummary & memoryStateSummary)
150 {
151 constexpr char separator = '-';
152 return util::strfmt(
153 memoryStateSummary.lambdaNode->DebugString(),
154 separator,
155 memoryStateSummary.node->DebugString(),
156 separator,
157 "(",
158 memoryStateSummary.node->region()->getRegionId(),
159 ":",
160 memoryStateSummary.node->GetNodeId(),
161 ")",
162 separator,
163 memoryStateSummary.numMemoryInputStates,
164 separator,
165 memoryStateSummary.numMemoryOutputStates);
166 };
167
168 size_t n = 0;
169 std::string summaryStr;
170 for (auto & summary : memoryStateDistribution)
171 {
172 summaryStr += toString(summary);
173 if (n != memoryStateDistribution.size() - 1)
174 summaryStr += ",";
175 n++;
176 }
177
178 return summaryStr;
179}
180
181}
Call operation class.
Definition call.hpp:251
const std::unordered_map< PointsToGraph::NodeIndex, ModRefEffect > & getModRefNodes() const
virtual const ModRefSet & GetGammaEntryModRef(const rvsdg::GammaNode &gamma) const =0
virtual const ModRefSet & GetThetaModRef(const rvsdg::ThetaNode &theta) const =0
virtual const ModRefSet & GetLambdaExitModRef(const rvsdg::LambdaNode &lambda) const =0
virtual const ModRefSet & GetLambdaEntryModRef(const rvsdg::LambdaNode &lambda) const =0
virtual const ModRefSet & GetGammaExitModRef(const rvsdg::GammaNode &gamma) const =0
virtual const ModRefSet & GetSimpleNodeModRef(const rvsdg::SimpleNode &node) const =0
Conditional operator / pattern matching.
Definition gamma.hpp:99
A phi node represents the fixpoint of mutually recursive definitions.
Definition Phi.hpp:46
rvsdg::Region * subregion() const noexcept
Definition Phi.hpp:320
Represent acyclic RVSDG subgraphs.
Definition region.hpp:213
NodeRange Nodes() noexcept
Definition region.hpp:375
#define JLM_ASSERT(x)
Definition common.hpp:16
std::string toString(const std::vector< MemoryStateSummary > &memoryStateDistribution)
std::vector< MemoryStateSummary > collectMemoryStateDistribution(const rvsdg::Graph &rvsdg, const ModRefSummary &modRefSummary)
bool hasMemoryState(const rvsdg::Node &node)
void MatchTypeOrFail(T &obj, const Fns &... fns)
Pattern match over subclass type of given object.