TPDE
Loading...
Searching...
No Matches
ValueRef.hpp
1// SPDX-FileCopyrightText: 2025 Contributors to TPDE <https://tpde.org>
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3#pragma once
4
5#include "tpde/ValueAssignment.hpp"
6
7#include <cstring>
8
9namespace tpde {
10
11template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
12struct CompilerBase<Adaptor, Derived, Config>::ValueRef {
13 struct AssignmentData {
14 /// 0 = unowned reference/invalid, 1 = ref-counted, 2 = owned
15 uint8_t mode;
16 ValLocalIdx local_idx;
17 ValueAssignment *assignment;
18 };
19 static_assert(ValRefSpecialStruct<AssignmentData>);
20
21 union {
22 AssignmentData a;
23 Derived::ValRefSpecial s;
24 } state;
25
26 CompilerBase *compiler;
27
28 ValueRef(CompilerBase *compiler)
29 : state{AssignmentData()}, compiler(compiler) {}
30
31 ValueRef(CompilerBase *compiler, ValLocalIdx local_idx)
32 : state{AssignmentData{
33 .local_idx = local_idx,
34 .assignment = compiler->val_assignment(local_idx),
35 }
36 }, compiler(compiler) {
37 assert(!state.a.assignment->pending_free && "access of free'd assignment");
38
39 // Extended liveness checks in debug builds.
40 if constexpr (WithAsserts) {
41 if (!variable_ref()) {
42 const auto &liveness =
43 compiler->analyzer.liveness_info(state.a.local_idx);
44 assert(liveness.last >= compiler->cur_block_idx &&
45 "ref-counted value used outside of its live range");
46 assert(state.a.assignment->references_left != 0);
47 if (state.a.assignment->references_left == 1 && !liveness.last_full) {
48 assert(liveness.last == compiler->cur_block_idx &&
49 "liveness of non-last-full value must end at last use");
50 }
51 }
52 }
53
54 if (variable_ref()) {
55 state.a.mode = 0;
56 } else if (state.a.assignment->references_left <= 1 &&
57 !state.a.assignment->delay_free) {
58 state.a.mode = 2;
59 } else {
60 state.a.mode = 1;
61 }
62 }
63
64 template <typename... T>
65 ValueRef(CompilerBase *compiler, T &&...args)
66 : state{.s = typename Derived::ValRefSpecial(std::forward<T>(args)...)},
67 compiler(compiler) {
68 assert(state.a.mode >= 4);
69 }
70
71private:
72 // Private copy constructor.
73 ValueRef(const ValueRef &other) = default;
74
75public:
76 ValueRef(ValueRef &&other) : state{other.state}, compiler(other.compiler) {
77 other.state.a = AssignmentData{};
78 }
79
80 ~ValueRef() { reset(); }
81
82 ValueRef &operator=(const ValueRef &) = delete;
83
84 ValueRef &operator=(ValueRef &&other) {
85 if (this == &other) {
86 return *this;
87 }
88 reset();
89 assert(compiler == other.compiler);
90 this->state = other.state;
91 other.state.a.mode = 0;
92 return *this;
93 }
94
95 bool has_assignment() const { return state.a.mode < 4; }
96
97 [[nodiscard]] ValueAssignment *assignment() const {
98 assert(has_assignment());
99 assert(state.a.assignment != nullptr);
100 return state.a.assignment;
101 }
102
103 /// Returns whether the value is destroyed after this use.
104 bool is_owned() { return state.a.mode == 2; }
105
106 /// Convert into an unowned reference; must be called before first part is
107 /// accessed.
108 void disown() {
109 if (has_assignment()) {
110 state.a.mode = 0;
111 }
112 }
113
114 /// Get an unowned reference to this value. Previously accessed parts might
115 /// already have been destroyed if the value is in its last use.
116 ValueRef disowned() {
117 ValueRef res = *this;
118 res.disown();
119 return res;
120 }
121
122 ValLocalIdx local_idx() const {
123 assert(has_assignment());
124 return state.a.local_idx;
125 }
126
127 ValuePartRef part(unsigned part) TPDE_LIFETIMEBOUND {
128 if (has_assignment()) {
129 return ValuePartRef{
130 compiler, local_idx(), state.a.assignment, part, is_owned()};
131 }
132 return ValuePartRef{
133 compiler, compiler->derived()->val_part_ref_special(state.s, part)};
134 }
135
136 /// Like part(), but the returned part is always unowned and will not release
137 /// registers of the value assignment when reset.
138 ValuePartRef part_unowned(unsigned part) TPDE_LIFETIMEBOUND {
139 if (has_assignment()) {
140 return ValuePartRef{
141 compiler, local_idx(), state.a.assignment, part, false};
142 }
143 return ValuePartRef{
144 compiler, compiler->derived()->val_part_ref_special(state.s, part)};
145 }
146
147 /// Reset the reference to the value part
148 void reset();
149
150 bool variable_ref() const {
151 assert(has_assignment());
152 return state.a.assignment->variable_ref;
153 }
154};
155
156template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
157void CompilerBase<Adaptor, Derived, Config>::ValueRef::reset() {
158 if (state.a.mode == 1 || state.a.mode == 2) {
159 state.a.mode = 0;
160
161 assert(!state.a.assignment->pending_free && "access of free'd assignment");
162
163 auto &ref_count = state.a.assignment->references_left;
164 assert(ref_count != 0);
165 if (--ref_count == 0) {
166 compiler->release_assignment(state.a.local_idx, state.a.assignment);
167 }
168 }
169
170 if constexpr (WithAsserts) {
171 state.a.assignment = nullptr;
172 state.a.local_idx = INVALID_VAL_LOCAL_IDX;
173 }
174}
175} // namespace tpde
A default implementation for ValRefSpecial.
The base class for the compiler.
void reset()
Reset any leftover data from the previous compilation such that it will not affect the next compilati...
CompilerBase(Adaptor *adaptor)
Initialize a CompilerBase, should be called by the derived classes.