inst_kind.h 13 KB

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