inst_kind.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_
  6. #include <cstdint>
  7. #include "common/enum_base.h"
  8. #include "llvm/ADT/FoldingSet.h"
  9. namespace Carbon::SemIR {
  10. // Whether an instruction produces or represents a value, and if so, what kind
  11. // of value.
  12. enum class InstValueKind : int8_t {
  13. // This instruction doesn't produce a value, and shouldn't be referenced by
  14. // other instructions.
  15. None,
  16. // This instruction represents an expression or expression-like construct that
  17. // produces a value of the type indicated by its `type_id` field.
  18. Typed,
  19. };
  20. // Whether an instruction can be used to define a constant value. This specifies
  21. // whether the instruction can be added to the `constants()` list. Note that
  22. // even instructions that cannot define a constant value can still have an
  23. // associated `constant_value()`, but the constant value will be a different
  24. // kind of instruction.
  25. enum class InstConstantKind : int8_t {
  26. // This instruction never defines a constant value. For example,
  27. // `UnaryOperatorNot` never defines a constant value; if its operand is a
  28. // template constant, its constant value will instead be a `BoolLiteral`. This
  29. // is also used for instructions that don't produce a value at all.
  30. Never,
  31. // This instruction may be a symbolic constant, depending on its operands, but
  32. // is never a template constant. For example, a `Call` instruction can be a
  33. // symbolic constant but never a template constant.
  34. SymbolicOnly,
  35. // This instruction can define a symbolic or template constant, but might not
  36. // have a constant value, depending on its operands. For example, a
  37. // `TupleValue` can define a constant if its operands are constants.
  38. Conditional,
  39. // This instruction always has a constant value of the same kind. For example,
  40. // `IntLiteral`.
  41. Always,
  42. };
  43. // Whether an instruction is a terminator or part of the terminator sequence.
  44. // The instructions in a block appear in the order NotTerminator, then
  45. // TerminatorSequence, then Terminator, which is also the numerical order of
  46. // these values.
  47. enum class TerminatorKind : int8_t {
  48. // This instruction is not a terminator.
  49. NotTerminator,
  50. // This instruction is not itself a terminator, but forms part of a terminator
  51. // sequence.
  52. TerminatorSequence,
  53. // This instruction is a terminator.
  54. Terminator,
  55. };
  56. CARBON_DEFINE_RAW_ENUM_CLASS(InstKind, uint8_t) {
  57. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  58. #include "toolchain/sem_ir/inst_kind.def"
  59. };
  60. class InstKind : public CARBON_ENUM_BASE(InstKind) {
  61. public:
  62. #define CARBON_SEM_IR_INST_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  63. #include "toolchain/sem_ir/inst_kind.def"
  64. template <typename TypedNodeId>
  65. class Definition;
  66. // Provides a definition for this instruction kind. Should only be called
  67. // once, to construct the kind as part of defining it in `typed_insts.h`.
  68. template <typename TypedNodeId>
  69. constexpr auto Define(
  70. llvm::StringLiteral ir_name,
  71. TerminatorKind terminator_kind = TerminatorKind::NotTerminator) const
  72. -> Definition<TypedNodeId>;
  73. using EnumBase::AsInt;
  74. using EnumBase::Make;
  75. // Returns the name to use for this instruction kind in Semantics IR.
  76. auto ir_name() const -> llvm::StringLiteral;
  77. // Returns whether this kind of instruction is expected to produce a value.
  78. auto value_kind() const -> InstValueKind;
  79. // Returns whether this kind of instruction is able to define a constant.
  80. auto constant_kind() const -> InstConstantKind;
  81. // Returns whether this instruction kind is a code block terminator, such as
  82. // an unconditional branch instruction, or part of the termination sequence,
  83. // such as a conditional branch instruction. The termination sequence of a
  84. // code block appears after all other instructions, and ends with a
  85. // terminator instruction.
  86. auto terminator_kind() const -> TerminatorKind;
  87. // Compute a fingerprint for this instruction kind, allowing its use as part
  88. // of the key in a `FoldingSet`.
  89. void Profile(llvm::FoldingSetNodeID& id) { id.AddInteger(AsInt()); }
  90. };
  91. #define CARBON_SEM_IR_INST_KIND(Name) \
  92. CARBON_ENUM_CONSTANT_DEFINITION(InstKind, Name)
  93. #include "toolchain/sem_ir/inst_kind.def"
  94. // We expect the instruction kind to fit compactly into 8 bits.
  95. static_assert(sizeof(InstKind) == 1, "Kind objects include padding!");
  96. // A definition of an instruction kind. This is an InstKind value, plus
  97. // ancillary data such as the name to use for the node kind in LLVM IR. These
  98. // are not copyable, and only one instance of this type is expected to exist per
  99. // instruction kind, specifically `TypedInst::Kind`. Use `InstKind` instead as a
  100. // thin wrapper around an instruction kind index.
  101. template <typename TypedNodeIdArg>
  102. class InstKind::Definition : public InstKind {
  103. public:
  104. using TypedNodeId = TypedNodeIdArg;
  105. // Not copyable.
  106. Definition(const Definition&) = delete;
  107. auto operator=(const Definition&) -> Definition& = delete;
  108. // Returns the name to use for this instruction kind in Semantics IR.
  109. constexpr auto ir_name() const -> llvm::StringLiteral { return ir_name_; }
  110. // Returns whether this instruction kind is a code block terminator. See
  111. // InstKind::terminator_kind().
  112. constexpr auto terminator_kind() const -> TerminatorKind {
  113. return terminator_kind_;
  114. }
  115. private:
  116. friend class InstKind;
  117. constexpr Definition(InstKind kind, llvm::StringLiteral ir_name,
  118. TerminatorKind terminator_kind)
  119. : InstKind(kind), ir_name_(ir_name), terminator_kind_(terminator_kind) {}
  120. llvm::StringLiteral ir_name_;
  121. TerminatorKind terminator_kind_;
  122. };
  123. template <typename TypedNodeId>
  124. constexpr auto InstKind::Define(llvm::StringLiteral ir_name,
  125. TerminatorKind terminator_kind) const
  126. -> Definition<TypedNodeId> {
  127. return Definition<TypedNodeId>(*this, ir_name, terminator_kind);
  128. }
  129. } // namespace Carbon::SemIR
  130. #endif // CARBON_TOOLCHAIN_SEM_IR_INST_KIND_H_