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