constant.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/sem_ir/constant.h"
  5. #include "toolchain/sem_ir/inst_profile.h"
  6. namespace Carbon::SemIR {
  7. auto ConstantStore::GetOrAdd(Inst inst, bool is_symbolic) -> ConstantId {
  8. // Check that we're allowed to form this kind of constant.
  9. switch (inst.kind().constant_kind()) {
  10. case InstConstantKind::Never:
  11. CARBON_FATAL() << "Should not form a constant from instruction " << inst;
  12. break;
  13. case InstConstantKind::SymbolicOnly:
  14. CARBON_CHECK(is_symbolic)
  15. << "Should only form a symbolic constant from instruction " << inst;
  16. break;
  17. case InstConstantKind::Conditional:
  18. break;
  19. case InstConstantKind::Always:
  20. CARBON_CHECK(!is_symbolic)
  21. << "Should only form a template constant from instruction " << inst;
  22. break;
  23. }
  24. // Compute the instruction's profile.
  25. ConstantNode node = {.inst = inst, .constant_id = ConstantId::NotConstant};
  26. llvm::FoldingSetNodeID id;
  27. node.Profile(id, constants_.getContext());
  28. // Check if we have already created this constant.
  29. void* insert_pos;
  30. if (ConstantNode* found = constants_.FindNodeOrInsertPos(id, insert_pos)) {
  31. CARBON_CHECK(found->constant_id.is_constant())
  32. << "Found non-constant in constant store for " << inst;
  33. CARBON_CHECK(found->constant_id.is_symbolic() == is_symbolic)
  34. << "Mismatch in phase for constant " << inst;
  35. return found->constant_id;
  36. }
  37. // Create the new inst and insert the new node.
  38. auto inst_id =
  39. constants_.getContext()->insts().AddInNoBlock(LocIdAndInst::NoLoc(inst));
  40. auto constant_id = is_symbolic
  41. ? SemIR::ConstantId::ForSymbolicConstant(inst_id)
  42. : SemIR::ConstantId::ForTemplateConstant(inst_id);
  43. node.constant_id = constant_id;
  44. constants_.InsertNode(new (*allocator_) ConstantNode(node), insert_pos);
  45. // The constant value of any constant instruction is that instruction itself.
  46. constants_.getContext()->constant_values().Set(inst_id, constant_id);
  47. return constant_id;
  48. }
  49. auto ConstantStore::GetAsVector() const -> llvm::SmallVector<InstId, 0> {
  50. llvm::SmallVector<InstId, 0> result;
  51. result.reserve(constants_.size());
  52. for (const ConstantNode& node : constants_) {
  53. result.push_back(node.constant_id.inst_id());
  54. }
  55. // For stability, put the results into index order. This happens to also be
  56. // insertion order.
  57. std::sort(result.begin(), result.end(),
  58. [](InstId a, InstId b) { return a.index < b.index; });
  59. return result;
  60. }
  61. auto ConstantStore::ConstantNode::Profile(llvm::FoldingSetNodeID& id,
  62. File* sem_ir) -> void {
  63. ProfileConstant(id, *sem_ir, inst);
  64. }
  65. } // namespace Carbon::SemIR