function_context.h 6.3 KB

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