function_context.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // Returns a phi node corresponding to the block argument of the given basic
  29. // block.
  30. auto GetBlockArg(SemIR::InstBlockId block_id, SemIR::TypeId type_id)
  31. -> llvm::PHINode*;
  32. // Returns a value for the given instruction.
  33. auto GetValue(SemIR::InstId inst_id) -> llvm::Value* {
  34. // All builtins are types, with the same empty lowered value.
  35. if (inst_id.is_builtin()) {
  36. return GetTypeAsValue();
  37. }
  38. auto it = locals_.find(inst_id);
  39. if (it != locals_.end()) {
  40. return it->second;
  41. }
  42. return file_context_->GetGlobal(inst_id);
  43. }
  44. // Sets the value for the given instruction.
  45. auto SetLocal(SemIR::InstId inst_id, llvm::Value* value) {
  46. bool added = locals_.insert({inst_id, value}).second;
  47. CARBON_CHECK(added) << "Duplicate local insert: " << inst_id << " "
  48. << sem_ir().insts().Get(inst_id);
  49. }
  50. // Gets a callable's function.
  51. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  52. return file_context_->GetFunction(function_id);
  53. }
  54. // Returns a lowered type for the given type_id.
  55. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  56. return file_context_->GetType(type_id);
  57. }
  58. // Returns a lowered value to use for a value of type `type`.
  59. auto GetTypeAsValue() -> llvm::Value* {
  60. return file_context_->GetTypeAsValue();
  61. }
  62. // Create a synthetic block that corresponds to no SemIR::InstBlockId. Such
  63. // a block should only ever have a single predecessor, and is used when we
  64. // need multiple `llvm::BasicBlock`s to model the linear control flow in a
  65. // single SemIR::File block.
  66. auto MakeSyntheticBlock() -> llvm::BasicBlock*;
  67. // Determine whether block is the most recently created synthetic block.
  68. auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
  69. return synthetic_block_ == block;
  70. }
  71. // After emitting an initializer `init_id`, finishes performing the
  72. // initialization of `dest_id` from that initializer. This is a no-op if the
  73. // initialization was performed in-place, and otherwise performs a store or a
  74. // copy.
  75. auto FinishInit(SemIR::TypeId type_id, SemIR::InstId dest_id,
  76. SemIR::InstId source_id) -> void;
  77. auto llvm_context() -> llvm::LLVMContext& {
  78. return file_context_->llvm_context();
  79. }
  80. auto llvm_module() -> llvm::Module& { return file_context_->llvm_module(); }
  81. auto builder() -> llvm::IRBuilder<>& { return builder_; }
  82. auto sem_ir() -> const SemIR::File& { return file_context_->sem_ir(); }
  83. private:
  84. // Emits a value copy for type `type_id` from `source_id` to `dest_id`.
  85. // `source_id` must produce a value representation for `type_id`, and
  86. // `dest_id` must be a pointer to a `type_id` object.
  87. auto CopyValue(SemIR::TypeId type_id, SemIR::InstId source_id,
  88. SemIR::InstId dest_id) -> void;
  89. // Context for the overall lowering process.
  90. FileContext* file_context_;
  91. // The IR function we're generating.
  92. llvm::Function* function_;
  93. llvm::IRBuilder<> builder_;
  94. // The optional vlog stream.
  95. llvm::raw_ostream* vlog_stream_;
  96. // Maps a function's SemIR::File blocks to lowered blocks.
  97. llvm::DenseMap<SemIR::InstBlockId, llvm::BasicBlock*> blocks_;
  98. // The synthetic block we most recently created. May be null if there is no
  99. // such block.
  100. llvm::BasicBlock* synthetic_block_ = nullptr;
  101. // Maps a function's SemIR::File instructions to lowered values.
  102. llvm::DenseMap<SemIR::InstId, llvm::Value*> locals_;
  103. };
  104. // Declare handlers for each SemIR::File instruction.
  105. #define CARBON_SEM_IR_INST_KIND(Name) \
  106. auto Handle##Name(FunctionContext& context, SemIR::InstId inst_id, \
  107. SemIR::Name inst) -> void;
  108. #include "toolchain/sem_ir/inst_kind.def"
  109. } // namespace Carbon::Lower
  110. #endif // CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_