handle.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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::NameBindingDecl /*inst*/) -> void {
  160. // A NameBindingDecl is lowered by pattern matching.
  161. }
  162. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  163. SemIR::NameRef inst) -> void {
  164. auto type_inst_id = context.sem_ir().types().GetInstId(inst.type_id);
  165. if (type_inst_id == SemIR::NamespaceType::SingletonInstId) {
  166. return;
  167. }
  168. auto inner_inst_id = inst.value_id;
  169. if (auto bind_name =
  170. context.sem_ir().insts().TryGetAs<SemIR::BindName>(inner_inst_id)) {
  171. inner_inst_id = bind_name->value_id;
  172. }
  173. context.SetLocal(inst_id, context.GetValue(inner_inst_id));
  174. }
  175. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  176. SemIR::OutParam /*inst*/) -> void {
  177. // Parameters are lowered by `BuildFunctionDefinition`.
  178. }
  179. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  180. SemIR::ValueParam /*inst*/) -> void {
  181. // Parameters are lowered by `BuildFunctionDefinition`.
  182. }
  183. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  184. SemIR::ReturnSlot inst) -> void {
  185. if (SemIR::InitRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  186. SemIR::InitRepr::InPlace) {
  187. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  188. }
  189. }
  190. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  191. SemIR::Return /*inst*/) -> void {
  192. context.builder().CreateRetVoid();
  193. }
  194. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  195. SemIR::ReturnExpr inst) -> void {
  196. auto result_type_id = context.sem_ir().insts().Get(inst.expr_id).type_id();
  197. switch (SemIR::InitRepr::ForType(context.sem_ir(), result_type_id).kind) {
  198. case SemIR::InitRepr::None:
  199. // Nothing to return.
  200. context.builder().CreateRetVoid();
  201. return;
  202. case SemIR::InitRepr::InPlace:
  203. context.FinishInit(result_type_id, inst.dest_id, inst.expr_id);
  204. context.builder().CreateRetVoid();
  205. return;
  206. case SemIR::InitRepr::ByCopy:
  207. // The expression produces the value representation for the type.
  208. context.builder().CreateRet(context.GetValue(inst.expr_id));
  209. return;
  210. case SemIR::InitRepr::Incomplete:
  211. CARBON_FATAL("Lowering return of incomplete type {0}",
  212. context.sem_ir().types().GetAsInst(result_type_id));
  213. }
  214. }
  215. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  216. SemIR::SpecificFunction /*inst*/) -> void {
  217. // Nothing to do. This value should never be consumed.
  218. }
  219. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  220. SemIR::SpliceBlock inst) -> void {
  221. context.LowerBlockContents(inst.block_id);
  222. context.SetLocal(inst_id, context.GetValue(inst.result_id));
  223. }
  224. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  225. SemIR::UnaryOperatorNot inst) -> void {
  226. context.SetLocal(
  227. inst_id, context.builder().CreateNot(context.GetValue(inst.operand_id)));
  228. }
  229. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  230. SemIR::VarStorage inst) -> void {
  231. auto* type = context.GetType(inst.type_id);
  232. // Position the first alloca right before the start of the executable code in
  233. // the function.
  234. auto saved_ip = context.builder().saveIP();
  235. if (auto* after_allocas = context.GetInstructionAfterAllocas()) {
  236. context.builder().SetInsertPoint(after_allocas);
  237. } else {
  238. context.builder().SetInsertPointPastAllocas(&context.llvm_function());
  239. }
  240. // Create an alloca for this variable in the entry block.
  241. auto* alloca = context.builder().CreateAlloca(type);
  242. context.builder().restoreIP(saved_ip);
  243. // Create a lifetime start intrinsic here to indicate where its scope really
  244. // begins.
  245. auto size = context.llvm_module().getDataLayout().getTypeAllocSize(type);
  246. context.builder().CreateLifetimeStart(
  247. alloca,
  248. llvm::ConstantInt::get(context.llvm_context(), llvm::APInt(64, size)));
  249. // If we just created the first alloca, there is now definitely at least one
  250. // instruction after it -- there is a lifetime start instruction if nothing
  251. // else. Use that instruction as our insert point for all future allocas.
  252. if (!context.GetInstructionAfterAllocas()) {
  253. auto loc = alloca->getIterator();
  254. ++loc;
  255. context.SetInstructionAfterAllocas(&*loc);
  256. }
  257. // TODO: Create a matching `@llvm.lifetime.end` intrinsic call when the
  258. // variable goes out of scope.
  259. context.SetLocal(inst_id, alloca);
  260. }
  261. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  262. SemIR::VtablePtr /*inst*/) -> void {
  263. // TODO: Initialize the virtual pointer to actually point to a virtual
  264. // function table.
  265. context.SetLocal(inst_id,
  266. llvm::ConstantPointerNull::get(
  267. llvm::PointerType::get(context.llvm_context(), 0)));
  268. }
  269. } // namespace Carbon::Lower