function_context.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. switch (inst.kind()) {
  38. #define CARBON_SEM_IR_INST_KIND(Name) \
  39. case SemIR::Name::Kind: \
  40. Handle##Name(*this, inst_id, inst.As<SemIR::Name>()); \
  41. break;
  42. #include "toolchain/sem_ir/inst_kind.def"
  43. }
  44. }
  45. }
  46. auto FunctionContext::GetBlockArg(SemIR::InstBlockId block_id,
  47. SemIR::TypeId type_id) -> llvm::PHINode* {
  48. llvm::BasicBlock* block = GetBlock(block_id);
  49. // Find the existing phi, if any.
  50. auto phis = block->phis();
  51. if (!phis.empty()) {
  52. CARBON_CHECK(std::next(phis.begin()) == phis.end())
  53. << "Expected at most one phi, found "
  54. << std::distance(phis.begin(), phis.end());
  55. return &*phis.begin();
  56. }
  57. // The number of predecessor slots to reserve.
  58. static constexpr unsigned NumReservedPredecessors = 2;
  59. auto* phi = llvm::PHINode::Create(GetType(type_id), NumReservedPredecessors);
  60. phi->insertInto(block, block->begin());
  61. return phi;
  62. }
  63. auto FunctionContext::CreateSyntheticBlock() -> llvm::BasicBlock* {
  64. synthetic_block_ = llvm::BasicBlock::Create(llvm_context(), "", function_);
  65. return synthetic_block_;
  66. }
  67. auto FunctionContext::FinishInit(SemIR::TypeId type_id, SemIR::InstId dest_id,
  68. SemIR::InstId source_id) -> void {
  69. switch (SemIR::GetInitRepr(sem_ir(), type_id).kind) {
  70. case SemIR::InitRepr::None:
  71. case SemIR::InitRepr::InPlace:
  72. break;
  73. case SemIR::InitRepr::ByCopy:
  74. CopyValue(type_id, source_id, dest_id);
  75. break;
  76. }
  77. }
  78. auto FunctionContext::CopyValue(SemIR::TypeId type_id, SemIR::InstId source_id,
  79. SemIR::InstId dest_id) -> void {
  80. switch (auto rep = SemIR::GetValueRepr(sem_ir(), type_id); rep.kind) {
  81. case SemIR::ValueRepr::Unknown:
  82. CARBON_FATAL() << "Attempt to copy incomplete type";
  83. case SemIR::ValueRepr::None:
  84. break;
  85. case SemIR::ValueRepr::Copy:
  86. builder().CreateStore(GetValue(source_id), GetValue(dest_id));
  87. break;
  88. case SemIR::ValueRepr::Pointer: {
  89. const auto& layout = llvm_module().getDataLayout();
  90. auto* type = GetType(type_id);
  91. // TODO: Compute known alignment of the source and destination, which may
  92. // be greater than the alignment computed by LLVM.
  93. auto align = layout.getABITypeAlign(type);
  94. // TODO: Attach !tbaa.struct metadata indicating which portions of the
  95. // type we actually need to copy and which are padding.
  96. builder().CreateMemCpy(GetValue(dest_id), align, GetValue(source_id),
  97. align, layout.getTypeAllocSize(type));
  98. break;
  99. }
  100. case SemIR::ValueRepr::Custom:
  101. CARBON_FATAL() << "TODO: Add support for CopyValue with custom value rep";
  102. }
  103. }
  104. } // namespace Carbon::Lower