inst_kind.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_
  6. #include <cstdint>
  7. #include "common/enum_base.h"
  8. #include "llvm/ADT/FoldingSet.h"
  9. namespace Carbon::SemIR {
  10. // Whether an instruction produces or represents a value, and if so, what kind
  11. // of value.
  12. enum class InstValueKind : int8_t {
  13. // This instruction doesn't produce a value, and shouldn't be referenced by
  14. // other
  15. // instructions.
  16. None,
  17. // This instruction represents an expression or expression-like construct that
  18. // produces a value of the type indicated by its `type_id` field.
  19. Typed,
  20. };
  21. // Whether an instruction is a terminator or part of the terminator sequence.
  22. // The instructions in a block appear in the order NotTerminator, then
  23. // TerminatorSequence, then Terminator, which is also the numerical order of
  24. // these values.
  25. enum class TerminatorKind : int8_t {
  26. // This instruction is not a terminator.
  27. NotTerminator,
  28. // This instruction is not itself a terminator, but forms part of a terminator
  29. // sequence.
  30. TerminatorSequence,
  31. // This instruction is a terminator.
  32. Terminator,
  33. };
  34. CARBON_DEFINE_RAW_ENUM_CLASS(InstKind, uint8_t) {
  35. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  36. #include "toolchain/sem_ir/inst_kind.def"
  37. };
  38. class InstKind : public CARBON_ENUM_BASE(InstKind) {
  39. public:
  40. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  41. #include "toolchain/sem_ir/inst_kind.def"
  42. using EnumBase::Create;
  43. // Returns the name to use for this instruction kind in Semantics IR.
  44. auto ir_name() const -> llvm::StringLiteral;
  45. // Returns whether this kind of instruction is expected to produce a value.
  46. auto value_kind() const -> InstValueKind;
  47. // Returns whether this instruction kind is a code block terminator, such as
  48. // an unconditional branch instruction, or part of the termination sequence,
  49. // such as a conditional branch instruction. The termination sequence of a
  50. // code block appears after all other instructions, and ends with a
  51. // terminator instruction.
  52. auto terminator_kind() const -> TerminatorKind;
  53. // Compute a fingerprint for this instruction kind, allowing its use as part
  54. // of the key in a `FoldingSet`.
  55. void Profile(llvm::FoldingSetNodeID& id) { id.AddInteger(AsInt()); }
  56. class Definition;
  57. // Provides a definition for this instruction kind. Should only be called
  58. // once, to construct the kind as part of defining it in `typed_insts.h`.
  59. constexpr auto Define(llvm::StringLiteral ir_name,
  60. TerminatorKind terminator_kind =
  61. TerminatorKind::NotTerminator) const -> Definition;
  62. private:
  63. // Looks up the definition for this instruction kind.
  64. auto definition() const -> const Definition&;
  65. };
  66. #define CARBON_SEM_IR_INST_KIND(Name) \
  67. CARBON_ENUM_CONSTANT_DEFINITION(InstKind, Name)
  68. #include "toolchain/sem_ir/inst_kind.def"
  69. // We expect the instruction kind to fit compactly into 8 bits.
  70. static_assert(sizeof(InstKind) == 1, "Kind objects include padding!");
  71. // A definition of an instruction kind. This is an InstKind value, plus
  72. // ancillary data such as the name to use for the node kind in LLVM IR. These
  73. // are not copyable, and only one instance of this type is expected to exist per
  74. // instruction kind, specifically `TypedInst::Kind`. Use `InstKind` instead as a
  75. // thin wrapper around an instruction kind index.
  76. class InstKind::Definition : public InstKind {
  77. public:
  78. // Not copyable.
  79. Definition(const Definition&) = delete;
  80. auto operator=(const Definition&) -> Definition& = delete;
  81. // Returns the name to use for this instruction kind in Semantics IR.
  82. constexpr auto ir_name() const -> llvm::StringLiteral { return ir_name_; }
  83. // Returns whether this instruction kind is a code block terminator. See
  84. // InstKind::terminator_kind().
  85. constexpr auto terminator_kind() const -> TerminatorKind {
  86. return terminator_kind_;
  87. }
  88. private:
  89. friend class InstKind;
  90. constexpr Definition(InstKind kind, llvm::StringLiteral ir_name,
  91. TerminatorKind terminator_kind)
  92. : InstKind(kind), ir_name_(ir_name), terminator_kind_(terminator_kind) {}
  93. llvm::StringLiteral ir_name_;
  94. TerminatorKind terminator_kind_;
  95. };
  96. constexpr auto InstKind::Define(llvm::StringLiteral ir_name,
  97. TerminatorKind terminator_kind) const
  98. -> Definition {
  99. return Definition(*this, ir_name, terminator_kind);
  100. }
  101. } // namespace Carbon::SemIR
  102. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_