function_context.cpp 6.0 KB

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