lowering_function_context.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_block_worklist.h"
  11. #include "toolchain/lowering/lowering_context.h"
  12. #include "toolchain/semantics/semantics_ir.h"
  13. #include "toolchain/semantics/semantics_node.h"
  14. namespace Carbon {
  15. // Context and shared functionality for lowering handlers that produce an
  16. // `llvm::Function` definition.
  17. class LoweringFunctionContext {
  18. public:
  19. explicit LoweringFunctionContext(LoweringContext& lowering_context,
  20. llvm::Function* function);
  21. // Returns a basic block corresponding to the start of the given semantics
  22. // block, and enqueues it for emission.
  23. auto GetBlock(SemanticsNodeBlockId block_id) -> llvm::BasicBlock*;
  24. // If we have not yet allocated a `BasicBlock` for this `block_id`, set it to
  25. // `block`, and enqueue `block_id` for emission. Returns whether we set the
  26. // block.
  27. auto TryToReuseBlock(SemanticsNodeBlockId block_id, llvm::BasicBlock* block)
  28. -> bool;
  29. // Returns a phi node corresponding to the block argument of the given basic
  30. // block.
  31. auto GetBlockArg(SemanticsNodeBlockId block_id, SemanticsTypeId type_id)
  32. -> llvm::PHINode*;
  33. // Returns a local (versus global) value for the given node.
  34. auto GetLocal(SemanticsNodeId node_id) -> llvm::Value* {
  35. auto it = locals_.find(node_id);
  36. CARBON_CHECK(it != locals_.end()) << "Missing local: " << node_id;
  37. return it->second;
  38. }
  39. // Returns a local (versus global) value for the given node in loaded state.
  40. // Loads will only be inserted on an as-needed basis.
  41. auto GetLocalLoaded(SemanticsNodeId node_id) -> llvm::Value*;
  42. // Sets the value for the given node.
  43. auto SetLocal(SemanticsNodeId node_id, llvm::Value* value) {
  44. bool added = locals_.insert({node_id, value}).second;
  45. CARBON_CHECK(added) << "Duplicate local insert: " << node_id;
  46. }
  47. // Gets a callable's function.
  48. auto GetFunction(SemanticsFunctionId function_id) -> llvm::Function* {
  49. return lowering_context_->GetFunction(function_id);
  50. }
  51. // Returns a lowered type for the given type_id.
  52. auto GetType(SemanticsTypeId type_id) -> llvm::Type* {
  53. return lowering_context_->GetType(type_id);
  54. }
  55. // Create a synthetic block that corresponds to no SemanticsNodeBlockId. Such
  56. // a block should only ever have a single predecessor, and is used when we
  57. // need multiple `llvm::BasicBlock`s to model the linear control flow in a
  58. // single SemanticsIR block.
  59. auto CreateSyntheticBlock() -> llvm::BasicBlock*;
  60. // Determine whether block is the most recently created synthetic block.
  61. auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
  62. return synthetic_block_ == block;
  63. }
  64. auto llvm_context() -> llvm::LLVMContext& {
  65. return lowering_context_->llvm_context();
  66. }
  67. auto llvm_module() -> llvm::Module& {
  68. return lowering_context_->llvm_module();
  69. }
  70. auto builder() -> llvm::IRBuilder<>& { return builder_; }
  71. auto block_worklist() -> LoweringBlockWorklist& { return block_worklist_; }
  72. auto semantics_ir() -> const SemanticsIR& {
  73. return lowering_context_->semantics_ir();
  74. }
  75. private:
  76. // Context for the overall lowering process.
  77. LoweringContext* lowering_context_;
  78. // The IR function we're generating.
  79. llvm::Function* function_;
  80. llvm::IRBuilder<> builder_;
  81. // A worklist for blocks we need to lower.
  82. LoweringBlockWorklist block_worklist_;
  83. // Maps a function's SemanticsIR blocks to lowered blocks.
  84. llvm::DenseMap<SemanticsNodeBlockId, llvm::BasicBlock*> blocks_;
  85. // The synthetic block we most recently created. May be null if there is no
  86. // such block.
  87. llvm::BasicBlock* synthetic_block_ = nullptr;
  88. // Maps a function's SemanticsIR nodes to lowered values.
  89. // TODO: Handle nested scopes. Right now this is just cleared at the end of
  90. // every block.
  91. llvm::DenseMap<SemanticsNodeId, llvm::Value*> locals_;
  92. };
  93. // Declare handlers for each SemanticsIR node.
  94. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  95. auto LoweringHandle##Name(LoweringFunctionContext& context, \
  96. SemanticsNodeId node_id, SemanticsNode node) \
  97. ->void;
  98. #include "toolchain/semantics/semantics_node_kind.def"
  99. } // namespace Carbon
  100. #endif // CARBON_TOOLCHAIN_LOWERING_LOWERING_FUNCTION_CONTEXT_H_