constant.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // Compute the instruction's profile.
  9. ConstantNode node = {.inst = inst, .constant_id = ConstantId::NotConstant};
  10. llvm::FoldingSetNodeID id;
  11. node.Profile(id, constants_.getContext());
  12. // Check if we have already created this constant.
  13. void* insert_pos;
  14. if (ConstantNode* found = constants_.FindNodeOrInsertPos(id, insert_pos)) {
  15. CARBON_CHECK(found->constant_id.is_constant())
  16. << "Found non-constant in constant store for " << inst;
  17. CARBON_CHECK(found->constant_id.is_symbolic() == is_symbolic)
  18. << "Mismatch in phase for constant " << inst;
  19. return found->constant_id;
  20. }
  21. // Create the new inst and insert the new node.
  22. auto inst_id = constants_.getContext()->insts().AddInNoBlock(
  23. NodeIdAndInst::Untyped(Parse::NodeId::Invalid, inst));
  24. auto constant_id = is_symbolic
  25. ? SemIR::ConstantId::ForSymbolicConstant(inst_id)
  26. : SemIR::ConstantId::ForTemplateConstant(inst_id);
  27. node.constant_id = constant_id;
  28. constants_.InsertNode(new (*allocator_) ConstantNode(node), insert_pos);
  29. // The constant value of any constant instruction is that instruction itself.
  30. constants_.getContext()->constant_values().Set(inst_id, constant_id);
  31. return constant_id;
  32. }
  33. auto ConstantStore::GetAsVector() const -> llvm::SmallVector<InstId, 0> {
  34. llvm::SmallVector<InstId, 0> result;
  35. result.reserve(constants_.size());
  36. for (const ConstantNode& node : constants_) {
  37. result.push_back(node.constant_id.inst_id());
  38. }
  39. // For stability, put the results into index order. This happens to also be
  40. // insertion order.
  41. std::sort(result.begin(), result.end(),
  42. [](InstId a, InstId b) { return a.index < b.index; });
  43. return result;
  44. }
  45. auto ConstantStore::ConstantNode::Profile(llvm::FoldingSetNodeID& id,
  46. File* sem_ir) -> void {
  47. ProfileConstant(id, *sem_ir, inst);
  48. }
  49. } // namespace Carbon::SemIR