|
12 | 12 |
|
13 | 13 | #include "phasar/Utils/ByRef.h" |
14 | 14 | #include "phasar/Utils/CRTPUtils.h" |
| 15 | +#include "phasar/Utils/Compressor.h" |
| 16 | +#include "phasar/Utils/GraphTraits.h" |
| 17 | +#include "phasar/Utils/IotaIterator.h" |
| 18 | +#include "phasar/Utils/NonNullPtr.h" |
15 | 19 | #include "phasar/Utils/TypeTraits.h" |
16 | 20 |
|
| 21 | +#include "llvm/ADT/STLExtras.h" |
| 22 | + |
17 | 23 | #include <concepts> |
18 | 24 | #include <type_traits> |
19 | 25 |
|
@@ -127,6 +133,92 @@ template <typename Derived> class CallGraphBase : public CRTPBase<Derived> { |
127 | 133 |
|
128 | 134 | [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; } |
129 | 135 | }; |
| 136 | + |
| 137 | +/// A view over a call-graph that implements psr::GraphTraits on the |
| 138 | +/// getCallersOf() relation. |
| 139 | +/// Useful to compute CG-SCCs. |
| 140 | +template <typename CallGraphTy, typename DB> class ReverseCGGraph { |
| 141 | + static constexpr bool NeedsMapping = !IdType<typename CallGraphTy::f_t>; |
| 142 | + |
| 143 | + enum class FunctionIdImpl : uint32_t {}; |
| 144 | + |
| 145 | +public: |
| 146 | + using FunctionId = std::conditional_t<NeedsMapping, FunctionIdImpl, |
| 147 | + typename CallGraphTy::f_t>; |
| 148 | + |
| 149 | + constexpr ReverseCGGraph( |
| 150 | + NonNullPtr<const CallGraphTy> CGView, NonNullPtr<const DB> IRDB, |
| 151 | + Compressor<typename CallGraphTy::f_t, FunctionId> FC) noexcept |
| 152 | + requires(NeedsMapping) |
| 153 | + : CGView(CGView), IRDB(IRDB), FC(std::move(FC)) {} |
| 154 | + |
| 155 | + constexpr ReverseCGGraph(NonNullPtr<const CallGraphTy> CGView, |
| 156 | + NonNullPtr<const DB> IRDB) noexcept |
| 157 | + : CGView(CGView), IRDB(IRDB) { |
| 158 | + FC.reserve(CGView->getNumVertexFunctions()); |
| 159 | + for (const auto &Fun : CGView->getAllVertexFunctions()) { |
| 160 | + FC.insert(Fun); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + NonNullPtr<const CallGraphTy> CGView; |
| 165 | + NonNullPtr<const DB> IRDB; |
| 166 | + [[no_unique_address]] std::conditional_t< |
| 167 | + NeedsMapping, Compressor<typename CallGraphTy::f_t, FunctionId>, |
| 168 | + NoneCompressor> FC{}; |
| 169 | +}; |
| 170 | + |
| 171 | +template <typename CallGraphTy, typename DB> |
| 172 | +ReverseCGGraph(NonNullPtr<const CallGraphTy>, NonNullPtr<const DB>) |
| 173 | + -> ReverseCGGraph<CallGraphTy, DB>; |
| 174 | +template <typename CallGraphTy, typename DB> |
| 175 | +ReverseCGGraph(const CallGraphTy *, const DB *) |
| 176 | + -> ReverseCGGraph<CallGraphTy, DB>; |
| 177 | + |
| 178 | +template <typename CallGraphTy, typename DB> |
| 179 | +struct GraphTraits<ReverseCGGraph<CallGraphTy, DB>> { |
| 180 | + using graph_type = ReverseCGGraph<CallGraphTy, DB>; |
| 181 | + using value_type = typename CallGraphTy::f_t; |
| 182 | + using vertex_t = typename graph_type::FunctionId; |
| 183 | + using edge_t = vertex_t; |
| 184 | + |
| 185 | + static constexpr vertex_t Invalid = vertex_t(UINT32_MAX); |
| 186 | + |
| 187 | + static constexpr auto mapToFunction(const graph_type &G) { |
| 188 | + return [&G](ByConstRef<typename CallGraphTy::n_t> Inst) { |
| 189 | + const auto &Fun = G.IRDB->getFunctionOf(Inst); |
| 190 | + return G.FC.getOrNull(Fun).value(); |
| 191 | + }; |
| 192 | + } |
| 193 | + |
| 194 | + static constexpr auto outEdges(const graph_type &G, vertex_t Vtx) { |
| 195 | + return llvm::map_range(G.CGView->getCallersOf(G.FC[Vtx]), mapToFunction(G)); |
| 196 | + } |
| 197 | + |
| 198 | + static constexpr decltype(auto) nodes(const graph_type &G) { |
| 199 | + return G.CGView->getAllVertexFunctions(); |
| 200 | + } |
| 201 | + |
| 202 | + // TODO: Roots |
| 203 | + |
| 204 | + static constexpr auto vertices(const graph_type &G) { |
| 205 | + return iota<vertex_t>(G.CGView->getNumVertexFunctions()); |
| 206 | + } |
| 207 | + |
| 208 | + static constexpr decltype(auto) node(const graph_type &G, vertex_t Vtx) { |
| 209 | + return G.FC[Vtx]; |
| 210 | + } |
| 211 | + |
| 212 | + static constexpr size_t size(const graph_type &G) { |
| 213 | + return G.CGView->getNumVertexFunctions(); |
| 214 | + } |
| 215 | + |
| 216 | + static constexpr vertex_t target(edge_t Edge) { return Edge; } |
| 217 | + static constexpr vertex_t withEdgeTarget(edge_t /*Edge*/, vertex_t Vtx) { |
| 218 | + return Vtx; |
| 219 | + } |
| 220 | +}; |
| 221 | + |
130 | 222 | } // namespace psr |
131 | 223 |
|
132 | 224 | #endif // PHASAR_CONTROLFLOW_CALLGRAPHBASE_H |
0 commit comments