inst_kind.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 <optional>
  8. #include "common/enum_base.h"
  9. #include "toolchain/parse/node_ids.h"
  10. namespace Carbon::SemIR {
  11. // Forward-declared to avoid a cycle.
  12. struct TypeId;
  13. // The expression category of an instruction. See /docs/design/values.md for
  14. // details.
  15. enum class ExprCategory : int8_t {
  16. // This instruction does not correspond to an expression, and as such has no
  17. // category.
  18. NotExpr,
  19. // The category of this instruction is not known due to an error.
  20. Error,
  21. // This instruction represents a pattern, not an expression.
  22. Pattern,
  23. // This instruction represents a value expression.
  24. Value,
  25. // This instruction represents a durable reference expression, that denotes an
  26. // object that outlives the current full expression context.
  27. DurableRef,
  28. // This instruction represents an ephemeral reference expression, that denotes
  29. // an object that does not outlive the current full expression context.
  30. EphemeralRef,
  31. // This instruction represents an initializing expression, that describes how
  32. // to initialize an object.
  33. Initializing,
  34. // This instruction represents a syntactic combination of expressions that are
  35. // permitted to have different expression categories. This is used for tuple
  36. // and struct literals, where the subexpressions for different elements can
  37. // have different categories.
  38. Mixed,
  39. Last = Mixed
  40. };
  41. // The computation used to determine the expression category for an instruction,
  42. // given its instruction kind. In the case where the instruction kind always has
  43. // the same category, a value from the `ExprCategory` enumeration is used
  44. // directly instead, so these values should not overlap with the `ExprCategory`
  45. // values.
  46. enum ComputedExprCategory : int8_t {
  47. // The expression category is `Value` if the instruction has a `type_id`
  48. // field, and `NotExpr` otherwise. This is the default, and is used for
  49. // convenience because it does the right thing for most instructions.
  50. ValueIfHasType = -1,
  51. // The expression category is the same as that of the first operand, which
  52. // is an `InstId`.
  53. SameAsFirstOperand = -2,
  54. // The expression category is the same as that of the first operand, which
  55. // is an `InstId`.
  56. SameAsSecondOperand = -3,
  57. // The expression category depends on the operands in some way not covered
  58. // by the above options. The category is determined by custom logic in
  59. // `GetExprCategory`.
  60. DependsOnOperands = -4,
  61. };
  62. // What kind of expression category an instruction kind produces. The expression
  63. // category in general may depend on the operands of the instruction, but we can
  64. // handle most cases based on the instruction kind alone.
  65. class InstExprCategory {
  66. public:
  67. constexpr explicit(false) InstExprCategory(ExprCategory cat)
  68. : kind_(static_cast<int8_t>(cat)) {}
  69. constexpr explicit(false) InstExprCategory(ComputedExprCategory kind)
  70. : kind_(static_cast<int8_t>(kind)) {}
  71. // If this instruction always has the same category, returns that category.
  72. // Otherwise returns nullopt.
  73. constexpr auto TryAsFixedCategory() const -> std::optional<ExprCategory> {
  74. return kind_ >= 0 ? std::optional(static_cast<ExprCategory>(kind_))
  75. : std::nullopt;
  76. }
  77. // If the category of this instruction depends on its operands, returns the
  78. // kind of computation to use to determine the category. Otherwise returns
  79. // nullopt.
  80. constexpr auto TryAsComputedCategory() const
  81. -> std::optional<ComputedExprCategory> {
  82. return kind_ < 0 ? std::optional(static_cast<ComputedExprCategory>(kind_))
  83. : std::nullopt;
  84. }
  85. private:
  86. // A value from either the `ExprCategory` or `ComputedExprCategory`
  87. // enumerations.
  88. int8_t kind_;
  89. };
  90. // Whether an instruction defines a type.
  91. enum class InstIsType : int8_t {
  92. // Always of type `type`, and might define a type constant.
  93. Always,
  94. // Sometimes of type `type`, and might define a type constant.
  95. Maybe,
  96. // Never defines a type constant. Note that such instructions can still have
  97. // type `type`, but are not the canonical definition of any type.
  98. Never,
  99. };
  100. // Whether an instruction can have a constant value, and whether it can be a
  101. // constant inst (i.e. an inst whose canonical ID defines a constant value; see
  102. // constant.h).
  103. //
  104. // This specifies whether an instruction of this kind can have a corresponding
  105. // constant value in the `constant_values()` list, and whether an instruction of
  106. // this kind can be added to the `constants()` list.
  107. enum class InstConstantKind : int8_t {
  108. // This instruction never has a constant value, and is never a constant inst.
  109. // This is also used for instructions that don't produce a value at all and
  110. // aren't used as constants.
  111. Never,
  112. // This instruction is never a constant inst, but can reduce to a
  113. // constant value of a different kind. For example, `UnaryOperatorNot` is
  114. // never a constant inst; if its operand is a concrete constant, its
  115. // constant value will instead be a `BoolLiteral`, and if its operand is not a
  116. // concrete constant, it is non-constant. This is the default.
  117. Indirect,
  118. // This instruction can be a symbolic constant inst, depending on its
  119. // operands, but never a concrete constant inst. For example, a `Call`
  120. // instruction can be a symbolic constant inst but never a concrete constant
  121. // inst. The instruction may have a concrete constant value of a different
  122. // kind.
  123. SymbolicOnly,
  124. // This instruction may be a symbolic constant inst if it has symbolic
  125. // operands, and may be a concrete constant inst if it is a reference
  126. // expression, but it is never a concrete constant if it is a value or
  127. // initializing expression. For example, a `TupleAccess` instruction can be a
  128. // symbolic constant inst when applied to a symbolic constant, and can be a
  129. // concrete reference constant inst when applied to a reference constant.
  130. SymbolicOrReference,
  131. // This instruction is a metaprogramming or template instantiation action that
  132. // generates an instruction. Like `SymbolicOnly`, it may be a symbolic
  133. // constant inst depending on its operands, but never a concrete constant
  134. // inst. The instruction may have a concrete constant value that is a
  135. // generated instruction. Constant evaluation support for types with this
  136. // constant kind is provided automatically, by calling `PerformDelayedAction`.
  137. InstAction,
  138. // This instruction's operands determine whether it has a constant value,
  139. // whether it is a constant inst, and/or whether it results in a compile-time
  140. // error, in ways not expressed by the other InstConstantKinds. For example,
  141. // `ArrayType` is a compile-time constant if its operands are constant and its
  142. // array bound is within a valid range, and `ConstType` is a constant inst if
  143. // its operand is the canonical ID of a constant inst that isn't a
  144. // `ConstType`.
  145. Conditional,
  146. // This instruction is a constant inst if and only if its operands are all the
  147. // canonical IDs of constant insts, it has a constant value if and only if its
  148. // operands all have constant values, and that constant value is the result of
  149. // substituting the operands with their canonical IDs. For example, a
  150. // `TupleValue` has all these properties. Constant evaluation support for
  151. // types with this constant kind is provided automatically.
  152. WheneverPossible,
  153. // The same as `WheneverPossible`, except that the operands are known in
  154. // advance to always have a constant value. For example, `IntValue`.
  155. Always,
  156. // The instruction may be a unique constant, as described below for
  157. // `AlwaysUnique`. Otherwise the instruction is not constant. This is used for
  158. // `VarStorage`, where global variables are `AlwaysUnique` and other variables
  159. // are non-constant.
  160. ConditionalUnique,
  161. // This instruction is itself a unique constant, and its ID is always
  162. // canonical. This is used for declarations whose constant identity is simply
  163. // themselves. The `ConstantId` for this instruction will always be a concrete
  164. // constant whose `InstId` refers directly back to the instruction, rather
  165. // than to a separate instruction in the constants block.
  166. // TODO: Decide if this is the model we want for these cases.
  167. AlwaysUnique,
  168. };
  169. // Whether constant evaluation of an instruction needs the instruction to have
  170. // been created and allocated an InstId, or only needs the instruction operands.
  171. enum class InstConstantNeedsInstIdKind : int8_t {
  172. // This instruction kind doesn't need an InstId to be evaluated.
  173. No,
  174. // This instruction needs an InstId during evaluation, but doesn't need the
  175. // instruction to persist after evaluation.
  176. DuringEvaluation,
  177. // This instruction needs a permanent instruction ID, for example because that
  178. // instruction ID can appear in the constant result of evaluation.
  179. Permanent,
  180. };
  181. // Whether an instruction is a terminator or part of the terminator sequence.
  182. // The instructions in a block appear in the order NotTerminator, then
  183. // TerminatorSequence, then Terminator, which is also the numerical order of
  184. // these values.
  185. enum class TerminatorKind : int8_t {
  186. // This instruction is not a terminator.
  187. NotTerminator,
  188. // This instruction is not itself a terminator, but forms part of a terminator
  189. // sequence.
  190. TerminatorSequence,
  191. // This instruction is a terminator.
  192. Terminator,
  193. };
  194. CARBON_DEFINE_RAW_ENUM_CLASS(InstKind, uint8_t) {
  195. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  196. #include "toolchain/sem_ir/inst_kind.def"
  197. };
  198. class InstKind : public CARBON_ENUM_BASE(InstKind) {
  199. public:
  200. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  201. #include "toolchain/sem_ir/inst_kind.def"
  202. // Returns the `InstKind` for an instruction, for `CARBON_KIND_SWITCH`.
  203. template <typename InstT>
  204. static constexpr auto& For = InstT::Kind;
  205. template <typename TypedNodeId>
  206. class Definition;
  207. // Information about a definition. See associated accessors below for
  208. // comments.
  209. struct DefinitionInfo {
  210. llvm::StringLiteral ir_name;
  211. InstExprCategory expr_category = ComputedExprCategory::ValueIfHasType;
  212. InstIsType is_type = InstIsType::Never;
  213. InstConstantKind constant_kind = InstConstantKind::Indirect;
  214. InstConstantNeedsInstIdKind constant_needs_inst_id =
  215. constant_kind == InstConstantKind::AlwaysUnique
  216. ? InstConstantNeedsInstIdKind::Permanent
  217. : InstConstantNeedsInstIdKind::No;
  218. TerminatorKind terminator_kind = TerminatorKind::NotTerminator;
  219. bool is_lowered = true;
  220. bool deduce_through = false;
  221. bool has_cleanup = false;
  222. };
  223. // Provides a definition for this instruction kind. Should only be called
  224. // once, to construct the kind as part of defining it in `typed_insts.h`.
  225. template <typename TypedNodeId>
  226. constexpr auto Define(DefinitionInfo info) const -> Definition<TypedNodeId>;
  227. using EnumBase::AsInt;
  228. using EnumBase::FromInt;
  229. using EnumBase::Make;
  230. // Returns true if the kind matches any of the provided instructions' kinds.
  231. template <typename... InstT>
  232. constexpr auto IsAnyOf() const -> bool {
  233. return ((*this == InstT::Kind) || ...);
  234. }
  235. // Returns the name to use for this instruction kind in Semantics IR.
  236. auto ir_name() const -> llvm::StringLiteral {
  237. return definition_info(*this).ir_name;
  238. }
  239. // Returns the category of expression represented by this instruction kind.
  240. auto expr_category() const -> InstExprCategory {
  241. return definition_info(*this).expr_category;
  242. }
  243. // Returns whether this instruction kind defines a type.
  244. auto is_type() const -> InstIsType { return definition_info(*this).is_type; }
  245. // Returns whether this instruction kind is expected to produce a typed value.
  246. auto has_type() const -> bool;
  247. // Returns this instruction kind's category of allowed constants.
  248. auto constant_kind() const -> InstConstantKind {
  249. return definition_info(*this).constant_kind;
  250. }
  251. // Returns whether we need an `InstId` referring to the instruction to
  252. // constant evaluate this instruction. If this is set to `true`, then:
  253. //
  254. // - `Check::TryEvalInst` will not allow this instruction to be directly
  255. // evaluated without an `InstId`.
  256. // - `Check::EvalConstantInst` will be passed an `InstId` for the original
  257. // instruction being evaluated.
  258. //
  259. // This is set to true for instructions whose evaluation either might need a
  260. // location, for example for diagnostics or for newly-created instructions,
  261. // and for instructions whose evaluation needs to inspect the original form of
  262. // its operands.
  263. auto constant_needs_inst_id() const -> InstConstantNeedsInstIdKind {
  264. return definition_info(*this).constant_needs_inst_id;
  265. }
  266. // Returns whether this instruction kind is a code block terminator, such as
  267. // an unconditional branch instruction, or part of the termination sequence,
  268. // such as a conditional branch instruction. The termination sequence of a
  269. // code block appears after all other instructions, and ends with a
  270. // terminator instruction.
  271. auto terminator_kind() const -> TerminatorKind {
  272. return definition_info(*this).terminator_kind;
  273. }
  274. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  275. // conclude `A` == `B`.
  276. auto deduce_through() const -> bool {
  277. return definition_info(*this).deduce_through;
  278. }
  279. // Returns true if this instruction has scoped cleanup associated, typically a
  280. // destructor.
  281. constexpr auto has_cleanup() const -> bool {
  282. return definition_info(*this).has_cleanup;
  283. }
  284. private:
  285. // Returns the DefinitionInfo for the kind.
  286. static auto definition_info(InstKind kind) -> const DefinitionInfo&;
  287. };
  288. #define CARBON_SEM_IR_INST_KIND(Name) \
  289. CARBON_ENUM_CONSTANT_DEFINITION(InstKind, Name)
  290. #include "toolchain/sem_ir/inst_kind.def"
  291. // We expect the instruction kind to fit compactly into 8 bits.
  292. static_assert(sizeof(InstKind) == 1, "Kind objects include padding!");
  293. // A definition of an instruction kind. This is an InstKind value, plus
  294. // ancillary data such as the name to use for the node kind in LLVM IR. These
  295. // are not copyable, and only one instance of this type is expected to exist
  296. // per instruction kind, specifically `TypedInst::Kind`. Use `InstKind`
  297. // instead as a thin wrapper around an instruction kind index.
  298. template <typename TypedNodeIdArg>
  299. class InstKind::Definition : public InstKind {
  300. public:
  301. using TypedNodeId = TypedNodeIdArg;
  302. // Not copyable.
  303. Definition(const Definition&) = delete;
  304. auto operator=(const Definition&) -> Definition& = delete;
  305. // Returns the name to use for this instruction kind in Semantics IR.
  306. constexpr auto ir_name() const -> llvm::StringLiteral {
  307. return info_.ir_name;
  308. }
  309. // Returns the category of expression represented by this instruction kind.
  310. constexpr auto expr_category() const -> InstExprCategory {
  311. return info_.expr_category;
  312. }
  313. // Returns whether this instruction kind defines a type.
  314. constexpr auto is_type() const -> InstIsType { return info_.is_type; }
  315. // Returns whether instructions of this kind are always symbolic whenever they
  316. // are types. For convenience, also returns false if the instruction cannot be
  317. // a type, because this is typically used in requires expressions where that
  318. // case is handled by a separate overload.
  319. constexpr auto is_symbolic_when_type() const -> bool {
  320. // Types are values (not references) of type `type`, so if the instruction
  321. // kind is always symbolic when it's a value, then it's always symbolic when
  322. // it's a type.
  323. return is_type() != InstIsType::Never &&
  324. (constant_kind() == InstConstantKind::SymbolicOnly ||
  325. constant_kind() == InstConstantKind::SymbolicOrReference);
  326. }
  327. // Returns this instruction kind's category of allowed constants.
  328. constexpr auto constant_kind() const -> InstConstantKind {
  329. return info_.constant_kind;
  330. }
  331. // Returns whether constant evaluation of this instruction needs an InstId.
  332. constexpr auto constant_needs_inst_id() const -> InstConstantNeedsInstIdKind {
  333. return info_.constant_needs_inst_id;
  334. }
  335. // Returns whether this instruction kind is a code block terminator. See
  336. // InstKind::terminator_kind().
  337. constexpr auto terminator_kind() const -> TerminatorKind {
  338. return info_.terminator_kind;
  339. }
  340. // Returns true if the instruction is lowered.
  341. constexpr auto is_lowered() const -> bool { return info_.is_lowered; }
  342. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  343. // conclude `A` == `B`.
  344. constexpr auto deduce_through() const -> bool { return info_.deduce_through; }
  345. // Returns true if this instruction has scoped cleanup associated, typically a
  346. // destructor.
  347. constexpr auto has_cleanup() const -> bool { return info_.has_cleanup; }
  348. private:
  349. friend class InstKind;
  350. constexpr Definition(InstKind kind, InstKind::DefinitionInfo info)
  351. : InstKind(kind), info_(info) {}
  352. InstKind::DefinitionInfo info_;
  353. };
  354. template <typename TypedNodeId>
  355. constexpr auto InstKind::Define(DefinitionInfo info) const
  356. -> Definition<TypedNodeId> {
  357. return Definition<TypedNodeId>(*this, info);
  358. }
  359. } // namespace Carbon::SemIR
  360. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_