inst_kind.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/check.h"
  8. #include "common/enum_base.h"
  9. #include "llvm/ADT/FoldingSet.h"
  10. namespace Carbon::SemIR {
  11. // Whether an instruction defines a type.
  12. enum class InstIsType : int8_t {
  13. // Always of type `type`, and might define a type constant.
  14. Always,
  15. // Sometimes of type `type`, and might define a type constant.
  16. Maybe,
  17. // Never defines a type constant. Note that such instructions can still have
  18. // type `type`, but are not the canonical definition of any type.
  19. Never,
  20. };
  21. // Whether an instruction produces or represents a value, and if so, what kind
  22. // of value.
  23. enum class InstValueKind : int8_t {
  24. // This instruction doesn't produce a value, and shouldn't be referenced by
  25. // other instructions.
  26. None,
  27. // This instruction represents an expression or expression-like construct that
  28. // produces a value of the type indicated by its `type_id` field.
  29. Typed,
  30. };
  31. // Whether an instruction can be used to define a constant value. This specifies
  32. // whether the instruction can be added to the `constants()` list. Note that
  33. // even instructions that cannot define a constant value can still have an
  34. // associated `constant_value()`, but the constant value will be a different
  35. // kind of instruction.
  36. enum class InstConstantKind : int8_t {
  37. // This instruction never defines a constant value. For example,
  38. // `UnaryOperatorNot` never defines a constant value; if its operand is a
  39. // template constant, its constant value will instead be a `BoolLiteral`. This
  40. // is also used for instructions that don't produce a value at all.
  41. Never,
  42. // This instruction may be a symbolic constant, depending on its operands, but
  43. // is never a template constant. For example, a `Call` instruction can be a
  44. // symbolic constant but never a template constant.
  45. SymbolicOnly,
  46. // This instruction can define a symbolic or template constant, but might not
  47. // have a constant value, depending on its operands. For example, a
  48. // `TupleValue` can define a constant if its operands are constants.
  49. Conditional,
  50. // This instruction always has a constant value of the same kind. For example,
  51. // `IntLiteral`.
  52. Always,
  53. };
  54. // Whether an instruction is a terminator or part of the terminator sequence.
  55. // The instructions in a block appear in the order NotTerminator, then
  56. // TerminatorSequence, then Terminator, which is also the numerical order of
  57. // these values.
  58. enum class TerminatorKind : int8_t {
  59. // This instruction is not a terminator.
  60. NotTerminator,
  61. // This instruction is not itself a terminator, but forms part of a terminator
  62. // sequence.
  63. TerminatorSequence,
  64. // This instruction is a terminator.
  65. Terminator,
  66. };
  67. CARBON_DEFINE_RAW_ENUM_CLASS(InstKind, uint8_t) {
  68. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  69. #include "toolchain/sem_ir/inst_kind.def"
  70. };
  71. class InstKind : public CARBON_ENUM_BASE(InstKind) {
  72. public:
  73. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  74. #include "toolchain/sem_ir/inst_kind.def"
  75. template <typename TypedNodeId>
  76. class Definition;
  77. // Information about a definition. See associated accessors below for
  78. // comments.
  79. struct DefinitionInfo {
  80. llvm::StringLiteral ir_name;
  81. InstIsType is_type = InstIsType::Never;
  82. InstConstantKind constant_kind = InstConstantKind::Never;
  83. TerminatorKind terminator_kind = TerminatorKind::NotTerminator;
  84. bool is_lowered = true;
  85. };
  86. // Provides a definition for this instruction kind. Should only be called
  87. // once, to construct the kind as part of defining it in `typed_insts.h`.
  88. template <typename TypedNodeId>
  89. constexpr auto Define(DefinitionInfo info) const -> Definition<TypedNodeId>;
  90. using EnumBase::AsInt;
  91. using EnumBase::Make;
  92. // Returns true if the kind matches any of the provided instructions' kinds.
  93. template <typename... InstT>
  94. constexpr auto IsAnyOf() const -> bool {
  95. return ((*this == InstT::Kind) || ...);
  96. }
  97. // Returns the name to use for this instruction kind in Semantics IR.
  98. auto ir_name() const -> llvm::StringLiteral {
  99. return definition_info(*this).ir_name;
  100. }
  101. // Returns whether this instruction kind defines a type.
  102. auto is_type() const -> InstIsType { return definition_info(*this).is_type; }
  103. // Returns whether this instruction kind is expected to produce a value.
  104. auto value_kind() const -> InstValueKind;
  105. // Returns this instruction kind's category of allowed constants.
  106. auto constant_kind() const -> InstConstantKind {
  107. return definition_info(*this).constant_kind;
  108. }
  109. // Returns whether this instruction kind is a code block terminator, such as
  110. // an unconditional branch instruction, or part of the termination sequence,
  111. // such as a conditional branch instruction. The termination sequence of a
  112. // code block appears after all other instructions, and ends with a
  113. // terminator instruction.
  114. auto terminator_kind() const -> TerminatorKind {
  115. return definition_info(*this).terminator_kind;
  116. }
  117. // Compute a fingerprint for this instruction kind, allowing its use as part
  118. // of the key in a `FoldingSet`.
  119. void Profile(llvm::FoldingSetNodeID& id) { id.AddInteger(AsInt()); }
  120. private:
  121. // Returns the DefinitionInfo for the kind.
  122. static auto definition_info(InstKind kind) -> const DefinitionInfo&;
  123. };
  124. #define CARBON_SEM_IR_INST_KIND(Name) \
  125. CARBON_ENUM_CONSTANT_DEFINITION(InstKind, Name)
  126. #include "toolchain/sem_ir/inst_kind.def"
  127. // We expect the instruction kind to fit compactly into 8 bits.
  128. static_assert(sizeof(InstKind) == 1, "Kind objects include padding!");
  129. // A definition of an instruction kind. This is an InstKind value, plus
  130. // ancillary data such as the name to use for the node kind in LLVM IR. These
  131. // are not copyable, and only one instance of this type is expected to exist
  132. // per instruction kind, specifically `TypedInst::Kind`. Use `InstKind`
  133. // instead as a thin wrapper around an instruction kind index.
  134. template <typename TypedNodeIdArg>
  135. class InstKind::Definition : public InstKind {
  136. public:
  137. using TypedNodeId = TypedNodeIdArg;
  138. // Not copyable.
  139. Definition(const Definition&) = delete;
  140. auto operator=(const Definition&) -> Definition& = delete;
  141. // Returns the name to use for this instruction kind in Semantics IR.
  142. constexpr auto ir_name() const -> llvm::StringLiteral {
  143. return info_.ir_name;
  144. }
  145. // Returns whether this instruction kind defines a type.
  146. constexpr auto is_type() const -> InstIsType { return info_.is_type; }
  147. // Returns this instruction kind's category of allowed constants.
  148. constexpr auto constant_kind() const -> InstConstantKind {
  149. return info_.constant_kind;
  150. }
  151. // Returns whether this instruction kind is a code block terminator. See
  152. // InstKind::terminator_kind().
  153. constexpr auto terminator_kind() const -> TerminatorKind {
  154. return info_.terminator_kind;
  155. }
  156. // Returns true if the instruction is lowered.
  157. constexpr auto is_lowered() const -> bool { return info_.is_lowered; }
  158. private:
  159. friend class InstKind;
  160. constexpr Definition(InstKind kind, InstKind::DefinitionInfo info)
  161. : InstKind(kind), info_(info) {}
  162. InstKind::DefinitionInfo info_;
  163. };
  164. template <typename TypedNodeId>
  165. constexpr auto InstKind::Define(DefinitionInfo info) const
  166. -> Definition<TypedNodeId> {
  167. return Definition<TypedNodeId>(*this, info);
  168. }
  169. } // namespace Carbon::SemIR
  170. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_