MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
scope.h
Go to the documentation of this file.
1#pragma once
2
3#include "mim/def.h"
4#include "mim/util/print.h"
5
6namespace mim {
7
8class CFA;
9template<bool> class CFG;
10
11/// @name Control Flow
12///@{
15///@}
16
17/// A @p Scope represents a region of @p Def%s that are live from the view of an @p entry's @p Var.
18/// Transitively, all user's of the @p entry's @p Var are pooled into this @p Scope (see @p defs()).
19/// Both @p entry() and @p exit() are @em NOT part of the @p Scope itself.
20/// The @p exit() is just a virtual dummy to have a unique exit dual to @p entry().
21class Scope {
22public:
23 Scope(const Scope&) = delete;
24 Scope& operator=(Scope) = delete;
25
26 explicit Scope(Def* entry);
27 ~Scope();
28
29 /// @name getters
30 ///@{
31 World& world() const { return world_; }
32 Def* entry() const { return entry_; }
33 Def* exit() const { return exit_; }
34 Sym sym() const { return entry_->sym(); }
35 ///@}
36
37 /// @name Def%s bound/free in this Scope
38 ///@{
39 bool bound(const Def* def) const { return bound().contains(def); }
40 // clang-format off
41 const DefSet& bound() const { calc_bound(); return bound_; } ///< All @p Def%s within this @p Scope.
42 const DefSet& free_defs() const { calc_bound(); return free_defs_; } ///< All @em non-const @p Def%s @em directly referenced but @em not @p bound within this @p Scope. May also include @p Var%s or @em muts.
43 const VarSet& free_vars() const { calc_free (); return free_vars_; } ///< All @p Var%s that occurr free in this @p Scope. Does @em not transitively contain any free @p Var%s from @p muts.
44 const MutSet& free_muts() const { calc_free (); return free_muts_; } ///< All @em muts that occurr free in this @p Scope.
45 // clang-format on
46 ///@}
47
48 /// @name simple CFA to construct a CFG
49 ///@{
50 const CFA& cfa() const;
51 const F_CFG& f_cfg() const;
52 const B_CFG& b_cfg() const;
53 ///@}
54
55 /// Does @p mut's Var occurr free in @p def?
56 static bool is_free(Def* mut, const Def* def);
57
58private:
59 void run();
60 void calc_bound() const;
61 void calc_free() const;
62
63 World& world_;
64 Def* entry_ = nullptr;
65 Def* exit_ = nullptr;
66 mutable bool has_bound_ = false;
67 mutable bool has_free_ = false;
68 mutable DefSet bound_;
69 mutable DefSet free_defs_;
70 mutable VarSet free_vars_;
71 mutable MutSet free_muts_;
72 mutable std::unique_ptr<const CFA> cfa_;
73};
74
75} // namespace mim
Control Flow Analysis.
Definition cfg.h:62
A Control-Flow Graph.
Definition scope.h:9
Base class for all Defs.
Definition def.h:220
Sym sym() const
Definition def.h:465
A Scope represents a region of Defs that are live from the view of an entry's Var.
Definition scope.h:21
Def * entry() const
Definition scope.h:32
Scope(const Scope &)=delete
Scope & operator=(Scope)=delete
World & world() const
Definition scope.h:31
const DefSet & bound() const
All Defs within this Scope.
Definition scope.h:41
const B_CFG & b_cfg() const
Definition scope.cpp:88
Sym sym() const
Definition scope.h:34
const DefSet & free_defs() const
All non-const Defs directly referenced but not bound within this Scope. May also include Vars or muts...
Definition scope.h:42
const CFA & cfa() const
Definition scope.cpp:86
const VarSet & free_vars() const
All Vars that occurr free in this Scope. Does not transitively contain any free Vars from muts.
Definition scope.h:43
const F_CFG & f_cfg() const
Definition scope.cpp:87
const MutSet & free_muts() const
All muts that occurr free in this Scope.
Definition scope.h:44
static bool is_free(Def *mut, const Def *def)
Does mut's Var occurr free in def?
Definition scope.cpp:90
Def * exit() const
Definition scope.h:33
bool bound(const Def *def) const
Definition scope.h:39
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:33
Definition cfg.h:11
GIDSet< const Var * > VarSet
Definition def.h:77
GIDSet< Def * > MutSet
Definition def.h:68
GIDSet< const Def * > DefSet
Definition def.h:58