Skip to content

Commit 0583457

Browse files
committed
[sil] Add a try_emplace/simple failable get method to BasicBlockData.
1 parent 467b742 commit 0583457

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

include/swift/SIL/BasicBlockData.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,32 @@ class BasicBlockData {
213213
}
214214
return data[getIndex(block)];
215215
}
216+
217+
/// Look up the state associated with \p block. Returns nullptr upon failure.
218+
NullablePtr<Data> get(SILBasicBlock *block) const {
219+
if (block->index < 0)
220+
return {nullptr};
221+
Data *d = &const_cast<BasicBlockData *>(this)->data[getIndex(block)];
222+
return NullablePtr<Data>(d);
223+
}
224+
225+
/// If \p block is a new block, i.e. created after this BasicBlockData was
226+
/// constructed, creates a new Data by calling
227+
/// Data(std::forward<ArgTypes>(Args)...).
228+
template <typename... ArgTypes>
229+
std::pair<Data *, bool> try_emplace(SILBasicBlock *block,
230+
ArgTypes &&...Args) {
231+
if (block->index != 0) {
232+
return {&data[getIndex(block)], false};
233+
}
234+
235+
assert(validForBlockOrder == function->BlockListChangeIdx &&
236+
"BasicBlockData invalid because the function's block list changed");
237+
validForBlockOrder = ++function->BlockListChangeIdx;
238+
block->index = data.size();
239+
data.emplace_back(std::forward<ArgTypes>(Args)...);
240+
return {&data.back(), true};
241+
}
216242
};
217243

218244
} // end swift namespace

0 commit comments

Comments
 (0)