function_context.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 local (versus global) value for the given instruction.
  34. auto GetLocal(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. CARBON_CHECK(it != locals_.end())
  41. << "Missing local: " << inst_id << " " << sem_ir().insts().Get(inst_id);
  42. return it->second;
  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. // Returns a value for the given instruction, which might not be local.
  51. auto GetLocalOrGlobal(SemIR::InstId inst_id) -> llvm::Value*;
  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 CreateSyntheticBlock() -> 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 FinishInitialization(SemIR::TypeId type_id, SemIR::InstId dest_id,
  78. SemIR::InstId init_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::IRBuilder<>& { return builder_; }
  84. auto sem_ir() -> const SemIR::File& { return file_context_->sem_ir(); }
  85. private:
  86. // Emits a value copy for type `type_id` from `source_id` to `dest_id`.
  87. // `source_id` must produce a value representation for `type_id`, and
  88. // `dest_id` must be a pointer to a `type_id` object.
  89. auto CopyValue(SemIR::TypeId type_id, SemIR::InstId source_id,
  90. SemIR::InstId dest_id) -> void;
  91. // Context for the overall lowering process.
  92. FileContext* file_context_;
  93. // The IR function we're generating.
  94. llvm::Function* function_;
  95. llvm::IRBuilder<> builder_;
  96. // The optional vlog stream.
  97. llvm::raw_ostream* vlog_stream_;
  98. // Maps a function's SemIR::File blocks to lowered blocks.
  99. llvm::DenseMap<SemIR::InstBlockId, llvm::BasicBlock*> blocks_;
  100. // The synthetic block we most recently created. May be null if there is no
  101. // such block.
  102. llvm::BasicBlock* synthetic_block_ = nullptr;
  103. // Maps a function's SemIR::File instructions to lowered values.
  104. // TODO: Handle nested scopes. Right now this is just cleared at the end of
  105. // every block.
  106. llvm::DenseMap<SemIR::InstId, llvm::Value*> locals_;
  107. };
  108. // Declare handlers for each SemIR::File instruction.
  109. #define CARBON_SEM_IR_INST_KIND(Name) \
  110. auto Handle##Name(FunctionContext& context, SemIR::InstId inst_id, \
  111. SemIR::Name inst) \
  112. ->void;
  113. #include "toolchain/sem_ir/inst_kind.def"
  114. } // namespace Carbon::Lower
  115. #endif // CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_