lowering_function_context.h 5.1 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_LOWERING_LOWERING_FUNCTION_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWERING_LOWERING_FUNCTION_CONTEXT_H_
  6. #include <optional>
  7. #include "llvm/IR/IRBuilder.h"
  8. #include "llvm/IR/LLVMContext.h"
  9. #include "llvm/IR/Module.h"
  10. #include "toolchain/lowering/lowering_context.h"
  11. #include "toolchain/semantics/semantics_ir.h"
  12. #include "toolchain/semantics/semantics_node.h"
  13. namespace Carbon {
  14. // Context and shared functionality for lowering handlers that produce an
  15. // `llvm::Function` definition.
  16. class LoweringFunctionContext {
  17. public:
  18. explicit LoweringFunctionContext(LoweringContext& lowering_context,
  19. llvm::Function* function);
  20. // Returns a basic block corresponding to the start of the given semantics
  21. // block, and enqueues it for emission.
  22. auto GetBlock(SemIR::NodeBlockId block_id) -> llvm::BasicBlock*;
  23. // If we have not yet allocated a `BasicBlock` for this `block_id`, set it to
  24. // `block`, and enqueue `block_id` for emission. Returns whether we set the
  25. // block.
  26. auto TryToReuseBlock(SemIR::NodeBlockId block_id, llvm::BasicBlock* block)
  27. -> bool;
  28. // Returns a phi node corresponding to the block argument of the given basic
  29. // block.
  30. auto GetBlockArg(SemIR::NodeBlockId block_id, SemIR::TypeId type_id)
  31. -> llvm::PHINode*;
  32. // Returns a local (versus global) value for the given node.
  33. auto GetLocal(SemIR::NodeId node_id) -> llvm::Value* {
  34. // All builtins are types, with the same empty lowered value.
  35. if (node_id.index < SemIR::BuiltinKind::ValidCount) {
  36. return GetTypeAsValue();
  37. }
  38. auto it = locals_.find(node_id);
  39. CARBON_CHECK(it != locals_.end()) << "Missing local: " << node_id;
  40. return it->second;
  41. }
  42. // Returns a local (versus global) value for the given node in loaded state.
  43. // Loads will only be inserted on an as-needed basis.
  44. auto GetLocalLoaded(SemIR::NodeId node_id) -> llvm::Value*;
  45. // Sets the value for the given node.
  46. auto SetLocal(SemIR::NodeId node_id, llvm::Value* value) {
  47. bool added = locals_.insert({node_id, value}).second;
  48. CARBON_CHECK(added) << "Duplicate local insert: " << node_id;
  49. }
  50. // Returns the requested index into val based on whether val is a pointer
  51. // type.
  52. auto GetIndexFromStructOrArray(llvm::Type* llvm_type, llvm::Value* val,
  53. unsigned idx, const llvm::Twine& name)
  54. -> llvm::Value* {
  55. return val->getType()->isPointerTy()
  56. ? builder().CreateStructGEP(llvm_type, val, idx, name)
  57. : builder().CreateExtractValue(val, idx, name);
  58. }
  59. // Gets a callable's function.
  60. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  61. return lowering_context_->GetFunction(function_id);
  62. }
  63. // Returns a lowered type for the given type_id.
  64. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  65. return lowering_context_->GetType(type_id);
  66. }
  67. // Returns a lowered value to use for a value of type `type`.
  68. auto GetTypeAsValue() -> llvm::Value* {
  69. return lowering_context_->GetTypeAsValue();
  70. }
  71. // Create a synthetic block that corresponds to no SemIR::NodeBlockId. Such
  72. // a block should only ever have a single predecessor, and is used when we
  73. // need multiple `llvm::BasicBlock`s to model the linear control flow in a
  74. // single SemIR::File block.
  75. auto CreateSyntheticBlock() -> llvm::BasicBlock*;
  76. // Determine whether block is the most recently created synthetic block.
  77. auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
  78. return synthetic_block_ == block;
  79. }
  80. auto llvm_context() -> llvm::LLVMContext& {
  81. return lowering_context_->llvm_context();
  82. }
  83. auto llvm_module() -> llvm::Module& {
  84. return lowering_context_->llvm_module();
  85. }
  86. auto builder() -> llvm::IRBuilder<>& { return builder_; }
  87. auto semantics_ir() -> const SemIR::File& {
  88. return lowering_context_->semantics_ir();
  89. }
  90. private:
  91. // Context for the overall lowering process.
  92. LoweringContext* lowering_context_;
  93. // The IR function we're generating.
  94. llvm::Function* function_;
  95. llvm::IRBuilder<> builder_;
  96. // Maps a function's SemIR::File blocks to lowered blocks.
  97. llvm::DenseMap<SemIR::NodeBlockId, 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 nodes to lowered values.
  102. // TODO: Handle nested scopes. Right now this is just cleared at the end of
  103. // every block.
  104. llvm::DenseMap<SemIR::NodeId, llvm::Value*> locals_;
  105. };
  106. // Declare handlers for each SemIR::File node.
  107. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  108. auto LoweringHandle##Name(LoweringFunctionContext& context, \
  109. SemIR::NodeId node_id, SemIR::Node node) \
  110. ->void;
  111. #include "toolchain/semantics/semantics_node_kind.def"
  112. } // namespace Carbon
  113. #endif // CARBON_TOOLCHAIN_LOWERING_LOWERING_FUNCTION_CONTEXT_H_