handle.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. // Returns whether this instruction names a namespace.
  20. static auto IsNamespace(FunctionContext& context, SemIR::InstId inst_id)
  21. -> bool {
  22. // Note, we don't use context.GetTypeOfInst here. An instruction can't change
  23. // from being a non-namespace in a generic to being a namespace in a specific,
  24. // because namespace names are not first-class.
  25. auto type_inst_id = context.sem_ir().types().GetInstId(
  26. context.sem_ir().insts().Get(inst_id).type_id());
  27. return type_inst_id == SemIR::NamespaceType::TypeInstId;
  28. }
  29. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  30. SemIR::AddrOf inst) -> void {
  31. context.SetLocal(inst_id, context.GetValue(inst.lvalue_id));
  32. }
  33. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  34. SemIR::ArrayIndex inst) -> void {
  35. auto* array_value = context.GetValue(inst.array_id);
  36. auto* llvm_type = context.GetTypeOfInst(inst.array_id);
  37. // The index in an `ArrayIndex` can be of any integer type, including
  38. // IntLiteral. If it is an IntLiteral, its value representation is empty, so
  39. // create a ConstantInt from its SemIR value directly.
  40. llvm::Value* index;
  41. auto index_type = context.GetTypeIdOfInst(inst.index_id);
  42. if (index_type.file->types().GetInstId(index_type.type_id) ==
  43. SemIR::IntLiteralType::TypeInstId) {
  44. auto value = context.sem_ir().insts().GetAs<SemIR::IntValue>(
  45. context.sem_ir().constant_values().GetConstantInstId(inst.index_id));
  46. const auto& apint_value = context.sem_ir().ints().Get(value.int_id);
  47. context.AddIntToCurrentFingerprint(apint_value.getSExtValue());
  48. index = llvm::ConstantInt::get(context.llvm_context(), apint_value);
  49. } else {
  50. context.AddIntToCurrentFingerprint(-1);
  51. index = context.GetValue(inst.index_id);
  52. }
  53. llvm::Value* indexes[2] = {
  54. llvm::ConstantInt::get(llvm::Type::getInt32Ty(context.llvm_context()), 0),
  55. index};
  56. context.SetLocal(inst_id,
  57. context.builder().CreateInBoundsGEP(llvm_type, array_value,
  58. indexes, "array.index"));
  59. }
  60. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  61. SemIR::ArrayInit inst) -> void {
  62. // The result of initialization is the return slot of the initializer.
  63. context.SetLocal(inst_id, context.GetValue(inst.dest_id));
  64. }
  65. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  66. SemIR::AsCompatible inst) -> void {
  67. context.SetLocal(inst_id, context.GetValue(inst.source_id));
  68. }
  69. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  70. SemIR::Assign inst) -> void {
  71. context.FinishInit(context.GetTypeIdOfInst(inst.lhs_id), inst.lhs_id,
  72. inst.rhs_id);
  73. }
  74. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  75. SemIR::BindAlias inst) -> void {
  76. if (IsNamespace(context, inst_id)) {
  77. return;
  78. }
  79. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  80. }
  81. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  82. SemIR::ExportDecl inst) -> void {
  83. if (IsNamespace(context, inst_id)) {
  84. return;
  85. }
  86. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  87. }
  88. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  89. SemIR::BindName inst) -> void {
  90. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  91. }
  92. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  93. SemIR::BindSymbolicName inst) -> void {
  94. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  95. }
  96. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  97. SemIR::BlockArg inst) -> void {
  98. context.SetLocal(
  99. inst_id,
  100. context.GetBlockArg(inst.block_id, context.GetTypeIdOfInst(inst_id)));
  101. }
  102. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  103. SemIR::BoundMethod inst) -> void {
  104. // Propagate just the function; the object is separately provided to the
  105. // enclosing call as an implicit argument.
  106. context.SetLocal(inst_id, context.GetValue(inst.function_decl_id));
  107. }
  108. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  109. SemIR::Branch inst) -> void {
  110. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  111. // TODO: Don't do this if it would remove a loop preheader block.
  112. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  113. if (block->empty() && context.TryToReuseBlock(inst.target_id, block)) {
  114. // Reuse this block as the branch target.
  115. } else {
  116. context.builder().CreateBr(context.GetBlock(inst.target_id));
  117. }
  118. context.builder().ClearInsertionPoint();
  119. }
  120. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  121. SemIR::BranchIf inst) -> void {
  122. llvm::Value* cond = context.GetValue(inst.cond_id);
  123. llvm::BasicBlock* then_block = context.GetBlock(inst.target_id);
  124. llvm::BasicBlock* else_block = context.MakeSyntheticBlock();
  125. context.builder().CreateCondBr(cond, then_block, else_block);
  126. context.builder().SetInsertPoint(else_block);
  127. }
  128. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  129. SemIR::BranchWithArg inst) -> void {
  130. llvm::Value* arg = context.GetValue(inst.arg_id);
  131. auto arg_type = context.GetTypeIdOfInst(inst.arg_id);
  132. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  133. // We only do this for a block that we know will only have a single
  134. // predecessor, so that we can correctly populate the predecessors of the
  135. // PHINode.
  136. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  137. llvm::BasicBlock* phi_predecessor = block;
  138. if (block->empty() && context.IsCurrentSyntheticBlock(block) &&
  139. context.TryToReuseBlock(inst.target_id, block)) {
  140. // Reuse this block as the branch target.
  141. phi_predecessor = block->getSinglePredecessor();
  142. CARBON_CHECK(phi_predecessor,
  143. "Synthetic block did not have a single predecessor");
  144. } else {
  145. context.builder().CreateBr(context.GetBlock(inst.target_id));
  146. }
  147. context.GetBlockArg(inst.target_id, arg_type)
  148. ->addIncoming(arg, phi_predecessor);
  149. context.builder().ClearInsertionPoint();
  150. }
  151. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  152. SemIR::Converted inst) -> void {
  153. context.SetLocal(inst_id, context.GetValue(inst.result_id));
  154. }
  155. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  156. SemIR::Deref inst) -> void {
  157. context.SetLocal(inst_id, context.GetValue(inst.pointer_id));
  158. }
  159. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  160. SemIR::FacetAccessType /*inst*/) -> void {
  161. context.SetLocal(inst_id, context.GetTypeAsValue());
  162. }
  163. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  164. SemIR::InitializeFrom inst) -> void {
  165. context.FinishInit(context.GetTypeIdOfInst(inst.dest_id), inst.dest_id,
  166. inst.src_id);
  167. }
  168. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  169. SemIR::NameBindingDecl /*inst*/) -> void {
  170. // A NameBindingDecl is lowered by pattern matching.
  171. }
  172. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  173. SemIR::NameRef inst) -> void {
  174. if (IsNamespace(context, inst_id)) {
  175. return;
  176. }
  177. auto inner_inst_id = inst.value_id;
  178. if (auto bind_name =
  179. context.sem_ir().insts().TryGetAs<SemIR::BindName>(inner_inst_id)) {
  180. inner_inst_id = bind_name->value_id;
  181. }
  182. context.SetLocal(inst_id, context.GetValue(inner_inst_id));
  183. }
  184. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  185. SemIR::OutParam /*inst*/) -> void {
  186. // Parameters are lowered by `BuildFunctionDefinition`.
  187. }
  188. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  189. SemIR::RefParam /*inst*/) -> void {
  190. // Parameters are lowered by `BuildFunctionDefinition`.
  191. }
  192. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  193. SemIR::ValueParam /*inst*/) -> void {
  194. // Parameters are lowered by `BuildFunctionDefinition`.
  195. }
  196. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  197. SemIR::ReturnSlot inst) -> void {
  198. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  199. }
  200. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  201. SemIR::Return /*inst*/) -> void {
  202. context.builder().CreateRetVoid();
  203. }
  204. auto HandleInst(FunctionContext& context, SemIR::InstId /*inst_id*/,
  205. SemIR::ReturnExpr inst) -> void {
  206. auto result_type = context.GetTypeIdOfInst(inst.expr_id);
  207. switch (context.GetInitRepr(result_type).kind) {
  208. case SemIR::InitRepr::None:
  209. // Nothing to return.
  210. context.builder().CreateRetVoid();
  211. return;
  212. case SemIR::InitRepr::InPlace:
  213. context.FinishInit(result_type, inst.dest_id, inst.expr_id);
  214. context.builder().CreateRetVoid();
  215. return;
  216. case SemIR::InitRepr::ByCopy:
  217. // The expression produces the value representation for the type.
  218. context.builder().CreateRet(context.GetValue(inst.expr_id));
  219. return;
  220. case SemIR::InitRepr::Incomplete:
  221. CARBON_FATAL("Lowering return of incomplete type {0}",
  222. result_type.file->types().GetAsInst(result_type.type_id));
  223. case SemIR::InitRepr::Dependent:
  224. CARBON_FATAL("Lowering return of dependent type {0}",
  225. result_type.file->types().GetAsInst(result_type.type_id));
  226. }
  227. }
  228. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  229. SemIR::SpecificFunction /*inst*/) -> void {
  230. // Nothing to do. This value should never be consumed.
  231. }
  232. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  233. SemIR::SpecificImplFunction /*inst*/) -> void {
  234. // Nothing to do. This value should never be consumed.
  235. }
  236. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  237. SemIR::SpliceBlock inst) -> void {
  238. context.LowerBlockContents(inst.block_id);
  239. context.SetLocal(inst_id, context.GetValue(inst.result_id));
  240. }
  241. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  242. SemIR::SpliceInst /*inst*/) -> void {
  243. // TODO: Get the constant value of the spliced instruction from the current
  244. // specific, and lower the instruction in that constant value.
  245. CARBON_FATAL("Template lowering not implemented yet");
  246. }
  247. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  248. SemIR::UnaryOperatorNot inst) -> void {
  249. context.SetLocal(
  250. inst_id, context.builder().CreateNot(context.GetValue(inst.operand_id)));
  251. }
  252. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  253. SemIR::VarStorage /* inst */) -> void {
  254. context.SetLocal(inst_id,
  255. context.CreateAlloca(context.GetTypeOfInst(inst_id)));
  256. }
  257. } // namespace Carbon::Lower