handle.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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::RefParam /*inst*/) -> void {
  181. // Parameters are lowered by `BuildFunctionDefinition`.
  182. }
  183. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  184. SemIR::ValueParam /*inst*/) -> void {
  185. // Parameters are lowered by `BuildFunctionDefinition`.
  186. }
  187. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  188. SemIR::ReturnSlot inst) -> void {
  189. if (SemIR::InitRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  190. SemIR::InitRepr::InPlace) {
  191. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  192. }
  193. }
  194. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  195. SemIR::Return /*inst*/) -> void {
  196. context.builder().CreateRetVoid();
  197. }
  198. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  199. SemIR::ReturnExpr inst) -> void {
  200. auto result_type_id = context.sem_ir().insts().Get(inst.expr_id).type_id();
  201. switch (SemIR::InitRepr::ForType(context.sem_ir(), result_type_id).kind) {
  202. case SemIR::InitRepr::None:
  203. // Nothing to return.
  204. context.builder().CreateRetVoid();
  205. return;
  206. case SemIR::InitRepr::InPlace:
  207. context.FinishInit(result_type_id, inst.dest_id, inst.expr_id);
  208. context.builder().CreateRetVoid();
  209. return;
  210. case SemIR::InitRepr::ByCopy:
  211. // The expression produces the value representation for the type.
  212. context.builder().CreateRet(context.GetValue(inst.expr_id));
  213. return;
  214. case SemIR::InitRepr::Incomplete:
  215. CARBON_FATAL("Lowering return of incomplete type {0}",
  216. context.sem_ir().types().GetAsInst(result_type_id));
  217. }
  218. }
  219. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  220. SemIR::SpecificFunction /*inst*/) -> void {
  221. // Nothing to do. This value should never be consumed.
  222. }
  223. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  224. SemIR::SpecificImplFunction /*inst*/) -> void {
  225. // Nothing to do. This value should never be consumed.
  226. }
  227. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  228. SemIR::SpliceBlock inst) -> void {
  229. context.LowerBlockContents(inst.block_id);
  230. context.SetLocal(inst_id, context.GetValue(inst.result_id));
  231. }
  232. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  233. SemIR::SpliceInst /*inst*/) -> void {
  234. // TODO: Get the constant value of the spliced instruction from the current
  235. // specific, and lower the instruction in that constant value.
  236. CARBON_FATAL("Template lowering not implemented yet");
  237. }
  238. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  239. SemIR::UnaryOperatorNot inst) -> void {
  240. context.SetLocal(
  241. inst_id, context.builder().CreateNot(context.GetValue(inst.operand_id)));
  242. }
  243. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  244. SemIR::VarStorage inst) -> void {
  245. auto* type = context.GetType(inst.type_id);
  246. // Position the first alloca right before the start of the executable code in
  247. // the function.
  248. auto saved_ip = context.builder().saveIP();
  249. if (auto* after_allocas = context.GetInstructionAfterAllocas()) {
  250. context.builder().SetInsertPoint(after_allocas);
  251. } else {
  252. context.builder().SetInsertPointPastAllocas(&context.llvm_function());
  253. }
  254. // Create an alloca for this variable in the entry block.
  255. auto* alloca = context.builder().CreateAlloca(type);
  256. context.builder().restoreIP(saved_ip);
  257. // Create a lifetime start intrinsic here to indicate where its scope really
  258. // begins.
  259. auto size = context.llvm_module().getDataLayout().getTypeAllocSize(type);
  260. context.builder().CreateLifetimeStart(
  261. alloca,
  262. llvm::ConstantInt::get(context.llvm_context(), llvm::APInt(64, size)));
  263. // If we just created the first alloca, there is now definitely at least one
  264. // instruction after it -- there is a lifetime start instruction if nothing
  265. // else. Use that instruction as our insert point for all future allocas.
  266. if (!context.GetInstructionAfterAllocas()) {
  267. auto loc = alloca->getIterator();
  268. ++loc;
  269. context.SetInstructionAfterAllocas(&*loc);
  270. }
  271. // TODO: Create a matching `@llvm.lifetime.end` intrinsic call when the
  272. // variable goes out of scope.
  273. context.SetLocal(inst_id, alloca);
  274. }
  275. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  276. SemIR::VtablePtr inst) -> void {
  277. context.SetLocal(inst_id, context.GetValue(inst.vtable_id));
  278. }
  279. } // namespace Carbon::Lower