handle_call.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 <algorithm>
  5. #include <vector>
  6. #include "common/raw_string_ostream.h"
  7. #include "llvm/IR/Type.h"
  8. #include "llvm/IR/Value.h"
  9. #include "toolchain/lower/function_context.h"
  10. #include "toolchain/sem_ir/builtin_function_kind.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. #include "toolchain/sem_ir/typed_insts.h"
  13. namespace Carbon::Lower {
  14. // Get the predicate to use for an `icmp` instruction generated for the
  15. // specified builtin.
  16. static auto GetBuiltinICmpPredicate(SemIR::BuiltinFunctionKind builtin_kind,
  17. bool is_signed)
  18. -> llvm::CmpInst::Predicate {
  19. switch (builtin_kind) {
  20. case SemIR::BuiltinFunctionKind::IntEq:
  21. case SemIR::BuiltinFunctionKind::BoolEq:
  22. return llvm::CmpInst::ICMP_EQ;
  23. case SemIR::BuiltinFunctionKind::IntNeq:
  24. case SemIR::BuiltinFunctionKind::BoolNeq:
  25. return llvm::CmpInst::ICMP_NE;
  26. case SemIR::BuiltinFunctionKind::IntLess:
  27. return is_signed ? llvm::CmpInst::ICMP_SLT : llvm::CmpInst::ICMP_ULT;
  28. case SemIR::BuiltinFunctionKind::IntLessEq:
  29. return is_signed ? llvm::CmpInst::ICMP_SLE : llvm::CmpInst::ICMP_ULE;
  30. case SemIR::BuiltinFunctionKind::IntGreater:
  31. return is_signed ? llvm::CmpInst::ICMP_SGT : llvm::CmpInst::ICMP_UGT;
  32. case SemIR::BuiltinFunctionKind::IntGreaterEq:
  33. return is_signed ? llvm::CmpInst::ICMP_SGE : llvm::CmpInst::ICMP_UGE;
  34. default:
  35. CARBON_FATAL("Unexpected builtin kind {0}", builtin_kind);
  36. }
  37. }
  38. // Get the predicate to use for an `fcmp` instruction generated for the
  39. // specified builtin.
  40. static auto GetBuiltinFCmpPredicate(SemIR::BuiltinFunctionKind builtin_kind)
  41. -> llvm::CmpInst::Predicate {
  42. switch (builtin_kind) {
  43. case SemIR::BuiltinFunctionKind::FloatEq:
  44. return llvm::CmpInst::FCMP_OEQ;
  45. case SemIR::BuiltinFunctionKind::FloatNeq:
  46. return llvm::CmpInst::FCMP_ONE;
  47. case SemIR::BuiltinFunctionKind::FloatLess:
  48. return llvm::CmpInst::FCMP_OLT;
  49. case SemIR::BuiltinFunctionKind::FloatLessEq:
  50. return llvm::CmpInst::FCMP_OLE;
  51. case SemIR::BuiltinFunctionKind::FloatGreater:
  52. return llvm::CmpInst::FCMP_OGT;
  53. case SemIR::BuiltinFunctionKind::FloatGreaterEq:
  54. return llvm::CmpInst::FCMP_OGE;
  55. default:
  56. CARBON_FATAL("Unexpected builtin kind {0}", builtin_kind);
  57. }
  58. }
  59. // Returns whether the specified instruction has a signed integer type.
  60. static auto IsSignedInt(FunctionContext& context, SemIR::InstId int_id)
  61. -> bool {
  62. auto [file, type_id] = context.GetTypeIdOfInst(int_id);
  63. return file->types().IsSignedInt(type_id);
  64. }
  65. // Creates a zext or sext instruction depending on the signedness of the
  66. // operand.
  67. static auto CreateExt(FunctionContext& context, llvm::Value* value,
  68. llvm::Type* type, bool is_signed,
  69. const llvm::Twine& name = "") -> llvm::Value* {
  70. return is_signed ? context.builder().CreateSExt(value, type, name)
  71. : context.builder().CreateZExt(value, type, name);
  72. }
  73. // Creates a zext, sext, or trunc instruction depending on the signedness of the
  74. // operand.
  75. static auto CreateExtOrTrunc(FunctionContext& context, llvm::Value* value,
  76. llvm::Type* type, bool is_signed,
  77. const llvm::Twine& name = "") -> llvm::Value* {
  78. return is_signed ? context.builder().CreateSExtOrTrunc(value, type, name)
  79. : context.builder().CreateZExtOrTrunc(value, type, name);
  80. }
  81. // Create a integer bit shift for a call to a builtin bit shift function.
  82. static auto CreateIntShift(FunctionContext& context,
  83. llvm::Instruction::BinaryOps bin_op,
  84. llvm::Value* lhs, llvm::Value* rhs) -> llvm::Value* {
  85. // Weirdly, LLVM requires the operands of bit shift operators to be of the
  86. // same type. We can always use the width of the LHS, because if the RHS
  87. // doesn't fit in that then the cast is out of range anyway. Zero-extending is
  88. // always fine because it's an error for the RHS to be negative.
  89. //
  90. // TODO: In a development build we should trap if the RHS is signed and
  91. // negative or greater than or equal to the number of bits in the left-hand
  92. // type.
  93. rhs = context.builder().CreateZExtOrTrunc(rhs, lhs->getType(), "rhs");
  94. return context.builder().CreateBinOp(bin_op, lhs, rhs);
  95. }
  96. // Handles a call to a builtin integer comparison operator.
  97. static auto HandleIntComparison(FunctionContext& context, SemIR::InstId inst_id,
  98. SemIR::BuiltinFunctionKind builtin_kind,
  99. SemIR::InstId lhs_id, SemIR::InstId rhs_id)
  100. -> void {
  101. llvm::Value* lhs = context.GetValue(lhs_id);
  102. llvm::Value* rhs = context.GetValue(rhs_id);
  103. const auto* lhs_type = cast<llvm::IntegerType>(lhs->getType());
  104. const auto* rhs_type = cast<llvm::IntegerType>(rhs->getType());
  105. // We perform a signed comparison if either operand is signed.
  106. bool lhs_signed = IsSignedInt(context, lhs_id);
  107. bool rhs_signed = IsSignedInt(context, rhs_id);
  108. bool cmp_signed = lhs_signed || rhs_signed;
  109. // Compute the width for the comparison. This is the smallest width that
  110. // fits both types, after widening them to include a sign bit if
  111. // necessary.
  112. auto width_for_cmp = [&](const llvm::IntegerType* type, bool is_signed) {
  113. unsigned width = type->getBitWidth();
  114. if (!is_signed && cmp_signed) {
  115. // We're performing a signed comparison but this input is unsigned.
  116. // Widen it by at least one bit to provide a sign bit.
  117. ++width;
  118. }
  119. return width;
  120. };
  121. // TODO: This might be an awkward size, such as 33 or 65 bits, for a
  122. // signed/unsigned comparison. Would it be better to round this up to a
  123. // "nicer" bit width?
  124. unsigned cmp_width = std::max(width_for_cmp(lhs_type, lhs_signed),
  125. width_for_cmp(rhs_type, rhs_signed));
  126. auto* cmp_type = llvm::IntegerType::get(context.llvm_context(), cmp_width);
  127. // Widen the operands as needed.
  128. lhs = CreateExt(context, lhs, cmp_type, lhs_signed, "lhs");
  129. rhs = CreateExt(context, rhs, cmp_type, rhs_signed, "rhs");
  130. context.SetLocal(
  131. inst_id,
  132. context.builder().CreateICmp(
  133. GetBuiltinICmpPredicate(builtin_kind, cmp_signed), lhs, rhs));
  134. }
  135. // Creates a binary operator for a call to a builtin for either that operation
  136. // or the corresponding compound assignment.
  137. static auto CreateBinaryOperatorForBuiltin(
  138. FunctionContext& context, SemIR::InstId inst_id,
  139. SemIR::BuiltinFunctionKind builtin_kind, llvm::Value* lhs, llvm::Value* rhs)
  140. -> llvm::Value* {
  141. // TODO: Consider setting this to true in the performance build mode if the
  142. // result type is a signed integer type.
  143. constexpr bool SignedOverflowIsUB = false;
  144. switch (builtin_kind) {
  145. case SemIR::BuiltinFunctionKind::IntSAdd:
  146. case SemIR::BuiltinFunctionKind::IntSAddAssign: {
  147. return context.builder().CreateAdd(lhs, rhs, "",
  148. /*HasNUW=*/false,
  149. /*HasNSW=*/SignedOverflowIsUB);
  150. }
  151. case SemIR::BuiltinFunctionKind::IntSSub:
  152. case SemIR::BuiltinFunctionKind::IntSSubAssign: {
  153. return context.builder().CreateSub(lhs, rhs, "",
  154. /*HasNUW=*/false,
  155. /*HasNSW=*/SignedOverflowIsUB);
  156. }
  157. case SemIR::BuiltinFunctionKind::IntSMul:
  158. case SemIR::BuiltinFunctionKind::IntSMulAssign: {
  159. return context.builder().CreateMul(lhs, rhs, "",
  160. /*HasNUW=*/false,
  161. /*HasNSW=*/SignedOverflowIsUB);
  162. }
  163. case SemIR::BuiltinFunctionKind::IntSDiv:
  164. case SemIR::BuiltinFunctionKind::IntSDivAssign: {
  165. return context.builder().CreateSDiv(lhs, rhs);
  166. }
  167. case SemIR::BuiltinFunctionKind::IntSMod:
  168. case SemIR::BuiltinFunctionKind::IntSModAssign: {
  169. return context.builder().CreateSRem(lhs, rhs);
  170. }
  171. case SemIR::BuiltinFunctionKind::IntUAdd:
  172. case SemIR::BuiltinFunctionKind::IntUAddAssign: {
  173. return context.builder().CreateAdd(lhs, rhs);
  174. }
  175. case SemIR::BuiltinFunctionKind::IntUSub:
  176. case SemIR::BuiltinFunctionKind::IntUSubAssign: {
  177. return context.builder().CreateSub(lhs, rhs);
  178. }
  179. case SemIR::BuiltinFunctionKind::IntUMul:
  180. case SemIR::BuiltinFunctionKind::IntUMulAssign: {
  181. return context.builder().CreateMul(lhs, rhs);
  182. }
  183. case SemIR::BuiltinFunctionKind::IntUDiv:
  184. case SemIR::BuiltinFunctionKind::IntUDivAssign: {
  185. return context.builder().CreateUDiv(lhs, rhs);
  186. }
  187. case SemIR::BuiltinFunctionKind::IntUMod:
  188. case SemIR::BuiltinFunctionKind::IntUModAssign: {
  189. return context.builder().CreateURem(lhs, rhs);
  190. }
  191. case SemIR::BuiltinFunctionKind::IntAnd:
  192. case SemIR::BuiltinFunctionKind::IntAndAssign: {
  193. return context.builder().CreateAnd(lhs, rhs);
  194. }
  195. case SemIR::BuiltinFunctionKind::IntOr:
  196. case SemIR::BuiltinFunctionKind::IntOrAssign: {
  197. return context.builder().CreateOr(lhs, rhs);
  198. }
  199. case SemIR::BuiltinFunctionKind::IntXor:
  200. case SemIR::BuiltinFunctionKind::IntXorAssign: {
  201. return context.builder().CreateXor(lhs, rhs);
  202. }
  203. case SemIR::BuiltinFunctionKind::IntLeftShift:
  204. case SemIR::BuiltinFunctionKind::IntLeftShiftAssign: {
  205. return CreateIntShift(context, llvm::Instruction::Shl, lhs, rhs);
  206. }
  207. case SemIR::BuiltinFunctionKind::IntRightShift:
  208. case SemIR::BuiltinFunctionKind::IntRightShiftAssign: {
  209. // TODO: Split each of these builtins into separate signed and unsigned
  210. // builtins rather than working out here whether we're performing an
  211. // arithmetic or logical shift.
  212. auto lhs_id = context.sem_ir().inst_blocks().Get(
  213. context.sem_ir().insts().GetAs<SemIR::Call>(inst_id).args_id)[0];
  214. auto [lhs_type_file, lhs_type_id] = context.GetTypeIdOfInst(lhs_id);
  215. if (builtin_kind == SemIR::BuiltinFunctionKind::IntRightShiftAssign) {
  216. lhs_type_id = lhs_type_file->GetPointeeType(lhs_type_id);
  217. }
  218. return CreateIntShift(context,
  219. lhs_type_file->types().IsSignedInt(lhs_type_id)
  220. ? llvm::Instruction::AShr
  221. : llvm::Instruction::LShr,
  222. lhs, rhs);
  223. }
  224. case SemIR::BuiltinFunctionKind::FloatAdd:
  225. case SemIR::BuiltinFunctionKind::FloatAddAssign: {
  226. return context.builder().CreateFAdd(lhs, rhs);
  227. }
  228. case SemIR::BuiltinFunctionKind::FloatSub:
  229. case SemIR::BuiltinFunctionKind::FloatSubAssign: {
  230. return context.builder().CreateFSub(lhs, rhs);
  231. }
  232. case SemIR::BuiltinFunctionKind::FloatMul:
  233. case SemIR::BuiltinFunctionKind::FloatMulAssign: {
  234. return context.builder().CreateFMul(lhs, rhs);
  235. }
  236. case SemIR::BuiltinFunctionKind::FloatDiv:
  237. case SemIR::BuiltinFunctionKind::FloatDivAssign: {
  238. return context.builder().CreateFDiv(lhs, rhs);
  239. }
  240. default: {
  241. CARBON_FATAL("Unexpected binary operator {0}", builtin_kind);
  242. }
  243. }
  244. }
  245. // Handles a call to a builtin function.
  246. static auto HandleBuiltinCall(FunctionContext& context, SemIR::InstId inst_id,
  247. SemIR::BuiltinFunctionKind builtin_kind,
  248. llvm::ArrayRef<SemIR::InstId> arg_ids) -> void {
  249. // TODO: Consider setting this to true in the performance build mode if the
  250. // result type is a signed integer type.
  251. constexpr bool SignedOverflowIsUB = false;
  252. // TODO: Move the instruction names here into InstNamer.
  253. switch (builtin_kind) {
  254. case SemIR::BuiltinFunctionKind::None:
  255. CARBON_FATAL("No callee in function call.");
  256. case SemIR::BuiltinFunctionKind::NoOp:
  257. return;
  258. case SemIR::BuiltinFunctionKind::PrimitiveCopy:
  259. context.SetLocal(inst_id, context.GetValue(arg_ids[0]));
  260. return;
  261. case SemIR::BuiltinFunctionKind::PrintChar: {
  262. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  263. llvm::Value* arg_value = context.builder().CreateSExtOrTrunc(
  264. context.GetValue(arg_ids[0]), i32_type);
  265. auto putchar = context.llvm_module().getOrInsertFunction(
  266. "putchar", i32_type, i32_type);
  267. auto* result = context.builder().CreateCall(putchar, {arg_value});
  268. context.SetLocal(inst_id, context.builder().CreateSExtOrTrunc(
  269. result, context.GetTypeOfInst(inst_id)));
  270. return;
  271. }
  272. case SemIR::BuiltinFunctionKind::PrintInt: {
  273. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  274. auto* ptr_type = llvm::PointerType::get(context.llvm_context(), 0);
  275. auto* printf_type = llvm::FunctionType::get(i32_type, {ptr_type},
  276. /*isVarArg=*/true);
  277. llvm::FunctionCallee printf =
  278. context.llvm_module().getOrInsertFunction("printf", printf_type);
  279. llvm::Value* format_string = context.printf_int_format_string();
  280. llvm::Value* arg_value = context.builder().CreateSExtOrTrunc(
  281. context.GetValue(arg_ids[0]), i32_type);
  282. context.SetLocal(inst_id, context.builder().CreateCall(
  283. printf, {format_string, arg_value}));
  284. return;
  285. }
  286. case SemIR::BuiltinFunctionKind::ReadChar: {
  287. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  288. auto getchar =
  289. context.llvm_module().getOrInsertFunction("getchar", i32_type);
  290. auto* result = context.builder().CreateCall(getchar, {});
  291. context.SetLocal(inst_id, context.builder().CreateSExtOrTrunc(
  292. result, context.GetTypeOfInst(inst_id)));
  293. return;
  294. }
  295. case SemIR::BuiltinFunctionKind::TypeAnd: {
  296. context.SetLocal(inst_id, context.GetTypeAsValue());
  297. return;
  298. }
  299. case SemIR::BuiltinFunctionKind::BoolMakeType:
  300. case SemIR::BuiltinFunctionKind::CharLiteralMakeType:
  301. case SemIR::BuiltinFunctionKind::FloatLiteralMakeType:
  302. case SemIR::BuiltinFunctionKind::FloatMakeType:
  303. case SemIR::BuiltinFunctionKind::IntLiteralMakeType:
  304. case SemIR::BuiltinFunctionKind::IntMakeTypeSigned:
  305. case SemIR::BuiltinFunctionKind::IntMakeTypeUnsigned:
  306. case SemIR::BuiltinFunctionKind::MaybeUnformedMakeType:
  307. context.SetLocal(inst_id, context.GetTypeAsValue());
  308. return;
  309. case SemIR::BuiltinFunctionKind::IntConvert: {
  310. context.SetLocal(inst_id,
  311. CreateExtOrTrunc(context, context.GetValue(arg_ids[0]),
  312. context.GetTypeOfInst(inst_id),
  313. IsSignedInt(context, arg_ids[0])));
  314. return;
  315. }
  316. case SemIR::BuiltinFunctionKind::IntSNegate: {
  317. // Lower `-x` as `0 - x`.
  318. auto* operand = context.GetValue(arg_ids[0]);
  319. context.SetLocal(
  320. inst_id,
  321. context.builder().CreateSub(
  322. llvm::ConstantInt::getNullValue(operand->getType()), operand, "",
  323. /*HasNUW=*/false,
  324. /*HasNSW=*/SignedOverflowIsUB));
  325. return;
  326. }
  327. case SemIR::BuiltinFunctionKind::IntUNegate: {
  328. // Lower `-x` as `0 - x`.
  329. auto* operand = context.GetValue(arg_ids[0]);
  330. context.SetLocal(
  331. inst_id,
  332. context.builder().CreateSub(
  333. llvm::ConstantInt::getNullValue(operand->getType()), operand));
  334. return;
  335. }
  336. case SemIR::BuiltinFunctionKind::IntComplement: {
  337. // Lower `^x` as `-1 ^ x`.
  338. auto* operand = context.GetValue(arg_ids[0]);
  339. context.SetLocal(
  340. inst_id,
  341. context.builder().CreateXor(
  342. llvm::ConstantInt::getSigned(operand->getType(), -1), operand));
  343. return;
  344. }
  345. case SemIR::BuiltinFunctionKind::IntSAdd:
  346. case SemIR::BuiltinFunctionKind::IntSSub:
  347. case SemIR::BuiltinFunctionKind::IntSMul:
  348. case SemIR::BuiltinFunctionKind::IntSDiv:
  349. case SemIR::BuiltinFunctionKind::IntSMod:
  350. case SemIR::BuiltinFunctionKind::IntUAdd:
  351. case SemIR::BuiltinFunctionKind::IntUSub:
  352. case SemIR::BuiltinFunctionKind::IntUMul:
  353. case SemIR::BuiltinFunctionKind::IntUDiv:
  354. case SemIR::BuiltinFunctionKind::IntUMod:
  355. case SemIR::BuiltinFunctionKind::IntAnd:
  356. case SemIR::BuiltinFunctionKind::IntOr:
  357. case SemIR::BuiltinFunctionKind::IntXor:
  358. case SemIR::BuiltinFunctionKind::IntLeftShift:
  359. case SemIR::BuiltinFunctionKind::IntRightShift:
  360. case SemIR::BuiltinFunctionKind::FloatAdd:
  361. case SemIR::BuiltinFunctionKind::FloatSub:
  362. case SemIR::BuiltinFunctionKind::FloatMul:
  363. case SemIR::BuiltinFunctionKind::FloatDiv: {
  364. context.SetLocal(inst_id, CreateBinaryOperatorForBuiltin(
  365. context, inst_id, builtin_kind,
  366. context.GetValue(arg_ids[0]),
  367. context.GetValue(arg_ids[1])));
  368. return;
  369. }
  370. case SemIR::BuiltinFunctionKind::IntSAddAssign:
  371. case SemIR::BuiltinFunctionKind::IntSSubAssign:
  372. case SemIR::BuiltinFunctionKind::IntSMulAssign:
  373. case SemIR::BuiltinFunctionKind::IntSDivAssign:
  374. case SemIR::BuiltinFunctionKind::IntSModAssign:
  375. case SemIR::BuiltinFunctionKind::IntUAddAssign:
  376. case SemIR::BuiltinFunctionKind::IntUSubAssign:
  377. case SemIR::BuiltinFunctionKind::IntUMulAssign:
  378. case SemIR::BuiltinFunctionKind::IntUDivAssign:
  379. case SemIR::BuiltinFunctionKind::IntUModAssign:
  380. case SemIR::BuiltinFunctionKind::IntAndAssign:
  381. case SemIR::BuiltinFunctionKind::IntOrAssign:
  382. case SemIR::BuiltinFunctionKind::IntXorAssign:
  383. case SemIR::BuiltinFunctionKind::IntLeftShiftAssign:
  384. case SemIR::BuiltinFunctionKind::IntRightShiftAssign:
  385. case SemIR::BuiltinFunctionKind::FloatAddAssign:
  386. case SemIR::BuiltinFunctionKind::FloatSubAssign:
  387. case SemIR::BuiltinFunctionKind::FloatMulAssign:
  388. case SemIR::BuiltinFunctionKind::FloatDivAssign: {
  389. auto* lhs_ptr = context.GetValue(arg_ids[0]);
  390. auto lhs_type = context.GetTypeIdOfInst(arg_ids[0]);
  391. auto pointee_type = lhs_type.GetPointeeType();
  392. auto* lhs_value = context.LoadObject(pointee_type, lhs_ptr);
  393. auto* result = CreateBinaryOperatorForBuiltin(
  394. context, inst_id, builtin_kind, lhs_value,
  395. context.GetValue(arg_ids[1]));
  396. context.StoreObject(pointee_type, result, lhs_ptr);
  397. // TODO: Add a helper to get a "no value representation" value.
  398. context.SetLocal(inst_id,
  399. llvm::PoisonValue::get(context.GetTypeOfInst(inst_id)));
  400. return;
  401. }
  402. case SemIR::BuiltinFunctionKind::IntEq:
  403. case SemIR::BuiltinFunctionKind::IntNeq:
  404. case SemIR::BuiltinFunctionKind::IntLess:
  405. case SemIR::BuiltinFunctionKind::IntLessEq:
  406. case SemIR::BuiltinFunctionKind::IntGreater:
  407. case SemIR::BuiltinFunctionKind::IntGreaterEq:
  408. case SemIR::BuiltinFunctionKind::BoolEq:
  409. case SemIR::BuiltinFunctionKind::BoolNeq: {
  410. HandleIntComparison(context, inst_id, builtin_kind, arg_ids[0],
  411. arg_ids[1]);
  412. return;
  413. }
  414. case SemIR::BuiltinFunctionKind::FloatNegate: {
  415. context.SetLocal(
  416. inst_id, context.builder().CreateFNeg(context.GetValue(arg_ids[0])));
  417. return;
  418. }
  419. case SemIR::BuiltinFunctionKind::FloatEq:
  420. case SemIR::BuiltinFunctionKind::FloatNeq:
  421. case SemIR::BuiltinFunctionKind::FloatLess:
  422. case SemIR::BuiltinFunctionKind::FloatLessEq:
  423. case SemIR::BuiltinFunctionKind::FloatGreater:
  424. case SemIR::BuiltinFunctionKind::FloatGreaterEq: {
  425. context.SetLocal(inst_id, context.builder().CreateFCmp(
  426. GetBuiltinFCmpPredicate(builtin_kind),
  427. context.GetValue(arg_ids[0]),
  428. context.GetValue(arg_ids[1])));
  429. return;
  430. }
  431. case SemIR::BuiltinFunctionKind::CharConvertChecked:
  432. case SemIR::BuiltinFunctionKind::FloatConvertChecked:
  433. case SemIR::BuiltinFunctionKind::IntConvertChecked:
  434. case SemIR::BuiltinFunctionKind::TypeCanAggregateDestroy: {
  435. // TODO: Check this statically.
  436. CARBON_CHECK(builtin_kind.IsCompTimeOnly(
  437. context.sem_ir(), arg_ids,
  438. context.sem_ir().insts().Get(inst_id).type_id()));
  439. CARBON_FATAL("Missing constant value for call to comptime-only function");
  440. }
  441. case SemIR::BuiltinFunctionKind::TypeAggregateDestroy:
  442. // TODO: Destroy aggregate members.
  443. return;
  444. }
  445. CARBON_FATAL("Unsupported builtin call.");
  446. }
  447. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  448. SemIR::Call inst) -> void {
  449. llvm::ArrayRef<SemIR::InstId> arg_ids =
  450. context.sem_ir().inst_blocks().Get(inst.args_id);
  451. // TODO: This duplicates the SpecificId handling in `GetCalleeFunction`.
  452. // TODO: Should the `bound_method` be removed when forming the `call`
  453. // instruction? The `self` parameter is transferred into the call argument
  454. // list.
  455. auto callee_id = inst.callee_id;
  456. if (auto bound_method =
  457. context.sem_ir().insts().TryGetAs<SemIR::BoundMethod>(callee_id)) {
  458. callee_id = bound_method->function_decl_id;
  459. }
  460. // Map to the callee in the specific. This might be in a different file than
  461. // the one we're currently lowering.
  462. const auto* callee_file = &context.sem_ir();
  463. if (context.specific_id().has_value()) {
  464. auto [const_file, const_id] = GetConstantValueInSpecific(
  465. context.specific_sem_ir(), context.specific_id(), context.sem_ir(),
  466. callee_id);
  467. callee_file = const_file;
  468. callee_id = const_file->constant_values().GetInstIdIfValid(const_id);
  469. CARBON_CHECK(callee_id.has_value());
  470. }
  471. auto callee_function = SemIR::GetCalleeFunction(*callee_file, callee_id);
  472. CARBON_CHECK(callee_function.function_id.has_value());
  473. const SemIR::Function& function =
  474. callee_file->functions().Get(callee_function.function_id);
  475. context.AddCallToCurrentFingerprint(callee_file->check_ir_id(),
  476. callee_function.function_id,
  477. callee_function.resolved_specific_id);
  478. if (auto builtin_kind = function.builtin_function_kind();
  479. builtin_kind != SemIR::BuiltinFunctionKind::None) {
  480. HandleBuiltinCall(context, inst_id, builtin_kind, arg_ids);
  481. return;
  482. }
  483. std::vector<llvm::Value*> args;
  484. auto inst_type = context.GetTypeIdOfInst(inst_id);
  485. bool call_has_return_slot =
  486. SemIR::ReturnTypeInfo::ForType(context.sem_ir(), inst.type_id)
  487. .has_return_slot();
  488. if (context.GetReturnTypeInfo(inst_type).info.has_return_slot()) {
  489. CARBON_CHECK(call_has_return_slot);
  490. args.push_back(context.GetValue(arg_ids.consume_back()));
  491. } else if (call_has_return_slot) {
  492. // Call instruction has a return slot but this specific callee does not.
  493. // Just ignore it.
  494. arg_ids.consume_back();
  495. }
  496. for (auto arg_id : arg_ids) {
  497. auto arg_type = context.GetTypeIdOfInst(arg_id);
  498. if (context.GetValueRepr(arg_type).repr.kind != SemIR::ValueRepr::None) {
  499. args.push_back(context.GetValue(arg_id));
  500. }
  501. }
  502. llvm::CallInst* call;
  503. if (function.virtual_index != -1) {
  504. CARBON_CHECK(!args.empty(),
  505. "Virtual functions must have at least one parameter");
  506. auto* ptr_type =
  507. llvm::PointerType::get(context.llvm_context(), /*AddressSpace=*/0);
  508. // The vtable pointer is always at the start of the object in the Carbon
  509. // ABI, so a pointer to the object is a pointer to the vtable pointer - load
  510. // that to get a pointer to the vtable.
  511. // TODO: Use `context.LoadObject`.
  512. auto* vtable =
  513. context.builder().CreateLoad(ptr_type, args.front(), "vtable");
  514. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  515. auto function_type_info =
  516. context.GetFileContext(callee_file)
  517. .BuildFunctionTypeInfo(function,
  518. callee_function.resolved_specific_id);
  519. call = context.builder().CreateCall(
  520. function_type_info.type,
  521. context.builder().CreateCall(
  522. llvm::Intrinsic::getOrInsertDeclaration(
  523. &context.llvm_module(), llvm::Intrinsic::load_relative,
  524. {i32_type}),
  525. {vtable,
  526. llvm::ConstantInt::get(
  527. i32_type, static_cast<uint64_t>(function.virtual_index) * 4)}),
  528. args);
  529. } else {
  530. auto* callee =
  531. context.GetFileContext(callee_file)
  532. .GetOrCreateFunction(callee_function.function_id,
  533. callee_function.resolved_specific_id);
  534. auto describe_call = [&] {
  535. RawStringOstream out;
  536. out << "call ";
  537. callee->printAsOperand(out);
  538. out << "(";
  539. llvm::ListSeparator sep;
  540. for (auto* arg : args) {
  541. out << sep;
  542. arg->printAsOperand(out);
  543. }
  544. out << ")\n";
  545. callee->print(out);
  546. return out.TakeStr();
  547. };
  548. CARBON_CHECK(callee->arg_size() == args.size(),
  549. "Argument count mismatch: {0}", describe_call());
  550. call = context.builder().CreateCall(callee, args);
  551. }
  552. context.SetLocal(inst_id, call);
  553. }
  554. } // namespace Carbon::Lower