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
5namespace mim {
6
7class CFA;
8template<bool> class CFG;
9
10/// @name Control Flow
11///@{
14///@}
15
16/// A @p Scope represents a region of @p Def%s that are live from the view of an @p entry's @p Var.
17/// Transitively, all user's of the @p entry's @p Var are pooled into this @p Scope (see @p defs()).
18/// Both @p entry() and @p exit() are @em NOT part of the @p Scope itself.
19/// The @p exit() is just a virtual dummy to have a unique exit dual to @p entry().
20class Scope {
21public:
22 Scope(const Scope&) = delete;
23 Scope& operator=(Scope) = delete;
24
25 explicit Scope(Def* entry);
26 ~Scope();
27
28 /// @name getters
29 ///@{
30 World& world() const { return world_; }
31 Def* entry() const { return entry_; }
32 Def* exit() const { return exit_; }
33 Sym sym() const { return entry_->sym(); }
34 ///@}
35
36 /// @name Def%s bound/free in this Scope
37 ///@{
38 bool bound(const Def* def) const { return bound().contains(def); }
39 // clang-format off
40 const DefSet& bound() const { calc_bound(); return bound_; } ///< All @p Def%s within this @p Scope.
41 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.
42 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.
43 const MutSet& free_muts() const { calc_free (); return free_muts_; } ///< All @em muts that occurr free in this @p Scope.
44 // clang-format on
45 ///@}
46
47 /// @name simple CFA to construct a CFG
48 ///@{
49 const CFA& cfa() const;
50 const F_CFG& f_cfg() const;
51 const B_CFG& b_cfg() const;
52 ///@}
53
54 /// Does @p mut's Var occurr free in @p def?
55 static bool is_free(Def* mut, const Def* def);
56
57private:
58 void run();
59 void calc_bound() const;
60 void calc_free() const;
61
62 World& world_;
63 Def* entry_ = nullptr;
64 Def* exit_ = nullptr;
65 mutable bool has_bound_ = false;
66 mutable bool has_free_ = false;
67 mutable DefSet bound_;
68 mutable DefSet free_defs_;
69 mutable VarSet free_vars_;
70 mutable MutSet free_muts_;
71 mutable std::unique_ptr<const CFA> cfa_;
72};
73
74} // namespace mim
Control Flow Analysis.
Definition cfg.h:62
A Control-Flow Graph.
Definition scope.h:8
Base class for all Defs.
Definition def.h:223
Sym sym() const
Definition def.h:468
A Scope represents a region of Defs that are live from the view of an entry's Var.
Definition scope.h:20
Def * entry() const
Definition scope.h:31
Scope(const Scope &)=delete
Scope & operator=(Scope)=delete
World & world() const
Definition scope.h:30
const DefSet & bound() const
All Defs within this Scope.
Definition scope.h:40
const B_CFG & b_cfg() const
Definition scope.cpp:88
Sym sym() const
Definition scope.h:33
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:41
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:42
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:43
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:32
bool bound(const Def *def) const
Definition scope.h:38
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:78
GIDSet< Def * > MutSet
Definition def.h:69
GIDSet< const Def * > DefSet
Definition def.h:59