function_context.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_LOWER_FUNCTION_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_
  6. #include "common/map.h"
  7. #include "llvm/IR/IRBuilder.h"
  8. #include "llvm/IR/LLVMContext.h"
  9. #include "llvm/IR/Module.h"
  10. #include "toolchain/lower/file_context.h"
  11. #include "toolchain/sem_ir/file.h"
  12. namespace Carbon::Lower {
  13. // Context and shared functionality for lowering handlers that produce an
  14. // `llvm::Function` definition.
  15. class FunctionContext {
  16. public:
  17. explicit FunctionContext(FileContext& file_context, llvm::Function* function,
  18. llvm::raw_ostream* vlog_stream);
  19. // Returns a basic block corresponding to the start of the given semantics
  20. // block, and enqueues it for emission.
  21. auto GetBlock(SemIR::InstBlockId block_id) -> llvm::BasicBlock*;
  22. // If we have not yet allocated a `BasicBlock` for this `block_id`, set it to
  23. // `block`, and enqueue `block_id` for emission. Returns whether we set the
  24. // block.
  25. auto TryToReuseBlock(SemIR::InstBlockId block_id, llvm::BasicBlock* block)
  26. -> bool;
  27. // Builds LLVM IR for the sequence of instructions in `block_id`.
  28. auto LowerBlock(SemIR::InstBlockId block_id) -> void;
  29. // Builds LLVM IR for the specified instruction.
  30. auto LowerInst(SemIR::InstId inst_id) -> void;
  31. // Returns a phi node corresponding to the block argument of the given basic
  32. // block.
  33. auto GetBlockArg(SemIR::InstBlockId block_id, SemIR::TypeId type_id)
  34. -> llvm::PHINode*;
  35. // Returns a value for the given instruction.
  36. auto GetValue(SemIR::InstId inst_id) -> llvm::Value* {
  37. // All builtins are types, with the same empty lowered value.
  38. if (inst_id.is_builtin()) {
  39. return GetTypeAsValue();
  40. }
  41. if (auto result = locals_.Lookup(inst_id)) {
  42. return result.value();
  43. }
  44. if (auto result = file_context_->global_variables().Lookup(inst_id)) {
  45. return result.value();
  46. }
  47. return file_context_->GetGlobal(inst_id);
  48. }
  49. // Sets the value for the given instruction.
  50. auto SetLocal(SemIR::InstId inst_id, llvm::Value* value) {
  51. bool added = locals_.Insert(inst_id, value).is_inserted();
  52. CARBON_CHECK(added) << "Duplicate local insert: " << inst_id << " "
  53. << sem_ir().insts().Get(inst_id);
  54. }
  55. // Gets a callable's function.
  56. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  57. return file_context_->GetFunction(function_id);
  58. }
  59. // Returns a lowered type for the given type_id.
  60. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  61. return file_context_->GetType(type_id);
  62. }
  63. // Returns a lowered value to use for a value of type `type`.
  64. auto GetTypeAsValue() -> llvm::Value* {
  65. return file_context_->GetTypeAsValue();
  66. }
  67. // Create a synthetic block that corresponds to no SemIR::InstBlockId. Such
  68. // a block should only ever have a single predecessor, and is used when we
  69. // need multiple `llvm::BasicBlock`s to model the linear control flow in a
  70. // single SemIR::File block.
  71. auto MakeSyntheticBlock() -> llvm::BasicBlock*;
  72. // Determine whether block is the most recently created synthetic block.
  73. auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
  74. return synthetic_block_ == block;
  75. }
  76. // After emitting an initializer `init_id`, finishes performing the
  77. // initialization of `dest_id` from that initializer. This is a no-op if the
  78. // initialization was performed in-place, and otherwise performs a store or a
  79. // copy.
  80. auto FinishInit(SemIR::TypeId type_id, SemIR::InstId dest_id,
  81. SemIR::InstId source_id) -> void;
  82. auto llvm_context() -> llvm::LLVMContext& {
  83. return file_context_->llvm_context();
  84. }
  85. auto llvm_module() -> llvm::Module& { return file_context_->llvm_module(); }
  86. auto builder() -> llvm::IRBuilderBase& { return builder_; }
  87. auto sem_ir() -> const SemIR::File& { return file_context_->sem_ir(); }
  88. private:
  89. // Custom instruction inserter for our IR builder. Automatically names
  90. // instructions.
  91. class Inserter : public llvm::IRBuilderDefaultInserter {
  92. public:
  93. explicit Inserter(const SemIR::InstNamer* inst_namer)
  94. : inst_namer_(inst_namer) {}
  95. // Sets the instruction we are currently emitting.
  96. void SetCurrentInstId(SemIR::InstId inst_id) { inst_id_ = inst_id; }
  97. private:
  98. auto InsertHelper(llvm::Instruction* inst, const llvm::Twine& name,
  99. llvm::BasicBlock::iterator insert_pt) const
  100. -> void override;
  101. // The instruction namer.
  102. const SemIR::InstNamer* inst_namer_;
  103. // The current instruction ID.
  104. SemIR::InstId inst_id_ = SemIR::InstId::Invalid;
  105. };
  106. // Emits a value copy for type `type_id` from `source_id` to `dest_id`.
  107. // `source_id` must produce a value representation for `type_id`, and
  108. // `dest_id` must be a pointer to a `type_id` object.
  109. auto CopyValue(SemIR::TypeId type_id, SemIR::InstId source_id,
  110. SemIR::InstId dest_id) -> void;
  111. // Emits an object representation copy for type `type_id` from `source_id` to
  112. // `dest_id`. `source_id` and `dest_id` must produce pointers to `type_id`
  113. // objects.
  114. auto CopyObject(SemIR::TypeId type_id, SemIR::InstId source_id,
  115. SemIR::InstId dest_id) -> void;
  116. // Context for the overall lowering process.
  117. FileContext* file_context_;
  118. // The IR function we're generating.
  119. llvm::Function* function_;
  120. llvm::IRBuilder<llvm::ConstantFolder, Inserter> builder_;
  121. // The optional vlog stream.
  122. llvm::raw_ostream* vlog_stream_;
  123. // Maps a function's SemIR::File blocks to lowered blocks.
  124. Map<SemIR::InstBlockId, llvm::BasicBlock*> blocks_;
  125. // The synthetic block we most recently created. May be null if there is no
  126. // such block.
  127. llvm::BasicBlock* synthetic_block_ = nullptr;
  128. // Maps a function's SemIR::File instructions to lowered values.
  129. Map<SemIR::InstId, llvm::Value*> locals_;
  130. };
  131. // Provides handlers for instructions that occur in a FunctionContext. Although
  132. // this is declared for all instructions, it should only be defined for
  133. // instructions which are non-constant and not always typed. See
  134. // `FunctionContext::LowerInst` for how this is used.
  135. #define CARBON_SEM_IR_INST_KIND(Name) \
  136. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id, \
  137. SemIR::Name inst) -> void;
  138. #include "toolchain/sem_ir/inst_kind.def"
  139. } // namespace Carbon::Lower
  140. #endif // CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_