function_context.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/node.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. // Returns a basic block corresponding to the start of the given semantics
  19. // block, and enqueues it for emission.
  20. auto GetBlock(SemIR::NodeBlockId 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::NodeBlockId block_id, llvm::BasicBlock* block)
  25. -> bool;
  26. // Returns a phi node corresponding to the block argument of the given basic
  27. // block.
  28. auto GetBlockArg(SemIR::NodeBlockId block_id, SemIR::TypeId type_id)
  29. -> llvm::PHINode*;
  30. // Returns a local (versus global) value for the given node.
  31. auto GetLocal(SemIR::NodeId node_id) -> llvm::Value* {
  32. // All builtins are types, with the same empty lowered value.
  33. if (node_id.index < SemIR::BuiltinKind::ValidCount) {
  34. return GetTypeAsValue();
  35. }
  36. auto it = locals_.find(node_id);
  37. CARBON_CHECK(it != locals_.end()) << "Missing local: " << node_id;
  38. return it->second;
  39. }
  40. // Returns a local (versus global) value for the given node in loaded state.
  41. // Loads will only be inserted on an as-needed basis.
  42. auto GetLocalLoaded(SemIR::NodeId node_id) -> llvm::Value*;
  43. // Sets the value for the given node.
  44. auto SetLocal(SemIR::NodeId node_id, llvm::Value* value) {
  45. bool added = locals_.insert({node_id, value}).second;
  46. CARBON_CHECK(added) << "Duplicate local insert: " << node_id;
  47. }
  48. // Returns the requested index into val based on whether val is a pointer
  49. // type.
  50. auto GetIndexFromStructOrArray(llvm::Type* llvm_type, llvm::Value* val,
  51. unsigned idx, const llvm::Twine& name)
  52. -> llvm::Value* {
  53. return val->getType()->isPointerTy()
  54. ? builder().CreateStructGEP(llvm_type, val, idx, name)
  55. : builder().CreateExtractValue(val, idx, name);
  56. }
  57. // Gets a callable's function.
  58. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  59. return file_context_->GetFunction(function_id);
  60. }
  61. // Returns a lowered type for the given type_id.
  62. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  63. return file_context_->GetType(type_id);
  64. }
  65. // Returns a lowered value to use for a value of type `type`.
  66. auto GetTypeAsValue() -> llvm::Value* {
  67. return file_context_->GetTypeAsValue();
  68. }
  69. // Create a synthetic block that corresponds to no SemIR::NodeBlockId. Such
  70. // a block should only ever have a single predecessor, and is used when we
  71. // need multiple `llvm::BasicBlock`s to model the linear control flow in a
  72. // single SemIR::File block.
  73. auto CreateSyntheticBlock() -> llvm::BasicBlock*;
  74. // Determine whether block is the most recently created synthetic block.
  75. auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
  76. return synthetic_block_ == block;
  77. }
  78. // After emitting an initializer `init_id`, finishes performing the
  79. // initialization of `dest_id` from that initializer. This is a no-op if the
  80. // initialization was performed in-place, and otherwise performs a store or a
  81. // copy.
  82. auto FinishInitialization(SemIR::TypeId type_id, SemIR::NodeId dest_id,
  83. SemIR::NodeId init_id) -> void;
  84. auto llvm_context() -> llvm::LLVMContext& {
  85. return file_context_->llvm_context();
  86. }
  87. auto llvm_module() -> llvm::Module& { return file_context_->llvm_module(); }
  88. auto builder() -> llvm::IRBuilder<>& { return builder_; }
  89. auto semantics_ir() -> const SemIR::File& {
  90. return file_context_->semantics_ir();
  91. }
  92. private:
  93. // Emits a value copy for type `type_id` from `source_id` to `dest_id`.
  94. // `source_id` must produce a value representation for `type_id`, and
  95. // `dest_id` must be a pointer to a `type_id` object.
  96. auto CopyValue(SemIR::TypeId type_id, SemIR::NodeId source_id,
  97. SemIR::NodeId dest_id) -> void;
  98. // Context for the overall lowering process.
  99. FileContext* file_context_;
  100. // The IR function we're generating.
  101. llvm::Function* function_;
  102. llvm::IRBuilder<> builder_;
  103. // Maps a function's SemIR::File blocks to lowered blocks.
  104. llvm::DenseMap<SemIR::NodeBlockId, llvm::BasicBlock*> blocks_;
  105. // The synthetic block we most recently created. May be null if there is no
  106. // such block.
  107. llvm::BasicBlock* synthetic_block_ = nullptr;
  108. // Maps a function's SemIR::File nodes to lowered values.
  109. // TODO: Handle nested scopes. Right now this is just cleared at the end of
  110. // every block.
  111. llvm::DenseMap<SemIR::NodeId, llvm::Value*> locals_;
  112. };
  113. // Declare handlers for each SemIR::File node.
  114. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  115. auto Handle##Name(FunctionContext& context, SemIR::NodeId node_id, \
  116. SemIR::Node node) \
  117. ->void;
  118. #include "toolchain/sem_ir/node_kind.def"
  119. } // namespace Carbon::Lower
  120. #endif // CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_