inst_kind.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. template <typename TypedNodeId>
  43. class Definition;
  44. // Provides a definition for this instruction kind. Should only be called
  45. // once, to construct the kind as part of defining it in `typed_insts.h`.
  46. template <typename TypedNodeId>
  47. constexpr auto Define(
  48. llvm::StringLiteral ir_name,
  49. TerminatorKind terminator_kind = TerminatorKind::NotTerminator) const
  50. -> Definition<TypedNodeId>;
  51. using EnumBase::AsInt;
  52. using EnumBase::Make;
  53. // Returns the name to use for this instruction kind in Semantics IR.
  54. auto ir_name() const -> llvm::StringLiteral;
  55. // Returns whether this kind of instruction is expected to produce a value.
  56. auto value_kind() const -> InstValueKind;
  57. // Returns whether this instruction kind is a code block terminator, such as
  58. // an unconditional branch instruction, or part of the termination sequence,
  59. // such as a conditional branch instruction. The termination sequence of a
  60. // code block appears after all other instructions, and ends with a
  61. // terminator instruction.
  62. auto terminator_kind() const -> TerminatorKind;
  63. // Compute a fingerprint for this instruction kind, allowing its use as part
  64. // of the key in a `FoldingSet`.
  65. void Profile(llvm::FoldingSetNodeID& id) { id.AddInteger(AsInt()); }
  66. };
  67. #define CARBON_SEM_IR_INST_KIND(Name) \
  68. CARBON_ENUM_CONSTANT_DEFINITION(InstKind, Name)
  69. #include "toolchain/sem_ir/inst_kind.def"
  70. // We expect the instruction kind to fit compactly into 8 bits.
  71. static_assert(sizeof(InstKind) == 1, "Kind objects include padding!");
  72. // A definition of an instruction kind. This is an InstKind value, plus
  73. // ancillary data such as the name to use for the node kind in LLVM IR. These
  74. // are not copyable, and only one instance of this type is expected to exist per
  75. // instruction kind, specifically `TypedInst::Kind`. Use `InstKind` instead as a
  76. // thin wrapper around an instruction kind index.
  77. template <typename TypedNodeIdArg>
  78. class InstKind::Definition : public InstKind {
  79. public:
  80. using TypedNodeId = TypedNodeIdArg;
  81. // Not copyable.
  82. Definition(const Definition&) = delete;
  83. auto operator=(const Definition&) -> Definition& = delete;
  84. // Returns the name to use for this instruction kind in Semantics IR.
  85. constexpr auto ir_name() const -> llvm::StringLiteral { return ir_name_; }
  86. // Returns whether this instruction kind is a code block terminator. See
  87. // InstKind::terminator_kind().
  88. constexpr auto terminator_kind() const -> TerminatorKind {
  89. return terminator_kind_;
  90. }
  91. private:
  92. friend class InstKind;
  93. constexpr Definition(InstKind kind, llvm::StringLiteral ir_name,
  94. TerminatorKind terminator_kind)
  95. : InstKind(kind), ir_name_(ir_name), terminator_kind_(terminator_kind) {}
  96. llvm::StringLiteral ir_name_;
  97. TerminatorKind terminator_kind_;
  98. };
  99. template <typename TypedNodeId>
  100. constexpr auto InstKind::Define(llvm::StringLiteral ir_name,
  101. TerminatorKind terminator_kind) const
  102. -> Definition<TypedNodeId> {
  103. return Definition<TypedNodeId>(*this, ir_name, terminator_kind);
  104. }
  105. } // namespace Carbon::SemIR
  106. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_