function_context.h 7.9 KB

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