|
@@ -10,30 +10,41 @@
|
|
|
|
|
|
|
|
namespace Carbon::SemIR {
|
|
namespace Carbon::SemIR {
|
|
|
|
|
|
|
|
-auto ConstantStore::GetOrAdd(Inst inst) -> std::pair<InstId, bool> {
|
|
|
|
|
|
|
+auto ConstantStore::GetOrAdd(Inst inst, bool is_symbolic) -> ConstantId {
|
|
|
// Compute the instruction's profile.
|
|
// Compute the instruction's profile.
|
|
|
- ConstantNode node = {.inst = inst, .inst_id = InstId::Invalid};
|
|
|
|
|
|
|
+ ConstantNode node = {.inst = inst, .constant_id = ConstantId::NotConstant};
|
|
|
llvm::FoldingSetNodeID id;
|
|
llvm::FoldingSetNodeID id;
|
|
|
node.Profile(id, constants_.getContext());
|
|
node.Profile(id, constants_.getContext());
|
|
|
|
|
|
|
|
// Check if we have already created this constant.
|
|
// Check if we have already created this constant.
|
|
|
void* insert_pos;
|
|
void* insert_pos;
|
|
|
if (ConstantNode* found = constants_.FindNodeOrInsertPos(id, insert_pos)) {
|
|
if (ConstantNode* found = constants_.FindNodeOrInsertPos(id, insert_pos)) {
|
|
|
- return {found->inst_id, false};
|
|
|
|
|
|
|
+ CARBON_CHECK(found->constant_id.is_constant())
|
|
|
|
|
+ << "Found non-constant in constant store for " << inst;
|
|
|
|
|
+ CARBON_CHECK(found->constant_id.is_symbolic() == is_symbolic)
|
|
|
|
|
+ << "Mismatch in phase for constant " << inst;
|
|
|
|
|
+ return found->constant_id;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Create the new inst and insert the new node.
|
|
// Create the new inst and insert the new node.
|
|
|
- node.inst_id = constants_.getContext()->insts().AddInNoBlock(
|
|
|
|
|
|
|
+ auto inst_id = constants_.getContext()->insts().AddInNoBlock(
|
|
|
ParseNodeAndInst::Untyped(Parse::NodeId::Invalid, inst));
|
|
ParseNodeAndInst::Untyped(Parse::NodeId::Invalid, inst));
|
|
|
|
|
+ auto constant_id = is_symbolic
|
|
|
|
|
+ ? SemIR::ConstantId::ForSymbolicConstant(inst_id)
|
|
|
|
|
+ : SemIR::ConstantId::ForTemplateConstant(inst_id);
|
|
|
|
|
+ node.constant_id = constant_id;
|
|
|
constants_.InsertNode(new (*allocator_) ConstantNode(node), insert_pos);
|
|
constants_.InsertNode(new (*allocator_) ConstantNode(node), insert_pos);
|
|
|
- return {node.inst_id, true};
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // The constant value of any constant instruction is that instruction itself.
|
|
|
|
|
+ constants_.getContext()->constant_values().Set(inst_id, constant_id);
|
|
|
|
|
+ return constant_id;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
auto ConstantStore::GetAsVector() const -> llvm::SmallVector<InstId, 0> {
|
|
auto ConstantStore::GetAsVector() const -> llvm::SmallVector<InstId, 0> {
|
|
|
llvm::SmallVector<InstId, 0> result;
|
|
llvm::SmallVector<InstId, 0> result;
|
|
|
result.reserve(constants_.size());
|
|
result.reserve(constants_.size());
|
|
|
for (const ConstantNode& node : constants_) {
|
|
for (const ConstantNode& node : constants_) {
|
|
|
- result.push_back(node.inst_id);
|
|
|
|
|
|
|
+ result.push_back(node.constant_id.inst_id());
|
|
|
}
|
|
}
|
|
|
// For stability, put the results into index order. This happens to also be
|
|
// For stability, put the results into index order. This happens to also be
|
|
|
// insertion order.
|
|
// insertion order.
|