|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (c) 2026 Fabian Schiebel. |
| 3 | + * All rights reserved. This program and the accompanying materials are made |
| 4 | + * available under the terms of LICENSE.txt. |
| 5 | + * |
| 6 | + * Contributors: |
| 7 | + * Fabian Schiebel and others |
| 8 | + *****************************************************************************/ |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include "phasar/Utils/TypeTraits.h" |
| 12 | + |
| 13 | +#include "llvm/Support/raw_ostream.h" |
| 14 | + |
| 15 | +#include <concepts> |
| 16 | +#include <utility> |
| 17 | + |
| 18 | +namespace psr { |
| 19 | + |
| 20 | +template <typename T> |
| 21 | +concept InstructionClassifier = |
| 22 | + requires(const T &IC, typename T::n_t Inst, typename T::n_t Succ) { |
| 23 | + { IC.isCallSite(Inst) } -> std::convertible_to<bool>; |
| 24 | + { IC.isFieldLoad(Inst) } -> std::convertible_to<bool>; |
| 25 | + { IC.isFieldStore(Inst) } -> std::convertible_to<bool>; |
| 26 | + { IC.isFallThroughSuccessor(Inst, Succ) } -> std::convertible_to<bool>; |
| 27 | + { IC.isBranchTarget(Inst, Succ) } -> std::convertible_to<bool>; |
| 28 | + }; |
| 29 | + |
| 30 | +template <typename T> |
| 31 | +concept CFG = requires(const T &CF, typename T::n_t Inst, typename T::f_t Fun) { |
| 32 | + typename T::n_t; |
| 33 | + typename T::f_t; |
| 34 | + |
| 35 | + /// Returns the function that contains the given instruction Inst. |
| 36 | + // TODO: Actually belongs into ProjectIRDB! |
| 37 | + { CF.getFunctionOf(Inst) } -> std::convertible_to<typename T::f_t>; |
| 38 | + /// Returns an iterable range of all instructions of the given function that |
| 39 | + /// are part of the control-flow graph. |
| 40 | + // TODO: We should have sth like this in the ProjectIRDB as well! |
| 41 | + { CF.getAllInstructionsOf(Fun) } -> psr::is_iterable_over_v<typename T::n_t>; |
| 42 | + |
| 43 | + /// Returns an iterable range of all successor instructions of Inst in the |
| 44 | + /// CFG. |
| 45 | + /// NOTE: This function is typically being called in a hot part of the |
| 46 | + /// analysis and should therefore be highly optimized for performance. |
| 47 | + { CF.getSuccsOf(Inst) } -> psr::is_iterable_over_v<typename T::n_t>; |
| 48 | + |
| 49 | + /// Returns an iterable range of all starting instructions of the given |
| 50 | + /// function. For a forward-CFG, this is typically a singleton range. |
| 51 | + { CF.getStartPointsOf(Fun) } -> psr::is_iterable_over_v<typename T::n_t>; |
| 52 | + |
| 53 | + /// Returns whether the given Inst is a root node of the CFG |
| 54 | + { CF.isStartPoint(Inst) } -> std::convertible_to<bool>; |
| 55 | + |
| 56 | + /// Returns whether the given Inst is a leaf node of the CFG |
| 57 | + { CF.isExitInst(Inst) } -> std::convertible_to<bool>; |
| 58 | + |
| 59 | + requires InstructionClassifier<T>; |
| 60 | +}; |
| 61 | + |
| 62 | +template <typename T> |
| 63 | +concept BidiCFG = |
| 64 | + CFG<T> && requires(const T &CF, typename T::n_t Inst, typename T::f_t Fun) { |
| 65 | + /// Returns an iterable range of all predecessor instructions of Inst in |
| 66 | + /// the CFG |
| 67 | + { CF.getPredsOf(Inst) } -> psr::is_iterable_over_v<typename T::n_t>; |
| 68 | + |
| 69 | + /// Returns an iterable range of all exit instructions (often return |
| 70 | + /// instructions) of the given function. For a backward-CFG, this is |
| 71 | + /// typically a singleton range |
| 72 | + { CF.getExitPointsOf(Fun) } -> psr::is_iterable_over_v<typename T::n_t>; |
| 73 | + }; |
| 74 | + |
| 75 | +template <typename T> |
| 76 | +concept CFGDump = requires(const T &CF, typename T::n_t Inst, |
| 77 | + typename T::f_t Fun, llvm::raw_ostream &OS) { |
| 78 | + { CF.getStatementId(Inst) } -> psr::is_string_like_v; |
| 79 | + { CF.getFunctionName(Fun) } -> psr::is_string_like_v; |
| 80 | + { CF.getDemangledFunctionName(Fun) } -> psr::is_string_like_v; |
| 81 | + CF.print(Fun, OS); |
| 82 | +}; |
| 83 | + |
| 84 | +template <typename T> |
| 85 | +concept CFGEdgesProvider = requires(const T &CF, typename T::f_t Fun) { |
| 86 | + { |
| 87 | + CF.getAllControlFlowEdges(Fun) |
| 88 | + } -> psr::is_iterable_over_v<std::pair<typename T::n_t, typename T::n_t>>; |
| 89 | +}; |
| 90 | +} // namespace psr |
0 commit comments