function_context.h 5.2 KB

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