handle_call.cpp 23 KB

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