Files
carbon-lang/toolchain/check/handle_if_statement.cpp
T
Jon Ross-PerkinsandRichard Smith f197219c10 Split parse nodes out from instructions because they're rarely used. (#3590)
The parse nodes are still tracked as part of the same value store
interface in order to ensure parity, but they're split out from Inst
itself in order to reduce the size of Inst -- the expectation is that
they don't need to be passed around quite as much.

This change doesn't actually reduce the passing very much, although
there are hints of it: AddInstAndPush doesn't typically need a separate
parse node from the one on the Inst itself, for example. In a couple
spots I changed code to rely a little more on the InstId until the
ParseNode is needed, but it's very low hanging fruit where done. I think
convert could do more to not eagerly fetch the parse node before its
use, but more cleanup felt it would be easier to handle separately. I'm
currently viewing this as making such cleanup _possible_ rather than
executing on it up-front.

But also, I want to make sure there's a consensus to head in this
direction before pulling the trigger. We speculated that this would
result in the parse node being passed around less, and I do think that's
the case, although it's a bit fuzzy in the change.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-12 19:01:51 +00:00

82 lines
2.8 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/context.h"
#include "toolchain/check/convert.h"
#include "toolchain/sem_ir/inst.h"
namespace Carbon::Check {
auto HandleIfConditionStart(Context& /*context*/,
Parse::IfConditionStartId /*parse_node*/) -> bool {
return true;
}
auto HandleIfCondition(Context& context, Parse::IfConditionId parse_node)
-> bool {
// Convert the condition to `bool`.
auto cond_value_id = context.node_stack().PopExpr();
cond_value_id = ConvertToBoolValue(context, parse_node, cond_value_id);
// Create the then block and the else block, and branch to the right one. If
// there is no `else`, the then block will terminate with a branch to the
// else block, which will be reused as the resumption block.
auto then_block_id =
context.AddDominatedBlockAndBranchIf(parse_node, cond_value_id);
auto else_block_id = context.AddDominatedBlockAndBranch(parse_node);
// Start emitting the `then` block.
context.inst_block_stack().Pop();
context.inst_block_stack().Push(then_block_id);
context.AddCurrentCodeBlockToFunction();
context.node_stack().Push(parse_node, else_block_id);
return true;
}
auto HandleIfStatementElse(Context& context,
Parse::IfStatementElseId parse_node) -> bool {
auto else_block_id = context.node_stack().Pop<Parse::NodeKind::IfCondition>();
// Switch to emitting the `else` block.
context.inst_block_stack().Push(else_block_id);
context.AddCurrentCodeBlockToFunction();
context.node_stack().Push(parse_node);
return true;
}
auto HandleIfStatement(Context& context, Parse::IfStatementId parse_node)
-> bool {
switch (auto kind = context.node_stack().PeekParseNodeKind()) {
case Parse::NodeKind::IfCondition: {
// Branch from then block to else block, and start emitting the else
// block.
auto else_block_id =
context.node_stack().Pop<Parse::NodeKind::IfCondition>();
context.AddInst({parse_node, SemIR::Branch{else_block_id}});
context.inst_block_stack().Pop();
context.inst_block_stack().Push(else_block_id);
break;
}
case Parse::NodeKind::IfStatementElse: {
// Branch from the then and else blocks to a new resumption block.
context.node_stack()
.PopAndDiscardSoloParseNode<Parse::NodeKind::IfStatementElse>();
context.AddConvergenceBlockAndPush(parse_node, /*num_blocks=*/2);
break;
}
default: {
CARBON_FATAL() << "Unexpected parse node at start of `if`: " << kind;
}
}
context.AddCurrentCodeBlockToFunction();
return true;
}
} // namespace Carbon::Check