handle.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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/STLExtras.h"
  5. #include "llvm/ADT/Sequence.h"
  6. #include "toolchain/lower/function_context.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. #include "toolchain/sem_ir/inst_kind.h"
  9. namespace Carbon::Lower {
  10. auto HandleCrossReference(FunctionContext& /*context*/,
  11. SemIR::InstId /*inst_id*/, SemIR::CrossReference inst)
  12. -> void {
  13. CARBON_FATAL() << "TODO: Add support: " << inst;
  14. }
  15. auto HandleAddressOf(FunctionContext& context, SemIR::InstId inst_id,
  16. SemIR::AddressOf inst) -> void {
  17. context.SetLocal(inst_id, context.GetLocal(inst.lvalue_id));
  18. }
  19. auto HandleArrayIndex(FunctionContext& context, SemIR::InstId inst_id,
  20. SemIR::ArrayIndex inst) -> void {
  21. auto* array_value = context.GetLocal(inst.array_id);
  22. auto* llvm_type =
  23. context.GetType(context.sem_ir().insts().Get(inst.array_id).type_id());
  24. llvm::Value* indexes[2] = {
  25. llvm::ConstantInt::get(llvm::Type::getInt32Ty(context.llvm_context()), 0),
  26. context.GetLocal(inst.index_id)};
  27. context.SetLocal(inst_id,
  28. context.builder().CreateInBoundsGEP(llvm_type, array_value,
  29. indexes, "array.index"));
  30. }
  31. auto HandleArrayInit(FunctionContext& context, SemIR::InstId inst_id,
  32. SemIR::ArrayInit inst) -> void {
  33. // The result of initialization is the return slot of the initializer.
  34. context.SetLocal(inst_id,
  35. context.GetLocal(context.sem_ir()
  36. .inst_blocks()
  37. .Get(inst.inits_and_return_slot_id)
  38. .back()));
  39. }
  40. auto HandleAssign(FunctionContext& context, SemIR::InstId /*inst_id*/,
  41. SemIR::Assign inst) -> void {
  42. auto storage_type_id = context.sem_ir().insts().Get(inst.lhs_id).type_id();
  43. context.FinishInitialization(storage_type_id, inst.lhs_id, inst.rhs_id);
  44. }
  45. auto HandleBinaryOperatorAdd(FunctionContext& /*context*/,
  46. SemIR::InstId /*inst_id*/,
  47. SemIR::BinaryOperatorAdd inst) -> void {
  48. CARBON_FATAL() << "TODO: Add support: " << inst;
  49. }
  50. auto HandleBindName(FunctionContext& context, SemIR::InstId inst_id,
  51. SemIR::BindName inst) -> void {
  52. context.SetLocal(inst_id, context.GetLocal(inst.value_id));
  53. }
  54. auto HandleBlockArg(FunctionContext& context, SemIR::InstId inst_id,
  55. SemIR::BlockArg inst) -> void {
  56. context.SetLocal(inst_id, context.GetBlockArg(inst.block_id, inst.type_id));
  57. }
  58. auto HandleBoolLiteral(FunctionContext& context, SemIR::InstId inst_id,
  59. SemIR::BoolLiteral inst) -> void {
  60. llvm::Value* v =
  61. llvm::ConstantInt::get(context.builder().getInt1Ty(), inst.value.index);
  62. context.SetLocal(inst_id, v);
  63. }
  64. auto HandleBoundMethod(FunctionContext& context, SemIR::InstId inst_id,
  65. SemIR::BoundMethod inst) -> void {
  66. // Propagate just the function; the object is separately provided to the
  67. // enclosing call as an implicit argument.
  68. context.SetLocal(inst_id, context.GetLocalOrGlobal(inst.function_id));
  69. }
  70. auto HandleBranch(FunctionContext& context, SemIR::InstId /*inst_id*/,
  71. SemIR::Branch inst) -> void {
  72. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  73. // TODO: Don't do this if it would remove a loop preheader block.
  74. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  75. if (block->empty() && context.TryToReuseBlock(inst.target_id, block)) {
  76. // Reuse this block as the branch target.
  77. } else {
  78. context.builder().CreateBr(context.GetBlock(inst.target_id));
  79. }
  80. context.builder().ClearInsertionPoint();
  81. }
  82. auto HandleBranchIf(FunctionContext& context, SemIR::InstId /*inst_id*/,
  83. SemIR::BranchIf inst) -> void {
  84. llvm::Value* cond = context.GetLocal(inst.cond_id);
  85. llvm::BasicBlock* then_block = context.GetBlock(inst.target_id);
  86. llvm::BasicBlock* else_block = context.CreateSyntheticBlock();
  87. context.builder().CreateCondBr(cond, then_block, else_block);
  88. context.builder().SetInsertPoint(else_block);
  89. }
  90. auto HandleBranchWithArg(FunctionContext& context, SemIR::InstId /*inst_id*/,
  91. SemIR::BranchWithArg inst) -> void {
  92. llvm::Value* arg = context.GetLocal(inst.arg_id);
  93. SemIR::TypeId arg_type_id =
  94. context.sem_ir().insts().Get(inst.arg_id).type_id();
  95. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  96. // We only do this for a block that we know will only have a single
  97. // predecessor, so that we can correctly populate the predecessors of the
  98. // PHINode.
  99. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  100. llvm::BasicBlock* phi_predecessor = block;
  101. if (block->empty() && context.IsCurrentSyntheticBlock(block) &&
  102. context.TryToReuseBlock(inst.target_id, block)) {
  103. // Reuse this block as the branch target.
  104. phi_predecessor = block->getSinglePredecessor();
  105. CARBON_CHECK(phi_predecessor)
  106. << "Synthetic block did not have a single predecessor";
  107. } else {
  108. context.builder().CreateBr(context.GetBlock(inst.target_id));
  109. }
  110. context.GetBlockArg(inst.target_id, arg_type_id)
  111. ->addIncoming(arg, phi_predecessor);
  112. context.builder().ClearInsertionPoint();
  113. }
  114. auto HandleBuiltin(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  115. SemIR::Builtin inst) -> void {
  116. CARBON_FATAL() << "TODO: Add support: " << inst;
  117. }
  118. auto HandleCall(FunctionContext& context, SemIR::InstId inst_id,
  119. SemIR::Call inst) -> void {
  120. auto* callee = llvm::cast<llvm::Function>(context.GetLocal(inst.callee_id));
  121. std::vector<llvm::Value*> args;
  122. llvm::ArrayRef<SemIR::InstId> arg_ids =
  123. context.sem_ir().inst_blocks().Get(inst.args_id);
  124. if (SemIR::GetInitializingRepresentation(context.sem_ir(), inst.type_id)
  125. .has_return_slot()) {
  126. args.push_back(context.GetLocal(arg_ids.back()));
  127. arg_ids = arg_ids.drop_back();
  128. }
  129. for (auto arg_id : arg_ids) {
  130. auto arg_type_id = context.sem_ir().insts().Get(arg_id).type_id();
  131. if (SemIR::GetValueRepresentation(context.sem_ir(), arg_type_id).kind !=
  132. SemIR::ValueRepresentation::None) {
  133. args.push_back(context.GetLocal(arg_id));
  134. }
  135. }
  136. auto* call = context.builder().CreateCall(callee, args);
  137. context.SetLocal(inst_id, call);
  138. // Name the call's result the same as the callee.
  139. // TODO: Is this a helpful name?
  140. if (!call->getType()->isVoidTy()) {
  141. call->setName(callee->getName());
  142. }
  143. }
  144. auto HandleClassDeclaration(FunctionContext& /*context*/,
  145. SemIR::InstId /*inst_id*/,
  146. SemIR::ClassDeclaration /*inst*/) -> void {
  147. // No action to perform.
  148. }
  149. // Extracts an element of an aggregate, such as a struct, tuple, or class, by
  150. // index. Depending on the expression category and value representation of the
  151. // aggregate input, this will either produce a value or a reference.
  152. static auto GetAggregateElement(FunctionContext& context,
  153. SemIR::InstId aggr_inst_id,
  154. SemIR::MemberIndex idx,
  155. SemIR::TypeId result_type_id, llvm::Twine name)
  156. -> llvm::Value* {
  157. auto aggr_inst = context.sem_ir().insts().Get(aggr_inst_id);
  158. auto* aggr_value = context.GetLocal(aggr_inst_id);
  159. switch (SemIR::GetExpressionCategory(context.sem_ir(), aggr_inst_id)) {
  160. case SemIR::ExpressionCategory::Error:
  161. case SemIR::ExpressionCategory::NotExpression:
  162. case SemIR::ExpressionCategory::Initializing:
  163. case SemIR::ExpressionCategory::Mixed:
  164. CARBON_FATAL() << "Unexpected expression category for aggregate access";
  165. case SemIR::ExpressionCategory::Value: {
  166. auto value_rep =
  167. SemIR::GetValueRepresentation(context.sem_ir(), aggr_inst.type_id());
  168. CARBON_CHECK(value_rep.aggregate_kind !=
  169. SemIR::ValueRepresentation::NotAggregate)
  170. << "aggregate type should have aggregate value representation";
  171. switch (value_rep.kind) {
  172. case SemIR::ValueRepresentation::Unknown:
  173. CARBON_FATAL() << "Lowering access to incomplete aggregate type";
  174. case SemIR::ValueRepresentation::None:
  175. return aggr_value;
  176. case SemIR::ValueRepresentation::Copy:
  177. // We are holding the values of the aggregate directly, elementwise.
  178. return context.builder().CreateExtractValue(aggr_value, idx.index,
  179. name);
  180. case SemIR::ValueRepresentation::Pointer: {
  181. // The value representation is a pointer to an aggregate that we want
  182. // to index into.
  183. auto pointee_type_id =
  184. context.sem_ir().GetPointeeType(value_rep.type_id);
  185. auto* value_type = context.GetType(pointee_type_id);
  186. auto* elem_ptr = context.builder().CreateStructGEP(
  187. value_type, aggr_value, idx.index, name);
  188. if (!value_rep.elements_are_values()) {
  189. // `elem_ptr` points to an object representation, which is our
  190. // result.
  191. return elem_ptr;
  192. }
  193. // `elem_ptr` points to a value representation. Load it.
  194. auto result_value_type_id =
  195. SemIR::GetValueRepresentation(context.sem_ir(), result_type_id)
  196. .type_id;
  197. return context.builder().CreateLoad(
  198. context.GetType(result_value_type_id), elem_ptr, name + ".load");
  199. }
  200. case SemIR::ValueRepresentation::Custom:
  201. CARBON_FATAL()
  202. << "Aggregate should never have custom value representation";
  203. }
  204. }
  205. case SemIR::ExpressionCategory::DurableReference:
  206. case SemIR::ExpressionCategory::EphemeralReference: {
  207. // Just locate the aggregate element.
  208. auto* aggr_type = context.GetType(aggr_inst.type_id());
  209. return context.builder().CreateStructGEP(aggr_type, aggr_value, idx.index,
  210. name);
  211. }
  212. }
  213. }
  214. static auto GetStructFieldName(FunctionContext& context,
  215. SemIR::TypeId struct_type_id,
  216. SemIR::MemberIndex index) -> llvm::StringRef {
  217. auto fields = context.sem_ir().inst_blocks().Get(
  218. context.sem_ir()
  219. .insts()
  220. .GetAs<SemIR::StructType>(
  221. context.sem_ir().types().Get(struct_type_id).inst_id)
  222. .fields_id);
  223. auto field = context.sem_ir().insts().GetAs<SemIR::StructTypeField>(
  224. fields[index.index]);
  225. return context.sem_ir().identifiers().Get(field.name_id);
  226. }
  227. auto HandleClassFieldAccess(FunctionContext& context, SemIR::InstId inst_id,
  228. SemIR::ClassFieldAccess inst) -> void {
  229. // Find the class that we're performing access into.
  230. auto class_type_id = context.sem_ir().insts().Get(inst.base_id).type_id();
  231. auto class_id =
  232. context.sem_ir()
  233. .insts()
  234. .GetAs<SemIR::ClassType>(
  235. context.sem_ir().GetTypeAllowBuiltinTypes(class_type_id))
  236. .class_id;
  237. auto& class_info = context.sem_ir().classes().Get(class_id);
  238. // Translate the class field access into a struct access on the object
  239. // representation.
  240. context.SetLocal(
  241. inst_id,
  242. GetAggregateElement(
  243. context, inst.base_id, inst.index, inst.type_id,
  244. GetStructFieldName(context, class_info.object_representation_id,
  245. inst.index)));
  246. }
  247. auto HandleDereference(FunctionContext& context, SemIR::InstId inst_id,
  248. SemIR::Dereference inst) -> void {
  249. context.SetLocal(inst_id, context.GetLocal(inst.pointer_id));
  250. }
  251. auto HandleField(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  252. SemIR::Field /*inst*/) -> void {
  253. // No action to perform.
  254. }
  255. auto HandleFunctionDeclaration(FunctionContext& /*context*/,
  256. SemIR::InstId /*inst_id*/,
  257. SemIR::FunctionDeclaration inst) -> void {
  258. CARBON_FATAL()
  259. << "Should not be encountered. If that changes, we may want to change "
  260. "higher-level logic to skip them rather than calling this. "
  261. << inst;
  262. }
  263. auto HandleInitializeFrom(FunctionContext& context, SemIR::InstId /*inst_id*/,
  264. SemIR::InitializeFrom inst) -> void {
  265. auto storage_type_id = context.sem_ir().insts().Get(inst.dest_id).type_id();
  266. context.FinishInitialization(storage_type_id, inst.dest_id, inst.src_id);
  267. }
  268. auto HandleIntegerLiteral(FunctionContext& context, SemIR::InstId inst_id,
  269. SemIR::IntegerLiteral inst) -> void {
  270. const llvm::APInt& i = context.sem_ir().integers().Get(inst.integer_id);
  271. // TODO: This won't offer correct semantics, but seems close enough for now.
  272. llvm::Value* v =
  273. llvm::ConstantInt::get(context.builder().getInt32Ty(), i.getZExtValue());
  274. context.SetLocal(inst_id, v);
  275. }
  276. auto HandleNameReference(FunctionContext& context, SemIR::InstId inst_id,
  277. SemIR::NameReference inst) -> void {
  278. auto type_inst_id = context.sem_ir().GetTypeAllowBuiltinTypes(inst.type_id);
  279. if (type_inst_id == SemIR::InstId::BuiltinNamespaceType) {
  280. return;
  281. }
  282. context.SetLocal(inst_id, context.GetLocalOrGlobal(inst.value_id));
  283. }
  284. auto HandleNamespace(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  285. SemIR::Namespace inst) -> void {
  286. CARBON_FATAL()
  287. << "Should not be encountered. If that changes, we may want to change "
  288. "higher-level logic to skip them rather than calling this. "
  289. << inst;
  290. }
  291. auto HandleNoOp(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  292. SemIR::NoOp /*inst*/) -> void {
  293. // No action to take.
  294. }
  295. auto HandleParameter(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  296. SemIR::Parameter /*inst*/) -> void {
  297. CARBON_FATAL() << "Parameters should be lowered by `BuildFunctionDefinition`";
  298. }
  299. auto HandleRealLiteral(FunctionContext& context, SemIR::InstId inst_id,
  300. SemIR::RealLiteral inst) -> void {
  301. const Real& real = context.sem_ir().reals().Get(inst.real_id);
  302. // TODO: This will probably have overflow issues, and should be fixed.
  303. double val =
  304. real.mantissa.getZExtValue() *
  305. std::pow((real.is_decimal ? 10 : 2), real.exponent.getSExtValue());
  306. llvm::APFloat llvm_val(val);
  307. context.SetLocal(inst_id, llvm::ConstantFP::get(
  308. context.builder().getDoubleTy(), llvm_val));
  309. }
  310. auto HandleReturn(FunctionContext& context, SemIR::InstId /*inst_id*/,
  311. SemIR::Return /*inst*/) -> void {
  312. context.builder().CreateRetVoid();
  313. }
  314. auto HandleReturnExpression(FunctionContext& context, SemIR::InstId /*inst_id*/,
  315. SemIR::ReturnExpression inst) -> void {
  316. switch (SemIR::GetInitializingRepresentation(
  317. context.sem_ir(),
  318. context.sem_ir().insts().Get(inst.expr_id).type_id())
  319. .kind) {
  320. case SemIR::InitializingRepresentation::None:
  321. case SemIR::InitializingRepresentation::InPlace:
  322. // Nothing to return.
  323. context.builder().CreateRetVoid();
  324. return;
  325. case SemIR::InitializingRepresentation::ByCopy:
  326. // The expression produces the value representation for the type.
  327. context.builder().CreateRet(context.GetLocal(inst.expr_id));
  328. return;
  329. }
  330. }
  331. auto HandleSelfParameter(FunctionContext& /*context*/,
  332. SemIR::InstId /*inst_id*/,
  333. SemIR::SelfParameter /*inst*/) -> void {
  334. CARBON_FATAL() << "Parameters should be lowered by `BuildFunctionDefinition`";
  335. }
  336. auto HandleSpliceBlock(FunctionContext& context, SemIR::InstId inst_id,
  337. SemIR::SpliceBlock inst) -> void {
  338. context.LowerBlock(inst.block_id);
  339. context.SetLocal(inst_id, context.GetLocal(inst.result_id));
  340. }
  341. auto HandleStringLiteral(FunctionContext& /*context*/,
  342. SemIR::InstId /*inst_id*/, SemIR::StringLiteral inst)
  343. -> void {
  344. CARBON_FATAL() << "TODO: Add support: " << inst;
  345. }
  346. auto HandleStructAccess(FunctionContext& context, SemIR::InstId inst_id,
  347. SemIR::StructAccess inst) -> void {
  348. auto struct_type_id = context.sem_ir().insts().Get(inst.struct_id).type_id();
  349. context.SetLocal(
  350. inst_id, GetAggregateElement(
  351. context, inst.struct_id, inst.index, inst.type_id,
  352. GetStructFieldName(context, struct_type_id, inst.index)));
  353. }
  354. auto HandleStructLiteral(FunctionContext& /*context*/,
  355. SemIR::InstId /*inst_id*/,
  356. SemIR::StructLiteral /*inst*/) -> void {
  357. // A StructLiteral should always be converted to a StructInit or StructValue
  358. // if its value is needed.
  359. }
  360. // Emits the value representation for a struct or tuple whose elements are the
  361. // contents of `refs_id`.
  362. auto EmitStructOrTupleValueRepresentation(FunctionContext& context,
  363. SemIR::TypeId type_id,
  364. SemIR::InstBlockId refs_id,
  365. llvm::Twine name) -> llvm::Value* {
  366. auto value_rep = SemIR::GetValueRepresentation(context.sem_ir(), type_id);
  367. switch (value_rep.kind) {
  368. case SemIR::ValueRepresentation::Unknown:
  369. CARBON_FATAL() << "Incomplete aggregate type in lowering";
  370. case SemIR::ValueRepresentation::None:
  371. // TODO: Add a helper to get a "no value representation" value.
  372. return llvm::PoisonValue::get(context.GetType(value_rep.type_id));
  373. case SemIR::ValueRepresentation::Copy: {
  374. auto refs = context.sem_ir().inst_blocks().Get(refs_id);
  375. CARBON_CHECK(refs.size() == 1)
  376. << "Unexpected size for aggregate with by-copy value representation";
  377. // TODO: Remove the LLVM StructType wrapper in this case, so we don't
  378. // need this `insert_value` wrapping.
  379. return context.builder().CreateInsertValue(
  380. llvm::PoisonValue::get(context.GetType(value_rep.type_id)),
  381. context.GetLocal(refs[0]), {0});
  382. }
  383. case SemIR::ValueRepresentation::Pointer: {
  384. auto pointee_type_id = context.sem_ir().GetPointeeType(value_rep.type_id);
  385. auto* llvm_value_rep_type = context.GetType(pointee_type_id);
  386. // Write the value representation to a local alloca so we can produce a
  387. // pointer to it as the value representation of the struct or tuple.
  388. auto* alloca =
  389. context.builder().CreateAlloca(llvm_value_rep_type,
  390. /*ArraySize=*/nullptr, name);
  391. for (auto [i, ref] :
  392. llvm::enumerate(context.sem_ir().inst_blocks().Get(refs_id))) {
  393. context.builder().CreateStore(
  394. context.GetLocal(ref),
  395. context.builder().CreateStructGEP(llvm_value_rep_type, alloca, i));
  396. }
  397. return alloca;
  398. }
  399. case SemIR::ValueRepresentation::Custom:
  400. CARBON_FATAL()
  401. << "Aggregate should never have custom value representation";
  402. }
  403. }
  404. auto HandleStructInit(FunctionContext& context, SemIR::InstId inst_id,
  405. SemIR::StructInit inst) -> void {
  406. auto* llvm_type = context.GetType(inst.type_id);
  407. switch (SemIR::GetInitializingRepresentation(context.sem_ir(), inst.type_id)
  408. .kind) {
  409. case SemIR::InitializingRepresentation::None:
  410. case SemIR::InitializingRepresentation::InPlace:
  411. // TODO: Add a helper to poison a value slot.
  412. context.SetLocal(inst_id, llvm::PoisonValue::get(llvm_type));
  413. break;
  414. case SemIR::InitializingRepresentation::ByCopy: {
  415. context.SetLocal(
  416. inst_id, EmitStructOrTupleValueRepresentation(
  417. context, inst.type_id, inst.elements_id, "struct.init"));
  418. break;
  419. }
  420. }
  421. }
  422. auto HandleStructValue(FunctionContext& context, SemIR::InstId inst_id,
  423. SemIR::StructValue inst) -> void {
  424. context.SetLocal(inst_id,
  425. EmitStructOrTupleValueRepresentation(
  426. context, inst.type_id, inst.elements_id, "struct"));
  427. }
  428. auto HandleStructTypeField(FunctionContext& /*context*/,
  429. SemIR::InstId /*inst_id*/,
  430. SemIR::StructTypeField /*inst*/) -> void {
  431. // No action to take.
  432. }
  433. auto HandleTupleAccess(FunctionContext& context, SemIR::InstId inst_id,
  434. SemIR::TupleAccess inst) -> void {
  435. context.SetLocal(inst_id,
  436. GetAggregateElement(context, inst.tuple_id, inst.index,
  437. inst.type_id, "tuple.elem"));
  438. }
  439. auto HandleTupleIndex(FunctionContext& context, SemIR::InstId inst_id,
  440. SemIR::TupleIndex inst) -> void {
  441. auto index_inst =
  442. context.sem_ir().insts().GetAs<SemIR::IntegerLiteral>(inst.index_id);
  443. auto index =
  444. context.sem_ir().integers().Get(index_inst.integer_id).getZExtValue();
  445. context.SetLocal(inst_id, GetAggregateElement(context, inst.tuple_id,
  446. SemIR::MemberIndex(index),
  447. inst.type_id, "tuple.index"));
  448. }
  449. auto HandleTupleLiteral(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  450. SemIR::TupleLiteral /*inst*/) -> void {
  451. // A TupleLiteral should always be converted to a TupleInit or TupleValue if
  452. // its value is needed.
  453. }
  454. auto HandleTupleInit(FunctionContext& context, SemIR::InstId inst_id,
  455. SemIR::TupleInit inst) -> void {
  456. auto* llvm_type = context.GetType(inst.type_id);
  457. switch (SemIR::GetInitializingRepresentation(context.sem_ir(), inst.type_id)
  458. .kind) {
  459. case SemIR::InitializingRepresentation::None:
  460. case SemIR::InitializingRepresentation::InPlace:
  461. // TODO: Add a helper to poison a value slot.
  462. context.SetLocal(inst_id, llvm::PoisonValue::get(llvm_type));
  463. break;
  464. case SemIR::InitializingRepresentation::ByCopy: {
  465. context.SetLocal(
  466. inst_id, EmitStructOrTupleValueRepresentation(
  467. context, inst.type_id, inst.elements_id, "tuple.init"));
  468. break;
  469. }
  470. }
  471. }
  472. auto HandleTupleValue(FunctionContext& context, SemIR::InstId inst_id,
  473. SemIR::TupleValue inst) -> void {
  474. context.SetLocal(
  475. inst_id, EmitStructOrTupleValueRepresentation(context, inst.type_id,
  476. inst.elements_id, "tuple"));
  477. }
  478. auto HandleUnaryOperatorNot(FunctionContext& context, SemIR::InstId inst_id,
  479. SemIR::UnaryOperatorNot inst) -> void {
  480. context.SetLocal(
  481. inst_id, context.builder().CreateNot(context.GetLocal(inst.operand_id)));
  482. }
  483. auto HandleVarStorage(FunctionContext& context, SemIR::InstId inst_id,
  484. SemIR::VarStorage inst) -> void {
  485. // TODO: Eventually this name will be optional, and we'll want to provide
  486. // something like `var` as a default. However, that's not possible right now
  487. // so cannot be tested.
  488. auto name = context.sem_ir().identifiers().Get(inst.name_id);
  489. auto* alloca = context.builder().CreateAlloca(context.GetType(inst.type_id),
  490. /*ArraySize=*/nullptr, name);
  491. context.SetLocal(inst_id, alloca);
  492. }
  493. } // namespace Carbon::Lower