inst_kind.h 13 KB

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