inst_kind.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 <concepts>
  7. #include <cstdint>
  8. #include <optional>
  9. #include "common/enum_base.h"
  10. #include "toolchain/parse/node_ids.h"
  11. namespace Carbon::SemIR {
  12. // Forward-declared to avoid a cycle.
  13. struct TypeId;
  14. // The expression category of an instruction. See /docs/design/values.md for
  15. // background.
  16. //
  17. // Several categories are concerned with object initialization. At the SemIR
  18. // level, initialization consists of several phase transitions:
  19. // 1. A _repr-initializing_ expression forms an initializing representation of
  20. // the object's eventual contents.
  21. // 2. An _in-place initializing_ expression commits to writing an object
  22. // representation to memory.
  23. // 3. An _ephemeral entire reference_ expression commits to a particular memory
  24. // location for the object.
  25. // 4. Finally, the owner/lifetime for that object is specified. This need not be
  26. // an expression, so it doesn't correspond to a particular expression
  27. // category. Instead, the inst kinds that perform this role are marked with
  28. // has_cleanup = true.`
  29. //
  30. // If an inst combines more than one of those transitions, its category is
  31. // determined by the last one it performs (which means a non-expression inst may
  32. // perform some of the first three steps). Note that the language-level category
  33. // "initializing expression" is the union of the repr-initializing and in-place
  34. // initializing categories, which exist only in the implementation.
  35. //
  36. // An _initializer_ is an inst in any of those three categories. An inst that
  37. // directly depends on it is said to _consume_ it, and typically an initializer
  38. // must be consumed by exactly one inst.
  39. //
  40. // Thus, the key distinction between an initializing expression and a reference
  41. // expression is that the storage location of a reference expression is fixed as
  42. // soon as it is evaluated, but the storage location of an initializing
  43. // expression is notionally set by the inst that consumes it. "Notionally",
  44. // because that distinction is obscured by two optimizations:
  45. // - The storage location inst is always a direct or indirect argument of the
  46. // in-place initializing inst. The ID of the storage argument inst is fixed
  47. // when the initializing inst is created, and can be found with
  48. // `FindStorageArgForInitializer`, but the inst stored at that ID may be
  49. // overwritten when the consumer is created. This makes the final SemIR appear
  50. // as though the location was set by the initializing inst.
  51. // - When the initializing inst and its consumer are created together, the
  52. // initializing inst is typically created with its storage argument already
  53. // set, rather than creating and then immediately overwriting a placeholder.
  54. //
  55. // TODO: Add an enumerator for ephemeral entire references, when needed.
  56. enum class ExprCategory : int8_t {
  57. // This instruction does not correspond to an expression, and as such has no
  58. // category.
  59. NotExpr,
  60. // The category of this instruction is not known due to an error.
  61. Error,
  62. // This instruction represents a pattern, not an expression.
  63. Pattern,
  64. // This instruction represents a value expression.
  65. Value,
  66. // This instruction represents a repr-initializing expression (see above),
  67. // which initializes an object using the type's initializing representation.
  68. // It must be consumed exactly once unless the type's initializing
  69. // representation is known not to be in-place.
  70. ReprInitializing,
  71. // This instruction represents an in-place initializing expression (see
  72. // above), which initializes an object in-place, regardless of the type's
  73. // initializing representation. It must be consumed exactly once.
  74. InPlaceInitializing,
  75. // This instruction represents a ephemeral non-entire reference, which denotes
  76. // an object that does not outlive the current full expression context.
  77. EphemeralRef,
  78. // This instruction represents a durable reference expression, which denotes
  79. // an object that outlives the current full expression context.
  80. DurableRef,
  81. // This instruction represents a syntactic combination of expressions that are
  82. // permitted to have different expression categories. This is used for tuple
  83. // and struct literals, where the subexpressions for different elements can
  84. // have different categories.
  85. Mixed,
  86. // The category of this instruction is dependent because its form is symbolic.
  87. Dependent,
  88. // This instruction is a `RefTagExpr`, and so its semantics (including its
  89. // expression category) depends on the usage context.
  90. RefTagged,
  91. Last = RefTagged
  92. };
  93. // The computation used to determine the expression category for an instruction,
  94. // given its instruction kind. In the case where the instruction kind always has
  95. // the same category, a value from the `ExprCategory` enumeration is used
  96. // directly instead, so these values should not overlap with the `ExprCategory`
  97. // values.
  98. enum ComputedExprCategory : int8_t {
  99. // The expression category is `Value` if the instruction has a `type_id`
  100. // field, and `NotExpr` otherwise. This is the default, and is used for
  101. // convenience because it does the right thing for most instructions.
  102. ValueIfHasType = -1,
  103. // The expression category is the same as that of the first operand, which
  104. // is an `InstId`.
  105. SameAsFirstOperand = -2,
  106. // The expression category is the same as that of the first operand, which
  107. // is an `InstId`.
  108. SameAsSecondOperand = -3,
  109. // The expression category depends on the operands in some way not covered
  110. // by the above options. The category is determined by custom logic in
  111. // `GetExprCategory`.
  112. DependsOnOperands = -4,
  113. };
  114. // What kind of expression category an instruction kind produces. The expression
  115. // category in general may depend on the operands of the instruction, but we can
  116. // handle most cases based on the instruction kind alone.
  117. class InstExprCategory {
  118. public:
  119. constexpr explicit(false) InstExprCategory(ExprCategory cat)
  120. : kind_(static_cast<int8_t>(cat)) {}
  121. constexpr explicit(false) InstExprCategory(ComputedExprCategory kind)
  122. : kind_(static_cast<int8_t>(kind)) {}
  123. // If this instruction always has the same category, returns that category.
  124. // Otherwise returns nullopt.
  125. constexpr auto TryAsFixedCategory() const -> std::optional<ExprCategory> {
  126. return kind_ >= 0 ? std::optional(static_cast<ExprCategory>(kind_))
  127. : std::nullopt;
  128. }
  129. // If the category of this instruction depends on its operands, returns the
  130. // kind of computation to use to determine the category. Otherwise returns
  131. // nullopt.
  132. constexpr auto TryAsComputedCategory() const
  133. -> std::optional<ComputedExprCategory> {
  134. return kind_ < 0 ? std::optional(static_cast<ComputedExprCategory>(kind_))
  135. : std::nullopt;
  136. }
  137. private:
  138. // A value from either the `ExprCategory` or `ComputedExprCategory`
  139. // enumerations.
  140. int8_t kind_;
  141. };
  142. // Whether an instruction defines a type.
  143. enum class InstIsType : int8_t {
  144. // Always of type `type`, and might define a type constant.
  145. Always,
  146. // Sometimes of type `type`, and might define a type constant.
  147. Maybe,
  148. // Never defines a type constant. Note that such instructions can still have
  149. // type `type`, but are not the canonical definition of any type.
  150. Never,
  151. };
  152. // Whether an instruction can have a constant value, and whether it can be a
  153. // constant inst (i.e. an inst whose canonical ID defines a constant value; see
  154. // constant.h).
  155. //
  156. // This specifies whether an instruction of this kind can have a corresponding
  157. // constant value in the `constant_values()` list, and whether an instruction of
  158. // this kind can be added to the `constants()` list.
  159. enum class InstConstantKind : int8_t {
  160. // This instruction never has a constant value, and is never a constant inst.
  161. // This is also used for instructions that don't produce a value at all and
  162. // aren't used as constants.
  163. Never,
  164. // This instruction is never a constant inst, but can reduce to a
  165. // constant value of a different kind. For example, `UnaryOperatorNot` is
  166. // never a constant inst; if its operand is a concrete constant, its
  167. // constant value will instead be a `BoolLiteral`, and if its operand is not a
  168. // concrete constant, it is non-constant. This is the default.
  169. Indirect,
  170. // This instruction can be a symbolic constant inst, depending on its
  171. // operands, but never a concrete constant inst. For example, a `Call`
  172. // instruction can be a symbolic constant inst but never a concrete constant
  173. // inst. The instruction may have a concrete constant value of a different
  174. // kind.
  175. SymbolicOnly,
  176. // This instruction may be a symbolic constant inst if it has symbolic
  177. // operands, and may be a concrete constant inst if it is a reference
  178. // expression, but it is never a concrete constant if it is a value or
  179. // initializing expression. For example, a `TupleAccess` instruction can be a
  180. // symbolic constant inst when applied to a symbolic constant, and can be a
  181. // concrete reference constant inst when applied to a reference constant.
  182. SymbolicOrReference,
  183. // This instruction is a metaprogramming or template instantiation action that
  184. // generates an instruction. Like `SymbolicOnly`, it may be a symbolic
  185. // constant inst depending on its operands, but never a concrete constant
  186. // inst. The instruction may or may not have a concrete constant value that is
  187. // a generated instruction. Constant evaluation support for types with this
  188. // constant kind is provided automatically, by calling `PerformDelayedAction`.
  189. InstAction,
  190. // Equivalent to InstAction, but this instruction is guaranteed to have a
  191. // constant value.
  192. ConstantInstAction,
  193. // This instruction's operands determine whether it has a constant value,
  194. // whether it is a constant inst, and/or whether it results in a compile-time
  195. // error, in ways not expressed by the other InstConstantKinds. For example,
  196. // `ArrayType` is a compile-time constant if its operands are constant and its
  197. // array bound is within a valid range, and `ConstType` is a constant inst if
  198. // its operand is the canonical ID of a constant inst that isn't a
  199. // `ConstType`.
  200. Conditional,
  201. // This instruction is a constant inst if and only if its operands are all the
  202. // canonical IDs of constant insts, it has a constant value if and only if its
  203. // operands all have constant values, and that constant value is the result of
  204. // substituting the operands with their canonical IDs. For example, a
  205. // `TupleValue` has all these properties. Constant evaluation support for
  206. // types with this constant kind is provided automatically.
  207. WheneverPossible,
  208. // The same as `WheneverPossible`, except that the operands are known in
  209. // advance to always have a constant value. For example, `IntValue`.
  210. Always,
  211. // The instruction may be a unique constant, as described below for
  212. // `AlwaysUnique`. Otherwise the instruction is not constant. This is used for
  213. // `VarStorage`, where global variables are `AlwaysUnique` and other variables
  214. // are non-constant.
  215. ConditionalUnique,
  216. // This instruction is itself a unique constant, and its ID is always
  217. // canonical. This is used for declarations whose constant identity is simply
  218. // themselves. The `ConstantId` for this instruction will always be a concrete
  219. // constant whose `InstId` refers directly back to the instruction, rather
  220. // than to a separate instruction in the constants block.
  221. // TODO: Decide if this is the model we want for these cases.
  222. AlwaysUnique,
  223. };
  224. // Whether constant evaluation of an instruction needs the instruction to have
  225. // been created and allocated an InstId, or only needs the instruction operands.
  226. enum class InstConstantNeedsInstIdKind : int8_t {
  227. // This instruction kind doesn't need an InstId to be evaluated.
  228. No,
  229. // This instruction needs an InstId during evaluation, but doesn't need the
  230. // instruction to persist after evaluation.
  231. DuringEvaluation,
  232. // This instruction needs a permanent instruction ID, for example because that
  233. // instruction ID can appear in the constant result of evaluation.
  234. Permanent,
  235. };
  236. // Whether an instruction is a terminator or part of the terminator sequence.
  237. // The instructions in a block appear in the order NotTerminator, then
  238. // TerminatorSequence, then Terminator, which is also the numerical order of
  239. // these values.
  240. enum class TerminatorKind : int8_t {
  241. // This instruction is not a terminator.
  242. NotTerminator,
  243. // This instruction is not itself a terminator, but forms part of a terminator
  244. // sequence.
  245. TerminatorSequence,
  246. // This instruction is a terminator.
  247. Terminator,
  248. };
  249. CARBON_DEFINE_RAW_ENUM_CLASS(InstKind, uint8_t) {
  250. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  251. #include "toolchain/sem_ir/inst_kind.def"
  252. };
  253. class InstKind : public CARBON_ENUM_BASE(InstKind) {
  254. public:
  255. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  256. #include "toolchain/sem_ir/inst_kind.def"
  257. // Returns the `InstKind` for an instruction, for `CARBON_KIND_SWITCH`.
  258. template <typename InstT>
  259. static constexpr auto& For = InstT::Kind;
  260. template <typename TypedNodeId>
  261. class Definition;
  262. // Information about a definition. See associated accessors below for
  263. // comments.
  264. struct DefinitionInfo {
  265. llvm::StringLiteral ir_name;
  266. InstExprCategory expr_category = ComputedExprCategory::ValueIfHasType;
  267. InstIsType is_type = InstIsType::Never;
  268. InstConstantKind constant_kind = InstConstantKind::Indirect;
  269. InstConstantNeedsInstIdKind constant_needs_inst_id =
  270. constant_kind == InstConstantKind::AlwaysUnique
  271. ? InstConstantNeedsInstIdKind::Permanent
  272. : InstConstantNeedsInstIdKind::No;
  273. TerminatorKind terminator_kind = TerminatorKind::NotTerminator;
  274. bool is_lowered = true;
  275. bool deduce_through = false;
  276. bool has_cleanup = false;
  277. // The inst's allowed node kinds, for `IsAllowedNodeKind`.
  278. //
  279. // Do not set these directly. They are set by the `TypedNodeId` template
  280. // parameter of `Define`.
  281. bool internal_allow_all_node_kinds = false;
  282. llvm::ArrayRef<Parse::NodeKind::RawEnumType> internal_allowed_node_kinds;
  283. };
  284. // Provides a definition for this instruction kind. Should only be called
  285. // once, to construct the kind as part of defining it in `typed_insts.h`.
  286. template <typename TypedNodeId>
  287. constexpr auto Define(DefinitionInfo info) const -> Definition<TypedNodeId>;
  288. using EnumBase::AsInt;
  289. using EnumBase::FromInt;
  290. using EnumBase::Make;
  291. // Returns true if the kind matches any of the provided instructions' kinds.
  292. template <typename... InstT>
  293. constexpr auto IsAnyOf() const -> bool {
  294. return ((*this == InstT::Kind) || ...);
  295. }
  296. // Returns the name to use for this instruction kind in Semantics IR.
  297. auto ir_name() const -> llvm::StringLiteral {
  298. return definition_info(*this).ir_name;
  299. }
  300. // Returns the category of expression represented by this instruction kind.
  301. auto expr_category() const -> InstExprCategory {
  302. return definition_info(*this).expr_category;
  303. }
  304. // Returns whether this instruction kind defines a type.
  305. auto is_type() const -> InstIsType { return definition_info(*this).is_type; }
  306. // Returns whether this instruction kind is expected to produce a typed value.
  307. auto has_type() const -> bool;
  308. // Returns this instruction kind's category of allowed constants.
  309. auto constant_kind() const -> InstConstantKind {
  310. return definition_info(*this).constant_kind;
  311. }
  312. // Returns whether we need an `InstId` referring to the instruction to
  313. // constant evaluate this instruction. If this is set to `true`, then:
  314. //
  315. // - `Check::TryEvalInst` will not allow this instruction to be directly
  316. // evaluated without an `InstId`.
  317. // - `Check::EvalConstantInst` will be passed an `InstId` for the original
  318. // instruction being evaluated.
  319. //
  320. // This is set to true for instructions whose evaluation either might need a
  321. // location, for example for diagnostics or for newly-created instructions,
  322. // and for instructions whose evaluation needs to inspect the original form of
  323. // its operands.
  324. auto constant_needs_inst_id() const -> InstConstantNeedsInstIdKind {
  325. return definition_info(*this).constant_needs_inst_id;
  326. }
  327. // Returns whether this instruction kind is a code block terminator, such as
  328. // an unconditional branch instruction, or part of the termination sequence,
  329. // such as a conditional branch instruction. The termination sequence of a
  330. // code block appears after all other instructions, and ends with a
  331. // terminator instruction.
  332. auto terminator_kind() const -> TerminatorKind {
  333. return definition_info(*this).terminator_kind;
  334. }
  335. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  336. // conclude `A` == `B`.
  337. auto deduce_through() const -> bool {
  338. return definition_info(*this).deduce_through;
  339. }
  340. // Returns true if this instruction has scoped cleanup associated, typically a
  341. // destructor.
  342. constexpr auto has_cleanup() const -> bool {
  343. return definition_info(*this).has_cleanup;
  344. }
  345. // Returns true if the passed `NodeKind` is allowed.
  346. auto IsAllowedNodeKind(Parse::NodeKind node_kind) const -> bool;
  347. // Returns true if all `NodeKind`s are allowed.
  348. auto allow_all_node_kinds() const -> bool {
  349. return definition_info(*this).internal_allow_all_node_kinds;
  350. }
  351. // Returns true if no `NodeKind`s are allowed.
  352. auto disallow_all_node_kinds() const -> bool {
  353. const auto& def = definition_info(*this);
  354. return !def.internal_allow_all_node_kinds &&
  355. def.internal_allowed_node_kinds.empty();
  356. }
  357. private:
  358. // Returns the DefinitionInfo for the kind.
  359. static auto definition_info(InstKind kind) -> const DefinitionInfo&;
  360. };
  361. #define CARBON_SEM_IR_INST_KIND(Name) \
  362. CARBON_ENUM_CONSTANT_DEFINITION(InstKind, Name)
  363. #include "toolchain/sem_ir/inst_kind.def"
  364. // We expect the instruction kind to fit compactly into 8 bits.
  365. static_assert(sizeof(InstKind) == 1, "Kind objects include padding!");
  366. // A definition of an instruction kind. This is an InstKind value, plus
  367. // ancillary data such as the name to use for the node kind in LLVM IR. These
  368. // are not copyable, and only one instance of this type is expected to exist
  369. // per instruction kind, specifically `TypedInst::Kind`. Use `InstKind`
  370. // instead as a thin wrapper around an instruction kind index.
  371. template <typename TypedNodeIdArg>
  372. class InstKind::Definition : public InstKind {
  373. public:
  374. using TypedNodeId = TypedNodeIdArg;
  375. // Not copyable.
  376. Definition(const Definition&) = delete;
  377. auto operator=(const Definition&) -> Definition& = delete;
  378. // Returns the name to use for this instruction kind in Semantics IR.
  379. constexpr auto ir_name() const -> llvm::StringLiteral {
  380. return info_.ir_name;
  381. }
  382. // Returns the category of expression represented by this instruction kind.
  383. constexpr auto expr_category() const -> InstExprCategory {
  384. return info_.expr_category;
  385. }
  386. // Returns whether this instruction kind defines a type.
  387. constexpr auto is_type() const -> InstIsType { return info_.is_type; }
  388. // Returns whether instructions of this kind are always symbolic whenever they
  389. // are types. For convenience, also returns false if the instruction cannot be
  390. // a type, because this is typically used in requires expressions where that
  391. // case is handled by a separate overload.
  392. constexpr auto is_symbolic_when_type() const -> bool {
  393. // Types are values (not references) of type `type`, so if the instruction
  394. // kind is always symbolic when it's a value, then it's always symbolic when
  395. // it's a type.
  396. return is_type() != InstIsType::Never &&
  397. (constant_kind() == InstConstantKind::SymbolicOnly ||
  398. constant_kind() == InstConstantKind::SymbolicOrReference);
  399. }
  400. // Returns this instruction kind's category of allowed constants.
  401. constexpr auto constant_kind() const -> InstConstantKind {
  402. return info_.constant_kind;
  403. }
  404. // Returns whether constant evaluation of this instruction needs an InstId.
  405. constexpr auto constant_needs_inst_id() const -> InstConstantNeedsInstIdKind {
  406. return info_.constant_needs_inst_id;
  407. }
  408. // Returns whether this instruction kind is a code block terminator. See
  409. // InstKind::terminator_kind().
  410. constexpr auto terminator_kind() const -> TerminatorKind {
  411. return info_.terminator_kind;
  412. }
  413. // Returns true if the instruction is lowered.
  414. constexpr auto is_lowered() const -> bool { return info_.is_lowered; }
  415. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  416. // conclude `A` == `B`.
  417. constexpr auto deduce_through() const -> bool { return info_.deduce_through; }
  418. // Returns true if this instruction has scoped cleanup associated, typically a
  419. // destructor.
  420. constexpr auto has_cleanup() const -> bool { return info_.has_cleanup; }
  421. private:
  422. friend class InstKind;
  423. constexpr Definition(InstKind kind, InstKind::DefinitionInfo info)
  424. : InstKind(kind), info_(info) {}
  425. InstKind::DefinitionInfo info_;
  426. };
  427. namespace Internal {
  428. // Storage for `internal_allowed_node_kinds` where there's a list of kinds.
  429. template <Parse::NodeKind::RawEnumType... T>
  430. constexpr std::array<Parse::NodeKind::RawEnumType, sizeof...(T)> Kinds = {T...};
  431. // `NoneNodeId` uses should never have a node associated; it's mainly for
  432. // builtins.
  433. constexpr auto GetAllowedNodeKinds(Parse::NoneNodeId* /*unused*/)
  434. -> llvm::ArrayRef<Parse::NodeKind::RawEnumType> {
  435. return {};
  436. }
  437. // For a regular `NodeId`, returns an array of just its kind.
  438. template <const Parse::NodeKind& Kind>
  439. constexpr auto GetAllowedNodeKinds(Parse::NodeIdForKind<Kind>* /*unused*/)
  440. -> llvm::ArrayRef<Parse::NodeKind::RawEnumType> {
  441. return Kinds<static_cast<Parse::NodeKind::RawEnumType>(Kind)>;
  442. }
  443. // For `NodeIdOneOf`, returns an array of each kind.
  444. template <typename... T>
  445. constexpr auto GetAllowedNodeKinds(Parse::NodeIdOneOf<T...>* /*unused*/)
  446. -> llvm::ArrayRef<Parse::NodeKind::RawEnumType> {
  447. return Kinds<T::Kind...>;
  448. }
  449. } // namespace Internal
  450. template <typename TypedNodeId>
  451. constexpr auto InstKind::Define(DefinitionInfo info) const
  452. -> Definition<TypedNodeId> {
  453. if constexpr (std::same_as<Parse::NodeId, TypedNodeId>) {
  454. info.internal_allow_all_node_kinds = true;
  455. } else {
  456. info.internal_allowed_node_kinds =
  457. Internal::GetAllowedNodeKinds(static_cast<TypedNodeId*>(nullptr));
  458. }
  459. return Definition<TypedNodeId>(*this, info);
  460. }
  461. } // namespace Carbon::SemIR
  462. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_