inst_kind.h 17 KB

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