inst_kind.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. namespace Carbon::SemIR {
  9. // Whether an instruction defines a type.
  10. enum class InstIsType : int8_t {
  11. // Always of type `type`, and might define a type constant.
  12. Always,
  13. // Sometimes of type `type`, and might define a type constant.
  14. Maybe,
  15. // Never defines a type constant. Note that such instructions can still have
  16. // type `type`, but are not the canonical definition of any type.
  17. Never,
  18. };
  19. // Whether an instruction produces or represents a value, and if so, what kind
  20. // of value.
  21. enum class InstValueKind : int8_t {
  22. // This instruction doesn't produce a value, and shouldn't be referenced by
  23. // other instructions.
  24. None,
  25. // This instruction represents an expression or expression-like construct that
  26. // produces a value of the type indicated by its `type_id` field.
  27. Typed,
  28. };
  29. // Whether an instruction can have a constant value, and whether it can be used
  30. // to define a constant value.
  31. //
  32. // This specifies whether an instruction of this kind can have a corresponding
  33. // constant value in the `constant_values()` list, and whether an instruction of
  34. // this kind can be added to the `constants()` list.
  35. enum class InstConstantKind : int8_t {
  36. // This instruction is never constant. Its constant value is always
  37. // `NotConstant`. This is also used for instructions that don't produce a
  38. // value at all and aren't used as constants.
  39. Never,
  40. // This instruction never defines a constant value, but can evaluate to a
  41. // constant value of a different kind. For example, `UnaryOperatorNot` never
  42. // defines a constant value; if its operand is a concrete constant, its
  43. // constant value will instead be a `BoolLiteral`, and if its operand is not a
  44. // concrete constant, the result is non-constant. This is the default.
  45. Indirect,
  46. // This instruction may define a symbolic constant, depending on its operands,
  47. // but never a concrete constant. For example, a `Call` instruction can define
  48. // a symbolic constant but never a concrete constant. The instruction may have
  49. // a concrete constant value of a different kind.
  50. SymbolicOnly,
  51. // This instruction is a metaprogramming or template instantiation action that
  52. // generates an instruction. Like `SymbolicOnly`, it may define a symbolic
  53. // constant, depending on its operands, but never defines a concrete constant.
  54. // The instruction may have a concrete constant value that is a generated
  55. // instruction. Constant evaluation support for types with this constant kind
  56. // is provided automatically, by calling `PerformDelayedAction`.
  57. InstAction,
  58. // This instruction can define a symbolic or concrete constant, but might not
  59. // have a constant value, might have a constant value that is not defined by
  60. // itself, or might result in a compile-time error, depending on its operands.
  61. // For example, `ArrayType` is a compile-time constant if its operands are
  62. // constant and its array bound is within a valid range.
  63. Conditional,
  64. // This instruction defines a symbolic or concrete constant whenever its
  65. // operands are constant. Otherwise, it is non-constant. For example, a
  66. // `TupleValue` defines a constant if and only if its operands are constants.
  67. // Constant evaluation support for types with this constant kind is provided
  68. // automatically.
  69. WheneverPossible,
  70. // This instruction always has a constant value of the same kind. This is the
  71. // same as `WheneverPossible`, except that the operands are known in advance
  72. // to always be constant. For example, `IntValue`.
  73. Always,
  74. // This instruction is itself a unique constant. This is used for declarations
  75. // whose constant identity is simply themselves. The `ConstantId` for this
  76. // instruction will always be a concrete constant whose `InstId` refers
  77. // directly back to the instruction, rather than to a separate instruction in
  78. // the constants block.
  79. // TODO: Decide if this is the model we want for these cases.
  80. Unique,
  81. };
  82. // Whether constant evaluation of an instruction needs the instruction to have
  83. // been created and allocated an InstId, or only needs the instruction operands.
  84. enum class InstConstantNeedsInstIdKind : int8_t {
  85. // This instruction kind doesn't need an InstId to be evaluated.
  86. No,
  87. // This instruction needs an InstId during evaluation, but doesn't need the
  88. // instruction to persist after evaluation.
  89. DuringEvaluation,
  90. // This instruction needs a permanent instruction ID, for example because that
  91. // instruction ID can appear in the constant result of evaluation.
  92. Permanent,
  93. };
  94. // Whether an instruction is a terminator or part of the terminator sequence.
  95. // The instructions in a block appear in the order NotTerminator, then
  96. // TerminatorSequence, then Terminator, which is also the numerical order of
  97. // these values.
  98. enum class TerminatorKind : int8_t {
  99. // This instruction is not a terminator.
  100. NotTerminator,
  101. // This instruction is not itself a terminator, but forms part of a terminator
  102. // sequence.
  103. TerminatorSequence,
  104. // This instruction is a terminator.
  105. Terminator,
  106. };
  107. CARBON_DEFINE_RAW_ENUM_CLASS(InstKind, uint8_t) {
  108. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  109. #include "toolchain/sem_ir/inst_kind.def"
  110. };
  111. class InstKind : public CARBON_ENUM_BASE(InstKind) {
  112. public:
  113. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  114. #include "toolchain/sem_ir/inst_kind.def"
  115. // Returns the `InstKind` for an instruction, for `CARBON_KIND_SWITCH`.
  116. template <typename InstT>
  117. static constexpr auto& For = InstT::Kind;
  118. template <typename TypedNodeId>
  119. class Definition;
  120. // Information about a definition. See associated accessors below for
  121. // comments.
  122. struct DefinitionInfo {
  123. llvm::StringLiteral ir_name;
  124. InstIsType is_type = InstIsType::Never;
  125. InstConstantKind constant_kind = InstConstantKind::Indirect;
  126. InstConstantNeedsInstIdKind constant_needs_inst_id =
  127. constant_kind == InstConstantKind::Unique
  128. ? InstConstantNeedsInstIdKind::Permanent
  129. : InstConstantNeedsInstIdKind::No;
  130. TerminatorKind terminator_kind = TerminatorKind::NotTerminator;
  131. bool is_lowered = true;
  132. bool deduce_through = false;
  133. bool has_cleanup = false;
  134. };
  135. // Provides a definition for this instruction kind. Should only be called
  136. // once, to construct the kind as part of defining it in `typed_insts.h`.
  137. template <typename TypedNodeId>
  138. constexpr auto Define(DefinitionInfo info) const -> Definition<TypedNodeId>;
  139. using EnumBase::AsInt;
  140. using EnumBase::FromInt;
  141. using EnumBase::Make;
  142. // Returns true if the kind matches any of the provided instructions' kinds.
  143. template <typename... InstT>
  144. constexpr auto IsAnyOf() const -> bool {
  145. return ((*this == InstT::Kind) || ...);
  146. }
  147. // Returns the name to use for this instruction kind in Semantics IR.
  148. auto ir_name() const -> llvm::StringLiteral {
  149. return definition_info(*this).ir_name;
  150. }
  151. // Returns whether this instruction kind defines a type.
  152. auto is_type() const -> InstIsType { return definition_info(*this).is_type; }
  153. // Returns whether this instruction kind is expected to produce a value.
  154. auto value_kind() const -> InstValueKind;
  155. // Returns this instruction kind's category of allowed constants.
  156. auto constant_kind() const -> InstConstantKind {
  157. return definition_info(*this).constant_kind;
  158. }
  159. // Returns whether we need an `InstId` referring to the instruction to
  160. // constant evaluate this instruction. If this is set to `true`, then:
  161. //
  162. // - `Check::TryEvalInst` will not allow this instruction to be directly
  163. // evaluated without an `InstId`.
  164. // - `Check::EvalConstantInst` will be passed an `InstId` for the original
  165. // instruction being evaluated.
  166. //
  167. // This is set to true for instructions whose evaluation either might need a
  168. // location, for example for diagnostics or for newly-created instructions,
  169. // and for instructions whose evaluation needs to inspect the original form of
  170. // its operands.
  171. auto constant_needs_inst_id() const -> InstConstantNeedsInstIdKind {
  172. return definition_info(*this).constant_needs_inst_id;
  173. }
  174. // Returns whether this instruction kind is a code block terminator, such as
  175. // an unconditional branch instruction, or part of the termination sequence,
  176. // such as a conditional branch instruction. The termination sequence of a
  177. // code block appears after all other instructions, and ends with a
  178. // terminator instruction.
  179. auto terminator_kind() const -> TerminatorKind {
  180. return definition_info(*this).terminator_kind;
  181. }
  182. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  183. // conclude `A` == `B`.
  184. auto deduce_through() const -> bool {
  185. return definition_info(*this).deduce_through;
  186. }
  187. // Returns true if this instruction has scoped cleanup associated, typically a
  188. // destructor.
  189. constexpr auto has_cleanup() const -> bool {
  190. return definition_info(*this).has_cleanup;
  191. }
  192. private:
  193. // Returns the DefinitionInfo for the kind.
  194. static auto definition_info(InstKind kind) -> const DefinitionInfo&;
  195. };
  196. #define CARBON_SEM_IR_INST_KIND(Name) \
  197. CARBON_ENUM_CONSTANT_DEFINITION(InstKind, Name)
  198. #include "toolchain/sem_ir/inst_kind.def"
  199. // We expect the instruction kind to fit compactly into 8 bits.
  200. static_assert(sizeof(InstKind) == 1, "Kind objects include padding!");
  201. // A definition of an instruction kind. This is an InstKind value, plus
  202. // ancillary data such as the name to use for the node kind in LLVM IR. These
  203. // are not copyable, and only one instance of this type is expected to exist
  204. // per instruction kind, specifically `TypedInst::Kind`. Use `InstKind`
  205. // instead as a thin wrapper around an instruction kind index.
  206. template <typename TypedNodeIdArg>
  207. class InstKind::Definition : public InstKind {
  208. public:
  209. using TypedNodeId = TypedNodeIdArg;
  210. // Not copyable.
  211. Definition(const Definition&) = delete;
  212. auto operator=(const Definition&) -> Definition& = delete;
  213. // Returns the name to use for this instruction kind in Semantics IR.
  214. constexpr auto ir_name() const -> llvm::StringLiteral {
  215. return info_.ir_name;
  216. }
  217. // Returns whether this instruction kind defines a type.
  218. constexpr auto is_type() const -> InstIsType { return info_.is_type; }
  219. // Returns this instruction kind's category of allowed constants.
  220. constexpr auto constant_kind() const -> InstConstantKind {
  221. return info_.constant_kind;
  222. }
  223. // Returns whether constant evaluation of this instruction needs an InstId.
  224. constexpr auto constant_needs_inst_id() const -> InstConstantNeedsInstIdKind {
  225. return info_.constant_needs_inst_id;
  226. }
  227. // Returns whether this instruction kind is a code block terminator. See
  228. // InstKind::terminator_kind().
  229. constexpr auto terminator_kind() const -> TerminatorKind {
  230. return info_.terminator_kind;
  231. }
  232. // Returns true if the instruction is lowered.
  233. constexpr auto is_lowered() const -> bool { return info_.is_lowered; }
  234. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  235. // conclude `A` == `B`.
  236. constexpr auto deduce_through() const -> bool { return info_.deduce_through; }
  237. // Returns true if this instruction has scoped cleanup associated, typically a
  238. // destructor.
  239. constexpr auto has_cleanup() const -> bool { return info_.has_cleanup; }
  240. private:
  241. friend class InstKind;
  242. constexpr Definition(InstKind kind, InstKind::DefinitionInfo info)
  243. : InstKind(kind), info_(info) {}
  244. InstKind::DefinitionInfo info_;
  245. };
  246. template <typename TypedNodeId>
  247. constexpr auto InstKind::Define(DefinitionInfo info) const
  248. -> Definition<TypedNodeId> {
  249. return Definition<TypedNodeId>(*this, info);
  250. }
  251. } // namespace Carbon::SemIR
  252. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_