handle.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "llvm/ADT/APFloat.h"
  5. #include "llvm/ADT/APInt.h"
  6. #include "llvm/ADT/ArrayRef.h"
  7. #include "llvm/IR/BasicBlock.h"
  8. #include "llvm/IR/Constants.h"
  9. #include "llvm/IR/IRBuilder.h"
  10. #include "llvm/IR/Type.h"
  11. #include "llvm/IR/Value.h"
  12. #include "llvm/Support/Casting.h"
  13. #include "toolchain/lower/function_context.h"
  14. #include "toolchain/sem_ir/builtin_function_kind.h"
  15. #include "toolchain/sem_ir/function.h"
  16. #include "toolchain/sem_ir/inst.h"
  17. #include "toolchain/sem_ir/typed_insts.h"
  18. namespace Carbon::Lower {
  19. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  20. SemIR::AddrOf inst) -> void {
  21. context.SetLocal(inst_id, context.GetValue(inst.lvalue_id));
  22. }
  23. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  24. SemIR::ArrayIndex inst) -> void {
  25. auto* array_value = context.GetValue(inst.array_id);
  26. auto* llvm_type =
  27. context.GetType(context.sem_ir().insts().Get(inst.array_id).type_id());
  28. // The index in an `ArrayIndex` can be of any integer type, including
  29. // IntLiteral. If it is an IntLiteral, its value representation is empty, so
  30. // create a ConstantInt from its SemIR value directly.
  31. llvm::Value* index;
  32. if (context.sem_ir().types().GetInstId(
  33. context.sem_ir().insts().Get(inst.index_id).type_id()) ==
  34. SemIR::IntLiteralType::SingletonInstId) {
  35. auto value = context.sem_ir().insts().GetAs<SemIR::IntValue>(
  36. context.sem_ir().constant_values().GetConstantInstId(inst.index_id));
  37. index = llvm::ConstantInt::get(context.llvm_context(),
  38. context.sem_ir().ints().Get(value.int_id));
  39. } else {
  40. index = context.GetValue(inst.index_id);
  41. }
  42. llvm::Value* indexes[2] = {
  43. llvm::ConstantInt::get(llvm::Type::getInt32Ty(context.llvm_context()), 0),
  44. index};
  45. context.SetLocal(inst_id,
  46. context.builder().CreateInBoundsGEP(llvm_type, array_value,
  47. indexes, "array.index"));
  48. }
  49. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  50. SemIR::ArrayInit inst) -> void {
  51. // The result of initialization is the return slot of the initializer.
  52. context.SetLocal(inst_id, context.GetValue(inst.dest_id));
  53. }
  54. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  55. SemIR::AsCompatible inst) -> void {
  56. context.SetLocal(inst_id, context.GetValue(inst.source_id));
  57. }
  58. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  59. SemIR::Assign inst) -> void {
  60. auto storage_type_id = context.sem_ir().insts().Get(inst.lhs_id).type_id();
  61. context.FinishInit(storage_type_id, inst.lhs_id, inst.rhs_id);
  62. }
  63. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  64. SemIR::BindAlias inst) -> void {
  65. auto type_inst_id = context.sem_ir().types().GetInstId(inst.type_id);
  66. if (type_inst_id == SemIR::NamespaceType::SingletonInstId) {
  67. return;
  68. }
  69. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  70. }
  71. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  72. SemIR::ExportDecl inst) -> void {
  73. auto type_inst_id = context.sem_ir().types().GetInstId(inst.type_id);
  74. if (type_inst_id == SemIR::NamespaceType::SingletonInstId) {
  75. return;
  76. }
  77. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  78. }
  79. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  80. SemIR::BindName inst) -> void {
  81. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  82. }
  83. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  84. SemIR::BindSymbolicName inst) -> void {
  85. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  86. }
  87. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  88. SemIR::BlockArg inst) -> void {
  89. context.SetLocal(inst_id, context.GetBlockArg(inst.block_id, inst.type_id));
  90. }
  91. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  92. SemIR::BoundMethod inst) -> void {
  93. // Propagate just the function; the object is separately provided to the
  94. // enclosing call as an implicit argument.
  95. context.SetLocal(inst_id, context.GetValue(inst.function_decl_id));
  96. }
  97. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  98. SemIR::Branch inst) -> void {
  99. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  100. // TODO: Don't do this if it would remove a loop preheader block.
  101. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  102. if (block->empty() && context.TryToReuseBlock(inst.target_id, block)) {
  103. // Reuse this block as the branch target.
  104. } else {
  105. context.builder().CreateBr(context.GetBlock(inst.target_id));
  106. }
  107. context.builder().ClearInsertionPoint();
  108. }
  109. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  110. SemIR::BranchIf inst) -> void {
  111. llvm::Value* cond = context.GetValue(inst.cond_id);
  112. llvm::BasicBlock* then_block = context.GetBlock(inst.target_id);
  113. llvm::BasicBlock* else_block = context.MakeSyntheticBlock();
  114. context.builder().CreateCondBr(cond, then_block, else_block);
  115. context.builder().SetInsertPoint(else_block);
  116. }
  117. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  118. SemIR::BranchWithArg inst) -> void {
  119. llvm::Value* arg = context.GetValue(inst.arg_id);
  120. SemIR::TypeId arg_type_id =
  121. context.sem_ir().insts().Get(inst.arg_id).type_id();
  122. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  123. // We only do this for a block that we know will only have a single
  124. // predecessor, so that we can correctly populate the predecessors of the
  125. // PHINode.
  126. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  127. llvm::BasicBlock* phi_predecessor = block;
  128. if (block->empty() && context.IsCurrentSyntheticBlock(block) &&
  129. context.TryToReuseBlock(inst.target_id, block)) {
  130. // Reuse this block as the branch target.
  131. phi_predecessor = block->getSinglePredecessor();
  132. CARBON_CHECK(phi_predecessor,
  133. "Synthetic block did not have a single predecessor");
  134. } else {
  135. context.builder().CreateBr(context.GetBlock(inst.target_id));
  136. }
  137. context.GetBlockArg(inst.target_id, arg_type_id)
  138. ->addIncoming(arg, phi_predecessor);
  139. context.builder().ClearInsertionPoint();
  140. }
  141. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  142. SemIR::Converted inst) -> void {
  143. context.SetLocal(inst_id, context.GetValue(inst.result_id));
  144. }
  145. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  146. SemIR::Deref inst) -> void {
  147. context.SetLocal(inst_id, context.GetValue(inst.pointer_id));
  148. }
  149. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  150. SemIR::FacetAccessType /*inst*/) -> void {
  151. context.SetLocal(inst_id, context.GetTypeAsValue());
  152. }
  153. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  154. SemIR::InitializeFrom inst) -> void {
  155. auto storage_type_id = context.sem_ir().insts().Get(inst.dest_id).type_id();
  156. context.FinishInit(storage_type_id, inst.dest_id, inst.src_id);
  157. }
  158. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  159. SemIR::NameRef inst) -> void {
  160. auto type_inst_id = context.sem_ir().types().GetInstId(inst.type_id);
  161. if (type_inst_id == SemIR::NamespaceType::SingletonInstId) {
  162. return;
  163. }
  164. auto inner_inst_id = inst.value_id;
  165. if (auto bind_name =
  166. context.sem_ir().insts().TryGetAs<SemIR::BindName>(inner_inst_id)) {
  167. inner_inst_id = bind_name->value_id;
  168. }
  169. context.SetLocal(inst_id, context.GetValue(inner_inst_id));
  170. }
  171. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  172. SemIR::OutParam /*inst*/) -> void {
  173. // Parameters are lowered by `BuildFunctionDefinition`.
  174. }
  175. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  176. SemIR::ValueParam /*inst*/) -> void {
  177. // Parameters are lowered by `BuildFunctionDefinition`.
  178. }
  179. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  180. SemIR::ReturnSlot inst) -> void {
  181. if (SemIR::InitRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  182. SemIR::InitRepr::InPlace) {
  183. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  184. }
  185. }
  186. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  187. SemIR::Return /*inst*/) -> void {
  188. context.builder().CreateRetVoid();
  189. }
  190. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  191. SemIR::ReturnExpr inst) -> void {
  192. auto result_type_id = context.sem_ir().insts().Get(inst.expr_id).type_id();
  193. switch (SemIR::InitRepr::ForType(context.sem_ir(), result_type_id).kind) {
  194. case SemIR::InitRepr::None:
  195. // Nothing to return.
  196. context.builder().CreateRetVoid();
  197. return;
  198. case SemIR::InitRepr::InPlace:
  199. context.FinishInit(result_type_id, inst.dest_id, inst.expr_id);
  200. context.builder().CreateRetVoid();
  201. return;
  202. case SemIR::InitRepr::ByCopy:
  203. // The expression produces the value representation for the type.
  204. context.builder().CreateRet(context.GetValue(inst.expr_id));
  205. return;
  206. case SemIR::InitRepr::Incomplete:
  207. CARBON_FATAL("Lowering return of incomplete type {0}",
  208. context.sem_ir().types().GetAsInst(result_type_id));
  209. }
  210. }
  211. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  212. SemIR::SpecificFunction /*inst*/) -> void {
  213. // Nothing to do. This value should never be consumed.
  214. }
  215. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  216. SemIR::SpliceBlock inst) -> void {
  217. context.LowerBlockContents(inst.block_id);
  218. context.SetLocal(inst_id, context.GetValue(inst.result_id));
  219. }
  220. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  221. SemIR::UnaryOperatorNot inst) -> void {
  222. context.SetLocal(
  223. inst_id, context.builder().CreateNot(context.GetValue(inst.operand_id)));
  224. }
  225. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  226. SemIR::VarStorage inst) -> void {
  227. auto* type = context.GetType(inst.type_id);
  228. // Position the first alloca right before the start of the executable code in
  229. // the function.
  230. auto saved_ip = context.builder().saveIP();
  231. if (auto* after_allocas = context.GetInstructionAfterAllocas()) {
  232. context.builder().SetInsertPoint(after_allocas);
  233. } else {
  234. context.builder().SetInsertPointPastAllocas(&context.llvm_function());
  235. }
  236. // Create an alloca for this variable in the entry block.
  237. auto* alloca = context.builder().CreateAlloca(type);
  238. context.builder().restoreIP(saved_ip);
  239. // Create a lifetime start intrinsic here to indicate where its scope really
  240. // begins.
  241. auto size = context.llvm_module().getDataLayout().getTypeAllocSize(type);
  242. context.builder().CreateLifetimeStart(
  243. alloca,
  244. llvm::ConstantInt::get(context.llvm_context(), llvm::APInt(64, size)));
  245. // If we just created the first alloca, there is now definitely at least one
  246. // instruction after it -- there is a lifetime start instruction if nothing
  247. // else. Use that instruction as our insert point for all future allocas.
  248. if (!context.GetInstructionAfterAllocas()) {
  249. auto loc = alloca->getIterator();
  250. ++loc;
  251. context.SetInstructionAfterAllocas(&*loc);
  252. }
  253. // TODO: Create a matching `@llvm.lifetime.end` intrinsic call when the
  254. // variable goes out of scope.
  255. context.SetLocal(inst_id, alloca);
  256. }
  257. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  258. SemIR::VtablePtr /*inst*/) -> void {
  259. // TODO: Initialize the virtual pointer to actually point to a virtual
  260. // function table.
  261. context.SetLocal(inst_id,
  262. llvm::ConstantPointerNull::get(
  263. llvm::PointerType::get(context.llvm_context(), 0)));
  264. }
  265. } // namespace Carbon::Lower