function_context.cpp 6.0 KB

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