handle_call.cpp 24 KB

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