handle_call.cpp 13 KB

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