function_context.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "toolchain/lower/function_context.h"
  5. #include "common/vlog.h"
  6. #include "toolchain/sem_ir/file.h"
  7. namespace Carbon::Lower {
  8. FunctionContext::FunctionContext(FileContext& file_context,
  9. llvm::Function* function,
  10. llvm::raw_ostream* vlog_stream)
  11. : file_context_(&file_context),
  12. function_(function),
  13. builder_(file_context.llvm_context()),
  14. vlog_stream_(vlog_stream) {}
  15. auto FunctionContext::GetBlock(SemIR::NodeBlockId block_id)
  16. -> llvm::BasicBlock* {
  17. llvm::BasicBlock*& entry = blocks_[block_id];
  18. if (!entry) {
  19. entry = llvm::BasicBlock::Create(llvm_context(), "", function_);
  20. }
  21. return entry;
  22. }
  23. auto FunctionContext::TryToReuseBlock(SemIR::NodeBlockId block_id,
  24. llvm::BasicBlock* block) -> bool {
  25. if (!blocks_.insert({block_id, block}).second) {
  26. return false;
  27. }
  28. if (block == synthetic_block_) {
  29. synthetic_block_ = nullptr;
  30. }
  31. return true;
  32. }
  33. auto FunctionContext::LowerBlock(SemIR::NodeBlockId block_id) -> void {
  34. for (const auto& node_id : semantics_ir().GetNodeBlock(block_id)) {
  35. auto node = semantics_ir().GetNode(node_id);
  36. CARBON_VLOG() << "Lowering " << node_id << ": " << node << "\n";
  37. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  38. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  39. switch (node.kind()) {
  40. #define CARBON_SEM_IR_NODE_KIND(Name) \
  41. case SemIR::Name::Kind: \
  42. Handle##Name(*this, node_id, node.As<SemIR::Name>()); \
  43. break;
  44. #include "toolchain/sem_ir/node_kind.def"
  45. }
  46. }
  47. }
  48. auto FunctionContext::GetBlockArg(SemIR::NodeBlockId block_id,
  49. SemIR::TypeId type_id) -> llvm::PHINode* {
  50. llvm::BasicBlock* block = GetBlock(block_id);
  51. // Find the existing phi, if any.
  52. auto phis = block->phis();
  53. if (!phis.empty()) {
  54. CARBON_CHECK(std::next(phis.begin()) == phis.end())
  55. << "Expected at most one phi, found "
  56. << std::distance(phis.begin(), phis.end());
  57. return &*phis.begin();
  58. }
  59. // The number of predecessor slots to reserve.
  60. static constexpr unsigned NumReservedPredecessors = 2;
  61. auto* phi = llvm::PHINode::Create(GetType(type_id), NumReservedPredecessors);
  62. phi->insertInto(block, block->begin());
  63. return phi;
  64. }
  65. auto FunctionContext::CreateSyntheticBlock() -> llvm::BasicBlock* {
  66. synthetic_block_ = llvm::BasicBlock::Create(llvm_context(), "", function_);
  67. return synthetic_block_;
  68. }
  69. auto FunctionContext::FinishInitialization(SemIR::TypeId type_id,
  70. SemIR::NodeId dest_id,
  71. SemIR::NodeId source_id) -> void {
  72. switch (SemIR::GetInitializingRepresentation(semantics_ir(), type_id).kind) {
  73. case SemIR::InitializingRepresentation::None:
  74. case SemIR::InitializingRepresentation::InPlace:
  75. break;
  76. case SemIR::InitializingRepresentation::ByCopy:
  77. CopyValue(type_id, source_id, dest_id);
  78. break;
  79. }
  80. }
  81. auto FunctionContext::CopyValue(SemIR::TypeId type_id, SemIR::NodeId source_id,
  82. SemIR::NodeId dest_id) -> void {
  83. switch (auto rep = SemIR::GetValueRepresentation(semantics_ir(), type_id);
  84. rep.kind) {
  85. case SemIR::ValueRepresentation::Unknown:
  86. CARBON_FATAL() << "Attempt to copy incomplete type";
  87. case SemIR::ValueRepresentation::None:
  88. break;
  89. case SemIR::ValueRepresentation::Copy:
  90. builder().CreateStore(GetLocal(source_id), GetLocal(dest_id));
  91. break;
  92. case SemIR::ValueRepresentation::Pointer: {
  93. const auto& layout = llvm_module().getDataLayout();
  94. auto* type = GetType(type_id);
  95. // TODO: Compute known alignment of the source and destination, which may
  96. // be greater than the alignment computed by LLVM.
  97. auto align = layout.getABITypeAlign(type);
  98. // TODO: Attach !tbaa.struct metadata indicating which portions of the
  99. // type we actually need to copy and which are padding.
  100. builder().CreateMemCpy(GetLocal(dest_id), align, GetLocal(source_id),
  101. align, layout.getTypeAllocSize(type));
  102. break;
  103. }
  104. case SemIR::ValueRepresentation::Custom:
  105. CARBON_FATAL() << "TODO: Add support for CopyValue with custom value rep";
  106. }
  107. }
  108. } // namespace Carbon::Lower