function_context.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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(), llvm::ConstantFolder(),
  14. Inserter(file_context.inst_namer())),
  15. vlog_stream_(vlog_stream) {}
  16. auto FunctionContext::GetBlock(SemIR::InstBlockId block_id)
  17. -> llvm::BasicBlock* {
  18. llvm::BasicBlock*& entry = blocks_[block_id];
  19. if (!entry) {
  20. llvm::StringRef label_name;
  21. if (const auto* inst_namer = file_context_->inst_namer()) {
  22. label_name = inst_namer->GetUnscopedLabelFor(block_id);
  23. }
  24. entry = llvm::BasicBlock::Create(llvm_context(), label_name, function_);
  25. }
  26. return entry;
  27. }
  28. auto FunctionContext::TryToReuseBlock(SemIR::InstBlockId block_id,
  29. llvm::BasicBlock* block) -> bool {
  30. if (!blocks_.insert({block_id, block}).second) {
  31. return false;
  32. }
  33. if (block == synthetic_block_) {
  34. synthetic_block_ = nullptr;
  35. }
  36. if (const auto* inst_namer = file_context_->inst_namer()) {
  37. block->setName(inst_namer->GetUnscopedLabelFor(block_id));
  38. }
  39. return true;
  40. }
  41. auto FunctionContext::LowerBlock(SemIR::InstBlockId block_id) -> void {
  42. for (auto inst_id : sem_ir().inst_blocks().Get(block_id)) {
  43. // Skip over constants. `FileContext::GetGlobal` lowers them as needed.
  44. if (sem_ir().constant_values().Get(inst_id).is_constant()) {
  45. continue;
  46. }
  47. LowerInst(inst_id);
  48. }
  49. }
  50. auto FunctionContext::LowerInst(SemIR::InstId inst_id) -> void {
  51. auto inst = sem_ir().insts().Get(inst_id);
  52. CARBON_VLOG() << "Lowering " << inst_id << ": " << inst << "\n";
  53. builder_.getInserter().SetCurrentInstId(inst_id);
  54. switch (inst.kind()) {
  55. #define CARBON_SEM_IR_INST_KIND(Name) \
  56. case SemIR::Name::Kind: \
  57. Handle##Name(*this, inst_id, inst.As<SemIR::Name>()); \
  58. break;
  59. #include "toolchain/sem_ir/inst_kind.def"
  60. }
  61. builder_.getInserter().SetCurrentInstId(SemIR::InstId::Invalid);
  62. }
  63. auto FunctionContext::GetBlockArg(SemIR::InstBlockId block_id,
  64. SemIR::TypeId type_id) -> llvm::PHINode* {
  65. llvm::BasicBlock* block = GetBlock(block_id);
  66. // Find the existing phi, if any.
  67. auto phis = block->phis();
  68. if (!phis.empty()) {
  69. CARBON_CHECK(std::next(phis.begin()) == phis.end())
  70. << "Expected at most one phi, found "
  71. << std::distance(phis.begin(), phis.end());
  72. return &*phis.begin();
  73. }
  74. // The number of predecessor slots to reserve.
  75. static constexpr unsigned NumReservedPredecessors = 2;
  76. auto* phi = llvm::PHINode::Create(GetType(type_id), NumReservedPredecessors);
  77. phi->insertInto(block, block->begin());
  78. return phi;
  79. }
  80. auto FunctionContext::MakeSyntheticBlock() -> llvm::BasicBlock* {
  81. synthetic_block_ = llvm::BasicBlock::Create(llvm_context(), "", function_);
  82. return synthetic_block_;
  83. }
  84. auto FunctionContext::FinishInit(SemIR::TypeId type_id, SemIR::InstId dest_id,
  85. SemIR::InstId source_id) -> void {
  86. switch (SemIR::GetInitRepr(sem_ir(), type_id).kind) {
  87. case SemIR::InitRepr::None:
  88. break;
  89. case SemIR::InitRepr::InPlace:
  90. if (sem_ir().constant_values().Get(source_id).is_constant()) {
  91. // When initializing from a constant, emission of the source doesn't
  92. // initialize the destination. Copy the constant value instead.
  93. CopyValue(type_id, source_id, dest_id);
  94. }
  95. break;
  96. case SemIR::InitRepr::ByCopy:
  97. CopyValue(type_id, source_id, dest_id);
  98. break;
  99. }
  100. }
  101. auto FunctionContext::CopyValue(SemIR::TypeId type_id, SemIR::InstId source_id,
  102. SemIR::InstId dest_id) -> void {
  103. switch (auto rep = SemIR::GetValueRepr(sem_ir(), type_id); rep.kind) {
  104. case SemIR::ValueRepr::Unknown:
  105. CARBON_FATAL() << "Attempt to copy incomplete type";
  106. case SemIR::ValueRepr::None:
  107. break;
  108. case SemIR::ValueRepr::Copy:
  109. builder().CreateStore(GetValue(source_id), GetValue(dest_id));
  110. break;
  111. case SemIR::ValueRepr::Pointer:
  112. CopyObject(type_id, source_id, dest_id);
  113. break;
  114. case SemIR::ValueRepr::Custom:
  115. CARBON_FATAL() << "TODO: Add support for CopyValue with custom value rep";
  116. }
  117. }
  118. auto FunctionContext::CopyObject(SemIR::TypeId type_id, SemIR::InstId source_id,
  119. SemIR::InstId dest_id) -> void {
  120. const auto& layout = llvm_module().getDataLayout();
  121. auto* type = GetType(type_id);
  122. // TODO: Compute known alignment of the source and destination, which may
  123. // be greater than the alignment computed by LLVM.
  124. auto align = layout.getABITypeAlign(type);
  125. // TODO: Attach !tbaa.struct metadata indicating which portions of the
  126. // type we actually need to copy and which are padding.
  127. builder().CreateMemCpy(GetValue(dest_id), align, GetValue(source_id), align,
  128. layout.getTypeAllocSize(type));
  129. }
  130. auto FunctionContext::Inserter::InsertHelper(
  131. llvm::Instruction* inst, const llvm::Twine& name, llvm::BasicBlock* block,
  132. llvm::BasicBlock::iterator insert_pt) const -> void {
  133. llvm::StringRef base_name;
  134. llvm::StringRef separator;
  135. if (inst_namer_ && !inst->getType()->isVoidTy()) {
  136. base_name = inst_namer_->GetUnscopedNameFor(inst_id_);
  137. }
  138. if (!base_name.empty() && !name.isTriviallyEmpty()) {
  139. separator = ".";
  140. }
  141. IRBuilderDefaultInserter::InsertHelper(inst, base_name + separator + name,
  142. block, insert_pt);
  143. }
  144. } // namespace Carbon::Lower