mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Move control flow block functions to their own file. (#4921)
context.cpp is getting large, so I'm looking at a few ways to cut out clusters of functions. This felt like a logical cluster of functions to move to their own file. Note this doesn't touch the implementation at all, beyond what's needed to change from `Context` members to context args.
This commit is contained in:
@@ -745,110 +745,6 @@ auto Context::LookupNameInCore(SemIR::LocId loc_id, llvm::StringRef name)
|
||||
return constant_values().GetConstantInstId(scope_result.target_inst_id());
|
||||
}
|
||||
|
||||
template <typename BranchNode, typename... Args>
|
||||
static auto AddDominatedBlockAndBranchImpl(Context& context,
|
||||
Parse::NodeId node_id, Args... args)
|
||||
-> SemIR::InstBlockId {
|
||||
if (!context.inst_block_stack().is_current_block_reachable()) {
|
||||
return SemIR::InstBlockId::Unreachable;
|
||||
}
|
||||
auto block_id = context.inst_blocks().AddDefaultValue();
|
||||
context.AddInst<BranchNode>(node_id, {block_id, args...});
|
||||
return block_id;
|
||||
}
|
||||
|
||||
auto Context::AddDominatedBlockAndBranch(Parse::NodeId node_id)
|
||||
-> SemIR::InstBlockId {
|
||||
return AddDominatedBlockAndBranchImpl<SemIR::Branch>(*this, node_id);
|
||||
}
|
||||
|
||||
auto Context::AddDominatedBlockAndBranchWithArg(Parse::NodeId node_id,
|
||||
SemIR::InstId arg_id)
|
||||
-> SemIR::InstBlockId {
|
||||
return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(*this, node_id,
|
||||
arg_id);
|
||||
}
|
||||
|
||||
auto Context::AddDominatedBlockAndBranchIf(Parse::NodeId node_id,
|
||||
SemIR::InstId cond_id)
|
||||
-> SemIR::InstBlockId {
|
||||
return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(*this, node_id,
|
||||
cond_id);
|
||||
}
|
||||
|
||||
auto Context::AddConvergenceBlockAndPush(Parse::NodeId node_id, int num_blocks)
|
||||
-> void {
|
||||
CARBON_CHECK(num_blocks >= 2, "no convergence");
|
||||
|
||||
SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
|
||||
for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
|
||||
if (inst_block_stack().is_current_block_reachable()) {
|
||||
if (new_block_id == SemIR::InstBlockId::Unreachable) {
|
||||
new_block_id = inst_blocks().AddDefaultValue();
|
||||
}
|
||||
CARBON_CHECK(node_id.has_value());
|
||||
AddInst<SemIR::Branch>(node_id, {.target_id = new_block_id});
|
||||
}
|
||||
inst_block_stack().Pop();
|
||||
}
|
||||
inst_block_stack().Push(new_block_id);
|
||||
AddToRegion(new_block_id, node_id);
|
||||
}
|
||||
|
||||
auto Context::AddConvergenceBlockWithArgAndPush(
|
||||
Parse::NodeId node_id, std::initializer_list<SemIR::InstId> block_args)
|
||||
-> SemIR::InstId {
|
||||
CARBON_CHECK(block_args.size() >= 2, "no convergence");
|
||||
|
||||
SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
|
||||
for (auto arg_id : block_args) {
|
||||
if (inst_block_stack().is_current_block_reachable()) {
|
||||
if (new_block_id == SemIR::InstBlockId::Unreachable) {
|
||||
new_block_id = inst_blocks().AddDefaultValue();
|
||||
}
|
||||
AddInst<SemIR::BranchWithArg>(
|
||||
node_id, {.target_id = new_block_id, .arg_id = arg_id});
|
||||
}
|
||||
inst_block_stack().Pop();
|
||||
}
|
||||
inst_block_stack().Push(new_block_id);
|
||||
AddToRegion(new_block_id, node_id);
|
||||
|
||||
// Acquire the result value.
|
||||
SemIR::TypeId result_type_id = insts().Get(*block_args.begin()).type_id();
|
||||
return AddInst<SemIR::BlockArg>(
|
||||
node_id, {.type_id = result_type_id, .block_id = new_block_id});
|
||||
}
|
||||
|
||||
auto Context::SetBlockArgResultBeforeConstantUse(SemIR::InstId select_id,
|
||||
SemIR::InstId cond_id,
|
||||
SemIR::InstId if_true,
|
||||
SemIR::InstId if_false)
|
||||
-> void {
|
||||
CARBON_CHECK(insts().Is<SemIR::BlockArg>(select_id));
|
||||
|
||||
// Determine the constant result based on the condition value.
|
||||
SemIR::ConstantId const_id = SemIR::ConstantId::NotConstant;
|
||||
auto cond_const_id = constant_values().Get(cond_id);
|
||||
if (!cond_const_id.is_template()) {
|
||||
// Symbolic or non-constant condition means a non-constant result.
|
||||
} else if (auto literal = insts().TryGetAs<SemIR::BoolLiteral>(
|
||||
constant_values().GetInstId(cond_const_id))) {
|
||||
const_id = constant_values().Get(literal.value().value.ToBool() ? if_true
|
||||
: if_false);
|
||||
} else {
|
||||
CARBON_CHECK(cond_const_id == SemIR::ErrorInst::SingletonConstantId,
|
||||
"Unexpected constant branch condition.");
|
||||
const_id = SemIR::ErrorInst::SingletonConstantId;
|
||||
}
|
||||
|
||||
if (const_id.is_constant()) {
|
||||
CARBON_VLOG("Constant: {0} -> {1}\n", insts().Get(select_id),
|
||||
constant_values().GetInstId(const_id));
|
||||
constant_values().Set(select_id, const_id);
|
||||
}
|
||||
}
|
||||
|
||||
auto Context::AddToRegion(SemIR::InstBlockId block_id, SemIR::LocId loc_id)
|
||||
-> void {
|
||||
if (region_stack_.empty()) {
|
||||
@@ -936,22 +832,6 @@ auto Context::InsertHere(SemIR::ExprRegionId region_id) -> SemIR::InstId {
|
||||
return region.result_id;
|
||||
}
|
||||
|
||||
auto Context::is_current_position_reachable() -> bool {
|
||||
if (!inst_block_stack().is_current_block_reachable()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Our current position is at the end of a reachable block. That position is
|
||||
// reachable unless the previous instruction is a terminator instruction.
|
||||
auto block_contents = inst_block_stack().PeekCurrentBlockContents();
|
||||
if (block_contents.empty()) {
|
||||
return true;
|
||||
}
|
||||
const auto& last_inst = insts().Get(block_contents.back());
|
||||
return last_inst.kind().terminator_kind() !=
|
||||
SemIR::TerminatorKind::Terminator;
|
||||
}
|
||||
|
||||
auto Context::Finalize() -> void {
|
||||
// Pop information for the file-level scope.
|
||||
sem_ir().set_top_inst_block_id(inst_block_stack().Pop());
|
||||
|
||||
Reference in New Issue
Block a user