function_context.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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::InstBlockId 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::InstBlockId 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::InstBlockId block_id) -> void {
  34. for (const auto& inst_id : sem_ir().inst_blocks().Get(block_id)) {
  35. auto inst = sem_ir().insts().Get(inst_id);
  36. CARBON_VLOG() << "Lowering " << inst_id << ": " << inst << "\n";
  37. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  38. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  39. switch (inst.kind()) {
  40. #define CARBON_SEM_IR_INST_KIND(Name) \
  41. case SemIR::Name::Kind: \
  42. Handle##Name(*this, inst_id, inst.As<SemIR::Name>()); \
  43. break;
  44. #include "toolchain/sem_ir/inst_kind.def"
  45. }
  46. }
  47. }
  48. auto FunctionContext::GetBlockArg(SemIR::InstBlockId 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::FinishInit(SemIR::TypeId type_id, SemIR::InstId dest_id,
  70. SemIR::InstId source_id) -> void {
  71. switch (SemIR::GetInitRepr(sem_ir(), type_id).kind) {
  72. case SemIR::InitRepr::None:
  73. case SemIR::InitRepr::InPlace:
  74. break;
  75. case SemIR::InitRepr::ByCopy:
  76. CopyValue(type_id, source_id, dest_id);
  77. break;
  78. }
  79. }
  80. auto FunctionContext::CopyValue(SemIR::TypeId type_id, SemIR::InstId source_id,
  81. SemIR::InstId dest_id) -> void {
  82. switch (auto rep = SemIR::GetValueRepr(sem_ir(), type_id); rep.kind) {
  83. case SemIR::ValueRepr::Unknown:
  84. CARBON_FATAL() << "Attempt to copy incomplete type";
  85. case SemIR::ValueRepr::None:
  86. break;
  87. case SemIR::ValueRepr::Copy:
  88. builder().CreateStore(GetValue(source_id), GetValue(dest_id));
  89. break;
  90. case SemIR::ValueRepr::Pointer: {
  91. const auto& layout = llvm_module().getDataLayout();
  92. auto* type = GetType(type_id);
  93. // TODO: Compute known alignment of the source and destination, which may
  94. // be greater than the alignment computed by LLVM.
  95. auto align = layout.getABITypeAlign(type);
  96. // TODO: Attach !tbaa.struct metadata indicating which portions of the
  97. // type we actually need to copy and which are padding.
  98. builder().CreateMemCpy(GetValue(dest_id), align, GetValue(source_id),
  99. align, layout.getTypeAllocSize(type));
  100. break;
  101. }
  102. case SemIR::ValueRepr::Custom:
  103. CARBON_FATAL() << "TODO: Add support for CopyValue with custom value rep";
  104. }
  105. }
  106. } // namespace Carbon::Lower