Files
carbon-lang/toolchain/check/inst.cpp
T
Jon Ross-Perkins 311b4ff03d Refactor AddInst-family functions to their own file (#4941)
This in particular uses free functions because it's likely to end up
more consistent with types (versus a wrapper object for InstStore).
Note, this is unlikely to have a performance impact, but if it does, we
can look into related approaches (and we've already discussed using
LTO).

Renames `PendingBlock::AddInst` to `PendingBlock::Add` because
`MakeElementAccessInst` expects the matching name to exist.
2025-02-14 19:44:36 +00:00

137 lines
5.2 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/check/inst.h"
#include "common/vlog.h"
#include "toolchain/check/context.h"
#include "toolchain/check/eval.h"
namespace Carbon::Check {
// Finish producing an instruction. Set its constant value, and register it in
// any applicable instruction lists.
static auto FinishInst(Context& context, SemIR::InstId inst_id,
SemIR::Inst inst) -> void {
GenericRegionStack::DependencyKind dep_kind =
GenericRegionStack::DependencyKind::None;
// If the instruction has a symbolic constant type, track that we need to
// substitute into it.
if (context.constant_values().DependsOnGenericParameter(
context.types().GetConstantId(inst.type_id()))) {
dep_kind |= GenericRegionStack::DependencyKind::SymbolicType;
}
// If the instruction has a constant value, compute it.
auto const_id = TryEvalInst(context, inst_id, inst);
context.constant_values().Set(inst_id, const_id);
if (const_id.is_constant()) {
CARBON_VLOG_TO(context.vlog_stream(), "Constant: {0} -> {1}\n", inst,
context.constant_values().GetInstId(const_id));
// If the constant value is symbolic, track that we need to substitute into
// it.
if (context.constant_values().DependsOnGenericParameter(const_id)) {
dep_kind |= GenericRegionStack::DependencyKind::SymbolicConstant;
}
}
// Keep track of dependent instructions.
if (dep_kind != GenericRegionStack::DependencyKind::None) {
// TODO: Also check for template-dependent instructions.
context.generic_region_stack().AddDependentInst(
{.inst_id = inst_id, .kind = dep_kind});
}
}
auto AddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId {
auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
context.inst_block_stack().AddInstId(inst_id);
return inst_id;
}
auto AddInstInNoBlock(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId {
auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
CARBON_VLOG_TO(context.vlog_stream(), "AddInst: {0}\n", loc_id_and_inst.inst);
FinishInst(context, inst_id, loc_id_and_inst.inst);
return inst_id;
}
auto AddPatternInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId {
auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
context.pattern_block_stack().AddInstId(inst_id);
return inst_id;
}
auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId {
if (loc_id_and_inst.loc_id.is_implicit()) {
auto const_id =
TryEvalInst(context, SemIR::InstId::None, loc_id_and_inst.inst);
if (const_id.has_value()) {
CARBON_VLOG_TO(context.vlog_stream(), "GetOrAddInst: constant: {0}\n",
loc_id_and_inst.inst);
return context.constant_values().GetInstId(const_id);
}
}
// TODO: For an implicit instruction, this reattempts evaluation.
return AddInst(context, loc_id_and_inst);
}
auto AddPlaceholderInstInNoBlock(Context& context,
SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId {
auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
CARBON_VLOG_TO(context.vlog_stream(), "AddPlaceholderInst: {0}\n",
loc_id_and_inst.inst);
context.constant_values().Set(inst_id, SemIR::ConstantId::None);
return inst_id;
}
auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId {
auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
context.inst_block_stack().AddInstId(inst_id);
return inst_id;
}
auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
SemIR::InstId inst_id,
SemIR::LocIdAndInst loc_id_and_inst)
-> void {
context.sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
loc_id_and_inst.inst);
FinishInst(context, inst_id, loc_id_and_inst.inst);
}
auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
SemIR::Inst inst) -> void {
context.sem_ir().insts().Set(inst_id, inst);
CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
inst);
FinishInst(context, inst_id, inst);
}
auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
SemIR::Inst inst) -> void {
auto old_const_id = context.constant_values().Get(inst_id);
context.sem_ir().insts().Set(inst_id, inst);
CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
inst);
auto new_const_id = TryEvalInst(context, inst_id, inst);
CARBON_CHECK(old_const_id == new_const_id);
}
auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
Parse::NodeId node_id) -> void {
context.sem_ir().insts().SetLocId(inst_id, SemIR::LocId(node_id));
}
} // namespace Carbon::Check