handle.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/Type.h"
  10. #include "llvm/IR/Value.h"
  11. #include "llvm/Support/Casting.h"
  12. #include "toolchain/lower/function_context.h"
  13. #include "toolchain/sem_ir/builtin_function_kind.h"
  14. #include "toolchain/sem_ir/function.h"
  15. #include "toolchain/sem_ir/inst.h"
  16. #include "toolchain/sem_ir/typed_insts.h"
  17. namespace Carbon::Lower {
  18. template <typename InstT>
  19. static auto FatalErrorIfEncountered(InstT inst) -> void {
  20. CARBON_FATAL()
  21. << "Encountered an instruction that isn't expected to lower. It's "
  22. "possible that logic needs to be changed in order to stop "
  23. "showing this instruction in lowered contexts. Instruction: "
  24. << inst;
  25. }
  26. auto HandleAdaptDecl(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  27. SemIR::AdaptDecl inst) -> void {
  28. FatalErrorIfEncountered(inst);
  29. }
  30. auto HandleAddrOf(FunctionContext& context, SemIR::InstId inst_id,
  31. SemIR::AddrOf inst) -> void {
  32. context.SetLocal(inst_id, context.GetValue(inst.lvalue_id));
  33. }
  34. auto HandleAddrPattern(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  35. SemIR::AddrPattern /*inst*/) -> void {
  36. CARBON_FATAL() << "`addr` should be lowered by `BuildFunctionDefinition`";
  37. }
  38. auto HandleArrayIndex(FunctionContext& context, SemIR::InstId inst_id,
  39. SemIR::ArrayIndex inst) -> void {
  40. auto* array_value = context.GetValue(inst.array_id);
  41. auto* llvm_type =
  42. context.GetType(context.sem_ir().insts().Get(inst.array_id).type_id());
  43. llvm::Value* indexes[2] = {
  44. llvm::ConstantInt::get(llvm::Type::getInt32Ty(context.llvm_context()), 0),
  45. context.GetValue(inst.index_id)};
  46. context.SetLocal(inst_id,
  47. context.builder().CreateInBoundsGEP(llvm_type, array_value,
  48. indexes, "array.index"));
  49. }
  50. auto HandleArrayInit(FunctionContext& context, SemIR::InstId inst_id,
  51. SemIR::ArrayInit inst) -> void {
  52. // The result of initialization is the return slot of the initializer.
  53. context.SetLocal(inst_id, context.GetValue(inst.dest_id));
  54. }
  55. auto HandleAsCompatible(FunctionContext& context, SemIR::InstId inst_id,
  56. SemIR::AsCompatible inst) -> void {
  57. context.SetLocal(inst_id, context.GetValue(inst.source_id));
  58. }
  59. auto HandleAssign(FunctionContext& context, SemIR::InstId /*inst_id*/,
  60. SemIR::Assign inst) -> void {
  61. auto storage_type_id = context.sem_ir().insts().Get(inst.lhs_id).type_id();
  62. context.FinishInit(storage_type_id, inst.lhs_id, inst.rhs_id);
  63. }
  64. auto HandleAssociatedConstantDecl(FunctionContext& /*context*/,
  65. SemIR::InstId /*inst_id*/,
  66. SemIR::AssociatedConstantDecl inst) -> void {
  67. FatalErrorIfEncountered(inst);
  68. }
  69. auto HandleBindAlias(FunctionContext& context, SemIR::InstId inst_id,
  70. SemIR::BindAlias inst) -> void {
  71. auto type_inst_id = context.sem_ir().types().GetInstId(inst.type_id);
  72. if (type_inst_id == SemIR::InstId::BuiltinNamespaceType) {
  73. return;
  74. }
  75. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  76. }
  77. auto HandleExportDecl(FunctionContext& context, SemIR::InstId inst_id,
  78. SemIR::ExportDecl inst) -> void {
  79. auto type_inst_id = context.sem_ir().types().GetInstId(inst.type_id);
  80. if (type_inst_id == SemIR::InstId::BuiltinNamespaceType) {
  81. return;
  82. }
  83. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  84. }
  85. auto HandleBindName(FunctionContext& context, SemIR::InstId inst_id,
  86. SemIR::BindName inst) -> void {
  87. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  88. }
  89. auto HandleBindSymbolicName(FunctionContext& context, SemIR::InstId inst_id,
  90. SemIR::BindSymbolicName inst) -> void {
  91. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  92. }
  93. auto HandleBlockArg(FunctionContext& context, SemIR::InstId inst_id,
  94. SemIR::BlockArg inst) -> void {
  95. context.SetLocal(inst_id, context.GetBlockArg(inst.block_id, inst.type_id));
  96. }
  97. auto HandleBoundMethod(FunctionContext& context, SemIR::InstId inst_id,
  98. SemIR::BoundMethod inst) -> void {
  99. // Propagate just the function; the object is separately provided to the
  100. // enclosing call as an implicit argument.
  101. context.SetLocal(inst_id, context.GetValue(inst.function_id));
  102. }
  103. auto HandleBranch(FunctionContext& context, SemIR::InstId /*inst_id*/,
  104. SemIR::Branch inst) -> void {
  105. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  106. // TODO: Don't do this if it would remove a loop preheader block.
  107. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  108. if (block->empty() && context.TryToReuseBlock(inst.target_id, block)) {
  109. // Reuse this block as the branch target.
  110. } else {
  111. context.builder().CreateBr(context.GetBlock(inst.target_id));
  112. }
  113. context.builder().ClearInsertionPoint();
  114. }
  115. auto HandleBranchIf(FunctionContext& context, SemIR::InstId /*inst_id*/,
  116. SemIR::BranchIf inst) -> void {
  117. llvm::Value* cond = context.GetValue(inst.cond_id);
  118. llvm::BasicBlock* then_block = context.GetBlock(inst.target_id);
  119. llvm::BasicBlock* else_block = context.MakeSyntheticBlock();
  120. context.builder().CreateCondBr(cond, then_block, else_block);
  121. context.builder().SetInsertPoint(else_block);
  122. }
  123. auto HandleBranchWithArg(FunctionContext& context, SemIR::InstId /*inst_id*/,
  124. SemIR::BranchWithArg inst) -> void {
  125. llvm::Value* arg = context.GetValue(inst.arg_id);
  126. SemIR::TypeId arg_type_id =
  127. context.sem_ir().insts().Get(inst.arg_id).type_id();
  128. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  129. // We only do this for a block that we know will only have a single
  130. // predecessor, so that we can correctly populate the predecessors of the
  131. // PHINode.
  132. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  133. llvm::BasicBlock* phi_predecessor = block;
  134. if (block->empty() && context.IsCurrentSyntheticBlock(block) &&
  135. context.TryToReuseBlock(inst.target_id, block)) {
  136. // Reuse this block as the branch target.
  137. phi_predecessor = block->getSinglePredecessor();
  138. CARBON_CHECK(phi_predecessor)
  139. << "Synthetic block did not have a single predecessor";
  140. } else {
  141. context.builder().CreateBr(context.GetBlock(inst.target_id));
  142. }
  143. context.GetBlockArg(inst.target_id, arg_type_id)
  144. ->addIncoming(arg, phi_predecessor);
  145. context.builder().ClearInsertionPoint();
  146. }
  147. auto HandleConverted(FunctionContext& context, SemIR::InstId inst_id,
  148. SemIR::Converted inst) -> void {
  149. context.SetLocal(inst_id, context.GetValue(inst.result_id));
  150. }
  151. auto HandleDeref(FunctionContext& context, SemIR::InstId inst_id,
  152. SemIR::Deref inst) -> void {
  153. context.SetLocal(inst_id, context.GetValue(inst.pointer_id));
  154. }
  155. auto HandleFunctionDecl(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  156. SemIR::FunctionDecl inst) -> void {
  157. FatalErrorIfEncountered(inst);
  158. }
  159. auto HandleImplDecl(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  160. SemIR::ImplDecl inst) -> void {
  161. FatalErrorIfEncountered(inst);
  162. }
  163. auto HandleImportDecl(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  164. SemIR::ImportDecl inst) -> void {
  165. FatalErrorIfEncountered(inst);
  166. }
  167. auto HandleImportRefUnloaded(FunctionContext& /*context*/,
  168. SemIR::InstId /*inst_id*/,
  169. SemIR::ImportRefUnloaded inst) -> void {
  170. FatalErrorIfEncountered(inst);
  171. }
  172. auto HandleImportRefLoaded(FunctionContext& /*context*/,
  173. SemIR::InstId /*inst_id*/,
  174. SemIR::ImportRefLoaded inst) -> void {
  175. FatalErrorIfEncountered(inst);
  176. }
  177. auto HandleInitializeFrom(FunctionContext& context, SemIR::InstId /*inst_id*/,
  178. SemIR::InitializeFrom inst) -> void {
  179. auto storage_type_id = context.sem_ir().insts().Get(inst.dest_id).type_id();
  180. context.FinishInit(storage_type_id, inst.dest_id, inst.src_id);
  181. }
  182. auto HandleInterfaceDecl(FunctionContext& /*context*/,
  183. SemIR::InstId /*inst_id*/, SemIR::InterfaceDecl inst)
  184. -> void {
  185. FatalErrorIfEncountered(inst);
  186. }
  187. auto HandleInterfaceWitness(FunctionContext& /*context*/,
  188. SemIR::InstId /*inst_id*/,
  189. SemIR::InterfaceWitness inst) -> void {
  190. FatalErrorIfEncountered(inst);
  191. }
  192. auto HandleInterfaceWitnessAccess(FunctionContext& /*context*/,
  193. SemIR::InstId /*inst_id*/,
  194. SemIR::InterfaceWitnessAccess inst) -> void {
  195. FatalErrorIfEncountered(inst);
  196. }
  197. auto HandleNameRef(FunctionContext& context, SemIR::InstId inst_id,
  198. SemIR::NameRef inst) -> void {
  199. auto type_inst_id = context.sem_ir().types().GetInstId(inst.type_id);
  200. if (type_inst_id == SemIR::InstId::BuiltinNamespaceType) {
  201. return;
  202. }
  203. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  204. }
  205. auto HandleParam(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  206. SemIR::Param /*inst*/) -> void {
  207. CARBON_FATAL() << "Parameters should be lowered by `BuildFunctionDefinition`";
  208. }
  209. auto HandleReturn(FunctionContext& context, SemIR::InstId /*inst_id*/,
  210. SemIR::Return /*inst*/) -> void {
  211. context.builder().CreateRetVoid();
  212. }
  213. auto HandleReturnExpr(FunctionContext& context, SemIR::InstId /*inst_id*/,
  214. SemIR::ReturnExpr inst) -> void {
  215. auto result_type_id = context.sem_ir().insts().Get(inst.expr_id).type_id();
  216. switch (SemIR::GetInitRepr(context.sem_ir(), result_type_id).kind) {
  217. case SemIR::InitRepr::None:
  218. // Nothing to return.
  219. context.builder().CreateRetVoid();
  220. return;
  221. case SemIR::InitRepr::InPlace:
  222. context.FinishInit(result_type_id, inst.dest_id, inst.expr_id);
  223. context.builder().CreateRetVoid();
  224. return;
  225. case SemIR::InitRepr::ByCopy:
  226. // The expression produces the value representation for the type.
  227. context.builder().CreateRet(context.GetValue(inst.expr_id));
  228. return;
  229. }
  230. }
  231. auto HandleSpliceBlock(FunctionContext& context, SemIR::InstId inst_id,
  232. SemIR::SpliceBlock inst) -> void {
  233. context.LowerBlock(inst.block_id);
  234. context.SetLocal(inst_id, context.GetValue(inst.result_id));
  235. }
  236. auto HandleUnaryOperatorNot(FunctionContext& context, SemIR::InstId inst_id,
  237. SemIR::UnaryOperatorNot inst) -> void {
  238. context.SetLocal(
  239. inst_id, context.builder().CreateNot(context.GetValue(inst.operand_id)));
  240. }
  241. auto HandleVarStorage(FunctionContext& context, SemIR::InstId inst_id,
  242. SemIR::VarStorage inst) -> void {
  243. context.SetLocal(inst_id,
  244. context.builder().CreateAlloca(context.GetType(inst.type_id),
  245. /*ArraySize=*/nullptr));
  246. }
  247. } // namespace Carbon::Lower