inst_kind.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // `IntValue`.
  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. bool deduce_through = false;
  86. };
  87. // Provides a definition for this instruction kind. Should only be called
  88. // once, to construct the kind as part of defining it in `typed_insts.h`.
  89. template <typename TypedNodeId>
  90. constexpr auto Define(DefinitionInfo info) const -> Definition<TypedNodeId>;
  91. using EnumBase::AsInt;
  92. using EnumBase::FromInt;
  93. using EnumBase::Make;
  94. // Returns true if the kind matches any of the provided instructions' kinds.
  95. template <typename... InstT>
  96. constexpr auto IsAnyOf() const -> bool {
  97. return ((*this == InstT::Kind) || ...);
  98. }
  99. // Returns the name to use for this instruction kind in Semantics IR.
  100. auto ir_name() const -> llvm::StringLiteral {
  101. return definition_info(*this).ir_name;
  102. }
  103. // Returns whether this instruction kind defines a type.
  104. auto is_type() const -> InstIsType { return definition_info(*this).is_type; }
  105. // Returns whether this instruction kind is expected to produce a value.
  106. auto value_kind() const -> InstValueKind;
  107. // Returns this instruction kind's category of allowed constants.
  108. auto constant_kind() const -> InstConstantKind {
  109. return definition_info(*this).constant_kind;
  110. }
  111. // Returns whether this instruction kind is a code block terminator, such as
  112. // an unconditional branch instruction, or part of the termination sequence,
  113. // such as a conditional branch instruction. The termination sequence of a
  114. // code block appears after all other instructions, and ends with a
  115. // terminator instruction.
  116. auto terminator_kind() const -> TerminatorKind {
  117. return definition_info(*this).terminator_kind;
  118. }
  119. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  120. // conclude `A` == `B`.
  121. auto deduce_through() const -> bool {
  122. return definition_info(*this).deduce_through;
  123. }
  124. // Compute a fingerprint for this instruction kind, allowing its use as part
  125. // of the key in a `FoldingSet`.
  126. auto Profile(llvm::FoldingSetNodeID& id) -> void { id.AddInteger(AsInt()); }
  127. private:
  128. // Returns the DefinitionInfo for the kind.
  129. static auto definition_info(InstKind kind) -> const DefinitionInfo&;
  130. };
  131. #define CARBON_SEM_IR_INST_KIND(Name) \
  132. CARBON_ENUM_CONSTANT_DEFINITION(InstKind, Name)
  133. #include "toolchain/sem_ir/inst_kind.def"
  134. // We expect the instruction kind to fit compactly into 8 bits.
  135. static_assert(sizeof(InstKind) == 1, "Kind objects include padding!");
  136. // A definition of an instruction kind. This is an InstKind value, plus
  137. // ancillary data such as the name to use for the node kind in LLVM IR. These
  138. // are not copyable, and only one instance of this type is expected to exist
  139. // per instruction kind, specifically `TypedInst::Kind`. Use `InstKind`
  140. // instead as a thin wrapper around an instruction kind index.
  141. template <typename TypedNodeIdArg>
  142. class InstKind::Definition : public InstKind {
  143. public:
  144. using TypedNodeId = TypedNodeIdArg;
  145. // Not copyable.
  146. Definition(const Definition&) = delete;
  147. auto operator=(const Definition&) -> Definition& = delete;
  148. // Returns the name to use for this instruction kind in Semantics IR.
  149. constexpr auto ir_name() const -> llvm::StringLiteral {
  150. return info_.ir_name;
  151. }
  152. // Returns whether this instruction kind defines a type.
  153. constexpr auto is_type() const -> InstIsType { return info_.is_type; }
  154. // Returns this instruction kind's category of allowed constants.
  155. constexpr auto constant_kind() const -> InstConstantKind {
  156. return info_.constant_kind;
  157. }
  158. // Returns whether this instruction kind is a code block terminator. See
  159. // InstKind::terminator_kind().
  160. constexpr auto terminator_kind() const -> TerminatorKind {
  161. return info_.terminator_kind;
  162. }
  163. // Returns true if the instruction is lowered.
  164. constexpr auto is_lowered() const -> bool { return info_.is_lowered; }
  165. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  166. // conclude `A` == `B`.
  167. constexpr auto deduce_through() const -> bool { return info_.deduce_through; }
  168. private:
  169. friend class InstKind;
  170. constexpr Definition(InstKind kind, InstKind::DefinitionInfo info)
  171. : InstKind(kind), info_(info) {}
  172. InstKind::DefinitionInfo info_;
  173. };
  174. template <typename TypedNodeId>
  175. constexpr auto InstKind::Define(DefinitionInfo info) const
  176. -> Definition<TypedNodeId> {
  177. return Definition<TypedNodeId>(*this, info);
  178. }
  179. } // namespace Carbon::SemIR
  180. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_