handle_call.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/IR/Type.h"
  5. #include "llvm/IR/Value.h"
  6. #include "toolchain/lower/function_context.h"
  7. #include "toolchain/sem_ir/builtin_function_kind.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::Lower {
  11. // Get the predicate to use for an `icmp` instruction generated for the
  12. // specified builtin.
  13. static auto GetBuiltinICmpPredicate(SemIR::BuiltinFunctionKind builtin_kind,
  14. bool is_signed)
  15. -> llvm::CmpInst::Predicate {
  16. switch (builtin_kind) {
  17. case SemIR::BuiltinFunctionKind::IntEq:
  18. case SemIR::BuiltinFunctionKind::BoolEq:
  19. return llvm::CmpInst::ICMP_EQ;
  20. case SemIR::BuiltinFunctionKind::IntNeq:
  21. case SemIR::BuiltinFunctionKind::BoolNeq:
  22. return llvm::CmpInst::ICMP_NE;
  23. case SemIR::BuiltinFunctionKind::IntLess:
  24. return is_signed ? llvm::CmpInst::ICMP_SLT : llvm::CmpInst::ICMP_ULT;
  25. case SemIR::BuiltinFunctionKind::IntLessEq:
  26. return is_signed ? llvm::CmpInst::ICMP_SLE : llvm::CmpInst::ICMP_ULE;
  27. case SemIR::BuiltinFunctionKind::IntGreater:
  28. return is_signed ? llvm::CmpInst::ICMP_SGT : llvm::CmpInst::ICMP_UGT;
  29. case SemIR::BuiltinFunctionKind::IntGreaterEq:
  30. return is_signed ? llvm::CmpInst::ICMP_SGE : llvm::CmpInst::ICMP_UGE;
  31. default:
  32. CARBON_FATAL("Unexpected builtin kind {0}", builtin_kind);
  33. }
  34. }
  35. // Get the predicate to use for an `fcmp` instruction generated for the
  36. // specified builtin.
  37. static auto GetBuiltinFCmpPredicate(SemIR::BuiltinFunctionKind builtin_kind)
  38. -> llvm::CmpInst::Predicate {
  39. switch (builtin_kind) {
  40. case SemIR::BuiltinFunctionKind::FloatEq:
  41. return llvm::CmpInst::FCMP_OEQ;
  42. case SemIR::BuiltinFunctionKind::FloatNeq:
  43. return llvm::CmpInst::FCMP_ONE;
  44. case SemIR::BuiltinFunctionKind::FloatLess:
  45. return llvm::CmpInst::FCMP_OLT;
  46. case SemIR::BuiltinFunctionKind::FloatLessEq:
  47. return llvm::CmpInst::FCMP_OLE;
  48. case SemIR::BuiltinFunctionKind::FloatGreater:
  49. return llvm::CmpInst::FCMP_OGT;
  50. case SemIR::BuiltinFunctionKind::FloatGreaterEq:
  51. return llvm::CmpInst::FCMP_OGE;
  52. default:
  53. CARBON_FATAL("Unexpected builtin kind {0}", builtin_kind);
  54. }
  55. }
  56. // Returns whether the specified instruction has a signed integer type.
  57. static auto IsSignedInt(FunctionContext& context, SemIR::InstId int_id)
  58. -> bool {
  59. return context.sem_ir().types().IsSignedInt(
  60. context.sem_ir().insts().Get(int_id).type_id());
  61. }
  62. // Handles a call to a builtin function.
  63. static auto HandleBuiltinCall(FunctionContext& context, SemIR::InstId inst_id,
  64. SemIR::BuiltinFunctionKind builtin_kind,
  65. llvm::ArrayRef<SemIR::InstId> arg_ids) -> void {
  66. // TODO: Consider setting this to true in the performance build mode if the
  67. // result type is a signed integer type.
  68. constexpr bool SignedOverflowIsUB = false;
  69. // TODO: Move the instruction names here into InstNamer.
  70. switch (builtin_kind) {
  71. case SemIR::BuiltinFunctionKind::None:
  72. CARBON_FATAL("No callee in function call.");
  73. case SemIR::BuiltinFunctionKind::PrintChar: {
  74. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  75. llvm::Value* arg_value = context.builder().CreateSExtOrTrunc(
  76. context.GetValue(arg_ids[0]), i32_type);
  77. auto putchar = context.llvm_module().getOrInsertFunction(
  78. "putchar", i32_type, i32_type);
  79. auto* result = context.builder().CreateCall(putchar, {arg_value});
  80. context.SetLocal(
  81. inst_id,
  82. context.builder().CreateSExtOrTrunc(
  83. result, context.GetType(
  84. context.sem_ir().insts().Get(inst_id).type_id())));
  85. return;
  86. }
  87. case SemIR::BuiltinFunctionKind::PrintInt: {
  88. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  89. auto* ptr_type = llvm::PointerType::get(context.llvm_context(), 0);
  90. auto* printf_type = llvm::FunctionType::get(i32_type, {ptr_type},
  91. /*isVarArg=*/true);
  92. llvm::FunctionCallee printf =
  93. context.llvm_module().getOrInsertFunction("printf", printf_type);
  94. llvm::Value* format_string =
  95. context.builder().CreateGlobalString("%d\n", "printf.int.format");
  96. llvm::Value* arg_value = context.builder().CreateSExtOrTrunc(
  97. context.GetValue(arg_ids[0]), i32_type);
  98. context.SetLocal(inst_id, context.builder().CreateCall(
  99. printf, {format_string, arg_value}));
  100. return;
  101. }
  102. case SemIR::BuiltinFunctionKind::ReadChar: {
  103. auto* i32_type = llvm::IntegerType::getInt32Ty(context.llvm_context());
  104. auto getchar =
  105. context.llvm_module().getOrInsertFunction("getchar", i32_type);
  106. auto* result = context.builder().CreateCall(getchar, {});
  107. context.SetLocal(
  108. inst_id,
  109. context.builder().CreateSExtOrTrunc(
  110. result, context.GetType(
  111. context.sem_ir().insts().Get(inst_id).type_id())));
  112. return;
  113. }
  114. case SemIR::BuiltinFunctionKind::BoolMakeType:
  115. case SemIR::BuiltinFunctionKind::FloatMakeType:
  116. case SemIR::BuiltinFunctionKind::IntLiteralMakeType:
  117. case SemIR::BuiltinFunctionKind::IntMakeTypeSigned:
  118. case SemIR::BuiltinFunctionKind::IntMakeTypeUnsigned:
  119. context.SetLocal(inst_id, context.GetTypeAsValue());
  120. return;
  121. case SemIR::BuiltinFunctionKind::IntSNegate: {
  122. // Lower `-x` as `0 - x`.
  123. auto* operand = context.GetValue(arg_ids[0]);
  124. context.SetLocal(
  125. inst_id,
  126. context.builder().CreateSub(
  127. llvm::ConstantInt::getNullValue(operand->getType()), operand, "",
  128. /*HasNUW=*/false,
  129. /*HasNSW=*/SignedOverflowIsUB));
  130. return;
  131. }
  132. case SemIR::BuiltinFunctionKind::IntUNegate: {
  133. // Lower `-x` as `0 - x`.
  134. auto* operand = context.GetValue(arg_ids[0]);
  135. context.SetLocal(
  136. inst_id,
  137. context.builder().CreateSub(
  138. llvm::ConstantInt::getNullValue(operand->getType()), operand));
  139. return;
  140. }
  141. case SemIR::BuiltinFunctionKind::IntComplement: {
  142. // Lower `^x` as `-1 ^ x`.
  143. auto* operand = context.GetValue(arg_ids[0]);
  144. context.SetLocal(
  145. inst_id,
  146. context.builder().CreateXor(
  147. llvm::ConstantInt::getSigned(operand->getType(), -1), operand));
  148. return;
  149. }
  150. case SemIR::BuiltinFunctionKind::IntSAdd: {
  151. context.SetLocal(
  152. inst_id, context.builder().CreateAdd(context.GetValue(arg_ids[0]),
  153. context.GetValue(arg_ids[1]), "",
  154. /*HasNUW=*/false,
  155. /*HasNSW=*/SignedOverflowIsUB));
  156. return;
  157. }
  158. case SemIR::BuiltinFunctionKind::IntSSub: {
  159. context.SetLocal(
  160. inst_id, context.builder().CreateSub(context.GetValue(arg_ids[0]),
  161. context.GetValue(arg_ids[1]), "",
  162. /*HasNUW=*/false,
  163. /*HasNSW=*/SignedOverflowIsUB));
  164. return;
  165. }
  166. case SemIR::BuiltinFunctionKind::IntSMul: {
  167. context.SetLocal(
  168. inst_id, context.builder().CreateMul(context.GetValue(arg_ids[0]),
  169. context.GetValue(arg_ids[1]), "",
  170. /*HasNUW=*/false,
  171. /*HasNSW=*/SignedOverflowIsUB));
  172. return;
  173. }
  174. case SemIR::BuiltinFunctionKind::IntSDiv: {
  175. context.SetLocal(
  176. inst_id, context.builder().CreateSDiv(context.GetValue(arg_ids[0]),
  177. context.GetValue(arg_ids[1])));
  178. return;
  179. }
  180. case SemIR::BuiltinFunctionKind::IntSMod: {
  181. context.SetLocal(
  182. inst_id, context.builder().CreateSRem(context.GetValue(arg_ids[0]),
  183. context.GetValue(arg_ids[1])));
  184. return;
  185. }
  186. case SemIR::BuiltinFunctionKind::IntUAdd: {
  187. context.SetLocal(
  188. inst_id, context.builder().CreateAdd(context.GetValue(arg_ids[0]),
  189. context.GetValue(arg_ids[1])));
  190. return;
  191. }
  192. case SemIR::BuiltinFunctionKind::IntUSub: {
  193. context.SetLocal(
  194. inst_id, context.builder().CreateSub(context.GetValue(arg_ids[0]),
  195. context.GetValue(arg_ids[1])));
  196. return;
  197. }
  198. case SemIR::BuiltinFunctionKind::IntUMul: {
  199. context.SetLocal(
  200. inst_id, context.builder().CreateMul(context.GetValue(arg_ids[0]),
  201. context.GetValue(arg_ids[1])));
  202. return;
  203. }
  204. case SemIR::BuiltinFunctionKind::IntUDiv: {
  205. context.SetLocal(
  206. inst_id, context.builder().CreateUDiv(context.GetValue(arg_ids[0]),
  207. context.GetValue(arg_ids[1])));
  208. return;
  209. }
  210. case SemIR::BuiltinFunctionKind::IntUMod: {
  211. context.SetLocal(
  212. inst_id, context.builder().CreateURem(context.GetValue(arg_ids[0]),
  213. context.GetValue(arg_ids[1])));
  214. return;
  215. }
  216. case SemIR::BuiltinFunctionKind::IntAnd: {
  217. context.SetLocal(
  218. inst_id, context.builder().CreateAnd(context.GetValue(arg_ids[0]),
  219. context.GetValue(arg_ids[1])));
  220. return;
  221. }
  222. case SemIR::BuiltinFunctionKind::IntOr: {
  223. context.SetLocal(
  224. inst_id, context.builder().CreateOr(context.GetValue(arg_ids[0]),
  225. context.GetValue(arg_ids[1])));
  226. return;
  227. }
  228. case SemIR::BuiltinFunctionKind::IntXor: {
  229. context.SetLocal(
  230. inst_id, context.builder().CreateXor(context.GetValue(arg_ids[0]),
  231. context.GetValue(arg_ids[1])));
  232. return;
  233. }
  234. case SemIR::BuiltinFunctionKind::IntLeftShift: {
  235. context.SetLocal(
  236. inst_id, context.builder().CreateShl(context.GetValue(arg_ids[0]),
  237. context.GetValue(arg_ids[1])));
  238. return;
  239. }
  240. case SemIR::BuiltinFunctionKind::IntRightShift: {
  241. context.SetLocal(
  242. inst_id,
  243. IsSignedInt(context, inst_id)
  244. ? context.builder().CreateAShr(context.GetValue(arg_ids[0]),
  245. context.GetValue(arg_ids[1]))
  246. : context.builder().CreateLShr(context.GetValue(arg_ids[0]),
  247. context.GetValue(arg_ids[1])));
  248. return;
  249. }
  250. case SemIR::BuiltinFunctionKind::IntEq:
  251. case SemIR::BuiltinFunctionKind::IntNeq:
  252. case SemIR::BuiltinFunctionKind::IntLess:
  253. case SemIR::BuiltinFunctionKind::IntLessEq:
  254. case SemIR::BuiltinFunctionKind::IntGreater:
  255. case SemIR::BuiltinFunctionKind::IntGreaterEq:
  256. case SemIR::BuiltinFunctionKind::BoolEq:
  257. case SemIR::BuiltinFunctionKind::BoolNeq: {
  258. context.SetLocal(
  259. inst_id,
  260. context.builder().CreateICmp(
  261. GetBuiltinICmpPredicate(builtin_kind,
  262. IsSignedInt(context, arg_ids[0])),
  263. context.GetValue(arg_ids[0]), context.GetValue(arg_ids[1])));
  264. return;
  265. }
  266. case SemIR::BuiltinFunctionKind::FloatNegate: {
  267. context.SetLocal(
  268. inst_id, context.builder().CreateFNeg(context.GetValue(arg_ids[0])));
  269. return;
  270. }
  271. case SemIR::BuiltinFunctionKind::FloatAdd: {
  272. context.SetLocal(
  273. inst_id, context.builder().CreateFAdd(context.GetValue(arg_ids[0]),
  274. context.GetValue(arg_ids[1])));
  275. return;
  276. }
  277. case SemIR::BuiltinFunctionKind::FloatSub: {
  278. context.SetLocal(
  279. inst_id, context.builder().CreateFSub(context.GetValue(arg_ids[0]),
  280. context.GetValue(arg_ids[1])));
  281. return;
  282. }
  283. case SemIR::BuiltinFunctionKind::FloatMul: {
  284. context.SetLocal(
  285. inst_id, context.builder().CreateFMul(context.GetValue(arg_ids[0]),
  286. context.GetValue(arg_ids[1])));
  287. return;
  288. }
  289. case SemIR::BuiltinFunctionKind::FloatDiv: {
  290. context.SetLocal(
  291. inst_id, context.builder().CreateFDiv(context.GetValue(arg_ids[0]),
  292. context.GetValue(arg_ids[1])));
  293. return;
  294. }
  295. case SemIR::BuiltinFunctionKind::FloatEq:
  296. case SemIR::BuiltinFunctionKind::FloatNeq:
  297. case SemIR::BuiltinFunctionKind::FloatLess:
  298. case SemIR::BuiltinFunctionKind::FloatLessEq:
  299. case SemIR::BuiltinFunctionKind::FloatGreater:
  300. case SemIR::BuiltinFunctionKind::FloatGreaterEq: {
  301. context.SetLocal(inst_id, context.builder().CreateFCmp(
  302. GetBuiltinFCmpPredicate(builtin_kind),
  303. context.GetValue(arg_ids[0]),
  304. context.GetValue(arg_ids[1])));
  305. return;
  306. }
  307. case SemIR::BuiltinFunctionKind::IntConvertChecked: {
  308. // TODO: Check this statically.
  309. CARBON_CHECK(builtin_kind.IsCompTimeOnly());
  310. CARBON_FATAL("Missing constant value for call to comptime-only function");
  311. }
  312. }
  313. CARBON_FATAL("Unsupported builtin call.");
  314. }
  315. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  316. SemIR::Call inst) -> void {
  317. llvm::ArrayRef<SemIR::InstId> arg_ids =
  318. context.sem_ir().inst_blocks().Get(inst.args_id);
  319. auto callee_function =
  320. SemIR::GetCalleeFunction(context.sem_ir(), inst.callee_id);
  321. CARBON_CHECK(callee_function.function_id.is_valid());
  322. if (auto builtin_kind = context.sem_ir()
  323. .functions()
  324. .Get(callee_function.function_id)
  325. .builtin_function_kind;
  326. builtin_kind != SemIR::BuiltinFunctionKind::None) {
  327. HandleBuiltinCall(context, inst_id, builtin_kind, arg_ids);
  328. return;
  329. }
  330. auto* callee = context.GetOrCreateFunction(
  331. callee_function.function_id, callee_function.resolved_specific_id);
  332. std::vector<llvm::Value*> args;
  333. if (SemIR::ReturnTypeInfo::ForType(context.sem_ir(), inst.type_id)
  334. .has_return_slot()) {
  335. args.push_back(context.GetValue(arg_ids.back()));
  336. arg_ids = arg_ids.drop_back();
  337. }
  338. for (auto arg_id : arg_ids) {
  339. auto arg_type_id = context.sem_ir().insts().Get(arg_id).type_id();
  340. if (SemIR::ValueRepr::ForType(context.sem_ir(), arg_type_id).kind !=
  341. SemIR::ValueRepr::None) {
  342. args.push_back(context.GetValue(arg_id));
  343. }
  344. }
  345. context.SetLocal(inst_id, context.builder().CreateCall(callee, args));
  346. }
  347. } // namespace Carbon::Lower