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) noexcept
29 : state{AssignmentData()}, compiler(compiler) {}
30
31 ValueRef(CompilerBase *compiler, ValLocalIdx local_idx) noexcept
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#ifndef NDEBUG
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#endif
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) noexcept
66 : state{.s = typename Derived::ValRefSpecial(std::forward<T>(args)...)},
67 compiler(compiler) {
68 assert(state.a.mode >= 4);
69 }
70
71 explicit ValueRef(const ValueRef &) = delete;
72
73 ValueRef(ValueRef &&other) noexcept
74 : state{other.state}, compiler(other.compiler) {
75 other.state.a = AssignmentData{};
76 }
77
78 ~ValueRef() noexcept { reset(); }
79
80 ValueRef &operator=(const ValueRef &) = delete;
81
82 ValueRef &operator=(ValueRef &&other) noexcept {
83 if (this == &other) {
84 return *this;
85 }
86 reset();
87 assert(compiler == other.compiler);
88 this->state = other.state;
89 other.state.a.mode = 0;
90 return *this;
91 }
92
93 bool has_assignment() const noexcept { return state.a.mode < 4; }
94
95 [[nodiscard]] ValueAssignment *assignment() const noexcept {
96 assert(has_assignment());
97 assert(state.a.assignment != nullptr);
98 return state.a.assignment;
99 }
100
101 /// Convert into an unowned reference; must be called before first part is
102 /// accessed.
103 void disown() noexcept {
104 if (has_assignment()) {
105 state.a.mode = 0;
106 }
107 }
108
109 ValLocalIdx local_idx() const noexcept {
110 assert(has_assignment());
111 return state.a.local_idx;
112 }
113
114 ValuePartRef part(unsigned part) noexcept TPDE_LIFETIMEBOUND {
115 if (has_assignment()) {
116 return ValuePartRef{
117 compiler, local_idx(), state.a.assignment, part, state.a.mode == 2};
118 }
119 return compiler->derived()->val_part_ref_special(state.s, part);
120 }
121
122 /// Reset the reference to the value part
123 void reset() noexcept;
124
125 bool variable_ref() const noexcept {
126 assert(has_assignment());
127 return state.a.assignment->variable_ref;
128 }
129};
130
131template <IRAdaptor Adaptor, typename Derived, CompilerConfig Config>
132void CompilerBase<Adaptor, Derived, Config>::ValueRef::reset() noexcept {
133 if (state.a.mode == 1 || state.a.mode == 2) {
134 state.a.mode = 0;
135
136 assert(!state.a.assignment->pending_free && "access of free'd assignment");
137
138 auto &ref_count = state.a.assignment->references_left;
139 assert(ref_count != 0);
140 if (--ref_count == 0) {
141 ValLocalIdx local_idx = state.a.local_idx;
142 if (!state.a.assignment->delay_free) {
143 compiler->free_assignment(local_idx);
144 } else {
145 // need to wait until release
146 TPDE_LOG_TRACE("Delay freeing assignment for value {}",
147 static_cast<u32>(local_idx));
148 const auto &liveness = compiler->analyzer.liveness_info(local_idx);
149 auto &free_list_head =
150 compiler->assignments.delayed_free_lists[u32(liveness.last)];
151 state.a.assignment->next_delayed_free_entry = free_list_head;
152#ifndef NDEBUG
153 state.a.assignment->pending_free = true;
154#endif
155 free_list_head = local_idx;
156 }
157 }
158 }
159
160#ifndef NDEBUG
161 state.a.assignment = nullptr;
162 state.a.local_idx = INVALID_VAL_LOCAL_IDX;
163#endif
164}
165} // namespace tpde
CompilerBase(Adaptor *adaptor)
Initialize a CompilerBase, should be called by the derived classes.