inst_kind.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 have a concrete constant value that is a
  187. // generated instruction. Constant evaluation support for types with this
  188. // constant kind is provided automatically, by calling `PerformDelayedAction`.
  189. InstAction,
  190. // This instruction's operands determine whether it has a constant value,
  191. // whether it is a constant inst, and/or whether it results in a compile-time
  192. // error, in ways not expressed by the other InstConstantKinds. For example,
  193. // `ArrayType` is a compile-time constant if its operands are constant and its
  194. // array bound is within a valid range, and `ConstType` is a constant inst if
  195. // its operand is the canonical ID of a constant inst that isn't a
  196. // `ConstType`.
  197. Conditional,
  198. // This instruction is a constant inst if and only if its operands are all the
  199. // canonical IDs of constant insts, it has a constant value if and only if its
  200. // operands all have constant values, and that constant value is the result of
  201. // substituting the operands with their canonical IDs. For example, a
  202. // `TupleValue` has all these properties. Constant evaluation support for
  203. // types with this constant kind is provided automatically.
  204. WheneverPossible,
  205. // The same as `WheneverPossible`, except that the operands are known in
  206. // advance to always have a constant value. For example, `IntValue`.
  207. Always,
  208. // The instruction may be a unique constant, as described below for
  209. // `AlwaysUnique`. Otherwise the instruction is not constant. This is used for
  210. // `VarStorage`, where global variables are `AlwaysUnique` and other variables
  211. // are non-constant.
  212. ConditionalUnique,
  213. // This instruction is itself a unique constant, and its ID is always
  214. // canonical. This is used for declarations whose constant identity is simply
  215. // themselves. The `ConstantId` for this instruction will always be a concrete
  216. // constant whose `InstId` refers directly back to the instruction, rather
  217. // than to a separate instruction in the constants block.
  218. // TODO: Decide if this is the model we want for these cases.
  219. AlwaysUnique,
  220. };
  221. // Whether constant evaluation of an instruction needs the instruction to have
  222. // been created and allocated an InstId, or only needs the instruction operands.
  223. enum class InstConstantNeedsInstIdKind : int8_t {
  224. // This instruction kind doesn't need an InstId to be evaluated.
  225. No,
  226. // This instruction needs an InstId during evaluation, but doesn't need the
  227. // instruction to persist after evaluation.
  228. DuringEvaluation,
  229. // This instruction needs a permanent instruction ID, for example because that
  230. // instruction ID can appear in the constant result of evaluation.
  231. Permanent,
  232. };
  233. // Whether an instruction is a terminator or part of the terminator sequence.
  234. // The instructions in a block appear in the order NotTerminator, then
  235. // TerminatorSequence, then Terminator, which is also the numerical order of
  236. // these values.
  237. enum class TerminatorKind : int8_t {
  238. // This instruction is not a terminator.
  239. NotTerminator,
  240. // This instruction is not itself a terminator, but forms part of a terminator
  241. // sequence.
  242. TerminatorSequence,
  243. // This instruction is a terminator.
  244. Terminator,
  245. };
  246. CARBON_DEFINE_RAW_ENUM_CLASS(InstKind, uint8_t) {
  247. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  248. #include "toolchain/sem_ir/inst_kind.def"
  249. };
  250. class InstKind : public CARBON_ENUM_BASE(InstKind) {
  251. public:
  252. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  253. #include "toolchain/sem_ir/inst_kind.def"
  254. // Returns the `InstKind` for an instruction, for `CARBON_KIND_SWITCH`.
  255. template <typename InstT>
  256. static constexpr auto& For = InstT::Kind;
  257. template <typename TypedNodeId>
  258. class Definition;
  259. // Information about a definition. See associated accessors below for
  260. // comments.
  261. struct DefinitionInfo {
  262. llvm::StringLiteral ir_name;
  263. InstExprCategory expr_category = ComputedExprCategory::ValueIfHasType;
  264. InstIsType is_type = InstIsType::Never;
  265. InstConstantKind constant_kind = InstConstantKind::Indirect;
  266. InstConstantNeedsInstIdKind constant_needs_inst_id =
  267. constant_kind == InstConstantKind::AlwaysUnique
  268. ? InstConstantNeedsInstIdKind::Permanent
  269. : InstConstantNeedsInstIdKind::No;
  270. TerminatorKind terminator_kind = TerminatorKind::NotTerminator;
  271. bool is_lowered = true;
  272. bool deduce_through = false;
  273. bool has_cleanup = false;
  274. // The inst's allowed node kinds, for `IsAllowedNodeKind`.
  275. //
  276. // Do not set these directly. They are set by the `TypedNodeId` template
  277. // parameter of `Define`.
  278. bool internal_allow_all_node_kinds = false;
  279. llvm::ArrayRef<Parse::NodeKind::RawEnumType> internal_allowed_node_kinds;
  280. };
  281. // Provides a definition for this instruction kind. Should only be called
  282. // once, to construct the kind as part of defining it in `typed_insts.h`.
  283. template <typename TypedNodeId>
  284. constexpr auto Define(DefinitionInfo info) const -> Definition<TypedNodeId>;
  285. using EnumBase::AsInt;
  286. using EnumBase::FromInt;
  287. using EnumBase::Make;
  288. // Returns true if the kind matches any of the provided instructions' kinds.
  289. template <typename... InstT>
  290. constexpr auto IsAnyOf() const -> bool {
  291. return ((*this == InstT::Kind) || ...);
  292. }
  293. // Returns the name to use for this instruction kind in Semantics IR.
  294. auto ir_name() const -> llvm::StringLiteral {
  295. return definition_info(*this).ir_name;
  296. }
  297. // Returns the category of expression represented by this instruction kind.
  298. auto expr_category() const -> InstExprCategory {
  299. return definition_info(*this).expr_category;
  300. }
  301. // Returns whether this instruction kind defines a type.
  302. auto is_type() const -> InstIsType { return definition_info(*this).is_type; }
  303. // Returns whether this instruction kind is expected to produce a typed value.
  304. auto has_type() const -> bool;
  305. // Returns this instruction kind's category of allowed constants.
  306. auto constant_kind() const -> InstConstantKind {
  307. return definition_info(*this).constant_kind;
  308. }
  309. // Returns whether we need an `InstId` referring to the instruction to
  310. // constant evaluate this instruction. If this is set to `true`, then:
  311. //
  312. // - `Check::TryEvalInst` will not allow this instruction to be directly
  313. // evaluated without an `InstId`.
  314. // - `Check::EvalConstantInst` will be passed an `InstId` for the original
  315. // instruction being evaluated.
  316. //
  317. // This is set to true for instructions whose evaluation either might need a
  318. // location, for example for diagnostics or for newly-created instructions,
  319. // and for instructions whose evaluation needs to inspect the original form of
  320. // its operands.
  321. auto constant_needs_inst_id() const -> InstConstantNeedsInstIdKind {
  322. return definition_info(*this).constant_needs_inst_id;
  323. }
  324. // Returns whether this instruction kind is a code block terminator, such as
  325. // an unconditional branch instruction, or part of the termination sequence,
  326. // such as a conditional branch instruction. The termination sequence of a
  327. // code block appears after all other instructions, and ends with a
  328. // terminator instruction.
  329. auto terminator_kind() const -> TerminatorKind {
  330. return definition_info(*this).terminator_kind;
  331. }
  332. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  333. // conclude `A` == `B`.
  334. auto deduce_through() const -> bool {
  335. return definition_info(*this).deduce_through;
  336. }
  337. // Returns true if this instruction has scoped cleanup associated, typically a
  338. // destructor.
  339. constexpr auto has_cleanup() const -> bool {
  340. return definition_info(*this).has_cleanup;
  341. }
  342. // Returns true if the passed `NodeKind` is allowed.
  343. auto IsAllowedNodeKind(Parse::NodeKind node_kind) const -> bool;
  344. // Returns true if all `NodeKind`s are allowed.
  345. auto allow_all_node_kinds() const -> bool {
  346. return definition_info(*this).internal_allow_all_node_kinds;
  347. }
  348. // Returns true if no `NodeKind`s are allowed.
  349. auto disallow_all_node_kinds() const -> bool {
  350. const auto& def = definition_info(*this);
  351. return !def.internal_allow_all_node_kinds &&
  352. def.internal_allowed_node_kinds.empty();
  353. }
  354. private:
  355. // Returns the DefinitionInfo for the kind.
  356. static auto definition_info(InstKind kind) -> const DefinitionInfo&;
  357. };
  358. #define CARBON_SEM_IR_INST_KIND(Name) \
  359. CARBON_ENUM_CONSTANT_DEFINITION(InstKind, Name)
  360. #include "toolchain/sem_ir/inst_kind.def"
  361. // We expect the instruction kind to fit compactly into 8 bits.
  362. static_assert(sizeof(InstKind) == 1, "Kind objects include padding!");
  363. // A definition of an instruction kind. This is an InstKind value, plus
  364. // ancillary data such as the name to use for the node kind in LLVM IR. These
  365. // are not copyable, and only one instance of this type is expected to exist
  366. // per instruction kind, specifically `TypedInst::Kind`. Use `InstKind`
  367. // instead as a thin wrapper around an instruction kind index.
  368. template <typename TypedNodeIdArg>
  369. class InstKind::Definition : public InstKind {
  370. public:
  371. using TypedNodeId = TypedNodeIdArg;
  372. // Not copyable.
  373. Definition(const Definition&) = delete;
  374. auto operator=(const Definition&) -> Definition& = delete;
  375. // Returns the name to use for this instruction kind in Semantics IR.
  376. constexpr auto ir_name() const -> llvm::StringLiteral {
  377. return info_.ir_name;
  378. }
  379. // Returns the category of expression represented by this instruction kind.
  380. constexpr auto expr_category() const -> InstExprCategory {
  381. return info_.expr_category;
  382. }
  383. // Returns whether this instruction kind defines a type.
  384. constexpr auto is_type() const -> InstIsType { return info_.is_type; }
  385. // Returns whether instructions of this kind are always symbolic whenever they
  386. // are types. For convenience, also returns false if the instruction cannot be
  387. // a type, because this is typically used in requires expressions where that
  388. // case is handled by a separate overload.
  389. constexpr auto is_symbolic_when_type() const -> bool {
  390. // Types are values (not references) of type `type`, so if the instruction
  391. // kind is always symbolic when it's a value, then it's always symbolic when
  392. // it's a type.
  393. return is_type() != InstIsType::Never &&
  394. (constant_kind() == InstConstantKind::SymbolicOnly ||
  395. constant_kind() == InstConstantKind::SymbolicOrReference);
  396. }
  397. // Returns this instruction kind's category of allowed constants.
  398. constexpr auto constant_kind() const -> InstConstantKind {
  399. return info_.constant_kind;
  400. }
  401. // Returns whether constant evaluation of this instruction needs an InstId.
  402. constexpr auto constant_needs_inst_id() const -> InstConstantNeedsInstIdKind {
  403. return info_.constant_needs_inst_id;
  404. }
  405. // Returns whether this instruction kind is a code block terminator. See
  406. // InstKind::terminator_kind().
  407. constexpr auto terminator_kind() const -> TerminatorKind {
  408. return info_.terminator_kind;
  409. }
  410. // Returns true if the instruction is lowered.
  411. constexpr auto is_lowered() const -> bool { return info_.is_lowered; }
  412. // Returns true if `Instruction(A)` == `Instruction(B)` allows deduction to
  413. // conclude `A` == `B`.
  414. constexpr auto deduce_through() const -> bool { return info_.deduce_through; }
  415. // Returns true if this instruction has scoped cleanup associated, typically a
  416. // destructor.
  417. constexpr auto has_cleanup() const -> bool { return info_.has_cleanup; }
  418. private:
  419. friend class InstKind;
  420. constexpr Definition(InstKind kind, InstKind::DefinitionInfo info)
  421. : InstKind(kind), info_(info) {}
  422. InstKind::DefinitionInfo info_;
  423. };
  424. namespace Internal {
  425. // Storage for `internal_allowed_node_kinds` where there's a list of kinds.
  426. template <Parse::NodeKind::RawEnumType... T>
  427. constexpr std::array<Parse::NodeKind::RawEnumType, sizeof...(T)> Kinds = {T...};
  428. // `NoneNodeId` uses should never have a node associated; it's mainly for
  429. // builtins.
  430. constexpr auto GetAllowedNodeKinds(Parse::NoneNodeId* /*unused*/)
  431. -> llvm::ArrayRef<Parse::NodeKind::RawEnumType> {
  432. return {};
  433. }
  434. // For a regular `NodeId`, returns an array of just its kind.
  435. template <const Parse::NodeKind& Kind>
  436. constexpr auto GetAllowedNodeKinds(Parse::NodeIdForKind<Kind>* /*unused*/)
  437. -> llvm::ArrayRef<Parse::NodeKind::RawEnumType> {
  438. return Kinds<static_cast<Parse::NodeKind::RawEnumType>(Kind)>;
  439. }
  440. // For `NodeIdOneOf`, returns an array of each kind.
  441. template <typename... T>
  442. constexpr auto GetAllowedNodeKinds(Parse::NodeIdOneOf<T...>* /*unused*/)
  443. -> llvm::ArrayRef<Parse::NodeKind::RawEnumType> {
  444. return Kinds<T::Kind...>;
  445. }
  446. } // namespace Internal
  447. template <typename TypedNodeId>
  448. constexpr auto InstKind::Define(DefinitionInfo info) const
  449. -> Definition<TypedNodeId> {
  450. if constexpr (std::same_as<Parse::NodeId, TypedNodeId>) {
  451. info.internal_allow_all_node_kinds = true;
  452. } else {
  453. info.internal_allowed_node_kinds =
  454. Internal::GetAllowedNodeKinds(static_cast<TypedNodeId*>(nullptr));
  455. }
  456. return Definition<TypedNodeId>(*this, info);
  457. }
  458. } // namespace Carbon::SemIR
  459. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_