Repository: carbon-language/carbon-lang
File: toolchain/check/inst.cpp (C++)

Code before:

    CARBON_VLOG_TO(context.vlog_stream(), "GetOrAddInst: constant: {0}\n",
                   loc_id_and_inst.inst);
    return context.constant_values().GetInstId(const_id);
  };

  // If the instruction is implicit, produce its constant value instead if
  // possible.
  if (loc_id_and_inst.loc_id.is_implicit()) {
    switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
      case SemIR::InstConstantNeedsInstIdKind::No: {
        // Evaluation doesn't need an InstId. Just do it.
        auto const_id = TryEvalInstUnsafe(context, SemIR::InstId::None,
                                          loc_id_and_inst.inst);
        if (auto result_inst_id = handle_constant_id(const_id)) {
          return *result_inst_id;
        }
        break;
      }

      case SemIR::InstConstantNeedsInstIdKind::DuringEvaluation: {
        // Evaluation temporarily needs an InstId. Add one for now.
        auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
        auto const_id = context.constant_values().Get(inst_id);
        if (auto result_inst_id = handle_constant_id(const_id)) {
          // TODO: We didn't end up needing the instruction. Consider removing
          // it from `insts` if it's still the most recently added instruction.
          return *result_inst_id;
        }
        context.inst_block_stack().AddInstId(inst_id);
        return inst_id;
      }

      case SemIR::InstConstantNeedsInstIdKind::Permanent: {
        // Evaluation needs a permanent InstId. Add the instruction.
        break;
      }
    }
  }

  // TODO: For an implicit instruction, this reattempts evaluation.
  return AddInst(context, loc_id_and_inst);
}

auto EvalOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
    -> SemIR::ConstantId {
  switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
    case SemIR::InstConstantNeedsInstIdKind::No: {
      // Evaluation doesn't need an InstId. Just do it.
      return TryEvalInstUnsafe(context, SemIR::InstId::None,
                               loc_id_and_inst.inst);

Review comment (suggestion):
```suggestion
          // TODO: We didn't end up needing the `inst_id` instruction. Consider removing
```

Code after:
    }

    CARBON_VLOG_TO(context.vlog_stream(), "GetOrAddInst: constant: {0}\n",
                   loc_id_and_inst.inst);
    return context.constant_values().GetInstId(const_id);
  };

  // If the instruction is implicit, produce its constant value instead if
  // possible.
  if (loc_id_and_inst.loc_id.is_implicit()) {
    switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
      case SemIR::InstConstantNeedsInstIdKind::No: {
        // Evaluation doesn't need an InstId. Just do it.
        auto const_id = TryEvalInstUnsafe(context, SemIR::InstId::None,
                                          loc_id_and_inst.inst);
        if (auto result_inst_id = handle_constant_id(const_id);
            result_inst_id.has_value()) {
          return result_inst_id;
        }
        break;
      }

      case SemIR::InstConstantNeedsInstIdKind::DuringEvaluation: {
        // Evaluation temporarily needs an InstId. Add one for now.
        auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
        auto const_id = context.constant_values().Get(inst_id);
        if (auto result_inst_id = handle_constant_id(const_id);
            result_inst_id.has_value()) {
          // TODO: We didn't end up needing the `inst_id` instruction. Consider
          // removing it from `insts` if it's still the most recently added
          // instruction.
          CARBON_CHECK(result_inst_id != inst_id);
          return result_inst_id;
        }
        context.inst_block_stack().AddInstId(inst_id);
        return inst_id;
      }

      case SemIR::InstConstantNeedsInstIdKind::Permanent: {
        // Evaluation needs a permanent InstId. Add the instruction.
        break;
      }
    }
  }

  // TODO: For an implicit instruction, this reattempts evaluation.
  return AddInst(context, loc_id_and_inst);
}

auto EvalOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
    -> SemIR::ConstantId {